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