23 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
36 #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
37 #define MOD_NAME "lirc_imon"
38 #define MOD_VERSION "0.8"
40 #define DISPLAY_MINOR_BASE 144
41 #define DEVICE_NAME "lcd%d"
43 #define BUF_CHUNK_SIZE 4
46 #define BIT_DURATION 250
54 static void usb_rx_callback(
struct urb *
urb);
55 static void usb_tx_callback(
struct urb *
urb);
67 size_t n_bytes, loff_t *
pos);
70 static int ir_open(
void *
data);
71 static void ir_close(
void *
data);
74 #define IMON_DATA_BUF_SZ 35
112 .open = &display_open,
114 .release = &display_close,
130 { USB_DEVICE(0x0aa8, 0x8001) },
133 { USB_DEVICE(0x04e8, 0xff30) },
136 { USB_DEVICE(0x0aa8, 0xffda) },
139 { USB_DEVICE(0x15c2, 0xffda) },
146 { USB_DEVICE(0x15c2, 0xffda) },
152 { USB_DEVICE(0x0aa8, 0x8001) },
153 { USB_DEVICE(0x04e8, 0xff30) },
158 static struct usb_driver imon_driver = {
161 .disconnect = imon_disconnect,
162 .suspend = imon_suspend,
163 .resume = imon_resume,
164 .id_table = imon_usb_id_table,
167 static struct usb_class_driver imon_class = {
169 .fops = &display_fops,
193 lirc_buffer_free(context->
driver->rbuf);
198 dev_dbg(dev,
"%s: iMON context freed\n", __func__);
209 ": %s: unable to deregister from lirc(%d)",
213 "(minor:%d)\n", minor);
231 subminor = iminor(inode);
235 ": %s: could not find interface for minor %d\n",
240 context = usb_get_intfdata(interface);
244 "%s: no context found for minor %d\n",
254 "%s: display not supported by device\n", __func__);
258 "%s: display port is already open\n", __func__);
277 static int display_close(
struct inode *inode,
struct file *file)
286 "%s: no context for device\n", __func__);
294 "%s: display not supported by device\n", __func__);
298 "%s: display is not open\n", __func__);
310 free_imon_context(context);
330 pipe = usb_sndintpipe(context->
usbdev,
334 usb_fill_int_urb(context->
tx_urb, context->
usbdev, pipe,
337 usb_tx_callback, context, interval);
339 context->
tx_urb->actual_length = 0;
341 init_completion(&context->
tx.finished);
348 "%s: error submitting urb(%d)\n", __func__, retval);
353 &context->
tx.finished);
356 "%s: task interrupted\n", __func__);
359 retval = context->
tx.status;
362 "%s: packet tx failed (%d)\n",
380 static ssize_t vfd_write(
struct file *file,
const char __user *
buf,
381 size_t n_bytes, loff_t *
pos)
388 const unsigned char vfd_packet6[] = {
389 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
395 "%s: no context for device\n", __func__);
403 "%s: no iMON device present\n", __func__);
410 "%s: invalid payload size\n", __func__);
416 if (IS_ERR(data_buf)) {
417 retval = PTR_ERR(data_buf);
421 memcpy(context->
tx.data_buf, data_buf, n_bytes);
425 context->
tx.data_buf[i] =
' ';
428 context->
tx.data_buf[i] = 0xFF;
440 "%s: send packet failed for packet #%d\n",
448 }
while (offset < IMON_DATA_BUF_SZ);
457 "%s: send packet failed for packet #%d\n",
465 return (!retval) ? n_bytes :
retval;
471 static void usb_tx_callback(
struct urb *
urb)
481 context->
tx.status = urb->status;
493 static int ir_open(
void *
data)
504 context->
rx.count = 0;
505 context->
rx.initial_space = 1;
506 context->
rx.prev_bit = 0;
518 static void ir_close(
void *
data)
525 "%s: no context for device\n", __func__);
539 deregister_from_lirc(context);
543 free_imon_context(context);
562 unsigned char buf[4];
570 if (context->
rx.prev_bit)
573 for (i = 0; i < 4; ++
i)
574 buf[i] = value>>(i*8);
576 lirc_buffer_write(context->
driver->rbuf, buf);
607 static void imon_incoming_packet(
struct imon_context *context,
608 struct urb *urb,
int intf)
610 int len = urb->actual_length;
611 unsigned char *buf = urb->transfer_buffer;
624 dev_warn(dev,
"imon %s: invalid incoming packet "
625 "size (len = %d, intf%d)\n", __func__, len, intf);
631 for (i = 0; i < len; ++
i)
650 if (buf[7] == 1 && context->
rx.initial_space) {
652 context->
rx.prev_bit = 0;
653 context->
rx.count = 4;
654 submit_data(context);
655 context->
rx.count = 0;
658 for (octet = 0; octet < 5; ++octet) {
660 for (bit = 0; bit < 8; ++
bit) {
661 int curr_bit = !(buf[octet] &
mask);
662 if (curr_bit != context->
rx.prev_bit) {
663 if (context->
rx.count) {
664 submit_data(context);
665 context->
rx.count = 0;
667 context->
rx.prev_bit = curr_bit;
675 if (context->
rx.count) {
676 submit_data(context);
677 context->
rx.count = 0;
679 context->
rx.initial_space = context->
rx.prev_bit;
686 static void usb_rx_callback(
struct urb *urb)
698 switch (urb->status) {
703 imon_incoming_packet(context, urb, intfnum);
708 __func__, urb->status);
723 struct usb_device *usbdev =
NULL;
724 struct usb_host_interface *iface_desc =
NULL;
727 struct urb *rx_urb =
NULL;
728 struct urb *tx_urb =
NULL;
731 struct device *dev = &interface->dev;
736 int display_ep_found = 0;
738 int alloc_status = 0;
739 int vfd_proto_6p = 0;
749 dev_err(dev,
"%s: kzalloc failed for context\n", __func__);
751 goto alloc_status_switch;
763 usbdev =
usb_get_dev(interface_to_usbdev(interface));
764 iface_desc = interface->cur_altsetting;
765 num_endpts = iface_desc->desc.bNumEndpoints;
766 ifnum = iface_desc->desc.bInterfaceNumber;
768 product =
le16_to_cpu(usbdev->descriptor.idProduct);
770 dev_dbg(dev,
"%s: found iMON device (%04x:%04x, intf%d)\n",
771 __func__, vendor, product, ifnum);
778 for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++
i) {
782 ep = &iface_desc->endpoint[
i].desc;
792 dev_dbg(dev,
"%s: found IR endpoint\n", __func__);
794 }
else if (!display_ep_found && ep_dir ==
USB_DIR_OUT &&
797 display_ep_found = 1;
798 dev_dbg(dev,
"%s: found display endpoint\n", __func__);
808 display_ep_found = 0;
809 dev_dbg(dev,
"%s: device has no display\n", __func__);
814 dev_err(dev,
"%s: no valid input (IR) endpoint found.\n", __func__);
817 goto alloc_status_switch;
821 if (display_ep_found) {
825 dev_dbg(dev,
"%s: vfd_proto_6p: %d\n",
826 __func__, vfd_proto_6p);
831 dev_err(dev,
"%s: kzalloc failed for lirc_driver\n", __func__);
833 goto alloc_status_switch;
837 dev_err(dev,
"%s: kmalloc failed for lirc_buffer\n", __func__);
839 goto alloc_status_switch;
842 dev_err(dev,
"%s: lirc_buffer_init failed\n", __func__);
844 goto alloc_status_switch;
848 dev_err(dev,
"%s: usb_alloc_urb failed for IR urb\n", __func__);
850 goto alloc_status_switch;
854 dev_err(dev,
"%s: usb_alloc_urb failed for display urb\n",
857 goto alloc_status_switch;
872 driver->
dev = &interface->dev;
881 if (lirc_minor < 0) {
882 dev_err(dev,
"%s: lirc_register_driver failed\n", __func__);
886 dev_info(dev,
"Registered iMON driver "
887 "(lirc minor: %d)\n", lirc_minor);
890 driver->
minor = lirc_minor;
904 if (display_ep_found)
908 usb_rcvintpipe(context->
usbdev,
911 usb_rx_callback, context,
917 dev_err(dev,
"%s: usb_submit_urb failed for intf0 (%d)\n",
923 usb_set_intfdata(interface, context);
925 if (context->
display && ifnum == 0) {
926 dev_dbg(dev,
"%s: Registering iMON display with sysfs\n",
931 dev_info(dev,
"%s: could not get a minor number for "
932 "display\n", __func__);
936 dev_info(dev,
"iMON device (%04x:%04x, intf%d) on "
937 "usb<%d:%d> initialized\n", vendor, product, ifnum,
938 usbdev->
bus->busnum, usbdev->devnum);
944 switch (alloc_status) {
951 lirc_buffer_free(rbuf);
984 context = usb_get_intfdata(interface);
985 ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
989 usb_set_intfdata(interface,
NULL);
1003 deregister_from_lirc(context);
1006 free_imon_context(context);
1031 usb_rcvintpipe(context->
usbdev,
1034 usb_rx_callback, context,