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