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 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/slab.h>
42 #include <linux/uaccess.h>
43 #include <linux/usb.h>
45 #include <media/lirc.h>
46 #include <media/lirc_dev.h>
49 #define MOD_AUTHOR "Oliver Stabel <oliver.stabel@gmx.de>, " \
50 "Tim Davies <tim@opensystems.net.au>"
51 #define MOD_DESC "USB Driver for Sasem Remote Controller V1.1"
52 #define MOD_NAME "lirc_sasem"
53 #define MOD_VERSION "0.5"
55 #define VFD_MINOR_BASE 144 /* Same as LCD */
56 #define DEVICE_NAME "lcd%d"
58 #define BUF_CHUNK_SIZE 8
61 #define IOCTL_LCD_CONTRAST 1
63 /*** P R O T O T Y P E S ***/
65 /* USB Callback prototypes */
66 static int sasem_probe(struct usb_interface *interface,
67 const struct usb_device_id *id);
68 static void sasem_disconnect(struct usb_interface *interface);
69 static void usb_rx_callback(struct urb *urb);
70 static void usb_tx_callback(struct urb *urb);
72 /* VFD file_operations function prototypes */
73 static int vfd_open(struct inode *inode, struct file *file);
74 static long vfd_ioctl(struct file *file, unsigned cmd, unsigned long arg);
75 static int vfd_close(struct inode *inode, struct file *file);
76 static ssize_t vfd_write(struct file *file, const char *buf,
77 size_t n_bytes, loff_t *pos);
79 /* LIRC driver function prototypes */
80 static int ir_open(void *data);
81 static void ir_close(void *data);
83 /* Driver init/exit prototypes */
84 static int __init sasem_init(void);
85 static void __exit sasem_exit(void);
87 /*** G L O B A L S ***/
88 #define SASEM_DATA_BUF_SZ 32
90 struct sasem_context {
92 struct usb_device *dev;
93 int vfd_isopen; /* VFD port has been opened */
94 unsigned int vfd_contrast; /* VFD contrast */
95 int ir_isopen; /* IR port has been opened */
96 int dev_present; /* USB device presence */
97 struct mutex ctx_lock; /* to lock this object */
98 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
100 struct lirc_driver *driver;
101 struct usb_endpoint_descriptor *rx_endpoint;
102 struct usb_endpoint_descriptor *tx_endpoint;
105 unsigned char usb_rx_buf[8];
106 unsigned char usb_tx_buf[8];
109 unsigned char data_buf[SASEM_DATA_BUF_SZ]; /* user data buffer */
110 struct completion finished; /* wait for write to finish */
111 atomic_t busy; /* write in progress */
112 int status; /* status of tx completion */
115 /* for dealing with repeat codes (wish there was a toggle bit!) */
116 struct timeval presstime;
121 /* VFD file operations */
122 static const struct file_operations vfd_fops = {
123 .owner = THIS_MODULE,
126 .unlocked_ioctl = &vfd_ioctl,
127 .release = &vfd_close,
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 printk(KERN_INFO "%s: context deleted\n", __func__);
179 static void deregister_from_lirc(struct sasem_context *context)
182 int minor = context->driver->minor;
184 retval = lirc_unregister_driver(minor);
186 err("%s: unable to deregister from lirc (%d)",
189 printk(KERN_INFO "Deregistered Sasem driver (minor:%d)\n",
195 * Called when the VFD device (e.g. /dev/usb/lcd)
196 * is opened by the application.
198 static int vfd_open(struct inode *inode, struct file *file)
200 struct usb_interface *interface;
201 struct sasem_context *context = NULL;
205 /* prevent races with disconnect */
206 mutex_lock(&disconnect_lock);
208 subminor = iminor(inode);
209 interface = usb_find_interface(&sasem_driver, subminor);
211 err("%s: could not find interface for minor %d",
216 context = usb_get_intfdata(interface);
219 err("%s: no context found for minor %d",
225 mutex_lock(&context->ctx_lock);
227 if (context->vfd_isopen) {
228 err("%s: VFD port is already open", __func__);
231 context->vfd_isopen = 1;
232 file->private_data = context;
233 printk(KERN_INFO "VFD port opened\n");
236 mutex_unlock(&context->ctx_lock);
239 mutex_unlock(&disconnect_lock);
244 * Called when the VFD device (e.g. /dev/usb/lcd)
245 * is closed by the application.
247 static long vfd_ioctl(struct file *file, unsigned cmd, unsigned long arg)
249 struct sasem_context *context = NULL;
251 context = (struct sasem_context *) file->private_data;
254 err("%s: no context for device", __func__);
258 mutex_lock(&context->ctx_lock);
261 case IOCTL_LCD_CONTRAST:
264 context->vfd_contrast = (unsigned int)arg;
267 printk(KERN_INFO "Unknown IOCTL command\n");
268 mutex_unlock(&context->ctx_lock);
269 return -ENOIOCTLCMD; /* not supported */
272 mutex_unlock(&context->ctx_lock);
277 * Called when the VFD device (e.g. /dev/usb/lcd)
278 * is closed by the application.
280 static int vfd_close(struct inode *inode, struct file *file)
282 struct sasem_context *context = NULL;
285 context = (struct sasem_context *) file->private_data;
288 err("%s: no context for device", __func__);
292 mutex_lock(&context->ctx_lock);
294 if (!context->vfd_isopen) {
295 err("%s: VFD is not open", __func__);
298 context->vfd_isopen = 0;
299 printk(KERN_INFO "VFD port closed\n");
300 if (!context->dev_present && !context->ir_isopen) {
302 /* Device disconnected before close and IR port is
303 * not open. If IR port is open, context will be
304 * deleted by ir_close. */
305 mutex_unlock(&context->ctx_lock);
306 delete_context(context);
311 mutex_unlock(&context->ctx_lock);
316 * Sends a packet to the VFD.
318 static int send_packet(struct sasem_context *context)
324 pipe = usb_sndintpipe(context->dev,
325 context->tx_endpoint->bEndpointAddress);
326 interval = context->tx_endpoint->bInterval;
328 usb_fill_int_urb(context->tx_urb, context->dev, pipe,
329 context->usb_tx_buf, sizeof(context->usb_tx_buf),
330 usb_tx_callback, context, interval);
332 context->tx_urb->actual_length = 0;
334 init_completion(&context->tx.finished);
335 atomic_set(&(context->tx.busy), 1);
337 retval = usb_submit_urb(context->tx_urb, GFP_KERNEL);
339 atomic_set(&(context->tx.busy), 0);
340 err("%s: error submitting urb (%d)", __func__, retval);
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 err("%s: packet tx failed (%d)", __func__, retval);
356 * Writes data to the VFD. The Sasem VFD is 2x16 characters
357 * and requires data in 9 consecutive USB interrupt packets,
358 * each packet carrying 8 bytes.
360 static ssize_t vfd_write(struct file *file, const char *buf,
361 size_t n_bytes, loff_t *pos)
365 struct sasem_context *context;
368 context = (struct sasem_context *) file->private_data;
370 err("%s: no context for device", __func__);
374 mutex_lock(&context->ctx_lock);
376 if (!context->dev_present) {
377 err("%s: no Sasem device present", __func__);
382 if (n_bytes <= 0 || n_bytes > SASEM_DATA_BUF_SZ) {
383 err("%s: invalid payload size", __func__);
388 data_buf = memdup_user(buf, n_bytes);
389 if (PTR_ERR(data_buf))
390 return PTR_ERR(data_buf);
392 memcpy(context->tx.data_buf, data_buf, n_bytes);
394 /* Pad with spaces */
395 for (i = n_bytes; i < SASEM_DATA_BUF_SZ; ++i)
396 context->tx.data_buf[i] = ' ';
398 /* Nine 8 byte packets to be sent */
399 /* NOTE: "\x07\x01\0\0\0\0\0\0" or "\x0c\0\0\0\0\0\0\0"
400 * will clear the VFD */
401 for (i = 0; i < 9; i++) {
404 memcpy(context->usb_tx_buf, "\x07\0\0\0\0\0\0\0", 8);
405 context->usb_tx_buf[1] = (context->vfd_contrast) ?
406 (0x2B - (context->vfd_contrast - 1) / 250)
410 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
413 memcpy(context->usb_tx_buf, "\x0b\x01\0\0\0\0\0\0", 8);
416 memcpy(context->usb_tx_buf, context->tx.data_buf, 8);
419 memcpy(context->usb_tx_buf,
420 context->tx.data_buf + 8, 8);
423 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
426 memcpy(context->usb_tx_buf, "\x0b\x02\0\0\0\0\0\0", 8);
429 memcpy(context->usb_tx_buf,
430 context->tx.data_buf + 16, 8);
433 memcpy(context->usb_tx_buf,
434 context->tx.data_buf + 24, 8);
437 retval = send_packet(context);
440 err("%s: send packet failed for packet #%d",
447 mutex_unlock(&context->ctx_lock);
449 return (!retval) ? n_bytes : retval;
453 * Callback function for USB core API: transmit data
455 static void usb_tx_callback(struct urb *urb)
457 struct sasem_context *context;
461 context = (struct sasem_context *) urb->context;
465 context->tx.status = urb->status;
467 /* notify waiters that write has finished */
468 atomic_set(&context->tx.busy, 0);
469 complete(&context->tx.finished);
475 * Called by lirc_dev when the application opens /dev/lirc
477 static int ir_open(void *data)
480 struct sasem_context *context;
482 /* prevent races with disconnect */
483 mutex_lock(&disconnect_lock);
485 context = (struct sasem_context *) data;
487 mutex_lock(&context->ctx_lock);
489 if (context->ir_isopen) {
490 err("%s: IR port is already open", __func__);
495 usb_fill_int_urb(context->rx_urb, context->dev,
496 usb_rcvintpipe(context->dev,
497 context->rx_endpoint->bEndpointAddress),
498 context->usb_rx_buf, sizeof(context->usb_rx_buf),
499 usb_rx_callback, context, context->rx_endpoint->bInterval);
501 retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
504 err("%s: usb_submit_urb failed for ir_open (%d)",
507 context->ir_isopen = 1;
508 printk(KERN_INFO "IR port opened\n");
512 mutex_unlock(&context->ctx_lock);
514 mutex_unlock(&disconnect_lock);
519 * Called by lirc_dev when the application closes /dev/lirc
521 static void ir_close(void *data)
523 struct sasem_context *context;
525 context = (struct sasem_context *)data;
527 err("%s: no context for device", __func__);
531 mutex_lock(&context->ctx_lock);
533 usb_kill_urb(context->rx_urb);
534 context->ir_isopen = 0;
535 printk(KERN_INFO "IR port closed\n");
537 if (!context->dev_present) {
540 * Device disconnected while IR port was
541 * still open. Driver was not deregistered
542 * at disconnect time, so do it now.
544 deregister_from_lirc(context);
546 if (!context->vfd_isopen) {
548 mutex_unlock(&context->ctx_lock);
549 delete_context(context);
552 /* If VFD port is open, context will be deleted by vfd_close */
555 mutex_unlock(&context->ctx_lock);
560 * Process the incoming packet
562 static void incoming_packet(struct sasem_context *context,
565 int len = urb->actual_length;
566 unsigned char *buf = urb->transfer_buffer;
571 printk(KERN_WARNING "%s: invalid incoming packet size (%d)\n",
578 for (i = 0; i < 8; ++i)
579 printk(KERN_INFO "%02x ", buf[i]);
580 printk(KERN_INFO "\n");
584 * Lirc could deal with the repeat code, but we really need to block it
585 * if it arrives too late. Otherwise we could repeat the wrong code.
588 /* get the time since the last button press */
589 do_gettimeofday(&tv);
590 ms = (tv.tv_sec - context->presstime.tv_sec) * 1000 +
591 (tv.tv_usec - context->presstime.tv_usec) / 1000;
593 if (memcmp(buf, "\x08\0\0\0\0\0\0\0", 8) == 0) {
595 * the repeat code is being sent, so we copy
596 * the old code to LIRC
600 * NOTE: Only if the last code was less than 250ms ago
601 * - no one should be able to push another (undetected) button
602 * in that time and then get a false repeat of the previous
603 * press but it is long enough for a genuine repeat
605 if ((ms < 250) && (context->codesaved != 0)) {
606 memcpy(buf, &context->lastcode, 8);
607 context->presstime.tv_sec = tv.tv_sec;
608 context->presstime.tv_usec = tv.tv_usec;
611 /* save the current valid code for repeats */
612 memcpy(&context->lastcode, buf, 8);
614 * set flag to signal a valid code was save;
615 * just for safety reasons
617 context->codesaved = 1;
618 context->presstime.tv_sec = tv.tv_sec;
619 context->presstime.tv_usec = tv.tv_usec;
622 lirc_buffer_write(context->driver->rbuf, buf);
623 wake_up(&context->driver->rbuf->wait_poll);
627 * Callback function for USB core API: receive data
629 static void usb_rx_callback(struct urb *urb)
631 struct sasem_context *context;
635 context = (struct sasem_context *) urb->context;
639 switch (urb->status) {
641 case -ENOENT: /* usbcore unlink successful! */
645 if (context->ir_isopen)
646 incoming_packet(context, urb);
650 printk(KERN_WARNING "%s: status (%d): ignored",
651 __func__, urb->status);
655 usb_submit_urb(context->rx_urb, GFP_ATOMIC);
662 * Callback function for USB core API: Probe
664 static int sasem_probe(struct usb_interface *interface,
665 const struct usb_device_id *id)
667 struct usb_device *dev = NULL;
668 struct usb_host_interface *iface_desc = NULL;
669 struct usb_endpoint_descriptor *rx_endpoint = NULL;
670 struct usb_endpoint_descriptor *tx_endpoint = NULL;
671 struct urb *rx_urb = NULL;
672 struct urb *tx_urb = NULL;
673 struct lirc_driver *driver = NULL;
674 struct lirc_buffer *rbuf = NULL;
681 struct sasem_context *context = NULL;
684 printk(KERN_INFO "%s: found Sasem device\n", __func__);
687 dev = usb_get_dev(interface_to_usbdev(interface));
688 iface_desc = interface->cur_altsetting;
689 num_endpoints = iface_desc->desc.bNumEndpoints;
692 * Scan the endpoint list and set:
693 * first input endpoint = IR endpoint
694 * first output endpoint = VFD endpoint
700 for (i = 0; i < num_endpoints && !(ir_ep_found && vfd_ep_found); ++i) {
702 struct usb_endpoint_descriptor *ep;
705 ep = &iface_desc->endpoint [i].desc;
706 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
707 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
710 ep_dir == USB_DIR_IN &&
711 ep_type == USB_ENDPOINT_XFER_INT) {
716 printk(KERN_INFO "%s: found IR endpoint\n",
719 } else if (!vfd_ep_found &&
720 ep_dir == USB_DIR_OUT &&
721 ep_type == USB_ENDPOINT_XFER_INT) {
726 printk(KERN_INFO "%s: found VFD endpoint\n",
731 /* Input endpoint is mandatory */
734 err("%s: no valid input (IR) endpoint found.", __func__);
740 printk(KERN_INFO "%s: no valid output (VFD) endpoint found.\n",
744 /* Allocate memory */
747 context = kzalloc(sizeof(struct sasem_context), GFP_KERNEL);
749 err("%s: kzalloc failed for context", __func__);
751 goto alloc_status_switch;
753 driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
755 err("%s: kzalloc failed for lirc_driver", __func__);
757 goto alloc_status_switch;
759 rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
761 err("%s: kmalloc failed for lirc_buffer", __func__);
763 goto alloc_status_switch;
765 if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
766 err("%s: lirc_buffer_init failed", __func__);
768 goto alloc_status_switch;
770 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
772 err("%s: usb_alloc_urb failed for IR urb", __func__);
774 goto alloc_status_switch;
777 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
779 err("%s: usb_alloc_urb failed for VFD urb",
782 goto alloc_status_switch;
786 mutex_init(&context->ctx_lock);
788 strcpy(driver->name, MOD_NAME);
790 driver->code_length = 64;
791 driver->sample_rate = 0;
792 driver->features = LIRC_CAN_REC_LIRCCODE;
793 driver->data = context;
795 driver->set_use_inc = ir_open;
796 driver->set_use_dec = ir_close;
797 driver->dev = &interface->dev;
798 driver->owner = THIS_MODULE;
800 mutex_lock(&context->ctx_lock);
802 lirc_minor = lirc_register_driver(driver);
803 if (lirc_minor < 0) {
804 err("%s: lirc_register_driver failed", __func__);
806 mutex_unlock(&context->ctx_lock);
808 printk(KERN_INFO "%s: Registered Sasem driver (minor:%d)\n",
809 __func__, lirc_minor);
813 switch (alloc_status) {
817 usb_free_urb(tx_urb);
819 usb_free_urb(rx_urb);
821 lirc_buffer_free(rbuf);
834 /* Needed while unregistering! */
835 driver->minor = lirc_minor;
838 context->dev_present = 1;
839 context->rx_endpoint = rx_endpoint;
840 context->rx_urb = rx_urb;
842 context->tx_endpoint = tx_endpoint;
843 context->tx_urb = tx_urb;
844 context->vfd_contrast = 1000; /* range 0 - 1000 */
846 context->driver = driver;
848 usb_set_intfdata(interface, context);
853 printk(KERN_INFO "Registering VFD with sysfs\n");
854 if (usb_register_dev(interface, &sasem_class))
855 /* Not a fatal error, so ignore */
856 printk(KERN_INFO "%s: could not get a minor number "
857 "for VFD\n", __func__);
860 printk(KERN_INFO "%s: Sasem device on usb<%d:%d> initialized\n",
861 __func__, dev->bus->busnum, dev->devnum);
863 mutex_unlock(&context->ctx_lock);
869 * Callback function for USB core API: disonnect
871 static void sasem_disconnect(struct usb_interface *interface)
873 struct sasem_context *context;
875 /* prevent races with ir_open()/vfd_open() */
876 mutex_lock(&disconnect_lock);
878 context = usb_get_intfdata(interface);
879 mutex_lock(&context->ctx_lock);
881 printk(KERN_INFO "%s: Sasem device disconnected\n", __func__);
883 usb_set_intfdata(interface, NULL);
884 context->dev_present = 0;
887 usb_kill_urb(context->rx_urb);
889 /* Abort ongoing write */
890 if (atomic_read(&context->tx.busy)) {
892 usb_kill_urb(context->tx_urb);
893 wait_for_completion(&context->tx.finished);
896 /* De-register from lirc_dev if IR port is not open */
897 if (!context->ir_isopen)
898 deregister_from_lirc(context);
900 usb_deregister_dev(interface, &sasem_class);
902 mutex_unlock(&context->ctx_lock);
904 if (!context->ir_isopen && !context->vfd_isopen)
905 delete_context(context);
907 mutex_unlock(&disconnect_lock);
910 static int __init sasem_init(void)
914 printk(KERN_INFO MOD_DESC ", v" MOD_VERSION "\n");
915 printk(KERN_INFO MOD_AUTHOR "\n");
917 rc = usb_register(&sasem_driver);
919 err("%s: usb register failed (%d)", __func__, rc);
925 static void __exit sasem_exit(void)
927 usb_deregister(&sasem_driver);
928 printk(KERN_INFO "module removed. Goodbye!\n");
932 module_init(sasem_init);
933 module_exit(sasem_exit);