serio_raw.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Raw serio device providing access to a raw byte stream from underlying
  3. * serio port. Closely emulates behavior of pre-2.6 /dev/psaux device
  4. *
  5. * Copyright (c) 2004 Dmitry Torokhov
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/poll.h>
  15. #include <linux/module.h>
  16. #include <linux/serio.h>
  17. #include <linux/init.h>
  18. #include <linux/major.h>
  19. #include <linux/device.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/wait.h>
  22. #include <linux/mutex.h>
  23. #define DRIVER_DESC "Raw serio driver"
  24. MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
  25. MODULE_DESCRIPTION(DRIVER_DESC);
  26. MODULE_LICENSE("GPL");
  27. #define SERIO_RAW_QUEUE_LEN 64
  28. struct serio_raw {
  29. unsigned char queue[SERIO_RAW_QUEUE_LEN];
  30. unsigned int tail, head;
  31. char name[16];
  32. unsigned int refcnt;
  33. struct serio *serio;
  34. struct miscdevice dev;
  35. wait_queue_head_t wait;
  36. struct list_head list;
  37. struct list_head node;
  38. };
  39. struct serio_raw_list {
  40. struct fasync_struct *fasync;
  41. struct serio_raw *serio_raw;
  42. struct list_head node;
  43. };
  44. static DEFINE_MUTEX(serio_raw_mutex);
  45. static LIST_HEAD(serio_raw_list);
  46. static unsigned int serio_raw_no;
  47. /*********************************************************************
  48. * Interface with userspace (file operations) *
  49. *********************************************************************/
  50. static int serio_raw_fasync(int fd, struct file *file, int on)
  51. {
  52. struct serio_raw_list *list = file->private_data;
  53. return fasync_helper(fd, file, on, &list->fasync);
  54. }
  55. static struct serio_raw *serio_raw_locate(int minor)
  56. {
  57. struct serio_raw *serio_raw;
  58. list_for_each_entry(serio_raw, &serio_raw_list, node) {
  59. if (serio_raw->dev.minor == minor)
  60. return serio_raw;
  61. }
  62. return NULL;
  63. }
  64. static int serio_raw_open(struct inode *inode, struct file *file)
  65. {
  66. struct serio_raw *serio_raw;
  67. struct serio_raw_list *list;
  68. int retval = 0;
  69. lock_kernel();
  70. retval = mutex_lock_interruptible(&serio_raw_mutex);
  71. if (retval)
  72. goto out_bkl;
  73. if (!(serio_raw = serio_raw_locate(iminor(inode)))) {
  74. retval = -ENODEV;
  75. goto out;
  76. }
  77. if (!serio_raw->serio) {
  78. retval = -ENODEV;
  79. goto out;
  80. }
  81. if (!(list = kzalloc(sizeof(struct serio_raw_list), GFP_KERNEL))) {
  82. retval = -ENOMEM;
  83. goto out;
  84. }
  85. list->serio_raw = serio_raw;
  86. file->private_data = list;
  87. serio_raw->refcnt++;
  88. list_add_tail(&list->node, &serio_raw->list);
  89. out:
  90. mutex_unlock(&serio_raw_mutex);
  91. out_bkl:
  92. unlock_kernel();
  93. return retval;
  94. }
  95. static int serio_raw_cleanup(struct serio_raw *serio_raw)
  96. {
  97. if (--serio_raw->refcnt == 0) {
  98. misc_deregister(&serio_raw->dev);
  99. list_del_init(&serio_raw->node);
  100. kfree(serio_raw);
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. static int serio_raw_release(struct inode *inode, struct file *file)
  106. {
  107. struct serio_raw_list *list = file->private_data;
  108. struct serio_raw *serio_raw = list->serio_raw;
  109. mutex_lock(&serio_raw_mutex);
  110. serio_raw_cleanup(serio_raw);
  111. mutex_unlock(&serio_raw_mutex);
  112. return 0;
  113. }
  114. static int serio_raw_fetch_byte(struct serio_raw *serio_raw, char *c)
  115. {
  116. unsigned long flags;
  117. int empty;
  118. spin_lock_irqsave(&serio_raw->serio->lock, flags);
  119. empty = serio_raw->head == serio_raw->tail;
  120. if (!empty) {
  121. *c = serio_raw->queue[serio_raw->tail];
  122. serio_raw->tail = (serio_raw->tail + 1) % SERIO_RAW_QUEUE_LEN;
  123. }
  124. spin_unlock_irqrestore(&serio_raw->serio->lock, flags);
  125. return !empty;
  126. }
  127. static ssize_t serio_raw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  128. {
  129. struct serio_raw_list *list = file->private_data;
  130. struct serio_raw *serio_raw = list->serio_raw;
  131. char uninitialized_var(c);
  132. ssize_t retval = 0;
  133. if (!serio_raw->serio)
  134. return -ENODEV;
  135. if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
  136. return -EAGAIN;
  137. retval = wait_event_interruptible(list->serio_raw->wait,
  138. serio_raw->head != serio_raw->tail || !serio_raw->serio);
  139. if (retval)
  140. return retval;
  141. if (!serio_raw->serio)
  142. return -ENODEV;
  143. while (retval < count && serio_raw_fetch_byte(serio_raw, &c)) {
  144. if (put_user(c, buffer++))
  145. return -EFAULT;
  146. retval++;
  147. }
  148. return retval;
  149. }
  150. static ssize_t serio_raw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  151. {
  152. struct serio_raw_list *list = file->private_data;
  153. ssize_t written = 0;
  154. int retval;
  155. unsigned char c;
  156. retval = mutex_lock_interruptible(&serio_raw_mutex);
  157. if (retval)
  158. return retval;
  159. if (!list->serio_raw->serio) {
  160. retval = -ENODEV;
  161. goto out;
  162. }
  163. if (count > 32)
  164. count = 32;
  165. while (count--) {
  166. if (get_user(c, buffer++)) {
  167. retval = -EFAULT;
  168. goto out;
  169. }
  170. if (serio_write(list->serio_raw->serio, c)) {
  171. retval = -EIO;
  172. goto out;
  173. }
  174. written++;
  175. };
  176. out:
  177. mutex_unlock(&serio_raw_mutex);
  178. return written;
  179. }
  180. static unsigned int serio_raw_poll(struct file *file, poll_table *wait)
  181. {
  182. struct serio_raw_list *list = file->private_data;
  183. poll_wait(file, &list->serio_raw->wait, wait);
  184. if (list->serio_raw->head != list->serio_raw->tail)
  185. return POLLIN | POLLRDNORM;
  186. return 0;
  187. }
  188. static const struct file_operations serio_raw_fops = {
  189. .owner = THIS_MODULE,
  190. .open = serio_raw_open,
  191. .release = serio_raw_release,
  192. .read = serio_raw_read,
  193. .write = serio_raw_write,
  194. .poll = serio_raw_poll,
  195. .fasync = serio_raw_fasync,
  196. };
  197. /*********************************************************************
  198. * Interface with serio port *
  199. *********************************************************************/
  200. static irqreturn_t serio_raw_interrupt(struct serio *serio, unsigned char data,
  201. unsigned int dfl)
  202. {
  203. struct serio_raw *serio_raw = serio_get_drvdata(serio);
  204. struct serio_raw_list *list;
  205. unsigned int head = serio_raw->head;
  206. /* we are holding serio->lock here so we are prootected */
  207. serio_raw->queue[head] = data;
  208. head = (head + 1) % SERIO_RAW_QUEUE_LEN;
  209. if (likely(head != serio_raw->tail)) {
  210. serio_raw->head = head;
  211. list_for_each_entry(list, &serio_raw->list, node)
  212. kill_fasync(&list->fasync, SIGIO, POLL_IN);
  213. wake_up_interruptible(&serio_raw->wait);
  214. }
  215. return IRQ_HANDLED;
  216. }
  217. static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
  218. {
  219. struct serio_raw *serio_raw;
  220. int err;
  221. if (!(serio_raw = kzalloc(sizeof(struct serio_raw), GFP_KERNEL))) {
  222. printk(KERN_ERR "serio_raw.c: can't allocate memory for a device\n");
  223. return -ENOMEM;
  224. }
  225. mutex_lock(&serio_raw_mutex);
  226. snprintf(serio_raw->name, sizeof(serio_raw->name), "serio_raw%d", serio_raw_no++);
  227. serio_raw->refcnt = 1;
  228. serio_raw->serio = serio;
  229. INIT_LIST_HEAD(&serio_raw->list);
  230. init_waitqueue_head(&serio_raw->wait);
  231. serio_set_drvdata(serio, serio_raw);
  232. err = serio_open(serio, drv);
  233. if (err)
  234. goto out_free;
  235. list_add_tail(&serio_raw->node, &serio_raw_list);
  236. serio_raw->dev.minor = PSMOUSE_MINOR;
  237. serio_raw->dev.name = serio_raw->name;
  238. serio_raw->dev.parent = &serio->dev;
  239. serio_raw->dev.fops = &serio_raw_fops;
  240. err = misc_register(&serio_raw->dev);
  241. if (err) {
  242. serio_raw->dev.minor = MISC_DYNAMIC_MINOR;
  243. err = misc_register(&serio_raw->dev);
  244. }
  245. if (err) {
  246. printk(KERN_INFO "serio_raw: failed to register raw access device for %s\n",
  247. serio->phys);
  248. goto out_close;
  249. }
  250. printk(KERN_INFO "serio_raw: raw access enabled on %s (%s, minor %d)\n",
  251. serio->phys, serio_raw->name, serio_raw->dev.minor);
  252. goto out;
  253. out_close:
  254. serio_close(serio);
  255. list_del_init(&serio_raw->node);
  256. out_free:
  257. serio_set_drvdata(serio, NULL);
  258. kfree(serio_raw);
  259. out:
  260. mutex_unlock(&serio_raw_mutex);
  261. return err;
  262. }
  263. static int serio_raw_reconnect(struct serio *serio)
  264. {
  265. struct serio_raw *serio_raw = serio_get_drvdata(serio);
  266. struct serio_driver *drv = serio->drv;
  267. if (!drv || !serio_raw) {
  268. printk(KERN_DEBUG "serio_raw: reconnect request, but serio is disconnected, ignoring...\n");
  269. return -1;
  270. }
  271. /*
  272. * Nothing needs to be done here, we just need this method to
  273. * keep the same device.
  274. */
  275. return 0;
  276. }
  277. static void serio_raw_disconnect(struct serio *serio)
  278. {
  279. struct serio_raw *serio_raw;
  280. mutex_lock(&serio_raw_mutex);
  281. serio_raw = serio_get_drvdata(serio);
  282. serio_close(serio);
  283. serio_set_drvdata(serio, NULL);
  284. serio_raw->serio = NULL;
  285. if (!serio_raw_cleanup(serio_raw))
  286. wake_up_interruptible(&serio_raw->wait);
  287. mutex_unlock(&serio_raw_mutex);
  288. }
  289. static struct serio_device_id serio_raw_serio_ids[] = {
  290. {
  291. .type = SERIO_8042,
  292. .proto = SERIO_ANY,
  293. .id = SERIO_ANY,
  294. .extra = SERIO_ANY,
  295. },
  296. {
  297. .type = SERIO_8042_XL,
  298. .proto = SERIO_ANY,
  299. .id = SERIO_ANY,
  300. .extra = SERIO_ANY,
  301. },
  302. { 0 }
  303. };
  304. MODULE_DEVICE_TABLE(serio, serio_raw_serio_ids);
  305. static struct serio_driver serio_raw_drv = {
  306. .driver = {
  307. .name = "serio_raw",
  308. },
  309. .description = DRIVER_DESC,
  310. .id_table = serio_raw_serio_ids,
  311. .interrupt = serio_raw_interrupt,
  312. .connect = serio_raw_connect,
  313. .reconnect = serio_raw_reconnect,
  314. .disconnect = serio_raw_disconnect,
  315. .manual_bind = 1,
  316. };
  317. static int __init serio_raw_init(void)
  318. {
  319. return serio_register_driver(&serio_raw_drv);
  320. }
  321. static void __exit serio_raw_exit(void)
  322. {
  323. serio_unregister_driver(&serio_raw_drv);
  324. }
  325. module_init(serio_raw_init);
  326. module_exit(serio_raw_exit);