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