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