blob: 2dc810f5a73f736f57bec58b8d0e52350de119ea [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jarod Wilson66e89522010-06-01 17:32:08 -03002/*
3 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
4 *
Jarod Wilsonfda516b2011-07-18 16:54:29 -03005 * Copyright (c) 2010-2011, Jarod Wilson <jarod@redhat.com>
Jarod Wilson66e89522010-06-01 17:32:08 -03006 *
7 * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
8 * Conti, Martin Blatter and Daniel Melander, the latter of which was
9 * in turn also based on the lirc_atiusb driver by Paul Miller. The
10 * two mce drivers were merged into one by Jarod Wilson, with transmit
11 * support for the 1st-gen device added primarily by Patrick Calhoun,
12 * with a bit of tweaks by Jarod. Debugging improvements and proper
13 * support for what appears to be 3rd-gen hardware added by Jarod.
14 * Initial port from lirc driver to ir-core drivery by Jarod, based
15 * partially on a port to an earlier proposed IR infrastructure by
16 * Jon Smirl, which included enhancements and simplifications to the
17 * incoming IR buffer parsing routines.
18 *
Jarod Wilsonfda516b2011-07-18 16:54:29 -030019 * Updated in July of 2011 with the aid of Microsoft's official
20 * remote/transceiver requirements and specification document, found at
21 * download.microsoft.com, title
22 * Windows-Media-Center-RC-IR-Collection-Green-Button-Specification-03-08-2011-V2.pdf
Jarod Wilson66e89522010-06-01 17:32:08 -030023 */
24
25#include <linux/device.h>
26#include <linux/module.h>
27#include <linux/slab.h>
A Suna06854a2017-03-26 15:28:08 -030028#include <linux/workqueue.h>
Paul Bender635f76b2010-12-16 13:23:07 -030029#include <linux/usb.h>
30#include <linux/usb/input.h>
Jarod Wilsonfa3348982011-07-18 16:54:23 -030031#include <linux/pm_wakeup.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030032#include <media/rc-core.h>
Jarod Wilson66e89522010-06-01 17:32:08 -030033
A Sun9fc3ce312019-08-15 13:41:19 -030034#define DRIVER_VERSION "1.95"
Jarod Wilsonfda516b2011-07-18 16:54:29 -030035#define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
Jarod Wilson66e89522010-06-01 17:32:08 -030036#define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
37 "device driver"
38#define DRIVER_NAME "mceusb"
39
A Sun9fc3ce312019-08-15 13:41:19 -030040#define USB_TX_TIMEOUT 1000 /* in milliseconds */
Jarod Wilson4a883912010-10-22 14:42:54 -030041#define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
42#define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
Jarod Wilson66e89522010-06-01 17:32:08 -030043
44/* MCE constants */
A Sun9fc3ce312019-08-15 13:41:19 -030045#define MCE_IRBUF_SIZE 128 /* TX IR buffer length */
Jarod Wilson4a883912010-10-22 14:42:54 -030046#define MCE_TIME_UNIT 50 /* Approx 50us resolution */
A Sun9fc3ce312019-08-15 13:41:19 -030047#define MCE_PACKET_SIZE 31 /* Max length of packet (with header) */
48#define MCE_IRDATA_HEADER (0x80 + MCE_PACKET_SIZE - 1)
49 /* Actual format is 0x80 + num_bytes */
Jarod Wilson4a883912010-10-22 14:42:54 -030050#define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
Jarod Wilson4a883912010-10-22 14:42:54 -030051#define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
52#define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
53#define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
54#define MCE_PULSE_MASK 0x7f /* Pulse mask */
55#define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
56
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -030057/*
58 * The interface between the host and the IR hardware is command-response
59 * based. All commands and responses have a consistent format, where a lead
60 * byte always identifies the type of data following it. The lead byte has
61 * a port value in the 3 highest bits and a length value in the 5 lowest
62 * bits.
63 *
64 * The length field is overloaded, with a value of 11111 indicating that the
65 * following byte is a command or response code, and the length of the entire
66 * message is determined by the code. If the length field is not 11111, then
67 * it specifies the number of bytes of port data that follow.
68 */
69#define MCE_CMD 0x1f
70#define MCE_PORT_IR 0x4 /* (0x4 << 5) | MCE_CMD = 0x9f */
71#define MCE_PORT_SYS 0x7 /* (0x7 << 5) | MCE_CMD = 0xff */
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -050072#define MCE_PORT_SER 0x6 /* 0xc0 through 0xdf flush & 0x1f bytes */
Sean Young6b4a16c2014-01-20 19:10:44 -030073#define MCE_PORT_MASK 0xe0 /* Mask out command bits */
Jarod Wilson4a883912010-10-22 14:42:54 -030074
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -030075/* Command port headers */
76#define MCE_CMD_PORT_IR 0x9f /* IR-related cmd/rsp */
77#define MCE_CMD_PORT_SYS 0xff /* System (non-IR) device cmd/rsp */
78
79/* Commands that set device state (2-4 bytes in length) */
80#define MCE_CMD_RESET 0xfe /* Reset device, 2 bytes */
81#define MCE_CMD_RESUME 0xaa /* Resume device after error, 2 bytes */
82#define MCE_CMD_SETIRCFS 0x06 /* Set tx carrier, 4 bytes */
83#define MCE_CMD_SETIRTIMEOUT 0x0c /* Set timeout, 4 bytes */
84#define MCE_CMD_SETIRTXPORTS 0x08 /* Set tx ports, 3 bytes */
85#define MCE_CMD_SETIRRXPORTEN 0x14 /* Set rx ports, 3 bytes */
86#define MCE_CMD_FLASHLED 0x23 /* Flash receiver LED, 2 bytes */
87
88/* Commands that query device state (all 2 bytes, unless noted) */
89#define MCE_CMD_GETIRCFS 0x07 /* Get carrier */
90#define MCE_CMD_GETIRTIMEOUT 0x0d /* Get timeout */
91#define MCE_CMD_GETIRTXPORTS 0x13 /* Get tx ports */
92#define MCE_CMD_GETIRRXPORTEN 0x15 /* Get rx ports */
93#define MCE_CMD_GETPORTSTATUS 0x11 /* Get tx port status, 3 bytes */
94#define MCE_CMD_GETIRNUMPORTS 0x16 /* Get number of ports */
95#define MCE_CMD_GETWAKESOURCE 0x17 /* Get wake source */
96#define MCE_CMD_GETEMVER 0x22 /* Get emulator interface version */
97#define MCE_CMD_GETDEVDETAILS 0x21 /* Get device details (em ver2 only) */
98#define MCE_CMD_GETWAKESUPPORT 0x20 /* Get wake details (em ver2 only) */
99#define MCE_CMD_GETWAKEVERSION 0x18 /* Get wake pattern (em ver2 only) */
100
101/* Misc commands */
102#define MCE_CMD_NOP 0xff /* No operation */
103
104/* Responses to commands (non-error cases) */
105#define MCE_RSP_EQIRCFS 0x06 /* tx carrier, 4 bytes */
106#define MCE_RSP_EQIRTIMEOUT 0x0c /* rx timeout, 4 bytes */
107#define MCE_RSP_GETWAKESOURCE 0x17 /* wake source, 3 bytes */
108#define MCE_RSP_EQIRTXPORTS 0x08 /* tx port mask, 3 bytes */
109#define MCE_RSP_EQIRRXPORTEN 0x14 /* rx port mask, 3 bytes */
110#define MCE_RSP_GETPORTSTATUS 0x11 /* tx port status, 7 bytes */
111#define MCE_RSP_EQIRRXCFCNT 0x15 /* rx carrier count, 4 bytes */
112#define MCE_RSP_EQIRNUMPORTS 0x16 /* number of ports, 4 bytes */
113#define MCE_RSP_EQWAKESUPPORT 0x20 /* wake capabilities, 3 bytes */
114#define MCE_RSP_EQWAKEVERSION 0x18 /* wake pattern details, 6 bytes */
115#define MCE_RSP_EQDEVDETAILS 0x21 /* device capabilities, 3 bytes */
116#define MCE_RSP_EQEMVER 0x22 /* emulator interface ver, 3 bytes */
117#define MCE_RSP_FLASHLED 0x23 /* success flashing LED, 2 bytes */
118
119/* Responses to error cases, must send MCE_CMD_RESUME to clear them */
120#define MCE_RSP_CMD_ILLEGAL 0xfe /* illegal command for port, 2 bytes */
121#define MCE_RSP_TX_TIMEOUT 0x81 /* tx timed out, 2 bytes */
122
123/* Misc commands/responses not defined in the MCE remote/transceiver spec */
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300124#define MCE_CMD_SIG_END 0x01 /* End of signal */
Jarod Wilson4a883912010-10-22 14:42:54 -0300125#define MCE_CMD_PING 0x03 /* Ping device */
126#define MCE_CMD_UNKNOWN 0x04 /* Unknown */
127#define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
Jarod Wilson4a883912010-10-22 14:42:54 -0300128#define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
129#define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
130#define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
Jarod Wilson4a883912010-10-22 14:42:54 -0300131#define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
132#define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
Jarod Wilson4a883912010-10-22 14:42:54 -0300133#define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
134#define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300135#define MCE_CMD_NULL 0x00 /* These show up various places... */
Jarod Wilson66e89522010-06-01 17:32:08 -0300136
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300137/* if buf[i] & MCE_PORT_MASK == 0x80 and buf[i] != MCE_CMD_PORT_IR,
138 * then we're looking at a raw IR data sample */
139#define MCE_COMMAND_IRDATA 0x80
140#define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
Jarod Wilson66e89522010-06-01 17:32:08 -0300141
Jarod Wilson66e89522010-06-01 17:32:08 -0300142#define VENDOR_PHILIPS 0x0471
143#define VENDOR_SMK 0x0609
144#define VENDOR_TATUNG 0x1460
145#define VENDOR_GATEWAY 0x107b
146#define VENDOR_SHUTTLE 0x1308
147#define VENDOR_SHUTTLE2 0x051c
148#define VENDOR_MITSUMI 0x03ee
149#define VENDOR_TOPSEED 0x1784
150#define VENDOR_RICAVISION 0x179d
151#define VENDOR_ITRON 0x195d
152#define VENDOR_FIC 0x1509
153#define VENDOR_LG 0x043e
154#define VENDOR_MICROSOFT 0x045e
155#define VENDOR_FORMOSA 0x147a
156#define VENDOR_FINTEK 0x1934
157#define VENDOR_PINNACLE 0x2304
158#define VENDOR_ECS 0x1019
159#define VENDOR_WISTRON 0x0fb8
160#define VENDOR_COMPRO 0x185b
161#define VENDOR_NORTHSTAR 0x04eb
162#define VENDOR_REALTEK 0x0bda
163#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300164#define VENDOR_CONEXANT 0x0572
Mark Lorda4de5f02012-07-11 18:53:28 -0300165#define VENDOR_TWISTEDMELON 0x2596
Matthias Schwarzotteda9dfd2013-11-21 17:26:42 -0300166#define VENDOR_HAUPPAUGE 0x2040
Mauro Carvalho Chehab9683e012014-07-27 17:06:02 -0300167#define VENDOR_PCTV 0x2013
Olli Salonene1866132016-03-17 10:11:09 -0300168#define VENDOR_ADAPTEC 0x03f3
Jarod Wilson66e89522010-06-01 17:32:08 -0300169
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300170enum mceusb_model_type {
171 MCE_GEN2 = 0, /* Most boards */
172 MCE_GEN1,
173 MCE_GEN3,
Sean Youngaec3ead2018-05-09 06:11:28 -0400174 MCE_GEN3_BROKEN_IRTIMEOUT,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300175 MCE_GEN2_TX_INV,
Sean Young35ecf2b2018-03-18 06:46:02 -0400176 MCE_GEN2_TX_INV_RX_GOOD,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300177 POLARIS_EVK,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300178 CX_HYBRID_TV,
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300179 MULTIFUNCTION,
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300180 TIVO_KIT,
Jarod Wilson2faa0ca2011-04-19 16:47:34 -0300181 MCE_GEN2_NO_TX,
Matthias Schwarzotteda9dfd2013-11-21 17:26:42 -0300182 HAUPPAUGE_CX_HYBRID_TV,
Oleh Kravchenko47f42f32017-10-28 09:38:16 -0400183 EVROMEDIA_FULL_HYBRID_FULLHD,
Oleh Kravchenko8ff19cd2017-10-28 09:38:18 -0400184 ASTROMETA_T2HYBRID,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300185};
186
187struct mceusb_model {
188 u32 mce_gen1:1;
189 u32 mce_gen2:1;
190 u32 mce_gen3:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300191 u32 tx_mask_normal:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300192 u32 no_tx:1;
Sean Youngaec3ead2018-05-09 06:11:28 -0400193 u32 broken_irtimeout:1;
A Sun279c60f2018-03-16 15:52:09 -0400194 /*
195 * 2nd IR receiver (short-range, wideband) for learning mode:
196 * 0, absent 2nd receiver (rx2)
197 * 1, rx2 present
198 * 2, rx2 which under counts IR carrier cycles
199 */
200 u32 rx2;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300201
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300202 int ir_intfnum;
203
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300204 const char *rc_map; /* Allow specify a per-board map */
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300205 const char *name; /* per-board name */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300206};
207
208static const struct mceusb_model mceusb_model[] = {
209 [MCE_GEN1] = {
210 .mce_gen1 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300211 .tx_mask_normal = 1,
A Sun279c60f2018-03-16 15:52:09 -0400212 .rx2 = 2,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300213 },
214 [MCE_GEN2] = {
215 .mce_gen2 = 1,
A Sun279c60f2018-03-16 15:52:09 -0400216 .rx2 = 2,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300217 },
Jarod Wilson2faa0ca2011-04-19 16:47:34 -0300218 [MCE_GEN2_NO_TX] = {
219 .mce_gen2 = 1,
220 .no_tx = 1,
221 },
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300222 [MCE_GEN2_TX_INV] = {
223 .mce_gen2 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300224 .tx_mask_normal = 1,
A Sun279c60f2018-03-16 15:52:09 -0400225 .rx2 = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300226 },
Sean Young35ecf2b2018-03-18 06:46:02 -0400227 [MCE_GEN2_TX_INV_RX_GOOD] = {
228 .mce_gen2 = 1,
229 .tx_mask_normal = 1,
230 .rx2 = 2,
231 },
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300232 [MCE_GEN3] = {
233 .mce_gen3 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300234 .tx_mask_normal = 1,
A Sun279c60f2018-03-16 15:52:09 -0400235 .rx2 = 2,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300236 },
Sean Youngaec3ead2018-05-09 06:11:28 -0400237 [MCE_GEN3_BROKEN_IRTIMEOUT] = {
238 .mce_gen3 = 1,
239 .tx_mask_normal = 1,
240 .rx2 = 2,
241 .broken_irtimeout = 1
242 },
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300243 [POLARIS_EVK] = {
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300244 /*
245 * In fact, the EVK is shipped without
246 * remotes, but we should have something handy,
247 * to allow testing it
248 */
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300249 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
A Sun279c60f2018-03-16 15:52:09 -0400250 .rx2 = 2,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300251 },
252 [CX_HYBRID_TV] = {
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300253 .no_tx = 1, /* tx isn't wired up at all */
254 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300255 },
Matthias Schwarzotteda9dfd2013-11-21 17:26:42 -0300256 [HAUPPAUGE_CX_HYBRID_TV] = {
Matthias Schwarzotteda9dfd2013-11-21 17:26:42 -0300257 .no_tx = 1, /* eeprom says it has no tx */
258 .name = "Conexant Hybrid TV (cx231xx) MCE IR no TX",
259 },
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300260 [MULTIFUNCTION] = {
261 .mce_gen2 = 1,
262 .ir_intfnum = 2,
A Sun279c60f2018-03-16 15:52:09 -0400263 .rx2 = 2,
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300264 },
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300265 [TIVO_KIT] = {
266 .mce_gen2 = 1,
267 .rc_map = RC_MAP_TIVO,
A Sun279c60f2018-03-16 15:52:09 -0400268 .rx2 = 2,
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300269 },
Oleh Kravchenko47f42f32017-10-28 09:38:16 -0400270 [EVROMEDIA_FULL_HYBRID_FULLHD] = {
271 .name = "Evromedia USB Full Hybrid Full HD",
272 .no_tx = 1,
273 .rc_map = RC_MAP_MSI_DIGIVOX_III,
274 },
Oleh Kravchenko8ff19cd2017-10-28 09:38:18 -0400275 [ASTROMETA_T2HYBRID] = {
276 .name = "Astrometa T2Hybrid",
277 .no_tx = 1,
278 .rc_map = RC_MAP_ASTROMETA_T2HYBRID,
279 }
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300280};
281
Arvind Yadav5fad16b2017-08-13 05:54:44 -0300282static const struct usb_device_id mceusb_dev_table[] = {
Jarod Wilson66e89522010-06-01 17:32:08 -0300283 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300284 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
285 .driver_info = MCE_GEN1 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300286 /* Philips Infrared Transceiver - Sahara branded */
287 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
288 /* Philips Infrared Transceiver - HP branded */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300289 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
290 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300291 /* Philips SRM5100 */
292 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
293 /* Philips Infrared Transceiver - Omaura */
294 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
295 /* Philips Infrared Transceiver - Spinel plus */
296 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
297 /* Philips eHome Infrared Transceiver */
298 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300299 /* Philips/Spinel plus IR transceiver for ASUS */
300 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
301 /* Philips/Spinel plus IR transceiver for ASUS */
302 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilsone296e122011-04-25 14:48:18 -0300303 /* Philips IR transceiver (Dell branded) */
Sean Young8dfef672013-01-29 08:19:29 -0300304 { USB_DEVICE(VENDOR_PHILIPS, 0x2093),
305 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300306 /* Realtek MCE IR Receiver and card reader */
307 { USB_DEVICE(VENDOR_REALTEK, 0x0161),
308 .driver_info = MULTIFUNCTION },
Jarod Wilson66e89522010-06-01 17:32:08 -0300309 /* SMK/Toshiba G83C0004D410 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300310 { USB_DEVICE(VENDOR_SMK, 0x031d),
Sean Young35ecf2b2018-03-18 06:46:02 -0400311 .driver_info = MCE_GEN2_TX_INV_RX_GOOD },
Jarod Wilson66e89522010-06-01 17:32:08 -0300312 /* SMK eHome Infrared Transceiver (Sony VAIO) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300313 { USB_DEVICE(VENDOR_SMK, 0x0322),
314 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300315 /* bundled with Hauppauge PVR-150 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300316 { USB_DEVICE(VENDOR_SMK, 0x0334),
317 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300318 /* SMK eHome Infrared Transceiver */
319 { USB_DEVICE(VENDOR_SMK, 0x0338) },
Jarod Wilsonb825fe1b2011-05-26 16:03:17 -0300320 /* SMK/I-O Data GV-MC7/RCKIT Receiver */
321 { USB_DEVICE(VENDOR_SMK, 0x0353),
322 .driver_info = MCE_GEN2_NO_TX },
Olli Salonen18693842016-03-17 10:11:10 -0300323 /* SMK RXX6000 Infrared Receiver */
324 { USB_DEVICE(VENDOR_SMK, 0x0357),
325 .driver_info = MCE_GEN2_NO_TX },
Jarod Wilson66e89522010-06-01 17:32:08 -0300326 /* Tatung eHome Infrared Transceiver */
327 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
328 /* Shuttle eHome Infrared Transceiver */
329 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
330 /* Shuttle eHome Infrared Transceiver */
331 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
332 /* Gateway eHome Infrared Transceiver */
333 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
334 /* Mitsumi */
335 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
336 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300337 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
338 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300339 /* Topseed HP eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300340 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
341 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300342 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300343 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
344 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300345 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300346 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
347 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300348 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300349 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
350 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300351 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300352 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
Sean Youngaec3ead2018-05-09 06:11:28 -0400353 .driver_info = MCE_GEN3_BROKEN_IRTIMEOUT },
Jarod Wilson66e89522010-06-01 17:32:08 -0300354 /* Ricavision internal Infrared Transceiver */
355 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
356 /* Itron ione Libra Q-11 */
357 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
358 /* FIC eHome Infrared Transceiver */
359 { USB_DEVICE(VENDOR_FIC, 0x9242) },
360 /* LG eHome Infrared Transceiver */
361 { USB_DEVICE(VENDOR_LG, 0x9803) },
362 /* Microsoft MCE Infrared Transceiver */
363 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
364 /* Formosa eHome Infrared Transceiver */
365 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
366 /* Formosa21 / eHome Infrared Receiver */
367 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
368 /* Formosa aim / Trust MCE Infrared Receiver */
Jarod Wilson2faa0ca2011-04-19 16:47:34 -0300369 { USB_DEVICE(VENDOR_FORMOSA, 0xe017),
370 .driver_info = MCE_GEN2_NO_TX },
Jarod Wilson66e89522010-06-01 17:32:08 -0300371 /* Formosa Industrial Computing / Beanbag Emulation Device */
372 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
373 /* Formosa21 / eHome Infrared Receiver */
374 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
375 /* Formosa Industrial Computing AIM IR605/A */
376 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
377 /* Formosa Industrial Computing */
378 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
Jarod Wilson22f17322012-03-12 13:27:03 -0300379 /* Formosa Industrial Computing */
380 { USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
Jarod Wilsonfbb1f1b2010-12-16 13:27:11 -0300381 /* Fintek eHome Infrared Transceiver (HP branded) */
Sean Youngdb8ee102013-01-29 08:19:30 -0300382 { USB_DEVICE(VENDOR_FINTEK, 0x5168),
383 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300384 /* Fintek eHome Infrared Transceiver */
385 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
386 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
387 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
388 /* Pinnacle Remote Kit */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300389 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
390 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300391 /* Elitegroup Computer Systems IR */
392 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
393 /* Wistron Corp. eHome Infrared Receiver */
394 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
395 /* Compro K100 */
396 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
397 /* Compro K100 v2 */
398 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
399 /* Northstar Systems, Inc. eHome Infrared Transceiver */
400 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
401 /* TiVo PC IR Receiver */
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300402 { USB_DEVICE(VENDOR_TIVO, 0x2000),
403 .driver_info = TIVO_KIT },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300404 /* Conexant Hybrid TV "Shelby" Polaris SDK */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300405 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
406 .driver_info = POLARIS_EVK },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300407 /* Conexant Hybrid TV RDU253S Polaris */
408 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
409 .driver_info = CX_HYBRID_TV },
Mark Lorda4de5f02012-07-11 18:53:28 -0300410 /* Twisted Melon Inc. - Manta Mini Receiver */
411 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
412 /* Twisted Melon Inc. - Manta Pico Receiver */
413 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
414 /* Twisted Melon Inc. - Manta Transceiver */
415 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
Matthias Schwarzotteda9dfd2013-11-21 17:26:42 -0300416 /* Hauppauge WINTV-HVR-HVR 930C-HD - based on cx231xx */
417 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb130),
418 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
Mauro Carvalho Chehab9683e012014-07-27 17:06:02 -0300419 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb131),
420 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
Matthias Schwarzott66756612014-08-31 08:35:10 -0300421 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb138),
422 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
423 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb139),
424 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
Brad Lovefd044de2018-10-05 11:19:49 -0400425 /* Hauppauge WinTV-HVR-935C - based on cx231xx */
426 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb151),
427 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
428 /* Hauppauge WinTV-HVR-955Q - based on cx231xx */
429 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb123),
430 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
431 /* Hauppauge WinTV-HVR-975 - based on cx231xx */
432 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb150),
433 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
Mauro Carvalho Chehab9683e012014-07-27 17:06:02 -0300434 { USB_DEVICE(VENDOR_PCTV, 0x0259),
435 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
436 { USB_DEVICE(VENDOR_PCTV, 0x025e),
437 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
Olli Salonene1866132016-03-17 10:11:09 -0300438 /* Adaptec / HP eHome Receiver */
439 { USB_DEVICE(VENDOR_ADAPTEC, 0x0094) },
Oleh Kravchenko47f42f32017-10-28 09:38:16 -0400440 /* Evromedia USB Full Hybrid Full HD */
441 { USB_DEVICE(0x1b80, 0xd3b2),
442 .driver_info = EVROMEDIA_FULL_HYBRID_FULLHD },
Oleh Kravchenko8ff19cd2017-10-28 09:38:18 -0400443 /* Astrometa T2hybrid */
444 { USB_DEVICE(0x15f4, 0x0135),
445 .driver_info = ASTROMETA_T2HYBRID },
Mauro Carvalho Chehab9683e012014-07-27 17:06:02 -0300446
Jarod Wilson66e89522010-06-01 17:32:08 -0300447 /* Terminating entry */
448 { }
449};
450
Jarod Wilson66e89522010-06-01 17:32:08 -0300451/* data structure for each usb transceiver */
452struct mceusb_dev {
453 /* ir-core bits */
David Härdemand8b4b582010-10-29 16:08:23 -0300454 struct rc_dev *rc;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300455
456 /* optional features we can enable */
A Sun279c60f2018-03-16 15:52:09 -0400457 bool carrier_report_enabled;
458 bool wideband_rx_enabled; /* aka learning mode, short-range rx */
Jarod Wilson66e89522010-06-01 17:32:08 -0300459
460 /* core device bits */
461 struct device *dev;
Jarod Wilson66e89522010-06-01 17:32:08 -0300462
463 /* usb */
464 struct usb_device *usbdev;
A Sun19d41a22019-07-14 22:51:26 -0400465 struct usb_interface *usbintf;
Jarod Wilson66e89522010-06-01 17:32:08 -0300466 struct urb *urb_in;
A Suna06854a2017-03-26 15:28:08 -0300467 unsigned int pipe_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300468 struct usb_endpoint_descriptor *usb_ep_out;
A Sunc779a9c2017-04-13 05:06:47 -0300469 unsigned int pipe_out;
Jarod Wilson66e89522010-06-01 17:32:08 -0300470
471 /* buffers and dma */
472 unsigned char *buf_in;
473 unsigned int len_in;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300474 dma_addr_t dma_in;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300475
476 enum {
477 CMD_HEADER = 0,
478 SUBCMD,
479 CMD_DATA,
480 PARSE_IRDATA,
481 } parser_state;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300482
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300483 u8 cmd, rem; /* Remaining IR data bytes in packet */
Jarod Wilson66e89522010-06-01 17:32:08 -0300484
485 struct {
486 u32 connected:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300487 u32 tx_mask_normal:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300488 u32 microsoft_gen1:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300489 u32 no_tx:1;
A Sun279c60f2018-03-16 15:52:09 -0400490 u32 rx2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300491 } flags;
492
Jarod Wilsone23fb962010-06-16 17:55:52 -0300493 /* transmit support */
Jarod Wilsone23fb962010-06-16 17:55:52 -0300494 u32 carrier;
495 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300496
497 char name[128];
498 char phys[64];
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300499 enum mceusb_model_type model;
Jarod Wilson4840b782011-07-18 16:54:24 -0300500
501 bool need_reset; /* flag to issue a device resume cmd */
Jarod Wilsonab1072e2011-07-18 16:54:25 -0300502 u8 emver; /* emulator interface version */
Jarod Wilsona411e832011-07-18 16:54:26 -0300503 u8 num_txports; /* number of transmit ports */
504 u8 num_rxports; /* number of receive sensors */
505 u8 txports_cabled; /* bitmask of transmitters with cable */
506 u8 rxports_active; /* bitmask of active receive sensors */
A Sun279c60f2018-03-16 15:52:09 -0400507 bool learning_active; /* wideband rx is active */
508
509 /* receiver carrier frequency detection support */
510 u32 pulse_tunit; /* IR pulse "on" cumulative time units */
511 u32 pulse_count; /* pulse "on" count in measurement interval */
A Suna06854a2017-03-26 15:28:08 -0300512
513 /*
514 * support for async error handler mceusb_deferred_kevent()
515 * where usb_clear_halt(), usb_reset_configuration(),
516 * usb_reset_device(), etc. must be done in process context
517 */
518 struct work_struct kevent;
519 unsigned long kevent_flags;
520# define EVENT_TX_HALT 0
521# define EVENT_RX_HALT 1
A Sun19d41a22019-07-14 22:51:26 -0400522# define EVENT_RST_PEND 31
Jarod Wilson66e89522010-06-01 17:32:08 -0300523};
524
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300525/* MCE Device Command Strings, generally a port and command pair */
526static char DEVICE_RESUME[] = {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
527 MCE_CMD_RESUME};
528static char GET_REVISION[] = {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
Jarod Wilsonab1072e2011-07-18 16:54:25 -0300529static char GET_EMVER[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300530static char GET_WAKEVERSION[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
Jarod Wilsonb71969b2011-07-18 16:54:27 -0300531static char FLASH_LED[] = {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300532static char GET_UNKNOWN2[] = {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
533static char GET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
534static char GET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
Jarod Wilsona411e832011-07-18 16:54:26 -0300535static char GET_NUM_PORTS[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300536static char GET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
537static char GET_RX_SENSOR[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
Jarod Wilson66e89522010-06-01 17:32:08 -0300538/* sub in desired values in lower byte or bytes for full command */
539/* FIXME: make use of these for transmit.
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300540static char SET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR,
541 MCE_CMD_SETIRCFS, 0x00, 0x00};
542static char SET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00};
543static char SET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR,
544 MCE_CMD_SETIRTIMEOUT, 0x00, 0x00};
545static char SET_RX_SENSOR[] = {MCE_CMD_PORT_IR,
546 MCE_RSP_EQIRRXPORTEN, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300547*/
548
William Steidtmann76dea4c2013-04-15 17:17:11 -0300549static int mceusb_cmd_datasize(u8 cmd, u8 subcmd)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300550{
551 int datasize = 0;
552
553 switch (cmd) {
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300554 case MCE_CMD_NULL:
555 if (subcmd == MCE_CMD_PORT_SYS)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300556 datasize = 1;
557 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300558 case MCE_CMD_PORT_SYS:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300559 switch (subcmd) {
William Steidtmann76dea4c2013-04-15 17:17:11 -0300560 case MCE_RSP_GETPORTSTATUS:
561 datasize = 5;
562 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300563 case MCE_RSP_EQWAKEVERSION:
564 datasize = 4;
565 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300566 case MCE_CMD_G_REVISION:
A Sune4314862019-09-06 09:17:20 -0300567 datasize = 4;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300568 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300569 case MCE_RSP_EQWAKESUPPORT:
William Steidtmann76dea4c2013-04-15 17:17:11 -0300570 case MCE_RSP_GETWAKESOURCE:
571 case MCE_RSP_EQDEVDETAILS:
572 case MCE_RSP_EQEMVER:
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300573 datasize = 1;
574 break;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300575 }
Sean Younge708e5a2018-05-10 07:49:49 -0400576 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300577 case MCE_CMD_PORT_IR:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300578 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300579 case MCE_CMD_UNKNOWN:
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300580 case MCE_RSP_EQIRCFS:
581 case MCE_RSP_EQIRTIMEOUT:
582 case MCE_RSP_EQIRRXCFCNT:
William Steidtmann76dea4c2013-04-15 17:17:11 -0300583 case MCE_RSP_EQIRNUMPORTS:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300584 datasize = 2;
585 break;
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300586 case MCE_CMD_SIG_END:
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300587 case MCE_RSP_EQIRTXPORTS:
588 case MCE_RSP_EQIRRXPORTEN:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300589 datasize = 1;
590 break;
591 }
592 }
593 return datasize;
594}
595
Sean Youngff05cf02017-08-04 05:30:20 -0400596static void mceusb_dev_printdata(struct mceusb_dev *ir, u8 *buf, int buf_len,
597 int offset, int len, bool out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300598{
Sean Young6b4a16c2014-01-20 19:10:44 -0300599#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
600 char *inout;
Sean Youngff05cf02017-08-04 05:30:20 -0400601 u8 cmd, subcmd, *data;
Jarod Wilson66e89522010-06-01 17:32:08 -0300602 struct device *dev = ir->dev;
Jarod Wilsone217fb42011-07-18 16:54:28 -0300603 u32 carrier, period;
Jarod Wilson36e9d262010-10-22 16:49:35 -0300604
A Sune4314862019-09-06 09:17:20 -0300605 if (offset < 0 || offset >= buf_len)
Jarod Wilson66e89522010-06-01 17:32:08 -0300606 return;
607
A Sun9fc3ce312019-08-15 13:41:19 -0300608 dev_dbg(dev, "%cx data[%d]: %*ph (len=%d sz=%d)",
609 (out ? 't' : 'r'), offset,
610 min(len, buf_len - offset), buf + offset, len, buf_len);
Jarod Wilson66e89522010-06-01 17:32:08 -0300611
Sean Young6b4a16c2014-01-20 19:10:44 -0300612 inout = out ? "Request" : "Got";
Jarod Wilson66e89522010-06-01 17:32:08 -0300613
A Sune4314862019-09-06 09:17:20 -0300614 cmd = buf[offset];
615 subcmd = (offset + 1 < buf_len) ? buf[offset + 1] : 0;
616 data = &buf[offset] + 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300617
A Sune4314862019-09-06 09:17:20 -0300618 /* Trace meaningless 0xb1 0x60 header bytes on original receiver */
619 if (ir->flags.microsoft_gen1 && !out && !offset) {
620 dev_dbg(dev, "MCE gen 1 header");
621 return;
622 }
623
624 /* Trace IR data header or trailer */
625 if (cmd != MCE_CMD_PORT_IR &&
626 (cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA) {
627 if (cmd == MCE_IRDATA_TRAILER)
628 dev_dbg(dev, "End of raw IR data");
629 else
630 dev_dbg(dev, "Raw IR data, %d pulse/space samples",
631 cmd & MCE_PACKET_LENGTH_MASK);
632 return;
633 }
634
635 /* Unexpected end of buffer? */
636 if (offset + len > buf_len)
637 return;
638
639 /* Decode MCE command/response */
Jarod Wilson66e89522010-06-01 17:32:08 -0300640 switch (cmd) {
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300641 case MCE_CMD_NULL:
Jarod Wilsona411e832011-07-18 16:54:26 -0300642 if (subcmd == MCE_CMD_NULL)
643 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300644 if ((subcmd == MCE_CMD_PORT_SYS) &&
Sean Youngff05cf02017-08-04 05:30:20 -0400645 (data[0] == MCE_CMD_RESUME))
Sean Young6b4a16c2014-01-20 19:10:44 -0300646 dev_dbg(dev, "Device resume requested");
Jarod Wilson66e89522010-06-01 17:32:08 -0300647 else
Sean Young6b4a16c2014-01-20 19:10:44 -0300648 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
Jarod Wilson66e89522010-06-01 17:32:08 -0300649 cmd, subcmd);
650 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300651 case MCE_CMD_PORT_SYS:
Jarod Wilson66e89522010-06-01 17:32:08 -0300652 switch (subcmd) {
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300653 case MCE_RSP_EQEMVER:
654 if (!out)
Sean Young6b4a16c2014-01-20 19:10:44 -0300655 dev_dbg(dev, "Emulator interface version %x",
Sean Youngff05cf02017-08-04 05:30:20 -0400656 data[0]);
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300657 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300658 case MCE_CMD_G_REVISION:
Jarod Wilson66e89522010-06-01 17:32:08 -0300659 if (len == 2)
Sean Young6b4a16c2014-01-20 19:10:44 -0300660 dev_dbg(dev, "Get hw/sw rev?");
Jarod Wilson66e89522010-06-01 17:32:08 -0300661 else
Mauro Carvalho Chehabeef8fc32016-03-06 10:00:09 -0300662 dev_dbg(dev, "hw/sw rev %*ph",
A Sune4314862019-09-06 09:17:20 -0300663 4, &buf[offset + 2]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300664 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300665 case MCE_CMD_RESUME:
Sean Young6b4a16c2014-01-20 19:10:44 -0300666 dev_dbg(dev, "Device resume requested");
Jarod Wilson66e89522010-06-01 17:32:08 -0300667 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300668 case MCE_RSP_CMD_ILLEGAL:
Sean Young6b4a16c2014-01-20 19:10:44 -0300669 dev_dbg(dev, "Illegal PORT_SYS command");
Jarod Wilson66e89522010-06-01 17:32:08 -0300670 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300671 case MCE_RSP_EQWAKEVERSION:
672 if (!out)
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200673 dev_dbg(dev, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
Sean Youngff05cf02017-08-04 05:30:20 -0400674 data[0], data[1], data[2], data[3]);
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300675 break;
676 case MCE_RSP_GETPORTSTATUS:
677 if (!out)
678 /* We use data1 + 1 here, to match hw labels */
Sean Young6b4a16c2014-01-20 19:10:44 -0300679 dev_dbg(dev, "TX port %d: blaster is%s connected",
Sean Youngff05cf02017-08-04 05:30:20 -0400680 data[0] + 1, data[3] ? " not" : "");
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300681 break;
Jarod Wilsonb71969b2011-07-18 16:54:27 -0300682 case MCE_CMD_FLASHLED:
Sean Young6b4a16c2014-01-20 19:10:44 -0300683 dev_dbg(dev, "Attempting to flash LED");
Jarod Wilsonb71969b2011-07-18 16:54:27 -0300684 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300685 default:
Sean Young6b4a16c2014-01-20 19:10:44 -0300686 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
Jarod Wilson66e89522010-06-01 17:32:08 -0300687 cmd, subcmd);
688 break;
689 }
690 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300691 case MCE_CMD_PORT_IR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300692 switch (subcmd) {
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300693 case MCE_CMD_SIG_END:
Sean Young6b4a16c2014-01-20 19:10:44 -0300694 dev_dbg(dev, "End of signal");
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300695 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300696 case MCE_CMD_PING:
Sean Young6b4a16c2014-01-20 19:10:44 -0300697 dev_dbg(dev, "Ping");
Jarod Wilson66e89522010-06-01 17:32:08 -0300698 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300699 case MCE_CMD_UNKNOWN:
Sean Young6b4a16c2014-01-20 19:10:44 -0300700 dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x",
Sean Youngff05cf02017-08-04 05:30:20 -0400701 data[0], data[1]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300702 break;
Jarod Wilsone217fb42011-07-18 16:54:28 -0300703 case MCE_RSP_EQIRCFS:
Sean Young9dec0f42021-01-19 14:53:50 +0100704 if (!data[0] && !data[1]) {
705 dev_dbg(dev, "%s: no carrier", inout);
706 break;
707 }
708 // prescaler should make sense
709 if (data[0] > 8)
710 break;
Sean Youngff05cf02017-08-04 05:30:20 -0400711 period = DIV_ROUND_CLOSEST((1U << data[0] * 2) *
712 (data[1] + 1), 10);
Jarod Wilsone217fb42011-07-18 16:54:28 -0300713 if (!period)
714 break;
Sean Young9dec0f42021-01-19 14:53:50 +0100715 carrier = USEC_PER_SEC / period;
Sean Young6b4a16c2014-01-20 19:10:44 -0300716 dev_dbg(dev, "%s carrier of %u Hz (period %uus)",
Jarod Wilsone217fb42011-07-18 16:54:28 -0300717 inout, carrier, period);
Jarod Wilson66e89522010-06-01 17:32:08 -0300718 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300719 case MCE_CMD_GETIRCFS:
Sean Young6b4a16c2014-01-20 19:10:44 -0300720 dev_dbg(dev, "Get carrier mode and freq");
Jarod Wilson66e89522010-06-01 17:32:08 -0300721 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300722 case MCE_RSP_EQIRTXPORTS:
Sean Young6b4a16c2014-01-20 19:10:44 -0300723 dev_dbg(dev, "%s transmit blaster mask of 0x%02x",
Sean Youngff05cf02017-08-04 05:30:20 -0400724 inout, data[0]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300725 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300726 case MCE_RSP_EQIRTIMEOUT:
Rafi Rubinf3e456c2011-07-03 17:13:52 -0300727 /* value is in units of 50us, so x*50/1000 ms */
Sean Youngff05cf02017-08-04 05:30:20 -0400728 period = ((data[0] << 8) | data[1]) *
729 MCE_TIME_UNIT / 1000;
Sean Young6b4a16c2014-01-20 19:10:44 -0300730 dev_dbg(dev, "%s receive timeout of %d ms",
Jarod Wilsone217fb42011-07-18 16:54:28 -0300731 inout, period);
Jarod Wilson66e89522010-06-01 17:32:08 -0300732 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300733 case MCE_CMD_GETIRTIMEOUT:
Sean Young6b4a16c2014-01-20 19:10:44 -0300734 dev_dbg(dev, "Get receive timeout");
Jarod Wilson66e89522010-06-01 17:32:08 -0300735 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300736 case MCE_CMD_GETIRTXPORTS:
Sean Young6b4a16c2014-01-20 19:10:44 -0300737 dev_dbg(dev, "Get transmit blaster mask");
Jarod Wilson66e89522010-06-01 17:32:08 -0300738 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300739 case MCE_RSP_EQIRRXPORTEN:
Sean Young6b4a16c2014-01-20 19:10:44 -0300740 dev_dbg(dev, "%s %s-range receive sensor in use",
Sean Youngff05cf02017-08-04 05:30:20 -0400741 inout, data[0] == 0x02 ? "short" : "long");
Jarod Wilson66e89522010-06-01 17:32:08 -0300742 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300743 case MCE_CMD_GETIRRXPORTEN:
744 /* aka MCE_RSP_EQIRRXCFCNT */
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300745 if (out)
Sean Young6b4a16c2014-01-20 19:10:44 -0300746 dev_dbg(dev, "Get receive sensor");
A Sun279c60f2018-03-16 15:52:09 -0400747 else
748 dev_dbg(dev, "RX carrier cycle count: %d",
Sean Youngff05cf02017-08-04 05:30:20 -0400749 ((data[0] << 8) | data[1]));
Jarod Wilson66e89522010-06-01 17:32:08 -0300750 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300751 case MCE_RSP_EQIRNUMPORTS:
752 if (out)
753 break;
Sean Young6b4a16c2014-01-20 19:10:44 -0300754 dev_dbg(dev, "Num TX ports: %x, num RX ports: %x",
Sean Youngff05cf02017-08-04 05:30:20 -0400755 data[0], data[1]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300756 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300757 case MCE_RSP_CMD_ILLEGAL:
Sean Young6b4a16c2014-01-20 19:10:44 -0300758 dev_dbg(dev, "Illegal PORT_IR command");
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -0300759 break;
A Sun9fc3ce312019-08-15 13:41:19 -0300760 case MCE_RSP_TX_TIMEOUT:
761 dev_dbg(dev, "IR TX timeout (TX buffer underrun)");
762 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300763 default:
Sean Young6b4a16c2014-01-20 19:10:44 -0300764 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
Jarod Wilson66e89522010-06-01 17:32:08 -0300765 cmd, subcmd);
766 break;
767 }
768 break;
769 default:
770 break;
771 }
Sean Young6b4a16c2014-01-20 19:10:44 -0300772#endif
Jarod Wilson66e89522010-06-01 17:32:08 -0300773}
774
A Suna06854a2017-03-26 15:28:08 -0300775/*
776 * Schedule work that can't be done in interrupt handlers
A Sun9fc3ce312019-08-15 13:41:19 -0300777 * (mceusb_dev_recv() and mce_write_callback()) nor tasklets.
A Suna06854a2017-03-26 15:28:08 -0300778 * Invokes mceusb_deferred_kevent() for recovering from
779 * error events specified by the kevent bit field.
780 */
781static void mceusb_defer_kevent(struct mceusb_dev *ir, int kevent)
782{
783 set_bit(kevent, &ir->kevent_flags);
A Sun19d41a22019-07-14 22:51:26 -0400784
785 if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) {
786 dev_dbg(ir->dev, "kevent %d dropped pending USB Reset Device",
787 kevent);
788 return;
789 }
790
A Suna06854a2017-03-26 15:28:08 -0300791 if (!schedule_work(&ir->kevent))
A Sun19d41a22019-07-14 22:51:26 -0400792 dev_dbg(ir->dev, "kevent %d already scheduled", kevent);
A Suna06854a2017-03-26 15:28:08 -0300793 else
794 dev_dbg(ir->dev, "kevent %d scheduled", kevent);
795}
796
A Sun9fc3ce312019-08-15 13:41:19 -0300797static void mce_write_callback(struct urb *urb)
Jarod Wilson66e89522010-06-01 17:32:08 -0300798{
Jarod Wilson66e89522010-06-01 17:32:08 -0300799 if (!urb)
800 return;
801
A Sun9fc3ce312019-08-15 13:41:19 -0300802 complete(urb->context);
803}
804
805/*
806 * Write (TX/send) data to MCE device USB endpoint out.
807 * Used for IR blaster TX and MCE device commands.
808 *
809 * Return: The number of bytes written (> 0) or errno (< 0).
810 */
811static int mce_write(struct mceusb_dev *ir, u8 *data, int size)
812{
813 int ret;
814 struct urb *urb;
815 struct device *dev = ir->dev;
816 unsigned char *buf_out;
817 struct completion tx_done;
818 unsigned long expire;
819 unsigned long ret_wait;
820
821 mceusb_dev_printdata(ir, data, size, 0, size, true);
822
823 urb = usb_alloc_urb(0, GFP_KERNEL);
824 if (unlikely(!urb)) {
825 dev_err(dev, "Error: mce write couldn't allocate urb");
826 return -ENOMEM;
827 }
828
829 buf_out = kmalloc(size, GFP_KERNEL);
830 if (!buf_out) {
831 usb_free_urb(urb);
832 return -ENOMEM;
833 }
834
835 init_completion(&tx_done);
836
837 /* outbound data */
838 if (usb_endpoint_xfer_int(ir->usb_ep_out))
839 usb_fill_int_urb(urb, ir->usbdev, ir->pipe_out,
840 buf_out, size, mce_write_callback, &tx_done,
841 ir->usb_ep_out->bInterval);
842 else
843 usb_fill_bulk_urb(urb, ir->usbdev, ir->pipe_out,
844 buf_out, size, mce_write_callback, &tx_done);
845 memcpy(buf_out, data, size);
846
847 ret = usb_submit_urb(urb, GFP_KERNEL);
848 if (ret) {
849 dev_err(dev, "Error: mce write submit urb error = %d", ret);
850 kfree(buf_out);
851 usb_free_urb(urb);
852 return ret;
853 }
854
855 expire = msecs_to_jiffies(USB_TX_TIMEOUT);
856 ret_wait = wait_for_completion_timeout(&tx_done, expire);
857 if (!ret_wait) {
858 dev_err(dev, "Error: mce write timed out (expire = %lu (%dms))",
859 expire, USB_TX_TIMEOUT);
860 usb_kill_urb(urb);
861 ret = (urb->status == -ENOENT ? -ETIMEDOUT : urb->status);
862 } else {
863 ret = urb->status;
864 }
865 if (ret >= 0)
866 ret = urb->actual_length; /* bytes written */
Sean Young6b4a16c2014-01-20 19:10:44 -0300867
868 switch (urb->status) {
869 /* success */
870 case 0:
Sean Young6b4a16c2014-01-20 19:10:44 -0300871 break;
872
873 case -ECONNRESET:
874 case -ENOENT:
875 case -EILSEQ:
876 case -ESHUTDOWN:
877 break;
878
879 case -EPIPE:
A Sun9fc3ce312019-08-15 13:41:19 -0300880 dev_err(ir->dev, "Error: mce write urb status = %d (TX HALT)",
A Sunc779a9c2017-04-13 05:06:47 -0300881 urb->status);
882 mceusb_defer_kevent(ir, EVENT_TX_HALT);
883 break;
884
Sean Young6b4a16c2014-01-20 19:10:44 -0300885 default:
A Sun9fc3ce312019-08-15 13:41:19 -0300886 dev_err(ir->dev, "Error: mce write urb status = %d",
887 urb->status);
Sean Young6b4a16c2014-01-20 19:10:44 -0300888 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300889 }
890
A Sun9fc3ce312019-08-15 13:41:19 -0300891 dev_dbg(dev, "tx done status = %d (wait = %lu, expire = %lu (%dms), urb->actual_length = %d, urb->status = %d)",
892 ret, ret_wait, expire, USB_TX_TIMEOUT,
893 urb->actual_length, urb->status);
894
895 kfree(buf_out);
Jarod Wilson0b43fcd2011-05-24 16:44:54 -0300896 usb_free_urb(urb);
A Sun9fc3ce312019-08-15 13:41:19 -0300897
898 return ret;
Jarod Wilson66e89522010-06-01 17:32:08 -0300899}
900
A Sun9fc3ce312019-08-15 13:41:19 -0300901static void mce_command_out(struct mceusb_dev *ir, u8 *data, int size)
Jarod Wilson66e89522010-06-01 17:32:08 -0300902{
Jarod Wilson4840b782011-07-18 16:54:24 -0300903 int rsize = sizeof(DEVICE_RESUME);
904
905 if (ir->need_reset) {
906 ir->need_reset = false;
A Sun9fc3ce312019-08-15 13:41:19 -0300907 mce_write(ir, DEVICE_RESUME, rsize);
Jarod Wilson4840b782011-07-18 16:54:24 -0300908 msleep(10);
909 }
910
A Sun9fc3ce312019-08-15 13:41:19 -0300911 mce_write(ir, data, size);
Jarod Wilson417c0a22011-07-18 16:54:22 -0300912 msleep(10);
Jarod Wilson66e89522010-06-01 17:32:08 -0300913}
914
A Sun9fc3ce312019-08-15 13:41:19 -0300915/*
916 * Transmit IR out the MCE device IR blaster port(s).
917 *
918 * Convert IR pulse/space sequence from LIRC to MCE format.
919 * Break up a long IR sequence into multiple parts (MCE IR data packets).
920 *
921 * u32 txbuf[] consists of IR pulse, space, ..., and pulse times in usec.
922 * Pulses and spaces are implicit by their position.
923 * The first IR sample, txbuf[0], is always a pulse.
924 *
925 * u8 irbuf[] consists of multiple IR data packets for the MCE device.
926 * A packet is 1 u8 MCE_IRDATA_HEADER and up to 30 u8 IR samples.
927 * An IR sample is 1-bit pulse/space flag with 7-bit time
928 * in MCE time units (50usec).
929 *
930 * Return: The number of IR samples sent (> 0) or errno (< 0).
931 */
David Härdeman5588dc22011-04-28 12:13:58 -0300932static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
Jarod Wilsone23fb962010-06-16 17:55:52 -0300933{
David Härdemand8b4b582010-10-29 16:08:23 -0300934 struct mceusb_dev *ir = dev->priv;
A Sun9fc3ce312019-08-15 13:41:19 -0300935 u8 cmdbuf[3] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00 };
936 u8 irbuf[MCE_IRBUF_SIZE];
937 int ircount = 0;
938 unsigned int irsample;
939 int i, length, ret;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300940
Sean Youngdb8ee102013-01-29 08:19:30 -0300941 /* Send the set TX ports command */
A Sun9fc3ce312019-08-15 13:41:19 -0300942 cmdbuf[2] = ir->tx_mask;
943 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
Sean Youngdb8ee102013-01-29 08:19:30 -0300944
A Sun9fc3ce312019-08-15 13:41:19 -0300945 /* Generate mce IR data packet */
946 for (i = 0; i < count; i++) {
947 irsample = txbuf[i] / MCE_TIME_UNIT;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300948
A Sun9fc3ce312019-08-15 13:41:19 -0300949 /* loop to support long pulses/spaces > 6350us (127*50us) */
950 while (irsample > 0) {
951 /* Insert IR header every 30th entry */
952 if (ircount % MCE_PACKET_SIZE == 0) {
953 /* Room for IR header and one IR sample? */
954 if (ircount >= MCE_IRBUF_SIZE - 1) {
955 /* Send near full buffer */
956 ret = mce_write(ir, irbuf, ircount);
957 if (ret < 0)
958 return ret;
959 ircount = 0;
960 }
961 irbuf[ircount++] = MCE_IRDATA_HEADER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300962 }
963
A Sun9fc3ce312019-08-15 13:41:19 -0300964 /* Insert IR sample */
965 if (irsample <= MCE_MAX_PULSE_LENGTH) {
966 irbuf[ircount] = irsample;
967 irsample = 0;
968 } else {
969 irbuf[ircount] = MCE_MAX_PULSE_LENGTH;
970 irsample -= MCE_MAX_PULSE_LENGTH;
971 }
972 /*
973 * Even i = IR pulse
974 * Odd i = IR space
975 */
976 irbuf[ircount] |= (i & 1 ? 0 : MCE_PULSE_BIT);
977 ircount++;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300978
A Sun9fc3ce312019-08-15 13:41:19 -0300979 /* IR buffer full? */
980 if (ircount >= MCE_IRBUF_SIZE) {
981 /* Fix packet length in last header */
982 length = ircount % MCE_PACKET_SIZE;
983 if (length > 0)
984 irbuf[ircount - length] -=
985 MCE_PACKET_SIZE - length;
986 /* Send full buffer */
987 ret = mce_write(ir, irbuf, ircount);
988 if (ret < 0)
989 return ret;
990 ircount = 0;
991 }
992 }
993 } /* after for loop, 0 <= ircount < MCE_IRBUF_SIZE */
Jarod Wilsone23fb962010-06-16 17:55:52 -0300994
Dan Carpenterb940a222013-02-12 08:22:08 -0300995 /* Fix packet length in last header */
A Sun9fc3ce312019-08-15 13:41:19 -0300996 length = ircount % MCE_PACKET_SIZE;
997 if (length > 0)
998 irbuf[ircount - length] -= MCE_PACKET_SIZE - length;
Dan Carpenterb940a222013-02-12 08:22:08 -0300999
A Sun9fc3ce312019-08-15 13:41:19 -03001000 /* Append IR trailer (0x80) to final partial (or empty) IR buffer */
1001 irbuf[ircount++] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001002
A Sun9fc3ce312019-08-15 13:41:19 -03001003 /* Send final buffer */
1004 ret = mce_write(ir, irbuf, ircount);
1005 if (ret < 0)
1006 return ret;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001007
A Sun9fc3ce312019-08-15 13:41:19 -03001008 return count;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001009}
1010
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001011/* Sets active IR outputs -- mce devices typically have two */
David Härdemand8b4b582010-10-29 16:08:23 -03001012static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
Jarod Wilson657290b2010-06-16 17:10:05 -03001013{
David Härdemand8b4b582010-10-29 16:08:23 -03001014 struct mceusb_dev *ir = dev->priv;
Jarod Wilson657290b2010-06-16 17:10:05 -03001015
Sean Young20f5a822016-07-10 13:34:32 -03001016 /* return number of transmitters */
1017 int emitters = ir->num_txports ? ir->num_txports : 2;
1018
1019 if (mask >= (1 << emitters))
1020 return emitters;
1021
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001022 if (ir->flags.tx_mask_normal)
1023 ir->tx_mask = mask;
1024 else
Jarod Wilson4a883912010-10-22 14:42:54 -03001025 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
1026 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001027
1028 return 0;
1029}
1030
Jarod Wilsone23fb962010-06-16 17:55:52 -03001031/* Sets the send carrier frequency and mode */
David Härdemand8b4b582010-10-29 16:08:23 -03001032static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
Jarod Wilsone23fb962010-06-16 17:55:52 -03001033{
David Härdemand8b4b582010-10-29 16:08:23 -03001034 struct mceusb_dev *ir = dev->priv;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001035 int clk = 10000000;
1036 int prescaler = 0, divisor = 0;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001037 unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
1038 MCE_CMD_SETIRCFS, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -03001039
1040 /* Carrier has changed */
1041 if (ir->carrier != carrier) {
1042
1043 if (carrier == 0) {
1044 ir->carrier = carrier;
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001045 cmdbuf[2] = MCE_CMD_SIG_END;
Jarod Wilson4a883912010-10-22 14:42:54 -03001046 cmdbuf[3] = MCE_IRDATA_TRAILER;
Sean Young6b4a16c2014-01-20 19:10:44 -03001047 dev_dbg(ir->dev, "disabling carrier modulation");
A Sun9fc3ce312019-08-15 13:41:19 -03001048 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
Sean Young3cf8d8e2016-12-02 15:16:07 -02001049 return 0;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001050 }
1051
1052 for (prescaler = 0; prescaler < 4; ++prescaler) {
1053 divisor = (clk >> (2 * prescaler)) / carrier;
Jarod Wilson4a883912010-10-22 14:42:54 -03001054 if (divisor <= 0xff) {
Jarod Wilsone23fb962010-06-16 17:55:52 -03001055 ir->carrier = carrier;
1056 cmdbuf[2] = prescaler;
1057 cmdbuf[3] = divisor;
Sean Young6b4a16c2014-01-20 19:10:44 -03001058 dev_dbg(ir->dev, "requesting %u HZ carrier",
1059 carrier);
Jarod Wilsone23fb962010-06-16 17:55:52 -03001060
1061 /* Transmit new carrier to mce device */
A Sun9fc3ce312019-08-15 13:41:19 -03001062 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
Sean Young3cf8d8e2016-12-02 15:16:07 -02001063 return 0;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001064 }
1065 }
1066
1067 return -EINVAL;
1068
1069 }
1070
Sean Young14d81882016-07-10 13:34:33 -03001071 return 0;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001072}
1073
Sean Young877f1a72018-04-08 11:06:49 -04001074static int mceusb_set_timeout(struct rc_dev *dev, unsigned int timeout)
1075{
1076 u8 cmdbuf[4] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTIMEOUT, 0, 0 };
1077 struct mceusb_dev *ir = dev->priv;
1078 unsigned int units;
1079
Sean Young528222d82020-08-23 19:23:05 +02001080 units = DIV_ROUND_CLOSEST(timeout, MCE_TIME_UNIT);
Sean Young877f1a72018-04-08 11:06:49 -04001081
1082 cmdbuf[2] = units >> 8;
1083 cmdbuf[3] = units;
1084
A Sun9fc3ce312019-08-15 13:41:19 -03001085 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
Sean Young877f1a72018-04-08 11:06:49 -04001086
1087 /* get receiver timeout value */
A Sun9fc3ce312019-08-15 13:41:19 -03001088 mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
Sean Young877f1a72018-04-08 11:06:49 -04001089
1090 return 0;
1091}
1092
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001093/*
A Sun279c60f2018-03-16 15:52:09 -04001094 * Select or deselect the 2nd receiver port.
1095 * Second receiver is learning mode, wide-band, short-range receiver.
1096 * Only one receiver (long or short range) may be active at a time.
1097 */
1098static int mceusb_set_rx_wideband(struct rc_dev *dev, int enable)
1099{
1100 struct mceusb_dev *ir = dev->priv;
1101 unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
1102 MCE_CMD_SETIRRXPORTEN, 0x00 };
1103
1104 dev_dbg(ir->dev, "select %s-range receive sensor",
1105 enable ? "short" : "long");
1106 if (enable) {
1107 ir->wideband_rx_enabled = true;
1108 cmdbuf[2] = 2; /* port 2 is short range receiver */
1109 } else {
1110 ir->wideband_rx_enabled = false;
1111 cmdbuf[2] = 1; /* port 1 is long range receiver */
1112 }
A Sun9fc3ce312019-08-15 13:41:19 -03001113 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
A Sun279c60f2018-03-16 15:52:09 -04001114 /* response from device sets ir->learning_active */
1115
1116 return 0;
1117}
1118
1119/*
1120 * Enable/disable receiver carrier frequency pass through reporting.
1121 * Only the short-range receiver has carrier frequency measuring capability.
1122 * Implicitly select this receiver when enabling carrier frequency reporting.
1123 */
1124static int mceusb_set_rx_carrier_report(struct rc_dev *dev, int enable)
1125{
1126 struct mceusb_dev *ir = dev->priv;
1127 unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
1128 MCE_CMD_SETIRRXPORTEN, 0x00 };
1129
1130 dev_dbg(ir->dev, "%s short-range receiver carrier reporting",
1131 enable ? "enable" : "disable");
1132 if (enable) {
1133 ir->carrier_report_enabled = true;
1134 if (!ir->learning_active) {
1135 cmdbuf[2] = 2; /* port 2 is short range receiver */
A Sun9fc3ce312019-08-15 13:41:19 -03001136 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
A Sun279c60f2018-03-16 15:52:09 -04001137 }
1138 } else {
1139 ir->carrier_report_enabled = false;
1140 /*
1141 * Revert to normal (long-range) receiver only if the
1142 * wideband (short-range) receiver wasn't explicitly
1143 * enabled.
1144 */
1145 if (ir->learning_active && !ir->wideband_rx_enabled) {
1146 cmdbuf[2] = 1; /* port 1 is long range receiver */
A Sun9fc3ce312019-08-15 13:41:19 -03001147 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
A Sun279c60f2018-03-16 15:52:09 -04001148 }
1149 }
1150
1151 return 0;
1152}
1153
1154/*
A Sune4314862019-09-06 09:17:20 -03001155 * Handle PORT_SYS/IR command response received from the MCE device.
1156 *
1157 * Assumes single response with all its data (not truncated)
1158 * in buf_in[]. The response itself determines its total length
1159 * (mceusb_cmd_datasize() + 2) and hence the minimum size of buf_in[].
1160 *
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001161 * We don't do anything but print debug spew for many of the command bits
1162 * we receive from the hardware, but some of them are useful information
1163 * we want to store so that we can use them.
1164 */
A Sune4314862019-09-06 09:17:20 -03001165static void mceusb_handle_command(struct mceusb_dev *ir, u8 *buf_in)
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001166{
A Sune4314862019-09-06 09:17:20 -03001167 u8 cmd = buf_in[0];
1168 u8 subcmd = buf_in[1];
1169 u8 *hi = &buf_in[2]; /* read only when required */
1170 u8 *lo = &buf_in[3]; /* read only when required */
Sean Young183e19f2018-08-21 15:57:52 -04001171 struct ir_raw_event rawir = {};
A Sun279c60f2018-03-16 15:52:09 -04001172 u32 carrier_cycles;
1173 u32 cycles_fix;
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001174
A Sune4314862019-09-06 09:17:20 -03001175 if (cmd == MCE_CMD_PORT_SYS) {
1176 switch (subcmd) {
1177 /* the one and only 5-byte return value command */
1178 case MCE_RSP_GETPORTSTATUS:
James Reynolds1b43bad2020-12-22 13:07:04 +01001179 if (buf_in[5] == 0 && *hi < 8)
A Sune4314862019-09-06 09:17:20 -03001180 ir->txports_cabled |= 1 << *hi;
1181 break;
Jarod Wilsona411e832011-07-18 16:54:26 -03001182
A Sune4314862019-09-06 09:17:20 -03001183 /* 1-byte return value commands */
1184 case MCE_RSP_EQEMVER:
1185 ir->emver = *hi;
1186 break;
1187
1188 /* No return value commands */
1189 case MCE_RSP_CMD_ILLEGAL:
1190 ir->need_reset = true;
1191 break;
1192
1193 default:
1194 break;
1195 }
1196
1197 return;
1198 }
1199
1200 if (cmd != MCE_CMD_PORT_IR)
1201 return;
1202
1203 switch (subcmd) {
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001204 /* 2-byte return value commands */
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001205 case MCE_RSP_EQIRTIMEOUT:
Sean Young528222d82020-08-23 19:23:05 +02001206 ir->rc->timeout = (*hi << 8 | *lo) * MCE_TIME_UNIT;
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001207 break;
Jarod Wilsona411e832011-07-18 16:54:26 -03001208 case MCE_RSP_EQIRNUMPORTS:
A Sune4314862019-09-06 09:17:20 -03001209 ir->num_txports = *hi;
1210 ir->num_rxports = *lo;
Jarod Wilsona411e832011-07-18 16:54:26 -03001211 break;
A Sun279c60f2018-03-16 15:52:09 -04001212 case MCE_RSP_EQIRRXCFCNT:
1213 /*
1214 * The carrier cycle counter can overflow and wrap around
1215 * without notice from the device. So frequency measurement
1216 * will be inaccurate with long duration IR.
1217 *
1218 * The long-range (non learning) receiver always reports
1219 * zero count so we always ignore its report.
1220 */
1221 if (ir->carrier_report_enabled && ir->learning_active &&
1222 ir->pulse_tunit > 0) {
A Sune4314862019-09-06 09:17:20 -03001223 carrier_cycles = (*hi << 8 | *lo);
A Sun279c60f2018-03-16 15:52:09 -04001224 /*
1225 * Adjust carrier cycle count by adding
1226 * 1 missed count per pulse "on"
1227 */
1228 cycles_fix = ir->flags.rx2 == 2 ? ir->pulse_count : 0;
1229 rawir.carrier_report = 1;
1230 rawir.carrier = (1000000u / MCE_TIME_UNIT) *
1231 (carrier_cycles + cycles_fix) /
1232 ir->pulse_tunit;
1233 dev_dbg(ir->dev, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)",
1234 rawir.carrier, ir->pulse_count, carrier_cycles,
1235 ir->pulse_tunit, ir->flags.rx2);
1236 ir_raw_event_store(ir->rc, &rawir);
1237 }
1238 break;
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001239
1240 /* 1-byte return value commands */
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001241 case MCE_RSP_EQIRTXPORTS:
A Sune4314862019-09-06 09:17:20 -03001242 ir->tx_mask = *hi;
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001243 break;
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001244 case MCE_RSP_EQIRRXPORTEN:
A Sune4314862019-09-06 09:17:20 -03001245 ir->learning_active = ((*hi & 0x02) == 0x02);
1246 if (ir->rxports_active != *hi) {
A Sun279c60f2018-03-16 15:52:09 -04001247 dev_info(ir->dev, "%s-range (0x%x) receiver active",
A Sune4314862019-09-06 09:17:20 -03001248 ir->learning_active ? "short" : "long", *hi);
1249 ir->rxports_active = *hi;
A Sun279c60f2018-03-16 15:52:09 -04001250 }
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001251 break;
A Sune4314862019-09-06 09:17:20 -03001252
1253 /* No return value commands */
Jarod Wilson4840b782011-07-18 16:54:24 -03001254 case MCE_RSP_CMD_ILLEGAL:
A Sun9fc3ce312019-08-15 13:41:19 -03001255 case MCE_RSP_TX_TIMEOUT:
Jarod Wilson4840b782011-07-18 16:54:24 -03001256 ir->need_reset = true;
1257 break;
A Sune4314862019-09-06 09:17:20 -03001258
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001259 default:
1260 break;
1261 }
1262}
1263
Jarod Wilson66e89522010-06-01 17:32:08 -03001264static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
1265{
Sean Young183e19f2018-08-21 15:57:52 -04001266 struct ir_raw_event rawir = {};
Sean Youngb83bfd12012-08-13 08:59:47 -03001267 bool event = false;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001268 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -03001269
1270 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
1271 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001272 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -03001273
Jarod Wilson29b44942010-11-09 18:41:46 -03001274 /* if there's no data, just return now */
1275 if (buf_len <= i)
1276 return;
1277
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001278 for (; i < buf_len; i++) {
1279 switch (ir->parser_state) {
1280 case SUBCMD:
William Steidtmann76dea4c2013-04-15 17:17:11 -03001281 ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]);
A Sun5c809922017-03-26 16:04:51 -03001282 mceusb_dev_printdata(ir, ir->buf_in, buf_len, i - 1,
Jarod Wilson36e9d262010-10-22 16:49:35 -03001283 ir->rem + 2, false);
A Sune4314862019-09-06 09:17:20 -03001284 if (i + ir->rem < buf_len)
1285 mceusb_handle_command(ir, &ir->buf_in[i - 1]);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001286 ir->parser_state = CMD_DATA;
1287 break;
1288 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -03001289 ir->rem--;
Jarod Wilson66e89522010-06-01 17:32:08 -03001290 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
A Sun279c60f2018-03-16 15:52:09 -04001291 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK);
Sean Youngd4589932018-05-10 07:37:51 -04001292 if (unlikely(!rawir.duration)) {
A Suna91418a2019-06-19 03:53:53 -04001293 dev_dbg(ir->dev, "nonsensical irdata %02x with duration 0",
1294 ir->buf_in[i]);
Sean Youngd4589932018-05-10 07:37:51 -04001295 break;
1296 }
A Sun279c60f2018-03-16 15:52:09 -04001297 if (rawir.pulse) {
1298 ir->pulse_tunit += rawir.duration;
1299 ir->pulse_count++;
1300 }
Sean Young528222d82020-08-23 19:23:05 +02001301 rawir.duration *= MCE_TIME_UNIT;
Jarod Wilson66e89522010-06-01 17:32:08 -03001302
Sean Young528222d82020-08-23 19:23:05 +02001303 dev_dbg(ir->dev, "Storing %s %u us (%02x)",
Jarod Wilson66e89522010-06-01 17:32:08 -03001304 rawir.pulse ? "pulse" : "space",
A Sun279c60f2018-03-16 15:52:09 -04001305 rawir.duration, ir->buf_in[i]);
Jarod Wilson66e89522010-06-01 17:32:08 -03001306
Sean Youngb83bfd12012-08-13 08:59:47 -03001307 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
1308 event = true;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001309 break;
1310 case CMD_DATA:
1311 ir->rem--;
1312 break;
1313 case CMD_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001314 ir->cmd = ir->buf_in[i];
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001315 if ((ir->cmd == MCE_CMD_PORT_IR) ||
1316 ((ir->cmd & MCE_PORT_MASK) !=
Jarod Wilson4a883912010-10-22 14:42:54 -03001317 MCE_COMMAND_IRDATA)) {
A Sune4314862019-09-06 09:17:20 -03001318 /*
1319 * got PORT_SYS, PORT_IR, or unknown
1320 * command response prefix
1321 */
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001322 ir->parser_state = SUBCMD;
1323 continue;
1324 }
A Sune4314862019-09-06 09:17:20 -03001325 /*
1326 * got IR data prefix (0x80 + num_bytes)
1327 * decode MCE packets of the form {0x83, AA, BB, CC}
1328 * IR data packets can span USB messages
1329 */
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001330 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
A Sun5c809922017-03-26 16:04:51 -03001331 mceusb_dev_printdata(ir, ir->buf_in, buf_len,
Jarod Wilson2ee95db2010-11-12 19:49:04 -03001332 i, ir->rem + 1, false);
A Sun279c60f2018-03-16 15:52:09 -04001333 if (ir->rem) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001334 ir->parser_state = PARSE_IRDATA;
A Sun279c60f2018-03-16 15:52:09 -04001335 } else {
Sean Young183e19f2018-08-21 15:57:52 -04001336 struct ir_raw_event ev = {
1337 .timeout = 1,
1338 .duration = ir->rc->timeout
1339 };
1340
Sean Young91352b52018-04-18 05:36:25 -04001341 if (ir_raw_event_store_with_filter(ir->rc,
Sean Young183e19f2018-08-21 15:57:52 -04001342 &ev))
Sean Young91352b52018-04-18 05:36:25 -04001343 event = true;
A Sun279c60f2018-03-16 15:52:09 -04001344 ir->pulse_tunit = 0;
1345 ir->pulse_count = 0;
1346 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001347 break;
Jarod Wilson66e89522010-06-01 17:32:08 -03001348 }
1349
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -03001350 if (ir->parser_state != CMD_HEADER && !ir->rem)
1351 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -03001352 }
A Sune4314862019-09-06 09:17:20 -03001353
1354 /*
1355 * Accept IR data spanning multiple rx buffers.
1356 * Reject MCE command response spanning multiple rx buffers.
1357 */
1358 if (ir->parser_state != PARSE_IRDATA || !ir->rem)
1359 ir->parser_state = CMD_HEADER;
1360
Sean Youngb83bfd12012-08-13 08:59:47 -03001361 if (event) {
Sean Young6b4a16c2014-01-20 19:10:44 -03001362 dev_dbg(ir->dev, "processed IR data");
Sean Youngb83bfd12012-08-13 08:59:47 -03001363 ir_raw_event_handle(ir->rc);
1364 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001365}
1366
Sean Young6ac454a2012-07-15 13:31:31 -03001367static void mceusb_dev_recv(struct urb *urb)
Jarod Wilson66e89522010-06-01 17:32:08 -03001368{
1369 struct mceusb_dev *ir;
Jarod Wilson66e89522010-06-01 17:32:08 -03001370
1371 if (!urb)
1372 return;
1373
1374 ir = urb->context;
1375 if (!ir) {
1376 usb_unlink_urb(urb);
1377 return;
1378 }
1379
Jarod Wilson66e89522010-06-01 17:32:08 -03001380 switch (urb->status) {
1381 /* success */
1382 case 0:
Sean Youngd26cec22016-04-14 17:42:49 -03001383 mceusb_process_ir_data(ir, urb->actual_length);
Jarod Wilson66e89522010-06-01 17:32:08 -03001384 break;
1385
1386 case -ECONNRESET:
1387 case -ENOENT:
Sean Young6b4a16c2014-01-20 19:10:44 -03001388 case -EILSEQ:
Rajat Asthana476db722021-08-18 22:31:10 +02001389 case -EPROTO:
Jarod Wilson66e89522010-06-01 17:32:08 -03001390 case -ESHUTDOWN:
1391 usb_unlink_urb(urb);
1392 return;
1393
1394 case -EPIPE:
A Suna06854a2017-03-26 15:28:08 -03001395 dev_err(ir->dev, "Error: urb status = %d (RX HALT)",
1396 urb->status);
1397 mceusb_defer_kevent(ir, EVENT_RX_HALT);
1398 return;
1399
Jarod Wilson66e89522010-06-01 17:32:08 -03001400 default:
Sean Young6b4a16c2014-01-20 19:10:44 -03001401 dev_err(ir->dev, "Error: urb status = %d", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -03001402 break;
1403 }
1404
1405 usb_submit_urb(urb, GFP_ATOMIC);
1406}
1407
Jarod Wilsonab1072e2011-07-18 16:54:25 -03001408static void mceusb_get_emulator_version(struct mceusb_dev *ir)
1409{
1410 /* If we get no reply or an illegal command reply, its ver 1, says MS */
1411 ir->emver = 1;
A Sun9fc3ce312019-08-15 13:41:19 -03001412 mce_command_out(ir, GET_EMVER, sizeof(GET_EMVER));
Jarod Wilsonab1072e2011-07-18 16:54:25 -03001413}
1414
Jarod Wilson66e89522010-06-01 17:32:08 -03001415static void mceusb_gen1_init(struct mceusb_dev *ir)
1416{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -03001417 int ret;
Jarod Wilson66e89522010-06-01 17:32:08 -03001418 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001419 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -03001420
1421 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
1422 if (!data) {
Sean Young6b4a16c2014-01-20 19:10:44 -03001423 dev_err(dev, "%s: memory allocation failed!", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -03001424 return;
1425 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001426
1427 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001428 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -03001429 * on the receive control pipe and expect a certain value pair back
1430 */
Jarod Wilson66e89522010-06-01 17:32:08 -03001431 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
1432 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Johan Hovold16394e92021-10-25 13:16:34 +01001433 data, USB_CTRL_MSG_SZ, 3000);
Sean Young6b4a16c2014-01-20 19:10:44 -03001434 dev_dbg(dev, "set address - ret = %d", ret);
1435 dev_dbg(dev, "set address - data[0] = %d, data[1] = %d",
1436 data[0], data[1]);
Jarod Wilson66e89522010-06-01 17:32:08 -03001437
1438 /* set feature: bit rate 38400 bps */
1439 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1440 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
Johan Hovold16394e92021-10-25 13:16:34 +01001441 0xc04e, 0x0000, NULL, 0, 3000);
Jarod Wilson66e89522010-06-01 17:32:08 -03001442
Sean Young6b4a16c2014-01-20 19:10:44 -03001443 dev_dbg(dev, "set feature - ret = %d", ret);
Jarod Wilson66e89522010-06-01 17:32:08 -03001444
1445 /* bRequest 4: set char length to 8 bits */
1446 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1447 4, USB_TYPE_VENDOR,
Johan Hovold16394e92021-10-25 13:16:34 +01001448 0x0808, 0x0000, NULL, 0, 3000);
Sean Young6b4a16c2014-01-20 19:10:44 -03001449 dev_dbg(dev, "set char length - retB = %d", ret);
Jarod Wilson66e89522010-06-01 17:32:08 -03001450
1451 /* bRequest 2: set handshaking to use DTR/DSR */
1452 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1453 2, USB_TYPE_VENDOR,
Johan Hovold16394e92021-10-25 13:16:34 +01001454 0x0000, 0x0100, NULL, 0, 3000);
Sean Young6b4a16c2014-01-20 19:10:44 -03001455 dev_dbg(dev, "set handshake - retC = %d", ret);
Jarod Wilson657290b2010-06-16 17:10:05 -03001456
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001457 /* device resume */
A Sun9fc3ce312019-08-15 13:41:19 -03001458 mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
Jarod Wilson22b07662010-07-08 12:38:57 -03001459
1460 /* get hw/sw revision? */
A Sun9fc3ce312019-08-15 13:41:19 -03001461 mce_command_out(ir, GET_REVISION, sizeof(GET_REVISION));
Jarod Wilson22b07662010-07-08 12:38:57 -03001462
Jarod Wilson657290b2010-06-16 17:10:05 -03001463 kfree(data);
Sean Young8dfef672013-01-29 08:19:29 -03001464}
Jarod Wilson66e89522010-06-01 17:32:08 -03001465
1466static void mceusb_gen2_init(struct mceusb_dev *ir)
1467{
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001468 /* device resume */
A Sun9fc3ce312019-08-15 13:41:19 -03001469 mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
Jarod Wilson66e89522010-06-01 17:32:08 -03001470
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001471 /* get wake version (protocol, key, address) */
A Sun9fc3ce312019-08-15 13:41:19 -03001472 mce_command_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));
Jarod Wilsona6fbd3b2011-07-18 16:54:21 -03001473
1474 /* unknown what this one actually returns... */
A Sun9fc3ce312019-08-15 13:41:19 -03001475 mce_command_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
Jarod Wilson66e89522010-06-01 17:32:08 -03001476}
1477
Jarod Wilson22b07662010-07-08 12:38:57 -03001478static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -03001479{
Jarod Wilsona411e832011-07-18 16:54:26 -03001480 int i;
1481 unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
1482 MCE_CMD_GETPORTSTATUS, 0x00 };
1483
1484 /* defaults, if the hardware doesn't support querying */
1485 ir->num_txports = 2;
1486 ir->num_rxports = 2;
1487
1488 /* get number of tx and rx ports */
A Sun9fc3ce312019-08-15 13:41:19 -03001489 mce_command_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));
Jarod Wilsona411e832011-07-18 16:54:26 -03001490
Jarod Wilson66e89522010-06-01 17:32:08 -03001491 /* get the carrier and frequency */
A Sun9fc3ce312019-08-15 13:41:19 -03001492 mce_command_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
Jarod Wilson66e89522010-06-01 17:32:08 -03001493
Jarod Wilsona411e832011-07-18 16:54:26 -03001494 if (ir->num_txports && !ir->flags.no_tx)
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001495 /* get the transmitter bitmask */
A Sun9fc3ce312019-08-15 13:41:19 -03001496 mce_command_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
Jarod Wilson66e89522010-06-01 17:32:08 -03001497
1498 /* get receiver timeout value */
A Sun9fc3ce312019-08-15 13:41:19 -03001499 mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
Jarod Wilson66e89522010-06-01 17:32:08 -03001500
1501 /* get receiver sensor setting */
A Sun9fc3ce312019-08-15 13:41:19 -03001502 mce_command_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
Jarod Wilsona411e832011-07-18 16:54:26 -03001503
1504 for (i = 0; i < ir->num_txports; i++) {
1505 cmdbuf[2] = i;
A Sun9fc3ce312019-08-15 13:41:19 -03001506 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
Jarod Wilsona411e832011-07-18 16:54:26 -03001507 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001508}
1509
Jarod Wilsonb71969b2011-07-18 16:54:27 -03001510static void mceusb_flash_led(struct mceusb_dev *ir)
1511{
1512 if (ir->emver < 2)
1513 return;
1514
A Sun9fc3ce312019-08-15 13:41:19 -03001515 mce_command_out(ir, FLASH_LED, sizeof(FLASH_LED));
Jarod Wilsonb71969b2011-07-18 16:54:27 -03001516}
1517
A Suna06854a2017-03-26 15:28:08 -03001518/*
1519 * Workqueue function
1520 * for resetting or recovering device after occurrence of error events
1521 * specified in ir->kevent bit field.
1522 * Function runs (via schedule_work()) in non-interrupt context, for
1523 * calls here (such as usb_clear_halt()) requiring non-interrupt context.
1524 */
1525static void mceusb_deferred_kevent(struct work_struct *work)
1526{
1527 struct mceusb_dev *ir =
1528 container_of(work, struct mceusb_dev, kevent);
1529 int status;
1530
A Sun19d41a22019-07-14 22:51:26 -04001531 dev_err(ir->dev, "kevent handler called (flags 0x%lx)",
1532 ir->kevent_flags);
1533
1534 if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) {
1535 dev_err(ir->dev, "kevent handler canceled pending USB Reset Device");
1536 return;
1537 }
1538
A Suna06854a2017-03-26 15:28:08 -03001539 if (test_bit(EVENT_RX_HALT, &ir->kevent_flags)) {
1540 usb_unlink_urb(ir->urb_in);
1541 status = usb_clear_halt(ir->usbdev, ir->pipe_in);
A Sun19d41a22019-07-14 22:51:26 -04001542 dev_err(ir->dev, "rx clear halt status = %d", status);
A Suna06854a2017-03-26 15:28:08 -03001543 if (status < 0) {
A Sun19d41a22019-07-14 22:51:26 -04001544 /*
1545 * Unable to clear RX halt/stall.
1546 * Will need to call usb_reset_device().
1547 */
1548 dev_err(ir->dev,
1549 "stuck RX HALT state requires USB Reset Device to clear");
1550 usb_queue_reset_device(ir->usbintf);
1551 set_bit(EVENT_RST_PEND, &ir->kevent_flags);
1552 clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
1553
1554 /* Cancel all other error events and handlers */
1555 clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
1556 return;
A Suna06854a2017-03-26 15:28:08 -03001557 }
1558 clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
A Sun19d41a22019-07-14 22:51:26 -04001559 status = usb_submit_urb(ir->urb_in, GFP_KERNEL);
1560 if (status < 0) {
1561 dev_err(ir->dev, "rx unhalt submit urb error = %d",
1562 status);
A Suna06854a2017-03-26 15:28:08 -03001563 }
1564 }
A Sunc779a9c2017-04-13 05:06:47 -03001565
1566 if (test_bit(EVENT_TX_HALT, &ir->kevent_flags)) {
1567 status = usb_clear_halt(ir->usbdev, ir->pipe_out);
A Sun19d41a22019-07-14 22:51:26 -04001568 dev_err(ir->dev, "tx clear halt status = %d", status);
1569 if (status < 0) {
1570 /*
1571 * Unable to clear TX halt/stall.
1572 * Will need to call usb_reset_device().
1573 */
1574 dev_err(ir->dev,
1575 "stuck TX HALT state requires USB Reset Device to clear");
1576 usb_queue_reset_device(ir->usbintf);
1577 set_bit(EVENT_RST_PEND, &ir->kevent_flags);
1578 clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
1579
1580 /* Cancel all other error events and handlers */
1581 clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
1582 return;
1583 }
A Sunc779a9c2017-04-13 05:06:47 -03001584 clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
1585 }
A Suna06854a2017-03-26 15:28:08 -03001586}
1587
Alexey Khoroshilove947d9a2014-09-08 19:10:43 -03001588static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -03001589{
Alexey Khoroshilove947d9a2014-09-08 19:10:43 -03001590 struct usb_device *udev = ir->usbdev;
Jarod Wilson66e89522010-06-01 17:32:08 -03001591 struct device *dev = ir->dev;
David Härdemand8b4b582010-10-29 16:08:23 -03001592 struct rc_dev *rc;
1593 int ret;
Jarod Wilson66e89522010-06-01 17:32:08 -03001594
Andi Shyti0f7499f2016-12-16 06:50:58 -02001595 rc = rc_allocate_device(RC_DRIVER_IR_RAW);
David Härdemand8b4b582010-10-29 16:08:23 -03001596 if (!rc) {
Sean Young6b4a16c2014-01-20 19:10:44 -03001597 dev_err(dev, "remote dev allocation failed");
David Härdemand8b4b582010-10-29 16:08:23 -03001598 goto out;
Jarod Wilson66e89522010-06-01 17:32:08 -03001599 }
1600
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001601 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
David Härdemand8b4b582010-10-29 16:08:23 -03001602 mceusb_model[ir->model].name ?
Paul Bender5ad1a552010-11-17 16:56:17 -03001603 mceusb_model[ir->model].name :
David Härdemand8b4b582010-10-29 16:08:23 -03001604 "Media Center Ed. eHome Infrared Remote Transceiver",
Jarod Wilson66e89522010-06-01 17:32:08 -03001605 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1606 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1607
Jarod Wilson66e89522010-06-01 17:32:08 -03001608 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
Jarod Wilson66e89522010-06-01 17:32:08 -03001609
Sean Young518f4b22017-07-01 12:13:19 -04001610 rc->device_name = ir->name;
David Härdemand8b4b582010-10-29 16:08:23 -03001611 rc->input_phys = ir->phys;
1612 usb_to_input_id(ir->usbdev, &rc->input_id);
1613 rc->dev.parent = dev;
1614 rc->priv = ir;
Sean Young6d741bf2017-08-07 16:20:58 -04001615 rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
Sean Younge6d025d2021-08-03 11:03:30 +02001616 rc->rx_resolution = MCE_TIME_UNIT;
Sean Young528222d82020-08-23 19:23:05 +02001617 rc->min_timeout = MCE_TIME_UNIT;
1618 rc->timeout = MS_TO_US(100);
Sean Youngaec3ead2018-05-09 06:11:28 -04001619 if (!mceusb_model[ir->model].broken_irtimeout) {
1620 rc->s_timeout = mceusb_set_timeout;
1621 rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
1622 } else {
1623 /*
1624 * If we can't set the timeout using CMD_SETIRTIMEOUT, we can
1625 * rely on software timeouts for timeouts < 100ms.
1626 */
1627 rc->max_timeout = rc->timeout;
1628 }
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001629 if (!ir->flags.no_tx) {
David Härdemand8b4b582010-10-29 16:08:23 -03001630 rc->s_tx_mask = mceusb_set_tx_mask;
1631 rc->s_tx_carrier = mceusb_set_tx_carrier;
1632 rc->tx_ir = mceusb_tx_ir;
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001633 }
A Sun279c60f2018-03-16 15:52:09 -04001634 if (ir->flags.rx2 > 0) {
Sean Young8b777ed2021-07-03 11:04:40 +02001635 rc->s_wideband_receiver = mceusb_set_rx_wideband;
A Sun279c60f2018-03-16 15:52:09 -04001636 rc->s_carrier_report = mceusb_set_rx_carrier_report;
1637 }
David Härdemand8b4b582010-10-29 16:08:23 -03001638 rc->driver_name = DRIVER_NAME;
Mauro Carvalho Chehab5b8c8d42014-07-27 17:28:48 -03001639
1640 switch (le16_to_cpu(udev->descriptor.idVendor)) {
1641 case VENDOR_HAUPPAUGE:
1642 rc->map_name = RC_MAP_HAUPPAUGE;
1643 break;
1644 case VENDOR_PCTV:
1645 rc->map_name = RC_MAP_PINNACLE_PCTV_HD;
1646 break;
1647 default:
1648 rc->map_name = RC_MAP_RC6_MCE;
1649 }
1650 if (mceusb_model[ir->model].rc_map)
1651 rc->map_name = mceusb_model[ir->model].rc_map;
Jarod Wilson66e89522010-06-01 17:32:08 -03001652
David Härdemand8b4b582010-10-29 16:08:23 -03001653 ret = rc_register_device(rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001654 if (ret < 0) {
Sean Young6b4a16c2014-01-20 19:10:44 -03001655 dev_err(dev, "remote dev registration failed");
David Härdemand8b4b582010-10-29 16:08:23 -03001656 goto out;
Jarod Wilson66e89522010-06-01 17:32:08 -03001657 }
1658
David Härdemand8b4b582010-10-29 16:08:23 -03001659 return rc;
Jarod Wilson66e89522010-06-01 17:32:08 -03001660
David Härdemand8b4b582010-10-29 16:08:23 -03001661out:
1662 rc_free_device(rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001663 return NULL;
1664}
1665
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001666static int mceusb_dev_probe(struct usb_interface *intf,
1667 const struct usb_device_id *id)
Jarod Wilson66e89522010-06-01 17:32:08 -03001668{
1669 struct usb_device *dev = interface_to_usbdev(intf);
1670 struct usb_host_interface *idesc;
1671 struct usb_endpoint_descriptor *ep = NULL;
1672 struct usb_endpoint_descriptor *ep_in = NULL;
1673 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001674 struct mceusb_dev *ir = NULL;
Sean Younge1159cb2016-04-14 17:42:50 -03001675 int pipe, maxp, i, res;
Jarod Wilson66e89522010-06-01 17:32:08 -03001676 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001677 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001678 bool is_gen3;
1679 bool is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001680 bool tx_mask_normal;
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001681 int ir_intfnum;
Jarod Wilson66e89522010-06-01 17:32:08 -03001682
Sean Young6b4a16c2014-01-20 19:10:44 -03001683 dev_dbg(&intf->dev, "%s called", __func__);
Jarod Wilson66e89522010-06-01 17:32:08 -03001684
Jarod Wilson66e89522010-06-01 17:32:08 -03001685 idesc = intf->cur_altsetting;
1686
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001687 is_gen3 = mceusb_model[model].mce_gen3;
1688 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001689 tx_mask_normal = mceusb_model[model].tx_mask_normal;
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001690 ir_intfnum = mceusb_model[model].ir_intfnum;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001691
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001692 /* There are multi-function devices with non-IR interfaces */
1693 if (idesc->desc.bInterfaceNumber != ir_intfnum)
1694 return -ENODEV;
Jarod Wilson66e89522010-06-01 17:32:08 -03001695
1696 /* step through the endpoints to find first bulk in and out endpoint */
1697 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1698 ep = &idesc->endpoint[i].desc;
1699
Matt DeVillier0cacb462014-04-24 11:16:31 -03001700 if (ep_in == NULL) {
1701 if (usb_endpoint_is_bulk_in(ep)) {
1702 ep_in = ep;
1703 dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n");
1704 } else if (usb_endpoint_is_int_in(ep)) {
1705 ep_in = ep;
1706 ep_in->bInterval = 1;
1707 dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n");
1708 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001709 }
1710
Matt DeVillier0cacb462014-04-24 11:16:31 -03001711 if (ep_out == NULL) {
1712 if (usb_endpoint_is_bulk_out(ep)) {
1713 ep_out = ep;
1714 dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n");
1715 } else if (usb_endpoint_is_int_out(ep)) {
1716 ep_out = ep;
1717 ep_out->bInterval = 1;
1718 dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n");
1719 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001720 }
1721 }
Johan Hovold03eb2a52017-03-07 15:14:13 -03001722 if (!ep_in || !ep_out) {
1723 dev_dbg(&intf->dev, "required endpoints not found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001724 return -ENODEV;
1725 }
1726
Matt DeVillier0cacb462014-04-24 11:16:31 -03001727 if (usb_endpoint_xfer_int(ep_in))
1728 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1729 else
1730 pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress);
Jarod Wilson66e89522010-06-01 17:32:08 -03001731 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1732
1733 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1734 if (!ir)
1735 goto mem_alloc_fail;
1736
A Suna06854a2017-03-26 15:28:08 -03001737 ir->pipe_in = pipe;
Christophe JAILLETddecfc72020-08-15 08:12:37 +02001738 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_KERNEL, &ir->dma_in);
Jarod Wilson66e89522010-06-01 17:32:08 -03001739 if (!ir->buf_in)
1740 goto buf_in_alloc_fail;
1741
1742 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1743 if (!ir->urb_in)
1744 goto urb_in_alloc_fail;
1745
A Sun19d41a22019-07-14 22:51:26 -04001746 ir->usbintf = intf;
Alexey Khoroshilove947d9a2014-09-08 19:10:43 -03001747 ir->usbdev = usb_get_dev(dev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001748 ir->dev = &intf->dev;
1749 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001750 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001751 ir->flags.tx_mask_normal = tx_mask_normal;
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001752 ir->flags.no_tx = mceusb_model[model].no_tx;
A Sun279c60f2018-03-16 15:52:09 -04001753 ir->flags.rx2 = mceusb_model[model].rx2;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001754 ir->model = model;
1755
Jarod Wilson66e89522010-06-01 17:32:08 -03001756 /* Saving usb interface data for use by the transmitter routine */
Jarod Wilson66e89522010-06-01 17:32:08 -03001757 ir->usb_ep_out = ep_out;
A Sunc779a9c2017-04-13 05:06:47 -03001758 if (usb_endpoint_xfer_int(ep_out))
1759 ir->pipe_out = usb_sndintpipe(ir->usbdev,
1760 ep_out->bEndpointAddress);
1761 else
1762 ir->pipe_out = usb_sndbulkpipe(ir->usbdev,
1763 ep_out->bEndpointAddress);
Jarod Wilson66e89522010-06-01 17:32:08 -03001764
1765 if (dev->descriptor.iManufacturer
1766 && usb_string(dev, dev->descriptor.iManufacturer,
1767 buf, sizeof(buf)) > 0)
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001768 strscpy(name, buf, sizeof(name));
Jarod Wilson66e89522010-06-01 17:32:08 -03001769 if (dev->descriptor.iProduct
1770 && usb_string(dev, dev->descriptor.iProduct,
1771 buf, sizeof(buf)) > 0)
1772 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1773 " %s", buf);
1774
A Suna06854a2017-03-26 15:28:08 -03001775 /*
1776 * Initialize async USB error handler before registering
1777 * or activating any mceusb RX and TX functions
1778 */
1779 INIT_WORK(&ir->kevent, mceusb_deferred_kevent);
1780
Alexey Khoroshilove947d9a2014-09-08 19:10:43 -03001781 ir->rc = mceusb_init_rc_dev(ir);
David Härdemand8b4b582010-10-29 16:08:23 -03001782 if (!ir->rc)
1783 goto rc_dev_fail;
Jarod Wilson66e89522010-06-01 17:32:08 -03001784
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001785 /* wire up inbound data handler */
A Sun8e175b22017-03-26 15:33:07 -03001786 if (usb_endpoint_xfer_int(ep_in))
1787 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1788 mceusb_dev_recv, ir, ep_in->bInterval);
1789 else
1790 usb_fill_bulk_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1791 mceusb_dev_recv, ir);
1792
Jarod Wilson66e89522010-06-01 17:32:08 -03001793 ir->urb_in->transfer_dma = ir->dma_in;
1794 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1795
Jarod Wilson3a918aa2011-05-26 14:23:18 -03001796 /* flush buffers on the device */
A Sun5c809922017-03-26 16:04:51 -03001797 dev_dbg(&intf->dev, "Flushing receive buffers");
Sean Younge1159cb2016-04-14 17:42:50 -03001798 res = usb_submit_urb(ir->urb_in, GFP_KERNEL);
1799 if (res)
A Sun5c809922017-03-26 16:04:51 -03001800 dev_err(&intf->dev, "failed to flush buffers: %d", res);
Jarod Wilson3a918aa2011-05-26 14:23:18 -03001801
Jarod Wilsonab1072e2011-07-18 16:54:25 -03001802 /* figure out which firmware/emulator version this hardware has */
1803 mceusb_get_emulator_version(ir);
1804
Jarod Wilson66e89522010-06-01 17:32:08 -03001805 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001806 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001807 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001808 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001809 mceusb_gen2_init(ir);
1810
Jarod Wilson22b07662010-07-08 12:38:57 -03001811 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001812
Jarod Wilsonb71969b2011-07-18 16:54:27 -03001813 mceusb_flash_led(ir);
1814
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001815 if (!ir->flags.no_tx)
David Härdemand8b4b582010-10-29 16:08:23 -03001816 mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001817
1818 usb_set_intfdata(intf, ir);
1819
Jarod Wilsonfa3348982011-07-18 16:54:23 -03001820 /* enable wake via this device */
1821 device_set_wakeup_capable(ir->dev, true);
1822 device_set_wakeup_enable(ir->dev, true);
1823
Sean Young6b4a16c2014-01-20 19:10:44 -03001824 dev_info(&intf->dev, "Registered %s with mce emulator interface version %x",
1825 name, ir->emver);
1826 dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
Jarod Wilsona411e832011-07-18 16:54:26 -03001827 ir->num_txports, ir->txports_cabled,
1828 ir->num_rxports, ir->rxports_active);
Jarod Wilson66e89522010-06-01 17:32:08 -03001829
1830 return 0;
1831
1832 /* Error-handling path */
David Härdemand8b4b582010-10-29 16:08:23 -03001833rc_dev_fail:
A Suna06854a2017-03-26 15:28:08 -03001834 cancel_work_sync(&ir->kevent);
Alexey Khoroshilove947d9a2014-09-08 19:10:43 -03001835 usb_put_dev(ir->usbdev);
Sean Younge1159cb2016-04-14 17:42:50 -03001836 usb_kill_urb(ir->urb_in);
Jarod Wilson66e89522010-06-01 17:32:08 -03001837 usb_free_urb(ir->urb_in);
1838urb_in_alloc_fail:
1839 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1840buf_in_alloc_fail:
1841 kfree(ir);
1842mem_alloc_fail:
Sean Young6b4a16c2014-01-20 19:10:44 -03001843 dev_err(&intf->dev, "%s: device setup failed!", __func__);
Jarod Wilson66e89522010-06-01 17:32:08 -03001844
1845 return -ENOMEM;
1846}
1847
1848
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001849static void mceusb_dev_disconnect(struct usb_interface *intf)
Jarod Wilson66e89522010-06-01 17:32:08 -03001850{
1851 struct usb_device *dev = interface_to_usbdev(intf);
1852 struct mceusb_dev *ir = usb_get_intfdata(intf);
1853
A Sun19d41a22019-07-14 22:51:26 -04001854 dev_dbg(&intf->dev, "%s called", __func__);
1855
Jarod Wilson66e89522010-06-01 17:32:08 -03001856 usb_set_intfdata(intf, NULL);
1857
1858 if (!ir)
1859 return;
1860
1861 ir->usbdev = NULL;
A Suna06854a2017-03-26 15:28:08 -03001862 cancel_work_sync(&ir->kevent);
David Härdemand8b4b582010-10-29 16:08:23 -03001863 rc_unregister_device(ir->rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001864 usb_kill_urb(ir->urb_in);
1865 usb_free_urb(ir->urb_in);
1866 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
Alexey Khoroshilove947d9a2014-09-08 19:10:43 -03001867 usb_put_dev(dev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001868
1869 kfree(ir);
1870}
1871
1872static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1873{
1874 struct mceusb_dev *ir = usb_get_intfdata(intf);
Sean Young6b4a16c2014-01-20 19:10:44 -03001875 dev_info(ir->dev, "suspend");
Jarod Wilson66e89522010-06-01 17:32:08 -03001876 usb_kill_urb(ir->urb_in);
1877 return 0;
1878}
1879
1880static int mceusb_dev_resume(struct usb_interface *intf)
1881{
1882 struct mceusb_dev *ir = usb_get_intfdata(intf);
Sean Young6b4a16c2014-01-20 19:10:44 -03001883 dev_info(ir->dev, "resume");
Jarod Wilson66e89522010-06-01 17:32:08 -03001884 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1885 return -EIO;
1886 return 0;
1887}
1888
1889static struct usb_driver mceusb_dev_driver = {
1890 .name = DRIVER_NAME,
1891 .probe = mceusb_dev_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001892 .disconnect = mceusb_dev_disconnect,
Jarod Wilson66e89522010-06-01 17:32:08 -03001893 .suspend = mceusb_dev_suspend,
1894 .resume = mceusb_dev_resume,
1895 .reset_resume = mceusb_dev_resume,
1896 .id_table = mceusb_dev_table
1897};
1898
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001899module_usb_driver(mceusb_dev_driver);
Jarod Wilson66e89522010-06-01 17:32:08 -03001900
1901MODULE_DESCRIPTION(DRIVER_DESC);
1902MODULE_AUTHOR(DRIVER_AUTHOR);
1903MODULE_LICENSE("GPL");
1904MODULE_DEVICE_TABLE(usb, mceusb_dev_table);