mceusb.c 42.3 KB
Newer Older
1 2 3
/*
 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
 *
4
 * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com>
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
 * Conti, Martin Blatter and Daniel Melander, the latter of which was
 * in turn also based on the lirc_atiusb driver by Paul Miller. The
 * two mce drivers were merged into one by Jarod Wilson, with transmit
 * support for the 1st-gen device added primarily by Patrick Calhoun,
 * with a bit of tweaks by Jarod. Debugging improvements and proper
 * support for what appears to be 3rd-gen hardware added by Jarod.
 * Initial port from lirc driver to ir-core drivery by Jarod, based
 * partially on a port to an earlier proposed IR infrastructure by
 * Jon Smirl, which included enhancements and simplifications to the
 * incoming IR buffer parsing routines.
 *
18 19 20 21 22
 * Updated in July of 2011 with the aid of Microsoft's official
 * remote/transceiver requirements and specification document, found at
 * download.microsoft.com, title
 * Windows-Media-Center-RC-IR-Collection-Green-Button-Specification-03-08-2011-V2.pdf
 *
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/slab.h>
43 44
#include <linux/usb.h>
#include <linux/usb/input.h>
45
#include <linux/pm_wakeup.h>
46
#include <media/rc-core.h>
47

48 49
#define DRIVER_VERSION	"1.92"
#define DRIVER_AUTHOR	"Jarod Wilson <jarod@redhat.com>"
50 51 52 53
#define DRIVER_DESC	"Windows Media Center Ed. eHome Infrared Transceiver " \
			"device driver"
#define DRIVER_NAME	"mceusb"

54 55 56
#define USB_BUFLEN		32 /* USB reception buffer length */
#define USB_CTRL_MSG_SZ		2  /* Size of usb ctrl msg on gen1 hw */
#define MCE_G1_INIT_MSGS	40 /* Init messages on gen1 hw to throw out */
57 58

/* MCE constants */
59 60 61 62 63 64 65 66 67 68 69 70
#define MCE_CMDBUF_SIZE		384  /* MCE Command buffer length */
#define MCE_TIME_UNIT		50   /* Approx 50us resolution */
#define MCE_CODE_LENGTH		5    /* Normal length of packet (with header) */
#define MCE_PACKET_SIZE		4    /* Normal length of packet (without header) */
#define MCE_IRDATA_HEADER	0x84 /* Actual header format is 0x80 + num_bytes */
#define MCE_IRDATA_TRAILER	0x80 /* End of IR data */
#define MCE_MAX_CHANNELS	2    /* Two transmitters, hardware dependent? */
#define MCE_DEFAULT_TX_MASK	0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
#define MCE_PULSE_BIT		0x80 /* Pulse bit, MSB set == PULSE else SPACE */
#define MCE_PULSE_MASK		0x7f /* Pulse mask */
#define MCE_MAX_PULSE_LENGTH	0x7f /* Longest transmittable pulse symbol */

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
 * The interface between the host and the IR hardware is command-response
 * based. All commands and responses have a consistent format, where a lead
 * byte always identifies the type of data following it. The lead byte has
 * a port value in the 3 highest bits and a length value in the 5 lowest
 * bits.
 *
 * The length field is overloaded, with a value of 11111 indicating that the
 * following byte is a command or response code, and the length of the entire
 * message is determined by the code. If the length field is not 11111, then
 * it specifies the number of bytes of port data that follow.
 */
#define MCE_CMD			0x1f
#define MCE_PORT_IR		0x4	/* (0x4 << 5) | MCE_CMD = 0x9f */
#define MCE_PORT_SYS		0x7	/* (0x7 << 5) | MCE_CMD = 0xff */
#define MCE_PORT_SER		0x6	/* 0xc0 thru 0xdf flush & 0x1f bytes */
87
#define MCE_PORT_MASK		0xe0	/* Mask out command bits */
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

/* Command port headers */
#define MCE_CMD_PORT_IR		0x9f	/* IR-related cmd/rsp */
#define MCE_CMD_PORT_SYS	0xff	/* System (non-IR) device cmd/rsp */

/* Commands that set device state  (2-4 bytes in length) */
#define MCE_CMD_RESET		0xfe	/* Reset device, 2 bytes */
#define MCE_CMD_RESUME		0xaa	/* Resume device after error, 2 bytes */
#define MCE_CMD_SETIRCFS	0x06	/* Set tx carrier, 4 bytes */
#define MCE_CMD_SETIRTIMEOUT	0x0c	/* Set timeout, 4 bytes */
#define MCE_CMD_SETIRTXPORTS	0x08	/* Set tx ports, 3 bytes */
#define MCE_CMD_SETIRRXPORTEN	0x14	/* Set rx ports, 3 bytes */
#define MCE_CMD_FLASHLED	0x23	/* Flash receiver LED, 2 bytes */

/* Commands that query device state (all 2 bytes, unless noted) */
#define MCE_CMD_GETIRCFS	0x07	/* Get carrier */
#define MCE_CMD_GETIRTIMEOUT	0x0d	/* Get timeout */
#define MCE_CMD_GETIRTXPORTS	0x13	/* Get tx ports */
#define MCE_CMD_GETIRRXPORTEN	0x15	/* Get rx ports */
#define MCE_CMD_GETPORTSTATUS	0x11	/* Get tx port status, 3 bytes */
#define MCE_CMD_GETIRNUMPORTS	0x16	/* Get number of ports */
#define MCE_CMD_GETWAKESOURCE	0x17	/* Get wake source */
#define MCE_CMD_GETEMVER	0x22	/* Get emulator interface version */
#define MCE_CMD_GETDEVDETAILS	0x21	/* Get device details (em ver2 only) */
#define MCE_CMD_GETWAKESUPPORT	0x20	/* Get wake details (em ver2 only) */
#define MCE_CMD_GETWAKEVERSION	0x18	/* Get wake pattern (em ver2 only) */

/* Misc commands */
#define MCE_CMD_NOP		0xff	/* No operation */

/* Responses to commands (non-error cases) */
#define MCE_RSP_EQIRCFS		0x06	/* tx carrier, 4 bytes */
#define MCE_RSP_EQIRTIMEOUT	0x0c	/* rx timeout, 4 bytes */
#define MCE_RSP_GETWAKESOURCE	0x17	/* wake source, 3 bytes */
#define MCE_RSP_EQIRTXPORTS	0x08	/* tx port mask, 3 bytes */
#define MCE_RSP_EQIRRXPORTEN	0x14	/* rx port mask, 3 bytes */
#define MCE_RSP_GETPORTSTATUS	0x11	/* tx port status, 7 bytes */
#define MCE_RSP_EQIRRXCFCNT	0x15	/* rx carrier count, 4 bytes */
#define MCE_RSP_EQIRNUMPORTS	0x16	/* number of ports, 4 bytes */
#define MCE_RSP_EQWAKESUPPORT	0x20	/* wake capabilities, 3 bytes */
#define MCE_RSP_EQWAKEVERSION	0x18	/* wake pattern details, 6 bytes */
#define MCE_RSP_EQDEVDETAILS	0x21	/* device capabilities, 3 bytes */
#define MCE_RSP_EQEMVER		0x22	/* emulator interface ver, 3 bytes */
#define MCE_RSP_FLASHLED	0x23	/* success flashing LED, 2 bytes */

