exm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * drivers/uio/uio.c
  3. *
  4. * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
  5. * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
  6. * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de>
  7. * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
  8. *
  9. * Userspace IO
  10. *
  11. * Base Functions
  12. *
  13. * Licensed under the GPLv2 only.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/poll.h>
  18. #include <linux/device.h>
  19. #include <linux/slab.h>
  20. #include <linux/mm.h>
  21. #include <linux/idr.h>
  22. #include <linux/sched.h>
  23. #include <linux/string.h>
  24. #include <linux/kobject.h>
  25. #include <linux/cdev.h>
  26. #include <linux/exm_driver.h>
  27. #define EXM_MAX_DEVICES (1U << MINORBITS)
  28. struct exm_device {
  29. struct module *owner;
  30. struct device *dev;
  31. int minor;
  32. atomic_t event;
  33. struct fasync_struct *async_queue;
  34. wait_queue_head_t wait;
  35. int vma_count;
  36. struct exm_info *info;
  37. struct kobject *map_dir;
  38. struct kobject *portio_dir;
  39. };
  40. static int exm_major;
  41. static struct cdev *exm_cdev;
  42. static DEFINE_IDR(exm_idr);
  43. static const struct file_operations exm_fops;
  44. /* Protect idr accesses */
  45. static DEFINE_MUTEX(minor_lock);
  46. /*
  47. * attributes
  48. */
  49. struct exm_map {
  50. struct kobject kobj;
  51. struct exm_mem *mem;
  52. };
  53. #define to_map(map) container_of(map, struct exm_map, kobj)
  54. static ssize_t map_name_show(struct exm_mem *mem, char *buf)
  55. {
  56. if (unlikely(!mem->name))
  57. mem->name = "";
  58. return sprintf(buf, "%s\n", mem->name);
  59. }
  60. static ssize_t map_addr_show(struct exm_mem *mem, char *buf)
  61. {
  62. return sprintf(buf, "0x%llx\n", (unsigned long long)mem->addr);
  63. }
  64. static ssize_t map_size_show(struct exm_mem *mem, char *buf)
  65. {
  66. return sprintf(buf, "0x%lx\n", mem->size);
  67. }
  68. static ssize_t map_offset_show(struct exm_mem *mem, char *buf)
  69. {
  70. return sprintf(buf, "0x%llx\n", (unsigned long long)mem->addr & ~PAGE_MASK);
  71. }
  72. struct map_sysfs_entry {
  73. struct attribute attr;
  74. ssize_t (*show)(struct exm_mem *, char *);
  75. ssize_t (*store)(struct exm_mem *, const char *, size_t);
  76. };
  77. static struct map_sysfs_entry name_attribute =
  78. __ATTR(name, S_IRUGO, map_name_show, NULL);
  79. static struct map_sysfs_entry addr_attribute =
  80. __ATTR(addr, S_IRUGO, map_addr_show, NULL);
  81. static struct map_sysfs_entry size_attribute =
  82. __ATTR(size, S_IRUGO, map_size_show, NULL);
  83. static struct map_sysfs_entry offset_attribute =
  84. __ATTR(offset, S_IRUGO, map_offset_show, NULL);
  85. static struct attribute *attrs[] = {
  86. &name_attribute.attr,
  87. &addr_attribute.attr,
  88. &size_attribute.attr,
  89. &offset_attribute.attr,
  90. NULL, /* need to NULL terminate the list of attributes */
  91. };
  92. static void map_release(struct kobject *kobj)
  93. {
  94. struct exm_map *map = to_map(kobj);
  95. kfree(map);
  96. }
  97. static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
  98. char *buf)
  99. {
  100. struct exm_map *map = to_map(kobj);
  101. struct exm_mem *mem = map->mem;
  102. struct map_sysfs_entry *entry;
  103. entry = container_of(attr, struct map_sysfs_entry, attr);
  104. if (!entry->show)
  105. return -EIO;
  106. return entry->show(mem, buf);
  107. }
  108. static const struct sysfs_ops map_sysfs_ops = {
  109. .show = map_type_show,
  110. };
  111. static struct kobj_type map_attr_type = {
  112. .release = map_release,
  113. .sysfs_ops = &map_sysfs_ops,
  114. .default_attrs = attrs,
  115. };
  116. struct exm_portio {
  117. struct kobject kobj;
  118. struct exm_port *port;
  119. };
  120. #define to_portio(portio) container_of(portio, struct exm_portio, kobj)
  121. static ssize_t portio_name_show(struct exm_port *port, char *buf)
  122. {
  123. if (unlikely(!port->name))
  124. port->name = "";
  125. return sprintf(buf, "%s\n", port->name);
  126. }
  127. static ssize_t portio_start_show(struct exm_port *port, char *buf)
  128. {
  129. return sprintf(buf, "0x%lx\n", port->start);
  130. }
  131. static ssize_t portio_size_show(struct exm_port *port, char *buf)
  132. {
  133. return sprintf(buf, "0x%lx\n", port->size);
  134. }
  135. static ssize_t portio_porttype_show(struct exm_port *port, char *buf)
  136. {
  137. const char *porttypes[4] = {"none", "x86", "gpio", "other"};
  138. if ((port->porttype < 0) || (port->porttype > EXM_PORT_OTHER))
  139. return -EINVAL;
  140. return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
  141. }
  142. struct portio_sysfs_entry {
  143. struct attribute attr;
  144. ssize_t (*show)(struct exm_port *, char *);
  145. ssize_t (*store)(struct exm_port *, const char *, size_t);
  146. };
  147. static struct portio_sysfs_entry portio_name_attribute =
  148. __ATTR(name, S_IRUGO, portio_name_show, NULL);
  149. static struct portio_sysfs_entry portio_start_attribute =
  150. __ATTR(start, S_IRUGO, portio_start_show, NULL);
  151. static struct portio_sysfs_entry portio_size_attribute =
  152. __ATTR(size, S_IRUGO, portio_size_show, NULL);
  153. static struct portio_sysfs_entry portio_porttype_attribute =
  154. __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
  155. static struct attribute *portio_attrs[] = {
  156. &portio_name_attribute.attr,
  157. &portio_start_attribute.attr,
  158. &portio_size_attribute.attr,
  159. &portio_porttype_attribute.attr,
  160. NULL,
  161. };
  162. static void portio_release(struct kobject *kobj)
  163. {
  164. struct exm_portio *portio = to_portio(kobj);
  165. kfree(portio);
  166. }
  167. static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
  168. char *buf)
  169. {
  170. struct exm_portio *portio = to_portio(kobj);
  171. struct exm_port *port = portio->port;
  172. struct portio_sysfs_entry *entry;
  173. entry = container_of(attr, struct portio_sysfs_entry, attr);
  174. if (!entry->show)
  175. return -EIO;
  176. return entry->show(port, buf);
  177. }
  178. static const struct sysfs_ops portio_sysfs_ops = {
  179. .show = portio_type_show,
  180. };
  181. static struct kobj_type portio_attr_type = {
  182. .release = portio_release,
  183. .sysfs_ops = &portio_sysfs_ops,
  184. .default_attrs = portio_attrs,
  185. };
  186. static ssize_t name_show(struct device *dev,
  187. struct device_attribute *attr, char *buf)
  188. {
  189. struct exm_device *idev = dev_get_drvdata(dev);
  190. return sprintf(buf, "%s\n", idev->info->name);
  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 exm_device *idev = dev_get_drvdata(dev);
  197. return sprintf(buf, "%s\n", idev->info->version);
  198. }
  199. static DEVICE_ATTR_RO(version);
  200. static ssize_t event_show(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. struct exm_device *idev = dev_get_drvdata(dev);
  204. return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
  205. }
  206. static DEVICE_ATTR_RO(event);
  207. static struct attribute *exm_attrs[] = {
  208. &dev_attr_name.attr,
  209. &dev_attr_version.attr,
  210. &dev_attr_event.attr,
  211. NULL,
  212. };
  213. ATTRIBUTE_GROUPS(exm);
  214. /* EXM class infrastructure */
  215. static struct class exm_class = {
  216. .name = "exm",
  217. .dev_groups = exm_groups,
  218. };
  219. /*
  220. * device functions
  221. */
  222. static int exm_dev_add_attributes(struct exm_device *idev)
  223. {
  224. int ret;
  225. int mi, pi;
  226. int map_found = 0;
  227. int portio_found = 0;
  228. struct exm_mem *mem;
  229. struct exm_map *map;
  230. struct exm_port *port;
  231. struct exm_portio *portio;
  232. for (mi = 0; mi < MAX_EXM_MAPS; mi++) {
  233. mem = &idev->info->mem[mi];
  234. if (mem->size == 0)
  235. break;
  236. if (!map_found) {
  237. map_found = 1;
  238. idev->map_dir = kobject_create_and_add("maps",
  239. &idev->dev->kobj);
  240. if (!idev->map_dir)
  241. goto err_map;
  242. }
  243. map = kzalloc(sizeof(*map), GFP_KERNEL);
  244. if (!map)
  245. goto err_map_kobj;
  246. kobject_init(&map->kobj, &map_attr_type);
  247. map->mem = mem;
  248. mem->map = map;
  249. ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
  250. if (ret)
  251. goto err_map_kobj;
  252. ret = kobject_uevent(&map->kobj, KOBJ_ADD);
  253. if (ret)
  254. goto err_map;
  255. }
  256. for (pi = 0; pi < MAX_EXM_PORT_REGIONS; pi++) {
  257. port = &idev->info->port[pi];
  258. if (port->size == 0)
  259. break;
  260. if (!portio_found) {
  261. portio_found = 1;
  262. idev->portio_dir = kobject_create_and_add("portio",
  263. &idev->dev->kobj);
  264. if (!idev->portio_dir)
  265. goto err_portio;
  266. }
  267. portio = kzalloc(sizeof(*portio), GFP_KERNEL);
  268. if (!portio)
  269. goto err_portio_kobj;
  270. kobject_init(&portio->kobj, &portio_attr_type);
  271. portio->port = port;
  272. port->portio = portio;
  273. ret = kobject_add(&portio->kobj, idev->portio_dir,
  274. "port%d", pi);
  275. if (ret)
  276. goto err_portio_kobj;
  277. ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
  278. if (ret)
  279. goto err_portio;
  280. }
  281. return 0;
  282. err_portio:
  283. pi--;
  284. err_portio_kobj:
  285. for (; pi >= 0; pi--) {
  286. port = &idev->info->port[pi];
  287. portio = port->portio;
  288. kobject_put(&portio->kobj);
  289. }
  290. kobject_put(idev->portio_dir);
  291. err_map:
  292. mi--;
  293. err_map_kobj:
  294. for (; mi >= 0; mi--) {
  295. mem = &idev->info->mem[mi];
  296. map = mem->map;
  297. kobject_put(&map->kobj);
  298. }
  299. kobject_put(idev->map_dir);
  300. dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
  301. return ret;
  302. }
  303. static void exm_dev_del_attributes(struct exm_device *idev)
  304. {
  305. int i;
  306. struct exm_mem *mem;
  307. struct exm_port *port;
  308. for (i = 0; i < MAX_EXM_MAPS; i++) {
  309. mem = &idev->info->mem[i];
  310. if (mem->size == 0)
  311. break;
  312. kobject_put(&mem->map->kobj);
  313. }
  314. kobject_put(idev->map_dir);
  315. for (i = 0; i < MAX_EXM_PORT_REGIONS; i++) {
  316. port = &idev->info->port[i];
  317. if (port->size == 0)
  318. break;
  319. kobject_put(&port->portio->kobj);
  320. }
  321. kobject_put(idev->portio_dir);
  322. }
  323. static int exm_get_minor(struct exm_device *idev)
  324. {
  325. int retval = -ENOMEM;
  326. mutex_lock(&minor_lock);
  327. retval = idr_alloc(&exm_idr, idev, 0, EXM_MAX_DEVICES, GFP_KERNEL);
  328. if (retval >= 0) {
  329. idev->minor = retval;
  330. retval = 0;
  331. } else if (retval == -ENOSPC) {
  332. dev_err(idev->dev, "too many uio devices\n");
  333. retval = -EINVAL;
  334. }
  335. mutex_unlock(&minor_lock);
  336. return retval;
  337. }
  338. static void exm_free_minor(struct exm_device *idev)
  339. {
  340. mutex_lock(&minor_lock);
  341. idr_remove(&exm_idr, idev->minor);
  342. mutex_unlock(&minor_lock);
  343. }
  344. /**
  345. * exm_event_notify - trigger an interrupt event
  346. * @info: EXM device capabilities
  347. */
  348. void exm_event_notify(struct exm_info *info)
  349. {
  350. struct exm_device *idev = info->exm_dev;
  351. atomic_inc(&idev->event);
  352. wake_up_interruptible(&idev->wait);
  353. kill_fasync(&idev->async_queue, SIGIO, POLL_IN);
  354. }
  355. EXPORT_SYMBOL_GPL(exm_event_notify);
  356. /**
  357. * exm_interrupt - hardware interrupt handler
  358. * @irq: IRQ number, can be EXM_IRQ_CYCLIC for cyclic timer
  359. * @dev_id: Pointer to the devices exm_device structure
  360. */
  361. static irqreturn_t exm_interrupt(int irq, void *dev_id)
  362. {
  363. struct exm_device *idev = (struct exm_device *)dev_id;
  364. irqreturn_t ret = idev->info->handler(irq, idev->info);
  365. if (ret == IRQ_HANDLED)
  366. exm_event_notify(idev->info);
  367. return ret;
  368. }
  369. struct exm_listener {
  370. struct exm_device *dev;
  371. s32 event_count;
  372. };
  373. static int exm_open(struct inode *inode, struct file *filep)
  374. {
  375. struct exm_device *idev;
  376. struct exm_listener *listener;
  377. int ret = 0;
  378. mutex_lock(&minor_lock);
  379. idev = idr_find(&exm_idr, iminor(inode));
  380. mutex_unlock(&minor_lock);
  381. if (!idev) {
  382. ret = -ENODEV;
  383. goto out;
  384. }
  385. if (!try_module_get(idev->owner)) {
  386. ret = -ENODEV;
  387. goto out;
  388. }
  389. listener = kmalloc(sizeof(*listener), GFP_KERNEL);
  390. if (!listener) {
  391. ret = -ENOMEM;
  392. goto err_alloc_listener;
  393. }
  394. listener->dev = idev;
  395. listener->event_count = atomic_read(&idev->event);
  396. filep->private_data = listener;
  397. if (idev->info->open) {
  398. ret = idev->info->open(idev->info, inode);
  399. if (ret)
  400. goto err_infoopen;
  401. }
  402. return 0;
  403. err_infoopen:
  404. kfree(listener);
  405. err_alloc_listener:
  406. module_put(idev->owner);
  407. out:
  408. return ret;
  409. }
  410. static int exm_fasync(int fd, struct file *filep, int on)
  411. {
  412. struct exm_listener *listener = filep->private_data;
  413. struct exm_device *idev = listener->dev;
  414. return fasync_helper(fd, filep, on, &idev->async_queue);
  415. }
  416. static int exm_release(struct inode *inode, struct file *filep)
  417. {
  418. int ret = 0;
  419. struct exm_listener *listener = filep->private_data;
  420. struct exm_device *idev = listener->dev;
  421. if (idev->info->release)
  422. ret = idev->info->release(idev->info, inode);
  423. module_put(idev->owner);
  424. kfree(listener);
  425. return ret;
  426. }
  427. static unsigned int exm_poll(struct file *filep, poll_table *wait)
  428. {
  429. struct exm_listener *listener = filep->private_data;
  430. struct exm_device *idev = listener->dev;
  431. if (!idev->info->irq)
  432. return -EIO;
  433. poll_wait(filep, &idev->wait, wait);
  434. if (listener->event_count != atomic_read(&idev->event))
  435. return POLLIN | POLLRDNORM;
  436. return 0;
  437. }
  438. static ssize_t exm_read(struct file *filep, char __user *buf,
  439. size_t count, loff_t *ppos)
  440. {
  441. struct exm_listener *listener = filep->private_data;
  442. struct exm_device *idev = listener->dev;
  443. DECLARE_WAITQUEUE(wait, current);
  444. ssize_t retval;
  445. s32 event_count;
  446. if (!idev->info->irq)
  447. return -EIO;
  448. if (count != sizeof(s32))
  449. return -EINVAL;
  450. add_wait_queue(&idev->wait, &wait);
  451. do {
  452. set_current_state(TASK_INTERRUPTIBLE);
  453. event_count = atomic_read(&idev->event);
  454. if (event_count != listener->event_count) {
  455. if (copy_to_user(buf, &event_count, count))
  456. retval = -EFAULT;
  457. else {
  458. listener->event_count = event_count;
  459. retval = count;
  460. }
  461. break;
  462. }
  463. if (filep->f_flags & O_NONBLOCK) {
  464. retval = -EAGAIN;
  465. break;
  466. }
  467. if (signal_pending(current)) {
  468. retval = -ERESTARTSYS;
  469. break;
  470. }
  471. schedule();
  472. } while (1);
  473. __set_current_state(TASK_RUNNING);
  474. remove_wait_queue(&idev->wait, &wait);
  475. return retval;
  476. }
  477. static ssize_t exm_write(struct file *filep, const char __user *buf,
  478. size_t count, loff_t *ppos)
  479. {
  480. struct exm_listener *listener = filep->private_data;
  481. struct exm_device *idev = listener->dev;
  482. ssize_t retval;
  483. s32 irq_on;
  484. if (!idev->info->irq)
  485. return -EIO;
  486. if (count != sizeof(s32))
  487. return -EINVAL;
  488. if (!idev->info->irqcontrol)
  489. return -ENOSYS;
  490. if (copy_from_user(&irq_on, buf, count))
  491. return -EFAULT;
  492. retval = idev->info->irqcontrol(idev->info, irq_on);
  493. return retval ? retval : sizeof(s32);
  494. }
  495. static int exm_find_mem_index(struct vm_area_struct *vma)
  496. {
  497. struct exm_device *idev = vma->vm_private_data;
  498. if (vma->vm_pgoff < MAX_EXM_MAPS) {
  499. if (idev->info->mem[vma->vm_pgoff].size == 0)
  500. return -1;
  501. return (int)vma->vm_pgoff;
  502. }
  503. return -1;
  504. }
  505. static void exm_vma_open(struct vm_area_struct *vma)
  506. {
  507. struct exm_device *idev = vma->vm_private_data;
  508. idev->vma_count++;
  509. }
  510. static void exm_vma_close(struct vm_area_struct *vma)
  511. {
  512. struct exm_device *idev = vma->vm_private_data;
  513. idev->vma_count--;
  514. }
  515. static int exm_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  516. {
  517. struct exm_device *idev = vma->vm_private_data;
  518. struct page *page;
  519. unsigned long offset;
  520. int mi = exm_find_mem_index(vma);
  521. if (mi < 0)
  522. return VM_FAULT_SIGBUS;
  523. /*
  524. * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
  525. * to use mem[N].
  526. */
  527. offset = (vmf->pgoff - mi) << PAGE_SHIFT;
  528. if (idev->info->mem[mi].memtype == EXM_MEM_LOGICAL)
  529. page = virt_to_page(idev->info->mem[mi].addr + offset);
  530. else
  531. page = vmalloc_to_page((void *)(unsigned long)idev->info->mem[mi].addr + offset);
  532. get_page(page);
  533. vmf->page = page;
  534. return 0;
  535. }
  536. static const struct vm_operations_struct exm_vm_ops = {
  537. .open = exm_vma_open,
  538. .close = exm_vma_close,
  539. .fault = exm_vma_fault,
  540. };
  541. static int exm_mmap_physical(struct vm_area_struct *vma)
  542. {
  543. struct exm_device *idev = vma->vm_private_data;
  544. int mi = exm_find_mem_index(vma);
  545. if (mi < 0)
  546. return -EINVAL;
  547. vma->vm_flags |= VM_IO | VM_RESERVED;
  548. #ifdef CONFIG_ARM64
  549. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  550. #else
  551. vma->vm_page_prot = __pgprot_modify(vma->vm_page_prot, L_PTE_MT_MASK, L_PTE_MT_WRITEBACK);
  552. #endif
  553. return remap_pfn_range(vma,
  554. vma->vm_start,
  555. idev->info->mem[mi].addr >> PAGE_SHIFT,
  556. vma->vm_end - vma->vm_start,
  557. vma->vm_page_prot);
  558. }
  559. static int exm_mmap_logical(struct vm_area_struct *vma)
  560. {
  561. vma->vm_flags |= VM_RESERVED;
  562. vma->vm_ops = &exm_vm_ops;
  563. exm_vma_open(vma);
  564. return 0;
  565. }
  566. static int exm_mmap(struct file *filep, struct vm_area_struct *vma)
  567. {
  568. struct exm_listener *listener = filep->private_data;
  569. struct exm_device *idev = listener->dev;
  570. int mi;
  571. unsigned long requested_pages, actual_pages;
  572. int ret = 0;
  573. if (vma->vm_end < vma->vm_start)
  574. return -EINVAL;
  575. vma->vm_private_data = idev;
  576. mi = exm_find_mem_index(vma);
  577. if (mi < 0)
  578. return -EINVAL;
  579. requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  580. actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
  581. + idev->info->mem[mi].size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  582. if (requested_pages > actual_pages)
  583. return -EINVAL;
  584. if (idev->info->mmap) {
  585. ret = idev->info->mmap(idev->info, vma);
  586. return ret;
  587. }
  588. switch (idev->info->mem[mi].memtype) {
  589. case EXM_MEM_PHYS:
  590. return exm_mmap_physical(vma);
  591. case EXM_MEM_LOGICAL:
  592. case EXM_MEM_VIRTUAL:
  593. return exm_mmap_logical(vma);
  594. default:
  595. return -EINVAL;
  596. }
  597. }
  598. static const struct file_operations exm_fops = {
  599. .owner = THIS_MODULE,
  600. .open = exm_open,
  601. .release = exm_release,
  602. .read = exm_read,
  603. .write = exm_write,
  604. .mmap = exm_mmap,
  605. .poll = exm_poll,
  606. .fasync = exm_fasync,
  607. .llseek = noop_llseek,
  608. };
  609. static int exm_major_init(void)
  610. {
  611. static const char name[] = "exm";
  612. struct cdev *cdev = NULL;
  613. dev_t exm_dev = 0;
  614. int result;
  615. result = alloc_chrdev_region(&exm_dev, 0, EXM_MAX_DEVICES, name);
  616. if (result)
  617. goto out;
  618. result = -ENOMEM;
  619. cdev = cdev_alloc();
  620. if (!cdev)
  621. goto out_unregister;
  622. cdev->owner = THIS_MODULE;
  623. cdev->ops = &exm_fops;
  624. kobject_set_name(&cdev->kobj, "%s", name);
  625. result = cdev_add(cdev, exm_dev, EXM_MAX_DEVICES);
  626. if (result)
  627. goto out_put;
  628. exm_major = MAJOR(exm_dev);
  629. exm_cdev = cdev;
  630. return 0;
  631. out_put:
  632. kobject_put(&cdev->kobj);
  633. out_unregister:
  634. unregister_chrdev_region(exm_dev, EXM_MAX_DEVICES);
  635. out:
  636. return result;
  637. }
  638. static void exm_major_cleanup(void)
  639. {
  640. unregister_chrdev_region(MKDEV(exm_major, 0), EXM_MAX_DEVICES);
  641. cdev_del(exm_cdev);
  642. }
  643. static int init_exm_class(void)
  644. {
  645. int ret;
  646. /* This is the first time in here, set everything up properly */
  647. ret = exm_major_init();
  648. if (ret)
  649. goto exit;
  650. ret = class_register(&exm_class);
  651. if (ret) {
  652. pr_err("class_register failed for exm\n");
  653. goto err_class_register;
  654. }
  655. return 0;
  656. err_class_register:
  657. exm_major_cleanup();
  658. exit:
  659. return ret;
  660. }
  661. static void release_exm_class(void)
  662. {
  663. class_unregister(&exm_class);
  664. exm_major_cleanup();
  665. }
  666. /**
  667. * exm_register_device - register a new userspace IO device
  668. * @owner: module that creates the new device
  669. * @parent: parent device
  670. * @info: EXM device capabilities
  671. *
  672. * returns zero on success or a negative error code.
  673. */
  674. int __exm_register_device(struct module *owner,
  675. struct device *parent,
  676. struct exm_info *info)
  677. {
  678. struct exm_device *idev;
  679. int ret = 0;
  680. if (!parent || !info || !info->name || !info->version)
  681. return -EINVAL;
  682. info->exm_dev = NULL;
  683. idev = kzalloc(sizeof(*idev), GFP_KERNEL);
  684. if (!idev) {
  685. ret = -ENOMEM;
  686. goto err_kzalloc;
  687. }
  688. idev->owner = owner;
  689. idev->info = info;
  690. init_waitqueue_head(&idev->wait);
  691. atomic_set(&idev->event, 0);
  692. ret = exm_get_minor(idev);
  693. if (ret)
  694. goto err_get_minor;
  695. idev->dev = device_create(&exm_class, parent,
  696. MKDEV(exm_major, idev->minor), idev,
  697. "exm%d", idev->minor);
  698. if (IS_ERR(idev->dev)) {
  699. pr_err("EXM: device register failed\n");
  700. ret = PTR_ERR(idev->dev);
  701. goto err_device_create;
  702. }
  703. ret = exm_dev_add_attributes(idev);
  704. if (ret)
  705. goto err_exm_dev_add_attributes;
  706. info->exm_dev = idev;
  707. if (info->irq && (info->irq != EXM_IRQ_CUSTOM)) {
  708. ret = request_irq(info->irq, exm_interrupt,
  709. info->irq_flags, info->name, idev);
  710. if (ret)
  711. goto err_request_irq;
  712. }
  713. return 0;
  714. err_request_irq:
  715. exm_dev_del_attributes(idev);
  716. err_exm_dev_add_attributes:
  717. device_destroy(&exm_class, MKDEV(exm_major, idev->minor));
  718. err_device_create:
  719. exm_free_minor(idev);
  720. err_get_minor:
  721. kfree(idev);
  722. err_kzalloc:
  723. return ret;
  724. }
  725. EXPORT_SYMBOL_GPL(__exm_register_device);
  726. /**
  727. * exm_unregister_device - unregister a industrial IO device
  728. * @info: EXM device capabilities
  729. *
  730. */
  731. void exm_unregister_device(struct exm_info *info)
  732. {
  733. struct exm_device *idev;
  734. if (!info || !info->exm_dev)
  735. return;
  736. idev = info->exm_dev;
  737. exm_free_minor(idev);
  738. if (info->irq && (info->irq != EXM_IRQ_CUSTOM))
  739. free_irq(info->irq, idev);
  740. exm_dev_del_attributes(idev);
  741. device_destroy(&exm_class, MKDEV(exm_major, idev->minor));
  742. kfree(idev);
  743. }
  744. EXPORT_SYMBOL_GPL(exm_unregister_device);
  745. static int __init exm_init(void)
  746. {
  747. return init_exm_class();
  748. }
  749. static void __exit exm_exit(void)
  750. {
  751. release_exm_class();
  752. }
  753. module_init(exm_init)
  754. module_exit(exm_exit)
  755. MODULE_LICENSE("GPL v2");