1a10e763bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ba0acb5eSDmitry Torokhov /*
3ba0acb5eSDmitry Torokhov * keyspan_remote: USB driver for the Keyspan DMR
4ba0acb5eSDmitry Torokhov *
5ba0acb5eSDmitry Torokhov * Copyright (C) 2005 Zymeta Corporation - Michael Downey (downey@zymeta.com)
6ba0acb5eSDmitry Torokhov *
7ba0acb5eSDmitry Torokhov * This driver has been put together with the support of Innosys, Inc.
8ba0acb5eSDmitry Torokhov * and Keyspan, Inc the manufacturers of the Keyspan USB DMR product.
9ba0acb5eSDmitry Torokhov */
10ba0acb5eSDmitry Torokhov
11ba0acb5eSDmitry Torokhov #include <linux/kernel.h>
12ba0acb5eSDmitry Torokhov #include <linux/errno.h>
13ba0acb5eSDmitry Torokhov #include <linux/slab.h>
14ba0acb5eSDmitry Torokhov #include <linux/module.h>
15ba0acb5eSDmitry Torokhov #include <linux/usb/input.h>
16ba0acb5eSDmitry Torokhov
17ba0acb5eSDmitry Torokhov /* Parameters that can be passed to the driver. */
18ba0acb5eSDmitry Torokhov static int debug;
19ba0acb5eSDmitry Torokhov module_param(debug, int, 0444);
20ba0acb5eSDmitry Torokhov MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
21ba0acb5eSDmitry Torokhov
22ba0acb5eSDmitry Torokhov /* Vendor and product ids */
23ba0acb5eSDmitry Torokhov #define USB_KEYSPAN_VENDOR_ID 0x06CD
24ba0acb5eSDmitry Torokhov #define USB_KEYSPAN_PRODUCT_UIA11 0x0202
25ba0acb5eSDmitry Torokhov
26ba0acb5eSDmitry Torokhov /* Defines for converting the data from the remote. */
27ba0acb5eSDmitry Torokhov #define ZERO 0x18
28ba0acb5eSDmitry Torokhov #define ZERO_MASK 0x1F /* 5 bits for a 0 */
29ba0acb5eSDmitry Torokhov #define ONE 0x3C
30ba0acb5eSDmitry Torokhov #define ONE_MASK 0x3F /* 6 bits for a 1 */
31ba0acb5eSDmitry Torokhov #define SYNC 0x3F80
32ba0acb5eSDmitry Torokhov #define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */
33ba0acb5eSDmitry Torokhov #define STOP 0x00
34ba0acb5eSDmitry Torokhov #define STOP_MASK 0x1F /* 5 bits for the STOP sequence */
35ba0acb5eSDmitry Torokhov #define GAP 0xFF
36ba0acb5eSDmitry Torokhov
37ba0acb5eSDmitry Torokhov #define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */
38ba0acb5eSDmitry Torokhov
39ba0acb5eSDmitry Torokhov /*
40ba0acb5eSDmitry Torokhov * Table that maps the 31 possible keycodes to input keys.
41ba0acb5eSDmitry Torokhov * Currently there are 15 and 17 button models so RESERVED codes
42ba0acb5eSDmitry Torokhov * are blank areas in the mapping.
43ba0acb5eSDmitry Torokhov */
441953ea2dSDmitry Torokhov static const unsigned short keyspan_key_table[] = {
45ba0acb5eSDmitry Torokhov KEY_RESERVED, /* 0 is just a place holder. */
46ba0acb5eSDmitry Torokhov KEY_RESERVED,
47ba0acb5eSDmitry Torokhov KEY_STOP,
48ba0acb5eSDmitry Torokhov KEY_PLAYCD,
49ba0acb5eSDmitry Torokhov KEY_RESERVED,
50ba0acb5eSDmitry Torokhov KEY_PREVIOUSSONG,
51ba0acb5eSDmitry Torokhov KEY_REWIND,
52ba0acb5eSDmitry Torokhov KEY_FORWARD,
53ba0acb5eSDmitry Torokhov KEY_NEXTSONG,
54ba0acb5eSDmitry Torokhov KEY_RESERVED,
55ba0acb5eSDmitry Torokhov KEY_RESERVED,
56ba0acb5eSDmitry Torokhov KEY_RESERVED,
57ba0acb5eSDmitry Torokhov KEY_PAUSE,
58ba0acb5eSDmitry Torokhov KEY_VOLUMEUP,
59ba0acb5eSDmitry Torokhov KEY_RESERVED,
60ba0acb5eSDmitry Torokhov KEY_RESERVED,
61ba0acb5eSDmitry Torokhov KEY_RESERVED,
62ba0acb5eSDmitry Torokhov KEY_VOLUMEDOWN,
63ba0acb5eSDmitry Torokhov KEY_RESERVED,
64ba0acb5eSDmitry Torokhov KEY_UP,
65ba0acb5eSDmitry Torokhov KEY_RESERVED,
66ba0acb5eSDmitry Torokhov KEY_MUTE,
67ba0acb5eSDmitry Torokhov KEY_LEFT,
68ba0acb5eSDmitry Torokhov KEY_ENTER,
69ba0acb5eSDmitry Torokhov KEY_RIGHT,
70ba0acb5eSDmitry Torokhov KEY_RESERVED,
71ba0acb5eSDmitry Torokhov KEY_RESERVED,
72ba0acb5eSDmitry Torokhov KEY_DOWN,
73ba0acb5eSDmitry Torokhov KEY_RESERVED,
74ba0acb5eSDmitry Torokhov KEY_KPASTERISK,
75ba0acb5eSDmitry Torokhov KEY_RESERVED,
76ba0acb5eSDmitry Torokhov KEY_MENU
77ba0acb5eSDmitry Torokhov };
78ba0acb5eSDmitry Torokhov
791953ea2dSDmitry Torokhov /* table of devices that work with this driver */
800360b3f6SArvind Yadav static const struct usb_device_id keyspan_table[] = {
811953ea2dSDmitry Torokhov { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
821953ea2dSDmitry Torokhov { } /* Terminating entry */
831953ea2dSDmitry Torokhov };
841953ea2dSDmitry Torokhov
851953ea2dSDmitry Torokhov /* Structure to store all the real stuff that a remote sends to us. */
861953ea2dSDmitry Torokhov struct keyspan_message {
871953ea2dSDmitry Torokhov u16 system;
881953ea2dSDmitry Torokhov u8 button;
891953ea2dSDmitry Torokhov u8 toggle;
901953ea2dSDmitry Torokhov };
911953ea2dSDmitry Torokhov
921953ea2dSDmitry Torokhov /* Structure used for all the bit testing magic needed to be done. */
931953ea2dSDmitry Torokhov struct bit_tester {
941953ea2dSDmitry Torokhov u32 tester;
951953ea2dSDmitry Torokhov int len;
961953ea2dSDmitry Torokhov int pos;
971953ea2dSDmitry Torokhov int bits_left;
981953ea2dSDmitry Torokhov u8 buffer[32];
991953ea2dSDmitry Torokhov };
1001953ea2dSDmitry Torokhov
1011953ea2dSDmitry Torokhov /* Structure to hold all of our driver specific stuff */
1021953ea2dSDmitry Torokhov struct usb_keyspan {
1031953ea2dSDmitry Torokhov char name[128];
1041953ea2dSDmitry Torokhov char phys[64];
1051953ea2dSDmitry Torokhov unsigned short keymap[ARRAY_SIZE(keyspan_key_table)];
1061953ea2dSDmitry Torokhov struct usb_device *udev;
1071953ea2dSDmitry Torokhov struct input_dev *input;
1081953ea2dSDmitry Torokhov struct usb_interface *interface;
1091953ea2dSDmitry Torokhov struct usb_endpoint_descriptor *in_endpoint;
1101953ea2dSDmitry Torokhov struct urb* irq_urb;
1111953ea2dSDmitry Torokhov int open;
1121953ea2dSDmitry Torokhov dma_addr_t in_dma;
1131953ea2dSDmitry Torokhov unsigned char *in_buffer;
1141953ea2dSDmitry Torokhov
1151953ea2dSDmitry Torokhov /* variables used to parse messages from remote. */
1161953ea2dSDmitry Torokhov struct bit_tester data;
1171953ea2dSDmitry Torokhov int stage;
1181953ea2dSDmitry Torokhov int toggle;
1191953ea2dSDmitry Torokhov };
1201953ea2dSDmitry Torokhov
121ba0acb5eSDmitry Torokhov static struct usb_driver keyspan_driver;
122ba0acb5eSDmitry Torokhov
123ba0acb5eSDmitry Torokhov /*
124ba0acb5eSDmitry Torokhov * Debug routine that prints out what we've received from the remote.
125ba0acb5eSDmitry Torokhov */
keyspan_print(struct usb_keyspan * dev)126ba0acb5eSDmitry Torokhov static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
127ba0acb5eSDmitry Torokhov {
128ba0acb5eSDmitry Torokhov char codes[4 * RECV_SIZE];
129ba0acb5eSDmitry Torokhov int i;
130ba0acb5eSDmitry Torokhov
131ba0acb5eSDmitry Torokhov for (i = 0; i < RECV_SIZE; i++)
132ba0acb5eSDmitry Torokhov snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]);
133ba0acb5eSDmitry Torokhov
134ba0acb5eSDmitry Torokhov dev_info(&dev->udev->dev, "%s\n", codes);
135ba0acb5eSDmitry Torokhov }
136ba0acb5eSDmitry Torokhov
137ba0acb5eSDmitry Torokhov /*
138ba0acb5eSDmitry Torokhov * Routine that manages the bit_tester structure. It makes sure that there are
139ba0acb5eSDmitry Torokhov * at least bits_needed bits loaded into the tester.
140ba0acb5eSDmitry Torokhov */
keyspan_load_tester(struct usb_keyspan * dev,int bits_needed)141ba0acb5eSDmitry Torokhov static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
142ba0acb5eSDmitry Torokhov {
143ba0acb5eSDmitry Torokhov if (dev->data.bits_left >= bits_needed)
144ba0acb5eSDmitry Torokhov return 0;
145ba0acb5eSDmitry Torokhov
146ba0acb5eSDmitry Torokhov /*
147ba0acb5eSDmitry Torokhov * Somehow we've missed the last message. The message will be repeated
148ba0acb5eSDmitry Torokhov * though so it's not too big a deal
149ba0acb5eSDmitry Torokhov */
150ba0acb5eSDmitry Torokhov if (dev->data.pos >= dev->data.len) {
151419b1a11SGreg Kroah-Hartman dev_dbg(&dev->interface->dev,
152ba0acb5eSDmitry Torokhov "%s - Error ran out of data. pos: %d, len: %d\n",
153ea3e6c59SHarvey Harrison __func__, dev->data.pos, dev->data.len);
154ba0acb5eSDmitry Torokhov return -1;
155ba0acb5eSDmitry Torokhov }
156ba0acb5eSDmitry Torokhov
157ba0acb5eSDmitry Torokhov /* Load as much as we can into the tester. */
158ba0acb5eSDmitry Torokhov while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
159ba0acb5eSDmitry Torokhov (dev->data.pos < dev->data.len)) {
160ba0acb5eSDmitry Torokhov dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
161ba0acb5eSDmitry Torokhov dev->data.bits_left += 8;
162ba0acb5eSDmitry Torokhov }
163ba0acb5eSDmitry Torokhov
164ba0acb5eSDmitry Torokhov return 0;
165ba0acb5eSDmitry Torokhov }
166ba0acb5eSDmitry Torokhov
keyspan_report_button(struct usb_keyspan * remote,int button,int press)1671953ea2dSDmitry Torokhov static void keyspan_report_button(struct usb_keyspan *remote, int button, int press)
1681953ea2dSDmitry Torokhov {
1691953ea2dSDmitry Torokhov struct input_dev *input = remote->input;
1701953ea2dSDmitry Torokhov
1711953ea2dSDmitry Torokhov input_event(input, EV_MSC, MSC_SCAN, button);
1721953ea2dSDmitry Torokhov input_report_key(input, remote->keymap[button], press);
1731953ea2dSDmitry Torokhov input_sync(input);
1741953ea2dSDmitry Torokhov }
1751953ea2dSDmitry Torokhov
176ba0acb5eSDmitry Torokhov /*
177ba0acb5eSDmitry Torokhov * Routine that handles all the logic needed to parse out the message from the remote.
178ba0acb5eSDmitry Torokhov */
keyspan_check_data(struct usb_keyspan * remote)179ba0acb5eSDmitry Torokhov static void keyspan_check_data(struct usb_keyspan *remote)
180ba0acb5eSDmitry Torokhov {
181ba0acb5eSDmitry Torokhov int i;
182ba0acb5eSDmitry Torokhov int found = 0;
183ba0acb5eSDmitry Torokhov struct keyspan_message message;
184ba0acb5eSDmitry Torokhov
185ba0acb5eSDmitry Torokhov switch(remote->stage) {
186ba0acb5eSDmitry Torokhov case 0:
187ba0acb5eSDmitry Torokhov /*
188ba0acb5eSDmitry Torokhov * In stage 0 we want to find the start of a message. The remote sends a 0xFF as filler.
189ba0acb5eSDmitry Torokhov * So the first byte that isn't a FF should be the start of a new message.
190ba0acb5eSDmitry Torokhov */
191ba0acb5eSDmitry Torokhov for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
192ba0acb5eSDmitry Torokhov
193ba0acb5eSDmitry Torokhov if (i < RECV_SIZE) {
194ba0acb5eSDmitry Torokhov memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
195ba0acb5eSDmitry Torokhov remote->data.len = RECV_SIZE;
196ba0acb5eSDmitry Torokhov remote->data.pos = 0;
197ba0acb5eSDmitry Torokhov remote->data.tester = 0;
198ba0acb5eSDmitry Torokhov remote->data.bits_left = 0;
199ba0acb5eSDmitry Torokhov remote->stage = 1;
200ba0acb5eSDmitry Torokhov }
201ba0acb5eSDmitry Torokhov break;
202ba0acb5eSDmitry Torokhov
203ba0acb5eSDmitry Torokhov case 1:
204ba0acb5eSDmitry Torokhov /*
205ba0acb5eSDmitry Torokhov * Stage 1 we should have 16 bytes and should be able to detect a
206ba0acb5eSDmitry Torokhov * SYNC. The SYNC is 14 bits, 7 0's and then 7 1's.
207ba0acb5eSDmitry Torokhov */
208ba0acb5eSDmitry Torokhov memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
209ba0acb5eSDmitry Torokhov remote->data.len += RECV_SIZE;
210ba0acb5eSDmitry Torokhov
211ba0acb5eSDmitry Torokhov found = 0;
212ba0acb5eSDmitry Torokhov while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
213ba0acb5eSDmitry Torokhov for (i = 0; i < 8; ++i) {
214ba0acb5eSDmitry Torokhov if (keyspan_load_tester(remote, 14) != 0) {
215ba0acb5eSDmitry Torokhov remote->stage = 0;
216ba0acb5eSDmitry Torokhov return;
217ba0acb5eSDmitry Torokhov }
218ba0acb5eSDmitry Torokhov
219ba0acb5eSDmitry Torokhov if ((remote->data.tester & SYNC_MASK) == SYNC) {
220ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 14;
221ba0acb5eSDmitry Torokhov remote->data.bits_left -= 14;
222ba0acb5eSDmitry Torokhov found = 1;
223ba0acb5eSDmitry Torokhov break;
224ba0acb5eSDmitry Torokhov } else {
225ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 1;
226ba0acb5eSDmitry Torokhov --remote->data.bits_left;
227ba0acb5eSDmitry Torokhov }
228ba0acb5eSDmitry Torokhov }
229ba0acb5eSDmitry Torokhov }
230ba0acb5eSDmitry Torokhov
231ba0acb5eSDmitry Torokhov if (!found) {
232ba0acb5eSDmitry Torokhov remote->stage = 0;
233ba0acb5eSDmitry Torokhov remote->data.len = 0;
234ba0acb5eSDmitry Torokhov } else {
235ba0acb5eSDmitry Torokhov remote->stage = 2;
236ba0acb5eSDmitry Torokhov }
237ba0acb5eSDmitry Torokhov break;
238ba0acb5eSDmitry Torokhov
239ba0acb5eSDmitry Torokhov case 2:
240ba0acb5eSDmitry Torokhov /*
241ba0acb5eSDmitry Torokhov * Stage 2 we should have 24 bytes which will be enough for a full
242ba0acb5eSDmitry Torokhov * message. We need to parse out the system code, button code,
243ba0acb5eSDmitry Torokhov * toggle code, and stop.
244ba0acb5eSDmitry Torokhov */
245ba0acb5eSDmitry Torokhov memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
246ba0acb5eSDmitry Torokhov remote->data.len += RECV_SIZE;
247ba0acb5eSDmitry Torokhov
248ba0acb5eSDmitry Torokhov message.system = 0;
249ba0acb5eSDmitry Torokhov for (i = 0; i < 9; i++) {
250ba0acb5eSDmitry Torokhov keyspan_load_tester(remote, 6);
251ba0acb5eSDmitry Torokhov
252ba0acb5eSDmitry Torokhov if ((remote->data.tester & ZERO_MASK) == ZERO) {
253ba0acb5eSDmitry Torokhov message.system = message.system << 1;
254ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 5;
255ba0acb5eSDmitry Torokhov remote->data.bits_left -= 5;
256ba0acb5eSDmitry Torokhov } else if ((remote->data.tester & ONE_MASK) == ONE) {
257ba0acb5eSDmitry Torokhov message.system = (message.system << 1) + 1;
258ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 6;
259ba0acb5eSDmitry Torokhov remote->data.bits_left -= 6;
260ba0acb5eSDmitry Torokhov } else {
261419b1a11SGreg Kroah-Hartman dev_err(&remote->interface->dev,
2624efeca58SGreg Kroah-Hartman "%s - Unknown sequence found in system data.\n",
2634efeca58SGreg Kroah-Hartman __func__);
264ba0acb5eSDmitry Torokhov remote->stage = 0;
265ba0acb5eSDmitry Torokhov return;
266ba0acb5eSDmitry Torokhov }
267ba0acb5eSDmitry Torokhov }
268ba0acb5eSDmitry Torokhov
269ba0acb5eSDmitry Torokhov message.button = 0;
270ba0acb5eSDmitry Torokhov for (i = 0; i < 5; i++) {
271ba0acb5eSDmitry Torokhov keyspan_load_tester(remote, 6);
272ba0acb5eSDmitry Torokhov
273ba0acb5eSDmitry Torokhov if ((remote->data.tester & ZERO_MASK) == ZERO) {
274ba0acb5eSDmitry Torokhov message.button = message.button << 1;
275ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 5;
276ba0acb5eSDmitry Torokhov remote->data.bits_left -= 5;
277ba0acb5eSDmitry Torokhov } else if ((remote->data.tester & ONE_MASK) == ONE) {
278ba0acb5eSDmitry Torokhov message.button = (message.button << 1) + 1;
279ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 6;
280ba0acb5eSDmitry Torokhov remote->data.bits_left -= 6;
281ba0acb5eSDmitry Torokhov } else {
282419b1a11SGreg Kroah-Hartman dev_err(&remote->interface->dev,
2834efeca58SGreg Kroah-Hartman "%s - Unknown sequence found in button data.\n",
2844efeca58SGreg Kroah-Hartman __func__);
285ba0acb5eSDmitry Torokhov remote->stage = 0;
286ba0acb5eSDmitry Torokhov return;
287ba0acb5eSDmitry Torokhov }
288ba0acb5eSDmitry Torokhov }
289ba0acb5eSDmitry Torokhov
290ba0acb5eSDmitry Torokhov keyspan_load_tester(remote, 6);
291ba0acb5eSDmitry Torokhov if ((remote->data.tester & ZERO_MASK) == ZERO) {
292ba0acb5eSDmitry Torokhov message.toggle = 0;
293ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 5;
294ba0acb5eSDmitry Torokhov remote->data.bits_left -= 5;
295ba0acb5eSDmitry Torokhov } else if ((remote->data.tester & ONE_MASK) == ONE) {
296ba0acb5eSDmitry Torokhov message.toggle = 1;
297ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 6;
298ba0acb5eSDmitry Torokhov remote->data.bits_left -= 6;
299ba0acb5eSDmitry Torokhov } else {
300419b1a11SGreg Kroah-Hartman dev_err(&remote->interface->dev,
3014efeca58SGreg Kroah-Hartman "%s - Error in message, invalid toggle.\n",
3024efeca58SGreg Kroah-Hartman __func__);
303ba0acb5eSDmitry Torokhov remote->stage = 0;
304ba0acb5eSDmitry Torokhov return;
305ba0acb5eSDmitry Torokhov }
306ba0acb5eSDmitry Torokhov
307ba0acb5eSDmitry Torokhov keyspan_load_tester(remote, 5);
308ba0acb5eSDmitry Torokhov if ((remote->data.tester & STOP_MASK) == STOP) {
309ba0acb5eSDmitry Torokhov remote->data.tester = remote->data.tester >> 5;
310ba0acb5eSDmitry Torokhov remote->data.bits_left -= 5;
311ba0acb5eSDmitry Torokhov } else {
312419b1a11SGreg Kroah-Hartman dev_err(&remote->interface->dev,
3134efeca58SGreg Kroah-Hartman "Bad message received, no stop bit found.\n");
314ba0acb5eSDmitry Torokhov }
315ba0acb5eSDmitry Torokhov
316419b1a11SGreg Kroah-Hartman dev_dbg(&remote->interface->dev,
317ba0acb5eSDmitry Torokhov "%s found valid message: system: %d, button: %d, toggle: %d\n",
318ea3e6c59SHarvey Harrison __func__, message.system, message.button, message.toggle);
319ba0acb5eSDmitry Torokhov
320ba0acb5eSDmitry Torokhov if (message.toggle != remote->toggle) {
3211953ea2dSDmitry Torokhov keyspan_report_button(remote, message.button, 1);
3221953ea2dSDmitry Torokhov keyspan_report_button(remote, message.button, 0);
323ba0acb5eSDmitry Torokhov remote->toggle = message.toggle;
324ba0acb5eSDmitry Torokhov }
325ba0acb5eSDmitry Torokhov
326ba0acb5eSDmitry Torokhov remote->stage = 0;
327ba0acb5eSDmitry Torokhov break;
328ba0acb5eSDmitry Torokhov }
329ba0acb5eSDmitry Torokhov }
330ba0acb5eSDmitry Torokhov
331ba0acb5eSDmitry Torokhov /*
332ba0acb5eSDmitry Torokhov * Routine for sending all the initialization messages to the remote.
333ba0acb5eSDmitry Torokhov */
keyspan_setup(struct usb_device * dev)334ba0acb5eSDmitry Torokhov static int keyspan_setup(struct usb_device* dev)
335ba0acb5eSDmitry Torokhov {
336ba0acb5eSDmitry Torokhov int retval = 0;
337ba0acb5eSDmitry Torokhov
338ba0acb5eSDmitry Torokhov retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
339ba9a103fSJohan Hovold 0x11, 0x40, 0x5601, 0x0, NULL, 0,
340ba9a103fSJohan Hovold USB_CTRL_SET_TIMEOUT);
341ba0acb5eSDmitry Torokhov if (retval) {
342ba0acb5eSDmitry Torokhov dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
343ea3e6c59SHarvey Harrison __func__, retval);
344ba0acb5eSDmitry Torokhov return(retval);
345ba0acb5eSDmitry Torokhov }
346ba0acb5eSDmitry Torokhov
347ba0acb5eSDmitry Torokhov retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
348ba9a103fSJohan Hovold 0x44, 0x40, 0x0, 0x0, NULL, 0,
349ba9a103fSJohan Hovold USB_CTRL_SET_TIMEOUT);
350ba0acb5eSDmitry Torokhov if (retval) {
351ba0acb5eSDmitry Torokhov dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
352ea3e6c59SHarvey Harrison __func__, retval);
353ba0acb5eSDmitry Torokhov return(retval);
354ba0acb5eSDmitry Torokhov }
355ba0acb5eSDmitry Torokhov
356ba0acb5eSDmitry Torokhov retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
357ba9a103fSJohan Hovold 0x22, 0x40, 0x0, 0x0, NULL, 0,
358ba9a103fSJohan Hovold USB_CTRL_SET_TIMEOUT);
359ba0acb5eSDmitry Torokhov if (retval) {
360ba0acb5eSDmitry Torokhov dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
361ea3e6c59SHarvey Harrison __func__, retval);
362ba0acb5eSDmitry Torokhov return(retval);
363ba0acb5eSDmitry Torokhov }
364ba0acb5eSDmitry Torokhov
365ea3e6c59SHarvey Harrison dev_dbg(&dev->dev, "%s - Setup complete.\n", __func__);
366ba0acb5eSDmitry Torokhov return(retval);
367ba0acb5eSDmitry Torokhov }
368ba0acb5eSDmitry Torokhov
369ba0acb5eSDmitry Torokhov /*
370ba0acb5eSDmitry Torokhov * Routine used to handle a new message that has come in.
371ba0acb5eSDmitry Torokhov */
keyspan_irq_recv(struct urb * urb)372ba0acb5eSDmitry Torokhov static void keyspan_irq_recv(struct urb *urb)
373ba0acb5eSDmitry Torokhov {
374ba0acb5eSDmitry Torokhov struct usb_keyspan *dev = urb->context;
375ba0acb5eSDmitry Torokhov int retval;
376ba0acb5eSDmitry Torokhov
377ba0acb5eSDmitry Torokhov /* Check our status in case we need to bail out early. */
378ba0acb5eSDmitry Torokhov switch (urb->status) {
379ba0acb5eSDmitry Torokhov case 0:
380ba0acb5eSDmitry Torokhov break;
381ba0acb5eSDmitry Torokhov
382ba0acb5eSDmitry Torokhov /* Device went away so don't keep trying to read from it. */
383ba0acb5eSDmitry Torokhov case -ECONNRESET:
384ba0acb5eSDmitry Torokhov case -ENOENT:
385ba0acb5eSDmitry Torokhov case -ESHUTDOWN:
386ba0acb5eSDmitry Torokhov return;
387ba0acb5eSDmitry Torokhov
388ba0acb5eSDmitry Torokhov default:
389ba0acb5eSDmitry Torokhov goto resubmit;
390ba0acb5eSDmitry Torokhov }
391ba0acb5eSDmitry Torokhov
392ba0acb5eSDmitry Torokhov if (debug)
393ba0acb5eSDmitry Torokhov keyspan_print(dev);
394ba0acb5eSDmitry Torokhov
395ba0acb5eSDmitry Torokhov keyspan_check_data(dev);
396ba0acb5eSDmitry Torokhov
397ba0acb5eSDmitry Torokhov resubmit:
398ba0acb5eSDmitry Torokhov retval = usb_submit_urb(urb, GFP_ATOMIC);
399ba0acb5eSDmitry Torokhov if (retval)
400419b1a11SGreg Kroah-Hartman dev_err(&dev->interface->dev,
4014efeca58SGreg Kroah-Hartman "%s - usb_submit_urb failed with result: %d\n",
4024efeca58SGreg Kroah-Hartman __func__, retval);
403ba0acb5eSDmitry Torokhov }
404ba0acb5eSDmitry Torokhov
keyspan_open(struct input_dev * dev)405ba0acb5eSDmitry Torokhov static int keyspan_open(struct input_dev *dev)
406ba0acb5eSDmitry Torokhov {
407ba0acb5eSDmitry Torokhov struct usb_keyspan *remote = input_get_drvdata(dev);
408ba0acb5eSDmitry Torokhov
409ba0acb5eSDmitry Torokhov remote->irq_urb->dev = remote->udev;
410ba0acb5eSDmitry Torokhov if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
411ba0acb5eSDmitry Torokhov return -EIO;
412ba0acb5eSDmitry Torokhov
413ba0acb5eSDmitry Torokhov return 0;
414ba0acb5eSDmitry Torokhov }
415ba0acb5eSDmitry Torokhov
keyspan_close(struct input_dev * dev)416ba0acb5eSDmitry Torokhov static void keyspan_close(struct input_dev *dev)
417ba0acb5eSDmitry Torokhov {
418ba0acb5eSDmitry Torokhov struct usb_keyspan *remote = input_get_drvdata(dev);
419ba0acb5eSDmitry Torokhov
420ba0acb5eSDmitry Torokhov usb_kill_urb(remote->irq_urb);
421ba0acb5eSDmitry Torokhov }
422ba0acb5eSDmitry Torokhov
keyspan_get_in_endpoint(struct usb_host_interface * iface)423ba0acb5eSDmitry Torokhov static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
424ba0acb5eSDmitry Torokhov {
425ba0acb5eSDmitry Torokhov
426ba0acb5eSDmitry Torokhov struct usb_endpoint_descriptor *endpoint;
427ba0acb5eSDmitry Torokhov int i;
428ba0acb5eSDmitry Torokhov
429ba0acb5eSDmitry Torokhov for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
430ba0acb5eSDmitry Torokhov endpoint = &iface->endpoint[i].desc;
431ba0acb5eSDmitry Torokhov
432ba0acb5eSDmitry Torokhov if (usb_endpoint_is_int_in(endpoint)) {
433ba0acb5eSDmitry Torokhov /* we found our interrupt in endpoint */
434ba0acb5eSDmitry Torokhov return endpoint;
435ba0acb5eSDmitry Torokhov }
436ba0acb5eSDmitry Torokhov }
437ba0acb5eSDmitry Torokhov
438ba0acb5eSDmitry Torokhov return NULL;
439ba0acb5eSDmitry Torokhov }
440ba0acb5eSDmitry Torokhov
441ba0acb5eSDmitry Torokhov /*
442ba0acb5eSDmitry Torokhov * Routine that sets up the driver to handle a specific USB device detected on the bus.
443ba0acb5eSDmitry Torokhov */
keyspan_probe(struct usb_interface * interface,const struct usb_device_id * id)444ba0acb5eSDmitry Torokhov static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
445ba0acb5eSDmitry Torokhov {
446ba0acb5eSDmitry Torokhov struct usb_device *udev = interface_to_usbdev(interface);
447ba0acb5eSDmitry Torokhov struct usb_endpoint_descriptor *endpoint;
448ba0acb5eSDmitry Torokhov struct usb_keyspan *remote;
449ba0acb5eSDmitry Torokhov struct input_dev *input_dev;
450ba0acb5eSDmitry Torokhov int i, error;
451ba0acb5eSDmitry Torokhov
452ba0acb5eSDmitry Torokhov endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
453ba0acb5eSDmitry Torokhov if (!endpoint)
454ba0acb5eSDmitry Torokhov return -ENODEV;
455ba0acb5eSDmitry Torokhov
456ba0acb5eSDmitry Torokhov remote = kzalloc(sizeof(*remote), GFP_KERNEL);
457ba0acb5eSDmitry Torokhov input_dev = input_allocate_device();
458ba0acb5eSDmitry Torokhov if (!remote || !input_dev) {
459ba0acb5eSDmitry Torokhov error = -ENOMEM;
460ba0acb5eSDmitry Torokhov goto fail1;
461ba0acb5eSDmitry Torokhov }
462ba0acb5eSDmitry Torokhov
463ba0acb5eSDmitry Torokhov remote->udev = udev;
464ba0acb5eSDmitry Torokhov remote->input = input_dev;
465ba0acb5eSDmitry Torokhov remote->interface = interface;
466ba0acb5eSDmitry Torokhov remote->in_endpoint = endpoint;
467ba0acb5eSDmitry Torokhov remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */
468ba0acb5eSDmitry Torokhov
469673831daSJia-Ju Bai remote->in_buffer = usb_alloc_coherent(udev, RECV_SIZE, GFP_KERNEL, &remote->in_dma);
470ba0acb5eSDmitry Torokhov if (!remote->in_buffer) {
471ba0acb5eSDmitry Torokhov error = -ENOMEM;
472ba0acb5eSDmitry Torokhov goto fail1;
473ba0acb5eSDmitry Torokhov }
474ba0acb5eSDmitry Torokhov
475ba0acb5eSDmitry Torokhov remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
476ba0acb5eSDmitry Torokhov if (!remote->irq_urb) {
477ba0acb5eSDmitry Torokhov error = -ENOMEM;
478ba0acb5eSDmitry Torokhov goto fail2;
479ba0acb5eSDmitry Torokhov }
480ba0acb5eSDmitry Torokhov
481ba0acb5eSDmitry Torokhov error = keyspan_setup(udev);
482ba0acb5eSDmitry Torokhov if (error) {
483ba0acb5eSDmitry Torokhov error = -ENODEV;
484ba0acb5eSDmitry Torokhov goto fail3;
485ba0acb5eSDmitry Torokhov }
486ba0acb5eSDmitry Torokhov
487ba0acb5eSDmitry Torokhov if (udev->manufacturer)
488*a9f08ad7SWolfram Sang strscpy(remote->name, udev->manufacturer, sizeof(remote->name));
489ba0acb5eSDmitry Torokhov
490ba0acb5eSDmitry Torokhov if (udev->product) {
491ba0acb5eSDmitry Torokhov if (udev->manufacturer)
492ba0acb5eSDmitry Torokhov strlcat(remote->name, " ", sizeof(remote->name));
493ba0acb5eSDmitry Torokhov strlcat(remote->name, udev->product, sizeof(remote->name));
494ba0acb5eSDmitry Torokhov }
495ba0acb5eSDmitry Torokhov
496ba0acb5eSDmitry Torokhov if (!strlen(remote->name))
497ba0acb5eSDmitry Torokhov snprintf(remote->name, sizeof(remote->name),
498ba0acb5eSDmitry Torokhov "USB Keyspan Remote %04x:%04x",
499ba0acb5eSDmitry Torokhov le16_to_cpu(udev->descriptor.idVendor),
500ba0acb5eSDmitry Torokhov le16_to_cpu(udev->descriptor.idProduct));
501ba0acb5eSDmitry Torokhov
502ba0acb5eSDmitry Torokhov usb_make_path(udev, remote->phys, sizeof(remote->phys));
503ba0acb5eSDmitry Torokhov strlcat(remote->phys, "/input0", sizeof(remote->phys));
5041953ea2dSDmitry Torokhov memcpy(remote->keymap, keyspan_key_table, sizeof(remote->keymap));
505ba0acb5eSDmitry Torokhov
506ba0acb5eSDmitry Torokhov input_dev->name = remote->name;
507ba0acb5eSDmitry Torokhov input_dev->phys = remote->phys;
508ba0acb5eSDmitry Torokhov usb_to_input_id(udev, &input_dev->id);
509ba0acb5eSDmitry Torokhov input_dev->dev.parent = &interface->dev;
5101953ea2dSDmitry Torokhov input_dev->keycode = remote->keymap;
5111953ea2dSDmitry Torokhov input_dev->keycodesize = sizeof(unsigned short);
5121953ea2dSDmitry Torokhov input_dev->keycodemax = ARRAY_SIZE(remote->keymap);
513ba0acb5eSDmitry Torokhov
5141953ea2dSDmitry Torokhov input_set_capability(input_dev, EV_MSC, MSC_SCAN);
5151953ea2dSDmitry Torokhov __set_bit(EV_KEY, input_dev->evbit);
516ba0acb5eSDmitry Torokhov for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
5171953ea2dSDmitry Torokhov __set_bit(keyspan_key_table[i], input_dev->keybit);
5181953ea2dSDmitry Torokhov __clear_bit(KEY_RESERVED, input_dev->keybit);
519ba0acb5eSDmitry Torokhov
520ba0acb5eSDmitry Torokhov input_set_drvdata(input_dev, remote);
521ba0acb5eSDmitry Torokhov
522ba0acb5eSDmitry Torokhov input_dev->open = keyspan_open;
523ba0acb5eSDmitry Torokhov input_dev->close = keyspan_close;
524ba0acb5eSDmitry Torokhov
525ba0acb5eSDmitry Torokhov /*
5261953ea2dSDmitry Torokhov * Initialize the URB to access the device.
5271953ea2dSDmitry Torokhov * The urb gets sent to the device in keyspan_open()
528ba0acb5eSDmitry Torokhov */
529ba0acb5eSDmitry Torokhov usb_fill_int_urb(remote->irq_urb,
5301953ea2dSDmitry Torokhov remote->udev,
5311953ea2dSDmitry Torokhov usb_rcvintpipe(remote->udev, endpoint->bEndpointAddress),
532ba0acb5eSDmitry Torokhov remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
5331953ea2dSDmitry Torokhov endpoint->bInterval);
534ba0acb5eSDmitry Torokhov remote->irq_urb->transfer_dma = remote->in_dma;
535ba0acb5eSDmitry Torokhov remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
536ba0acb5eSDmitry Torokhov
537ba0acb5eSDmitry Torokhov /* we can register the device now, as it is ready */
538ba0acb5eSDmitry Torokhov error = input_register_device(remote->input);
539ba0acb5eSDmitry Torokhov if (error)
540ba0acb5eSDmitry Torokhov goto fail3;
541ba0acb5eSDmitry Torokhov
542ba0acb5eSDmitry Torokhov /* save our data pointer in this interface device */
543ba0acb5eSDmitry Torokhov usb_set_intfdata(interface, remote);
544ba0acb5eSDmitry Torokhov
545ba0acb5eSDmitry Torokhov return 0;
546ba0acb5eSDmitry Torokhov
547ba0acb5eSDmitry Torokhov fail3: usb_free_urb(remote->irq_urb);
548997ea58eSDaniel Mack fail2: usb_free_coherent(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
549ba0acb5eSDmitry Torokhov fail1: kfree(remote);
550ba0acb5eSDmitry Torokhov input_free_device(input_dev);
551ba0acb5eSDmitry Torokhov
552ba0acb5eSDmitry Torokhov return error;
553ba0acb5eSDmitry Torokhov }
554ba0acb5eSDmitry Torokhov
555ba0acb5eSDmitry Torokhov /*
556ba0acb5eSDmitry Torokhov * Routine called when a device is disconnected from the USB.
557ba0acb5eSDmitry Torokhov */
keyspan_disconnect(struct usb_interface * interface)558ba0acb5eSDmitry Torokhov static void keyspan_disconnect(struct usb_interface *interface)
559ba0acb5eSDmitry Torokhov {
560ba0acb5eSDmitry Torokhov struct usb_keyspan *remote;
561ba0acb5eSDmitry Torokhov
562ba0acb5eSDmitry Torokhov remote = usb_get_intfdata(interface);
563ba0acb5eSDmitry Torokhov usb_set_intfdata(interface, NULL);
564ba0acb5eSDmitry Torokhov
565ba0acb5eSDmitry Torokhov if (remote) { /* We have a valid driver structure so clean up everything we allocated. */
566ba0acb5eSDmitry Torokhov input_unregister_device(remote->input);
567ba0acb5eSDmitry Torokhov usb_kill_urb(remote->irq_urb);
568ba0acb5eSDmitry Torokhov usb_free_urb(remote->irq_urb);
569997ea58eSDaniel Mack usb_free_coherent(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
570ba0acb5eSDmitry Torokhov kfree(remote);
571ba0acb5eSDmitry Torokhov }
572ba0acb5eSDmitry Torokhov }
573ba0acb5eSDmitry Torokhov
574ba0acb5eSDmitry Torokhov /*
575ba0acb5eSDmitry Torokhov * Standard driver set up sections
576ba0acb5eSDmitry Torokhov */
577ba0acb5eSDmitry Torokhov static struct usb_driver keyspan_driver =
578ba0acb5eSDmitry Torokhov {
579ba0acb5eSDmitry Torokhov .name = "keyspan_remote",
580ba0acb5eSDmitry Torokhov .probe = keyspan_probe,
581ba0acb5eSDmitry Torokhov .disconnect = keyspan_disconnect,
582ba0acb5eSDmitry Torokhov .id_table = keyspan_table
583ba0acb5eSDmitry Torokhov };
584ba0acb5eSDmitry Torokhov
58508642e7cSGreg Kroah-Hartman module_usb_driver(keyspan_driver);
586ba0acb5eSDmitry Torokhov
587ba0acb5eSDmitry Torokhov MODULE_DEVICE_TABLE(usb, keyspan_table);
588698c03b4SJulia Lawall MODULE_AUTHOR("Michael Downey <downey@zymeta.com>");
589698c03b4SJulia Lawall MODULE_DESCRIPTION("Driver for the USB Keyspan remote control.");
5909f1d1ea3SGreg Kroah-Hartman MODULE_LICENSE("GPL");
591