18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
88c2ecf20Sopenharmony_ci * Conti, Martin Blatter and Daniel Melander, the latter of which was
98c2ecf20Sopenharmony_ci * in turn also based on the lirc_atiusb driver by Paul Miller. The
108c2ecf20Sopenharmony_ci * two mce drivers were merged into one by Jarod Wilson, with transmit
118c2ecf20Sopenharmony_ci * support for the 1st-gen device added primarily by Patrick Calhoun,
128c2ecf20Sopenharmony_ci * with a bit of tweaks by Jarod. Debugging improvements and proper
138c2ecf20Sopenharmony_ci * support for what appears to be 3rd-gen hardware added by Jarod.
148c2ecf20Sopenharmony_ci * Initial port from lirc driver to ir-core drivery by Jarod, based
158c2ecf20Sopenharmony_ci * partially on a port to an earlier proposed IR infrastructure by
168c2ecf20Sopenharmony_ci * Jon Smirl, which included enhancements and simplifications to the
178c2ecf20Sopenharmony_ci * incoming IR buffer parsing routines.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Updated in July of 2011 with the aid of Microsoft's official
208c2ecf20Sopenharmony_ci * remote/transceiver requirements and specification document, found at
218c2ecf20Sopenharmony_ci * download.microsoft.com, title
228c2ecf20Sopenharmony_ci * Windows-Media-Center-RC-IR-Collection-Green-Button-Specification-03-08-2011-V2.pdf
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/device.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
298c2ecf20Sopenharmony_ci#include <linux/usb.h>
308c2ecf20Sopenharmony_ci#include <linux/usb/input.h>
318c2ecf20Sopenharmony_ci#include <linux/pm_wakeup.h>
328c2ecf20Sopenharmony_ci#include <media/rc-core.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define DRIVER_VERSION	"1.95"
358c2ecf20Sopenharmony_ci#define DRIVER_AUTHOR	"Jarod Wilson <jarod@redhat.com>"
368c2ecf20Sopenharmony_ci#define DRIVER_DESC	"Windows Media Center Ed. eHome Infrared Transceiver " \
378c2ecf20Sopenharmony_ci			"device driver"
388c2ecf20Sopenharmony_ci#define DRIVER_NAME	"mceusb"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define USB_TX_TIMEOUT		1000 /* in milliseconds */
418c2ecf20Sopenharmony_ci#define USB_CTRL_MSG_SZ		2  /* Size of usb ctrl msg on gen1 hw */
428c2ecf20Sopenharmony_ci#define MCE_G1_INIT_MSGS	40 /* Init messages on gen1 hw to throw out */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* MCE constants */
458c2ecf20Sopenharmony_ci#define MCE_IRBUF_SIZE		128  /* TX IR buffer length */
468c2ecf20Sopenharmony_ci#define MCE_TIME_UNIT		50   /* Approx 50us resolution */
478c2ecf20Sopenharmony_ci#define MCE_PACKET_SIZE		31   /* Max length of packet (with header) */
488c2ecf20Sopenharmony_ci#define MCE_IRDATA_HEADER	(0x80 + MCE_PACKET_SIZE - 1)
498c2ecf20Sopenharmony_ci				     /* Actual format is 0x80 + num_bytes */
508c2ecf20Sopenharmony_ci#define MCE_IRDATA_TRAILER	0x80 /* End of IR data */
518c2ecf20Sopenharmony_ci#define MCE_MAX_CHANNELS	2    /* Two transmitters, hardware dependent? */
528c2ecf20Sopenharmony_ci#define MCE_DEFAULT_TX_MASK	0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
538c2ecf20Sopenharmony_ci#define MCE_PULSE_BIT		0x80 /* Pulse bit, MSB set == PULSE else SPACE */
548c2ecf20Sopenharmony_ci#define MCE_PULSE_MASK		0x7f /* Pulse mask */
558c2ecf20Sopenharmony_ci#define MCE_MAX_PULSE_LENGTH	0x7f /* Longest transmittable pulse symbol */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/*
588c2ecf20Sopenharmony_ci * The interface between the host and the IR hardware is command-response
598c2ecf20Sopenharmony_ci * based. All commands and responses have a consistent format, where a lead
608c2ecf20Sopenharmony_ci * byte always identifies the type of data following it. The lead byte has
618c2ecf20Sopenharmony_ci * a port value in the 3 highest bits and a length value in the 5 lowest
628c2ecf20Sopenharmony_ci * bits.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * The length field is overloaded, with a value of 11111 indicating that the
658c2ecf20Sopenharmony_ci * following byte is a command or response code, and the length of the entire
668c2ecf20Sopenharmony_ci * message is determined by the code. If the length field is not 11111, then
678c2ecf20Sopenharmony_ci * it specifies the number of bytes of port data that follow.
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_ci#define MCE_CMD			0x1f
708c2ecf20Sopenharmony_ci#define MCE_PORT_IR		0x4	/* (0x4 << 5) | MCE_CMD = 0x9f */
718c2ecf20Sopenharmony_ci#define MCE_PORT_SYS		0x7	/* (0x7 << 5) | MCE_CMD = 0xff */
728c2ecf20Sopenharmony_ci#define MCE_PORT_SER		0x6	/* 0xc0 through 0xdf flush & 0x1f bytes */
738c2ecf20Sopenharmony_ci#define MCE_PORT_MASK		0xe0	/* Mask out command bits */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* Command port headers */
768c2ecf20Sopenharmony_ci#define MCE_CMD_PORT_IR		0x9f	/* IR-related cmd/rsp */
778c2ecf20Sopenharmony_ci#define MCE_CMD_PORT_SYS	0xff	/* System (non-IR) device cmd/rsp */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* Commands that set device state  (2-4 bytes in length) */
808c2ecf20Sopenharmony_ci#define MCE_CMD_RESET		0xfe	/* Reset device, 2 bytes */
818c2ecf20Sopenharmony_ci#define MCE_CMD_RESUME		0xaa	/* Resume device after error, 2 bytes */
828c2ecf20Sopenharmony_ci#define MCE_CMD_SETIRCFS	0x06	/* Set tx carrier, 4 bytes */
838c2ecf20Sopenharmony_ci#define MCE_CMD_SETIRTIMEOUT	0x0c	/* Set timeout, 4 bytes */
848c2ecf20Sopenharmony_ci#define MCE_CMD_SETIRTXPORTS	0x08	/* Set tx ports, 3 bytes */
858c2ecf20Sopenharmony_ci#define MCE_CMD_SETIRRXPORTEN	0x14	/* Set rx ports, 3 bytes */
868c2ecf20Sopenharmony_ci#define MCE_CMD_FLASHLED	0x23	/* Flash receiver LED, 2 bytes */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* Commands that query device state (all 2 bytes, unless noted) */
898c2ecf20Sopenharmony_ci#define MCE_CMD_GETIRCFS	0x07	/* Get carrier */
908c2ecf20Sopenharmony_ci#define MCE_CMD_GETIRTIMEOUT	0x0d	/* Get timeout */
918c2ecf20Sopenharmony_ci#define MCE_CMD_GETIRTXPORTS	0x13	/* Get tx ports */
928c2ecf20Sopenharmony_ci#define MCE_CMD_GETIRRXPORTEN	0x15	/* Get rx ports */
938c2ecf20Sopenharmony_ci#define MCE_CMD_GETPORTSTATUS	0x11	/* Get tx port status, 3 bytes */
948c2ecf20Sopenharmony_ci#define MCE_CMD_GETIRNUMPORTS	0x16	/* Get number of ports */
958c2ecf20Sopenharmony_ci#define MCE_CMD_GETWAKESOURCE	0x17	/* Get wake source */
968c2ecf20Sopenharmony_ci#define MCE_CMD_GETEMVER	0x22	/* Get emulator interface version */
978c2ecf20Sopenharmony_ci#define MCE_CMD_GETDEVDETAILS	0x21	/* Get device details (em ver2 only) */
988c2ecf20Sopenharmony_ci#define MCE_CMD_GETWAKESUPPORT	0x20	/* Get wake details (em ver2 only) */
998c2ecf20Sopenharmony_ci#define MCE_CMD_GETWAKEVERSION	0x18	/* Get wake pattern (em ver2 only) */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* Misc commands */
1028c2ecf20Sopenharmony_ci#define MCE_CMD_NOP		0xff	/* No operation */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* Responses to commands (non-error cases) */
1058c2ecf20Sopenharmony_ci#define MCE_RSP_EQIRCFS		0x06	/* tx carrier, 4 bytes */
1068c2ecf20Sopenharmony_ci#define MCE_RSP_EQIRTIMEOUT	0x0c	/* rx timeout, 4 bytes */
1078c2ecf20Sopenharmony_ci#define MCE_RSP_GETWAKESOURCE	0x17	/* wake source, 3 bytes */
1088c2ecf20Sopenharmony_ci#define MCE_RSP_EQIRTXPORTS	0x08	/* tx port mask, 3 bytes */
1098c2ecf20Sopenharmony_ci#define MCE_RSP_EQIRRXPORTEN	0x14	/* rx port mask, 3 bytes */
1108c2ecf20Sopenharmony_ci#define MCE_RSP_GETPORTSTATUS	0x11	/* tx port status, 7 bytes */
1118c2ecf20Sopenharmony_ci#define MCE_RSP_EQIRRXCFCNT	0x15	/* rx carrier count, 4 bytes */
1128c2ecf20Sopenharmony_ci#define MCE_RSP_EQIRNUMPORTS	0x16	/* number of ports, 4 bytes */
1138c2ecf20Sopenharmony_ci#define MCE_RSP_EQWAKESUPPORT	0x20	/* wake capabilities, 3 bytes */
1148c2ecf20Sopenharmony_ci#define MCE_RSP_EQWAKEVERSION	0x18	/* wake pattern details, 6 bytes */
1158c2ecf20Sopenharmony_ci#define MCE_RSP_EQDEVDETAILS	0x21	/* device capabilities, 3 bytes */
1168c2ecf20Sopenharmony_ci#define MCE_RSP_EQEMVER		0x22	/* emulator interface ver, 3 bytes */
1178c2ecf20Sopenharmony_ci#define MCE_RSP_FLASHLED	0x23	/* success flashing LED, 2 bytes */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* Responses to error cases, must send MCE_CMD_RESUME to clear them */
1208c2ecf20Sopenharmony_ci#define MCE_RSP_CMD_ILLEGAL	0xfe	/* illegal command for port, 2 bytes */
1218c2ecf20Sopenharmony_ci#define MCE_RSP_TX_TIMEOUT	0x81	/* tx timed out, 2 bytes */
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* Misc commands/responses not defined in the MCE remote/transceiver spec */
1248c2ecf20Sopenharmony_ci#define MCE_CMD_SIG_END		0x01	/* End of signal */
1258c2ecf20Sopenharmony_ci#define MCE_CMD_PING		0x03	/* Ping device */
1268c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN		0x04	/* Unknown */
1278c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN2	0x05	/* Unknown */
1288c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN3	0x09	/* Unknown */
1298c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN4	0x0a	/* Unknown */
1308c2ecf20Sopenharmony_ci#define MCE_CMD_G_REVISION	0x0b	/* Get hw/sw revision */
1318c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN5	0x0e	/* Unknown */
1328c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN6	0x0f	/* Unknown */
1338c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN8	0x19	/* Unknown */
1348c2ecf20Sopenharmony_ci#define MCE_CMD_UNKNOWN9	0x1b	/* Unknown */
1358c2ecf20Sopenharmony_ci#define MCE_CMD_NULL		0x00	/* These show up various places... */
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
1388c2ecf20Sopenharmony_ci * then we're looking at a raw IR data sample */
1398c2ecf20Sopenharmony_ci#define MCE_COMMAND_IRDATA	0x80
1408c2ecf20Sopenharmony_ci#define MCE_PACKET_LENGTH_MASK	0x1f /* Packet length mask */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#define VENDOR_PHILIPS		0x0471
1438c2ecf20Sopenharmony_ci#define VENDOR_SMK		0x0609
1448c2ecf20Sopenharmony_ci#define VENDOR_TATUNG		0x1460
1458c2ecf20Sopenharmony_ci#define VENDOR_GATEWAY		0x107b
1468c2ecf20Sopenharmony_ci#define VENDOR_SHUTTLE		0x1308
1478c2ecf20Sopenharmony_ci#define VENDOR_SHUTTLE2		0x051c
1488c2ecf20Sopenharmony_ci#define VENDOR_MITSUMI		0x03ee
1498c2ecf20Sopenharmony_ci#define VENDOR_TOPSEED		0x1784
1508c2ecf20Sopenharmony_ci#define VENDOR_RICAVISION	0x179d
1518c2ecf20Sopenharmony_ci#define VENDOR_ITRON		0x195d
1528c2ecf20Sopenharmony_ci#define VENDOR_FIC		0x1509
1538c2ecf20Sopenharmony_ci#define VENDOR_LG		0x043e
1548c2ecf20Sopenharmony_ci#define VENDOR_MICROSOFT	0x045e
1558c2ecf20Sopenharmony_ci#define VENDOR_FORMOSA		0x147a
1568c2ecf20Sopenharmony_ci#define VENDOR_FINTEK		0x1934
1578c2ecf20Sopenharmony_ci#define VENDOR_PINNACLE		0x2304
1588c2ecf20Sopenharmony_ci#define VENDOR_ECS		0x1019
1598c2ecf20Sopenharmony_ci#define VENDOR_WISTRON		0x0fb8
1608c2ecf20Sopenharmony_ci#define VENDOR_COMPRO		0x185b
1618c2ecf20Sopenharmony_ci#define VENDOR_NORTHSTAR	0x04eb
1628c2ecf20Sopenharmony_ci#define VENDOR_REALTEK		0x0bda
1638c2ecf20Sopenharmony_ci#define VENDOR_TIVO		0x105a
1648c2ecf20Sopenharmony_ci#define VENDOR_CONEXANT		0x0572
1658c2ecf20Sopenharmony_ci#define VENDOR_TWISTEDMELON	0x2596
1668c2ecf20Sopenharmony_ci#define VENDOR_HAUPPAUGE	0x2040
1678c2ecf20Sopenharmony_ci#define VENDOR_PCTV		0x2013
1688c2ecf20Sopenharmony_ci#define VENDOR_ADAPTEC		0x03f3
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cienum mceusb_model_type {
1718c2ecf20Sopenharmony_ci	MCE_GEN2 = 0,		/* Most boards */
1728c2ecf20Sopenharmony_ci	MCE_GEN1,
1738c2ecf20Sopenharmony_ci	MCE_GEN3,
1748c2ecf20Sopenharmony_ci	MCE_GEN3_BROKEN_IRTIMEOUT,
1758c2ecf20Sopenharmony_ci	MCE_GEN2_TX_INV,
1768c2ecf20Sopenharmony_ci	MCE_GEN2_TX_INV_RX_GOOD,
1778c2ecf20Sopenharmony_ci	POLARIS_EVK,
1788c2ecf20Sopenharmony_ci	CX_HYBRID_TV,
1798c2ecf20Sopenharmony_ci	MULTIFUNCTION,
1808c2ecf20Sopenharmony_ci	TIVO_KIT,
1818c2ecf20Sopenharmony_ci	MCE_GEN2_NO_TX,
1828c2ecf20Sopenharmony_ci	HAUPPAUGE_CX_HYBRID_TV,
1838c2ecf20Sopenharmony_ci	EVROMEDIA_FULL_HYBRID_FULLHD,
1848c2ecf20Sopenharmony_ci	ASTROMETA_T2HYBRID,
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistruct mceusb_model {
1888c2ecf20Sopenharmony_ci	u32 mce_gen1:1;
1898c2ecf20Sopenharmony_ci	u32 mce_gen2:1;
1908c2ecf20Sopenharmony_ci	u32 mce_gen3:1;
1918c2ecf20Sopenharmony_ci	u32 tx_mask_normal:1;
1928c2ecf20Sopenharmony_ci	u32 no_tx:1;
1938c2ecf20Sopenharmony_ci	u32 broken_irtimeout:1;
1948c2ecf20Sopenharmony_ci	/*
1958c2ecf20Sopenharmony_ci	 * 2nd IR receiver (short-range, wideband) for learning mode:
1968c2ecf20Sopenharmony_ci	 *     0, absent 2nd receiver (rx2)
1978c2ecf20Sopenharmony_ci	 *     1, rx2 present
1988c2ecf20Sopenharmony_ci	 *     2, rx2 which under counts IR carrier cycles
1998c2ecf20Sopenharmony_ci	 */
2008c2ecf20Sopenharmony_ci	u32 rx2;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	int ir_intfnum;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	const char *rc_map;	/* Allow specify a per-board map */
2058c2ecf20Sopenharmony_ci	const char *name;	/* per-board name */
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic const struct mceusb_model mceusb_model[] = {
2098c2ecf20Sopenharmony_ci	[MCE_GEN1] = {
2108c2ecf20Sopenharmony_ci		.mce_gen1 = 1,
2118c2ecf20Sopenharmony_ci		.tx_mask_normal = 1,
2128c2ecf20Sopenharmony_ci		.rx2 = 2,
2138c2ecf20Sopenharmony_ci	},
2148c2ecf20Sopenharmony_ci	[MCE_GEN2] = {
2158c2ecf20Sopenharmony_ci		.mce_gen2 = 1,
2168c2ecf20Sopenharmony_ci		.rx2 = 2,
2178c2ecf20Sopenharmony_ci	},
2188c2ecf20Sopenharmony_ci	[MCE_GEN2_NO_TX] = {
2198c2ecf20Sopenharmony_ci		.mce_gen2 = 1,
2208c2ecf20Sopenharmony_ci		.no_tx = 1,
2218c2ecf20Sopenharmony_ci	},
2228c2ecf20Sopenharmony_ci	[MCE_GEN2_TX_INV] = {
2238c2ecf20Sopenharmony_ci		.mce_gen2 = 1,
2248c2ecf20Sopenharmony_ci		.tx_mask_normal = 1,
2258c2ecf20Sopenharmony_ci		.rx2 = 1,
2268c2ecf20Sopenharmony_ci	},
2278c2ecf20Sopenharmony_ci	[MCE_GEN2_TX_INV_RX_GOOD] = {
2288c2ecf20Sopenharmony_ci		.mce_gen2 = 1,
2298c2ecf20Sopenharmony_ci		.tx_mask_normal = 1,
2308c2ecf20Sopenharmony_ci		.rx2 = 2,
2318c2ecf20Sopenharmony_ci	},
2328c2ecf20Sopenharmony_ci	[MCE_GEN3] = {
2338c2ecf20Sopenharmony_ci		.mce_gen3 = 1,
2348c2ecf20Sopenharmony_ci		.tx_mask_normal = 1,
2358c2ecf20Sopenharmony_ci		.rx2 = 2,
2368c2ecf20Sopenharmony_ci	},
2378c2ecf20Sopenharmony_ci	[MCE_GEN3_BROKEN_IRTIMEOUT] = {
2388c2ecf20Sopenharmony_ci		.mce_gen3 = 1,
2398c2ecf20Sopenharmony_ci		.tx_mask_normal = 1,
2408c2ecf20Sopenharmony_ci		.rx2 = 2,
2418c2ecf20Sopenharmony_ci		.broken_irtimeout = 1
2428c2ecf20Sopenharmony_ci	},
2438c2ecf20Sopenharmony_ci	[POLARIS_EVK] = {
2448c2ecf20Sopenharmony_ci		/*
2458c2ecf20Sopenharmony_ci		 * In fact, the EVK is shipped without
2468c2ecf20Sopenharmony_ci		 * remotes, but we should have something handy,
2478c2ecf20Sopenharmony_ci		 * to allow testing it
2488c2ecf20Sopenharmony_ci		 */
2498c2ecf20Sopenharmony_ci		.name = "Conexant Hybrid TV (cx231xx) MCE IR",
2508c2ecf20Sopenharmony_ci		.rx2 = 2,
2518c2ecf20Sopenharmony_ci	},
2528c2ecf20Sopenharmony_ci	[CX_HYBRID_TV] = {
2538c2ecf20Sopenharmony_ci		.no_tx = 1, /* tx isn't wired up at all */
2548c2ecf20Sopenharmony_ci		.name = "Conexant Hybrid TV (cx231xx) MCE IR",
2558c2ecf20Sopenharmony_ci	},
2568c2ecf20Sopenharmony_ci	[HAUPPAUGE_CX_HYBRID_TV] = {
2578c2ecf20Sopenharmony_ci		.no_tx = 1, /* eeprom says it has no tx */
2588c2ecf20Sopenharmony_ci		.name = "Conexant Hybrid TV (cx231xx) MCE IR no TX",
2598c2ecf20Sopenharmony_ci	},
2608c2ecf20Sopenharmony_ci	[MULTIFUNCTION] = {
2618c2ecf20Sopenharmony_ci		.mce_gen2 = 1,
2628c2ecf20Sopenharmony_ci		.ir_intfnum = 2,
2638c2ecf20Sopenharmony_ci		.rx2 = 2,
2648c2ecf20Sopenharmony_ci	},
2658c2ecf20Sopenharmony_ci	[TIVO_KIT] = {
2668c2ecf20Sopenharmony_ci		.mce_gen2 = 1,
2678c2ecf20Sopenharmony_ci		.rc_map = RC_MAP_TIVO,
2688c2ecf20Sopenharmony_ci		.rx2 = 2,
2698c2ecf20Sopenharmony_ci	},
2708c2ecf20Sopenharmony_ci	[EVROMEDIA_FULL_HYBRID_FULLHD] = {
2718c2ecf20Sopenharmony_ci		.name = "Evromedia USB Full Hybrid Full HD",
2728c2ecf20Sopenharmony_ci		.no_tx = 1,
2738c2ecf20Sopenharmony_ci		.rc_map = RC_MAP_MSI_DIGIVOX_III,
2748c2ecf20Sopenharmony_ci	},
2758c2ecf20Sopenharmony_ci	[ASTROMETA_T2HYBRID] = {
2768c2ecf20Sopenharmony_ci		.name = "Astrometa T2Hybrid",
2778c2ecf20Sopenharmony_ci		.no_tx = 1,
2788c2ecf20Sopenharmony_ci		.rc_map = RC_MAP_ASTROMETA_T2HYBRID,
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci};
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic const struct usb_device_id mceusb_dev_table[] = {
2838c2ecf20Sopenharmony_ci	/* Original Microsoft MCE IR Transceiver (often HP-branded) */
2848c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
2858c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN1 },
2868c2ecf20Sopenharmony_ci	/* Philips Infrared Transceiver - Sahara branded */
2878c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
2888c2ecf20Sopenharmony_ci	/* Philips Infrared Transceiver - HP branded */
2898c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x060c),
2908c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
2918c2ecf20Sopenharmony_ci	/* Philips SRM5100 */
2928c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
2938c2ecf20Sopenharmony_ci	/* Philips Infrared Transceiver - Omaura */
2948c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
2958c2ecf20Sopenharmony_ci	/* Philips Infrared Transceiver - Spinel plus */
2968c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
2978c2ecf20Sopenharmony_ci	/* Philips eHome Infrared Transceiver */
2988c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
2998c2ecf20Sopenharmony_ci	/* Philips/Spinel plus IR transceiver for ASUS */
3008c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
3018c2ecf20Sopenharmony_ci	/* Philips/Spinel plus IR transceiver for ASUS */
3028c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
3038c2ecf20Sopenharmony_ci	/* Philips IR transceiver (Dell branded) */
3048c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PHILIPS, 0x2093),
3058c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3068c2ecf20Sopenharmony_ci	/* Realtek MCE IR Receiver and card reader */
3078c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_REALTEK, 0x0161),
3088c2ecf20Sopenharmony_ci	  .driver_info = MULTIFUNCTION },
3098c2ecf20Sopenharmony_ci	/* SMK/Toshiba G83C0004D410 */
3108c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SMK, 0x031d),
3118c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV_RX_GOOD },
3128c2ecf20Sopenharmony_ci	/* SMK eHome Infrared Transceiver (Sony VAIO) */
3138c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SMK, 0x0322),
3148c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3158c2ecf20Sopenharmony_ci	/* bundled with Hauppauge PVR-150 */
3168c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SMK, 0x0334),
3178c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3188c2ecf20Sopenharmony_ci	/* SMK eHome Infrared Transceiver */
3198c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SMK, 0x0338) },
3208c2ecf20Sopenharmony_ci	/* SMK/I-O Data GV-MC7/RCKIT Receiver */
3218c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SMK, 0x0353),
3228c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_NO_TX },
3238c2ecf20Sopenharmony_ci	/* SMK RXX6000 Infrared Receiver */
3248c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SMK, 0x0357),
3258c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_NO_TX },
3268c2ecf20Sopenharmony_ci	/* Tatung eHome Infrared Transceiver */
3278c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TATUNG, 0x9150) },
3288c2ecf20Sopenharmony_ci	/* Shuttle eHome Infrared Transceiver */
3298c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
3308c2ecf20Sopenharmony_ci	/* Shuttle eHome Infrared Transceiver */
3318c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
3328c2ecf20Sopenharmony_ci	/* Gateway eHome Infrared Transceiver */
3338c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
3348c2ecf20Sopenharmony_ci	/* Mitsumi */
3358c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
3368c2ecf20Sopenharmony_ci	/* Topseed eHome Infrared Transceiver */
3378c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TOPSEED, 0x0001),
3388c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3398c2ecf20Sopenharmony_ci	/* Topseed HP eHome Infrared Transceiver */
3408c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TOPSEED, 0x0006),
3418c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3428c2ecf20Sopenharmony_ci	/* Topseed eHome Infrared Transceiver */
3438c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TOPSEED, 0x0007),
3448c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3458c2ecf20Sopenharmony_ci	/* Topseed eHome Infrared Transceiver */
3468c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TOPSEED, 0x0008),
3478c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN3 },
3488c2ecf20Sopenharmony_ci	/* Topseed eHome Infrared Transceiver */
3498c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TOPSEED, 0x000a),
3508c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3518c2ecf20Sopenharmony_ci	/* Topseed eHome Infrared Transceiver */
3528c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TOPSEED, 0x0011),
3538c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN3_BROKEN_IRTIMEOUT },
3548c2ecf20Sopenharmony_ci	/* Ricavision internal Infrared Transceiver */
3558c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
3568c2ecf20Sopenharmony_ci	/* Itron ione Libra Q-11 */
3578c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_ITRON, 0x7002) },
3588c2ecf20Sopenharmony_ci	/* FIC eHome Infrared Transceiver */
3598c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FIC, 0x9242) },
3608c2ecf20Sopenharmony_ci	/* LG eHome Infrared Transceiver */
3618c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_LG, 0x9803) },
3628c2ecf20Sopenharmony_ci	/* Microsoft MCE Infrared Transceiver */
3638c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
3648c2ecf20Sopenharmony_ci	/* Formosa eHome Infrared Transceiver */
3658c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
3668c2ecf20Sopenharmony_ci	/* Formosa21 / eHome Infrared Receiver */
3678c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
3688c2ecf20Sopenharmony_ci	/* Formosa aim / Trust MCE Infrared Receiver */
3698c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe017),
3708c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_NO_TX },
3718c2ecf20Sopenharmony_ci	/* Formosa Industrial Computing / Beanbag Emulation Device */
3728c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
3738c2ecf20Sopenharmony_ci	/* Formosa21 / eHome Infrared Receiver */
3748c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
3758c2ecf20Sopenharmony_ci	/* Formosa Industrial Computing AIM IR605/A */
3768c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
3778c2ecf20Sopenharmony_ci	/* Formosa Industrial Computing */
3788c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
3798c2ecf20Sopenharmony_ci	/* Formosa Industrial Computing */
3808c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
3818c2ecf20Sopenharmony_ci	/* Fintek eHome Infrared Transceiver (HP branded) */
3828c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FINTEK, 0x5168),
3838c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN2_TX_INV },
3848c2ecf20Sopenharmony_ci	/* Fintek eHome Infrared Transceiver */
3858c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FINTEK, 0x0602) },
3868c2ecf20Sopenharmony_ci	/* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
3878c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_FINTEK, 0x0702) },
3888c2ecf20Sopenharmony_ci	/* Pinnacle Remote Kit */
3898c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PINNACLE, 0x0225),
3908c2ecf20Sopenharmony_ci	  .driver_info = MCE_GEN3 },
3918c2ecf20Sopenharmony_ci	/* Elitegroup Computer Systems IR */
3928c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_ECS, 0x0f38) },
3938c2ecf20Sopenharmony_ci	/* Wistron Corp. eHome Infrared Receiver */
3948c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_WISTRON, 0x0002) },
3958c2ecf20Sopenharmony_ci	/* Compro K100 */
3968c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_COMPRO, 0x3020) },
3978c2ecf20Sopenharmony_ci	/* Compro K100 v2 */
3988c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_COMPRO, 0x3082) },
3998c2ecf20Sopenharmony_ci	/* Northstar Systems, Inc. eHome Infrared Transceiver */
4008c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
4018c2ecf20Sopenharmony_ci	/* TiVo PC IR Receiver */
4028c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TIVO, 0x2000),
4038c2ecf20Sopenharmony_ci	  .driver_info = TIVO_KIT },
4048c2ecf20Sopenharmony_ci	/* Conexant Hybrid TV "Shelby" Polaris SDK */
4058c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
4068c2ecf20Sopenharmony_ci	  .driver_info = POLARIS_EVK },
4078c2ecf20Sopenharmony_ci	/* Conexant Hybrid TV RDU253S Polaris */
4088c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
4098c2ecf20Sopenharmony_ci	  .driver_info = CX_HYBRID_TV },
4108c2ecf20Sopenharmony_ci	/* Twisted Melon Inc. - Manta Mini Receiver */
4118c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
4128c2ecf20Sopenharmony_ci	/* Twisted Melon Inc. - Manta Pico Receiver */
4138c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
4148c2ecf20Sopenharmony_ci	/* Twisted Melon Inc. - Manta Transceiver */
4158c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
4168c2ecf20Sopenharmony_ci	/* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
4178c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb130),
4188c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4198c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb131),
4208c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4218c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb138),
4228c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4238c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb139),
4248c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4258c2ecf20Sopenharmony_ci	/* Hauppauge WinTV-HVR-935C - based on cx231xx */
4268c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb151),
4278c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4288c2ecf20Sopenharmony_ci	/* Hauppauge WinTV-HVR-955Q - based on cx231xx */
4298c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb123),
4308c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4318c2ecf20Sopenharmony_ci	/* Hauppauge WinTV-HVR-975 - based on cx231xx */
4328c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_HAUPPAUGE, 0xb150),
4338c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4348c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PCTV, 0x0259),
4358c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4368c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_PCTV, 0x025e),
4378c2ecf20Sopenharmony_ci	  .driver_info = HAUPPAUGE_CX_HYBRID_TV },
4388c2ecf20Sopenharmony_ci	/* Adaptec / HP eHome Receiver */
4398c2ecf20Sopenharmony_ci	{ USB_DEVICE(VENDOR_ADAPTEC, 0x0094) },
4408c2ecf20Sopenharmony_ci	/* Evromedia USB Full Hybrid Full HD */
4418c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1b80, 0xd3b2),
4428c2ecf20Sopenharmony_ci	  .driver_info = EVROMEDIA_FULL_HYBRID_FULLHD },
4438c2ecf20Sopenharmony_ci	/* Astrometa T2hybrid */
4448c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x15f4, 0x0135),
4458c2ecf20Sopenharmony_ci	  .driver_info = ASTROMETA_T2HYBRID },
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	/* Terminating entry */
4488c2ecf20Sopenharmony_ci	{ }
4498c2ecf20Sopenharmony_ci};
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci/* data structure for each usb transceiver */
4528c2ecf20Sopenharmony_cistruct mceusb_dev {
4538c2ecf20Sopenharmony_ci	/* ir-core bits */
4548c2ecf20Sopenharmony_ci	struct rc_dev *rc;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	/* optional features we can enable */
4578c2ecf20Sopenharmony_ci	bool carrier_report_enabled;
4588c2ecf20Sopenharmony_ci	bool wideband_rx_enabled;	/* aka learning mode, short-range rx */
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	/* core device bits */
4618c2ecf20Sopenharmony_ci	struct device *dev;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/* usb */
4648c2ecf20Sopenharmony_ci	struct usb_device *usbdev;
4658c2ecf20Sopenharmony_ci	struct usb_interface *usbintf;
4668c2ecf20Sopenharmony_ci	struct urb *urb_in;
4678c2ecf20Sopenharmony_ci	unsigned int pipe_in;
4688c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *usb_ep_out;
4698c2ecf20Sopenharmony_ci	unsigned int pipe_out;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/* buffers and dma */
4728c2ecf20Sopenharmony_ci	unsigned char *buf_in;
4738c2ecf20Sopenharmony_ci	unsigned int len_in;
4748c2ecf20Sopenharmony_ci	dma_addr_t dma_in;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	enum {
4778c2ecf20Sopenharmony_ci		CMD_HEADER = 0,
4788c2ecf20Sopenharmony_ci		SUBCMD,
4798c2ecf20Sopenharmony_ci		CMD_DATA,
4808c2ecf20Sopenharmony_ci		PARSE_IRDATA,
4818c2ecf20Sopenharmony_ci	} parser_state;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	u8 cmd, rem;		/* Remaining IR data bytes in packet */
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	struct {
4868c2ecf20Sopenharmony_ci		u32 connected:1;
4878c2ecf20Sopenharmony_ci		u32 tx_mask_normal:1;
4888c2ecf20Sopenharmony_ci		u32 microsoft_gen1:1;
4898c2ecf20Sopenharmony_ci		u32 no_tx:1;
4908c2ecf20Sopenharmony_ci		u32 rx2;
4918c2ecf20Sopenharmony_ci	} flags;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* transmit support */
4948c2ecf20Sopenharmony_ci	u32 carrier;
4958c2ecf20Sopenharmony_ci	unsigned char tx_mask;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	char name[128];
4988c2ecf20Sopenharmony_ci	char phys[64];
4998c2ecf20Sopenharmony_ci	enum mceusb_model_type model;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	bool need_reset;	/* flag to issue a device resume cmd */
5028c2ecf20Sopenharmony_ci	u8 emver;		/* emulator interface version */
5038c2ecf20Sopenharmony_ci	u8 num_txports;		/* number of transmit ports */
5048c2ecf20Sopenharmony_ci	u8 num_rxports;		/* number of receive sensors */
5058c2ecf20Sopenharmony_ci	u8 txports_cabled;	/* bitmask of transmitters with cable */
5068c2ecf20Sopenharmony_ci	u8 rxports_active;	/* bitmask of active receive sensors */
5078c2ecf20Sopenharmony_ci	bool learning_active;	/* wideband rx is active */
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	/* receiver carrier frequency detection support */
5108c2ecf20Sopenharmony_ci	u32 pulse_tunit;	/* IR pulse "on" cumulative time units */
5118c2ecf20Sopenharmony_ci	u32 pulse_count;	/* pulse "on" count in measurement interval */
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	/*
5148c2ecf20Sopenharmony_ci	 * support for async error handler mceusb_deferred_kevent()
5158c2ecf20Sopenharmony_ci	 * where usb_clear_halt(), usb_reset_configuration(),
5168c2ecf20Sopenharmony_ci	 * usb_reset_device(), etc. must be done in process context
5178c2ecf20Sopenharmony_ci	 */
5188c2ecf20Sopenharmony_ci	struct work_struct kevent;
5198c2ecf20Sopenharmony_ci	unsigned long kevent_flags;
5208c2ecf20Sopenharmony_ci#		define EVENT_TX_HALT	0
5218c2ecf20Sopenharmony_ci#		define EVENT_RX_HALT	1
5228c2ecf20Sopenharmony_ci#		define EVENT_RST_PEND	31
5238c2ecf20Sopenharmony_ci};
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci/* MCE Device Command Strings, generally a port and command pair */
5268c2ecf20Sopenharmony_cistatic char DEVICE_RESUME[]	= {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
5278c2ecf20Sopenharmony_ci				   MCE_CMD_RESUME};
5288c2ecf20Sopenharmony_cistatic char GET_REVISION[]	= {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
5298c2ecf20Sopenharmony_cistatic char GET_EMVER[]		= {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
5308c2ecf20Sopenharmony_cistatic char GET_WAKEVERSION[]	= {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
5318c2ecf20Sopenharmony_cistatic char FLASH_LED[]		= {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
5328c2ecf20Sopenharmony_cistatic char GET_UNKNOWN2[]	= {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
5338c2ecf20Sopenharmony_cistatic char GET_CARRIER_FREQ[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
5348c2ecf20Sopenharmony_cistatic char GET_RX_TIMEOUT[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
5358c2ecf20Sopenharmony_cistatic char GET_NUM_PORTS[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
5368c2ecf20Sopenharmony_cistatic char GET_TX_BITMASK[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
5378c2ecf20Sopenharmony_cistatic char GET_RX_SENSOR[]	= {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
5388c2ecf20Sopenharmony_ci/* sub in desired values in lower byte or bytes for full command */
5398c2ecf20Sopenharmony_ci/* FIXME: make use of these for transmit.
5408c2ecf20Sopenharmony_cistatic char SET_CARRIER_FREQ[]	= {MCE_CMD_PORT_IR,
5418c2ecf20Sopenharmony_ci				   MCE_CMD_SETIRCFS, 0x00, 0x00};
5428c2ecf20Sopenharmony_cistatic char SET_TX_BITMASK[]	= {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
5438c2ecf20Sopenharmony_cistatic char SET_RX_TIMEOUT[]	= {MCE_CMD_PORT_IR,
5448c2ecf20Sopenharmony_ci				   MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
5458c2ecf20Sopenharmony_cistatic char SET_RX_SENSOR[]	= {MCE_CMD_PORT_IR,
5468c2ecf20Sopenharmony_ci				   MCE_RSP_EQIRRXPORTEN, 0x00};
5478c2ecf20Sopenharmony_ci*/
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_cistatic int mceusb_cmd_datasize(u8 cmd, u8 subcmd)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	int datasize = 0;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	switch (cmd) {
5548c2ecf20Sopenharmony_ci	case MCE_CMD_NULL:
5558c2ecf20Sopenharmony_ci		if (subcmd == MCE_CMD_PORT_SYS)
5568c2ecf20Sopenharmony_ci			datasize = 1;
5578c2ecf20Sopenharmony_ci		break;
5588c2ecf20Sopenharmony_ci	case MCE_CMD_PORT_SYS:
5598c2ecf20Sopenharmony_ci		switch (subcmd) {
5608c2ecf20Sopenharmony_ci		case MCE_RSP_GETPORTSTATUS:
5618c2ecf20Sopenharmony_ci			datasize = 5;
5628c2ecf20Sopenharmony_ci			break;
5638c2ecf20Sopenharmony_ci		case MCE_RSP_EQWAKEVERSION:
5648c2ecf20Sopenharmony_ci			datasize = 4;
5658c2ecf20Sopenharmony_ci			break;
5668c2ecf20Sopenharmony_ci		case MCE_CMD_G_REVISION:
5678c2ecf20Sopenharmony_ci			datasize = 4;
5688c2ecf20Sopenharmony_ci			break;
5698c2ecf20Sopenharmony_ci		case MCE_RSP_EQWAKESUPPORT:
5708c2ecf20Sopenharmony_ci		case MCE_RSP_GETWAKESOURCE:
5718c2ecf20Sopenharmony_ci		case MCE_RSP_EQDEVDETAILS:
5728c2ecf20Sopenharmony_ci		case MCE_RSP_EQEMVER:
5738c2ecf20Sopenharmony_ci			datasize = 1;
5748c2ecf20Sopenharmony_ci			break;
5758c2ecf20Sopenharmony_ci		}
5768c2ecf20Sopenharmony_ci		break;
5778c2ecf20Sopenharmony_ci	case MCE_CMD_PORT_IR:
5788c2ecf20Sopenharmony_ci		switch (subcmd) {
5798c2ecf20Sopenharmony_ci		case MCE_CMD_UNKNOWN:
5808c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRCFS:
5818c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRTIMEOUT:
5828c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRRXCFCNT:
5838c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRNUMPORTS:
5848c2ecf20Sopenharmony_ci			datasize = 2;
5858c2ecf20Sopenharmony_ci			break;
5868c2ecf20Sopenharmony_ci		case MCE_CMD_SIG_END:
5878c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRTXPORTS:
5888c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRRXPORTEN:
5898c2ecf20Sopenharmony_ci			datasize = 1;
5908c2ecf20Sopenharmony_ci			break;
5918c2ecf20Sopenharmony_ci		}
5928c2ecf20Sopenharmony_ci	}
5938c2ecf20Sopenharmony_ci	return datasize;
5948c2ecf20Sopenharmony_ci}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cistatic void mceusb_dev_printdata(struct mceusb_dev *ir, u8 *buf, int buf_len,
5978c2ecf20Sopenharmony_ci				 int offset, int len, bool out)
5988c2ecf20Sopenharmony_ci{
5998c2ecf20Sopenharmony_ci#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
6008c2ecf20Sopenharmony_ci	char *inout;
6018c2ecf20Sopenharmony_ci	u8 cmd, subcmd, *data;
6028c2ecf20Sopenharmony_ci	struct device *dev = ir->dev;
6038c2ecf20Sopenharmony_ci	u32 carrier, period;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	if (offset < 0 || offset >= buf_len)
6068c2ecf20Sopenharmony_ci		return;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	dev_dbg(dev, "%cx data[%d]: %*ph (len=%d sz=%d)",
6098c2ecf20Sopenharmony_ci		(out ? 't' : 'r'), offset,
6108c2ecf20Sopenharmony_ci		min(len, buf_len - offset), buf + offset, len, buf_len);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	inout = out ? "Request" : "Got";
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	cmd    = buf[offset];
6158c2ecf20Sopenharmony_ci	subcmd = (offset + 1 < buf_len) ? buf[offset + 1] : 0;
6168c2ecf20Sopenharmony_ci	data   = &buf[offset] + 2;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	/* Trace meaningless 0xb1 0x60 header bytes on original receiver */
6198c2ecf20Sopenharmony_ci	if (ir->flags.microsoft_gen1 && !out && !offset) {
6208c2ecf20Sopenharmony_ci		dev_dbg(dev, "MCE gen 1 header");
6218c2ecf20Sopenharmony_ci		return;
6228c2ecf20Sopenharmony_ci	}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	/* Trace IR data header or trailer */
6258c2ecf20Sopenharmony_ci	if (cmd != MCE_CMD_PORT_IR &&
6268c2ecf20Sopenharmony_ci	    (cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA) {
6278c2ecf20Sopenharmony_ci		if (cmd == MCE_IRDATA_TRAILER)
6288c2ecf20Sopenharmony_ci			dev_dbg(dev, "End of raw IR data");
6298c2ecf20Sopenharmony_ci		else
6308c2ecf20Sopenharmony_ci			dev_dbg(dev, "Raw IR data, %d pulse/space samples",
6318c2ecf20Sopenharmony_ci				cmd & MCE_PACKET_LENGTH_MASK);
6328c2ecf20Sopenharmony_ci		return;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* Unexpected end of buffer? */
6368c2ecf20Sopenharmony_ci	if (offset + len > buf_len)
6378c2ecf20Sopenharmony_ci		return;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	/* Decode MCE command/response */
6408c2ecf20Sopenharmony_ci	switch (cmd) {
6418c2ecf20Sopenharmony_ci	case MCE_CMD_NULL:
6428c2ecf20Sopenharmony_ci		if (subcmd == MCE_CMD_NULL)
6438c2ecf20Sopenharmony_ci			break;
6448c2ecf20Sopenharmony_ci		if ((subcmd == MCE_CMD_PORT_SYS) &&
6458c2ecf20Sopenharmony_ci		    (data[0] == MCE_CMD_RESUME))
6468c2ecf20Sopenharmony_ci			dev_dbg(dev, "Device resume requested");
6478c2ecf20Sopenharmony_ci		else
6488c2ecf20Sopenharmony_ci			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
6498c2ecf20Sopenharmony_ci				 cmd, subcmd);
6508c2ecf20Sopenharmony_ci		break;
6518c2ecf20Sopenharmony_ci	case MCE_CMD_PORT_SYS:
6528c2ecf20Sopenharmony_ci		switch (subcmd) {
6538c2ecf20Sopenharmony_ci		case MCE_RSP_EQEMVER:
6548c2ecf20Sopenharmony_ci			if (!out)
6558c2ecf20Sopenharmony_ci				dev_dbg(dev, "Emulator interface version %x",
6568c2ecf20Sopenharmony_ci					 data[0]);
6578c2ecf20Sopenharmony_ci			break;
6588c2ecf20Sopenharmony_ci		case MCE_CMD_G_REVISION:
6598c2ecf20Sopenharmony_ci			if (len == 2)
6608c2ecf20Sopenharmony_ci				dev_dbg(dev, "Get hw/sw rev?");
6618c2ecf20Sopenharmony_ci			else
6628c2ecf20Sopenharmony_ci				dev_dbg(dev, "hw/sw rev %*ph",
6638c2ecf20Sopenharmony_ci					4, &buf[offset + 2]);
6648c2ecf20Sopenharmony_ci			break;
6658c2ecf20Sopenharmony_ci		case MCE_CMD_RESUME:
6668c2ecf20Sopenharmony_ci			dev_dbg(dev, "Device resume requested");
6678c2ecf20Sopenharmony_ci			break;
6688c2ecf20Sopenharmony_ci		case MCE_RSP_CMD_ILLEGAL:
6698c2ecf20Sopenharmony_ci			dev_dbg(dev, "Illegal PORT_SYS command");
6708c2ecf20Sopenharmony_ci			break;
6718c2ecf20Sopenharmony_ci		case MCE_RSP_EQWAKEVERSION:
6728c2ecf20Sopenharmony_ci			if (!out)
6738c2ecf20Sopenharmony_ci				dev_dbg(dev, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
6748c2ecf20Sopenharmony_ci					data[0], data[1], data[2], data[3]);
6758c2ecf20Sopenharmony_ci			break;
6768c2ecf20Sopenharmony_ci		case MCE_RSP_GETPORTSTATUS:
6778c2ecf20Sopenharmony_ci			if (!out)
6788c2ecf20Sopenharmony_ci				/* We use data1 + 1 here, to match hw labels */
6798c2ecf20Sopenharmony_ci				dev_dbg(dev, "TX port %d: blaster is%s connected",
6808c2ecf20Sopenharmony_ci					 data[0] + 1, data[3] ? " not" : "");
6818c2ecf20Sopenharmony_ci			break;
6828c2ecf20Sopenharmony_ci		case MCE_CMD_FLASHLED:
6838c2ecf20Sopenharmony_ci			dev_dbg(dev, "Attempting to flash LED");
6848c2ecf20Sopenharmony_ci			break;
6858c2ecf20Sopenharmony_ci		default:
6868c2ecf20Sopenharmony_ci			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
6878c2ecf20Sopenharmony_ci				 cmd, subcmd);
6888c2ecf20Sopenharmony_ci			break;
6898c2ecf20Sopenharmony_ci		}
6908c2ecf20Sopenharmony_ci		break;
6918c2ecf20Sopenharmony_ci	case MCE_CMD_PORT_IR:
6928c2ecf20Sopenharmony_ci		switch (subcmd) {
6938c2ecf20Sopenharmony_ci		case MCE_CMD_SIG_END:
6948c2ecf20Sopenharmony_ci			dev_dbg(dev, "End of signal");
6958c2ecf20Sopenharmony_ci			break;
6968c2ecf20Sopenharmony_ci		case MCE_CMD_PING:
6978c2ecf20Sopenharmony_ci			dev_dbg(dev, "Ping");
6988c2ecf20Sopenharmony_ci			break;
6998c2ecf20Sopenharmony_ci		case MCE_CMD_UNKNOWN:
7008c2ecf20Sopenharmony_ci			dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x",
7018c2ecf20Sopenharmony_ci				data[0], data[1]);
7028c2ecf20Sopenharmony_ci			break;
7038c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRCFS:
7048c2ecf20Sopenharmony_ci			if (!data[0] && !data[1]) {
7058c2ecf20Sopenharmony_ci				dev_dbg(dev, "%s: no carrier", inout);
7068c2ecf20Sopenharmony_ci				break;
7078c2ecf20Sopenharmony_ci			}
7088c2ecf20Sopenharmony_ci			// prescaler should make sense
7098c2ecf20Sopenharmony_ci			if (data[0] > 8)
7108c2ecf20Sopenharmony_ci				break;
7118c2ecf20Sopenharmony_ci			period = DIV_ROUND_CLOSEST((1U << data[0] * 2) *
7128c2ecf20Sopenharmony_ci						   (data[1] + 1), 10);
7138c2ecf20Sopenharmony_ci			if (!period)
7148c2ecf20Sopenharmony_ci				break;
7158c2ecf20Sopenharmony_ci			carrier = USEC_PER_SEC / period;
7168c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s carrier of %u Hz (period %uus)",
7178c2ecf20Sopenharmony_ci				 inout, carrier, period);
7188c2ecf20Sopenharmony_ci			break;
7198c2ecf20Sopenharmony_ci		case MCE_CMD_GETIRCFS:
7208c2ecf20Sopenharmony_ci			dev_dbg(dev, "Get carrier mode and freq");
7218c2ecf20Sopenharmony_ci			break;
7228c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRTXPORTS:
7238c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s transmit blaster mask of 0x%02x",
7248c2ecf20Sopenharmony_ci				 inout, data[0]);
7258c2ecf20Sopenharmony_ci			break;
7268c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRTIMEOUT:
7278c2ecf20Sopenharmony_ci			/* value is in units of 50us, so x*50/1000 ms */
7288c2ecf20Sopenharmony_ci			period = ((data[0] << 8) | data[1]) *
7298c2ecf20Sopenharmony_ci				  MCE_TIME_UNIT / 1000;
7308c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s receive timeout of %d ms",
7318c2ecf20Sopenharmony_ci				 inout, period);
7328c2ecf20Sopenharmony_ci			break;
7338c2ecf20Sopenharmony_ci		case MCE_CMD_GETIRTIMEOUT:
7348c2ecf20Sopenharmony_ci			dev_dbg(dev, "Get receive timeout");
7358c2ecf20Sopenharmony_ci			break;
7368c2ecf20Sopenharmony_ci		case MCE_CMD_GETIRTXPORTS:
7378c2ecf20Sopenharmony_ci			dev_dbg(dev, "Get transmit blaster mask");
7388c2ecf20Sopenharmony_ci			break;
7398c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRRXPORTEN:
7408c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s %s-range receive sensor in use",
7418c2ecf20Sopenharmony_ci				 inout, data[0] == 0x02 ? "short" : "long");
7428c2ecf20Sopenharmony_ci			break;
7438c2ecf20Sopenharmony_ci		case MCE_CMD_GETIRRXPORTEN:
7448c2ecf20Sopenharmony_ci		/* aka MCE_RSP_EQIRRXCFCNT */
7458c2ecf20Sopenharmony_ci			if (out)
7468c2ecf20Sopenharmony_ci				dev_dbg(dev, "Get receive sensor");
7478c2ecf20Sopenharmony_ci			else
7488c2ecf20Sopenharmony_ci				dev_dbg(dev, "RX carrier cycle count: %d",
7498c2ecf20Sopenharmony_ci					((data[0] << 8) | data[1]));
7508c2ecf20Sopenharmony_ci			break;
7518c2ecf20Sopenharmony_ci		case MCE_RSP_EQIRNUMPORTS:
7528c2ecf20Sopenharmony_ci			if (out)
7538c2ecf20Sopenharmony_ci				break;
7548c2ecf20Sopenharmony_ci			dev_dbg(dev, "Num TX ports: %x, num RX ports: %x",
7558c2ecf20Sopenharmony_ci				data[0], data[1]);
7568c2ecf20Sopenharmony_ci			break;
7578c2ecf20Sopenharmony_ci		case MCE_RSP_CMD_ILLEGAL:
7588c2ecf20Sopenharmony_ci			dev_dbg(dev, "Illegal PORT_IR command");
7598c2ecf20Sopenharmony_ci			break;
7608c2ecf20Sopenharmony_ci		case MCE_RSP_TX_TIMEOUT:
7618c2ecf20Sopenharmony_ci			dev_dbg(dev, "IR TX timeout (TX buffer underrun)");
7628c2ecf20Sopenharmony_ci			break;
7638c2ecf20Sopenharmony_ci		default:
7648c2ecf20Sopenharmony_ci			dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
7658c2ecf20Sopenharmony_ci				 cmd, subcmd);
7668c2ecf20Sopenharmony_ci			break;
7678c2ecf20Sopenharmony_ci		}
7688c2ecf20Sopenharmony_ci		break;
7698c2ecf20Sopenharmony_ci	default:
7708c2ecf20Sopenharmony_ci		break;
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci#endif
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci/*
7768c2ecf20Sopenharmony_ci * Schedule work that can't be done in interrupt handlers
7778c2ecf20Sopenharmony_ci * (mceusb_dev_recv() and mce_write_callback()) nor tasklets.
7788c2ecf20Sopenharmony_ci * Invokes mceusb_deferred_kevent() for recovering from
7798c2ecf20Sopenharmony_ci * error events specified by the kevent bit field.
7808c2ecf20Sopenharmony_ci */
7818c2ecf20Sopenharmony_cistatic void mceusb_defer_kevent(struct mceusb_dev *ir, int kevent)
7828c2ecf20Sopenharmony_ci{
7838c2ecf20Sopenharmony_ci	set_bit(kevent, &ir->kevent_flags);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) {
7868c2ecf20Sopenharmony_ci		dev_dbg(ir->dev, "kevent %d dropped pending USB Reset Device",
7878c2ecf20Sopenharmony_ci			kevent);
7888c2ecf20Sopenharmony_ci		return;
7898c2ecf20Sopenharmony_ci	}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	if (!schedule_work(&ir->kevent))
7928c2ecf20Sopenharmony_ci		dev_dbg(ir->dev, "kevent %d already scheduled", kevent);
7938c2ecf20Sopenharmony_ci	else
7948c2ecf20Sopenharmony_ci		dev_dbg(ir->dev, "kevent %d scheduled", kevent);
7958c2ecf20Sopenharmony_ci}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_cistatic void mce_write_callback(struct urb *urb)
7988c2ecf20Sopenharmony_ci{
7998c2ecf20Sopenharmony_ci	if (!urb)
8008c2ecf20Sopenharmony_ci		return;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	complete(urb->context);
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci/*
8068c2ecf20Sopenharmony_ci * Write (TX/send) data to MCE device USB endpoint out.
8078c2ecf20Sopenharmony_ci * Used for IR blaster TX and MCE device commands.
8088c2ecf20Sopenharmony_ci *
8098c2ecf20Sopenharmony_ci * Return: The number of bytes written (> 0) or errno (< 0).
8108c2ecf20Sopenharmony_ci */
8118c2ecf20Sopenharmony_cistatic int mce_write(struct mceusb_dev *ir, u8 *data, int size)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	int ret;
8148c2ecf20Sopenharmony_ci	struct urb *urb;
8158c2ecf20Sopenharmony_ci	struct device *dev = ir->dev;
8168c2ecf20Sopenharmony_ci	unsigned char *buf_out;
8178c2ecf20Sopenharmony_ci	struct completion tx_done;
8188c2ecf20Sopenharmony_ci	unsigned long expire;
8198c2ecf20Sopenharmony_ci	unsigned long ret_wait;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	mceusb_dev_printdata(ir, data, size, 0, size, true);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	urb = usb_alloc_urb(0, GFP_KERNEL);
8248c2ecf20Sopenharmony_ci	if (unlikely(!urb)) {
8258c2ecf20Sopenharmony_ci		dev_err(dev, "Error: mce write couldn't allocate urb");
8268c2ecf20Sopenharmony_ci		return -ENOMEM;
8278c2ecf20Sopenharmony_ci	}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	buf_out = kmalloc(size, GFP_KERNEL);
8308c2ecf20Sopenharmony_ci	if (!buf_out) {
8318c2ecf20Sopenharmony_ci		usb_free_urb(urb);
8328c2ecf20Sopenharmony_ci		return -ENOMEM;
8338c2ecf20Sopenharmony_ci	}
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	init_completion(&tx_done);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	/* outbound data */
8388c2ecf20Sopenharmony_ci	if (usb_endpoint_xfer_int(ir->usb_ep_out))
8398c2ecf20Sopenharmony_ci		usb_fill_int_urb(urb, ir->usbdev, ir->pipe_out,
8408c2ecf20Sopenharmony_ci				 buf_out, size, mce_write_callback, &tx_done,
8418c2ecf20Sopenharmony_ci				 ir->usb_ep_out->bInterval);
8428c2ecf20Sopenharmony_ci	else
8438c2ecf20Sopenharmony_ci		usb_fill_bulk_urb(urb, ir->usbdev, ir->pipe_out,
8448c2ecf20Sopenharmony_ci				  buf_out, size, mce_write_callback, &tx_done);
8458c2ecf20Sopenharmony_ci	memcpy(buf_out, data, size);
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	ret = usb_submit_urb(urb, GFP_KERNEL);
8488c2ecf20Sopenharmony_ci	if (ret) {
8498c2ecf20Sopenharmony_ci		dev_err(dev, "Error: mce write submit urb error = %d", ret);
8508c2ecf20Sopenharmony_ci		kfree(buf_out);
8518c2ecf20Sopenharmony_ci		usb_free_urb(urb);
8528c2ecf20Sopenharmony_ci		return ret;
8538c2ecf20Sopenharmony_ci	}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	expire = msecs_to_jiffies(USB_TX_TIMEOUT);
8568c2ecf20Sopenharmony_ci	ret_wait = wait_for_completion_timeout(&tx_done, expire);
8578c2ecf20Sopenharmony_ci	if (!ret_wait) {
8588c2ecf20Sopenharmony_ci		dev_err(dev, "Error: mce write timed out (expire = %lu (%dms))",
8598c2ecf20Sopenharmony_ci			expire, USB_TX_TIMEOUT);
8608c2ecf20Sopenharmony_ci		usb_kill_urb(urb);
8618c2ecf20Sopenharmony_ci		ret = (urb->status == -ENOENT ? -ETIMEDOUT : urb->status);
8628c2ecf20Sopenharmony_ci	} else {
8638c2ecf20Sopenharmony_ci		ret = urb->status;
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci	if (ret >= 0)
8668c2ecf20Sopenharmony_ci		ret = urb->actual_length;	/* bytes written */
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	switch (urb->status) {
8698c2ecf20Sopenharmony_ci	/* success */
8708c2ecf20Sopenharmony_ci	case 0:
8718c2ecf20Sopenharmony_ci		break;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	case -ECONNRESET:
8748c2ecf20Sopenharmony_ci	case -ENOENT:
8758c2ecf20Sopenharmony_ci	case -EILSEQ:
8768c2ecf20Sopenharmony_ci	case -ESHUTDOWN:
8778c2ecf20Sopenharmony_ci		break;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	case -EPIPE:
8808c2ecf20Sopenharmony_ci		dev_err(ir->dev, "Error: mce write urb status = %d (TX HALT)",
8818c2ecf20Sopenharmony_ci			urb->status);
8828c2ecf20Sopenharmony_ci		mceusb_defer_kevent(ir, EVENT_TX_HALT);
8838c2ecf20Sopenharmony_ci		break;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	default:
8868c2ecf20Sopenharmony_ci		dev_err(ir->dev, "Error: mce write urb status = %d",
8878c2ecf20Sopenharmony_ci			urb->status);
8888c2ecf20Sopenharmony_ci		break;
8898c2ecf20Sopenharmony_ci	}
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	dev_dbg(dev, "tx done status = %d (wait = %lu, expire = %lu (%dms), urb->actual_length = %d, urb->status = %d)",
8928c2ecf20Sopenharmony_ci		ret, ret_wait, expire, USB_TX_TIMEOUT,
8938c2ecf20Sopenharmony_ci		urb->actual_length, urb->status);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	kfree(buf_out);
8968c2ecf20Sopenharmony_ci	usb_free_urb(urb);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	return ret;
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cistatic void mce_command_out(struct mceusb_dev *ir, u8 *data, int size)
9028c2ecf20Sopenharmony_ci{
9038c2ecf20Sopenharmony_ci	int rsize = sizeof(DEVICE_RESUME);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	if (ir->need_reset) {
9068c2ecf20Sopenharmony_ci		ir->need_reset = false;
9078c2ecf20Sopenharmony_ci		mce_write(ir, DEVICE_RESUME, rsize);
9088c2ecf20Sopenharmony_ci		msleep(10);
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	mce_write(ir, data, size);
9128c2ecf20Sopenharmony_ci	msleep(10);
9138c2ecf20Sopenharmony_ci}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci/*
9168c2ecf20Sopenharmony_ci * Transmit IR out the MCE device IR blaster port(s).
9178c2ecf20Sopenharmony_ci *
9188c2ecf20Sopenharmony_ci * Convert IR pulse/space sequence from LIRC to MCE format.
9198c2ecf20Sopenharmony_ci * Break up a long IR sequence into multiple parts (MCE IR data packets).
9208c2ecf20Sopenharmony_ci *
9218c2ecf20Sopenharmony_ci * u32 txbuf[] consists of IR pulse, space, ..., and pulse times in usec.
9228c2ecf20Sopenharmony_ci * Pulses and spaces are implicit by their position.
9238c2ecf20Sopenharmony_ci * The first IR sample, txbuf[0], is always a pulse.
9248c2ecf20Sopenharmony_ci *
9258c2ecf20Sopenharmony_ci * u8 irbuf[] consists of multiple IR data packets for the MCE device.
9268c2ecf20Sopenharmony_ci * A packet is 1 u8 MCE_IRDATA_HEADER and up to 30 u8 IR samples.
9278c2ecf20Sopenharmony_ci * An IR sample is 1-bit pulse/space flag with 7-bit time
9288c2ecf20Sopenharmony_ci * in MCE time units (50usec).
9298c2ecf20Sopenharmony_ci *
9308c2ecf20Sopenharmony_ci * Return: The number of IR samples sent (> 0) or errno (< 0).
9318c2ecf20Sopenharmony_ci */
9328c2ecf20Sopenharmony_cistatic int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = dev->priv;
9358c2ecf20Sopenharmony_ci	u8 cmdbuf[3] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00 };
9368c2ecf20Sopenharmony_ci	u8 irbuf[MCE_IRBUF_SIZE];
9378c2ecf20Sopenharmony_ci	int ircount = 0;
9388c2ecf20Sopenharmony_ci	unsigned int irsample;
9398c2ecf20Sopenharmony_ci	int i, length, ret;
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	/* Send the set TX ports command */
9428c2ecf20Sopenharmony_ci	cmdbuf[2] = ir->tx_mask;
9438c2ecf20Sopenharmony_ci	mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	/* Generate mce IR data packet */
9468c2ecf20Sopenharmony_ci	for (i = 0; i < count; i++) {
9478c2ecf20Sopenharmony_ci		irsample = txbuf[i] / MCE_TIME_UNIT;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci		/* loop to support long pulses/spaces > 6350us (127*50us) */
9508c2ecf20Sopenharmony_ci		while (irsample > 0) {
9518c2ecf20Sopenharmony_ci			/* Insert IR header every 30th entry */
9528c2ecf20Sopenharmony_ci			if (ircount % MCE_PACKET_SIZE == 0) {
9538c2ecf20Sopenharmony_ci				/* Room for IR header and one IR sample? */
9548c2ecf20Sopenharmony_ci				if (ircount >= MCE_IRBUF_SIZE - 1) {
9558c2ecf20Sopenharmony_ci					/* Send near full buffer */
9568c2ecf20Sopenharmony_ci					ret = mce_write(ir, irbuf, ircount);
9578c2ecf20Sopenharmony_ci					if (ret < 0)
9588c2ecf20Sopenharmony_ci						return ret;
9598c2ecf20Sopenharmony_ci					ircount = 0;
9608c2ecf20Sopenharmony_ci				}
9618c2ecf20Sopenharmony_ci				irbuf[ircount++] = MCE_IRDATA_HEADER;
9628c2ecf20Sopenharmony_ci			}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci			/* Insert IR sample */
9658c2ecf20Sopenharmony_ci			if (irsample <= MCE_MAX_PULSE_LENGTH) {
9668c2ecf20Sopenharmony_ci				irbuf[ircount] = irsample;
9678c2ecf20Sopenharmony_ci				irsample = 0;
9688c2ecf20Sopenharmony_ci			} else {
9698c2ecf20Sopenharmony_ci				irbuf[ircount] = MCE_MAX_PULSE_LENGTH;
9708c2ecf20Sopenharmony_ci				irsample -= MCE_MAX_PULSE_LENGTH;
9718c2ecf20Sopenharmony_ci			}
9728c2ecf20Sopenharmony_ci			/*
9738c2ecf20Sopenharmony_ci			 * Even i = IR pulse
9748c2ecf20Sopenharmony_ci			 * Odd  i = IR space
9758c2ecf20Sopenharmony_ci			 */
9768c2ecf20Sopenharmony_ci			irbuf[ircount] |= (i & 1 ? 0 : MCE_PULSE_BIT);
9778c2ecf20Sopenharmony_ci			ircount++;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci			/* IR buffer full? */
9808c2ecf20Sopenharmony_ci			if (ircount >= MCE_IRBUF_SIZE) {
9818c2ecf20Sopenharmony_ci				/* Fix packet length in last header */
9828c2ecf20Sopenharmony_ci				length = ircount % MCE_PACKET_SIZE;
9838c2ecf20Sopenharmony_ci				if (length > 0)
9848c2ecf20Sopenharmony_ci					irbuf[ircount - length] -=
9858c2ecf20Sopenharmony_ci						MCE_PACKET_SIZE - length;
9868c2ecf20Sopenharmony_ci				/* Send full buffer */
9878c2ecf20Sopenharmony_ci				ret = mce_write(ir, irbuf, ircount);
9888c2ecf20Sopenharmony_ci				if (ret < 0)
9898c2ecf20Sopenharmony_ci					return ret;
9908c2ecf20Sopenharmony_ci				ircount = 0;
9918c2ecf20Sopenharmony_ci			}
9928c2ecf20Sopenharmony_ci		}
9938c2ecf20Sopenharmony_ci	} /* after for loop, 0 <= ircount < MCE_IRBUF_SIZE */
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	/* Fix packet length in last header */
9968c2ecf20Sopenharmony_ci	length = ircount % MCE_PACKET_SIZE;
9978c2ecf20Sopenharmony_ci	if (length > 0)
9988c2ecf20Sopenharmony_ci		irbuf[ircount - length] -= MCE_PACKET_SIZE - length;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	/* Append IR trailer (0x80) to final partial (or empty) IR buffer */
10018c2ecf20Sopenharmony_ci	irbuf[ircount++] = MCE_IRDATA_TRAILER;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	/* Send final buffer */
10048c2ecf20Sopenharmony_ci	ret = mce_write(ir, irbuf, ircount);
10058c2ecf20Sopenharmony_ci	if (ret < 0)
10068c2ecf20Sopenharmony_ci		return ret;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	return count;
10098c2ecf20Sopenharmony_ci}
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci/* Sets active IR outputs -- mce devices typically have two */
10128c2ecf20Sopenharmony_cistatic int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
10138c2ecf20Sopenharmony_ci{
10148c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = dev->priv;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	/* return number of transmitters */
10178c2ecf20Sopenharmony_ci	int emitters = ir->num_txports ? ir->num_txports : 2;
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	if (mask >= (1 << emitters))
10208c2ecf20Sopenharmony_ci		return emitters;
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	if (ir->flags.tx_mask_normal)
10238c2ecf20Sopenharmony_ci		ir->tx_mask = mask;
10248c2ecf20Sopenharmony_ci	else
10258c2ecf20Sopenharmony_ci		ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
10268c2ecf20Sopenharmony_ci				mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	return 0;
10298c2ecf20Sopenharmony_ci}
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci/* Sets the send carrier frequency and mode */
10328c2ecf20Sopenharmony_cistatic int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
10338c2ecf20Sopenharmony_ci{
10348c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = dev->priv;
10358c2ecf20Sopenharmony_ci	int clk = 10000000;
10368c2ecf20Sopenharmony_ci	int prescaler = 0, divisor = 0;
10378c2ecf20Sopenharmony_ci	unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
10388c2ecf20Sopenharmony_ci				    MCE_CMD_SETIRCFS, 0x00, 0x00 };
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	/* Carrier has changed */
10418c2ecf20Sopenharmony_ci	if (ir->carrier != carrier) {
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci		if (carrier == 0) {
10448c2ecf20Sopenharmony_ci			ir->carrier = carrier;
10458c2ecf20Sopenharmony_ci			cmdbuf[2] = MCE_CMD_SIG_END;
10468c2ecf20Sopenharmony_ci			cmdbuf[3] = MCE_IRDATA_TRAILER;
10478c2ecf20Sopenharmony_ci			dev_dbg(ir->dev, "disabling carrier modulation");
10488c2ecf20Sopenharmony_ci			mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
10498c2ecf20Sopenharmony_ci			return 0;
10508c2ecf20Sopenharmony_ci		}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci		for (prescaler = 0; prescaler < 4; ++prescaler) {
10538c2ecf20Sopenharmony_ci			divisor = (clk >> (2 * prescaler)) / carrier;
10548c2ecf20Sopenharmony_ci			if (divisor <= 0xff) {
10558c2ecf20Sopenharmony_ci				ir->carrier = carrier;
10568c2ecf20Sopenharmony_ci				cmdbuf[2] = prescaler;
10578c2ecf20Sopenharmony_ci				cmdbuf[3] = divisor;
10588c2ecf20Sopenharmony_ci				dev_dbg(ir->dev, "requesting %u HZ carrier",
10598c2ecf20Sopenharmony_ci								carrier);
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci				/* Transmit new carrier to mce device */
10628c2ecf20Sopenharmony_ci				mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
10638c2ecf20Sopenharmony_ci				return 0;
10648c2ecf20Sopenharmony_ci			}
10658c2ecf20Sopenharmony_ci		}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci		return -EINVAL;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	return 0;
10728c2ecf20Sopenharmony_ci}
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_cistatic int mceusb_set_timeout(struct rc_dev *dev, unsigned int timeout)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	u8 cmdbuf[4] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTIMEOUT, 0, 0 };
10778c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = dev->priv;
10788c2ecf20Sopenharmony_ci	unsigned int units;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	units = DIV_ROUND_UP(timeout, MCE_TIME_UNIT);
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	cmdbuf[2] = units >> 8;
10838c2ecf20Sopenharmony_ci	cmdbuf[3] = units;
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	/* get receiver timeout value */
10888c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	return 0;
10918c2ecf20Sopenharmony_ci}
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci/*
10948c2ecf20Sopenharmony_ci * Select or deselect the 2nd receiver port.
10958c2ecf20Sopenharmony_ci * Second receiver is learning mode, wide-band, short-range receiver.
10968c2ecf20Sopenharmony_ci * Only one receiver (long or short range) may be active at a time.
10978c2ecf20Sopenharmony_ci */
10988c2ecf20Sopenharmony_cistatic int mceusb_set_rx_wideband(struct rc_dev *dev, int enable)
10998c2ecf20Sopenharmony_ci{
11008c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = dev->priv;
11018c2ecf20Sopenharmony_ci	unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
11028c2ecf20Sopenharmony_ci				    MCE_CMD_SETIRRXPORTEN, 0x00 };
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	dev_dbg(ir->dev, "select %s-range receive sensor",
11058c2ecf20Sopenharmony_ci		enable ? "short" : "long");
11068c2ecf20Sopenharmony_ci	if (enable) {
11078c2ecf20Sopenharmony_ci		ir->wideband_rx_enabled = true;
11088c2ecf20Sopenharmony_ci		cmdbuf[2] = 2;	/* port 2 is short range receiver */
11098c2ecf20Sopenharmony_ci	} else {
11108c2ecf20Sopenharmony_ci		ir->wideband_rx_enabled = false;
11118c2ecf20Sopenharmony_ci		cmdbuf[2] = 1;	/* port 1 is long range receiver */
11128c2ecf20Sopenharmony_ci	}
11138c2ecf20Sopenharmony_ci	mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
11148c2ecf20Sopenharmony_ci	/* response from device sets ir->learning_active */
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	return 0;
11178c2ecf20Sopenharmony_ci}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci/*
11208c2ecf20Sopenharmony_ci * Enable/disable receiver carrier frequency pass through reporting.
11218c2ecf20Sopenharmony_ci * Only the short-range receiver has carrier frequency measuring capability.
11228c2ecf20Sopenharmony_ci * Implicitly select this receiver when enabling carrier frequency reporting.
11238c2ecf20Sopenharmony_ci */
11248c2ecf20Sopenharmony_cistatic int mceusb_set_rx_carrier_report(struct rc_dev *dev, int enable)
11258c2ecf20Sopenharmony_ci{
11268c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = dev->priv;
11278c2ecf20Sopenharmony_ci	unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
11288c2ecf20Sopenharmony_ci				    MCE_CMD_SETIRRXPORTEN, 0x00 };
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	dev_dbg(ir->dev, "%s short-range receiver carrier reporting",
11318c2ecf20Sopenharmony_ci		enable ? "enable" : "disable");
11328c2ecf20Sopenharmony_ci	if (enable) {
11338c2ecf20Sopenharmony_ci		ir->carrier_report_enabled = true;
11348c2ecf20Sopenharmony_ci		if (!ir->learning_active) {
11358c2ecf20Sopenharmony_ci			cmdbuf[2] = 2;	/* port 2 is short range receiver */
11368c2ecf20Sopenharmony_ci			mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
11378c2ecf20Sopenharmony_ci		}
11388c2ecf20Sopenharmony_ci	} else {
11398c2ecf20Sopenharmony_ci		ir->carrier_report_enabled = false;
11408c2ecf20Sopenharmony_ci		/*
11418c2ecf20Sopenharmony_ci		 * Revert to normal (long-range) receiver only if the
11428c2ecf20Sopenharmony_ci		 * wideband (short-range) receiver wasn't explicitly
11438c2ecf20Sopenharmony_ci		 * enabled.
11448c2ecf20Sopenharmony_ci		 */
11458c2ecf20Sopenharmony_ci		if (ir->learning_active && !ir->wideband_rx_enabled) {
11468c2ecf20Sopenharmony_ci			cmdbuf[2] = 1;	/* port 1 is long range receiver */
11478c2ecf20Sopenharmony_ci			mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
11488c2ecf20Sopenharmony_ci		}
11498c2ecf20Sopenharmony_ci	}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	return 0;
11528c2ecf20Sopenharmony_ci}
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci/*
11558c2ecf20Sopenharmony_ci * Handle PORT_SYS/IR command response received from the MCE device.
11568c2ecf20Sopenharmony_ci *
11578c2ecf20Sopenharmony_ci * Assumes single response with all its data (not truncated)
11588c2ecf20Sopenharmony_ci * in buf_in[]. The response itself determines its total length
11598c2ecf20Sopenharmony_ci * (mceusb_cmd_datasize() + 2) and hence the minimum size of buf_in[].
11608c2ecf20Sopenharmony_ci *
11618c2ecf20Sopenharmony_ci * We don't do anything but print debug spew for many of the command bits
11628c2ecf20Sopenharmony_ci * we receive from the hardware, but some of them are useful information
11638c2ecf20Sopenharmony_ci * we want to store so that we can use them.
11648c2ecf20Sopenharmony_ci */
11658c2ecf20Sopenharmony_cistatic void mceusb_handle_command(struct mceusb_dev *ir, u8 *buf_in)
11668c2ecf20Sopenharmony_ci{
11678c2ecf20Sopenharmony_ci	u8 cmd = buf_in[0];
11688c2ecf20Sopenharmony_ci	u8 subcmd = buf_in[1];
11698c2ecf20Sopenharmony_ci	u8 *hi = &buf_in[2];		/* read only when required */
11708c2ecf20Sopenharmony_ci	u8 *lo = &buf_in[3];		/* read only when required */
11718c2ecf20Sopenharmony_ci	struct ir_raw_event rawir = {};
11728c2ecf20Sopenharmony_ci	u32 carrier_cycles;
11738c2ecf20Sopenharmony_ci	u32 cycles_fix;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	if (cmd == MCE_CMD_PORT_SYS) {
11768c2ecf20Sopenharmony_ci		switch (subcmd) {
11778c2ecf20Sopenharmony_ci		/* the one and only 5-byte return value command */
11788c2ecf20Sopenharmony_ci		case MCE_RSP_GETPORTSTATUS:
11798c2ecf20Sopenharmony_ci			if (buf_in[5] == 0 && *hi < 8)
11808c2ecf20Sopenharmony_ci				ir->txports_cabled |= 1 << *hi;
11818c2ecf20Sopenharmony_ci			break;
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci		/* 1-byte return value commands */
11848c2ecf20Sopenharmony_ci		case MCE_RSP_EQEMVER:
11858c2ecf20Sopenharmony_ci			ir->emver = *hi;
11868c2ecf20Sopenharmony_ci			break;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci		/* No return value commands */
11898c2ecf20Sopenharmony_ci		case MCE_RSP_CMD_ILLEGAL:
11908c2ecf20Sopenharmony_ci			ir->need_reset = true;
11918c2ecf20Sopenharmony_ci			break;
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci		default:
11948c2ecf20Sopenharmony_ci			break;
11958c2ecf20Sopenharmony_ci		}
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci		return;
11988c2ecf20Sopenharmony_ci	}
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	if (cmd != MCE_CMD_PORT_IR)
12018c2ecf20Sopenharmony_ci		return;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	switch (subcmd) {
12048c2ecf20Sopenharmony_ci	/* 2-byte return value commands */
12058c2ecf20Sopenharmony_ci	case MCE_RSP_EQIRTIMEOUT:
12068c2ecf20Sopenharmony_ci		ir->rc->timeout = (*hi << 8 | *lo) * MCE_TIME_UNIT;
12078c2ecf20Sopenharmony_ci		break;
12088c2ecf20Sopenharmony_ci	case MCE_RSP_EQIRNUMPORTS:
12098c2ecf20Sopenharmony_ci		ir->num_txports = *hi;
12108c2ecf20Sopenharmony_ci		ir->num_rxports = *lo;
12118c2ecf20Sopenharmony_ci		break;
12128c2ecf20Sopenharmony_ci	case MCE_RSP_EQIRRXCFCNT:
12138c2ecf20Sopenharmony_ci		/*
12148c2ecf20Sopenharmony_ci		 * The carrier cycle counter can overflow and wrap around
12158c2ecf20Sopenharmony_ci		 * without notice from the device. So frequency measurement
12168c2ecf20Sopenharmony_ci		 * will be inaccurate with long duration IR.
12178c2ecf20Sopenharmony_ci		 *
12188c2ecf20Sopenharmony_ci		 * The long-range (non learning) receiver always reports
12198c2ecf20Sopenharmony_ci		 * zero count so we always ignore its report.
12208c2ecf20Sopenharmony_ci		 */
12218c2ecf20Sopenharmony_ci		if (ir->carrier_report_enabled && ir->learning_active &&
12228c2ecf20Sopenharmony_ci		    ir->pulse_tunit > 0) {
12238c2ecf20Sopenharmony_ci			carrier_cycles = (*hi << 8 | *lo);
12248c2ecf20Sopenharmony_ci			/*
12258c2ecf20Sopenharmony_ci			 * Adjust carrier cycle count by adding
12268c2ecf20Sopenharmony_ci			 * 1 missed count per pulse "on"
12278c2ecf20Sopenharmony_ci			 */
12288c2ecf20Sopenharmony_ci			cycles_fix = ir->flags.rx2 == 2 ? ir->pulse_count : 0;
12298c2ecf20Sopenharmony_ci			rawir.carrier_report = 1;
12308c2ecf20Sopenharmony_ci			rawir.carrier = (1000000u / MCE_TIME_UNIT) *
12318c2ecf20Sopenharmony_ci					(carrier_cycles + cycles_fix) /
12328c2ecf20Sopenharmony_ci					ir->pulse_tunit;
12338c2ecf20Sopenharmony_ci			dev_dbg(ir->dev, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)",
12348c2ecf20Sopenharmony_ci				rawir.carrier, ir->pulse_count, carrier_cycles,
12358c2ecf20Sopenharmony_ci				ir->pulse_tunit, ir->flags.rx2);
12368c2ecf20Sopenharmony_ci			ir_raw_event_store(ir->rc, &rawir);
12378c2ecf20Sopenharmony_ci		}
12388c2ecf20Sopenharmony_ci		break;
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	/* 1-byte return value commands */
12418c2ecf20Sopenharmony_ci	case MCE_RSP_EQIRTXPORTS:
12428c2ecf20Sopenharmony_ci		ir->tx_mask = *hi;
12438c2ecf20Sopenharmony_ci		break;
12448c2ecf20Sopenharmony_ci	case MCE_RSP_EQIRRXPORTEN:
12458c2ecf20Sopenharmony_ci		ir->learning_active = ((*hi & 0x02) == 0x02);
12468c2ecf20Sopenharmony_ci		if (ir->rxports_active != *hi) {
12478c2ecf20Sopenharmony_ci			dev_info(ir->dev, "%s-range (0x%x) receiver active",
12488c2ecf20Sopenharmony_ci				 ir->learning_active ? "short" : "long", *hi);
12498c2ecf20Sopenharmony_ci			ir->rxports_active = *hi;
12508c2ecf20Sopenharmony_ci		}
12518c2ecf20Sopenharmony_ci		break;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	/* No return value commands */
12548c2ecf20Sopenharmony_ci	case MCE_RSP_CMD_ILLEGAL:
12558c2ecf20Sopenharmony_ci	case MCE_RSP_TX_TIMEOUT:
12568c2ecf20Sopenharmony_ci		ir->need_reset = true;
12578c2ecf20Sopenharmony_ci		break;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	default:
12608c2ecf20Sopenharmony_ci		break;
12618c2ecf20Sopenharmony_ci	}
12628c2ecf20Sopenharmony_ci}
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_cistatic void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
12658c2ecf20Sopenharmony_ci{
12668c2ecf20Sopenharmony_ci	struct ir_raw_event rawir = {};
12678c2ecf20Sopenharmony_ci	bool event = false;
12688c2ecf20Sopenharmony_ci	int i = 0;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	/* skip meaningless 0xb1 0x60 header bytes on orig receiver */
12718c2ecf20Sopenharmony_ci	if (ir->flags.microsoft_gen1)
12728c2ecf20Sopenharmony_ci		i = 2;
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	/* if there's no data, just return now */
12758c2ecf20Sopenharmony_ci	if (buf_len <= i)
12768c2ecf20Sopenharmony_ci		return;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	for (; i < buf_len; i++) {
12798c2ecf20Sopenharmony_ci		switch (ir->parser_state) {
12808c2ecf20Sopenharmony_ci		case SUBCMD:
12818c2ecf20Sopenharmony_ci			ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]);
12828c2ecf20Sopenharmony_ci			mceusb_dev_printdata(ir, ir->buf_in, buf_len, i - 1,
12838c2ecf20Sopenharmony_ci					     ir->rem + 2, false);
12848c2ecf20Sopenharmony_ci			if (i + ir->rem < buf_len)
12858c2ecf20Sopenharmony_ci				mceusb_handle_command(ir, &ir->buf_in[i - 1]);
12868c2ecf20Sopenharmony_ci			ir->parser_state = CMD_DATA;
12878c2ecf20Sopenharmony_ci			break;
12888c2ecf20Sopenharmony_ci		case PARSE_IRDATA:
12898c2ecf20Sopenharmony_ci			ir->rem--;
12908c2ecf20Sopenharmony_ci			rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
12918c2ecf20Sopenharmony_ci			rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK);
12928c2ecf20Sopenharmony_ci			if (unlikely(!rawir.duration)) {
12938c2ecf20Sopenharmony_ci				dev_dbg(ir->dev, "nonsensical irdata %02x with duration 0",
12948c2ecf20Sopenharmony_ci					ir->buf_in[i]);
12958c2ecf20Sopenharmony_ci				break;
12968c2ecf20Sopenharmony_ci			}
12978c2ecf20Sopenharmony_ci			if (rawir.pulse) {
12988c2ecf20Sopenharmony_ci				ir->pulse_tunit += rawir.duration;
12998c2ecf20Sopenharmony_ci				ir->pulse_count++;
13008c2ecf20Sopenharmony_ci			}
13018c2ecf20Sopenharmony_ci			rawir.duration *= MCE_TIME_UNIT;
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci			dev_dbg(ir->dev, "Storing %s %u us (%02x)",
13048c2ecf20Sopenharmony_ci				rawir.pulse ? "pulse" : "space",
13058c2ecf20Sopenharmony_ci				rawir.duration,	ir->buf_in[i]);
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci			if (ir_raw_event_store_with_filter(ir->rc, &rawir))
13088c2ecf20Sopenharmony_ci				event = true;
13098c2ecf20Sopenharmony_ci			break;
13108c2ecf20Sopenharmony_ci		case CMD_DATA:
13118c2ecf20Sopenharmony_ci			ir->rem--;
13128c2ecf20Sopenharmony_ci			break;
13138c2ecf20Sopenharmony_ci		case CMD_HEADER:
13148c2ecf20Sopenharmony_ci			ir->cmd = ir->buf_in[i];
13158c2ecf20Sopenharmony_ci			if ((ir->cmd == MCE_CMD_PORT_IR) ||
13168c2ecf20Sopenharmony_ci			    ((ir->cmd & MCE_PORT_MASK) !=
13178c2ecf20Sopenharmony_ci			     MCE_COMMAND_IRDATA)) {
13188c2ecf20Sopenharmony_ci				/*
13198c2ecf20Sopenharmony_ci				 * got PORT_SYS, PORT_IR, or unknown
13208c2ecf20Sopenharmony_ci				 * command response prefix
13218c2ecf20Sopenharmony_ci				 */
13228c2ecf20Sopenharmony_ci				ir->parser_state = SUBCMD;
13238c2ecf20Sopenharmony_ci				continue;
13248c2ecf20Sopenharmony_ci			}
13258c2ecf20Sopenharmony_ci			/*
13268c2ecf20Sopenharmony_ci			 * got IR data prefix (0x80 + num_bytes)
13278c2ecf20Sopenharmony_ci			 * decode MCE packets of the form {0x83, AA, BB, CC}
13288c2ecf20Sopenharmony_ci			 * IR data packets can span USB messages
13298c2ecf20Sopenharmony_ci			 */
13308c2ecf20Sopenharmony_ci			ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
13318c2ecf20Sopenharmony_ci			mceusb_dev_printdata(ir, ir->buf_in, buf_len,
13328c2ecf20Sopenharmony_ci					     i, ir->rem + 1, false);
13338c2ecf20Sopenharmony_ci			if (ir->rem) {
13348c2ecf20Sopenharmony_ci				ir->parser_state = PARSE_IRDATA;
13358c2ecf20Sopenharmony_ci			} else {
13368c2ecf20Sopenharmony_ci				struct ir_raw_event ev = {
13378c2ecf20Sopenharmony_ci					.timeout = 1,
13388c2ecf20Sopenharmony_ci					.duration = ir->rc->timeout
13398c2ecf20Sopenharmony_ci				};
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci				if (ir_raw_event_store_with_filter(ir->rc,
13428c2ecf20Sopenharmony_ci								   &ev))
13438c2ecf20Sopenharmony_ci					event = true;
13448c2ecf20Sopenharmony_ci				ir->pulse_tunit = 0;
13458c2ecf20Sopenharmony_ci				ir->pulse_count = 0;
13468c2ecf20Sopenharmony_ci			}
13478c2ecf20Sopenharmony_ci			break;
13488c2ecf20Sopenharmony_ci		}
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci		if (ir->parser_state != CMD_HEADER && !ir->rem)
13518c2ecf20Sopenharmony_ci			ir->parser_state = CMD_HEADER;
13528c2ecf20Sopenharmony_ci	}
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	/*
13558c2ecf20Sopenharmony_ci	 * Accept IR data spanning multiple rx buffers.
13568c2ecf20Sopenharmony_ci	 * Reject MCE command response spanning multiple rx buffers.
13578c2ecf20Sopenharmony_ci	 */
13588c2ecf20Sopenharmony_ci	if (ir->parser_state != PARSE_IRDATA || !ir->rem)
13598c2ecf20Sopenharmony_ci		ir->parser_state = CMD_HEADER;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	if (event) {
13628c2ecf20Sopenharmony_ci		dev_dbg(ir->dev, "processed IR data");
13638c2ecf20Sopenharmony_ci		ir_raw_event_handle(ir->rc);
13648c2ecf20Sopenharmony_ci	}
13658c2ecf20Sopenharmony_ci}
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_cistatic void mceusb_dev_recv(struct urb *urb)
13688c2ecf20Sopenharmony_ci{
13698c2ecf20Sopenharmony_ci	struct mceusb_dev *ir;
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	if (!urb)
13728c2ecf20Sopenharmony_ci		return;
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	ir = urb->context;
13758c2ecf20Sopenharmony_ci	if (!ir) {
13768c2ecf20Sopenharmony_ci		usb_unlink_urb(urb);
13778c2ecf20Sopenharmony_ci		return;
13788c2ecf20Sopenharmony_ci	}
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	switch (urb->status) {
13818c2ecf20Sopenharmony_ci	/* success */
13828c2ecf20Sopenharmony_ci	case 0:
13838c2ecf20Sopenharmony_ci		mceusb_process_ir_data(ir, urb->actual_length);
13848c2ecf20Sopenharmony_ci		break;
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	case -ECONNRESET:
13878c2ecf20Sopenharmony_ci	case -ENOENT:
13888c2ecf20Sopenharmony_ci	case -EILSEQ:
13898c2ecf20Sopenharmony_ci	case -EPROTO:
13908c2ecf20Sopenharmony_ci	case -ESHUTDOWN:
13918c2ecf20Sopenharmony_ci		usb_unlink_urb(urb);
13928c2ecf20Sopenharmony_ci		return;
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	case -EPIPE:
13958c2ecf20Sopenharmony_ci		dev_err(ir->dev, "Error: urb status = %d (RX HALT)",
13968c2ecf20Sopenharmony_ci			urb->status);
13978c2ecf20Sopenharmony_ci		mceusb_defer_kevent(ir, EVENT_RX_HALT);
13988c2ecf20Sopenharmony_ci		return;
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	default:
14018c2ecf20Sopenharmony_ci		dev_err(ir->dev, "Error: urb status = %d", urb->status);
14028c2ecf20Sopenharmony_ci		break;
14038c2ecf20Sopenharmony_ci	}
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci	usb_submit_urb(urb, GFP_ATOMIC);
14068c2ecf20Sopenharmony_ci}
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_cistatic void mceusb_get_emulator_version(struct mceusb_dev *ir)
14098c2ecf20Sopenharmony_ci{
14108c2ecf20Sopenharmony_ci	/* If we get no reply or an illegal command reply, its ver 1, says MS */
14118c2ecf20Sopenharmony_ci	ir->emver = 1;
14128c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_EMVER, sizeof(GET_EMVER));
14138c2ecf20Sopenharmony_ci}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_cistatic void mceusb_gen1_init(struct mceusb_dev *ir)
14168c2ecf20Sopenharmony_ci{
14178c2ecf20Sopenharmony_ci	int ret;
14188c2ecf20Sopenharmony_ci	struct device *dev = ir->dev;
14198c2ecf20Sopenharmony_ci	char data[USB_CTRL_MSG_SZ];
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	/*
14228c2ecf20Sopenharmony_ci	 * This is a strange one. Windows issues a set address to the device
14238c2ecf20Sopenharmony_ci	 * on the receive control pipe and expect a certain value pair back
14248c2ecf20Sopenharmony_ci	 */
14258c2ecf20Sopenharmony_ci	ret = usb_control_msg_recv(ir->usbdev, 0, USB_REQ_SET_ADDRESS,
14268c2ecf20Sopenharmony_ci				   USB_DIR_IN | USB_TYPE_VENDOR,
14278c2ecf20Sopenharmony_ci				   0, 0, data, USB_CTRL_MSG_SZ, 3000,
14288c2ecf20Sopenharmony_ci				   GFP_KERNEL);
14298c2ecf20Sopenharmony_ci	dev_dbg(dev, "set address - ret = %d", ret);
14308c2ecf20Sopenharmony_ci	dev_dbg(dev, "set address - data[0] = %d, data[1] = %d",
14318c2ecf20Sopenharmony_ci						data[0], data[1]);
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	/* set feature: bit rate 38400 bps */
14348c2ecf20Sopenharmony_ci	ret = usb_control_msg_send(ir->usbdev, 0,
14358c2ecf20Sopenharmony_ci				   USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
14368c2ecf20Sopenharmony_ci				   0xc04e, 0x0000, NULL, 0, 3000, GFP_KERNEL);
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci	dev_dbg(dev, "set feature - ret = %d", ret);
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	/* bRequest 4: set char length to 8 bits */
14418c2ecf20Sopenharmony_ci	ret = usb_control_msg_send(ir->usbdev, 0,
14428c2ecf20Sopenharmony_ci				   4, USB_TYPE_VENDOR,
14438c2ecf20Sopenharmony_ci				   0x0808, 0x0000, NULL, 0, 3000, GFP_KERNEL);
14448c2ecf20Sopenharmony_ci	dev_dbg(dev, "set char length - retB = %d", ret);
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	/* bRequest 2: set handshaking to use DTR/DSR */
14478c2ecf20Sopenharmony_ci	ret = usb_control_msg_send(ir->usbdev, 0,
14488c2ecf20Sopenharmony_ci				   2, USB_TYPE_VENDOR,
14498c2ecf20Sopenharmony_ci				   0x0000, 0x0100, NULL, 0, 3000, GFP_KERNEL);
14508c2ecf20Sopenharmony_ci	dev_dbg(dev, "set handshake  - retC = %d", ret);
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	/* device resume */
14538c2ecf20Sopenharmony_ci	mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	/* get hw/sw revision? */
14568c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_REVISION, sizeof(GET_REVISION));
14578c2ecf20Sopenharmony_ci}
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_cistatic void mceusb_gen2_init(struct mceusb_dev *ir)
14608c2ecf20Sopenharmony_ci{
14618c2ecf20Sopenharmony_ci	/* device resume */
14628c2ecf20Sopenharmony_ci	mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	/* get wake version (protocol, key, address) */
14658c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	/* unknown what this one actually returns... */
14688c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
14698c2ecf20Sopenharmony_ci}
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_cistatic void mceusb_get_parameters(struct mceusb_dev *ir)
14728c2ecf20Sopenharmony_ci{
14738c2ecf20Sopenharmony_ci	int i;
14748c2ecf20Sopenharmony_ci	unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
14758c2ecf20Sopenharmony_ci				    MCE_CMD_GETPORTSTATUS, 0x00 };
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_ci	/* defaults, if the hardware doesn't support querying */
14788c2ecf20Sopenharmony_ci	ir->num_txports = 2;
14798c2ecf20Sopenharmony_ci	ir->num_rxports = 2;
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	/* get number of tx and rx ports */
14828c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	/* get the carrier and frequency */
14858c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	if (ir->num_txports && !ir->flags.no_tx)
14888c2ecf20Sopenharmony_ci		/* get the transmitter bitmask */
14898c2ecf20Sopenharmony_ci		mce_command_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	/* get receiver timeout value */
14928c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	/* get receiver sensor setting */
14958c2ecf20Sopenharmony_ci	mce_command_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci	for (i = 0; i < ir->num_txports; i++) {
14988c2ecf20Sopenharmony_ci		cmdbuf[2] = i;
14998c2ecf20Sopenharmony_ci		mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
15008c2ecf20Sopenharmony_ci	}
15018c2ecf20Sopenharmony_ci}
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_cistatic void mceusb_flash_led(struct mceusb_dev *ir)
15048c2ecf20Sopenharmony_ci{
15058c2ecf20Sopenharmony_ci	if (ir->emver < 2)
15068c2ecf20Sopenharmony_ci		return;
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci	mce_command_out(ir, FLASH_LED, sizeof(FLASH_LED));
15098c2ecf20Sopenharmony_ci}
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci/*
15128c2ecf20Sopenharmony_ci * Workqueue function
15138c2ecf20Sopenharmony_ci * for resetting or recovering device after occurrence of error events
15148c2ecf20Sopenharmony_ci * specified in ir->kevent bit field.
15158c2ecf20Sopenharmony_ci * Function runs (via schedule_work()) in non-interrupt context, for
15168c2ecf20Sopenharmony_ci * calls here (such as usb_clear_halt()) requiring non-interrupt context.
15178c2ecf20Sopenharmony_ci */
15188c2ecf20Sopenharmony_cistatic void mceusb_deferred_kevent(struct work_struct *work)
15198c2ecf20Sopenharmony_ci{
15208c2ecf20Sopenharmony_ci	struct mceusb_dev *ir =
15218c2ecf20Sopenharmony_ci		container_of(work, struct mceusb_dev, kevent);
15228c2ecf20Sopenharmony_ci	int status;
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	dev_err(ir->dev, "kevent handler called (flags 0x%lx)",
15258c2ecf20Sopenharmony_ci		ir->kevent_flags);
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci	if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) {
15288c2ecf20Sopenharmony_ci		dev_err(ir->dev, "kevent handler canceled pending USB Reset Device");
15298c2ecf20Sopenharmony_ci		return;
15308c2ecf20Sopenharmony_ci	}
15318c2ecf20Sopenharmony_ci
15328c2ecf20Sopenharmony_ci	if (test_bit(EVENT_RX_HALT, &ir->kevent_flags)) {
15338c2ecf20Sopenharmony_ci		usb_unlink_urb(ir->urb_in);
15348c2ecf20Sopenharmony_ci		status = usb_clear_halt(ir->usbdev, ir->pipe_in);
15358c2ecf20Sopenharmony_ci		dev_err(ir->dev, "rx clear halt status = %d", status);
15368c2ecf20Sopenharmony_ci		if (status < 0) {
15378c2ecf20Sopenharmony_ci			/*
15388c2ecf20Sopenharmony_ci			 * Unable to clear RX halt/stall.
15398c2ecf20Sopenharmony_ci			 * Will need to call usb_reset_device().
15408c2ecf20Sopenharmony_ci			 */
15418c2ecf20Sopenharmony_ci			dev_err(ir->dev,
15428c2ecf20Sopenharmony_ci				"stuck RX HALT state requires USB Reset Device to clear");
15438c2ecf20Sopenharmony_ci			usb_queue_reset_device(ir->usbintf);
15448c2ecf20Sopenharmony_ci			set_bit(EVENT_RST_PEND, &ir->kevent_flags);
15458c2ecf20Sopenharmony_ci			clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci			/* Cancel all other error events and handlers */
15488c2ecf20Sopenharmony_ci			clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
15498c2ecf20Sopenharmony_ci			return;
15508c2ecf20Sopenharmony_ci		}
15518c2ecf20Sopenharmony_ci		clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
15528c2ecf20Sopenharmony_ci		status = usb_submit_urb(ir->urb_in, GFP_KERNEL);
15538c2ecf20Sopenharmony_ci		if (status < 0) {
15548c2ecf20Sopenharmony_ci			dev_err(ir->dev, "rx unhalt submit urb error = %d",
15558c2ecf20Sopenharmony_ci				status);
15568c2ecf20Sopenharmony_ci		}
15578c2ecf20Sopenharmony_ci	}
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	if (test_bit(EVENT_TX_HALT, &ir->kevent_flags)) {
15608c2ecf20Sopenharmony_ci		status = usb_clear_halt(ir->usbdev, ir->pipe_out);
15618c2ecf20Sopenharmony_ci		dev_err(ir->dev, "tx clear halt status = %d", status);
15628c2ecf20Sopenharmony_ci		if (status < 0) {
15638c2ecf20Sopenharmony_ci			/*
15648c2ecf20Sopenharmony_ci			 * Unable to clear TX halt/stall.
15658c2ecf20Sopenharmony_ci			 * Will need to call usb_reset_device().
15668c2ecf20Sopenharmony_ci			 */
15678c2ecf20Sopenharmony_ci			dev_err(ir->dev,
15688c2ecf20Sopenharmony_ci				"stuck TX HALT state requires USB Reset Device to clear");
15698c2ecf20Sopenharmony_ci			usb_queue_reset_device(ir->usbintf);
15708c2ecf20Sopenharmony_ci			set_bit(EVENT_RST_PEND, &ir->kevent_flags);
15718c2ecf20Sopenharmony_ci			clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci			/* Cancel all other error events and handlers */
15748c2ecf20Sopenharmony_ci			clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
15758c2ecf20Sopenharmony_ci			return;
15768c2ecf20Sopenharmony_ci		}
15778c2ecf20Sopenharmony_ci		clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
15788c2ecf20Sopenharmony_ci	}
15798c2ecf20Sopenharmony_ci}
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_cistatic struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
15828c2ecf20Sopenharmony_ci{
15838c2ecf20Sopenharmony_ci	struct usb_device *udev = ir->usbdev;
15848c2ecf20Sopenharmony_ci	struct device *dev = ir->dev;
15858c2ecf20Sopenharmony_ci	struct rc_dev *rc;
15868c2ecf20Sopenharmony_ci	int ret;
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_ci	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
15898c2ecf20Sopenharmony_ci	if (!rc) {
15908c2ecf20Sopenharmony_ci		dev_err(dev, "remote dev allocation failed");
15918c2ecf20Sopenharmony_ci		goto out;
15928c2ecf20Sopenharmony_ci	}
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci	snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
15958c2ecf20Sopenharmony_ci		 mceusb_model[ir->model].name ?
15968c2ecf20Sopenharmony_ci			mceusb_model[ir->model].name :
15978c2ecf20Sopenharmony_ci			"Media Center Ed. eHome Infrared Remote Transceiver",
15988c2ecf20Sopenharmony_ci		 le16_to_cpu(ir->usbdev->descriptor.idVendor),
15998c2ecf20Sopenharmony_ci		 le16_to_cpu(ir->usbdev->descriptor.idProduct));
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_ci	usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_ci	rc->device_name = ir->name;
16048c2ecf20Sopenharmony_ci	rc->input_phys = ir->phys;
16058c2ecf20Sopenharmony_ci	usb_to_input_id(ir->usbdev, &rc->input_id);
16068c2ecf20Sopenharmony_ci	rc->dev.parent = dev;
16078c2ecf20Sopenharmony_ci	rc->priv = ir;
16088c2ecf20Sopenharmony_ci	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
16098c2ecf20Sopenharmony_ci	rc->min_timeout = MCE_TIME_UNIT;
16108c2ecf20Sopenharmony_ci	rc->timeout = MS_TO_US(100);
16118c2ecf20Sopenharmony_ci	if (!mceusb_model[ir->model].broken_irtimeout) {
16128c2ecf20Sopenharmony_ci		rc->s_timeout = mceusb_set_timeout;
16138c2ecf20Sopenharmony_ci		rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
16148c2ecf20Sopenharmony_ci	} else {
16158c2ecf20Sopenharmony_ci		/*
16168c2ecf20Sopenharmony_ci		 * If we can't set the timeout using CMD_SETIRTIMEOUT, we can
16178c2ecf20Sopenharmony_ci		 * rely on software timeouts for timeouts < 100ms.
16188c2ecf20Sopenharmony_ci		 */
16198c2ecf20Sopenharmony_ci		rc->max_timeout = rc->timeout;
16208c2ecf20Sopenharmony_ci	}
16218c2ecf20Sopenharmony_ci	if (!ir->flags.no_tx) {
16228c2ecf20Sopenharmony_ci		rc->s_tx_mask = mceusb_set_tx_mask;
16238c2ecf20Sopenharmony_ci		rc->s_tx_carrier = mceusb_set_tx_carrier;
16248c2ecf20Sopenharmony_ci		rc->tx_ir = mceusb_tx_ir;
16258c2ecf20Sopenharmony_ci	}
16268c2ecf20Sopenharmony_ci	if (ir->flags.rx2 > 0) {
16278c2ecf20Sopenharmony_ci		rc->s_learning_mode = mceusb_set_rx_wideband;
16288c2ecf20Sopenharmony_ci		rc->s_carrier_report = mceusb_set_rx_carrier_report;
16298c2ecf20Sopenharmony_ci	}
16308c2ecf20Sopenharmony_ci	rc->driver_name = DRIVER_NAME;
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci	switch (le16_to_cpu(udev->descriptor.idVendor)) {
16338c2ecf20Sopenharmony_ci	case VENDOR_HAUPPAUGE:
16348c2ecf20Sopenharmony_ci		rc->map_name = RC_MAP_HAUPPAUGE;
16358c2ecf20Sopenharmony_ci		break;
16368c2ecf20Sopenharmony_ci	case VENDOR_PCTV:
16378c2ecf20Sopenharmony_ci		rc->map_name = RC_MAP_PINNACLE_PCTV_HD;
16388c2ecf20Sopenharmony_ci		break;
16398c2ecf20Sopenharmony_ci	default:
16408c2ecf20Sopenharmony_ci		rc->map_name = RC_MAP_RC6_MCE;
16418c2ecf20Sopenharmony_ci	}
16428c2ecf20Sopenharmony_ci	if (mceusb_model[ir->model].rc_map)
16438c2ecf20Sopenharmony_ci		rc->map_name = mceusb_model[ir->model].rc_map;
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_ci	ret = rc_register_device(rc);
16468c2ecf20Sopenharmony_ci	if (ret < 0) {
16478c2ecf20Sopenharmony_ci		dev_err(dev, "remote dev registration failed");
16488c2ecf20Sopenharmony_ci		goto out;
16498c2ecf20Sopenharmony_ci	}
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	return rc;
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_ciout:
16548c2ecf20Sopenharmony_ci	rc_free_device(rc);
16558c2ecf20Sopenharmony_ci	return NULL;
16568c2ecf20Sopenharmony_ci}
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_cistatic int mceusb_dev_probe(struct usb_interface *intf,
16598c2ecf20Sopenharmony_ci			    const struct usb_device_id *id)
16608c2ecf20Sopenharmony_ci{
16618c2ecf20Sopenharmony_ci	struct usb_device *dev = interface_to_usbdev(intf);
16628c2ecf20Sopenharmony_ci	struct usb_host_interface *idesc;
16638c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *ep = NULL;
16648c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *ep_in = NULL;
16658c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *ep_out = NULL;
16668c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = NULL;
16678c2ecf20Sopenharmony_ci	int pipe, maxp, i, res;
16688c2ecf20Sopenharmony_ci	char buf[63], name[128] = "";
16698c2ecf20Sopenharmony_ci	enum mceusb_model_type model = id->driver_info;
16708c2ecf20Sopenharmony_ci	bool is_gen3;
16718c2ecf20Sopenharmony_ci	bool is_microsoft_gen1;
16728c2ecf20Sopenharmony_ci	bool tx_mask_normal;
16738c2ecf20Sopenharmony_ci	int ir_intfnum;
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	dev_dbg(&intf->dev, "%s called", __func__);
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	idesc  = intf->cur_altsetting;
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci	is_gen3 = mceusb_model[model].mce_gen3;
16808c2ecf20Sopenharmony_ci	is_microsoft_gen1 = mceusb_model[model].mce_gen1;
16818c2ecf20Sopenharmony_ci	tx_mask_normal = mceusb_model[model].tx_mask_normal;
16828c2ecf20Sopenharmony_ci	ir_intfnum = mceusb_model[model].ir_intfnum;
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ci	/* There are multi-function devices with non-IR interfaces */
16858c2ecf20Sopenharmony_ci	if (idesc->desc.bInterfaceNumber != ir_intfnum)
16868c2ecf20Sopenharmony_ci		return -ENODEV;
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	/* step through the endpoints to find first bulk in and out endpoint */
16898c2ecf20Sopenharmony_ci	for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
16908c2ecf20Sopenharmony_ci		ep = &idesc->endpoint[i].desc;
16918c2ecf20Sopenharmony_ci
16928c2ecf20Sopenharmony_ci		if (ep_in == NULL) {
16938c2ecf20Sopenharmony_ci			if (usb_endpoint_is_bulk_in(ep)) {
16948c2ecf20Sopenharmony_ci				ep_in = ep;
16958c2ecf20Sopenharmony_ci				dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n");
16968c2ecf20Sopenharmony_ci			} else if (usb_endpoint_is_int_in(ep)) {
16978c2ecf20Sopenharmony_ci				ep_in = ep;
16988c2ecf20Sopenharmony_ci				ep_in->bInterval = 1;
16998c2ecf20Sopenharmony_ci				dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n");
17008c2ecf20Sopenharmony_ci			}
17018c2ecf20Sopenharmony_ci		}
17028c2ecf20Sopenharmony_ci
17038c2ecf20Sopenharmony_ci		if (ep_out == NULL) {
17048c2ecf20Sopenharmony_ci			if (usb_endpoint_is_bulk_out(ep)) {
17058c2ecf20Sopenharmony_ci				ep_out = ep;
17068c2ecf20Sopenharmony_ci				dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n");
17078c2ecf20Sopenharmony_ci			} else if (usb_endpoint_is_int_out(ep)) {
17088c2ecf20Sopenharmony_ci				ep_out = ep;
17098c2ecf20Sopenharmony_ci				ep_out->bInterval = 1;
17108c2ecf20Sopenharmony_ci				dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n");
17118c2ecf20Sopenharmony_ci			}
17128c2ecf20Sopenharmony_ci		}
17138c2ecf20Sopenharmony_ci	}
17148c2ecf20Sopenharmony_ci	if (!ep_in || !ep_out) {
17158c2ecf20Sopenharmony_ci		dev_dbg(&intf->dev, "required endpoints not found\n");
17168c2ecf20Sopenharmony_ci		return -ENODEV;
17178c2ecf20Sopenharmony_ci	}
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci	if (usb_endpoint_xfer_int(ep_in))
17208c2ecf20Sopenharmony_ci		pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
17218c2ecf20Sopenharmony_ci	else
17228c2ecf20Sopenharmony_ci		pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress);
17238c2ecf20Sopenharmony_ci	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
17248c2ecf20Sopenharmony_ci
17258c2ecf20Sopenharmony_ci	ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
17268c2ecf20Sopenharmony_ci	if (!ir)
17278c2ecf20Sopenharmony_ci		goto mem_alloc_fail;
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci	ir->pipe_in = pipe;
17308c2ecf20Sopenharmony_ci	ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_KERNEL, &ir->dma_in);
17318c2ecf20Sopenharmony_ci	if (!ir->buf_in)
17328c2ecf20Sopenharmony_ci		goto buf_in_alloc_fail;
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
17358c2ecf20Sopenharmony_ci	if (!ir->urb_in)
17368c2ecf20Sopenharmony_ci		goto urb_in_alloc_fail;
17378c2ecf20Sopenharmony_ci
17388c2ecf20Sopenharmony_ci	ir->usbintf = intf;
17398c2ecf20Sopenharmony_ci	ir->usbdev = usb_get_dev(dev);
17408c2ecf20Sopenharmony_ci	ir->dev = &intf->dev;
17418c2ecf20Sopenharmony_ci	ir->len_in = maxp;
17428c2ecf20Sopenharmony_ci	ir->flags.microsoft_gen1 = is_microsoft_gen1;
17438c2ecf20Sopenharmony_ci	ir->flags.tx_mask_normal = tx_mask_normal;
17448c2ecf20Sopenharmony_ci	ir->flags.no_tx = mceusb_model[model].no_tx;
17458c2ecf20Sopenharmony_ci	ir->flags.rx2 = mceusb_model[model].rx2;
17468c2ecf20Sopenharmony_ci	ir->model = model;
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci	/* Saving usb interface data for use by the transmitter routine */
17498c2ecf20Sopenharmony_ci	ir->usb_ep_out = ep_out;
17508c2ecf20Sopenharmony_ci	if (usb_endpoint_xfer_int(ep_out))
17518c2ecf20Sopenharmony_ci		ir->pipe_out = usb_sndintpipe(ir->usbdev,
17528c2ecf20Sopenharmony_ci					      ep_out->bEndpointAddress);
17538c2ecf20Sopenharmony_ci	else
17548c2ecf20Sopenharmony_ci		ir->pipe_out = usb_sndbulkpipe(ir->usbdev,
17558c2ecf20Sopenharmony_ci					       ep_out->bEndpointAddress);
17568c2ecf20Sopenharmony_ci
17578c2ecf20Sopenharmony_ci	if (dev->descriptor.iManufacturer
17588c2ecf20Sopenharmony_ci	    && usb_string(dev, dev->descriptor.iManufacturer,
17598c2ecf20Sopenharmony_ci			  buf, sizeof(buf)) > 0)
17608c2ecf20Sopenharmony_ci		strscpy(name, buf, sizeof(name));
17618c2ecf20Sopenharmony_ci	if (dev->descriptor.iProduct
17628c2ecf20Sopenharmony_ci	    && usb_string(dev, dev->descriptor.iProduct,
17638c2ecf20Sopenharmony_ci			  buf, sizeof(buf)) > 0)
17648c2ecf20Sopenharmony_ci		snprintf(name + strlen(name), sizeof(name) - strlen(name),
17658c2ecf20Sopenharmony_ci			 " %s", buf);
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ci	/*
17688c2ecf20Sopenharmony_ci	 * Initialize async USB error handler before registering
17698c2ecf20Sopenharmony_ci	 * or activating any mceusb RX and TX functions
17708c2ecf20Sopenharmony_ci	 */
17718c2ecf20Sopenharmony_ci	INIT_WORK(&ir->kevent, mceusb_deferred_kevent);
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_ci	ir->rc = mceusb_init_rc_dev(ir);
17748c2ecf20Sopenharmony_ci	if (!ir->rc)
17758c2ecf20Sopenharmony_ci		goto rc_dev_fail;
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci	/* wire up inbound data handler */
17788c2ecf20Sopenharmony_ci	if (usb_endpoint_xfer_int(ep_in))
17798c2ecf20Sopenharmony_ci		usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
17808c2ecf20Sopenharmony_ci				 mceusb_dev_recv, ir, ep_in->bInterval);
17818c2ecf20Sopenharmony_ci	else
17828c2ecf20Sopenharmony_ci		usb_fill_bulk_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
17838c2ecf20Sopenharmony_ci				  mceusb_dev_recv, ir);
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	ir->urb_in->transfer_dma = ir->dma_in;
17868c2ecf20Sopenharmony_ci	ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_ci	/* flush buffers on the device */
17898c2ecf20Sopenharmony_ci	dev_dbg(&intf->dev, "Flushing receive buffers");
17908c2ecf20Sopenharmony_ci	res = usb_submit_urb(ir->urb_in, GFP_KERNEL);
17918c2ecf20Sopenharmony_ci	if (res)
17928c2ecf20Sopenharmony_ci		dev_err(&intf->dev, "failed to flush buffers: %d", res);
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci	/* figure out which firmware/emulator version this hardware has */
17958c2ecf20Sopenharmony_ci	mceusb_get_emulator_version(ir);
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci	/* initialize device */
17988c2ecf20Sopenharmony_ci	if (ir->flags.microsoft_gen1)
17998c2ecf20Sopenharmony_ci		mceusb_gen1_init(ir);
18008c2ecf20Sopenharmony_ci	else if (!is_gen3)
18018c2ecf20Sopenharmony_ci		mceusb_gen2_init(ir);
18028c2ecf20Sopenharmony_ci
18038c2ecf20Sopenharmony_ci	mceusb_get_parameters(ir);
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci	mceusb_flash_led(ir);
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_ci	if (!ir->flags.no_tx)
18088c2ecf20Sopenharmony_ci		mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
18098c2ecf20Sopenharmony_ci
18108c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, ir);
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	/* enable wake via this device */
18138c2ecf20Sopenharmony_ci	device_set_wakeup_capable(ir->dev, true);
18148c2ecf20Sopenharmony_ci	device_set_wakeup_enable(ir->dev, true);
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	dev_info(&intf->dev, "Registered %s with mce emulator interface version %x",
18178c2ecf20Sopenharmony_ci		name, ir->emver);
18188c2ecf20Sopenharmony_ci	dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
18198c2ecf20Sopenharmony_ci		 ir->num_txports, ir->txports_cabled,
18208c2ecf20Sopenharmony_ci		 ir->num_rxports, ir->rxports_active);
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ci	return 0;
18238c2ecf20Sopenharmony_ci
18248c2ecf20Sopenharmony_ci	/* Error-handling path */
18258c2ecf20Sopenharmony_circ_dev_fail:
18268c2ecf20Sopenharmony_ci	cancel_work_sync(&ir->kevent);
18278c2ecf20Sopenharmony_ci	usb_put_dev(ir->usbdev);
18288c2ecf20Sopenharmony_ci	usb_kill_urb(ir->urb_in);
18298c2ecf20Sopenharmony_ci	usb_free_urb(ir->urb_in);
18308c2ecf20Sopenharmony_ciurb_in_alloc_fail:
18318c2ecf20Sopenharmony_ci	usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
18328c2ecf20Sopenharmony_cibuf_in_alloc_fail:
18338c2ecf20Sopenharmony_ci	kfree(ir);
18348c2ecf20Sopenharmony_cimem_alloc_fail:
18358c2ecf20Sopenharmony_ci	dev_err(&intf->dev, "%s: device setup failed!", __func__);
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci	return -ENOMEM;
18388c2ecf20Sopenharmony_ci}
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_cistatic void mceusb_dev_disconnect(struct usb_interface *intf)
18428c2ecf20Sopenharmony_ci{
18438c2ecf20Sopenharmony_ci	struct usb_device *dev = interface_to_usbdev(intf);
18448c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = usb_get_intfdata(intf);
18458c2ecf20Sopenharmony_ci
18468c2ecf20Sopenharmony_ci	dev_dbg(&intf->dev, "%s called", __func__);
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, NULL);
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci	if (!ir)
18518c2ecf20Sopenharmony_ci		return;
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci	ir->usbdev = NULL;
18548c2ecf20Sopenharmony_ci	cancel_work_sync(&ir->kevent);
18558c2ecf20Sopenharmony_ci	rc_unregister_device(ir->rc);
18568c2ecf20Sopenharmony_ci	usb_kill_urb(ir->urb_in);
18578c2ecf20Sopenharmony_ci	usb_free_urb(ir->urb_in);
18588c2ecf20Sopenharmony_ci	usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
18598c2ecf20Sopenharmony_ci	usb_put_dev(dev);
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_ci	kfree(ir);
18628c2ecf20Sopenharmony_ci}
18638c2ecf20Sopenharmony_ci
18648c2ecf20Sopenharmony_cistatic int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
18658c2ecf20Sopenharmony_ci{
18668c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = usb_get_intfdata(intf);
18678c2ecf20Sopenharmony_ci	dev_info(ir->dev, "suspend");
18688c2ecf20Sopenharmony_ci	usb_kill_urb(ir->urb_in);
18698c2ecf20Sopenharmony_ci	return 0;
18708c2ecf20Sopenharmony_ci}
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_cistatic int mceusb_dev_resume(struct usb_interface *intf)
18738c2ecf20Sopenharmony_ci{
18748c2ecf20Sopenharmony_ci	struct mceusb_dev *ir = usb_get_intfdata(intf);
18758c2ecf20Sopenharmony_ci	dev_info(ir->dev, "resume");
18768c2ecf20Sopenharmony_ci	if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
18778c2ecf20Sopenharmony_ci		return -EIO;
18788c2ecf20Sopenharmony_ci	return 0;
18798c2ecf20Sopenharmony_ci}
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_cistatic struct usb_driver mceusb_dev_driver = {
18828c2ecf20Sopenharmony_ci	.name =		DRIVER_NAME,
18838c2ecf20Sopenharmony_ci	.probe =	mceusb_dev_probe,
18848c2ecf20Sopenharmony_ci	.disconnect =	mceusb_dev_disconnect,
18858c2ecf20Sopenharmony_ci	.suspend =	mceusb_dev_suspend,
18868c2ecf20Sopenharmony_ci	.resume =	mceusb_dev_resume,
18878c2ecf20Sopenharmony_ci	.reset_resume =	mceusb_dev_resume,
18888c2ecf20Sopenharmony_ci	.id_table =	mceusb_dev_table
18898c2ecf20Sopenharmony_ci};
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_cimodule_usb_driver(mceusb_dev_driver);
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
18948c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR);
18958c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
18968c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1897