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>
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 int 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 {
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!) */
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 ***/
157 MODULE_AUTHOR(MOD_AUTHOR);
158 MODULE_DESCRIPTION(MOD_DESC);
159 MODULE_LICENSE("GPL");
160 module_param(debug, int, S_IRUGO | S_IWUSR);
161 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
163 static void delete_context(struct sasem_context *context)
165 usb_free_urb(context->tx_urb); /* VFD */
166 usb_free_urb(context->rx_urb); /* IR */
167 lirc_buffer_free(context->driver->rbuf);
168 kfree(context->driver->rbuf);
169 kfree(context->driver);
173 static void deregister_from_lirc(struct sasem_context *context)
176 int minor = context->driver->minor;
178 retval = lirc_unregister_driver(minor);
180 dev_err(&context->dev->dev,
181 "%s: unable to deregister from lirc (%d)\n",
184 dev_info(&context->dev->dev,
185 "Deregistered Sasem driver (minor:%d)\n", minor);
189 * Called when the VFD device (e.g. /dev/usb/lcd)
190 * is opened by the application.
192 static int vfd_open(struct inode *inode, struct file *file)
194 struct usb_interface *interface;
195 struct sasem_context *context = NULL;
199 /* prevent races with disconnect */
200 mutex_lock(&disconnect_lock);
202 subminor = iminor(inode);
203 interface = usb_find_interface(&sasem_driver, subminor);
205 pr_err("%s: could not find interface for minor %d\n",
210 context = usb_get_intfdata(interface);
213 dev_err(&interface->dev, "no context found for minor %d\n",
219 mutex_lock(&context->ctx_lock);
221 if (context->vfd_isopen) {
222 dev_err(&interface->dev,
223 "%s: VFD port is already open", __func__);
226 context->vfd_isopen = 1;
227 file->private_data = context;
228 dev_info(&interface->dev, "VFD port opened\n");
231 mutex_unlock(&context->ctx_lock);
234 mutex_unlock(&disconnect_lock);
239 * Called when the VFD device (e.g. /dev/usb/lcd)
240 * is closed by the application.
242 static long vfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
244 struct sasem_context *context;
246 context = (struct sasem_context *) file->private_data;
249 pr_err("%s: no context for device\n", __func__);
253 mutex_lock(&context->ctx_lock);
256 case IOCTL_LCD_CONTRAST:
259 context->vfd_contrast = (unsigned int)arg;
262 pr_info("Unknown IOCTL command\n");
263 mutex_unlock(&context->ctx_lock);
264 return -ENOIOCTLCMD; /* not supported */
267 mutex_unlock(&context->ctx_lock);
272 * Called when the VFD device (e.g. /dev/usb/lcd)
273 * is closed by the application.
275 static int vfd_close(struct inode *inode, struct file *file)
277 struct sasem_context *context = NULL;
280 context = (struct sasem_context *) file->private_data;
283 pr_err("%s: no context for device\n", __func__);
287 mutex_lock(&context->ctx_lock);
289 if (!context->vfd_isopen) {
290 dev_err(&context->dev->dev, "%s: VFD is not open\n", __func__);
293 context->vfd_isopen = 0;
294 dev_info(&context->dev->dev, "VFD port closed\n");
295 if (!context->dev_present && !context->ir_isopen) {
296 /* Device disconnected before close and IR port is
297 * not open. If IR port is open, context will be
298 * deleted by ir_close. */
299 mutex_unlock(&context->ctx_lock);
300 delete_context(context);
305 mutex_unlock(&context->ctx_lock);
310 * Sends a packet to the VFD.
312 static int send_packet(struct sasem_context *context)
318 pipe = usb_sndintpipe(context->dev,
319 context->tx_endpoint->bEndpointAddress);
320 interval = context->tx_endpoint->bInterval;
322 usb_fill_int_urb(context->tx_urb, context->dev, pipe,
323 context->usb_tx_buf, sizeof(context->usb_tx_buf),
324 usb_tx_callback, context, interval);
326 context->tx_urb->actual_length = 0;
328 init_completion(&context->tx.finished);
329 atomic_set(&context->tx.busy, 1);
331 retval = usb_submit_urb(context->tx_urb, GFP_KERNEL);
333 atomic_set(&context->tx.busy, 0);
334 dev_err(&context->dev->dev, "error submitting urb (%d)\n",
337 /* Wait for transmission to complete (or abort) */
338 mutex_unlock(&context->ctx_lock);
339 wait_for_completion(&context->tx.finished);
340 mutex_lock(&context->ctx_lock);
342 retval = context->tx.status;
344 dev_err(&context->dev->dev,
345 "packet tx failed (%d)\n", retval);
352 * Writes data to the VFD. The Sasem VFD is 2x16 characters
353 * and requires data in 9 consecutive USB interrupt packets,
354 * each packet carrying 8 bytes.
356 static ssize_t vfd_write(struct file *file, const char __user *buf,
357 size_t n_bytes, loff_t *pos)
361 struct sasem_context *context;
362 int *data_buf = NULL;
364 context = (struct sasem_context *) file->private_data;
366 pr_err("%s: no context for device\n", __func__);
370 mutex_lock(&context->ctx_lock);
372 if (!context->dev_present) {
373 pr_err("%s: no Sasem device present\n", __func__);
378 if (n_bytes <= 0 || n_bytes > SASEM_DATA_BUF_SZ) {
379 dev_err(&context->dev->dev, "%s: invalid payload size\n",
385 data_buf = memdup_user(buf, n_bytes);
386 if (IS_ERR(data_buf)) {
387 retval = 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);
439 dev_err(&context->dev->dev,
440 "send packet failed for packet #%d\n", i);
446 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);
473 * Called by lirc_dev when the application opens /dev/lirc
475 static int ir_open(void *data)
478 struct sasem_context *context;
480 /* prevent races with disconnect */
481 mutex_lock(&disconnect_lock);
485 mutex_lock(&context->ctx_lock);
487 if (context->ir_isopen) {
488 dev_err(&context->dev->dev, "%s: IR port is already open\n",
494 usb_fill_int_urb(context->rx_urb, context->dev,
495 usb_rcvintpipe(context->dev,
496 context->rx_endpoint->bEndpointAddress),
497 context->usb_rx_buf, sizeof(context->usb_rx_buf),
498 usb_rx_callback, context, context->rx_endpoint->bInterval);
500 retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
503 dev_err(&context->dev->dev,
504 "usb_submit_urb failed for ir_open (%d)\n", retval);
506 context->ir_isopen = 1;
507 dev_info(&context->dev->dev, "IR port opened\n");
511 mutex_unlock(&context->ctx_lock);
513 mutex_unlock(&disconnect_lock);
518 * Called by lirc_dev when the application closes /dev/lirc
520 static void ir_close(void *data)
522 struct sasem_context *context;
526 pr_err("%s: no context for device\n", __func__);
530 mutex_lock(&context->ctx_lock);
532 usb_kill_urb(context->rx_urb);
533 context->ir_isopen = 0;
534 pr_info("IR port closed\n");
536 if (!context->dev_present) {
539 * Device disconnected while IR port was
540 * still open. Driver was not deregistered
541 * at disconnect time, so do it now.
543 deregister_from_lirc(context);
544 if (!context->vfd_isopen) {
545 mutex_unlock(&context->ctx_lock);
546 delete_context(context);
549 /* If VFD port is open, context will be deleted by vfd_close */
552 mutex_unlock(&context->ctx_lock);
556 * Process the incoming packet
558 static void incoming_packet(struct sasem_context *context,
561 int len = urb->actual_length;
562 unsigned char *buf = urb->transfer_buffer;
567 dev_warn(&context->dev->dev,
568 "%s: invalid incoming packet size (%d)\n",
574 dev_info(&context->dev->dev, "Incoming data: %*ph\n", len, buf);
576 * Lirc could deal with the repeat code, but we really need to block it
577 * if it arrives too late. Otherwise we could repeat the wrong code.
580 /* get the time since the last button press */
582 ns = ktime_to_ns(ktime_sub(kt, context->presstime));
584 if (memcmp(buf, "\x08\0\0\0\0\0\0\0", 8) == 0) {
586 * the repeat code is being sent, so we copy
587 * the old code to LIRC
591 * NOTE: Only if the last code was less than 250ms ago
592 * - no one should be able to push another (undetected) button
593 * in that time and then get a false repeat of the previous
594 * press but it is long enough for a genuine repeat
596 if ((ns < 250 * NSEC_PER_MSEC) && (context->codesaved != 0)) {
597 memcpy(buf, &context->lastcode, 8);
598 context->presstime = kt;
601 /* save the current valid code for repeats */
602 memcpy(&context->lastcode, buf, 8);
604 * set flag to signal a valid code was save;
605 * just for safety reasons
607 context->codesaved = 1;
608 context->presstime = kt;
611 lirc_buffer_write(context->driver->rbuf, buf);
612 wake_up(&context->driver->rbuf->wait_poll);
616 * Callback function for USB core API: receive data
618 static void usb_rx_callback(struct urb *urb)
620 struct sasem_context *context;
624 context = (struct sasem_context *) urb->context;
628 switch (urb->status) {
629 case -ENOENT: /* usbcore unlink successful! */
633 if (context->ir_isopen)
634 incoming_packet(context, urb);
638 dev_warn(&urb->dev->dev, "%s: status (%d): ignored",
639 __func__, urb->status);
643 usb_submit_urb(context->rx_urb, GFP_ATOMIC);
647 * Callback function for USB core API: Probe
649 static int sasem_probe(struct usb_interface *interface,
650 const struct usb_device_id *id)
652 struct usb_device *dev = NULL;
653 struct usb_host_interface *iface_desc = NULL;
654 struct usb_endpoint_descriptor *rx_endpoint = NULL;
655 struct usb_endpoint_descriptor *tx_endpoint = NULL;
656 struct urb *rx_urb = NULL;
657 struct urb *tx_urb = NULL;
658 struct lirc_driver *driver = NULL;
659 struct lirc_buffer *rbuf = NULL;
666 struct sasem_context *context = NULL;
669 dev_info(&interface->dev, "%s: found Sasem device\n", __func__);
672 dev = usb_get_dev(interface_to_usbdev(interface));
673 iface_desc = interface->cur_altsetting;
674 num_endpoints = iface_desc->desc.bNumEndpoints;
677 * Scan the endpoint list and set:
678 * first input endpoint = IR endpoint
679 * first output endpoint = VFD endpoint
685 for (i = 0; i < num_endpoints && !(ir_ep_found && vfd_ep_found); ++i) {
687 struct usb_endpoint_descriptor *ep;
689 ep = &iface_desc->endpoint [i].desc;
692 usb_endpoint_is_int_in(ep)) {
697 dev_info(&interface->dev,
698 "%s: found IR endpoint\n", __func__);
700 } else if (!vfd_ep_found &&
701 usb_endpoint_is_int_out(ep)) {
705 dev_info(&interface->dev,
706 "%s: found VFD endpoint\n", __func__);
710 /* Input endpoint is mandatory */
712 dev_err(&interface->dev,
713 "%s: no valid input (IR) endpoint found.\n", __func__);
719 dev_info(&interface->dev,
720 "%s: no valid output (VFD) endpoint found.\n",
724 /* Allocate memory */
727 context = kzalloc(sizeof(*context), GFP_KERNEL);
730 goto alloc_status_switch;
732 driver = kzalloc(sizeof(*driver), GFP_KERNEL);
735 goto alloc_status_switch;
737 rbuf = kmalloc(sizeof(*rbuf), GFP_KERNEL);
740 goto alloc_status_switch;
742 if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
743 dev_err(&interface->dev,
744 "%s: lirc_buffer_init failed\n", __func__);
746 goto alloc_status_switch;
748 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
751 goto alloc_status_switch;
754 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
757 goto alloc_status_switch;
761 mutex_init(&context->ctx_lock);
763 strcpy(driver->name, MOD_NAME);
765 driver->code_length = 64;
766 driver->sample_rate = 0;
767 driver->features = LIRC_CAN_REC_LIRCCODE;
768 driver->data = context;
770 driver->set_use_inc = ir_open;
771 driver->set_use_dec = ir_close;
772 driver->dev = &interface->dev;
773 driver->owner = THIS_MODULE;
775 mutex_lock(&context->ctx_lock);
777 lirc_minor = lirc_register_driver(driver);
778 if (lirc_minor < 0) {
779 dev_err(&interface->dev,
780 "%s: lirc_register_driver failed\n", __func__);
785 dev_info(&interface->dev,
786 "%s: Registered Sasem driver (minor:%d)\n",
787 __func__, lirc_minor);
789 /* Needed while unregistering! */
790 driver->minor = lirc_minor;
793 context->dev_present = 1;
794 context->rx_endpoint = rx_endpoint;
795 context->rx_urb = rx_urb;
797 context->tx_endpoint = tx_endpoint;
798 context->tx_urb = tx_urb;
799 context->vfd_contrast = 1000; /* range 0 - 1000 */
801 context->driver = driver;
803 usb_set_intfdata(interface, context);
808 dev_info(&interface->dev,
809 "Registering VFD with sysfs\n");
810 if (usb_register_dev(interface, &sasem_class))
811 /* Not a fatal error, so ignore */
812 dev_info(&interface->dev,
813 "%s: could not get a minor number for VFD\n",
817 dev_info(&interface->dev,
818 "%s: Sasem device on usb<%d:%d> initialized\n",
819 __func__, dev->bus->busnum, dev->devnum);
821 mutex_unlock(&context->ctx_lock);
824 switch (alloc_status) {
828 usb_free_urb(tx_urb);
830 usb_free_urb(rx_urb);
833 lirc_buffer_free(rbuf);
855 * Callback function for USB core API: disconnect
857 static void sasem_disconnect(struct usb_interface *interface)
859 struct sasem_context *context;
861 /* prevent races with ir_open()/vfd_open() */
862 mutex_lock(&disconnect_lock);
864 context = usb_get_intfdata(interface);
865 mutex_lock(&context->ctx_lock);
867 dev_info(&interface->dev, "%s: Sasem device disconnected\n",
870 usb_set_intfdata(interface, NULL);
871 context->dev_present = 0;
874 usb_kill_urb(context->rx_urb);
876 /* Abort ongoing write */
877 if (atomic_read(&context->tx.busy)) {
879 usb_kill_urb(context->tx_urb);
880 wait_for_completion(&context->tx.finished);
883 /* De-register from lirc_dev if IR port is not open */
884 if (!context->ir_isopen)
885 deregister_from_lirc(context);
887 usb_deregister_dev(interface, &sasem_class);
889 mutex_unlock(&context->ctx_lock);
891 if (!context->ir_isopen && !context->vfd_isopen)
892 delete_context(context);
894 mutex_unlock(&disconnect_lock);
897 module_usb_driver(sasem_driver);