uio.c 24 KB

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