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> |
| 13 | #include <asm/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; |
Heiner Kallweit | 5a442c5 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 24 | int cfg_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Krzysztof Wilczyński | 69d00a7 | 2021-07-29 23:37:54 +0000 | [diff] [blame] | 26 | err = -EPERM; |
| 27 | dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | if (!capable(CAP_SYS_ADMIN)) |
Krzysztof Wilczyński | 69d00a7 | 2021-07-29 23:37:54 +0000 | [diff] [blame] | 29 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | err = -ENODEV; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 32 | dev = pci_get_bus_and_slot(bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | if (!dev) |
| 34 | goto error; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | switch (len) { |
| 37 | case 1: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 38 | cfg_ret = pci_user_read_config_byte(dev, off, &byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | break; |
| 40 | case 2: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 41 | cfg_ret = pci_user_read_config_word(dev, off, &word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | break; |
| 43 | case 4: |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 44 | cfg_ret = pci_user_read_config_dword(dev, off, &dword); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | break; |
| 46 | default: |
| 47 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | goto error; |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 49 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | err = -EIO; |
Heiner Kallweit | 5a442c5 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 52 | if (cfg_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | goto error; |
| 54 | |
| 55 | switch (len) { |
| 56 | case 1: |
| 57 | err = put_user(byte, (unsigned char __user *)buf); |
| 58 | break; |
| 59 | case 2: |
| 60 | err = put_user(word, (unsigned short __user *)buf); |
| 61 | break; |
| 62 | case 4: |
| 63 | err = put_user(dword, (unsigned int __user *)buf); |
| 64 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 65 | } |
| 66 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return err; |
| 68 | |
| 69 | error: |
| 70 | /* ??? XFree86 doesn't even check the return value. They |
| 71 | just look for 0xffffffff in the output, since that's what |
| 72 | they get instead of a machine check on x86. */ |
| 73 | switch (len) { |
| 74 | case 1: |
| 75 | put_user(-1, (unsigned char __user *)buf); |
| 76 | break; |
| 77 | case 2: |
| 78 | put_user(-1, (unsigned short __user *)buf); |
| 79 | break; |
| 80 | case 4: |
| 81 | put_user(-1, (unsigned int __user *)buf); |
| 82 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 83 | } |
| 84 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return err; |
| 86 | } |
| 87 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 88 | SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn, |
| 89 | unsigned long, off, unsigned long, len, void __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | struct pci_dev *dev; |
| 92 | u8 byte; |
| 93 | u16 word; |
| 94 | u32 dword; |
| 95 | int err = 0; |
| 96 | |
| 97 | if (!capable(CAP_SYS_ADMIN)) |
| 98 | return -EPERM; |
| 99 | |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 100 | dev = pci_get_bus_and_slot(bus, dfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (!dev) |
| 102 | return -ENODEV; |
| 103 | |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 104 | switch (len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | case 1: |
| 106 | err = get_user(byte, (u8 __user *)buf); |
| 107 | if (err) |
| 108 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 109 | err = pci_user_write_config_byte(dev, off, byte); |
Heiner Kallweit | 5a442c5 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 110 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | err = -EIO; |
| 112 | break; |
| 113 | |
| 114 | case 2: |
| 115 | err = get_user(word, (u16 __user *)buf); |
| 116 | if (err) |
| 117 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 118 | err = pci_user_write_config_word(dev, off, word); |
Heiner Kallweit | 5a442c5 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 119 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | err = -EIO; |
| 121 | break; |
| 122 | |
| 123 | case 4: |
| 124 | err = get_user(dword, (u32 __user *)buf); |
| 125 | if (err) |
| 126 | break; |
Brian King | e04b0ea | 2005-09-27 01:21:55 -0700 | [diff] [blame] | 127 | err = pci_user_write_config_dword(dev, off, dword); |
Heiner Kallweit | 5a442c5 | 2021-01-24 16:39:32 +0100 | [diff] [blame] | 128 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | err = -EIO; |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | err = -EINVAL; |
| 134 | break; |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 135 | } |
Alan Cox | e4585da | 2007-04-23 14:57:37 +0100 | [diff] [blame] | 136 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return err; |
| 138 | } |