serio_raw.c 9.9 KB

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