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