blob: 904a4667bbb8bebe3cb43bf5201be9d533ada07a [file] [log] [blame]
Jarod Wilsonc5ac4572010-07-26 20:31:11 -03001/*
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
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -030037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Jarod Wilsonc5ac4572010-07-26 20:31:11 -030039#include <linux/errno.h>
Jarod Wilsonc5ac4572010-07-26 20:31:11 -030040#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 */
67static int sasem_probe(struct usb_interface *interface,
68 const struct usb_device_id *id);
69static void sasem_disconnect(struct usb_interface *interface);
70static void usb_rx_callback(struct urb *urb);
71static void usb_tx_callback(struct urb *urb);
72
73/* VFD file_operations function prototypes */
74static int vfd_open(struct inode *inode, struct file *file);
75static long vfd_ioctl(struct file *file, unsigned cmd, unsigned long arg);
76static int vfd_close(struct inode *inode, struct file *file);
Ebru Akagunduzee3a6362014-03-05 19:15:30 +020077static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilsonc5ac4572010-07-26 20:31:11 -030078 size_t n_bytes, loff_t *pos);
79
80/* LIRC driver function prototypes */
81static int ir_open(void *data);
82static void ir_close(void *data);
83
Jarod Wilsonc5ac4572010-07-26 20:31:11 -030084/*** G L O B A L S ***/
85#define SASEM_DATA_BUF_SZ 32
86
87struct sasem_context {
88
89 struct usb_device *dev;
Andrew Miller3be11132012-02-29 17:10:08 -050090 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 */
Jarod Wilsonc5ac4572010-07-26 20:31:11 -030095 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 {
Andrew Miller3be11132012-02-29 17:10:08 -0500106 unsigned char data_buf[SASEM_DATA_BUF_SZ]; /* user data
107 * buffer */
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300108 struct completion finished; /* wait for write to finish */
Andrew Miller3be11132012-02-29 17:10:08 -0500109 atomic_t busy; /* write in progress */
110 int status; /* status of tx completion */
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300111 } 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 */
Mauro Carvalho Chehab0f9313a2010-07-27 18:44:45 -0300120static const struct file_operations vfd_fops = {
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300121 .owner = THIS_MODULE,
122 .open = &vfd_open,
Ebru Akagunduzee3a6362014-03-05 19:15:30 +0200123 .write = vfd_write,
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300124 .unlocked_ioctl = &vfd_ioctl,
125 .release = &vfd_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200126 .llseek = noop_llseek,
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300127};
128
129/* USB Device ID for Sasem USB Control Board */
130static 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 */
138static 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
145static 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() */
152static DEFINE_MUTEX(disconnect_lock);
153
154static int debug;
155
156
157/*** M O D U L E C O D E ***/
158
159MODULE_AUTHOR(MOD_AUTHOR);
160MODULE_DESCRIPTION(MOD_DESC);
161MODULE_LICENSE("GPL");
162module_param(debug, int, S_IRUGO | S_IWUSR);
163MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes (default: no)");
164
165static 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);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300173}
174
175static void deregister_from_lirc(struct sasem_context *context)
176{
177 int retval;
178 int minor = context->driver->minor;
179
180 retval = lirc_unregister_driver(minor);
181 if (retval)
Haneen Mohammed2c9356d2015-03-18 13:09:50 +0300182 dev_err(&context->dev->dev,
183 "%s: unable to deregister from lirc (%d)\n",
Maciek Borzeckibc28bb32015-09-04 17:04:04 -0300184 __func__, retval);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300185 else
Haneen Mohammed2c9356d2015-03-18 13:09:50 +0300186 dev_info(&context->dev->dev,
Sunil Shahua4c4f952015-06-20 05:53:50 -0300187 "Deregistered Sasem driver (minor:%d)\n", minor);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300188
189}
190
191/**
192 * Called when the VFD device (e.g. /dev/usb/lcd)
193 * is opened by the application.
194 */
195static int vfd_open(struct inode *inode, struct file *file)
196{
197 struct usb_interface *interface;
198 struct sasem_context *context = NULL;
199 int subminor;
200 int retval = 0;
201
202 /* prevent races with disconnect */
203 mutex_lock(&disconnect_lock);
204
205 subminor = iminor(inode);
206 interface = usb_find_interface(&sasem_driver, subminor);
207 if (!interface) {
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300208 pr_err("%s: could not find interface for minor %d\n",
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700209 __func__, subminor);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300210 retval = -ENODEV;
211 goto exit;
212 }
213 context = usb_get_intfdata(interface);
214
215 if (!context) {
Haneen Mohammed8c6ccbe2015-03-06 22:00:10 +0300216 dev_err(&interface->dev, "no context found for minor %d\n",
217 subminor);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300218 retval = -ENODEV;
219 goto exit;
220 }
221
222 mutex_lock(&context->ctx_lock);
223
224 if (context->vfd_isopen) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700225 dev_err(&interface->dev,
226 "%s: VFD port is already open", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300227 retval = -EBUSY;
228 } else {
229 context->vfd_isopen = 1;
230 file->private_data = context;
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700231 dev_info(&interface->dev, "VFD port opened\n");
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300232 }
233
234 mutex_unlock(&context->ctx_lock);
235
236exit:
237 mutex_unlock(&disconnect_lock);
238 return retval;
239}
240
241/**
242 * Called when the VFD device (e.g. /dev/usb/lcd)
243 * is closed by the application.
244 */
245static long vfd_ioctl(struct file *file, unsigned cmd, unsigned long arg)
246{
247 struct sasem_context *context = NULL;
248
249 context = (struct sasem_context *) file->private_data;
250
251 if (!context) {
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300252 pr_err("%s: no context for device\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300253 return -ENODEV;
254 }
255
256 mutex_lock(&context->ctx_lock);
257
258 switch (cmd) {
259 case IOCTL_LCD_CONTRAST:
260 if (arg > 1000)
261 arg = 1000;
262 context->vfd_contrast = (unsigned int)arg;
263 break;
264 default:
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300265 pr_info("Unknown IOCTL command\n");
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300266 mutex_unlock(&context->ctx_lock);
267 return -ENOIOCTLCMD; /* not supported */
268 }
269
270 mutex_unlock(&context->ctx_lock);
271 return 0;
272}
273
274/**
275 * Called when the VFD device (e.g. /dev/usb/lcd)
276 * is closed by the application.
277 */
278static int vfd_close(struct inode *inode, struct file *file)
279{
280 struct sasem_context *context = NULL;
281 int retval = 0;
282
283 context = (struct sasem_context *) file->private_data;
284
285 if (!context) {
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300286 pr_err("%s: no context for device\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300287 return -ENODEV;
288 }
289
290 mutex_lock(&context->ctx_lock);
291
292 if (!context->vfd_isopen) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700293 dev_err(&context->dev->dev, "%s: VFD is not open\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300294 retval = -EIO;
295 } else {
296 context->vfd_isopen = 0;
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300297 dev_info(&context->dev->dev, "VFD port closed\n");
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300298 if (!context->dev_present && !context->ir_isopen) {
299
300 /* Device disconnected before close and IR port is
301 * not open. If IR port is open, context will be
302 * deleted by ir_close. */
303 mutex_unlock(&context->ctx_lock);
304 delete_context(context);
305 return retval;
306 }
307 }
308
309 mutex_unlock(&context->ctx_lock);
310 return retval;
311}
312
313/**
314 * Sends a packet to the VFD.
315 */
316static int send_packet(struct sasem_context *context)
317{
318 unsigned int pipe;
319 int interval = 0;
320 int retval = 0;
321
322 pipe = usb_sndintpipe(context->dev,
323 context->tx_endpoint->bEndpointAddress);
324 interval = context->tx_endpoint->bInterval;
325
326 usb_fill_int_urb(context->tx_urb, context->dev, pipe,
327 context->usb_tx_buf, sizeof(context->usb_tx_buf),
328 usb_tx_callback, context, interval);
329
330 context->tx_urb->actual_length = 0;
331
332 init_completion(&context->tx.finished);
Aya Mahfouz028b2422015-03-10 18:56:37 +0200333 atomic_set(&context->tx.busy, 1);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300334
335 retval = usb_submit_urb(context->tx_urb, GFP_KERNEL);
336 if (retval) {
Aya Mahfouz028b2422015-03-10 18:56:37 +0200337 atomic_set(&context->tx.busy, 0);
Haneen Mohammed8c6ccbe2015-03-06 22:00:10 +0300338 dev_err(&context->dev->dev, "error submitting urb (%d)\n",
339 retval);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300340 } else {
341 /* Wait for transmission to complete (or abort) */
342 mutex_unlock(&context->ctx_lock);
343 wait_for_completion(&context->tx.finished);
344 mutex_lock(&context->ctx_lock);
345
346 retval = context->tx.status;
347 if (retval)
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700348 dev_err(&context->dev->dev,
Haneen Mohammed8c6ccbe2015-03-06 22:00:10 +0300349 "packet tx failed (%d)\n", retval);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300350 }
351
352 return retval;
353}
354
355/**
356 * Writes data to the VFD. The Sasem VFD is 2x16 characters
357 * and requires data in 9 consecutive USB interrupt packets,
358 * each packet carrying 8 bytes.
359 */
Ebru Akagunduzee3a6362014-03-05 19:15:30 +0200360static ssize_t vfd_write(struct file *file, const char __user *buf,
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300361 size_t n_bytes, loff_t *pos)
362{
363 int i;
364 int retval = 0;
365 struct sasem_context *context;
Jarod Wilson55734782011-03-04 17:57:24 -0300366 int *data_buf = NULL;
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300367
368 context = (struct sasem_context *) file->private_data;
369 if (!context) {
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300370 pr_err("%s: no context for device\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300371 return -ENODEV;
372 }
373
374 mutex_lock(&context->ctx_lock);
375
376 if (!context->dev_present) {
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300377 pr_err("%s: no Sasem device present\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300378 retval = -ENODEV;
379 goto exit;
380 }
381
382 if (n_bytes <= 0 || n_bytes > SASEM_DATA_BUF_SZ) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700383 dev_err(&context->dev->dev, "%s: invalid payload size\n",
384 __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300385 retval = -EINVAL;
386 goto exit;
387 }
388
Aya Mahfouz028b2422015-03-10 18:56:37 +0200389 data_buf = memdup_user(buf, n_bytes);
Dan Carpenter79e7c562010-10-15 05:42:35 +0200390 if (IS_ERR(data_buf)) {
Jiri Slabyff7d3682010-09-04 14:16:26 +0200391 retval = PTR_ERR(data_buf);
Dan Carpenter3232e042014-09-04 08:10:05 -0300392 data_buf = NULL;
Jiri Slabyff7d3682010-09-04 14:16:26 +0200393 goto exit;
394 }
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300395
396 memcpy(context->tx.data_buf, data_buf, n_bytes);
397
398 /* Pad with spaces */
399 for (i = n_bytes; i < SASEM_DATA_BUF_SZ; ++i)
400 context->tx.data_buf[i] = ' ';
401
402 /* Nine 8 byte packets to be sent */
403 /* NOTE: "\x07\x01\0\0\0\0\0\0" or "\x0c\0\0\0\0\0\0\0"
404 * will clear the VFD */
405 for (i = 0; i < 9; i++) {
406 switch (i) {
407 case 0:
408 memcpy(context->usb_tx_buf, "\x07\0\0\0\0\0\0\0", 8);
409 context->usb_tx_buf[1] = (context->vfd_contrast) ?
410 (0x2B - (context->vfd_contrast - 1) / 250)
411 : 0x2B;
412 break;
413 case 1:
414 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
415 break;
416 case 2:
417 memcpy(context->usb_tx_buf, "\x0b\x01\0\0\0\0\0\0", 8);
418 break;
419 case 3:
420 memcpy(context->usb_tx_buf, context->tx.data_buf, 8);
421 break;
422 case 4:
423 memcpy(context->usb_tx_buf,
424 context->tx.data_buf + 8, 8);
425 break;
426 case 5:
427 memcpy(context->usb_tx_buf, "\x09\x01\0\0\0\0\0\0", 8);
428 break;
429 case 6:
430 memcpy(context->usb_tx_buf, "\x0b\x02\0\0\0\0\0\0", 8);
431 break;
432 case 7:
433 memcpy(context->usb_tx_buf,
434 context->tx.data_buf + 16, 8);
435 break;
436 case 8:
437 memcpy(context->usb_tx_buf,
438 context->tx.data_buf + 24, 8);
439 break;
440 }
441 retval = send_packet(context);
442 if (retval) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700443 dev_err(&context->dev->dev,
Haneen Mohammed8c6ccbe2015-03-06 22:00:10 +0300444 "send packet failed for packet #%d\n", i);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300445 goto exit;
446 }
447 }
448exit:
449
450 mutex_unlock(&context->ctx_lock);
Jarod Wilson88914bd2011-01-17 16:02:00 -0300451 kfree(data_buf);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300452
453 return (!retval) ? n_bytes : retval;
454}
455
456/**
457 * Callback function for USB core API: transmit data
458 */
459static void usb_tx_callback(struct urb *urb)
460{
461 struct sasem_context *context;
462
463 if (!urb)
464 return;
465 context = (struct sasem_context *) urb->context;
466 if (!context)
467 return;
468
469 context->tx.status = urb->status;
470
471 /* notify waiters that write has finished */
472 atomic_set(&context->tx.busy, 0);
473 complete(&context->tx.finished);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300474}
475
476/**
477 * Called by lirc_dev when the application opens /dev/lirc
478 */
479static int ir_open(void *data)
480{
481 int retval = 0;
482 struct sasem_context *context;
483
484 /* prevent races with disconnect */
485 mutex_lock(&disconnect_lock);
486
Tapasweni Pathakd82e62d2014-10-30 17:02:48 +0530487 context = data;
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300488
489 mutex_lock(&context->ctx_lock);
490
491 if (context->ir_isopen) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700492 dev_err(&context->dev->dev, "%s: IR port is already open\n",
493 __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300494 retval = -EBUSY;
495 goto exit;
496 }
497
498 usb_fill_int_urb(context->rx_urb, context->dev,
499 usb_rcvintpipe(context->dev,
500 context->rx_endpoint->bEndpointAddress),
501 context->usb_rx_buf, sizeof(context->usb_rx_buf),
502 usb_rx_callback, context, context->rx_endpoint->bInterval);
503
504 retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
505
506 if (retval)
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700507 dev_err(&context->dev->dev,
Haneen Mohammed8c6ccbe2015-03-06 22:00:10 +0300508 "usb_submit_urb failed for ir_open (%d)\n", retval);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300509 else {
510 context->ir_isopen = 1;
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300511 dev_info(&context->dev->dev, "IR port opened\n");
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300512 }
513
514exit:
515 mutex_unlock(&context->ctx_lock);
516
517 mutex_unlock(&disconnect_lock);
Julia Lawall4d9db972010-08-16 18:26:36 +0200518 return retval;
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300519}
520
521/**
522 * Called by lirc_dev when the application closes /dev/lirc
523 */
524static void ir_close(void *data)
525{
526 struct sasem_context *context;
527
Tapasweni Pathakd82e62d2014-10-30 17:02:48 +0530528 context = data;
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300529 if (!context) {
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300530 pr_err("%s: no context for device\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300531 return;
532 }
533
534 mutex_lock(&context->ctx_lock);
535
536 usb_kill_urb(context->rx_urb);
537 context->ir_isopen = 0;
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300538 pr_info("IR port closed\n");
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300539
540 if (!context->dev_present) {
541
542 /*
543 * Device disconnected while IR port was
544 * still open. Driver was not deregistered
545 * at disconnect time, so do it now.
546 */
547 deregister_from_lirc(context);
548
549 if (!context->vfd_isopen) {
550
551 mutex_unlock(&context->ctx_lock);
552 delete_context(context);
553 return;
554 }
555 /* If VFD port is open, context will be deleted by vfd_close */
556 }
557
558 mutex_unlock(&context->ctx_lock);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300559}
560
561/**
562 * Process the incoming packet
563 */
564static void incoming_packet(struct sasem_context *context,
565 struct urb *urb)
566{
567 int len = urb->actual_length;
568 unsigned char *buf = urb->transfer_buffer;
569 long ms;
570 struct timeval tv;
571
572 if (len != 8) {
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300573 dev_warn(&context->dev->dev,
574 "%s: invalid incoming packet size (%d)\n",
575 __func__, len);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300576 return;
577 }
578
Amber Thrallf3a75502014-10-02 23:33:30 -0300579 if (debug)
580 dev_info(&context->dev->dev, "Incoming data: %*ph\n", len, buf);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300581 /*
582 * Lirc could deal with the repeat code, but we really need to block it
583 * if it arrives too late. Otherwise we could repeat the wrong code.
584 */
585
586 /* get the time since the last button press */
587 do_gettimeofday(&tv);
588 ms = (tv.tv_sec - context->presstime.tv_sec) * 1000 +
589 (tv.tv_usec - context->presstime.tv_usec) / 1000;
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 */
603 if ((ms < 250) && (context->codesaved != 0)) {
604 memcpy(buf, &context->lastcode, 8);
605 context->presstime.tv_sec = tv.tv_sec;
606 context->presstime.tv_usec = tv.tv_usec;
607 }
608 } else {
609 /* save the current valid code for repeats */
610 memcpy(&context->lastcode, buf, 8);
611 /*
612 * set flag to signal a valid code was save;
613 * just for safety reasons
614 */
615 context->codesaved = 1;
616 context->presstime.tv_sec = tv.tv_sec;
617 context->presstime.tv_usec = tv.tv_usec;
618 }
619
620 lirc_buffer_write(context->driver->rbuf, buf);
621 wake_up(&context->driver->rbuf->wait_poll);
622}
623
624/**
625 * Callback function for USB core API: receive data
626 */
627static void usb_rx_callback(struct urb *urb)
628{
629 struct sasem_context *context;
630
631 if (!urb)
632 return;
633 context = (struct sasem_context *) urb->context;
634 if (!context)
635 return;
636
637 switch (urb->status) {
638
639 case -ENOENT: /* usbcore unlink successful! */
640 return;
641
642 case 0:
643 if (context->ir_isopen)
644 incoming_packet(context, urb);
645 break;
646
647 default:
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300648 dev_warn(&urb->dev->dev, "%s: status (%d): ignored",
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300649 __func__, urb->status);
650 break;
651 }
652
653 usb_submit_urb(context->rx_urb, GFP_ATOMIC);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300654}
655
656
657
658/**
659 * Callback function for USB core API: Probe
660 */
661static int sasem_probe(struct usb_interface *interface,
662 const struct usb_device_id *id)
663{
664 struct usb_device *dev = NULL;
665 struct usb_host_interface *iface_desc = NULL;
666 struct usb_endpoint_descriptor *rx_endpoint = NULL;
667 struct usb_endpoint_descriptor *tx_endpoint = NULL;
668 struct urb *rx_urb = NULL;
669 struct urb *tx_urb = NULL;
670 struct lirc_driver *driver = NULL;
671 struct lirc_buffer *rbuf = NULL;
672 int lirc_minor = 0;
673 int num_endpoints;
674 int retval = 0;
675 int vfd_ep_found;
676 int ir_ep_found;
677 int alloc_status;
678 struct sasem_context *context = NULL;
679 int i;
680
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700681 dev_info(&interface->dev, "%s: found Sasem device\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300682
683
684 dev = usb_get_dev(interface_to_usbdev(interface));
685 iface_desc = interface->cur_altsetting;
686 num_endpoints = iface_desc->desc.bNumEndpoints;
687
688 /*
689 * Scan the endpoint list and set:
690 * first input endpoint = IR endpoint
691 * first output endpoint = VFD endpoint
692 */
693
694 ir_ep_found = 0;
695 vfd_ep_found = 0;
696
697 for (i = 0; i < num_endpoints && !(ir_ep_found && vfd_ep_found); ++i) {
698
699 struct usb_endpoint_descriptor *ep;
700 int ep_dir;
701 int ep_type;
Gulsah Kose3f802802014-09-21 01:02:21 +0300702
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300703 ep = &iface_desc->endpoint [i].desc;
704 ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
705 ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
706
707 if (!ir_ep_found &&
708 ep_dir == USB_DIR_IN &&
709 ep_type == USB_ENDPOINT_XFER_INT) {
710
711 rx_endpoint = ep;
712 ir_ep_found = 1;
713 if (debug)
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700714 dev_info(&interface->dev,
715 "%s: found IR endpoint\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300716
717 } else if (!vfd_ep_found &&
718 ep_dir == USB_DIR_OUT &&
719 ep_type == USB_ENDPOINT_XFER_INT) {
720
721 tx_endpoint = ep;
722 vfd_ep_found = 1;
723 if (debug)
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700724 dev_info(&interface->dev,
725 "%s: found VFD endpoint\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300726 }
727 }
728
729 /* Input endpoint is mandatory */
730 if (!ir_ep_found) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700731 dev_err(&interface->dev,
732 "%s: no valid input (IR) endpoint found.\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300733 retval = -ENODEV;
734 goto exit;
735 }
736
737 if (!vfd_ep_found)
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700738 dev_info(&interface->dev,
739 "%s: no valid output (VFD) endpoint found.\n",
740 __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300741
742
743 /* Allocate memory */
744 alloc_status = 0;
745
746 context = kzalloc(sizeof(struct sasem_context), GFP_KERNEL);
747 if (!context) {
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300748 alloc_status = 1;
749 goto alloc_status_switch;
750 }
751 driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
752 if (!driver) {
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300753 alloc_status = 2;
754 goto alloc_status_switch;
755 }
756 rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
757 if (!rbuf) {
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300758 alloc_status = 3;
759 goto alloc_status_switch;
760 }
761 if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700762 dev_err(&interface->dev,
763 "%s: lirc_buffer_init failed\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300764 alloc_status = 4;
765 goto alloc_status_switch;
766 }
767 rx_urb = usb_alloc_urb(0, GFP_KERNEL);
768 if (!rx_urb) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700769 dev_err(&interface->dev,
770 "%s: usb_alloc_urb failed for IR urb\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300771 alloc_status = 5;
772 goto alloc_status_switch;
773 }
774 if (vfd_ep_found) {
775 tx_urb = usb_alloc_urb(0, GFP_KERNEL);
776 if (!tx_urb) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700777 dev_err(&interface->dev,
778 "%s: usb_alloc_urb failed for VFD urb",
779 __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300780 alloc_status = 6;
781 goto alloc_status_switch;
782 }
783 }
784
785 mutex_init(&context->ctx_lock);
786
787 strcpy(driver->name, MOD_NAME);
788 driver->minor = -1;
789 driver->code_length = 64;
790 driver->sample_rate = 0;
791 driver->features = LIRC_CAN_REC_LIRCCODE;
792 driver->data = context;
793 driver->rbuf = rbuf;
794 driver->set_use_inc = ir_open;
795 driver->set_use_dec = ir_close;
796 driver->dev = &interface->dev;
797 driver->owner = THIS_MODULE;
798
799 mutex_lock(&context->ctx_lock);
800
801 lirc_minor = lirc_register_driver(driver);
802 if (lirc_minor < 0) {
Greg Kroah-Hartman17122542012-04-23 17:27:48 -0700803 dev_err(&interface->dev,
804 "%s: lirc_register_driver failed\n", __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300805 alloc_status = 7;
Jiri Slabyff7d3682010-09-04 14:16:26 +0200806 retval = lirc_minor;
807 goto unlock;
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300808 } else
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300809 dev_info(&interface->dev,
810 "%s: Registered Sasem driver (minor:%d)\n",
811 __func__, lirc_minor);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300812
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300813 /* Needed while unregistering! */
814 driver->minor = lirc_minor;
815
816 context->dev = dev;
817 context->dev_present = 1;
818 context->rx_endpoint = rx_endpoint;
819 context->rx_urb = rx_urb;
820 if (vfd_ep_found) {
821 context->tx_endpoint = tx_endpoint;
822 context->tx_urb = tx_urb;
823 context->vfd_contrast = 1000; /* range 0 - 1000 */
824 }
825 context->driver = driver;
826
827 usb_set_intfdata(interface, context);
828
829 if (vfd_ep_found) {
830
831 if (debug)
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300832 dev_info(&interface->dev,
833 "Registering VFD with sysfs\n");
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300834 if (usb_register_dev(interface, &sasem_class))
835 /* Not a fatal error, so ignore */
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300836 dev_info(&interface->dev,
837 "%s: could not get a minor number for VFD\n",
838 __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300839 }
840
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300841 dev_info(&interface->dev,
842 "%s: Sasem device on usb<%d:%d> initialized\n",
843 __func__, dev->bus->busnum, dev->devnum);
Jiri Slabyff7d3682010-09-04 14:16:26 +0200844unlock:
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300845 mutex_unlock(&context->ctx_lock);
Alexey Khoroshilov06b3f442011-08-30 00:54:21 +0400846
847alloc_status_switch:
848 switch (alloc_status) {
849
850 case 7:
851 if (vfd_ep_found)
852 usb_free_urb(tx_urb);
853 case 6:
854 usb_free_urb(rx_urb);
Andreea-Cristina Bernat5a9e30e2014-03-12 22:50:01 +0200855 /* fall-through */
Alexey Khoroshilov06b3f442011-08-30 00:54:21 +0400856 case 5:
857 lirc_buffer_free(rbuf);
Andreea-Cristina Bernat5a9e30e2014-03-12 22:50:01 +0200858 /* fall-through */
Alexey Khoroshilov06b3f442011-08-30 00:54:21 +0400859 case 4:
860 kfree(rbuf);
Andreea-Cristina Bernat5a9e30e2014-03-12 22:50:01 +0200861 /* fall-through */
Alexey Khoroshilov06b3f442011-08-30 00:54:21 +0400862 case 3:
863 kfree(driver);
Andreea-Cristina Bernat5a9e30e2014-03-12 22:50:01 +0200864 /* fall-through */
Alexey Khoroshilov06b3f442011-08-30 00:54:21 +0400865 case 2:
866 kfree(context);
867 context = NULL;
Andreea-Cristina Bernat5a9e30e2014-03-12 22:50:01 +0200868 /* fall-through */
Alexey Khoroshilov06b3f442011-08-30 00:54:21 +0400869 case 1:
870 if (retval == 0)
871 retval = -ENOMEM;
872 }
873
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300874exit:
875 return retval;
876}
877
878/**
Masanari Iida6dc8f382012-10-31 11:52:45 -0300879 * Callback function for USB core API: disconnect
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300880 */
881static void sasem_disconnect(struct usb_interface *interface)
882{
883 struct sasem_context *context;
884
885 /* prevent races with ir_open()/vfd_open() */
886 mutex_lock(&disconnect_lock);
887
888 context = usb_get_intfdata(interface);
889 mutex_lock(&context->ctx_lock);
890
YAMANE Toshiakie174e6c2012-11-08 15:52:49 -0300891 dev_info(&interface->dev, "%s: Sasem device disconnected\n",
892 __func__);
Jarod Wilsonc5ac4572010-07-26 20:31:11 -0300893
894 usb_set_intfdata(interface, NULL);
895 context->dev_present = 0;
896
897 /* Stop reception */
898 usb_kill_urb(context->rx_urb);
899
900 /* Abort ongoing write */
901 if (atomic_read(&context->tx.busy)) {
902
903 usb_kill_urb(context->tx_urb);
904 wait_for_completion(&context->tx.finished);
905 }
906
907 /* De-register from lirc_dev if IR port is not open */
908 if (!context->ir_isopen)
909 deregister_from_lirc(context);
910
911 usb_deregister_dev(interface, &sasem_class);
912
913 mutex_unlock(&context->ctx_lock);
914
915 if (!context->ir_isopen && !context->vfd_isopen)
916 delete_context(context);
917
918 mutex_unlock(&disconnect_lock);
919}
920
Greg Kroah-Hartmanbac2c122011-11-18 09:42:11 -0800921module_usb_driver(sasem_driver);