uio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. bool uio_class_registered;
  208. /*
  209. * device functions
  210. */
  211. static int uio_dev_add_attributes(struct uio_device *idev)
  212. {
  213. int ret;
  214. int mi, pi;
  215. int map_found = 0;
  216. int portio_found = 0;
  217. struct uio_mem *mem;
  218. struct uio_map *map;
  219. struct uio_port *port;
  220. struct uio_portio *portio;
  221. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  222. mem = &idev->info->mem[mi];
  223. if (mem->size == 0)
  224. break;
  225. if (!map_found) {
  226. map_found = 1;
  227. idev->map_dir = kobject_create_and_add("maps",
  228. &idev->dev->kobj);
  229. if (!idev->map_dir)
  230. goto err_map;
  231. }
  232. map = kzalloc(sizeof(*map), GFP_KERNEL);
  233. if (!map)
  234. goto err_map_kobj;
  235. kobject_init(&map->kobj, &map_attr_type);
  236. map->mem = mem;
  237. mem->map = map;
  238. ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
  239. if (ret)
  240. goto err_map_kobj;
  241. ret = kobject_uevent(&map->kobj, KOBJ_ADD);
  242. if (ret)
  243. goto err_map;
  244. }
  245. for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
  246. port = &idev->info->port[pi];
  247. if (port->size == 0)
  248. break;
  249. if (!portio_found) {
  250. portio_found = 1;
  251. idev->portio_dir = kobject_create_and_add("portio",
  252. &idev->dev->kobj);
  253. if (!idev->portio_dir)
  254. goto err_portio;
  255. }
  256. portio = kzalloc(sizeof(*portio), GFP_KERNEL);
  257. if (!portio)
  258. goto err_portio_kobj;
  259. kobject_init(&portio->kobj, &portio_attr_type);
  260. portio->port = port;
  261. port->portio = portio;
  262. ret = kobject_add(&portio->kobj, idev->portio_dir,
  263. "port%d", pi);
  264. if (ret)
  265. goto err_portio_kobj;
  266. ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
  267. if (ret)
  268. goto err_portio;
  269. }
  270. return 0;
  271. err_portio:
  272. pi--;
  273. err_portio_kobj:
  274. for (; pi >= 0; pi--) {
  275. port = &idev->info->port[pi];
  276. portio = port->portio;
  277. kobject_put(&portio->kobj);
  278. }
  279. kobject_put(idev->portio_dir);
  280. err_map:
  281. mi--;
  282. err_map_kobj:
  283. for (; 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. dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
  290. return ret;
  291. }
  292. static void uio_dev_del_attributes(struct uio_device *idev)
  293. {
  294. int i;
  295. struct uio_mem *mem;
  296. struct uio_port *port;
  297. for (i = 0; i < MAX_UIO_MAPS; i++) {
  298. mem = &idev->info->mem[i];
  299. if (mem->size == 0)
  300. break;
  301. kobject_put(&mem->map->kobj);
  302. }
  303. kobject_put(idev->map_dir);
  304. for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
  305. port = &idev->info->port[i];
  306. if (port->size == 0)
  307. break;
  308. kobject_put(&port->portio->kobj);
  309. }
  310. kobject_put(idev->portio_dir);
  311. }
  312. static int uio_get_minor(struct uio_device *idev)
  313. {
  314. int retval = -ENOMEM;
  315. mutex_lock(&minor_lock);
  316. retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
  317. if (retval >= 0) {
  318. idev->minor = retval;
  319. retval = 0;
  320. } else if (retval == -ENOSPC) {
  321. dev_err(idev->dev, "too many uio devices\n");
  322. retval = -EINVAL;
  323. }
  324. mutex_unlock(&minor_lock);
  325. return retval;
  326. }
  327. static void uio_free_minor(struct uio_device *idev)
  328. {
  329. mutex_lock(&minor_lock);
  330. idr_remove(&uio_idr, idev->minor);
  331. mutex_unlock(&minor_lock);
  332. }
  333. /**
  334. * uio_event_notify - trigger an interrupt event
  335. * @info: UIO device capabilities
  336. */
  337. void uio_event_notify(struct uio_info *info)
  338. {
  339. struct uio_device *idev = info->uio_dev;
  340. atomic_inc(&idev->event);
  341. wake_up_interruptible(&idev->wait);
  342. kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
  343. }
  344. EXPORT_SYMBOL_GPL(uio_event_notify);
  345. /**
  346. * uio_interrupt - hardware interrupt handler
  347. * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
  348. * @dev_id: Pointer to the devices uio_device structure
  349. */
  350. static irqreturn_t uio_interrupt(int irq, void *dev_id)
  351. {
  352. struct uio_device *idev = (struct uio_device *)dev_id;
  353. irqreturn_t ret = idev->info->handler(irq, idev->info);
  354. if (ret == IRQ_HANDLED)
  355. uio_event_notify(idev->info);
  356. return ret;
  357. }
  358. struct uio_listener {
  359. struct uio_device *dev;
  360. s32 event_count;
  361. };
  362. static int uio_open(struct inode *inode, struct file *filep)
  363. {
  364. struct uio_device *idev;
  365. struct uio_listener *listener;
  366. int ret = 0;
  367. mutex_lock(&minor_lock);
  368. idev = idr_find(&uio_idr, iminor(inode));
  369. mutex_unlock(&minor_lock);
  370. if (!idev) {
  371. ret = -ENODEV;
  372. goto out;
  373. }
  374. if (!try_module_get(idev->owner)) {
  375. ret = -ENODEV;
  376. goto out;
  377. }
  378. listener = kmalloc(sizeof(*listener), GFP_KERNEL);
  379. if (!listener) {
  380. ret = -ENOMEM;
  381. goto err_alloc_listener;
  382. }
  383. listener->dev = idev;
  384. listener->event_count = atomic_read(&idev->event);
  385. filep->private_data = listener;
  386. if (idev->info->open) {
  387. ret = idev->info->open(idev->info, inode);
  388. if (ret)
  389. goto err_infoopen;
  390. }
  391. return 0;
  392. err_infoopen:
  393. kfree(listener);
  394. err_alloc_listener:
  395. module_put(idev->owner);
  396. out:
  397. return ret;
  398. }
  399. static int uio_fasync(int fd, struct file *filep, int on)
  400. {
  401. struct uio_listener *listener = filep->private_data;
  402. struct uio_device *idev = listener->dev;
  403. return fasync_helper(fd, filep, on, &idev->async_queue);
  404. }
  405. static int uio_release(struct inode *inode, struct file *filep)
  406. {
  407. int ret = 0;
  408. struct uio_listener *listener = filep->private_data;
  409. struct uio_device *idev = listener->dev;
  410. if (idev->info->release)
  411. ret = idev->info->release(idev->info, inode);
  412. module_put(idev->owner);
  413. kfree(listener);
  414. return ret;
  415. }
  416. static unsigned int uio_poll(struct file *filep, poll_table *wait)
  417. {
  418. struct uio_listener *listener = filep->private_data;
  419. struct uio_device *idev = listener->dev;
  420. if (!idev->info->irq)
  421. return -EIO;
  422. poll_wait(filep, &idev->wait, wait);
  423. if (listener->event_count != atomic_read(&idev->event))
  424. return POLLIN | POLLRDNORM;
  425. return 0;
  426. }
  427. static ssize_t uio_read(struct file *filep, char __user *buf,
  428. size_t count, loff_t *ppos)
  429. {
  430. struct uio_listener *listener = filep->private_data;
  431. struct uio_device *idev = listener->dev;
  432. DECLARE_WAITQUEUE(wait, current);
  433. ssize_t retval;
  434. s32 event_count;
  435. if (!idev->info->irq)
  436. return -EIO;
  437. if (count != sizeof(s32))
  438. return -EINVAL;
  439. add_wait_queue(&idev->wait, &wait);
  440. do {
  441. set_current_state(TASK_INTERRUPTIBLE);
  442. event_count = atomic_read(&idev->event);
  443. if (event_count != listener->event_count) {
  444. __set_current_state(TASK_RUNNING);
  445. if (copy_to_user(buf, &event_count, count))
  446. retval = -EFAULT;
  447. else {
  448. listener->event_count = event_count;
  449. retval = count;
  450. }
  451. break;
  452. }
  453. if (filep->f_flags & O_NONBLOCK) {
  454. retval = -EAGAIN;
  455. break;
  456. }
  457. if (signal_pending(current)) {
  458. retval = -ERESTARTSYS;
  459. break;
  460. }
  461. schedule();
  462. } while (1);
  463. __set_current_state(TASK_RUNNING);
  464. remove_wait_queue(&idev->wait, &wait);
  465. return retval;
  466. }
  467. static ssize_t uio_write(struct file *filep, const char __user *buf,
  468. size_t count, loff_t *ppos)
  469. {
  470. struct uio_listener *listener = filep->private_data;
  471. struct uio_device *idev = listener->dev;
  472. ssize_t retval;
  473. s32 irq_on;
  474. if (!idev->info->irq)
  475. return -EIO;
  476. if (count != sizeof(s32))
  477. return -EINVAL;
  478. if (!idev->info->irqcontrol)
  479. return -ENOSYS;
  480. if (copy_from_user(&irq_on, buf, count))
  481. return -EFAULT;
  482. retval = idev->info->irqcontrol(idev->info, irq_on);
  483. return retval ? retval : sizeof(s32);
  484. }
  485. static int uio_find_mem_index(struct vm_area_struct *vma)
  486. {
  487. struct uio_device *idev = vma->vm_private_data;
  488. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  489. if (idev->info->mem[vma->vm_pgoff].size == 0)
  490. return -1;
  491. return (int)vma->vm_pgoff;
  492. }
  493. return -1;
  494. }
  495. static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  496. {
  497. struct uio_device *idev = vma->vm_private_data;
  498. struct page *page;
  499. unsigned long offset;
  500. void *addr;
  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. addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
  510. if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
  511. page = virt_to_page(addr);
  512. else
  513. page = vmalloc_to_page(addr);
  514. get_page(page);
  515. vmf->page = page;
  516. return 0;
  517. }
  518. static const struct vm_operations_struct uio_logical_vm_ops = {
  519. .fault = uio_vma_fault,
  520. };
  521. static int uio_mmap_logical(struct vm_area_struct *vma)
  522. {
  523. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  524. vma->vm_ops = &uio_logical_vm_ops;
  525. return 0;
  526. }
  527. static const struct vm_operations_struct uio_physical_vm_ops = {
  528. #ifdef CONFIG_HAVE_IOREMAP_PROT
  529. .access = generic_access_phys,
  530. #endif
  531. };
  532. static int uio_mmap_physical(struct vm_area_struct *vma)
  533. {
  534. struct uio_device *idev = vma->vm_private_data;
  535. int mi = uio_find_mem_index(vma);
  536. struct uio_mem *mem;
  537. if (mi < 0)
  538. return -EINVAL;
  539. mem = idev->info->mem + mi;
  540. if (mem->addr & ~PAGE_MASK)
  541. return -ENODEV;
  542. if (vma->vm_end - vma->vm_start > mem->size)
  543. return -EINVAL;
  544. vma->vm_ops = &uio_physical_vm_ops;
  545. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  546. /*
  547. * We cannot use the vm_iomap_memory() helper here,
  548. * because vma->vm_pgoff is the map index we looked
  549. * up above in uio_find_mem_index(), rather than an
  550. * actual page offset into the mmap.
  551. *
  552. * So we just do the physical mmap without a page
  553. * offset.
  554. */
  555. return remap_pfn_range(vma,
  556. vma->vm_start,
  557. mem->addr >> PAGE_SHIFT,
  558. vma->vm_end - vma->vm_start,
  559. vma->vm_page_prot);
  560. }
  561. static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
  562. {
  563. struct uio_listener *listener = filep->private_data;
  564. struct uio_device *idev = listener->dev;
  565. int mi;
  566. unsigned long requested_pages, actual_pages;
  567. int ret = 0;
  568. if (vma->vm_end < vma->vm_start)
  569. return -EINVAL;
  570. vma->vm_private_data = idev;
  571. mi = uio_find_mem_index(vma);
  572. if (mi < 0)
  573. return -EINVAL;
  574. requested_pages = vma_pages(vma);
  575. actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
  576. + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
  577. if (requested_pages > actual_pages)
  578. return -EINVAL;
  579. if (idev->info->mmap) {
  580. ret = idev->info->mmap(idev->info, vma);
  581. return ret;
  582. }
  583. switch (idev->info->mem[mi].memtype) {
  584. case UIO_MEM_PHYS:
  585. return uio_mmap_physical(vma);
  586. case UIO_MEM_LOGICAL:
  587. case UIO_MEM_VIRTUAL:
  588. return uio_mmap_logical(vma);
  589. default:
  590. return -EINVAL;
  591. }
  592. }
  593. static const struct file_operations uio_fops = {
  594. .owner = THIS_MODULE,
  595. .open = uio_open,
  596. .release = uio_release,
  597. .read = uio_read,
  598. .write = uio_write,
  599. .mmap = uio_mmap,
  600. .poll = uio_poll,
  601. .fasync = uio_fasync,
  602. .llseek = noop_llseek,
  603. };
  604. static int uio_major_init(void)
  605. {
  606. static const char name[] = "uio";
  607. struct cdev *cdev = NULL;
  608. dev_t uio_dev = 0;
  609. int result;
  610. result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
  611. if (result)
  612. goto out;
  613. result = -ENOMEM;
  614. cdev = cdev_alloc();
  615. if (!cdev)
  616. goto out_unregister;
  617. cdev->owner = THIS_MODULE;
  618. cdev->ops = &uio_fops;
  619. kobject_set_name(&cdev->kobj, "%s", name);
  620. result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
  621. if (result)
  622. goto out_put;
  623. uio_major = MAJOR(uio_dev);
  624. uio_cdev = cdev;
  625. return 0;
  626. out_put:
  627. kobject_put(&cdev->kobj);
  628. out_unregister:
  629. unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
  630. out:
  631. return result;
  632. }
  633. static void uio_major_cleanup(void)
  634. {
  635. unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
  636. cdev_del(uio_cdev);
  637. }
  638. static int init_uio_class(void)
  639. {
  640. int ret;
  641. /* This is the first time in here, set everything up properly */
  642. ret = uio_major_init();
  643. if (ret)
  644. goto exit;
  645. ret = class_register(&uio_class);
  646. if (ret) {
  647. printk(KERN_ERR "class_register failed for uio\n");
  648. goto err_class_register;
  649. }
  650. uio_class_registered = true;
  651. return 0;
  652. err_class_register:
  653. uio_major_cleanup();
  654. exit:
  655. return ret;
  656. }
  657. static void release_uio_class(void)
  658. {
  659. uio_class_registered = false;
  660. class_unregister(&uio_class);
  661. uio_major_cleanup();
  662. }
  663. /**
  664. * uio_register_device - register a new userspace IO device
  665. * @owner: module that creates the new device
  666. * @parent: parent device
  667. * @info: UIO device capabilities
  668. *
  669. * returns zero on success or a negative error code.
  670. */
  671. int __uio_register_device(struct module *owner,
  672. struct device *parent,
  673. struct uio_info *info)
  674. {
  675. struct uio_device *idev;
  676. int ret = 0;
  677. if (!uio_class_registered)
  678. return -EPROBE_DEFER;
  679. if (!parent || !info || !info->name || !info->version)
  680. return -EINVAL;
  681. info->uio_dev = NULL;
  682. idev = devm_kzalloc(parent, sizeof(*idev), GFP_KERNEL);
  683. if (!idev) {
  684. return -ENOMEM;
  685. }
  686. idev->owner = owner;
  687. idev->info = info;
  688. init_waitqueue_head(&idev->wait);
  689. atomic_set(&idev->event, 0);
  690. ret = uio_get_minor(idev);
  691. if (ret)
  692. return ret;
  693. idev->dev = device_create(&uio_class, parent,
  694. MKDEV(uio_major, idev->minor), idev,
  695. "uio%d", idev->minor);
  696. if (IS_ERR(idev->dev)) {
  697. printk(KERN_ERR "UIO: device register failed\n");
  698. ret = PTR_ERR(idev->dev);
  699. goto err_device_create;
  700. }
  701. ret = uio_dev_add_attributes(idev);
  702. if (ret)
  703. goto err_uio_dev_add_attributes;
  704. info->uio_dev = idev;
  705. if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
  706. /*
  707. * Note that we deliberately don't use devm_request_irq
  708. * here. The parent module can unregister the UIO device
  709. * and call pci_disable_msi, which requires that this
  710. * irq has been freed. However, the device may have open
  711. * FDs at the time of unregister and therefore may not be
  712. * freed until they are released.
  713. */
  714. ret = request_irq(info->irq, uio_interrupt,
  715. info->irq_flags, info->name, idev);
  716. if (ret) {
  717. info->uio_dev = NULL;
  718. goto err_request_irq;
  719. }
  720. }
  721. return 0;
  722. err_request_irq:
  723. uio_dev_del_attributes(idev);
  724. err_uio_dev_add_attributes:
  725. device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
  726. err_device_create:
  727. uio_free_minor(idev);
  728. return ret;
  729. }
  730. EXPORT_SYMBOL_GPL(__uio_register_device);
  731. /**
  732. * uio_unregister_device - unregister a industrial IO device
  733. * @info: UIO device capabilities
  734. *
  735. */
  736. void uio_unregister_device(struct uio_info *info)
  737. {
  738. struct uio_device *idev;
  739. if (!info || !info->uio_dev)
  740. return;
  741. idev = info->uio_dev;
  742. uio_free_minor(idev);
  743. uio_dev_del_attributes(idev);
  744. if (info->irq && info->irq != UIO_IRQ_CUSTOM)
  745. free_irq(info->irq, idev);
  746. device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
  747. return;
  748. }
  749. EXPORT_SYMBOL_GPL(uio_unregister_device);
  750. static int __init uio_init(void)
  751. {
  752. return init_uio_class();
  753. }
  754. static void __exit uio_exit(void)
  755. {
  756. release_uio_class();
  757. idr_destroy(&uio_idr);
  758. }
  759. module_init(uio_init)
  760. module_exit(uio_exit)
  761. MODULE_LICENSE("GPL v2");