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