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