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