/* Responses to error cases, must send MCE_CMD_RESUME to clear them */
#define MCE_RSP_CMD_ILLEGAL	0xfe	/* illegal command for port, 2 bytes */
#define MCE_RSP_TX_TIMEOUT	0x81	/* tx timed out, 2 bytes */

/* Misc commands/responses not defined in the MCE remote/transceiver spec */
138
#define MCE_CMD_SIG_END		0x01	/* End of signal */
139 140 141 142 143 144 145 146 147 148
#define MCE_CMD_PING		0x03	/* Ping device */
#define MCE_CMD_UNKNOWN		0x04	/* Unknown */
#define MCE_CMD_UNKNOWN2	0x05	/* Unknown */
#define MCE_CMD_UNKNOWN3	0x09	/* Unknown */
#define MCE_CMD_UNKNOWN4	0x0a	/* Unknown */
#define MCE_CMD_G_REVISION	0x0b	/* Get hw/sw revision */
#define MCE_CMD_UNKNOWN5	0x0e	/* Unknown */
#define MCE_CMD_UNKNOWN6	0x0f	/* Unknown */
#define MCE_CMD_UNKNOWN8	0x19	/* Unknown */
#define MCE_CMD_UNKNOWN9	0x1b	/* Unknown */
149
#define MCE_CMD_NULL		0x00	/* These show up various places... */
150

151 152 153 154
/* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
 * then we're looking at a raw IR data sample */
#define MCE_COMMAND_IRDATA	0x80
#define MCE_PACKET_LENGTH_MASK	0x1f /* Packet length mask */
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

/* general constants */
#define SEND_FLAG_IN_PROGRESS	1
#define SEND_FLAG_COMPLETE	2
#define RECV_FLAG_IN_PROGRESS	3
#define RECV_FLAG_COMPLETE	4

#define MCEUSB_RX		1
#define MCEUSB_TX		2

#define VENDOR_PHILIPS		0x0471
#define VENDOR_SMK		0x0609
#define VENDOR_TATUNG		0x1460
#define VENDOR_GATEWAY		0x107b
#define VENDOR_SHUTTLE		0x1308
#define VENDOR_SHUTTLE2		0x051c
#define VENDOR_MITSUMI		0x03ee
#define VENDOR_TOPSEED		0x1784
#define VENDOR_RICAVISION	0x179d
#define VENDOR_ITRON		0x195d
#define VENDOR_FIC		0x1509
#define VENDOR_LG		0x043e
#define VENDOR_MICROSOFT	0x045e
#define VENDOR_FORMOSA		0x147a
#define VENDOR_FINTEK		0x1934
#define VENDOR_PINNACLE		0x2304
#define VENDOR_ECS		0x1019
#define VENDOR_WISTRON		0x0fb8
#define VENDOR_COMPRO		0x185b
#define VENDOR_NORTHSTAR	0x04eb
#define VENDOR_REALTEK		0x0bda
#define VENDOR_TIVO		0x105a
187
#define VENDOR_CONEXANT		0x0572
188
#define VENDOR_TWISTEDMELON	0x2596
189
#define VENDOR_HAUPPAUGE	0x2040
190
#define VENDOR_PCTV		0x2013
191
#define VENDOR_ADAPTEC		0x03f3
192

193 194 195 196 197 198
enum mceusb_model_type {
	MCE_GEN2 = 0,		/* Most boards */
	MCE_GEN1,
	MCE_GEN3,
	MCE_GEN2_TX_INV,
	POLARIS_EVK,
199
	CX_HYBRID_TV,
200
	MULTIFUNCTION,
201
	TIVO_KIT,
202
	MCE_GEN2_NO_TX,
203
	HAUPPAUGE_CX_HYBRID_TV,
204 205 206 207 208 209
};

struct mceusb_model {
	u32 mce_gen1:1;
	u32 mce_gen2:1;
	u32 mce_gen3:1;
210
	u32 tx_mask_normal:1;
211
	u32 no_tx:1;
212

213 214
	int ir_intfnum;

215
	const char *rc_map;	/* Allow specify a per-board map */
216
	const char *name;	/* per-board name */
217 218 219 220 221
};

static const struct mceusb_model mceusb_model[] = {
	[MCE_GEN1] = {
		.mce_gen1 = 1,
222
		.tx_mask_normal = 1,
223 224 225 226
	},
	[MCE_GEN2] = {
		.mce_gen2 = 1,
	},
227 228 229 230
	[MCE_GEN2_NO_TX] = {
		.mce_gen2 = 1,
		.no_tx = 1,
	},
231 232
	[MCE_GEN2_TX_INV] = {
		.mce_gen2 = 1,
233
		.tx_mask_normal = 1,
234 235 236
	},
	[MCE_GEN3] = {
		.mce_gen3 = 1,
237
		.tx_mask_normal = 1,
238 239
	},
	[POLARIS_EVK] = {
240 241 242 243 244
		/*
		 * In fact, the EVK is shipped without
		 * remotes, but we should have something handy,
		 * to allow testing it
		 */
245 246 247 248 249
		.name = "Conexant Hybrid TV (cx231xx) MCE IR",
	},
	[CX_HYBRID_TV] = {
		.no_tx = 1, /* tx isn't wired up at all */
		.name = "Conexant Hybrid TV (cx231xx) MCE IR",
250
	},
251 252 253 254
	[HAUPPAUGE_CX_HYBRID_TV] = {
		.no_tx = 1, /* eeprom says it has no tx */
		.name = "Conexant Hybrid TV (cx231xx) MCE IR no TX",
	},
255 256 257 258
	[MULTIFUNCTION] = {
		.mce_gen2 = 1,
		.ir_intfnum = 2,
	},
259 260 261 262
	[TIVO_KIT] = {
		.mce_gen2 = 1,
		.rc_map = RC_MAP_TIVO,
	},
263 264
};

