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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/uaccess.h>
30 #include <linux/usb.h>
32 #include <media/lirc.h>
33 #include <media/lirc_dev.h>
36 #define MOD_AUTHOR "Venky Raju <dev@venky.ws>"
37 #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display"
38 #define MOD_NAME "lirc_imon"
39 #define MOD_VERSION "0.8"
41 #define DISPLAY_MINOR_BASE 144
42 #define DEVICE_NAME "lcd%d"
44 #define BUF_CHUNK_SIZE 4
47 #define BIT_DURATION 250 /* each bit received is 250us */
49 /*** P R O T O T Y P E S ***/
51 /* USB Callback prototypes */
52 static int imon_probe(struct usb_interface
*interface
,
53 const struct usb_device_id
*id
);
54 static void imon_disconnect(struct usb_interface
*interface
);
55 static void usb_rx_callback(struct urb
*urb
);
56 static void usb_tx_callback(struct urb
*urb
);
58 /* suspend/resume support */
59 static int imon_resume(struct usb_interface
*intf
);
60 static int imon_suspend(struct usb_interface
*intf
, pm_message_t message
);
62 /* Display file_operations function prototypes */
63 static int display_open(struct inode
*inode
, struct file
*file
);
64 static int display_close(struct inode
*inode
, struct file
*file
);
66 /* VFD write operation */
67 static ssize_t
vfd_write(struct file
*file
, const char __user
*buf
,
68 size_t n_bytes
, loff_t
*pos
);
70 /* LIRC driver function prototypes */
71 static int ir_open(void *data
);
72 static void ir_close(void *data
);
74 /*** G L O B A L S ***/
75 #define IMON_DATA_BUF_SZ 35
78 struct usb_device
*usbdev
;
79 /* Newer devices have two interfaces */
80 int display
; /* not all controllers do */
81 int display_isopen
; /* display port has been opened */
82 int ir_isopen
; /* IR port open */
83 int dev_present
; /* USB device presence */
84 struct mutex ctx_lock
; /* to lock this object */
85 wait_queue_head_t remove_ok
; /* For unexpected USB disconnects */
87 int vfd_proto_6p
; /* some VFD require a 6th packet */
89 struct lirc_driver
*driver
;
90 struct usb_endpoint_descriptor
*rx_endpoint
;
91 struct usb_endpoint_descriptor
*tx_endpoint
;
94 unsigned char usb_rx_buf
[8];
95 unsigned char usb_tx_buf
[8];
98 int count
; /* length of 0 or 1 sequence */
99 int prev_bit
; /* logic level of sequence */
100 int initial_space
; /* initial space flag */
104 unsigned char data_buf
[IMON_DATA_BUF_SZ
]; /* user data buffer */
105 struct completion finished
; /* wait for write to finish */
106 atomic_t busy
; /* write in progress */
107 int status
; /* status of tx completion */
111 static const struct file_operations display_fops
= {
112 .owner
= THIS_MODULE
,
113 .open
= &display_open
,
115 .release
= &display_close
,
116 .llseek
= noop_llseek
,
120 * USB Device ID for iMON USB Control Boards
122 * The Windows drivers contain 6 different inf files, more or less one for
123 * each new device until the 0x0034-0x0046 devices, which all use the same
124 * driver. Some of the devices in the 34-46 range haven't been definitively
125 * identified yet. Early devices have either a TriGem Computer, Inc. or a
126 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
127 * devices use the SoundGraph vendor ID (0x15c2).
129 static struct usb_device_id imon_usb_id_table
[] = {
130 /* TriGem iMON (IR only) -- TG_iMON.inf */
131 { USB_DEVICE(0x0aa8, 0x8001) },
133 /* SoundGraph iMON (IR only) -- sg_imon.inf */
134 { USB_DEVICE(0x04e8, 0xff30) },
136 /* SoundGraph iMON VFD (IR & VFD) -- iMON_VFD.inf */
137 { USB_DEVICE(0x0aa8, 0xffda) },
139 /* SoundGraph iMON SS (IR & VFD) -- iMON_SS.inf */
140 { USB_DEVICE(0x15c2, 0xffda) },
145 /* Some iMON VFD models requires a 6th packet for VFD writes */
146 static struct usb_device_id vfd_proto_6p_list
[] = {
147 { USB_DEVICE(0x15c2, 0xffda) },
151 /* Some iMON devices have no lcd/vfd, don't set one up */
152 static struct usb_device_id ir_only_list
[] = {
153 { USB_DEVICE(0x0aa8, 0x8001) },
154 { USB_DEVICE(0x04e8, 0xff30) },
158 /* USB Device data */
159 static struct usb_driver imon_driver
= {
162 .disconnect
= imon_disconnect
,
163 .suspend
= imon_suspend
,
164 .resume
= imon_resume
,
165 .id_table
= imon_usb_id_table
,
168 static struct usb_class_driver imon_class
= {
170 .fops
= &display_fops
,
171 .minor_base
= DISPLAY_MINOR_BASE
,
174 /* to prevent races between open() and disconnect(), probing, etc */
175 static DEFINE_MUTEX(driver_lock
);
179 /*** M O D U L E C O D E ***/
181 MODULE_AUTHOR(MOD_AUTHOR
);
182 MODULE_DESCRIPTION(MOD_DESC
);
183 MODULE_VERSION(MOD_VERSION
);
184 MODULE_LICENSE("GPL");
185 MODULE_DEVICE_TABLE(usb
, imon_usb_id_table
);
186 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
187 MODULE_PARM_DESC(debug
, "Debug messages: 0=no, 1=yes(default: no)");
189 static void free_imon_context(struct imon_context
*context
)
191 struct device
*dev
= context
->driver
->dev
;
193 usb_free_urb(context
->tx_urb
);
194 usb_free_urb(context
->rx_urb
);
195 lirc_buffer_free(context
->driver
->rbuf
);
196 kfree(context
->driver
->rbuf
);
197 kfree(context
->driver
);
200 dev_dbg(dev
, "%s: iMON context freed\n", __func__
);
203 static void deregister_from_lirc(struct imon_context
*context
)
206 int minor
= context
->driver
->minor
;
208 retval
= lirc_unregister_driver(minor
);
210 dev_err(&context
->usbdev
->dev
,
211 "unable to deregister from lirc(%d)", retval
);
213 dev_info(&context
->usbdev
->dev
,
214 "Deregistered iMON driver (minor:%d)\n", minor
);
219 * Called when the Display device (e.g. /dev/lcd0)
220 * is opened by the application.
222 static int display_open(struct inode
*inode
, struct file
*file
)
224 struct usb_interface
*interface
;
225 struct imon_context
*context
= NULL
;
229 /* prevent races with disconnect */
230 mutex_lock(&driver_lock
);
232 subminor
= iminor(inode
);
233 interface
= usb_find_interface(&imon_driver
, subminor
);
235 pr_err("%s: could not find interface for minor %d\n",
240 context
= usb_get_intfdata(interface
);
243 dev_err(&interface
->dev
, "no context found for minor %d\n",
249 mutex_lock(&context
->ctx_lock
);
251 if (!context
->display
) {
252 dev_err(&interface
->dev
,
253 "%s: display not supported by device\n", __func__
);
255 } else if (context
->display_isopen
) {
256 dev_err(&interface
->dev
,
257 "%s: display port is already open\n", __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 pr_err("%s: no context for device\n", __func__
);
288 mutex_lock(&context
->ctx_lock
);
290 if (!context
->display
) {
291 dev_err(&context
->usbdev
->dev
,
292 "%s: display not supported by device\n", __func__
);
294 } else if (!context
->display_isopen
) {
295 dev_err(&context
->usbdev
->dev
,
296 "%s: display is not open\n", __func__
);
299 context
->display_isopen
= 0;
300 dev_info(context
->driver
->dev
, "display port closed\n");
301 if (!context
->dev_present
&& !context
->ir_isopen
) {
303 * Device disconnected before close and IR port is not
304 * open. If IR port is open, context will be deleted by
307 mutex_unlock(&context
->ctx_lock
);
308 free_imon_context(context
);
313 mutex_unlock(&context
->ctx_lock
);
318 * Sends a packet to the device -- this function must be called
319 * with context->ctx_lock held.
321 static int send_packet(struct imon_context
*context
)
327 /* Check if we need to use control or interrupt urb */
328 pipe
= usb_sndintpipe(context
->usbdev
,
329 context
->tx_endpoint
->bEndpointAddress
);
330 interval
= context
->tx_endpoint
->bInterval
;
332 usb_fill_int_urb(context
->tx_urb
, context
->usbdev
, pipe
,
334 sizeof(context
->usb_tx_buf
),
335 usb_tx_callback
, context
, interval
);
337 context
->tx_urb
->actual_length
= 0;
339 init_completion(&context
->tx
.finished
);
340 atomic_set(&context
->tx
.busy
, 1);
342 retval
= usb_submit_urb(context
->tx_urb
, GFP_KERNEL
);
344 atomic_set(&context
->tx
.busy
, 0);
345 dev_err(&context
->usbdev
->dev
, "error submitting urb(%d)\n",
348 /* Wait for transmission to complete (or abort) */
349 mutex_unlock(&context
->ctx_lock
);
350 retval
= wait_for_completion_interruptible(
351 &context
->tx
.finished
);
353 dev_err(&context
->usbdev
->dev
,
354 "%s: task interrupted\n", __func__
);
355 mutex_lock(&context
->ctx_lock
);
357 retval
= context
->tx
.status
;
359 dev_err(&context
->usbdev
->dev
,
360 "packet tx failed (%d)\n", retval
);
367 * Writes data to the VFD. The iMON VFD is 2x16 characters
368 * and requires data in 5 consecutive USB interrupt packets,
369 * each packet but the last carrying 7 bytes.
371 * I don't know if the VFD board supports features such as
372 * scrolling, clearing rows, blanking, etc. so at
373 * the caller must provide a full screen of data. If fewer
374 * than 32 bytes are provided spaces will be appended to
375 * generate a full screen.
377 static ssize_t
vfd_write(struct file
*file
, const char __user
*buf
,
378 size_t n_bytes
, loff_t
*pos
)
384 struct imon_context
*context
;
385 const unsigned char vfd_packet6
[] = {
386 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
387 int *data_buf
= NULL
;
389 context
= file
->private_data
;
391 pr_err("%s: no context for device\n", __func__
);
395 mutex_lock(&context
->ctx_lock
);
397 if (!context
->dev_present
) {
398 dev_err(&context
->usbdev
->dev
,
399 "%s: no iMON device present\n", __func__
);
404 if (n_bytes
<= 0 || n_bytes
> IMON_DATA_BUF_SZ
- 3) {
405 dev_err(&context
->usbdev
->dev
,
406 "%s: invalid payload size\n", __func__
);
411 data_buf
= memdup_user(buf
, n_bytes
);
412 if (IS_ERR(data_buf
)) {
413 retval
= PTR_ERR(data_buf
);
418 memcpy(context
->tx
.data_buf
, data_buf
, n_bytes
);
420 /* Pad with spaces */
421 for (i
= n_bytes
; i
< IMON_DATA_BUF_SZ
- 3; ++i
)
422 context
->tx
.data_buf
[i
] = ' ';
424 for (i
= IMON_DATA_BUF_SZ
- 3; i
< IMON_DATA_BUF_SZ
; ++i
)
425 context
->tx
.data_buf
[i
] = 0xFF;
431 memcpy(context
->usb_tx_buf
, context
->tx
.data_buf
+ offset
, 7);
432 context
->usb_tx_buf
[7] = (unsigned char) seq
;
434 retval
= send_packet(context
);
436 dev_err(&context
->usbdev
->dev
,
437 "send packet failed for packet #%d\n",
445 } while (offset
< IMON_DATA_BUF_SZ
);
447 if (context
->vfd_proto_6p
) {
449 memcpy(context
->usb_tx_buf
, &vfd_packet6
, sizeof(vfd_packet6
));
450 context
->usb_tx_buf
[7] = (unsigned char) seq
;
451 retval
= send_packet(context
);
453 dev_err(&context
->usbdev
->dev
,
454 "send packet failed for packet #%d\n",
459 mutex_unlock(&context
->ctx_lock
);
462 return (!retval
) ? n_bytes
: retval
;
466 * Callback function for USB core API: transmit data
468 static void usb_tx_callback(struct urb
*urb
)
470 struct imon_context
*context
;
474 context
= (struct imon_context
*)urb
->context
;
478 context
->tx
.status
= urb
->status
;
480 /* notify waiters that write has finished */
481 atomic_set(&context
->tx
.busy
, 0);
482 complete(&context
->tx
.finished
);
486 * Called by lirc_dev when the application opens /dev/lirc
488 static int ir_open(void *data
)
490 struct imon_context
*context
;
492 /* prevent races with disconnect */
493 mutex_lock(&driver_lock
);
497 /* initial IR protocol decode variables */
498 context
->rx
.count
= 0;
499 context
->rx
.initial_space
= 1;
500 context
->rx
.prev_bit
= 0;
502 context
->ir_isopen
= 1;
503 dev_info(context
->driver
->dev
, "IR port opened\n");
505 mutex_unlock(&driver_lock
);
510 * Called by lirc_dev when the application closes /dev/lirc
512 static void ir_close(void *data
)
514 struct imon_context
*context
;
518 pr_err("%s: no context for device\n", __func__
);
522 mutex_lock(&context
->ctx_lock
);
524 context
->ir_isopen
= 0;
525 dev_info(context
->driver
->dev
, "IR port closed\n");
527 if (!context
->dev_present
) {
529 * Device disconnected while IR port was still open. Driver
530 * was not deregistered at disconnect time, so do it now.
532 deregister_from_lirc(context
);
534 if (!context
->display_isopen
) {
535 mutex_unlock(&context
->ctx_lock
);
536 free_imon_context(context
);
540 * If display port is open, context will be deleted by
545 mutex_unlock(&context
->ctx_lock
);
549 * Convert bit count to time duration (in us) and submit
550 * the value to lirc_dev.
552 static void submit_data(struct imon_context
*context
)
554 unsigned char buf
[4];
555 int value
= context
->rx
.count
;
558 dev_dbg(context
->driver
->dev
, "submitting data to LIRC\n");
560 value
*= BIT_DURATION
;
562 if (context
->rx
.prev_bit
)
565 for (i
= 0; i
< 4; ++i
)
566 buf
[i
] = value
>>(i
*8);
568 lirc_buffer_write(context
->driver
->rbuf
, buf
);
569 wake_up(&context
->driver
->rbuf
->wait_poll
);
573 * Process the incoming packet
575 static void imon_incoming_packet(struct imon_context
*context
,
576 struct urb
*urb
, int intf
)
578 int len
= urb
->actual_length
;
579 unsigned char *buf
= urb
->transfer_buffer
;
580 struct device
*dev
= context
->driver
->dev
;
585 * just bail out if no listening IR client
587 if (!context
->ir_isopen
)
591 dev_warn(dev
, "imon %s: invalid incoming packet size (len = %d, intf%d)\n",
592 __func__
, len
, intf
);
597 dev_info(dev
, "raw packet: %*ph\n", len
, buf
);
599 * Translate received data to pulse and space lengths.
600 * Received data is active low, i.e. pulses are 0 and
603 * My original algorithm was essentially similar to
604 * Changwoo Ryu's with the exception that he switched
605 * the incoming bits to active high and also fed an
606 * initial space to LIRC at the start of a new sequence
607 * if the previous bit was a pulse.
609 * I've decided to adopt his algorithm.
612 if (buf
[7] == 1 && context
->rx
.initial_space
) {
613 /* LIRC requires a leading space */
614 context
->rx
.prev_bit
= 0;
615 context
->rx
.count
= 4;
616 submit_data(context
);
617 context
->rx
.count
= 0;
620 for (octet
= 0; octet
< 5; ++octet
) {
622 for (bit
= 0; bit
< 8; ++bit
) {
623 int curr_bit
= !(buf
[octet
] & mask
);
625 if (curr_bit
!= context
->rx
.prev_bit
) {
626 if (context
->rx
.count
) {
627 submit_data(context
);
628 context
->rx
.count
= 0;
630 context
->rx
.prev_bit
= curr_bit
;
638 if (context
->rx
.count
) {
639 submit_data(context
);
640 context
->rx
.count
= 0;
642 context
->rx
.initial_space
= context
->rx
.prev_bit
;
647 * Callback function for USB core API: receive data
649 static void usb_rx_callback(struct urb
*urb
)
651 struct imon_context
*context
;
657 context
= (struct imon_context
*)urb
->context
;
661 switch (urb
->status
) {
662 case -ENOENT
: /* usbcore unlink successful! */
666 imon_incoming_packet(context
, urb
, intfnum
);
670 dev_warn(context
->driver
->dev
, "imon %s: status(%d): ignored\n",
671 __func__
, urb
->status
);
675 usb_submit_urb(context
->rx_urb
, GFP_ATOMIC
);
679 * Callback function for USB core API: Probe
681 static int imon_probe(struct usb_interface
*interface
,
682 const struct usb_device_id
*id
)
684 struct usb_device
*usbdev
= NULL
;
685 struct usb_host_interface
*iface_desc
= NULL
;
686 struct usb_endpoint_descriptor
*rx_endpoint
= NULL
;
687 struct usb_endpoint_descriptor
*tx_endpoint
= NULL
;
688 struct urb
*rx_urb
= NULL
;
689 struct urb
*tx_urb
= NULL
;
690 struct lirc_driver
*driver
= NULL
;
691 struct lirc_buffer
*rbuf
= NULL
;
692 struct device
*dev
= &interface
->dev
;
696 int retval
= -ENOMEM
;
697 int display_ep_found
= 0;
699 int vfd_proto_6p
= 0;
700 struct imon_context
*context
= NULL
;
704 /* prevent races probing devices w/multiple interfaces */
705 mutex_lock(&driver_lock
);
707 context
= kzalloc(sizeof(struct imon_context
), GFP_KERNEL
);
712 * Try to auto-detect the type of display if the user hasn't set
713 * it by hand via the display_type modparam. Default is VFD.
715 if (usb_match_id(interface
, ir_only_list
))
716 context
->display
= 0;
718 context
->display
= 1;
720 usbdev
= usb_get_dev(interface_to_usbdev(interface
));
721 iface_desc
= interface
->cur_altsetting
;
722 num_endpts
= iface_desc
->desc
.bNumEndpoints
;
723 ifnum
= iface_desc
->desc
.bInterfaceNumber
;
724 vendor
= le16_to_cpu(usbdev
->descriptor
.idVendor
);
725 product
= le16_to_cpu(usbdev
->descriptor
.idProduct
);
727 dev_dbg(dev
, "%s: found iMON device (%04x:%04x, intf%d)\n",
728 __func__
, vendor
, product
, ifnum
);
731 * Scan the endpoint list and set:
732 * first input endpoint = IR endpoint
733 * first output endpoint = display endpoint
735 for (i
= 0; i
< num_endpts
&& !(ir_ep_found
&& display_ep_found
); ++i
) {
736 struct usb_endpoint_descriptor
*ep
;
740 ep
= &iface_desc
->endpoint
[i
].desc
;
741 ep_dir
= ep
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
;
742 ep_type
= usb_endpoint_type(ep
);
745 ep_dir
== USB_DIR_IN
&&
746 ep_type
== USB_ENDPOINT_XFER_INT
) {
750 dev_dbg(dev
, "%s: found IR endpoint\n", __func__
);
752 } else if (!display_ep_found
&& ep_dir
== USB_DIR_OUT
&&
753 ep_type
== USB_ENDPOINT_XFER_INT
) {
755 display_ep_found
= 1;
756 dev_dbg(dev
, "%s: found display endpoint\n", __func__
);
761 * Some iMON receivers have no display. Unfortunately, it seems
762 * that SoundGraph recycles device IDs between devices both with
765 if (context
->display
== 0) {
766 display_ep_found
= 0;
767 dev_dbg(dev
, "%s: device has no display\n", __func__
);
770 /* Input endpoint is mandatory */
772 dev_err(dev
, "%s: no valid input (IR) endpoint found.\n",
778 /* Determine if display requires 6 packets */
779 if (display_ep_found
) {
780 if (usb_match_id(interface
, vfd_proto_6p_list
))
783 dev_dbg(dev
, "%s: vfd_proto_6p: %d\n",
784 __func__
, vfd_proto_6p
);
787 driver
= kzalloc(sizeof(struct lirc_driver
), GFP_KERNEL
);
791 rbuf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
795 if (lirc_buffer_init(rbuf
, BUF_CHUNK_SIZE
, BUF_SIZE
)) {
796 dev_err(dev
, "%s: lirc_buffer_init failed\n", __func__
);
799 rx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
801 dev_err(dev
, "%s: usb_alloc_urb failed for IR urb\n", __func__
);
804 tx_urb
= usb_alloc_urb(0, GFP_KERNEL
);
806 dev_err(dev
, "%s: usb_alloc_urb failed for display urb\n",
811 mutex_init(&context
->ctx_lock
);
812 context
->vfd_proto_6p
= vfd_proto_6p
;
814 strcpy(driver
->name
, MOD_NAME
);
816 driver
->code_length
= BUF_CHUNK_SIZE
* 8;
817 driver
->sample_rate
= 0;
818 driver
->features
= LIRC_CAN_REC_MODE2
;
819 driver
->data
= context
;
821 driver
->set_use_inc
= ir_open
;
822 driver
->set_use_dec
= ir_close
;
823 driver
->dev
= &interface
->dev
;
824 driver
->owner
= THIS_MODULE
;
826 mutex_lock(&context
->ctx_lock
);
828 context
->driver
= driver
;
829 /* start out in keyboard mode */
831 lirc_minor
= lirc_register_driver(driver
);
832 if (lirc_minor
< 0) {
833 dev_err(dev
, "%s: lirc_register_driver failed\n", __func__
);
837 dev_info(dev
, "Registered iMON driver (lirc minor: %d)\n",
840 /* Needed while unregistering! */
841 driver
->minor
= lirc_minor
;
843 context
->usbdev
= usbdev
;
844 context
->dev_present
= 1;
845 context
->rx_endpoint
= rx_endpoint
;
846 context
->rx_urb
= rx_urb
;
849 * tx is used to send characters to lcd/vfd, associate RF
850 * remotes, set IR protocol, and maybe more...
852 context
->tx_endpoint
= tx_endpoint
;
853 context
->tx_urb
= tx_urb
;
855 if (display_ep_found
)
856 context
->display
= 1;
858 usb_fill_int_urb(context
->rx_urb
, context
->usbdev
,
859 usb_rcvintpipe(context
->usbdev
,
860 context
->rx_endpoint
->bEndpointAddress
),
861 context
->usb_rx_buf
, sizeof(context
->usb_rx_buf
),
862 usb_rx_callback
, context
,
863 context
->rx_endpoint
->bInterval
);
865 retval
= usb_submit_urb(context
->rx_urb
, GFP_KERNEL
);
867 dev_err(dev
, "usb_submit_urb failed for intf0 (%d)\n", retval
);
868 goto unregister_lirc
;
871 usb_set_intfdata(interface
, context
);
873 if (context
->display
&& ifnum
== 0) {
874 dev_dbg(dev
, "%s: Registering iMON display with sysfs\n",
877 if (usb_register_dev(interface
, &imon_class
)) {
878 /* Not a fatal error, so ignore */
879 dev_info(dev
, "%s: could not get a minor number for display\n",
884 dev_info(dev
, "iMON device (%04x:%04x, intf%d) on usb<%d:%d> initialized\n",
885 vendor
, product
, ifnum
, usbdev
->bus
->busnum
, usbdev
->devnum
);
887 /* Everything went fine. Just unlock and return retval (with is 0) */
888 mutex_unlock(&context
->ctx_lock
);
892 lirc_unregister_driver(driver
->minor
);
895 mutex_unlock(&context
->ctx_lock
);
896 usb_free_urb(tx_urb
);
899 usb_free_urb(rx_urb
);
902 lirc_buffer_free(rbuf
);
914 mutex_unlock(&driver_lock
);
920 * Callback function for USB core API: disconnect
922 static void imon_disconnect(struct usb_interface
*interface
)
924 struct imon_context
*context
;
927 /* prevent races with ir_open()/display_open() */
928 mutex_lock(&driver_lock
);
930 context
= usb_get_intfdata(interface
);
931 ifnum
= interface
->cur_altsetting
->desc
.bInterfaceNumber
;
933 mutex_lock(&context
->ctx_lock
);
935 usb_set_intfdata(interface
, NULL
);
937 /* Abort ongoing write */
938 if (atomic_read(&context
->tx
.busy
)) {
939 usb_kill_urb(context
->tx_urb
);
940 complete_all(&context
->tx
.finished
);
943 context
->dev_present
= 0;
944 usb_kill_urb(context
->rx_urb
);
945 if (context
->display
)
946 usb_deregister_dev(interface
, &imon_class
);
948 if (!context
->ir_isopen
&& !context
->dev_present
) {
949 deregister_from_lirc(context
);
950 mutex_unlock(&context
->ctx_lock
);
951 if (!context
->display_isopen
)
952 free_imon_context(context
);
954 mutex_unlock(&context
->ctx_lock
);
956 mutex_unlock(&driver_lock
);
958 dev_info(&interface
->dev
, "%s: iMON device (intf%d) disconnected\n",
962 static int imon_suspend(struct usb_interface
*intf
, pm_message_t message
)
964 struct imon_context
*context
= usb_get_intfdata(intf
);
966 usb_kill_urb(context
->rx_urb
);
971 static int imon_resume(struct usb_interface
*intf
)
973 struct imon_context
*context
= usb_get_intfdata(intf
);
975 usb_fill_int_urb(context
->rx_urb
, context
->usbdev
,
976 usb_rcvintpipe(context
->usbdev
,
977 context
->rx_endpoint
->bEndpointAddress
),
978 context
->usb_rx_buf
, sizeof(context
->usb_rx_buf
),
979 usb_rx_callback
, context
,
980 context
->rx_endpoint
->bInterval
);
982 return usb_submit_urb(context
->rx_urb
, GFP_ATOMIC
);
985 module_usb_driver(imon_driver
);