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