uio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. struct kobject *portio_dir;
  36. };
  37. static int uio_major;
  38. static DEFINE_IDR(uio_idr);
  39. static const struct file_operations uio_fops;
  40. /* UIO class infrastructure */
  41. static struct uio_class {
  42. struct kref kref;
  43. struct class *class;
  44. } *uio_class;
  45. /* Protect idr accesses */
  46. static DEFINE_MUTEX(minor_lock);
  47. /*
  48. * attributes
  49. */
  50. struct uio_map {
  51. struct kobject kobj;
  52. struct uio_mem *mem;
  53. };
  54. #define to_map(map) container_of(map, struct uio_map, kobj)
  55. static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
  56. {
  57. return sprintf(buf, "0x%lx\n", mem->addr);
  58. }
  59. static ssize_t map_size_show(struct uio_mem *mem, char *buf)
  60. {
  61. return sprintf(buf, "0x%lx\n", mem->size);
  62. }
  63. static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
  64. {
  65. return sprintf(buf, "0x%lx\n", mem->addr & ~PAGE_MASK);
  66. }
  67. struct map_sysfs_entry {
  68. struct attribute attr;
  69. ssize_t (*show)(struct uio_mem *, char *);
  70. ssize_t (*store)(struct uio_mem *, const char *, size_t);
  71. };
  72. static struct map_sysfs_entry addr_attribute =
  73. __ATTR(addr, S_IRUGO, map_addr_show, NULL);
  74. static struct map_sysfs_entry size_attribute =
  75. __ATTR(size, S_IRUGO, map_size_show, NULL);
  76. static struct map_sysfs_entry offset_attribute =
  77. __ATTR(offset, S_IRUGO, map_offset_show, NULL);
  78. static struct attribute *attrs[] = {
  79. &addr_attribute.attr,
  80. &size_attribute.attr,
  81. &offset_attribute.attr,
  82. NULL, /* need to NULL terminate the list of attributes */
  83. };
  84. static void map_release(struct kobject *kobj)
  85. {
  86. struct uio_map *map = to_map(kobj);
  87. kfree(map);
  88. }
  89. static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
  90. char *buf)
  91. {
  92. struct uio_map *map = to_map(kobj);
  93. struct uio_mem *mem = map->mem;
  94. struct map_sysfs_entry *entry;
  95. entry = container_of(attr, struct map_sysfs_entry, attr);
  96. if (!entry->show)
  97. return -EIO;
  98. return entry->show(mem, buf);
  99. }
  100. static struct sysfs_ops map_sysfs_ops = {
  101. .show = map_type_show,
  102. };
  103. static struct kobj_type map_attr_type = {
  104. .release = map_release,
  105. .sysfs_ops = &map_sysfs_ops,
  106. .default_attrs = attrs,
  107. };
  108. struct uio_portio {
  109. struct kobject kobj;
  110. struct uio_port *port;
  111. };
  112. #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
  113. static ssize_t portio_start_show(struct uio_port *port, char *buf)
  114. {
  115. return sprintf(buf, "0x%lx\n", port->start);
  116. }
  117. static ssize_t portio_size_show(struct uio_port *port, char *buf)
  118. {
  119. return sprintf(buf, "0x%lx\n", port->size);
  120. }
  121. static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
  122. {
  123. const char *porttypes[] = {"none", "x86", "gpio", "other"};
  124. if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
  125. return -EINVAL;
  126. return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
  127. }
  128. struct portio_sysfs_entry {
  129. struct attribute attr;
  130. ssize_t (*show)(struct uio_port *, char *);
  131. ssize_t (*store)(struct uio_port *, const char *, size_t);
  132. };
  133. static struct portio_sysfs_entry portio_start_attribute =
  134. __ATTR(start, S_IRUGO, portio_start_show, NULL);
  135. static struct portio_sysfs_entry portio_size_attribute =
  136. __ATTR(size, S_IRUGO, portio_size_show, NULL);
  137. static struct portio_sysfs_entry portio_porttype_attribute =
  138. __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
  139. static struct attribute *portio_attrs[] = {
  140. &portio_start_attribute.attr,
  141. &portio_size_attribute.attr,
  142. &portio_porttype_attribute.attr,
  143. NULL,
  144. };
  145. static void portio_release(struct kobject *kobj)
  146. {
  147. struct uio_portio *portio = to_portio(kobj);
  148. kfree(portio);
  149. }
  150. static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
  151. char *buf)
  152. {
  153. struct uio_portio *portio = to_portio(kobj);
  154. struct uio_port *port = portio->port;
  155. struct portio_sysfs_entry *entry;
  156. entry = container_of(attr, struct portio_sysfs_entry, attr);
  157. if (!entry->show)
  158. return -EIO;
  159. return entry->show(port, buf);
  160. }
  161. static struct sysfs_ops portio_sysfs_ops = {
  162. .show = portio_type_show,
  163. };
  164. static struct kobj_type portio_attr_type = {
  165. .release = portio_release,
  166. .sysfs_ops = &portio_sysfs_ops,
  167. .default_attrs = portio_attrs,
  168. };
  169. static ssize_t show_name(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct uio_device *idev = dev_get_drvdata(dev);
  173. if (idev)
  174. return sprintf(buf, "%s\n", idev->info->name);
  175. else
  176. return -ENODEV;
  177. }
  178. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  179. static ssize_t show_version(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct uio_device *idev = dev_get_drvdata(dev);
  183. if (idev)
  184. return sprintf(buf, "%s\n", idev->info->version);
  185. else
  186. return -ENODEV;
  187. }
  188. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  189. static ssize_t show_event(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. struct uio_device *idev = dev_get_drvdata(dev);
  193. if (idev)
  194. return sprintf(buf, "%u\n",
  195. (unsigned int)atomic_read(&idev->event));
  196. else
  197. return -ENODEV;
  198. }
  199. static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
  200. static struct attribute *uio_attrs[] = {
  201. &dev_attr_name.attr,
  202. &dev_attr_version.attr,
  203. &dev_attr_event.attr,
  204. NULL,
  205. };
  206. static struct attribute_group uio_attr_grp = {
  207. .attrs = uio_attrs,
  208. };
  209. /*
  210. * device functions
  211. */
  212. static int uio_dev_add_attributes(struct uio_device *idev)
  213. {
  214. int ret;
  215. int mi, pi;
  216. int map_found = 0;
  217. int portio_found = 0;
  218. struct uio_mem *mem;
  219. struct uio_map *map;
  220. struct uio_port *port;
  221. struct uio_portio *portio;
  222. ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
  223. if (ret)
  224. goto err_group;
  225. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  226. mem = &idev->info->mem[mi];
  227. if (mem->size == 0)
  228. break;
  229. if (!map_found) {
  230. map_found = 1;
  231. idev->map_dir = kobject_create_and_add("maps",
  232. &idev->dev->kobj);
  233. if (!idev->map_dir)
  234. goto err_map;
  235. }
  236. map = kzalloc(sizeof(*map), GFP_KERNEL);
  237. if (!map)
  238. goto err_map;
  239. kobject_init(&map->kobj, &map_attr_type);
  240. map->mem = mem;
  241. mem->map = map;
  242. ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
  243. if (ret)
  244. goto err_map;
  245. ret = kobject_uevent(&map->kobj, KOBJ_ADD);
  246. if (ret)
  247. goto err_map;
  248. }
  249. for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
  250. port = &idev->info->port[pi];
  251. if (port->size == 0)
  252. break;
  253. if (!portio_found) {
  254. portio_found = 1;
  255. idev->portio_dir = kobject_create_and_add("portio",
  256. &idev->dev->kobj);
  257. if (!idev->portio_dir)
  258. goto err_portio;
  259. }
  260. portio = kzalloc(sizeof(*portio), GFP_KERNEL);
  261. if (!portio)
  262. goto err_portio;
  263. kobject_init(&portio->kobj, &portio_attr_type);
  264. portio->port = port;
  265. port->portio = portio;
  266. ret = kobject_add(&portio->kobj, idev->portio_dir,
  267. "port%d", pi);
  268. if (ret)
  269. goto err_portio;
  270. ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
  271. if (ret)
  272. goto err_portio;
  273. }
  274. return 0;
  275. err_portio:
  276. for (pi--; pi >= 0; pi--) {
  277. port = &idev->info->port[pi];
  278. portio = port->portio;
  279. kobject_put(&portio->kobj);
  280. }
  281. kobject_put(idev->portio_dir);
  282. err_map:
  283. for (mi--; mi>=0; mi--) {
  284. mem = &idev->info->mem[mi];
  285. map = mem->map;
  286. kobject_put(&map->kobj);
  287. }
  288. kobject_put(idev->map_dir);
  289. sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
  290. err_group:
  291. dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
  292. return ret;
  293. }
  294. static void uio_dev_del_attributes(struct uio_device *idev)
  295. {
  296. int i;
  297. struct uio_mem *mem;
  298. struct uio_port *port;
  299. for (i = 0; i < MAX_UIO_MAPS; i++) {
  300. mem = &idev->info->mem[i];
  301. if (mem->size == 0)
  302. break;
  303. kobject_put(&mem->map->kobj);
  304. }
  305. kobject_put(idev->map_dir);
  306. for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
  307. port = &idev->info->port[i];
  308. if (port->size == 0)
  309. break;
  310. kobject_put(&port->portio->kobj);
  311. }
  312. kobject_put(idev->portio_dir);
  313. sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
  314. }
  315. static int uio_get_minor(struct uio_device *idev)
  316. {
  317. int retval = -ENOMEM;
  318. int id;
  319. mutex_lock(&minor_lock);
  320. if (idr_pre_get(&uio_idr, GFP_KERNEL) == 0)
  321. goto exit;
  322. retval = idr_get_new(&uio_idr, idev, &id);
  323. if (retval < 0) {
  324. if (retval == -EAGAIN)
  325. retval = -ENOMEM;
  326. goto exit;
  327. }
  328. idev->minor = id & MAX_ID_MASK;
  329. exit:
  330. mutex_unlock(&minor_lock);
  331. return retval;
  332. }
  333. static void uio_free_minor(struct uio_device *idev)
  334. {
  335. mutex_lock(&minor_lock);
  336. idr_remove(&uio_idr, idev->minor);
  337. mutex_unlock(&minor_lock);
  338. }
  339. /**
  340. * uio_event_notify - trigger an interrupt event
  341. * @info: UIO device capabilities
  342. */
  343. void uio_event_notify(struct uio_info *info)
  344. {
  345. struct uio_device *idev = info->uio_dev;
  346. atomic_inc(&idev->event);
  347. wake_up_interruptible(&idev->wait);
  348. kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
  349. }
  350. EXPORT_SYMBOL_GPL(uio_event_notify);
  351. /**
  352. * uio_interrupt - hardware interrupt handler
  353. * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
  354. * @dev_id: Pointer to the devices uio_device structure
  355. */
  356. static irqreturn_t uio_interrupt(int irq, void *dev_id)
  357. {
  358. struct uio_device *idev = (struct uio_device *)dev_id;
  359. irqreturn_t ret = idev->info->handler(irq, idev->info);
  360. if (ret == IRQ_HANDLED)
  361. uio_event_notify(idev->info);
  362. return ret;
  363. }
  364. struct uio_listener {
  365. struct uio_device *dev;
  366. s32 event_count;
  367. };
  368. static int uio_open(struct inode *inode, struct file *filep)
  369. {
  370. struct uio_device *idev;
  371. struct uio_listener *listener;
  372. int ret = 0;
  373. mutex_lock(&minor_lock);
  374. idev = idr_find(&uio_idr, iminor(inode));
  375. mutex_unlock(&minor_lock);
  376. if (!idev) {
  377. ret = -ENODEV;
  378. goto out;
  379. }
  380. if (!try_module_get(idev->owner)) {
  381. ret = -ENODEV;
  382. goto out;
  383. }
  384. listener = kmalloc(sizeof(*listener), GFP_KERNEL);
  385. if (!listener) {
  386. ret = -ENOMEM;
  387. goto err_alloc_listener;
  388. }
  389. listener->dev = idev;
  390. listener->event_count = atomic_read(&idev->event);
  391. filep->private_data = listener;
  392. if (idev->info->open) {
  393. ret = idev->info->open(idev->info, inode);
  394. if (ret)
  395. goto err_infoopen;
  396. }
  397. return 0;
  398. err_infoopen:
  399. kfree(listener);
  400. err_alloc_listener:
  401. module_put(idev->owner);
  402. out:
  403. return ret;
  404. }
  405. static int uio_fasync(int fd, struct file *filep, int on)
  406. {
  407. struct uio_listener *listener = filep->private_data;
  408. struct uio_device *idev = listener->dev;
  409. return fasync_helper(fd, filep, on, &idev->async_queue);
  410. }
  411. static int uio_release(struct inode *inode, struct file *filep)
  412. {
  413. int ret = 0;
  414. struct uio_listener *listener = filep->private_data;
  415. struct uio_device *idev = listener->dev;
  416. if (idev->info->release)
  417. ret = idev->info->release(idev->info, inode);
  418. module_put(idev->owner);
  419. kfree(listener);
  420. return ret;
  421. }
  422. static unsigned int uio_poll(struct file *filep, poll_table *wait)
  423. {
  424. struct uio_listener *listener = filep->private_data;
  425. struct uio_device *idev = listener->dev;
  426. if (idev->info->irq == UIO_IRQ_NONE)
  427. return -EIO;
  428. poll_wait(filep, &idev->wait, wait);
  429. if (listener->event_count != atomic_read(&idev->event))
  430. return POLLIN | POLLRDNORM;
  431. return 0;
  432. }
  433. static ssize_t uio_read(struct file *filep, char __user *buf,
  434. size_t count, loff_t *ppos)
  435. {
  436. struct uio_listener *listener = filep->private_data;
  437. struct uio_device *idev = listener->dev;
  438. DECLARE_WAITQUEUE(wait, current);
  439. ssize_t retval;
  440. s32 event_count;
  441. if (idev->info->irq == UIO_IRQ_NONE)
  442. return -EIO;
  443. if (count != sizeof(s32))
  444. return -EINVAL;
  445. add_wait_queue(&idev->wait, &wait);
  446. do {
  447. set_current_state(TASK_INTERRUPTIBLE);
  448. event_count = atomic_read(&idev->event);
  449. if (event_count != listener->event_count) {
  450. if (copy_to_user(buf, &event_count, count))
  451. retval = -EFAULT;
  452. else {
  453. listener->event_count = event_count;
  454. retval = count;
  455. }
  456. break;
  457. }
  458. if (filep->f_flags & O_NONBLOCK) {
  459. retval = -EAGAIN;
  460. break;
  461. }
  462. if (signal_pending(current)) {
  463. retval = -ERESTARTSYS;
  464. break;
  465. }
  466. schedule();
  467. } while (1);
  468. __set_current_state(TASK_RUNNING);
  469. remove_wait_queue(&idev->wait, &wait);
  470. return retval;
  471. }
  472. static ssize_t uio_write(struct file *filep, const char __user *buf,
  473. size_t count, loff_t *ppos)
  474. {
  475. struct uio_listener *listener = filep->private_data;
  476. struct uio_device *idev = listener->dev;
  477. ssize_t retval;
  478. s32 irq_on;
  479. if (idev->info->irq == UIO_IRQ_NONE)
  480. return -EIO;
  481. if (count != sizeof(s32))
  482. return -EINVAL;
  483. if (!idev->info->irqcontrol)
  484. return -ENOSYS;
  485. if (copy_from_user(&irq_on, buf, count))
  486. return -EFAULT;
  487. retval = idev->info->irqcontrol(idev->info, irq_on);
  488. return retval ? retval : sizeof(s32);
  489. }
  490. static int uio_find_mem_index(struct vm_area_struct *vma)
  491. {
  492. int mi;
  493. struct uio_device *idev = vma->vm_private_data;
  494. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  495. if (idev->info->mem[mi].size == 0)
  496. return -1;
  497. if (vma->vm_pgoff == mi)
  498. return mi;
  499. }
  500. return -1;
  501. }
  502. static void uio_vma_open(struct vm_area_struct *vma)
  503. {
  504. struct uio_device *idev = vma->vm_private_data;
  505. idev->vma_count++;
  506. }
  507. static void uio_vma_close(struct vm_area_struct *vma)
  508. {
  509. struct uio_device *idev = vma->vm_private_data;
  510. idev->vma_count--;
  511. }
  512. static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  513. {
  514. struct uio_device *idev = vma->vm_private_data;
  515. struct page *page;
  516. unsigned long offset;
  517. int mi = uio_find_mem_index(vma);
  518. if (mi < 0)
  519. return VM_FAULT_SIGBUS;
  520. /*
  521. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  522. * to use mem[N].
  523. */
  524. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  525. if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
  526. page = virt_to_page(idev->info->mem[mi].addr + offset);
  527. else
  528. page = vmalloc_to_page((void *)idev->info->mem[mi].addr
  529. + offset);
  530. get_page(page);
  531. vmf->page = page;
  532. return 0;
  533. }
  534. static struct vm_operations_struct uio_vm_ops = {
  535. .open = uio_vma_open,
  536. .close = uio_vma_close,
  537. .fault = uio_vma_fault,
  538. };
  539. static int uio_mmap_physical(struct vm_area_struct *vma)
  540. {
  541. struct uio_device *idev = vma->vm_private_data;
  542. int mi = uio_find_mem_index(vma);
  543. if (mi < 0)
  544. return -EINVAL;
  545. vma->vm_flags |= VM_IO | VM_RESERVED;
  546. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  547. return remap_pfn_range(vma,
  548. vma->vm_start,
  549. idev->info->mem[mi].addr >> PAGE_SHIFT,
  550. vma->vm_end - vma->vm_start,
  551. vma->vm_page_prot);
  552. }
  553. static int uio_mmap_logical(struct vm_area_struct *vma)
  554. {
  555. vma->vm_flags |= VM_RESERVED;
  556. vma->vm_ops = &uio_vm_ops;
  557. uio_vma_open(vma);
  558. return 0;
  559. }
  560. static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
  561. {
  562. struct uio_listener *listener = filep->private_data;
  563. struct uio_device *idev = listener->dev;
  564. int mi;
  565. unsigned long requested_pages, actual_pages;
  566. int ret = 0;
  567. if (vma->vm_end < vma->vm_start)
  568. return -EINVAL;
  569. vma->vm_private_data = idev;
  570. mi = uio_find_mem_index(vma);
  571. if (mi < 0)
  572. return -EINVAL;
  573. requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  574. actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
  575. if (requested_pages > actual_pages)
  576. return -EINVAL;
  577. if (idev->info->mmap) {
  578. ret = idev->info->mmap(idev->info, vma);
  579. return ret;
  580. }
  581. switch (idev->info->mem[mi].memtype) {
  582. case UIO_MEM_PHYS:
  583. return uio_mmap_physical(vma);
  584. case UIO_MEM_LOGICAL:
  585. case UIO_MEM_VIRTUAL:
  586. return uio_mmap_logical(vma);
  587. default:
  588. return -EINVAL;
  589. }
  590. }
  591. static const struct file_operations uio_fops = {
  592. .owner = THIS_MODULE,
  593. .open = uio_open,
  594. .release = uio_release,
  595. .read = uio_read,
  596. .write = uio_write,
  597. .mmap = uio_mmap,
  598. .poll = uio_poll,
  599. .fasync = uio_fasync,
  600. };
  601. static int uio_major_init(void)
  602. {
  603. uio_major = register_chrdev(0, "uio", &uio_fops);
  604. if (uio_major < 0)
  605. return uio_major;
  606. return 0;
  607. }
  608. static void uio_major_cleanup(void)
  609. {
  610. unregister_chrdev(uio_major, "uio");
  611. }
  612. static int init_uio_class(void)
  613. {
  614. int ret = 0;
  615. if (uio_class != NULL) {
  616. kref_get(&uio_class->kref);
  617. goto exit;
  618. }
  619. /* This is the first time in here, set everything up properly */
  620. ret = uio_major_init();
  621. if (ret)
  622. goto exit;
  623. uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
  624. if (!uio_class) {
  625. ret = -ENOMEM;
  626. goto err_kzalloc;
  627. }
  628. kref_init(&uio_class->kref);
  629. uio_class->class = class_create(THIS_MODULE, "uio");
  630. if (IS_ERR(uio_class->class)) {
  631. ret = IS_ERR(uio_class->class);
  632. printk(KERN_ERR "class_create failed for uio\n");
  633. goto err_class_create;
  634. }
  635. return 0;
  636. err_class_create:
  637. kfree(uio_class);
  638. uio_class = NULL;
  639. err_kzalloc:
  640. uio_major_cleanup();
  641. exit:
  642. return ret;
  643. }
  644. static void release_uio_class(struct kref *kref)
  645. {
  646. /* Ok, we cheat as we know we only have one uio_class */
  647. class_destroy(uio_class->class);
  648. kfree(uio_class);
  649. uio_major_cleanup();
  650. uio_class = NULL;
  651. }
  652. static void uio_class_destroy(void)
  653. {
  654. if (uio_class)
  655. kref_put(&uio_class->kref, release_uio_class);
  656. }
  657. /**
  658. * uio_register_device - register a new userspace IO device
  659. * @owner: module that creates the new device
  660. * @parent: parent device
  661. * @info: UIO device capabilities
  662. *
  663. * returns zero on success or a negative error code.
  664. */
  665. int __uio_register_device(struct module *owner,
  666. struct device *parent,
  667. struct uio_info *info)
  668. {
  669. struct uio_device *idev;
  670. int ret = 0;
  671. if (!parent || !info || !info->name || !info->version)
  672. return -EINVAL;
  673. info->uio_dev = NULL;
  674. ret = init_uio_class();
  675. if (ret)
  676. return ret;
  677. idev = kzalloc(sizeof(*idev), GFP_KERNEL);
  678. if (!idev) {
  679. ret = -ENOMEM;
  680. goto err_kzalloc;
  681. }
  682. idev->owner = owner;
  683. idev->info = info;
  684. init_waitqueue_head(&idev->wait);
  685. atomic_set(&idev->event, 0);
  686. ret = uio_get_minor(idev);
  687. if (ret)
  688. goto err_get_minor;
  689. idev->dev = device_create(uio_class->class, parent,
  690. MKDEV(uio_major, idev->minor), idev,
  691. "uio%d", idev->minor);
  692. if (IS_ERR(idev->dev)) {
  693. printk(KERN_ERR "UIO: device register failed\n");
  694. ret = PTR_ERR(idev->dev);
  695. goto err_device_create;
  696. }
  697. ret = uio_dev_add_attributes(idev);
  698. if (ret)
  699. goto err_uio_dev_add_attributes;
  700. info->uio_dev = idev;
  701. if (idev->info->irq >= 0) {
  702. ret = request_irq(idev->info->irq, uio_interrupt,
  703. idev->info->irq_flags, idev->info->name, idev);
  704. if (ret)
  705. goto err_request_irq;
  706. }
  707. return 0;
  708. err_request_irq:
  709. uio_dev_del_attributes(idev);
  710. err_uio_dev_add_attributes:
  711. device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
  712. err_device_create:
  713. uio_free_minor(idev);
  714. err_get_minor:
  715. kfree(idev);
  716. err_kzalloc:
  717. uio_class_destroy();
  718. return ret;
  719. }
  720. EXPORT_SYMBOL_GPL(__uio_register_device);
  721. /**
  722. * uio_unregister_device - unregister a industrial IO device
  723. * @info: UIO device capabilities
  724. *
  725. */
  726. void uio_unregister_device(struct uio_info *info)
  727. {
  728. struct uio_device *idev;
  729. if (!info || !info->uio_dev)
  730. return;
  731. idev = info->uio_dev;
  732. uio_free_minor(idev);
  733. if (info->irq >= 0)
  734. free_irq(info->irq, idev);
  735. uio_dev_del_attributes(idev);
  736. dev_set_drvdata(idev->dev, NULL);
  737. device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
  738. kfree(idev);
  739. uio_class_destroy();
  740. return;
  741. }
  742. EXPORT_SYMBOL_GPL(uio_unregister_device);
  743. static int __init uio_init(void)
  744. {
  745. return 0;
  746. }
  747. static void __exit uio_exit(void)
  748. {
  749. }
  750. module_init(uio_init)
  751. module_exit(uio_exit)
  752. MODULE_LICENSE("GPL v2");