blob: 64fcefb50fbe00f2fb3d045b8f13f994fec8fc0e [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;
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070041 struct list_head client_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct list_head node;
43};
44
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070045struct serio_raw_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 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{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070061 struct serio_raw_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070063 return fasync_helper(fd, file, on, &client->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;
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070081 struct serio_raw_client *client;
82 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
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
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070099 client = kzalloc(sizeof(struct serio_raw_client), GFP_KERNEL);
100 if (!client) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 retval = -ENOMEM;
102 goto out;
103 }
104
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700105 client->serio_raw = serio_raw;
106 file->private_data = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700108 kref_get(&serio_raw->kref);
Dmitry Torokhov843e7842011-10-10 18:30:26 -0700109
110 serio_pause_rx(serio_raw->serio);
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700111 list_add_tail(&client->node, &serio_raw->client_list);
Dmitry Torokhov843e7842011-10-10 18:30:26 -0700112 serio_continue_rx(serio_raw->serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500115 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return retval;
117}
118
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700119static void serio_raw_cleanup(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700121 struct serio_raw *serio_raw =
122 container_of(kref, struct serio_raw, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700124 misc_deregister(&serio_raw->dev);
125 list_del_init(&serio_raw->node);
126 kfree(serio_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static int serio_raw_release(struct inode *inode, struct file *file)
130{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700131 struct serio_raw_client *client = file->private_data;
132 struct serio_raw *serio_raw = client->serio_raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500134 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700136 kref_put(&serio_raw->kref, serio_raw_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500138 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return 0;
140}
141
Dmitry Torokhov8c31eb02011-10-10 18:30:36 -0700142static bool serio_raw_fetch_byte(struct serio_raw *serio_raw, char *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Dmitry Torokhov8c31eb02011-10-10 18:30:36 -0700144 bool empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Dmitry Torokhov843e7842011-10-10 18:30:26 -0700146 serio_pause_rx(serio_raw->serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 empty = serio_raw->head == serio_raw->tail;
149 if (!empty) {
150 *c = serio_raw->queue[serio_raw->tail];
151 serio_raw->tail = (serio_raw->tail + 1) % SERIO_RAW_QUEUE_LEN;
152 }
153
Dmitry Torokhov843e7842011-10-10 18:30:26 -0700154 serio_continue_rx(serio_raw->serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return !empty;
157}
158
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700159static ssize_t serio_raw_read(struct file *file, char __user *buffer,
160 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700162 struct serio_raw_client *client = file->private_data;
163 struct serio_raw *serio_raw = client->serio_raw;
Andrew Morton4f179f72007-07-10 00:38:31 -0400164 char uninitialized_var(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ssize_t retval = 0;
166
167 if (!serio_raw->serio)
168 return -ENODEV;
169
170 if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
171 return -EAGAIN;
172
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700173 retval = wait_event_interruptible(serio_raw->wait,
174 serio_raw->head != serio_raw->tail ||
175 !serio_raw->serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (retval)
177 return retval;
178
179 if (!serio_raw->serio)
180 return -ENODEV;
181
182 while (retval < count && serio_raw_fetch_byte(serio_raw, &c)) {
183 if (put_user(c, buffer++))
184 return -EFAULT;
185 retval++;
186 }
187
188 return retval;
189}
190
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700191static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
192 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700194 struct serio_raw_client *client = file->private_data;
195 struct serio_raw *serio_raw = client->serio_raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ssize_t written = 0;
197 int retval;
198 unsigned char c;
199
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500200 retval = mutex_lock_interruptible(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (retval)
202 return retval;
203
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700204 if (!serio_raw->serio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 retval = -ENODEV;
206 goto out;
207 }
208
209 if (count > 32)
210 count = 32;
211
212 while (count--) {
213 if (get_user(c, buffer++)) {
214 retval = -EFAULT;
215 goto out;
216 }
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700217 if (serio_write(serio_raw->serio, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 retval = -EIO;
219 goto out;
220 }
221 written++;
222 };
223
224out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500225 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return written;
227}
228
229static unsigned int serio_raw_poll(struct file *file, poll_table *wait)
230{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700231 struct serio_raw_client *client = file->private_data;
232 struct serio_raw *serio_raw = client->serio_raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700234 poll_wait(file, &serio_raw->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700236 if (serio_raw->head != serio_raw->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return POLLIN | POLLRDNORM;
238
239 return 0;
240}
241
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800242static const struct file_operations serio_raw_fops = {
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700243 .owner = THIS_MODULE,
244 .open = serio_raw_open,
245 .release = serio_raw_release,
246 .read = serio_raw_read,
247 .write = serio_raw_write,
248 .poll = serio_raw_poll,
249 .fasync = serio_raw_fasync,
250 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
253
254/*********************************************************************
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700255 * Interface with serio port *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *********************************************************************/
257
258static irqreturn_t serio_raw_interrupt(struct serio *serio, unsigned char data,
David Howells7d12e782006-10-05 14:55:46 +0100259 unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct serio_raw *serio_raw = serio_get_drvdata(serio);
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700262 struct serio_raw_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned int head = serio_raw->head;
264
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700265 /* we are holding serio->lock here so we are protected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 serio_raw->queue[head] = data;
267 head = (head + 1) % SERIO_RAW_QUEUE_LEN;
268 if (likely(head != serio_raw->tail)) {
269 serio_raw->head = head;
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700270 list_for_each_entry(client, &serio_raw->client_list, node)
271 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 wake_up_interruptible(&serio_raw->wait);
273 }
274
275 return IRQ_HANDLED;
276}
277
278static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
279{
280 struct serio_raw *serio_raw;
281 int err;
282
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700283 serio_raw = kzalloc(sizeof(struct serio_raw), GFP_KERNEL);
284 if (!serio_raw) {
Dmitry Torokhov8d928472011-10-10 18:30:46 -0700285 dev_dbg(&serio->dev, "can't allocate memory for a device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -ENOMEM;
287 }
288
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500289 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700291 snprintf(serio_raw->name, sizeof(serio_raw->name),
292 "serio_raw%d", serio_raw_no++);
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700293 kref_init(&serio_raw->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 serio_raw->serio = serio;
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700295 INIT_LIST_HEAD(&serio_raw->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 init_waitqueue_head(&serio_raw->wait);
297
298 serio_set_drvdata(serio, serio_raw);
299
300 err = serio_open(serio, drv);
301 if (err)
302 goto out_free;
303
304 list_add_tail(&serio_raw->node, &serio_raw_list);
305
306 serio_raw->dev.minor = PSMOUSE_MINOR;
307 serio_raw->dev.name = serio_raw->name;
Greg Kroah-Hartman94fbcde2006-07-27 16:16:04 -0700308 serio_raw->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 serio_raw->dev.fops = &serio_raw_fops;
310
311 err = misc_register(&serio_raw->dev);
312 if (err) {
313 serio_raw->dev.minor = MISC_DYNAMIC_MINOR;
314 err = misc_register(&serio_raw->dev);
315 }
316
317 if (err) {
Dmitry Torokhov8d928472011-10-10 18:30:46 -0700318 dev_err(&serio->dev,
319 "failed to register raw access device for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 serio->phys);
321 goto out_close;
322 }
323
Dmitry Torokhov8d928472011-10-10 18:30:46 -0700324 dev_info(&serio->dev, "raw access enabled on %s (%s, minor %d)\n",
325 serio->phys, serio_raw->name, serio_raw->dev.minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 goto out;
327
328out_close:
329 serio_close(serio);
330 list_del_init(&serio_raw->node);
331out_free:
332 serio_set_drvdata(serio, NULL);
333 kfree(serio_raw);
334out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500335 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return err;
337}
338
339static int serio_raw_reconnect(struct serio *serio)
340{
341 struct serio_raw *serio_raw = serio_get_drvdata(serio);
342 struct serio_driver *drv = serio->drv;
343
344 if (!drv || !serio_raw) {
Dmitry Torokhov8d928472011-10-10 18:30:46 -0700345 dev_dbg(&serio->dev,
346 "reconnect request, but serio is disconnected, ignoring...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -1;
348 }
349
350 /*
351 * Nothing needs to be done here, we just need this method to
352 * keep the same device.
353 */
354 return 0;
355}
356
357static void serio_raw_disconnect(struct serio *serio)
358{
359 struct serio_raw *serio_raw;
360
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500361 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 serio_raw = serio_get_drvdata(serio);
364
365 serio_close(serio);
366 serio_set_drvdata(serio, NULL);
367
368 serio_raw->serio = NULL;
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700369 wake_up_interruptible(&serio_raw->wait);
370 kref_put(&serio_raw->kref, serio_raw_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500372 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static struct serio_device_id serio_raw_serio_ids[] = {
376 {
377 .type = SERIO_8042,
378 .proto = SERIO_ANY,
379 .id = SERIO_ANY,
380 .extra = SERIO_ANY,
381 },
Niels de Vosd19497e2008-09-04 22:20:11 -0400382 {
383 .type = SERIO_8042_XL,
384 .proto = SERIO_ANY,
385 .id = SERIO_ANY,
386 .extra = SERIO_ANY,
387 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 { 0 }
389};
390
391MODULE_DEVICE_TABLE(serio, serio_raw_serio_ids);
392
393static struct serio_driver serio_raw_drv = {
394 .driver = {
395 .name = "serio_raw",
396 },
397 .description = DRIVER_DESC,
398 .id_table = serio_raw_serio_ids,
399 .interrupt = serio_raw_interrupt,
400 .connect = serio_raw_connect,
401 .reconnect = serio_raw_reconnect,
402 .disconnect = serio_raw_disconnect,
Dmitry Torokhov8c31eb02011-10-10 18:30:36 -0700403 .manual_bind = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
406static int __init serio_raw_init(void)
407{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500408 return serio_register_driver(&serio_raw_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411static void __exit serio_raw_exit(void)
412{
413 serio_unregister_driver(&serio_raw_drv);
414}
415
416module_init(serio_raw_init);
417module_exit(serio_raw_exit);