uio.c 20 KB

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