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