Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * pci_syscall.c |
| 3 | * |
| 4 | * For architectures where we want to allow direct access |
| 5 | * to the PCI config stuff - it would probably be preferable |
| 6 | * on PCs too, but there people just do it by hand with the |
| 7 | * magic northbridge registers.. |
| 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/syscalls.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 14 | #include "pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 16 | SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn, |
| 17 | unsigned long, off, unsigned long, len, void __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { |
| 19 | struct pci_dev *dev; |
| 20 | u8 byte; |
| 21 | u16 word; |
| 22 | u32 dword; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 23 | long err; |
| 24 | long cfg_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | if (!capable(CAP_SYS_ADMIN)) |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 27 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | err = -ENODEV; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 30 | dev = pci_get_bus_and_slot(bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | if (!dev) |
| 32 | goto error; |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | switch (len) { |
| 35 | case 1: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 36 | cfg_ret = pci_user_read_config_byte(dev, off, &byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | break; |
| 38 | case 2: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 39 | cfg_ret = pci_user_read_config_word(dev, off, &word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | break; |
| 41 | case 4: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 42 | cfg_ret = pci_user_read_config_dword(dev, off, &dword); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | break; |
| 44 | default: |
| 45 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | goto error; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 47 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | err = -EIO; |
| 50 | if (cfg_ret != PCIBIOS_SUCCESSFUL) |
| 51 | goto error; |
| 52 | |
| 53 | switch (len) { |
| 54 | case 1: |
| 55 | err = put_user(byte, (unsigned char __user *)buf); |
| 56 | break; |
| 57 | case 2: |
| 58 | err = put_user(word, (unsigned short __user *)buf); |
| 59 | break; |
| 60 | case 4: |
| 61 | err = put_user(dword, (unsigned int __user *)buf); |
| 62 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 63 | } |
| 64 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return err; |
| 66 | |
| 67 | error: |
| 68 | /* ??? XFree86 doesn't even check the return value. They |
| 69 | just look for 0xffffffff in the output, since that's what |
| 70 | they get instead of a machine check on x86. */ |
| 71 | switch (len) { |
| 72 | case 1: |
| 73 | put_user(-1, (unsigned char __user *)buf); |
| 74 | break; |
| 75 | case 2: |
| 76 | put_user(-1, (unsigned short __user *)buf); |
| 77 | break; |
| 78 | case 4: |
| 79 | put_user(-1, (unsigned int __user *)buf); |
| 80 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 81 | } |
| 82 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return err; |
| 84 | } |
| 85 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 86 | SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn, |
| 87 | unsigned long, off, unsigned long, len, void __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | struct pci_dev *dev; |
| 90 | u8 byte; |
| 91 | u16 word; |
| 92 | u32 dword; |
| 93 | int err = 0; |
| 94 | |
| 95 | if (!capable(CAP_SYS_ADMIN)) |
| 96 | return -EPERM; |
| 97 | |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 98 | dev = pci_get_bus_and_slot(bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | if (!dev) |
| 100 | return -ENODEV; |
| 101 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 102 | switch (len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | case 1: |
| 104 | err = get_user(byte, (u8 __user *)buf); |
| 105 | if (err) |
| 106 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 107 | err = pci_user_write_config_byte(dev, off, byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | if (err != PCIBIOS_SUCCESSFUL) |
| 109 | err = -EIO; |
| 110 | break; |
| 111 | |
| 112 | case 2: |
| 113 | err = get_user(word, (u16 __user *)buf); |
| 114 | if (err) |
| 115 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 116 | err = pci_user_write_config_word(dev, off, word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | if (err != PCIBIOS_SUCCESSFUL) |
| 118 | err = -EIO; |
| 119 | break; |
| 120 | |
| 121 | case 4: |
| 122 | err = get_user(dword, (u32 __user *)buf); |
| 123 | if (err) |
| 124 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 125 | err = pci_user_write_config_dword(dev, off, dword); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if (err != PCIBIOS_SUCCESSFUL) |
| 127 | err = -EIO; |
| 128 | break; |
| 129 | |
| 130 | default: |
| 131 | err = -EINVAL; |
| 132 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 133 | } |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 134 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return err; |
| 136 | } |