uio.c 20 KB

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