xref: /openbmc/linux/drivers/media/rc/mceusb.c (revision addee42a)
1 /*
2  * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
3  *
4  * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com>
5  *
6  * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
7  * Conti, Martin Blatter and Daniel Melander, the latter of which was
8  * in turn also based on the lirc_atiusb driver by Paul Miller. The
9  * two mce drivers were merged into one by Jarod Wilson, with transmit
10  * support for the 1st-gen device added primarily by Patrick Calhoun,
11  * with a bit of tweaks by Jarod. Debugging improvements and proper
12  * support for what appears to be 3rd-gen hardware added by Jarod.
13  * Initial port from lirc driver to ir-core drivery by Jarod, based
14  * partially on a port to an earlier proposed IR infrastructure by
15  * Jon Smirl, which included enhancements and simplifications to the
16  * incoming IR buffer parsing routines.
17  *
18  * Updated in July of 2011 with the aid of Microsoft's official
19  * remote/transceiver requirements and specification document, found at
20  * download.microsoft.com, title
21  * Windows-Media-Center-RC-IR-Collection-Green-Button-Specification-03-08-2011-V2.pdf
22  *
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  */
35 
36 #include <linux/device.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/workqueue.h>
40 #include <linux/usb.h>
41 #include <linux/usb/input.h>
42 #include <linux/pm_wakeup.h>
43 #include <media/rc-core.h>
44 
45 #define DRIVER_VERSION	"1.94"
46 #define DRIVER_AUTHOR	"Jarod Wilson <jarod@redhat.com>"
47 #define DRIVER_DESC	"Windows Media Center Ed. eHome Infrared Transceiver " \
48 			"device driver"
49 #define DRIVER_NAME	"mceusb"
50 
51 #define USB_CTRL_MSG_SZ		2  /* Size of usb ctrl msg on gen1 hw */
52 #define MCE_G1_INIT_MSGS	40 /* Init messages on gen1 hw to throw out */
53 
54 /* MCE constants */
55 #define MCE_CMDBUF_SIZE		384  /* MCE Command buffer length */
56 #define MCE_TIME_UNIT		50   /* Approx 50us resolution */
57 #define MCE_CODE_LENGTH		5    /* Normal length of packet (with header) */
58 #define MCE_PACKET_SIZE		4    /* Normal length of packet (without header) */
59 #define MCE_IRDATA_HEADER	0x84 /* Actual header format is 0x80 + num_bytes */
60 #define MCE_IRDATA_TRAILER	0x80 /* End of IR data */
61 #define MCE_MAX_CHANNELS	2    /* Two transmitters, hardware dependent? */
62 #define MCE_DEFAULT_TX_MASK	0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
63 #define MCE_PULSE_BIT		0x80 /* Pulse bit, MSB set == PULSE else SPACE */
64 #define MCE_PULSE_MASK		0x7f /* Pulse mask */
65 #define MCE_MAX_PULSE_LENGTH	0x7f /* Longest transmittable pulse symbol */
66 
67 /*
68  * The interface between the host and the IR hardware is command-response
69  * based. All commands and responses have a consistent format, where a lead
70  * byte always identifies the type of data following it. The lead byte has
71  * a port value in the 3 highest bits and a length value in the 5 lowest
72  * bits.
73  *
74  * The length field is overloaded, with a value of 11111 indicating that the
75  * following byte is a command or response code, and the length of the entire
76  * message is determined by the code. If the length field is not 11111, then
77  * it specifies the number of bytes of port data that follow.
78  */
79 #define MCE_CMD			0x1f
80 #define MCE_PORT_IR		0x4	/* (0x4 << 5) | MCE_CMD = 0x9f */
81 #define MCE_PORT_SYS		0x7	/* (0x7 << 5) | MCE_CMD = 0xff */
82 #define MCE_PORT_SER		0x6	/* 0xc0 thru 0xdf flush & 0x1f bytes */
83 #define MCE_PORT_MASK		0xe0	/* Mask out command bits */
84 
85 /* Command port headers */
86 #define MCE_CMD_PORT_IR		0x9f	/* IR-related cmd/rsp */
87 #define MCE_CMD_PORT_SYS	0xff	/* System (non-IR) device cmd/rsp */
88 
89 /* Commands that set device state  (2-4 bytes in length) */
90 #define MCE_CMD_RESET		0xfe	/* Reset device, 2 bytes */
91 #define MCE_CMD_RESUME		0xaa	/* Resume device after error, 2 bytes */
92 #define MCE_CMD_SETIRCFS	0x06	/* Set tx carrier, 4 bytes */
93 #define MCE_CMD_SETIRTIMEOUT	0x0c	/* Set timeout, 4 bytes */
94 #define MCE_CMD_SETIRTXPORTS	0x08	/* Set tx ports, 3 bytes */
95 #define MCE_CMD_SETIRRXPORTEN	0x14	/* Set rx ports, 3 bytes */
96 #define MCE_CMD_FLASHLED	0x23	/* Flash receiver LED, 2 bytes */
97 
98 /* Commands that query device state (all 2 bytes, unless noted) */
99 #define MCE_CMD_GETIRCFS	0x07	/* Get carrier */
100 #define MCE_CMD_GETIRTIMEOUT	0x0d	/* Get timeout */
101 #define MCE_CMD_GETIRTXPORTS	0x13	/* Get tx ports */
102 #define MCE_CMD_GETIRRXPORTEN	0x15	/* Get rx ports */
103 #define MCE_CMD_GETPORTSTATUS	0x11	/* Get tx port status, 3 bytes */
104 #define MCE_CMD_GETIRNUMPORTS	0x16	/* Get number of ports */
105 #define MCE_CMD_GETWAKESOURCE	0x17	/* Get wake source */
106 #define MCE_CMD_GETEMVER	0x22	/* Get emulator interface version */
107 #define MCE_CMD_GETDEVDETAILS	0x21	/* Get device details (em ver2 only) */
108 #define MCE_CMD_GETWAKESUPPORT	0x20	/* Get wake details (em ver2 only) */
109 #define MCE_CMD_GETWAKEVERSION	0x18	/* Get wake pattern (em ver2 only) */
110 
111 /* Misc commands */
112 #define MCE_CMD_NOP		0xff	/* No operation */
113 
114 /* Responses to commands (non-error cases) */
115 #define MCE_RSP_EQIRCFS		0x06	/* tx carrier, 4 bytes */
116 #define MCE_RSP_EQIRTIMEOUT	0x0c	/* rx timeout, 4 bytes */
117 #define MCE_RSP_GETWAKESOURCE	0x17	/* wake source, 3 bytes */
118 #define MCE_RSP_EQIRTXPORTS	0x08	/* tx port mask, 3 bytes */
119 #define MCE_RSP_EQIRRXPORTEN	0x14	/* rx port mask, 3 bytes */
120 #define MCE_RSP_GETPORTSTATUS	0x11	/* tx port status, 7 bytes */
121 #define MCE_RSP_EQIRRXCFCNT	0x15	/* rx carrier count, 4 bytes */
122 #define MCE_RSP_EQIRNUMPORTS	0x16	/* number of ports, 4 bytes */
123 #define MCE_RSP_EQWAKESUPPORT	0x20	/* wake capabilities, 3 bytes */
124 #define MCE_RSP_EQWAKEVERSION	0x18	/* wake pattern details, 6 bytes */
125 #define MCE_RSP_EQDEVDETAILS	0x21	/* device capabilities, 3 bytes */
126 #define MCE_RSP_EQEMVER		0x22	/* emulator interface ver, 3 bytes */
127 #define MCE_RSP_FLASHLED	0x23	/* success flashing LED, 2 bytes */
128 
129 /* Responses to error cases, must send MCE_CMD_RESUME to clear them */
130 #define MCE_RSP_CMD_ILLEGAL	0xfe	/* illegal command for port, 2 bytes */
131 #define MCE_RSP_TX_TIMEOUT	0x81	/* tx timed out, 2 bytes */
132 
133 /* Misc commands/responses not defined in the MCE remote/transceiver spec */
134 #define MCE_CMD_SIG_END		0x01	/* End of signal */
135 #define MCE_CMD_PING		0x03	/* Ping device */
136 #define MCE_CMD_UNKNOWN		0x04	/* Unknown */
137 #define MCE_CMD_UNKNOWN2	0x05	/* Unknown */
138 #define MCE_CMD_UNKNOWN3	0x09	/* Unknown */
139 #define MCE_CMD_UNKNOWN4	0x0a	/* Unknown */
140 #define MCE_CMD_G_REVISION	0x0b	/* Get hw/sw revision */
141 #define MCE_CMD_UNKNOWN5	0x0e	/* Unknown */
142 #define MCE_CMD_UNKNOWN6	0x0f	/* Unknown */
143 #define MCE_CMD_UNKNOWN8	0x19	/* Unknown */
144 #define MCE_CMD_UNKNOWN9	0x1b	/* Unknown */
145 #define MCE_CMD_NULL		0x00	/* These show up various places... */
146 
147 /* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
148  * then we're looking at a raw IR data sample */
149 #define MCE_COMMAND_IRDATA	0x80
150 #define MCE_PACKET_LENGTH_MASK	0x1f /* Packet length mask */
151 
152 #define VENDOR_PHILIPS		0x0471
153 #define VENDOR_SMK		0x0609
154 #define VENDOR_TATUNG		0x1460
155 #define VENDOR_GATEWAY		0x107b
156 #define VENDOR_SHUTTLE		0x1308
157 #define VENDOR_SHUTTLE2		0x051c
158 #define VENDOR_MITSUMI		0x03ee
159 #define VENDOR_TOPSEED		0x1784
160 #define VENDOR_RICAVISION	0x179d
161 #define VENDOR_ITRON		0x195d
162 #define VENDOR_FIC		0x1509
163 #define VENDOR_LG		0x043e
164 #define VENDOR_MICROSOFT	0x045e
165 #define VENDOR_FORMOSA		0x147a
166 #define VENDOR_FINTEK		0x1934
167 #define VENDOR_PINNACLE		0x2304
168 #define VENDOR_ECS		0x1019
169 #define VENDOR_WISTRON		0x0fb8
170 #define VENDOR_COMPRO		0x185b
171 #define VENDOR_NORTHSTAR	0x04eb
172 #define VENDOR_REALTEK		0x0bda
173 #define VENDOR_TIVO		0x105a
174 #define VENDOR_CONEXANT		0x0572
175 #define VENDOR_TWISTEDMELON	0x2596
176 #define VENDOR_HAUPPAUGE	0x2040
177 #define VENDOR_PCTV		0x2013
178 #define VENDOR_ADAPTEC		0x03f3
179 
180 enum mceusb_model_type {
181 	MCE_GEN2 = 0,		/* Most boards */
182 	MCE_GEN1,
183 	MCE_GEN3,
184 	MCE_GEN2_TX_INV,
185 	MCE_GEN2_TX_INV_RX_GOOD,
186 	POLARIS_EVK,
187 	CX_HYBRID_TV,
188 	MULTIFUNCTION,
189 	TIVO_KIT,
190 	MCE_GEN2_NO_TX,
191 	HAUPPAUGE_CX_HYBRID_TV,
192 	EVROMEDIA_FULL_HYBRID_FULLHD,
193 	ASTROMETA_T2HYBRID,
194 };
195 
196 struct mceusb_model {
197 	u32 mce_gen1:1;
198 	u32 mce_gen2:1;
199 	u32 mce_gen3:1;
200 	u32 tx_mask_normal:1;
201 	u32 no_tx:1;
202 	/*
203 	 * 2nd IR receiver (short-range, wideband) for learning mode:
204 	 *     0, absent 2nd receiver (rx2)
205 	 *     1, rx2 present
206 	 *     2, rx2 which under counts IR carrier cycles
207 	 */
208 	u32 rx2;
209 
210 	int ir_intfnum;
211 
212 	const char *rc_map;	/* Allow specify a per-board map */
213 	const char *name;	/* per-board name */
214 };
215 
216 static const struct mceusb_model mceusb_model[] = {
217 	[MCE_GEN1] = {
218 		.mce_gen1 = 1,
219 		.tx_mask_normal = 1,
220 		.rx2 = 2,
221 	},
222 	[MCE_GEN2] = {
223 		.mce_gen2 = 1,
224 		.rx2 = 2,
225 	},
226 	[MCE_GEN2_NO_TX] = {
227 		.mce_gen2 = 1,
228 		.no_tx = 1,
229 	},
230 	[MCE_GEN2_TX_INV] = {
231 		.mce_gen2 = 1,
232 		.tx_mask_normal = 1,
233 		.rx2 = 1,
234 	},
235 	[MCE_GEN2_TX_INV_RX_GOOD] = {
236 		.mce_gen2 = 1,
237 		.tx_mask_normal = 1,
238 		.rx2 = 2,
239 	},
240 	[MCE_GEN3] = {
241 		.mce_gen3 = 1,
242 		.tx_mask_normal = 1,
243 		.rx2 = 2,
244 	},
245 	[POLARIS_EVK] = {
246 		/*
247 		 * In fact, the EVK is shipped without
248 		 * remotes, but we should have something handy,
249 		 * to allow testing it
250 		 */
251 		.name = "Conexant Hybrid TV (cx231xx) MCE IR",
252 		.rx2 = 2,
253 	},
254 	[CX_HYBRID_TV] = {
255 		.no_tx = 1, /* tx isn't wired up at all */
256 		.name = "Conexant Hybrid TV (cx231xx) MCE IR",
257 	},
258 	[HAUPPAUGE_CX_HYBRID_TV] = {
259 		.no_tx = 1, /* eeprom says it has no tx */
260 		.name = "Conexant Hybrid TV (cx231xx) MCE IR no TX",
261 	},
262 	[MULTIFUNCTION] = {
263 		.mce_gen2 = 1,
264 		.ir_intfnum = 2,
265 		.rx2 = 2,
266 	},
267 	[TIVO_KIT] = {
268 		.mce_gen2 = 1,
269 		.rc_map = RC_MAP_TIVO,
270 		.rx2 = 2,
271 	},
272 	[EVROMEDIA_FULL_HYBRID_FULLHD] = {
273 		.name = "Evromedia USB Full Hybrid Full HD",
274 		.no_tx = 1,
275 		.rc_map = RC_MAP_MSI_DIGIVOX_III,
276 	},
277 	[ASTROMETA_T2HYBRID] = {
278 		.name = "Astrometa T2Hybrid",
279 		.no_tx = 1,
280 		.rc_map = RC_MAP_ASTROMETA_T2HYBRID,
281 	}
282 };
283 
284 static const struct usb_device_id mceusb_dev_table[] = {
285 	/* Original Microsoft MCE IR Transceiver (often HP-branded) */
286 	{ USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
287 	  .driver_info = MCE_GEN1 },
288 	/* Philips Infrared Transceiver - Sahara branded */
289 	{ USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
290 	/* Philips Infrared Transceiver - HP branded */
291 	{ USB_DEVICE(VENDOR_PHILIPS, 0x060c),
292 	  .driver_info = MCE_GEN2_TX_INV },
293 	/* Philips SRM5100 */
294 	{ USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
295 	/* Philips Infrared Transceiver - Omaura */
296 	{ USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
297 	/* Philips Infrared Transceiver - Spinel plus */
298 	{ USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
299 	/* Philips eHome Infrared Transceiver */
300 	{ USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
301 	/* Philips/Spinel plus IR transceiver for ASUS */
302 	{ USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
303 	/* Philips/Spinel plus IR transceiver for ASUS */
304 	{ USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
305 	/* Philips IR transceiver (Dell branded) */
306 	{ USB_DEVICE(VENDOR_PHILIPS, 0x2093),
307 	  .driver_info = MCE_GEN2_TX_INV },
308 	/* Realtek MCE IR Receiver and card reader */
309 	{ USB_DEVICE(VENDOR_REALTEK, 0x0161),
310 	  .driver_info = MULTIFUNCTION },
311 	/* SMK/Toshiba G83C0004D410 */
312 	{ USB_DEVICE(VENDOR_SMK, 0x031d),
313 	  .driver_info = MCE_GEN2_TX_INV_RX_GOOD },
314 	/* SMK eHome Infrared Transceiver (Sony VAIO) */
315 	{ USB_DEVICE(VENDOR_SMK, 0x0322),
316 	  .driver_info = MCE_GEN2_TX_INV },
317 	/* bundled with Hauppauge PVR-150 */
318 	{ USB_DEVICE(VENDOR_SMK, 0x0334),
319 	  .driver_info = MCE_GEN2_TX_INV },
320 	/* SMK eHome Infrared Transceiver */
321 	{ USB_DEVICE(VENDOR_SMK, 0x0338) },
322 	/* SMK/I-O Data GV-MC7/RCKIT Receiver */
323 	{ USB_DEVICE(VENDOR_SMK, 0x0353),
324 	  .driver_info = MCE_GEN2_NO_TX },
325 	/* SMK RXX6000 Infrared Receiver */
326 	{ USB_DEVICE(VENDOR_SMK, 0x0357),
327 	  .driver_info = MCE_GEN2_NO_TX },
328 	/* Tatung eHome Infrared Transceiver */
329 	{ USB_DEVICE(VENDOR_TATUNG, 0x9150) },
330 	/* Shuttle eHome Infrared Transceiver */
331 	{ USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
332 	/* Shuttle eHome Infrared Transceiver */
333 	{ USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
334 	/* Gateway eHome Infrared Transceiver */
335 	{ USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
336 	/* Mitsumi */
337 	{ USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
338 	/* Topseed eHome Infrared Transceiver */
339 	{ USB_DEVICE(VENDOR_TOPSEED, 0x0001),
340 	  .driver_info = MCE_GEN2_TX_INV },
341 	/* Topseed HP eHome Infrared Transceiver */
342 	{ USB_DEVICE(VENDOR_TOPSEED, 0x0006),
343 	  .driver_info = MCE_GEN2_TX_INV },
344 	/* Topseed eHome Infrared Transceiver */
345 	{ USB_DEVICE(VENDOR_TOPSEED, 0x0007),
346 	  .driver_info = MCE_GEN2_TX_INV },
347 	/* Topseed eHome Infrared Transceiver */
348 	{ USB_DEVICE(VENDOR_TOPSEED, 0x0008),
349 	  .driver_info = MCE_GEN3 },
350 	/* Topseed eHome Infrared Transceiver */
351 	{ USB_DEVICE(VENDOR_TOPSEED, 0x000a),
352 	  .driver_info = MCE_GEN2_TX_INV },
353 	/* Topseed eHome Infrared Transceiver */
354 	{ USB_DEVICE(VENDOR_TOPSEED, 0x0011),
355 	  .driver_info = MCE_GEN3 },
356 	/* Ricavision internal Infrared Transceiver */
357 	{ USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
358 	/* Itron ione Libra Q-11 */
359 	{ USB_DEVICE(VENDOR_ITRON, 0x7002) },
360 	/* FIC eHome Infrared Transceiver */
361 	{ USB_DEVICE(VENDOR_FIC, 0x9242) },
362 	/* LG eHome Infrared Transceiver */
363 	{ USB_DEVICE(VENDOR_LG, 0x9803) },
364 	/* Microsoft MCE Infrared Transceiver */
365 	{ USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
366 	/* Formosa eHome Infrared Transceiver */
367 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
368 	/* Formosa21 / eHome Infrared Receiver */
369 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
370 	/* Formosa aim / Trust MCE Infrared Receiver */
371 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe017),
372 	  .driver_info = MCE_GEN2_NO_TX },
373 	/* Formosa Industrial Computing / Beanbag Emulation Device */
374 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
375 	/* Formosa21 / eHome Infrared Receiver */
376 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
377 	/* Formosa Industrial Computing AIM IR605/A */
378 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
379 	/* Formosa Industrial Computing */
380 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
381 	/* Formosa Industrial Computing */
382 	{ USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
383 	/* Fintek eHome Infrared Transceiver (HP branded) */
384 	{ USB_DEVICE(VENDOR_FINTEK, 0x5168),
385 	  .driver_info = MCE_GEN2_TX_INV },
386 	/* Fintek eHome Infrared Transceiver */
387 	{ USB_DEVICE(VENDOR_FINTEK, 0x0602) },
388 	/* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
389 	{ USB_DEVICE(VENDOR_FINTEK, 0x0702) },
390 	/* Pinnacle Remote Kit */
391 	{ USB_DEVICE(VENDOR_PINNACLE, 0x0225),
392 	  .driver_info = MCE_GEN3 },
393 	/* Elitegroup Computer Systems IR */
394 	{ USB_DEVICE(VENDOR_ECS, 0x0f38) },
395 	/* Wistron Corp. eHome Infrared Receiver */
396 	{ USB_DEVICE(VENDOR_WISTRON, 0x0002) },
397 	/* Compro K100 */
398 	{ USB_DEVICE(VENDOR_COMPRO, 0x3020) },
399 	/* Compro K100 v2 */
400 	{ USB_DEVICE(VENDOR_COMPRO, 0x3082) },
401 	/* Northstar Systems, Inc. eHome Infrared Transceiver */
402 	{ USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
403 	/* TiVo PC IR Receiver */
404 	{ USB_DEVICE(VENDOR_TIVO, 0x2000),
405 	  .driver_info = TIVO_KIT },
406 	/* Conexant Hybrid TV "Shelby" Polaris SDK */
407 	{ USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
408 	  .driver_info = POLARIS_EVK },
409 	/* Conexant Hybrid TV RDU253S Polaris */
410 	{ USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
411 	  .driver_info = CX_HYBRID_TV },
412 	/* Twisted Melon Inc. - Manta Mini Receiver */
413 	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
414 	/* Twisted Melon Inc. - Manta Pico Receiver */
415 	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
416 	/* Twisted Melon Inc. - Manta Transceiver */
417 	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
418 	/* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
419 	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb130),
420 	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
421 	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb131),
422 	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
423 	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb138),
424 	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
425 	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb139),
426 	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
427 	{ USB_DEVICE(VENDOR_PCTV, 0x0259),
428 	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
429 	{ USB_DEVICE(VENDOR_PCTV, 0x025e),
430 	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
431 	/* Adaptec / HP eHome Receiver */
432 	{ USB_DEVICE(VENDOR_ADAPTEC, 0x0094) },
433 	/* Evromedia USB Full Hybrid Full HD */
434 	{ USB_DEVICE(0x1b80, 0xd3b2),
435 	  .driver_info = EVROMEDIA_FULL_HYBRID_FULLHD },
436 	/* Astrometa T2hybrid */
437 	{ USB_DEVICE(0x15f4, 0x0135),
438 	  .driver_info = ASTROMETA_T2HYBRID },
439 
440 	/* Terminating entry */
441 	{ }
442 };
443 
444 /* data structure for each usb transceiver */
445 struct mceusb_dev {
446 	/* ir-core bits */
447 	struct rc_dev *rc;
448 
449 	/* optional features we can enable */
450 	bool carrier_report_enabled;
451 	bool wideband_rx_enabled;	/* aka learning mode, short-range rx */
452 
453 	/* core device bits */
454 	struct device *dev;
455 
456 	/* usb */
457 	struct usb_device *usbdev;
458 	struct urb *urb_in;
459 	unsigned int pipe_in;
460 	struct usb_endpoint_descriptor *usb_ep_out;
461 	unsigned int pipe_out;
462 
463 	/* buffers and dma */
464 	unsigned char *buf_in;
465 	unsigned int len_in;
466 	dma_addr_t dma_in;
467 
468 	enum {
469 		CMD_HEADER = 0,
470 		SUBCMD,
471 		CMD_DATA,
472 		PARSE_IRDATA,
473 	} parser_state;
474 
475 	u8 cmd, rem;		/* Remaining IR data bytes in packet */
476 
477 	struct {
478 		u32 connected:1;
479 		u32 tx_mask_normal:1;
480 		u32 microsoft_gen1:1;
481 		u32 no_tx:1;
482 		u32 rx2;
483 	} flags;
484 
485 	/* transmit support */
486 	u32 carrier;
487 	unsigned char tx_mask;
488 
489 	char name[128];
490 	char phys[64];
491 	enum mceusb_model_type model;
492 
493 	bool need_reset;	/* flag to issue a device resume cmd */
494 	u8 emver;		/* emulator interface version */
495 	u8 num_txports;		/* number of transmit ports */
496 	u8 num_rxports;		/* number of receive sensors */
497 	u8 txports_cabled;	/* bitmask of transmitters with cable */
498 	u8 rxports_active;	/* bitmask of active receive sensors */
499 	bool learning_active;	/* wideband rx is active */
500 
501 	/* receiver carrier frequency detection support */
502 	u32 pulse_tunit;	/* IR pulse "on" cumulative time units */
503 	u32 pulse_count;	/* pulse "on" count in measurement interval */
504 
505 	/*
506 	 * support for async error handler mceusb_deferred_kevent()
507 	 * where usb_clear_halt(), usb_reset_configuration(),
508 	 * usb_reset_device(), etc. must be done in process context
509 	 */
510 	struct work_struct kevent;
511 	unsigned long kevent_flags;
512 #		define EVENT_TX_HALT	0
513 #		define EVENT_RX_HALT	1
514 };
515 
516 /* MCE Device Command Strings, generally a port and command pair */
517 static char DEVICE_RESUME[]	= {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
518 				   MCE_CMD_RESUME};
519 static char GET_REVISION[]	= {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
520 static char GET_EMVER[]		= {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
521 static char GET_WAKEVERSION[]	= {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
522 static char FLASH_LED[]		= {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
523 static char GET_UNKNOWN2[]	= {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
524 static char GET_CARRIER_FREQ[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
525 static char GET_RX_TIMEOUT[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
526 static char GET_NUM_PORTS[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
527 static char GET_TX_BITMASK[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
528 static char GET_RX_SENSOR[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
529 /* sub in desired values in lower byte or bytes for full command */
530 /* FIXME: make use of these for transmit.
531 static char SET_CARRIER_FREQ[]	= {MCE_CMD_PORT_IR,
532 				   MCE_CMD_SETIRCFS, 0x00, 0x00};
533 static char SET_TX_BITMASK[]	= {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
534 static char SET_RX_TIMEOUT[]	= {MCE_CMD_PORT_IR,
535 				   MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
536 static char SET_RX_SENSOR[]	= {MCE_CMD_PORT_IR,
537 				   MCE_RSP_EQIRRXPORTEN, 0x00};
538 */
539 
540 static int mceusb_cmd_datasize(u8 cmd, u8 subcmd)
541 {
542 	int datasize = 0;
543 
544 	switch (cmd) {
545 	case MCE_CMD_NULL:
546 		if (subcmd == MCE_CMD_PORT_SYS)
547 			datasize = 1;
548 		break;
549 	case MCE_CMD_PORT_SYS:
550 		switch (subcmd) {
551 		case MCE_RSP_GETPORTSTATUS:
552 			datasize = 5;
553 			break;
554 		case MCE_RSP_EQWAKEVERSION:
555 			datasize = 4;
556 			break;
557 		case MCE_CMD_G_REVISION:
558 			datasize = 2;
559 			break;
560 		case MCE_RSP_EQWAKESUPPORT:
561 		case MCE_RSP_GETWAKESOURCE:
562 		case MCE_RSP_EQDEVDETAILS:
563 		case MCE_RSP_EQEMVER:
564 			datasize = 1;
565 			break;
566 		}
567 	case MCE_CMD_PORT_IR:
568 		switch (subcmd) {
569 		case MCE_CMD_UNKNOWN:
570 		case MCE_RSP_EQIRCFS:
571 		case MCE_RSP_EQIRTIMEOUT:
572 		case MCE_RSP_EQIRRXCFCNT:
573 		case MCE_RSP_EQIRNUMPORTS:
574 			datasize = 2;
575 			break;
576 		case MCE_CMD_SIG_END:
577 		case MCE_RSP_EQIRTXPORTS:
578 		case MCE_RSP_EQIRRXPORTEN:
579 			datasize = 1;
580 			break;
581 		}
582 	}
583 	return datasize;
584 }
585 
586 static void mceusb_dev_printdata(struct mceusb_dev *ir, u8 *buf, int buf_len,
587 				 int offset, int len, bool out)
588 {
589 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
590 	char *inout;
591 	u8 cmd, subcmd, *data;
592 	struct device *dev = ir->dev;
593 	int start, skip = 0;
594 	u32 carrier, period;
595 
596 	/* skip meaningless 0xb1 0x60 header bytes on orig receiver */
597 	if (ir->flags.microsoft_gen1 && !out && !offset)
598 		skip = 2;
599 
600 	if (len <= skip)
601 		return;
602 
603 	dev_dbg(dev, "%cx data: %*ph (length=%d)",
604 		(out ? 't' : 'r'),
605 		min(len, buf_len - offset), buf + offset, len);
606 
607 	inout = out ? "Request" : "Got";
608 
609 	start  = offset + skip;
610 	cmd    = buf[start] & 0xff;
611 	subcmd = buf[start + 1] & 0xff;
612 	data = buf + start + 2;
613 
614 	switch (cmd) {
615 	case MCE_CMD_NULL:
616 		if (subcmd == MCE_CMD_NULL)
617 			break;
618 		if ((subcmd == MCE_CMD_PORT_SYS) &&
619 		    (data[0] == MCE_CMD_RESUME))
620 			dev_dbg(dev, "Device resume requested");
621 		else
622 			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
623 				 cmd, subcmd);
624 		break;
625 	case MCE_CMD_PORT_SYS:
626 		switch (subcmd) {
627 		case MCE_RSP_EQEMVER:
628 			if (!out)
629 				dev_dbg(dev, "Emulator interface version %x",
630 					 data[0]);
631 			break;
632 		case MCE_CMD_G_REVISION:
633 			if (len == 2)
634 				dev_dbg(dev, "Get hw/sw rev?");
635 			else
636 				dev_dbg(dev, "hw/sw rev %*ph",
637 					4, &buf[start + 2]);
638 			break;
639 		case MCE_CMD_RESUME:
640 			dev_dbg(dev, "Device resume requested");
641 			break;
642 		case MCE_RSP_CMD_ILLEGAL:
643 			dev_dbg(dev, "Illegal PORT_SYS command");
644 			break;
645 		case MCE_RSP_EQWAKEVERSION:
646 			if (!out)
647 				dev_dbg(dev, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
648 					data[0], data[1], data[2], data[3]);
649 			break;
650 		case MCE_RSP_GETPORTSTATUS:
651 			if (!out)
652 				/* We use data1 + 1 here, to match hw labels */
653 				dev_dbg(dev, "TX port %d: blaster is%s connected",
654 					 data[0] + 1, data[3] ? " not" : "");
655 			break;
656 		case MCE_CMD_FLASHLED:
657 			dev_dbg(dev, "Attempting to flash LED");
658 			break;
659 		default:
660 			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
661 				 cmd, subcmd);
662 			break;
663 		}
664 		break;
665 	case MCE_CMD_PORT_IR:
666 		switch (subcmd) {
667 		case MCE_CMD_SIG_END:
668 			dev_dbg(dev, "End of signal");
669 			break;
670 		case MCE_CMD_PING:
671 			dev_dbg(dev, "Ping");
672 			break;
673 		case MCE_CMD_UNKNOWN:
674 			dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x",
675 				data[0], data[1]);
676 			break;
677 		case MCE_RSP_EQIRCFS:
678 			period = DIV_ROUND_CLOSEST((1U << data[0] * 2) *
679 						   (data[1] + 1), 10);
680 			if (!period)
681 				break;
682 			carrier = (1000 * 1000) / period;
683 			dev_dbg(dev, "%s carrier of %u Hz (period %uus)",
684 				 inout, carrier, period);
685 			break;
686 		case MCE_CMD_GETIRCFS:
687 			dev_dbg(dev, "Get carrier mode and freq");
688 			break;
689 		case MCE_RSP_EQIRTXPORTS:
690 			dev_dbg(dev, "%s transmit blaster mask of 0x%02x",
691 				 inout, data[0]);
692 			break;
693 		case MCE_RSP_EQIRTIMEOUT:
694 			/* value is in units of 50us, so x*50/1000 ms */
695 			period = ((data[0] << 8) | data[1]) *
696 				  MCE_TIME_UNIT / 1000;
697 			dev_dbg(dev, "%s receive timeout of %d ms",
698 				 inout, period);
699 			break;
700 		case MCE_CMD_GETIRTIMEOUT:
701 			dev_dbg(dev, "Get receive timeout");
702 			break;
703 		case MCE_CMD_GETIRTXPORTS:
704 			dev_dbg(dev, "Get transmit blaster mask");
705 			break;
706 		case MCE_RSP_EQIRRXPORTEN:
707 			dev_dbg(dev, "%s %s-range receive sensor in use",
708 				 inout, data[0] == 0x02 ? "short" : "long");
709 			break;
710 		case MCE_CMD_GETIRRXPORTEN:
711 		/* aka MCE_RSP_EQIRRXCFCNT */
712 			if (out)
713 				dev_dbg(dev, "Get receive sensor");
714 			else
715 				dev_dbg(dev, "RX carrier cycle count: %d",
716 					((data[0] << 8) | data[1]));
717 			break;
718 		case MCE_RSP_EQIRNUMPORTS:
719 			if (out)
720 				break;
721 			dev_dbg(dev, "Num TX ports: %x, num RX ports: %x",
722 				data[0], data[1]);
723 			break;
724 		case MCE_RSP_CMD_ILLEGAL:
725 			dev_dbg(dev, "Illegal PORT_IR command");
726 			break;
727 		default:
728 			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
729 				 cmd, subcmd);
730 			break;
731 		}
732 		break;
733 	default:
734 		break;
735 	}
736 
737 	if (cmd == MCE_IRDATA_TRAILER)
738 		dev_dbg(dev, "End of raw IR data");
739 	else if ((cmd != MCE_CMD_PORT_IR) &&
740 		 ((cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA))
741 		dev_dbg(dev, "Raw IR data, %d pulse/space samples", ir->rem);
742 #endif
743 }
744 
745 /*
746  * Schedule work that can't be done in interrupt handlers
747  * (mceusb_dev_recv() and mce_async_callback()) nor tasklets.
748  * Invokes mceusb_deferred_kevent() for recovering from
749  * error events specified by the kevent bit field.
750  */
751 static void mceusb_defer_kevent(struct mceusb_dev *ir, int kevent)
752 {
753 	set_bit(kevent, &ir->kevent_flags);
754 	if (!schedule_work(&ir->kevent))
755 		dev_err(ir->dev, "kevent %d may have been dropped", kevent);
756 	else
757 		dev_dbg(ir->dev, "kevent %d scheduled", kevent);
758 }
759 
760 static void mce_async_callback(struct urb *urb)
761 {
762 	struct mceusb_dev *ir;
763 	int len;
764 
765 	if (!urb)
766 		return;
767 
768 	ir = urb->context;
769 
770 	switch (urb->status) {
771 	/* success */
772 	case 0:
773 		len = urb->actual_length;
774 
775 		mceusb_dev_printdata(ir, urb->transfer_buffer, len,
776 				     0, len, true);
777 		break;
778 
779 	case -ECONNRESET:
780 	case -ENOENT:
781 	case -EILSEQ:
782 	case -ESHUTDOWN:
783 		break;
784 
785 	case -EPIPE:
786 		dev_err(ir->dev, "Error: request urb status = %d (TX HALT)",
787 			urb->status);
788 		mceusb_defer_kevent(ir, EVENT_TX_HALT);
789 		break;
790 
791 	default:
792 		dev_err(ir->dev, "Error: request urb status = %d", urb->status);
793 		break;
794 	}
795 
796 	/* the transfer buffer and urb were allocated in mce_request_packet */
797 	kfree(urb->transfer_buffer);
798 	usb_free_urb(urb);
799 }
800 
801 /* request outgoing (send) usb packet - used to initialize remote */
802 static void mce_request_packet(struct mceusb_dev *ir, unsigned char *data,
803 								int size)
804 {
805 	int res;
806 	struct urb *async_urb;
807 	struct device *dev = ir->dev;
808 	unsigned char *async_buf;
809 
810 	async_urb = usb_alloc_urb(0, GFP_KERNEL);
811 	if (unlikely(!async_urb)) {
812 		dev_err(dev, "Error, couldn't allocate urb!");
813 		return;
814 	}
815 
816 	async_buf = kmalloc(size, GFP_KERNEL);
817 	if (!async_buf) {
818 		usb_free_urb(async_urb);
819 		return;
820 	}
821 
822 	/* outbound data */
823 	if (usb_endpoint_xfer_int(ir->usb_ep_out))
824 		usb_fill_int_urb(async_urb, ir->usbdev, ir->pipe_out,
825 				 async_buf, size, mce_async_callback, ir,
826 				 ir->usb_ep_out->bInterval);
827 	else
828 		usb_fill_bulk_urb(async_urb, ir->usbdev, ir->pipe_out,
829 				  async_buf, size, mce_async_callback, ir);
830 
831 	memcpy(async_buf, data, size);
832 
833 	dev_dbg(dev, "send request called (size=%#x)", size);
834 
835 	res = usb_submit_urb(async_urb, GFP_ATOMIC);
836 	if (res) {
837 		dev_err(dev, "send request FAILED! (res=%d)", res);
838 		kfree(async_buf);
839 		usb_free_urb(async_urb);
840 		return;
841 	}
842 	dev_dbg(dev, "send request complete (res=%d)", res);
843 }
844 
845 static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
846 {
847 	int rsize = sizeof(DEVICE_RESUME);
848 
849 	if (ir->need_reset) {
850 		ir->need_reset = false;
851 		mce_request_packet(ir, DEVICE_RESUME, rsize);
852 		msleep(10);
853 	}
854 
855 	mce_request_packet(ir, data, size);
856 	msleep(10);
857 }
858 
859 /* Send data out the IR blaster port(s) */
860 static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
861 {
862 	struct mceusb_dev *ir = dev->priv;
863 	int i, length, ret = 0;
864 	int cmdcount = 0;
865 	unsigned char cmdbuf[MCE_CMDBUF_SIZE];
866 
867 	/* MCE tx init header */
868 	cmdbuf[cmdcount++] = MCE_CMD_PORT_IR;
869 	cmdbuf[cmdcount++] = MCE_CMD_SETIRTXPORTS;
870 	cmdbuf[cmdcount++] = ir->tx_mask;
871 
872 	/* Send the set TX ports command */
873 	mce_async_out(ir, cmdbuf, cmdcount);
874 	cmdcount = 0;
875 
876 	/* Generate mce packet data */
877 	for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
878 		txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
879 
880 		do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
881 
882 			/* Insert mce packet header every 4th entry */
883 			if ((cmdcount < MCE_CMDBUF_SIZE) &&
884 			    (cmdcount % MCE_CODE_LENGTH) == 0)
885 				cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
886 
887 			/* Insert mce packet data */
888 			if (cmdcount < MCE_CMDBUF_SIZE)
889 				cmdbuf[cmdcount++] =
890 					(txbuf[i] < MCE_PULSE_BIT ?
891 					 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
892 					 (i & 1 ? 0x00 : MCE_PULSE_BIT);
893 			else {
894 				ret = -EINVAL;
895 				goto out;
896 			}
897 
898 		} while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
899 			 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
900 	}
901 
902 	/* Check if we have room for the empty packet at the end */
903 	if (cmdcount >= MCE_CMDBUF_SIZE) {
904 		ret = -EINVAL;
905 		goto out;
906 	}
907 
908 	/* Fix packet length in last header */
909 	length = cmdcount % MCE_CODE_LENGTH;
910 	cmdbuf[cmdcount - length] -= MCE_CODE_LENGTH - length;
911 
912 	/* All mce commands end with an empty packet (0x80) */
913 	cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
914 
915 	/* Transmit the command to the mce device */
916 	mce_async_out(ir, cmdbuf, cmdcount);
917 
918 out:
919 	return ret ? ret : count;
920 }
921 
922 /* Sets active IR outputs -- mce devices typically have two */
923 static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
924 {
925 	struct mceusb_dev *ir = dev->priv;
926 
927 	/* return number of transmitters */
928 	int emitters = ir->num_txports ? ir->num_txports : 2;
929 
930 	if (mask >= (1 << emitters))
931 		return emitters;
932 
933 	if (ir->flags.tx_mask_normal)
934 		ir->tx_mask = mask;
935 	else
936 		ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
937 				mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
938 
939 	return 0;
940 }
941 
942 /* Sets the send carrier frequency and mode */
943 static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
944 {
945 	struct mceusb_dev *ir = dev->priv;
946 	int clk = 10000000;
947 	int prescaler = 0, divisor = 0;
948 	unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
949 				    MCE_CMD_SETIRCFS, 0x00, 0x00 };
950 
951 	/* Carrier has changed */
952 	if (ir->carrier != carrier) {
953 
954 		if (carrier == 0) {
955 			ir->carrier = carrier;
956 			cmdbuf[2] = MCE_CMD_SIG_END;
957 			cmdbuf[3] = MCE_IRDATA_TRAILER;
958 			dev_dbg(ir->dev, "disabling carrier modulation");
959 			mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
960 			return 0;
961 		}
962 
963 		for (prescaler = 0; prescaler < 4; ++prescaler) {
964 			divisor = (clk >> (2 * prescaler)) / carrier;
965 			if (divisor <= 0xff) {
966 				ir->carrier = carrier;
967 				cmdbuf[2] = prescaler;
968 				cmdbuf[3] = divisor;
969 				dev_dbg(ir->dev, "requesting %u HZ carrier",
970 								carrier);
971 
972 				/* Transmit new carrier to mce device */
973 				mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
974 				return 0;
975 			}
976 		}
977 
978 		return -EINVAL;
979 
980 	}
981 
982 	return 0;
983 }
984 
985 /*
986  * Select or deselect the 2nd receiver port.
987  * Second receiver is learning mode, wide-band, short-range receiver.
988  * Only one receiver (long or short range) may be active at a time.
989  */
990 static int mceusb_set_rx_wideband(struct rc_dev *dev, int enable)
991 {
992 	struct mceusb_dev *ir = dev->priv;
993 	unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
994 				    MCE_CMD_SETIRRXPORTEN, 0x00 };
995 
996 	dev_dbg(ir->dev, "select %s-range receive sensor",
997 		enable ? "short" : "long");
998 	if (enable) {
999 		ir->wideband_rx_enabled = true;
1000 		cmdbuf[2] = 2;	/* port 2 is short range receiver */
1001 	} else {
1002 		ir->wideband_rx_enabled = false;
1003 		cmdbuf[2] = 1;	/* port 1 is long range receiver */
1004 	}
1005 	mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
1006 	/* response from device sets ir->learning_active */
1007 
1008 	return 0;
1009 }
1010 
1011 /*
1012  * Enable/disable receiver carrier frequency pass through reporting.
1013  * Only the short-range receiver has carrier frequency measuring capability.
1014  * Implicitly select this receiver when enabling carrier frequency reporting.
1015  */
1016 static int mceusb_set_rx_carrier_report(struct rc_dev *dev, int enable)
1017 {
1018 	struct mceusb_dev *ir = dev->priv;
1019 	unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
1020 				    MCE_CMD_SETIRRXPORTEN, 0x00 };
1021 
1022 	dev_dbg(ir->dev, "%s short-range receiver carrier reporting",
1023 		enable ? "enable" : "disable");
1024 	if (enable) {
1025 		ir->carrier_report_enabled = true;
1026 		if (!ir->learning_active) {
1027 			cmdbuf[2] = 2;	/* port 2 is short range receiver */
1028 			mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
1029 		}
1030 	} else {
1031 		ir->carrier_report_enabled = false;
1032 		/*
1033 		 * Revert to normal (long-range) receiver only if the
1034 		 * wideband (short-range) receiver wasn't explicitly
1035 		 * enabled.
1036 		 */
1037 		if (ir->learning_active && !ir->wideband_rx_enabled) {
1038 			cmdbuf[2] = 1;	/* port 1 is long range receiver */
1039 			mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
1040 		}
1041 	}
1042 
1043 	return 0;
1044 }
1045 
1046 /*
1047  * We don't do anything but print debug spew for many of the command bits
1048  * we receive from the hardware, but some of them are useful information
1049  * we want to store so that we can use them.
1050  */
1051 static void mceusb_handle_command(struct mceusb_dev *ir, int index)
1052 {
1053 	DEFINE_IR_RAW_EVENT(rawir);
1054 	u8 hi = ir->buf_in[index + 1] & 0xff;
1055 	u8 lo = ir->buf_in[index + 2] & 0xff;
1056 	u32 carrier_cycles;
1057 	u32 cycles_fix;
1058 
1059 	switch (ir->buf_in[index]) {
1060 	/* the one and only 5-byte return value command */
1061 	case MCE_RSP_GETPORTSTATUS:
1062 		if ((ir->buf_in[index + 4] & 0xff) == 0x00)
1063 			ir->txports_cabled |= 1 << hi;
1064 		break;
1065 
1066 	/* 2-byte return value commands */
1067 	case MCE_RSP_EQIRTIMEOUT:
1068 		ir->rc->timeout = US_TO_NS((hi << 8 | lo) * MCE_TIME_UNIT);
1069 		break;
1070 	case MCE_RSP_EQIRNUMPORTS:
1071 		ir->num_txports = hi;
1072 		ir->num_rxports = lo;
1073 		break;
1074 	case MCE_RSP_EQIRRXCFCNT:
1075 		/*
1076 		 * The carrier cycle counter can overflow and wrap around
1077 		 * without notice from the device. So frequency measurement
1078 		 * will be inaccurate with long duration IR.
1079 		 *
1080 		 * The long-range (non learning) receiver always reports
1081 		 * zero count so we always ignore its report.
1082 		 */
1083 		if (ir->carrier_report_enabled && ir->learning_active &&
1084 		    ir->pulse_tunit > 0) {
1085 			carrier_cycles = (hi << 8 | lo);
1086 			/*
1087 			 * Adjust carrier cycle count by adding
1088 			 * 1 missed count per pulse "on"
1089 			 */
1090 			cycles_fix = ir->flags.rx2 == 2 ? ir->pulse_count : 0;
1091 			rawir.carrier_report = 1;
1092 			rawir.carrier = (1000000u / MCE_TIME_UNIT) *
1093 					(carrier_cycles + cycles_fix) /
1094 					ir->pulse_tunit;
1095 			dev_dbg(ir->dev, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)",
1096 				rawir.carrier, ir->pulse_count, carrier_cycles,
1097 				ir->pulse_tunit, ir->flags.rx2);
1098 			ir_raw_event_store(ir->rc, &rawir);
1099 		}
1100 		break;
1101 
1102 	/* 1-byte return value commands */
1103 	case MCE_RSP_EQEMVER:
1104 		ir->emver = hi;
1105 		break;
1106 	case MCE_RSP_EQIRTXPORTS:
1107 		ir->tx_mask = hi;
1108 		break;
1109 	case MCE_RSP_EQIRRXPORTEN:
1110 		ir->learning_active = ((hi & 0x02) == 0x02);
1111 		if (ir->rxports_active != hi) {
1112 			dev_info(ir->dev, "%s-range (0x%x) receiver active",
1113 				 ir->learning_active ? "short" : "long", hi);
1114 			ir->rxports_active = hi;
1115 		}
1116 		break;
1117 	case MCE_RSP_CMD_ILLEGAL:
1118 		ir->need_reset = true;
1119 		break;
1120 	default:
1121 		break;
1122 	}
1123 }
1124 
1125 static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
1126 {
1127 	DEFINE_IR_RAW_EVENT(rawir);
1128 	bool event = false;
1129 	int i = 0;
1130 
1131 	/* skip meaningless 0xb1 0x60 header bytes on orig receiver */
1132 	if (ir->flags.microsoft_gen1)
1133 		i = 2;
1134 
1135 	/* if there's no data, just return now */
1136 	if (buf_len <= i)
1137 		return;
1138 
1139 	for (; i < buf_len; i++) {
1140 		switch (ir->parser_state) {
1141 		case SUBCMD:
1142 			ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]);
1143 			mceusb_dev_printdata(ir, ir->buf_in, buf_len, i - 1,
1144 					     ir->rem + 2, false);
1145 			mceusb_handle_command(ir, i);
1146 			ir->parser_state = CMD_DATA;
1147 			break;
1148 		case PARSE_IRDATA:
1149 			ir->rem--;
1150 			init_ir_raw_event(&rawir);
1151 			rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
1152 			rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK);
1153 			if (rawir.pulse) {
1154 				ir->pulse_tunit += rawir.duration;
1155 				ir->pulse_count++;
1156 			}
1157 			rawir.duration *= US_TO_NS(MCE_TIME_UNIT);
1158 
1159 			dev_dbg(ir->dev, "Storing %s %u ns (%02x)",
1160 				rawir.pulse ? "pulse" : "space",
1161 				rawir.duration,	ir->buf_in[i]);
1162 
1163 			if (ir_raw_event_store_with_filter(ir->rc, &rawir))
1164 				event = true;
1165 			break;
1166 		case CMD_DATA:
1167 			ir->rem--;
1168 			break;
1169 		case CMD_HEADER:
1170 			/* decode mce packets of the form (84),AA,BB,CC,DD */
1171 			/* IR data packets can span USB messages - rem */
1172 			ir->cmd = ir->buf_in[i];
1173 			if ((ir->cmd == MCE_CMD_PORT_IR) ||
1174 			    ((ir->cmd & MCE_PORT_MASK) !=
1175 			     MCE_COMMAND_IRDATA)) {
1176 				ir->parser_state = SUBCMD;
1177 				continue;
1178 			}
1179 			ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
1180 			mceusb_dev_printdata(ir, ir->buf_in, buf_len,
1181 					     i, ir->rem + 1, false);
1182 			if (ir->rem) {
1183 				ir->parser_state = PARSE_IRDATA;
1184 			} else {
1185 				ir_raw_event_reset(ir->rc);
1186 				ir->pulse_tunit = 0;
1187 				ir->pulse_count = 0;
1188 			}
1189 			break;
1190 		}
1191 
1192 		if (ir->parser_state != CMD_HEADER && !ir->rem)
1193 			ir->parser_state = CMD_HEADER;
1194 	}
1195 	if (event) {
1196 		dev_dbg(ir->dev, "processed IR data");
1197 		ir_raw_event_handle(ir->rc);
1198 	}
1199 }
1200 
1201 static void mceusb_dev_recv(struct urb *urb)
1202 {
1203 	struct mceusb_dev *ir;
1204 
1205 	if (!urb)
1206 		return;
1207 
1208 	ir = urb->context;
1209 	if (!ir) {
1210 		usb_unlink_urb(urb);
1211 		return;
1212 	}
1213 
1214 	switch (urb->status) {
1215 	/* success */
1216 	case 0:
1217 		mceusb_process_ir_data(ir, urb->actual_length);
1218 		break;
1219 
1220 	case -ECONNRESET:
1221 	case -ENOENT:
1222 	case -EILSEQ:
1223 	case -ESHUTDOWN:
1224 		usb_unlink_urb(urb);
1225 		return;
1226 
1227 	case -EPIPE:
1228 		dev_err(ir->dev, "Error: urb status = %d (RX HALT)",
1229 			urb->status);
1230 		mceusb_defer_kevent(ir, EVENT_RX_HALT);
1231 		return;
1232 
1233 	default:
1234 		dev_err(ir->dev, "Error: urb status = %d", urb->status);
1235 		break;
1236 	}
1237 
1238 	usb_submit_urb(urb, GFP_ATOMIC);
1239 }
1240 
1241 static void mceusb_get_emulator_version(struct mceusb_dev *ir)
1242 {
1243 	/* If we get no reply or an illegal command reply, its ver 1, says MS */
1244 	ir->emver = 1;
1245 	mce_async_out(ir, GET_EMVER, sizeof(GET_EMVER));
1246 }
1247 
1248 static void mceusb_gen1_init(struct mceusb_dev *ir)
1249 {
1250 	int ret;
1251 	struct device *dev = ir->dev;
1252 	char *data;
1253 
1254 	data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
1255 	if (!data) {
1256 		dev_err(dev, "%s: memory allocation failed!", __func__);
1257 		return;
1258 	}
1259 
1260 	/*
1261 	 * This is a strange one. Windows issues a set address to the device
1262 	 * on the receive control pipe and expect a certain value pair back
1263 	 */
1264 	ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
1265 			      USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
1266 			      data, USB_CTRL_MSG_SZ, HZ * 3);
1267 	dev_dbg(dev, "set address - ret = %d", ret);
1268 	dev_dbg(dev, "set address - data[0] = %d, data[1] = %d",
1269 						data[0], data[1]);
1270 
1271 	/* set feature: bit rate 38400 bps */
1272 	ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1273 			      USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
1274 			      0xc04e, 0x0000, NULL, 0, HZ * 3);
1275 
1276 	dev_dbg(dev, "set feature - ret = %d", ret);
1277 
1278 	/* bRequest 4: set char length to 8 bits */
1279 	ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1280 			      4, USB_TYPE_VENDOR,
1281 			      0x0808, 0x0000, NULL, 0, HZ * 3);
1282 	dev_dbg(dev, "set char length - retB = %d", ret);
1283 
1284 	/* bRequest 2: set handshaking to use DTR/DSR */
1285 	ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1286 			      2, USB_TYPE_VENDOR,
1287 			      0x0000, 0x0100, NULL, 0, HZ * 3);
1288 	dev_dbg(dev, "set handshake  - retC = %d", ret);
1289 
1290 	/* device resume */
1291 	mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1292 
1293 	/* get hw/sw revision? */
1294 	mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
1295 
1296 	kfree(data);
1297 }
1298 
1299 static void mceusb_gen2_init(struct mceusb_dev *ir)
1300 {
1301 	/* device resume */
1302 	mce_async_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1303 
1304 	/* get wake version (protocol, key, address) */
1305 	mce_async_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));
1306 
1307 	/* unknown what this one actually returns... */
1308 	mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
1309 }
1310 
1311 static void mceusb_get_parameters(struct mceusb_dev *ir)
1312 {
1313 	int i;
1314 	unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
1315 				    MCE_CMD_GETPORTSTATUS, 0x00 };
1316 
1317 	/* defaults, if the hardware doesn't support querying */
1318 	ir->num_txports = 2;
1319 	ir->num_rxports = 2;
1320 
1321 	/* get number of tx and rx ports */
1322 	mce_async_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));
1323 
1324 	/* get the carrier and frequency */
1325 	mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
1326 
1327 	if (ir->num_txports && !ir->flags.no_tx)
1328 		/* get the transmitter bitmask */
1329 		mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
1330 
1331 	/* get receiver timeout value */
1332 	mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
1333 
1334 	/* get receiver sensor setting */
1335 	mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
1336 
1337 	for (i = 0; i < ir->num_txports; i++) {
1338 		cmdbuf[2] = i;
1339 		mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
1340 	}
1341 }
1342 
1343 static void mceusb_flash_led(struct mceusb_dev *ir)
1344 {
1345 	if (ir->emver < 2)
1346 		return;
1347 
1348 	mce_async_out(ir, FLASH_LED, sizeof(FLASH_LED));
1349 }
1350 
1351 /*
1352  * Workqueue function
1353  * for resetting or recovering device after occurrence of error events
1354  * specified in ir->kevent bit field.
1355  * Function runs (via schedule_work()) in non-interrupt context, for
1356  * calls here (such as usb_clear_halt()) requiring non-interrupt context.
1357  */
1358 static void mceusb_deferred_kevent(struct work_struct *work)
1359 {
1360 	struct mceusb_dev *ir =
1361 		container_of(work, struct mceusb_dev, kevent);
1362 	int status;
1363 
1364 	if (test_bit(EVENT_RX_HALT, &ir->kevent_flags)) {
1365 		usb_unlink_urb(ir->urb_in);
1366 		status = usb_clear_halt(ir->usbdev, ir->pipe_in);
1367 		if (status < 0) {
1368 			dev_err(ir->dev, "rx clear halt error %d",
1369 				status);
1370 		}
1371 		clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
1372 		if (status == 0) {
1373 			status = usb_submit_urb(ir->urb_in, GFP_KERNEL);
1374 			if (status < 0) {
1375 				dev_err(ir->dev,
1376 					"rx unhalt submit urb error %d",
1377 					status);
1378 			}
1379 		}
1380 	}
1381 
1382 	if (test_bit(EVENT_TX_HALT, &ir->kevent_flags)) {
1383 		status = usb_clear_halt(ir->usbdev, ir->pipe_out);
1384 		if (status < 0)
1385 			dev_err(ir->dev, "tx clear halt error %d", status);
1386 		clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
1387 	}
1388 }
1389 
1390 static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
1391 {
1392 	struct usb_device *udev = ir->usbdev;
1393 	struct device *dev = ir->dev;
1394 	struct rc_dev *rc;
1395 	int ret;
1396 
1397 	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
1398 	if (!rc) {
1399 		dev_err(dev, "remote dev allocation failed");
1400 		goto out;
1401 	}
1402 
1403 	snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1404 		 mceusb_model[ir->model].name ?
1405 			mceusb_model[ir->model].name :
1406 			"Media Center Ed. eHome Infrared Remote Transceiver",
1407 		 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1408 		 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1409 
1410 	usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
1411 
1412 	rc->device_name = ir->name;
1413 	rc->input_phys = ir->phys;
1414 	usb_to_input_id(ir->usbdev, &rc->input_id);
1415 	rc->dev.parent = dev;
1416 	rc->priv = ir;
1417 	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
1418 	rc->timeout = MS_TO_NS(100);
1419 	if (!ir->flags.no_tx) {
1420 		rc->s_tx_mask = mceusb_set_tx_mask;
1421 		rc->s_tx_carrier = mceusb_set_tx_carrier;
1422 		rc->tx_ir = mceusb_tx_ir;
1423 	}
1424 	if (ir->flags.rx2 > 0) {
1425 		rc->s_learning_mode = mceusb_set_rx_wideband;
1426 		rc->s_carrier_report = mceusb_set_rx_carrier_report;
1427 	}
1428 	rc->driver_name = DRIVER_NAME;
1429 
1430 	switch (le16_to_cpu(udev->descriptor.idVendor)) {
1431 	case VENDOR_HAUPPAUGE:
1432 		rc->map_name = RC_MAP_HAUPPAUGE;
1433 		break;
1434 	case VENDOR_PCTV:
1435 		rc->map_name = RC_MAP_PINNACLE_PCTV_HD;
1436 		break;
1437 	default:
1438 		rc->map_name = RC_MAP_RC6_MCE;
1439 	}
1440 	if (mceusb_model[ir->model].rc_map)
1441 		rc->map_name = mceusb_model[ir->model].rc_map;
1442 
1443 	ret = rc_register_device(rc);
1444 	if (ret < 0) {
1445 		dev_err(dev, "remote dev registration failed");
1446 		goto out;
1447 	}
1448 
1449 	return rc;
1450 
1451 out:
1452 	rc_free_device(rc);
1453 	return NULL;
1454 }
1455 
1456 static int mceusb_dev_probe(struct usb_interface *intf,
1457 			    const struct usb_device_id *id)
1458 {
1459 	struct usb_device *dev = interface_to_usbdev(intf);
1460 	struct usb_host_interface *idesc;
1461 	struct usb_endpoint_descriptor *ep = NULL;
1462 	struct usb_endpoint_descriptor *ep_in = NULL;
1463 	struct usb_endpoint_descriptor *ep_out = NULL;
1464 	struct mceusb_dev *ir = NULL;
1465 	int pipe, maxp, i, res;
1466 	char buf[63], name[128] = "";
1467 	enum mceusb_model_type model = id->driver_info;
1468 	bool is_gen3;
1469 	bool is_microsoft_gen1;
1470 	bool tx_mask_normal;
1471 	int ir_intfnum;
1472 
1473 	dev_dbg(&intf->dev, "%s called", __func__);
1474 
1475 	idesc  = intf->cur_altsetting;
1476 
1477 	is_gen3 = mceusb_model[model].mce_gen3;
1478 	is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1479 	tx_mask_normal = mceusb_model[model].tx_mask_normal;
1480 	ir_intfnum = mceusb_model[model].ir_intfnum;
1481 
1482 	/* There are multi-function devices with non-IR interfaces */
1483 	if (idesc->desc.bInterfaceNumber != ir_intfnum)
1484 		return -ENODEV;
1485 
1486 	/* step through the endpoints to find first bulk in and out endpoint */
1487 	for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1488 		ep = &idesc->endpoint[i].desc;
1489 
1490 		if (ep_in == NULL) {
1491 			if (usb_endpoint_is_bulk_in(ep)) {
1492 				ep_in = ep;
1493 				dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n");
1494 			} else if (usb_endpoint_is_int_in(ep)) {
1495 				ep_in = ep;
1496 				ep_in->bInterval = 1;
1497 				dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n");
1498 			}
1499 		}
1500 
1501 		if (ep_out == NULL) {
1502 			if (usb_endpoint_is_bulk_out(ep)) {
1503 				ep_out = ep;
1504 				dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n");
1505 			} else if (usb_endpoint_is_int_out(ep)) {
1506 				ep_out = ep;
1507 				ep_out->bInterval = 1;
1508 				dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n");
1509 			}
1510 		}
1511 	}
1512 	if (!ep_in || !ep_out) {
1513 		dev_dbg(&intf->dev, "required endpoints not found\n");
1514 		return -ENODEV;
1515 	}
1516 
1517 	if (usb_endpoint_xfer_int(ep_in))
1518 		pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1519 	else
1520 		pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress);
1521 	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1522 
1523 	ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1524 	if (!ir)
1525 		goto mem_alloc_fail;
1526 
1527 	ir->pipe_in = pipe;
1528 	ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1529 	if (!ir->buf_in)
1530 		goto buf_in_alloc_fail;
1531 
1532 	ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1533 	if (!ir->urb_in)
1534 		goto urb_in_alloc_fail;
1535 
1536 	ir->usbdev = usb_get_dev(dev);
1537 	ir->dev = &intf->dev;
1538 	ir->len_in = maxp;
1539 	ir->flags.microsoft_gen1 = is_microsoft_gen1;
1540 	ir->flags.tx_mask_normal = tx_mask_normal;
1541 	ir->flags.no_tx = mceusb_model[model].no_tx;
1542 	ir->flags.rx2 = mceusb_model[model].rx2;
1543 	ir->model = model;
1544 
1545 	/* Saving usb interface data for use by the transmitter routine */
1546 	ir->usb_ep_out = ep_out;
1547 	if (usb_endpoint_xfer_int(ep_out))
1548 		ir->pipe_out = usb_sndintpipe(ir->usbdev,
1549 					      ep_out->bEndpointAddress);
1550 	else
1551 		ir->pipe_out = usb_sndbulkpipe(ir->usbdev,
1552 					       ep_out->bEndpointAddress);
1553 
1554 	if (dev->descriptor.iManufacturer
1555 	    && usb_string(dev, dev->descriptor.iManufacturer,
1556 			  buf, sizeof(buf)) > 0)
1557 		strlcpy(name, buf, sizeof(name));
1558 	if (dev->descriptor.iProduct
1559 	    && usb_string(dev, dev->descriptor.iProduct,
1560 			  buf, sizeof(buf)) > 0)
1561 		snprintf(name + strlen(name), sizeof(name) - strlen(name),
1562 			 " %s", buf);
1563 
1564 	/*
1565 	 * Initialize async USB error handler before registering
1566 	 * or activating any mceusb RX and TX functions
1567 	 */
1568 	INIT_WORK(&ir->kevent, mceusb_deferred_kevent);
1569 
1570 	ir->rc = mceusb_init_rc_dev(ir);
1571 	if (!ir->rc)
1572 		goto rc_dev_fail;
1573 
1574 	/* wire up inbound data handler */
1575 	if (usb_endpoint_xfer_int(ep_in))
1576 		usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1577 				 mceusb_dev_recv, ir, ep_in->bInterval);
1578 	else
1579 		usb_fill_bulk_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1580 				  mceusb_dev_recv, ir);
1581 
1582 	ir->urb_in->transfer_dma = ir->dma_in;
1583 	ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1584 
1585 	/* flush buffers on the device */
1586 	dev_dbg(&intf->dev, "Flushing receive buffers");
1587 	res = usb_submit_urb(ir->urb_in, GFP_KERNEL);
1588 	if (res)
1589 		dev_err(&intf->dev, "failed to flush buffers: %d", res);
1590 
1591 	/* figure out which firmware/emulator version this hardware has */
1592 	mceusb_get_emulator_version(ir);
1593 
1594 	/* initialize device */
1595 	if (ir->flags.microsoft_gen1)
1596 		mceusb_gen1_init(ir);
1597 	else if (!is_gen3)
1598 		mceusb_gen2_init(ir);
1599 
1600 	mceusb_get_parameters(ir);
1601 
1602 	mceusb_flash_led(ir);
1603 
1604 	if (!ir->flags.no_tx)
1605 		mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
1606 
1607 	usb_set_intfdata(intf, ir);
1608 
1609 	/* enable wake via this device */
1610 	device_set_wakeup_capable(ir->dev, true);
1611 	device_set_wakeup_enable(ir->dev, true);
1612 
1613 	dev_info(&intf->dev, "Registered %s with mce emulator interface version %x",
1614 		name, ir->emver);
1615 	dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
1616 		 ir->num_txports, ir->txports_cabled,
1617 		 ir->num_rxports, ir->rxports_active);
1618 
1619 	return 0;
1620 
1621 	/* Error-handling path */
1622 rc_dev_fail:
1623 	cancel_work_sync(&ir->kevent);
1624 	usb_put_dev(ir->usbdev);
1625 	usb_kill_urb(ir->urb_in);
1626 	usb_free_urb(ir->urb_in);
1627 urb_in_alloc_fail:
1628 	usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1629 buf_in_alloc_fail:
1630 	kfree(ir);
1631 mem_alloc_fail:
1632 	dev_err(&intf->dev, "%s: device setup failed!", __func__);
1633 
1634 	return -ENOMEM;
1635 }
1636 
1637 
1638 static void mceusb_dev_disconnect(struct usb_interface *intf)
1639 {
1640 	struct usb_device *dev = interface_to_usbdev(intf);
1641 	struct mceusb_dev *ir = usb_get_intfdata(intf);
1642 
1643 	usb_set_intfdata(intf, NULL);
1644 
1645 	if (!ir)
1646 		return;
1647 
1648 	ir->usbdev = NULL;
1649 	cancel_work_sync(&ir->kevent);
1650 	rc_unregister_device(ir->rc);
1651 	usb_kill_urb(ir->urb_in);
1652 	usb_free_urb(ir->urb_in);
1653 	usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1654 	usb_put_dev(dev);
1655 
1656 	kfree(ir);
1657 }
1658 
1659 static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1660 {
1661 	struct mceusb_dev *ir = usb_get_intfdata(intf);
1662 	dev_info(ir->dev, "suspend");
1663 	usb_kill_urb(ir->urb_in);
1664 	return 0;
1665 }
1666 
1667 static int mceusb_dev_resume(struct usb_interface *intf)
1668 {
1669 	struct mceusb_dev *ir = usb_get_intfdata(intf);
1670 	dev_info(ir->dev, "resume");
1671 	if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1672 		return -EIO;
1673 	return 0;
1674 }
1675 
1676 static struct usb_driver mceusb_dev_driver = {
1677 	.name =		DRIVER_NAME,
1678 	.probe =	mceusb_dev_probe,
1679 	.disconnect =	mceusb_dev_disconnect,
1680 	.suspend =	mceusb_dev_suspend,
1681 	.resume =	mceusb_dev_resume,
1682 	.reset_resume =	mceusb_dev_resume,
1683 	.id_table =	mceusb_dev_table
1684 };
1685 
1686 module_usb_driver(mceusb_dev_driver);
1687 
1688 MODULE_DESCRIPTION(DRIVER_DESC);
1689 MODULE_AUTHOR(DRIVER_AUTHOR);
1690 MODULE_LICENSE("GPL");
1691 MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1692