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.
36 #include <linux/device.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/workqueue.h>
40 #include <linux/usb.h>
41 #include <linux/usb/input.h>
42 #include <linux/pm_wakeup.h>
43 #include <media/rc-core.h>
45 #define DRIVER_VERSION "1.94"
46 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
47 #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
49 #define DRIVER_NAME "mceusb"
51 #define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
52 #define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
55 #define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
56 #define MCE_TIME_UNIT 50 /* Approx 50us resolution */
57 #define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
58 #define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
59 #define MCE_IRDATA_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
60 #define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
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 */
68 * The interface between the host and the IR hardware is command-response
69 * based. All commands and responses have a consistent format, where a lead
70 * byte always identifies the type of data following it. The lead byte has
71 * a port value in the 3 highest bits and a length value in the 5 lowest
74 * The length field is overloaded, with a value of 11111 indicating that the
75 * following byte is a command or response code, and the length of the entire
76 * message is determined by the code. If the length field is not 11111, then
77 * it specifies the number of bytes of port data that follow.
80 #define MCE_PORT_IR 0x4 /* (0x4 << 5) | MCE_CMD = 0x9f */
81 #define MCE_PORT_SYS 0x7 /* (0x7 << 5) | MCE_CMD = 0xff */
82 #define MCE_PORT_SER 0x6 /* 0xc0 thru 0xdf flush & 0x1f bytes */
83 #define MCE_PORT_MASK 0xe0 /* Mask out command bits */
85 /* Command port headers */
86 #define MCE_CMD_PORT_IR 0x9f /* IR-related cmd/rsp */
87 #define MCE_CMD_PORT_SYS 0xff /* System (non-IR) device cmd/rsp */
89 /* Commands that set device state (2-4 bytes in length) */
90 #define MCE_CMD_RESET 0xfe /* Reset device, 2 bytes */
91 #define MCE_CMD_RESUME 0xaa /* Resume device after error, 2 bytes */
92 #define MCE_CMD_SETIRCFS 0x06 /* Set tx carrier, 4 bytes */
93 #define MCE_CMD_SETIRTIMEOUT 0x0c /* Set timeout, 4 bytes */
94 #define MCE_CMD_SETIRTXPORTS 0x08 /* Set tx ports, 3 bytes */
95 #define MCE_CMD_SETIRRXPORTEN 0x14 /* Set rx ports, 3 bytes */
96 #define MCE_CMD_FLASHLED 0x23 /* Flash receiver LED, 2 bytes */
98 /* Commands that query device state (all 2 bytes, unless noted) */
99 #define MCE_CMD_GETIRCFS 0x07 /* Get carrier */
100 #define MCE_CMD_GETIRTIMEOUT 0x0d /* Get timeout */
101 #define MCE_CMD_GETIRTXPORTS 0x13 /* Get tx ports */
102 #define MCE_CMD_GETIRRXPORTEN 0x15 /* Get rx ports */
103 #define MCE_CMD_GETPORTSTATUS 0x11 /* Get tx port status, 3 bytes */
104 #define MCE_CMD_GETIRNUMPORTS 0x16 /* Get number of ports */
105 #define MCE_CMD_GETWAKESOURCE 0x17 /* Get wake source */
106 #define MCE_CMD_GETEMVER 0x22 /* Get emulator interface version */
107 #define MCE_CMD_GETDEVDETAILS 0x21 /* Get device details (em ver2 only) */
108 #define MCE_CMD_GETWAKESUPPORT 0x20 /* Get wake details (em ver2 only) */
109 #define MCE_CMD_GETWAKEVERSION 0x18 /* Get wake pattern (em ver2 only) */
112 #define MCE_CMD_NOP 0xff /* No operation */
114 /* Responses to commands (non-error cases) */
115 #define MCE_RSP_EQIRCFS 0x06 /* tx carrier, 4 bytes */
116 #define MCE_RSP_EQIRTIMEOUT 0x0c /* rx timeout, 4 bytes */
117 #define MCE_RSP_GETWAKESOURCE 0x17 /* wake source, 3 bytes */
118 #define MCE_RSP_EQIRTXPORTS 0x08 /* tx port mask, 3 bytes */
119 #define MCE_RSP_EQIRRXPORTEN 0x14 /* rx port mask, 3 bytes */
120 #define MCE_RSP_GETPORTSTATUS 0x11 /* tx port status, 7 bytes */
121 #define MCE_RSP_EQIRRXCFCNT 0x15 /* rx carrier count, 4 bytes */
122 #define MCE_RSP_EQIRNUMPORTS 0x16 /* number of ports, 4 bytes */
123 #define MCE_RSP_EQWAKESUPPORT 0x20 /* wake capabilities, 3 bytes */
124 #define MCE_RSP_EQWAKEVERSION 0x18 /* wake pattern details, 6 bytes */
125 #define MCE_RSP_EQDEVDETAILS 0x21 /* device capabilities, 3 bytes */
126 #define MCE_RSP_EQEMVER 0x22 /* emulator interface ver, 3 bytes */
127 #define MCE_RSP_FLASHLED 0x23 /* success flashing LED, 2 bytes */
129 /* Responses to error cases, must send MCE_CMD_RESUME to clear them */
130 #define MCE_RSP_CMD_ILLEGAL 0xfe /* illegal command for port, 2 bytes */
131 #define MCE_RSP_TX_TIMEOUT 0x81 /* tx timed out, 2 bytes */
133 /* Misc commands/responses not defined in the MCE remote/transceiver spec */
134 #define MCE_CMD_SIG_END 0x01 /* End of signal */
135 #define MCE_CMD_PING 0x03 /* Ping device */
136 #define MCE_CMD_UNKNOWN 0x04 /* Unknown */
137 #define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
138 #define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
139 #define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
140 #define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
141 #define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
142 #define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
143 #define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
144 #define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
145 #define MCE_CMD_NULL 0x00 /* These show up various places... */
147 /* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
148 * then we're looking at a raw IR data sample */
149 #define MCE_COMMAND_IRDATA 0x80
150 #define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
152 #define VENDOR_PHILIPS 0x0471
153 #define VENDOR_SMK 0x0609
154 #define VENDOR_TATUNG 0x1460
155 #define VENDOR_GATEWAY 0x107b
156 #define VENDOR_SHUTTLE 0x1308
157 #define VENDOR_SHUTTLE2 0x051c
158 #define VENDOR_MITSUMI 0x03ee
159 #define VENDOR_TOPSEED 0x1784
160 #define VENDOR_RICAVISION 0x179d
161 #define VENDOR_ITRON 0x195d
162 #define VENDOR_FIC 0x1509
163 #define VENDOR_LG 0x043e
164 #define VENDOR_MICROSOFT 0x045e
165 #define VENDOR_FORMOSA 0x147a
166 #define VENDOR_FINTEK 0x1934
167 #define VENDOR_PINNACLE 0x2304
168 #define VENDOR_ECS 0x1019
169 #define VENDOR_WISTRON 0x0fb8
170 #define VENDOR_COMPRO 0x185b
171 #define VENDOR_NORTHSTAR 0x04eb
172 #define VENDOR_REALTEK 0x0bda
173 #define VENDOR_TIVO 0x105a
174 #define VENDOR_CONEXANT 0x0572
175 #define VENDOR_TWISTEDMELON 0x2596
176 #define VENDOR_HAUPPAUGE 0x2040
177 #define VENDOR_PCTV 0x2013
178 #define VENDOR_ADAPTEC 0x03f3
180 enum mceusb_model_type
{
181 MCE_GEN2
= 0, /* Most boards */
185 MCE_GEN2_TX_INV_RX_GOOD
,
191 HAUPPAUGE_CX_HYBRID_TV
,
192 EVROMEDIA_FULL_HYBRID_FULLHD
,
196 struct mceusb_model
{
200 u32 tx_mask_normal
:1;
203 * 2nd IR receiver (short-range, wideband) for learning mode:
204 * 0, absent 2nd receiver (rx2)
206 * 2, rx2 which under counts IR carrier cycles
212 const char *rc_map
; /* Allow specify a per-board map */
213 const char *name
; /* per-board name */
216 static const struct mceusb_model mceusb_model
[] = {
230 [MCE_GEN2_TX_INV
] = {
235 [MCE_GEN2_TX_INV_RX_GOOD
] = {
247 * In fact, the EVK is shipped without
248 * remotes, but we should have something handy,
249 * to allow testing it
251 .name
= "Conexant Hybrid TV (cx231xx) MCE IR",
255 .no_tx
= 1, /* tx isn't wired up at all */
256 .name
= "Conexant Hybrid TV (cx231xx) MCE IR",
258 [HAUPPAUGE_CX_HYBRID_TV
] = {
259 .no_tx
= 1, /* eeprom says it has no tx */
260 .name
= "Conexant Hybrid TV (cx231xx) MCE IR no TX",
269 .rc_map
= RC_MAP_TIVO
,
272 [EVROMEDIA_FULL_HYBRID_FULLHD
] = {
273 .name
= "Evromedia USB Full Hybrid Full HD",
275 .rc_map
= RC_MAP_MSI_DIGIVOX_III
,
277 [ASTROMETA_T2HYBRID
] = {
278 .name
= "Astrometa T2Hybrid",
280 .rc_map
= RC_MAP_ASTROMETA_T2HYBRID
,
284 static const struct usb_device_id mceusb_dev_table
[] = {
285 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
286 { USB_DEVICE(VENDOR_MICROSOFT
, 0x006d),
287 .driver_info
= MCE_GEN1
},
288 /* Philips Infrared Transceiver - Sahara branded */
289 { USB_DEVICE(VENDOR_PHILIPS
, 0x0608) },
290 /* Philips Infrared Transceiver - HP branded */
291 { USB_DEVICE(VENDOR_PHILIPS
, 0x060c),
292 .driver_info
= MCE_GEN2_TX_INV
},
293 /* Philips SRM5100 */
294 { USB_DEVICE(VENDOR_PHILIPS
, 0x060d) },
295 /* Philips Infrared Transceiver - Omaura */
296 { USB_DEVICE(VENDOR_PHILIPS
, 0x060f) },
297 /* Philips Infrared Transceiver - Spinel plus */
298 { USB_DEVICE(VENDOR_PHILIPS
, 0x0613) },
299 /* Philips eHome Infrared Transceiver */
300 { USB_DEVICE(VENDOR_PHILIPS
, 0x0815) },
301 /* Philips/Spinel plus IR transceiver for ASUS */
302 { USB_DEVICE(VENDOR_PHILIPS
, 0x206c) },
303 /* Philips/Spinel plus IR transceiver for ASUS */
304 { USB_DEVICE(VENDOR_PHILIPS
, 0x2088) },
305 /* Philips IR transceiver (Dell branded) */
306 { USB_DEVICE(VENDOR_PHILIPS
, 0x2093),
307 .driver_info
= MCE_GEN2_TX_INV
},
308 /* Realtek MCE IR Receiver and card reader */
309 { USB_DEVICE(VENDOR_REALTEK
, 0x0161),
310 .driver_info
= MULTIFUNCTION
},
311 /* SMK/Toshiba G83C0004D410 */
312 { USB_DEVICE(VENDOR_SMK
, 0x031d),
313 .driver_info
= MCE_GEN2_TX_INV_RX_GOOD
},
314 /* SMK eHome Infrared Transceiver (Sony VAIO) */
315 { USB_DEVICE(VENDOR_SMK
, 0x0322),
316 .driver_info
= MCE_GEN2_TX_INV
},
317 /* bundled with Hauppauge PVR-150 */
318 { USB_DEVICE(VENDOR_SMK
, 0x0334),
319 .driver_info
= MCE_GEN2_TX_INV
},
320 /* SMK eHome Infrared Transceiver */
321 { USB_DEVICE(VENDOR_SMK
, 0x0338) },
322 /* SMK/I-O Data GV-MC7/RCKIT Receiver */
323 { USB_DEVICE(VENDOR_SMK
, 0x0353),
324 .driver_info
= MCE_GEN2_NO_TX
},
325 /* SMK RXX6000 Infrared Receiver */
326 { USB_DEVICE(VENDOR_SMK
, 0x0357),
327 .driver_info
= MCE_GEN2_NO_TX
},
328 /* Tatung eHome Infrared Transceiver */
329 { USB_DEVICE(VENDOR_TATUNG
, 0x9150) },
330 /* Shuttle eHome Infrared Transceiver */
331 { USB_DEVICE(VENDOR_SHUTTLE
, 0xc001) },
332 /* Shuttle eHome Infrared Transceiver */
333 { USB_DEVICE(VENDOR_SHUTTLE2
, 0xc001) },
334 /* Gateway eHome Infrared Transceiver */
335 { USB_DEVICE(VENDOR_GATEWAY
, 0x3009) },
337 { USB_DEVICE(VENDOR_MITSUMI
, 0x2501) },
338 /* Topseed eHome Infrared Transceiver */
339 { USB_DEVICE(VENDOR_TOPSEED
, 0x0001),
340 .driver_info
= MCE_GEN2_TX_INV
},
341 /* Topseed HP eHome Infrared Transceiver */
342 { USB_DEVICE(VENDOR_TOPSEED
, 0x0006),
343 .driver_info
= MCE_GEN2_TX_INV
},
344 /* Topseed eHome Infrared Transceiver */
345 { USB_DEVICE(VENDOR_TOPSEED
, 0x0007),
346 .driver_info
= MCE_GEN2_TX_INV
},
347 /* Topseed eHome Infrared Transceiver */
348 { USB_DEVICE(VENDOR_TOPSEED
, 0x0008),
349 .driver_info
= MCE_GEN3
},
350 /* Topseed eHome Infrared Transceiver */
351 { USB_DEVICE(VENDOR_TOPSEED
, 0x000a),
352 .driver_info
= MCE_GEN2_TX_INV
},
353 /* Topseed eHome Infrared Transceiver */
354 { USB_DEVICE(VENDOR_TOPSEED
, 0x0011),
355 .driver_info
= MCE_GEN3
},
356 /* Ricavision internal Infrared Transceiver */
357 { USB_DEVICE(VENDOR_RICAVISION
, 0x0010) },
358 /* Itron ione Libra Q-11 */
359 { USB_DEVICE(VENDOR_ITRON
, 0x7002) },
360 /* FIC eHome Infrared Transceiver */
361 { USB_DEVICE(VENDOR_FIC
, 0x9242) },
362 /* LG eHome Infrared Transceiver */
363 { USB_DEVICE(VENDOR_LG
, 0x9803) },
364 /* Microsoft MCE Infrared Transceiver */
365 { USB_DEVICE(VENDOR_MICROSOFT
, 0x00a0) },
366 /* Formosa eHome Infrared Transceiver */
367 { USB_DEVICE(VENDOR_FORMOSA
, 0xe015) },
368 /* Formosa21 / eHome Infrared Receiver */
369 { USB_DEVICE(VENDOR_FORMOSA
, 0xe016) },
370 /* Formosa aim / Trust MCE Infrared Receiver */
371 { USB_DEVICE(VENDOR_FORMOSA
, 0xe017),
372 .driver_info
= MCE_GEN2_NO_TX
},
373 /* Formosa Industrial Computing / Beanbag Emulation Device */
374 { USB_DEVICE(VENDOR_FORMOSA
, 0xe018) },
375 /* Formosa21 / eHome Infrared Receiver */
376 { USB_DEVICE(VENDOR_FORMOSA
, 0xe03a) },
377 /* Formosa Industrial Computing AIM IR605/A */
378 { USB_DEVICE(VENDOR_FORMOSA
, 0xe03c) },
379 /* Formosa Industrial Computing */
380 { USB_DEVICE(VENDOR_FORMOSA
, 0xe03e) },
381 /* Formosa Industrial Computing */
382 { USB_DEVICE(VENDOR_FORMOSA
, 0xe042) },
383 /* Fintek eHome Infrared Transceiver (HP branded) */
384 { USB_DEVICE(VENDOR_FINTEK
, 0x5168),
385 .driver_info
= MCE_GEN2_TX_INV
},
386 /* Fintek eHome Infrared Transceiver */
387 { USB_DEVICE(VENDOR_FINTEK
, 0x0602) },
388 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
389 { USB_DEVICE(VENDOR_FINTEK
, 0x0702) },
390 /* Pinnacle Remote Kit */
391 { USB_DEVICE(VENDOR_PINNACLE
, 0x0225),
392 .driver_info
= MCE_GEN3
},
393 /* Elitegroup Computer Systems IR */
394 { USB_DEVICE(VENDOR_ECS
, 0x0f38) },
395 /* Wistron Corp. eHome Infrared Receiver */
396 { USB_DEVICE(VENDOR_WISTRON
, 0x0002) },
398 { USB_DEVICE(VENDOR_COMPRO
, 0x3020) },
400 { USB_DEVICE(VENDOR_COMPRO
, 0x3082) },
401 /* Northstar Systems, Inc. eHome Infrared Transceiver */
402 { USB_DEVICE(VENDOR_NORTHSTAR
, 0xe004) },
403 /* TiVo PC IR Receiver */
404 { USB_DEVICE(VENDOR_TIVO
, 0x2000),
405 .driver_info
= TIVO_KIT
},
406 /* Conexant Hybrid TV "Shelby" Polaris SDK */
407 { USB_DEVICE(VENDOR_CONEXANT
, 0x58a1),
408 .driver_info
= POLARIS_EVK
},
409 /* Conexant Hybrid TV RDU253S Polaris */
410 { USB_DEVICE(VENDOR_CONEXANT
, 0x58a5),
411 .driver_info
= CX_HYBRID_TV
},
412 /* Twisted Melon Inc. - Manta Mini Receiver */
413 { USB_DEVICE(VENDOR_TWISTEDMELON
, 0x8008) },
414 /* Twisted Melon Inc. - Manta Pico Receiver */
415 { USB_DEVICE(VENDOR_TWISTEDMELON
, 0x8016) },
416 /* Twisted Melon Inc. - Manta Transceiver */
417 { USB_DEVICE(VENDOR_TWISTEDMELON
, 0x8042) },
418 /* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
419 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb130),
420 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
421 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb131),
422 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
423 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb138),
424 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
425 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb139),
426 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
427 { USB_DEVICE(VENDOR_PCTV
, 0x0259),
428 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
429 { USB_DEVICE(VENDOR_PCTV
, 0x025e),
430 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
431 /* Adaptec / HP eHome Receiver */
432 { USB_DEVICE(VENDOR_ADAPTEC
, 0x0094) },
433 /* Evromedia USB Full Hybrid Full HD */
434 { USB_DEVICE(0x1b80, 0xd3b2),
435 .driver_info
= EVROMEDIA_FULL_HYBRID_FULLHD
},
436 /* Astrometa T2hybrid */
437 { USB_DEVICE(0x15f4, 0x0135),
438 .driver_info
= ASTROMETA_T2HYBRID
},
440 /* Terminating entry */
444 /* data structure for each usb transceiver */
449 /* optional features we can enable */
450 bool carrier_report_enabled
;
451 bool wideband_rx_enabled
; /* aka learning mode, short-range rx */
453 /* core device bits */
457 struct usb_device
*usbdev
;
459 unsigned int pipe_in
;
460 struct usb_endpoint_descriptor
*usb_ep_out
;
461 unsigned int pipe_out
;
463 /* buffers and dma */
464 unsigned char *buf_in
;
475 u8 cmd
, rem
; /* Remaining IR data bytes in packet */
479 u32 tx_mask_normal
:1;
480 u32 microsoft_gen1
:1;
485 /* transmit support */
487 unsigned char tx_mask
;
491 enum mceusb_model_type model
;
493 bool need_reset
; /* flag to issue a device resume cmd */
494 u8 emver
; /* emulator interface version */
495 u8 num_txports
; /* number of transmit ports */
496 u8 num_rxports
; /* number of receive sensors */
497 u8 txports_cabled
; /* bitmask of transmitters with cable */
498 u8 rxports_active
; /* bitmask of active receive sensors */
499 bool learning_active
; /* wideband rx is active */
501 /* receiver carrier frequency detection support */
502 u32 pulse_tunit
; /* IR pulse "on" cumulative time units */
503 u32 pulse_count
; /* pulse "on" count in measurement interval */
506 * support for async error handler mceusb_deferred_kevent()
507 * where usb_clear_halt(), usb_reset_configuration(),
508 * usb_reset_device(), etc. must be done in process context
510 struct work_struct kevent
;
511 unsigned long kevent_flags
;
512 # define EVENT_TX_HALT 0
513 # define EVENT_RX_HALT 1
516 /* MCE Device Command Strings, generally a port and command pair */
517 static char DEVICE_RESUME
[] = {MCE_CMD_NULL
, MCE_CMD_PORT_SYS
,
519 static char GET_REVISION
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_G_REVISION
};
520 static char GET_EMVER
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_GETEMVER
};
521 static char GET_WAKEVERSION
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_GETWAKEVERSION
};
522 static char FLASH_LED
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_FLASHLED
};
523 static char GET_UNKNOWN2
[] = {MCE_CMD_PORT_IR
, MCE_CMD_UNKNOWN2
};
524 static char GET_CARRIER_FREQ
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRCFS
};
525 static char GET_RX_TIMEOUT
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRTIMEOUT
};
526 static char GET_NUM_PORTS
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRNUMPORTS
};
527 static char GET_TX_BITMASK
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRTXPORTS
};
528 static char GET_RX_SENSOR
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRRXPORTEN
};
529 /* sub in desired values in lower byte or bytes for full command */
530 /* FIXME: make use of these for transmit.
531 static char SET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR,
532 MCE_CMD_SETIRCFS, 0x00, 0x00};
533 static char SET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
534 static char SET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR,
535 MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
536 static char SET_RX_SENSOR[] = {MCE_CMD_PORT_IR,
537 MCE_RSP_EQIRRXPORTEN, 0x00};
540 static int mceusb_cmd_datasize(u8 cmd
, u8 subcmd
)
546 if (subcmd
== MCE_CMD_PORT_SYS
)
549 case MCE_CMD_PORT_SYS
:
551 case MCE_RSP_GETPORTSTATUS
:
554 case MCE_RSP_EQWAKEVERSION
:
557 case MCE_CMD_G_REVISION
:
560 case MCE_RSP_EQWAKESUPPORT
:
561 case MCE_RSP_GETWAKESOURCE
:
562 case MCE_RSP_EQDEVDETAILS
:
563 case MCE_RSP_EQEMVER
:
567 case MCE_CMD_PORT_IR
:
569 case MCE_CMD_UNKNOWN
:
570 case MCE_RSP_EQIRCFS
:
571 case MCE_RSP_EQIRTIMEOUT
:
572 case MCE_RSP_EQIRRXCFCNT
:
573 case MCE_RSP_EQIRNUMPORTS
:
576 case MCE_CMD_SIG_END
:
577 case MCE_RSP_EQIRTXPORTS
:
578 case MCE_RSP_EQIRRXPORTEN
:
586 static void mceusb_dev_printdata(struct mceusb_dev
*ir
, u8
*buf
, int buf_len
,
587 int offset
, int len
, bool out
)
589 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
591 u8 cmd
, subcmd
, *data
;
592 struct device
*dev
= ir
->dev
;
596 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
597 if (ir
->flags
.microsoft_gen1
&& !out
&& !offset
)
603 dev_dbg(dev
, "%cx data: %*ph (length=%d)",
605 min(len
, buf_len
- offset
), buf
+ offset
, len
);
607 inout
= out
? "Request" : "Got";
609 start
= offset
+ skip
;
610 cmd
= buf
[start
] & 0xff;
611 subcmd
= buf
[start
+ 1] & 0xff;
612 data
= buf
+ start
+ 2;
616 if (subcmd
== MCE_CMD_NULL
)
618 if ((subcmd
== MCE_CMD_PORT_SYS
) &&
619 (data
[0] == MCE_CMD_RESUME
))
620 dev_dbg(dev
, "Device resume requested");
622 dev_dbg(dev
, "Unknown command 0x%02x 0x%02x",
625 case MCE_CMD_PORT_SYS
:
627 case MCE_RSP_EQEMVER
:
629 dev_dbg(dev
, "Emulator interface version %x",
632 case MCE_CMD_G_REVISION
:
634 dev_dbg(dev
, "Get hw/sw rev?");
636 dev_dbg(dev
, "hw/sw rev %*ph",
640 dev_dbg(dev
, "Device resume requested");
642 case MCE_RSP_CMD_ILLEGAL
:
643 dev_dbg(dev
, "Illegal PORT_SYS command");
645 case MCE_RSP_EQWAKEVERSION
:
647 dev_dbg(dev
, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
648 data
[0], data
[1], data
[2], data
[3]);
650 case MCE_RSP_GETPORTSTATUS
:
652 /* We use data1 + 1 here, to match hw labels */
653 dev_dbg(dev
, "TX port %d: blaster is%s connected",
654 data
[0] + 1, data
[3] ? " not" : "");
656 case MCE_CMD_FLASHLED
:
657 dev_dbg(dev
, "Attempting to flash LED");
660 dev_dbg(dev
, "Unknown command 0x%02x 0x%02x",
665 case MCE_CMD_PORT_IR
:
667 case MCE_CMD_SIG_END
:
668 dev_dbg(dev
, "End of signal");
671 dev_dbg(dev
, "Ping");
673 case MCE_CMD_UNKNOWN
:
674 dev_dbg(dev
, "Resp to 9f 05 of 0x%02x 0x%02x",
677 case MCE_RSP_EQIRCFS
:
678 period
= DIV_ROUND_CLOSEST((1U << data
[0] * 2) *
682 carrier
= (1000 * 1000) / period
;
683 dev_dbg(dev
, "%s carrier of %u Hz (period %uus)",
684 inout
, carrier
, period
);
686 case MCE_CMD_GETIRCFS
:
687 dev_dbg(dev
, "Get carrier mode and freq");
689 case MCE_RSP_EQIRTXPORTS
:
690 dev_dbg(dev
, "%s transmit blaster mask of 0x%02x",
693 case MCE_RSP_EQIRTIMEOUT
:
694 /* value is in units of 50us, so x*50/1000 ms */
695 period
= ((data
[0] << 8) | data
[1]) *
696 MCE_TIME_UNIT
/ 1000;
697 dev_dbg(dev
, "%s receive timeout of %d ms",
700 case MCE_CMD_GETIRTIMEOUT
:
701 dev_dbg(dev
, "Get receive timeout");
703 case MCE_CMD_GETIRTXPORTS
:
704 dev_dbg(dev
, "Get transmit blaster mask");
706 case MCE_RSP_EQIRRXPORTEN
:
707 dev_dbg(dev
, "%s %s-range receive sensor in use",
708 inout
, data
[0] == 0x02 ? "short" : "long");
710 case MCE_CMD_GETIRRXPORTEN
:
711 /* aka MCE_RSP_EQIRRXCFCNT */
713 dev_dbg(dev
, "Get receive sensor");
715 dev_dbg(dev
, "RX carrier cycle count: %d",
716 ((data
[0] << 8) | data
[1]));
718 case MCE_RSP_EQIRNUMPORTS
:
721 dev_dbg(dev
, "Num TX ports: %x, num RX ports: %x",
724 case MCE_RSP_CMD_ILLEGAL
:
725 dev_dbg(dev
, "Illegal PORT_IR command");
728 dev_dbg(dev
, "Unknown command 0x%02x 0x%02x",
737 if (cmd
== MCE_IRDATA_TRAILER
)
738 dev_dbg(dev
, "End of raw IR data");
739 else if ((cmd
!= MCE_CMD_PORT_IR
) &&
740 ((cmd
& MCE_PORT_MASK
) == MCE_COMMAND_IRDATA
))
741 dev_dbg(dev
, "Raw IR data, %d pulse/space samples", ir
->rem
);
746 * Schedule work that can't be done in interrupt handlers
747 * (mceusb_dev_recv() and mce_async_callback()) nor tasklets.
748 * Invokes mceusb_deferred_kevent() for recovering from
749 * error events specified by the kevent bit field.
751 static void mceusb_defer_kevent(struct mceusb_dev
*ir
, int kevent
)
753 set_bit(kevent
, &ir
->kevent_flags
);
754 if (!schedule_work(&ir
->kevent
))
755 dev_err(ir
->dev
, "kevent %d may have been dropped", kevent
);
757 dev_dbg(ir
->dev
, "kevent %d scheduled", kevent
);
760 static void mce_async_callback(struct urb
*urb
)
762 struct mceusb_dev
*ir
;
770 switch (urb
->status
) {
773 len
= urb
->actual_length
;
775 mceusb_dev_printdata(ir
, urb
->transfer_buffer
, len
,
786 dev_err(ir
->dev
, "Error: request urb status = %d (TX HALT)",
788 mceusb_defer_kevent(ir
, EVENT_TX_HALT
);
792 dev_err(ir
->dev
, "Error: request urb status = %d", urb
->status
);
796 /* the transfer buffer and urb were allocated in mce_request_packet */
797 kfree(urb
->transfer_buffer
);
801 /* request outgoing (send) usb packet - used to initialize remote */
802 static void mce_request_packet(struct mceusb_dev
*ir
, unsigned char *data
,
806 struct urb
*async_urb
;
807 struct device
*dev
= ir
->dev
;
808 unsigned char *async_buf
;
810 async_urb
= usb_alloc_urb(0, GFP_KERNEL
);
811 if (unlikely(!async_urb
)) {
812 dev_err(dev
, "Error, couldn't allocate urb!");
816 async_buf
= kmalloc(size
, GFP_KERNEL
);
818 usb_free_urb(async_urb
);
823 if (usb_endpoint_xfer_int(ir
->usb_ep_out
))
824 usb_fill_int_urb(async_urb
, ir
->usbdev
, ir
->pipe_out
,
825 async_buf
, size
, mce_async_callback
, ir
,
826 ir
->usb_ep_out
->bInterval
);
828 usb_fill_bulk_urb(async_urb
, ir
->usbdev
, ir
->pipe_out
,
829 async_buf
, size
, mce_async_callback
, ir
);
831 memcpy(async_buf
, data
, size
);
833 dev_dbg(dev
, "send request called (size=%#x)", size
);
835 res
= usb_submit_urb(async_urb
, GFP_ATOMIC
);
837 dev_err(dev
, "send request FAILED! (res=%d)", res
);
839 usb_free_urb(async_urb
);
842 dev_dbg(dev
, "send request complete (res=%d)", res
);
845 static void mce_async_out(struct mceusb_dev
*ir
, unsigned char *data
, int size
)
847 int rsize
= sizeof(DEVICE_RESUME
);
849 if (ir
->need_reset
) {
850 ir
->need_reset
= false;
851 mce_request_packet(ir
, DEVICE_RESUME
, rsize
);
855 mce_request_packet(ir
, data
, size
);
859 /* Send data out the IR blaster port(s) */
860 static int mceusb_tx_ir(struct rc_dev
*dev
, unsigned *txbuf
, unsigned count
)
862 struct mceusb_dev
*ir
= dev
->priv
;
863 int i
, length
, ret
= 0;
865 unsigned char cmdbuf
[MCE_CMDBUF_SIZE
];
867 /* MCE tx init header */
868 cmdbuf
[cmdcount
++] = MCE_CMD_PORT_IR
;
869 cmdbuf
[cmdcount
++] = MCE_CMD_SETIRTXPORTS
;
870 cmdbuf
[cmdcount
++] = ir
->tx_mask
;
872 /* Send the set TX ports command */
873 mce_async_out(ir
, cmdbuf
, cmdcount
);
876 /* Generate mce packet data */
877 for (i
= 0; (i
< count
) && (cmdcount
< MCE_CMDBUF_SIZE
); i
++) {
878 txbuf
[i
] = txbuf
[i
] / MCE_TIME_UNIT
;
880 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
882 /* Insert mce packet header every 4th entry */
883 if ((cmdcount
< MCE_CMDBUF_SIZE
) &&
884 (cmdcount
% MCE_CODE_LENGTH
) == 0)
885 cmdbuf
[cmdcount
++] = MCE_IRDATA_HEADER
;
887 /* Insert mce packet data */
888 if (cmdcount
< MCE_CMDBUF_SIZE
)
890 (txbuf
[i
] < MCE_PULSE_BIT
?
891 txbuf
[i
] : MCE_MAX_PULSE_LENGTH
) |
892 (i
& 1 ? 0x00 : MCE_PULSE_BIT
);
898 } while ((txbuf
[i
] > MCE_MAX_PULSE_LENGTH
) &&
899 (txbuf
[i
] -= MCE_MAX_PULSE_LENGTH
));
902 /* Check if we have room for the empty packet at the end */
903 if (cmdcount
>= MCE_CMDBUF_SIZE
) {
908 /* Fix packet length in last header */
909 length
= cmdcount
% MCE_CODE_LENGTH
;
910 cmdbuf
[cmdcount
- length
] -= MCE_CODE_LENGTH
- length
;
912 /* All mce commands end with an empty packet (0x80) */
913 cmdbuf
[cmdcount
++] = MCE_IRDATA_TRAILER
;
915 /* Transmit the command to the mce device */
916 mce_async_out(ir
, cmdbuf
, cmdcount
);
919 return ret
? ret
: count
;
922 /* Sets active IR outputs -- mce devices typically have two */
923 static int mceusb_set_tx_mask(struct rc_dev
*dev
, u32 mask
)
925 struct mceusb_dev
*ir
= dev
->priv
;
927 /* return number of transmitters */
928 int emitters
= ir
->num_txports
? ir
->num_txports
: 2;
930 if (mask
>= (1 << emitters
))
933 if (ir
->flags
.tx_mask_normal
)
936 ir
->tx_mask
= (mask
!= MCE_DEFAULT_TX_MASK
?
937 mask
^ MCE_DEFAULT_TX_MASK
: mask
) << 1;
942 /* Sets the send carrier frequency and mode */
943 static int mceusb_set_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
945 struct mceusb_dev
*ir
= dev
->priv
;
947 int prescaler
= 0, divisor
= 0;
948 unsigned char cmdbuf
[4] = { MCE_CMD_PORT_IR
,
949 MCE_CMD_SETIRCFS
, 0x00, 0x00 };
951 /* Carrier has changed */
952 if (ir
->carrier
!= carrier
) {
955 ir
->carrier
= carrier
;
956 cmdbuf
[2] = MCE_CMD_SIG_END
;
957 cmdbuf
[3] = MCE_IRDATA_TRAILER
;
958 dev_dbg(ir
->dev
, "disabling carrier modulation");
959 mce_async_out(ir
, cmdbuf
, sizeof(cmdbuf
));
963 for (prescaler
= 0; prescaler
< 4; ++prescaler
) {
964 divisor
= (clk
>> (2 * prescaler
)) / carrier
;
965 if (divisor
<= 0xff) {
966 ir
->carrier
= carrier
;
967 cmdbuf
[2] = prescaler
;
969 dev_dbg(ir
->dev
, "requesting %u HZ carrier",
972 /* Transmit new carrier to mce device */
973 mce_async_out(ir
, cmdbuf
, sizeof(cmdbuf
));
986 * Select or deselect the 2nd receiver port.
987 * Second receiver is learning mode, wide-band, short-range receiver.
988 * Only one receiver (long or short range) may be active at a time.
990 static int mceusb_set_rx_wideband(struct rc_dev
*dev
, int enable
)
992 struct mceusb_dev
*ir
= dev
->priv
;
993 unsigned char cmdbuf
[3] = { MCE_CMD_PORT_IR
,
994 MCE_CMD_SETIRRXPORTEN
, 0x00 };
996 dev_dbg(ir
->dev
, "select %s-range receive sensor",
997 enable
? "short" : "long");
999 ir
->wideband_rx_enabled
= true;
1000 cmdbuf
[2] = 2; /* port 2 is short range receiver */
1002 ir
->wideband_rx_enabled
= false;
1003 cmdbuf
[2] = 1; /* port 1 is long range receiver */
1005 mce_async_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1006 /* response from device sets ir->learning_active */
1012 * Enable/disable receiver carrier frequency pass through reporting.
1013 * Only the short-range receiver has carrier frequency measuring capability.
1014 * Implicitly select this receiver when enabling carrier frequency reporting.
1016 static int mceusb_set_rx_carrier_report(struct rc_dev
*dev
, int enable
)
1018 struct mceusb_dev
*ir
= dev
->priv
;
1019 unsigned char cmdbuf
[3] = { MCE_CMD_PORT_IR
,
1020 MCE_CMD_SETIRRXPORTEN
, 0x00 };
1022 dev_dbg(ir
->dev
, "%s short-range receiver carrier reporting",
1023 enable
? "enable" : "disable");
1025 ir
->carrier_report_enabled
= true;
1026 if (!ir
->learning_active
) {
1027 cmdbuf
[2] = 2; /* port 2 is short range receiver */
1028 mce_async_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1031 ir
->carrier_report_enabled
= false;
1033 * Revert to normal (long-range) receiver only if the
1034 * wideband (short-range) receiver wasn't explicitly
1037 if (ir
->learning_active
&& !ir
->wideband_rx_enabled
) {
1038 cmdbuf
[2] = 1; /* port 1 is long range receiver */
1039 mce_async_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1047 * We don't do anything but print debug spew for many of the command bits
1048 * we receive from the hardware, but some of them are useful information
1049 * we want to store so that we can use them.
1051 static void mceusb_handle_command(struct mceusb_dev
*ir
, int index
)
1053 DEFINE_IR_RAW_EVENT(rawir
);
1054 u8 hi
= ir
->buf_in
[index
+ 1] & 0xff;
1055 u8 lo
= ir
->buf_in
[index
+ 2] & 0xff;
1059 switch (ir
->buf_in
[index
]) {
1060 /* the one and only 5-byte return value command */
1061 case MCE_RSP_GETPORTSTATUS
:
1062 if ((ir
->buf_in
[index
+ 4] & 0xff) == 0x00)
1063 ir
->txports_cabled
|= 1 << hi
;
1066 /* 2-byte return value commands */
1067 case MCE_RSP_EQIRTIMEOUT
:
1068 ir
->rc
->timeout
= US_TO_NS((hi
<< 8 | lo
) * MCE_TIME_UNIT
);
1070 case MCE_RSP_EQIRNUMPORTS
:
1071 ir
->num_txports
= hi
;
1072 ir
->num_rxports
= lo
;
1074 case MCE_RSP_EQIRRXCFCNT
:
1076 * The carrier cycle counter can overflow and wrap around
1077 * without notice from the device. So frequency measurement
1078 * will be inaccurate with long duration IR.
1080 * The long-range (non learning) receiver always reports
1081 * zero count so we always ignore its report.
1083 if (ir
->carrier_report_enabled
&& ir
->learning_active
&&
1084 ir
->pulse_tunit
> 0) {
1085 carrier_cycles
= (hi
<< 8 | lo
);
1087 * Adjust carrier cycle count by adding
1088 * 1 missed count per pulse "on"
1090 cycles_fix
= ir
->flags
.rx2
== 2 ? ir
->pulse_count
: 0;
1091 rawir
.carrier_report
= 1;
1092 rawir
.carrier
= (1000000u / MCE_TIME_UNIT
) *
1093 (carrier_cycles
+ cycles_fix
) /
1095 dev_dbg(ir
->dev
, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)",
1096 rawir
.carrier
, ir
->pulse_count
, carrier_cycles
,
1097 ir
->pulse_tunit
, ir
->flags
.rx2
);
1098 ir_raw_event_store(ir
->rc
, &rawir
);
1102 /* 1-byte return value commands */
1103 case MCE_RSP_EQEMVER
:
1106 case MCE_RSP_EQIRTXPORTS
:
1109 case MCE_RSP_EQIRRXPORTEN
:
1110 ir
->learning_active
= ((hi
& 0x02) == 0x02);
1111 if (ir
->rxports_active
!= hi
) {
1112 dev_info(ir
->dev
, "%s-range (0x%x) receiver active",
1113 ir
->learning_active
? "short" : "long", hi
);
1114 ir
->rxports_active
= hi
;
1117 case MCE_RSP_CMD_ILLEGAL
:
1118 ir
->need_reset
= true;
1125 static void mceusb_process_ir_data(struct mceusb_dev
*ir
, int buf_len
)
1127 DEFINE_IR_RAW_EVENT(rawir
);
1131 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
1132 if (ir
->flags
.microsoft_gen1
)
1135 /* if there's no data, just return now */
1139 for (; i
< buf_len
; i
++) {
1140 switch (ir
->parser_state
) {
1142 ir
->rem
= mceusb_cmd_datasize(ir
->cmd
, ir
->buf_in
[i
]);
1143 mceusb_dev_printdata(ir
, ir
->buf_in
, buf_len
, i
- 1,
1144 ir
->rem
+ 2, false);
1145 mceusb_handle_command(ir
, i
);
1146 ir
->parser_state
= CMD_DATA
;
1150 init_ir_raw_event(&rawir
);
1151 rawir
.pulse
= ((ir
->buf_in
[i
] & MCE_PULSE_BIT
) != 0);
1152 rawir
.duration
= (ir
->buf_in
[i
] & MCE_PULSE_MASK
);
1154 ir
->pulse_tunit
+= rawir
.duration
;
1157 rawir
.duration
*= US_TO_NS(MCE_TIME_UNIT
);
1159 dev_dbg(ir
->dev
, "Storing %s %u ns (%02x)",
1160 rawir
.pulse
? "pulse" : "space",
1161 rawir
.duration
, ir
->buf_in
[i
]);
1163 if (ir_raw_event_store_with_filter(ir
->rc
, &rawir
))
1170 /* decode mce packets of the form (84),AA,BB,CC,DD */
1171 /* IR data packets can span USB messages - rem */
1172 ir
->cmd
= ir
->buf_in
[i
];
1173 if ((ir
->cmd
== MCE_CMD_PORT_IR
) ||
1174 ((ir
->cmd
& MCE_PORT_MASK
) !=
1175 MCE_COMMAND_IRDATA
)) {
1176 ir
->parser_state
= SUBCMD
;
1179 ir
->rem
= (ir
->cmd
& MCE_PACKET_LENGTH_MASK
);
1180 mceusb_dev_printdata(ir
, ir
->buf_in
, buf_len
,
1181 i
, ir
->rem
+ 1, false);
1183 ir
->parser_state
= PARSE_IRDATA
;
1185 ir_raw_event_reset(ir
->rc
);
1186 ir
->pulse_tunit
= 0;
1187 ir
->pulse_count
= 0;
1192 if (ir
->parser_state
!= CMD_HEADER
&& !ir
->rem
)
1193 ir
->parser_state
= CMD_HEADER
;
1196 dev_dbg(ir
->dev
, "processed IR data");
1197 ir_raw_event_handle(ir
->rc
);
1201 static void mceusb_dev_recv(struct urb
*urb
)
1203 struct mceusb_dev
*ir
;
1210 usb_unlink_urb(urb
);
1214 switch (urb
->status
) {
1217 mceusb_process_ir_data(ir
, urb
->actual_length
);
1224 usb_unlink_urb(urb
);
1228 dev_err(ir
->dev
, "Error: urb status = %d (RX HALT)",
1230 mceusb_defer_kevent(ir
, EVENT_RX_HALT
);
1234 dev_err(ir
->dev
, "Error: urb status = %d", urb
->status
);
1238 usb_submit_urb(urb
, GFP_ATOMIC
);
1241 static void mceusb_get_emulator_version(struct mceusb_dev
*ir
)
1243 /* If we get no reply or an illegal command reply, its ver 1, says MS */
1245 mce_async_out(ir
, GET_EMVER
, sizeof(GET_EMVER
));
1248 static void mceusb_gen1_init(struct mceusb_dev
*ir
)
1251 struct device
*dev
= ir
->dev
;
1254 data
= kzalloc(USB_CTRL_MSG_SZ
, GFP_KERNEL
);
1256 dev_err(dev
, "%s: memory allocation failed!", __func__
);
1261 * This is a strange one. Windows issues a set address to the device
1262 * on the receive control pipe and expect a certain value pair back
1264 ret
= usb_control_msg(ir
->usbdev
, usb_rcvctrlpipe(ir
->usbdev
, 0),
1265 USB_REQ_SET_ADDRESS
, USB_TYPE_VENDOR
, 0, 0,
1266 data
, USB_CTRL_MSG_SZ
, HZ
* 3);
1267 dev_dbg(dev
, "set address - ret = %d", ret
);
1268 dev_dbg(dev
, "set address - data[0] = %d, data[1] = %d",
1271 /* set feature: bit rate 38400 bps */
1272 ret
= usb_control_msg(ir
->usbdev
, usb_sndctrlpipe(ir
->usbdev
, 0),
1273 USB_REQ_SET_FEATURE
, USB_TYPE_VENDOR
,
1274 0xc04e, 0x0000, NULL
, 0, HZ
* 3);
1276 dev_dbg(dev
, "set feature - ret = %d", ret
);
1278 /* bRequest 4: set char length to 8 bits */
1279 ret
= usb_control_msg(ir
->usbdev
, usb_sndctrlpipe(ir
->usbdev
, 0),
1281 0x0808, 0x0000, NULL
, 0, HZ
* 3);
1282 dev_dbg(dev
, "set char length - retB = %d", ret
);
1284 /* bRequest 2: set handshaking to use DTR/DSR */
1285 ret
= usb_control_msg(ir
->usbdev
, usb_sndctrlpipe(ir
->usbdev
, 0),
1287 0x0000, 0x0100, NULL
, 0, HZ
* 3);
1288 dev_dbg(dev
, "set handshake - retC = %d", ret
);
1291 mce_async_out(ir
, DEVICE_RESUME
, sizeof(DEVICE_RESUME
));
1293 /* get hw/sw revision? */
1294 mce_async_out(ir
, GET_REVISION
, sizeof(GET_REVISION
));
1299 static void mceusb_gen2_init(struct mceusb_dev
*ir
)
1302 mce_async_out(ir
, DEVICE_RESUME
, sizeof(DEVICE_RESUME
));
1304 /* get wake version (protocol, key, address) */
1305 mce_async_out(ir
, GET_WAKEVERSION
, sizeof(GET_WAKEVERSION
));
1307 /* unknown what this one actually returns... */
1308 mce_async_out(ir
, GET_UNKNOWN2
, sizeof(GET_UNKNOWN2
));
1311 static void mceusb_get_parameters(struct mceusb_dev
*ir
)
1314 unsigned char cmdbuf
[3] = { MCE_CMD_PORT_SYS
,
1315 MCE_CMD_GETPORTSTATUS
, 0x00 };
1317 /* defaults, if the hardware doesn't support querying */
1318 ir
->num_txports
= 2;
1319 ir
->num_rxports
= 2;
1321 /* get number of tx and rx ports */
1322 mce_async_out(ir
, GET_NUM_PORTS
, sizeof(GET_NUM_PORTS
));
1324 /* get the carrier and frequency */
1325 mce_async_out(ir
, GET_CARRIER_FREQ
, sizeof(GET_CARRIER_FREQ
));
1327 if (ir
->num_txports
&& !ir
->flags
.no_tx
)
1328 /* get the transmitter bitmask */
1329 mce_async_out(ir
, GET_TX_BITMASK
, sizeof(GET_TX_BITMASK
));
1331 /* get receiver timeout value */
1332 mce_async_out(ir
, GET_RX_TIMEOUT
, sizeof(GET_RX_TIMEOUT
));
1334 /* get receiver sensor setting */
1335 mce_async_out(ir
, GET_RX_SENSOR
, sizeof(GET_RX_SENSOR
));
1337 for (i
= 0; i
< ir
->num_txports
; i
++) {
1339 mce_async_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1343 static void mceusb_flash_led(struct mceusb_dev
*ir
)
1348 mce_async_out(ir
, FLASH_LED
, sizeof(FLASH_LED
));
1352 * Workqueue function
1353 * for resetting or recovering device after occurrence of error events
1354 * specified in ir->kevent bit field.
1355 * Function runs (via schedule_work()) in non-interrupt context, for
1356 * calls here (such as usb_clear_halt()) requiring non-interrupt context.
1358 static void mceusb_deferred_kevent(struct work_struct
*work
)
1360 struct mceusb_dev
*ir
=
1361 container_of(work
, struct mceusb_dev
, kevent
);
1364 if (test_bit(EVENT_RX_HALT
, &ir
->kevent_flags
)) {
1365 usb_unlink_urb(ir
->urb_in
);
1366 status
= usb_clear_halt(ir
->usbdev
, ir
->pipe_in
);
1368 dev_err(ir
->dev
, "rx clear halt error %d",
1371 clear_bit(EVENT_RX_HALT
, &ir
->kevent_flags
);
1373 status
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
1376 "rx unhalt submit urb error %d",
1382 if (test_bit(EVENT_TX_HALT
, &ir
->kevent_flags
)) {
1383 status
= usb_clear_halt(ir
->usbdev
, ir
->pipe_out
);
1385 dev_err(ir
->dev
, "tx clear halt error %d", status
);
1386 clear_bit(EVENT_TX_HALT
, &ir
->kevent_flags
);
1390 static struct rc_dev
*mceusb_init_rc_dev(struct mceusb_dev
*ir
)
1392 struct usb_device
*udev
= ir
->usbdev
;
1393 struct device
*dev
= ir
->dev
;
1397 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
1399 dev_err(dev
, "remote dev allocation failed");
1403 snprintf(ir
->name
, sizeof(ir
->name
), "%s (%04x:%04x)",
1404 mceusb_model
[ir
->model
].name
?
1405 mceusb_model
[ir
->model
].name
:
1406 "Media Center Ed. eHome Infrared Remote Transceiver",
1407 le16_to_cpu(ir
->usbdev
->descriptor
.idVendor
),
1408 le16_to_cpu(ir
->usbdev
->descriptor
.idProduct
));
1410 usb_make_path(ir
->usbdev
, ir
->phys
, sizeof(ir
->phys
));
1412 rc
->device_name
= ir
->name
;
1413 rc
->input_phys
= ir
->phys
;
1414 usb_to_input_id(ir
->usbdev
, &rc
->input_id
);
1415 rc
->dev
.parent
= dev
;
1417 rc
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
1418 rc
->timeout
= MS_TO_NS(100);
1419 if (!ir
->flags
.no_tx
) {
1420 rc
->s_tx_mask
= mceusb_set_tx_mask
;
1421 rc
->s_tx_carrier
= mceusb_set_tx_carrier
;
1422 rc
->tx_ir
= mceusb_tx_ir
;
1424 if (ir
->flags
.rx2
> 0) {
1425 rc
->s_learning_mode
= mceusb_set_rx_wideband
;
1426 rc
->s_carrier_report
= mceusb_set_rx_carrier_report
;
1428 rc
->driver_name
= DRIVER_NAME
;
1430 switch (le16_to_cpu(udev
->descriptor
.idVendor
)) {
1431 case VENDOR_HAUPPAUGE
:
1432 rc
->map_name
= RC_MAP_HAUPPAUGE
;
1435 rc
->map_name
= RC_MAP_PINNACLE_PCTV_HD
;
1438 rc
->map_name
= RC_MAP_RC6_MCE
;
1440 if (mceusb_model
[ir
->model
].rc_map
)
1441 rc
->map_name
= mceusb_model
[ir
->model
].rc_map
;
1443 ret
= rc_register_device(rc
);
1445 dev_err(dev
, "remote dev registration failed");
1456 static int mceusb_dev_probe(struct usb_interface
*intf
,
1457 const struct usb_device_id
*id
)
1459 struct usb_device
*dev
= interface_to_usbdev(intf
);
1460 struct usb_host_interface
*idesc
;
1461 struct usb_endpoint_descriptor
*ep
= NULL
;
1462 struct usb_endpoint_descriptor
*ep_in
= NULL
;
1463 struct usb_endpoint_descriptor
*ep_out
= NULL
;
1464 struct mceusb_dev
*ir
= NULL
;
1465 int pipe
, maxp
, i
, res
;
1466 char buf
[63], name
[128] = "";
1467 enum mceusb_model_type model
= id
->driver_info
;
1469 bool is_microsoft_gen1
;
1470 bool tx_mask_normal
;
1473 dev_dbg(&intf
->dev
, "%s called", __func__
);
1475 idesc
= intf
->cur_altsetting
;
1477 is_gen3
= mceusb_model
[model
].mce_gen3
;
1478 is_microsoft_gen1
= mceusb_model
[model
].mce_gen1
;
1479 tx_mask_normal
= mceusb_model
[model
].tx_mask_normal
;
1480 ir_intfnum
= mceusb_model
[model
].ir_intfnum
;
1482 /* There are multi-function devices with non-IR interfaces */
1483 if (idesc
->desc
.bInterfaceNumber
!= ir_intfnum
)
1486 /* step through the endpoints to find first bulk in and out endpoint */
1487 for (i
= 0; i
< idesc
->desc
.bNumEndpoints
; ++i
) {
1488 ep
= &idesc
->endpoint
[i
].desc
;
1490 if (ep_in
== NULL
) {
1491 if (usb_endpoint_is_bulk_in(ep
)) {
1493 dev_dbg(&intf
->dev
, "acceptable bulk inbound endpoint found\n");
1494 } else if (usb_endpoint_is_int_in(ep
)) {
1496 ep_in
->bInterval
= 1;
1497 dev_dbg(&intf
->dev
, "acceptable interrupt inbound endpoint found\n");
1501 if (ep_out
== NULL
) {
1502 if (usb_endpoint_is_bulk_out(ep
)) {
1504 dev_dbg(&intf
->dev
, "acceptable bulk outbound endpoint found\n");
1505 } else if (usb_endpoint_is_int_out(ep
)) {
1507 ep_out
->bInterval
= 1;
1508 dev_dbg(&intf
->dev
, "acceptable interrupt outbound endpoint found\n");
1512 if (!ep_in
|| !ep_out
) {
1513 dev_dbg(&intf
->dev
, "required endpoints not found\n");
1517 if (usb_endpoint_xfer_int(ep_in
))
1518 pipe
= usb_rcvintpipe(dev
, ep_in
->bEndpointAddress
);
1520 pipe
= usb_rcvbulkpipe(dev
, ep_in
->bEndpointAddress
);
1521 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
1523 ir
= kzalloc(sizeof(struct mceusb_dev
), GFP_KERNEL
);
1525 goto mem_alloc_fail
;
1528 ir
->buf_in
= usb_alloc_coherent(dev
, maxp
, GFP_ATOMIC
, &ir
->dma_in
);
1530 goto buf_in_alloc_fail
;
1532 ir
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
1534 goto urb_in_alloc_fail
;
1536 ir
->usbdev
= usb_get_dev(dev
);
1537 ir
->dev
= &intf
->dev
;
1539 ir
->flags
.microsoft_gen1
= is_microsoft_gen1
;
1540 ir
->flags
.tx_mask_normal
= tx_mask_normal
;
1541 ir
->flags
.no_tx
= mceusb_model
[model
].no_tx
;
1542 ir
->flags
.rx2
= mceusb_model
[model
].rx2
;
1545 /* Saving usb interface data for use by the transmitter routine */
1546 ir
->usb_ep_out
= ep_out
;
1547 if (usb_endpoint_xfer_int(ep_out
))
1548 ir
->pipe_out
= usb_sndintpipe(ir
->usbdev
,
1549 ep_out
->bEndpointAddress
);
1551 ir
->pipe_out
= usb_sndbulkpipe(ir
->usbdev
,
1552 ep_out
->bEndpointAddress
);
1554 if (dev
->descriptor
.iManufacturer
1555 && usb_string(dev
, dev
->descriptor
.iManufacturer
,
1556 buf
, sizeof(buf
)) > 0)
1557 strlcpy(name
, buf
, sizeof(name
));
1558 if (dev
->descriptor
.iProduct
1559 && usb_string(dev
, dev
->descriptor
.iProduct
,
1560 buf
, sizeof(buf
)) > 0)
1561 snprintf(name
+ strlen(name
), sizeof(name
) - strlen(name
),
1565 * Initialize async USB error handler before registering
1566 * or activating any mceusb RX and TX functions
1568 INIT_WORK(&ir
->kevent
, mceusb_deferred_kevent
);
1570 ir
->rc
= mceusb_init_rc_dev(ir
);
1574 /* wire up inbound data handler */
1575 if (usb_endpoint_xfer_int(ep_in
))
1576 usb_fill_int_urb(ir
->urb_in
, dev
, pipe
, ir
->buf_in
, maxp
,
1577 mceusb_dev_recv
, ir
, ep_in
->bInterval
);
1579 usb_fill_bulk_urb(ir
->urb_in
, dev
, pipe
, ir
->buf_in
, maxp
,
1580 mceusb_dev_recv
, ir
);
1582 ir
->urb_in
->transfer_dma
= ir
->dma_in
;
1583 ir
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1585 /* flush buffers on the device */
1586 dev_dbg(&intf
->dev
, "Flushing receive buffers");
1587 res
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
1589 dev_err(&intf
->dev
, "failed to flush buffers: %d", res
);
1591 /* figure out which firmware/emulator version this hardware has */
1592 mceusb_get_emulator_version(ir
);
1594 /* initialize device */
1595 if (ir
->flags
.microsoft_gen1
)
1596 mceusb_gen1_init(ir
);
1598 mceusb_gen2_init(ir
);
1600 mceusb_get_parameters(ir
);
1602 mceusb_flash_led(ir
);
1604 if (!ir
->flags
.no_tx
)
1605 mceusb_set_tx_mask(ir
->rc
, MCE_DEFAULT_TX_MASK
);
1607 usb_set_intfdata(intf
, ir
);
1609 /* enable wake via this device */
1610 device_set_wakeup_capable(ir
->dev
, true);
1611 device_set_wakeup_enable(ir
->dev
, true);
1613 dev_info(&intf
->dev
, "Registered %s with mce emulator interface version %x",
1615 dev_info(&intf
->dev
, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
1616 ir
->num_txports
, ir
->txports_cabled
,
1617 ir
->num_rxports
, ir
->rxports_active
);
1621 /* Error-handling path */
1623 cancel_work_sync(&ir
->kevent
);
1624 usb_put_dev(ir
->usbdev
);
1625 usb_kill_urb(ir
->urb_in
);
1626 usb_free_urb(ir
->urb_in
);
1628 usb_free_coherent(dev
, maxp
, ir
->buf_in
, ir
->dma_in
);
1632 dev_err(&intf
->dev
, "%s: device setup failed!", __func__
);
1638 static void mceusb_dev_disconnect(struct usb_interface
*intf
)
1640 struct usb_device
*dev
= interface_to_usbdev(intf
);
1641 struct mceusb_dev
*ir
= usb_get_intfdata(intf
);
1643 usb_set_intfdata(intf
, NULL
);
1649 cancel_work_sync(&ir
->kevent
);
1650 rc_unregister_device(ir
->rc
);
1651 usb_kill_urb(ir
->urb_in
);
1652 usb_free_urb(ir
->urb_in
);
1653 usb_free_coherent(dev
, ir
->len_in
, ir
->buf_in
, ir
->dma_in
);
1659 static int mceusb_dev_suspend(struct usb_interface
*intf
, pm_message_t message
)
1661 struct mceusb_dev
*ir
= usb_get_intfdata(intf
);
1662 dev_info(ir
->dev
, "suspend");
1663 usb_kill_urb(ir
->urb_in
);
1667 static int mceusb_dev_resume(struct usb_interface
*intf
)
1669 struct mceusb_dev
*ir
= usb_get_intfdata(intf
);
1670 dev_info(ir
->dev
, "resume");
1671 if (usb_submit_urb(ir
->urb_in
, GFP_ATOMIC
))
1676 static struct usb_driver mceusb_dev_driver
= {
1677 .name
= DRIVER_NAME
,
1678 .probe
= mceusb_dev_probe
,
1679 .disconnect
= mceusb_dev_disconnect
,
1680 .suspend
= mceusb_dev_suspend
,
1681 .resume
= mceusb_dev_resume
,
1682 .reset_resume
= mceusb_dev_resume
,
1683 .id_table
= mceusb_dev_table
1686 module_usb_driver(mceusb_dev_driver
);
1688 MODULE_DESCRIPTION(DRIVER_DESC
);
1689 MODULE_AUTHOR(DRIVER_AUTHOR
);
1690 MODULE_LICENSE("GPL");
1691 MODULE_DEVICE_TABLE(usb
, mceusb_dev_table
);