1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers 4 * 5 * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com> 6 * 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. 18 * 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 23 */ 24 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> 33 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 " \ 37 "device driver" 38 #define DRIVER_NAME "mceusb" 39 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 */ 43 44 /* MCE constants */ 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 */ 56 57 /* 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 62 * bits. 63 * 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. 68 */ 69 #define MCE_CMD 0x1f 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 */ 74 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 */ 78 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 */ 87 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) */ 100 101 /* Misc commands */ 102 #define MCE_CMD_NOP 0xff /* No operation */ 103 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 */ 118 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 */ 122 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... */ 136 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 */ 141 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 169 170 enum mceusb_model_type { 171 MCE_GEN2 = 0, /* Most boards */ 172 MCE_GEN1, 173 MCE_GEN3, 174 MCE_GEN3_BROKEN_IRTIMEOUT, 175 MCE_GEN2_TX_INV, 176 MCE_GEN2_TX_INV_RX_GOOD, 177 POLARIS_EVK, 178 CX_HYBRID_TV, 179 MULTIFUNCTION, 180 TIVO_KIT, 181 MCE_GEN2_NO_TX, 182 HAUPPAUGE_CX_HYBRID_TV, 183 EVROMEDIA_FULL_HYBRID_FULLHD, 184 ASTROMETA_T2HYBRID, 185 }; 186 187 struct mceusb_model { 188 u32 mce_gen1:1; 189 u32 mce_gen2:1; 190 u32 mce_gen3:1; 191 u32 tx_mask_normal:1; 192 u32 no_tx:1; 193 u32 broken_irtimeout:1; 194 /* 195 * 2nd IR receiver (short-range, wideband) for learning mode: 196 * 0, absent 2nd receiver (rx2) 197 * 1, rx2 present 198 * 2, rx2 which under counts IR carrier cycles 199 */ 200 u32 rx2; 201 202 int ir_intfnum; 203 204 const char *rc_map; /* Allow specify a per-board map */ 205 const char *name; /* per-board name */ 206 }; 207 208 static const struct mceusb_model mceusb_model[] = { 209 [MCE_GEN1] = { 210 .mce_gen1 = 1, 211 .tx_mask_normal = 1, 212 .rx2 = 2, 213 }, 214 [MCE_GEN2] = { 215 .mce_gen2 = 1, 216 .rx2 = 2, 217 }, 218 [MCE_GEN2_NO_TX] = { 219 .mce_gen2 = 1, 220 .no_tx = 1, 221 }, 222 [MCE_GEN2_TX_INV] = { 223 .mce_gen2 = 1, 224 .tx_mask_normal = 1, 225 .rx2 = 1, 226 }, 227 [MCE_GEN2_TX_INV_RX_GOOD] = { 228 .mce_gen2 = 1, 229 .tx_mask_normal = 1, 230 .rx2 = 2, 231 }, 232 [MCE_GEN3] = { 233 .mce_gen3 = 1, 234 .tx_mask_normal = 1, 235 .rx2 = 2, 236 }, 237 [MCE_GEN3_BROKEN_IRTIMEOUT] = { 238 .mce_gen3 = 1, 239 .tx_mask_normal = 1, 240 .rx2 = 2, 241 .broken_irtimeout = 1 242 }, 243 [POLARIS_EVK] = { 244 /* 245 * In fact, the EVK is shipped without 246 * remotes, but we should have something handy, 247 * to allow testing it 248 */ 249 .name = "Conexant Hybrid TV (cx231xx) MCE IR", 250 .rx2 = 2, 251 }, 252 [CX_HYBRID_TV] = { 253 .no_tx = 1, /* tx isn't wired up at all */ 254 .name = "Conexant Hybrid TV (cx231xx) MCE IR", 255 }, 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", 259 }, 260 [MULTIFUNCTION] = { 261 .mce_gen2 = 1, 262 .ir_intfnum = 2, 263 .rx2 = 2, 264 }, 265 [TIVO_KIT] = { 266 .mce_gen2 = 1, 267 .rc_map = RC_MAP_TIVO, 268 .rx2 = 2, 269 }, 270 [EVROMEDIA_FULL_HYBRID_FULLHD] = { 271 .name = "Evromedia USB Full Hybrid Full HD", 272 .no_tx = 1, 273 .rc_map = RC_MAP_MSI_DIGIVOX_III, 274 }, 275 [ASTROMETA_T2HYBRID] = { 276 .name = "Astrometa T2Hybrid", 277 .no_tx = 1, 278 .rc_map = RC_MAP_ASTROMETA_T2HYBRID, 279 } 280 }; 281 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) }, 334 /* Mitsumi */ 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) }, 395 /* Compro K100 */ 396 { USB_DEVICE(VENDOR_COMPRO, 0x3020) }, 397 /* Compro K100 v2 */ 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 }, 446 447 /* Terminating entry */ 448 { } 449 }; 450 451 /* data structure for each usb transceiver */ 452 struct mceusb_dev { 453 /* ir-core bits */ 454 struct rc_dev *rc; 455 456 /* optional features we can enable */ 457 bool carrier_report_enabled; 458 bool wideband_rx_enabled; /* aka learning mode, short-range rx */ 459 460 /* core device bits */ 461 struct device *dev; 462 463 /* usb */ 464 struct usb_device *usbdev; 465 struct usb_interface *usbintf; 466 struct urb *urb_in; 467 unsigned int pipe_in; 468 struct usb_endpoint_descriptor *usb_ep_out; 469 unsigned int pipe_out; 470 471 /* buffers and dma */ 472 unsigned char *buf_in; 473 unsigned int len_in; 474 dma_addr_t dma_in; 475 476 enum { 477 CMD_HEADER = 0, 478 SUBCMD, 479 CMD_DATA, 480 PARSE_IRDATA, 481 } parser_state; 482 483 u8 cmd, rem; /* Remaining IR data bytes in packet */ 484 485 struct { 486 u32 connected:1; 487 u32 tx_mask_normal:1; 488 u32 microsoft_gen1:1; 489 u32 no_tx:1; 490 u32 rx2; 491 } flags; 492 493 /* transmit support */ 494 u32 carrier; 495 unsigned char tx_mask; 496 497 char name[128]; 498 char phys[64]; 499 enum mceusb_model_type model; 500 501 bool need_reset; /* flag to issue a device resume cmd */ 502 u8 emver; /* emulator interface version */ 503 u8 num_txports; /* number of transmit ports */ 504 u8 num_rxports; /* number of receive sensors */ 505 u8 txports_cabled; /* bitmask of transmitters with cable */ 506 u8 rxports_active; /* bitmask of active receive sensors */ 507 bool learning_active; /* wideband rx is active */ 508 509 /* receiver carrier frequency detection support */ 510 u32 pulse_tunit; /* IR pulse "on" cumulative time units */ 511 u32 pulse_count; /* pulse "on" count in measurement interval */ 512 513 /* 514 * support for async error handler mceusb_deferred_kevent() 515 * where usb_clear_halt(), usb_reset_configuration(), 516 * usb_reset_device(), etc. must be done in process context 517 */ 518 struct work_struct kevent; 519 unsigned long kevent_flags; 520 # define EVENT_TX_HALT 0 521 # define EVENT_RX_HALT 1 522 # define EVENT_RST_PEND 31 523 }; 524 525 /* MCE Device Command Strings, generally a port and command pair */ 526 static char DEVICE_RESUME[] = {MCE_CMD_NULL, MCE_CMD_PORT_SYS, 527 MCE_CMD_RESUME}; 528 static char GET_REVISION[] = {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION}; 529 static char GET_EMVER[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER}; 530 static char GET_WAKEVERSION[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION}; 531 static char FLASH_LED[] = {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED}; 532 static char GET_UNKNOWN2[] = {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2}; 533 static char GET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS}; 534 static char GET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT}; 535 static char GET_NUM_PORTS[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS}; 536 static char GET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS}; 537 static char GET_RX_SENSOR[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN}; 538 /* sub in desired values in lower byte or bytes for full command */ 539 /* FIXME: make use of these for transmit. 540 static char SET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR, 541 MCE_CMD_SETIRCFS, 0x00, 0x00}; 542 static char SET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00}; 543 static char SET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR, 544 MCE_CMD_SETIRTIMEOUT, 0x00, 0x00}; 545 static char SET_RX_SENSOR[] = {MCE_CMD_PORT_IR, 546 MCE_RSP_EQIRRXPORTEN, 0x00}; 547 */ 548 549 static int mceusb_cmd_datasize(u8 cmd, u8 subcmd) 550 { 551 int datasize = 0; 552 553 switch (cmd) { 554 case MCE_CMD_NULL: 555 if (subcmd == MCE_CMD_PORT_SYS) 556 datasize = 1; 557 break; 558 case MCE_CMD_PORT_SYS: 559 switch (subcmd) { 560 case MCE_RSP_GETPORTSTATUS: 561 datasize = 5; 562 break; 563 case MCE_RSP_EQWAKEVERSION: 564 datasize = 4; 565 break; 566 case MCE_CMD_G_REVISION: 567 datasize = 4; 568 break; 569 case MCE_RSP_EQWAKESUPPORT: 570 case MCE_RSP_GETWAKESOURCE: 571 case MCE_RSP_EQDEVDETAILS: 572 case MCE_RSP_EQEMVER: 573 datasize = 1; 574 break; 575 } 576 break; 577 case MCE_CMD_PORT_IR: 578 switch (subcmd) { 579 case MCE_CMD_UNKNOWN: 580 case MCE_RSP_EQIRCFS: 581 case MCE_RSP_EQIRTIMEOUT: 582 case MCE_RSP_EQIRRXCFCNT: 583 case MCE_RSP_EQIRNUMPORTS: 584 datasize = 2; 585 break; 586 case MCE_CMD_SIG_END: 587 case MCE_RSP_EQIRTXPORTS: 588 case MCE_RSP_EQIRRXPORTEN: 589 datasize = 1; 590 break; 591 } 592 } 593 return datasize; 594 } 595 596 static void mceusb_dev_printdata(struct mceusb_dev *ir, u8 *buf, int buf_len, 597 int offset, int len, bool out) 598 { 599 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) 600 char *inout; 601 u8 cmd, subcmd, *data; 602 struct device *dev = ir->dev; 603 u32 carrier, period; 604 605 if (offset < 0 || offset >= buf_len) 606 return; 607 608 dev_dbg(dev, "%cx data[%d]: %*ph (len=%d sz=%d)", 609 (out ? 't' : 'r'), offset, 610 min(len, buf_len - offset), buf + offset, len, buf_len); 611 612 inout = out ? "Request" : "Got"; 613 614 cmd = buf[offset]; 615 subcmd = (offset + 1 < buf_len) ? buf[offset + 1] : 0; 616 data = &buf[offset] + 2; 617 618 /* Trace meaningless 0xb1 0x60 header bytes on original receiver */ 619 if (ir->flags.microsoft_gen1 && !out && !offset) { 620 dev_dbg(dev, "MCE gen 1 header"); 621 return; 622 } 623 624 /* Trace IR data header or trailer */ 625 if (cmd != MCE_CMD_PORT_IR && 626 (cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA) { 627 if (cmd == MCE_IRDATA_TRAILER) 628 dev_dbg(dev, "End of raw IR data"); 629 else 630 dev_dbg(dev, "Raw IR data, %d pulse/space samples", 631 cmd & MCE_PACKET_LENGTH_MASK); 632 return; 633 } 634 635 /* Unexpected end of buffer? */ 636 if (offset + len > buf_len) 637 return; 638 639 /* Decode MCE command/response */ 640 switch (cmd) { 641 case MCE_CMD_NULL: 642 if (subcmd == MCE_CMD_NULL) 643 break; 644 if ((subcmd == MCE_CMD_PORT_SYS) && 645 (data[0] == MCE_CMD_RESUME)) 646 dev_dbg(dev, "Device resume requested"); 647 else 648 dev_dbg(dev, "Unknown command 0x%02x 0x%02x", 649 cmd, subcmd); 650 break; 651 case MCE_CMD_PORT_SYS: 652 switch (subcmd) { 653 case MCE_RSP_EQEMVER: 654 if (!out) 655 dev_dbg(dev, "Emulator interface version %x", 656 data[0]); 657 break; 658 case MCE_CMD_G_REVISION: 659 if (len == 2) 660 dev_dbg(dev, "Get hw/sw rev?"); 661 else 662 dev_dbg(dev, "hw/sw rev %*ph", 663 4, &buf[offset + 2]); 664 break; 665 case MCE_CMD_RESUME: 666 dev_dbg(dev, "Device resume requested"); 667 break; 668 case MCE_RSP_CMD_ILLEGAL: 669 dev_dbg(dev, "Illegal PORT_SYS command"); 670 break; 671 case MCE_RSP_EQWAKEVERSION: 672 if (!out) 673 dev_dbg(dev, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x", 674 data[0], data[1], data[2], data[3]); 675 break; 676 case MCE_RSP_GETPORTSTATUS: 677 if (!out) 678 /* We use data1 + 1 here, to match hw labels */ 679 dev_dbg(dev, "TX port %d: blaster is%s connected", 680 data[0] + 1, data[3] ? " not" : ""); 681 break; 682 case MCE_CMD_FLASHLED: 683 dev_dbg(dev, "Attempting to flash LED"); 684 break; 685 default: 686 dev_dbg(dev, "Unknown command 0x%02x 0x%02x", 687 cmd, subcmd); 688 break; 689 } 690 break; 691 case MCE_CMD_PORT_IR: 692 switch (subcmd) { 693 case MCE_CMD_SIG_END: 694 dev_dbg(dev, "End of signal"); 695 break; 696 case MCE_CMD_PING: 697 dev_dbg(dev, "Ping"); 698 break; 699 case MCE_CMD_UNKNOWN: 700 dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x", 701 data[0], data[1]); 702 break; 703 case MCE_RSP_EQIRCFS: 704 period = DIV_ROUND_CLOSEST((1U << data[0] * 2) * 705 (data[1] + 1), 10); 706 if (!period) 707 break; 708 carrier = (1000 * 1000) / period; 709 dev_dbg(dev, "%s carrier of %u Hz (period %uus)", 710 inout, carrier, period); 711 break; 712 case MCE_CMD_GETIRCFS: 713 dev_dbg(dev, "Get carrier mode and freq"); 714 break; 715 case MCE_RSP_EQIRTXPORTS: 716 dev_dbg(dev, "%s transmit blaster mask of 0x%02x", 717 inout, data[0]); 718 break; 719 case MCE_RSP_EQIRTIMEOUT: 720 /* value is in units of 50us, so x*50/1000 ms */ 721 period = ((data[0] << 8) | data[1]) * 722 MCE_TIME_UNIT / 1000; 723 dev_dbg(dev, "%s receive timeout of %d ms", 724 inout, period); 725 break; 726 case MCE_CMD_GETIRTIMEOUT: 727 dev_dbg(dev, "Get receive timeout"); 728 break; 729 case MCE_CMD_GETIRTXPORTS: 730 dev_dbg(dev, "Get transmit blaster mask"); 731 break; 732 case MCE_RSP_EQIRRXPORTEN: 733 dev_dbg(dev, "%s %s-range receive sensor in use", 734 inout, data[0] == 0x02 ? "short" : "long"); 735 break; 736 case MCE_CMD_GETIRRXPORTEN: 737 /* aka MCE_RSP_EQIRRXCFCNT */ 738 if (out) 739 dev_dbg(dev, "Get receive sensor"); 740 else 741 dev_dbg(dev, "RX carrier cycle count: %d", 742 ((data[0] << 8) | data[1])); 743 break; 744 case MCE_RSP_EQIRNUMPORTS: 745 if (out) 746 break; 747 dev_dbg(dev, "Num TX ports: %x, num RX ports: %x", 748 data[0], data[1]); 749 break; 750 case MCE_RSP_CMD_ILLEGAL: 751 dev_dbg(dev, "Illegal PORT_IR command"); 752 break; 753 case MCE_RSP_TX_TIMEOUT: 754 dev_dbg(dev, "IR TX timeout (TX buffer underrun)"); 755 break; 756 default: 757 dev_dbg(dev, "Unknown command 0x%02x 0x%02x", 758 cmd, subcmd); 759 break; 760 } 761 break; 762 default: 763 break; 764 } 765 #endif 766 } 767 768 /* 769 * Schedule work that can't be done in interrupt handlers 770 * (mceusb_dev_recv() and mce_write_callback()) nor tasklets. 771 * Invokes mceusb_deferred_kevent() for recovering from 772 * error events specified by the kevent bit field. 773 */ 774 static void mceusb_defer_kevent(struct mceusb_dev *ir, int kevent) 775 { 776 set_bit(kevent, &ir->kevent_flags); 777 778 if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) { 779 dev_dbg(ir->dev, "kevent %d dropped pending USB Reset Device", 780 kevent); 781 return; 782 } 783 784 if (!schedule_work(&ir->kevent)) 785 dev_dbg(ir->dev, "kevent %d already scheduled", kevent); 786 else 787 dev_dbg(ir->dev, "kevent %d scheduled", kevent); 788 } 789 790 static void mce_write_callback(struct urb *urb) 791 { 792 if (!urb) 793 return; 794 795 complete(urb->context); 796 } 797 798 /* 799 * Write (TX/send) data to MCE device USB endpoint out. 800 * Used for IR blaster TX and MCE device commands. 801 * 802 * Return: The number of bytes written (> 0) or errno (< 0). 803 */ 804 static int mce_write(struct mceusb_dev *ir, u8 *data, int size) 805 { 806 int ret; 807 struct urb *urb; 808 struct device *dev = ir->dev; 809 unsigned char *buf_out; 810 struct completion tx_done; 811 unsigned long expire; 812 unsigned long ret_wait; 813 814 mceusb_dev_printdata(ir, data, size, 0, size, true); 815 816 urb = usb_alloc_urb(0, GFP_KERNEL); 817 if (unlikely(!urb)) { 818 dev_err(dev, "Error: mce write couldn't allocate urb"); 819 return -ENOMEM; 820 } 821 822 buf_out = kmalloc(size, GFP_KERNEL); 823 if (!buf_out) { 824 usb_free_urb(urb); 825 return -ENOMEM; 826 } 827 828 init_completion(&tx_done); 829 830 /* outbound data */ 831 if (usb_endpoint_xfer_int(ir->usb_ep_out)) 832 usb_fill_int_urb(urb, ir->usbdev, ir->pipe_out, 833 buf_out, size, mce_write_callback, &tx_done, 834 ir->usb_ep_out->bInterval); 835 else 836 usb_fill_bulk_urb(urb, ir->usbdev, ir->pipe_out, 837 buf_out, size, mce_write_callback, &tx_done); 838 memcpy(buf_out, data, size); 839 840 ret = usb_submit_urb(urb, GFP_KERNEL); 841 if (ret) { 842 dev_err(dev, "Error: mce write submit urb error = %d", ret); 843 kfree(buf_out); 844 usb_free_urb(urb); 845 return ret; 846 } 847 848 expire = msecs_to_jiffies(USB_TX_TIMEOUT); 849 ret_wait = wait_for_completion_timeout(&tx_done, expire); 850 if (!ret_wait) { 851 dev_err(dev, "Error: mce write timed out (expire = %lu (%dms))", 852 expire, USB_TX_TIMEOUT); 853 usb_kill_urb(urb); 854 ret = (urb->status == -ENOENT ? -ETIMEDOUT : urb->status); 855 } else { 856 ret = urb->status; 857 } 858 if (ret >= 0) 859 ret = urb->actual_length; /* bytes written */ 860 861 switch (urb->status) { 862 /* success */ 863 case 0: 864 break; 865 866 case -ECONNRESET: 867 case -ENOENT: 868 case -EILSEQ: 869 case -ESHUTDOWN: 870 break; 871 872 case -EPIPE: 873 dev_err(ir->dev, "Error: mce write urb status = %d (TX HALT)", 874 urb->status); 875 mceusb_defer_kevent(ir, EVENT_TX_HALT); 876 break; 877 878 default: 879 dev_err(ir->dev, "Error: mce write urb status = %d", 880 urb->status); 881 break; 882 } 883 884 dev_dbg(dev, "tx done status = %d (wait = %lu, expire = %lu (%dms), urb->actual_length = %d, urb->status = %d)", 885 ret, ret_wait, expire, USB_TX_TIMEOUT, 886 urb->actual_length, urb->status); 887 888 kfree(buf_out); 889 usb_free_urb(urb); 890 891 return ret; 892 } 893 894 static void mce_command_out(struct mceusb_dev *ir, u8 *data, int size) 895 { 896 int rsize = sizeof(DEVICE_RESUME); 897 898 if (ir->need_reset) { 899 ir->need_reset = false; 900 mce_write(ir, DEVICE_RESUME, rsize); 901 msleep(10); 902 } 903 904 mce_write(ir, data, size); 905 msleep(10); 906 } 907 908 /* 909 * Transmit IR out the MCE device IR blaster port(s). 910 * 911 * Convert IR pulse/space sequence from LIRC to MCE format. 912 * Break up a long IR sequence into multiple parts (MCE IR data packets). 913 * 914 * u32 txbuf[] consists of IR pulse, space, ..., and pulse times in usec. 915 * Pulses and spaces are implicit by their position. 916 * The first IR sample, txbuf[0], is always a pulse. 917 * 918 * u8 irbuf[] consists of multiple IR data packets for the MCE device. 919 * A packet is 1 u8 MCE_IRDATA_HEADER and up to 30 u8 IR samples. 920 * An IR sample is 1-bit pulse/space flag with 7-bit time 921 * in MCE time units (50usec). 922 * 923 * Return: The number of IR samples sent (> 0) or errno (< 0). 924 */ 925 static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count) 926 { 927 struct mceusb_dev *ir = dev->priv; 928 u8 cmdbuf[3] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00 }; 929 u8 irbuf[MCE_IRBUF_SIZE]; 930 int ircount = 0; 931 unsigned int irsample; 932 int i, length, ret; 933 934 /* Send the set TX ports command */ 935 cmdbuf[2] = ir->tx_mask; 936 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 937 938 /* Generate mce IR data packet */ 939 for (i = 0; i < count; i++) { 940 irsample = txbuf[i] / MCE_TIME_UNIT; 941 942 /* loop to support long pulses/spaces > 6350us (127*50us) */ 943 while (irsample > 0) { 944 /* Insert IR header every 30th entry */ 945 if (ircount % MCE_PACKET_SIZE == 0) { 946 /* Room for IR header and one IR sample? */ 947 if (ircount >= MCE_IRBUF_SIZE - 1) { 948 /* Send near full buffer */ 949 ret = mce_write(ir, irbuf, ircount); 950 if (ret < 0) 951 return ret; 952 ircount = 0; 953 } 954 irbuf[ircount++] = MCE_IRDATA_HEADER; 955 } 956 957 /* Insert IR sample */ 958 if (irsample <= MCE_MAX_PULSE_LENGTH) { 959 irbuf[ircount] = irsample; 960 irsample = 0; 961 } else { 962 irbuf[ircount] = MCE_MAX_PULSE_LENGTH; 963 irsample -= MCE_MAX_PULSE_LENGTH; 964 } 965 /* 966 * Even i = IR pulse 967 * Odd i = IR space 968 */ 969 irbuf[ircount] |= (i & 1 ? 0 : MCE_PULSE_BIT); 970 ircount++; 971 972 /* IR buffer full? */ 973 if (ircount >= MCE_IRBUF_SIZE) { 974 /* Fix packet length in last header */ 975 length = ircount % MCE_PACKET_SIZE; 976 if (length > 0) 977 irbuf[ircount - length] -= 978 MCE_PACKET_SIZE - length; 979 /* Send full buffer */ 980 ret = mce_write(ir, irbuf, ircount); 981 if (ret < 0) 982 return ret; 983 ircount = 0; 984 } 985 } 986 } /* after for loop, 0 <= ircount < MCE_IRBUF_SIZE */ 987 988 /* Fix packet length in last header */ 989 length = ircount % MCE_PACKET_SIZE; 990 if (length > 0) 991 irbuf[ircount - length] -= MCE_PACKET_SIZE - length; 992 993 /* Append IR trailer (0x80) to final partial (or empty) IR buffer */ 994 irbuf[ircount++] = MCE_IRDATA_TRAILER; 995 996 /* Send final buffer */ 997 ret = mce_write(ir, irbuf, ircount); 998 if (ret < 0) 999 return ret; 1000 1001 return count; 1002 } 1003 1004 /* Sets active IR outputs -- mce devices typically have two */ 1005 static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask) 1006 { 1007 struct mceusb_dev *ir = dev->priv; 1008 1009 /* return number of transmitters */ 1010 int emitters = ir->num_txports ? ir->num_txports : 2; 1011 1012 if (mask >= (1 << emitters)) 1013 return emitters; 1014 1015 if (ir->flags.tx_mask_normal) 1016 ir->tx_mask = mask; 1017 else 1018 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ? 1019 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1; 1020 1021 return 0; 1022 } 1023 1024 /* Sets the send carrier frequency and mode */ 1025 static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier) 1026 { 1027 struct mceusb_dev *ir = dev->priv; 1028 int clk = 10000000; 1029 int prescaler = 0, divisor = 0; 1030 unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR, 1031 MCE_CMD_SETIRCFS, 0x00, 0x00 }; 1032 1033 /* Carrier has changed */ 1034 if (ir->carrier != carrier) { 1035 1036 if (carrier == 0) { 1037 ir->carrier = carrier; 1038 cmdbuf[2] = MCE_CMD_SIG_END; 1039 cmdbuf[3] = MCE_IRDATA_TRAILER; 1040 dev_dbg(ir->dev, "disabling carrier modulation"); 1041 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 1042 return 0; 1043 } 1044 1045 for (prescaler = 0; prescaler < 4; ++prescaler) { 1046 divisor = (clk >> (2 * prescaler)) / carrier; 1047 if (divisor <= 0xff) { 1048 ir->carrier = carrier; 1049 cmdbuf[2] = prescaler; 1050 cmdbuf[3] = divisor; 1051 dev_dbg(ir->dev, "requesting %u HZ carrier", 1052 carrier); 1053 1054 /* Transmit new carrier to mce device */ 1055 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 1056 return 0; 1057 } 1058 } 1059 1060 return -EINVAL; 1061 1062 } 1063 1064 return 0; 1065 } 1066 1067 static int mceusb_set_timeout(struct rc_dev *dev, unsigned int timeout) 1068 { 1069 u8 cmdbuf[4] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTIMEOUT, 0, 0 }; 1070 struct mceusb_dev *ir = dev->priv; 1071 unsigned int units; 1072 1073 units = DIV_ROUND_CLOSEST(timeout, US_TO_NS(MCE_TIME_UNIT)); 1074 1075 cmdbuf[2] = units >> 8; 1076 cmdbuf[3] = units; 1077 1078 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 1079 1080 /* get receiver timeout value */ 1081 mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT)); 1082 1083 return 0; 1084 } 1085 1086 /* 1087 * Select or deselect the 2nd receiver port. 1088 * Second receiver is learning mode, wide-band, short-range receiver. 1089 * Only one receiver (long or short range) may be active at a time. 1090 */ 1091 static int mceusb_set_rx_wideband(struct rc_dev *dev, int enable) 1092 { 1093 struct mceusb_dev *ir = dev->priv; 1094 unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR, 1095 MCE_CMD_SETIRRXPORTEN, 0x00 }; 1096 1097 dev_dbg(ir->dev, "select %s-range receive sensor", 1098 enable ? "short" : "long"); 1099 if (enable) { 1100 ir->wideband_rx_enabled = true; 1101 cmdbuf[2] = 2; /* port 2 is short range receiver */ 1102 } else { 1103 ir->wideband_rx_enabled = false; 1104 cmdbuf[2] = 1; /* port 1 is long range receiver */ 1105 } 1106 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 1107 /* response from device sets ir->learning_active */ 1108 1109 return 0; 1110 } 1111 1112 /* 1113 * Enable/disable receiver carrier frequency pass through reporting. 1114 * Only the short-range receiver has carrier frequency measuring capability. 1115 * Implicitly select this receiver when enabling carrier frequency reporting. 1116 */ 1117 static int mceusb_set_rx_carrier_report(struct rc_dev *dev, int enable) 1118 { 1119 struct mceusb_dev *ir = dev->priv; 1120 unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR, 1121 MCE_CMD_SETIRRXPORTEN, 0x00 }; 1122 1123 dev_dbg(ir->dev, "%s short-range receiver carrier reporting", 1124 enable ? "enable" : "disable"); 1125 if (enable) { 1126 ir->carrier_report_enabled = true; 1127 if (!ir->learning_active) { 1128 cmdbuf[2] = 2; /* port 2 is short range receiver */ 1129 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 1130 } 1131 } else { 1132 ir->carrier_report_enabled = false; 1133 /* 1134 * Revert to normal (long-range) receiver only if the 1135 * wideband (short-range) receiver wasn't explicitly 1136 * enabled. 1137 */ 1138 if (ir->learning_active && !ir->wideband_rx_enabled) { 1139 cmdbuf[2] = 1; /* port 1 is long range receiver */ 1140 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 1141 } 1142 } 1143 1144 return 0; 1145 } 1146 1147 /* 1148 * Handle PORT_SYS/IR command response received from the MCE device. 1149 * 1150 * Assumes single response with all its data (not truncated) 1151 * in buf_in[]. The response itself determines its total length 1152 * (mceusb_cmd_datasize() + 2) and hence the minimum size of buf_in[]. 1153 * 1154 * We don't do anything but print debug spew for many of the command bits 1155 * we receive from the hardware, but some of them are useful information 1156 * we want to store so that we can use them. 1157 */ 1158 static void mceusb_handle_command(struct mceusb_dev *ir, u8 *buf_in) 1159 { 1160 u8 cmd = buf_in[0]; 1161 u8 subcmd = buf_in[1]; 1162 u8 *hi = &buf_in[2]; /* read only when required */ 1163 u8 *lo = &buf_in[3]; /* read only when required */ 1164 struct ir_raw_event rawir = {}; 1165 u32 carrier_cycles; 1166 u32 cycles_fix; 1167 1168 if (cmd == MCE_CMD_PORT_SYS) { 1169 switch (subcmd) { 1170 /* the one and only 5-byte return value command */ 1171 case MCE_RSP_GETPORTSTATUS: 1172 if (buf_in[5] == 0) 1173 ir->txports_cabled |= 1 << *hi; 1174 break; 1175 1176 /* 1-byte return value commands */ 1177 case MCE_RSP_EQEMVER: 1178 ir->emver = *hi; 1179 break; 1180 1181 /* No return value commands */ 1182 case MCE_RSP_CMD_ILLEGAL: 1183 ir->need_reset = true; 1184 break; 1185 1186 default: 1187 break; 1188 } 1189 1190 return; 1191 } 1192 1193 if (cmd != MCE_CMD_PORT_IR) 1194 return; 1195 1196 switch (subcmd) { 1197 /* 2-byte return value commands */ 1198 case MCE_RSP_EQIRTIMEOUT: 1199 ir->rc->timeout = US_TO_NS((*hi << 8 | *lo) * MCE_TIME_UNIT); 1200 break; 1201 case MCE_RSP_EQIRNUMPORTS: 1202 ir->num_txports = *hi; 1203 ir->num_rxports = *lo; 1204 break; 1205 case MCE_RSP_EQIRRXCFCNT: 1206 /* 1207 * The carrier cycle counter can overflow and wrap around 1208 * without notice from the device. So frequency measurement 1209 * will be inaccurate with long duration IR. 1210 * 1211 * The long-range (non learning) receiver always reports 1212 * zero count so we always ignore its report. 1213 */ 1214 if (ir->carrier_report_enabled && ir->learning_active && 1215 ir->pulse_tunit > 0) { 1216 carrier_cycles = (*hi << 8 | *lo); 1217 /* 1218 * Adjust carrier cycle count by adding 1219 * 1 missed count per pulse "on" 1220 */ 1221 cycles_fix = ir->flags.rx2 == 2 ? ir->pulse_count : 0; 1222 rawir.carrier_report = 1; 1223 rawir.carrier = (1000000u / MCE_TIME_UNIT) * 1224 (carrier_cycles + cycles_fix) / 1225 ir->pulse_tunit; 1226 dev_dbg(ir->dev, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)", 1227 rawir.carrier, ir->pulse_count, carrier_cycles, 1228 ir->pulse_tunit, ir->flags.rx2); 1229 ir_raw_event_store(ir->rc, &rawir); 1230 } 1231 break; 1232 1233 /* 1-byte return value commands */ 1234 case MCE_RSP_EQIRTXPORTS: 1235 ir->tx_mask = *hi; 1236 break; 1237 case MCE_RSP_EQIRRXPORTEN: 1238 ir->learning_active = ((*hi & 0x02) == 0x02); 1239 if (ir->rxports_active != *hi) { 1240 dev_info(ir->dev, "%s-range (0x%x) receiver active", 1241 ir->learning_active ? "short" : "long", *hi); 1242 ir->rxports_active = *hi; 1243 } 1244 break; 1245 1246 /* No return value commands */ 1247 case MCE_RSP_CMD_ILLEGAL: 1248 case MCE_RSP_TX_TIMEOUT: 1249 ir->need_reset = true; 1250 break; 1251 1252 default: 1253 break; 1254 } 1255 } 1256 1257 static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len) 1258 { 1259 struct ir_raw_event rawir = {}; 1260 bool event = false; 1261 int i = 0; 1262 1263 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */ 1264 if (ir->flags.microsoft_gen1) 1265 i = 2; 1266 1267 /* if there's no data, just return now */ 1268 if (buf_len <= i) 1269 return; 1270 1271 for (; i < buf_len; i++) { 1272 switch (ir->parser_state) { 1273 case SUBCMD: 1274 ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]); 1275 mceusb_dev_printdata(ir, ir->buf_in, buf_len, i - 1, 1276 ir->rem + 2, false); 1277 if (i + ir->rem < buf_len) 1278 mceusb_handle_command(ir, &ir->buf_in[i - 1]); 1279 ir->parser_state = CMD_DATA; 1280 break; 1281 case PARSE_IRDATA: 1282 ir->rem--; 1283 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0); 1284 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK); 1285 if (unlikely(!rawir.duration)) { 1286 dev_dbg(ir->dev, "nonsensical irdata %02x with duration 0", 1287 ir->buf_in[i]); 1288 break; 1289 } 1290 if (rawir.pulse) { 1291 ir->pulse_tunit += rawir.duration; 1292 ir->pulse_count++; 1293 } 1294 rawir.duration *= US_TO_NS(MCE_TIME_UNIT); 1295 1296 dev_dbg(ir->dev, "Storing %s %u ns (%02x)", 1297 rawir.pulse ? "pulse" : "space", 1298 rawir.duration, ir->buf_in[i]); 1299 1300 if (ir_raw_event_store_with_filter(ir->rc, &rawir)) 1301 event = true; 1302 break; 1303 case CMD_DATA: 1304 ir->rem--; 1305 break; 1306 case CMD_HEADER: 1307 ir->cmd = ir->buf_in[i]; 1308 if ((ir->cmd == MCE_CMD_PORT_IR) || 1309 ((ir->cmd & MCE_PORT_MASK) != 1310 MCE_COMMAND_IRDATA)) { 1311 /* 1312 * got PORT_SYS, PORT_IR, or unknown 1313 * command response prefix 1314 */ 1315 ir->parser_state = SUBCMD; 1316 continue; 1317 } 1318 /* 1319 * got IR data prefix (0x80 + num_bytes) 1320 * decode MCE packets of the form {0x83, AA, BB, CC} 1321 * IR data packets can span USB messages 1322 */ 1323 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK); 1324 mceusb_dev_printdata(ir, ir->buf_in, buf_len, 1325 i, ir->rem + 1, false); 1326 if (ir->rem) { 1327 ir->parser_state = PARSE_IRDATA; 1328 } else { 1329 struct ir_raw_event ev = { 1330 .timeout = 1, 1331 .duration = ir->rc->timeout 1332 }; 1333 1334 if (ir_raw_event_store_with_filter(ir->rc, 1335 &ev)) 1336 event = true; 1337 ir->pulse_tunit = 0; 1338 ir->pulse_count = 0; 1339 } 1340 break; 1341 } 1342 1343 if (ir->parser_state != CMD_HEADER && !ir->rem) 1344 ir->parser_state = CMD_HEADER; 1345 } 1346 1347 /* 1348 * Accept IR data spanning multiple rx buffers. 1349 * Reject MCE command response spanning multiple rx buffers. 1350 */ 1351 if (ir->parser_state != PARSE_IRDATA || !ir->rem) 1352 ir->parser_state = CMD_HEADER; 1353 1354 if (event) { 1355 dev_dbg(ir->dev, "processed IR data"); 1356 ir_raw_event_handle(ir->rc); 1357 } 1358 } 1359 1360 static void mceusb_dev_recv(struct urb *urb) 1361 { 1362 struct mceusb_dev *ir; 1363 1364 if (!urb) 1365 return; 1366 1367 ir = urb->context; 1368 if (!ir) { 1369 usb_unlink_urb(urb); 1370 return; 1371 } 1372 1373 switch (urb->status) { 1374 /* success */ 1375 case 0: 1376 mceusb_process_ir_data(ir, urb->actual_length); 1377 break; 1378 1379 case -ECONNRESET: 1380 case -ENOENT: 1381 case -EILSEQ: 1382 case -ESHUTDOWN: 1383 usb_unlink_urb(urb); 1384 return; 1385 1386 case -EPIPE: 1387 dev_err(ir->dev, "Error: urb status = %d (RX HALT)", 1388 urb->status); 1389 mceusb_defer_kevent(ir, EVENT_RX_HALT); 1390 return; 1391 1392 default: 1393 dev_err(ir->dev, "Error: urb status = %d", urb->status); 1394 break; 1395 } 1396 1397 usb_submit_urb(urb, GFP_ATOMIC); 1398 } 1399 1400 static void mceusb_get_emulator_version(struct mceusb_dev *ir) 1401 { 1402 /* If we get no reply or an illegal command reply, its ver 1, says MS */ 1403 ir->emver = 1; 1404 mce_command_out(ir, GET_EMVER, sizeof(GET_EMVER)); 1405 } 1406 1407 static void mceusb_gen1_init(struct mceusb_dev *ir) 1408 { 1409 int ret; 1410 struct device *dev = ir->dev; 1411 char *data; 1412 1413 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL); 1414 if (!data) { 1415 dev_err(dev, "%s: memory allocation failed!", __func__); 1416 return; 1417 } 1418 1419 /* 1420 * This is a strange one. Windows issues a set address to the device 1421 * on the receive control pipe and expect a certain value pair back 1422 */ 1423 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0), 1424 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0, 1425 data, USB_CTRL_MSG_SZ, HZ * 3); 1426 dev_dbg(dev, "set address - ret = %d", ret); 1427 dev_dbg(dev, "set address - data[0] = %d, data[1] = %d", 1428 data[0], data[1]); 1429 1430 /* set feature: bit rate 38400 bps */ 1431 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0), 1432 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR, 1433 0xc04e, 0x0000, NULL, 0, HZ * 3); 1434 1435 dev_dbg(dev, "set feature - ret = %d", ret); 1436 1437 /* bRequest 4: set char length to 8 bits */ 1438 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0), 1439 4, USB_TYPE_VENDOR, 1440 0x0808, 0x0000, NULL, 0, HZ * 3); 1441 dev_dbg(dev, "set char length - retB = %d", ret); 1442 1443 /* bRequest 2: set handshaking to use DTR/DSR */ 1444 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0), 1445 2, USB_TYPE_VENDOR, 1446 0x0000, 0x0100, NULL, 0, HZ * 3); 1447 dev_dbg(dev, "set handshake - retC = %d", ret); 1448 1449 /* device resume */ 1450 mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME)); 1451 1452 /* get hw/sw revision? */ 1453 mce_command_out(ir, GET_REVISION, sizeof(GET_REVISION)); 1454 1455 kfree(data); 1456 } 1457 1458 static void mceusb_gen2_init(struct mceusb_dev *ir) 1459 { 1460 /* device resume */ 1461 mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME)); 1462 1463 /* get wake version (protocol, key, address) */ 1464 mce_command_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION)); 1465 1466 /* unknown what this one actually returns... */ 1467 mce_command_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2)); 1468 } 1469 1470 static void mceusb_get_parameters(struct mceusb_dev *ir) 1471 { 1472 int i; 1473 unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS, 1474 MCE_CMD_GETPORTSTATUS, 0x00 }; 1475 1476 /* defaults, if the hardware doesn't support querying */ 1477 ir->num_txports = 2; 1478 ir->num_rxports = 2; 1479 1480 /* get number of tx and rx ports */ 1481 mce_command_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS)); 1482 1483 /* get the carrier and frequency */ 1484 mce_command_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ)); 1485 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)); 1489 1490 /* get receiver timeout value */ 1491 mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT)); 1492 1493 /* get receiver sensor setting */ 1494 mce_command_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR)); 1495 1496 for (i = 0; i < ir->num_txports; i++) { 1497 cmdbuf[2] = i; 1498 mce_command_out(ir, cmdbuf, sizeof(cmdbuf)); 1499 } 1500 } 1501 1502 static void mceusb_flash_led(struct mceusb_dev *ir) 1503 { 1504 if (ir->emver < 2) 1505 return; 1506 1507 mce_command_out(ir, FLASH_LED, sizeof(FLASH_LED)); 1508 } 1509 1510 /* 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. 1516 */ 1517 static void mceusb_deferred_kevent(struct work_struct *work) 1518 { 1519 struct mceusb_dev *ir = 1520 container_of(work, struct mceusb_dev, kevent); 1521 int status; 1522 1523 dev_err(ir->dev, "kevent handler called (flags 0x%lx)", 1524 ir->kevent_flags); 1525 1526 if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) { 1527 dev_err(ir->dev, "kevent handler canceled pending USB Reset Device"); 1528 return; 1529 } 1530 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); 1535 if (status < 0) { 1536 /* 1537 * Unable to clear RX halt/stall. 1538 * Will need to call usb_reset_device(). 1539 */ 1540 dev_err(ir->dev, 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); 1545 1546 /* Cancel all other error events and handlers */ 1547 clear_bit(EVENT_TX_HALT, &ir->kevent_flags); 1548 return; 1549 } 1550 clear_bit(EVENT_RX_HALT, &ir->kevent_flags); 1551 status = usb_submit_urb(ir->urb_in, GFP_KERNEL); 1552 if (status < 0) { 1553 dev_err(ir->dev, "rx unhalt submit urb error = %d", 1554 status); 1555 } 1556 } 1557 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); 1561 if (status < 0) { 1562 /* 1563 * Unable to clear TX halt/stall. 1564 * Will need to call usb_reset_device(). 1565 */ 1566 dev_err(ir->dev, 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); 1571 1572 /* Cancel all other error events and handlers */ 1573 clear_bit(EVENT_RX_HALT, &ir->kevent_flags); 1574 return; 1575 } 1576 clear_bit(EVENT_TX_HALT, &ir->kevent_flags); 1577 } 1578 } 1579 1580 static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir) 1581 { 1582 struct usb_device *udev = ir->usbdev; 1583 struct device *dev = ir->dev; 1584 struct rc_dev *rc; 1585 int ret; 1586 1587 rc = rc_allocate_device(RC_DRIVER_IR_RAW); 1588 if (!rc) { 1589 dev_err(dev, "remote dev allocation failed"); 1590 goto out; 1591 } 1592 1593 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)", 1594 mceusb_model[ir->model].name ? 1595 mceusb_model[ir->model].name : 1596 "Media Center Ed. eHome Infrared Remote Transceiver", 1597 le16_to_cpu(ir->usbdev->descriptor.idVendor), 1598 le16_to_cpu(ir->usbdev->descriptor.idProduct)); 1599 1600 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys)); 1601 1602 rc->device_name = ir->name; 1603 rc->input_phys = ir->phys; 1604 usb_to_input_id(ir->usbdev, &rc->input_id); 1605 rc->dev.parent = dev; 1606 rc->priv = ir; 1607 rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER; 1608 rc->min_timeout = US_TO_NS(MCE_TIME_UNIT); 1609 rc->timeout = MS_TO_NS(100); 1610 if (!mceusb_model[ir->model].broken_irtimeout) { 1611 rc->s_timeout = mceusb_set_timeout; 1612 rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT; 1613 } else { 1614 /* 1615 * If we can't set the timeout using CMD_SETIRTIMEOUT, we can 1616 * rely on software timeouts for timeouts < 100ms. 1617 */ 1618 rc->max_timeout = rc->timeout; 1619 } 1620 if (!ir->flags.no_tx) { 1621 rc->s_tx_mask = mceusb_set_tx_mask; 1622 rc->s_tx_carrier = mceusb_set_tx_carrier; 1623 rc->tx_ir = mceusb_tx_ir; 1624 } 1625 if (ir->flags.rx2 > 0) { 1626 rc->s_learning_mode = mceusb_set_rx_wideband; 1627 rc->s_carrier_report = mceusb_set_rx_carrier_report; 1628 } 1629 rc->driver_name = DRIVER_NAME; 1630 1631 switch (le16_to_cpu(udev->descriptor.idVendor)) { 1632 case VENDOR_HAUPPAUGE: 1633 rc->map_name = RC_MAP_HAUPPAUGE; 1634 break; 1635 case VENDOR_PCTV: 1636 rc->map_name = RC_MAP_PINNACLE_PCTV_HD; 1637 break; 1638 default: 1639 rc->map_name = RC_MAP_RC6_MCE; 1640 } 1641 if (mceusb_model[ir->model].rc_map) 1642 rc->map_name = mceusb_model[ir->model].rc_map; 1643 1644 ret = rc_register_device(rc); 1645 if (ret < 0) { 1646 dev_err(dev, "remote dev registration failed"); 1647 goto out; 1648 } 1649 1650 return rc; 1651 1652 out: 1653 rc_free_device(rc); 1654 return NULL; 1655 } 1656 1657 static int mceusb_dev_probe(struct usb_interface *intf, 1658 const struct usb_device_id *id) 1659 { 1660 struct usb_device *dev = interface_to_usbdev(intf); 1661 struct usb_host_interface *idesc; 1662 struct usb_endpoint_descriptor *ep = NULL; 1663 struct usb_endpoint_descriptor *ep_in = NULL; 1664 struct usb_endpoint_descriptor *ep_out = NULL; 1665 struct mceusb_dev *ir = NULL; 1666 int pipe, maxp, i, res; 1667 char buf[63], name[128] = ""; 1668 enum mceusb_model_type model = id->driver_info; 1669 bool is_gen3; 1670 bool is_microsoft_gen1; 1671 bool tx_mask_normal; 1672 int ir_intfnum; 1673 1674 dev_dbg(&intf->dev, "%s called", __func__); 1675 1676 idesc = intf->cur_altsetting; 1677 1678 is_gen3 = mceusb_model[model].mce_gen3; 1679 is_microsoft_gen1 = mceusb_model[model].mce_gen1; 1680 tx_mask_normal = mceusb_model[model].tx_mask_normal; 1681 ir_intfnum = mceusb_model[model].ir_intfnum; 1682 1683 /* There are multi-function devices with non-IR interfaces */ 1684 if (idesc->desc.bInterfaceNumber != ir_intfnum) 1685 return -ENODEV; 1686 1687 /* step through the endpoints to find first bulk in and out endpoint */ 1688 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) { 1689 ep = &idesc->endpoint[i].desc; 1690 1691 if (ep_in == NULL) { 1692 if (usb_endpoint_is_bulk_in(ep)) { 1693 ep_in = ep; 1694 dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n"); 1695 } else if (usb_endpoint_is_int_in(ep)) { 1696 ep_in = ep; 1697 ep_in->bInterval = 1; 1698 dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n"); 1699 } 1700 } 1701 1702 if (ep_out == NULL) { 1703 if (usb_endpoint_is_bulk_out(ep)) { 1704 ep_out = ep; 1705 dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n"); 1706 } else if (usb_endpoint_is_int_out(ep)) { 1707 ep_out = ep; 1708 ep_out->bInterval = 1; 1709 dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n"); 1710 } 1711 } 1712 } 1713 if (!ep_in || !ep_out) { 1714 dev_dbg(&intf->dev, "required endpoints not found\n"); 1715 return -ENODEV; 1716 } 1717 1718 if (usb_endpoint_xfer_int(ep_in)) 1719 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress); 1720 else 1721 pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress); 1722 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); 1723 1724 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL); 1725 if (!ir) 1726 goto mem_alloc_fail; 1727 1728 ir->pipe_in = pipe; 1729 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in); 1730 if (!ir->buf_in) 1731 goto buf_in_alloc_fail; 1732 1733 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL); 1734 if (!ir->urb_in) 1735 goto urb_in_alloc_fail; 1736 1737 ir->usbintf = intf; 1738 ir->usbdev = usb_get_dev(dev); 1739 ir->dev = &intf->dev; 1740 ir->len_in = maxp; 1741 ir->flags.microsoft_gen1 = is_microsoft_gen1; 1742 ir->flags.tx_mask_normal = tx_mask_normal; 1743 ir->flags.no_tx = mceusb_model[model].no_tx; 1744 ir->flags.rx2 = mceusb_model[model].rx2; 1745 ir->model = model; 1746 1747 /* Saving usb interface data for use by the transmitter routine */ 1748 ir->usb_ep_out = ep_out; 1749 if (usb_endpoint_xfer_int(ep_out)) 1750 ir->pipe_out = usb_sndintpipe(ir->usbdev, 1751 ep_out->bEndpointAddress); 1752 else 1753 ir->pipe_out = usb_sndbulkpipe(ir->usbdev, 1754 ep_out->bEndpointAddress); 1755 1756 if (dev->descriptor.iManufacturer 1757 && usb_string(dev, dev->descriptor.iManufacturer, 1758 buf, sizeof(buf)) > 0) 1759 strscpy(name, buf, sizeof(name)); 1760 if (dev->descriptor.iProduct 1761 && usb_string(dev, dev->descriptor.iProduct, 1762 buf, sizeof(buf)) > 0) 1763 snprintf(name + strlen(name), sizeof(name) - strlen(name), 1764 " %s", buf); 1765 1766 /* 1767 * Initialize async USB error handler before registering 1768 * or activating any mceusb RX and TX functions 1769 */ 1770 INIT_WORK(&ir->kevent, mceusb_deferred_kevent); 1771 1772 ir->rc = mceusb_init_rc_dev(ir); 1773 if (!ir->rc) 1774 goto rc_dev_fail; 1775 1776 /* wire up inbound data handler */ 1777 if (usb_endpoint_xfer_int(ep_in)) 1778 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp, 1779 mceusb_dev_recv, ir, ep_in->bInterval); 1780 else 1781 usb_fill_bulk_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp, 1782 mceusb_dev_recv, ir); 1783 1784 ir->urb_in->transfer_dma = ir->dma_in; 1785 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1786 1787 /* flush buffers on the device */ 1788 dev_dbg(&intf->dev, "Flushing receive buffers"); 1789 res = usb_submit_urb(ir->urb_in, GFP_KERNEL); 1790 if (res) 1791 dev_err(&intf->dev, "failed to flush buffers: %d", res); 1792 1793 /* figure out which firmware/emulator version this hardware has */ 1794 mceusb_get_emulator_version(ir); 1795 1796 /* initialize device */ 1797 if (ir->flags.microsoft_gen1) 1798 mceusb_gen1_init(ir); 1799 else if (!is_gen3) 1800 mceusb_gen2_init(ir); 1801 1802 mceusb_get_parameters(ir); 1803 1804 mceusb_flash_led(ir); 1805 1806 if (!ir->flags.no_tx) 1807 mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK); 1808 1809 usb_set_intfdata(intf, ir); 1810 1811 /* enable wake via this device */ 1812 device_set_wakeup_capable(ir->dev, true); 1813 device_set_wakeup_enable(ir->dev, true); 1814 1815 dev_info(&intf->dev, "Registered %s with mce emulator interface version %x", 1816 name, ir->emver); 1817 dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)", 1818 ir->num_txports, ir->txports_cabled, 1819 ir->num_rxports, ir->rxports_active); 1820 1821 return 0; 1822 1823 /* Error-handling path */ 1824 rc_dev_fail: 1825 cancel_work_sync(&ir->kevent); 1826 usb_put_dev(ir->usbdev); 1827 usb_kill_urb(ir->urb_in); 1828 usb_free_urb(ir->urb_in); 1829 urb_in_alloc_fail: 1830 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in); 1831 buf_in_alloc_fail: 1832 kfree(ir); 1833 mem_alloc_fail: 1834 dev_err(&intf->dev, "%s: device setup failed!", __func__); 1835 1836 return -ENOMEM; 1837 } 1838 1839 1840 static void mceusb_dev_disconnect(struct usb_interface *intf) 1841 { 1842 struct usb_device *dev = interface_to_usbdev(intf); 1843 struct mceusb_dev *ir = usb_get_intfdata(intf); 1844 1845 dev_dbg(&intf->dev, "%s called", __func__); 1846 1847 usb_set_intfdata(intf, NULL); 1848 1849 if (!ir) 1850 return; 1851 1852 ir->usbdev = NULL; 1853 cancel_work_sync(&ir->kevent); 1854 rc_unregister_device(ir->rc); 1855 usb_kill_urb(ir->urb_in); 1856 usb_free_urb(ir->urb_in); 1857 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in); 1858 usb_put_dev(dev); 1859 1860 kfree(ir); 1861 } 1862 1863 static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message) 1864 { 1865 struct mceusb_dev *ir = usb_get_intfdata(intf); 1866 dev_info(ir->dev, "suspend"); 1867 usb_kill_urb(ir->urb_in); 1868 return 0; 1869 } 1870 1871 static int mceusb_dev_resume(struct usb_interface *intf) 1872 { 1873 struct mceusb_dev *ir = usb_get_intfdata(intf); 1874 dev_info(ir->dev, "resume"); 1875 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC)) 1876 return -EIO; 1877 return 0; 1878 } 1879 1880 static struct usb_driver mceusb_dev_driver = { 1881 .name = DRIVER_NAME, 1882 .probe = mceusb_dev_probe, 1883 .disconnect = mceusb_dev_disconnect, 1884 .suspend = mceusb_dev_suspend, 1885 .resume = mceusb_dev_resume, 1886 .reset_resume = mceusb_dev_resume, 1887 .id_table = mceusb_dev_table 1888 }; 1889 1890 module_usb_driver(mceusb_dev_driver); 1891 1892 MODULE_DESCRIPTION(DRIVER_DESC); 1893 MODULE_AUTHOR(DRIVER_AUTHOR); 1894 MODULE_LICENSE("GPL"); 1895 MODULE_DEVICE_TABLE(usb, mceusb_dev_table); 1896