blob: ef3a69c304d38bb0242d470ec6532442054dd297 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torokhovba538cd2011-10-10 18:30:03 -070012#include <linux/kref.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040013#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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 Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/miscdevice.h>
22#include <linux/wait.h>
Arjan van de Venc4e32e92006-02-19 00:21:55 -050023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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];
Dmitry Torokhovba538cd2011-10-10 18:30:03 -070037 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct serio *serio;
39 struct miscdevice dev;
40 wait_queue_head_t wait;
41 struct list_head list;
42 struct list_head node;
43};
44
45struct serio_raw_list {
46 struct fasync_struct *fasync;
47 struct serio_raw *serio_raw;
48 struct list_head node;
49};
50
Arjan van de Venc4e32e92006-02-19 00:21:55 -050051static DEFINE_MUTEX(serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static LIST_HEAD(serio_raw_list);
53static unsigned int serio_raw_no;
54
55/*********************************************************************
56 * Interface with userspace (file operations) *
57 *********************************************************************/
58
59static int serio_raw_fasync(int fd, struct file *file, int on)
60{
61 struct serio_raw_list *list = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jonathan Corbet60aa4922009-02-01 14:52:56 -070063 return fasync_helper(fd, file, on, &list->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66static 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
78static int serio_raw_open(struct inode *inode, struct file *file)
79{
80 struct serio_raw *serio_raw;
81 struct serio_raw_list *list;
82 int retval = 0;
83
Arjan van de Venc4e32e92006-02-19 00:21:55 -050084 retval = mutex_lock_interruptible(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (retval)
Thadeu Lima de Souza Cascardo77554b42010-03-09 20:38:47 -080086 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Thadeu Lima de Souza Cascardo77554b42010-03-09 20:38:47 -080088 serio_raw = serio_raw_locate(iminor(inode));
89 if (!serio_raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 retval = -ENODEV;
91 goto out;
92 }
93
94 if (!serio_raw->serio) {
95 retval = -ENODEV;
96 goto out;
97 }
98
Thadeu Lima de Souza Cascardo77554b42010-03-09 20:38:47 -080099 list = kzalloc(sizeof(struct serio_raw_list), GFP_KERNEL);
100 if (!list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 retval = -ENOMEM;
102 goto out;
103 }
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 list->serio_raw = serio_raw;
106 file->private_data = list;
107
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700108 kref_get(&serio_raw->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 list_add_tail(&list->node, &serio_raw->list);
110
111out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500112 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return retval;
114}
115
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700116static void serio_raw_cleanup(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700118 struct serio_raw *serio_raw =
119 container_of(kref, struct serio_raw, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700121 misc_deregister(&serio_raw->dev);
122 list_del_init(&serio_raw->node);
123 kfree(serio_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static int serio_raw_release(struct inode *inode, struct file *file)
127{
128 struct serio_raw_list *list = file->private_data;
129 struct serio_raw *serio_raw = list->serio_raw;
130
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500131 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700133 kref_put(&serio_raw->kref, serio_raw_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500135 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
137}
138
139static int serio_raw_fetch_byte(struct serio_raw *serio_raw, char *c)
140{
141 unsigned long flags;
142 int empty;
143
144 spin_lock_irqsave(&serio_raw->serio->lock, flags);
145
146 empty = serio_raw->head == serio_raw->tail;
147 if (!empty) {
148 *c = serio_raw->queue[serio_raw->tail];
149 serio_raw->tail = (serio_raw->tail + 1) % SERIO_RAW_QUEUE_LEN;
150 }
151
152 spin_unlock_irqrestore(&serio_raw->serio->lock, flags);
153
154 return !empty;
155}
156
157static ssize_t serio_raw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
158{
159 struct serio_raw_list *list = file->private_data;
160 struct serio_raw *serio_raw = list->serio_raw;
Andrew Morton4f179f72007-07-10 00:38:31 -0400161 char uninitialized_var(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 ssize_t retval = 0;
163
164 if (!serio_raw->serio)
165 return -ENODEV;
166
167 if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
168 return -EAGAIN;
169
170 retval = wait_event_interruptible(list->serio_raw->wait,
171 serio_raw->head != serio_raw->tail || !serio_raw->serio);
172 if (retval)
173 return retval;
174
175 if (!serio_raw->serio)
176 return -ENODEV;
177
178 while (retval < count && serio_raw_fetch_byte(serio_raw, &c)) {
179 if (put_user(c, buffer++))
180 return -EFAULT;
181 retval++;
182 }
183
184 return retval;
185}
186
187static ssize_t serio_raw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
188{
189 struct serio_raw_list *list = file->private_data;
190 ssize_t written = 0;
191 int retval;
192 unsigned char c;
193
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500194 retval = mutex_lock_interruptible(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (retval)
196 return retval;
197
198 if (!list->serio_raw->serio) {
199 retval = -ENODEV;
200 goto out;
201 }
202
203 if (count > 32)
204 count = 32;
205
206 while (count--) {
207 if (get_user(c, buffer++)) {
208 retval = -EFAULT;
209 goto out;
210 }
211 if (serio_write(list->serio_raw->serio, c)) {
212 retval = -EIO;
213 goto out;
214 }
215 written++;
216 };
217
218out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500219 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return written;
221}
222
223static unsigned int serio_raw_poll(struct file *file, poll_table *wait)
224{
225 struct serio_raw_list *list = file->private_data;
226
227 poll_wait(file, &list->serio_raw->wait, wait);
228
229 if (list->serio_raw->head != list->serio_raw->tail)
230 return POLLIN | POLLRDNORM;
231
232 return 0;
233}
234
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800235static const struct file_operations serio_raw_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .owner = THIS_MODULE,
237 .open = serio_raw_open,
238 .release = serio_raw_release,
239 .read = serio_raw_read,
240 .write = serio_raw_write,
241 .poll = serio_raw_poll,
242 .fasync = serio_raw_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200243 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
246
247/*********************************************************************
248 * Interface with serio port *
249 *********************************************************************/
250
251static irqreturn_t serio_raw_interrupt(struct serio *serio, unsigned char data,
David Howells7d12e782006-10-05 14:55:46 +0100252 unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 struct serio_raw *serio_raw = serio_get_drvdata(serio);
255 struct serio_raw_list *list;
256 unsigned int head = serio_raw->head;
257
258 /* we are holding serio->lock here so we are prootected */
259 serio_raw->queue[head] = data;
260 head = (head + 1) % SERIO_RAW_QUEUE_LEN;
261 if (likely(head != serio_raw->tail)) {
262 serio_raw->head = head;
263 list_for_each_entry(list, &serio_raw->list, node)
264 kill_fasync(&list->fasync, SIGIO, POLL_IN);
265 wake_up_interruptible(&serio_raw->wait);
266 }
267
268 return IRQ_HANDLED;
269}
270
271static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
272{
273 struct serio_raw *serio_raw;
274 int err;
275
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500276 if (!(serio_raw = kzalloc(sizeof(struct serio_raw), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 printk(KERN_ERR "serio_raw.c: can't allocate memory for a device\n");
278 return -ENOMEM;
279 }
280
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500281 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 snprintf(serio_raw->name, sizeof(serio_raw->name), "serio_raw%d", serio_raw_no++);
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700284 kref_init(&serio_raw->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 serio_raw->serio = serio;
286 INIT_LIST_HEAD(&serio_raw->list);
287 init_waitqueue_head(&serio_raw->wait);
288
289 serio_set_drvdata(serio, serio_raw);
290
291 err = serio_open(serio, drv);
292 if (err)
293 goto out_free;
294
295 list_add_tail(&serio_raw->node, &serio_raw_list);
296
297 serio_raw->dev.minor = PSMOUSE_MINOR;
298 serio_raw->dev.name = serio_raw->name;
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700299 serio_raw->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 serio_raw->dev.fops = &serio_raw_fops;
301
302 err = misc_register(&serio_raw->dev);
303 if (err) {
304 serio_raw->dev.minor = MISC_DYNAMIC_MINOR;
305 err = misc_register(&serio_raw->dev);
306 }
307
308 if (err) {
309 printk(KERN_INFO "serio_raw: failed to register raw access device for %s\n",
310 serio->phys);
311 goto out_close;
312 }
313
314 printk(KERN_INFO "serio_raw: raw access enabled on %s (%s, minor %d)\n",
315 serio->phys, serio_raw->name, serio_raw->dev.minor);
316 goto out;
317
318out_close:
319 serio_close(serio);
320 list_del_init(&serio_raw->node);
321out_free:
322 serio_set_drvdata(serio, NULL);
323 kfree(serio_raw);
324out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500325 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return err;
327}
328
329static int serio_raw_reconnect(struct serio *serio)
330{
331 struct serio_raw *serio_raw = serio_get_drvdata(serio);
332 struct serio_driver *drv = serio->drv;
333
334 if (!drv || !serio_raw) {
335 printk(KERN_DEBUG "serio_raw: reconnect request, but serio is disconnected, ignoring...\n");
336 return -1;
337 }
338
339 /*
340 * Nothing needs to be done here, we just need this method to
341 * keep the same device.
342 */
343 return 0;
344}
345
346static void serio_raw_disconnect(struct serio *serio)
347{
348 struct serio_raw *serio_raw;
349
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500350 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 serio_raw = serio_get_drvdata(serio);
353
354 serio_close(serio);
355 serio_set_drvdata(serio, NULL);
356
357 serio_raw->serio = NULL;
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700358 wake_up_interruptible(&serio_raw->wait);
359 kref_put(&serio_raw->kref, serio_raw_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500361 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364static struct serio_device_id serio_raw_serio_ids[] = {
365 {
366 .type = SERIO_8042,
367 .proto = SERIO_ANY,
368 .id = SERIO_ANY,
369 .extra = SERIO_ANY,
370 },
Niels de Vosd19497e2008-09-04 22:20:11 -0400371 {
372 .type = SERIO_8042_XL,
373 .proto = SERIO_ANY,
374 .id = SERIO_ANY,
375 .extra = SERIO_ANY,
376 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 { 0 }
378};
379
380MODULE_DEVICE_TABLE(serio, serio_raw_serio_ids);
381
382static struct serio_driver serio_raw_drv = {
383 .driver = {
384 .name = "serio_raw",
385 },
386 .description = DRIVER_DESC,
387 .id_table = serio_raw_serio_ids,
388 .interrupt = serio_raw_interrupt,
389 .connect = serio_raw_connect,
390 .reconnect = serio_raw_reconnect,
391 .disconnect = serio_raw_disconnect,
392 .manual_bind = 1,
393};
394
395static int __init serio_raw_init(void)
396{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500397 return serio_register_driver(&serio_raw_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static void __exit serio_raw_exit(void)
401{
402 serio_unregister_driver(&serio_raw_drv);
403}
404
405module_init(serio_raw_init);
406module_exit(serio_raw_exit);