Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us