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