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