uio.c 20 KB

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