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
5 * Copyright (c) 2004 Dmitry Torokhov
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.
12 #include <linux/kref.h>
13 #include <linux/sched.h>
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>
21 #include <linux/miscdevice.h>
22 #include <linux/wait.h>
23 #include <linux/mutex.h>
25 #define DRIVER_DESC "Raw serio driver"
27 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
28 MODULE_DESCRIPTION(DRIVER_DESC
);
29 MODULE_LICENSE("GPL");
31 #define SERIO_RAW_QUEUE_LEN 64
33 unsigned char queue
[SERIO_RAW_QUEUE_LEN
];
34 unsigned int tail
, head
;
39 struct miscdevice dev
;
40 wait_queue_head_t wait
;
41 struct list_head client_list
;
42 struct list_head node
;
46 struct serio_raw_client
{
47 struct fasync_struct
*fasync
;
48 struct serio_raw
*serio_raw
;
49 struct list_head node
;
52 static DEFINE_MUTEX(serio_raw_mutex
);
53 static LIST_HEAD(serio_raw_list
);
55 /*********************************************************************
56 * Interface with userspace (file operations) *
57 *********************************************************************/
59 static int serio_raw_fasync(int fd
, struct file
*file
, int on
)
61 struct serio_raw_client
*client
= file
->private_data
;
63 return fasync_helper(fd
, file
, on
, &client
->fasync
);
66 static struct serio_raw
*serio_raw_locate(int minor
)
68 struct serio_raw
*serio_raw
;
70 list_for_each_entry(serio_raw
, &serio_raw_list
, node
) {
71 if (serio_raw
->dev
.minor
== minor
)
78 static int serio_raw_open(struct inode
*inode
, struct file
*file
)
80 struct serio_raw
*serio_raw
;
81 struct serio_raw_client
*client
;
84 retval
= mutex_lock_interruptible(&serio_raw_mutex
);
88 serio_raw
= serio_raw_locate(iminor(inode
));
94 if (serio_raw
->dead
) {
99 client
= kzalloc(sizeof(struct serio_raw_client
), GFP_KERNEL
);
105 client
->serio_raw
= serio_raw
;
106 file
->private_data
= client
;
108 kref_get(&serio_raw
->kref
);
110 serio_pause_rx(serio_raw
->serio
);
111 list_add_tail(&client
->node
, &serio_raw
->client_list
);
112 serio_continue_rx(serio_raw
->serio
);
115 mutex_unlock(&serio_raw_mutex
);
119 static void serio_raw_free(struct kref
*kref
)
121 struct serio_raw
*serio_raw
=
122 container_of(kref
, struct serio_raw
, kref
);
124 put_device(&serio_raw
->serio
->dev
);
128 static int serio_raw_release(struct inode
*inode
, struct file
*file
)
130 struct serio_raw_client
*client
= file
->private_data
;
131 struct serio_raw
*serio_raw
= client
->serio_raw
;
133 serio_pause_rx(serio_raw
->serio
);
134 list_del(&client
->node
);
135 serio_continue_rx(serio_raw
->serio
);
139 kref_put(&serio_raw
->kref
, serio_raw_free
);
144 static bool serio_raw_fetch_byte(struct serio_raw
*serio_raw
, char *c
)
148 serio_pause_rx(serio_raw
->serio
);
150 empty
= serio_raw
->head
== serio_raw
->tail
;
152 *c
= serio_raw
->queue
[serio_raw
->tail
];
153 serio_raw
->tail
= (serio_raw
->tail
+ 1) % SERIO_RAW_QUEUE_LEN
;
156 serio_continue_rx(serio_raw
->serio
);
161 static ssize_t
serio_raw_read(struct file
*file
, char __user
*buffer
,
162 size_t count
, loff_t
*ppos
)
164 struct serio_raw_client
*client
= file
->private_data
;
165 struct serio_raw
*serio_raw
= client
->serio_raw
;
166 char uninitialized_var(c
);
172 if (serio_raw
->head
== serio_raw
->tail
&& (file
->f_flags
& O_NONBLOCK
))
175 retval
= wait_event_interruptible(serio_raw
->wait
,
176 serio_raw
->head
!= serio_raw
->tail
|| serio_raw
->dead
);
183 while (retval
< count
&& serio_raw_fetch_byte(serio_raw
, &c
)) {
184 if (put_user(c
, buffer
++))
192 static ssize_t
serio_raw_write(struct file
*file
, const char __user
*buffer
,
193 size_t count
, loff_t
*ppos
)
195 struct serio_raw_client
*client
= file
->private_data
;
196 struct serio_raw
*serio_raw
= client
->serio_raw
;
201 retval
= mutex_lock_interruptible(&serio_raw_mutex
);
205 if (serio_raw
->dead
) {
214 if (get_user(c
, buffer
++)) {
218 if (serio_write(serio_raw
->serio
, c
)) {
226 mutex_unlock(&serio_raw_mutex
);
230 static unsigned int serio_raw_poll(struct file
*file
, poll_table
*wait
)
232 struct serio_raw_client
*client
= file
->private_data
;
233 struct serio_raw
*serio_raw
= client
->serio_raw
;
236 poll_wait(file
, &serio_raw
->wait
, wait
);
238 mask
= serio_raw
->dead
? POLLHUP
| POLLERR
: POLLOUT
| POLLWRNORM
;
239 if (serio_raw
->head
!= serio_raw
->tail
)
240 return POLLIN
| POLLRDNORM
;
245 static const struct file_operations serio_raw_fops
= {
246 .owner
= THIS_MODULE
,
247 .open
= serio_raw_open
,
248 .release
= serio_raw_release
,
249 .read
= serio_raw_read
,
250 .write
= serio_raw_write
,
251 .poll
= serio_raw_poll
,
252 .fasync
= serio_raw_fasync
,
253 .llseek
= noop_llseek
,
257 /*********************************************************************
258 * Interface with serio port *
259 *********************************************************************/
261 static irqreturn_t
serio_raw_interrupt(struct serio
*serio
, unsigned char data
,
264 struct serio_raw
*serio_raw
= serio_get_drvdata(serio
);
265 struct serio_raw_client
*client
;
266 unsigned int head
= serio_raw
->head
;
268 /* we are holding serio->lock here so we are protected */
269 serio_raw
->queue
[head
] = data
;
270 head
= (head
+ 1) % SERIO_RAW_QUEUE_LEN
;
271 if (likely(head
!= serio_raw
->tail
)) {
272 serio_raw
->head
= head
;
273 list_for_each_entry(client
, &serio_raw
->client_list
, node
)
274 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
275 wake_up_interruptible(&serio_raw
->wait
);
281 static int serio_raw_connect(struct serio
*serio
, struct serio_driver
*drv
)
283 static atomic_t serio_raw_no
= ATOMIC_INIT(0);
284 struct serio_raw
*serio_raw
;
287 serio_raw
= kzalloc(sizeof(struct serio_raw
), GFP_KERNEL
);
289 dev_dbg(&serio
->dev
, "can't allocate memory for a device\n");
293 snprintf(serio_raw
->name
, sizeof(serio_raw
->name
),
294 "serio_raw%ld", (long)atomic_inc_return(&serio_raw_no
) - 1);
295 kref_init(&serio_raw
->kref
);
296 INIT_LIST_HEAD(&serio_raw
->client_list
);
297 init_waitqueue_head(&serio_raw
->wait
);
299 serio_raw
->serio
= serio
;
300 get_device(&serio
->dev
);
302 serio_set_drvdata(serio
, serio_raw
);
304 err
= serio_open(serio
, drv
);
308 err
= mutex_lock_killable(&serio_raw_mutex
);
312 list_add_tail(&serio_raw
->node
, &serio_raw_list
);
313 mutex_unlock(&serio_raw_mutex
);
315 serio_raw
->dev
.minor
= PSMOUSE_MINOR
;
316 serio_raw
->dev
.name
= serio_raw
->name
;
317 serio_raw
->dev
.parent
= &serio
->dev
;
318 serio_raw
->dev
.fops
= &serio_raw_fops
;
320 err
= misc_register(&serio_raw
->dev
);
322 serio_raw
->dev
.minor
= MISC_DYNAMIC_MINOR
;
323 err
= misc_register(&serio_raw
->dev
);
328 "failed to register raw access device for %s\n",
333 dev_info(&serio
->dev
, "raw access enabled on %s (%s, minor %d)\n",
334 serio
->phys
, serio_raw
->name
, serio_raw
->dev
.minor
);
338 list_del_init(&serio_raw
->node
);
342 serio_set_drvdata(serio
, NULL
);
343 kref_put(&serio_raw
->kref
, serio_raw_free
);
347 static int serio_raw_reconnect(struct serio
*serio
)
349 struct serio_raw
*serio_raw
= serio_get_drvdata(serio
);
350 struct serio_driver
*drv
= serio
->drv
;
352 if (!drv
|| !serio_raw
) {
354 "reconnect request, but serio is disconnected, ignoring...\n");
359 * Nothing needs to be done here, we just need this method to
360 * keep the same device.
366 * Wake up users waiting for IO so they can disconnect from
369 static void serio_raw_hangup(struct serio_raw
*serio_raw
)
371 struct serio_raw_client
*client
;
373 serio_pause_rx(serio_raw
->serio
);
374 list_for_each_entry(client
, &serio_raw
->client_list
, node
)
375 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
376 serio_continue_rx(serio_raw
->serio
);
378 wake_up_interruptible(&serio_raw
->wait
);
382 static void serio_raw_disconnect(struct serio
*serio
)
384 struct serio_raw
*serio_raw
= serio_get_drvdata(serio
);
386 misc_deregister(&serio_raw
->dev
);
388 mutex_lock(&serio_raw_mutex
);
389 serio_raw
->dead
= true;
390 list_del_init(&serio_raw
->node
);
391 mutex_unlock(&serio_raw_mutex
);
393 serio_raw_hangup(serio_raw
);
396 kref_put(&serio_raw
->kref
, serio_raw_free
);
398 serio_set_drvdata(serio
, NULL
);
401 static struct serio_device_id serio_raw_serio_ids
[] = {
409 .type
= SERIO_8042_XL
,
417 MODULE_DEVICE_TABLE(serio
, serio_raw_serio_ids
);
419 static struct serio_driver serio_raw_drv
= {
423 .description
= DRIVER_DESC
,
424 .id_table
= serio_raw_serio_ids
,
425 .interrupt
= serio_raw_interrupt
,
426 .connect
= serio_raw_connect
,
427 .reconnect
= serio_raw_reconnect
,
428 .disconnect
= serio_raw_disconnect
,
432 static int __init
serio_raw_init(void)
434 return serio_register_driver(&serio_raw_drv
);
437 static void __exit
serio_raw_exit(void)
439 serio_unregister_driver(&serio_raw_drv
);
442 module_init(serio_raw_init
);
443 module_exit(serio_raw_exit
);