uio.c 19 KB

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