Commit | Line | Data |
---|---|---|
66e89522 JW |
1 | /* |
2 | * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers | |
3 | * | |
4 | * Copyright (c) 2010 by Jarod Wilson <jarod@redhat.com> | |
5 | * | |
6 | * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan | |
7 | * Conti, Martin Blatter and Daniel Melander, the latter of which was | |
8 | * in turn also based on the lirc_atiusb driver by Paul Miller. The | |
9 | * two mce drivers were merged into one by Jarod Wilson, with transmit | |
10 | * support for the 1st-gen device added primarily by Patrick Calhoun, | |
11 | * with a bit of tweaks by Jarod. Debugging improvements and proper | |
12 | * support for what appears to be 3rd-gen hardware added by Jarod. | |
13 | * Initial port from lirc driver to ir-core drivery by Jarod, based | |
14 | * partially on a port to an earlier proposed IR infrastructure by | |
15 | * Jon Smirl, which included enhancements and simplifications to the | |
16 | * incoming IR buffer parsing routines. | |
17 | * | |
66e89522 JW |
18 | * |
19 | * This program is free software; you can redistribute it and/or modify | |
20 | * it under the terms of the GNU General Public License as published by | |
21 | * the Free Software Foundation; either version 2 of the License, or | |
22 | * (at your option) any later version. | |
23 | * | |
24 | * This program is distributed in the hope that it will be useful, | |
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
27 | * GNU General Public License for more details. | |
28 | * | |
29 | * You should have received a copy of the GNU General Public License | |
30 | * along with this program; if not, write to the Free Software | |
31 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
32 | * | |
33 | */ | |
34 | ||
35 | #include <linux/device.h> | |
36 | #include <linux/module.h> | |
37 | #include <linux/slab.h> | |
38 | #include <linux/usb.h> | |
39 | #include <linux/input.h> | |
40 | #include <media/ir-core.h> | |
41 | #include <media/ir-common.h> | |
42 | ||
43 | #define DRIVER_VERSION "1.91" | |
44 | #define DRIVER_AUTHOR "Jarod Wilson <jarod@wilsonet.com>" | |
45 | #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \ | |
46 | "device driver" | |
47 | #define DRIVER_NAME "mceusb" | |
48 | ||
49 | #define USB_BUFLEN 32 /* USB reception buffer length */ | |
657290b6 JW |
50 | #define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */ |
51 | #define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */ | |
66e89522 JW |
52 | |
53 | /* MCE constants */ | |
54 | #define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */ | |
55 | #define MCE_TIME_UNIT 50 /* Approx 50us resolution */ | |
56 | #define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */ | |
57 | #define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */ | |
58 | #define MCE_PACKET_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */ | |
59 | #define MCE_CONTROL_HEADER 0x9F /* MCE status header */ | |
60 | #define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */ | |
61 | #define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */ | |
62 | #define MCE_DEFAULT_TX_MASK 0x03 /* Val opts: TX1=0x01, TX2=0x02, ALL=0x03 */ | |
63 | #define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */ | |
64 | #define MCE_PULSE_MASK 0x7F /* Pulse mask */ | |
65 | #define MCE_MAX_PULSE_LENGTH 0x7F /* Longest transmittable pulse symbol */ | |
d732a72d | 66 | #define MCE_PACKET_LENGTH_MASK 0x1F /* Packet length mask */ |
66e89522 JW |
67 | |
68 | ||
69 | /* module parameters */ | |
70 | #ifdef CONFIG_USB_DEBUG | |
71 | static int debug = 1; | |
72 | #else | |
73 | static int debug; | |
74 | #endif | |
75 | ||
76 | /* general constants */ | |
77 | #define SEND_FLAG_IN_PROGRESS 1 | |
78 | #define SEND_FLAG_COMPLETE 2 | |
79 | #define RECV_FLAG_IN_PROGRESS 3 | |
80 | #define RECV_FLAG_COMPLETE 4 | |
81 | ||
82 | #define MCEUSB_RX 1 | |
83 | #define MCEUSB_TX 2 | |
84 | ||
85 | #define VENDOR_PHILIPS 0x0471 | |
86 | #define VENDOR_SMK 0x0609 | |
87 | #define VENDOR_TATUNG 0x1460 | |
88 | #define VENDOR_GATEWAY 0x107b | |
89 | #define VENDOR_SHUTTLE 0x1308 | |
90 | #define VENDOR_SHUTTLE2 0x051c | |
91 | #define VENDOR_MITSUMI 0x03ee | |
92 | #define VENDOR_TOPSEED 0x1784 | |
93 | #define VENDOR_RICAVISION 0x179d | |
94 | #define VENDOR_ITRON 0x195d | |
95 | #define VENDOR_FIC 0x1509 | |
96 | #define VENDOR_LG 0x043e | |
97 | #define VENDOR_MICROSOFT 0x045e | |
98 | #define VENDOR_FORMOSA 0x147a | |
99 | #define VENDOR_FINTEK 0x1934 | |
100 | #define VENDOR_PINNACLE 0x2304 | |
101 | #define VENDOR_ECS 0x1019 | |
102 | #define VENDOR_WISTRON 0x0fb8 | |
103 | #define VENDOR_COMPRO 0x185b | |
104 | #define VENDOR_NORTHSTAR 0x04eb | |
105 | #define VENDOR_REALTEK 0x0bda | |
106 | #define VENDOR_TIVO 0x105a | |
107 | ||
108 | static struct usb_device_id mceusb_dev_table[] = { | |
109 | /* Original Microsoft MCE IR Transceiver (often HP-branded) */ | |
110 | { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) }, | |
111 | /* Philips Infrared Transceiver - Sahara branded */ | |
112 | { USB_DEVICE(VENDOR_PHILIPS, 0x0608) }, | |
113 | /* Philips Infrared Transceiver - HP branded */ | |
114 | { USB_DEVICE(VENDOR_PHILIPS, 0x060c) }, | |
115 | /* Philips SRM5100 */ | |
116 | { USB_DEVICE(VENDOR_PHILIPS, 0x060d) }, | |
117 | /* Philips Infrared Transceiver - Omaura */ | |
118 | { USB_DEVICE(VENDOR_PHILIPS, 0x060f) }, | |
119 | /* Philips Infrared Transceiver - Spinel plus */ | |
120 | { USB_DEVICE(VENDOR_PHILIPS, 0x0613) }, | |
121 | /* Philips eHome Infrared Transceiver */ | |
122 | { USB_DEVICE(VENDOR_PHILIPS, 0x0815) }, | |
123 | /* Realtek MCE IR Receiver */ | |
124 | { USB_DEVICE(VENDOR_REALTEK, 0x0161) }, | |
125 | /* SMK/Toshiba G83C0004D410 */ | |
126 | { USB_DEVICE(VENDOR_SMK, 0x031d) }, | |
127 | /* SMK eHome Infrared Transceiver (Sony VAIO) */ | |
128 | { USB_DEVICE(VENDOR_SMK, 0x0322) }, | |
129 | /* bundled with Hauppauge PVR-150 */ | |
130 | { USB_DEVICE(VENDOR_SMK, 0x0334) }, | |
131 | /* SMK eHome Infrared Transceiver */ | |
132 | { USB_DEVICE(VENDOR_SMK, 0x0338) }, | |
133 | /* Tatung eHome Infrared Transceiver */ | |
134 | { USB_DEVICE(VENDOR_TATUNG, 0x9150) }, | |
135 | /* Shuttle eHome Infrared Transceiver */ | |
136 | { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) }, | |
137 | /* Shuttle eHome Infrared Transceiver */ | |
138 | { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) }, | |
139 | /* Gateway eHome Infrared Transceiver */ | |
140 | { USB_DEVICE(VENDOR_GATEWAY, 0x3009) }, | |
141 | /* Mitsumi */ | |
142 | { USB_DEVICE(VENDOR_MITSUMI, 0x2501) }, | |
143 | /* Topseed eHome Infrared Transceiver */ | |
144 | { USB_DEVICE(VENDOR_TOPSEED, 0x0001) }, | |
145 | /* Topseed HP eHome Infrared Transceiver */ | |
146 | { USB_DEVICE(VENDOR_TOPSEED, 0x0006) }, | |
147 | /* Topseed eHome Infrared Transceiver */ | |
148 | { USB_DEVICE(VENDOR_TOPSEED, 0x0007) }, | |
149 | /* Topseed eHome Infrared Transceiver */ | |
150 | { USB_DEVICE(VENDOR_TOPSEED, 0x0008) }, | |
151 | /* Topseed eHome Infrared Transceiver */ | |
152 | { USB_DEVICE(VENDOR_TOPSEED, 0x000a) }, | |
153 | /* Topseed eHome Infrared Transceiver */ | |
154 | { USB_DEVICE(VENDOR_TOPSEED, 0x0011) }, | |
155 | /* Ricavision internal Infrared Transceiver */ | |
156 | { USB_DEVICE(VENDOR_RICAVISION, 0x0010) }, | |
157 | /* Itron ione Libra Q-11 */ | |
158 | { USB_DEVICE(VENDOR_ITRON, 0x7002) }, | |
159 | /* FIC eHome Infrared Transceiver */ | |
160 | { USB_DEVICE(VENDOR_FIC, 0x9242) }, | |
161 | /* LG eHome Infrared Transceiver */ | |
162 | { USB_DEVICE(VENDOR_LG, 0x9803) }, | |
163 | /* Microsoft MCE Infrared Transceiver */ | |
164 | { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) }, | |
165 | /* Formosa eHome Infrared Transceiver */ | |
166 | { USB_DEVICE(VENDOR_FORMOSA, 0xe015) }, | |
167 | /* Formosa21 / eHome Infrared Receiver */ | |
168 | { USB_DEVICE(VENDOR_FORMOSA, 0xe016) }, | |
169 | /* Formosa aim / Trust MCE Infrared Receiver */ | |
170 | { USB_DEVICE(VENDOR_FORMOSA, 0xe017) }, | |
171 | /* Formosa Industrial Computing / Beanbag Emulation Device */ | |
172 | { USB_DEVICE(VENDOR_FORMOSA, 0xe018) }, | |
173 | /* Formosa21 / eHome Infrared Receiver */ | |
174 | { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) }, | |
175 | /* Formosa Industrial Computing AIM IR605/A */ | |
176 | { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) }, | |
177 | /* Formosa Industrial Computing */ | |
178 | { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) }, | |
179 | /* Fintek eHome Infrared Transceiver */ | |
180 | { USB_DEVICE(VENDOR_FINTEK, 0x0602) }, | |
181 | /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */ | |
182 | { USB_DEVICE(VENDOR_FINTEK, 0x0702) }, | |
183 | /* Pinnacle Remote Kit */ | |
184 | { USB_DEVICE(VENDOR_PINNACLE, 0x0225) }, | |
185 | /* Elitegroup Computer Systems IR */ | |
186 | { USB_DEVICE(VENDOR_ECS, 0x0f38) }, | |
187 | /* Wistron Corp. eHome Infrared Receiver */ | |
188 | { USB_DEVICE(VENDOR_WISTRON, 0x0002) }, | |
189 | /* Compro K100 */ | |
190 | { USB_DEVICE(VENDOR_COMPRO, 0x3020) }, | |
191 | /* Compro K100 v2 */ | |
192 | { USB_DEVICE(VENDOR_COMPRO, 0x3082) }, | |
193 | /* Northstar Systems, Inc. eHome Infrared Transceiver */ | |
194 | { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) }, | |
195 | /* TiVo PC IR Receiver */ | |
196 | { USB_DEVICE(VENDOR_TIVO, 0x2000) }, | |
197 | /* Terminating entry */ | |
198 | { } | |
199 | }; | |
200 | ||
201 | static struct usb_device_id gen3_list[] = { | |
202 | { USB_DEVICE(VENDOR_PINNACLE, 0x0225) }, | |
203 | { USB_DEVICE(VENDOR_TOPSEED, 0x0008) }, | |
204 | {} | |
205 | }; | |
206 | ||
66e89522 JW |
207 | static struct usb_device_id microsoft_gen1_list[] = { |
208 | { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) }, | |
209 | {} | |
210 | }; | |
211 | ||
657290b6 JW |
212 | static struct usb_device_id std_tx_mask_list[] = { |
213 | { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) }, | |
214 | { USB_DEVICE(VENDOR_PHILIPS, 0x060c) }, | |
215 | { USB_DEVICE(VENDOR_SMK, 0x031d) }, | |
216 | { USB_DEVICE(VENDOR_SMK, 0x0322) }, | |
217 | { USB_DEVICE(VENDOR_SMK, 0x0334) }, | |
218 | { USB_DEVICE(VENDOR_TOPSEED, 0x0001) }, | |
219 | { USB_DEVICE(VENDOR_TOPSEED, 0x0006) }, | |
220 | { USB_DEVICE(VENDOR_TOPSEED, 0x0007) }, | |
221 | { USB_DEVICE(VENDOR_TOPSEED, 0x0008) }, | |
222 | { USB_DEVICE(VENDOR_TOPSEED, 0x000a) }, | |
223 | { USB_DEVICE(VENDOR_TOPSEED, 0x0011) }, | |
224 | { USB_DEVICE(VENDOR_PINNACLE, 0x0225) }, | |
225 | {} | |
226 | }; | |
227 | ||
66e89522 JW |
228 | /* data structure for each usb transceiver */ |
229 | struct mceusb_dev { | |
230 | /* ir-core bits */ | |
231 | struct ir_input_dev *irdev; | |
232 | struct ir_dev_props *props; | |
66e89522 JW |
233 | struct ir_raw_event rawir; |
234 | ||
235 | /* core device bits */ | |
236 | struct device *dev; | |
237 | struct input_dev *idev; | |
238 | ||
239 | /* usb */ | |
240 | struct usb_device *usbdev; | |
241 | struct urb *urb_in; | |
242 | struct usb_endpoint_descriptor *usb_ep_in; | |
243 | struct usb_endpoint_descriptor *usb_ep_out; | |
244 | ||
245 | /* buffers and dma */ | |
246 | unsigned char *buf_in; | |
247 | unsigned int len_in; | |
248 | u8 cmd; /* MCE command type */ | |
249 | u8 rem; /* Remaining IR data bytes in packet */ | |
250 | dma_addr_t dma_in; | |
251 | dma_addr_t dma_out; | |
252 | ||
253 | struct { | |
254 | u32 connected:1; | |
657290b6 | 255 | u32 tx_mask_inverted:1; |
66e89522 JW |
256 | u32 microsoft_gen1:1; |
257 | u32 gen3:1; | |
258 | u32 reserved:28; | |
259 | } flags; | |
260 | ||
e23fb964 | 261 | /* transmit support */ |
66e89522 | 262 | int send_flags; |
e23fb964 JW |
263 | u32 carrier; |
264 | unsigned char tx_mask; | |
66e89522 JW |
265 | |
266 | char name[128]; | |
267 | char phys[64]; | |
66e89522 JW |
268 | }; |
269 | ||
270 | /* | |
271 | * MCE Device Command Strings | |
272 | * Device command responses vary from device to device... | |
273 | * - DEVICE_RESET resets the hardware to its default state | |
274 | * - GET_REVISION fetches the hardware/software revision, common | |
275 | * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42 | |
276 | * - GET_CARRIER_FREQ gets the carrier mode and frequency of the | |
277 | * device, with replies in the form of 9f 06 MM FF, where MM is 0-3, | |
278 | * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is | |
279 | * ((clk / frequency) - 1) | |
280 | * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us, | |
281 | * response in the form of 9f 0c msb lsb | |
282 | * - GET_TX_BITMASK fetches the transmitter bitmask, replies in | |
283 | * the form of 9f 08 bm, where bm is the bitmask | |
284 | * - GET_RX_SENSOR fetches the RX sensor setting -- long-range | |
285 | * general use one or short-range learning one, in the form of | |
286 | * 9f 14 ss, where ss is either 01 for long-range or 02 for short | |
287 | * - SET_CARRIER_FREQ sets a new carrier mode and frequency | |
288 | * - SET_TX_BITMASK sets the transmitter bitmask | |
289 | * - SET_RX_TIMEOUT sets the receiver timeout | |
290 | * - SET_RX_SENSOR sets which receiver sensor to use | |
291 | */ | |
292 | static char DEVICE_RESET[] = {0x00, 0xff, 0xaa}; | |
293 | static char GET_REVISION[] = {0xff, 0x0b}; | |
294 | static char GET_UNKNOWN[] = {0xff, 0x18}; | |
295 | static char GET_CARRIER_FREQ[] = {0x9f, 0x07}; | |
296 | static char GET_RX_TIMEOUT[] = {0x9f, 0x0d}; | |
297 | static char GET_TX_BITMASK[] = {0x9f, 0x13}; | |
298 | static char GET_RX_SENSOR[] = {0x9f, 0x15}; | |
299 | /* sub in desired values in lower byte or bytes for full command */ | |
300 | /* FIXME: make use of these for transmit. | |
301 | static char SET_CARRIER_FREQ[] = {0x9f, 0x06, 0x00, 0x00}; | |
302 | static char SET_TX_BITMASK[] = {0x9f, 0x08, 0x00}; | |
303 | static char SET_RX_TIMEOUT[] = {0x9f, 0x0c, 0x00, 0x00}; | |
304 | static char SET_RX_SENSOR[] = {0x9f, 0x14, 0x00}; | |
305 | */ | |
306 | ||
307 | static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf, | |
308 | int len, bool out) | |
309 | { | |
310 | char codes[USB_BUFLEN * 3 + 1]; | |
311 | char inout[9]; | |
312 | int i; | |
313 | u8 cmd, subcmd, data1, data2; | |
314 | struct device *dev = ir->dev; | |
657290b6 | 315 | int idx = 0; |
66e89522 | 316 | |
657290b6 JW |
317 | /* skip meaningless 0xb1 0x60 header bytes on orig receiver */ |
318 | if (ir->flags.microsoft_gen1 && !out) | |
319 | idx = 2; | |
66e89522 | 320 | |
657290b6 | 321 | if (len <= idx) |
66e89522 JW |
322 | return; |
323 | ||
324 | for (i = 0; i < len && i < USB_BUFLEN; i++) | |
325 | snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF); | |
326 | ||
327 | dev_info(dev, "%sx data: %s (length=%d)\n", | |
328 | (out ? "t" : "r"), codes, len); | |
329 | ||
330 | if (out) | |
331 | strcpy(inout, "Request\0"); | |
332 | else | |
333 | strcpy(inout, "Got\0"); | |
334 | ||
657290b6 JW |
335 | cmd = buf[idx] & 0xff; |
336 | subcmd = buf[idx + 1] & 0xff; | |
337 | data1 = buf[idx + 2] & 0xff; | |
338 | data2 = buf[idx + 3] & 0xff; | |
66e89522 JW |
339 | |
340 | switch (cmd) { | |
341 | case 0x00: | |
342 | if (subcmd == 0xff && data1 == 0xaa) | |
343 | dev_info(dev, "Device reset requested\n"); | |
344 | else | |
345 | dev_info(dev, "Unknown command 0x%02x 0x%02x\n", | |
346 | cmd, subcmd); | |
347 | break; | |
348 | case 0xff: | |
349 | switch (subcmd) { | |
350 | case 0x0b: | |
351 | if (len == 2) | |
352 | dev_info(dev, "Get hw/sw rev?\n"); | |
353 | else | |
354 | dev_info(dev, "hw/sw rev 0x%02x 0x%02x " | |
355 | "0x%02x 0x%02x\n", data1, data2, | |
657290b6 | 356 | buf[idx + 4], buf[idx + 5]); |
66e89522 JW |
357 | break; |
358 | case 0xaa: | |
359 | dev_info(dev, "Device reset requested\n"); | |
360 | break; | |
361 | case 0xfe: | |
362 | dev_info(dev, "Previous command not supported\n"); | |
363 | break; | |
364 | case 0x18: | |
365 | case 0x1b: | |
366 | default: | |
367 | dev_info(dev, "Unknown command 0x%02x 0x%02x\n", | |
368 | cmd, subcmd); | |
369 | break; | |
370 | } | |
371 | break; | |
372 | case 0x9f: | |
373 | switch (subcmd) { | |
374 | case 0x03: | |
375 | dev_info(dev, "Ping\n"); | |
376 | break; | |
377 | case 0x04: | |
378 | dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n", | |
379 | data1, data2); | |
380 | break; | |
381 | case 0x06: | |
382 | dev_info(dev, "%s carrier mode and freq of " | |
383 | "0x%02x 0x%02x\n", inout, data1, data2); | |
384 | break; | |
385 | case 0x07: | |
386 | dev_info(dev, "Get carrier mode and freq\n"); | |
387 | break; | |
388 | case 0x08: | |
389 | dev_info(dev, "%s transmit blaster mask of 0x%02x\n", | |
390 | inout, data1); | |
391 | break; | |
392 | case 0x0c: | |
393 | /* value is in units of 50us, so x*50/100 or x/2 ms */ | |
394 | dev_info(dev, "%s receive timeout of %d ms\n", | |
395 | inout, ((data1 << 8) | data2) / 2); | |
396 | break; | |
397 | case 0x0d: | |
398 | dev_info(dev, "Get receive timeout\n"); | |
399 | break; | |
400 | case 0x13: | |
401 | dev_info(dev, "Get transmit blaster mask\n"); | |
402 | break; | |
403 | case 0x14: | |
404 | dev_info(dev, "%s %s-range receive sensor in use\n", | |
405 | inout, data1 == 0x02 ? "short" : "long"); | |
406 | break; | |
407 | case 0x15: | |
408 | if (len == 2) | |
409 | dev_info(dev, "Get receive sensor\n"); | |
410 | else | |
411 | dev_info(dev, "Received pulse count is %d\n", | |
412 | ((data1 << 8) | data2)); | |
413 | break; | |
414 | case 0xfe: | |
415 | dev_info(dev, "Error! Hardware is likely wedged...\n"); | |
416 | break; | |
417 | case 0x05: | |
418 | case 0x09: | |
419 | case 0x0f: | |
420 | default: | |
421 | dev_info(dev, "Unknown command 0x%02x 0x%02x\n", | |
422 | cmd, subcmd); | |
423 | break; | |
424 | } | |
425 | break; | |
426 | default: | |
427 | break; | |
428 | } | |
429 | } | |
430 | ||
431 | static void usb_async_callback(struct urb *urb, struct pt_regs *regs) | |
432 | { | |
433 | struct mceusb_dev *ir; | |
434 | int len; | |
435 | ||
436 | if (!urb) | |
437 | return; | |
438 | ||
439 | ir = urb->context; | |
440 | if (ir) { | |
441 | len = urb->actual_length; | |
442 | ||
443 | dev_dbg(ir->dev, "callback called (status=%d len=%d)\n", | |
444 | urb->status, len); | |
445 | ||
446 | if (debug) | |
447 | mceusb_dev_printdata(ir, urb->transfer_buffer, | |
448 | len, true); | |
449 | } | |
450 | ||
451 | } | |
452 | ||
453 | /* request incoming or send outgoing usb packet - used to initialize remote */ | |
454 | static void mce_request_packet(struct mceusb_dev *ir, | |
455 | struct usb_endpoint_descriptor *ep, | |
456 | unsigned char *data, int size, int urb_type) | |
457 | { | |
458 | int res; | |
459 | struct urb *async_urb; | |
460 | struct device *dev = ir->dev; | |
461 | unsigned char *async_buf; | |
462 | ||
463 | if (urb_type == MCEUSB_TX) { | |
464 | async_urb = usb_alloc_urb(0, GFP_KERNEL); | |
465 | if (unlikely(!async_urb)) { | |
466 | dev_err(dev, "Error, couldn't allocate urb!\n"); | |
467 | return; | |
468 | } | |
469 | ||
470 | async_buf = kzalloc(size, GFP_KERNEL); | |
471 | if (!async_buf) { | |
472 | dev_err(dev, "Error, couldn't allocate buf!\n"); | |
473 | usb_free_urb(async_urb); | |
474 | return; | |
475 | } | |
476 | ||
477 | /* outbound data */ | |
478 | usb_fill_int_urb(async_urb, ir->usbdev, | |
479 | usb_sndintpipe(ir->usbdev, ep->bEndpointAddress), | |
480 | async_buf, size, (usb_complete_t) usb_async_callback, | |
481 | ir, ep->bInterval); | |
482 | memcpy(async_buf, data, size); | |
483 | ||
484 | } else if (urb_type == MCEUSB_RX) { | |
485 | /* standard request */ | |
486 | async_urb = ir->urb_in; | |
487 | ir->send_flags = RECV_FLAG_IN_PROGRESS; | |
488 | ||
489 | } else { | |
490 | dev_err(dev, "Error! Unknown urb type %d\n", urb_type); | |
491 | return; | |
492 | } | |
493 | ||
494 | dev_dbg(dev, "receive request called (size=%#x)\n", size); | |
495 | ||
496 | async_urb->transfer_buffer_length = size; | |
497 | async_urb->dev = ir->usbdev; | |
498 | ||
499 | res = usb_submit_urb(async_urb, GFP_ATOMIC); | |
500 | if (res) { | |
501 | dev_dbg(dev, "receive request FAILED! (res=%d)\n", res); | |
502 | return; | |
503 | } | |
504 | dev_dbg(dev, "receive request complete (res=%d)\n", res); | |
505 | } | |
506 | ||
507 | static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size) | |
508 | { | |
509 | mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX); | |
510 | } | |
511 | ||
512 | static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size) | |
513 | { | |
514 | mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX); | |
515 | } | |
516 | ||
e23fb964 JW |
517 | /* Send data out the IR blaster port(s) */ |
518 | static int mceusb_tx_ir(void *priv, int *txbuf, u32 n) | |
519 | { | |
520 | struct mceusb_dev *ir = priv; | |
521 | int i, ret = 0; | |
522 | int count, cmdcount = 0; | |
523 | unsigned char *cmdbuf; /* MCE command buffer */ | |
524 | long signal_duration = 0; /* Singnal length in us */ | |
525 | struct timeval start_time, end_time; | |
526 | ||
527 | do_gettimeofday(&start_time); | |
528 | ||
529 | count = n / sizeof(int); | |
530 | ||
531 | cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL); | |
532 | if (!cmdbuf) | |
533 | return -ENOMEM; | |
534 | ||
535 | /* MCE tx init header */ | |
536 | cmdbuf[cmdcount++] = MCE_CONTROL_HEADER; | |
537 | cmdbuf[cmdcount++] = 0x08; | |
538 | cmdbuf[cmdcount++] = ir->tx_mask; | |
539 | ||
540 | /* Generate mce packet data */ | |
541 | for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) { | |
542 | signal_duration += txbuf[i]; | |
543 | txbuf[i] = txbuf[i] / MCE_TIME_UNIT; | |
544 | ||
545 | do { /* loop to support long pulses/spaces > 127*50us=6.35ms */ | |
546 | ||
547 | /* Insert mce packet header every 4th entry */ | |
548 | if ((cmdcount < MCE_CMDBUF_SIZE) && | |
549 | (cmdcount - MCE_TX_HEADER_LENGTH) % | |
550 | MCE_CODE_LENGTH == 0) | |
551 | cmdbuf[cmdcount++] = MCE_PACKET_HEADER; | |
552 | ||
553 | /* Insert mce packet data */ | |
554 | if (cmdcount < MCE_CMDBUF_SIZE) | |
555 | cmdbuf[cmdcount++] = | |
556 | (txbuf[i] < MCE_PULSE_BIT ? | |
557 | txbuf[i] : MCE_MAX_PULSE_LENGTH) | | |
558 | (i & 1 ? 0x00 : MCE_PULSE_BIT); | |
559 | else { | |
560 | ret = -EINVAL; | |
561 | goto out; | |
562 | } | |
563 | ||
564 | } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) && | |
565 | (txbuf[i] -= MCE_MAX_PULSE_LENGTH)); | |
566 | } | |
567 | ||
568 | /* Fix packet length in last header */ | |
569 | cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] = | |
570 | 0x80 + (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH - 1; | |
571 | ||
572 | /* Check if we have room for the empty packet at the end */ | |
573 | if (cmdcount >= MCE_CMDBUF_SIZE) { | |
574 | ret = -EINVAL; | |
575 | goto out; | |
576 | } | |
577 | ||
578 | /* All mce commands end with an empty packet (0x80) */ | |
579 | cmdbuf[cmdcount++] = 0x80; | |
580 | ||
581 | /* Transmit the command to the mce device */ | |
582 | mce_async_out(ir, cmdbuf, cmdcount); | |
583 | ||
584 | /* | |
585 | * The lircd gap calculation expects the write function to | |
586 | * wait the time it takes for the ircommand to be sent before | |
587 | * it returns. | |
588 | */ | |
589 | do_gettimeofday(&end_time); | |
590 | signal_duration -= (end_time.tv_usec - start_time.tv_usec) + | |
591 | (end_time.tv_sec - start_time.tv_sec) * 1000000; | |
592 | ||
593 | /* delay with the closest number of ticks */ | |
594 | set_current_state(TASK_INTERRUPTIBLE); | |
595 | schedule_timeout(usecs_to_jiffies(signal_duration)); | |
596 | ||
597 | out: | |
598 | kfree(cmdbuf); | |
599 | return ret ? ret : n; | |
600 | } | |
601 | ||
657290b6 JW |
602 | /* Sets active IR outputs -- mce devices typically (all?) have two */ |
603 | static int mceusb_set_tx_mask(void *priv, u32 mask) | |
604 | { | |
605 | struct mceusb_dev *ir = priv; | |
606 | ||
607 | if (ir->flags.tx_mask_inverted) | |
608 | ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1; | |
609 | else | |
610 | ir->tx_mask = mask; | |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
e23fb964 JW |
615 | /* Sets the send carrier frequency and mode */ |
616 | static int mceusb_set_tx_carrier(void *priv, u32 carrier) | |
617 | { | |
618 | struct mceusb_dev *ir = priv; | |
619 | int clk = 10000000; | |
620 | int prescaler = 0, divisor = 0; | |
621 | unsigned char cmdbuf[4] = { 0x9f, 0x06, 0x00, 0x00 }; | |
622 | ||
623 | /* Carrier has changed */ | |
624 | if (ir->carrier != carrier) { | |
625 | ||
626 | if (carrier == 0) { | |
627 | ir->carrier = carrier; | |
628 | cmdbuf[2] = 0x01; | |
629 | cmdbuf[3] = 0x80; | |
630 | dev_dbg(ir->dev, "%s: disabling carrier " | |
631 | "modulation\n", __func__); | |
632 | mce_async_out(ir, cmdbuf, sizeof(cmdbuf)); | |
633 | return carrier; | |
634 | } | |
635 | ||
636 | for (prescaler = 0; prescaler < 4; ++prescaler) { | |
637 | divisor = (clk >> (2 * prescaler)) / carrier; | |
638 | if (divisor <= 0xFF) { | |
639 | ir->carrier = carrier; | |
640 | cmdbuf[2] = prescaler; | |
641 | cmdbuf[3] = divisor; | |
642 | dev_dbg(ir->dev, "%s: requesting %u HZ " | |
643 | "carrier\n", __func__, carrier); | |
644 | ||
645 | /* Transmit new carrier to mce device */ | |
646 | mce_async_out(ir, cmdbuf, sizeof(cmdbuf)); | |
647 | return carrier; | |
648 | } | |
649 | } | |
650 | ||
651 | return -EINVAL; | |
652 | ||
653 | } | |
654 | ||
655 | return carrier; | |
656 | } | |
657 | ||
66e89522 JW |
658 | static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len) |
659 | { | |
660 | struct ir_raw_event rawir = { .pulse = false, .duration = 0 }; | |
661 | int i, start_index = 0; | |
d732a72d | 662 | u8 hdr = MCE_CONTROL_HEADER; |
66e89522 JW |
663 | |
664 | /* skip meaningless 0xb1 0x60 header bytes on orig receiver */ | |
665 | if (ir->flags.microsoft_gen1) | |
666 | start_index = 2; | |
667 | ||
668 | for (i = start_index; i < buf_len;) { | |
669 | if (ir->rem == 0) { | |
670 | /* decode mce packets of the form (84),AA,BB,CC,DD */ | |
671 | /* IR data packets can span USB messages - rem */ | |
d732a72d JW |
672 | hdr = ir->buf_in[i]; |
673 | ir->rem = (hdr & MCE_PACKET_LENGTH_MASK); | |
674 | ir->cmd = (hdr & ~MCE_PACKET_LENGTH_MASK); | |
66e89522 JW |
675 | dev_dbg(ir->dev, "New data. rem: 0x%02x, cmd: 0x%02x\n", |
676 | ir->rem, ir->cmd); | |
677 | i++; | |
678 | } | |
679 | ||
d732a72d JW |
680 | /* don't process MCE commands */ |
681 | if (hdr == MCE_CONTROL_HEADER || hdr == 0xff) { | |
66e89522 JW |
682 | ir->rem = 0; |
683 | return; | |
684 | } | |
685 | ||
686 | for (; (ir->rem > 0) && (i < buf_len); i++) { | |
687 | ir->rem--; | |
688 | ||
689 | rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0); | |
690 | rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK) | |
691 | * MCE_TIME_UNIT * 1000; | |
692 | ||
693 | if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) { | |
694 | if (ir->rawir.pulse == rawir.pulse) | |
695 | ir->rawir.duration += rawir.duration; | |
696 | else { | |
697 | ir->rawir.duration = rawir.duration; | |
698 | ir->rawir.pulse = rawir.pulse; | |
699 | } | |
700 | continue; | |
701 | } | |
702 | rawir.duration += ir->rawir.duration; | |
703 | ir->rawir.duration = 0; | |
704 | ir->rawir.pulse = rawir.pulse; | |
705 | ||
706 | dev_dbg(ir->dev, "Storing %s with duration %d\n", | |
707 | rawir.pulse ? "pulse" : "space", | |
708 | rawir.duration); | |
709 | ||
710 | ir_raw_event_store(ir->idev, &rawir); | |
711 | } | |
712 | ||
713 | if (ir->buf_in[i] == 0x80 || ir->buf_in[i] == 0x9f) | |
714 | ir->rem = 0; | |
715 | ||
716 | dev_dbg(ir->dev, "calling ir_raw_event_handle\n"); | |
717 | ir_raw_event_handle(ir->idev); | |
718 | } | |
719 | } | |
720 | ||
66e89522 JW |
721 | static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs) |
722 | { | |
723 | struct mceusb_dev *ir; | |
724 | int buf_len; | |
725 | ||
726 | if (!urb) | |
727 | return; | |
728 | ||
729 | ir = urb->context; | |
730 | if (!ir) { | |
731 | usb_unlink_urb(urb); | |
732 | return; | |
733 | } | |
734 | ||
735 | buf_len = urb->actual_length; | |
736 | ||
66e89522 JW |
737 | if (debug) |
738 | mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false); | |
739 | ||
740 | if (ir->send_flags == RECV_FLAG_IN_PROGRESS) { | |
741 | ir->send_flags = SEND_FLAG_COMPLETE; | |
742 | dev_dbg(&ir->irdev->dev, "setup answer received %d bytes\n", | |
743 | buf_len); | |
744 | } | |
745 | ||
746 | switch (urb->status) { | |
747 | /* success */ | |
748 | case 0: | |
749 | mceusb_process_ir_data(ir, buf_len); | |
750 | break; | |
751 | ||
752 | case -ECONNRESET: | |
753 | case -ENOENT: | |
754 | case -ESHUTDOWN: | |
755 | usb_unlink_urb(urb); | |
756 | return; | |
757 | ||
758 | case -EPIPE: | |
759 | default: | |
760 | break; | |
761 | } | |
762 | ||
763 | usb_submit_urb(urb, GFP_ATOMIC); | |
764 | } | |
765 | ||
766 | static void mceusb_gen1_init(struct mceusb_dev *ir) | |
767 | { | |
768 | int i, ret; | |
66e89522 JW |
769 | int partial = 0; |
770 | struct device *dev = ir->dev; | |
657290b6 JW |
771 | char *junk, *data; |
772 | ||
773 | junk = kmalloc(2 * USB_BUFLEN, GFP_KERNEL); | |
774 | if (!junk) { | |
775 | dev_err(dev, "%s: memory allocation failed!\n", __func__); | |
776 | return; | |
777 | } | |
778 | ||
779 | data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL); | |
780 | if (!data) { | |
781 | dev_err(dev, "%s: memory allocation failed!\n", __func__); | |
782 | kfree(junk); | |
783 | return; | |
784 | } | |
66e89522 JW |
785 | |
786 | /* | |
787 | * Clear off the first few messages. These look like calibration | |
788 | * or test data, I can't really tell. This also flushes in case | |
789 | * we have random ir data queued up. | |
790 | */ | |
657290b6 | 791 | for (i = 0; i < MCE_G1_INIT_MSGS; i++) |
66e89522 JW |
792 | usb_bulk_msg(ir->usbdev, |
793 | usb_rcvbulkpipe(ir->usbdev, | |
794 | ir->usb_ep_in->bEndpointAddress), | |
657290b6 | 795 | junk, sizeof(junk), &partial, HZ * 10); |
66e89522 JW |
796 | |
797 | /* Get Status */ | |
798 | ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0), | |
799 | USB_REQ_GET_STATUS, USB_DIR_IN, | |
657290b6 | 800 | 0, 0, data, USB_CTRL_MSG_SZ, HZ * 3); |
66e89522 JW |
801 | |
802 | /* ret = usb_get_status( ir->usbdev, 0, 0, data ); */ | |
803 | dev_dbg(dev, "%s - ret = %d status = 0x%x 0x%x\n", __func__, | |
804 | ret, data[0], data[1]); | |
805 | ||
806 | /* | |
807 | * This is a strange one. They issue a set address to the device | |
808 | * on the receive control pipe and expect a certain value pair back | |
809 | */ | |
657290b6 | 810 | memset(data, 0, sizeof(data)); |
66e89522 JW |
811 | |
812 | ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0), | |
813 | USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0, | |
657290b6 | 814 | data, USB_CTRL_MSG_SZ, HZ * 3); |
66e89522 JW |
815 | dev_dbg(dev, "%s - ret = %d\n", __func__, ret); |
816 | dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n", | |
817 | __func__, data[0], data[1]); | |
818 | ||
819 | /* set feature: bit rate 38400 bps */ | |
820 | ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0), | |
821 | USB_REQ_SET_FEATURE, USB_TYPE_VENDOR, | |
822 | 0xc04e, 0x0000, NULL, 0, HZ * 3); | |
823 | ||
824 | dev_dbg(dev, "%s - ret = %d\n", __func__, ret); | |
825 | ||
826 | /* bRequest 4: set char length to 8 bits */ | |
827 | ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0), | |
828 | 4, USB_TYPE_VENDOR, | |
829 | 0x0808, 0x0000, NULL, 0, HZ * 3); | |
830 | dev_dbg(dev, "%s - retB = %d\n", __func__, ret); | |
831 | ||
832 | /* bRequest 2: set handshaking to use DTR/DSR */ | |
833 | ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0), | |
834 | 2, USB_TYPE_VENDOR, | |
835 | 0x0000, 0x0100, NULL, 0, HZ * 3); | |
836 | dev_dbg(dev, "%s - retC = %d\n", __func__, ret); | |
657290b6 JW |
837 | |
838 | kfree(data); | |
839 | kfree(junk); | |
66e89522 JW |
840 | }; |
841 | ||
842 | static void mceusb_gen2_init(struct mceusb_dev *ir) | |
843 | { | |
844 | int maxp = ir->len_in; | |
845 | ||
846 | mce_sync_in(ir, NULL, maxp); | |
847 | mce_sync_in(ir, NULL, maxp); | |
848 | ||
e23fb964 JW |
849 | set_current_state(TASK_INTERRUPTIBLE); |
850 | schedule_timeout(msecs_to_jiffies(100)); | |
851 | ||
66e89522 JW |
852 | /* device reset */ |
853 | mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET)); | |
854 | mce_sync_in(ir, NULL, maxp); | |
855 | ||
856 | /* get hw/sw revision? */ | |
857 | mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION)); | |
858 | mce_sync_in(ir, NULL, maxp); | |
859 | ||
860 | /* unknown what this actually returns... */ | |
861 | mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN)); | |
862 | mce_sync_in(ir, NULL, maxp); | |
863 | } | |
864 | ||
865 | static void mceusb_gen3_init(struct mceusb_dev *ir) | |
866 | { | |
867 | int maxp = ir->len_in; | |
868 | ||
869 | mce_sync_in(ir, NULL, maxp); | |
870 | ||
871 | /* device reset */ | |
872 | mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET)); | |
873 | mce_sync_in(ir, NULL, maxp); | |
874 | ||
875 | /* get the carrier and frequency */ | |
876 | mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ)); | |
877 | mce_sync_in(ir, NULL, maxp); | |
878 | ||
879 | /* get the transmitter bitmask */ | |
880 | mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK)); | |
881 | mce_sync_in(ir, NULL, maxp); | |
882 | ||
883 | /* get receiver timeout value */ | |
884 | mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT)); | |
885 | mce_sync_in(ir, NULL, maxp); | |
886 | ||
887 | /* get receiver sensor setting */ | |
888 | mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR)); | |
889 | mce_sync_in(ir, NULL, maxp); | |
890 | } | |
891 | ||
892 | static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir) | |
893 | { | |
894 | struct input_dev *idev; | |
895 | struct ir_dev_props *props; | |
896 | struct ir_input_dev *irdev; | |
66e89522 JW |
897 | struct device *dev = ir->dev; |
898 | int ret = -ENODEV; | |
899 | ||
900 | idev = input_allocate_device(); | |
901 | if (!idev) { | |
902 | dev_err(dev, "remote input dev allocation failed\n"); | |
903 | goto idev_alloc_failed; | |
904 | } | |
905 | ||
906 | ret = -ENOMEM; | |
907 | props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL); | |
908 | if (!props) { | |
909 | dev_err(dev, "remote ir dev props allocation failed\n"); | |
910 | goto props_alloc_failed; | |
911 | } | |
912 | ||
913 | irdev = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL); | |
914 | if (!irdev) { | |
915 | dev_err(dev, "remote ir input dev allocation failed\n"); | |
916 | goto ir_dev_alloc_failed; | |
917 | } | |
918 | ||
e23fb964 | 919 | snprintf(ir->name, sizeof(ir->name), "Media Center Ed. eHome " |
66e89522 JW |
920 | "Infrared Remote Transceiver (%04x:%04x)", |
921 | le16_to_cpu(ir->usbdev->descriptor.idVendor), | |
922 | le16_to_cpu(ir->usbdev->descriptor.idProduct)); | |
923 | ||
66e89522 | 924 | idev->name = ir->name; |
66e89522 JW |
925 | usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys)); |
926 | strlcat(ir->phys, "/input0", sizeof(ir->phys)); | |
927 | idev->phys = ir->phys; | |
928 | ||
66e89522 JW |
929 | props->priv = ir; |
930 | props->driver_type = RC_DRIVER_IR_RAW; | |
931 | props->allowed_protos = IR_TYPE_ALL; | |
e23fb964 JW |
932 | props->s_tx_mask = mceusb_set_tx_mask; |
933 | props->s_tx_carrier = mceusb_set_tx_carrier; | |
934 | props->tx_ir = mceusb_tx_ir; | |
66e89522 JW |
935 | |
936 | ir->props = props; | |
937 | ir->irdev = irdev; | |
66e89522 JW |
938 | |
939 | input_set_drvdata(idev, irdev); | |
940 | ||
941 | ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME); | |
942 | if (ret < 0) { | |
943 | dev_err(dev, "remote input device register failed\n"); | |
944 | goto irdev_failed; | |
945 | } | |
946 | ||
947 | return idev; | |
948 | ||
949 | irdev_failed: | |
66e89522 JW |
950 | kfree(irdev); |
951 | ir_dev_alloc_failed: | |
952 | kfree(props); | |
953 | props_alloc_failed: | |
954 | input_free_device(idev); | |
955 | idev_alloc_failed: | |
956 | return NULL; | |
957 | } | |
958 | ||
959 | static int __devinit mceusb_dev_probe(struct usb_interface *intf, | |
960 | const struct usb_device_id *id) | |
961 | { | |
962 | struct usb_device *dev = interface_to_usbdev(intf); | |
963 | struct usb_host_interface *idesc; | |
964 | struct usb_endpoint_descriptor *ep = NULL; | |
965 | struct usb_endpoint_descriptor *ep_in = NULL; | |
966 | struct usb_endpoint_descriptor *ep_out = NULL; | |
967 | struct usb_host_config *config; | |
968 | struct mceusb_dev *ir = NULL; | |
d732a72d | 969 | int pipe, maxp, i; |
66e89522 JW |
970 | char buf[63], name[128] = ""; |
971 | bool is_gen3; | |
972 | bool is_microsoft_gen1; | |
657290b6 | 973 | bool tx_mask_inverted; |
66e89522 JW |
974 | |
975 | dev_dbg(&intf->dev, ": %s called\n", __func__); | |
976 | ||
977 | usb_reset_device(dev); | |
978 | ||
979 | config = dev->actconfig; | |
980 | idesc = intf->cur_altsetting; | |
981 | ||
982 | is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0; | |
983 | is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0; | |
657290b6 | 984 | tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1; |
66e89522 JW |
985 | |
986 | /* step through the endpoints to find first bulk in and out endpoint */ | |
987 | for (i = 0; i < idesc->desc.bNumEndpoints; ++i) { | |
988 | ep = &idesc->endpoint[i].desc; | |
989 | ||
990 | if ((ep_in == NULL) | |
991 | && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) | |
992 | == USB_DIR_IN) | |
993 | && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
994 | == USB_ENDPOINT_XFER_BULK) | |
995 | || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
996 | == USB_ENDPOINT_XFER_INT))) { | |
997 | ||
66e89522 JW |
998 | ep_in = ep; |
999 | ep_in->bmAttributes = USB_ENDPOINT_XFER_INT; | |
d732a72d JW |
1000 | ep_in->bInterval = 1; |
1001 | dev_dbg(&intf->dev, ": acceptable inbound endpoint " | |
1002 | "found\n"); | |
66e89522 JW |
1003 | } |
1004 | ||
1005 | if ((ep_out == NULL) | |
1006 | && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) | |
1007 | == USB_DIR_OUT) | |
1008 | && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
1009 | == USB_ENDPOINT_XFER_BULK) | |
1010 | || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
1011 | == USB_ENDPOINT_XFER_INT))) { | |
1012 | ||
66e89522 JW |
1013 | ep_out = ep; |
1014 | ep_out->bmAttributes = USB_ENDPOINT_XFER_INT; | |
d732a72d JW |
1015 | ep_out->bInterval = 1; |
1016 | dev_dbg(&intf->dev, ": acceptable outbound endpoint " | |
1017 | "found\n"); | |
66e89522 JW |
1018 | } |
1019 | } | |
1020 | if (ep_in == NULL) { | |
1021 | dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n"); | |
1022 | return -ENODEV; | |
1023 | } | |
1024 | ||
1025 | pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress); | |
1026 | maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); | |
1027 | ||
1028 | ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL); | |
1029 | if (!ir) | |
1030 | goto mem_alloc_fail; | |
1031 | ||
1032 | ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in); | |
1033 | if (!ir->buf_in) | |
1034 | goto buf_in_alloc_fail; | |
1035 | ||
1036 | ir->urb_in = usb_alloc_urb(0, GFP_KERNEL); | |
1037 | if (!ir->urb_in) | |
1038 | goto urb_in_alloc_fail; | |
1039 | ||
1040 | ir->usbdev = dev; | |
1041 | ir->dev = &intf->dev; | |
1042 | ir->len_in = maxp; | |
1043 | ir->flags.gen3 = is_gen3; | |
1044 | ir->flags.microsoft_gen1 = is_microsoft_gen1; | |
657290b6 | 1045 | ir->flags.tx_mask_inverted = tx_mask_inverted; |
66e89522 JW |
1046 | |
1047 | /* Saving usb interface data for use by the transmitter routine */ | |
1048 | ir->usb_ep_in = ep_in; | |
1049 | ir->usb_ep_out = ep_out; | |
1050 | ||
1051 | if (dev->descriptor.iManufacturer | |
1052 | && usb_string(dev, dev->descriptor.iManufacturer, | |
1053 | buf, sizeof(buf)) > 0) | |
1054 | strlcpy(name, buf, sizeof(name)); | |
1055 | if (dev->descriptor.iProduct | |
1056 | && usb_string(dev, dev->descriptor.iProduct, | |
1057 | buf, sizeof(buf)) > 0) | |
1058 | snprintf(name + strlen(name), sizeof(name) - strlen(name), | |
1059 | " %s", buf); | |
1060 | ||
1061 | ir->idev = mceusb_init_input_dev(ir); | |
1062 | if (!ir->idev) | |
1063 | goto input_dev_fail; | |
1064 | ||
1065 | /* inbound data */ | |
1066 | usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, | |
1067 | maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval); | |
1068 | ir->urb_in->transfer_dma = ir->dma_in; | |
1069 | ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
1070 | ||
66e89522 JW |
1071 | /* initialize device */ |
1072 | if (ir->flags.gen3) | |
1073 | mceusb_gen3_init(ir); | |
1074 | ||
1075 | else if (ir->flags.microsoft_gen1) | |
1076 | mceusb_gen1_init(ir); | |
1077 | ||
1078 | else | |
1079 | mceusb_gen2_init(ir); | |
1080 | ||
1081 | mce_sync_in(ir, NULL, maxp); | |
1082 | ||
657290b6 | 1083 | mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK); |
66e89522 JW |
1084 | |
1085 | usb_set_intfdata(intf, ir); | |
1086 | ||
1087 | dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name, | |
1088 | dev->bus->busnum, dev->devnum); | |
1089 | ||
1090 | return 0; | |
1091 | ||
1092 | /* Error-handling path */ | |
1093 | input_dev_fail: | |
1094 | usb_free_urb(ir->urb_in); | |
1095 | urb_in_alloc_fail: | |
1096 | usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in); | |
1097 | buf_in_alloc_fail: | |
1098 | kfree(ir); | |
1099 | mem_alloc_fail: | |
1100 | dev_err(&intf->dev, "%s: device setup failed!\n", __func__); | |
1101 | ||
1102 | return -ENOMEM; | |
1103 | } | |
1104 | ||
1105 | ||
1106 | static void __devexit mceusb_dev_disconnect(struct usb_interface *intf) | |
1107 | { | |
1108 | struct usb_device *dev = interface_to_usbdev(intf); | |
1109 | struct mceusb_dev *ir = usb_get_intfdata(intf); | |
1110 | ||
1111 | usb_set_intfdata(intf, NULL); | |
1112 | ||
1113 | if (!ir) | |
1114 | return; | |
1115 | ||
1116 | ir->usbdev = NULL; | |
bd3881b1 | 1117 | ir_input_unregister(ir->idev); |
66e89522 JW |
1118 | usb_kill_urb(ir->urb_in); |
1119 | usb_free_urb(ir->urb_in); | |
1120 | usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in); | |
1121 | ||
1122 | kfree(ir); | |
1123 | } | |
1124 | ||
1125 | static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message) | |
1126 | { | |
1127 | struct mceusb_dev *ir = usb_get_intfdata(intf); | |
1128 | dev_info(ir->dev, "suspend\n"); | |
1129 | usb_kill_urb(ir->urb_in); | |
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | static int mceusb_dev_resume(struct usb_interface *intf) | |
1134 | { | |
1135 | struct mceusb_dev *ir = usb_get_intfdata(intf); | |
1136 | dev_info(ir->dev, "resume\n"); | |
1137 | if (usb_submit_urb(ir->urb_in, GFP_ATOMIC)) | |
1138 | return -EIO; | |
1139 | return 0; | |
1140 | } | |
1141 | ||
1142 | static struct usb_driver mceusb_dev_driver = { | |
1143 | .name = DRIVER_NAME, | |
1144 | .probe = mceusb_dev_probe, | |
1145 | .disconnect = mceusb_dev_disconnect, | |
1146 | .suspend = mceusb_dev_suspend, | |
1147 | .resume = mceusb_dev_resume, | |
1148 | .reset_resume = mceusb_dev_resume, | |
1149 | .id_table = mceusb_dev_table | |
1150 | }; | |
1151 | ||
1152 | static int __init mceusb_dev_init(void) | |
1153 | { | |
1154 | int ret; | |
1155 | ||
1156 | ret = usb_register(&mceusb_dev_driver); | |
1157 | if (ret < 0) | |
1158 | printk(KERN_ERR DRIVER_NAME | |
1159 | ": usb register failed, result = %d\n", ret); | |
1160 | ||
1161 | return ret; | |
1162 | } | |
1163 | ||
1164 | static void __exit mceusb_dev_exit(void) | |
1165 | { | |
1166 | usb_deregister(&mceusb_dev_driver); | |
1167 | } | |
1168 | ||
1169 | module_init(mceusb_dev_init); | |
1170 | module_exit(mceusb_dev_exit); | |
1171 | ||
1172 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1173 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
1174 | MODULE_LICENSE("GPL"); | |
1175 | MODULE_DEVICE_TABLE(usb, mceusb_dev_table); | |
1176 | ||
1177 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
1178 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |