serio_raw.c 9.7 KB

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