blob: f602176eb8b04169924ded820d911c22628488d0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/syscalls.h>
13#include <asm/uaccess.h>
Brian Kinge04b0ea2005-09-27 01:21:55 -070014#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010016SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn,
17 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
19 struct pci_dev *dev;
20 u8 byte;
21 u16 word;
22 u32 dword;
Alan Coxe4585da2007-04-23 14:57:37 +010023 long err;
Heiner Kallweit5a442c52021-01-24 16:39:32 +010024 int cfg_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Krzysztof Wilczyński69d00a72021-07-29 23:37:54 +000026 err = -EPERM;
27 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 if (!capable(CAP_SYS_ADMIN))
Krzysztof Wilczyński69d00a72021-07-29 23:37:54 +000029 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31 err = -ENODEV;
Alan Coxe4585da2007-04-23 14:57:37 +010032 dev = pci_get_bus_and_slot(bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (!dev)
34 goto error;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 switch (len) {
37 case 1:
Brian Kinge04b0ea2005-09-27 01:21:55 -070038 cfg_ret = pci_user_read_config_byte(dev, off, &byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 break;
40 case 2:
Brian Kinge04b0ea2005-09-27 01:21:55 -070041 cfg_ret = pci_user_read_config_word(dev, off, &word);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 break;
43 case 4:
Brian Kinge04b0ea2005-09-27 01:21:55 -070044 cfg_ret = pci_user_read_config_dword(dev, off, &dword);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 break;
46 default:
47 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 goto error;
Bjorn Helgaasf7625982013-11-14 11:28:18 -070049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 err = -EIO;
Heiner Kallweit5a442c52021-01-24 16:39:32 +010052 if (cfg_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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 Coxe4585da2007-04-23 14:57:37 +010065 }
66 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return err;
68
69error:
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 Coxe4585da2007-04-23 14:57:37 +010083 }
84 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return err;
86}
87
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010088SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
89 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
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 Coxe4585da2007-04-23 14:57:37 +0100100 dev = pci_get_bus_and_slot(bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!dev)
102 return -ENODEV;
103
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400104 switch (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 case 1:
106 err = get_user(byte, (u8 __user *)buf);
107 if (err)
108 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700109 err = pci_user_write_config_byte(dev, off, byte);
Heiner Kallweit5a442c52021-01-24 16:39:32 +0100110 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 err = -EIO;
112 break;
113
114 case 2:
115 err = get_user(word, (u16 __user *)buf);
116 if (err)
117 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700118 err = pci_user_write_config_word(dev, off, word);
Heiner Kallweit5a442c52021-01-24 16:39:32 +0100119 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 err = -EIO;
121 break;
122
123 case 4:
124 err = get_user(dword, (u32 __user *)buf);
125 if (err)
126 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700127 err = pci_user_write_config_dword(dev, off, dword);
Heiner Kallweit5a442c52021-01-24 16:39:32 +0100128 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 err = -EIO;
130 break;
131
132 default:
133 err = -EINVAL;
134 break;
Alan Coxe4585da2007-04-23 14:57:37 +0100135 }
Alan Coxe4585da2007-04-23 14:57:37 +0100136 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return err;
138}