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 static void deregister_from_lirc(struct sasem_context *context)
178 int minor = context->driver->minor;
180 retval = lirc_unregister_driver(minor);
182 dev_err(&context->dev->dev,
183 "%s: unable to deregister from lirc (%d)\n",
186 dev_info(&context->dev->dev,
187 "Deregistered Sasem driver (minor:%d)\n", minor);
192 * Called when the VFD device (e.g. /dev/usb/lcd)
193 * is opened by the application.
195 static int vfd_open(struct inode *inode, struct file *file)
197 struct usb_interface *interface;
198 struct sasem_context *context = NULL;
202 /* prevent races with disconnect */
203 mutex_lock(&disconnect_lock);
205 subminor = iminor(inode);
206 interface = usb_find_interface(&sasem_driver, subminor);
208 pr_err("%s: could not find interface for minor %d\n",
213 context = usb_get_intfdata(interface);
216 dev_err(&interface->dev, "no context found for minor %d\n",
222 mutex_lock(&context->ctx_lock);
224 if (context->vfd_isopen) {
225 dev_err(&interface->dev,
226 "%s: VFD port is already open", __func__);
229 context->vfd_isopen = 1;
230 file->private_data = context;
231 dev_info(&interface->dev, "VFD port opened\n");
234 mutex_unlock(&context->ctx_lock);
237 mutex_unlock(&disconnect_lock);
242 * Called when the VFD device (e.g. /dev/usb/lcd)
243 * is closed by the application.
245 static long vfd_ioctl(struct file *file, unsigned cmd, unsigned long arg)
247 struct sasem_context *context = NULL;
249 context = (struct sasem_context *) file->private_data;
252 pr_err("%s: no context for device\n", __func__);
256 mutex_lock(&context->ctx_lock);
259 case IOCTL_LCD_CONTRAST:
262 context->vfd_contrast = (unsigned int)arg;
265 pr_info("Unknown IOCTL command\n");
266 mutex_unlock(&context->ctx_lock);
267 return -ENOIOCTLCMD; /* not supported */
270 mutex_unlock(&context->ctx_lock);
275 * Called when the VFD device (e.g. /dev/usb/lcd)
276 * is closed by the application.
278 static int vfd_close(struct inode *inode, struct file *file)
280 struct sasem_context *context = NULL;
283 context = (struct sasem_context *) file->private_data;
286 pr_err("%s: no context for device\n", __func__);
290 mutex_lock(&context->ctx_lock);
292 if (!context->vfd_isopen) {
293 dev_err(&context->dev->dev, "%s: VFD is not open\n", __func__);
296 context->vfd_isopen = 0;
297 dev_info(&context->dev->dev, "VFD port closed\n");
298 if (!context->dev_present && !context->ir_isopen) {
300 /* Device disconnected before close and IR port is
301 * not open. If IR port is open, context will be
302 * deleted by ir_close. */
303 mutex_unlock(&context->ctx_lock);
304 delete_context(context);
309 mutex_unlock(&context->ctx_lock);
314 * Sends a packet to the VFD.
316 static int send_packet(struct sasem_context *context)
322 pipe = usb_sndintpipe(context->dev,
323 context->tx_endpoint->bEndpointAddress);
324 interval = context->tx_endpoint->bInterval;
326 usb_fill_int_urb(context->tx_urb, context->dev, pipe,
327 context->usb_tx_buf, sizeof(context->usb_tx_buf),
328 usb_tx_callback, context, interval);
330 context->tx_urb->actual_length = 0;
332 init_completion(&context->tx.finished);
333 atomic_set(&context->tx.busy, 1);
335 retval = usb_submit_urb(context->tx_urb, GFP_KERNEL);
337 atomic_set(&context->tx.busy, 0);
338 dev_err(&context->dev->dev, "error submitting urb (%d)\n",
341 /* Wait for transmission to complete (or abort) */
342 mutex_unlock(&context->ctx_lock);
343 wait_for_completion(&context->tx.finished);
344 mutex_lock(&context->ctx_lock);
346 retval = context->tx.status;
348 dev_err(&context->dev->dev,
349 "packet tx failed (%d)\n", 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 __user *buf,
361 size_t n_bytes, loff_t *pos)
365 struct sasem_context *context;
366 int *data_buf = NULL;
368 context = (struct sasem_context *) file->private_data;
370 pr_err("%s: no context for device\n", __func__);
374 mutex_lock(&context->ctx_lock);
376 if (!context->dev_present) {
377 pr_err("%s: no Sasem device present\n", __func__);
382 if (n_bytes <= 0 || n_bytes > SASEM_DATA_BUF_SZ) {
383 dev_err(&context->dev->dev, "%s: invalid payload size\n",
389 data_buf = memdup_user(buf, n_bytes);
390 if (IS_ERR(data_buf)) {
391 retval = PTR_ERR(data_buf);
396 memcpy(context->tx.data_buf, data_buf, n_bytes);
398 /* Pad with spaces */
399 for (i = n_bytes; i < SASEM_DATA_BUF_SZ; ++i)
400 context->tx.data_buf[i] = ' ';
402 /* Nine 8 byte packets to be sent */
403 /* NOTE: "\x07\x01\0\0\0\0\0\0" or "\x0c\0\0\0\0\0\0\0"
404 * will clear the VFD */
405 for (i = 0; i < 9; i++) {
408 memcpy(context->usb_tx_buf, "\x07\0\0\0\0\0\0\0", 8);
409 context->usb_tx_buf[1] = (context->vfd_contrast) ?
410 (0x2B - (context->vfd_contrast - 1) / 250)
414 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
417 memcpy(context->usb_tx_buf, "\x0b\x01\0\0\0\0\0\0", 8);
420 memcpy(context->usb_tx_buf, context->tx.data_buf, 8);
423 memcpy(context->usb_tx_buf,
424 context->tx.data_buf + 8, 8);
427 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
430 memcpy(context->usb_tx_buf, "\x0b\x02\0\0\0\0\0\0", 8);
433 memcpy(context->usb_tx_buf,
434 context->tx.data_buf + 16, 8);
437 memcpy(context->usb_tx_buf,
438 context->tx.data_buf + 24, 8);
441 retval = send_packet(context);
443 dev_err(&context->dev->dev,
444 "send packet failed for packet #%d\n", i);
450 mutex_unlock(&context->ctx_lock);
453 return (!retval) ? n_bytes : retval;
457 * Callback function for USB core API: transmit data
459 static void usb_tx_callback(struct urb *urb)
461 struct sasem_context *context;
465 context = (struct sasem_context *) urb->context;
469 context->tx.status = urb->status;
471 /* notify waiters that write has finished */
472 atomic_set(&context->tx.busy, 0);
473 complete(&context->tx.finished);
477 * Called by lirc_dev when the application opens /dev/lirc
479 static int ir_open(void *data)
482 struct sasem_context *context;
484 /* prevent races with disconnect */
485 mutex_lock(&disconnect_lock);
489 mutex_lock(&context->ctx_lock);
491 if (context->ir_isopen) {
492 dev_err(&context->dev->dev, "%s: IR port is already open\n",
498 usb_fill_int_urb(context->rx_urb, context->dev,
499 usb_rcvintpipe(context->dev,
500 context->rx_endpoint->bEndpointAddress),
501 context->usb_rx_buf, sizeof(context->usb_rx_buf),
502 usb_rx_callback, context, context->rx_endpoint->bInterval);
504 retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
507 dev_err(&context->dev->dev,
508 "usb_submit_urb failed for ir_open (%d)\n", retval);
510 context->ir_isopen = 1;
511 dev_info(&context->dev->dev, "IR port opened\n");
515 mutex_unlock(&context->ctx_lock);
517 mutex_unlock(&disconnect_lock);
522 * Called by lirc_dev when the application closes /dev/lirc
524 static void ir_close(void *data)
526 struct sasem_context *context;
530 pr_err("%s: no context for device\n", __func__);
534 mutex_lock(&context->ctx_lock);
536 usb_kill_urb(context->rx_urb);
537 context->ir_isopen = 0;
538 pr_info("IR port closed\n");
540 if (!context->dev_present) {
543 * Device disconnected while IR port was
544 * still open. Driver was not deregistered
545 * at disconnect time, so do it now.
547 deregister_from_lirc(context);
549 if (!context->vfd_isopen) {
551 mutex_unlock(&context->ctx_lock);
552 delete_context(context);
555 /* If VFD port is open, context will be deleted by vfd_close */
558 mutex_unlock(&context->ctx_lock);
562 * Process the incoming packet
564 static void incoming_packet(struct sasem_context *context,
567 int len = urb->actual_length;
568 unsigned char *buf = urb->transfer_buffer;
573 dev_warn(&context->dev->dev,
574 "%s: invalid incoming packet size (%d)\n",
580 dev_info(&context->dev->dev, "Incoming data: %*ph\n", len, buf);
582 * Lirc could deal with the repeat code, but we really need to block it
583 * if it arrives too late. Otherwise we could repeat the wrong code.
586 /* get the time since the last button press */
587 do_gettimeofday(&tv);
588 ms = (tv.tv_sec - context->presstime.tv_sec) * 1000 +
589 (tv.tv_usec - context->presstime.tv_usec) / 1000;
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 ((ms < 250) && (context->codesaved != 0)) {
604 memcpy(buf, &context->lastcode, 8);
605 context->presstime.tv_sec = tv.tv_sec;
606 context->presstime.tv_usec = tv.tv_usec;
609 /* save the current valid code for repeats */
610 memcpy(&context->lastcode, buf, 8);
612 * set flag to signal a valid code was save;
613 * just for safety reasons
615 context->codesaved = 1;
616 context->presstime.tv_sec = tv.tv_sec;
617 context->presstime.tv_usec = tv.tv_usec;
620 lirc_buffer_write(context->driver->rbuf, buf);
621 wake_up(&context->driver->rbuf->wait_poll);
625 * Callback function for USB core API: receive data
627 static void usb_rx_callback(struct urb *urb)
629 struct sasem_context *context;
633 context = (struct sasem_context *) urb->context;
637 switch (urb->status) {
639 case -ENOENT: /* usbcore unlink successful! */
643 if (context->ir_isopen)
644 incoming_packet(context, urb);
648 dev_warn(&urb->dev->dev, "%s: status (%d): ignored",
649 __func__, urb->status);
653 usb_submit_urb(context->rx_urb, GFP_ATOMIC);
659 * Callback function for USB core API: Probe
661 static int sasem_probe(struct usb_interface *interface,
662 const struct usb_device_id *id)
664 struct usb_device *dev = NULL;
665 struct usb_host_interface *iface_desc = NULL;
666 struct usb_endpoint_descriptor *rx_endpoint = NULL;
667 struct usb_endpoint_descriptor *tx_endpoint = NULL;
668 struct urb *rx_urb = NULL;
669 struct urb *tx_urb = NULL;
670 struct lirc_driver *driver = NULL;
671 struct lirc_buffer *rbuf = NULL;
678 struct sasem_context *context = NULL;
681 dev_info(&interface->dev, "%s: found Sasem device\n", __func__);
684 dev = usb_get_dev(interface_to_usbdev(interface));
685 iface_desc = interface->cur_altsetting;
686 num_endpoints = iface_desc->desc.bNumEndpoints;
689 * Scan the endpoint list and set:
690 * first input endpoint = IR endpoint
691 * first output endpoint = VFD endpoint
697 for (i = 0; i < num_endpoints && !(ir_ep_found && vfd_ep_found); ++i) {
699 struct usb_endpoint_descriptor *ep;
703 ep = &iface_desc->endpoint [i].desc;
704 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
705 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
708 ep_dir == USB_DIR_IN &&
709 ep_type == USB_ENDPOINT_XFER_INT) {
714 dev_info(&interface->dev,
715 "%s: found IR endpoint\n", __func__);
717 } else if (!vfd_ep_found &&
718 ep_dir == USB_DIR_OUT &&
719 ep_type == USB_ENDPOINT_XFER_INT) {
724 dev_info(&interface->dev,
725 "%s: found VFD endpoint\n", __func__);
729 /* Input endpoint is mandatory */
731 dev_err(&interface->dev,
732 "%s: no valid input (IR) endpoint found.\n", __func__);
738 dev_info(&interface->dev,
739 "%s: no valid output (VFD) endpoint found.\n",
743 /* Allocate memory */
746 context = kzalloc(sizeof(struct sasem_context), GFP_KERNEL);
749 goto alloc_status_switch;
751 driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
754 goto alloc_status_switch;
756 rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
759 goto alloc_status_switch;
761 if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
762 dev_err(&interface->dev,
763 "%s: lirc_buffer_init failed\n", __func__);
765 goto alloc_status_switch;
767 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
769 dev_err(&interface->dev,
770 "%s: usb_alloc_urb failed for IR urb\n", __func__);
772 goto alloc_status_switch;
775 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
777 dev_err(&interface->dev,
778 "%s: usb_alloc_urb failed for VFD urb",
781 goto alloc_status_switch;
785 mutex_init(&context->ctx_lock);
787 strcpy(driver->name, MOD_NAME);
789 driver->code_length = 64;
790 driver->sample_rate = 0;
791 driver->features = LIRC_CAN_REC_LIRCCODE;
792 driver->data = context;
794 driver->set_use_inc = ir_open;
795 driver->set_use_dec = ir_close;
796 driver->dev = &interface->dev;
797 driver->owner = THIS_MODULE;
799 mutex_lock(&context->ctx_lock);
801 lirc_minor = lirc_register_driver(driver);
802 if (lirc_minor < 0) {
803 dev_err(&interface->dev,
804 "%s: lirc_register_driver failed\n", __func__);
809 dev_info(&interface->dev,
810 "%s: Registered Sasem driver (minor:%d)\n",
811 __func__, lirc_minor);
813 /* Needed while unregistering! */
814 driver->minor = lirc_minor;
817 context->dev_present = 1;
818 context->rx_endpoint = rx_endpoint;
819 context->rx_urb = rx_urb;
821 context->tx_endpoint = tx_endpoint;
822 context->tx_urb = tx_urb;
823 context->vfd_contrast = 1000; /* range 0 - 1000 */
825 context->driver = driver;
827 usb_set_intfdata(interface, context);
832 dev_info(&interface->dev,
833 "Registering VFD with sysfs\n");
834 if (usb_register_dev(interface, &sasem_class))
835 /* Not a fatal error, so ignore */
836 dev_info(&interface->dev,
837 "%s: could not get a minor number for VFD\n",
841 dev_info(&interface->dev,
842 "%s: Sasem device on usb<%d:%d> initialized\n",
843 __func__, dev->bus->busnum, dev->devnum);
845 mutex_unlock(&context->ctx_lock);
848 switch (alloc_status) {
852 usb_free_urb(tx_urb);
854 usb_free_urb(rx_urb);
857 lirc_buffer_free(rbuf);
879 * Callback function for USB core API: disconnect
881 static void sasem_disconnect(struct usb_interface *interface)
883 struct sasem_context *context;
885 /* prevent races with ir_open()/vfd_open() */
886 mutex_lock(&disconnect_lock);
888 context = usb_get_intfdata(interface);
889 mutex_lock(&context->ctx_lock);
891 dev_info(&interface->dev, "%s: Sasem device disconnected\n",
894 usb_set_intfdata(interface, NULL);
895 context->dev_present = 0;
898 usb_kill_urb(context->rx_urb);
900 /* Abort ongoing write */
901 if (atomic_read(&context->tx.busy)) {
903 usb_kill_urb(context->tx_urb);
904 wait_for_completion(&context->tx.finished);
907 /* De-register from lirc_dev if IR port is not open */
908 if (!context->ir_isopen)
909 deregister_from_lirc(context);
911 usb_deregister_dev(interface, &sasem_class);
913 mutex_unlock(&context->ctx_lock);
915 if (!context->ir_isopen && !context->vfd_isopen)
916 delete_context(context);
918 mutex_unlock(&disconnect_lock);
921 module_usb_driver(sasem_driver);