2 * lirc_imon.c: LIRC/VFD/LCD driver for SoundGraph iMON IR/VFD/LCD
3 * including the iMON PAD model
5 * Copyright(C) 2004 Venky Raju(dev@venky.ws)
6 * Copyright(C) 2009 Jarod Wilson <jarod@wilsonet.com>
8 * lirc_imon is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/uaccess.h>
29 #include <linux/usb.h>
31 #include <media/lirc.h>
32 #include <media/lirc_dev.h>
35 #define MOD_AUTHOR "Venky Raju <dev@venky.ws>"
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 /* each bit received is 250us */
48 /*** P R O T O T Y P E S ***/
50 /* USB Callback prototypes */
51 static int imon_probe(struct usb_interface
*interface
,
52 const struct usb_device_id
*id
);
53 static void imon_disconnect(struct usb_interface
*interface
);
54 static void usb_rx_callback(struct urb
*urb
);
55 static void usb_tx_callback(struct urb
*urb
);
57 /* suspend/resume support */
58 static int imon_resume(struct usb_interface
*intf
);
59 static int imon_suspend(struct usb_interface
*intf
, pm_message_t message
);
61 /* Display file_operations function prototypes */
62 static int display_open(struct inode
*inode
, struct file
*file
);
63 static int display_close(struct inode
*inode
, struct file
*file
);
65 /* VFD write operation */
66 static ssize_t
vfd_write(struct file
*file
, const char *buf
,
67 size_t n_bytes
, loff_t
*pos
);
69 /* LIRC driver function prototypes */
70 static int ir_open(void *data
);
71 static void ir_close(void *data
);
73 /* Driver init/exit prototypes */
74 static int __init
imon_init(void);
75 static void __exit
imon_exit(void);
77 /*** G L O B A L S ***/
78 #define IMON_DATA_BUF_SZ 35
81 struct usb_device
*usbdev
;
82 /* Newer devices have two interfaces */
83 int display
; /* not all controllers do */
84 int display_isopen
; /* display port has been opened */
85 int ir_isopen
; /* IR port open */
86 int dev_present
; /* USB device presence */
87 struct mutex ctx_lock
; /* to lock this object */
88 wait_queue_head_t remove_ok
; /* For unexpected USB disconnects */
90 int vfd_proto_6p
; /* some VFD require a 6th packet */
92 struct lirc_driver
*driver
;
93 struct usb_endpoint_descriptor
*rx_endpoint
;
94 struct usb_endpoint_descriptor
*tx_endpoint
;
97 unsigned char usb_rx_buf
[8];
98 unsigned char usb_tx_buf
[8];
101 int count
; /* length of 0 or 1 sequence */
102 int prev_bit
; /* logic level of sequence */
103 int initial_space
; /* initial space flag */
107 unsigned char data_buf
[IMON_DATA_BUF_SZ
]; /* user data buffer */
108 struct completion finished
; /* wait for write to finish */
109 atomic_t busy
; /* write in progress */
110 int status
; /* status of tx completion */
114 static const struct file_operations display_fops
= {
115 .owner
= THIS_MODULE
,
116 .open
= &display_open
,
118 .release
= &display_close
,
119 .llseek
= noop_llseek
,
123 * USB Device ID for iMON USB Control Boards
125 * The Windows drivers contain 6 different inf files, more or less one for
126 * each new device until the 0x0034-0x0046 devices, which all use the same
127 * driver. Some of the devices in the 34-46 range haven't been definitively
128 * identified yet. Early devices have either a TriGem Computer, Inc. or a
129 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
130 * devices use the SoundGraph vendor ID (0x15c2).
132 static struct usb_device_id imon_usb_id_table
[] = {
133 /* TriGem iMON (IR only) -- TG_iMON.inf */
134 { USB_DEVICE(0x0aa8, 0x8001) },
136 /* SoundGraph iMON (IR only) -- sg_imon.inf */
137 { USB_DEVICE(0x04e8, 0xff30) },
139 /* SoundGraph iMON VFD (IR & VFD) -- iMON_VFD.inf */
140 { USB_DEVICE(0x0aa8, 0xffda) },
142 /* SoundGraph iMON SS (IR & VFD) -- iMON_SS.inf */
143 { USB_DEVICE(0x15c2, 0xffda) },
148 /* Some iMON VFD models requires a 6th packet for VFD writes */
149 static struct usb_device_id vfd_proto_6p_list
[] = {
150 { USB_DEVICE(0x15c2, 0xffda) },
154 /* Some iMON devices have no lcd/vfd, don't set one up */
155 static struct usb_device_id ir_only_list
[] = {
156 { USB_DEVICE(0x0aa8, 0x8001) },
157 { USB_DEVICE(0x04e8, 0xff30) },
161 /* USB Device data */
162 static struct usb_driver imon_driver
= {
165 .disconnect
= imon_disconnect
,
166 .suspend
= imon_suspend
,
167 .resume
= imon_resume
,
168 .id_table
= imon_usb_id_table
,
171 static struct usb_class_driver imon_class
= {
173 .fops
= &display_fops
,
174 .minor_base
= DISPLAY_MINOR_BASE
,
177 /* to prevent races between open() and disconnect(), probing, etc */
178 static DEFINE_MUTEX(driver_lock
);
182 /*** M O D U L E C O D E ***/
184 MODULE_AUTHOR(MOD_AUTHOR
);
185 MODULE_DESCRIPTION(MOD_DESC
);
186 MODULE_VERSION(MOD_VERSION
);
187 MODULE_LICENSE("GPL");
188 MODULE_DEVICE_TABLE(usb
, imon_usb_id_table
);
189 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
190 MODULE_PARM_DESC(debug
, "Debug messages: 0=no, 1=yes(default: no)");
192 static void free_imon_context(struct imon_context
*context
)
194 struct device
*dev
= context
->driver
->dev
;
195 usb_free_urb(context
->tx_urb
);
196 usb_free_urb(context
->rx_urb
);
197 lirc_buffer_free(context
->driver
->rbuf
);
198 kfree(context
->driver
->rbuf
);
199 kfree(context
->driver
);
202 dev_dbg(dev
, "%s: iMON context freed\n", __func__
);
205 static void deregister_from_lirc(struct imon_context
*context
)
208 int minor
= context
->driver
->minor
;
210 retval
= lirc_unregister_driver(minor
);
212 err("%s: unable to deregister from lirc(%d)",
215 printk(KERN_INFO MOD_NAME
": Deregistered iMON driver "
216 "(minor:%d)\n", minor
);
221 * Called when the Display device (e.g. /dev/lcd0)
222 * is opened by the application.
224 static int display_open(struct inode
*inode
, struct file
*file
)
226 struct usb_interface
*interface
;
227 struct imon_context
*context
= NULL
;
231 /* prevent races with disconnect */
232 mutex_lock(&driver_lock
);
234 subminor
= iminor(inode
);
235 interface
= usb_find_interface(&imon_driver
, subminor
);
237 err("%s: could not find interface for minor %d",
242 context
= usb_get_intfdata(interface
);
245 err("%s: no context found for minor %d",
251 mutex_lock(&context
->ctx_lock
);
253 if (!context
->display
) {
254 err("%s: display not supported by device", __func__
);
256 } else if (context
->display_isopen
) {
257 err("%s: display port is already open", __func__
);
260 context
->display_isopen
= 1;
261 file
->private_data
= context
;
262 dev_info(context
->driver
->dev
, "display port opened\n");
265 mutex_unlock(&context
->ctx_lock
);
268 mutex_unlock(&driver_lock
);
273 * Called when the display device (e.g. /dev/lcd0)
274 * is closed by the application.
276 static int display_close(struct inode
*inode
, struct file
*file
)
278 struct imon_context
*context
= NULL
;
281 context
= file
->private_data
;
284 err("%s: no context for device", __func__
);
288 mutex_lock(&context
->ctx_lock
);
290 if (!context
->display
) {
291 err("%s: display not supported by device", __func__
);
293 } else if (!context
->display_isopen
) {
294 err("%s: display is not open", __func__
);
297 context
->display_isopen
= 0;
298 dev_info(context
->driver
->dev
, "display port closed\n");
299 if (!context
->dev_present
&& !context
->ir_isopen
) {
301 * Device disconnected before close and IR port is not
302 * open. If IR port is open, context will be deleted by
305 mutex_unlock(&context
->ctx_lock
);
306 free_imon_context(context
);
311 mutex_unlock(&context
->ctx_lock
);
316 * Sends a packet to the device -- this function must be called
317 * with context->ctx_lock held.
319 static int send_packet(struct imon_context
*context
)
325 /* Check if we need to use control or interrupt urb */
326 pipe
= usb_sndintpipe(context
->usbdev
,
327 context
->tx_endpoint
->bEndpointAddress
);
328 interval
= context
->tx_endpoint
->bInterval
;
330 usb_fill_int_urb(context
->tx_urb
, context
->usbdev
, pipe
,
332 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 err("%s: error submitting urb(%d)", __func__
, retval
);
345 /* Wait for transmission to complete (or abort) */
346 mutex_unlock(&context
->ctx_lock
);
347 retval
= wait_for_completion_interruptible(
348 &context
->tx
.finished
);
350 err("%s: task interrupted", __func__
);
351 mutex_lock(&context
->ctx_lock
);
353 retval
= context
->tx
.status
;
355 err("%s: packet tx failed (%d)", __func__
, retval
);
362 * Writes data to the VFD. The iMON VFD is 2x16 characters
363 * and requires data in 5 consecutive USB interrupt packets,
364 * each packet but the last carrying 7 bytes.
366 * I don't know if the VFD board supports features such as
367 * scrolling, clearing rows, blanking, etc. so at
368 * the caller must provide a full screen of data. If fewer
369 * than 32 bytes are provided spaces will be appended to
370 * generate a full screen.
372 static ssize_t
vfd_write(struct file
*file
, const char *buf
,
373 size_t n_bytes
, loff_t
*pos
)
379 struct imon_context
*context
;
380 const unsigned char vfd_packet6
[] = {
381 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
384 context
= file
->private_data
;
386 err("%s: no context for device", __func__
);
390 mutex_lock(&context
->ctx_lock
);
392 if (!context
->dev_present
) {
393 err("%s: no iMON device present", __func__
);
398 if (n_bytes
<= 0 || n_bytes
> IMON_DATA_BUF_SZ
- 3) {
399 err("%s: invalid payload size", __func__
);
404 data_buf
= memdup_user(buf
, n_bytes
);
405 if (IS_ERR(data_buf
)) {
406 retval
= PTR_ERR(data_buf
);
410 memcpy(context
->tx
.data_buf
, data_buf
, n_bytes
);
412 /* Pad with spaces */
413 for (i
= n_bytes
; i
< IMON_DATA_BUF_SZ
- 3; ++i
)
414 context
->tx
.data_buf
[i
] = ' ';
416 for (i
= IMON_DATA_BUF_SZ
- 3; i
< IMON_DATA_BUF_SZ
; ++i
)
417 context
->tx
.data_buf
[i
] = 0xFF;
423 memcpy(context
->usb_tx_buf
, context
->tx
.data_buf
+ offset
, 7);
424 context
->usb_tx_buf
[7] = (unsigned char) seq
;
426 retval
= send_packet(context
);
428 err("%s: send packet failed for packet #%d",
436 } while (offset
< IMON_DATA_BUF_SZ
);
438 if (context
->vfd_proto_6p
) {
440 memcpy(context
->usb_tx_buf
, &vfd_packet6
, sizeof(vfd_packet6
));
441 context
->usb_tx_buf
[7] = (unsigned char) seq
;
442 retval
= send_packet(context
);
444 err("%s: send packet failed for packet #%d",
449 mutex_unlock(&context
->ctx_lock
);
451 return (!retval
) ? n_bytes
: retval
;
455 * Callback function for USB core API: transmit data
457 static void usb_tx_callback(struct urb
*urb
)
459 struct imon_context
*context
;
463 context
= (struct imon_context
*)urb
->context
;
467 context
->tx
.status
= urb
->status
;
469 /* notify waiters that write has finished */
470 atomic_set(&context
->tx
.busy
, 0);
471 complete(&context
->tx
.finished
);
477 * Called by lirc_dev when the application opens /dev/lirc
479 static int ir_open(void *data
)
482 struct imon_context
*context
;
484 /* prevent races with disconnect */
485 mutex_lock(&driver_lock
);
487 context
= (struct imon_context
*)data
;
489 /* initial IR protocol decode variables */
490 context
->rx
.count
= 0;
491 context
->rx
.initial_space
= 1;
492 context
->rx
.prev_bit
= 0;
494 context
->ir_isopen
= 1;
495 dev_info(context
->driver
->dev
, "IR port opened\n");
497 mutex_unlock(&driver_lock
);
502 * Called by lirc_dev when the application closes /dev/lirc
504 static void ir_close(void *data
)
506 struct imon_context
*context
;
508 context
= (struct imon_context
*)data
;
510 err("%s: no context for device", __func__
);
514 mutex_lock(&context
->ctx_lock
);
516 context
->ir_isopen
= 0;
517 dev_info(context
->driver
->dev
, "IR port closed\n");
519 if (!context
->dev_present
) {
521 * Device disconnected while IR port was still open. Driver
522 * was not deregistered at disconnect time, so do it now.
524 deregister_from_lirc(context
);
526 if (!context
->display_isopen
) {
527 mutex_unlock(&context
->ctx_lock
);
528 free_imon_context(context
);
532 * If display port is open, context will be deleted by
537 mutex_unlock(&context
->ctx_lock
);
542 * Convert bit count to time duration (in us) and submit
543 * the value to lirc_dev.
545 static void submit_data(struct imon_context
*context
)
547 unsigned char buf
[4];
548 int value
= context
->rx
.count
;
551 dev_dbg(context
->driver
->dev
, "submitting data to LIRC\n");
553 value
*= BIT_DURATION
;
555 if (context
->rx
.prev_bit
)
558 for (i
= 0; i
< 4; ++i
)
559 buf
[i
] = value
>>(i
*8);
561 lirc_buffer_write(context
->driver
->rbuf
, buf
);
562 wake_up(&context
->driver
->rbuf
->wait_poll
);
566 static inline int tv2int(const struct timeval
*a
, const struct timeval
*b
)
571 if (b
->tv_usec
> a
->tv_usec
) {
576 usecs
+= a
->tv_usec
- b
->tv_usec
;
578 sec
+= a
->tv_sec
- b
->tv_sec
;
590 * Process the incoming packet
592 static void imon_incoming_packet(struct imon_context
*context
,
593 struct urb
*urb
, int intf
)
595 int len
= urb
->actual_length
;
596 unsigned char *buf
= urb
->transfer_buffer
;
597 struct device
*dev
= context
->driver
->dev
;
603 * just bail out if no listening IR client
605 if (!context
->ir_isopen
)
609 dev_warn(dev
, "imon %s: invalid incoming packet "
610 "size (len = %d, intf%d)\n", __func__
, len
, intf
);
615 printk(KERN_INFO
"raw packet: ");
616 for (i
= 0; i
< len
; ++i
)
617 printk("%02x ", buf
[i
]);
622 * Translate received data to pulse and space lengths.
623 * Received data is active low, i.e. pulses are 0 and
626 * My original algorithm was essentially similar to
627 * Changwoo Ryu's with the exception that he switched
628 * the incoming bits to active high and also fed an
629 * initial space to LIRC at the start of a new sequence
630 * if the previous bit was a pulse.
632 * I've decided to adopt his algorithm.
635 if (buf
[7] == 1 && context
->rx
.initial_space
) {
636 /* LIRC requires a leading space */
637 context
->rx
.prev_bit
= 0;
638 context
->rx
.count
= 4;
639 submit_data(context
);
640 context
->rx
.count
= 0;
643 for (octet
= 0; octet
< 5; ++octet
) {
645 for (bit
= 0; bit
< 8; ++bit
) {
646 int curr_bit
= !(buf
[octet
] & mask
);
647 if (curr_bit
!= context
->rx
.prev_bit
) {
648 if (context
->rx
.count
) {
649 submit_data(context
);
650 context
->rx
.count
= 0;
652 context
->rx
.prev_bit
= curr_bit
;
660 if (context
->rx
.count
) {
661 submit_data(context
);
662 context
->rx
.count
= 0;
664 context
->rx
.initial_space
= context
->rx
.prev_bit
;
669 * Callback function for USB core API: receive data
671 static void usb_rx_callback(struct urb
*urb
)
673 struct imon_context
*context
;
681 context
= (struct imon_context
*)urb
->context
;
685 buf
= urb
->transfer_buffer
;
686 len
= urb
->actual_length
;
688 switch (urb
->status
) {
689 case -ENOENT
: /* usbcore unlink successful! */
693 imon_incoming_packet(context
, urb
, intfnum
);
697 dev_warn(context
->driver
->dev
, "imon %s: status(%d): ignored\n",
698 __func__
, urb
->status
);
702 usb_submit_urb(context
->rx_urb
, GFP_ATOMIC
);
708 * Callback function for USB core API: Probe
710 static int imon_probe(struct usb_interface
*interface
,
711 const struct usb_device_id
*id
)
713 struct usb_device
*usbdev
= NULL
;
714 struct usb_host_interface
*iface_desc
= NULL
;
715 struct usb_endpoint_descriptor
*rx_endpoint
= NULL
;
716 struct usb_endpoint_descriptor
*tx_endpoint
= NULL
;
717 struct urb
*rx_urb
= NULL
;
718 struct urb
*tx_urb
= NULL
;
719 struct lirc_driver
*driver
= NULL
;
720 struct lirc_buffer
*rbuf
= NULL
;
721 struct device
*dev
= &interface
->dev
;
726 int display_ep_found
= 0;
728 int alloc_status
= 0;
729 int vfd_proto_6p
= 0;
731 struct imon_context
*context
= NULL
;
735 context
= kzalloc(sizeof(struct imon_context
), GFP_KERNEL
);
737 err("%s: kzalloc failed for context", __func__
);
739 goto alloc_status_switch
;
743 * Try to auto-detect the type of display if the user hasn't set
744 * it by hand via the display_type modparam. Default is VFD.
746 if (usb_match_id(interface
, ir_only_list
))
747 context
->display
= 0;
749 context
->display
= 1;
751 code_length
= BUF_CHUNK_SIZE
* 8;
753 usbdev
= usb_get_dev(interface_to_usbdev(interface
));
754 iface_desc
= interface
->cur_altsetting
;
755 num_endpts
= iface_desc
->desc
.bNumEndpoints
;
756 ifnum
= iface_desc
->desc
.bInterfaceNumber
;
757 vendor
= le16_to_cpu(usbdev
->descriptor
.idVendor
);
758 product
= le16_to_cpu(usbdev
->descriptor
.idProduct
);
760 dev_dbg(dev
, "%s: found iMON device (%04x:%04x, intf%d)\n",
761 __func__
, vendor
, product
, ifnum
);
763 /* prevent races probing devices w/multiple interfaces */
764 mutex_lock(&driver_lock
);
767 * Scan the endpoint list and set:
768 * first input endpoint = IR endpoint
769 * first output endpoint = display endpoint
771 for (i
= 0; i
< num_endpts
&& !(ir_ep_found
&& display_ep_found
); ++i
) {
772 struct usb_endpoint_descriptor
*ep
;
775 ep
= &iface_desc
->endpoint
[i
].desc
;
776 ep_dir
= ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
;
777 ep_type
= ep
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
780 ep_dir
== USB_DIR_IN
&&
781 ep_type
== USB_ENDPOINT_XFER_INT
) {
785 dev_dbg(dev
, "%s: found IR endpoint\n", __func__
);
787 } else if (!display_ep_found
&& ep_dir
== USB_DIR_OUT
&&
788 ep_type
== USB_ENDPOINT_XFER_INT
) {
790 display_ep_found
= 1;
791 dev_dbg(dev
, "%s: found display endpoint\n", __func__
);
796 * Some iMON receivers have no display. Unfortunately, it seems
797 * that SoundGraph recycles device IDs between devices both with
800 if (context
->display
== 0) {
801 display_ep_found
= 0;
802 dev_dbg(dev
, "%s: device has no display\n", __func__
);
805 /* Input endpoint is mandatory */
807 err("%s: no valid input (IR) endpoint found.", __func__
);
810 goto alloc_status_switch
;
813 /* Determine if display requires 6 packets */
814 if (display_ep_found
) {
815 if (usb_match_id(interface
, vfd_proto_6p_list
))
818 dev_dbg(dev
, "%s: vfd_proto_6p: %d\n",
819 __func__
, vfd_proto_6p
);
822 driver
= kzalloc(sizeof(struct lirc_driver
), GFP_KERNEL
);
824 err("%s: kzalloc failed for lirc_driver", __func__
);
826 goto alloc_status_switch
;
828 rbuf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
830 err("%s: kmalloc failed for lirc_buffer", __func__
);
832 goto alloc_status_switch
;
834 if (lirc_buffer_init(rbuf
, BUF_CHUNK_SIZE
, BUF_SIZE
)) {
835 err("%s: lirc_buffer_init failed", __func__
);
837 goto alloc_status_switch
;
839 rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
841 err("%s: usb_alloc_urb failed for IR urb", __func__
);
843 goto alloc_status_switch
;
845 tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
847 err("%s: usb_alloc_urb failed for display urb",
850 goto alloc_status_switch
;
853 mutex_init(&context
->ctx_lock
);
854 context
->vfd_proto_6p
= vfd_proto_6p
;
856 strcpy(driver
->name
, MOD_NAME
);
858 driver
->code_length
= sizeof(int) * 8;
859 driver
->sample_rate
= 0;
860 driver
->features
= LIRC_CAN_REC_MODE2
;
861 driver
->data
= context
;
863 driver
->set_use_inc
= ir_open
;
864 driver
->set_use_dec
= ir_close
;
865 driver
->dev
= &interface
->dev
;
866 driver
->owner
= THIS_MODULE
;
868 mutex_lock(&context
->ctx_lock
);
870 context
->driver
= driver
;
871 /* start out in keyboard mode */
873 lirc_minor
= lirc_register_driver(driver
);
874 if (lirc_minor
< 0) {
875 err("%s: lirc_register_driver failed", __func__
);
879 dev_info(dev
, "Registered iMON driver "
880 "(lirc minor: %d)\n", lirc_minor
);
882 /* Needed while unregistering! */
883 driver
->minor
= lirc_minor
;
885 context
->usbdev
= usbdev
;
886 context
->dev_present
= 1;
887 context
->rx_endpoint
= rx_endpoint
;
888 context
->rx_urb
= rx_urb
;
891 * tx is used to send characters to lcd/vfd, associate RF
892 * remotes, set IR protocol, and maybe more...
894 context
->tx_endpoint
= tx_endpoint
;
895 context
->tx_urb
= tx_urb
;
897 if (display_ep_found
)
898 context
->display
= 1;
900 usb_fill_int_urb(context
->rx_urb
, context
->usbdev
,
901 usb_rcvintpipe(context
->usbdev
,
902 context
->rx_endpoint
->bEndpointAddress
),
903 context
->usb_rx_buf
, sizeof(context
->usb_rx_buf
),
904 usb_rx_callback
, context
,
905 context
->rx_endpoint
->bInterval
);
907 retval
= usb_submit_urb(context
->rx_urb
, GFP_KERNEL
);
910 err("%s: usb_submit_urb failed for intf0 (%d)",
912 mutex_unlock(&context
->ctx_lock
);
916 usb_set_intfdata(interface
, context
);
918 if (context
->display
&& ifnum
== 0) {
919 dev_dbg(dev
, "%s: Registering iMON display with sysfs\n",
922 if (usb_register_dev(interface
, &imon_class
)) {
923 /* Not a fatal error, so ignore */
924 dev_info(dev
, "%s: could not get a minor number for "
925 "display\n", __func__
);
929 dev_info(dev
, "iMON device (%04x:%04x, intf%d) on "
930 "usb<%d:%d> initialized\n", vendor
, product
, ifnum
,
931 usbdev
->bus
->busnum
, usbdev
->devnum
);
934 mutex_unlock(&context
->ctx_lock
);
937 switch (alloc_status
) {
939 usb_free_urb(tx_urb
);
941 usb_free_urb(rx_urb
);
944 lirc_buffer_free(rbuf
);
953 if (retval
!= -ENODEV
)
961 mutex_unlock(&driver_lock
);
967 * Callback function for USB core API: disconnect
969 static void imon_disconnect(struct usb_interface
*interface
)
971 struct imon_context
*context
;
974 /* prevent races with ir_open()/display_open() */
975 mutex_lock(&driver_lock
);
977 context
= usb_get_intfdata(interface
);
978 ifnum
= interface
->cur_altsetting
->desc
.bInterfaceNumber
;
980 mutex_lock(&context
->ctx_lock
);
982 usb_set_intfdata(interface
, NULL
);
984 /* Abort ongoing write */
985 if (atomic_read(&context
->tx
.busy
)) {
986 usb_kill_urb(context
->tx_urb
);
987 complete_all(&context
->tx
.finished
);
990 context
->dev_present
= 0;
991 usb_kill_urb(context
->rx_urb
);
992 if (context
->display
)
993 usb_deregister_dev(interface
, &imon_class
);
995 if (!context
->ir_isopen
&& !context
->dev_present
) {
996 deregister_from_lirc(context
);
997 mutex_unlock(&context
->ctx_lock
);
998 if (!context
->display_isopen
)
999 free_imon_context(context
);
1001 mutex_unlock(&context
->ctx_lock
);
1003 mutex_unlock(&driver_lock
);
1005 printk(KERN_INFO
"%s: iMON device (intf%d) disconnected\n",
1009 static int imon_suspend(struct usb_interface
*intf
, pm_message_t message
)
1011 struct imon_context
*context
= usb_get_intfdata(intf
);
1013 usb_kill_urb(context
->rx_urb
);
1018 static int imon_resume(struct usb_interface
*intf
)
1021 struct imon_context
*context
= usb_get_intfdata(intf
);
1023 usb_fill_int_urb(context
->rx_urb
, context
->usbdev
,
1024 usb_rcvintpipe(context
->usbdev
,
1025 context
->rx_endpoint
->bEndpointAddress
),
1026 context
->usb_rx_buf
, sizeof(context
->usb_rx_buf
),
1027 usb_rx_callback
, context
,
1028 context
->rx_endpoint
->bInterval
);
1030 rc
= usb_submit_urb(context
->rx_urb
, GFP_ATOMIC
);
1035 static int __init
imon_init(void)
1039 printk(KERN_INFO MOD_NAME
": " MOD_DESC
", v" MOD_VERSION
"\n");
1041 rc
= usb_register(&imon_driver
);
1043 err("%s: usb register failed(%d)", __func__
, rc
);
1050 static void __exit
imon_exit(void)
1052 usb_deregister(&imon_driver
);
1053 printk(KERN_INFO MOD_NAME
": module removed. Goodbye!\n");
1056 module_init(imon_init
);
1057 module_exit(imon_exit
);