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