uio.c 23 KB

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