265 266
static struct usb_device_id mceusb_dev_table[] = {
	/* Original Microsoft MCE IR Transceiver (often HP-branded) */
267 268
	{ USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
	  .driver_info = MCE_GEN1 },
269 270 271
	/* Philips Infrared Transceiver - Sahara branded */
	{ USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
	/* Philips Infrared Transceiver - HP branded */
272 273
	{ USB_DEVICE(VENDOR_PHILIPS, 0x060c),
	  .driver_info = MCE_GEN2_TX_INV },
274 275 276 277 278 279 280 281
	/* Philips SRM5100 */
	{ USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
	/* Philips Infrared Transceiver - Omaura */
	{ USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
	/* Philips Infrared Transceiver - Spinel plus */
	{ USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
	/* Philips eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
282 283 284 285
	/* Philips/Spinel plus IR transceiver for ASUS */
	{ USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
	/* Philips/Spinel plus IR transceiver for ASUS */
	{ USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
286
	/* Philips IR transceiver (Dell branded) */
287 288
	{ USB_DEVICE(VENDOR_PHILIPS, 0x2093),
	  .driver_info = MCE_GEN2_TX_INV },
289 290 291
	/* Realtek MCE IR Receiver and card reader */
	{ USB_DEVICE(VENDOR_REALTEK, 0x0161),
	  .driver_info = MULTIFUNCTION },
292
	/* SMK/Toshiba G83C0004D410 */
293 294
	{ USB_DEVICE(VENDOR_SMK, 0x031d),
	  .driver_info = MCE_GEN2_TX_INV },
295
	/* SMK eHome Infrared Transceiver (Sony VAIO) */
296 297
	{ USB_DEVICE(VENDOR_SMK, 0x0322),
	  .driver_info = MCE_GEN2_TX_INV },
298
	/* bundled with Hauppauge PVR-150 */
299 300
	{ USB_DEVICE(VENDOR_SMK, 0x0334),
	  .driver_info = MCE_GEN2_TX_INV },
301 302
	/* SMK eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_SMK, 0x0338) },
303 304 305
	/* SMK/I-O Data GV-MC7/RCKIT Receiver */
	{ USB_DEVICE(VENDOR_SMK, 0x0353),
	  .driver_info = MCE_GEN2_NO_TX },
306 307 308
	/* SMK RXX6000 Infrared Receiver */
	{ USB_DEVICE(VENDOR_SMK, 0x0357),
	  .driver_info = MCE_GEN2_NO_TX },
309 310 311 312 313 314 315 316 317 318 319
	/* Tatung eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_TATUNG, 0x9150) },
	/* Shuttle eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
	/* Shuttle eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
	/* Gateway eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
	/* Mitsumi */
	{ USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
	/* Topseed eHome Infrared Transceiver */
320 321
	{ USB_DEVICE(VENDOR_TOPSEED, 0x0001),
	  .driver_info = MCE_GEN2_TX_INV },
322
	/* Topseed HP eHome Infrared Transceiver */
323 324
	{ USB_DEVICE(VENDOR_TOPSEED, 0x0006),
	  .driver_info = MCE_GEN2_TX_INV },
325
	/* Topseed eHome Infrared Transceiver */
326 327
	{ USB_DEVICE(VENDOR_TOPSEED, 0x0007),
	  .driver_info = MCE_GEN2_TX_INV },
328
	/* Topseed eHome Infrared Transceiver */
329 330
	{ USB_DEVICE(VENDOR_TOPSEED, 0x0008),
	  .driver_info = MCE_GEN3 },
331
	/* Topseed eHome Infrared Transceiver */
332 333
	{ USB_DEVICE(VENDOR_TOPSEED, 0x000a),
	  .driver_info = MCE_GEN2_TX_INV },
334
	/* Topseed eHome Infrared Transceiver */
335
	{ USB_DEVICE(VENDOR_TOPSEED, 0x0011),
336
	  .driver_info = MCE_GEN3 },
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	/* Ricavision internal Infrared Transceiver */
	{ USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
	/* Itron ione Libra Q-11 */
	{ USB_DEVICE(VENDOR_ITRON, 0x7002) },
	/* FIC eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_FIC, 0x9242) },
	/* LG eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_LG, 0x9803) },
	/* Microsoft MCE Infrared Transceiver */
	{ USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
	/* Formosa eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
	/* Formosa21 / eHome Infrared Receiver */
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
	/* Formosa aim / Trust MCE Infrared Receiver */
352 353
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe017),
	  .driver_info = MCE_GEN2_NO_TX },
354 355 356 357 358 359 360 361
	/* Formosa Industrial Computing / Beanbag Emulation Device */
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
	/* Formosa21 / eHome Infrared Receiver */
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
	/* Formosa Industrial Computing AIM IR605/A */
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
	/* Formosa Industrial Computing */
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
362 363
	/* Formosa Industrial Computing */
	{ USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
364
	/* Fintek eHome Infrared Transceiver (HP branded) */
365 366
	{ USB_DEVICE(VENDOR_FINTEK, 0x5168),
	  .driver_info = MCE_GEN2_TX_INV },
367 368 369 370 371
	/* Fintek eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_FINTEK, 0x0602) },
	/* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
	{ USB_DEVICE(VENDOR_FINTEK, 0x0702) },
	/* Pinnacle Remote Kit */
372 373
	{ USB_DEVICE(VENDOR_PINNACLE, 0x0225),
	  .driver_info = MCE_GEN3 },
374 375 376 377 378 379 380 381 382 383 384
	/* Elitegroup Computer Systems IR */
	{ USB_DEVICE(VENDOR_ECS, 0x0f38) },
	/* Wistron Corp. eHome Infrared Receiver */
	{ USB_DEVICE(VENDOR_WISTRON, 0x0002) },
	/* Compro K100 */
	{ USB_DEVICE(VENDOR_COMPRO, 0x3020) },
	/* Compro K100 v2 */
	{ USB_DEVICE(VENDOR_COMPRO, 0x3082) },
	/* Northstar Systems, Inc. eHome Infrared Transceiver */
	{ USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
	/* TiVo PC IR Receiver */
385 386
	{ USB_DEVICE(VENDOR_TIVO, 0x2000),
	  .driver_info = TIVO_KIT },
387
	/* Conexant Hybrid TV "Shelby" Polaris SDK */
388 389
	{ USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
	  .driver_info = POLARIS_EVK },
390 391 392
	/* Conexant Hybrid TV RDU253S Polaris */
	{ USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
	  .driver_info = CX_HYBRID_TV },
393 394 395 396 397 398
	/* Twisted Melon Inc. - Manta Mini Receiver */
	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
	/* Twisted Melon Inc. - Manta Pico Receiver */
	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
	/* Twisted Melon Inc. - Manta Transceiver */
	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
399 400 401
	/* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb130),
	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
402 403
	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb131),
	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
404 405 406 407
	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb138),
	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb139),
	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
408 409 410 411
	{ USB_DEVICE(VENDOR_PCTV, 0x0259),
	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
	{ USB_DEVICE(VENDOR_PCTV, 0x025e),
	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
412 413
	/* Adaptec / HP eHome Receiver */
	{ USB_DEVICE(VENDOR_ADAPTEC, 0x0094) },
414

415 416 417 418 419 420 421
	/* Terminating entry */
	{ }
};

/* data structure for each usb transceiver */
struct mceusb_dev {
	/* ir-core bits */
422
	struct rc_dev *rc;
423 424 425 426

	/* optional features we can enable */
	bool carrier_report_enabled;
	bool learning_enabled;
427 428 429 430 431 432 433 434 435 436 437 438

	/* core device bits */
	struct device *dev;

	/* usb */
	struct usb_device *usbdev;
	struct urb *urb_in;
	struct usb_endpoint_descriptor *usb_ep_out;

	/* buffers and dma */
	unsigned char *buf_in;
	unsigned int len_in;
439
	dma_addr_t dma_in;
440 441 442 443 444 445 446 447

	enum {
		CMD_HEADER = 0,
		SUBCMD,
		CMD_DATA,
		PARSE_IRDATA,
	} parser_state;

448
	u8 cmd, rem;		/* Remaining IR data bytes in packet */
449 450 451

	struct {
		u32 connected:1;
452
		u32 tx_mask_normal:1;
453
		u32 microsoft_gen1:1;
454
		u32 no_tx:1;
455 456
	} flags;

457
	/* transmit support */
458
	int send_flags;
459 460
	u32 carrier;
	unsigned char tx_mask;
461 462 463

	char name[128];
	char phys[64];
464
	enum mceusb_model_type model;
465 466

	bool need_reset;	/* flag to issue a device resume cmd */
467
	u8 emver;		/* emulator interface version */
468 469 470 471
	u8 num_txports;		/* number of transmit ports */
	u8 num_rxports;		/* number of receive sensors */
	u8 txports_cabled;	/* bitmask of transmitters with cable */
	u8 rxports_active;	/* bitmask of active receive sensors */
472 473
};

474 475 476 477
/* MCE Device Command Strings, generally a port and command pair */
static char DEVICE_RESUME[]	= {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
				   MCE_CMD_RESUME};
static char GET_REVISION[]	= {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
478
static char GET_EMVER[]		= {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
479
static char GET_WAKEVERSION[]	= {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
480
static char FLASH_LED[]		= {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
481 482 483
static char GET_UNKNOWN2[]	= {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
static char GET_CARRIER_FREQ[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
static char GET_RX_TIMEOUT[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
484
static char GET_NUM_PORTS[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
485 486
static char GET_TX_BITMASK[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
static char GET_RX_SENSOR[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
487 488
/* sub in desired values in lower byte or bytes for full command */
/* FIXME: make use of these for transmit.
489 490 491 492 493 494 495
static char SET_CARRIER_FREQ[]	= {MCE_CMD_PORT_IR,
				   MCE_CMD_SETIRCFS, 0x00, 0x00};
static char SET_TX_BITMASK[]	= {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
static char SET_RX_TIMEOUT[]	= {MCE_CMD_PORT_IR,
				   MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
static char SET_RX_SENSOR[]	= {MCE_CMD_PORT_IR,
				   MCE_RSP_EQIRRXPORTEN, 0x00};
496 497
*/

498
static int mceusb_cmd_datasize(u8 cmd, u8 subcmd)
499 500 501 502
{
	int datasize = 0;

	switch (cmd) {
503 504
	case MCE_CMD_NULL:
		if (subcmd == MCE_CMD_PORT_SYS)
505 506
			datasize = 1;
		break;
507
	case MCE_CMD_PORT_SYS:
508
		switch (subcmd) {
509 510 511
		case MCE_RSP_GETPORTSTATUS:
			datasize = 5;
			break;
512 513 514
		case MCE_RSP_EQWAKEVERSION:
			datasize = 4;
			break;
515
		case MCE_CMD_G_REVISION:
516 517
			datasize = 2;
			break;
518
		case MCE_RSP_EQWAKESUPPORT:
519 520 521
		case MCE_RSP_GETWAKESOURCE:
		case MCE_RSP_EQDEVDETAILS:
		case MCE_RSP_EQEMVER:
522 523
			datasize = 1;
			break;
524
		}
525
	case MCE_CMD_PORT_IR:
526
		switch (subcmd) {
527
		case MCE_CMD_UNKNOWN:
528 529 530
		case MCE_RSP_EQIRCFS:
		case MCE_RSP_EQIRTIMEOUT:
		case MCE_RSP_EQIRRXCFCNT:
531
		case MCE_RSP_EQIRNUMPORTS:
532 533
			datasize = 2;
			break;
534
		case MCE_CMD_SIG_END:
535 536
		case MCE_RSP_EQIRTXPORTS:
		case MCE_RSP_EQIRRXPORTEN:
537 538 539 540 541 542 543
			datasize = 1;
			break;
		}
	}
	return datasize;
}

544
static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
545
				 int offset, int len, bool out)
546
{
547 548
#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
	char *inout;
549
	u8 cmd, subcmd, data1, data2, data3, data4;
550
	struct device *dev = ir->dev;
551
	int start, skip = 0;
552
	u32 carrier, period;
553

554
	/* skip meaningless 0xb1 0x60 header bytes on orig receiver */
555
	if (ir->flags.microsoft_gen1 && !out && !offset)
556
		skip = 2;
557

558
	if (len <= skip)
559 560
		return;

561 562
	dev_dbg(dev, "%cx data: %*ph (length=%d)",
		(out ? 't' : 'r'), min(len, USB_BUFLEN), buf, len);
563

564
	inout = out ? "Request" : "Got";
565

566 567 568 569 570
	start  = offset + skip;
	cmd    = buf[start] & 0xff;
	subcmd = buf[start + 1] & 0xff;
	data1  = buf[start + 2] & 0xff;
	data2  = buf[start + 3] & 0xff;
571 572
	data3  = buf[start + 4] & 0xff;
	data4  = buf[start + 5] & 0xff;
573 574

	switch (cmd) {
575
	case MCE_CMD_NULL:
576 577
		if (subcmd == MCE_CMD_NULL)
			break;
578 579
		if ((subcmd == MCE_CMD_PORT_SYS) &&
		    (data1 == MCE_CMD_RESUME))
580
			dev_dbg(dev, "Device resume requested");
581
		else
582
			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
583 584
				 cmd, subcmd);
		break;
585
	case MCE_CMD_PORT_SYS:
586
		switch (subcmd) {
587 588
		case MCE_RSP_EQEMVER:
			if (!out)
589
				dev_dbg(dev, "Emulator interface version %x",
590 591
					 data1);
			break;
592
		case MCE_CMD_G_REVISION:
593
			if (len == 2)
594
				dev_dbg(dev, "Get hw/sw rev?");
595
			else
596 597
				dev_dbg(dev, "hw/sw rev %*ph",
					4, &buf[start + 2]);
598
			break;
599
		case MCE_CMD_RESUME:
600
			dev_dbg(dev, "Device resume requested");
601 602
			break;
		case MCE_RSP_CMD_ILLEGAL:
603
			dev_dbg(dev, "Illegal PORT_SYS command");
604 605 606
			break;
		case MCE_RSP_EQWAKEVERSION:
			if (!out)
607
				dev_dbg(dev, "Wake version, proto: 0x%02x, "
608
					 "payload: 0x%02x, address: 0x%02x, "
609
					 "version: 0x%02x",
610
					 data1, data2, data3, data4);
611
			break;
612 613 614
		case MCE_RSP_GETPORTSTATUS:
			if (!out)
				/* We use data1 + 1 here, to match hw labels */
615
				dev_dbg(dev, "TX port %d: blaster is%s connected",
616
					 data1 + 1, data4 ? " not" : "");
617
			break;
618
		case MCE_CMD_FLASHLED:
619
			dev_dbg(dev, "Attempting to flash LED");
620
			break;
621
		default:
622
			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
623 624 625 626
				 cmd, subcmd);
			break;
		}
		break;
627
	case MCE_CMD_PORT_IR:
628
		switch (subcmd) {
629
		case MCE_CMD_SIG_END:
630
			dev_dbg(dev, "End of signal");
631
			break;
632
		case MCE_CMD_PING:
633
			dev_dbg(dev, "Ping");
634
			break;
635
		case MCE_CMD_UNKNOWN:
636
			dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x",
637 638
				 data1, data2);
			break;
639 640
		case MCE_RSP_EQIRCFS:
			period = DIV_ROUND_CLOSEST(
641
					(1U << data1 * 2) * (data2 + 1), 10);
642 643 644
			if (!period)
				break;
			carrier = (1000 * 1000) / period;
645
			dev_dbg(dev, "%s carrier of %u Hz (period %uus)",
646
				 inout, carrier, period);
647
			break;
648
		case MCE_CMD_GETIRCFS:
649
			dev_dbg(dev, "Get carrier mode and freq");
650
			break;
651
		case MCE_RSP_EQIRTXPORTS:
652
			dev_dbg(dev, "%s transmit blaster mask of 0x%02x",
653 654
				 inout, data1);
			break;
655
		case MCE_RSP_EQIRTIMEOUT:
656
			/* value is in units of 50us, so x*50/1000 ms */
657
			period = ((data1 << 8) | data2) * MCE_TIME_UNIT / 1000;
658
			dev_dbg(dev, "%s receive timeout of %d ms",
659
				 inout, period);
660
			break;
661
		case MCE_CMD_GETIRTIMEOUT:
662
			dev_dbg(dev, "Get receive timeout");
663
			break;
664
		case MCE_CMD_GETIRTXPORTS:
665
			dev_dbg(dev, "Get transmit blaster mask");
666
			break;
667
		case MCE_RSP_EQIRRXPORTEN:
668
			dev_dbg(dev, "%s %s-range receive sensor in use",
669 670
				 inout, data1 == 0x02 ? "short" : "long");
			break;
671 672
		case MCE_CMD_GETIRRXPORTEN:
		/* aka MCE_RSP_EQIRRXCFCNT */
673
			if (out)
674
				dev_dbg(dev, "Get receive sensor");
675
			else if (ir->learning_enabled)
676
				dev_dbg(dev, "RX pulse count: %d",
677 678
					 ((data1 << 8) | data2));
			break;
679 680 681
		case MCE_RSP_EQIRNUMPORTS:
			if (out)
				break;
682
			dev_dbg(dev, "Num TX ports: %x, num RX ports: %x",
683 684 685
				 data1, data2);
			break;
		case MCE_RSP_CMD_ILLEGAL:
686
			dev_dbg(dev, "Illegal PORT_IR command");
687 688
			break;
		default:
689
			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
690 691 692 693 694 695 696
				 cmd, subcmd);
			break;
		}
		break;
	default:
		break;
	}
697 698

	if (cmd == MCE_IRDATA_TRAILER)
699
		dev_dbg(dev, "End of raw IR data");
700 701
	else if ((cmd != MCE_CMD_PORT_IR) &&
		 ((cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA))
702 703
		dev_dbg(dev, "Raw IR data, %d pulse/space samples", ir->rem);
#endif
704 705
}

S
Sean Young 已提交
706
static void mce_async_callback(struct urb *urb)
707 708 709 710 711 712 713 714
{
	struct mceusb_dev *ir;
	int len;

	if (!urb)
		return;

	ir = urb->context;
715 716 717 718

	switch (urb->status) {
	/* success */
	case 0:
719 720
		len = urb->actual_length;

721
		mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
722 723 724 725 726 727 728 729 730 731 732 733
		break;

	case -ECONNRESET:
	case -ENOENT:
	case -EILSEQ:
	case -ESHUTDOWN:
		break;

	case -EPIPE:
	default:
		dev_err(ir->dev, "Error: request urb status = %d", urb->status);
		break;
734 735
	}

736 737 738
	/* the transfer buffer and urb were allocated in mce_request_packet */
	kfree(urb->transfer_buffer);
	usb_free_urb(urb);
739 740 741
}

/* request incoming or send outgoing usb packet - used to initialize remote */
742 743
static void mce_request_packet(struct mceusb_dev *ir, unsigned char *data,
			       int size, int urb_type)
744
{
745
	int res, pipe;
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	struct urb *async_urb;
	struct device *dev = ir->dev;
	unsigned char *async_buf;

	if (urb_type == MCEUSB_TX) {
		async_urb = usb_alloc_urb(0, GFP_KERNEL);
		if (unlikely(!async_urb)) {
			dev_err(dev, "Error, couldn't allocate urb!\n");
			return;
		}

		async_buf = kzalloc(size, GFP_KERNEL);
		if (!async_buf) {
			dev_err(dev, "Error, couldn't allocate buf!\n");
			usb_free_urb(async_urb);
			return;
		}

		/* outbound data */
765 766 767 768 769 770 771 772 773 774 775 776 777
		if (usb_endpoint_xfer_int(ir->usb_ep_out)) {
			pipe = usb_sndintpipe(ir->usbdev,
					 ir->usb_ep_out->bEndpointAddress);
			usb_fill_int_urb(async_urb, ir->usbdev, pipe, async_buf,
					 size, mce_async_callback, ir,
					 ir->usb_ep_out->bInterval);
		} else {
			pipe = usb_sndbulkpipe(ir->usbdev,
					 ir->usb_ep_out->bEndpointAddress);
			usb_fill_bulk_urb(async_urb, ir->usbdev, pipe,
					 async_buf, size, mce_async_callback,
					 ir);
		}
778 779 780 781 782 783 784 785 786 787 788 789
		memcpy(async_buf, data, size);

	} else if (urb_type == MCEUSB_RX) {
		/* standard request */
		async_urb = ir->urb_in;
		ir->send_flags = RECV_FLAG_IN_PROGRESS;

	} else {
		dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
		return;
	}

790
	dev_dbg(dev, "receive request called (size=%#x)", size);
791 792 793 794 795 796

	async_urb->transfer_buffer_length = size;
	async_urb->dev = ir->usbdev;

	res = usb_submit_urb(async_urb, GFP_ATOMIC);
	if (res) {
797
		dev_err(dev, "receive request FAILED! (res=%d)", res);
798 799
		return;
	}
800
	dev_dbg(dev, "receive request complete (res=%d)", res);
801 802 803 804
}

static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
{
805 806 807 808 809 810 811 812
	int rsize = sizeof(DEVICE_RESUME);

	if (ir->need_reset) {
		ir->need_reset = false;
		mce_request_packet(ir, DEVICE_RESUME, rsize, MCEUSB_TX);
		msleep(10);
	}

813
	mce_request_packet(ir, data, size, MCEUSB_TX);
814
	msleep(10);
815 816
}

817
static void mce_flush_rx_buffer(struct mceusb_dev *ir, int size)
818
{
819
	mce_request_packet(ir, NULL, size, MCEUSB_RX);
820 821
}

822
/* Send data out the IR blaster port(s) */
823
static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
824
{
825
	struct mceusb_dev *ir = dev->priv;
826
	int i, length, ret = 0;
827
	int cmdcount = 0;
828
	unsigned char cmdbuf[MCE_CMDBUF_SIZE];
829 830

	/* MCE tx init header */
831 832
	cmdbuf[cmdcount++] = MCE_CMD_PORT_IR;
	cmdbuf[cmdcount++] = MCE_CMD_SETIRTXPORTS;
833 834
	cmdbuf[cmdcount++] = ir->tx_mask;

835 836 837 838
	/* Send the set TX ports command */
	mce_async_out(ir, cmdbuf, cmdcount);
	cmdcount = 0;

839 840 841 842 843 844 845 846
	/* Generate mce packet data */
	for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
		txbuf[i] = txbuf[i] / MCE_TIME_UNIT;

		do { /* loop to support long pulses/spaces > 127*50us=6.35ms */

			/* Insert mce packet header every 4th entry */
			if ((cmdcount < MCE_CMDBUF_SIZE) &&
847
			    (cmdcount % MCE_CODE_LENGTH) == 0)
848
				cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

			/* Insert mce packet data */
			if (cmdcount < MCE_CMDBUF_SIZE)
				cmdbuf[cmdcount++] =
					(txbuf[i] < MCE_PULSE_BIT ?
					 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
					 (i & 1 ? 0x00 : MCE_PULSE_BIT);
			else {
				ret = -EINVAL;
				goto out;
			}

		} while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
			 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
	}

	/* Check if we have room for the empty packet at the end */
	if (cmdcount >= MCE_CMDBUF_SIZE) {
		ret = -EINVAL;
		goto out;
	}

871 872 873 874
	/* Fix packet length in last header */
	length = cmdcount % MCE_CODE_LENGTH;
	cmdbuf[cmdcount - length] -= MCE_CODE_LENGTH - length;

875
	/* All mce commands end with an empty packet (0x80) */
876
	cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
877 878 879 880 881

	/* Transmit the command to the mce device */
	mce_async_out(ir, cmdbuf, cmdcount);

out:
882
	return ret ? ret : count;
883 884
}

885
/* Sets active IR outputs -- mce devices typically have two */
886
static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
887
{
888
	struct mceusb_dev *ir = dev->priv;
889

890 891 892 893 894 895
	/* return number of transmitters */
	int emitters = ir->num_txports ? ir->num_txports : 2;

	if (mask >= (1 << emitters))
		return emitters;

896 897 898
	if (ir->flags.tx_mask_normal)
		ir->tx_mask = mask;
	else
899 900
		ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
				mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
901 902 903 904

	return 0;
}

905
/* Sets the send carrier frequency and mode */
906
static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
907
{
908
	struct mceusb_dev *ir = dev->priv;
909 910
	int clk = 10000000;
	int prescaler = 0, divisor = 0;
911 912
	unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
				    MCE_CMD_SETIRCFS, 0x00, 0x00 };
913 914 915 916 917 918

	/* Carrier has changed */
	if (ir->carrier != carrier) {

		if (carrier == 0) {
			ir->carrier = carrier;
919
			cmdbuf[2] = MCE_CMD_SIG_END;
920
			cmdbuf[3] = MCE_IRDATA_TRAILER;
921
			dev_dbg(ir->dev, "disabling carrier modulation");
922 923 924 925 926 927
			mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
			return carrier;
		}

		for (prescaler = 0; prescaler < 4; ++prescaler) {
			divisor = (clk >> (2 * prescaler)) / carrier;
928
			if (divisor <= 0xff) {
929 930 931
				ir->carrier = carrier;
				cmdbuf[2] = prescaler;
				cmdbuf[3] = divisor;
932 933
				dev_dbg(ir->dev, "requesting %u HZ carrier",
								carrier);
934 935 936 937 938 939 940 941 942 943 944

				/* Transmit new carrier to mce device */
				mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
				return carrier;
			}
		}

		return -EINVAL;

	}

945
	return 0;
946 947
}

948 949 950 951 952 953 954 955 956 957 958
/*
 * We don't do anything but print debug spew for many of the command bits
 * we receive from the hardware, but some of them are useful information
 * we want to store so that we can use them.
 */
static void mceusb_handle_command(struct mceusb_dev *ir, int index)
{
	u8 hi = ir->buf_in[index + 1] & 0xff;
	u8 lo = ir->buf_in[index + 2] & 0xff;

	switch (ir->buf_in[index]) {
959 960 961 962 963 964
	/* the one and only 5-byte return value command */
	case MCE_RSP_GETPORTSTATUS:
		if ((ir->buf_in[index + 4] & 0xff) == 0x00)
			ir->txports_cabled |= 1 << hi;
		break;

965
	/* 2-byte return value commands */
966
	case MCE_RSP_EQIRTIMEOUT:
967
		ir->rc->timeout = US_TO_NS((hi << 8 | lo) * MCE_TIME_UNIT);
968
		break;
969 970 971 972
	case MCE_RSP_EQIRNUMPORTS:
		ir->num_txports = hi;
		ir->num_rxports = lo;
		break;
973 974

	/* 1-byte return value commands */
975 976 977
	case MCE_RSP_EQEMVER:
		ir->emver = hi;
		break;
978
	case MCE_RSP_EQIRTXPORTS:
979 980
		ir->tx_mask = hi;
		break;
981 982
	case MCE_RSP_EQIRRXPORTEN:
		ir->learning_enabled = ((hi & 0x02) == 0x02);
983
		ir->rxports_active = hi;
984
		break;
985 986 987
	case MCE_RSP_CMD_ILLEGAL:
		ir->need_reset = true;
		break;
988 989 990 991 992
	default:
		break;
	}
}

993 994
static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
{
995
	DEFINE_IR_RAW_EVENT(rawir);
996
	bool event = false;
997
	int i = 0;
998 999 1000

	/* skip meaningless 0xb1 0x60 header bytes on orig receiver */
	if (ir->flags.microsoft_gen1)
1001
		i = 2;
1002

1003 1004 1005 1006
	/* if there's no data, just return now */
	if (buf_len <= i)
		return;

1007 1008 1009
	for (; i < buf_len; i++) {
		switch (ir->parser_state) {
		case SUBCMD:
1010
			ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]);
1011 1012
			mceusb_dev_printdata(ir, ir->buf_in, i - 1,
					     ir->rem + 2, false);
1013
			mceusb_handle_command(ir, i);
1014 1015 1016
			ir->parser_state = CMD_DATA;
			break;
		case PARSE_IRDATA:
1017
			ir->rem--;
1018
			init_ir_raw_event(&rawir);
1019 1020
			rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
			rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
1021
					 * US_TO_NS(MCE_TIME_UNIT);
1022

1023
			dev_dbg(ir->dev, "Storing %s with duration %d",
1024 1025 1026
				rawir.pulse ? "pulse" : "space",
				rawir.duration);

1027 1028
			if (ir_raw_event_store_with_filter(ir->rc, &rawir))
				event = true;
1029 1030 1031 1032 1033 1034 1035 1036
			break;
		case CMD_DATA:
			ir->rem--;
			break;
		case CMD_HEADER:
			/* decode mce packets of the form (84),AA,BB,CC,DD */
			/* IR data packets can span USB messages - rem */
			ir->cmd = ir->buf_in[i];
1037 1038
			if ((ir->cmd == MCE_CMD_PORT_IR) ||
			    ((ir->cmd & MCE_PORT_MASK) !=
1039
			     MCE_COMMAND_IRDATA)) {
1040 1041 1042 1043
				ir->parser_state = SUBCMD;
				continue;
			}
			ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
1044 1045
			mceusb_dev_printdata(ir, ir->buf_in,
					     i, ir->rem + 1, false);
1046
			if (ir->rem)
1047
				ir->parser_state = PARSE_IRDATA;
1048 1049
			else
				ir_raw_event_reset(ir->rc);
1050
			break;
1051 1052
		}

1053 1054
		if (ir->parser_state != CMD_HEADER && !ir->rem)
			ir->parser_state = CMD_HEADER;
1055
	}
1056
	if (event) {
1057
		dev_dbg(ir->dev, "processed IR data");
1058 1059
		ir_raw_event_handle(ir->rc);
	}
1060 1061
}

S
Sean Young 已提交
1062
static void mceusb_dev_recv(struct urb *urb)
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
{
	struct mceusb_dev *ir;
	int buf_len;

	if (!urb)
		return;

	ir = urb->context;
	if (!ir) {
		usb_unlink_urb(urb);
		return;
	}

	buf_len = urb->actual_length;

	if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
		ir->send_flags = SEND_FLAG_COMPLETE;
1080
		dev_dbg(ir->dev, "setup answer received %d bytes\n",
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
			buf_len);
	}

	switch (urb->status) {
	/* success */
	case 0:
		mceusb_process_ir_data(ir, buf_len);
		break;

	case -ECONNRESET:
	case -ENOENT:
1092
	case -EILSEQ:
1093 1094 1095 1096 1097 1098
	case -ESHUTDOWN:
		usb_unlink_urb(urb);
		return;

	case -EPIPE:
	default:
1099
		dev_err(ir->dev, "Error: urb status = %d", urb->status);
1100 1101 1102 1103 1104 1105
		break;
	}

	usb_submit_urb(urb, GFP_ATOMIC);
}

1106 1107 1108 1109 1110 1111 1112
static void mceusb_get_emulator_version(struct mceusb_dev *ir)
{
	/* If we get no reply or an illegal command reply, its ver 1, says MS */
	ir->emver = 1;
	mce_async_out(ir, GET_EMVER, sizeof(GET_EMVER));
}

1113 1114
static void mceusb_gen1_init(struct mceusb_dev *ir)
{
1115
	int ret;
1116
	struct device *dev = ir->dev;
1117
	char *data;
1118 1119 1120

	data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
	if (!data) {
1121
		dev_err(dev, "%s: memory allocation failed!", __func__);
1122 1123
		return;
	}
1124 1125

	/*
1126
	 * This is a strange one. Windows issues a set address to the device
1127 1128 1129 1130
	 * on the receive control pipe and expect a certain value pair back
	 */
	ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
			      USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
1131
			      data, USB_CTRL_MSG_SZ, HZ * 3);
1132 1133 1134
	dev_dbg(dev, "set address - ret = %d", ret);
	dev_dbg(dev, "set address - data[0] = %d, data[1] = %d",
						data[0], data[1]);
1135 1136 1137 1138 1139 1140

	/* set feature: bit rate 38400 bps */
	ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
			      USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
			      0xc04e, 0x0000, NULL, 0, HZ * 3);

1141
	dev_dbg(dev, "set feature - ret = %d", ret);
1142 1143 1144 1145 1146

	/* bRequest 4: set char length to 8 bits */
	ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
			      4, USB_TYPE_VENDOR,
			      0x0808, 0x0000, NULL, 0, HZ * 3);
1147
	dev_dbg(dev, "set char length - retB = %d", ret);
1148 1149 1150 1151 1152

	/* bRequest 2: set handshaking to use DTR/DSR */
	ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
			      2, USB_TYPE_VENDOR,
			      0x0000, 0x0100, NULL, 0, HZ * 3);
1153
	dev_dbg(dev, "set handshake  - retC = %d", ret);
1154

1155 1156
	/* device resume */
	mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1157 1158 1159 1160

	/* get hw/sw revision? */
	mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));

1161
	kfree(data);
1162
}
1163 1164 1165

static void mceusb_gen2_init(struct mceusb_dev *ir)
{
1166 1167
	/* device resume */
	mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1168

1169 1170 1171 1172
	/* get wake version (protocol, key, address) */
	mce_async_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));

	/* unknown what this one actually returns... */
1173
	mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
1174 1175
}

1176
static void mceusb_get_parameters(struct mceusb_dev *ir)
1177
{
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	int i;
	unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
				    MCE_CMD_GETPORTSTATUS, 0x00 };

	/* defaults, if the hardware doesn't support querying */
	ir->num_txports = 2;
	ir->num_rxports = 2;

	/* get number of tx and rx ports */
	mce_async_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));

1189 1190 1191
	/* get the carrier and frequency */
	mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));

