Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dmitry Torokhov | ba538cd | 2011-10-10 18:30:03 -0700 | [diff] [blame] | 12 | #include <linux/kref.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 13 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/poll.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/serio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/major.h> |
| 19 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/miscdevice.h> |
| 21 | #include <linux/wait.h> |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define DRIVER_DESC "Raw serio driver" |
| 25 | |
| 26 | MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>"); |
| 27 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
| 30 | #define SERIO_RAW_QUEUE_LEN 64 |
| 31 | struct serio_raw { |
| 32 | unsigned char queue[SERIO_RAW_QUEUE_LEN]; |
| 33 | unsigned int tail, head; |
| 34 | |
| 35 | char name[16]; |
Dmitry Torokhov | ba538cd | 2011-10-10 18:30:03 -0700 | [diff] [blame] | 36 | struct kref kref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct serio *serio; |
| 38 | struct miscdevice dev; |
| 39 | wait_queue_head_t wait; |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 40 | struct list_head client_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct list_head node; |
Dmitry Torokhov | 85f5b35 | 2011-10-10 18:31:04 -0700 | [diff] [blame] | 42 | bool dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 45 | struct serio_raw_client { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | struct fasync_struct *fasync; |
| 47 | struct serio_raw *serio_raw; |
| 48 | struct list_head node; |
| 49 | }; |
| 50 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 51 | static DEFINE_MUTEX(serio_raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static LIST_HEAD(serio_raw_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /********************************************************************* |
| 55 | * Interface with userspace (file operations) * |
| 56 | *********************************************************************/ |
| 57 | |
| 58 | static int serio_raw_fasync(int fd, struct file *file, int on) |
| 59 | { |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 60 | struct serio_raw_client *client = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 62 | return fasync_helper(fd, file, on, &client->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static struct serio_raw *serio_raw_locate(int minor) |
| 66 | { |
| 67 | struct serio_raw *serio_raw; |
| 68 | |
| 69 | list_for_each_entry(serio_raw, &serio_raw_list, node) { |
| 70 | if (serio_raw->dev.minor == minor) |
| 71 | return serio_raw; |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | static int serio_raw_open(struct inode *inode, struct file *file) |
| 78 | { |
| 79 | struct serio_raw *serio_raw; |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 80 | struct serio_raw_client *client; |
| 81 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 83 | retval = mutex_lock_interruptible(&serio_raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (retval) |
Thadeu Lima de Souza Cascardo | 77554b4 | 2010-03-09 20:38:47 -0800 | [diff] [blame] | 85 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Thadeu Lima de Souza Cascardo | 77554b4 | 2010-03-09 20:38:47 -0800 | [diff] [blame] | 87 | serio_raw = serio_raw_locate(iminor(inode)); |
| 88 | if (!serio_raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | retval = -ENODEV; |
| 90 | goto out; |
| 91 | } |
| 92 | |
Dmitry Torokhov | 85f5b35 | 2011-10-10 18:31:04 -0700 | [diff] [blame] | 93 | if (serio_raw->dead) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | retval = -ENODEV; |
| 95 | goto out; |
| 96 | } |
| 97 | |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 98 | client = kzalloc(sizeof(struct serio_raw_client), GFP_KERNEL); |
| 99 | if (!client) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | retval = -ENOMEM; |
| 101 | goto out; |
| 102 | } |
| 103 | |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 104 | client->serio_raw = serio_raw; |
| 105 | file->private_data = client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Dmitry Torokhov | ba538cd | 2011-10-10 18:30:03 -0700 | [diff] [blame] | 107 | kref_get(&serio_raw->kref); |
Dmitry Torokhov | 843e784 | 2011-10-10 18:30:26 -0700 | [diff] [blame] | 108 | |
| 109 | serio_pause_rx(serio_raw->serio); |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 110 | list_add_tail(&client->node, &serio_raw->client_list); |
Dmitry Torokhov | 843e784 | 2011-10-10 18:30:26 -0700 | [diff] [blame] | 111 | serio_continue_rx(serio_raw->serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | out: |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 114 | mutex_unlock(&serio_raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return retval; |
| 116 | } |
| 117 | |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 118 | static void serio_raw_free(struct kref *kref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Dmitry Torokhov | ba538cd | 2011-10-10 18:30:03 -0700 | [diff] [blame] | 120 | struct serio_raw *serio_raw = |
| 121 | container_of(kref, struct serio_raw, kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Dmitry Torokhov | 85f5b35 | 2011-10-10 18:31:04 -0700 | [diff] [blame] | 123 | put_device(&serio_raw->serio->dev); |
Dmitry Torokhov | ba538cd | 2011-10-10 18:30:03 -0700 | [diff] [blame] | 124 | kfree(serio_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static int serio_raw_release(struct inode *inode, struct file *file) |
| 128 | { |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 129 | struct serio_raw_client *client = file->private_data; |
| 130 | struct serio_raw *serio_raw = client->serio_raw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 132 | serio_pause_rx(serio_raw->serio); |
| 133 | list_del(&client->node); |
| 134 | serio_continue_rx(serio_raw->serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 136 | kfree(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 138 | kref_put(&serio_raw->kref, serio_raw_free); |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Dmitry Torokhov | 8c31eb0 | 2011-10-10 18:30:36 -0700 | [diff] [blame] | 143 | static bool serio_raw_fetch_byte(struct serio_raw *serio_raw, char *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Dmitry Torokhov | 8c31eb0 | 2011-10-10 18:30:36 -0700 | [diff] [blame] | 145 | bool empty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Dmitry Torokhov | 843e784 | 2011-10-10 18:30:26 -0700 | [diff] [blame] | 147 | serio_pause_rx(serio_raw->serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | empty = serio_raw->head == serio_raw->tail; |
| 150 | if (!empty) { |
| 151 | *c = serio_raw->queue[serio_raw->tail]; |
| 152 | serio_raw->tail = (serio_raw->tail + 1) % SERIO_RAW_QUEUE_LEN; |
| 153 | } |
| 154 | |
Dmitry Torokhov | 843e784 | 2011-10-10 18:30:26 -0700 | [diff] [blame] | 155 | serio_continue_rx(serio_raw->serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | return !empty; |
| 158 | } |
| 159 | |
Dmitry Torokhov | 15a564d | 2011-10-10 18:30:56 -0700 | [diff] [blame] | 160 | static ssize_t serio_raw_read(struct file *file, char __user *buffer, |
| 161 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 163 | struct serio_raw_client *client = file->private_data; |
| 164 | struct serio_raw *serio_raw = client->serio_raw; |
Andrew Morton | 4f179f7 | 2007-07-10 00:38:31 -0400 | [diff] [blame] | 165 | char uninitialized_var(c); |
Che-Liang Chiou | 7a0a27d | 2012-02-01 09:25:35 -0800 | [diff] [blame] | 166 | ssize_t read = 0; |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 167 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 169 | for (;;) { |
Dmitry Torokhov | 486c8ab | 2012-04-20 22:33:08 -0700 | [diff] [blame] | 170 | if (serio_raw->dead) |
| 171 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Dmitry Torokhov | 486c8ab | 2012-04-20 22:33:08 -0700 | [diff] [blame] | 173 | if (serio_raw->head == serio_raw->tail && |
| 174 | (file->f_flags & O_NONBLOCK)) |
| 175 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Dmitry Torokhov | 486c8ab | 2012-04-20 22:33:08 -0700 | [diff] [blame] | 177 | if (count == 0) |
Che-Liang Chiou | 7a0a27d | 2012-02-01 09:25:35 -0800 | [diff] [blame] | 178 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Dmitry Torokhov | 486c8ab | 2012-04-20 22:33:08 -0700 | [diff] [blame] | 180 | while (read < count && serio_raw_fetch_byte(serio_raw, &c)) { |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 181 | if (put_user(c, buffer++)) |
| 182 | return -EFAULT; |
Dmitry Torokhov | 486c8ab | 2012-04-20 22:33:08 -0700 | [diff] [blame] | 183 | read++; |
| 184 | } |
| 185 | |
| 186 | if (read) |
| 187 | break; |
| 188 | |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 189 | if (!(file->f_flags & O_NONBLOCK)) { |
Dmitry Torokhov | 486c8ab | 2012-04-20 22:33:08 -0700 | [diff] [blame] | 190 | error = wait_event_interruptible(serio_raw->wait, |
| 191 | serio_raw->head != serio_raw->tail || |
| 192 | serio_raw->dead); |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 193 | if (error) |
| 194 | return error; |
| 195 | } |
| 196 | } |
Dmitry Torokhov | 486c8ab | 2012-04-20 22:33:08 -0700 | [diff] [blame] | 197 | |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 198 | return read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Dmitry Torokhov | 15a564d | 2011-10-10 18:30:56 -0700 | [diff] [blame] | 201 | static ssize_t serio_raw_write(struct file *file, const char __user *buffer, |
| 202 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 204 | struct serio_raw_client *client = file->private_data; |
| 205 | struct serio_raw *serio_raw = client->serio_raw; |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 206 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | unsigned char c; |
| 208 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 209 | retval = mutex_lock_interruptible(&serio_raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (retval) |
| 211 | return retval; |
| 212 | |
Dmitry Torokhov | 85f5b35 | 2011-10-10 18:31:04 -0700 | [diff] [blame] | 213 | if (serio_raw->dead) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | retval = -ENODEV; |
| 215 | goto out; |
| 216 | } |
| 217 | |
| 218 | if (count > 32) |
| 219 | count = 32; |
| 220 | |
| 221 | while (count--) { |
| 222 | if (get_user(c, buffer++)) { |
| 223 | retval = -EFAULT; |
| 224 | goto out; |
| 225 | } |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 226 | |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 227 | if (serio_write(serio_raw->serio, c)) { |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 228 | /* Either signal error or partial write */ |
| 229 | if (retval == 0) |
| 230 | retval = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | goto out; |
| 232 | } |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 233 | |
| 234 | retval++; |
Che-Liang Chiou | d89c9bc | 2012-01-10 00:45:12 -0800 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | out: |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 238 | mutex_unlock(&serio_raw_mutex); |
Dmitry Torokhov | 46f49b7 | 2012-05-02 00:13:38 -0700 | [diff] [blame] | 239 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 242 | static __poll_t serio_raw_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 244 | struct serio_raw_client *client = file->private_data; |
| 245 | struct serio_raw *serio_raw = client->serio_raw; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 246 | __poll_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 248 | poll_wait(file, &serio_raw->wait, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 250 | mask = serio_raw->dead ? EPOLLHUP | EPOLLERR : EPOLLOUT | EPOLLWRNORM; |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 251 | if (serio_raw->head != serio_raw->tail) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 252 | mask |= EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Dmitry Torokhov | 0c62fbf | 2012-01-10 00:45:12 -0800 | [diff] [blame] | 254 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 257 | static const struct file_operations serio_raw_fops = { |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 258 | .owner = THIS_MODULE, |
| 259 | .open = serio_raw_open, |
| 260 | .release = serio_raw_release, |
| 261 | .read = serio_raw_read, |
| 262 | .write = serio_raw_write, |
| 263 | .poll = serio_raw_poll, |
| 264 | .fasync = serio_raw_fasync, |
| 265 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | |
| 269 | /********************************************************************* |
Dmitry Torokhov | 15a564d | 2011-10-10 18:30:56 -0700 | [diff] [blame] | 270 | * Interface with serio port * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | *********************************************************************/ |
| 272 | |
| 273 | static irqreturn_t serio_raw_interrupt(struct serio *serio, unsigned char data, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 274 | unsigned int dfl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | struct serio_raw *serio_raw = serio_get_drvdata(serio); |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 277 | struct serio_raw_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | unsigned int head = serio_raw->head; |
| 279 | |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 280 | /* we are holding serio->lock here so we are protected */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | serio_raw->queue[head] = data; |
| 282 | head = (head + 1) % SERIO_RAW_QUEUE_LEN; |
| 283 | if (likely(head != serio_raw->tail)) { |
| 284 | serio_raw->head = head; |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 285 | list_for_each_entry(client, &serio_raw->client_list, node) |
| 286 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | wake_up_interruptible(&serio_raw->wait); |
| 288 | } |
| 289 | |
| 290 | return IRQ_HANDLED; |
| 291 | } |
| 292 | |
| 293 | static int serio_raw_connect(struct serio *serio, struct serio_driver *drv) |
| 294 | { |
Aniroop Mathur | 939ffb1 | 2014-12-03 14:27:42 -0800 | [diff] [blame] | 295 | static atomic_t serio_raw_no = ATOMIC_INIT(-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | struct serio_raw *serio_raw; |
| 297 | int err; |
| 298 | |
Dmitry Torokhov | 15a564d | 2011-10-10 18:30:56 -0700 | [diff] [blame] | 299 | serio_raw = kzalloc(sizeof(struct serio_raw), GFP_KERNEL); |
| 300 | if (!serio_raw) { |
Dmitry Torokhov | 8d92847 | 2011-10-10 18:30:46 -0700 | [diff] [blame] | 301 | dev_dbg(&serio->dev, "can't allocate memory for a device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return -ENOMEM; |
| 303 | } |
| 304 | |
Dmitry Torokhov | 15a564d | 2011-10-10 18:30:56 -0700 | [diff] [blame] | 305 | snprintf(serio_raw->name, sizeof(serio_raw->name), |
Aniroop Mathur | 939ffb1 | 2014-12-03 14:27:42 -0800 | [diff] [blame] | 306 | "serio_raw%ld", (long)atomic_inc_return(&serio_raw_no)); |
Dmitry Torokhov | ba538cd | 2011-10-10 18:30:03 -0700 | [diff] [blame] | 307 | kref_init(&serio_raw->kref); |
Dmitry Torokhov | 7c5bbb2 | 2011-10-10 18:30:14 -0700 | [diff] [blame] | 308 | INIT_LIST_HEAD(&serio_raw->client_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | init_waitqueue_head(&serio_raw->wait); |
| 310 | |
Dmitry Torokhov | 85f5b35 | 2011-10-10 18:31:04 -0700 | [diff] [blame] | 311 | serio_raw->serio = serio; |
| 312 | get_device(&serio->dev); |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | serio_set_drvdata(serio, serio_raw); |
| 315 | |
| 316 | err = serio_open(serio, drv); |
| 317 | if (err) |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 318 | goto err_free; |
| 319 | |
| 320 | err = mutex_lock_killable(&serio_raw_mutex); |
| 321 | if (err) |
| 322 | goto err_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | list_add_tail(&serio_raw->node, &serio_raw_list); |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 325 | mutex_unlock(&serio_raw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | serio_raw->dev.minor = PSMOUSE_MINOR; |
| 328 | serio_raw->dev.name = serio_raw->name; |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 329 | serio_raw->dev.parent = &serio->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | serio_raw->dev.fops = &serio_raw_fops; |
| 331 | |
| 332 | err = misc_register(&serio_raw->dev); |
| 333 | if (err) { |
| 334 | serio_raw->dev.minor = MISC_DYNAMIC_MINOR; |
| 335 | err = misc_register(&serio_raw->dev); |
| 336 | } |
| 337 | |
| 338 | if (err) { |
Dmitry Torokhov | 8d92847 | 2011-10-10 18:30:46 -0700 | [diff] [blame] | 339 | dev_err(&serio->dev, |
| 340 | "failed to register raw access device for %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | serio->phys); |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 342 | goto err_unlink; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Dmitry Torokhov | 8d92847 | 2011-10-10 18:30:46 -0700 | [diff] [blame] | 345 | dev_info(&serio->dev, "raw access enabled on %s (%s, minor %d)\n", |
| 346 | serio->phys, serio_raw->name, serio_raw->dev.minor); |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 347 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 349 | err_unlink: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | list_del_init(&serio_raw->node); |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 351 | err_close: |
| 352 | serio_close(serio); |
| 353 | err_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 355 | kref_put(&serio_raw->kref, serio_raw_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return err; |
| 357 | } |
| 358 | |
| 359 | static int serio_raw_reconnect(struct serio *serio) |
| 360 | { |
| 361 | struct serio_raw *serio_raw = serio_get_drvdata(serio); |
| 362 | struct serio_driver *drv = serio->drv; |
| 363 | |
| 364 | if (!drv || !serio_raw) { |
Dmitry Torokhov | 8d92847 | 2011-10-10 18:30:46 -0700 | [diff] [blame] | 365 | dev_dbg(&serio->dev, |
| 366 | "reconnect request, but serio is disconnected, ignoring...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Nothing needs to be done here, we just need this method to |
| 372 | * keep the same device. |
| 373 | */ |
| 374 | return 0; |
| 375 | } |
| 376 | |
Dmitry Torokhov | 8c1c10d | 2011-10-10 18:31:30 -0700 | [diff] [blame] | 377 | /* |
| 378 | * Wake up users waiting for IO so they can disconnect from |
| 379 | * dead device. |
| 380 | */ |
| 381 | static void serio_raw_hangup(struct serio_raw *serio_raw) |
| 382 | { |
| 383 | struct serio_raw_client *client; |
| 384 | |
| 385 | serio_pause_rx(serio_raw->serio); |
| 386 | list_for_each_entry(client, &serio_raw->client_list, node) |
| 387 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); |
| 388 | serio_continue_rx(serio_raw->serio); |
| 389 | |
| 390 | wake_up_interruptible(&serio_raw->wait); |
| 391 | } |
| 392 | |
| 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | static void serio_raw_disconnect(struct serio *serio) |
| 395 | { |
Dmitry Torokhov | 8c1c10d | 2011-10-10 18:31:30 -0700 | [diff] [blame] | 396 | struct serio_raw *serio_raw = serio_get_drvdata(serio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 398 | misc_deregister(&serio_raw->dev); |
| 399 | |
Arjan van de Ven | c4e32e9 | 2006-02-19 00:21:55 -0500 | [diff] [blame] | 400 | mutex_lock(&serio_raw_mutex); |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 401 | serio_raw->dead = true; |
| 402 | list_del_init(&serio_raw->node); |
| 403 | mutex_unlock(&serio_raw_mutex); |
| 404 | |
| 405 | serio_raw_hangup(serio_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | serio_close(serio); |
Dmitry Torokhov | 550eca7 | 2011-10-10 18:31:39 -0700 | [diff] [blame] | 408 | kref_put(&serio_raw->kref, serio_raw_free); |
Dmitry Torokhov | 8c1c10d | 2011-10-10 18:31:30 -0700 | [diff] [blame] | 409 | |
| 410 | serio_set_drvdata(serio, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Arvind Yadav | 0aea22b | 2017-08-18 17:10:24 -0700 | [diff] [blame] | 413 | static const struct serio_device_id serio_raw_serio_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
| 415 | .type = SERIO_8042, |
| 416 | .proto = SERIO_ANY, |
| 417 | .id = SERIO_ANY, |
| 418 | .extra = SERIO_ANY, |
| 419 | }, |
Niels de Vos | d19497e | 2008-09-04 22:20:11 -0400 | [diff] [blame] | 420 | { |
| 421 | .type = SERIO_8042_XL, |
| 422 | .proto = SERIO_ANY, |
| 423 | .id = SERIO_ANY, |
| 424 | .extra = SERIO_ANY, |
| 425 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { 0 } |
| 427 | }; |
| 428 | |
| 429 | MODULE_DEVICE_TABLE(serio, serio_raw_serio_ids); |
| 430 | |
| 431 | static struct serio_driver serio_raw_drv = { |
| 432 | .driver = { |
| 433 | .name = "serio_raw", |
| 434 | }, |
| 435 | .description = DRIVER_DESC, |
| 436 | .id_table = serio_raw_serio_ids, |
| 437 | .interrupt = serio_raw_interrupt, |
| 438 | .connect = serio_raw_connect, |
| 439 | .reconnect = serio_raw_reconnect, |
| 440 | .disconnect = serio_raw_disconnect, |
Dmitry Torokhov | 8c31eb0 | 2011-10-10 18:30:36 -0700 | [diff] [blame] | 441 | .manual_bind = true, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | }; |
| 443 | |
Axel Lin | 65ac9f7 | 2012-04-03 23:50:17 -0700 | [diff] [blame] | 444 | module_serio_driver(serio_raw_drv); |