uio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * drivers/uio/uio.c
  3. *
  4. * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
  5. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  6. * Copyright(C) 2006, Hans J. Koch <hjk@linutronix.de>
  7. * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
  8. *
  9. * Userspace IO
  10. *
  11. * Base Functions
  12. *
  13. * Licensed under the GPLv2 only.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/poll.h>
  18. #include <linux/device.h>
  19. #include <linux/mm.h>
  20. #include <linux/idr.h>
  21. #include <linux/string.h>
  22. #include <linux/kobject.h>
  23. #include <linux/uio_driver.h>
  24. #define UIO_MAX_DEVICES 255
  25. struct uio_device {
  26. struct module *owner;
  27. struct device *dev;
  28. int minor;
  29. atomic_t event;
  30. struct fasync_struct *async_queue;
  31. wait_queue_head_t wait;
  32. int vma_count;
  33. struct uio_info *info;
  34. struct kobject *map_dir;
  35. };
  36. static int uio_major;
  37. static DEFINE_IDR(uio_idr);
  38. static const struct file_operations uio_fops;
  39. /* UIO class infrastructure */
  40. static struct uio_class {
  41. struct kref kref;
  42. struct class *class;
  43. } *uio_class;
  44. /*
  45. * attributes
  46. */
  47. struct uio_map {
  48. struct kobject kobj;
  49. struct uio_mem *mem;
  50. };
  51. #define to_map(map) container_of(map, struct uio_map, kobj)
  52. static ssize_t map_attr_show(struct kobject *kobj, struct kobj_attribute *attr,
  53. char *buf)
  54. {
  55. struct uio_map *map = to_map(kobj);
  56. struct uio_mem *mem = map->mem;
  57. if (strncmp(attr->attr.name, "addr", 4) == 0)
  58. return sprintf(buf, "0x%lx\n", mem->addr);
  59. if (strncmp(attr->attr.name, "size", 4) == 0)
  60. return sprintf(buf, "0x%lx\n", mem->size);
  61. return -ENODEV;
  62. }
  63. static struct kobj_attribute attr_attribute =
  64. __ATTR(addr, S_IRUGO, map_attr_show, NULL);
  65. static struct kobj_attribute size_attribute =
  66. __ATTR(size, S_IRUGO, map_attr_show, NULL);
  67. static struct attribute *attrs[] = {
  68. &attr_attribute.attr,
  69. &size_attribute.attr,
  70. NULL, /* need to NULL terminate the list of attributes */
  71. };
  72. static void map_release(struct kobject *kobj)
  73. {
  74. struct uio_map *map = to_map(kobj);
  75. kfree(map);
  76. }
  77. static struct kobj_type map_attr_type = {
  78. .release = map_release,
  79. .default_attrs = attrs,
  80. };
  81. static ssize_t show_name(struct device *dev,
  82. struct device_attribute *attr, char *buf)
  83. {
  84. struct uio_device *idev = dev_get_drvdata(dev);
  85. if (idev)
  86. return sprintf(buf, "%s\n", idev->info->name);
  87. else
  88. return -ENODEV;
  89. }
  90. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  91. static ssize_t show_version(struct device *dev,
  92. struct device_attribute *attr, char *buf)
  93. {
  94. struct uio_device *idev = dev_get_drvdata(dev);
  95. if (idev)
  96. return sprintf(buf, "%s\n", idev->info->version);
  97. else
  98. return -ENODEV;
  99. }
  100. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  101. static ssize_t show_event(struct device *dev,
  102. struct device_attribute *attr, char *buf)
  103. {
  104. struct uio_device *idev = dev_get_drvdata(dev);
  105. if (idev)
  106. return sprintf(buf, "%u\n",
  107. (unsigned int)atomic_read(&idev->event));
  108. else
  109. return -ENODEV;
  110. }
  111. static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
  112. static struct attribute *uio_attrs[] = {
  113. &dev_attr_name.attr,
  114. &dev_attr_version.attr,
  115. &dev_attr_event.attr,
  116. NULL,
  117. };
  118. static struct attribute_group uio_attr_grp = {
  119. .attrs = uio_attrs,
  120. };
  121. /*
  122. * device functions
  123. */
  124. static int uio_dev_add_attributes(struct uio_device *idev)
  125. {
  126. int ret;
  127. int mi;
  128. int map_found = 0;
  129. struct uio_mem *mem;
  130. struct uio_map *map;
  131. ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
  132. if (ret)
  133. goto err_group;
  134. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  135. mem = &idev->info->mem[mi];
  136. if (mem->size == 0)
  137. break;
  138. if (!map_found) {
  139. map_found = 1;
  140. idev->map_dir = kobject_create_and_add("maps",
  141. &idev->dev->kobj);
  142. if (!idev->map_dir)
  143. goto err;
  144. }
  145. map = kzalloc(sizeof(*map), GFP_KERNEL);
  146. if (!map)
  147. goto err;
  148. kobject_init(&map->kobj, &map_attr_type);
  149. map->mem = mem;
  150. mem->map = map;
  151. ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
  152. if (ret)
  153. goto err;
  154. ret = kobject_uevent(&map->kobj, KOBJ_ADD);
  155. if (ret)
  156. goto err;
  157. }
  158. return 0;
  159. err:
  160. for (mi--; mi>=0; mi--) {
  161. mem = &idev->info->mem[mi];
  162. map = mem->map;
  163. kobject_put(&map->kobj);
  164. }
  165. kobject_put(idev->map_dir);
  166. sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
  167. err_group:
  168. dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
  169. return ret;
  170. }
  171. static void uio_dev_del_attributes(struct uio_device *idev)
  172. {
  173. int mi;
  174. struct uio_mem *mem;
  175. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  176. mem = &idev->info->mem[mi];
  177. if (mem->size == 0)
  178. break;
  179. kobject_put(&mem->map->kobj);
  180. }
  181. kobject_put(idev->map_dir);
  182. sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
  183. }
  184. static int uio_get_minor(struct uio_device *idev)
  185. {
  186. static DEFINE_MUTEX(minor_lock);
  187. int retval = -ENOMEM;
  188. int id;
  189. mutex_lock(&minor_lock);
  190. if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
  191. goto exit;
  192. retval = idr_get_new(&uio_idr, idev, &id);
  193. if (retval < 0) {
  194. if (retval == -EAGAIN)
  195. retval = -ENOMEM;
  196. goto exit;
  197. }
  198. idev->minor = id & MAX_ID_MASK;
  199. exit:
  200. mutex_unlock(&minor_lock);
  201. return retval;
  202. }
  203. static void uio_free_minor(struct uio_device *idev)
  204. {
  205. idr_remove(&uio_idr, idev->minor);
  206. }
  207. /**
  208. * uio_event_notify - trigger an interrupt event
  209. * @info: UIO device capabilities
  210. */
  211. void uio_event_notify(struct uio_info *info)
  212. {
  213. struct uio_device *idev = info->uio_dev;
  214. atomic_inc(&idev->event);
  215. wake_up_interruptible(&idev->wait);
  216. kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
  217. }
  218. EXPORT_SYMBOL_GPL(uio_event_notify);
  219. /**
  220. * uio_interrupt - hardware interrupt handler
  221. * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
  222. * @dev_id: Pointer to the devices uio_device structure
  223. */
  224. static irqreturn_t uio_interrupt(int irq, void *dev_id)
  225. {
  226. struct uio_device *idev = (struct uio_device *)dev_id;
  227. irqreturn_t ret = idev->info->handler(irq, idev->info);
  228. if (ret == IRQ_HANDLED)
  229. uio_event_notify(idev->info);
  230. return ret;
  231. }
  232. struct uio_listener {
  233. struct uio_device *dev;
  234. s32 event_count;
  235. };
  236. static int uio_open(struct inode *inode, struct file *filep)
  237. {
  238. struct uio_device *idev;
  239. struct uio_listener *listener;
  240. int ret = 0;
  241. idev = idr_find(&uio_idr, iminor(inode));
  242. if (!idev)
  243. return -ENODEV;
  244. listener = kmalloc(sizeof(*listener), GFP_KERNEL);
  245. if (!listener)
  246. return -ENOMEM;
  247. listener->dev = idev;
  248. listener->event_count = atomic_read(&idev->event);
  249. filep->private_data = listener;
  250. if (idev->info->open) {
  251. if (!try_module_get(idev->owner))
  252. return -ENODEV;
  253. ret = idev->info->open(idev->info, inode);
  254. module_put(idev->owner);
  255. }
  256. if (ret)
  257. kfree(listener);
  258. return ret;
  259. }
  260. static int uio_fasync(int fd, struct file *filep, int on)
  261. {
  262. struct uio_listener *listener = filep->private_data;
  263. struct uio_device *idev = listener->dev;
  264. return fasync_helper(fd, filep, on, &idev->async_queue);
  265. }
  266. static int uio_release(struct inode *inode, struct file *filep)
  267. {
  268. int ret = 0;
  269. struct uio_listener *listener = filep->private_data;
  270. struct uio_device *idev = listener->dev;
  271. if (idev->info->release) {
  272. if (!try_module_get(idev->owner))
  273. return -ENODEV;
  274. ret = idev->info->release(idev->info, inode);
  275. module_put(idev->owner);
  276. }
  277. if (filep->f_flags & FASYNC)
  278. ret = uio_fasync(-1, filep, 0);
  279. kfree(listener);
  280. return ret;
  281. }
  282. static unsigned int uio_poll(struct file *filep, poll_table *wait)
  283. {
  284. struct uio_listener *listener = filep->private_data;
  285. struct uio_device *idev = listener->dev;
  286. if (idev->info->irq == UIO_IRQ_NONE)
  287. return -EIO;
  288. poll_wait(filep, &idev->wait, wait);
  289. if (listener->event_count != atomic_read(&idev->event))
  290. return POLLIN | POLLRDNORM;
  291. return 0;
  292. }
  293. static ssize_t uio_read(struct file *filep, char __user *buf,
  294. size_t count, loff_t *ppos)
  295. {
  296. struct uio_listener *listener = filep->private_data;
  297. struct uio_device *idev = listener->dev;
  298. DECLARE_WAITQUEUE(wait, current);
  299. ssize_t retval;
  300. s32 event_count;
  301. if (idev->info->irq == UIO_IRQ_NONE)
  302. return -EIO;
  303. if (count != sizeof(s32))
  304. return -EINVAL;
  305. add_wait_queue(&idev->wait, &wait);
  306. do {
  307. set_current_state(TASK_INTERRUPTIBLE);
  308. event_count = atomic_read(&idev->event);
  309. if (event_count != listener->event_count) {
  310. if (copy_to_user(buf, &event_count, count))
  311. retval = -EFAULT;
  312. else {
  313. listener->event_count = event_count;
  314. retval = count;
  315. }
  316. break;
  317. }
  318. if (filep->f_flags & O_NONBLOCK) {
  319. retval = -EAGAIN;
  320. break;
  321. }
  322. if (signal_pending(current)) {
  323. retval = -ERESTARTSYS;
  324. break;
  325. }
  326. schedule();
  327. } while (1);
  328. __set_current_state(TASK_RUNNING);
  329. remove_wait_queue(&idev->wait, &wait);
  330. return retval;
  331. }
  332. static int uio_find_mem_index(struct vm_area_struct *vma)
  333. {
  334. int mi;
  335. struct uio_device *idev = vma->vm_private_data;
  336. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  337. if (idev->info->mem[mi].size == 0)
  338. return -1;
  339. if (vma->vm_pgoff == mi)
  340. return mi;
  341. }
  342. return -1;
  343. }
  344. static void uio_vma_open(struct vm_area_struct *vma)
  345. {
  346. struct uio_device *idev = vma->vm_private_data;
  347. idev->vma_count++;
  348. }
  349. static void uio_vma_close(struct vm_area_struct *vma)
  350. {
  351. struct uio_device *idev = vma->vm_private_data;
  352. idev->vma_count--;
  353. }
  354. static struct page *uio_vma_nopage(struct vm_area_struct *vma,
  355. unsigned long address, int *type)
  356. {
  357. struct uio_device *idev = vma->vm_private_data;
  358. struct page* page = NOPAGE_SIGBUS;
  359. int mi = uio_find_mem_index(vma);
  360. if (mi < 0)
  361. return page;
  362. if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
  363. page = virt_to_page(idev->info->mem[mi].addr);
  364. else
  365. page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
  366. get_page(page);
  367. if (type)
  368. *type = VM_FAULT_MINOR;
  369. return page;
  370. }
  371. static struct vm_operations_struct uio_vm_ops = {
  372. .open = uio_vma_open,
  373. .close = uio_vma_close,
  374. .nopage = uio_vma_nopage,
  375. };
  376. static int uio_mmap_physical(struct vm_area_struct *vma)
  377. {
  378. struct uio_device *idev = vma->vm_private_data;
  379. int mi = uio_find_mem_index(vma);
  380. if (mi < 0)
  381. return -EINVAL;
  382. vma->vm_flags |= VM_IO | VM_RESERVED;
  383. return remap_pfn_range(vma,
  384. vma->vm_start,
  385. idev->info->mem[mi].addr >> PAGE_SHIFT,
  386. vma->vm_end - vma->vm_start,
  387. vma->vm_page_prot);
  388. }
  389. static int uio_mmap_logical(struct vm_area_struct *vma)
  390. {
  391. vma->vm_flags |= VM_RESERVED;
  392. vma->vm_ops = &uio_vm_ops;
  393. uio_vma_open(vma);
  394. return 0;
  395. }
  396. static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
  397. {
  398. struct uio_listener *listener = filep->private_data;
  399. struct uio_device *idev = listener->dev;
  400. int mi;
  401. unsigned long requested_pages, actual_pages;
  402. int ret = 0;
  403. if (vma->vm_end < vma->vm_start)
  404. return -EINVAL;
  405. vma->vm_private_data = idev;
  406. mi = uio_find_mem_index(vma);
  407. if (mi < 0)
  408. return -EINVAL;
  409. requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  410. actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
  411. if (requested_pages > actual_pages)
  412. return -EINVAL;
  413. if (idev->info->mmap) {
  414. if (!try_module_get(idev->owner))
  415. return -ENODEV;
  416. ret = idev->info->mmap(idev->info, vma);
  417. module_put(idev->owner);
  418. return ret;
  419. }
  420. switch (idev->info->mem[mi].memtype) {
  421. case UIO_MEM_PHYS:
  422. return uio_mmap_physical(vma);
  423. case UIO_MEM_LOGICAL:
  424. case UIO_MEM_VIRTUAL:
  425. return uio_mmap_logical(vma);
  426. default:
  427. return -EINVAL;
  428. }
  429. }
  430. static const struct file_operations uio_fops = {
  431. .owner = THIS_MODULE,
  432. .open = uio_open,
  433. .release = uio_release,
  434. .read = uio_read,
  435. .mmap = uio_mmap,
  436. .poll = uio_poll,
  437. .fasync = uio_fasync,
  438. };
  439. static int uio_major_init(void)
  440. {
  441. uio_major = register_chrdev(0, "uio", &uio_fops);
  442. if (uio_major < 0)
  443. return uio_major;
  444. return 0;
  445. }
  446. static void uio_major_cleanup(void)
  447. {
  448. unregister_chrdev(uio_major, "uio");
  449. }
  450. static int init_uio_class(void)
  451. {
  452. int ret = 0;
  453. if (uio_class != NULL) {
  454. kref_get(&uio_class->kref);
  455. goto exit;
  456. }
  457. /* This is the first time in here, set everything up properly */
  458. ret = uio_major_init();
  459. if (ret)
  460. goto exit;
  461. uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
  462. if (!uio_class) {
  463. ret = -ENOMEM;
  464. goto err_kzalloc;
  465. }
  466. kref_init(&uio_class->kref);
  467. uio_class->class = class_create(THIS_MODULE, "uio");
  468. if (IS_ERR(uio_class->class)) {
  469. ret = IS_ERR(uio_class->class);
  470. printk(KERN_ERR "class_create failed for uio\n");
  471. goto err_class_create;
  472. }
  473. return 0;
  474. err_class_create:
  475. kfree(uio_class);
  476. uio_class = NULL;
  477. err_kzalloc:
  478. uio_major_cleanup();
  479. exit:
  480. return ret;
  481. }
  482. static void release_uio_class(struct kref *kref)
  483. {
  484. /* Ok, we cheat as we know we only have one uio_class */
  485. class_destroy(uio_class->class);
  486. kfree(uio_class);
  487. uio_major_cleanup();
  488. uio_class = NULL;
  489. }
  490. static void uio_class_destroy(void)
  491. {
  492. if (uio_class)
  493. kref_put(&uio_class->kref, release_uio_class);
  494. }
  495. /**
  496. * uio_register_device - register a new userspace IO device
  497. * @owner: module that creates the new device
  498. * @parent: parent device
  499. * @info: UIO device capabilities
  500. *
  501. * returns zero on success or a negative error code.
  502. */
  503. int __uio_register_device(struct module *owner,
  504. struct device *parent,
  505. struct uio_info *info)
  506. {
  507. struct uio_device *idev;
  508. int ret = 0;
  509. if (!parent || !info || !info->name || !info->version)
  510. return -EINVAL;
  511. info->uio_dev = NULL;
  512. ret = init_uio_class();
  513. if (ret)
  514. return ret;
  515. idev = kzalloc(sizeof(*idev), GFP_KERNEL);
  516. if (!idev) {
  517. ret = -ENOMEM;
  518. goto err_kzalloc;
  519. }
  520. idev->owner = owner;
  521. idev->info = info;
  522. init_waitqueue_head(&idev->wait);
  523. atomic_set(&idev->event, 0);
  524. ret = uio_get_minor(idev);
  525. if (ret)
  526. goto err_get_minor;
  527. idev->dev = device_create(uio_class->class, parent,
  528. MKDEV(uio_major, idev->minor),
  529. "uio%d", idev->minor);
  530. if (IS_ERR(idev->dev)) {
  531. printk(KERN_ERR "UIO: device register failed\n");
  532. ret = PTR_ERR(idev->dev);
  533. goto err_device_create;
  534. }
  535. dev_set_drvdata(idev->dev, idev);
  536. ret = uio_dev_add_attributes(idev);
  537. if (ret)
  538. goto err_uio_dev_add_attributes;
  539. info->uio_dev = idev;
  540. if (idev->info->irq >= 0) {
  541. ret = request_irq(idev->info->irq, uio_interrupt,
  542. idev->info->irq_flags, idev->info->name, idev);
  543. if (ret)
  544. goto err_request_irq;
  545. }
  546. return 0;
  547. err_request_irq:
  548. uio_dev_del_attributes(idev);
  549. err_uio_dev_add_attributes:
  550. device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
  551. err_device_create:
  552. uio_free_minor(idev);
  553. err_get_minor:
  554. kfree(idev);
  555. err_kzalloc:
  556. uio_class_destroy();
  557. return ret;
  558. }
  559. EXPORT_SYMBOL_GPL(__uio_register_device);
  560. /**
  561. * uio_unregister_device - unregister a industrial IO device
  562. * @info: UIO device capabilities
  563. *
  564. */
  565. void uio_unregister_device(struct uio_info *info)
  566. {
  567. struct uio_device *idev;
  568. if (!info || !info->uio_dev)
  569. return;
  570. idev = info->uio_dev;
  571. uio_free_minor(idev);
  572. if (info->irq >= 0)
  573. free_irq(info->irq, idev);
  574. uio_dev_del_attributes(idev);
  575. dev_set_drvdata(idev->dev, NULL);
  576. device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
  577. kfree(idev);
  578. uio_class_destroy();
  579. return;
  580. }
  581. EXPORT_SYMBOL_GPL(uio_unregister_device);
  582. static int __init uio_init(void)
  583. {
  584. return 0;
  585. }
  586. static void __exit uio_exit(void)
  587. {
  588. }
  589. module_init(uio_init)
  590. module_exit(uio_exit)
  591. MODULE_LICENSE("GPL v2");