1192
	if (ir->num_txports && !ir->flags.no_tx)
1193 1194
		/* get the transmitter bitmask */
		mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
1195 1196 1197 1198 1199 1200

	/* get receiver timeout value */
	mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));

	/* get receiver sensor setting */
	mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
1201 1202 1203 1204 1205

	for (i = 0; i < ir->num_txports; i++) {
		cmdbuf[2] = i;
		mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
	}
1206 1207
}

1208 1209 1210 1211 1212 1213 1214 1215
static void mceusb_flash_led(struct mceusb_dev *ir)
{
	if (ir->emver < 2)
		return;

	mce_async_out(ir, FLASH_LED, sizeof(FLASH_LED));
}

1216
static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
1217
{
1218
	struct usb_device *udev = ir->usbdev;
1219
	struct device *dev = ir->dev;
1220 1221
	struct rc_dev *rc;
	int ret;
1222

1223 1224
	rc = rc_allocate_device();
	if (!rc) {
1225
		dev_err(dev, "remote dev allocation failed");
1226
		goto out;
1227 1228
	}

1229
	snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1230
		 mceusb_model[ir->model].name ?
1231
			mceusb_model[ir->model].name :
1232
			"Media Center Ed. eHome Infrared Remote Transceiver",
1233 1234 1235 1236 1237
		 le16_to_cpu(ir->usbdev->descriptor.idVendor),
		 le16_to_cpu(ir->usbdev->descriptor.idProduct));

	usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));

