serio_raw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/kref.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.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. struct kref kref;
  33. struct serio *serio;
  34. struct miscdevice dev;
  35. wait_queue_head_t wait;
  36. struct list_head client_list;
  37. struct list_head node;
  38. bool dead;
  39. };
  40. struct serio_raw_client {
  41. struct fasync_struct *fasync;
  42. struct serio_raw *serio_raw;
  43. struct list_head node;
  44. };
  45. static DEFINE_MUTEX(serio_raw_mutex);
  46. static LIST_HEAD(serio_raw_list);
  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_client *client = file->private_data;
  53. return fasync_helper(fd, file, on, &client->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_client *client;
  68. int retval;
  69. retval = mutex_lock_interruptible(&serio_raw_mutex);
  70. if (retval)
  71. return retval;
  72. serio_raw = serio_raw_locate(iminor(inode));
  73. if (!serio_raw) {
  74. retval = -ENODEV;
  75. goto out;
  76. }
  77. if (serio_raw->dead) {
  78. retval = -ENODEV;
  79. goto out;
  80. }
  81. client = kzalloc(sizeof(struct serio_raw_client), GFP_KERNEL);
  82. if (!client) {
  83. retval = -ENOMEM;
  84. goto out;
  85. }
  86. client->serio_raw = serio_raw;
  87. file->private_data = client;
  88. kref_get(&serio_raw->kref);
  89. serio_pause_rx(serio_raw->serio);
  90. list_add_tail(&client->node, &serio_raw->client_list);
  91. serio_continue_rx(serio_raw->serio);
  92. out:
  93. mutex_unlock(&serio_raw_mutex);
  94. return retval;
  95. }
  96. static void serio_raw_free(struct kref *kref)
  97. {
  98. struct serio_raw *serio_raw =
  99. container_of(kref, struct serio_raw, kref);
  100. put_device(&serio_raw->serio->dev);
  101. kfree(serio_raw);
  102. }
  103. static int serio_raw_release(struct inode *inode, struct file *file)
  104. {
  105. struct serio_raw_client *client = file->private_data;
  106. struct serio_raw *serio_raw = client->serio_raw;
  107. serio_pause_rx(serio_raw->serio);
  108. list_del(&client->node);
  109. serio_continue_rx(serio_raw->serio);
  110. kfree(client);
  111. kref_put(&serio_raw->kref, serio_raw_free);
  112. return 0;
  113. }
  114. static bool serio_raw_fetch_byte(struct serio_raw *serio_raw, char *c)
  115. {
  116. bool empty;
  117. serio_pause_rx(serio_raw->serio);
  118. empty = serio_raw->head == serio_raw->tail;
  119. if (!empty) {
  120. *c = serio_raw->queue[serio_raw->tail];
  121. serio_raw->tail = (serio_raw->tail + 1) % SERIO_RAW_QUEUE_LEN;
  122. }
  123. serio_continue_rx(serio_raw->serio);
  124. return !empty;
  125. }
  126. static ssize_t serio_raw_read(struct file *file, char __user *buffer,
  127. size_t count, loff_t *ppos)
  128. {
  129. struct serio_raw_client *client = file->private_data;
  130. struct serio_raw *serio_raw = client->serio_raw;
  131. char uninitialized_var(c);
  132. ssize_t read = 0;
  133. int retval;
  134. if (serio_raw->dead)
  135. return -ENODEV;
  136. if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
  137. return -EAGAIN;
  138. retval = wait_event_interruptible(serio_raw->wait,
  139. serio_raw->head != serio_raw->tail || serio_raw->dead);
  140. if (retval)
  141. return retval;
  142. if (serio_raw->dead)
  143. return -ENODEV;
  144. while (read < count && serio_raw_fetch_byte(serio_raw, &c)) {
  145. if (put_user(c, buffer++)) {
  146. retval = -EFAULT;
  147. break;
  148. }
  149. read++;
  150. }
  151. return read ?: retval;
  152. }
  153. static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
  154. size_t count, loff_t *ppos)
  155. {
  156. struct serio_raw_client *client = file->private_data;
  157. struct serio_raw *serio_raw = client->serio_raw;
  158. ssize_t written = 0;
  159. int retval;
  160. unsigned char c;
  161. retval = mutex_lock_interruptible(&serio_raw_mutex);
  162. if (retval)
  163. return retval;
  164. if (serio_raw->dead) {
  165. retval = -ENODEV;
  166. goto out;
  167. }
  168. if (count > 32)
  169. count = 32;
  170. while (count--) {
  171. if (get_user(c, buffer++)) {
  172. retval = -EFAULT;
  173. goto out;
  174. }
  175. if (serio_write(serio_raw->serio, c)) {
  176. retval = -EIO;
  177. goto out;
  178. }
  179. written++;
  180. }
  181. out:
  182. mutex_unlock(&serio_raw_mutex);
  183. return written ?: retval;
  184. }
  185. static unsigned int serio_raw_poll(struct file *file, poll_table *wait)
  186. {
  187. struct serio_raw_client *client = file->private_data;
  188. struct serio_raw *serio_raw = client->serio_raw;
  189. unsigned int mask;
  190. poll_wait(file, &serio_raw->wait, wait);
  191. mask = serio_raw->dead ? POLLHUP | POLLERR : POLLOUT | POLLWRNORM;
  192. if (serio_raw->head != serio_raw->tail)
  193. mask |= POLLIN | POLLRDNORM;
  194. return mask;
  195. }
  196. static const struct file_operations serio_raw_fops = {
  197. .owner = THIS_MODULE,
  198. .open = serio_raw_open,
  199. .release = serio_raw_release,
  200. .read = serio_raw_read,
  201. .write = serio_raw_write,
  202. .poll = serio_raw_poll,
  203. .fasync = serio_raw_fasync,
  204. .llseek = noop_llseek,
  205. };
  206. /*********************************************************************
  207. * Interface with serio port *
  208. *********************************************************************/
  209. static irqreturn_t serio_raw_interrupt(struct serio *serio, unsigned char data,
  210. unsigned int dfl)
  211. {
  212. struct serio_raw *serio_raw = serio_get_drvdata(serio);
  213. struct serio_raw_client *client;
  214. unsigned int head = serio_raw->head;
  215. /* we are holding serio->lock here so we are protected */
  216. serio_raw->queue[head] = data;
  217. head = (head + 1) % SERIO_RAW_QUEUE_LEN;
  218. if (likely(head != serio_raw->tail)) {
  219. serio_raw->head = head;
  220. list_for_each_entry(client, &serio_raw->client_list, node)
  221. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  222. wake_up_interruptible(&serio_raw->wait);
  223. }
  224. return IRQ_HANDLED;
  225. }
  226. static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
  227. {
  228. static atomic_t serio_raw_no = ATOMIC_INIT(0);
  229. struct serio_raw *serio_raw;
  230. int err;
  231. serio_raw = kzalloc(sizeof(struct serio_raw), GFP_KERNEL);
  232. if (!serio_raw) {
  233. dev_dbg(&serio->dev, "can't allocate memory for a device\n");
  234. return -ENOMEM;
  235. }
  236. snprintf(serio_raw->name, sizeof(serio_raw->name),
  237. "serio_raw%ld", (long)atomic_inc_return(&serio_raw_no) - 1);
  238. kref_init(&serio_raw->kref);
  239. INIT_LIST_HEAD(&serio_raw->client_list);
  240. init_waitqueue_head(&serio_raw->wait);
  241. serio_raw->serio = serio;
  242. get_device(&serio->dev);
  243. serio_set_drvdata(serio, serio_raw);
  244. err = serio_open(serio, drv);
  245. if (err)
  246. goto err_free;
  247. err = mutex_lock_killable(&serio_raw_mutex);
  248. if (err)
  249. goto err_close;
  250. list_add_tail(&serio_raw->node, &serio_raw_list);
  251. mutex_unlock(&serio_raw_mutex);
  252. serio_raw->dev.minor = PSMOUSE_MINOR;
  253. serio_raw->dev.name = serio_raw->name;
  254. serio_raw->dev.parent = &serio->dev;
  255. serio_raw->dev.fops = &serio_raw_fops;
  256. err = misc_register(&serio_raw->dev);
  257. if (err) {
  258. serio_raw->dev.minor = MISC_DYNAMIC_MINOR;
  259. err = misc_register(&serio_raw->dev);
  260. }
  261. if (err) {
  262. dev_err(&serio->dev,
  263. "failed to register raw access device for %s\n",
  264. serio->phys);
  265. goto err_unlink;
  266. }
  267. dev_info(&serio->dev, "raw access enabled on %s (%s, minor %d)\n",
  268. serio->phys, serio_raw->name, serio_raw->dev.minor);
  269. return 0;
  270. err_unlink:
  271. list_del_init(&serio_raw->node);
  272. err_close:
  273. serio_close(serio);
  274. err_free:
  275. serio_set_drvdata(serio, NULL);
  276. kref_put(&serio_raw->kref, serio_raw_free);
  277. return err;
  278. }
  279. static int serio_raw_reconnect(struct serio *serio)
  280. {
  281. struct serio_raw *serio_raw = serio_get_drvdata(serio);
  282. struct serio_driver *drv = serio->drv;
  283. if (!drv || !serio_raw) {
  284. dev_dbg(&serio->dev,
  285. "reconnect request, but serio is disconnected, ignoring...\n");
  286. return -1;
  287. }
  288. /*
  289. * Nothing needs to be done here, we just need this method to
  290. * keep the same device.
  291. */
  292. return 0;
  293. }
  294. /*
  295. * Wake up users waiting for IO so they can disconnect from
  296. * dead device.
  297. */
  298. static void serio_raw_hangup(struct serio_raw *serio_raw)
  299. {
  300. struct serio_raw_client *client;
  301. serio_pause_rx(serio_raw->serio);
  302. list_for_each_entry(client, &serio_raw->client_list, node)
  303. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  304. serio_continue_rx(serio_raw->serio);
  305. wake_up_interruptible(&serio_raw->wait);
  306. }
  307. static void serio_raw_disconnect(struct serio *serio)
  308. {
  309. struct serio_raw *serio_raw = serio_get_drvdata(serio);
  310. misc_deregister(&serio_raw->dev);
  311. mutex_lock(&serio_raw_mutex);
  312. serio_raw->dead = true;
  313. list_del_init(&serio_raw->node);
  314. mutex_unlock(&serio_raw_mutex);
  315. serio_raw_hangup(serio_raw);
  316. serio_close(serio);
  317. kref_put(&serio_raw->kref, serio_raw_free);
  318. serio_set_drvdata(serio, NULL);
  319. }
  320. static struct serio_device_id serio_raw_serio_ids[] = {
  321. {
  322. .type = SERIO_8042,
  323. .proto = SERIO_ANY,
  324. .id = SERIO_ANY,
  325. .extra = SERIO_ANY,
  326. },
  327. {
  328. .type = SERIO_8042_XL,
  329. .proto = SERIO_ANY,
  330. .id = SERIO_ANY,
  331. .extra = SERIO_ANY,
  332. },
  333. { 0 }
  334. };
  335. MODULE_DEVICE_TABLE(serio, serio_raw_serio_ids);
  336. static struct serio_driver serio_raw_drv = {
  337. .driver = {
  338. .name = "serio_raw",
  339. },
  340. .description = DRIVER_DESC,
  341. .id_table = serio_raw_serio_ids,
  342. .interrupt = serio_raw_interrupt,
  343. .connect = serio_raw_connect,
  344. .reconnect = serio_raw_reconnect,
  345. .disconnect = serio_raw_disconnect,
  346. .manual_bind = true,
  347. };
  348. static int __init serio_raw_init(void)
  349. {
  350. return serio_register_driver(&serio_raw_drv);
  351. }
  352. static void __exit serio_raw_exit(void)
  353. {
  354. serio_unregister_driver(&serio_raw_drv);
  355. }
  356. module_init(serio_raw_init);
  357. module_exit(serio_raw_exit);