uio.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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. 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(unsigned long minor)
  355. {
  356. mutex_lock(&minor_lock);
  357. idr_remove(&uio_idr, 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. int 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. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  617. /*
  618. * We cannot use the vm_iomap_memory() helper here,
  619. * because vma->vm_pgoff is the map index we looked
  620. * up above in uio_find_mem_index(), rather than an
  621. * actual page offset into the mmap.
  622. *
  623. * So we just do the physical mmap without a page
  624. * offset.
  625. */
  626. return remap_pfn_range(vma,
  627. vma->vm_start,
  628. mem->addr >> PAGE_SHIFT,
  629. vma->vm_end - vma->vm_start,
  630. vma->vm_page_prot);
  631. }
  632. static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
  633. {
  634. struct uio_listener *listener = filep->private_data;
  635. struct uio_device *idev = listener->dev;
  636. int mi;
  637. unsigned long requested_pages, actual_pages;
  638. int ret = 0;
  639. if (vma->vm_end < vma->vm_start)
  640. return -EINVAL;
  641. vma->vm_private_data = idev;
  642. mutex_lock(&idev->info_lock);
  643. if (!idev->info) {
  644. ret = -EINVAL;
  645. goto out;
  646. }
  647. mi = uio_find_mem_index(vma);
  648. if (mi < 0) {
  649. ret = -EINVAL;
  650. goto out;
  651. }
  652. requested_pages = vma_pages(vma);
  653. actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
  654. + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
  655. if (requested_pages > actual_pages) {
  656. ret = -EINVAL;
  657. goto out;
  658. }
  659. if (idev->info->mmap) {
  660. ret = idev->info->mmap(idev->info, vma);
  661. goto out;
  662. }
  663. switch (idev->info->mem[mi].memtype) {
  664. case UIO_MEM_PHYS:
  665. ret = uio_mmap_physical(vma);
  666. break;
  667. case UIO_MEM_LOGICAL:
  668. case UIO_MEM_VIRTUAL:
  669. ret = uio_mmap_logical(vma);
  670. break;
  671. default:
  672. ret = -EINVAL;
  673. }
  674. out:
  675. mutex_unlock(&idev->info_lock);
  676. return ret;
  677. }
  678. static const struct file_operations uio_fops = {
  679. .owner = THIS_MODULE,
  680. .open = uio_open,
  681. .release = uio_release,
  682. .read = uio_read,
  683. .write = uio_write,
  684. .mmap = uio_mmap,
  685. .poll = uio_poll,
  686. .fasync = uio_fasync,
  687. .llseek = noop_llseek,
  688. };
  689. static int uio_major_init(void)
  690. {
  691. static const char name[] = "uio";
  692. struct cdev *cdev = NULL;
  693. dev_t uio_dev = 0;
  694. int result;
  695. result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
  696. if (result)
  697. goto out;
  698. result = -ENOMEM;
  699. cdev = cdev_alloc();
  700. if (!cdev)
  701. goto out_unregister;
  702. cdev->owner = THIS_MODULE;
  703. cdev->ops = &uio_fops;
  704. kobject_set_name(&cdev->kobj, "%s", name);
  705. result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
  706. if (result)
  707. goto out_put;
  708. uio_major = MAJOR(uio_dev);
  709. uio_cdev = cdev;
  710. return 0;
  711. out_put:
  712. kobject_put(&cdev->kobj);
  713. out_unregister:
  714. unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
  715. out:
  716. return result;
  717. }
  718. static void uio_major_cleanup(void)
  719. {
  720. unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
  721. cdev_del(uio_cdev);
  722. }
  723. static int init_uio_class(void)
  724. {
  725. int ret;
  726. /* This is the first time in here, set everything up properly */
  727. ret = uio_major_init();
  728. if (ret)
  729. goto exit;
  730. ret = class_register(&uio_class);
  731. if (ret) {
  732. printk(KERN_ERR "class_register failed for uio\n");
  733. goto err_class_register;
  734. }
  735. uio_class_registered = true;
  736. return 0;
  737. err_class_register:
  738. uio_major_cleanup();
  739. exit:
  740. return ret;
  741. }
  742. static void release_uio_class(void)
  743. {
  744. uio_class_registered = false;
  745. class_unregister(&uio_class);
  746. uio_major_cleanup();
  747. }
  748. static void uio_device_release(struct device *dev)
  749. {
  750. struct uio_device *idev = dev_get_drvdata(dev);
  751. kfree(idev);
  752. }
  753. /**
  754. * uio_register_device - register a new userspace IO device
  755. * @owner: module that creates the new device
  756. * @parent: parent device
  757. * @info: UIO device capabilities
  758. *
  759. * returns zero on success or a negative error code.
  760. */
  761. int __uio_register_device(struct module *owner,
  762. struct device *parent,
  763. struct uio_info *info)
  764. {
  765. struct uio_device *idev;
  766. int ret = 0;
  767. if (!uio_class_registered)
  768. return -EPROBE_DEFER;
  769. if (!parent || !info || !info->name || !info->version)
  770. return -EINVAL;
  771. info->uio_dev = NULL;
  772. idev = kzalloc(sizeof(*idev), GFP_KERNEL);
  773. if (!idev) {
  774. return -ENOMEM;
  775. }
  776. idev->owner = owner;
  777. idev->info = info;
  778. mutex_init(&idev->info_lock);
  779. init_waitqueue_head(&idev->wait);
  780. atomic_set(&idev->event, 0);
  781. ret = uio_get_minor(idev);
  782. if (ret) {
  783. kfree(idev);
  784. return ret;
  785. }
  786. device_initialize(&idev->dev);
  787. idev->dev.devt = MKDEV(uio_major, idev->minor);
  788. idev->dev.class = &uio_class;
  789. idev->dev.parent = parent;
  790. idev->dev.release = uio_device_release;
  791. dev_set_drvdata(&idev->dev, idev);
  792. ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
  793. if (ret)
  794. goto err_device_create;
  795. ret = device_add(&idev->dev);
  796. if (ret)
  797. goto err_device_create;
  798. ret = uio_dev_add_attributes(idev);
  799. if (ret)
  800. goto err_uio_dev_add_attributes;
  801. info->uio_dev = idev;
  802. if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
  803. /*
  804. * Note that we deliberately don't use devm_request_irq
  805. * here. The parent module can unregister the UIO device
  806. * and call pci_disable_msi, which requires that this
  807. * irq has been freed. However, the device may have open
  808. * FDs at the time of unregister and therefore may not be
  809. * freed until they are released.
  810. */
  811. ret = request_irq(info->irq, uio_interrupt,
  812. info->irq_flags, info->name, idev);
  813. if (ret) {
  814. info->uio_dev = NULL;
  815. goto err_request_irq;
  816. }
  817. }
  818. return 0;
  819. err_request_irq:
  820. uio_dev_del_attributes(idev);
  821. err_uio_dev_add_attributes:
  822. device_del(&idev->dev);
  823. err_device_create:
  824. uio_free_minor(idev->minor);
  825. put_device(&idev->dev);
  826. return ret;
  827. }
  828. EXPORT_SYMBOL_GPL(__uio_register_device);
  829. /**
  830. * uio_unregister_device - unregister a industrial IO device
  831. * @info: UIO device capabilities
  832. *
  833. */
  834. void uio_unregister_device(struct uio_info *info)
  835. {
  836. struct uio_device *idev;
  837. unsigned long minor;
  838. if (!info || !info->uio_dev)
  839. return;
  840. idev = info->uio_dev;
  841. minor = idev->minor;
  842. mutex_lock(&idev->info_lock);
  843. uio_dev_del_attributes(idev);
  844. if (info->irq && info->irq != UIO_IRQ_CUSTOM)
  845. free_irq(info->irq, idev);
  846. idev->info = NULL;
  847. mutex_unlock(&idev->info_lock);
  848. device_unregister(&idev->dev);
  849. uio_free_minor(minor);
  850. return;
  851. }
  852. EXPORT_SYMBOL_GPL(uio_unregister_device);
  853. static int __init uio_init(void)
  854. {
  855. return init_uio_class();
  856. }
  857. static void __exit uio_exit(void)
  858. {
  859. release_uio_class();
  860. idr_destroy(&uio_idr);
  861. }
  862. module_init(uio_init)
  863. module_exit(uio_exit)
  864. MODULE_LICENSE("GPL v2");