cgroup: superblock can't be released with active dentries
[firefly-linux-kernel-4.4.55.git] / drivers / staging / media / lirc / lirc_imon.c
1 /*
2  *   lirc_imon.c:  LIRC/VFD/LCD driver for SoundGraph iMON IR/VFD/LCD
3  *                 including the iMON PAD model
4  *
5  *   Copyright(C) 2004  Venky Raju(dev@venky.ws)
6  *   Copyright(C) 2009  Jarod Wilson <jarod@wilsonet.com>
7  *
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.
12  *
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.
17  *
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.
21  */
22
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>
30
31 #include <media/lirc.h>
32 #include <media/lirc_dev.h>
33
34
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"
39
40 #define DISPLAY_MINOR_BASE      144
41 #define DEVICE_NAME     "lcd%d"
42
43 #define BUF_CHUNK_SIZE  4
44 #define BUF_SIZE        128
45
46 #define BIT_DURATION    250     /* each bit received is 250us */
47
48 /*** P R O T O T Y P E S ***/
49
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);
56
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);
60
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);
64
65 /* VFD write operation */
66 static ssize_t vfd_write(struct file *file, const char __user *buf,
67                          size_t n_bytes, loff_t *pos);
68
69 /* LIRC driver function prototypes */
70 static int ir_open(void *data);
71 static void ir_close(void *data);
72
73 /* Driver init/exit prototypes */
74 static int __init imon_init(void);
75 static void __exit imon_exit(void);
76
77 /*** G L O B A L S ***/
78 #define IMON_DATA_BUF_SZ        35
79
80 struct imon_context {
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 */
89
90         int vfd_proto_6p;               /* some VFD require a 6th packet */
91
92         struct lirc_driver *driver;
93         struct usb_endpoint_descriptor *rx_endpoint;
94         struct usb_endpoint_descriptor *tx_endpoint;
95         struct urb *rx_urb;
96         struct urb *tx_urb;
97         unsigned char usb_rx_buf[8];
98         unsigned char usb_tx_buf[8];
99
100         struct rx_data {
101                 int count;              /* length of 0 or 1 sequence */
102                 int prev_bit;           /* logic level of sequence */
103                 int initial_space;      /* initial space flag */
104         } rx;
105
106         struct tx_t {
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 */
111         } tx;
112 };
113
114 static const struct file_operations display_fops = {
115         .owner          = THIS_MODULE,
116         .open           = &display_open,
117         .write          = &vfd_write,
118         .release        = &display_close,
119         .llseek         = noop_llseek,
120 };
121
122 /*
123  * USB Device ID for iMON USB Control Boards
124  *
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).
131  */
132 static struct usb_device_id imon_usb_id_table[] = {
133         /* TriGem iMON (IR only) -- TG_iMON.inf */
134         { USB_DEVICE(0x0aa8, 0x8001) },
135
136         /* SoundGraph iMON (IR only) -- sg_imon.inf */
137         { USB_DEVICE(0x04e8, 0xff30) },
138
139         /* SoundGraph iMON VFD (IR & VFD) -- iMON_VFD.inf */
140         { USB_DEVICE(0x0aa8, 0xffda) },
141
142         /* SoundGraph iMON SS (IR & VFD) -- iMON_SS.inf */
143         { USB_DEVICE(0x15c2, 0xffda) },
144
145         {}
146 };
147
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) },
151         {}
152 };
153
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) },
158         {}
159 };
160
161 /* USB Device data */
162 static struct usb_driver imon_driver = {
163         .name           = MOD_NAME,
164         .probe          = imon_probe,
165         .disconnect     = imon_disconnect,
166         .suspend        = imon_suspend,
167         .resume         = imon_resume,
168         .id_table       = imon_usb_id_table,
169 };
170
171 static struct usb_class_driver imon_class = {
172         .name           = DEVICE_NAME,
173         .fops           = &display_fops,
174         .minor_base     = DISPLAY_MINOR_BASE,
175 };
176
177 /* to prevent races between open() and disconnect(), probing, etc */
178 static DEFINE_MUTEX(driver_lock);
179
180 static int debug;
181
182 /***  M O D U L E   C O D E ***/
183
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)");
191
192 static void free_imon_context(struct imon_context *context)
193 {
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);
200         kfree(context);
201
202         dev_dbg(dev, "%s: iMON context freed\n", __func__);
203 }
204
205 static void deregister_from_lirc(struct imon_context *context)
206 {
207         int retval;
208         int minor = context->driver->minor;
209
210         retval = lirc_unregister_driver(minor);
211         if (retval)
212                 printk(KERN_ERR KBUILD_MODNAME
213                        ": %s: unable to deregister from lirc(%d)",
214                        __func__, retval);
215         else
216                 printk(KERN_INFO MOD_NAME ": Deregistered iMON driver "
217                        "(minor:%d)\n", minor);
218
219 }
220
221 /**
222  * Called when the Display device (e.g. /dev/lcd0)
223  * is opened by the application.
224  */
225 static int display_open(struct inode *inode, struct file *file)
226 {
227         struct usb_interface *interface;
228         struct imon_context *context = NULL;
229         int subminor;
230         int retval = 0;
231
232         /* prevent races with disconnect */
233         mutex_lock(&driver_lock);
234
235         subminor = iminor(inode);
236         interface = usb_find_interface(&imon_driver, subminor);
237         if (!interface) {
238                 printk(KERN_ERR KBUILD_MODNAME
239                        ": %s: could not find interface for minor %d\n",
240                        __func__, subminor);
241                 retval = -ENODEV;
242                 goto exit;
243         }
244         context = usb_get_intfdata(interface);
245
246         if (!context) {
247                 dev_err(&interface->dev,
248                         "%s: no context found for minor %d\n",
249                         __func__, subminor);
250                 retval = -ENODEV;
251                 goto exit;
252         }
253
254         mutex_lock(&context->ctx_lock);
255
256         if (!context->display) {
257                 dev_err(&interface->dev,
258                         "%s: display not supported by device\n", __func__);
259                 retval = -ENODEV;
260         } else if (context->display_isopen) {
261                 dev_err(&interface->dev,
262                         "%s: display port is already open\n", __func__);
263                 retval = -EBUSY;
264         } else {
265                 context->display_isopen = 1;
266                 file->private_data = context;
267                 dev_info(context->driver->dev, "display port opened\n");
268         }
269
270         mutex_unlock(&context->ctx_lock);
271
272 exit:
273         mutex_unlock(&driver_lock);
274         return retval;
275 }
276
277 /**
278  * Called when the display device (e.g. /dev/lcd0)
279  * is closed by the application.
280  */
281 static int display_close(struct inode *inode, struct file *file)
282 {
283         struct imon_context *context = NULL;
284         int retval = 0;
285
286         context = file->private_data;
287
288         if (!context) {
289                 printk(KERN_ERR KBUILD_MODNAME
290                        "%s: no context for device\n", __func__);
291                 return -ENODEV;
292         }
293
294         mutex_lock(&context->ctx_lock);
295
296         if (!context->display) {
297                 dev_err(&context->usbdev->dev,
298                         "%s: display not supported by device\n", __func__);
299                 retval = -ENODEV;
300         } else if (!context->display_isopen) {
301                 dev_err(&context->usbdev->dev,
302                         "%s: display is not open\n", __func__);
303                 retval = -EIO;
304         } else {
305                 context->display_isopen = 0;
306                 dev_info(context->driver->dev, "display port closed\n");
307                 if (!context->dev_present && !context->ir_isopen) {
308                         /*
309                          * Device disconnected before close and IR port is not
310                          * open. If IR port is open, context will be deleted by
311                          * ir_close.
312                          */
313                         mutex_unlock(&context->ctx_lock);
314                         free_imon_context(context);
315                         return retval;
316                 }
317         }
318
319         mutex_unlock(&context->ctx_lock);
320         return retval;
321 }
322
323 /**
324  * Sends a packet to the device -- this function must be called
325  * with context->ctx_lock held.
326  */
327 static int send_packet(struct imon_context *context)
328 {
329         unsigned int pipe;
330         int interval = 0;
331         int retval = 0;
332
333         /* Check if we need to use control or interrupt urb */
334         pipe = usb_sndintpipe(context->usbdev,
335                               context->tx_endpoint->bEndpointAddress);
336         interval = context->tx_endpoint->bInterval;
337
338         usb_fill_int_urb(context->tx_urb, context->usbdev, pipe,
339                          context->usb_tx_buf,
340                          sizeof(context->usb_tx_buf),
341                          usb_tx_callback, context, interval);
342
343         context->tx_urb->actual_length = 0;
344
345         init_completion(&context->tx.finished);
346         atomic_set(&(context->tx.busy), 1);
347
348         retval = usb_submit_urb(context->tx_urb, GFP_KERNEL);
349         if (retval) {
350                 atomic_set(&(context->tx.busy), 0);
351                 dev_err(&context->usbdev->dev,
352                         "%s: error submitting urb(%d)\n", __func__, retval);
353         } else {
354                 /* Wait for transmission to complete (or abort) */
355                 mutex_unlock(&context->ctx_lock);
356                 retval = wait_for_completion_interruptible(
357                                 &context->tx.finished);
358                 if (retval)
359                         dev_err(&context->usbdev->dev,
360                                 "%s: task interrupted\n", __func__);
361                 mutex_lock(&context->ctx_lock);
362
363                 retval = context->tx.status;
364                 if (retval)
365                         dev_err(&context->usbdev->dev,
366                                 "%s: packet tx failed (%d)\n",
367                                 __func__, retval);
368         }
369
370         return retval;
371 }
372
373 /**
374  * Writes data to the VFD.  The iMON VFD is 2x16 characters
375  * and requires data in 5 consecutive USB interrupt packets,
376  * each packet but the last carrying 7 bytes.
377  *
378  * I don't know if the VFD board supports features such as
379  * scrolling, clearing rows, blanking, etc. so at
380  * the caller must provide a full screen of data.  If fewer
381  * than 32 bytes are provided spaces will be appended to
382  * generate a full screen.
383  */
384 static ssize_t vfd_write(struct file *file, const char __user *buf,
385                          size_t n_bytes, loff_t *pos)
386 {
387         int i;
388         int offset;
389         int seq;
390         int retval = 0;
391         struct imon_context *context;
392         const unsigned char vfd_packet6[] = {
393                 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
394         int *data_buf = NULL;
395
396         context = file->private_data;
397         if (!context) {
398                 printk(KERN_ERR KBUILD_MODNAME
399                        "%s: no context for device\n", __func__);
400                 return -ENODEV;
401         }
402
403         mutex_lock(&context->ctx_lock);
404
405         if (!context->dev_present) {
406                 dev_err(&context->usbdev->dev,
407                         "%s: no iMON device present\n", __func__);
408                 retval = -ENODEV;
409                 goto exit;
410         }
411
412         if (n_bytes <= 0 || n_bytes > IMON_DATA_BUF_SZ - 3) {
413                 dev_err(&context->usbdev->dev,
414                         "%s: invalid payload size\n", __func__);
415                 retval = -EINVAL;
416                 goto exit;
417         }
418
419         data_buf = memdup_user(buf, n_bytes);
420         if (IS_ERR(data_buf)) {
421                 retval = PTR_ERR(data_buf);
422                 goto exit;
423         }
424
425         memcpy(context->tx.data_buf, data_buf, n_bytes);
426
427         /* Pad with spaces */
428         for (i = n_bytes; i < IMON_DATA_BUF_SZ - 3; ++i)
429                 context->tx.data_buf[i] = ' ';
430
431         for (i = IMON_DATA_BUF_SZ - 3; i < IMON_DATA_BUF_SZ; ++i)
432                 context->tx.data_buf[i] = 0xFF;
433
434         offset = 0;
435         seq = 0;
436
437         do {
438                 memcpy(context->usb_tx_buf, context->tx.data_buf + offset, 7);
439                 context->usb_tx_buf[7] = (unsigned char) seq;
440
441                 retval = send_packet(context);
442                 if (retval) {
443                         dev_err(&context->usbdev->dev,
444                                 "%s: send packet failed for packet #%d\n",
445                                 __func__, seq/2);
446                         goto exit;
447                 } else {
448                         seq += 2;
449                         offset += 7;
450                 }
451
452         } while (offset < IMON_DATA_BUF_SZ);
453
454         if (context->vfd_proto_6p) {
455                 /* Send packet #6 */
456                 memcpy(context->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
457                 context->usb_tx_buf[7] = (unsigned char) seq;
458                 retval = send_packet(context);
459                 if (retval)
460                         dev_err(&context->usbdev->dev,
461                                 "%s: send packet failed for packet #%d\n",
462                                         __func__, seq/2);
463         }
464
465 exit:
466         mutex_unlock(&context->ctx_lock);
467         kfree(data_buf);
468
469         return (!retval) ? n_bytes : retval;
470 }
471
472 /**
473  * Callback function for USB core API: transmit data
474  */
475 static void usb_tx_callback(struct urb *urb)
476 {
477         struct imon_context *context;
478
479         if (!urb)
480                 return;
481         context = (struct imon_context *)urb->context;
482         if (!context)
483                 return;
484
485         context->tx.status = urb->status;
486
487         /* notify waiters that write has finished */
488         atomic_set(&context->tx.busy, 0);
489         complete(&context->tx.finished);
490
491         return;
492 }
493
494 /**
495  * Called by lirc_dev when the application opens /dev/lirc
496  */
497 static int ir_open(void *data)
498 {
499         int retval = 0;
500         struct imon_context *context;
501
502         /* prevent races with disconnect */
503         mutex_lock(&driver_lock);
504
505         context = (struct imon_context *)data;
506
507         /* initial IR protocol decode variables */
508         context->rx.count = 0;
509         context->rx.initial_space = 1;
510         context->rx.prev_bit = 0;
511
512         context->ir_isopen = 1;
513         dev_info(context->driver->dev, "IR port opened\n");
514
515         mutex_unlock(&driver_lock);
516         return retval;
517 }
518
519 /**
520  * Called by lirc_dev when the application closes /dev/lirc
521  */
522 static void ir_close(void *data)
523 {
524         struct imon_context *context;
525
526         context = (struct imon_context *)data;
527         if (!context) {
528                 printk(KERN_ERR KBUILD_MODNAME
529                        "%s: no context for device\n", __func__);
530                 return;
531         }
532
533         mutex_lock(&context->ctx_lock);
534
535         context->ir_isopen = 0;
536         dev_info(context->driver->dev, "IR port closed\n");
537
538         if (!context->dev_present) {
539                 /*
540                  * Device disconnected while IR port was still open. Driver
541                  * was not deregistered at disconnect time, so do it now.
542                  */
543                 deregister_from_lirc(context);
544
545                 if (!context->display_isopen) {
546                         mutex_unlock(&context->ctx_lock);
547                         free_imon_context(context);
548                         return;
549                 }
550                 /*
551                  * If display port is open, context will be deleted by
552                  * display_close
553                  */
554         }
555
556         mutex_unlock(&context->ctx_lock);
557         return;
558 }
559
560 /**
561  * Convert bit count to time duration (in us) and submit
562  * the value to lirc_dev.
563  */
564 static void submit_data(struct imon_context *context)
565 {
566         unsigned char buf[4];
567         int value = context->rx.count;
568         int i;
569
570         dev_dbg(context->driver->dev, "submitting data to LIRC\n");
571
572         value *= BIT_DURATION;
573         value &= PULSE_MASK;
574         if (context->rx.prev_bit)
575                 value |= PULSE_BIT;
576
577         for (i = 0; i < 4; ++i)
578                 buf[i] = value>>(i*8);
579
580         lirc_buffer_write(context->driver->rbuf, buf);
581         wake_up(&context->driver->rbuf->wait_poll);
582         return;
583 }
584
585 static inline int tv2int(const struct timeval *a, const struct timeval *b)
586 {
587         int usecs = 0;
588         int sec   = 0;
589
590         if (b->tv_usec > a->tv_usec) {
591                 usecs = 1000000;
592                 sec--;
593         }
594
595         usecs += a->tv_usec - b->tv_usec;
596
597         sec += a->tv_sec - b->tv_sec;
598         sec *= 1000;
599         usecs /= 1000;
600         sec += usecs;
601
602         if (sec < 0)
603                 sec = 1000;
604
605         return sec;
606 }
607
608 /**
609  * Process the incoming packet
610  */
611 static void imon_incoming_packet(struct imon_context *context,
612                                  struct urb *urb, int intf)
613 {
614         int len = urb->actual_length;
615         unsigned char *buf = urb->transfer_buffer;
616         struct device *dev = context->driver->dev;
617         int octet, bit;
618         unsigned char mask;
619         int i;
620
621         /*
622          * just bail out if no listening IR client
623          */
624         if (!context->ir_isopen)
625                 return;
626
627         if (len != 8) {
628                 dev_warn(dev, "imon %s: invalid incoming packet "
629                          "size (len = %d, intf%d)\n", __func__, len, intf);
630                 return;
631         }
632
633         if (debug) {
634                 printk(KERN_INFO "raw packet: ");
635                 for (i = 0; i < len; ++i)
636                         printk("%02x ", buf[i]);
637                 printk("\n");
638         }
639
640         /*
641          * Translate received data to pulse and space lengths.
642          * Received data is active low, i.e. pulses are 0 and
643          * spaces are 1.
644          *
645          * My original algorithm was essentially similar to
646          * Changwoo Ryu's with the exception that he switched
647          * the incoming bits to active high and also fed an
648          * initial space to LIRC at the start of a new sequence
649          * if the previous bit was a pulse.
650          *
651          * I've decided to adopt his algorithm.
652          */
653
654         if (buf[7] == 1 && context->rx.initial_space) {
655                 /* LIRC requires a leading space */
656                 context->rx.prev_bit = 0;
657                 context->rx.count = 4;
658                 submit_data(context);
659                 context->rx.count = 0;
660         }
661
662         for (octet = 0; octet < 5; ++octet) {
663                 mask = 0x80;
664                 for (bit = 0; bit < 8; ++bit) {
665                         int curr_bit = !(buf[octet] & mask);
666                         if (curr_bit != context->rx.prev_bit) {
667                                 if (context->rx.count) {
668                                         submit_data(context);
669                                         context->rx.count = 0;
670                                 }
671                                 context->rx.prev_bit = curr_bit;
672                         }
673                         ++context->rx.count;
674                         mask >>= 1;
675                 }
676         }
677
678         if (buf[7] == 10) {
679                 if (context->rx.count) {
680                         submit_data(context);
681                         context->rx.count = 0;
682                 }
683                 context->rx.initial_space = context->rx.prev_bit;
684         }
685 }
686
687 /**
688  * Callback function for USB core API: receive data
689  */
690 static void usb_rx_callback(struct urb *urb)
691 {
692         struct imon_context *context;
693         int intfnum = 0;
694
695         if (!urb)
696                 return;
697
698         context = (struct imon_context *)urb->context;
699         if (!context)
700                 return;
701
702         switch (urb->status) {
703         case -ENOENT:           /* usbcore unlink successful! */
704                 return;
705
706         case 0:
707                 imon_incoming_packet(context, urb, intfnum);
708                 break;
709
710         default:
711                 dev_warn(context->driver->dev, "imon %s: status(%d): ignored\n",
712                          __func__, urb->status);
713                 break;
714         }
715
716         usb_submit_urb(context->rx_urb, GFP_ATOMIC);
717
718         return;
719 }
720
721 /**
722  * Callback function for USB core API: Probe
723  */
724 static int imon_probe(struct usb_interface *interface,
725                       const struct usb_device_id *id)
726 {
727         struct usb_device *usbdev = NULL;
728         struct usb_host_interface *iface_desc = NULL;
729         struct usb_endpoint_descriptor *rx_endpoint = NULL;
730         struct usb_endpoint_descriptor *tx_endpoint = NULL;
731         struct urb *rx_urb = NULL;
732         struct urb *tx_urb = NULL;
733         struct lirc_driver *driver = NULL;
734         struct lirc_buffer *rbuf = NULL;
735         struct device *dev = &interface->dev;
736         int ifnum;
737         int lirc_minor = 0;
738         int num_endpts;
739         int retval = 0;
740         int display_ep_found = 0;
741         int ir_ep_found = 0;
742         int alloc_status = 0;
743         int vfd_proto_6p = 0;
744         struct imon_context *context = NULL;
745         int i;
746         u16 vendor, product;
747
748         /* prevent races probing devices w/multiple interfaces */
749         mutex_lock(&driver_lock);
750
751         context = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
752         if (!context) {
753                 dev_err(dev, "%s: kzalloc failed for context\n", __func__);
754                 alloc_status = 1;
755                 goto alloc_status_switch;
756         }
757
758         /*
759          * Try to auto-detect the type of display if the user hasn't set
760          * it by hand via the display_type modparam. Default is VFD.
761          */
762         if (usb_match_id(interface, ir_only_list))
763                 context->display = 0;
764         else
765                 context->display = 1;
766
767         usbdev     = usb_get_dev(interface_to_usbdev(interface));
768         iface_desc = interface->cur_altsetting;
769         num_endpts = iface_desc->desc.bNumEndpoints;
770         ifnum      = iface_desc->desc.bInterfaceNumber;
771         vendor     = le16_to_cpu(usbdev->descriptor.idVendor);
772         product    = le16_to_cpu(usbdev->descriptor.idProduct);
773
774         dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
775                 __func__, vendor, product, ifnum);
776
777         /*
778          * Scan the endpoint list and set:
779          *      first input endpoint = IR endpoint
780          *      first output endpoint = display endpoint
781          */
782         for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
783                 struct usb_endpoint_descriptor *ep;
784                 int ep_dir;
785                 int ep_type;
786                 ep = &iface_desc->endpoint[i].desc;
787                 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
788                 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
789
790                 if (!ir_ep_found &&
791                         ep_dir == USB_DIR_IN &&
792                         ep_type == USB_ENDPOINT_XFER_INT) {
793
794                         rx_endpoint = ep;
795                         ir_ep_found = 1;
796                         dev_dbg(dev, "%s: found IR endpoint\n", __func__);
797
798                 } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
799                            ep_type == USB_ENDPOINT_XFER_INT) {
800                         tx_endpoint = ep;
801                         display_ep_found = 1;
802                         dev_dbg(dev, "%s: found display endpoint\n", __func__);
803                 }
804         }
805
806         /*
807          * Some iMON receivers have no display. Unfortunately, it seems
808          * that SoundGraph recycles device IDs between devices both with
809          * and without... :\
810          */
811         if (context->display == 0) {
812                 display_ep_found = 0;
813                 dev_dbg(dev, "%s: device has no display\n", __func__);
814         }
815
816         /* Input endpoint is mandatory */
817         if (!ir_ep_found) {
818                 dev_err(dev, "%s: no valid input (IR) endpoint found.\n", __func__);
819                 retval = -ENODEV;
820                 alloc_status = 2;
821                 goto alloc_status_switch;
822         }
823
824         /* Determine if display requires 6 packets */
825         if (display_ep_found) {
826                 if (usb_match_id(interface, vfd_proto_6p_list))
827                         vfd_proto_6p = 1;
828
829                 dev_dbg(dev, "%s: vfd_proto_6p: %d\n",
830                         __func__, vfd_proto_6p);
831         }
832
833         driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
834         if (!driver) {
835                 dev_err(dev, "%s: kzalloc failed for lirc_driver\n", __func__);
836                 alloc_status = 2;
837                 goto alloc_status_switch;
838         }
839         rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
840         if (!rbuf) {
841                 dev_err(dev, "%s: kmalloc failed for lirc_buffer\n", __func__);
842                 alloc_status = 3;
843                 goto alloc_status_switch;
844         }
845         if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
846                 dev_err(dev, "%s: lirc_buffer_init failed\n", __func__);
847                 alloc_status = 4;
848                 goto alloc_status_switch;
849         }
850         rx_urb = usb_alloc_urb(0, GFP_KERNEL);
851         if (!rx_urb) {
852                 dev_err(dev, "%s: usb_alloc_urb failed for IR urb\n", __func__);
853                 alloc_status = 5;
854                 goto alloc_status_switch;
855         }
856         tx_urb = usb_alloc_urb(0, GFP_KERNEL);
857         if (!tx_urb) {
858                 dev_err(dev, "%s: usb_alloc_urb failed for display urb\n",
859                     __func__);
860                 alloc_status = 6;
861                 goto alloc_status_switch;
862         }
863
864         mutex_init(&context->ctx_lock);
865         context->vfd_proto_6p = vfd_proto_6p;
866
867         strcpy(driver->name, MOD_NAME);
868         driver->minor = -1;
869         driver->code_length = BUF_CHUNK_SIZE * 8;
870         driver->sample_rate = 0;
871         driver->features = LIRC_CAN_REC_MODE2;
872         driver->data = context;
873         driver->rbuf = rbuf;
874         driver->set_use_inc = ir_open;
875         driver->set_use_dec = ir_close;
876         driver->dev = &interface->dev;
877         driver->owner = THIS_MODULE;
878
879         mutex_lock(&context->ctx_lock);
880
881         context->driver = driver;
882         /* start out in keyboard mode */
883
884         lirc_minor = lirc_register_driver(driver);
885         if (lirc_minor < 0) {
886                 dev_err(dev, "%s: lirc_register_driver failed\n", __func__);
887                 alloc_status = 7;
888                 goto unlock;
889         } else
890                 dev_info(dev, "Registered iMON driver "
891                          "(lirc minor: %d)\n", lirc_minor);
892
893         /* Needed while unregistering! */
894         driver->minor = lirc_minor;
895
896         context->usbdev = usbdev;
897         context->dev_present = 1;
898         context->rx_endpoint = rx_endpoint;
899         context->rx_urb = rx_urb;
900
901         /*
902          * tx is used to send characters to lcd/vfd, associate RF
903          * remotes, set IR protocol, and maybe more...
904          */
905         context->tx_endpoint = tx_endpoint;
906         context->tx_urb = tx_urb;
907
908         if (display_ep_found)
909                 context->display = 1;
910
911         usb_fill_int_urb(context->rx_urb, context->usbdev,
912                 usb_rcvintpipe(context->usbdev,
913                         context->rx_endpoint->bEndpointAddress),
914                 context->usb_rx_buf, sizeof(context->usb_rx_buf),
915                 usb_rx_callback, context,
916                 context->rx_endpoint->bInterval);
917
918         retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
919
920         if (retval) {
921                 dev_err(dev, "%s: usb_submit_urb failed for intf0 (%d)\n",
922                         __func__, retval);
923                 mutex_unlock(&context->ctx_lock);
924                 goto exit;
925         }
926
927         usb_set_intfdata(interface, context);
928
929         if (context->display && ifnum == 0) {
930                 dev_dbg(dev, "%s: Registering iMON display with sysfs\n",
931                         __func__);
932
933                 if (usb_register_dev(interface, &imon_class)) {
934                         /* Not a fatal error, so ignore */
935                         dev_info(dev, "%s: could not get a minor number for "
936                                  "display\n", __func__);
937                 }
938         }
939
940         dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
941                  "usb<%d:%d> initialized\n", vendor, product, ifnum,
942                  usbdev->bus->busnum, usbdev->devnum);
943
944 unlock:
945         mutex_unlock(&context->ctx_lock);
946 alloc_status_switch:
947
948         switch (alloc_status) {
949         case 7:
950                 usb_free_urb(tx_urb);
951         case 6:
952                 usb_free_urb(rx_urb);
953         case 5:
954                 if (rbuf)
955                         lirc_buffer_free(rbuf);
956         case 4:
957                 kfree(rbuf);
958         case 3:
959                 kfree(driver);
960         case 2:
961                 kfree(context);
962                 context = NULL;
963         case 1:
964                 if (retval != -ENODEV)
965                         retval = -ENOMEM;
966                 break;
967         case 0:
968                 retval = 0;
969         }
970
971 exit:
972         mutex_unlock(&driver_lock);
973
974         return retval;
975 }
976
977 /**
978  * Callback function for USB core API: disconnect
979  */
980 static void imon_disconnect(struct usb_interface *interface)
981 {
982         struct imon_context *context;
983         int ifnum;
984
985         /* prevent races with ir_open()/display_open() */
986         mutex_lock(&driver_lock);
987
988         context = usb_get_intfdata(interface);
989         ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
990
991         mutex_lock(&context->ctx_lock);
992
993         usb_set_intfdata(interface, NULL);
994
995         /* Abort ongoing write */
996         if (atomic_read(&context->tx.busy)) {
997                 usb_kill_urb(context->tx_urb);
998                 complete_all(&context->tx.finished);
999         }
1000
1001         context->dev_present = 0;
1002         usb_kill_urb(context->rx_urb);
1003         if (context->display)
1004                 usb_deregister_dev(interface, &imon_class);
1005
1006         if (!context->ir_isopen && !context->dev_present) {
1007                 deregister_from_lirc(context);
1008                 mutex_unlock(&context->ctx_lock);
1009                 if (!context->display_isopen)
1010                         free_imon_context(context);
1011         } else
1012                 mutex_unlock(&context->ctx_lock);
1013
1014         mutex_unlock(&driver_lock);
1015
1016         printk(KERN_INFO "%s: iMON device (intf%d) disconnected\n",
1017                __func__, ifnum);
1018 }
1019
1020 static int imon_suspend(struct usb_interface *intf, pm_message_t message)
1021 {
1022         struct imon_context *context = usb_get_intfdata(intf);
1023
1024         usb_kill_urb(context->rx_urb);
1025
1026         return 0;
1027 }
1028
1029 static int imon_resume(struct usb_interface *intf)
1030 {
1031         int rc = 0;
1032         struct imon_context *context = usb_get_intfdata(intf);
1033
1034         usb_fill_int_urb(context->rx_urb, context->usbdev,
1035                 usb_rcvintpipe(context->usbdev,
1036                         context->rx_endpoint->bEndpointAddress),
1037                 context->usb_rx_buf, sizeof(context->usb_rx_buf),
1038                 usb_rx_callback, context,
1039                 context->rx_endpoint->bInterval);
1040
1041         rc = usb_submit_urb(context->rx_urb, GFP_ATOMIC);
1042
1043         return rc;
1044 }
1045
1046 module_usb_driver(imon_driver);