1238 1239 1240 1241 1242 1243
	rc->input_name = ir->name;
	rc->input_phys = ir->phys;
	usb_to_input_id(ir->usbdev, &rc->input_id);
	rc->dev.parent = dev;
	rc->priv = ir;
	rc->driver_type = RC_DRIVER_IR_RAW;
1244
	rc->allowed_protocols = RC_BIT_ALL;
1245
	rc->timeout = MS_TO_NS(100);
1246
	if (!ir->flags.no_tx) {
1247 1248 1249
		rc->s_tx_mask = mceusb_set_tx_mask;
		rc->s_tx_carrier = mceusb_set_tx_carrier;
		rc->tx_ir = mceusb_tx_ir;
1250
	}
1251
	rc->driver_name = DRIVER_NAME;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264

	switch (le16_to_cpu(udev->descriptor.idVendor)) {
	case VENDOR_HAUPPAUGE:
		rc->map_name = RC_MAP_HAUPPAUGE;
		break;
	case VENDOR_PCTV:
		rc->map_name = RC_MAP_PINNACLE_PCTV_HD;
		break;
	default:
		rc->map_name = RC_MAP_RC6_MCE;
	}
	if (mceusb_model[ir->model].rc_map)
		rc->map_name = mceusb_model[ir->model].rc_map;
