proc.c 9.9 KB

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