proc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Procfs interface for the PCI bus.
  3. *
  4. * Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/pci.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/capability.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/byteorder.h>
  15. #include "pci.h"
  16. static int proc_initialized; /* = 0 */
  17. static loff_t
  18. proc_bus_pci_lseek(struct file *file, loff_t off, int whence)
  19. {
  20. loff_t new = -1;
  21. struct inode *inode = file->f_path.dentry->d_inode;
  22. mutex_lock(&inode->i_mutex);
  23. switch (whence) {
  24. case 0:
  25. new = off;
  26. break;
  27. case 1:
  28. new = file->f_pos + off;
  29. break;
  30. case 2:
  31. new = inode->i_size + off;
  32. break;
  33. }
  34. if (new < 0 || new > inode->i_size)
  35. new = -EINVAL;
  36. else
  37. file->f_pos = new;
  38. mutex_unlock(&inode->i_mutex);
  39. return new;
  40. }
  41. static ssize_t
  42. proc_bus_pci_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
  43. {
  44. const struct inode *ino = file->f_path.dentry->d_inode;
  45. const struct proc_dir_entry *dp = PDE(ino);
  46. struct pci_dev *dev = dp->data;
  47. unsigned int pos = *ppos;
  48. unsigned int cnt, size;
  49. /*
  50. * Normal users can read only the standardized portion of the
  51. * configuration space as several chips lock up when trying to read
  52. * undefined locations (think of Intel PIIX4 as a typical example).
  53. */
  54. if (capable(CAP_SYS_ADMIN))
  55. size = dp->size;
  56. else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  57. size = 128;
  58. else
  59. size = 64;
  60. if (pos >= size)
  61. return 0;
  62. if (nbytes >= size)
  63. nbytes = size;
  64. if (pos + nbytes > size)
  65. nbytes = size - pos;
  66. cnt = nbytes;
  67. if (!access_ok(VERIFY_WRITE, buf, cnt))
  68. return -EINVAL;
  69. if ((pos & 1) && cnt) {
  70. unsigned char val;
  71. pci_user_read_config_byte(dev, pos, &val);
  72. __put_user(val, buf);
  73. buf++;
  74. pos++;
  75. cnt--;
  76. }
  77. if ((pos & 3) && cnt > 2) {
  78. unsigned short val;
  79. pci_user_read_config_word(dev, pos, &val);
  80. __put_user(cpu_to_le16(val), (__le16 __user *) buf);
  81. buf += 2;
  82. pos += 2;
  83. cnt -= 2;
  84. }
  85. while (cnt >= 4) {
  86. unsigned int val;
  87. pci_user_read_config_dword(dev, pos, &val);
  88. __put_user(cpu_to_le32(val), (__le32 __user *) buf);
  89. buf += 4;
  90. pos += 4;
  91. cnt -= 4;
  92. }
  93. if (cnt >= 2) {
  94. unsigned short val;
  95. pci_user_read_config_word(dev, pos, &val);
  96. __put_user(cpu_to_le16(val), (__le16 __user *) buf);
  97. buf += 2;
  98. pos += 2;
  99. cnt -= 2;
  100. }
  101. if (cnt) {
  102. unsigned char val;
  103. pci_user_read_config_byte(dev, pos, &val);
  104. __put_user(val, buf);
  105. buf++;
  106. pos++;
  107. cnt--;
  108. }
  109. *ppos = pos;
  110. return nbytes;
  111. }
  112. static ssize_t
  113. proc_bus_pci_write(struct file *file, const char __user *buf, size_t nbytes, loff_t *ppos)
  114. {
  115. struct inode *ino = file->f_path.dentry->d_inode;
  116. const struct proc_dir_entry *dp = PDE(ino);
  117. struct pci_dev *dev = dp->data;
  118. int pos = *ppos;
  119. int size = dp->size;
  120. int cnt;
  121. if (pos >= size)
  122. return 0;
  123. if (nbytes >= size)
  124. nbytes = size;
  125. if (pos + nbytes > size)
  126. nbytes = size - pos;
  127. cnt = nbytes;
  128. if (!access_ok(VERIFY_READ, buf, cnt))
  129. return -EINVAL;
  130. if ((pos & 1) && cnt) {
  131. unsigned char val;
  132. __get_user(val, buf);
  133. pci_user_write_config_byte(dev, pos, val);
  134. buf++;
  135. pos++;
  136. cnt--;
  137. }
  138. if ((pos & 3) && cnt > 2) {
  139. __le16 val;
  140. __get_user(val, (__le16 __user *) buf);
  141. pci_user_write_config_word(dev, pos, le16_to_cpu(val));
  142. buf += 2;
  143. pos += 2;
  144. cnt -= 2;
  145. }
  146. while (cnt >= 4) {
  147. __le32 val;
  148. __get_user(val, (__le32 __user *) buf);
  149. pci_user_write_config_dword(dev, pos, le32_to_cpu(val));
  150. buf += 4;
  151. pos += 4;
  152. cnt -= 4;
  153. }
  154. if (cnt >= 2) {
  155. __le16 val;
  156. __get_user(val, (__le16 __user *) buf);
  157. pci_user_write_config_word(dev, pos, le16_to_cpu(val));
  158. buf += 2;
  159. pos += 2;
  160. cnt -= 2;
  161. }
  162. if (cnt) {
  163. unsigned char val;
  164. __get_user(val, buf);
  165. pci_user_write_config_byte(dev, pos, val);
  166. buf++;
  167. pos++;
  168. cnt--;
  169. }
  170. *ppos = pos;
  171. i_size_write(ino, dp->size);
  172. return nbytes;
  173. }
  174. struct pci_filp_private {
  175. enum pci_mmap_state mmap_state;
  176. int write_combine;
  177. };
  178. static long proc_bus_pci_ioctl(struct file *file, unsigned int cmd,
  179. unsigned long arg)
  180. {
  181. const struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
  182. struct pci_dev *dev = dp->data;
  183. #ifdef HAVE_PCI_MMAP
  184. struct pci_filp_private *fpriv = file->private_data;
  185. #endif /* HAVE_PCI_MMAP */
  186. int ret = 0;
  187. switch (cmd) {
  188. case PCIIOC_CONTROLLER:
  189. ret = pci_domain_nr(dev->bus);
  190. break;
  191. #ifdef HAVE_PCI_MMAP
  192. case PCIIOC_MMAP_IS_IO:
  193. fpriv->mmap_state = pci_mmap_io;
  194. break;
  195. case PCIIOC_MMAP_IS_MEM:
  196. fpriv->mmap_state = pci_mmap_mem;
  197. break;
  198. case PCIIOC_WRITE_COMBINE:
  199. if (arg)
  200. fpriv->write_combine = 1;
  201. else
  202. fpriv->write_combine = 0;
  203. break;
  204. #endif /* HAVE_PCI_MMAP */
  205. default:
  206. ret = -EINVAL;
  207. break;
  208. };
  209. return ret;
  210. }
  211. #ifdef HAVE_PCI_MMAP
  212. static int proc_bus_pci_mmap(struct file *file, struct vm_area_struct *vma)
  213. {
  214. struct inode *inode = file->f_path.dentry->d_inode;
  215. const struct proc_dir_entry *dp = PDE(inode);
  216. struct pci_dev *dev = dp->data;
  217. struct pci_filp_private *fpriv = file->private_data;
  218. int i, ret;
  219. if (!capable(CAP_SYS_RAWIO))
  220. return -EPERM;
  221. /* Make sure the caller is mapping a real resource for this device */
  222. for (i = 0; i < PCI_ROM_RESOURCE; i++) {
  223. if (pci_mmap_fits(dev, i, vma, PCI_MMAP_PROCFS))
  224. break;
  225. }
  226. if (i >= PCI_ROM_RESOURCE)
  227. return -ENODEV;
  228. ret = pci_mmap_page_range(dev, vma,
  229. fpriv->mmap_state,
  230. fpriv->write_combine);
  231. if (ret < 0)
  232. return ret;
  233. return 0;
  234. }
  235. static int proc_bus_pci_open(struct inode *inode, struct file *file)
  236. {
  237. struct pci_filp_private *fpriv = kmalloc(sizeof(*fpriv), GFP_KERNEL);
  238. if (!fpriv)
  239. return -ENOMEM;
  240. fpriv->mmap_state = pci_mmap_io;
  241. fpriv->write_combine = 0;
  242. file->private_data = fpriv;
  243. return 0;
  244. }
  245. static int proc_bus_pci_release(struct inode *inode, struct file *file)
  246. {
  247. kfree(file->private_data);
  248. file->private_data = NULL;
  249. return 0;
  250. }
  251. #endif /* HAVE_PCI_MMAP */
  252. static const struct file_operations proc_bus_pci_operations = {
  253. .owner = THIS_MODULE,
  254. .llseek = proc_bus_pci_lseek,
  255. .read = proc_bus_pci_read,
  256. .write = proc_bus_pci_write,
  257. .unlocked_ioctl = proc_bus_pci_ioctl,
  258. .compat_ioctl = proc_bus_pci_ioctl,
  259. #ifdef HAVE_PCI_MMAP
  260. .open = proc_bus_pci_open,
  261. .release = proc_bus_pci_release,
  262. .mmap = proc_bus_pci_mmap,
  263. #ifdef HAVE_ARCH_PCI_GET_UNMAPPED_AREA
  264. .get_unmapped_area = get_pci_unmapped_area,
  265. #endif /* HAVE_ARCH_PCI_GET_UNMAPPED_AREA */
  266. #endif /* HAVE_PCI_MMAP */
  267. };
  268. /* iterator */
  269. static void *pci_seq_start(struct seq_file *m, loff_t *pos)
  270. {
  271. struct pci_dev *dev = NULL;
  272. loff_t n = *pos;
  273. for_each_pci_dev(dev) {
  274. if (!n--)
  275. break;
  276. }
  277. return dev;
  278. }
  279. static void *pci_seq_next(struct seq_file *m, void *v, loff_t *pos)
  280. {
  281. struct pci_dev *dev = v;
  282. (*pos)++;
  283. dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
  284. return dev;
  285. }
  286. static void pci_seq_stop(struct seq_file *m, void *v)
  287. {
  288. if (v) {
  289. struct pci_dev *dev = v;
  290. pci_dev_put(dev);
  291. }
  292. }
  293. static int show_device(struct seq_file *m, void *v)
  294. {
  295. const struct pci_dev *dev = v;
  296. const struct pci_driver *drv;
  297. int i;
  298. if (dev == NULL)
  299. return 0;
  300. drv = pci_dev_driver(dev);
  301. seq_printf(m, "%02x%02x\t%04x%04x\t%x",
  302. dev->bus->number,
  303. dev->devfn,
  304. dev->vendor,
  305. dev->device,
  306. dev->irq);
  307. /* only print standard and ROM resources to preserve compatibility */
  308. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  309. resource_size_t start, end;
  310. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  311. seq_printf(m, "\t%16llx",
  312. (unsigned long long)(start |
  313. (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
  314. }
  315. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  316. resource_size_t start, end;
  317. pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
  318. seq_printf(m, "\t%16llx",
  319. dev->resource[i].start < dev->resource[i].end ?
  320. (unsigned long long)(end - start) + 1 : 0);
  321. }
  322. seq_putc(m, '\t');
  323. if (drv)
  324. seq_printf(m, "%s", drv->name);
  325. seq_putc(m, '\n');
  326. return 0;
  327. }
  328. static const struct seq_operations proc_bus_pci_devices_op = {
  329. .start = pci_seq_start,
  330. .next = pci_seq_next,
  331. .stop = pci_seq_stop,
  332. .show = show_device
  333. };
  334. static struct proc_dir_entry *proc_bus_pci_dir;
  335. int pci_proc_attach_device(struct pci_dev *dev)
  336. {
  337. struct pci_bus *bus = dev->bus;
  338. struct proc_dir_entry *e;
  339. char name[16];
  340. if (!proc_initialized)
  341. return -EACCES;
  342. if (!bus->procdir) {
  343. if (pci_proc_domain(bus)) {
  344. sprintf(name, "%04x:%02x", pci_domain_nr(bus),
  345. bus->number);
  346. } else {
  347. sprintf(name, "%02x", bus->number);
  348. }
  349. bus->procdir = proc_mkdir(name, proc_bus_pci_dir);
  350. if (!bus->procdir)
  351. return -ENOMEM;
  352. }
  353. sprintf(name, "%02x.%x", PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  354. e = proc_create_data(name, S_IFREG | S_IRUGO | S_IWUSR, bus->procdir,
  355. &proc_bus_pci_operations, dev);
  356. if (!e)
  357. return -ENOMEM;
  358. e->size = dev->cfg_size;
  359. dev->procent = e;
  360. return 0;
  361. }
  362. int pci_proc_detach_device(struct pci_dev *dev)
  363. {
  364. struct proc_dir_entry *e;
  365. if ((e = dev->procent)) {
  366. remove_proc_entry(e->name, dev->bus->procdir);
  367. dev->procent = NULL;
  368. }
  369. return 0;
  370. }
  371. #if 0
  372. int pci_proc_attach_bus(struct pci_bus* bus)
  373. {
  374. struct proc_dir_entry *de = bus->procdir;
  375. if (!proc_initialized)
  376. return -EACCES;
  377. if (!de) {
  378. char name[16];
  379. sprintf(name, "%02x", bus->number);
  380. de = bus->procdir = proc_mkdir(name, proc_bus_pci_dir);
  381. if (!de)
  382. return -ENOMEM;
  383. }
  384. return 0;
  385. }
  386. #endif /* 0 */
  387. int pci_proc_detach_bus(struct pci_bus* bus)
  388. {
  389. struct proc_dir_entry *de = bus->procdir;
  390. if (de)
  391. remove_proc_entry(de->name, proc_bus_pci_dir);
  392. return 0;
  393. }
  394. static int proc_bus_pci_dev_open(struct inode *inode, struct file *file)
  395. {
  396. return seq_open(file, &proc_bus_pci_devices_op);
  397. }
  398. static const struct file_operations proc_bus_pci_dev_operations = {
  399. .owner = THIS_MODULE,
  400. .open = proc_bus_pci_dev_open,
  401. .read = seq_read,
  402. .llseek = seq_lseek,
  403. .release = seq_release,
  404. };
  405. static int __init pci_proc_init(void)
  406. {
  407. struct pci_dev *dev = NULL;
  408. proc_bus_pci_dir = proc_mkdir("bus/pci", NULL);
  409. proc_create("devices", 0, proc_bus_pci_dir,
  410. &proc_bus_pci_dev_operations);
  411. proc_initialized = 1;
  412. for_each_pci_dev(dev)
  413. pci_proc_attach_device(dev);
  414. return 0;
  415. }
  416. device_initcall(pci_proc_init);