1265

1266
	ret = rc_register_device(rc);
1267
	if (ret < 0) {
1268
		dev_err(dev, "remote dev registration failed");
1269
		goto out;
1270 1271
	}

1272
	return rc;
1273

1274 1275
out:
	rc_free_device(rc);
1276 1277 1278
	return NULL;
}

1279 1280
static int mceusb_dev_probe(struct usb_interface *intf,
			    const struct usb_device_id *id)
1281 1282 1283 1284 1285 1286 1287
{
	struct usb_device *dev = interface_to_usbdev(intf);
	struct usb_host_interface *idesc;
	struct usb_endpoint_descriptor *ep = NULL;
	struct usb_endpoint_descriptor *ep_in = NULL;
	struct usb_endpoint_descriptor *ep_out = NULL;
	struct mceusb_dev *ir = NULL;
1288
	int pipe, maxp, i;
1289
	char buf[63], name[128] = "";
1290
	enum mceusb_model_type model = id->driver_info;
1291 1292
	bool is_gen3;
	bool is_microsoft_gen1;
1293
	bool tx_mask_normal;
1294
	int ir_intfnum;
1295

1296
	dev_dbg(&intf->dev, "%s called", __func__);
1297 1298 1299

	idesc  = intf->cur_altsetting;

1300 1301
	is_gen3 = mceusb_model[model].mce_gen3;
	is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1302
	tx_mask_normal = mceusb_model[model].tx_mask_normal;
1303
	ir_intfnum = mceusb_model[model].ir_intfnum;
1304

1305 1306 1307
	/* There are multi-function devices with non-IR interfaces */
	if (idesc->desc.bInterfaceNumber != ir_intfnum)
		return -ENODEV;
1308 1309 1310 1311 1312

	/* step through the endpoints to find first bulk in and out endpoint */
	for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
		ep = &idesc->endpoint[i].desc;

1313 1314 1315 1316 1317 1318 1319 1320 1321
		if (ep_in == NULL) {
			if (usb_endpoint_is_bulk_in(ep)) {
				ep_in = ep;
				dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n");
			} else if (usb_endpoint_is_int_in(ep)) {
				ep_in = ep;
				ep_in->bInterval = 1;
				dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n");
			}
1322 1323
		}

1324 1325 1326 1327 1328 1329 1330 1331 1332
		if (ep_out == NULL) {
			if (usb_endpoint_is_bulk_out(ep)) {
				ep_out = ep;
				dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n");
			} else if (usb_endpoint_is_int_out(ep)) {
				ep_out = ep;
				ep_out->bInterval = 1;
				dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n");
			}
1333 1334 1335
		}
	}
	if (ep_in == NULL) {
1336
		dev_dbg(&intf->dev, "inbound and/or endpoint not found");
1337 1338 1339
		return -ENODEV;
	}

