uio.c 20 KB

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