1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
5 * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com>
7 * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
8 * Conti, Martin Blatter and Daniel Melander, the latter of which was
9 * in turn also based on the lirc_atiusb driver by Paul Miller. The
10 * two mce drivers were merged into one by Jarod Wilson, with transmit
11 * support for the 1st-gen device added primarily by Patrick Calhoun,
12 * with a bit of tweaks by Jarod. Debugging improvements and proper
13 * support for what appears to be 3rd-gen hardware added by Jarod.
14 * Initial port from lirc driver to ir-core drivery by Jarod, based
15 * partially on a port to an earlier proposed IR infrastructure by
16 * Jon Smirl, which included enhancements and simplifications to the
17 * incoming IR buffer parsing routines.
19 * Updated in July of 2011 with the aid of Microsoft's official
20 * remote/transceiver requirements and specification document, found at
21 * download.microsoft.com, title
22 * Windows-Media-Center-RC-IR-Collection-Green-Button-Specification-03-08-2011-V2.pdf
25 #include <linux/device.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/workqueue.h>
29 #include <linux/usb.h>
30 #include <linux/usb/input.h>
31 #include <linux/pm_wakeup.h>
32 #include <media/rc-core.h>
34 #define DRIVER_VERSION "1.95"
35 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
36 #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
38 #define DRIVER_NAME "mceusb"
40 #define USB_TX_TIMEOUT 1000 /* in milliseconds */
41 #define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
42 #define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
45 #define MCE_IRBUF_SIZE 128 /* TX IR buffer length */
46 #define MCE_TIME_UNIT 50 /* Approx 50us resolution */
47 #define MCE_PACKET_SIZE 31 /* Max length of packet (with header) */
48 #define MCE_IRDATA_HEADER (0x80 + MCE_PACKET_SIZE - 1)
49 /* Actual format is 0x80 + num_bytes */
50 #define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
51 #define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
52 #define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
53 #define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
54 #define MCE_PULSE_MASK 0x7f /* Pulse mask */
55 #define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
58 * The interface between the host and the IR hardware is command-response
59 * based. All commands and responses have a consistent format, where a lead
60 * byte always identifies the type of data following it. The lead byte has
61 * a port value in the 3 highest bits and a length value in the 5 lowest
64 * The length field is overloaded, with a value of 11111 indicating that the
65 * following byte is a command or response code, and the length of the entire
66 * message is determined by the code. If the length field is not 11111, then
67 * it specifies the number of bytes of port data that follow.
70 #define MCE_PORT_IR 0x4 /* (0x4 << 5) | MCE_CMD = 0x9f */
71 #define MCE_PORT_SYS 0x7 /* (0x7 << 5) | MCE_CMD = 0xff */
72 #define MCE_PORT_SER 0x6 /* 0xc0 through 0xdf flush & 0x1f bytes */
73 #define MCE_PORT_MASK 0xe0 /* Mask out command bits */
75 /* Command port headers */
76 #define MCE_CMD_PORT_IR 0x9f /* IR-related cmd/rsp */
77 #define MCE_CMD_PORT_SYS 0xff /* System (non-IR) device cmd/rsp */
79 /* Commands that set device state (2-4 bytes in length) */
80 #define MCE_CMD_RESET 0xfe /* Reset device, 2 bytes */
81 #define MCE_CMD_RESUME 0xaa /* Resume device after error, 2 bytes */
82 #define MCE_CMD_SETIRCFS 0x06 /* Set tx carrier, 4 bytes */
83 #define MCE_CMD_SETIRTIMEOUT 0x0c /* Set timeout, 4 bytes */
84 #define MCE_CMD_SETIRTXPORTS 0x08 /* Set tx ports, 3 bytes */
85 #define MCE_CMD_SETIRRXPORTEN 0x14 /* Set rx ports, 3 bytes */
86 #define MCE_CMD_FLASHLED 0x23 /* Flash receiver LED, 2 bytes */
88 /* Commands that query device state (all 2 bytes, unless noted) */
89 #define MCE_CMD_GETIRCFS 0x07 /* Get carrier */
90 #define MCE_CMD_GETIRTIMEOUT 0x0d /* Get timeout */
91 #define MCE_CMD_GETIRTXPORTS 0x13 /* Get tx ports */
92 #define MCE_CMD_GETIRRXPORTEN 0x15 /* Get rx ports */
93 #define MCE_CMD_GETPORTSTATUS 0x11 /* Get tx port status, 3 bytes */
94 #define MCE_CMD_GETIRNUMPORTS 0x16 /* Get number of ports */
95 #define MCE_CMD_GETWAKESOURCE 0x17 /* Get wake source */
96 #define MCE_CMD_GETEMVER 0x22 /* Get emulator interface version */
97 #define MCE_CMD_GETDEVDETAILS 0x21 /* Get device details (em ver2 only) */
98 #define MCE_CMD_GETWAKESUPPORT 0x20 /* Get wake details (em ver2 only) */
99 #define MCE_CMD_GETWAKEVERSION 0x18 /* Get wake pattern (em ver2 only) */
102 #define MCE_CMD_NOP 0xff /* No operation */
104 /* Responses to commands (non-error cases) */
105 #define MCE_RSP_EQIRCFS 0x06 /* tx carrier, 4 bytes */
106 #define MCE_RSP_EQIRTIMEOUT 0x0c /* rx timeout, 4 bytes */
107 #define MCE_RSP_GETWAKESOURCE 0x17 /* wake source, 3 bytes */
108 #define MCE_RSP_EQIRTXPORTS 0x08 /* tx port mask, 3 bytes */
109 #define MCE_RSP_EQIRRXPORTEN 0x14 /* rx port mask, 3 bytes */
110 #define MCE_RSP_GETPORTSTATUS 0x11 /* tx port status, 7 bytes */
111 #define MCE_RSP_EQIRRXCFCNT 0x15 /* rx carrier count, 4 bytes */
112 #define MCE_RSP_EQIRNUMPORTS 0x16 /* number of ports, 4 bytes */
113 #define MCE_RSP_EQWAKESUPPORT 0x20 /* wake capabilities, 3 bytes */
114 #define MCE_RSP_EQWAKEVERSION 0x18 /* wake pattern details, 6 bytes */
115 #define MCE_RSP_EQDEVDETAILS 0x21 /* device capabilities, 3 bytes */
116 #define MCE_RSP_EQEMVER 0x22 /* emulator interface ver, 3 bytes */
117 #define MCE_RSP_FLASHLED 0x23 /* success flashing LED, 2 bytes */
119 /* Responses to error cases, must send MCE_CMD_RESUME to clear them */
120 #define MCE_RSP_CMD_ILLEGAL 0xfe /* illegal command for port, 2 bytes */
121 #define MCE_RSP_TX_TIMEOUT 0x81 /* tx timed out, 2 bytes */
123 /* Misc commands/responses not defined in the MCE remote/transceiver spec */
124 #define MCE_CMD_SIG_END 0x01 /* End of signal */
125 #define MCE_CMD_PING 0x03 /* Ping device */
126 #define MCE_CMD_UNKNOWN 0x04 /* Unknown */
127 #define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
128 #define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
129 #define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
130 #define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
131 #define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
132 #define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
133 #define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
134 #define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
135 #define MCE_CMD_NULL 0x00 /* These show up various places... */
137 /* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
138 * then we're looking at a raw IR data sample */
139 #define MCE_COMMAND_IRDATA 0x80
140 #define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
142 #define VENDOR_PHILIPS 0x0471
143 #define VENDOR_SMK 0x0609
144 #define VENDOR_TATUNG 0x1460
145 #define VENDOR_GATEWAY 0x107b
146 #define VENDOR_SHUTTLE 0x1308
147 #define VENDOR_SHUTTLE2 0x051c
148 #define VENDOR_MITSUMI 0x03ee
149 #define VENDOR_TOPSEED 0x1784
150 #define VENDOR_RICAVISION 0x179d
151 #define VENDOR_ITRON 0x195d
152 #define VENDOR_FIC 0x1509
153 #define VENDOR_LG 0x043e
154 #define VENDOR_MICROSOFT 0x045e
155 #define VENDOR_FORMOSA 0x147a
156 #define VENDOR_FINTEK 0x1934
157 #define VENDOR_PINNACLE 0x2304
158 #define VENDOR_ECS 0x1019
159 #define VENDOR_WISTRON 0x0fb8
160 #define VENDOR_COMPRO 0x185b
161 #define VENDOR_NORTHSTAR 0x04eb
162 #define VENDOR_REALTEK 0x0bda
163 #define VENDOR_TIVO 0x105a
164 #define VENDOR_CONEXANT 0x0572
165 #define VENDOR_TWISTEDMELON 0x2596
166 #define VENDOR_HAUPPAUGE 0x2040
167 #define VENDOR_PCTV 0x2013
168 #define VENDOR_ADAPTEC 0x03f3
170 enum mceusb_model_type
{
171 MCE_GEN2
= 0, /* Most boards */
174 MCE_GEN3_BROKEN_IRTIMEOUT
,
176 MCE_GEN2_TX_INV_RX_GOOD
,
182 HAUPPAUGE_CX_HYBRID_TV
,
183 EVROMEDIA_FULL_HYBRID_FULLHD
,
187 struct mceusb_model
{
191 u32 tx_mask_normal
:1;
193 u32 broken_irtimeout
:1;
195 * 2nd IR receiver (short-range, wideband) for learning mode:
196 * 0, absent 2nd receiver (rx2)
198 * 2, rx2 which under counts IR carrier cycles
204 const char *rc_map
; /* Allow specify a per-board map */
205 const char *name
; /* per-board name */
208 static const struct mceusb_model mceusb_model
[] = {
222 [MCE_GEN2_TX_INV
] = {
227 [MCE_GEN2_TX_INV_RX_GOOD
] = {
237 [MCE_GEN3_BROKEN_IRTIMEOUT
] = {
241 .broken_irtimeout
= 1
245 * In fact, the EVK is shipped without
246 * remotes, but we should have something handy,
247 * to allow testing it
249 .name
= "Conexant Hybrid TV (cx231xx) MCE IR",
253 .no_tx
= 1, /* tx isn't wired up at all */
254 .name
= "Conexant Hybrid TV (cx231xx) MCE IR",
256 [HAUPPAUGE_CX_HYBRID_TV
] = {
257 .no_tx
= 1, /* eeprom says it has no tx */
258 .name
= "Conexant Hybrid TV (cx231xx) MCE IR no TX",
267 .rc_map
= RC_MAP_TIVO
,
270 [EVROMEDIA_FULL_HYBRID_FULLHD
] = {
271 .name
= "Evromedia USB Full Hybrid Full HD",
273 .rc_map
= RC_MAP_MSI_DIGIVOX_III
,
275 [ASTROMETA_T2HYBRID
] = {
276 .name
= "Astrometa T2Hybrid",
278 .rc_map
= RC_MAP_ASTROMETA_T2HYBRID
,
282 static const struct usb_device_id mceusb_dev_table
[] = {
283 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
284 { USB_DEVICE(VENDOR_MICROSOFT
, 0x006d),
285 .driver_info
= MCE_GEN1
},
286 /* Philips Infrared Transceiver - Sahara branded */
287 { USB_DEVICE(VENDOR_PHILIPS
, 0x0608) },
288 /* Philips Infrared Transceiver - HP branded */
289 { USB_DEVICE(VENDOR_PHILIPS
, 0x060c),
290 .driver_info
= MCE_GEN2_TX_INV
},
291 /* Philips SRM5100 */
292 { USB_DEVICE(VENDOR_PHILIPS
, 0x060d) },
293 /* Philips Infrared Transceiver - Omaura */
294 { USB_DEVICE(VENDOR_PHILIPS
, 0x060f) },
295 /* Philips Infrared Transceiver - Spinel plus */
296 { USB_DEVICE(VENDOR_PHILIPS
, 0x0613) },
297 /* Philips eHome Infrared Transceiver */
298 { USB_DEVICE(VENDOR_PHILIPS
, 0x0815) },
299 /* Philips/Spinel plus IR transceiver for ASUS */
300 { USB_DEVICE(VENDOR_PHILIPS
, 0x206c) },
301 /* Philips/Spinel plus IR transceiver for ASUS */
302 { USB_DEVICE(VENDOR_PHILIPS
, 0x2088) },
303 /* Philips IR transceiver (Dell branded) */
304 { USB_DEVICE(VENDOR_PHILIPS
, 0x2093),
305 .driver_info
= MCE_GEN2_TX_INV
},
306 /* Realtek MCE IR Receiver and card reader */
307 { USB_DEVICE(VENDOR_REALTEK
, 0x0161),
308 .driver_info
= MULTIFUNCTION
},
309 /* SMK/Toshiba G83C0004D410 */
310 { USB_DEVICE(VENDOR_SMK
, 0x031d),
311 .driver_info
= MCE_GEN2_TX_INV_RX_GOOD
},
312 /* SMK eHome Infrared Transceiver (Sony VAIO) */
313 { USB_DEVICE(VENDOR_SMK
, 0x0322),
314 .driver_info
= MCE_GEN2_TX_INV
},
315 /* bundled with Hauppauge PVR-150 */
316 { USB_DEVICE(VENDOR_SMK
, 0x0334),
317 .driver_info
= MCE_GEN2_TX_INV
},
318 /* SMK eHome Infrared Transceiver */
319 { USB_DEVICE(VENDOR_SMK
, 0x0338) },
320 /* SMK/I-O Data GV-MC7/RCKIT Receiver */
321 { USB_DEVICE(VENDOR_SMK
, 0x0353),
322 .driver_info
= MCE_GEN2_NO_TX
},
323 /* SMK RXX6000 Infrared Receiver */
324 { USB_DEVICE(VENDOR_SMK
, 0x0357),
325 .driver_info
= MCE_GEN2_NO_TX
},
326 /* Tatung eHome Infrared Transceiver */
327 { USB_DEVICE(VENDOR_TATUNG
, 0x9150) },
328 /* Shuttle eHome Infrared Transceiver */
329 { USB_DEVICE(VENDOR_SHUTTLE
, 0xc001) },
330 /* Shuttle eHome Infrared Transceiver */
331 { USB_DEVICE(VENDOR_SHUTTLE2
, 0xc001) },
332 /* Gateway eHome Infrared Transceiver */
333 { USB_DEVICE(VENDOR_GATEWAY
, 0x3009) },
335 { USB_DEVICE(VENDOR_MITSUMI
, 0x2501) },
336 /* Topseed eHome Infrared Transceiver */
337 { USB_DEVICE(VENDOR_TOPSEED
, 0x0001),
338 .driver_info
= MCE_GEN2_TX_INV
},
339 /* Topseed HP eHome Infrared Transceiver */
340 { USB_DEVICE(VENDOR_TOPSEED
, 0x0006),
341 .driver_info
= MCE_GEN2_TX_INV
},
342 /* Topseed eHome Infrared Transceiver */
343 { USB_DEVICE(VENDOR_TOPSEED
, 0x0007),
344 .driver_info
= MCE_GEN2_TX_INV
},
345 /* Topseed eHome Infrared Transceiver */
346 { USB_DEVICE(VENDOR_TOPSEED
, 0x0008),
347 .driver_info
= MCE_GEN3
},
348 /* Topseed eHome Infrared Transceiver */
349 { USB_DEVICE(VENDOR_TOPSEED
, 0x000a),
350 .driver_info
= MCE_GEN2_TX_INV
},
351 /* Topseed eHome Infrared Transceiver */
352 { USB_DEVICE(VENDOR_TOPSEED
, 0x0011),
353 .driver_info
= MCE_GEN3_BROKEN_IRTIMEOUT
},
354 /* Ricavision internal Infrared Transceiver */
355 { USB_DEVICE(VENDOR_RICAVISION
, 0x0010) },
356 /* Itron ione Libra Q-11 */
357 { USB_DEVICE(VENDOR_ITRON
, 0x7002) },
358 /* FIC eHome Infrared Transceiver */
359 { USB_DEVICE(VENDOR_FIC
, 0x9242) },
360 /* LG eHome Infrared Transceiver */
361 { USB_DEVICE(VENDOR_LG
, 0x9803) },
362 /* Microsoft MCE Infrared Transceiver */
363 { USB_DEVICE(VENDOR_MICROSOFT
, 0x00a0) },
364 /* Formosa eHome Infrared Transceiver */
365 { USB_DEVICE(VENDOR_FORMOSA
, 0xe015) },
366 /* Formosa21 / eHome Infrared Receiver */
367 { USB_DEVICE(VENDOR_FORMOSA
, 0xe016) },
368 /* Formosa aim / Trust MCE Infrared Receiver */
369 { USB_DEVICE(VENDOR_FORMOSA
, 0xe017),
370 .driver_info
= MCE_GEN2_NO_TX
},
371 /* Formosa Industrial Computing / Beanbag Emulation Device */
372 { USB_DEVICE(VENDOR_FORMOSA
, 0xe018) },
373 /* Formosa21 / eHome Infrared Receiver */
374 { USB_DEVICE(VENDOR_FORMOSA
, 0xe03a) },
375 /* Formosa Industrial Computing AIM IR605/A */
376 { USB_DEVICE(VENDOR_FORMOSA
, 0xe03c) },
377 /* Formosa Industrial Computing */
378 { USB_DEVICE(VENDOR_FORMOSA
, 0xe03e) },
379 /* Formosa Industrial Computing */
380 { USB_DEVICE(VENDOR_FORMOSA
, 0xe042) },
381 /* Fintek eHome Infrared Transceiver (HP branded) */
382 { USB_DEVICE(VENDOR_FINTEK
, 0x5168),
383 .driver_info
= MCE_GEN2_TX_INV
},
384 /* Fintek eHome Infrared Transceiver */
385 { USB_DEVICE(VENDOR_FINTEK
, 0x0602) },
386 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
387 { USB_DEVICE(VENDOR_FINTEK
, 0x0702) },
388 /* Pinnacle Remote Kit */
389 { USB_DEVICE(VENDOR_PINNACLE
, 0x0225),
390 .driver_info
= MCE_GEN3
},
391 /* Elitegroup Computer Systems IR */
392 { USB_DEVICE(VENDOR_ECS
, 0x0f38) },
393 /* Wistron Corp. eHome Infrared Receiver */
394 { USB_DEVICE(VENDOR_WISTRON
, 0x0002) },
396 { USB_DEVICE(VENDOR_COMPRO
, 0x3020) },
398 { USB_DEVICE(VENDOR_COMPRO
, 0x3082) },
399 /* Northstar Systems, Inc. eHome Infrared Transceiver */
400 { USB_DEVICE(VENDOR_NORTHSTAR
, 0xe004) },
401 /* TiVo PC IR Receiver */
402 { USB_DEVICE(VENDOR_TIVO
, 0x2000),
403 .driver_info
= TIVO_KIT
},
404 /* Conexant Hybrid TV "Shelby" Polaris SDK */
405 { USB_DEVICE(VENDOR_CONEXANT
, 0x58a1),
406 .driver_info
= POLARIS_EVK
},
407 /* Conexant Hybrid TV RDU253S Polaris */
408 { USB_DEVICE(VENDOR_CONEXANT
, 0x58a5),
409 .driver_info
= CX_HYBRID_TV
},
410 /* Twisted Melon Inc. - Manta Mini Receiver */
411 { USB_DEVICE(VENDOR_TWISTEDMELON
, 0x8008) },
412 /* Twisted Melon Inc. - Manta Pico Receiver */
413 { USB_DEVICE(VENDOR_TWISTEDMELON
, 0x8016) },
414 /* Twisted Melon Inc. - Manta Transceiver */
415 { USB_DEVICE(VENDOR_TWISTEDMELON
, 0x8042) },
416 /* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
417 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb130),
418 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
419 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb131),
420 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
421 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb138),
422 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
423 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb139),
424 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
425 /* Hauppauge WinTV-HVR-935C - based on cx231xx */
426 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb151),
427 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
428 /* Hauppauge WinTV-HVR-955Q - based on cx231xx */
429 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb123),
430 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
431 /* Hauppauge WinTV-HVR-975 - based on cx231xx */
432 { USB_DEVICE(VENDOR_HAUPPAUGE
, 0xb150),
433 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
434 { USB_DEVICE(VENDOR_PCTV
, 0x0259),
435 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
436 { USB_DEVICE(VENDOR_PCTV
, 0x025e),
437 .driver_info
= HAUPPAUGE_CX_HYBRID_TV
},
438 /* Adaptec / HP eHome Receiver */
439 { USB_DEVICE(VENDOR_ADAPTEC
, 0x0094) },
440 /* Evromedia USB Full Hybrid Full HD */
441 { USB_DEVICE(0x1b80, 0xd3b2),
442 .driver_info
= EVROMEDIA_FULL_HYBRID_FULLHD
},
443 /* Astrometa T2hybrid */
444 { USB_DEVICE(0x15f4, 0x0135),
445 .driver_info
= ASTROMETA_T2HYBRID
},
447 /* Terminating entry */
451 /* data structure for each usb transceiver */
456 /* optional features we can enable */
457 bool carrier_report_enabled
;
458 bool wideband_rx_enabled
; /* aka learning mode, short-range rx */
460 /* core device bits */
464 struct usb_device
*usbdev
;
465 struct usb_interface
*usbintf
;
467 unsigned int pipe_in
;
468 struct usb_endpoint_descriptor
*usb_ep_out
;
469 unsigned int pipe_out
;
471 /* buffers and dma */
472 unsigned char *buf_in
;
483 u8 cmd
, rem
; /* Remaining IR data bytes in packet */
487 u32 tx_mask_normal
:1;
488 u32 microsoft_gen1
:1;
493 /* transmit support */
495 unsigned char tx_mask
;
498 enum mceusb_model_type model
;
500 bool need_reset
; /* flag to issue a device resume cmd */
501 u8 emver
; /* emulator interface version */
502 u8 num_txports
; /* number of transmit ports */
503 u8 num_rxports
; /* number of receive sensors */
504 u8 txports_cabled
; /* bitmask of transmitters with cable */
505 u8 rxports_active
; /* bitmask of active receive sensors */
506 bool learning_active
; /* wideband rx is active */
508 /* receiver carrier frequency detection support */
509 u32 pulse_tunit
; /* IR pulse "on" cumulative time units */
510 u32 pulse_count
; /* pulse "on" count in measurement interval */
513 * support for async error handler mceusb_deferred_kevent()
514 * where usb_clear_halt(), usb_reset_configuration(),
515 * usb_reset_device(), etc. must be done in process context
517 struct work_struct kevent
;
518 unsigned long kevent_flags
;
519 # define EVENT_TX_HALT 0
520 # define EVENT_RX_HALT 1
521 # define EVENT_RST_PEND 31
524 /* MCE Device Command Strings, generally a port and command pair */
525 static char DEVICE_RESUME
[] = {MCE_CMD_NULL
, MCE_CMD_PORT_SYS
,
527 static char GET_REVISION
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_G_REVISION
};
528 static char GET_EMVER
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_GETEMVER
};
529 static char GET_WAKEVERSION
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_GETWAKEVERSION
};
530 static char FLASH_LED
[] = {MCE_CMD_PORT_SYS
, MCE_CMD_FLASHLED
};
531 static char GET_UNKNOWN2
[] = {MCE_CMD_PORT_IR
, MCE_CMD_UNKNOWN2
};
532 static char GET_CARRIER_FREQ
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRCFS
};
533 static char GET_RX_TIMEOUT
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRTIMEOUT
};
534 static char GET_NUM_PORTS
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRNUMPORTS
};
535 static char GET_TX_BITMASK
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRTXPORTS
};
536 static char GET_RX_SENSOR
[] = {MCE_CMD_PORT_IR
, MCE_CMD_GETIRRXPORTEN
};
537 /* sub in desired values in lower byte or bytes for full command */
538 /* FIXME: make use of these for transmit.
539 static char SET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR,
540 MCE_CMD_SETIRCFS, 0x00, 0x00};
541 static char SET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
542 static char SET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR,
543 MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
544 static char SET_RX_SENSOR[] = {MCE_CMD_PORT_IR,
545 MCE_RSP_EQIRRXPORTEN, 0x00};
548 static int mceusb_cmd_datasize(u8 cmd
, u8 subcmd
)
554 if (subcmd
== MCE_CMD_PORT_SYS
)
557 case MCE_CMD_PORT_SYS
:
559 case MCE_RSP_GETPORTSTATUS
:
562 case MCE_RSP_EQWAKEVERSION
:
565 case MCE_CMD_G_REVISION
:
568 case MCE_RSP_EQWAKESUPPORT
:
569 case MCE_RSP_GETWAKESOURCE
:
570 case MCE_RSP_EQDEVDETAILS
:
571 case MCE_RSP_EQEMVER
:
576 case MCE_CMD_PORT_IR
:
578 case MCE_CMD_UNKNOWN
:
579 case MCE_RSP_EQIRCFS
:
580 case MCE_RSP_EQIRTIMEOUT
:
581 case MCE_RSP_EQIRRXCFCNT
:
582 case MCE_RSP_EQIRNUMPORTS
:
585 case MCE_CMD_SIG_END
:
586 case MCE_RSP_EQIRTXPORTS
:
587 case MCE_RSP_EQIRRXPORTEN
:
595 static void mceusb_dev_printdata(struct mceusb_dev
*ir
, u8
*buf
, int buf_len
,
596 int offset
, int len
, bool out
)
598 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
600 u8 cmd
, subcmd
, *data
;
601 struct device
*dev
= ir
->dev
;
604 if (offset
< 0 || offset
>= buf_len
)
607 dev_dbg(dev
, "%cx data[%d]: %*ph (len=%d sz=%d)",
608 (out
? 't' : 'r'), offset
,
609 min(len
, buf_len
- offset
), buf
+ offset
, len
, buf_len
);
611 inout
= out
? "Request" : "Got";
614 subcmd
= (offset
+ 1 < buf_len
) ? buf
[offset
+ 1] : 0;
615 data
= &buf
[offset
] + 2;
617 /* Trace meaningless 0xb1 0x60 header bytes on original receiver */
618 if (ir
->flags
.microsoft_gen1
&& !out
&& !offset
) {
619 dev_dbg(dev
, "MCE gen 1 header");
623 /* Trace IR data header or trailer */
624 if (cmd
!= MCE_CMD_PORT_IR
&&
625 (cmd
& MCE_PORT_MASK
) == MCE_COMMAND_IRDATA
) {
626 if (cmd
== MCE_IRDATA_TRAILER
)
627 dev_dbg(dev
, "End of raw IR data");
629 dev_dbg(dev
, "Raw IR data, %d pulse/space samples",
630 cmd
& MCE_PACKET_LENGTH_MASK
);
634 /* Unexpected end of buffer? */
635 if (offset
+ len
> buf_len
)
638 /* Decode MCE command/response */
641 if (subcmd
== MCE_CMD_NULL
)
643 if ((subcmd
== MCE_CMD_PORT_SYS
) &&
644 (data
[0] == MCE_CMD_RESUME
))
645 dev_dbg(dev
, "Device resume requested");
647 dev_dbg(dev
, "Unknown command 0x%02x 0x%02x",
650 case MCE_CMD_PORT_SYS
:
652 case MCE_RSP_EQEMVER
:
654 dev_dbg(dev
, "Emulator interface version %x",
657 case MCE_CMD_G_REVISION
:
659 dev_dbg(dev
, "Get hw/sw rev?");
661 dev_dbg(dev
, "hw/sw rev %*ph",
662 4, &buf
[offset
+ 2]);
665 dev_dbg(dev
, "Device resume requested");
667 case MCE_RSP_CMD_ILLEGAL
:
668 dev_dbg(dev
, "Illegal PORT_SYS command");
670 case MCE_RSP_EQWAKEVERSION
:
672 dev_dbg(dev
, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
673 data
[0], data
[1], data
[2], data
[3]);
675 case MCE_RSP_GETPORTSTATUS
:
677 /* We use data1 + 1 here, to match hw labels */
678 dev_dbg(dev
, "TX port %d: blaster is%s connected",
679 data
[0] + 1, data
[3] ? " not" : "");
681 case MCE_CMD_FLASHLED
:
682 dev_dbg(dev
, "Attempting to flash LED");
685 dev_dbg(dev
, "Unknown command 0x%02x 0x%02x",
690 case MCE_CMD_PORT_IR
:
692 case MCE_CMD_SIG_END
:
693 dev_dbg(dev
, "End of signal");
696 dev_dbg(dev
, "Ping");
698 case MCE_CMD_UNKNOWN
:
699 dev_dbg(dev
, "Resp to 9f 05 of 0x%02x 0x%02x",
702 case MCE_RSP_EQIRCFS
:
703 if (!data
[0] && !data
[1]) {
704 dev_dbg(dev
, "%s: no carrier", inout
);
707 // prescaler should make sense
710 period
= DIV_ROUND_CLOSEST((1U << data
[0] * 2) *
714 carrier
= USEC_PER_SEC
/ period
;
715 dev_dbg(dev
, "%s carrier of %u Hz (period %uus)",
716 inout
, carrier
, period
);
718 case MCE_CMD_GETIRCFS
:
719 dev_dbg(dev
, "Get carrier mode and freq");
721 case MCE_RSP_EQIRTXPORTS
:
722 dev_dbg(dev
, "%s transmit blaster mask of 0x%02x",
725 case MCE_RSP_EQIRTIMEOUT
:
726 /* value is in units of 50us, so x*50/1000 ms */
727 period
= ((data
[0] << 8) | data
[1]) *
728 MCE_TIME_UNIT
/ 1000;
729 dev_dbg(dev
, "%s receive timeout of %d ms",
732 case MCE_CMD_GETIRTIMEOUT
:
733 dev_dbg(dev
, "Get receive timeout");
735 case MCE_CMD_GETIRTXPORTS
:
736 dev_dbg(dev
, "Get transmit blaster mask");
738 case MCE_RSP_EQIRRXPORTEN
:
739 dev_dbg(dev
, "%s %s-range receive sensor in use",
740 inout
, data
[0] == 0x02 ? "short" : "long");
742 case MCE_CMD_GETIRRXPORTEN
:
743 /* aka MCE_RSP_EQIRRXCFCNT */
745 dev_dbg(dev
, "Get receive sensor");
747 dev_dbg(dev
, "RX carrier cycle count: %d",
748 ((data
[0] << 8) | data
[1]));
750 case MCE_RSP_EQIRNUMPORTS
:
753 dev_dbg(dev
, "Num TX ports: %x, num RX ports: %x",
756 case MCE_RSP_CMD_ILLEGAL
:
757 dev_dbg(dev
, "Illegal PORT_IR command");
759 case MCE_RSP_TX_TIMEOUT
:
760 dev_dbg(dev
, "IR TX timeout (TX buffer underrun)");
763 dev_dbg(dev
, "Unknown command 0x%02x 0x%02x",
775 * Schedule work that can't be done in interrupt handlers
776 * (mceusb_dev_recv() and mce_write_callback()) nor BH work.
777 * Invokes mceusb_deferred_kevent() for recovering from
778 * error events specified by the kevent bit field.
780 static void mceusb_defer_kevent(struct mceusb_dev
*ir
, int kevent
)
782 set_bit(kevent
, &ir
->kevent_flags
);
784 if (test_bit(EVENT_RST_PEND
, &ir
->kevent_flags
)) {
785 dev_dbg(ir
->dev
, "kevent %d dropped pending USB Reset Device",
790 if (!schedule_work(&ir
->kevent
))
791 dev_dbg(ir
->dev
, "kevent %d already scheduled", kevent
);
793 dev_dbg(ir
->dev
, "kevent %d scheduled", kevent
);
796 static void mce_write_callback(struct urb
*urb
)
801 complete(urb
->context
);
805 * Write (TX/send) data to MCE device USB endpoint out.
806 * Used for IR blaster TX and MCE device commands.
808 * Return: The number of bytes written (> 0) or errno (< 0).
810 static int mce_write(struct mceusb_dev
*ir
, u8
*data
, int size
)
814 struct device
*dev
= ir
->dev
;
815 unsigned char *buf_out
;
816 struct completion tx_done
;
817 unsigned long expire
;
818 unsigned long ret_wait
;
820 mceusb_dev_printdata(ir
, data
, size
, 0, size
, true);
822 urb
= usb_alloc_urb(0, GFP_KERNEL
);
823 if (unlikely(!urb
)) {
824 dev_err(dev
, "Error: mce write couldn't allocate urb");
828 buf_out
= kmalloc(size
, GFP_KERNEL
);
834 init_completion(&tx_done
);
837 if (usb_endpoint_xfer_int(ir
->usb_ep_out
))
838 usb_fill_int_urb(urb
, ir
->usbdev
, ir
->pipe_out
,
839 buf_out
, size
, mce_write_callback
, &tx_done
,
840 ir
->usb_ep_out
->bInterval
);
842 usb_fill_bulk_urb(urb
, ir
->usbdev
, ir
->pipe_out
,
843 buf_out
, size
, mce_write_callback
, &tx_done
);
844 memcpy(buf_out
, data
, size
);
846 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
848 dev_err(dev
, "Error: mce write submit urb error = %d", ret
);
854 expire
= msecs_to_jiffies(USB_TX_TIMEOUT
);
855 ret_wait
= wait_for_completion_timeout(&tx_done
, expire
);
857 dev_err(dev
, "Error: mce write timed out (expire = %lu (%dms))",
858 expire
, USB_TX_TIMEOUT
);
860 ret
= (urb
->status
== -ENOENT
? -ETIMEDOUT
: urb
->status
);
865 ret
= urb
->actual_length
; /* bytes written */
867 switch (urb
->status
) {
879 dev_err(ir
->dev
, "Error: mce write urb status = %d (TX HALT)",
881 mceusb_defer_kevent(ir
, EVENT_TX_HALT
);
885 dev_err(ir
->dev
, "Error: mce write urb status = %d",
890 dev_dbg(dev
, "tx done status = %d (wait = %lu, expire = %lu (%dms), urb->actual_length = %d, urb->status = %d)",
891 ret
, ret_wait
, expire
, USB_TX_TIMEOUT
,
892 urb
->actual_length
, urb
->status
);
900 static void mce_command_out(struct mceusb_dev
*ir
, u8
*data
, int size
)
902 int rsize
= sizeof(DEVICE_RESUME
);
904 if (ir
->need_reset
) {
905 ir
->need_reset
= false;
906 mce_write(ir
, DEVICE_RESUME
, rsize
);
910 mce_write(ir
, data
, size
);
915 * Transmit IR out the MCE device IR blaster port(s).
917 * Convert IR pulse/space sequence from LIRC to MCE format.
918 * Break up a long IR sequence into multiple parts (MCE IR data packets).
920 * u32 txbuf[] consists of IR pulse, space, ..., and pulse times in usec.
921 * Pulses and spaces are implicit by their position.
922 * The first IR sample, txbuf[0], is always a pulse.
924 * u8 irbuf[] consists of multiple IR data packets for the MCE device.
925 * A packet is 1 u8 MCE_IRDATA_HEADER and up to 30 u8 IR samples.
926 * An IR sample is 1-bit pulse/space flag with 7-bit time
927 * in MCE time units (50usec).
929 * Return: The number of IR samples sent (> 0) or errno (< 0).
931 static int mceusb_tx_ir(struct rc_dev
*dev
, unsigned *txbuf
, unsigned count
)
933 struct mceusb_dev
*ir
= dev
->priv
;
934 u8 cmdbuf
[3] = { MCE_CMD_PORT_IR
, MCE_CMD_SETIRTXPORTS
, 0x00 };
935 u8 irbuf
[MCE_IRBUF_SIZE
];
937 unsigned int irsample
;
940 /* Send the set TX ports command */
941 cmdbuf
[2] = ir
->tx_mask
;
942 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
944 /* Generate mce IR data packet */
945 for (i
= 0; i
< count
; i
++) {
946 irsample
= txbuf
[i
] / MCE_TIME_UNIT
;
948 /* loop to support long pulses/spaces > 6350us (127*50us) */
949 while (irsample
> 0) {
950 /* Insert IR header every 30th entry */
951 if (ircount
% MCE_PACKET_SIZE
== 0) {
952 /* Room for IR header and one IR sample? */
953 if (ircount
>= MCE_IRBUF_SIZE
- 1) {
954 /* Send near full buffer */
955 ret
= mce_write(ir
, irbuf
, ircount
);
960 irbuf
[ircount
++] = MCE_IRDATA_HEADER
;
963 /* Insert IR sample */
964 if (irsample
<= MCE_MAX_PULSE_LENGTH
) {
965 irbuf
[ircount
] = irsample
;
968 irbuf
[ircount
] = MCE_MAX_PULSE_LENGTH
;
969 irsample
-= MCE_MAX_PULSE_LENGTH
;
975 irbuf
[ircount
] |= (i
& 1 ? 0 : MCE_PULSE_BIT
);
978 /* IR buffer full? */
979 if (ircount
>= MCE_IRBUF_SIZE
) {
980 /* Fix packet length in last header */
981 length
= ircount
% MCE_PACKET_SIZE
;
983 irbuf
[ircount
- length
] -=
984 MCE_PACKET_SIZE
- length
;
985 /* Send full buffer */
986 ret
= mce_write(ir
, irbuf
, ircount
);
992 } /* after for loop, 0 <= ircount < MCE_IRBUF_SIZE */
994 /* Fix packet length in last header */
995 length
= ircount
% MCE_PACKET_SIZE
;
997 irbuf
[ircount
- length
] -= MCE_PACKET_SIZE
- length
;
999 /* Append IR trailer (0x80) to final partial (or empty) IR buffer */
1000 irbuf
[ircount
++] = MCE_IRDATA_TRAILER
;
1002 /* Send final buffer */
1003 ret
= mce_write(ir
, irbuf
, ircount
);
1010 /* Sets active IR outputs -- mce devices typically have two */
1011 static int mceusb_set_tx_mask(struct rc_dev
*dev
, u32 mask
)
1013 struct mceusb_dev
*ir
= dev
->priv
;
1015 /* return number of transmitters */
1016 int emitters
= ir
->num_txports
? ir
->num_txports
: 2;
1018 if (mask
>= (1 << emitters
))
1021 if (ir
->flags
.tx_mask_normal
)
1024 ir
->tx_mask
= (mask
!= MCE_DEFAULT_TX_MASK
?
1025 mask
^ MCE_DEFAULT_TX_MASK
: mask
) << 1;
1030 /* Sets the send carrier frequency and mode */
1031 static int mceusb_set_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
1033 struct mceusb_dev
*ir
= dev
->priv
;
1035 int prescaler
= 0, divisor
= 0;
1036 unsigned char cmdbuf
[4] = { MCE_CMD_PORT_IR
,
1037 MCE_CMD_SETIRCFS
, 0x00, 0x00 };
1039 /* Carrier has changed */
1040 if (ir
->carrier
!= carrier
) {
1043 ir
->carrier
= carrier
;
1044 cmdbuf
[2] = MCE_CMD_SIG_END
;
1045 cmdbuf
[3] = MCE_IRDATA_TRAILER
;
1046 dev_dbg(ir
->dev
, "disabling carrier modulation");
1047 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1051 for (prescaler
= 0; prescaler
< 4; ++prescaler
) {
1052 divisor
= (clk
>> (2 * prescaler
)) / carrier
;
1053 if (divisor
<= 0xff) {
1054 ir
->carrier
= carrier
;
1055 cmdbuf
[2] = prescaler
;
1056 cmdbuf
[3] = divisor
;
1057 dev_dbg(ir
->dev
, "requesting %u HZ carrier",
1060 /* Transmit new carrier to mce device */
1061 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1073 static int mceusb_set_timeout(struct rc_dev
*dev
, unsigned int timeout
)
1075 u8 cmdbuf
[4] = { MCE_CMD_PORT_IR
, MCE_CMD_SETIRTIMEOUT
, 0, 0 };
1076 struct mceusb_dev
*ir
= dev
->priv
;
1079 units
= DIV_ROUND_UP(timeout
, MCE_TIME_UNIT
);
1081 cmdbuf
[2] = units
>> 8;
1084 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1086 /* get receiver timeout value */
1087 mce_command_out(ir
, GET_RX_TIMEOUT
, sizeof(GET_RX_TIMEOUT
));
1093 * Select or deselect the 2nd receiver port.
1094 * Second receiver is learning mode, wide-band, short-range receiver.
1095 * Only one receiver (long or short range) may be active at a time.
1097 static int mceusb_set_rx_wideband(struct rc_dev
*dev
, int enable
)
1099 struct mceusb_dev
*ir
= dev
->priv
;
1100 unsigned char cmdbuf
[3] = { MCE_CMD_PORT_IR
,
1101 MCE_CMD_SETIRRXPORTEN
, 0x00 };
1103 dev_dbg(ir
->dev
, "select %s-range receive sensor",
1104 enable
? "short" : "long");
1106 ir
->wideband_rx_enabled
= true;
1107 cmdbuf
[2] = 2; /* port 2 is short range receiver */
1109 ir
->wideband_rx_enabled
= false;
1110 cmdbuf
[2] = 1; /* port 1 is long range receiver */
1112 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1113 /* response from device sets ir->learning_active */
1119 * Enable/disable receiver carrier frequency pass through reporting.
1120 * Only the short-range receiver has carrier frequency measuring capability.
1121 * Implicitly select this receiver when enabling carrier frequency reporting.
1123 static int mceusb_set_rx_carrier_report(struct rc_dev
*dev
, int enable
)
1125 struct mceusb_dev
*ir
= dev
->priv
;
1126 unsigned char cmdbuf
[3] = { MCE_CMD_PORT_IR
,
1127 MCE_CMD_SETIRRXPORTEN
, 0x00 };
1129 dev_dbg(ir
->dev
, "%s short-range receiver carrier reporting",
1130 enable
? "enable" : "disable");
1132 ir
->carrier_report_enabled
= true;
1133 if (!ir
->learning_active
) {
1134 cmdbuf
[2] = 2; /* port 2 is short range receiver */
1135 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1138 ir
->carrier_report_enabled
= false;
1140 * Revert to normal (long-range) receiver only if the
1141 * wideband (short-range) receiver wasn't explicitly
1144 if (ir
->learning_active
&& !ir
->wideband_rx_enabled
) {
1145 cmdbuf
[2] = 1; /* port 1 is long range receiver */
1146 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1154 * Handle PORT_SYS/IR command response received from the MCE device.
1156 * Assumes single response with all its data (not truncated)
1157 * in buf_in[]. The response itself determines its total length
1158 * (mceusb_cmd_datasize() + 2) and hence the minimum size of buf_in[].
1160 * We don't do anything but print debug spew for many of the command bits
1161 * we receive from the hardware, but some of them are useful information
1162 * we want to store so that we can use them.
1164 static void mceusb_handle_command(struct mceusb_dev
*ir
, u8
*buf_in
)
1167 u8 subcmd
= buf_in
[1];
1168 u8
*hi
= &buf_in
[2]; /* read only when required */
1169 u8
*lo
= &buf_in
[3]; /* read only when required */
1170 struct ir_raw_event rawir
= {};
1174 if (cmd
== MCE_CMD_PORT_SYS
) {
1176 /* the one and only 5-byte return value command */
1177 case MCE_RSP_GETPORTSTATUS
:
1178 if (buf_in
[5] == 0 && *hi
< 8)
1179 ir
->txports_cabled
|= 1 << *hi
;
1182 /* 1-byte return value commands */
1183 case MCE_RSP_EQEMVER
:
1187 /* No return value commands */
1188 case MCE_RSP_CMD_ILLEGAL
:
1189 ir
->need_reset
= true;
1199 if (cmd
!= MCE_CMD_PORT_IR
)
1203 /* 2-byte return value commands */
1204 case MCE_RSP_EQIRTIMEOUT
:
1205 ir
->rc
->timeout
= (*hi
<< 8 | *lo
) * MCE_TIME_UNIT
;
1207 case MCE_RSP_EQIRNUMPORTS
:
1208 ir
->num_txports
= *hi
;
1209 ir
->num_rxports
= *lo
;
1211 case MCE_RSP_EQIRRXCFCNT
:
1213 * The carrier cycle counter can overflow and wrap around
1214 * without notice from the device. So frequency measurement
1215 * will be inaccurate with long duration IR.
1217 * The long-range (non learning) receiver always reports
1218 * zero count so we always ignore its report.
1220 if (ir
->carrier_report_enabled
&& ir
->learning_active
&&
1221 ir
->pulse_tunit
> 0) {
1222 carrier_cycles
= (*hi
<< 8 | *lo
);
1224 * Adjust carrier cycle count by adding
1225 * 1 missed count per pulse "on"
1227 cycles_fix
= ir
->flags
.rx2
== 2 ? ir
->pulse_count
: 0;
1228 rawir
.carrier_report
= 1;
1229 rawir
.carrier
= (1000000u / MCE_TIME_UNIT
) *
1230 (carrier_cycles
+ cycles_fix
) /
1232 dev_dbg(ir
->dev
, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)",
1233 rawir
.carrier
, ir
->pulse_count
, carrier_cycles
,
1234 ir
->pulse_tunit
, ir
->flags
.rx2
);
1235 ir_raw_event_store(ir
->rc
, &rawir
);
1239 /* 1-byte return value commands */
1240 case MCE_RSP_EQIRTXPORTS
:
1243 case MCE_RSP_EQIRRXPORTEN
:
1244 ir
->learning_active
= ((*hi
& 0x02) == 0x02);
1245 if (ir
->rxports_active
!= *hi
) {
1246 dev_info(ir
->dev
, "%s-range (0x%x) receiver active",
1247 ir
->learning_active
? "short" : "long", *hi
);
1248 ir
->rxports_active
= *hi
;
1252 /* No return value commands */
1253 case MCE_RSP_CMD_ILLEGAL
:
1254 case MCE_RSP_TX_TIMEOUT
:
1255 ir
->need_reset
= true;
1263 static void mceusb_process_ir_data(struct mceusb_dev
*ir
, int buf_len
)
1265 struct ir_raw_event rawir
= {};
1269 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
1270 if (ir
->flags
.microsoft_gen1
)
1273 /* if there's no data, just return now */
1277 for (; i
< buf_len
; i
++) {
1278 switch (ir
->parser_state
) {
1280 ir
->rem
= mceusb_cmd_datasize(ir
->cmd
, ir
->buf_in
[i
]);
1281 mceusb_dev_printdata(ir
, ir
->buf_in
, buf_len
, i
- 1,
1282 ir
->rem
+ 2, false);
1283 if (i
+ ir
->rem
< buf_len
)
1284 mceusb_handle_command(ir
, &ir
->buf_in
[i
- 1]);
1285 ir
->parser_state
= CMD_DATA
;
1289 rawir
.pulse
= ((ir
->buf_in
[i
] & MCE_PULSE_BIT
) != 0);
1290 rawir
.duration
= (ir
->buf_in
[i
] & MCE_PULSE_MASK
);
1291 if (unlikely(!rawir
.duration
)) {
1292 dev_dbg(ir
->dev
, "nonsensical irdata %02x with duration 0",
1297 ir
->pulse_tunit
+= rawir
.duration
;
1300 rawir
.duration
*= MCE_TIME_UNIT
;
1302 dev_dbg(ir
->dev
, "Storing %s %u us (%02x)",
1303 rawir
.pulse
? "pulse" : "space",
1304 rawir
.duration
, ir
->buf_in
[i
]);
1306 if (ir_raw_event_store_with_filter(ir
->rc
, &rawir
))
1313 ir
->cmd
= ir
->buf_in
[i
];
1314 if ((ir
->cmd
== MCE_CMD_PORT_IR
) ||
1315 ((ir
->cmd
& MCE_PORT_MASK
) !=
1316 MCE_COMMAND_IRDATA
)) {
1318 * got PORT_SYS, PORT_IR, or unknown
1319 * command response prefix
1321 ir
->parser_state
= SUBCMD
;
1325 * got IR data prefix (0x80 + num_bytes)
1326 * decode MCE packets of the form {0x83, AA, BB, CC}
1327 * IR data packets can span USB messages
1329 ir
->rem
= (ir
->cmd
& MCE_PACKET_LENGTH_MASK
);
1330 mceusb_dev_printdata(ir
, ir
->buf_in
, buf_len
,
1331 i
, ir
->rem
+ 1, false);
1333 ir
->parser_state
= PARSE_IRDATA
;
1335 struct ir_raw_event ev
= {
1337 .duration
= ir
->rc
->timeout
1340 if (ir_raw_event_store_with_filter(ir
->rc
,
1343 ir
->pulse_tunit
= 0;
1344 ir
->pulse_count
= 0;
1349 if (ir
->parser_state
!= CMD_HEADER
&& !ir
->rem
)
1350 ir
->parser_state
= CMD_HEADER
;
1354 * Accept IR data spanning multiple rx buffers.
1355 * Reject MCE command response spanning multiple rx buffers.
1357 if (ir
->parser_state
!= PARSE_IRDATA
|| !ir
->rem
)
1358 ir
->parser_state
= CMD_HEADER
;
1361 dev_dbg(ir
->dev
, "processed IR data");
1362 ir_raw_event_handle(ir
->rc
);
1366 static void mceusb_dev_recv(struct urb
*urb
)
1368 struct mceusb_dev
*ir
;
1375 usb_unlink_urb(urb
);
1379 switch (urb
->status
) {
1382 mceusb_process_ir_data(ir
, urb
->actual_length
);
1390 usb_unlink_urb(urb
);
1394 dev_err(ir
->dev
, "Error: urb status = %d (RX HALT)",
1396 mceusb_defer_kevent(ir
, EVENT_RX_HALT
);
1400 dev_err(ir
->dev
, "Error: urb status = %d", urb
->status
);
1404 usb_submit_urb(urb
, GFP_ATOMIC
);
1407 static void mceusb_get_emulator_version(struct mceusb_dev
*ir
)
1409 /* If we get no reply or an illegal command reply, its ver 1, says MS */
1411 mce_command_out(ir
, GET_EMVER
, sizeof(GET_EMVER
));
1414 static void mceusb_gen1_init(struct mceusb_dev
*ir
)
1417 struct device
*dev
= ir
->dev
;
1418 char data
[USB_CTRL_MSG_SZ
];
1421 * This is a strange one. Windows issues a set address to the device
1422 * on the receive control pipe and expect a certain value pair back
1424 ret
= usb_control_msg_recv(ir
->usbdev
, 0, USB_REQ_SET_ADDRESS
,
1425 USB_DIR_IN
| USB_TYPE_VENDOR
,
1426 0, 0, data
, USB_CTRL_MSG_SZ
, 3000,
1428 dev_dbg(dev
, "set address - ret = %d", ret
);
1429 dev_dbg(dev
, "set address - data[0] = %d, data[1] = %d",
1432 /* set feature: bit rate 38400 bps */
1433 ret
= usb_control_msg_send(ir
->usbdev
, 0,
1434 USB_REQ_SET_FEATURE
, USB_TYPE_VENDOR
,
1435 0xc04e, 0x0000, NULL
, 0, 3000, GFP_KERNEL
);
1437 dev_dbg(dev
, "set feature - ret = %d", ret
);
1439 /* bRequest 4: set char length to 8 bits */
1440 ret
= usb_control_msg_send(ir
->usbdev
, 0,
1442 0x0808, 0x0000, NULL
, 0, 3000, GFP_KERNEL
);
1443 dev_dbg(dev
, "set char length - retB = %d", ret
);
1445 /* bRequest 2: set handshaking to use DTR/DSR */
1446 ret
= usb_control_msg_send(ir
->usbdev
, 0,
1448 0x0000, 0x0100, NULL
, 0, 3000, GFP_KERNEL
);
1449 dev_dbg(dev
, "set handshake - retC = %d", ret
);
1452 mce_command_out(ir
, DEVICE_RESUME
, sizeof(DEVICE_RESUME
));
1454 /* get hw/sw revision? */
1455 mce_command_out(ir
, GET_REVISION
, sizeof(GET_REVISION
));
1458 static void mceusb_gen2_init(struct mceusb_dev
*ir
)
1461 mce_command_out(ir
, DEVICE_RESUME
, sizeof(DEVICE_RESUME
));
1463 /* get wake version (protocol, key, address) */
1464 mce_command_out(ir
, GET_WAKEVERSION
, sizeof(GET_WAKEVERSION
));
1466 /* unknown what this one actually returns... */
1467 mce_command_out(ir
, GET_UNKNOWN2
, sizeof(GET_UNKNOWN2
));
1470 static void mceusb_get_parameters(struct mceusb_dev
*ir
)
1473 unsigned char cmdbuf
[3] = { MCE_CMD_PORT_SYS
,
1474 MCE_CMD_GETPORTSTATUS
, 0x00 };
1476 /* defaults, if the hardware doesn't support querying */
1477 ir
->num_txports
= 2;
1478 ir
->num_rxports
= 2;
1480 /* get number of tx and rx ports */
1481 mce_command_out(ir
, GET_NUM_PORTS
, sizeof(GET_NUM_PORTS
));
1483 /* get the carrier and frequency */
1484 mce_command_out(ir
, GET_CARRIER_FREQ
, sizeof(GET_CARRIER_FREQ
));
1486 if (ir
->num_txports
&& !ir
->flags
.no_tx
)
1487 /* get the transmitter bitmask */
1488 mce_command_out(ir
, GET_TX_BITMASK
, sizeof(GET_TX_BITMASK
));
1490 /* get receiver timeout value */
1491 mce_command_out(ir
, GET_RX_TIMEOUT
, sizeof(GET_RX_TIMEOUT
));
1493 /* get receiver sensor setting */
1494 mce_command_out(ir
, GET_RX_SENSOR
, sizeof(GET_RX_SENSOR
));
1496 for (i
= 0; i
< ir
->num_txports
; i
++) {
1498 mce_command_out(ir
, cmdbuf
, sizeof(cmdbuf
));
1502 static void mceusb_flash_led(struct mceusb_dev
*ir
)
1507 mce_command_out(ir
, FLASH_LED
, sizeof(FLASH_LED
));
1511 * Workqueue function
1512 * for resetting or recovering device after occurrence of error events
1513 * specified in ir->kevent bit field.
1514 * Function runs (via schedule_work()) in non-interrupt context, for
1515 * calls here (such as usb_clear_halt()) requiring non-interrupt context.
1517 static void mceusb_deferred_kevent(struct work_struct
*work
)
1519 struct mceusb_dev
*ir
=
1520 container_of(work
, struct mceusb_dev
, kevent
);
1523 dev_err(ir
->dev
, "kevent handler called (flags 0x%lx)",
1526 if (test_bit(EVENT_RST_PEND
, &ir
->kevent_flags
)) {
1527 dev_err(ir
->dev
, "kevent handler canceled pending USB Reset Device");
1531 if (test_bit(EVENT_RX_HALT
, &ir
->kevent_flags
)) {
1532 usb_unlink_urb(ir
->urb_in
);
1533 status
= usb_clear_halt(ir
->usbdev
, ir
->pipe_in
);
1534 dev_err(ir
->dev
, "rx clear halt status = %d", status
);
1537 * Unable to clear RX halt/stall.
1538 * Will need to call usb_reset_device().
1541 "stuck RX HALT state requires USB Reset Device to clear");
1542 usb_queue_reset_device(ir
->usbintf
);
1543 set_bit(EVENT_RST_PEND
, &ir
->kevent_flags
);
1544 clear_bit(EVENT_RX_HALT
, &ir
->kevent_flags
);
1546 /* Cancel all other error events and handlers */
1547 clear_bit(EVENT_TX_HALT
, &ir
->kevent_flags
);
1550 clear_bit(EVENT_RX_HALT
, &ir
->kevent_flags
);
1551 status
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
1553 dev_err(ir
->dev
, "rx unhalt submit urb error = %d",
1558 if (test_bit(EVENT_TX_HALT
, &ir
->kevent_flags
)) {
1559 status
= usb_clear_halt(ir
->usbdev
, ir
->pipe_out
);
1560 dev_err(ir
->dev
, "tx clear halt status = %d", status
);
1563 * Unable to clear TX halt/stall.
1564 * Will need to call usb_reset_device().
1567 "stuck TX HALT state requires USB Reset Device to clear");
1568 usb_queue_reset_device(ir
->usbintf
);
1569 set_bit(EVENT_RST_PEND
, &ir
->kevent_flags
);
1570 clear_bit(EVENT_TX_HALT
, &ir
->kevent_flags
);
1572 /* Cancel all other error events and handlers */
1573 clear_bit(EVENT_RX_HALT
, &ir
->kevent_flags
);
1576 clear_bit(EVENT_TX_HALT
, &ir
->kevent_flags
);
1580 static struct rc_dev
*mceusb_init_rc_dev(struct mceusb_dev
*ir
)
1582 struct usb_device
*udev
= ir
->usbdev
;
1583 struct device
*dev
= ir
->dev
;
1587 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
1589 dev_err(dev
, "remote dev allocation failed");
1593 usb_make_path(ir
->usbdev
, ir
->phys
, sizeof(ir
->phys
));
1595 rc
->device_name
= mceusb_model
[ir
->model
].name
? :
1596 "Media Center Ed. eHome Infrared Remote Transceiver";
1597 rc
->input_phys
= ir
->phys
;
1598 usb_to_input_id(ir
->usbdev
, &rc
->input_id
);
1599 rc
->dev
.parent
= dev
;
1601 rc
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
1602 rc
->rx_resolution
= MCE_TIME_UNIT
;
1603 rc
->min_timeout
= MCE_TIME_UNIT
;
1604 rc
->timeout
= MS_TO_US(100);
1605 if (!mceusb_model
[ir
->model
].broken_irtimeout
) {
1606 rc
->s_timeout
= mceusb_set_timeout
;
1607 rc
->max_timeout
= 10 * IR_DEFAULT_TIMEOUT
;
1610 * If we can't set the timeout using CMD_SETIRTIMEOUT, we can
1611 * rely on software timeouts for timeouts < 100ms.
1613 rc
->max_timeout
= rc
->timeout
;
1615 if (!ir
->flags
.no_tx
) {
1616 rc
->s_tx_mask
= mceusb_set_tx_mask
;
1617 rc
->s_tx_carrier
= mceusb_set_tx_carrier
;
1618 rc
->tx_ir
= mceusb_tx_ir
;
1620 if (ir
->flags
.rx2
> 0) {
1621 rc
->s_wideband_receiver
= mceusb_set_rx_wideband
;
1622 rc
->s_carrier_report
= mceusb_set_rx_carrier_report
;
1624 rc
->driver_name
= DRIVER_NAME
;
1626 switch (le16_to_cpu(udev
->descriptor
.idVendor
)) {
1627 case VENDOR_HAUPPAUGE
:
1628 rc
->map_name
= RC_MAP_HAUPPAUGE
;
1631 rc
->map_name
= RC_MAP_PINNACLE_PCTV_HD
;
1634 rc
->map_name
= RC_MAP_RC6_MCE
;
1636 if (mceusb_model
[ir
->model
].rc_map
)
1637 rc
->map_name
= mceusb_model
[ir
->model
].rc_map
;
1639 ret
= rc_register_device(rc
);
1641 dev_err(dev
, "remote dev registration failed");
1652 static int mceusb_dev_probe(struct usb_interface
*intf
,
1653 const struct usb_device_id
*id
)
1655 struct usb_device
*dev
= interface_to_usbdev(intf
);
1656 struct usb_host_interface
*idesc
;
1657 struct usb_endpoint_descriptor
*ep
= NULL
;
1658 struct usb_endpoint_descriptor
*ep_in
= NULL
;
1659 struct usb_endpoint_descriptor
*ep_out
= NULL
;
1660 struct mceusb_dev
*ir
= NULL
;
1661 int pipe
, maxp
, i
, res
;
1662 char buf
[63], name
[128] = "";
1663 enum mceusb_model_type model
= id
->driver_info
;
1665 bool is_microsoft_gen1
;
1666 bool tx_mask_normal
;
1669 dev_dbg(&intf
->dev
, "%s called", __func__
);
1671 idesc
= intf
->cur_altsetting
;
1673 is_gen3
= mceusb_model
[model
].mce_gen3
;
1674 is_microsoft_gen1
= mceusb_model
[model
].mce_gen1
;
1675 tx_mask_normal
= mceusb_model
[model
].tx_mask_normal
;
1676 ir_intfnum
= mceusb_model
[model
].ir_intfnum
;
1678 /* There are multi-function devices with non-IR interfaces */
1679 if (idesc
->desc
.bInterfaceNumber
!= ir_intfnum
)
1682 /* step through the endpoints to find first bulk in and out endpoint */
1683 for (i
= 0; i
< idesc
->desc
.bNumEndpoints
; ++i
) {
1684 ep
= &idesc
->endpoint
[i
].desc
;
1686 if (ep_in
== NULL
) {
1687 if (usb_endpoint_is_bulk_in(ep
)) {
1689 dev_dbg(&intf
->dev
, "acceptable bulk inbound endpoint found\n");
1690 } else if (usb_endpoint_is_int_in(ep
)) {
1692 ep_in
->bInterval
= 1;
1693 dev_dbg(&intf
->dev
, "acceptable interrupt inbound endpoint found\n");
1697 if (ep_out
== NULL
) {
1698 if (usb_endpoint_is_bulk_out(ep
)) {
1700 dev_dbg(&intf
->dev
, "acceptable bulk outbound endpoint found\n");
1701 } else if (usb_endpoint_is_int_out(ep
)) {
1703 ep_out
->bInterval
= 1;
1704 dev_dbg(&intf
->dev
, "acceptable interrupt outbound endpoint found\n");
1708 if (!ep_in
|| !ep_out
) {
1709 dev_dbg(&intf
->dev
, "required endpoints not found\n");
1713 if (usb_endpoint_xfer_int(ep_in
))
1714 pipe
= usb_rcvintpipe(dev
, ep_in
->bEndpointAddress
);
1716 pipe
= usb_rcvbulkpipe(dev
, ep_in
->bEndpointAddress
);
1717 maxp
= usb_maxpacket(dev
, pipe
);
1719 ir
= kzalloc(sizeof(struct mceusb_dev
), GFP_KERNEL
);
1721 goto mem_alloc_fail
;
1724 ir
->buf_in
= usb_alloc_coherent(dev
, maxp
, GFP_KERNEL
, &ir
->dma_in
);
1726 goto buf_in_alloc_fail
;
1728 ir
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
1730 goto urb_in_alloc_fail
;
1733 ir
->usbdev
= usb_get_dev(dev
);
1734 ir
->dev
= &intf
->dev
;
1736 ir
->flags
.microsoft_gen1
= is_microsoft_gen1
;
1737 ir
->flags
.tx_mask_normal
= tx_mask_normal
;
1738 ir
->flags
.no_tx
= mceusb_model
[model
].no_tx
;
1739 ir
->flags
.rx2
= mceusb_model
[model
].rx2
;
1742 /* Saving usb interface data for use by the transmitter routine */
1743 ir
->usb_ep_out
= ep_out
;
1744 if (usb_endpoint_xfer_int(ep_out
))
1745 ir
->pipe_out
= usb_sndintpipe(ir
->usbdev
,
1746 ep_out
->bEndpointAddress
);
1748 ir
->pipe_out
= usb_sndbulkpipe(ir
->usbdev
,
1749 ep_out
->bEndpointAddress
);
1751 if (dev
->descriptor
.iManufacturer
1752 && usb_string(dev
, dev
->descriptor
.iManufacturer
,
1753 buf
, sizeof(buf
)) > 0)
1754 strscpy(name
, buf
, sizeof(name
));
1755 if (dev
->descriptor
.iProduct
1756 && usb_string(dev
, dev
->descriptor
.iProduct
,
1757 buf
, sizeof(buf
)) > 0)
1758 snprintf(name
+ strlen(name
), sizeof(name
) - strlen(name
),
1762 * Initialize async USB error handler before registering
1763 * or activating any mceusb RX and TX functions
1765 INIT_WORK(&ir
->kevent
, mceusb_deferred_kevent
);
1767 ir
->rc
= mceusb_init_rc_dev(ir
);
1771 /* wire up inbound data handler */
1772 if (usb_endpoint_xfer_int(ep_in
))
1773 usb_fill_int_urb(ir
->urb_in
, dev
, pipe
, ir
->buf_in
, maxp
,
1774 mceusb_dev_recv
, ir
, ep_in
->bInterval
);
1776 usb_fill_bulk_urb(ir
->urb_in
, dev
, pipe
, ir
->buf_in
, maxp
,
1777 mceusb_dev_recv
, ir
);
1779 ir
->urb_in
->transfer_dma
= ir
->dma_in
;
1780 ir
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1782 /* flush buffers on the device */
1783 dev_dbg(&intf
->dev
, "Flushing receive buffers");
1784 res
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
1786 dev_err(&intf
->dev
, "failed to flush buffers: %d", res
);
1788 /* figure out which firmware/emulator version this hardware has */
1789 mceusb_get_emulator_version(ir
);
1791 /* initialize device */
1792 if (ir
->flags
.microsoft_gen1
)
1793 mceusb_gen1_init(ir
);
1795 mceusb_gen2_init(ir
);
1797 mceusb_get_parameters(ir
);
1799 mceusb_flash_led(ir
);
1801 if (!ir
->flags
.no_tx
)
1802 mceusb_set_tx_mask(ir
->rc
, MCE_DEFAULT_TX_MASK
);
1804 usb_set_intfdata(intf
, ir
);
1806 /* enable wake via this device */
1807 device_set_wakeup_capable(ir
->dev
, true);
1808 device_set_wakeup_enable(ir
->dev
, true);
1810 dev_info(&intf
->dev
, "Registered %s with mce emulator interface version %x",
1812 dev_info(&intf
->dev
, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
1813 ir
->num_txports
, ir
->txports_cabled
,
1814 ir
->num_rxports
, ir
->rxports_active
);
1818 /* Error-handling path */
1820 cancel_work_sync(&ir
->kevent
);
1821 usb_put_dev(ir
->usbdev
);
1822 usb_kill_urb(ir
->urb_in
);
1823 usb_free_urb(ir
->urb_in
);
1825 usb_free_coherent(dev
, maxp
, ir
->buf_in
, ir
->dma_in
);
1829 dev_err(&intf
->dev
, "%s: device setup failed!", __func__
);
1835 static void mceusb_dev_disconnect(struct usb_interface
*intf
)
1837 struct usb_device
*dev
= interface_to_usbdev(intf
);
1838 struct mceusb_dev
*ir
= usb_get_intfdata(intf
);
1840 dev_dbg(&intf
->dev
, "%s called", __func__
);
1842 usb_set_intfdata(intf
, NULL
);
1848 cancel_work_sync(&ir
->kevent
);
1849 rc_unregister_device(ir
->rc
);
1850 usb_kill_urb(ir
->urb_in
);
1851 usb_free_urb(ir
->urb_in
);
1852 usb_free_coherent(dev
, ir
->len_in
, ir
->buf_in
, ir
->dma_in
);
1858 static int mceusb_dev_suspend(struct usb_interface
*intf
, pm_message_t message
)
1860 struct mceusb_dev
*ir
= usb_get_intfdata(intf
);
1861 dev_info(ir
->dev
, "suspend");
1862 usb_kill_urb(ir
->urb_in
);
1866 static int mceusb_dev_resume(struct usb_interface
*intf
)
1868 struct mceusb_dev
*ir
= usb_get_intfdata(intf
);
1869 dev_info(ir
->dev
, "resume");
1870 if (usb_submit_urb(ir
->urb_in
, GFP_ATOMIC
))
1875 static struct usb_driver mceusb_dev_driver
= {
1876 .name
= DRIVER_NAME
,
1877 .probe
= mceusb_dev_probe
,
1878 .disconnect
= mceusb_dev_disconnect
,
1879 .suspend
= mceusb_dev_suspend
,
1880 .resume
= mceusb_dev_resume
,
1881 .reset_resume
= mceusb_dev_resume
,
1882 .id_table
= mceusb_dev_table
1885 module_usb_driver(mceusb_dev_driver
);
1887 MODULE_DESCRIPTION(DRIVER_DESC
);
1888 MODULE_AUTHOR(DRIVER_AUTHOR
);
1889 MODULE_LICENSE("GPL");
1890 MODULE_DEVICE_TABLE(usb
, mceusb_dev_table
);