blob: ec22284eed307ccf565eb83df55f960876f57cd4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070010#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 Kinge04b0ea2005-09-27 01:21:55 -070015#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010017SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn,
18 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
20 struct pci_dev *dev;
21 u8 byte;
22 u16 word;
23 u32 dword;
Alan Coxe4585da2007-04-23 14:57:37 +010024 long err;
25 long cfg_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 if (!capable(CAP_SYS_ADMIN))
Alan Coxe4585da2007-04-23 14:57:37 +010028 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 err = -ENODEV;
Alan Coxe4585da2007-04-23 14:57:37 +010031 dev = pci_get_bus_and_slot(bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 if (!dev)
33 goto error;
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 switch (len) {
36 case 1:
Brian Kinge04b0ea2005-09-27 01:21:55 -070037 cfg_ret = pci_user_read_config_byte(dev, off, &byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 break;
39 case 2:
Brian Kinge04b0ea2005-09-27 01:21:55 -070040 cfg_ret = pci_user_read_config_word(dev, off, &word);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 break;
42 case 4:
Brian Kinge04b0ea2005-09-27 01:21:55 -070043 cfg_ret = pci_user_read_config_dword(dev, off, &dword);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 break;
45 default:
46 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 goto error;
48 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
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 Coxe4585da2007-04-23 14:57:37 +010064 }
65 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return err;
67
68error:
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 Coxe4585da2007-04-23 14:57:37 +010082 }
83 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return err;
85}
86
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010087SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
88 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
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 Coxe4585da2007-04-23 14:57:37 +010099 dev = pci_get_bus_and_slot(bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (!dev)
101 return -ENODEV;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 switch(len) {
104 case 1:
105 err = get_user(byte, (u8 __user *)buf);
106 if (err)
107 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700108 err = pci_user_write_config_byte(dev, off, byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 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 Kinge04b0ea2005-09-27 01:21:55 -0700117 err = pci_user_write_config_word(dev, off, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 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 Kinge04b0ea2005-09-27 01:21:55 -0700126 err = pci_user_write_config_dword(dev, off, dword);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (err != PCIBIOS_SUCCESSFUL)
128 err = -EIO;
129 break;
130
131 default:
132 err = -EINVAL;
133 break;
Alan Coxe4585da2007-04-23 14:57:37 +0100134 }
Alan Coxe4585da2007-04-23 14:57:37 +0100135 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return err;
137}