blob: 30ff963881989b2ac0be232750b5348a9f06806b [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;
Dmitry Torokhov85f5b352011-10-10 18:31:04 -070043 bool dead;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070046struct serio_raw_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct fasync_struct *fasync;
48 struct serio_raw *serio_raw;
49 struct list_head node;
50};
51
Arjan van de Venc4e32e92006-02-19 00:21:55 -050052static DEFINE_MUTEX(serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static 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{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070062 struct serio_raw_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070064 return fasync_helper(fd, file, on, &client->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
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;
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -070082 struct serio_raw_client *client;
83 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Arjan van de Venc4e32e92006-02-19 00:21:55 -050085 retval = mutex_lock_interruptible(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (retval)
Thadeu Lima de Souza Cascardo77554b42010-03-09 20:38:47 -080087 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Thadeu Lima de Souza Cascardo77554b42010-03-09 20:38:47 -080089 serio_raw = serio_raw_locate(iminor(inode));
90 if (!serio_raw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 retval = -ENODEV;
92 goto out;
93 }
94
Dmitry Torokhov85f5b352011-10-10 18:31:04 -070095 if (serio_raw->dead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 retval = -ENODEV;
97 goto out;
98 }
99
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700100 client = kzalloc(sizeof(struct serio_raw_client), GFP_KERNEL);
101 if (!client) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 retval = -ENOMEM;
103 goto out;
104 }
105
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700106 client->serio_raw = serio_raw;
107 file->private_data = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700109 kref_get(&serio_raw->kref);
Dmitry Torokhov843e7842011-10-10 18:30:26 -0700110
111 serio_pause_rx(serio_raw->serio);
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700112 list_add_tail(&client->node, &serio_raw->client_list);
Dmitry Torokhov843e7842011-10-10 18:30:26 -0700113 serio_continue_rx(serio_raw->serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500116 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return retval;
118}
119
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700120static void serio_raw_cleanup(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700122 struct serio_raw *serio_raw =
123 container_of(kref, struct serio_raw, kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700125 misc_deregister(&serio_raw->dev);
126 list_del_init(&serio_raw->node);
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700127
128 put_device(&serio_raw->serio->dev);
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700129 kfree(serio_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static int serio_raw_release(struct inode *inode, struct file *file)
133{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700134 struct serio_raw_client *client = file->private_data;
135 struct serio_raw *serio_raw = client->serio_raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500137 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700139 kref_put(&serio_raw->kref, serio_raw_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500141 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
143}
144
Dmitry Torokhov8c31eb02011-10-10 18:30:36 -0700145static bool serio_raw_fetch_byte(struct serio_raw *serio_raw, char *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Dmitry Torokhov8c31eb02011-10-10 18:30:36 -0700147 bool empty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dmitry Torokhov843e7842011-10-10 18:30:26 -0700149 serio_pause_rx(serio_raw->serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
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 Torokhov843e7842011-10-10 18:30:26 -0700157 serio_continue_rx(serio_raw->serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return !empty;
160}
161
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700162static ssize_t serio_raw_read(struct file *file, char __user *buffer,
163 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700165 struct serio_raw_client *client = file->private_data;
166 struct serio_raw *serio_raw = client->serio_raw;
Andrew Morton4f179f72007-07-10 00:38:31 -0400167 char uninitialized_var(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 ssize_t retval = 0;
169
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700170 if (serio_raw->dead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -ENODEV;
172
173 if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
174 return -EAGAIN;
175
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700176 retval = wait_event_interruptible(serio_raw->wait,
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700177 serio_raw->head != serio_raw->tail || serio_raw->dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (retval)
179 return retval;
180
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700181 if (serio_raw->dead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 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 Torokhov15a564d2011-10-10 18:30:56 -0700193static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
194 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700196 struct serio_raw_client *client = file->private_data;
197 struct serio_raw *serio_raw = client->serio_raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ssize_t written = 0;
199 int retval;
200 unsigned char c;
201
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500202 retval = mutex_lock_interruptible(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (retval)
204 return retval;
205
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700206 if (serio_raw->dead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 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 Torokhov7c5bbb22011-10-10 18:30:14 -0700219 if (serio_write(serio_raw->serio, c)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 retval = -EIO;
221 goto out;
222 }
223 written++;
224 };
225
226out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500227 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return written;
229}
230
231static unsigned int serio_raw_poll(struct file *file, poll_table *wait)
232{
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700233 struct serio_raw_client *client = file->private_data;
234 struct serio_raw *serio_raw = client->serio_raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700236 poll_wait(file, &serio_raw->wait, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700238 if (serio_raw->head != serio_raw->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return POLLIN | POLLRDNORM;
240
241 return 0;
242}
243
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800244static const struct file_operations serio_raw_fops = {
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700245 .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 Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
255
256/*********************************************************************
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700257 * Interface with serio port *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *********************************************************************/
259
260static irqreturn_t serio_raw_interrupt(struct serio *serio, unsigned char data,
David Howells7d12e782006-10-05 14:55:46 +0100261 unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct serio_raw *serio_raw = serio_get_drvdata(serio);
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700264 struct serio_raw_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 unsigned int head = serio_raw->head;
266
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700267 /* we are holding serio->lock here so we are protected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 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 Torokhov7c5bbb22011-10-10 18:30:14 -0700272 list_for_each_entry(client, &serio_raw->client_list, node)
273 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 wake_up_interruptible(&serio_raw->wait);
275 }
276
277 return IRQ_HANDLED;
278}
279
280static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
281{
282 struct serio_raw *serio_raw;
283 int err;
284
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700285 serio_raw = kzalloc(sizeof(struct serio_raw), GFP_KERNEL);
286 if (!serio_raw) {
Dmitry Torokhov8d928472011-10-10 18:30:46 -0700287 dev_dbg(&serio->dev, "can't allocate memory for a device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -ENOMEM;
289 }
290
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500291 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Dmitry Torokhov15a564d2011-10-10 18:30:56 -0700293 snprintf(serio_raw->name, sizeof(serio_raw->name),
294 "serio_raw%d", serio_raw_no++);
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700295 kref_init(&serio_raw->kref);
Dmitry Torokhov7c5bbb22011-10-10 18:30:14 -0700296 INIT_LIST_HEAD(&serio_raw->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 init_waitqueue_head(&serio_raw->wait);
298
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700299 serio_raw->serio = serio;
300 get_device(&serio->dev);
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 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-Hartman94fbcde2006-07-27 16:16:04 -0700312 serio_raw->dev.parent = &serio->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 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 Torokhov8d928472011-10-10 18:30:46 -0700322 dev_err(&serio->dev,
323 "failed to register raw access device for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 serio->phys);
325 goto out_close;
326 }
327
Dmitry Torokhov8d928472011-10-10 18:30:46 -0700328 dev_info(&serio->dev, "raw access enabled on %s (%s, minor %d)\n",
329 serio->phys, serio_raw->name, serio_raw->dev.minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 goto out;
331
332out_close:
333 serio_close(serio);
334 list_del_init(&serio_raw->node);
335out_free:
336 serio_set_drvdata(serio, NULL);
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700337 put_device(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 kfree(serio_raw);
339out:
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500340 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return err;
342}
343
344static 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 Torokhov8d928472011-10-10 18:30:46 -0700350 dev_dbg(&serio->dev,
351 "reconnect request, but serio is disconnected, ignoring...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 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
362static void serio_raw_disconnect(struct serio *serio)
363{
364 struct serio_raw *serio_raw;
365
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500366 mutex_lock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 serio_raw = serio_get_drvdata(serio);
369
370 serio_close(serio);
371 serio_set_drvdata(serio, NULL);
372
Dmitry Torokhov85f5b352011-10-10 18:31:04 -0700373 serio_raw->dead = true;
Dmitry Torokhovba538cd2011-10-10 18:30:03 -0700374 wake_up_interruptible(&serio_raw->wait);
375 kref_put(&serio_raw->kref, serio_raw_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500377 mutex_unlock(&serio_raw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380static 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 Vosd19497e2008-09-04 22:20:11 -0400387 {
388 .type = SERIO_8042_XL,
389 .proto = SERIO_ANY,
390 .id = SERIO_ANY,
391 .extra = SERIO_ANY,
392 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 { 0 }
394};
395
396MODULE_DEVICE_TABLE(serio, serio_raw_serio_ids);
397
398static 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 Torokhov8c31eb02011-10-10 18:30:36 -0700408 .manual_bind = true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
411static int __init serio_raw_init(void)
412{
Akinobu Mita153a9df02006-11-23 23:35:10 -0500413 return serio_register_driver(&serio_raw_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416static void __exit serio_raw_exit(void)
417{
418 serio_unregister_driver(&serio_raw_drv);
419}
420
421module_init(serio_raw_init);
422module_exit(serio_raw_exit);