uio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  355. {
  356. struct uio_device *idev = vma->vm_private_data;
  357. struct page *page;
  358. int mi = uio_find_mem_index(vma);
  359. if (mi < 0)
  360. return VM_FAULT_SIGBUS;
  361. if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
  362. page = virt_to_page(idev->info->mem[mi].addr);
  363. else
  364. page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
  365. get_page(page);
  366. vmf->page = page;
  367. return 0;
  368. }
  369. static struct vm_operations_struct uio_vm_ops = {
  370. .open = uio_vma_open,
  371. .close = uio_vma_close,
  372. .fault = uio_vma_fault,
  373. };
  374. static int uio_mmap_physical(struct vm_area_struct *vma)
  375. {
  376. struct uio_device *idev = vma->vm_private_data;
  377. int mi = uio_find_mem_index(vma);
  378. if (mi < 0)
  379. return -EINVAL;
  380. vma->vm_flags |= VM_IO | VM_RESERVED;
  381. return remap_pfn_range(vma,
  382. vma->vm_start,
  383. idev->info->mem[mi].addr >> PAGE_SHIFT,
  384. vma->vm_end - vma->vm_start,
  385. vma->vm_page_prot);
  386. }
  387. static int uio_mmap_logical(struct vm_area_struct *vma)
  388. {
  389. vma->vm_flags |= VM_RESERVED;
  390. vma->vm_ops = &uio_vm_ops;
  391. uio_vma_open(vma);
  392. return 0;
  393. }
  394. static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
  395. {
  396. struct uio_listener *listener = filep->private_data;
  397. struct uio_device *idev = listener->dev;
  398. int mi;
  399. unsigned long requested_pages, actual_pages;
  400. int ret = 0;
  401. if (vma->vm_end < vma->vm_start)
  402. return -EINVAL;
  403. vma->vm_private_data = idev;
  404. mi = uio_find_mem_index(vma);
  405. if (mi < 0)
  406. return -EINVAL;
  407. requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  408. actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
  409. if (requested_pages > actual_pages)
  410. return -EINVAL;
  411. if (idev->info->mmap) {
  412. if (!try_module_get(idev->owner))
  413. return -ENODEV;
  414. ret = idev->info->mmap(idev->info, vma);
  415. module_put(idev->owner);
  416. return ret;
  417. }
  418. switch (idev->info->mem[mi].memtype) {
  419. case UIO_MEM_PHYS:
  420. return uio_mmap_physical(vma);
  421. case UIO_MEM_LOGICAL:
  422. case UIO_MEM_VIRTUAL:
  423. return uio_mmap_logical(vma);
  424. default:
  425. return -EINVAL;
  426. }
  427. }
  428. static const struct file_operations uio_fops = {
  429. .owner = THIS_MODULE,
  430. .open = uio_open,
  431. .release = uio_release,
  432. .read = uio_read,
  433. .mmap = uio_mmap,
  434. .poll = uio_poll,
  435. .fasync = uio_fasync,
  436. };
  437. static int uio_major_init(void)
  438. {
  439. uio_major = register_chrdev(0, "uio", &uio_fops);
  440. if (uio_major < 0)
  441. return uio_major;
  442. return 0;
  443. }
  444. static void uio_major_cleanup(void)
  445. {
  446. unregister_chrdev(uio_major, "uio");
  447. }
  448. static int init_uio_class(void)
  449. {
  450. int ret = 0;
  451. if (uio_class != NULL) {
  452. kref_get(&uio_class->kref);
  453. goto exit;
  454. }
  455. /* This is the first time in here, set everything up properly */
  456. ret = uio_major_init();
  457. if (ret)
  458. goto exit;
  459. uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
  460. if (!uio_class) {
  461. ret = -ENOMEM;
  462. goto err_kzalloc;
  463. }
  464. kref_init(&uio_class->kref);
  465. uio_class->class = class_create(THIS_MODULE, "uio");
  466. if (IS_ERR(uio_class->class)) {
  467. ret = IS_ERR(uio_class->class);
  468. printk(KERN_ERR "class_create failed for uio\n");
  469. goto err_class_create;
  470. }
  471. return 0;
  472. err_class_create:
  473. kfree(uio_class);
  474. uio_class = NULL;
  475. err_kzalloc:
  476. uio_major_cleanup();
  477. exit:
  478. return ret;
  479. }
  480. static void release_uio_class(struct kref *kref)
  481. {
  482. /* Ok, we cheat as we know we only have one uio_class */
  483. class_destroy(uio_class->class);
  484. kfree(uio_class);
  485. uio_major_cleanup();
  486. uio_class = NULL;
  487. }
  488. static void uio_class_destroy(void)
  489. {
  490. if (uio_class)
  491. kref_put(&uio_class->kref, release_uio_class);
  492. }
  493. /**
  494. * uio_register_device - register a new userspace IO device
  495. * @owner: module that creates the new device
  496. * @parent: parent device
  497. * @info: UIO device capabilities
  498. *
  499. * returns zero on success or a negative error code.
  500. */
  501. int __uio_register_device(struct module *owner,
  502. struct device *parent,
  503. struct uio_info *info)
  504. {
  505. struct uio_device *idev;
  506. int ret = 0;
  507. if (!parent || !info || !info->name || !info->version)
  508. return -EINVAL;
  509. info->uio_dev = NULL;
  510. ret = init_uio_class();
  511. if (ret)
  512. return ret;
  513. idev = kzalloc(sizeof(*idev), GFP_KERNEL);
  514. if (!idev) {
  515. ret = -ENOMEM;
  516. goto err_kzalloc;
  517. }
  518. idev->owner = owner;
  519. idev->info = info;
  520. init_waitqueue_head(&idev->wait);
  521. atomic_set(&idev->event, 0);
  522. ret = uio_get_minor(idev);
  523. if (ret)
  524. goto err_get_minor;
  525. idev->dev = device_create(uio_class->class, parent,
  526. MKDEV(uio_major, idev->minor),
  527. "uio%d", idev->minor);
  528. if (IS_ERR(idev->dev)) {
  529. printk(KERN_ERR "UIO: device register failed\n");
  530. ret = PTR_ERR(idev->dev);
  531. goto err_device_create;
  532. }
  533. dev_set_drvdata(idev->dev, idev);
  534. ret = uio_dev_add_attributes(idev);
  535. if (ret)
  536. goto err_uio_dev_add_attributes;
  537. info->uio_dev = idev;
  538. if (idev->info->irq >= 0) {
  539. ret = request_irq(idev->info->irq, uio_interrupt,
  540. idev->info->irq_flags, idev->info->name, idev);
  541. if (ret)
  542. goto err_request_irq;
  543. }
  544. return 0;
  545. err_request_irq:
  546. uio_dev_del_attributes(idev);
  547. err_uio_dev_add_attributes:
  548. device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
  549. err_device_create:
  550. uio_free_minor(idev);
  551. err_get_minor:
  552. kfree(idev);
  553. err_kzalloc:
  554. uio_class_destroy();
  555. return ret;
  556. }
  557. EXPORT_SYMBOL_GPL(__uio_register_device);
  558. /**
  559. * uio_unregister_device - unregister a industrial IO device
  560. * @info: UIO device capabilities
  561. *
  562. */
  563. void uio_unregister_device(struct uio_info *info)
  564. {
  565. struct uio_device *idev;
  566. if (!info || !info->uio_dev)
  567. return;
  568. idev = info->uio_dev;
  569. uio_free_minor(idev);
  570. if (info->irq >= 0)
  571. free_irq(info->irq, idev);
  572. uio_dev_del_attributes(idev);
  573. dev_set_drvdata(idev->dev, NULL);
  574. device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
  575. kfree(idev);
  576. uio_class_destroy();
  577. return;
  578. }
  579. EXPORT_SYMBOL_GPL(uio_unregister_device);
  580. static int __init uio_init(void)
  581. {
  582. return 0;
  583. }
  584. static void __exit uio_exit(void)
  585. {
  586. }
  587. module_init(uio_init)
  588. module_exit(uio_exit)
  589. MODULE_LICENSE("GPL v2");