1340 1341 1342 1343
	if (usb_endpoint_xfer_int(ep_in))
		pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
	else
		pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress);
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));

	ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
	if (!ir)
		goto mem_alloc_fail;

	ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
	if (!ir->buf_in)
		goto buf_in_alloc_fail;

	ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
	if (!ir->urb_in)
		goto urb_in_alloc_fail;

1358
	ir->usbdev = usb_get_dev(dev);
1359 1360 1361
	ir->dev = &intf->dev;
	ir->len_in = maxp;
	ir->flags.microsoft_gen1 = is_microsoft_gen1;
1362
	ir->flags.tx_mask_normal = tx_mask_normal;
1363
	ir->flags.no_tx = mceusb_model[model].no_tx;
1364 1365
	ir->model = model;

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
	/* Saving usb interface data for use by the transmitter routine */
	ir->usb_ep_out = ep_out;

	if (dev->descriptor.iManufacturer
	    && usb_string(dev, dev->descriptor.iManufacturer,
			  buf, sizeof(buf)) > 0)
		strlcpy(name, buf, sizeof(name));
	if (dev->descriptor.iProduct
	    && usb_string(dev, dev->descriptor.iProduct,
			  buf, sizeof(buf)) > 0)
		snprintf(name + strlen(name), sizeof(name) - strlen(name),
			 " %s", buf);

