uio.c 20 KB

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