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 /*** G L O B A L S ***/
84 #define SASEM_DATA_BUF_SZ 32
86 struct sasem_context {
88 struct usb_device *dev;
89 int vfd_isopen; /* VFD port has been opened */
90 unsigned int vfd_contrast; /* VFD contrast */
91 int ir_isopen; /* IR port has been opened */
92 int dev_present; /* USB device presence */
93 struct mutex ctx_lock; /* to lock this object */
94 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
96 struct lirc_driver *driver;
97 struct usb_endpoint_descriptor *rx_endpoint;
98 struct usb_endpoint_descriptor *tx_endpoint;
101 unsigned char usb_rx_buf[8];
102 unsigned char usb_tx_buf[8];
105 unsigned char data_buf[SASEM_DATA_BUF_SZ]; /* user data
107 struct completion finished; /* wait for write to finish */
108 atomic_t busy; /* write in progress */
109 int status; /* status of tx completion */
112 /* for dealing with repeat codes (wish there was a toggle bit!) */
113 struct timeval presstime;
118 /* VFD file operations */
119 static const struct file_operations vfd_fops = {
120 .owner = THIS_MODULE,
123 .unlocked_ioctl = &vfd_ioctl,
124 .release = &vfd_close,
125 .llseek = noop_llseek,
128 /* USB Device ID for Sasem USB Control Board */
129 static struct usb_device_id sasem_usb_id_table[] = {
130 /* Sasem USB Control Board */
131 { USB_DEVICE(0x11ba, 0x0101) },
132 /* Terminating entry */
136 /* USB Device data */
137 static struct usb_driver sasem_driver = {
139 .probe = sasem_probe,
140 .disconnect = sasem_disconnect,
141 .id_table = sasem_usb_id_table,
144 static struct usb_class_driver sasem_class = {
147 .minor_base = VFD_MINOR_BASE,
150 /* to prevent races between open() and disconnect() */
151 static DEFINE_MUTEX(disconnect_lock);
156 /*** M O D U L E C O D E ***/
158 MODULE_AUTHOR(MOD_AUTHOR);
159 MODULE_DESCRIPTION(MOD_DESC);
160 MODULE_LICENSE("GPL");
161 module_param(debug, int, S_IRUGO | S_IWUSR);
162 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
164 static void delete_context(struct sasem_context *context)
166 usb_free_urb(context->tx_urb); /* VFD */
167 usb_free_urb(context->rx_urb); /* IR */
168 lirc_buffer_free(context->driver->rbuf);
169 kfree(context->driver->rbuf);
170 kfree(context->driver);
174 printk(KERN_INFO "%s: context deleted\n", __func__);
177 static void deregister_from_lirc(struct sasem_context *context)
180 int minor = context->driver->minor;
182 retval = lirc_unregister_driver(minor);
184 printk(KERN_ERR "%s: unable to deregister from lirc (%d)\n",
187 printk(KERN_INFO "Deregistered Sasem driver (minor:%d)\n",
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 printk(KERN_ERR KBUILD_MODNAME
210 ": %s: could not find interface for minor %d\n",
215 context = usb_get_intfdata(interface);
218 dev_err(&interface->dev,
219 "%s: no context found for minor %d\n",
225 mutex_lock(&context->ctx_lock);
227 if (context->vfd_isopen) {
228 dev_err(&interface->dev,
229 "%s: VFD port is already open", __func__);
232 context->vfd_isopen = 1;
233 file->private_data = context;
234 dev_info(&interface->dev, "VFD port opened\n");
237 mutex_unlock(&context->ctx_lock);
240 mutex_unlock(&disconnect_lock);
245 * Called when the VFD device (e.g. /dev/usb/lcd)
246 * is closed by the application.
248 static long vfd_ioctl(struct file *file, unsigned cmd, unsigned long arg)
250 struct sasem_context *context = NULL;
252 context = (struct sasem_context *) file->private_data;
255 printk(KERN_ERR KBUILD_MODNAME
256 ": %s: no context for device\n", __func__);
260 mutex_lock(&context->ctx_lock);
263 case IOCTL_LCD_CONTRAST:
266 context->vfd_contrast = (unsigned int)arg;
269 printk(KERN_INFO "Unknown IOCTL command\n");
270 mutex_unlock(&context->ctx_lock);
271 return -ENOIOCTLCMD; /* not supported */
274 mutex_unlock(&context->ctx_lock);
279 * Called when the VFD device (e.g. /dev/usb/lcd)
280 * is closed by the application.
282 static int vfd_close(struct inode *inode, struct file *file)
284 struct sasem_context *context = NULL;
287 context = (struct sasem_context *) file->private_data;
290 printk(KERN_ERR KBUILD_MODNAME
291 ": %s: no context for device\n", __func__);
295 mutex_lock(&context->ctx_lock);
297 if (!context->vfd_isopen) {
298 dev_err(&context->dev->dev, "%s: VFD is not open\n", __func__);
301 context->vfd_isopen = 0;
302 printk(KERN_INFO "VFD port closed\n");
303 if (!context->dev_present && !context->ir_isopen) {
305 /* Device disconnected before close and IR port is
306 * not open. If IR port is open, context will be
307 * deleted by ir_close. */
308 mutex_unlock(&context->ctx_lock);
309 delete_context(context);
314 mutex_unlock(&context->ctx_lock);
319 * Sends a packet to the VFD.
321 static int send_packet(struct sasem_context *context)
327 pipe = usb_sndintpipe(context->dev,
328 context->tx_endpoint->bEndpointAddress);
329 interval = context->tx_endpoint->bInterval;
331 usb_fill_int_urb(context->tx_urb, context->dev, pipe,
332 context->usb_tx_buf, sizeof(context->usb_tx_buf),
333 usb_tx_callback, context, interval);
335 context->tx_urb->actual_length = 0;
337 init_completion(&context->tx.finished);
338 atomic_set(&(context->tx.busy), 1);
340 retval = usb_submit_urb(context->tx_urb, GFP_KERNEL);
342 atomic_set(&(context->tx.busy), 0);
343 dev_err(&context->dev->dev, "%s: error submitting urb (%d)\n",
346 /* Wait for transmission to complete (or abort) */
347 mutex_unlock(&context->ctx_lock);
348 wait_for_completion(&context->tx.finished);
349 mutex_lock(&context->ctx_lock);
351 retval = context->tx.status;
353 dev_err(&context->dev->dev,
354 "%s: packet tx failed (%d)\n",
362 * Writes data to the VFD. The Sasem VFD is 2x16 characters
363 * and requires data in 9 consecutive USB interrupt packets,
364 * each packet carrying 8 bytes.
366 static ssize_t vfd_write(struct file *file, const char *buf,
367 size_t n_bytes, loff_t *pos)
371 struct sasem_context *context;
372 int *data_buf = NULL;
374 context = (struct sasem_context *) file->private_data;
376 printk(KERN_ERR KBUILD_MODNAME
377 ": %s: no context for device\n", __func__);
381 mutex_lock(&context->ctx_lock);
383 if (!context->dev_present) {
384 printk(KERN_ERR KBUILD_MODNAME
385 ": %s: no Sasem device present\n", __func__);
390 if (n_bytes <= 0 || n_bytes > SASEM_DATA_BUF_SZ) {
391 dev_err(&context->dev->dev, "%s: invalid payload size\n",
397 data_buf = memdup_user(buf, n_bytes);
398 if (IS_ERR(data_buf)) {
399 retval = PTR_ERR(data_buf);
403 memcpy(context->tx.data_buf, data_buf, n_bytes);
405 /* Pad with spaces */
406 for (i = n_bytes; i < SASEM_DATA_BUF_SZ; ++i)
407 context->tx.data_buf[i] = ' ';
409 /* Nine 8 byte packets to be sent */
410 /* NOTE: "\x07\x01\0\0\0\0\0\0" or "\x0c\0\0\0\0\0\0\0"
411 * will clear the VFD */
412 for (i = 0; i < 9; i++) {
415 memcpy(context->usb_tx_buf, "\x07\0\0\0\0\0\0\0", 8);
416 context->usb_tx_buf[1] = (context->vfd_contrast) ?
417 (0x2B - (context->vfd_contrast - 1) / 250)
421 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
424 memcpy(context->usb_tx_buf, "\x0b\x01\0\0\0\0\0\0", 8);
427 memcpy(context->usb_tx_buf, context->tx.data_buf, 8);
430 memcpy(context->usb_tx_buf,
431 context->tx.data_buf + 8, 8);
434 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
437 memcpy(context->usb_tx_buf, "\x0b\x02\0\0\0\0\0\0", 8);
440 memcpy(context->usb_tx_buf,
441 context->tx.data_buf + 16, 8);
444 memcpy(context->usb_tx_buf,
445 context->tx.data_buf + 24, 8);
448 retval = send_packet(context);
450 dev_err(&context->dev->dev,
451 "%s: send packet failed for packet #%d\n",
458 mutex_unlock(&context->ctx_lock);
461 return (!retval) ? n_bytes : retval;
465 * Callback function for USB core API: transmit data
467 static void usb_tx_callback(struct urb *urb)
469 struct sasem_context *context;
473 context = (struct sasem_context *) urb->context;
477 context->tx.status = urb->status;
479 /* notify waiters that write has finished */
480 atomic_set(&context->tx.busy, 0);
481 complete(&context->tx.finished);
487 * Called by lirc_dev when the application opens /dev/lirc
489 static int ir_open(void *data)
492 struct sasem_context *context;
494 /* prevent races with disconnect */
495 mutex_lock(&disconnect_lock);
497 context = (struct sasem_context *) data;
499 mutex_lock(&context->ctx_lock);
501 if (context->ir_isopen) {
502 dev_err(&context->dev->dev, "%s: IR port is already open\n",
508 usb_fill_int_urb(context->rx_urb, context->dev,
509 usb_rcvintpipe(context->dev,
510 context->rx_endpoint->bEndpointAddress),
511 context->usb_rx_buf, sizeof(context->usb_rx_buf),
512 usb_rx_callback, context, context->rx_endpoint->bInterval);
514 retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
517 dev_err(&context->dev->dev,
518 "%s: usb_submit_urb failed for ir_open (%d)\n",
521 context->ir_isopen = 1;
522 printk(KERN_INFO "IR port opened\n");
526 mutex_unlock(&context->ctx_lock);
528 mutex_unlock(&disconnect_lock);
533 * Called by lirc_dev when the application closes /dev/lirc
535 static void ir_close(void *data)
537 struct sasem_context *context;
539 context = (struct sasem_context *)data;
541 printk(KERN_ERR KBUILD_MODNAME
542 ": %s: no context for device\n", __func__);
546 mutex_lock(&context->ctx_lock);
548 usb_kill_urb(context->rx_urb);
549 context->ir_isopen = 0;
550 printk(KERN_INFO "IR port closed\n");
552 if (!context->dev_present) {
555 * Device disconnected while IR port was
556 * still open. Driver was not deregistered
557 * at disconnect time, so do it now.
559 deregister_from_lirc(context);
561 if (!context->vfd_isopen) {
563 mutex_unlock(&context->ctx_lock);
564 delete_context(context);
567 /* If VFD port is open, context will be deleted by vfd_close */
570 mutex_unlock(&context->ctx_lock);
575 * Process the incoming packet
577 static void incoming_packet(struct sasem_context *context,
580 int len = urb->actual_length;
581 unsigned char *buf = urb->transfer_buffer;
587 printk(KERN_WARNING "%s: invalid incoming packet size (%d)\n",
593 printk(KERN_INFO "Incoming data: ");
594 for (i = 0; i < 8; ++i)
595 printk(KERN_CONT "%02x ", buf[i]);
596 printk(KERN_CONT "\n");
600 * Lirc could deal with the repeat code, but we really need to block it
601 * if it arrives too late. Otherwise we could repeat the wrong code.
604 /* get the time since the last button press */
605 do_gettimeofday(&tv);
606 ms = (tv.tv_sec - context->presstime.tv_sec) * 1000 +
607 (tv.tv_usec - context->presstime.tv_usec) / 1000;
609 if (memcmp(buf, "\x08\0\0\0\0\0\0\0", 8) == 0) {
611 * the repeat code is being sent, so we copy
612 * the old code to LIRC
616 * NOTE: Only if the last code was less than 250ms ago
617 * - no one should be able to push another (undetected) button
618 * in that time and then get a false repeat of the previous
619 * press but it is long enough for a genuine repeat
621 if ((ms < 250) && (context->codesaved != 0)) {
622 memcpy(buf, &context->lastcode, 8);
623 context->presstime.tv_sec = tv.tv_sec;
624 context->presstime.tv_usec = tv.tv_usec;
627 /* save the current valid code for repeats */
628 memcpy(&context->lastcode, buf, 8);
630 * set flag to signal a valid code was save;
631 * just for safety reasons
633 context->codesaved = 1;
634 context->presstime.tv_sec = tv.tv_sec;
635 context->presstime.tv_usec = tv.tv_usec;
638 lirc_buffer_write(context->driver->rbuf, buf);
639 wake_up(&context->driver->rbuf->wait_poll);
643 * Callback function for USB core API: receive data
645 static void usb_rx_callback(struct urb *urb)
647 struct sasem_context *context;
651 context = (struct sasem_context *) urb->context;
655 switch (urb->status) {
657 case -ENOENT: /* usbcore unlink successful! */
661 if (context->ir_isopen)
662 incoming_packet(context, urb);
666 printk(KERN_WARNING "%s: status (%d): ignored",
667 __func__, urb->status);
671 usb_submit_urb(context->rx_urb, GFP_ATOMIC);
678 * Callback function for USB core API: Probe
680 static int sasem_probe(struct usb_interface *interface,
681 const struct usb_device_id *id)
683 struct usb_device *dev = NULL;
684 struct usb_host_interface *iface_desc = NULL;
685 struct usb_endpoint_descriptor *rx_endpoint = NULL;
686 struct usb_endpoint_descriptor *tx_endpoint = NULL;
687 struct urb *rx_urb = NULL;
688 struct urb *tx_urb = NULL;
689 struct lirc_driver *driver = NULL;
690 struct lirc_buffer *rbuf = NULL;
697 struct sasem_context *context = NULL;
700 dev_info(&interface->dev, "%s: found Sasem device\n", __func__);
703 dev = usb_get_dev(interface_to_usbdev(interface));
704 iface_desc = interface->cur_altsetting;
705 num_endpoints = iface_desc->desc.bNumEndpoints;
708 * Scan the endpoint list and set:
709 * first input endpoint = IR endpoint
710 * first output endpoint = VFD endpoint
716 for (i = 0; i < num_endpoints && !(ir_ep_found && vfd_ep_found); ++i) {
718 struct usb_endpoint_descriptor *ep;
721 ep = &iface_desc->endpoint [i].desc;
722 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
723 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
726 ep_dir == USB_DIR_IN &&
727 ep_type == USB_ENDPOINT_XFER_INT) {
732 dev_info(&interface->dev,
733 "%s: found IR endpoint\n", __func__);
735 } else if (!vfd_ep_found &&
736 ep_dir == USB_DIR_OUT &&
737 ep_type == USB_ENDPOINT_XFER_INT) {
742 dev_info(&interface->dev,
743 "%s: found VFD endpoint\n", __func__);
747 /* Input endpoint is mandatory */
749 dev_err(&interface->dev,
750 "%s: no valid input (IR) endpoint found.\n", __func__);
756 dev_info(&interface->dev,
757 "%s: no valid output (VFD) endpoint found.\n",
761 /* Allocate memory */
764 context = kzalloc(sizeof(struct sasem_context), GFP_KERNEL);
766 dev_err(&interface->dev,
767 "%s: kzalloc failed for context\n", __func__);
769 goto alloc_status_switch;
771 driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
773 dev_err(&interface->dev,
774 "%s: kzalloc failed for lirc_driver\n", __func__);
776 goto alloc_status_switch;
778 rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
780 dev_err(&interface->dev,
781 "%s: kmalloc failed for lirc_buffer\n", __func__);
783 goto alloc_status_switch;
785 if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
786 dev_err(&interface->dev,
787 "%s: lirc_buffer_init failed\n", __func__);
789 goto alloc_status_switch;
791 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
793 dev_err(&interface->dev,
794 "%s: usb_alloc_urb failed for IR urb\n", __func__);
796 goto alloc_status_switch;
799 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
801 dev_err(&interface->dev,
802 "%s: usb_alloc_urb failed for VFD urb",
805 goto alloc_status_switch;
809 mutex_init(&context->ctx_lock);
811 strcpy(driver->name, MOD_NAME);
813 driver->code_length = 64;
814 driver->sample_rate = 0;
815 driver->features = LIRC_CAN_REC_LIRCCODE;
816 driver->data = context;
818 driver->set_use_inc = ir_open;
819 driver->set_use_dec = ir_close;
820 driver->dev = &interface->dev;
821 driver->owner = THIS_MODULE;
823 mutex_lock(&context->ctx_lock);
825 lirc_minor = lirc_register_driver(driver);
826 if (lirc_minor < 0) {
827 dev_err(&interface->dev,
828 "%s: lirc_register_driver failed\n", __func__);
833 printk(KERN_INFO "%s: Registered Sasem driver (minor:%d)\n",
834 __func__, lirc_minor);
836 /* Needed while unregistering! */
837 driver->minor = lirc_minor;
840 context->dev_present = 1;
841 context->rx_endpoint = rx_endpoint;
842 context->rx_urb = rx_urb;
844 context->tx_endpoint = tx_endpoint;
845 context->tx_urb = tx_urb;
846 context->vfd_contrast = 1000; /* range 0 - 1000 */
848 context->driver = driver;
850 usb_set_intfdata(interface, context);
855 printk(KERN_INFO "Registering VFD with sysfs\n");
856 if (usb_register_dev(interface, &sasem_class))
857 /* Not a fatal error, so ignore */
858 printk(KERN_INFO "%s: could not get a minor number "
859 "for VFD\n", __func__);
862 printk(KERN_INFO "%s: Sasem device on usb<%d:%d> initialized\n",
863 __func__, dev->bus->busnum, dev->devnum);
865 mutex_unlock(&context->ctx_lock);
868 switch (alloc_status) {
872 usb_free_urb(tx_urb);
874 usb_free_urb(rx_urb);
876 lirc_buffer_free(rbuf);
894 * Callback function for USB core API: disonnect
896 static void sasem_disconnect(struct usb_interface *interface)
898 struct sasem_context *context;
900 /* prevent races with ir_open()/vfd_open() */
901 mutex_lock(&disconnect_lock);
903 context = usb_get_intfdata(interface);
904 mutex_lock(&context->ctx_lock);
906 printk(KERN_INFO "%s: Sasem device disconnected\n", __func__);
908 usb_set_intfdata(interface, NULL);
909 context->dev_present = 0;
912 usb_kill_urb(context->rx_urb);
914 /* Abort ongoing write */
915 if (atomic_read(&context->tx.busy)) {
917 usb_kill_urb(context->tx_urb);
918 wait_for_completion(&context->tx.finished);
921 /* De-register from lirc_dev if IR port is not open */
922 if (!context->ir_isopen)
923 deregister_from_lirc(context);
925 usb_deregister_dev(interface, &sasem_class);
927 mutex_unlock(&context->ctx_lock);
929 if (!context->ir_isopen && !context->vfd_isopen)
930 delete_context(context);
932 mutex_unlock(&disconnect_lock);
935 module_usb_driver(sasem_driver);