Linux-libre 4.9.18-gnu
[librecmc/linux-libre.git] / drivers / staging / media / lirc / lirc_sasem.c
1 /*
2  * lirc_sasem.c - USB remote support for LIRC
3  * Version 0.5
4  *
5  * Copyright (C) 2004-2005 Oliver Stabel <oliver.stabel@gmx.de>
6  *                       Tim Davies <tim@opensystems.net.au>
7  *
8  * This driver was derived from:
9  *   Venky Raju <dev@venky.ws>
10  *      "lirc_imon - "LIRC/VFD driver for Ahanix/Soundgraph IMON IR/VFD"
11  *   Paul Miller <pmiller9@users.sourceforge.net>'s 2003-2004
12  *      "lirc_atiusb - USB remote support for LIRC"
13  *   Culver Consulting Services <henry@culcon.com>'s 2003
14  *      "Sasem OnAir VFD/IR USB driver"
15  *
16  *
17  * NOTE - The LCDproc iMon driver should work with this module.  More info at
18  *      http://www.frogstorm.info/sasem
19  */
20
21 /*
22  *  This program is free software; you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License as published by
24  *  the Free Software Foundation; either version 2 of the License, or
25  *  (at your option) any later version.
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with this program; if not, write to the Free Software
34  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35  */
36
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
39 #include <linux/errno.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/uaccess.h>
44 #include <linux/usb.h>
45 #include <linux/ktime.h>
46
47 #include <media/lirc.h>
48 #include <media/lirc_dev.h>
49
50 #define MOD_AUTHOR      "Oliver Stabel <oliver.stabel@gmx.de>, " \
51                         "Tim Davies <tim@opensystems.net.au>"
52 #define MOD_DESC        "USB Driver for Sasem Remote Controller V1.1"
53 #define MOD_NAME        "lirc_sasem"
54 #define MOD_VERSION     "0.5"
55
56 #define VFD_MINOR_BASE  144     /* Same as LCD */
57 #define DEVICE_NAME     "lcd%d"
58
59 #define BUF_CHUNK_SIZE  8
60 #define BUF_SIZE        128
61
62 #define IOCTL_LCD_CONTRAST 1
63
64 /*** P R O T O T Y P E S ***/
65
66 /* USB Callback prototypes */
67 static int sasem_probe(struct usb_interface *interface,
68                         const struct usb_device_id *id);
69 static void sasem_disconnect(struct usb_interface *interface);
70 static void usb_rx_callback(struct urb *urb);
71 static void usb_tx_callback(struct urb *urb);
72
73 /* VFD file_operations function prototypes */
74 static int vfd_open(struct inode *inode, struct file *file);
75 static long vfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
76 static int vfd_close(struct inode *inode, struct file *file);
77 static ssize_t vfd_write(struct file *file, const char __user *buf,
78                                 size_t n_bytes, loff_t *pos);
79
80 /* LIRC driver function prototypes */
81 static int ir_open(void *data);
82 static void ir_close(void *data);
83
84 /*** G L O B A L S ***/
85 #define SASEM_DATA_BUF_SZ       32
86
87 struct sasem_context {
88         struct usb_device *dev;
89         int vfd_isopen;                 /* VFD port has been opened */
90         unsigned int vfd_contrast;      /* VFD contrast */
91         int ir_isopen;                  /* IR port has been opened */
92         int dev_present;                /* USB device presence */
93         struct mutex ctx_lock;          /* to lock this object */
94         wait_queue_head_t remove_ok;    /* For unexpected USB disconnects */
95
96         struct lirc_driver *driver;
97         struct usb_endpoint_descriptor *rx_endpoint;
98         struct usb_endpoint_descriptor *tx_endpoint;
99         struct urb *rx_urb;
100         struct urb *tx_urb;
101         unsigned char usb_rx_buf[8];
102         unsigned char usb_tx_buf[8];
103
104         struct tx_t {
105                 unsigned char data_buf[SASEM_DATA_BUF_SZ]; /* user data
106                                                             * buffer */
107                 struct completion finished;  /* wait for write to finish  */
108                 atomic_t busy;               /* write in progress */
109                 int status;                  /* status of tx completion */
110         } tx;
111
112         /* for dealing with repeat codes (wish there was a toggle bit!) */
113         ktime_t presstime;
114         char lastcode[8];
115         int codesaved;
116 };
117
118 /* VFD file operations */
119 static const struct file_operations vfd_fops = {
120         .owner          = THIS_MODULE,
121         .open           = &vfd_open,
122         .write          = vfd_write,
123         .unlocked_ioctl = &vfd_ioctl,
124         .release        = &vfd_close,
125         .llseek         = noop_llseek,
126 };
127
128 /* USB Device ID for Sasem USB Control Board */
129 static struct usb_device_id sasem_usb_id_table[] = {
130         /* Sasem USB Control Board */
131         { USB_DEVICE(0x11ba, 0x0101) },
132         /* Terminating entry */
133         {}
134 };
135
136 /* USB Device data */
137 static struct usb_driver sasem_driver = {
138         .name           = MOD_NAME,
139         .probe          = sasem_probe,
140         .disconnect     = sasem_disconnect,
141         .id_table       = sasem_usb_id_table,
142 };
143
144 static struct usb_class_driver sasem_class = {
145         .name           = DEVICE_NAME,
146         .fops           = &vfd_fops,
147         .minor_base     = VFD_MINOR_BASE,
148 };
149
150 /* to prevent races between open() and disconnect() */
151 static DEFINE_MUTEX(disconnect_lock);
152
153 static int debug;
154
155
156 /*** M O D U L E   C O D E ***/
157 MODULE_AUTHOR(MOD_AUTHOR);
158 MODULE_DESCRIPTION(MOD_DESC);
159 MODULE_LICENSE("GPL");
160 module_param(debug, int, S_IRUGO | S_IWUSR);
161 MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
162
163 static void delete_context(struct sasem_context *context)
164 {
165         usb_free_urb(context->tx_urb);  /* VFD */
166         usb_free_urb(context->rx_urb);  /* IR */
167         lirc_buffer_free(context->driver->rbuf);
168         kfree(context->driver->rbuf);
169         kfree(context->driver);
170         kfree(context);
171 }
172
173 static void deregister_from_lirc(struct sasem_context *context)
174 {
175         int retval;
176         int minor = context->driver->minor;
177
178         retval = lirc_unregister_driver(minor);
179         if (retval)
180                 dev_err(&context->dev->dev,
181                         "%s: unable to deregister from lirc (%d)\n",
182                         __func__, retval);
183         else
184                 dev_info(&context->dev->dev,
185                          "Deregistered Sasem driver (minor:%d)\n", minor);
186 }
187
188 /**
189  * Called when the VFD device (e.g. /dev/usb/lcd)
190  * is opened by the application.
191  */
192 static int vfd_open(struct inode *inode, struct file *file)
193 {
194         struct usb_interface *interface;
195         struct sasem_context *context = NULL;
196         int subminor;
197         int retval = 0;
198
199         /* prevent races with disconnect */
200         mutex_lock(&disconnect_lock);
201
202         subminor = iminor(inode);
203         interface = usb_find_interface(&sasem_driver, subminor);
204         if (!interface) {
205                 pr_err("%s: could not find interface for minor %d\n",
206                        __func__, subminor);
207                 retval = -ENODEV;
208                 goto exit;
209         }
210         context = usb_get_intfdata(interface);
211
212         if (!context) {
213                 dev_err(&interface->dev, "no context found for minor %d\n",
214                         subminor);
215                 retval = -ENODEV;
216                 goto exit;
217         }
218
219         mutex_lock(&context->ctx_lock);
220
221         if (context->vfd_isopen) {
222                 dev_err(&interface->dev,
223                         "%s: VFD port is already open", __func__);
224                 retval = -EBUSY;
225         } else {
226                 context->vfd_isopen = 1;
227                 file->private_data = context;
228                 dev_info(&interface->dev, "VFD port opened\n");
229         }
230
231         mutex_unlock(&context->ctx_lock);
232
233 exit:
234         mutex_unlock(&disconnect_lock);
235         return retval;
236 }
237
238 /**
239  * Called when the VFD device (e.g. /dev/usb/lcd)
240  * is closed by the application.
241  */
242 static long vfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
243 {
244         struct sasem_context *context;
245
246         context = (struct sasem_context *) file->private_data;
247
248         if (!context) {
249                 pr_err("%s: no context for device\n", __func__);
250                 return -ENODEV;
251         }
252
253         mutex_lock(&context->ctx_lock);
254
255         switch (cmd) {
256         case IOCTL_LCD_CONTRAST:
257                 if (arg > 1000)
258                         arg = 1000;
259                 context->vfd_contrast = (unsigned int)arg;
260                 break;
261         default:
262                 pr_info("Unknown IOCTL command\n");
263                 mutex_unlock(&context->ctx_lock);
264                 return -ENOIOCTLCMD;  /* not supported */
265         }
266
267         mutex_unlock(&context->ctx_lock);
268         return 0;
269 }
270
271 /**
272  * Called when the VFD device (e.g. /dev/usb/lcd)
273  * is closed by the application.
274  */
275 static int vfd_close(struct inode *inode, struct file *file)
276 {
277         struct sasem_context *context = NULL;
278         int retval = 0;
279
280         context = (struct sasem_context *) file->private_data;
281
282         if (!context) {
283                 pr_err("%s: no context for device\n", __func__);
284                 return -ENODEV;
285         }
286
287         mutex_lock(&context->ctx_lock);
288
289         if (!context->vfd_isopen) {
290                 dev_err(&context->dev->dev, "%s: VFD is not open\n", __func__);
291                 retval = -EIO;
292         } else {
293                 context->vfd_isopen = 0;
294                 dev_info(&context->dev->dev, "VFD port closed\n");
295                 if (!context->dev_present && !context->ir_isopen) {
296                         /* Device disconnected before close and IR port is
297                          * not open. If IR port is open, context will be
298                          * deleted by ir_close. */
299                         mutex_unlock(&context->ctx_lock);
300                         delete_context(context);
301                         return retval;
302                 }
303         }
304
305         mutex_unlock(&context->ctx_lock);
306         return retval;
307 }
308
309 /**
310  * Sends a packet to the VFD.
311  */
312 static int send_packet(struct sasem_context *context)
313 {
314         unsigned int pipe;
315         int interval = 0;
316         int retval = 0;
317
318         pipe = usb_sndintpipe(context->dev,
319                         context->tx_endpoint->bEndpointAddress);
320         interval = context->tx_endpoint->bInterval;
321
322         usb_fill_int_urb(context->tx_urb, context->dev, pipe,
323                 context->usb_tx_buf, sizeof(context->usb_tx_buf),
324                 usb_tx_callback, context, interval);
325
326         context->tx_urb->actual_length = 0;
327
328         init_completion(&context->tx.finished);
329         atomic_set(&context->tx.busy, 1);
330
331         retval =  usb_submit_urb(context->tx_urb, GFP_KERNEL);
332         if (retval) {
333                 atomic_set(&context->tx.busy, 0);
334                 dev_err(&context->dev->dev, "error submitting urb (%d)\n",
335                         retval);
336         } else {
337                 /* Wait for transmission to complete (or abort) */
338                 mutex_unlock(&context->ctx_lock);
339                 wait_for_completion(&context->tx.finished);
340                 mutex_lock(&context->ctx_lock);
341
342                 retval = context->tx.status;
343                 if (retval)
344                         dev_err(&context->dev->dev,
345                                 "packet tx failed (%d)\n", retval);
346         }
347
348         return retval;
349 }
350
351 /**
352  * Writes data to the VFD.  The Sasem VFD is 2x16 characters
353  * and requires data in 9 consecutive USB interrupt packets,
354  * each packet carrying 8 bytes.
355  */
356 static ssize_t vfd_write(struct file *file, const char __user *buf,
357                                 size_t n_bytes, loff_t *pos)
358 {
359         int i;
360         int retval = 0;
361         struct sasem_context *context;
362         int *data_buf = NULL;
363
364         context = (struct sasem_context *) file->private_data;
365         if (!context) {
366                 pr_err("%s: no context for device\n", __func__);
367                 return -ENODEV;
368         }
369
370         mutex_lock(&context->ctx_lock);
371
372         if (!context->dev_present) {
373                 pr_err("%s: no Sasem device present\n", __func__);
374                 retval = -ENODEV;
375                 goto exit;
376         }
377
378         if (n_bytes <= 0 || n_bytes > SASEM_DATA_BUF_SZ) {
379                 dev_err(&context->dev->dev, "%s: invalid payload size\n",
380                         __func__);
381                 retval = -EINVAL;
382                 goto exit;
383         }
384
385         data_buf = memdup_user(buf, n_bytes);
386         if (IS_ERR(data_buf)) {
387                 retval = PTR_ERR(data_buf);
388                 data_buf = NULL;
389                 goto exit;
390         }
391
392         memcpy(context->tx.data_buf, data_buf, n_bytes);
393
394         /* Pad with spaces */
395         for (i = n_bytes; i < SASEM_DATA_BUF_SZ; ++i)
396                 context->tx.data_buf[i] = ' ';
397
398         /* Nine 8 byte packets to be sent */
399         /* NOTE: "\x07\x01\0\0\0\0\0\0" or "\x0c\0\0\0\0\0\0\0"
400          *       will clear the VFD */
401         for (i = 0; i < 9; i++) {
402                 switch (i) {
403                 case 0:
404                         memcpy(context->usb_tx_buf, "\x07\0\0\0\0\0\0\0", 8);
405                         context->usb_tx_buf[1] = (context->vfd_contrast) ?
406                                 (0x2B - (context->vfd_contrast - 1) / 250)
407                                 : 0x2B;
408                         break;
409                 case 1:
410                         memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
411                         break;
412                 case 2:
413                         memcpy(context->usb_tx_buf, "\x0b\x01\0\0\0\0\0\0", 8);
414                         break;
415                 case 3:
416                         memcpy(context->usb_tx_buf, context->tx.data_buf, 8);
417                         break;
418                 case 4:
419                         memcpy(context->usb_tx_buf,
420                                context->tx.data_buf + 8, 8);
421                         break;
422                 case 5:
423                         memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
424                         break;
425                 case 6:
426                         memcpy(context->usb_tx_buf, "\x0b\x02\0\0\0\0\0\0", 8);
427                         break;
428                 case 7:
429                         memcpy(context->usb_tx_buf,
430                                context->tx.data_buf + 16, 8);
431                         break;
432                 case 8:
433                         memcpy(context->usb_tx_buf,
434                                context->tx.data_buf + 24, 8);
435                         break;
436                 }
437                 retval = send_packet(context);
438                 if (retval) {
439                         dev_err(&context->dev->dev,
440                                 "send packet failed for packet #%d\n", i);
441                         goto exit;
442                 }
443         }
444 exit:
445
446         mutex_unlock(&context->ctx_lock);
447         kfree(data_buf);
448
449         return (!retval) ? n_bytes : retval;
450 }
451
452 /**
453  * Callback function for USB core API: transmit data
454  */
455 static void usb_tx_callback(struct urb *urb)
456 {
457         struct sasem_context *context;
458
459         if (!urb)
460                 return;
461         context = (struct sasem_context *) urb->context;
462         if (!context)
463                 return;
464
465         context->tx.status = urb->status;
466
467         /* notify waiters that write has finished */
468         atomic_set(&context->tx.busy, 0);
469         complete(&context->tx.finished);
470 }
471
472 /**
473  * Called by lirc_dev when the application opens /dev/lirc
474  */
475 static int ir_open(void *data)
476 {
477         int retval = 0;
478         struct sasem_context *context;
479
480         /* prevent races with disconnect */
481         mutex_lock(&disconnect_lock);
482
483         context = data;
484
485         mutex_lock(&context->ctx_lock);
486
487         if (context->ir_isopen) {
488                 dev_err(&context->dev->dev, "%s: IR port is already open\n",
489                         __func__);
490                 retval = -EBUSY;
491                 goto exit;
492         }
493
494         usb_fill_int_urb(context->rx_urb, context->dev,
495                 usb_rcvintpipe(context->dev,
496                                 context->rx_endpoint->bEndpointAddress),
497                 context->usb_rx_buf, sizeof(context->usb_rx_buf),
498                 usb_rx_callback, context, context->rx_endpoint->bInterval);
499
500         retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
501
502         if (retval)
503                 dev_err(&context->dev->dev,
504                         "usb_submit_urb failed for ir_open (%d)\n", retval);
505         else {
506                 context->ir_isopen = 1;
507                 dev_info(&context->dev->dev, "IR port opened\n");
508         }
509
510 exit:
511         mutex_unlock(&context->ctx_lock);
512
513         mutex_unlock(&disconnect_lock);
514         return retval;
515 }
516
517 /**
518  * Called by lirc_dev when the application closes /dev/lirc
519  */
520 static void ir_close(void *data)
521 {
522         struct sasem_context *context;
523
524         context = data;
525         if (!context) {
526                 pr_err("%s: no context for device\n", __func__);
527                 return;
528         }
529
530         mutex_lock(&context->ctx_lock);
531
532         usb_kill_urb(context->rx_urb);
533         context->ir_isopen = 0;
534         pr_info("IR port closed\n");
535
536         if (!context->dev_present) {
537
538                 /*
539                  * Device disconnected while IR port was
540                  * still open. Driver was not deregistered
541                  * at disconnect time, so do it now.
542                  */
543                 deregister_from_lirc(context);
544                 if (!context->vfd_isopen) {
545                         mutex_unlock(&context->ctx_lock);
546                         delete_context(context);
547                         return;
548                 }
549                 /* If VFD port is open, context will be deleted by vfd_close */
550         }
551
552         mutex_unlock(&context->ctx_lock);
553 }
554
555 /**
556  * Process the incoming packet
557  */
558 static void incoming_packet(struct sasem_context *context,
559                                    struct urb *urb)
560 {
561         int len = urb->actual_length;
562         unsigned char *buf = urb->transfer_buffer;
563         u64 ns;
564         ktime_t kt;
565
566         if (len != 8) {
567                 dev_warn(&context->dev->dev,
568                          "%s: invalid incoming packet size (%d)\n",
569                          __func__, len);
570                 return;
571         }
572
573         if (debug)
574                 dev_info(&context->dev->dev, "Incoming data: %*ph\n", len, buf);
575         /*
576          * Lirc could deal with the repeat code, but we really need to block it
577          * if it arrives too late.  Otherwise we could repeat the wrong code.
578          */
579
580         /* get the time since the last button press */
581         kt = ktime_get();
582         ns = ktime_to_ns(ktime_sub(kt, context->presstime));
583
584         if (memcmp(buf, "\x08\0\0\0\0\0\0\0", 8) == 0) {
585                 /*
586                  * the repeat code is being sent, so we copy
587                  * the old code to LIRC
588                  */
589
590                 /*
591                  * NOTE: Only if the last code was less than 250ms ago
592                  * - no one should be able to push another (undetected) button
593                  *   in that time and then get a false repeat of the previous
594                  *   press but it is long enough for a genuine repeat
595                  */
596                 if ((ns < 250 * NSEC_PER_MSEC) && (context->codesaved != 0)) {
597                         memcpy(buf, &context->lastcode, 8);
598                         context->presstime = kt;
599                 }
600         } else {
601                 /* save the current valid code for repeats */
602                 memcpy(&context->lastcode, buf, 8);
603                 /*
604                  * set flag to signal a valid code was save;
605                  * just for safety reasons
606                  */
607                 context->codesaved = 1;
608                 context->presstime = kt;
609         }
610
611         lirc_buffer_write(context->driver->rbuf, buf);
612         wake_up(&context->driver->rbuf->wait_poll);
613 }
614
615 /**
616  * Callback function for USB core API: receive data
617  */
618 static void usb_rx_callback(struct urb *urb)
619 {
620         struct sasem_context *context;
621
622         if (!urb)
623                 return;
624         context = (struct sasem_context *) urb->context;
625         if (!context)
626                 return;
627
628         switch (urb->status) {
629         case -ENOENT:           /* usbcore unlink successful! */
630                 return;
631
632         case 0:
633                 if (context->ir_isopen)
634                         incoming_packet(context, urb);
635                 break;
636
637         default:
638                 dev_warn(&urb->dev->dev, "%s: status (%d): ignored",
639                          __func__, urb->status);
640                 break;
641         }
642
643         usb_submit_urb(context->rx_urb, GFP_ATOMIC);
644 }
645
646 /**
647  * Callback function for USB core API: Probe
648  */
649 static int sasem_probe(struct usb_interface *interface,
650                         const struct usb_device_id *id)
651 {
652         struct usb_device *dev = NULL;
653         struct usb_host_interface *iface_desc = NULL;
654         struct usb_endpoint_descriptor *rx_endpoint = NULL;
655         struct usb_endpoint_descriptor *tx_endpoint = NULL;
656         struct urb *rx_urb = NULL;
657         struct urb *tx_urb = NULL;
658         struct lirc_driver *driver = NULL;
659         struct lirc_buffer *rbuf = NULL;
660         int lirc_minor = 0;
661         int num_endpoints;
662         int retval = 0;
663         int vfd_ep_found;
664         int ir_ep_found;
665         int alloc_status;
666         struct sasem_context *context = NULL;
667         int i;
668
669         dev_info(&interface->dev, "%s: found Sasem device\n", __func__);
670
671
672         dev = usb_get_dev(interface_to_usbdev(interface));
673         iface_desc = interface->cur_altsetting;
674         num_endpoints = iface_desc->desc.bNumEndpoints;
675
676         /*
677          * Scan the endpoint list and set:
678          *      first input endpoint = IR endpoint
679          *      first output endpoint = VFD endpoint
680          */
681
682         ir_ep_found = 0;
683         vfd_ep_found = 0;
684
685         for (i = 0; i < num_endpoints && !(ir_ep_found && vfd_ep_found); ++i) {
686
687                 struct usb_endpoint_descriptor *ep;
688
689                 ep = &iface_desc->endpoint [i].desc;
690
691                 if (!ir_ep_found &&
692                         usb_endpoint_is_int_in(ep)) {
693
694                         rx_endpoint = ep;
695                         ir_ep_found = 1;
696                         if (debug)
697                                 dev_info(&interface->dev,
698                                         "%s: found IR endpoint\n", __func__);
699
700                 } else if (!vfd_ep_found &&
701                         usb_endpoint_is_int_out(ep)) {
702                         tx_endpoint = ep;
703                         vfd_ep_found = 1;
704                         if (debug)
705                                 dev_info(&interface->dev,
706                                         "%s: found VFD endpoint\n", __func__);
707                 }
708         }
709
710         /* Input endpoint is mandatory */
711         if (!ir_ep_found) {
712                 dev_err(&interface->dev,
713                         "%s: no valid input (IR) endpoint found.\n", __func__);
714                 retval = -ENODEV;
715                 goto exit;
716         }
717
718         if (!vfd_ep_found)
719                 dev_info(&interface->dev,
720                         "%s: no valid output (VFD) endpoint found.\n",
721                         __func__);
722
723
724         /* Allocate memory */
725         alloc_status = 0;
726
727         context = kzalloc(sizeof(*context), GFP_KERNEL);
728         if (!context) {
729                 alloc_status = 1;
730                 goto alloc_status_switch;
731         }
732         driver = kzalloc(sizeof(*driver), GFP_KERNEL);
733         if (!driver) {
734                 alloc_status = 2;
735                 goto alloc_status_switch;
736         }
737         rbuf = kmalloc(sizeof(*rbuf), GFP_KERNEL);
738         if (!rbuf) {
739                 alloc_status = 3;
740                 goto alloc_status_switch;
741         }
742         if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
743                 dev_err(&interface->dev,
744                         "%s: lirc_buffer_init failed\n", __func__);
745                 alloc_status = 4;
746                 goto alloc_status_switch;
747         }
748         rx_urb = usb_alloc_urb(0, GFP_KERNEL);
749         if (!rx_urb) {
750                 alloc_status = 5;
751                 goto alloc_status_switch;
752         }
753         if (vfd_ep_found) {
754                 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
755                 if (!tx_urb) {
756                         alloc_status = 6;
757                         goto alloc_status_switch;
758                 }
759         }
760
761         mutex_init(&context->ctx_lock);
762
763         strcpy(driver->name, MOD_NAME);
764         driver->minor = -1;
765         driver->code_length = 64;
766         driver->sample_rate = 0;
767         driver->features = LIRC_CAN_REC_LIRCCODE;
768         driver->data = context;
769         driver->rbuf = rbuf;
770         driver->set_use_inc = ir_open;
771         driver->set_use_dec = ir_close;
772         driver->dev   = &interface->dev;
773         driver->owner = THIS_MODULE;
774
775         mutex_lock(&context->ctx_lock);
776
777         lirc_minor = lirc_register_driver(driver);
778         if (lirc_minor < 0) {
779                 dev_err(&interface->dev,
780                         "%s: lirc_register_driver failed\n", __func__);
781                 alloc_status = 7;
782                 retval = lirc_minor;
783                 goto unlock;
784         } else
785                 dev_info(&interface->dev,
786                          "%s: Registered Sasem driver (minor:%d)\n",
787                          __func__, lirc_minor);
788
789         /* Needed while unregistering! */
790         driver->minor = lirc_minor;
791
792         context->dev = dev;
793         context->dev_present = 1;
794         context->rx_endpoint = rx_endpoint;
795         context->rx_urb = rx_urb;
796         if (vfd_ep_found) {
797                 context->tx_endpoint = tx_endpoint;
798                 context->tx_urb = tx_urb;
799                 context->vfd_contrast = 1000;   /* range 0 - 1000 */
800         }
801         context->driver = driver;
802
803         usb_set_intfdata(interface, context);
804
805         if (vfd_ep_found) {
806
807                 if (debug)
808                         dev_info(&interface->dev,
809                                  "Registering VFD with sysfs\n");
810                 if (usb_register_dev(interface, &sasem_class))
811                         /* Not a fatal error, so ignore */
812                         dev_info(&interface->dev,
813                                  "%s: could not get a minor number for VFD\n",
814                                  __func__);
815         }
816
817         dev_info(&interface->dev,
818                  "%s: Sasem device on usb<%d:%d> initialized\n",
819                  __func__, dev->bus->busnum, dev->devnum);
820 unlock:
821         mutex_unlock(&context->ctx_lock);
822
823 alloc_status_switch:
824         switch (alloc_status) {
825
826         case 7:
827                 if (vfd_ep_found)
828                         usb_free_urb(tx_urb);
829         case 6:
830                 usb_free_urb(rx_urb);
831                 /* fall-through */
832         case 5:
833                 lirc_buffer_free(rbuf);
834                 /* fall-through */
835         case 4:
836                 kfree(rbuf);
837                 /* fall-through */
838         case 3:
839                 kfree(driver);
840                 /* fall-through */
841         case 2:
842                 kfree(context);
843                 context = NULL;
844                 /* fall-through */
845         case 1:
846                 if (retval == 0)
847                         retval = -ENOMEM;
848         }
849
850 exit:
851         return retval;
852 }
853
854 /**
855  * Callback function for USB core API: disconnect
856  */
857 static void sasem_disconnect(struct usb_interface *interface)
858 {
859         struct sasem_context *context;
860
861         /* prevent races with ir_open()/vfd_open() */
862         mutex_lock(&disconnect_lock);
863
864         context = usb_get_intfdata(interface);
865         mutex_lock(&context->ctx_lock);
866
867         dev_info(&interface->dev, "%s: Sasem device disconnected\n",
868                  __func__);
869
870         usb_set_intfdata(interface, NULL);
871         context->dev_present = 0;
872
873         /* Stop reception */
874         usb_kill_urb(context->rx_urb);
875
876         /* Abort ongoing write */
877         if (atomic_read(&context->tx.busy)) {
878
879                 usb_kill_urb(context->tx_urb);
880                 wait_for_completion(&context->tx.finished);
881         }
882
883         /* De-register from lirc_dev if IR port is not open */
884         if (!context->ir_isopen)
885                 deregister_from_lirc(context);
886
887         usb_deregister_dev(interface, &sasem_class);
888
889         mutex_unlock(&context->ctx_lock);
890
891         if (!context->ir_isopen && !context->vfd_isopen)
892                 delete_context(context);
893
894         mutex_unlock(&disconnect_lock);
895 }
896
897 module_usb_driver(sasem_driver);