serio_raw.c 9.1 KB

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