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>
46 #include <media/lirc.h>
47 #include <media/lirc_dev.h>
50 #define MOD_AUTHOR "Oliver Stabel <oliver.stabel@gmx.de>, " \
51 "Tim Davies <tim@opensystems.net.au>"
52 #define MOD_DESC "USB Driver for Sasem Remote Controller V1.1"
53 #define MOD_NAME "lirc_sasem"
54 #define MOD_VERSION "0.5"
56 #define VFD_MINOR_BASE 144 /* Same as LCD */
57 #define DEVICE_NAME "lcd%d"
59 #define BUF_CHUNK_SIZE 8
62 #define IOCTL_LCD_CONTRAST 1
64 /*** P R O T O T Y P E S ***/
66 /* USB Callback prototypes */
67 static int sasem_probe(struct usb_interface *interface,
68 const struct usb_device_id *id);
69 static void sasem_disconnect(struct usb_interface *interface);
70 static void usb_rx_callback(struct urb *urb);
71 static void usb_tx_callback(struct urb *urb);
73 /* VFD file_operations function prototypes */
74 static int vfd_open(struct inode *inode, struct file *file);
75 static long vfd_ioctl(struct file *file, unsigned cmd, unsigned long arg);
76 static int vfd_close(struct inode *inode, struct file *file);
77 static ssize_t vfd_write(struct file *file, const char __user *buf,
78 size_t n_bytes, loff_t *pos);
80 /* LIRC driver function prototypes */
81 static int ir_open(void *data);
82 static void ir_close(void *data);
84 /*** G L O B A L S ***/
85 #define SASEM_DATA_BUF_SZ 32
87 struct sasem_context {
89 struct usb_device *dev;
90 int vfd_isopen; /* VFD port has been opened */
91 unsigned int vfd_contrast; /* VFD contrast */
92 int ir_isopen; /* IR port has been opened */
93 int dev_present; /* USB device presence */
94 struct mutex ctx_lock; /* to lock this object */
95 wait_queue_head_t remove_ok; /* For unexpected USB disconnects */
97 struct lirc_driver *driver;
98 struct usb_endpoint_descriptor *rx_endpoint;
99 struct usb_endpoint_descriptor *tx_endpoint;
102 unsigned char usb_rx_buf[8];
103 unsigned char usb_tx_buf[8];
106 unsigned char data_buf[SASEM_DATA_BUF_SZ]; /* user data
108 struct completion finished; /* wait for write to finish */
109 atomic_t busy; /* write in progress */
110 int status; /* status of tx completion */
113 /* for dealing with repeat codes (wish there was a toggle bit!) */
114 struct timeval presstime;
119 /* VFD file operations */
120 static const struct file_operations vfd_fops = {
121 .owner = THIS_MODULE,
124 .unlocked_ioctl = &vfd_ioctl,
125 .release = &vfd_close,
126 .llseek = noop_llseek,
129 /* USB Device ID for Sasem USB Control Board */
130 static struct usb_device_id sasem_usb_id_table[] = {
131 /* Sasem USB Control Board */
132 { USB_DEVICE(0x11ba, 0x0101) },
133 /* Terminating entry */
137 /* USB Device data */
138 static struct usb_driver sasem_driver = {
140 .probe = sasem_probe,
141 .disconnect = sasem_disconnect,
142 .id_table = sasem_usb_id_table,
145 static struct usb_class_driver sasem_class = {
148 .minor_base = VFD_MINOR_BASE,
151 /* to prevent races between open() and disconnect() */
152 static DEFINE_MUTEX(disconnect_lock);
157 /*** M O D U L E C O D E ***/
159 MODULE_AUTHOR(MOD_AUTHOR);
160 MODULE_DESCRIPTION(MOD_DESC);
161 MODULE_LICENSE("GPL");
162 module_param(debug, int, S_IRUGO | S_IWUSR);
163 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
165 static void delete_context(struct sasem_context *context)
167 usb_free_urb(context->tx_urb); /* VFD */
168 usb_free_urb(context->rx_urb); /* IR */
169 lirc_buffer_free(context->driver->rbuf);
170 kfree(context->driver->rbuf);
171 kfree(context->driver);
175 pr_info("%s: context deleted\n", __func__);
178 static void deregister_from_lirc(struct sasem_context *context)
181 int minor = context->driver->minor;
183 retval = lirc_unregister_driver(minor);
185 pr_err("%s: unable to deregister from lirc (%d)\n",
188 pr_info("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,
218 "%s: no context found for minor %d\n",
224 mutex_lock(&context->ctx_lock);
226 if (context->vfd_isopen) {
227 dev_err(&interface->dev,
228 "%s: VFD port is already open", __func__);
231 context->vfd_isopen = 1;
232 file->private_data = context;
233 dev_info(&interface->dev, "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 pr_err("%s: no context for device\n", __func__);
258 mutex_lock(&context->ctx_lock);
261 case IOCTL_LCD_CONTRAST:
264 context->vfd_contrast = (unsigned int)arg;
267 pr_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 pr_err("%s: no context for device\n", __func__);
292 mutex_lock(&context->ctx_lock);
294 if (!context->vfd_isopen) {
295 dev_err(&context->dev->dev, "%s: VFD is not open\n", __func__);
298 context->vfd_isopen = 0;
299 dev_info(&context->dev->dev, "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 dev_err(&context->dev->dev, "%s: error submitting urb (%d)\n",
343 /* Wait for transmission to complete (or abort) */
344 mutex_unlock(&context->ctx_lock);
345 wait_for_completion(&context->tx.finished);
346 mutex_lock(&context->ctx_lock);
348 retval = context->tx.status;
350 dev_err(&context->dev->dev,
351 "%s: packet tx failed (%d)\n",
359 * Writes data to the VFD. The Sasem VFD is 2x16 characters
360 * and requires data in 9 consecutive USB interrupt packets,
361 * each packet carrying 8 bytes.
363 static ssize_t vfd_write(struct file *file, const char __user *buf,
364 size_t n_bytes, loff_t *pos)
368 struct sasem_context *context;
369 int *data_buf = NULL;
371 context = (struct sasem_context *) file->private_data;
373 pr_err("%s: no context for device\n", __func__);
377 mutex_lock(&context->ctx_lock);
379 if (!context->dev_present) {
380 pr_err("%s: no Sasem device present\n", __func__);
385 if (n_bytes <= 0 || n_bytes > SASEM_DATA_BUF_SZ) {
386 dev_err(&context->dev->dev, "%s: invalid payload size\n",
392 data_buf = memdup_user((void const __user *)buf, n_bytes);
393 if (IS_ERR(data_buf)) {
394 retval = PTR_ERR(data_buf);
399 memcpy(context->tx.data_buf, data_buf, n_bytes);
401 /* Pad with spaces */
402 for (i = n_bytes; i < SASEM_DATA_BUF_SZ; ++i)
403 context->tx.data_buf[i] = ' ';
405 /* Nine 8 byte packets to be sent */
406 /* NOTE: "\x07\x01\0\0\0\0\0\0" or "\x0c\0\0\0\0\0\0\0"
407 * will clear the VFD */
408 for (i = 0; i < 9; i++) {
411 memcpy(context->usb_tx_buf, "\x07\0\0\0\0\0\0\0", 8);
412 context->usb_tx_buf[1] = (context->vfd_contrast) ?
413 (0x2B - (context->vfd_contrast - 1) / 250)
417 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
420 memcpy(context->usb_tx_buf, "\x0b\x01\0\0\0\0\0\0", 8);
423 memcpy(context->usb_tx_buf, context->tx.data_buf, 8);
426 memcpy(context->usb_tx_buf,
427 context->tx.data_buf + 8, 8);
430 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
433 memcpy(context->usb_tx_buf, "\x0b\x02\0\0\0\0\0\0", 8);
436 memcpy(context->usb_tx_buf,
437 context->tx.data_buf + 16, 8);
440 memcpy(context->usb_tx_buf,
441 context->tx.data_buf + 24, 8);
444 retval = send_packet(context);
446 dev_err(&context->dev->dev,
447 "%s: send packet failed for packet #%d\n",
454 mutex_unlock(&context->ctx_lock);
457 return (!retval) ? n_bytes : retval;
461 * Callback function for USB core API: transmit data
463 static void usb_tx_callback(struct urb *urb)
465 struct sasem_context *context;
469 context = (struct sasem_context *) urb->context;
473 context->tx.status = urb->status;
475 /* notify waiters that write has finished */
476 atomic_set(&context->tx.busy, 0);
477 complete(&context->tx.finished);
481 * Called by lirc_dev when the application opens /dev/lirc
483 static int ir_open(void *data)
486 struct sasem_context *context;
488 /* prevent races with disconnect */
489 mutex_lock(&disconnect_lock);
493 mutex_lock(&context->ctx_lock);
495 if (context->ir_isopen) {
496 dev_err(&context->dev->dev, "%s: IR port is already open\n",
502 usb_fill_int_urb(context->rx_urb, context->dev,
503 usb_rcvintpipe(context->dev,
504 context->rx_endpoint->bEndpointAddress),
505 context->usb_rx_buf, sizeof(context->usb_rx_buf),
506 usb_rx_callback, context, context->rx_endpoint->bInterval);
508 retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
511 dev_err(&context->dev->dev,
512 "%s: usb_submit_urb failed for ir_open (%d)\n",
515 context->ir_isopen = 1;
516 dev_info(&context->dev->dev, "IR port opened\n");
520 mutex_unlock(&context->ctx_lock);
522 mutex_unlock(&disconnect_lock);
527 * Called by lirc_dev when the application closes /dev/lirc
529 static void ir_close(void *data)
531 struct sasem_context *context;
535 pr_err("%s: no context for device\n", __func__);
539 mutex_lock(&context->ctx_lock);
541 usb_kill_urb(context->rx_urb);
542 context->ir_isopen = 0;
543 pr_info("IR port closed\n");
545 if (!context->dev_present) {
548 * Device disconnected while IR port was
549 * still open. Driver was not deregistered
550 * at disconnect time, so do it now.
552 deregister_from_lirc(context);
554 if (!context->vfd_isopen) {
556 mutex_unlock(&context->ctx_lock);
557 delete_context(context);
560 /* If VFD port is open, context will be deleted by vfd_close */
563 mutex_unlock(&context->ctx_lock);
567 * Process the incoming packet
569 static void incoming_packet(struct sasem_context *context,
572 int len = urb->actual_length;
573 unsigned char *buf = urb->transfer_buffer;
578 dev_warn(&context->dev->dev,
579 "%s: invalid incoming packet size (%d)\n",
585 dev_info(&context->dev->dev, "Incoming data: %*ph\n", len, buf);
587 * Lirc could deal with the repeat code, but we really need to block it
588 * if it arrives too late. Otherwise we could repeat the wrong code.
591 /* get the time since the last button press */
592 do_gettimeofday(&tv);
593 ms = (tv.tv_sec - context->presstime.tv_sec) * 1000 +
594 (tv.tv_usec - context->presstime.tv_usec) / 1000;
596 if (memcmp(buf, "\x08\0\0\0\0\0\0\0", 8) == 0) {
598 * the repeat code is being sent, so we copy
599 * the old code to LIRC
603 * NOTE: Only if the last code was less than 250ms ago
604 * - no one should be able to push another (undetected) button
605 * in that time and then get a false repeat of the previous
606 * press but it is long enough for a genuine repeat
608 if ((ms < 250) && (context->codesaved != 0)) {
609 memcpy(buf, &context->lastcode, 8);
610 context->presstime.tv_sec = tv.tv_sec;
611 context->presstime.tv_usec = tv.tv_usec;
614 /* save the current valid code for repeats */
615 memcpy(&context->lastcode, buf, 8);
617 * set flag to signal a valid code was save;
618 * just for safety reasons
620 context->codesaved = 1;
621 context->presstime.tv_sec = tv.tv_sec;
622 context->presstime.tv_usec = tv.tv_usec;
625 lirc_buffer_write(context->driver->rbuf, buf);
626 wake_up(&context->driver->rbuf->wait_poll);
630 * Callback function for USB core API: receive data
632 static void usb_rx_callback(struct urb *urb)
634 struct sasem_context *context;
638 context = (struct sasem_context *) urb->context;
642 switch (urb->status) {
644 case -ENOENT: /* usbcore unlink successful! */
648 if (context->ir_isopen)
649 incoming_packet(context, urb);
653 dev_warn(&urb->dev->dev, "%s: status (%d): ignored",
654 __func__, urb->status);
658 usb_submit_urb(context->rx_urb, GFP_ATOMIC);
664 * Callback function for USB core API: Probe
666 static int sasem_probe(struct usb_interface *interface,
667 const struct usb_device_id *id)
669 struct usb_device *dev = NULL;
670 struct usb_host_interface *iface_desc = NULL;
671 struct usb_endpoint_descriptor *rx_endpoint = NULL;
672 struct usb_endpoint_descriptor *tx_endpoint = NULL;
673 struct urb *rx_urb = NULL;
674 struct urb *tx_urb = NULL;
675 struct lirc_driver *driver = NULL;
676 struct lirc_buffer *rbuf = NULL;
683 struct sasem_context *context = NULL;
686 dev_info(&interface->dev, "%s: found Sasem device\n", __func__);
689 dev = usb_get_dev(interface_to_usbdev(interface));
690 iface_desc = interface->cur_altsetting;
691 num_endpoints = iface_desc->desc.bNumEndpoints;
694 * Scan the endpoint list and set:
695 * first input endpoint = IR endpoint
696 * first output endpoint = VFD endpoint
702 for (i = 0; i < num_endpoints && !(ir_ep_found && vfd_ep_found); ++i) {
704 struct usb_endpoint_descriptor *ep;
708 ep = &iface_desc->endpoint [i].desc;
709 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
710 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
713 ep_dir == USB_DIR_IN &&
714 ep_type == USB_ENDPOINT_XFER_INT) {
719 dev_info(&interface->dev,
720 "%s: found IR endpoint\n", __func__);
722 } else if (!vfd_ep_found &&
723 ep_dir == USB_DIR_OUT &&
724 ep_type == USB_ENDPOINT_XFER_INT) {
729 dev_info(&interface->dev,
730 "%s: found VFD endpoint\n", __func__);
734 /* Input endpoint is mandatory */
736 dev_err(&interface->dev,
737 "%s: no valid input (IR) endpoint found.\n", __func__);
743 dev_info(&interface->dev,
744 "%s: no valid output (VFD) endpoint found.\n",
748 /* Allocate memory */
751 context = kzalloc(sizeof(struct sasem_context), GFP_KERNEL);
754 goto alloc_status_switch;
756 driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
759 goto alloc_status_switch;
761 rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
764 goto alloc_status_switch;
766 if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
767 dev_err(&interface->dev,
768 "%s: lirc_buffer_init failed\n", __func__);
770 goto alloc_status_switch;
772 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
774 dev_err(&interface->dev,
775 "%s: usb_alloc_urb failed for IR urb\n", __func__);
777 goto alloc_status_switch;
780 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
782 dev_err(&interface->dev,
783 "%s: usb_alloc_urb failed for VFD urb",
786 goto alloc_status_switch;
790 mutex_init(&context->ctx_lock);
792 strcpy(driver->name, MOD_NAME);
794 driver->code_length = 64;
795 driver->sample_rate = 0;
796 driver->features = LIRC_CAN_REC_LIRCCODE;
797 driver->data = context;
799 driver->set_use_inc = ir_open;
800 driver->set_use_dec = ir_close;
801 driver->dev = &interface->dev;
802 driver->owner = THIS_MODULE;
804 mutex_lock(&context->ctx_lock);
806 lirc_minor = lirc_register_driver(driver);
807 if (lirc_minor < 0) {
808 dev_err(&interface->dev,
809 "%s: lirc_register_driver failed\n", __func__);
814 dev_info(&interface->dev,
815 "%s: Registered Sasem driver (minor:%d)\n",
816 __func__, lirc_minor);
818 /* Needed while unregistering! */
819 driver->minor = lirc_minor;
822 context->dev_present = 1;
823 context->rx_endpoint = rx_endpoint;
824 context->rx_urb = rx_urb;
826 context->tx_endpoint = tx_endpoint;
827 context->tx_urb = tx_urb;
828 context->vfd_contrast = 1000; /* range 0 - 1000 */
830 context->driver = driver;
832 usb_set_intfdata(interface, context);
837 dev_info(&interface->dev,
838 "Registering VFD with sysfs\n");
839 if (usb_register_dev(interface, &sasem_class))
840 /* Not a fatal error, so ignore */
841 dev_info(&interface->dev,
842 "%s: could not get a minor number for VFD\n",
846 dev_info(&interface->dev,
847 "%s: Sasem device on usb<%d:%d> initialized\n",
848 __func__, dev->bus->busnum, dev->devnum);
850 mutex_unlock(&context->ctx_lock);
853 switch (alloc_status) {
857 usb_free_urb(tx_urb);
859 usb_free_urb(rx_urb);
862 lirc_buffer_free(rbuf);
884 * Callback function for USB core API: disconnect
886 static void sasem_disconnect(struct usb_interface *interface)
888 struct sasem_context *context;
890 /* prevent races with ir_open()/vfd_open() */
891 mutex_lock(&disconnect_lock);
893 context = usb_get_intfdata(interface);
894 mutex_lock(&context->ctx_lock);
896 dev_info(&interface->dev, "%s: Sasem device disconnected\n",
899 usb_set_intfdata(interface, NULL);
900 context->dev_present = 0;
903 usb_kill_urb(context->rx_urb);
905 /* Abort ongoing write */
906 if (atomic_read(&context->tx.busy)) {
908 usb_kill_urb(context->tx_urb);
909 wait_for_completion(&context->tx.finished);
912 /* De-register from lirc_dev if IR port is not open */
913 if (!context->ir_isopen)
914 deregister_from_lirc(context);
916 usb_deregister_dev(interface, &sasem_class);
918 mutex_unlock(&context->ctx_lock);
920 if (!context->ir_isopen && !context->vfd_isopen)
921 delete_context(context);
923 mutex_unlock(&disconnect_lock);
926 module_usb_driver(sasem_driver);