uio.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. /*
  227. * device functions
  228. */
  229. static int uio_dev_add_attributes(struct uio_device *idev)
  230. {
  231. int ret;
  232. int mi, pi;
  233. int map_found = 0;
  234. int portio_found = 0;
  235. struct uio_mem *mem;
  236. struct uio_map *map;
  237. struct uio_port *port;
  238. struct uio_portio *portio;
  239. for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
  240. mem = &idev->info->mem[mi];
  241. if (mem->size == 0)
  242. break;
  243. if (!map_found) {
  244. map_found = 1;
  245. idev->map_dir = kobject_create_and_add("maps",
  246. &idev->dev.kobj);
  247. if (!idev->map_dir) {
  248. ret = -ENOMEM;
  249. goto err_map;
  250. }
  251. }
  252. map = kzalloc(sizeof(*map), GFP_KERNEL);
  253. if (!map) {
  254. ret = -ENOMEM;
  255. goto err_map;
  256. }
  257. kobject_init(&map->kobj, &map_attr_type);
  258. map->mem = mem;
  259. mem->map = map;
  260. ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
  261. if (ret)
  262. goto err_map_kobj;
  263. ret = kobject_uevent(&map->kobj, KOBJ_ADD);
  264. if (ret)
  265. goto err_map_kobj;
  266. }
  267. for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
  268. port = &idev->info->port[pi];
  269. if (port->size == 0)
  270. break;
  271. if (!portio_found) {
  272. portio_found = 1;
  273. idev->portio_dir = kobject_create_and_add("portio",
  274. &idev->dev.kobj);
  275. if (!idev->portio_dir) {
  276. ret = -ENOMEM;
  277. goto err_portio;
  278. }
  279. }
  280. portio = kzalloc(sizeof(*portio), GFP_KERNEL);
  281. if (!portio) {
  282. ret = -ENOMEM;
  283. goto err_portio;
  284. }
  285. kobject_init(&portio->kobj, &portio_attr_type);
  286. portio->port = port;
  287. port->portio = portio;
  288. ret = kobject_add(&portio->kobj, idev->portio_dir,
  289. "port%d", pi);
  290. if (ret)
  291. goto err_portio_kobj;
  292. ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
  293. if (ret)
  294. goto err_portio_kobj;
  295. }
  296. return 0;
  297. err_portio:
  298. pi--;
  299. err_portio_kobj:
  300. for (; pi >= 0; pi--) {
  301. port = &idev->info->port[pi];
  302. portio = port->portio;
  303. kobject_put(&portio->kobj);
  304. }
  305. kobject_put(idev->portio_dir);
  306. err_map:
  307. mi--;
  308. err_map_kobj:
  309. for (; mi >= 0; mi--) {
  310. mem = &idev->info->mem[mi];
  311. map = mem->map;
  312. kobject_put(&map->kobj);
  313. }
  314. kobject_put(idev->map_dir);
  315. dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
  316. return ret;
  317. }
  318. static void uio_dev_del_attributes(struct uio_device *idev)
  319. {
  320. int i;
  321. struct uio_mem *mem;
  322. struct uio_port *port;
  323. for (i = 0; i < MAX_UIO_MAPS; i++) {
  324. mem = &idev->info->mem[i];
  325. if (mem->size == 0)
  326. break;
  327. kobject_put(&mem->map->kobj);
  328. }
  329. kobject_put(idev->map_dir);
  330. for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
  331. port = &idev->info->port[i];
  332. if (port->size == 0)
  333. break;
  334. kobject_put(&port->portio->kobj);
  335. }
  336. kobject_put(idev->portio_dir);
  337. }
  338. static int uio_get_minor(struct uio_device *idev)
  339. {
  340. int retval = -ENOMEM;
  341. mutex_lock(&minor_lock);
  342. retval = idr_alloc(&uio_idr, idev, 0, UIO_MAX_DEVICES, GFP_KERNEL);
  343. if (retval >= 0) {
  344. idev->minor = retval;
  345. retval = 0;
  346. } else if (retval == -ENOSPC) {
  347. dev_err(&idev->dev, "too many uio devices\n");
  348. retval = -EINVAL;
  349. }
  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;
  380. ret = idev->info->handler(irq, idev->info);
  381. if (ret == IRQ_HANDLED)
  382. uio_event_notify(idev->info);
  383. return ret;
  384. }
  385. struct uio_listener {
  386. struct uio_device *dev;
  387. s32 event_count;
  388. };
  389. static int uio_open(struct inode *inode, struct file *filep)
  390. {
  391. struct uio_device *idev;
  392. struct uio_listener *listener;
  393. int ret = 0;
  394. mutex_lock(&minor_lock);
  395. idev = idr_find(&uio_idr, iminor(inode));
  396. mutex_unlock(&minor_lock);
  397. if (!idev) {
  398. ret = -ENODEV;
  399. goto out;
  400. }
  401. get_device(&idev->dev);
  402. if (!try_module_get(idev->owner)) {
  403. ret = -ENODEV;
  404. goto err_module_get;
  405. }
  406. listener = kmalloc(sizeof(*listener), GFP_KERNEL);
  407. if (!listener) {
  408. ret = -ENOMEM;
  409. goto err_alloc_listener;
  410. }
  411. listener->dev = idev;
  412. listener->event_count = atomic_read(&idev->event);
  413. filep->private_data = listener;
  414. mutex_lock(&idev->info_lock);
  415. if (!idev->info) {
  416. mutex_unlock(&idev->info_lock);
  417. ret = -EINVAL;
  418. goto err_alloc_listener;
  419. }
  420. if (idev->info && idev->info->open)
  421. ret = idev->info->open(idev->info, inode);
  422. mutex_unlock(&idev->info_lock);
  423. if (ret)
  424. goto err_infoopen;
  425. return 0;
  426. err_infoopen:
  427. kfree(listener);
  428. err_alloc_listener:
  429. module_put(idev->owner);
  430. err_module_get:
  431. put_device(&idev->dev);
  432. out:
  433. return ret;
  434. }
  435. static int uio_fasync(int fd, struct file *filep, int on)
  436. {
  437. struct uio_listener *listener = filep->private_data;
  438. struct uio_device *idev = listener->dev;
  439. return fasync_helper(fd, filep, on, &idev->async_queue);
  440. }
  441. static int uio_release(struct inode *inode, struct file *filep)
  442. {
  443. int ret = 0;
  444. struct uio_listener *listener = filep->private_data;
  445. struct uio_device *idev = listener->dev;
  446. mutex_lock(&idev->info_lock);
  447. if (idev->info && idev->info->release)
  448. ret = idev->info->release(idev->info, inode);
  449. mutex_unlock(&idev->info_lock);
  450. module_put(idev->owner);
  451. kfree(listener);
  452. put_device(&idev->dev);
  453. return ret;
  454. }
  455. static __poll_t uio_poll(struct file *filep, poll_table *wait)
  456. {
  457. struct uio_listener *listener = filep->private_data;
  458. struct uio_device *idev = listener->dev;
  459. __poll_t ret = 0;
  460. mutex_lock(&idev->info_lock);
  461. if (!idev->info || !idev->info->irq)
  462. ret = -EIO;
  463. mutex_unlock(&idev->info_lock);
  464. if (ret)
  465. return ret;
  466. poll_wait(filep, &idev->wait, wait);
  467. if (listener->event_count != atomic_read(&idev->event))
  468. return EPOLLIN | EPOLLRDNORM;
  469. return 0;
  470. }
  471. static ssize_t uio_read(struct file *filep, char __user *buf,
  472. size_t count, loff_t *ppos)
  473. {
  474. struct uio_listener *listener = filep->private_data;
  475. struct uio_device *idev = listener->dev;
  476. DECLARE_WAITQUEUE(wait, current);
  477. ssize_t retval = 0;
  478. s32 event_count;
  479. mutex_lock(&idev->info_lock);
  480. if (!idev->info || !idev->info->irq)
  481. retval = -EIO;
  482. mutex_unlock(&idev->info_lock);
  483. if (retval)
  484. return retval;
  485. if (count != sizeof(s32))
  486. return -EINVAL;
  487. add_wait_queue(&idev->wait, &wait);
  488. do {
  489. set_current_state(TASK_INTERRUPTIBLE);
  490. event_count = atomic_read(&idev->event);
  491. if (event_count != listener->event_count) {
  492. __set_current_state(TASK_RUNNING);
  493. if (copy_to_user(buf, &event_count, count))
  494. retval = -EFAULT;
  495. else {
  496. listener->event_count = event_count;
  497. retval = count;
  498. }
  499. break;
  500. }
  501. if (filep->f_flags & O_NONBLOCK) {
  502. retval = -EAGAIN;
  503. break;
  504. }
  505. if (signal_pending(current)) {
  506. retval = -ERESTARTSYS;
  507. break;
  508. }
  509. schedule();
  510. } while (1);
  511. __set_current_state(TASK_RUNNING);
  512. remove_wait_queue(&idev->wait, &wait);
  513. return retval;
  514. }
  515. static ssize_t uio_write(struct file *filep, const char __user *buf,
  516. size_t count, loff_t *ppos)
  517. {
  518. struct uio_listener *listener = filep->private_data;
  519. struct uio_device *idev = listener->dev;
  520. ssize_t retval;
  521. s32 irq_on;
  522. if (count != sizeof(s32))
  523. return -EINVAL;
  524. if (copy_from_user(&irq_on, buf, count))
  525. return -EFAULT;
  526. mutex_lock(&idev->info_lock);
  527. if (!idev->info) {
  528. retval = -EINVAL;
  529. goto out;
  530. }
  531. if (!idev->info || !idev->info->irq) {
  532. retval = -EIO;
  533. goto out;
  534. }
  535. if (!idev->info->irqcontrol) {
  536. retval = -ENOSYS;
  537. goto out;
  538. }
  539. retval = idev->info->irqcontrol(idev->info, irq_on);
  540. out:
  541. mutex_unlock(&idev->info_lock);
  542. return retval ? retval : sizeof(s32);
  543. }
  544. static int uio_find_mem_index(struct vm_area_struct *vma)
  545. {
  546. struct uio_device *idev = vma->vm_private_data;
  547. if (vma->vm_pgoff < MAX_UIO_MAPS) {
  548. if (idev->info->mem[vma->vm_pgoff].size == 0)
  549. return -1;
  550. return (int)vma->vm_pgoff;
  551. }
  552. return -1;
  553. }
  554. static vm_fault_t uio_vma_fault(struct vm_fault *vmf)
  555. {
  556. struct uio_device *idev = vmf->vma->vm_private_data;
  557. struct page *page;
  558. unsigned long offset;
  559. void *addr;
  560. int ret = 0;
  561. int mi;
  562. mutex_lock(&idev->info_lock);
  563. if (!idev->info) {
  564. ret = VM_FAULT_SIGBUS;
  565. goto out;
  566. }
  567. mi = uio_find_mem_index(vmf->vma);
  568. if (mi < 0) {
  569. ret = VM_FAULT_SIGBUS;
  570. goto out;
  571. }
  572. /*
  573. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  574. * to use mem[N].
  575. */
  576. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  577. addr = (void *)(unsigned long)idev->info->mem[mi].addr + offset;
  578. if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
  579. page = virt_to_page(addr);
  580. else
  581. page = vmalloc_to_page(addr);
  582. get_page(page);
  583. vmf->page = page;
  584. out:
  585. mutex_unlock(&idev->info_lock);
  586. return ret;
  587. }
  588. static const struct vm_operations_struct uio_logical_vm_ops = {
  589. .fault = uio_vma_fault,
  590. };
  591. static int uio_mmap_logical(struct vm_area_struct *vma)
  592. {
  593. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  594. vma->vm_ops = &uio_logical_vm_ops;
  595. return 0;
  596. }
  597. static const struct vm_operations_struct uio_physical_vm_ops = {
  598. #ifdef CONFIG_HAVE_IOREMAP_PROT
  599. .access = generic_access_phys,
  600. #endif
  601. };
  602. static int uio_mmap_physical(struct vm_area_struct *vma)
  603. {
  604. struct uio_device *idev = vma->vm_private_data;
  605. int mi = uio_find_mem_index(vma);
  606. struct uio_mem *mem;
  607. if (mi < 0)
  608. return -EINVAL;
  609. mem = idev->info->mem + mi;
  610. if (mem->addr & ~PAGE_MASK)
  611. return -ENODEV;
  612. if (vma->vm_end - vma->vm_start > mem->size)
  613. return -EINVAL;
  614. vma->vm_ops = &uio_physical_vm_ops;
  615. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  616. /*
  617. * We cannot use the vm_iomap_memory() helper here,
  618. * because vma->vm_pgoff is the map index we looked
  619. * up above in uio_find_mem_index(), rather than an
  620. * actual page offset into the mmap.
  621. *
  622. * So we just do the physical mmap without a page
  623. * offset.
  624. */
  625. return remap_pfn_range(vma,
  626. vma->vm_start,
  627. mem->addr >> PAGE_SHIFT,
  628. vma->vm_end - vma->vm_start,
  629. vma->vm_page_prot);
  630. }
  631. static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
  632. {
  633. struct uio_listener *listener = filep->private_data;
  634. struct uio_device *idev = listener->dev;
  635. int mi;
  636. unsigned long requested_pages, actual_pages;
  637. int ret = 0;
  638. if (vma->vm_end < vma->vm_start)
  639. return -EINVAL;
  640. vma->vm_private_data = idev;
  641. mutex_lock(&idev->info_lock);
  642. if (!idev->info) {
  643. ret = -EINVAL;
  644. goto out;
  645. }
  646. mi = uio_find_mem_index(vma);
  647. if (mi < 0) {
  648. ret = -EINVAL;
  649. goto out;
  650. }
  651. requested_pages = vma_pages(vma);
  652. actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
  653. + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
  654. if (requested_pages > actual_pages) {
  655. ret = -EINVAL;
  656. goto out;
  657. }
  658. if (idev->info->mmap) {
  659. ret = idev->info->mmap(idev->info, vma);
  660. goto out;
  661. }
  662. switch (idev->info->mem[mi].memtype) {
  663. case UIO_MEM_PHYS:
  664. ret = uio_mmap_physical(vma);
  665. break;
  666. case UIO_MEM_LOGICAL:
  667. case UIO_MEM_VIRTUAL:
  668. ret = uio_mmap_logical(vma);
  669. break;
  670. default:
  671. ret = -EINVAL;
  672. }
  673. out:
  674. mutex_unlock(&idev->info_lock);
  675. return ret;
  676. }
  677. static const struct file_operations uio_fops = {
  678. .owner = THIS_MODULE,
  679. .open = uio_open,
  680. .release = uio_release,
  681. .read = uio_read,
  682. .write = uio_write,
  683. .mmap = uio_mmap,
  684. .poll = uio_poll,
  685. .fasync = uio_fasync,
  686. .llseek = noop_llseek,
  687. };
  688. static int uio_major_init(void)
  689. {
  690. static const char name[] = "uio";
  691. struct cdev *cdev = NULL;
  692. dev_t uio_dev = 0;
  693. int result;
  694. result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
  695. if (result)
  696. goto out;
  697. result = -ENOMEM;
  698. cdev = cdev_alloc();
  699. if (!cdev)
  700. goto out_unregister;
  701. cdev->owner = THIS_MODULE;
  702. cdev->ops = &uio_fops;
  703. kobject_set_name(&cdev->kobj, "%s", name);
  704. result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
  705. if (result)
  706. goto out_put;
  707. uio_major = MAJOR(uio_dev);
  708. uio_cdev = cdev;
  709. return 0;
  710. out_put:
  711. kobject_put(&cdev->kobj);
  712. out_unregister:
  713. unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
  714. out:
  715. return result;
  716. }
  717. static void uio_major_cleanup(void)
  718. {
  719. unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
  720. cdev_del(uio_cdev);
  721. }
  722. static int init_uio_class(void)
  723. {
  724. int ret;
  725. /* This is the first time in here, set everything up properly */
  726. ret = uio_major_init();
  727. if (ret)
  728. goto exit;
  729. ret = class_register(&uio_class);
  730. if (ret) {
  731. printk(KERN_ERR "class_register failed for uio\n");
  732. goto err_class_register;
  733. }
  734. return 0;
  735. err_class_register:
  736. uio_major_cleanup();
  737. exit:
  738. return ret;
  739. }
  740. static void release_uio_class(void)
  741. {
  742. class_unregister(&uio_class);
  743. uio_major_cleanup();
  744. }
  745. static void uio_device_release(struct device *dev)
  746. {
  747. struct uio_device *idev = dev_get_drvdata(dev);
  748. kfree(idev);
  749. }
  750. /**
  751. * uio_register_device - register a new userspace IO device
  752. * @owner: module that creates the new device
  753. * @parent: parent device
  754. * @info: UIO device capabilities
  755. *
  756. * returns zero on success or a negative error code.
  757. */
  758. int __uio_register_device(struct module *owner,
  759. struct device *parent,
  760. struct uio_info *info)
  761. {
  762. struct uio_device *idev;
  763. int ret = 0;
  764. if (!parent || !info || !info->name || !info->version)
  765. return -EINVAL;
  766. info->uio_dev = NULL;
  767. idev = kzalloc(sizeof(*idev), GFP_KERNEL);
  768. if (!idev) {
  769. return -ENOMEM;
  770. }
  771. idev->owner = owner;
  772. idev->info = info;
  773. mutex_init(&idev->info_lock);
  774. init_waitqueue_head(&idev->wait);
  775. atomic_set(&idev->event, 0);
  776. ret = uio_get_minor(idev);
  777. if (ret)
  778. return ret;
  779. idev->dev.devt = MKDEV(uio_major, idev->minor);
  780. idev->dev.class = &uio_class;
  781. idev->dev.parent = parent;
  782. idev->dev.release = uio_device_release;
  783. dev_set_drvdata(&idev->dev, idev);
  784. ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
  785. if (ret)
  786. goto err_device_create;
  787. ret = device_register(&idev->dev);
  788. if (ret)
  789. goto err_device_create;
  790. ret = uio_dev_add_attributes(idev);
  791. if (ret)
  792. goto err_uio_dev_add_attributes;
  793. if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
  794. /*
  795. * Note that we deliberately don't use devm_request_irq
  796. * here. The parent module can unregister the UIO device
  797. * and call pci_disable_msi, which requires that this
  798. * irq has been freed. However, the device may have open
  799. * FDs at the time of unregister and therefore may not be
  800. * freed until they are released.
  801. */
  802. ret = request_irq(info->irq, uio_interrupt,
  803. info->irq_flags, info->name, idev);
  804. if (ret)
  805. goto err_request_irq;
  806. }
  807. info->uio_dev = idev;
  808. return 0;
  809. err_request_irq:
  810. uio_dev_del_attributes(idev);
  811. err_uio_dev_add_attributes:
  812. device_unregister(&idev->dev);
  813. err_device_create:
  814. uio_free_minor(idev);
  815. return ret;
  816. }
  817. EXPORT_SYMBOL_GPL(__uio_register_device);
  818. /**
  819. * uio_unregister_device - unregister a industrial IO device
  820. * @info: UIO device capabilities
  821. *
  822. */
  823. void uio_unregister_device(struct uio_info *info)
  824. {
  825. struct uio_device *idev;
  826. if (!info || !info->uio_dev)
  827. return;
  828. idev = info->uio_dev;
  829. uio_free_minor(idev);
  830. mutex_lock(&idev->info_lock);
  831. uio_dev_del_attributes(idev);
  832. if (info->irq && info->irq != UIO_IRQ_CUSTOM)
  833. free_irq(info->irq, idev);
  834. idev->info = NULL;
  835. mutex_unlock(&idev->info_lock);
  836. device_unregister(&idev->dev);
  837. return;
  838. }
  839. EXPORT_SYMBOL_GPL(uio_unregister_device);
  840. static int __init uio_init(void)
  841. {
  842. return init_uio_class();
  843. }
  844. static void __exit uio_exit(void)
  845. {
  846. release_uio_class();
  847. idr_destroy(&uio_idr);
  848. }
  849. module_init(uio_init)
  850. module_exit(uio_exit)
  851. MODULE_LICENSE("GPL v2");