uio.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/uio/uio.c
  4. *
  5. * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
  6. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  7. * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de>
  8. * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
  9. *
  10. * Userspace IO
  11. *
  12. * Base Functions
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/poll.h>
  17. #include <linux/device.h>
  18. #include <linux/slab.h>
  19. #include <linux/mm.h>
  20. #include <linux/idr.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/string.h>
  23. #include <linux/kobject.h>
  24. #include <linux/cdev.h>
  25. #include <linux/uio_driver.h>
  26. #define UIO_MAX_DEVICES (1U << MINORBITS)
  27. static int uio_major;
  28. static struct cdev *uio_cdev;
  29. static DEFINE_IDR(uio_idr);
  30. static const struct file_operations uio_fops;
  31. /* Protect idr accesses */
  32. static DEFINE_MUTEX(minor_lock);
  33. /*
  34. * attributes
  35. */
  36. struct uio_map {
  37. struct kobject kobj;
  38. struct uio_mem *mem;
  39. };
  40. #define to_map(map) container_of(map, struct uio_map, kobj)
  41. static ssize_t map_name_show(struct uio_mem *mem, char *buf)
  42. {
  43. if (unlikely(!mem->name))
  44. mem->name = "";
  45. return sprintf(buf, "%s\n", mem->name);
  46. }
  47. static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
  48. {
  49. return sprintf(buf, "%pa\n", &mem->addr);
  50. }
  51. static ssize_t map_size_show(struct uio_mem *mem, char *buf)
  52. {
  53. return sprintf(buf, "%pa\n", &mem->size);
  54. }
  55. static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
  56. {
  57. return sprintf(buf, "0x%llx\n", (unsigned long long)mem->offs);
  58. }
  59. struct map_sysfs_entry {
  60. struct attribute attr;
  61. ssize_t (*show)(struct uio_mem *, char *);
  62. ssize_t (*store)(struct uio_mem *, const char *, size_t);
  63. };
  64. static struct map_sysfs_entry name_attribute =
  65. __ATTR(name, S_IRUGO, map_name_show, NULL);
  66. static struct map_sysfs_entry addr_attribute =
  67. __ATTR(addr, S_IRUGO, map_addr_show, NULL);
  68. static struct map_sysfs_entry size_attribute =
  69. __ATTR(size, S_IRUGO, map_size_show, NULL);
  70. static struct map_sysfs_entry offset_attribute =
  71. __ATTR(offset, S_IRUGO, map_offset_show, NULL);
  72. static struct attribute *attrs[] = {
  73. &name_attribute.attr,
  74. &addr_attribute.attr,
  75. &size_attribute.attr,
  76. &offset_attribute.attr,
  77. NULL, /* need to NULL terminate the list of attributes */
  78. };
  79. static void map_release(struct kobject *kobj)
  80. {
  81. struct uio_map *map = to_map(kobj);
  82. kfree(map);
  83. }
  84. static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
  85. char *buf)
  86. {
  87. struct uio_map *map = to_map(kobj);
  88. struct uio_mem *mem = map->mem;
  89. struct map_sysfs_entry *entry;
  90. entry = container_of(attr, struct map_sysfs_entry, attr);
  91. if (!entry->show)
  92. return -EIO;
  93. return entry->show(mem, buf);
  94. }
  95. static const struct sysfs_ops map_sysfs_ops = {
  96. .show = map_type_show,
  97. };
  98. static struct kobj_type map_attr_type = {
  99. .release = map_release,
  100. .sysfs_ops = &map_sysfs_ops,
  101. .default_attrs = attrs,
  102. };
  103. struct uio_portio {
  104. struct kobject kobj;
  105. struct uio_port *port;
  106. };
  107. #define to_portio(portio) container_of(portio, struct uio_portio, kobj)
  108. static ssize_t portio_name_show(struct uio_port *port, char *buf)
  109. {
  110. if (unlikely(!port->name))
  111. port->name = "";
  112. return sprintf(buf, "%s\n", port->name);
  113. }
  114. static ssize_t portio_start_show(struct uio_port *port, char *buf)
  115. {
  116. return sprintf(buf, "0x%lx\n", port->start);
  117. }
  118. static ssize_t portio_size_show(struct uio_port *port, char *buf)
  119. {
  120. return sprintf(buf, "0x%lx\n", port->size);
  121. }
  122. static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
  123. {
  124. const char *porttypes[] = {"none", "x86", "gpio", "other"};
  125. if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
  126. return -EINVAL;
  127. return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
  128. }
  129. struct portio_sysfs_entry {
  130. struct attribute attr;
  131. ssize_t (*show)(struct uio_port *, char *);
  132. ssize_t (*store)(struct uio_port *, const char *, size_t);
  133. };
  134. static struct portio_sysfs_entry portio_name_attribute =
  135. __ATTR(name, S_IRUGO, portio_name_show, NULL);
  136. static struct portio_sysfs_entry portio_start_attribute =
  137. __ATTR(start, S_IRUGO, portio_start_show, NULL);
  138. static struct portio_sysfs_entry portio_size_attribute =
  139. __ATTR(size, S_IRUGO, portio_size_show, NULL);
  140. static struct portio_sysfs_entry portio_porttype_attribute =
  141. __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
  142. static struct attribute *portio_attrs[] = {
  143. &portio_name_attribute.attr,
  144. &portio_start_attribute.attr,
  145. &portio_size_attribute.attr,
  146. &portio_porttype_attribute.attr,
  147. NULL,
  148. };
  149. static void portio_release(struct kobject *kobj)
  150. {
  151. struct uio_portio *portio = to_portio(kobj);
  152. kfree(portio);
  153. }
  154. static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
  155. char *buf)
  156. {
  157. struct uio_portio *portio = to_portio(kobj);
  158. struct uio_port *port = portio->port;
  159. struct portio_sysfs_entry *entry;
  160. entry = container_of(attr, struct portio_sysfs_entry, attr);
  161. if (!entry->show)
  162. return -EIO;
  163. return entry->show(port, buf);
  164. }
  165. static const struct sysfs_ops portio_sysfs_ops = {
  166. .show = portio_type_show,
  167. };
  168. static struct kobj_type portio_attr_type = {
  169. .release = portio_release,
  170. .sysfs_ops = &portio_sysfs_ops,
  171. .default_attrs = portio_attrs,
  172. };
  173. static ssize_t name_show(struct device *dev,
  174. struct device_attribute *attr, char *buf)
  175. {
  176. struct uio_device *idev = dev_get_drvdata(dev);
  177. int ret;
  178. mutex_lock(&idev->info_lock);
  179. if (!idev->info) {
  180. ret = -EINVAL;
  181. dev_err(dev, "the device has been unregistered\n");
  182. goto out;
  183. }
  184. ret = sprintf(buf, "%s\n", idev->info->name);
  185. out:
  186. mutex_unlock(&idev->info_lock);
  187. return ret;
  188. }
  189. static DEVICE_ATTR_RO(name);
  190. static ssize_t version_show(struct device *dev,
  191. struct device_attribute *attr, char *buf)
  192. {
  193. struct uio_device *idev = dev_get_drvdata(dev);
  194. int ret;
  195. mutex_lock(&idev->info_lock);
  196. if (!idev->info) {
  197. ret = -EINVAL;
  198. dev_err(dev, "the device has been unregistered\n");
  199. goto out;
  200. }
  201. ret = sprintf(buf, "%s\n", idev->info->version);
  202. out:
  203. mutex_unlock(&idev->info_lock);
  204. return ret;
  205. }
  206. static DEVICE_ATTR_RO(version);
  207. static ssize_t event_show(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. struct uio_device *idev = dev_get_drvdata(dev);
  211. return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
  212. }
  213. static DEVICE_ATTR_RO(event);
  214. static struct attribute *uio_attrs[] = {
  215. &dev_attr_name.attr,
  216. &dev_attr_version.attr,
  217. &dev_attr_event.attr,
  218. NULL,
  219. };
  220. ATTRIBUTE_GROUPS(uio);
  221. /* UIO class infrastructure */
  222. static struct class uio_class = {
  223. .name = "uio",
  224. .dev_groups = uio_groups,
  225. };
  226. static bool uio_class_registered;
  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. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  241. mem = &idev->info->mem[mi];
  242. if (mem->size == 0)
  243. break;
  244. if (!map_found) {
  245. map_found = 1;
  246. idev->map_dir = kobject_create_and_add("maps",
  247. &idev->dev.kobj);
  248. if (!idev->map_dir) {
  249. ret = -ENOMEM;
  250. goto err_map;
  251. }
  252. }
  253. map = kzalloc(sizeof(*map), GFP_KERNEL);
  254. if (!map) {
  255. ret = -ENOMEM;
  256. goto err_map;
  257. }
  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_kobj;
  264. ret = kobject_uevent(&map->kobj, KOBJ_ADD);
  265. if (ret)
  266. goto err_map_kobj;
  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. ret = -ENOMEM;
  278. goto err_portio;
  279. }
  280. }
  281. portio = kzalloc(sizeof(*portio), GFP_KERNEL);
  282. if (!portio) {
  283. ret = -ENOMEM;
  284. goto err_portio;
  285. }
  286. kobject_init(&portio->kobj, &portio_attr_type);
  287. portio->port = port;
  288. port->portio = portio;
  289. ret = kobject_add(&portio->kobj, idev->portio_dir,
  290. "port%d", pi);
  291. if (ret)
  292. goto err_portio_kobj;
  293. ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
  294. if (ret)
  295. goto err_portio_kobj;
  296. }
  297. return 0;
  298. err_portio:
  299. pi--;
  300. err_portio_kobj:
  301. for (; pi >= 0; pi--) {
  302. port = &idev->info->port[pi];
  303. portio = port->portio;
  304. kobject_put(&portio->kobj);
  305. }
  306. kobject_put(idev->portio_dir);
  307. err_map:
  308. mi--;
  309. err_map_kobj:
  310. for (; mi >= 0; mi--) {
  311. mem = &idev->info->mem[mi];
  312. map = mem->map;
  313. kobject_put(&map->kobj);
  314. }
  315. kobject_put(idev->map_dir);
  316. dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
  317. return ret;
  318. }
  319. static void uio_dev_del_attributes(struct uio_device *idev)
  320. {
  321. int i;
  322. struct uio_mem *mem;
  323. struct uio_port *port;
  324. for (i = 0; i < MAX_UIO_MAPS; i++) {
  325. mem = &idev->info->mem[i];
  326. if (mem->size == 0)
  327. break;
  328. kobject_put(&mem->map->kobj);
  329. }
  330. kobject_put(idev->map_dir);
  331. for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
  332. port = &idev->info->port[i];
  333. if (port->size == 0)
  334. break;
  335. kobject_put(&port->portio->kobj);
  336. }
  337. kobject_put(idev->portio_dir);
  338. }
  339. static int uio_get_minor(struct uio_device *idev)
  340. {
  341. int retval = -ENOMEM;
  342. mutex_lock(&minor_lock);
  343. retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
  344. if (retval >= 0) {
  345. idev->minor = retval;
  346. retval = 0;
  347. } else if (retval == -ENOSPC) {
  348. dev_err(&idev->dev, "too many uio devices\n");
  349. retval = -EINVAL;
  350. }
  351. mutex_unlock(&minor_lock);
  352. return retval;
  353. }
  354. static void uio_free_minor(struct uio_device *idev)
  355. {
  356. mutex_lock(&minor_lock);
  357. idr_remove(&uio_idr, idev->minor);
  358. mutex_unlock(&minor_lock);
  359. }
  360. /**
  361. * uio_event_notify - trigger an interrupt event
  362. * @info: UIO device capabilities
  363. */
  364. void uio_event_notify(struct uio_info *info)
  365. {
  366. struct uio_device *idev = info->uio_dev;
  367. atomic_inc(&idev->event);
  368. wake_up_interruptible(&idev->wait);
  369. kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
  370. }
  371. EXPORT_SYMBOL_GPL(uio_event_notify);
  372. /**
  373. * uio_interrupt - hardware interrupt handler
  374. * @irq: IRQ number, can be UIO_IRQ_CYCLIC for cyclic timer
  375. * @dev_id: Pointer to the devices uio_device structure
  376. */
  377. static irqreturn_t uio_interrupt(int irq, void *dev_id)
  378. {
  379. struct uio_device *idev = (struct uio_device *)dev_id;
  380. irqreturn_t ret;
  381. ret = idev->info->handler(irq, idev->info);
  382. if (ret == IRQ_HANDLED)
  383. uio_event_notify(idev->info);
  384. return ret;
  385. }
  386. struct uio_listener {
  387. struct uio_device *dev;
  388. s32 event_count;
  389. };
  390. static int uio_open(struct inode *inode, struct file *filep)
  391. {
  392. struct uio_device *idev;
  393. struct uio_listener *listener;
  394. int ret = 0;
  395. mutex_lock(&minor_lock);
  396. idev = idr_find(&uio_idr, iminor(inode));
  397. mutex_unlock(&minor_lock);
  398. if (!idev) {
  399. ret = -ENODEV;
  400. goto out;
  401. }
  402. get_device(&idev->dev);
  403. if (!try_module_get(idev->owner)) {
  404. ret = -ENODEV;
  405. goto err_module_get;
  406. }
  407. listener = kmalloc(sizeof(*listener), GFP_KERNEL);
  408. if (!listener) {
  409. ret = -ENOMEM;
  410. goto err_alloc_listener;
  411. }
  412. listener->dev = idev;
  413. listener->event_count = atomic_read(&idev->event);
  414. filep->private_data = listener;
  415. mutex_lock(&idev->info_lock);
  416. if (!idev->info) {
  417. mutex_unlock(&idev->info_lock);
  418. ret = -EINVAL;
  419. goto err_alloc_listener;
  420. }
  421. if (idev->info && idev->info->open)
  422. ret = idev->info->open(idev->info, inode);
  423. mutex_unlock(&idev->info_lock);
  424. if (ret)
  425. goto err_infoopen;
  426. return 0;
  427. err_infoopen:
  428. kfree(listener);
  429. err_alloc_listener:
  430. module_put(idev->owner);
  431. err_module_get:
  432. put_device(&idev->dev);
  433. out:
  434. return ret;
  435. }
  436. static int uio_fasync(int fd, struct file *filep, int on)
  437. {
  438. struct uio_listener *listener = filep->private_data;
  439. struct uio_device *idev = listener->dev;
  440. return fasync_helper(fd, filep, on, &idev->async_queue);
  441. }
  442. static int uio_release(struct inode *inode, struct file *filep)
  443. {
  444. int ret = 0;
  445. struct uio_listener *listener = filep->private_data;
  446. struct uio_device *idev = listener->dev;
  447. mutex_lock(&idev->info_lock);
  448. if (idev->info && idev->info->release)
  449. ret = idev->info->release(idev->info, inode);
  450. mutex_unlock(&idev->info_lock);
  451. module_put(idev->owner);
  452. kfree(listener);
  453. put_device(&idev->dev);
  454. return ret;
  455. }
  456. static __poll_t uio_poll(struct file *filep, poll_table *wait)
  457. {
  458. struct uio_listener *listener = filep->private_data;
  459. struct uio_device *idev = listener->dev;
  460. __poll_t ret = 0;
  461. mutex_lock(&idev->info_lock);
  462. if (!idev->info || !idev->info->irq)
  463. ret = -EIO;
  464. mutex_unlock(&idev->info_lock);
  465. if (ret)
  466. return ret;
  467. poll_wait(filep, &idev->wait, wait);
  468. if (listener->event_count != atomic_read(&idev->event))
  469. return EPOLLIN | EPOLLRDNORM;
  470. return 0;
  471. }
  472. static ssize_t uio_read(struct file *filep, char __user *buf,
  473. size_t count, loff_t *ppos)
  474. {
  475. struct uio_listener *listener = filep->private_data;
  476. struct uio_device *idev = listener->dev;
  477. DECLARE_WAITQUEUE(wait, current);
  478. ssize_t retval = 0;
  479. s32 event_count;
  480. mutex_lock(&idev->info_lock);
  481. if (!idev->info || !idev->info->irq)
  482. retval = -EIO;
  483. mutex_unlock(&idev->info_lock);
  484. if (retval)
  485. return retval;
  486. if (count != sizeof(s32))
  487. return -EINVAL;
  488. add_wait_queue(&idev->wait, &wait);
  489. do {
  490. set_current_state(TASK_INTERRUPTIBLE);
  491. event_count = atomic_read(&idev->event);
  492. if (event_count != listener->event_count) {
  493. __set_current_state(TASK_RUNNING);
  494. if (copy_to_user(buf, &event_count, count))
  495. retval = -EFAULT;
  496. else {
  497. listener->event_count = event_count;
  498. retval = count;
  499. }
  500. break;
  501. }
  502. if (filep->f_flags & O_NONBLOCK) {
  503. retval = -EAGAIN;
  504. break;
  505. }
  506. if (signal_pending(current)) {
  507. retval = -ERESTARTSYS;
  508. break;
  509. }
  510. schedule();
  511. } while (1);
  512. __set_current_state(TASK_RUNNING);
  513. remove_wait_queue(&idev->wait, &wait);
  514. return retval;
  515. }
  516. static ssize_t uio_write(struct file *filep, const char __user *buf,
  517. size_t count, loff_t *ppos)
  518. {
  519. struct uio_listener *listener = filep->private_data;
  520. struct uio_device *idev = listener->dev;
  521. ssize_t retval;
  522. s32 irq_on;
  523. if (count != sizeof(s32))
  524. return -EINVAL;
  525. if (copy_from_user(&irq_on, buf, count))
  526. return -EFAULT;
  527. mutex_lock(&idev->info_lock);
  528. if (!idev->info) {
  529. retval = -EINVAL;
  530. goto out;
  531. }
  532. if (!idev->info || !idev->info->irq) {
  533. retval = -EIO;
  534. goto out;
  535. }
  536. if (!idev->info->irqcontrol) {
  537. retval = -ENOSYS;
  538. goto out;
  539. }
  540. retval = idev->info->irqcontrol(idev->info, irq_on);
  541. out:
  542. mutex_unlock(&idev->info_lock);
  543. return retval ? retval : sizeof(s32);
  544. }
  545. static int uio_find_mem_index(struct vm_area_struct *vma)
  546. {
  547. struct uio_device *idev = vma->vm_private_data;
  548. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  549. if (idev->info->mem[vma->vm_pgoff].size == 0)
  550. return -1;
  551. return (int)vma->vm_pgoff;
  552. }
  553. return -1;
  554. }
  555. static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
  556. {
  557. struct uio_device *idev = vmf->vma->vm_private_data;
  558. struct page *page;
  559. unsigned long offset;
  560. void *addr;
  561. vm_fault_t ret = 0;
  562. int mi;
  563. mutex_lock(&idev->info_lock);
  564. if (!idev->info) {
  565. ret = VM_FAULT_SIGBUS;
  566. goto out;
  567. }
  568. mi = uio_find_mem_index(vmf->vma);
  569. if (mi < 0) {
  570. ret = VM_FAULT_SIGBUS;
  571. goto out;
  572. }
  573. /*
  574. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  575. * to use mem[N].
  576. */
  577. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  578. addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
  579. if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
  580. page = virt_to_page(addr);
  581. else
  582. page = vmalloc_to_page(addr);
  583. get_page(page);
  584. vmf->page = page;
  585. out:
  586. mutex_unlock(&idev->info_lock);
  587. return ret;
  588. }
  589. static const struct vm_operations_struct uio_logical_vm_ops = {
  590. .fault = uio_vma_fault,
  591. };
  592. static int uio_mmap_logical(struct vm_area_struct *vma)
  593. {
  594. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  595. vma->vm_ops = &uio_logical_vm_ops;
  596. return 0;
  597. }
  598. static const struct vm_operations_struct uio_physical_vm_ops = {
  599. #ifdef CONFIG_HAVE_IOREMAP_PROT
  600. .access = generic_access_phys,
  601. #endif
  602. };
  603. static int uio_mmap_physical(struct vm_area_struct *vma)
  604. {
  605. struct uio_device *idev = vma->vm_private_data;
  606. int mi = uio_find_mem_index(vma);
  607. struct uio_mem *mem;
  608. if (mi < 0)
  609. return -EINVAL;
  610. mem = idev->info->mem + mi;
  611. if (mem->addr & ~PAGE_MASK)
  612. return -ENODEV;
  613. if (vma->vm_end - vma->vm_start > mem->size)
  614. return -EINVAL;
  615. vma->vm_ops = &uio_physical_vm_ops;
  616. if (idev->info->mem[mi].memtype == UIO_MEM_PHYS)
  617. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  618. /*
  619. * We cannot use the vm_iomap_memory() helper here,
  620. * because vma->vm_pgoff is the map index we looked
  621. * up above in uio_find_mem_index(), rather than an
  622. * actual page offset into the mmap.
  623. *
  624. * So we just do the physical mmap without a page
  625. * offset.
  626. */
  627. return remap_pfn_range(vma,
  628. vma->vm_start,
  629. mem->addr >> PAGE_SHIFT,
  630. vma->vm_end - vma->vm_start,
  631. vma->vm_page_prot);
  632. }
  633. static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
  634. {
  635. struct uio_listener *listener = filep->private_data;
  636. struct uio_device *idev = listener->dev;
  637. int mi;
  638. unsigned long requested_pages, actual_pages;
  639. int ret = 0;
  640. if (vma->vm_end < vma->vm_start)
  641. return -EINVAL;
  642. vma->vm_private_data = idev;
  643. mutex_lock(&idev->info_lock);
  644. if (!idev->info) {
  645. ret = -EINVAL;
  646. goto out;
  647. }
  648. mi = uio_find_mem_index(vma);
  649. if (mi < 0) {
  650. ret = -EINVAL;
  651. goto out;
  652. }
  653. requested_pages = vma_pages(vma);
  654. actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
  655. + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
  656. if (requested_pages > actual_pages) {
  657. ret = -EINVAL;
  658. goto out;
  659. }
  660. if (idev->info->mmap) {
  661. ret = idev->info->mmap(idev->info, vma);
  662. goto out;
  663. }
  664. switch (idev->info->mem[mi].memtype) {
  665. case UIO_MEM_IOVA:
  666. case UIO_MEM_PHYS:
  667. ret = uio_mmap_physical(vma);
  668. break;
  669. case UIO_MEM_LOGICAL:
  670. case UIO_MEM_VIRTUAL:
  671. ret = uio_mmap_logical(vma);
  672. break;
  673. default:
  674. ret = -EINVAL;
  675. }
  676. out:
  677. mutex_unlock(&idev->info_lock);
  678. return ret;
  679. }
  680. static const struct file_operations uio_fops = {
  681. .owner = THIS_MODULE,
  682. .open = uio_open,
  683. .release = uio_release,
  684. .read = uio_read,
  685. .write = uio_write,
  686. .mmap = uio_mmap,
  687. .poll = uio_poll,
  688. .fasync = uio_fasync,
  689. .llseek = noop_llseek,
  690. };
  691. static int uio_major_init(void)
  692. {
  693. static const char name[] = "uio";
  694. struct cdev *cdev = NULL;
  695. dev_t uio_dev = 0;
  696. int result;
  697. result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
  698. if (result)
  699. goto out;
  700. result = -ENOMEM;
  701. cdev = cdev_alloc();
  702. if (!cdev)
  703. goto out_unregister;
  704. cdev->owner = THIS_MODULE;
  705. cdev->ops = &uio_fops;
  706. kobject_set_name(&cdev->kobj, "%s", name);
  707. result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
  708. if (result)
  709. goto out_put;
  710. uio_major = MAJOR(uio_dev);
  711. uio_cdev = cdev;
  712. return 0;
  713. out_put:
  714. kobject_put(&cdev->kobj);
  715. out_unregister:
  716. unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
  717. out:
  718. return result;
  719. }
  720. static void uio_major_cleanup(void)
  721. {
  722. unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
  723. cdev_del(uio_cdev);
  724. }
  725. static int init_uio_class(void)
  726. {
  727. int ret;
  728. /* This is the first time in here, set everything up properly */
  729. ret = uio_major_init();
  730. if (ret)
  731. goto exit;
  732. ret = class_register(&uio_class);
  733. if (ret) {
  734. printk(KERN_ERR "class_register failed for uio\n");
  735. goto err_class_register;
  736. }
  737. uio_class_registered = true;
  738. return 0;
  739. err_class_register:
  740. uio_major_cleanup();
  741. exit:
  742. return ret;
  743. }
  744. static void release_uio_class(void)
  745. {
  746. uio_class_registered = false;
  747. class_unregister(&uio_class);
  748. uio_major_cleanup();
  749. }
  750. static void uio_device_release(struct device *dev)
  751. {
  752. struct uio_device *idev = dev_get_drvdata(dev);
  753. kfree(idev);
  754. }
  755. /**
  756. * uio_register_device - register a new userspace IO device
  757. * @owner: module that creates the new device
  758. * @parent: parent device
  759. * @info: UIO device capabilities
  760. *
  761. * returns zero on success or a negative error code.
  762. */
  763. int __uio_register_device(struct module *owner,
  764. struct device *parent,
  765. struct uio_info *info)
  766. {
  767. struct uio_device *idev;
  768. int ret = 0;
  769. if (!uio_class_registered)
  770. return -EPROBE_DEFER;
  771. if (!parent || !info || !info->name || !info->version)
  772. return -EINVAL;
  773. info->uio_dev = NULL;
  774. idev = kzalloc(sizeof(*idev), GFP_KERNEL);
  775. if (!idev) {
  776. return -ENOMEM;
  777. }
  778. idev->owner = owner;
  779. idev->info = info;
  780. mutex_init(&idev->info_lock);
  781. init_waitqueue_head(&idev->wait);
  782. atomic_set(&idev->event, 0);
  783. ret = uio_get_minor(idev);
  784. if (ret)
  785. return ret;
  786. idev->dev.devt = MKDEV(uio_major, idev->minor);
  787. idev->dev.class = &uio_class;
  788. idev->dev.parent = parent;
  789. idev->dev.release = uio_device_release;
  790. dev_set_drvdata(&idev->dev, idev);
  791. ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
  792. if (ret)
  793. goto err_device_create;
  794. ret = device_register(&idev->dev);
  795. if (ret)
  796. goto err_device_create;
  797. ret = uio_dev_add_attributes(idev);
  798. if (ret)
  799. goto err_uio_dev_add_attributes;
  800. info->uio_dev = idev;
  801. if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
  802. /*
  803. * Note that we deliberately don't use devm_request_irq
  804. * here. The parent module can unregister the UIO device
  805. * and call pci_disable_msi, which requires that this
  806. * irq has been freed. However, the device may have open
  807. * FDs at the time of unregister and therefore may not be
  808. * freed until they are released.
  809. */
  810. ret = request_irq(info->irq, uio_interrupt,
  811. info->irq_flags, info->name, idev);
  812. if (ret) {
  813. info->uio_dev = NULL;
  814. goto err_request_irq;
  815. }
  816. }
  817. return 0;
  818. err_request_irq:
  819. uio_dev_del_attributes(idev);
  820. err_uio_dev_add_attributes:
  821. device_unregister(&idev->dev);
  822. err_device_create:
  823. uio_free_minor(idev);
  824. return ret;
  825. }
  826. EXPORT_SYMBOL_GPL(__uio_register_device);
  827. /**
  828. * uio_unregister_device - unregister a industrial IO device
  829. * @info: UIO device capabilities
  830. *
  831. */
  832. void uio_unregister_device(struct uio_info *info)
  833. {
  834. struct uio_device *idev;
  835. if (!info || !info->uio_dev)
  836. return;
  837. idev = info->uio_dev;
  838. uio_free_minor(idev);
  839. mutex_lock(&idev->info_lock);
  840. uio_dev_del_attributes(idev);
  841. if (info->irq && info->irq != UIO_IRQ_CUSTOM)
  842. free_irq(info->irq, idev);
  843. idev->info = NULL;
  844. mutex_unlock(&idev->info_lock);
  845. device_unregister(&idev->dev);
  846. return;
  847. }
  848. EXPORT_SYMBOL_GPL(uio_unregister_device);
  849. static int __init uio_init(void)
  850. {
  851. return init_uio_class();
  852. }
  853. static void __exit uio_exit(void)
  854. {
  855. release_uio_class();
  856. idr_destroy(&uio_idr);
  857. }
  858. module_init(uio_init)
  859. module_exit(uio_exit)
  860. MODULE_LICENSE("GPL v2");