1379
	ir->rc = mceusb_init_rc_dev(ir);
1380 1381
	if (!ir->rc)
		goto rc_dev_fail;
1382

1383
	/* wire up inbound data handler */
S
Sean Young 已提交
1384 1385
	usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
				mceusb_dev_recv, ir, ep_in->bInterval);
1386 1387 1388
	ir->urb_in->transfer_dma = ir->dma_in;
	ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

1389
	/* flush buffers on the device */
1390
	dev_dbg(&intf->dev, "Flushing receive buffers\n");
1391 1392
	mce_flush_rx_buffer(ir, maxp);

1393 1394 1395
	/* figure out which firmware/emulator version this hardware has */
	mceusb_get_emulator_version(ir);

1396
	/* initialize device */
1397
	if (ir->flags.microsoft_gen1)
1398
		mceusb_gen1_init(ir);
1399
	else if (!is_gen3)
1400 1401
		mceusb_gen2_init(ir);

1402
	mceusb_get_parameters(ir);
1403

1404 1405
	mceusb_flash_led(ir);

1406
	if (!ir->flags.no_tx)
1407
		mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
1408 1409 1410

	usb_set_intfdata(intf, ir);

1411 1412 1413 1414
	/* enable wake via this device */
	device_set_wakeup_capable(ir->dev, true);
	device_set_wakeup_enable(ir->dev, true);

1415 1416 1417
	dev_info(&intf->dev, "Registered %s with mce emulator interface version %x",
		name, ir->emver);
	dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
1418 1419
		 ir->num_txports, ir->txports_cabled,
		 ir->num_rxports, ir->rxports_active);
1420 1421 1422 1423

	return 0;

	/* Error-handling path */
1424
rc_dev_fail:
1425
	usb_put_dev(ir->usbdev);
1426 1427 1428 1429 1430 1431
	usb_free_urb(ir->urb_in);
urb_in_alloc_fail:
	usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
buf_in_alloc_fail:
	kfree(ir);
mem_alloc_fail:
1432
	dev_err(&intf->dev, "%s: device setup failed!", __func__);
1433 1434 1435 1436 1437

	return -ENOMEM;
}


1438
static void mceusb_dev_disconnect(struct usb_interface *intf)
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
{
	struct usb_device *dev = interface_to_usbdev(intf);
	struct mceusb_dev *ir = usb_get_intfdata(intf);

	usb_set_intfdata(intf, NULL);

	if (!ir)
		return;

	ir->usbdev = NULL;
1449
	rc_unregister_device(ir->rc);
1450 1451 1452
	usb_kill_urb(ir->urb_in);
	usb_free_urb(ir->urb_in);
	usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1453
	usb_put_dev(dev);
1454 1455 1456 1457 1458 1459 1460

	kfree(ir);
}

static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct mceusb_dev *ir = usb_get_intfdata(intf);
1461
	dev_info(ir->dev, "suspend");
1462 1463 1464 1465 1466 1467 1468
	usb_kill_urb(ir->urb_in);
	return 0;
}

static int mceusb_dev_resume(struct usb_interface *intf)
{
	struct mceusb_dev *ir = usb_get_intfdata(intf);
1469
	dev_info(ir->dev, "resume");
1470 1471 1472 1473 1474 1475 1476 1477
	if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
		return -EIO;
	return 0;
}

static struct usb_driver mceusb_dev_driver = {
	.name =		DRIVER_NAME,
	.probe =	mceusb_dev_probe,
1478
	.disconnect =	mceusb_dev_disconnect,
1479 1480 1481 1482 1483 1484
	.suspend =	mceusb_dev_suspend,
	.resume =	mceusb_dev_resume,
	.reset_resume =	mceusb_dev_resume,
	.id_table =	mceusb_dev_table
};

1485
module_usb_driver(mceusb_dev_driver);
1486 1487 1488 1489 1490

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(usb, mceusb_dev_table);