sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / rc / mceusb.c
blob9bf69179eee0e7b21b11b6e0a4b1e220254f8181
1 /*
2 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
4 * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com>
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.
18 * Updated in July of 2011 with the aid of Microsoft's official
19 * remote/transceiver requirements and specification document, found at
20 * download.microsoft.com, title
21 * Windows-Media-Center-RC-IR-Collection-Green-Button-Specification-03-08-2011-V2.pdf
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include <linux/device.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/usb.h>
44 #include <linux/usb/input.h>
45 #include <linux/pm_wakeup.h>
46 #include <media/rc-core.h>
48 #define DRIVER_VERSION "1.92"
49 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
50 #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
51 "device driver"
52 #define DRIVER_NAME "mceusb"
54 #define USB_BUFLEN 32 /* USB reception buffer length */
55 #define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
56 #define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
58 /* MCE constants */
59 #define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
60 #define MCE_TIME_UNIT 50 /* Approx 50us resolution */
61 #define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
62 #define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
63 #define MCE_IRDATA_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
64 #define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
65 #define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
66 #define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
67 #define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
68 #define MCE_PULSE_MASK 0x7f /* Pulse mask */
69 #define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
72 * The interface between the host and the IR hardware is command-response
73 * based. All commands and responses have a consistent format, where a lead
74 * byte always identifies the type of data following it. The lead byte has
75 * a port value in the 3 highest bits and a length value in the 5 lowest
76 * bits.
78 * The length field is overloaded, with a value of 11111 indicating that the
79 * following byte is a command or response code, and the length of the entire
80 * message is determined by the code. If the length field is not 11111, then
81 * it specifies the number of bytes of port data that follow.
83 #define MCE_CMD 0x1f
84 #define MCE_PORT_IR 0x4 /* (0x4 << 5) | MCE_CMD = 0x9f */
85 #define MCE_PORT_SYS 0x7 /* (0x7 << 5) | MCE_CMD = 0xff */
86 #define MCE_PORT_SER 0x6 /* 0xc0 thru 0xdf flush & 0x1f bytes */
87 #define MCE_PORT_MASK 0xe0 /* Mask out command bits */
89 /* Command port headers */
90 #define MCE_CMD_PORT_IR 0x9f /* IR-related cmd/rsp */
91 #define MCE_CMD_PORT_SYS 0xff /* System (non-IR) device cmd/rsp */
93 /* Commands that set device state (2-4 bytes in length) */
94 #define MCE_CMD_RESET 0xfe /* Reset device, 2 bytes */
95 #define MCE_CMD_RESUME 0xaa /* Resume device after error, 2 bytes */
96 #define MCE_CMD_SETIRCFS 0x06 /* Set tx carrier, 4 bytes */
97 #define MCE_CMD_SETIRTIMEOUT 0x0c /* Set timeout, 4 bytes */
98 #define MCE_CMD_SETIRTXPORTS 0x08 /* Set tx ports, 3 bytes */
99 #define MCE_CMD_SETIRRXPORTEN 0x14 /* Set rx ports, 3 bytes */
100 #define MCE_CMD_FLASHLED 0x23 /* Flash receiver LED, 2 bytes */
102 /* Commands that query device state (all 2 bytes, unless noted) */
103 #define MCE_CMD_GETIRCFS 0x07 /* Get carrier */
104 #define MCE_CMD_GETIRTIMEOUT 0x0d /* Get timeout */
105 #define MCE_CMD_GETIRTXPORTS 0x13 /* Get tx ports */
106 #define MCE_CMD_GETIRRXPORTEN 0x15 /* Get rx ports */
107 #define MCE_CMD_GETPORTSTATUS 0x11 /* Get tx port status, 3 bytes */
108 #define MCE_CMD_GETIRNUMPORTS 0x16 /* Get number of ports */
109 #define MCE_CMD_GETWAKESOURCE 0x17 /* Get wake source */
110 #define MCE_CMD_GETEMVER 0x22 /* Get emulator interface version */
111 #define MCE_CMD_GETDEVDETAILS 0x21 /* Get device details (em ver2 only) */
112 #define MCE_CMD_GETWAKESUPPORT 0x20 /* Get wake details (em ver2 only) */
113 #define MCE_CMD_GETWAKEVERSION 0x18 /* Get wake pattern (em ver2 only) */
115 /* Misc commands */
116 #define MCE_CMD_NOP 0xff /* No operation */
118 /* Responses to commands (non-error cases) */
119 #define MCE_RSP_EQIRCFS 0x06 /* tx carrier, 4 bytes */
120 #define MCE_RSP_EQIRTIMEOUT 0x0c /* rx timeout, 4 bytes */
121 #define MCE_RSP_GETWAKESOURCE 0x17 /* wake source, 3 bytes */
122 #define MCE_RSP_EQIRTXPORTS 0x08 /* tx port mask, 3 bytes */
123 #define MCE_RSP_EQIRRXPORTEN 0x14 /* rx port mask, 3 bytes */
124 #define MCE_RSP_GETPORTSTATUS 0x11 /* tx port status, 7 bytes */
125 #define MCE_RSP_EQIRRXCFCNT 0x15 /* rx carrier count, 4 bytes */
126 #define MCE_RSP_EQIRNUMPORTS 0x16 /* number of ports, 4 bytes */
127 #define MCE_RSP_EQWAKESUPPORT 0x20 /* wake capabilities, 3 bytes */
128 #define MCE_RSP_EQWAKEVERSION 0x18 /* wake pattern details, 6 bytes */
129 #define MCE_RSP_EQDEVDETAILS 0x21 /* device capabilities, 3 bytes */
130 #define MCE_RSP_EQEMVER 0x22 /* emulator interface ver, 3 bytes */
131 #define MCE_RSP_FLASHLED 0x23 /* success flashing LED, 2 bytes */
133 /* Responses to error cases, must send MCE_CMD_RESUME to clear them */
134 #define MCE_RSP_CMD_ILLEGAL 0xfe /* illegal command for port, 2 bytes */
135 #define MCE_RSP_TX_TIMEOUT 0x81 /* tx timed out, 2 bytes */
137 /* Misc commands/responses not defined in the MCE remote/transceiver spec */
138 #define MCE_CMD_SIG_END 0x01 /* End of signal */
139 #define MCE_CMD_PING 0x03 /* Ping device */
140 #define MCE_CMD_UNKNOWN 0x04 /* Unknown */
141 #define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
142 #define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
143 #define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
144 #define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
145 #define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
146 #define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
147 #define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
148 #define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
149 #define MCE_CMD_NULL 0x00 /* These show up various places... */
151 /* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
152 * then we're looking at a raw IR data sample */
153 #define MCE_COMMAND_IRDATA 0x80
154 #define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
156 #define VENDOR_PHILIPS 0x0471
157 #define VENDOR_SMK 0x0609
158 #define VENDOR_TATUNG 0x1460
159 #define VENDOR_GATEWAY 0x107b
160 #define VENDOR_SHUTTLE 0x1308
161 #define VENDOR_SHUTTLE2 0x051c
162 #define VENDOR_MITSUMI 0x03ee
163 #define VENDOR_TOPSEED 0x1784
164 #define VENDOR_RICAVISION 0x179d
165 #define VENDOR_ITRON 0x195d
166 #define VENDOR_FIC 0x1509
167 #define VENDOR_LG 0x043e
168 #define VENDOR_MICROSOFT 0x045e
169 #define VENDOR_FORMOSA 0x147a
170 #define VENDOR_FINTEK 0x1934
171 #define VENDOR_PINNACLE 0x2304
172 #define VENDOR_ECS 0x1019
173 #define VENDOR_WISTRON 0x0fb8
174 #define VENDOR_COMPRO 0x185b
175 #define VENDOR_NORTHSTAR 0x04eb
176 #define VENDOR_REALTEK 0x0bda
177 #define VENDOR_TIVO 0x105a
178 #define VENDOR_CONEXANT 0x0572
179 #define VENDOR_TWISTEDMELON 0x2596
180 #define VENDOR_HAUPPAUGE 0x2040
181 #define VENDOR_PCTV 0x2013
182 #define VENDOR_ADAPTEC 0x03f3
184 enum mceusb_model_type {
185 MCE_GEN2 = 0, /* Most boards */
186 MCE_GEN1,
187 MCE_GEN3,
188 MCE_GEN2_TX_INV,
189 POLARIS_EVK,
190 CX_HYBRID_TV,
191 MULTIFUNCTION,
192 TIVO_KIT,
193 MCE_GEN2_NO_TX,
194 HAUPPAUGE_CX_HYBRID_TV,
197 struct mceusb_model {
198 u32 mce_gen1:1;
199 u32 mce_gen2:1;
200 u32 mce_gen3:1;
201 u32 tx_mask_normal:1;
202 u32 no_tx:1;
204 int ir_intfnum;
206 const char *rc_map; /* Allow specify a per-board map */
207 const char *name; /* per-board name */
210 static const struct mceusb_model mceusb_model[] = {
211 [MCE_GEN1] = {
212 .mce_gen1 = 1,
213 .tx_mask_normal = 1,
215 [MCE_GEN2] = {
216 .mce_gen2 = 1,
218 [MCE_GEN2_NO_TX] = {
219 .mce_gen2 = 1,
220 .no_tx = 1,
222 [MCE_GEN2_TX_INV] = {
223 .mce_gen2 = 1,
224 .tx_mask_normal = 1,
226 [MCE_GEN3] = {
227 .mce_gen3 = 1,
228 .tx_mask_normal = 1,
230 [POLARIS_EVK] = {
232 * In fact, the EVK is shipped without
233 * remotes, but we should have something handy,
234 * to allow testing it
236 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
238 [CX_HYBRID_TV] = {
239 .no_tx = 1, /* tx isn't wired up at all */
240 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
242 [HAUPPAUGE_CX_HYBRID_TV] = {
243 .no_tx = 1, /* eeprom says it has no tx */
244 .name = "Conexant Hybrid TV (cx231xx) MCE IR no TX",
246 [MULTIFUNCTION] = {
247 .mce_gen2 = 1,
248 .ir_intfnum = 2,
250 [TIVO_KIT] = {
251 .mce_gen2 = 1,
252 .rc_map = RC_MAP_TIVO,
256 static struct usb_device_id mceusb_dev_table[] = {
257 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
258 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
259 .driver_info = MCE_GEN1 },
260 /* Philips Infrared Transceiver - Sahara branded */
261 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
262 /* Philips Infrared Transceiver - HP branded */
263 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
264 .driver_info = MCE_GEN2_TX_INV },
265 /* Philips SRM5100 */
266 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
267 /* Philips Infrared Transceiver - Omaura */
268 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
269 /* Philips Infrared Transceiver - Spinel plus */
270 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
271 /* Philips eHome Infrared Transceiver */
272 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
273 /* Philips/Spinel plus IR transceiver for ASUS */
274 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
275 /* Philips/Spinel plus IR transceiver for ASUS */
276 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
277 /* Philips IR transceiver (Dell branded) */
278 { USB_DEVICE(VENDOR_PHILIPS, 0x2093),
279 .driver_info = MCE_GEN2_TX_INV },
280 /* Realtek MCE IR Receiver and card reader */
281 { USB_DEVICE(VENDOR_REALTEK, 0x0161),
282 .driver_info = MULTIFUNCTION },
283 /* SMK/Toshiba G83C0004D410 */
284 { USB_DEVICE(VENDOR_SMK, 0x031d),
285 .driver_info = MCE_GEN2_TX_INV },
286 /* SMK eHome Infrared Transceiver (Sony VAIO) */
287 { USB_DEVICE(VENDOR_SMK, 0x0322),
288 .driver_info = MCE_GEN2_TX_INV },
289 /* bundled with Hauppauge PVR-150 */
290 { USB_DEVICE(VENDOR_SMK, 0x0334),
291 .driver_info = MCE_GEN2_TX_INV },
292 /* SMK eHome Infrared Transceiver */
293 { USB_DEVICE(VENDOR_SMK, 0x0338) },
294 /* SMK/I-O Data GV-MC7/RCKIT Receiver */
295 { USB_DEVICE(VENDOR_SMK, 0x0353),
296 .driver_info = MCE_GEN2_NO_TX },
297 /* SMK RXX6000 Infrared Receiver */
298 { USB_DEVICE(VENDOR_SMK, 0x0357),
299 .driver_info = MCE_GEN2_NO_TX },
300 /* Tatung eHome Infrared Transceiver */
301 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
302 /* Shuttle eHome Infrared Transceiver */
303 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
304 /* Shuttle eHome Infrared Transceiver */
305 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
306 /* Gateway eHome Infrared Transceiver */
307 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
308 /* Mitsumi */
309 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
310 /* Topseed eHome Infrared Transceiver */
311 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
312 .driver_info = MCE_GEN2_TX_INV },
313 /* Topseed HP eHome Infrared Transceiver */
314 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
315 .driver_info = MCE_GEN2_TX_INV },
316 /* Topseed eHome Infrared Transceiver */
317 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
318 .driver_info = MCE_GEN2_TX_INV },
319 /* Topseed eHome Infrared Transceiver */
320 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
321 .driver_info = MCE_GEN3 },
322 /* Topseed eHome Infrared Transceiver */
323 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
324 .driver_info = MCE_GEN2_TX_INV },
325 /* Topseed eHome Infrared Transceiver */
326 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
327 .driver_info = MCE_GEN3 },
328 /* Ricavision internal Infrared Transceiver */
329 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
330 /* Itron ione Libra Q-11 */
331 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
332 /* FIC eHome Infrared Transceiver */
333 { USB_DEVICE(VENDOR_FIC, 0x9242) },
334 /* LG eHome Infrared Transceiver */
335 { USB_DEVICE(VENDOR_LG, 0x9803) },
336 /* Microsoft MCE Infrared Transceiver */
337 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
338 /* Formosa eHome Infrared Transceiver */
339 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
340 /* Formosa21 / eHome Infrared Receiver */
341 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
342 /* Formosa aim / Trust MCE Infrared Receiver */
343 { USB_DEVICE(VENDOR_FORMOSA, 0xe017),
344 .driver_info = MCE_GEN2_NO_TX },
345 /* Formosa Industrial Computing / Beanbag Emulation Device */
346 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
347 /* Formosa21 / eHome Infrared Receiver */
348 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
349 /* Formosa Industrial Computing AIM IR605/A */
350 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
351 /* Formosa Industrial Computing */
352 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
353 /* Formosa Industrial Computing */
354 { USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
355 /* Fintek eHome Infrared Transceiver (HP branded) */
356 { USB_DEVICE(VENDOR_FINTEK, 0x5168),
357 .driver_info = MCE_GEN2_TX_INV },
358 /* Fintek eHome Infrared Transceiver */
359 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
360 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
361 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
362 /* Pinnacle Remote Kit */
363 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
364 .driver_info = MCE_GEN3 },
365 /* Elitegroup Computer Systems IR */
366 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
367 /* Wistron Corp. eHome Infrared Receiver */
368 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
369 /* Compro K100 */
370 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
371 /* Compro K100 v2 */
372 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
373 /* Northstar Systems, Inc. eHome Infrared Transceiver */
374 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
375 /* TiVo PC IR Receiver */
376 { USB_DEVICE(VENDOR_TIVO, 0x2000),
377 .driver_info = TIVO_KIT },
378 /* Conexant Hybrid TV "Shelby" Polaris SDK */
379 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
380 .driver_info = POLARIS_EVK },
381 /* Conexant Hybrid TV RDU253S Polaris */
382 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
383 .driver_info = CX_HYBRID_TV },
384 /* Twisted Melon Inc. - Manta Mini Receiver */
385 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
386 /* Twisted Melon Inc. - Manta Pico Receiver */
387 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
388 /* Twisted Melon Inc. - Manta Transceiver */
389 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
390 /* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
391 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb130),
392 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
393 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb131),
394 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
395 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb138),
396 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
397 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb139),
398 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
399 { USB_DEVICE(VENDOR_PCTV, 0x0259),
400 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
401 { USB_DEVICE(VENDOR_PCTV, 0x025e),
402 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
403 /* Adaptec / HP eHome Receiver */
404 { USB_DEVICE(VENDOR_ADAPTEC, 0x0094) },
406 /* Terminating entry */
410 /* data structure for each usb transceiver */
411 struct mceusb_dev {
412 /* ir-core bits */
413 struct rc_dev *rc;
415 /* optional features we can enable */
416 bool learning_enabled;
418 /* core device bits */
419 struct device *dev;
421 /* usb */
422 struct usb_device *usbdev;
423 struct urb *urb_in;
424 struct usb_endpoint_descriptor *usb_ep_out;
426 /* buffers and dma */
427 unsigned char *buf_in;
428 unsigned int len_in;
429 dma_addr_t dma_in;
431 enum {
432 CMD_HEADER = 0,
433 SUBCMD,
434 CMD_DATA,
435 PARSE_IRDATA,
436 } parser_state;
438 u8 cmd, rem; /* Remaining IR data bytes in packet */
440 struct {
441 u32 connected:1;
442 u32 tx_mask_normal:1;
443 u32 microsoft_gen1:1;
444 u32 no_tx:1;
445 } flags;
447 /* transmit support */
448 u32 carrier;
449 unsigned char tx_mask;
451 char name[128];
452 char phys[64];
453 enum mceusb_model_type model;
455 bool need_reset; /* flag to issue a device resume cmd */
456 u8 emver; /* emulator interface version */
457 u8 num_txports; /* number of transmit ports */
458 u8 num_rxports; /* number of receive sensors */
459 u8 txports_cabled; /* bitmask of transmitters with cable */
460 u8 rxports_active; /* bitmask of active receive sensors */
463 /* MCE Device Command Strings, generally a port and command pair */
464 static char DEVICE_RESUME[] = {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
465 MCE_CMD_RESUME};
466 static char GET_REVISION[] = {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
467 static char GET_EMVER[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
468 static char GET_WAKEVERSION[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
469 static char FLASH_LED[] = {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
470 static char GET_UNKNOWN2[] = {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
471 static char GET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
472 static char GET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
473 static char GET_NUM_PORTS[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
474 static char GET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
475 static char GET_RX_SENSOR[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
476 /* sub in desired values in lower byte or bytes for full command */
477 /* FIXME: make use of these for transmit.
478 static char SET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR,
479 MCE_CMD_SETIRCFS, 0x00, 0x00};
480 static char SET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
481 static char SET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR,
482 MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
483 static char SET_RX_SENSOR[] = {MCE_CMD_PORT_IR,
484 MCE_RSP_EQIRRXPORTEN, 0x00};
487 static int mceusb_cmd_datasize(u8 cmd, u8 subcmd)
489 int datasize = 0;
491 switch (cmd) {
492 case MCE_CMD_NULL:
493 if (subcmd == MCE_CMD_PORT_SYS)
494 datasize = 1;
495 break;
496 case MCE_CMD_PORT_SYS:
497 switch (subcmd) {
498 case MCE_RSP_GETPORTSTATUS:
499 datasize = 5;
500 break;
501 case MCE_RSP_EQWAKEVERSION:
502 datasize = 4;
503 break;
504 case MCE_CMD_G_REVISION:
505 datasize = 2;
506 break;
507 case MCE_RSP_EQWAKESUPPORT:
508 case MCE_RSP_GETWAKESOURCE:
509 case MCE_RSP_EQDEVDETAILS:
510 case MCE_RSP_EQEMVER:
511 datasize = 1;
512 break;
514 case MCE_CMD_PORT_IR:
515 switch (subcmd) {
516 case MCE_CMD_UNKNOWN:
517 case MCE_RSP_EQIRCFS:
518 case MCE_RSP_EQIRTIMEOUT:
519 case MCE_RSP_EQIRRXCFCNT:
520 case MCE_RSP_EQIRNUMPORTS:
521 datasize = 2;
522 break;
523 case MCE_CMD_SIG_END:
524 case MCE_RSP_EQIRTXPORTS:
525 case MCE_RSP_EQIRRXPORTEN:
526 datasize = 1;
527 break;
530 return datasize;
533 static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
534 int offset, int len, bool out)
536 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
537 char *inout;
538 u8 cmd, subcmd, data1, data2, data3, data4;
539 struct device *dev = ir->dev;
540 int start, skip = 0;
541 u32 carrier, period;
543 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
544 if (ir->flags.microsoft_gen1 && !out && !offset)
545 skip = 2;
547 if (len <= skip)
548 return;
550 dev_dbg(dev, "%cx data: %*ph (length=%d)",
551 (out ? 't' : 'r'), min(len, USB_BUFLEN), buf, len);
553 inout = out ? "Request" : "Got";
555 start = offset + skip;
556 cmd = buf[start] & 0xff;
557 subcmd = buf[start + 1] & 0xff;
558 data1 = buf[start + 2] & 0xff;
559 data2 = buf[start + 3] & 0xff;
560 data3 = buf[start + 4] & 0xff;
561 data4 = buf[start + 5] & 0xff;
563 switch (cmd) {
564 case MCE_CMD_NULL:
565 if (subcmd == MCE_CMD_NULL)
566 break;
567 if ((subcmd == MCE_CMD_PORT_SYS) &&
568 (data1 == MCE_CMD_RESUME))
569 dev_dbg(dev, "Device resume requested");
570 else
571 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
572 cmd, subcmd);
573 break;
574 case MCE_CMD_PORT_SYS:
575 switch (subcmd) {
576 case MCE_RSP_EQEMVER:
577 if (!out)
578 dev_dbg(dev, "Emulator interface version %x",
579 data1);
580 break;
581 case MCE_CMD_G_REVISION:
582 if (len == 2)
583 dev_dbg(dev, "Get hw/sw rev?");
584 else
585 dev_dbg(dev, "hw/sw rev %*ph",
586 4, &buf[start + 2]);
587 break;
588 case MCE_CMD_RESUME:
589 dev_dbg(dev, "Device resume requested");
590 break;
591 case MCE_RSP_CMD_ILLEGAL:
592 dev_dbg(dev, "Illegal PORT_SYS command");
593 break;
594 case MCE_RSP_EQWAKEVERSION:
595 if (!out)
596 dev_dbg(dev, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
597 data1, data2, data3, data4);
598 break;
599 case MCE_RSP_GETPORTSTATUS:
600 if (!out)
601 /* We use data1 + 1 here, to match hw labels */
602 dev_dbg(dev, "TX port %d: blaster is%s connected",
603 data1 + 1, data4 ? " not" : "");
604 break;
605 case MCE_CMD_FLASHLED:
606 dev_dbg(dev, "Attempting to flash LED");
607 break;
608 default:
609 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
610 cmd, subcmd);
611 break;
613 break;
614 case MCE_CMD_PORT_IR:
615 switch (subcmd) {
616 case MCE_CMD_SIG_END:
617 dev_dbg(dev, "End of signal");
618 break;
619 case MCE_CMD_PING:
620 dev_dbg(dev, "Ping");
621 break;
622 case MCE_CMD_UNKNOWN:
623 dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x",
624 data1, data2);
625 break;
626 case MCE_RSP_EQIRCFS:
627 period = DIV_ROUND_CLOSEST(
628 (1U << data1 * 2) * (data2 + 1), 10);
629 if (!period)
630 break;
631 carrier = (1000 * 1000) / period;
632 dev_dbg(dev, "%s carrier of %u Hz (period %uus)",
633 inout, carrier, period);
634 break;
635 case MCE_CMD_GETIRCFS:
636 dev_dbg(dev, "Get carrier mode and freq");
637 break;
638 case MCE_RSP_EQIRTXPORTS:
639 dev_dbg(dev, "%s transmit blaster mask of 0x%02x",
640 inout, data1);
641 break;
642 case MCE_RSP_EQIRTIMEOUT:
643 /* value is in units of 50us, so x*50/1000 ms */
644 period = ((data1 << 8) | data2) * MCE_TIME_UNIT / 1000;
645 dev_dbg(dev, "%s receive timeout of %d ms",
646 inout, period);
647 break;
648 case MCE_CMD_GETIRTIMEOUT:
649 dev_dbg(dev, "Get receive timeout");
650 break;
651 case MCE_CMD_GETIRTXPORTS:
652 dev_dbg(dev, "Get transmit blaster mask");
653 break;
654 case MCE_RSP_EQIRRXPORTEN:
655 dev_dbg(dev, "%s %s-range receive sensor in use",
656 inout, data1 == 0x02 ? "short" : "long");
657 break;
658 case MCE_CMD_GETIRRXPORTEN:
659 /* aka MCE_RSP_EQIRRXCFCNT */
660 if (out)
661 dev_dbg(dev, "Get receive sensor");
662 else if (ir->learning_enabled)
663 dev_dbg(dev, "RX pulse count: %d",
664 ((data1 << 8) | data2));
665 break;
666 case MCE_RSP_EQIRNUMPORTS:
667 if (out)
668 break;
669 dev_dbg(dev, "Num TX ports: %x, num RX ports: %x",
670 data1, data2);
671 break;
672 case MCE_RSP_CMD_ILLEGAL:
673 dev_dbg(dev, "Illegal PORT_IR command");
674 break;
675 default:
676 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
677 cmd, subcmd);
678 break;
680 break;
681 default:
682 break;
685 if (cmd == MCE_IRDATA_TRAILER)
686 dev_dbg(dev, "End of raw IR data");
687 else if ((cmd != MCE_CMD_PORT_IR) &&
688 ((cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA))
689 dev_dbg(dev, "Raw IR data, %d pulse/space samples", ir->rem);
690 #endif
693 static void mce_async_callback(struct urb *urb)
695 struct mceusb_dev *ir;
696 int len;
698 if (!urb)
699 return;
701 ir = urb->context;
703 switch (urb->status) {
704 /* success */
705 case 0:
706 len = urb->actual_length;
708 mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
709 break;
711 case -ECONNRESET:
712 case -ENOENT:
713 case -EILSEQ:
714 case -ESHUTDOWN:
715 break;
717 case -EPIPE:
718 default:
719 dev_err(ir->dev, "Error: request urb status = %d", urb->status);
720 break;
723 /* the transfer buffer and urb were allocated in mce_request_packet */
724 kfree(urb->transfer_buffer);
725 usb_free_urb(urb);
728 /* request incoming or send outgoing usb packet - used to initialize remote */
729 static void mce_request_packet(struct mceusb_dev *ir, unsigned char *data,
730 int size)
732 int res, pipe;
733 struct urb *async_urb;
734 struct device *dev = ir->dev;
735 unsigned char *async_buf;
737 async_urb = usb_alloc_urb(0, GFP_KERNEL);
738 if (unlikely(!async_urb)) {
739 dev_err(dev, "Error, couldn't allocate urb!\n");
740 return;
743 async_buf = kmalloc(size, GFP_KERNEL);
744 if (!async_buf) {
745 usb_free_urb(async_urb);
746 return;
749 /* outbound data */
750 if (usb_endpoint_xfer_int(ir->usb_ep_out)) {
751 pipe = usb_sndintpipe(ir->usbdev,
752 ir->usb_ep_out->bEndpointAddress);
753 usb_fill_int_urb(async_urb, ir->usbdev, pipe, async_buf,
754 size, mce_async_callback, ir,
755 ir->usb_ep_out->bInterval);
756 } else {
757 pipe = usb_sndbulkpipe(ir->usbdev,
758 ir->usb_ep_out->bEndpointAddress);
759 usb_fill_bulk_urb(async_urb, ir->usbdev, pipe,
760 async_buf, size, mce_async_callback,
761 ir);
763 memcpy(async_buf, data, size);
765 dev_dbg(dev, "receive request called (size=%#x)", size);
767 async_urb->transfer_buffer_length = size;
768 async_urb->dev = ir->usbdev;
770 res = usb_submit_urb(async_urb, GFP_ATOMIC);
771 if (res) {
772 dev_err(dev, "receive request FAILED! (res=%d)", res);
773 return;
775 dev_dbg(dev, "receive request complete (res=%d)", res);
778 static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
780 int rsize = sizeof(DEVICE_RESUME);
782 if (ir->need_reset) {
783 ir->need_reset = false;
784 mce_request_packet(ir, DEVICE_RESUME, rsize);
785 msleep(10);
788 mce_request_packet(ir, data, size);
789 msleep(10);
792 /* Send data out the IR blaster port(s) */
793 static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
795 struct mceusb_dev *ir = dev->priv;
796 int i, length, ret = 0;
797 int cmdcount = 0;
798 unsigned char cmdbuf[MCE_CMDBUF_SIZE];
800 /* MCE tx init header */
801 cmdbuf[cmdcount++] = MCE_CMD_PORT_IR;
802 cmdbuf[cmdcount++] = MCE_CMD_SETIRTXPORTS;
803 cmdbuf[cmdcount++] = ir->tx_mask;
805 /* Send the set TX ports command */
806 mce_async_out(ir, cmdbuf, cmdcount);
807 cmdcount = 0;
809 /* Generate mce packet data */
810 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
811 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
813 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
815 /* Insert mce packet header every 4th entry */
816 if ((cmdcount < MCE_CMDBUF_SIZE) &&
817 (cmdcount % MCE_CODE_LENGTH) == 0)
818 cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
820 /* Insert mce packet data */
821 if (cmdcount < MCE_CMDBUF_SIZE)
822 cmdbuf[cmdcount++] =
823 (txbuf[i] < MCE_PULSE_BIT ?
824 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
825 (i & 1 ? 0x00 : MCE_PULSE_BIT);
826 else {
827 ret = -EINVAL;
828 goto out;
831 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
832 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
835 /* Check if we have room for the empty packet at the end */
836 if (cmdcount >= MCE_CMDBUF_SIZE) {
837 ret = -EINVAL;
838 goto out;
841 /* Fix packet length in last header */
842 length = cmdcount % MCE_CODE_LENGTH;
843 cmdbuf[cmdcount - length] -= MCE_CODE_LENGTH - length;
845 /* All mce commands end with an empty packet (0x80) */
846 cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
848 /* Transmit the command to the mce device */
849 mce_async_out(ir, cmdbuf, cmdcount);
851 out:
852 return ret ? ret : count;
855 /* Sets active IR outputs -- mce devices typically have two */
856 static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
858 struct mceusb_dev *ir = dev->priv;
860 /* return number of transmitters */
861 int emitters = ir->num_txports ? ir->num_txports : 2;
863 if (mask >= (1 << emitters))
864 return emitters;
866 if (ir->flags.tx_mask_normal)
867 ir->tx_mask = mask;
868 else
869 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
870 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
872 return 0;
875 /* Sets the send carrier frequency and mode */
876 static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
878 struct mceusb_dev *ir = dev->priv;
879 int clk = 10000000;
880 int prescaler = 0, divisor = 0;
881 unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
882 MCE_CMD_SETIRCFS, 0x00, 0x00 };
884 /* Carrier has changed */
885 if (ir->carrier != carrier) {
887 if (carrier == 0) {
888 ir->carrier = carrier;
889 cmdbuf[2] = MCE_CMD_SIG_END;
890 cmdbuf[3] = MCE_IRDATA_TRAILER;
891 dev_dbg(ir->dev, "disabling carrier modulation");
892 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
893 return carrier;
896 for (prescaler = 0; prescaler < 4; ++prescaler) {
897 divisor = (clk >> (2 * prescaler)) / carrier;
898 if (divisor <= 0xff) {
899 ir->carrier = carrier;
900 cmdbuf[2] = prescaler;
901 cmdbuf[3] = divisor;
902 dev_dbg(ir->dev, "requesting %u HZ carrier",
903 carrier);
905 /* Transmit new carrier to mce device */
906 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
907 return carrier;
911 return -EINVAL;
915 return 0;
919 * We don't do anything but print debug spew for many of the command bits
920 * we receive from the hardware, but some of them are useful information
921 * we want to store so that we can use them.
923 static void mceusb_handle_command(struct mceusb_dev *ir, int index)
925 u8 hi = ir->buf_in[index + 1] & 0xff;
926 u8 lo = ir->buf_in[index + 2] & 0xff;
928 switch (ir->buf_in[index]) {
929 /* the one and only 5-byte return value command */
930 case MCE_RSP_GETPORTSTATUS:
931 if ((ir->buf_in[index + 4] & 0xff) == 0x00)
932 ir->txports_cabled |= 1 << hi;
933 break;
935 /* 2-byte return value commands */
936 case MCE_RSP_EQIRTIMEOUT:
937 ir->rc->timeout = US_TO_NS((hi << 8 | lo) * MCE_TIME_UNIT);
938 break;
939 case MCE_RSP_EQIRNUMPORTS:
940 ir->num_txports = hi;
941 ir->num_rxports = lo;
942 break;
944 /* 1-byte return value commands */
945 case MCE_RSP_EQEMVER:
946 ir->emver = hi;
947 break;
948 case MCE_RSP_EQIRTXPORTS:
949 ir->tx_mask = hi;
950 break;
951 case MCE_RSP_EQIRRXPORTEN:
952 ir->learning_enabled = ((hi & 0x02) == 0x02);
953 ir->rxports_active = hi;
954 break;
955 case MCE_RSP_CMD_ILLEGAL:
956 ir->need_reset = true;
957 break;
958 default:
959 break;
963 static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
965 DEFINE_IR_RAW_EVENT(rawir);
966 bool event = false;
967 int i = 0;
969 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
970 if (ir->flags.microsoft_gen1)
971 i = 2;
973 /* if there's no data, just return now */
974 if (buf_len <= i)
975 return;
977 for (; i < buf_len; i++) {
978 switch (ir->parser_state) {
979 case SUBCMD:
980 ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]);
981 mceusb_dev_printdata(ir, ir->buf_in, i - 1,
982 ir->rem + 2, false);
983 mceusb_handle_command(ir, i);
984 ir->parser_state = CMD_DATA;
985 break;
986 case PARSE_IRDATA:
987 ir->rem--;
988 init_ir_raw_event(&rawir);
989 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
990 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
991 * US_TO_NS(MCE_TIME_UNIT);
993 dev_dbg(ir->dev, "Storing %s with duration %d",
994 rawir.pulse ? "pulse" : "space",
995 rawir.duration);
997 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
998 event = true;
999 break;
1000 case CMD_DATA:
1001 ir->rem--;
1002 break;
1003 case CMD_HEADER:
1004 /* decode mce packets of the form (84),AA,BB,CC,DD */
1005 /* IR data packets can span USB messages - rem */
1006 ir->cmd = ir->buf_in[i];
1007 if ((ir->cmd == MCE_CMD_PORT_IR) ||
1008 ((ir->cmd & MCE_PORT_MASK) !=
1009 MCE_COMMAND_IRDATA)) {
1010 ir->parser_state = SUBCMD;
1011 continue;
1013 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
1014 mceusb_dev_printdata(ir, ir->buf_in,
1015 i, ir->rem + 1, false);
1016 if (ir->rem)
1017 ir->parser_state = PARSE_IRDATA;
1018 else
1019 ir_raw_event_reset(ir->rc);
1020 break;
1023 if (ir->parser_state != CMD_HEADER && !ir->rem)
1024 ir->parser_state = CMD_HEADER;
1026 if (event) {
1027 dev_dbg(ir->dev, "processed IR data");
1028 ir_raw_event_handle(ir->rc);
1032 static void mceusb_dev_recv(struct urb *urb)
1034 struct mceusb_dev *ir;
1036 if (!urb)
1037 return;
1039 ir = urb->context;
1040 if (!ir) {
1041 usb_unlink_urb(urb);
1042 return;
1045 switch (urb->status) {
1046 /* success */
1047 case 0:
1048 mceusb_process_ir_data(ir, urb->actual_length);
1049 break;
1051 case -ECONNRESET:
1052 case -ENOENT:
1053 case -EILSEQ:
1054 case -ESHUTDOWN:
1055 usb_unlink_urb(urb);
1056 return;
1058 case -EPIPE:
1059 default:
1060 dev_err(ir->dev, "Error: urb status = %d", urb->status);
1061 break;
1064 usb_submit_urb(urb, GFP_ATOMIC);
1067 static void mceusb_get_emulator_version(struct mceusb_dev *ir)
1069 /* If we get no reply or an illegal command reply, its ver 1, says MS */
1070 ir->emver = 1;
1071 mce_async_out(ir, GET_EMVER, sizeof(GET_EMVER));
1074 static void mceusb_gen1_init(struct mceusb_dev *ir)
1076 int ret;
1077 struct device *dev = ir->dev;
1078 char *data;
1080 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
1081 if (!data) {
1082 dev_err(dev, "%s: memory allocation failed!", __func__);
1083 return;
1087 * This is a strange one. Windows issues a set address to the device
1088 * on the receive control pipe and expect a certain value pair back
1090 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
1091 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
1092 data, USB_CTRL_MSG_SZ, HZ * 3);
1093 dev_dbg(dev, "set address - ret = %d", ret);
1094 dev_dbg(dev, "set address - data[0] = %d, data[1] = %d",
1095 data[0], data[1]);
1097 /* set feature: bit rate 38400 bps */
1098 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1099 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
1100 0xc04e, 0x0000, NULL, 0, HZ * 3);
1102 dev_dbg(dev, "set feature - ret = %d", ret);
1104 /* bRequest 4: set char length to 8 bits */
1105 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1106 4, USB_TYPE_VENDOR,
1107 0x0808, 0x0000, NULL, 0, HZ * 3);
1108 dev_dbg(dev, "set char length - retB = %d", ret);
1110 /* bRequest 2: set handshaking to use DTR/DSR */
1111 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1112 2, USB_TYPE_VENDOR,
1113 0x0000, 0x0100, NULL, 0, HZ * 3);
1114 dev_dbg(dev, "set handshake - retC = %d", ret);
1116 /* device resume */
1117 mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1119 /* get hw/sw revision? */
1120 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
1122 kfree(data);
1125 static void mceusb_gen2_init(struct mceusb_dev *ir)
1127 /* device resume */
1128 mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1130 /* get wake version (protocol, key, address) */
1131 mce_async_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));
1133 /* unknown what this one actually returns... */
1134 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
1137 static void mceusb_get_parameters(struct mceusb_dev *ir)
1139 int i;
1140 unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
1141 MCE_CMD_GETPORTSTATUS, 0x00 };
1143 /* defaults, if the hardware doesn't support querying */
1144 ir->num_txports = 2;
1145 ir->num_rxports = 2;
1147 /* get number of tx and rx ports */
1148 mce_async_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));
1150 /* get the carrier and frequency */
1151 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
1153 if (ir->num_txports && !ir->flags.no_tx)
1154 /* get the transmitter bitmask */
1155 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
1157 /* get receiver timeout value */
1158 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
1160 /* get receiver sensor setting */
1161 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
1163 for (i = 0; i < ir->num_txports; i++) {
1164 cmdbuf[2] = i;
1165 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
1169 static void mceusb_flash_led(struct mceusb_dev *ir)
1171 if (ir->emver < 2)
1172 return;
1174 mce_async_out(ir, FLASH_LED, sizeof(FLASH_LED));
1177 static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
1179 struct usb_device *udev = ir->usbdev;
1180 struct device *dev = ir->dev;
1181 struct rc_dev *rc;
1182 int ret;
1184 rc = rc_allocate_device();
1185 if (!rc) {
1186 dev_err(dev, "remote dev allocation failed");
1187 goto out;
1190 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1191 mceusb_model[ir->model].name ?
1192 mceusb_model[ir->model].name :
1193 "Media Center Ed. eHome Infrared Remote Transceiver",
1194 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1195 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1197 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
1199 rc->input_name = ir->name;
1200 rc->input_phys = ir->phys;
1201 usb_to_input_id(ir->usbdev, &rc->input_id);
1202 rc->dev.parent = dev;
1203 rc->priv = ir;
1204 rc->driver_type = RC_DRIVER_IR_RAW;
1205 rc->allowed_protocols = RC_BIT_ALL;
1206 rc->timeout = MS_TO_NS(100);
1207 if (!ir->flags.no_tx) {
1208 rc->s_tx_mask = mceusb_set_tx_mask;
1209 rc->s_tx_carrier = mceusb_set_tx_carrier;
1210 rc->tx_ir = mceusb_tx_ir;
1212 rc->driver_name = DRIVER_NAME;
1214 switch (le16_to_cpu(udev->descriptor.idVendor)) {
1215 case VENDOR_HAUPPAUGE:
1216 rc->map_name = RC_MAP_HAUPPAUGE;
1217 break;
1218 case VENDOR_PCTV:
1219 rc->map_name = RC_MAP_PINNACLE_PCTV_HD;
1220 break;
1221 default:
1222 rc->map_name = RC_MAP_RC6_MCE;
1224 if (mceusb_model[ir->model].rc_map)
1225 rc->map_name = mceusb_model[ir->model].rc_map;
1227 ret = rc_register_device(rc);
1228 if (ret < 0) {
1229 dev_err(dev, "remote dev registration failed");
1230 goto out;
1233 return rc;
1235 out:
1236 rc_free_device(rc);
1237 return NULL;
1240 static int mceusb_dev_probe(struct usb_interface *intf,
1241 const struct usb_device_id *id)
1243 struct usb_device *dev = interface_to_usbdev(intf);
1244 struct usb_host_interface *idesc;
1245 struct usb_endpoint_descriptor *ep = NULL;
1246 struct usb_endpoint_descriptor *ep_in = NULL;
1247 struct usb_endpoint_descriptor *ep_out = NULL;
1248 struct mceusb_dev *ir = NULL;
1249 int pipe, maxp, i, res;
1250 char buf[63], name[128] = "";
1251 enum mceusb_model_type model = id->driver_info;
1252 bool is_gen3;
1253 bool is_microsoft_gen1;
1254 bool tx_mask_normal;
1255 int ir_intfnum;
1257 dev_dbg(&intf->dev, "%s called", __func__);
1259 idesc = intf->cur_altsetting;
1261 is_gen3 = mceusb_model[model].mce_gen3;
1262 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1263 tx_mask_normal = mceusb_model[model].tx_mask_normal;
1264 ir_intfnum = mceusb_model[model].ir_intfnum;
1266 /* There are multi-function devices with non-IR interfaces */
1267 if (idesc->desc.bInterfaceNumber != ir_intfnum)
1268 return -ENODEV;
1270 /* step through the endpoints to find first bulk in and out endpoint */
1271 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1272 ep = &idesc->endpoint[i].desc;
1274 if (ep_in == NULL) {
1275 if (usb_endpoint_is_bulk_in(ep)) {
1276 ep_in = ep;
1277 dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n");
1278 } else if (usb_endpoint_is_int_in(ep)) {
1279 ep_in = ep;
1280 ep_in->bInterval = 1;
1281 dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n");
1285 if (ep_out == NULL) {
1286 if (usb_endpoint_is_bulk_out(ep)) {
1287 ep_out = ep;
1288 dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n");
1289 } else if (usb_endpoint_is_int_out(ep)) {
1290 ep_out = ep;
1291 ep_out->bInterval = 1;
1292 dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n");
1296 if (ep_in == NULL) {
1297 dev_dbg(&intf->dev, "inbound and/or endpoint not found");
1298 return -ENODEV;
1301 if (usb_endpoint_xfer_int(ep_in))
1302 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1303 else
1304 pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress);
1305 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1307 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1308 if (!ir)
1309 goto mem_alloc_fail;
1311 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1312 if (!ir->buf_in)
1313 goto buf_in_alloc_fail;
1315 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1316 if (!ir->urb_in)
1317 goto urb_in_alloc_fail;
1319 ir->usbdev = usb_get_dev(dev);
1320 ir->dev = &intf->dev;
1321 ir->len_in = maxp;
1322 ir->flags.microsoft_gen1 = is_microsoft_gen1;
1323 ir->flags.tx_mask_normal = tx_mask_normal;
1324 ir->flags.no_tx = mceusb_model[model].no_tx;
1325 ir->model = model;
1327 /* Saving usb interface data for use by the transmitter routine */
1328 ir->usb_ep_out = ep_out;
1330 if (dev->descriptor.iManufacturer
1331 && usb_string(dev, dev->descriptor.iManufacturer,
1332 buf, sizeof(buf)) > 0)
1333 strlcpy(name, buf, sizeof(name));
1334 if (dev->descriptor.iProduct
1335 && usb_string(dev, dev->descriptor.iProduct,
1336 buf, sizeof(buf)) > 0)
1337 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1338 " %s", buf);
1340 ir->rc = mceusb_init_rc_dev(ir);
1341 if (!ir->rc)
1342 goto rc_dev_fail;
1344 /* wire up inbound data handler */
1345 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1346 mceusb_dev_recv, ir, ep_in->bInterval);
1347 ir->urb_in->transfer_dma = ir->dma_in;
1348 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1350 /* flush buffers on the device */
1351 dev_dbg(&intf->dev, "Flushing receive buffers\n");
1352 res = usb_submit_urb(ir->urb_in, GFP_KERNEL);
1353 if (res)
1354 dev_err(&intf->dev, "failed to flush buffers: %d\n", res);
1356 /* figure out which firmware/emulator version this hardware has */
1357 mceusb_get_emulator_version(ir);
1359 /* initialize device */
1360 if (ir->flags.microsoft_gen1)
1361 mceusb_gen1_init(ir);
1362 else if (!is_gen3)
1363 mceusb_gen2_init(ir);
1365 mceusb_get_parameters(ir);
1367 mceusb_flash_led(ir);
1369 if (!ir->flags.no_tx)
1370 mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
1372 usb_set_intfdata(intf, ir);
1374 /* enable wake via this device */
1375 device_set_wakeup_capable(ir->dev, true);
1376 device_set_wakeup_enable(ir->dev, true);
1378 dev_info(&intf->dev, "Registered %s with mce emulator interface version %x",
1379 name, ir->emver);
1380 dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
1381 ir->num_txports, ir->txports_cabled,
1382 ir->num_rxports, ir->rxports_active);
1384 return 0;
1386 /* Error-handling path */
1387 rc_dev_fail:
1388 usb_put_dev(ir->usbdev);
1389 usb_kill_urb(ir->urb_in);
1390 usb_free_urb(ir->urb_in);
1391 urb_in_alloc_fail:
1392 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1393 buf_in_alloc_fail:
1394 kfree(ir);
1395 mem_alloc_fail:
1396 dev_err(&intf->dev, "%s: device setup failed!", __func__);
1398 return -ENOMEM;
1402 static void mceusb_dev_disconnect(struct usb_interface *intf)
1404 struct usb_device *dev = interface_to_usbdev(intf);
1405 struct mceusb_dev *ir = usb_get_intfdata(intf);
1407 usb_set_intfdata(intf, NULL);
1409 if (!ir)
1410 return;
1412 ir->usbdev = NULL;
1413 rc_unregister_device(ir->rc);
1414 usb_kill_urb(ir->urb_in);
1415 usb_free_urb(ir->urb_in);
1416 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1417 usb_put_dev(dev);
1419 kfree(ir);
1422 static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1424 struct mceusb_dev *ir = usb_get_intfdata(intf);
1425 dev_info(ir->dev, "suspend");
1426 usb_kill_urb(ir->urb_in);
1427 return 0;
1430 static int mceusb_dev_resume(struct usb_interface *intf)
1432 struct mceusb_dev *ir = usb_get_intfdata(intf);
1433 dev_info(ir->dev, "resume");
1434 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1435 return -EIO;
1436 return 0;
1439 static struct usb_driver mceusb_dev_driver = {
1440 .name = DRIVER_NAME,
1441 .probe = mceusb_dev_probe,
1442 .disconnect = mceusb_dev_disconnect,
1443 .suspend = mceusb_dev_suspend,
1444 .resume = mceusb_dev_resume,
1445 .reset_resume = mceusb_dev_resume,
1446 .id_table = mceusb_dev_table
1449 module_usb_driver(mceusb_dev_driver);
1451 MODULE_DESCRIPTION(DRIVER_DESC);
1452 MODULE_AUTHOR(DRIVER_AUTHOR);
1453 MODULE_LICENSE("GPL");
1454 MODULE_DEVICE_TABLE(usb, mceusb_dev_table);