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