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