2 * lirc_sasem.c - USB remote support for LIRC
5 * Copyright (C) 2004-2005 Oliver Stabel <oliver.stabel@gmx.de>
6 * Tim Davies <tim@opensystems.net.au>
8 * This driver was derived from:
9 * Venky Raju <dev@venky.ws>
10 * "lirc_imon - "LIRC/VFD driver for Ahanix/Soundgraph IMON IR/VFD"
11 * Paul Miller <pmiller9@users.sourceforge.net>'s 2003-2004
12 * "lirc_atiusb - USB remote support for LIRC"
13 * Culver Consulting Services <henry@culcon.com>'s 2003
14 * "Sasem OnAir VFD/IR USB driver"
17 * NOTE - The LCDproc iMon driver should work with this module. More info at
18 * http://www.frogstorm.info/sasem
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39 #include <linux/errno.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/uaccess.h>
44 #include <linux/usb.h>
45 #include <linux/ktime.h>
47 #include <media/lirc.h>
48 #include <media/lirc_dev.h>
51 #define MOD_AUTHOR "Oliver Stabel <oliver.stabel@gmx.de>, " \
52 "Tim Davies <tim@opensystems.net.au>"
53 #define MOD_DESC "USB Driver for Sasem Remote Controller V1.1"
54 #define MOD_NAME "lirc_sasem"
55 #define MOD_VERSION "0.5"
57 #define VFD_MINOR_BASE 144 /* Same as LCD */
58 #define DEVICE_NAME "lcd%d"
60 #define BUF_CHUNK_SIZE 8
63 #define IOCTL_LCD_CONTRAST 1
65 /*** P R O T O T Y P E S ***/
67 /* USB Callback prototypes */
68 static int sasem_probe(struct usb_interface
*interface
,
69 const struct usb_device_id
*id
);
70 static void sasem_disconnect(struct usb_interface
*interface
);
71 static void usb_rx_callback(struct urb
*urb
);
72 static void usb_tx_callback(struct urb
*urb
);
74 /* VFD file_operations function prototypes */
75 static int vfd_open(struct inode
*inode
, struct file
*file
);
76 static long vfd_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
);
77 static int vfd_close(struct inode
*inode
, struct file
*file
);
78 static ssize_t
vfd_write(struct file
*file
, const char __user
*buf
,
79 size_t n_bytes
, loff_t
*pos
);
81 /* LIRC driver function prototypes */
82 static int ir_open(void *data
);
83 static void ir_close(void *data
);
85 /*** G L O B A L S ***/
86 #define SASEM_DATA_BUF_SZ 32
88 struct sasem_context
{
90 struct usb_device
*dev
;
91 int vfd_isopen
; /* VFD port has been opened */
92 unsigned int vfd_contrast
; /* VFD contrast */
93 int ir_isopen
; /* IR port has been opened */
94 int dev_present
; /* USB device presence */
95 struct mutex ctx_lock
; /* to lock this object */
96 wait_queue_head_t remove_ok
; /* For unexpected USB disconnects */
98 struct lirc_driver
*driver
;
99 struct usb_endpoint_descriptor
*rx_endpoint
;
100 struct usb_endpoint_descriptor
*tx_endpoint
;
103 unsigned char usb_rx_buf
[8];
104 unsigned char usb_tx_buf
[8];
107 unsigned char data_buf
[SASEM_DATA_BUF_SZ
]; /* user data
109 struct completion finished
; /* wait for write to finish */
110 atomic_t busy
; /* write in progress */
111 int status
; /* status of tx completion */
114 /* for dealing with repeat codes (wish there was a toggle bit!) */
120 /* VFD file operations */
121 static const struct file_operations vfd_fops
= {
122 .owner
= THIS_MODULE
,
125 .unlocked_ioctl
= &vfd_ioctl
,
126 .release
= &vfd_close
,
127 .llseek
= noop_llseek
,
130 /* USB Device ID for Sasem USB Control Board */
131 static struct usb_device_id sasem_usb_id_table
[] = {
132 /* Sasem USB Control Board */
133 { USB_DEVICE(0x11ba, 0x0101) },
134 /* Terminating entry */
138 /* USB Device data */
139 static struct usb_driver sasem_driver
= {
141 .probe
= sasem_probe
,
142 .disconnect
= sasem_disconnect
,
143 .id_table
= sasem_usb_id_table
,
146 static struct usb_class_driver sasem_class
= {
149 .minor_base
= VFD_MINOR_BASE
,
152 /* to prevent races between open() and disconnect() */
153 static DEFINE_MUTEX(disconnect_lock
);
158 /*** M O D U L E C O D E ***/
160 MODULE_AUTHOR(MOD_AUTHOR
);
161 MODULE_DESCRIPTION(MOD_DESC
);
162 MODULE_LICENSE("GPL");
163 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
164 MODULE_PARM_DESC(debug
, "Debug messages: 0=no, 1=yes (default: no)");
166 static void delete_context(struct sasem_context
*context
)
168 usb_free_urb(context
->tx_urb
); /* VFD */
169 usb_free_urb(context
->rx_urb
); /* IR */
170 lirc_buffer_free(context
->driver
->rbuf
);
171 kfree(context
->driver
->rbuf
);
172 kfree(context
->driver
);
176 static void deregister_from_lirc(struct sasem_context
*context
)
179 int minor
= context
->driver
->minor
;
181 retval
= lirc_unregister_driver(minor
);
183 dev_err(&context
->dev
->dev
,
184 "%s: unable to deregister from lirc (%d)\n",
187 dev_info(&context
->dev
->dev
,
188 "Deregistered Sasem driver (minor:%d)\n", minor
);
193 * Called when the VFD device (e.g. /dev/usb/lcd)
194 * is opened by the application.
196 static int vfd_open(struct inode
*inode
, struct file
*file
)
198 struct usb_interface
*interface
;
199 struct sasem_context
*context
= NULL
;
203 /* prevent races with disconnect */
204 mutex_lock(&disconnect_lock
);
206 subminor
= iminor(inode
);
207 interface
= usb_find_interface(&sasem_driver
, subminor
);
209 pr_err("%s: could not find interface for minor %d\n",
214 context
= usb_get_intfdata(interface
);
217 dev_err(&interface
->dev
, "no context found for minor %d\n",
223 mutex_lock(&context
->ctx_lock
);
225 if (context
->vfd_isopen
) {
226 dev_err(&interface
->dev
,
227 "%s: VFD port is already open", __func__
);
230 context
->vfd_isopen
= 1;
231 file
->private_data
= context
;
232 dev_info(&interface
->dev
, "VFD port opened\n");
235 mutex_unlock(&context
->ctx_lock
);
238 mutex_unlock(&disconnect_lock
);
243 * Called when the VFD device (e.g. /dev/usb/lcd)
244 * is closed by the application.
246 static long vfd_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
248 struct sasem_context
*context
;
250 context
= (struct sasem_context
*) file
->private_data
;
253 pr_err("%s: no context for device\n", __func__
);
257 mutex_lock(&context
->ctx_lock
);
260 case IOCTL_LCD_CONTRAST
:
263 context
->vfd_contrast
= (unsigned int)arg
;
266 pr_info("Unknown IOCTL command\n");
267 mutex_unlock(&context
->ctx_lock
);
268 return -ENOIOCTLCMD
; /* not supported */
271 mutex_unlock(&context
->ctx_lock
);
276 * Called when the VFD device (e.g. /dev/usb/lcd)
277 * is closed by the application.
279 static int vfd_close(struct inode
*inode
, struct file
*file
)
281 struct sasem_context
*context
= NULL
;
284 context
= (struct sasem_context
*) file
->private_data
;
287 pr_err("%s: no context for device\n", __func__
);
291 mutex_lock(&context
->ctx_lock
);
293 if (!context
->vfd_isopen
) {
294 dev_err(&context
->dev
->dev
, "%s: VFD is not open\n", __func__
);
297 context
->vfd_isopen
= 0;
298 dev_info(&context
->dev
->dev
, "VFD port closed\n");
299 if (!context
->dev_present
&& !context
->ir_isopen
) {
301 /* Device disconnected before close and IR port is
302 * not open. If IR port is open, context will be
303 * deleted by ir_close. */
304 mutex_unlock(&context
->ctx_lock
);
305 delete_context(context
);
310 mutex_unlock(&context
->ctx_lock
);
315 * Sends a packet to the VFD.
317 static int send_packet(struct sasem_context
*context
)
323 pipe
= usb_sndintpipe(context
->dev
,
324 context
->tx_endpoint
->bEndpointAddress
);
325 interval
= context
->tx_endpoint
->bInterval
;
327 usb_fill_int_urb(context
->tx_urb
, context
->dev
, pipe
,
328 context
->usb_tx_buf
, sizeof(context
->usb_tx_buf
),
329 usb_tx_callback
, context
, interval
);
331 context
->tx_urb
->actual_length
= 0;
333 init_completion(&context
->tx
.finished
);
334 atomic_set(&context
->tx
.busy
, 1);
336 retval
= usb_submit_urb(context
->tx_urb
, GFP_KERNEL
);
338 atomic_set(&context
->tx
.busy
, 0);
339 dev_err(&context
->dev
->dev
, "error submitting urb (%d)\n",
342 /* Wait for transmission to complete (or abort) */
343 mutex_unlock(&context
->ctx_lock
);
344 wait_for_completion(&context
->tx
.finished
);
345 mutex_lock(&context
->ctx_lock
);
347 retval
= context
->tx
.status
;
349 dev_err(&context
->dev
->dev
,
350 "packet tx failed (%d)\n", retval
);
357 * Writes data to the VFD. The Sasem VFD is 2x16 characters
358 * and requires data in 9 consecutive USB interrupt packets,
359 * each packet carrying 8 bytes.
361 static ssize_t
vfd_write(struct file
*file
, const char __user
*buf
,
362 size_t n_bytes
, loff_t
*pos
)
366 struct sasem_context
*context
;
367 int *data_buf
= NULL
;
369 context
= (struct sasem_context
*) file
->private_data
;
371 pr_err("%s: no context for device\n", __func__
);
375 mutex_lock(&context
->ctx_lock
);
377 if (!context
->dev_present
) {
378 pr_err("%s: no Sasem device present\n", __func__
);
383 if (n_bytes
<= 0 || n_bytes
> SASEM_DATA_BUF_SZ
) {
384 dev_err(&context
->dev
->dev
, "%s: invalid payload size\n",
390 data_buf
= memdup_user(buf
, n_bytes
);
391 if (IS_ERR(data_buf
)) {
392 retval
= PTR_ERR(data_buf
);
397 memcpy(context
->tx
.data_buf
, data_buf
, n_bytes
);
399 /* Pad with spaces */
400 for (i
= n_bytes
; i
< SASEM_DATA_BUF_SZ
; ++i
)
401 context
->tx
.data_buf
[i
] = ' ';
403 /* Nine 8 byte packets to be sent */
404 /* NOTE: "\x07\x01\0\0\0\0\0\0" or "\x0c\0\0\0\0\0\0\0"
405 * will clear the VFD */
406 for (i
= 0; i
< 9; i
++) {
409 memcpy(context
->usb_tx_buf
, "\x07\0\0\0\0\0\0\0", 8);
410 context
->usb_tx_buf
[1] = (context
->vfd_contrast
) ?
411 (0x2B - (context
->vfd_contrast
- 1) / 250)
415 memcpy(context
->usb_tx_buf
, "\x09\x01\0\0\0\0\0\0", 8);
418 memcpy(context
->usb_tx_buf
, "\x0b\x01\0\0\0\0\0\0", 8);
421 memcpy(context
->usb_tx_buf
, context
->tx
.data_buf
, 8);
424 memcpy(context
->usb_tx_buf
,
425 context
->tx
.data_buf
+ 8, 8);
428 memcpy(context
->usb_tx_buf
, "\x09\x01\0\0\0\0\0\0", 8);
431 memcpy(context
->usb_tx_buf
, "\x0b\x02\0\0\0\0\0\0", 8);
434 memcpy(context
->usb_tx_buf
,
435 context
->tx
.data_buf
+ 16, 8);
438 memcpy(context
->usb_tx_buf
,
439 context
->tx
.data_buf
+ 24, 8);
442 retval
= send_packet(context
);
444 dev_err(&context
->dev
->dev
,
445 "send packet failed for packet #%d\n", i
);
451 mutex_unlock(&context
->ctx_lock
);
454 return (!retval
) ? n_bytes
: retval
;
458 * Callback function for USB core API: transmit data
460 static void usb_tx_callback(struct urb
*urb
)
462 struct sasem_context
*context
;
466 context
= (struct sasem_context
*) urb
->context
;
470 context
->tx
.status
= urb
->status
;
472 /* notify waiters that write has finished */
473 atomic_set(&context
->tx
.busy
, 0);
474 complete(&context
->tx
.finished
);
478 * Called by lirc_dev when the application opens /dev/lirc
480 static int ir_open(void *data
)
483 struct sasem_context
*context
;
485 /* prevent races with disconnect */
486 mutex_lock(&disconnect_lock
);
490 mutex_lock(&context
->ctx_lock
);
492 if (context
->ir_isopen
) {
493 dev_err(&context
->dev
->dev
, "%s: IR port is already open\n",
499 usb_fill_int_urb(context
->rx_urb
, context
->dev
,
500 usb_rcvintpipe(context
->dev
,
501 context
->rx_endpoint
->bEndpointAddress
),
502 context
->usb_rx_buf
, sizeof(context
->usb_rx_buf
),
503 usb_rx_callback
, context
, context
->rx_endpoint
->bInterval
);
505 retval
= usb_submit_urb(context
->rx_urb
, GFP_KERNEL
);
508 dev_err(&context
->dev
->dev
,
509 "usb_submit_urb failed for ir_open (%d)\n", retval
);
511 context
->ir_isopen
= 1;
512 dev_info(&context
->dev
->dev
, "IR port opened\n");
516 mutex_unlock(&context
->ctx_lock
);
518 mutex_unlock(&disconnect_lock
);
523 * Called by lirc_dev when the application closes /dev/lirc
525 static void ir_close(void *data
)
527 struct sasem_context
*context
;
531 pr_err("%s: no context for device\n", __func__
);
535 mutex_lock(&context
->ctx_lock
);
537 usb_kill_urb(context
->rx_urb
);
538 context
->ir_isopen
= 0;
539 pr_info("IR port closed\n");
541 if (!context
->dev_present
) {
544 * Device disconnected while IR port was
545 * still open. Driver was not deregistered
546 * at disconnect time, so do it now.
548 deregister_from_lirc(context
);
550 if (!context
->vfd_isopen
) {
552 mutex_unlock(&context
->ctx_lock
);
553 delete_context(context
);
556 /* If VFD port is open, context will be deleted by vfd_close */
559 mutex_unlock(&context
->ctx_lock
);
563 * Process the incoming packet
565 static void incoming_packet(struct sasem_context
*context
,
568 int len
= urb
->actual_length
;
569 unsigned char *buf
= urb
->transfer_buffer
;
574 dev_warn(&context
->dev
->dev
,
575 "%s: invalid incoming packet size (%d)\n",
581 dev_info(&context
->dev
->dev
, "Incoming data: %*ph\n", len
, buf
);
583 * Lirc could deal with the repeat code, but we really need to block it
584 * if it arrives too late. Otherwise we could repeat the wrong code.
587 /* get the time since the last button press */
589 ns
= ktime_to_ns(ktime_sub(kt
, context
->presstime
));
591 if (memcmp(buf
, "\x08\0\0\0\0\0\0\0", 8) == 0) {
593 * the repeat code is being sent, so we copy
594 * the old code to LIRC
598 * NOTE: Only if the last code was less than 250ms ago
599 * - no one should be able to push another (undetected) button
600 * in that time and then get a false repeat of the previous
601 * press but it is long enough for a genuine repeat
603 if ((ns
< 250 * NSEC_PER_MSEC
) && (context
->codesaved
!= 0)) {
604 memcpy(buf
, &context
->lastcode
, 8);
605 context
->presstime
= kt
;
608 /* save the current valid code for repeats */
609 memcpy(&context
->lastcode
, buf
, 8);
611 * set flag to signal a valid code was save;
612 * just for safety reasons
614 context
->codesaved
= 1;
615 context
->presstime
= kt
;
618 lirc_buffer_write(context
->driver
->rbuf
, buf
);
619 wake_up(&context
->driver
->rbuf
->wait_poll
);
623 * Callback function for USB core API: receive data
625 static void usb_rx_callback(struct urb
*urb
)
627 struct sasem_context
*context
;
631 context
= (struct sasem_context
*) urb
->context
;
635 switch (urb
->status
) {
637 case -ENOENT
: /* usbcore unlink successful! */
641 if (context
->ir_isopen
)
642 incoming_packet(context
, urb
);
646 dev_warn(&urb
->dev
->dev
, "%s: status (%d): ignored",
647 __func__
, urb
->status
);
651 usb_submit_urb(context
->rx_urb
, GFP_ATOMIC
);
657 * Callback function for USB core API: Probe
659 static int sasem_probe(struct usb_interface
*interface
,
660 const struct usb_device_id
*id
)
662 struct usb_device
*dev
= NULL
;
663 struct usb_host_interface
*iface_desc
= NULL
;
664 struct usb_endpoint_descriptor
*rx_endpoint
= NULL
;
665 struct usb_endpoint_descriptor
*tx_endpoint
= NULL
;
666 struct urb
*rx_urb
= NULL
;
667 struct urb
*tx_urb
= NULL
;
668 struct lirc_driver
*driver
= NULL
;
669 struct lirc_buffer
*rbuf
= NULL
;
676 struct sasem_context
*context
= NULL
;
679 dev_info(&interface
->dev
, "%s: found Sasem device\n", __func__
);
682 dev
= usb_get_dev(interface_to_usbdev(interface
));
683 iface_desc
= interface
->cur_altsetting
;
684 num_endpoints
= iface_desc
->desc
.bNumEndpoints
;
687 * Scan the endpoint list and set:
688 * first input endpoint = IR endpoint
689 * first output endpoint = VFD endpoint
695 for (i
= 0; i
< num_endpoints
&& !(ir_ep_found
&& vfd_ep_found
); ++i
) {
697 struct usb_endpoint_descriptor
*ep
;
699 ep
= &iface_desc
->endpoint
[i
].desc
;
702 usb_endpoint_is_int_in(ep
)) {
707 dev_info(&interface
->dev
,
708 "%s: found IR endpoint\n", __func__
);
710 } else if (!vfd_ep_found
&&
711 usb_endpoint_is_int_out(ep
)) {
716 dev_info(&interface
->dev
,
717 "%s: found VFD endpoint\n", __func__
);
721 /* Input endpoint is mandatory */
723 dev_err(&interface
->dev
,
724 "%s: no valid input (IR) endpoint found.\n", __func__
);
730 dev_info(&interface
->dev
,
731 "%s: no valid output (VFD) endpoint found.\n",
735 /* Allocate memory */
738 context
= kzalloc(sizeof(struct sasem_context
), GFP_KERNEL
);
741 goto alloc_status_switch
;
743 driver
= kzalloc(sizeof(struct lirc_driver
), GFP_KERNEL
);
746 goto alloc_status_switch
;
748 rbuf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
751 goto alloc_status_switch
;
753 if (lirc_buffer_init(rbuf
, BUF_CHUNK_SIZE
, BUF_SIZE
)) {
754 dev_err(&interface
->dev
,
755 "%s: lirc_buffer_init failed\n", __func__
);
757 goto alloc_status_switch
;
759 rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
761 dev_err(&interface
->dev
,
762 "%s: usb_alloc_urb failed for IR urb\n", __func__
);
764 goto alloc_status_switch
;
767 tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
769 dev_err(&interface
->dev
,
770 "%s: usb_alloc_urb failed for VFD urb",
773 goto alloc_status_switch
;
777 mutex_init(&context
->ctx_lock
);
779 strcpy(driver
->name
, MOD_NAME
);
781 driver
->code_length
= 64;
782 driver
->sample_rate
= 0;
783 driver
->features
= LIRC_CAN_REC_LIRCCODE
;
784 driver
->data
= context
;
786 driver
->set_use_inc
= ir_open
;
787 driver
->set_use_dec
= ir_close
;
788 driver
->dev
= &interface
->dev
;
789 driver
->owner
= THIS_MODULE
;
791 mutex_lock(&context
->ctx_lock
);
793 lirc_minor
= lirc_register_driver(driver
);
794 if (lirc_minor
< 0) {
795 dev_err(&interface
->dev
,
796 "%s: lirc_register_driver failed\n", __func__
);
801 dev_info(&interface
->dev
,
802 "%s: Registered Sasem driver (minor:%d)\n",
803 __func__
, lirc_minor
);
805 /* Needed while unregistering! */
806 driver
->minor
= lirc_minor
;
809 context
->dev_present
= 1;
810 context
->rx_endpoint
= rx_endpoint
;
811 context
->rx_urb
= rx_urb
;
813 context
->tx_endpoint
= tx_endpoint
;
814 context
->tx_urb
= tx_urb
;
815 context
->vfd_contrast
= 1000; /* range 0 - 1000 */
817 context
->driver
= driver
;
819 usb_set_intfdata(interface
, context
);
824 dev_info(&interface
->dev
,
825 "Registering VFD with sysfs\n");
826 if (usb_register_dev(interface
, &sasem_class
))
827 /* Not a fatal error, so ignore */
828 dev_info(&interface
->dev
,
829 "%s: could not get a minor number for VFD\n",
833 dev_info(&interface
->dev
,
834 "%s: Sasem device on usb<%d:%d> initialized\n",
835 __func__
, dev
->bus
->busnum
, dev
->devnum
);
837 mutex_unlock(&context
->ctx_lock
);
840 switch (alloc_status
) {
844 usb_free_urb(tx_urb
);
846 usb_free_urb(rx_urb
);
849 lirc_buffer_free(rbuf
);
871 * Callback function for USB core API: disconnect
873 static void sasem_disconnect(struct usb_interface
*interface
)
875 struct sasem_context
*context
;
877 /* prevent races with ir_open()/vfd_open() */
878 mutex_lock(&disconnect_lock
);
880 context
= usb_get_intfdata(interface
);
881 mutex_lock(&context
->ctx_lock
);
883 dev_info(&interface
->dev
, "%s: Sasem device disconnected\n",
886 usb_set_intfdata(interface
, NULL
);
887 context
->dev_present
= 0;
890 usb_kill_urb(context
->rx_urb
);
892 /* Abort ongoing write */
893 if (atomic_read(&context
->tx
.busy
)) {
895 usb_kill_urb(context
->tx_urb
);
896 wait_for_completion(&context
->tx
.finished
);
899 /* De-register from lirc_dev if IR port is not open */
900 if (!context
->ir_isopen
)
901 deregister_from_lirc(context
);
903 usb_deregister_dev(interface
, &sasem_class
);
905 mutex_unlock(&context
->ctx_lock
);
907 if (!context
->ir_isopen
&& !context
->vfd_isopen
)
908 delete_context(context
);
910 mutex_unlock(&disconnect_lock
);
913 module_usb_driver(sasem_driver
);