uio.c 20 KB

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