xref: /linux/drivers/input/misc/keyspan_remote.c (revision 32235b07b52e948900613e27c5cbd955d084ae8a)
1 /*
2  * keyspan_remote: USB driver for the Keyspan DMR
3  *
4  * Copyright (C) 2005 Zymeta Corporation - Michael Downey (downey@zymeta.com)
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation, version 2.
9  *
10  * This driver has been put together with the support of Innosys, Inc.
11  * and Keyspan, Inc the manufacturers of the Keyspan USB DMR product.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/usb/input.h>
20 
21 #define DRIVER_VERSION	"v0.1"
22 #define DRIVER_AUTHOR	"Michael Downey <downey@zymeta.com>"
23 #define DRIVER_DESC	"Driver for the USB Keyspan remote control."
24 #define DRIVER_LICENSE	"GPL"
25 
26 /* Parameters that can be passed to the driver. */
27 static int debug;
28 module_param(debug, int, 0444);
29 MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
30 
31 /* Vendor and product ids */
32 #define USB_KEYSPAN_VENDOR_ID		0x06CD
33 #define USB_KEYSPAN_PRODUCT_UIA11	0x0202
34 
35 /* Defines for converting the data from the remote. */
36 #define ZERO		0x18
37 #define ZERO_MASK	0x1F	/* 5 bits for a 0 */
38 #define ONE		0x3C
39 #define ONE_MASK	0x3F	/* 6 bits for a 1 */
40 #define SYNC		0x3F80
41 #define SYNC_MASK	0x3FFF	/* 14 bits for a SYNC sequence */
42 #define STOP		0x00
43 #define STOP_MASK	0x1F	/* 5 bits for the STOP sequence */
44 #define GAP		0xFF
45 
46 #define RECV_SIZE	8	/* The UIA-11 type have a 8 byte limit. */
47 
48 /*
49  * Table that maps the 31 possible keycodes to input keys.
50  * Currently there are 15 and 17 button models so RESERVED codes
51  * are blank areas in the mapping.
52  */
53 static const unsigned short keyspan_key_table[] = {
54 	KEY_RESERVED,		/* 0 is just a place holder. */
55 	KEY_RESERVED,
56 	KEY_STOP,
57 	KEY_PLAYCD,
58 	KEY_RESERVED,
59 	KEY_PREVIOUSSONG,
60 	KEY_REWIND,
61 	KEY_FORWARD,
62 	KEY_NEXTSONG,
63 	KEY_RESERVED,
64 	KEY_RESERVED,
65 	KEY_RESERVED,
66 	KEY_PAUSE,
67 	KEY_VOLUMEUP,
68 	KEY_RESERVED,
69 	KEY_RESERVED,
70 	KEY_RESERVED,
71 	KEY_VOLUMEDOWN,
72 	KEY_RESERVED,
73 	KEY_UP,
74 	KEY_RESERVED,
75 	KEY_MUTE,
76 	KEY_LEFT,
77 	KEY_ENTER,
78 	KEY_RIGHT,
79 	KEY_RESERVED,
80 	KEY_RESERVED,
81 	KEY_DOWN,
82 	KEY_RESERVED,
83 	KEY_KPASTERISK,
84 	KEY_RESERVED,
85 	KEY_MENU
86 };
87 
88 /* table of devices that work with this driver */
89 static struct usb_device_id keyspan_table[] = {
90 	{ USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
91 	{ }					/* Terminating entry */
92 };
93 
94 /* Structure to store all the real stuff that a remote sends to us. */
95 struct keyspan_message {
96 	u16	system;
97 	u8	button;
98 	u8	toggle;
99 };
100 
101 /* Structure used for all the bit testing magic needed to be done. */
102 struct bit_tester {
103 	u32	tester;
104 	int	len;
105 	int	pos;
106 	int	bits_left;
107 	u8	buffer[32];
108 };
109 
110 /* Structure to hold all of our driver specific stuff */
111 struct usb_keyspan {
112 	char				name[128];
113 	char				phys[64];
114 	unsigned short			keymap[ARRAY_SIZE(keyspan_key_table)];
115 	struct usb_device		*udev;
116 	struct input_dev		*input;
117 	struct usb_interface		*interface;
118 	struct usb_endpoint_descriptor	*in_endpoint;
119 	struct urb*			irq_urb;
120 	int				open;
121 	dma_addr_t			in_dma;
122 	unsigned char			*in_buffer;
123 
124 	/* variables used to parse messages from remote. */
125 	struct bit_tester		data;
126 	int				stage;
127 	int				toggle;
128 };
129 
130 static struct usb_driver keyspan_driver;
131 
132 /*
133  * Debug routine that prints out what we've received from the remote.
134  */
135 static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
136 {
137 	char codes[4 * RECV_SIZE];
138 	int i;
139 
140 	for (i = 0; i < RECV_SIZE; i++)
141 		snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]);
142 
143 	dev_info(&dev->udev->dev, "%s\n", codes);
144 }
145 
146 /*
147  * Routine that manages the bit_tester structure.  It makes sure that there are
148  * at least bits_needed bits loaded into the tester.
149  */
150 static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
151 {
152 	if (dev->data.bits_left >= bits_needed)
153 		return 0;
154 
155 	/*
156 	 * Somehow we've missed the last message. The message will be repeated
157 	 * though so it's not too big a deal
158 	 */
159 	if (dev->data.pos >= dev->data.len) {
160 		dev_dbg(&dev->udev->dev,
161 			"%s - Error ran out of data. pos: %d, len: %d\n",
162 			__func__, dev->data.pos, dev->data.len);
163 		return -1;
164 	}
165 
166 	/* Load as much as we can into the tester. */
167 	while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
168 	       (dev->data.pos < dev->data.len)) {
169 		dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
170 		dev->data.bits_left += 8;
171 	}
172 
173 	return 0;
174 }
175 
176 static void keyspan_report_button(struct usb_keyspan *remote, int button, int press)
177 {
178 	struct input_dev *input = remote->input;
179 
180 	input_event(input, EV_MSC, MSC_SCAN, button);
181 	input_report_key(input, remote->keymap[button], press);
182 	input_sync(input);
183 }
184 
185 /*
186  * Routine that handles all the logic needed to parse out the message from the remote.
187  */
188 static void keyspan_check_data(struct usb_keyspan *remote)
189 {
190 	int i;
191 	int found = 0;
192 	struct keyspan_message message;
193 
194 	switch(remote->stage) {
195 	case 0:
196 		/*
197 		 * In stage 0 we want to find the start of a message.  The remote sends a 0xFF as filler.
198 		 * So the first byte that isn't a FF should be the start of a new message.
199 		 */
200 		for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
201 
202 		if (i < RECV_SIZE) {
203 			memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
204 			remote->data.len = RECV_SIZE;
205 			remote->data.pos = 0;
206 			remote->data.tester = 0;
207 			remote->data.bits_left = 0;
208 			remote->stage = 1;
209 		}
210 		break;
211 
212 	case 1:
213 		/*
214 		 * Stage 1 we should have 16 bytes and should be able to detect a
215 		 * SYNC.  The SYNC is 14 bits, 7 0's and then 7 1's.
216 		 */
217 		memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
218 		remote->data.len += RECV_SIZE;
219 
220 		found = 0;
221 		while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
222 			for (i = 0; i < 8; ++i) {
223 				if (keyspan_load_tester(remote, 14) != 0) {
224 					remote->stage = 0;
225 					return;
226 				}
227 
228 				if ((remote->data.tester & SYNC_MASK) == SYNC) {
229 					remote->data.tester = remote->data.tester >> 14;
230 					remote->data.bits_left -= 14;
231 					found = 1;
232 					break;
233 				} else {
234 					remote->data.tester = remote->data.tester >> 1;
235 					--remote->data.bits_left;
236 				}
237 			}
238 		}
239 
240 		if (!found) {
241 			remote->stage = 0;
242 			remote->data.len = 0;
243 		} else {
244 			remote->stage = 2;
245 		}
246 		break;
247 
248 	case 2:
249 		/*
250 		 * Stage 2 we should have 24 bytes which will be enough for a full
251 		 * message.  We need to parse out the system code, button code,
252 		 * toggle code, and stop.
253 		 */
254 		memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
255 		remote->data.len += RECV_SIZE;
256 
257 		message.system = 0;
258 		for (i = 0; i < 9; i++) {
259 			keyspan_load_tester(remote, 6);
260 
261 			if ((remote->data.tester & ZERO_MASK) == ZERO) {
262 				message.system = message.system << 1;
263 				remote->data.tester = remote->data.tester >> 5;
264 				remote->data.bits_left -= 5;
265 			} else if ((remote->data.tester & ONE_MASK) == ONE) {
266 				message.system = (message.system << 1) + 1;
267 				remote->data.tester = remote->data.tester >> 6;
268 				remote->data.bits_left -= 6;
269 			} else {
270 				err("%s - Unknown sequence found in system data.\n", __func__);
271 				remote->stage = 0;
272 				return;
273 			}
274 		}
275 
276 		message.button = 0;
277 		for (i = 0; i < 5; i++) {
278 			keyspan_load_tester(remote, 6);
279 
280 			if ((remote->data.tester & ZERO_MASK) == ZERO) {
281 				message.button = message.button << 1;
282 				remote->data.tester = remote->data.tester >> 5;
283 				remote->data.bits_left -= 5;
284 			} else if ((remote->data.tester & ONE_MASK) == ONE) {
285 				message.button = (message.button << 1) + 1;
286 				remote->data.tester = remote->data.tester >> 6;
287 				remote->data.bits_left -= 6;
288 			} else {
289 				err("%s - Unknown sequence found in button data.\n", __func__);
290 				remote->stage = 0;
291 				return;
292 			}
293 		}
294 
295 		keyspan_load_tester(remote, 6);
296 		if ((remote->data.tester & ZERO_MASK) == ZERO) {
297 			message.toggle = 0;
298 			remote->data.tester = remote->data.tester >> 5;
299 			remote->data.bits_left -= 5;
300 		} else if ((remote->data.tester & ONE_MASK) == ONE) {
301 			message.toggle = 1;
302 			remote->data.tester = remote->data.tester >> 6;
303 			remote->data.bits_left -= 6;
304 		} else {
305 			err("%s - Error in message, invalid toggle.\n", __func__);
306 			remote->stage = 0;
307 			return;
308 		}
309 
310 		keyspan_load_tester(remote, 5);
311 		if ((remote->data.tester & STOP_MASK) == STOP) {
312 			remote->data.tester = remote->data.tester >> 5;
313 			remote->data.bits_left -= 5;
314 		} else {
315 			err("Bad message received, no stop bit found.\n");
316 		}
317 
318 		dev_dbg(&remote->udev->dev,
319 			"%s found valid message: system: %d, button: %d, toggle: %d\n",
320 			__func__, message.system, message.button, message.toggle);
321 
322 		if (message.toggle != remote->toggle) {
323 			keyspan_report_button(remote, message.button, 1);
324 			keyspan_report_button(remote, message.button, 0);
325 			remote->toggle = message.toggle;
326 		}
327 
328 		remote->stage = 0;
329 		break;
330 	}
331 }
332 
333 /*
334  * Routine for sending all the initialization messages to the remote.
335  */
336 static int keyspan_setup(struct usb_device* dev)
337 {
338 	int retval = 0;
339 
340 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
341 				 0x11, 0x40, 0x5601, 0x0, NULL, 0, 0);
342 	if (retval) {
343 		dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
344 			__func__, retval);
345 		return(retval);
346 	}
347 
348 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
349 				 0x44, 0x40, 0x0, 0x0, NULL, 0, 0);
350 	if (retval) {
351 		dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
352 			__func__, retval);
353 		return(retval);
354 	}
355 
356 	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
357 				 0x22, 0x40, 0x0, 0x0, NULL, 0, 0);
358 	if (retval) {
359 		dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
360 			__func__, retval);
361 		return(retval);
362 	}
363 
364 	dev_dbg(&dev->dev, "%s - Setup complete.\n", __func__);
365 	return(retval);
366 }
367 
368 /*
369  * Routine used to handle a new message that has come in.
370  */
371 static void keyspan_irq_recv(struct urb *urb)
372 {
373 	struct usb_keyspan *dev = urb->context;
374 	int retval;
375 
376 	/* Check our status in case we need to bail out early. */
377 	switch (urb->status) {
378 	case 0:
379 		break;
380 
381 	/* Device went away so don't keep trying to read from it. */
382 	case -ECONNRESET:
383 	case -ENOENT:
384 	case -ESHUTDOWN:
385 		return;
386 
387 	default:
388 		goto resubmit;
389 		break;
390 	}
391 
392 	if (debug)
393 		keyspan_print(dev);
394 
395 	keyspan_check_data(dev);
396 
397 resubmit:
398 	retval = usb_submit_urb(urb, GFP_ATOMIC);
399 	if (retval)
400 		err ("%s - usb_submit_urb failed with result: %d", __func__, retval);
401 }
402 
403 static int keyspan_open(struct input_dev *dev)
404 {
405 	struct usb_keyspan *remote = input_get_drvdata(dev);
406 
407 	remote->irq_urb->dev = remote->udev;
408 	if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
409 		return -EIO;
410 
411 	return 0;
412 }
413 
414 static void keyspan_close(struct input_dev *dev)
415 {
416 	struct usb_keyspan *remote = input_get_drvdata(dev);
417 
418 	usb_kill_urb(remote->irq_urb);
419 }
420 
421 static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
422 {
423 
424 	struct usb_endpoint_descriptor *endpoint;
425 	int i;
426 
427 	for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
428 		endpoint = &iface->endpoint[i].desc;
429 
430 		if (usb_endpoint_is_int_in(endpoint)) {
431 			/* we found our interrupt in endpoint */
432 			return endpoint;
433 		}
434 	}
435 
436 	return NULL;
437 }
438 
439 /*
440  * Routine that sets up the driver to handle a specific USB device detected on the bus.
441  */
442 static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
443 {
444 	struct usb_device *udev = interface_to_usbdev(interface);
445 	struct usb_endpoint_descriptor *endpoint;
446 	struct usb_keyspan *remote;
447 	struct input_dev *input_dev;
448 	int i, error;
449 
450 	endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
451 	if (!endpoint)
452 		return -ENODEV;
453 
454 	remote = kzalloc(sizeof(*remote), GFP_KERNEL);
455 	input_dev = input_allocate_device();
456 	if (!remote || !input_dev) {
457 		error = -ENOMEM;
458 		goto fail1;
459 	}
460 
461 	remote->udev = udev;
462 	remote->input = input_dev;
463 	remote->interface = interface;
464 	remote->in_endpoint = endpoint;
465 	remote->toggle = -1;	/* Set to -1 so we will always not match the toggle from the first remote message. */
466 
467 	remote->in_buffer = usb_alloc_coherent(udev, RECV_SIZE, GFP_ATOMIC, &remote->in_dma);
468 	if (!remote->in_buffer) {
469 		error = -ENOMEM;
470 		goto fail1;
471 	}
472 
473 	remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
474 	if (!remote->irq_urb) {
475 		error = -ENOMEM;
476 		goto fail2;
477 	}
478 
479 	error = keyspan_setup(udev);
480 	if (error) {
481 		error = -ENODEV;
482 		goto fail3;
483 	}
484 
485 	if (udev->manufacturer)
486 		strlcpy(remote->name, udev->manufacturer, sizeof(remote->name));
487 
488 	if (udev->product) {
489 		if (udev->manufacturer)
490 			strlcat(remote->name, " ", sizeof(remote->name));
491 		strlcat(remote->name, udev->product, sizeof(remote->name));
492 	}
493 
494 	if (!strlen(remote->name))
495 		snprintf(remote->name, sizeof(remote->name),
496 			 "USB Keyspan Remote %04x:%04x",
497 			 le16_to_cpu(udev->descriptor.idVendor),
498 			 le16_to_cpu(udev->descriptor.idProduct));
499 
500 	usb_make_path(udev, remote->phys, sizeof(remote->phys));
501 	strlcat(remote->phys, "/input0", sizeof(remote->phys));
502 	memcpy(remote->keymap, keyspan_key_table, sizeof(remote->keymap));
503 
504 	input_dev->name = remote->name;
505 	input_dev->phys = remote->phys;
506 	usb_to_input_id(udev, &input_dev->id);
507 	input_dev->dev.parent = &interface->dev;
508 	input_dev->keycode = remote->keymap;
509 	input_dev->keycodesize = sizeof(unsigned short);
510 	input_dev->keycodemax = ARRAY_SIZE(remote->keymap);
511 
512 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
513 	__set_bit(EV_KEY, input_dev->evbit);
514 	for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
515 		__set_bit(keyspan_key_table[i], input_dev->keybit);
516 	__clear_bit(KEY_RESERVED, input_dev->keybit);
517 
518 	input_set_drvdata(input_dev, remote);
519 
520 	input_dev->open = keyspan_open;
521 	input_dev->close = keyspan_close;
522 
523 	/*
524 	 * Initialize the URB to access the device.
525 	 * The urb gets sent to the device in keyspan_open()
526 	 */
527 	usb_fill_int_urb(remote->irq_urb,
528 			 remote->udev,
529 			 usb_rcvintpipe(remote->udev, endpoint->bEndpointAddress),
530 			 remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
531 			 endpoint->bInterval);
532 	remote->irq_urb->transfer_dma = remote->in_dma;
533 	remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
534 
535 	/* we can register the device now, as it is ready */
536 	error = input_register_device(remote->input);
537 	if (error)
538 		goto fail3;
539 
540 	/* save our data pointer in this interface device */
541 	usb_set_intfdata(interface, remote);
542 
543 	return 0;
544 
545  fail3:	usb_free_urb(remote->irq_urb);
546  fail2:	usb_free_coherent(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
547  fail1:	kfree(remote);
548 	input_free_device(input_dev);
549 
550 	return error;
551 }
552 
553 /*
554  * Routine called when a device is disconnected from the USB.
555  */
556 static void keyspan_disconnect(struct usb_interface *interface)
557 {
558 	struct usb_keyspan *remote;
559 
560 	remote = usb_get_intfdata(interface);
561 	usb_set_intfdata(interface, NULL);
562 
563 	if (remote) {	/* We have a valid driver structure so clean up everything we allocated. */
564 		input_unregister_device(remote->input);
565 		usb_kill_urb(remote->irq_urb);
566 		usb_free_urb(remote->irq_urb);
567 		usb_free_coherent(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
568 		kfree(remote);
569 	}
570 }
571 
572 /*
573  * Standard driver set up sections
574  */
575 static struct usb_driver keyspan_driver =
576 {
577 	.name =		"keyspan_remote",
578 	.probe =	keyspan_probe,
579 	.disconnect =	keyspan_disconnect,
580 	.id_table =	keyspan_table
581 };
582 
583 static int __init usb_keyspan_init(void)
584 {
585 	int result;
586 
587 	/* register this driver with the USB subsystem */
588 	result = usb_register(&keyspan_driver);
589 	if (result)
590 		err("usb_register failed. Error number %d\n", result);
591 
592 	return result;
593 }
594 
595 static void __exit usb_keyspan_exit(void)
596 {
597 	/* deregister this driver with the USB subsystem */
598 	usb_deregister(&keyspan_driver);
599 }
600 
601 module_init(usb_keyspan_init);
602 module_exit(usb_keyspan_exit);
603 
604 MODULE_DEVICE_TABLE(usb, keyspan_table);
605 MODULE_AUTHOR(DRIVER_AUTHOR);
606 MODULE_DESCRIPTION(DRIVER_DESC);
607 MODULE_LICENSE(DRIVER_LICENSE);
608