blob: 6cf22012079ab6a1f39d1356bd86abb387d5e7e7 [file] [log] [blame]
Jarod Wilson66e89522010-06-01 17:32:08 -03001/*
2 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
3 *
4 * Copyright (c) 2010 by Jarod Wilson <jarod@redhat.com>
5 *
6 * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
7 * Conti, Martin Blatter and Daniel Melander, the latter of which was
8 * in turn also based on the lirc_atiusb driver by Paul Miller. The
9 * two mce drivers were merged into one by Jarod Wilson, with transmit
10 * support for the 1st-gen device added primarily by Patrick Calhoun,
11 * with a bit of tweaks by Jarod. Debugging improvements and proper
12 * support for what appears to be 3rd-gen hardware added by Jarod.
13 * Initial port from lirc driver to ir-core drivery by Jarod, based
14 * partially on a port to an earlier proposed IR infrastructure by
15 * Jon Smirl, which included enhancements and simplifications to the
16 * incoming IR buffer parsing routines.
17 *
Jarod Wilson66e89522010-06-01 17:32:08 -030018 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 *
33 */
34
35#include <linux/device.h>
36#include <linux/module.h>
37#include <linux/slab.h>
Paul Bender635f76b2010-12-16 13:23:07 -030038#include <linux/usb.h>
39#include <linux/usb/input.h>
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030040#include <media/rc-core.h>
Jarod Wilson66e89522010-06-01 17:32:08 -030041
42#define DRIVER_VERSION "1.91"
43#define DRIVER_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
44#define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
45 "device driver"
46#define DRIVER_NAME "mceusb"
47
Jarod Wilson4a883912010-10-22 14:42:54 -030048#define USB_BUFLEN 32 /* USB reception buffer length */
49#define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
50#define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
Jarod Wilson66e89522010-06-01 17:32:08 -030051
52/* MCE constants */
Jarod Wilson4a883912010-10-22 14:42:54 -030053#define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
54#define MCE_TIME_UNIT 50 /* Approx 50us resolution */
55#define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
56#define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
57#define MCE_IRDATA_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
58#define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
59#define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
60#define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
61#define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
62#define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
63#define MCE_PULSE_MASK 0x7f /* Pulse mask */
64#define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
65
66#define MCE_HW_CMD_HEADER 0xff /* MCE hardware command header */
67#define MCE_COMMAND_HEADER 0x9f /* MCE command header */
68#define MCE_COMMAND_MASK 0xe0 /* Mask out command bits */
69#define MCE_COMMAND_NULL 0x00 /* These show up various places... */
70/* if buf[i] & MCE_COMMAND_MASK == 0x80 and buf[i] != MCE_COMMAND_HEADER,
71 * then we're looking at a raw IR data sample */
72#define MCE_COMMAND_IRDATA 0x80
73#define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
74
75/* Sub-commands, which follow MCE_COMMAND_HEADER or MCE_HW_CMD_HEADER */
Jarod Wilson1cd50f22010-11-09 18:41:03 -030076#define MCE_CMD_SIG_END 0x01 /* End of signal */
Jarod Wilson4a883912010-10-22 14:42:54 -030077#define MCE_CMD_PING 0x03 /* Ping device */
78#define MCE_CMD_UNKNOWN 0x04 /* Unknown */
79#define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
80#define MCE_CMD_S_CARRIER 0x06 /* Set TX carrier frequency */
81#define MCE_CMD_G_CARRIER 0x07 /* Get TX carrier frequency */
82#define MCE_CMD_S_TXMASK 0x08 /* Set TX port bitmask */
83#define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
84#define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
85#define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
86#define MCE_CMD_S_TIMEOUT 0x0c /* Set RX timeout value */
87#define MCE_CMD_G_TIMEOUT 0x0d /* Get RX timeout value */
88#define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
89#define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
90#define MCE_CMD_G_RXPORTSTS 0x11 /* Get RX port status */
91#define MCE_CMD_G_TXMASK 0x13 /* Set TX port bitmask */
92#define MCE_CMD_S_RXSENSOR 0x14 /* Set RX sensor (std/learning) */
93#define MCE_CMD_G_RXSENSOR 0x15 /* Get RX sensor (std/learning) */
Jarod Wilson2ee95db2010-11-12 19:49:04 -030094#define MCE_RSP_PULSE_COUNT 0x15 /* RX pulse count (only if learning) */
Jarod Wilson4a883912010-10-22 14:42:54 -030095#define MCE_CMD_TX_PORTS 0x16 /* Get number of TX ports */
96#define MCE_CMD_G_WAKESRC 0x17 /* Get wake source */
97#define MCE_CMD_UNKNOWN7 0x18 /* Unknown */
98#define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
99#define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
100#define MCE_CMD_DEVICE_RESET 0xaa /* Reset the hardware */
101#define MCE_RSP_CMD_INVALID 0xfe /* Invalid command issued */
Jarod Wilson66e89522010-06-01 17:32:08 -0300102
103
104/* module parameters */
105#ifdef CONFIG_USB_DEBUG
106static int debug = 1;
107#else
108static int debug;
109#endif
110
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300111#define mce_dbg(dev, fmt, ...) \
112 do { \
113 if (debug) \
114 dev_info(dev, fmt, ## __VA_ARGS__); \
115 } while (0)
116
Jarod Wilson66e89522010-06-01 17:32:08 -0300117/* general constants */
118#define SEND_FLAG_IN_PROGRESS 1
119#define SEND_FLAG_COMPLETE 2
120#define RECV_FLAG_IN_PROGRESS 3
121#define RECV_FLAG_COMPLETE 4
122
123#define MCEUSB_RX 1
124#define MCEUSB_TX 2
125
126#define VENDOR_PHILIPS 0x0471
127#define VENDOR_SMK 0x0609
128#define VENDOR_TATUNG 0x1460
129#define VENDOR_GATEWAY 0x107b
130#define VENDOR_SHUTTLE 0x1308
131#define VENDOR_SHUTTLE2 0x051c
132#define VENDOR_MITSUMI 0x03ee
133#define VENDOR_TOPSEED 0x1784
134#define VENDOR_RICAVISION 0x179d
135#define VENDOR_ITRON 0x195d
136#define VENDOR_FIC 0x1509
137#define VENDOR_LG 0x043e
138#define VENDOR_MICROSOFT 0x045e
139#define VENDOR_FORMOSA 0x147a
140#define VENDOR_FINTEK 0x1934
141#define VENDOR_PINNACLE 0x2304
142#define VENDOR_ECS 0x1019
143#define VENDOR_WISTRON 0x0fb8
144#define VENDOR_COMPRO 0x185b
145#define VENDOR_NORTHSTAR 0x04eb
146#define VENDOR_REALTEK 0x0bda
147#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300148#define VENDOR_CONEXANT 0x0572
Jarod Wilson66e89522010-06-01 17:32:08 -0300149
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300150enum mceusb_model_type {
151 MCE_GEN2 = 0, /* Most boards */
152 MCE_GEN1,
153 MCE_GEN3,
154 MCE_GEN2_TX_INV,
155 POLARIS_EVK,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300156 CX_HYBRID_TV,
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300157 MULTIFUNCTION,
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300158 TIVO_KIT,
Jarod Wilson2faa0ca2011-04-19 16:47:34 -0300159 MCE_GEN2_NO_TX,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300160};
161
162struct mceusb_model {
163 u32 mce_gen1:1;
164 u32 mce_gen2:1;
165 u32 mce_gen3:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300166 u32 tx_mask_normal:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300167 u32 no_tx:1;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300168
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300169 int ir_intfnum;
170
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300171 const char *rc_map; /* Allow specify a per-board map */
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300172 const char *name; /* per-board name */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300173};
174
175static const struct mceusb_model mceusb_model[] = {
176 [MCE_GEN1] = {
177 .mce_gen1 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300178 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300179 },
180 [MCE_GEN2] = {
181 .mce_gen2 = 1,
182 },
Jarod Wilson2faa0ca2011-04-19 16:47:34 -0300183 [MCE_GEN2_NO_TX] = {
184 .mce_gen2 = 1,
185 .no_tx = 1,
186 },
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300187 [MCE_GEN2_TX_INV] = {
188 .mce_gen2 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300189 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300190 },
191 [MCE_GEN3] = {
192 .mce_gen3 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300193 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300194 },
195 [POLARIS_EVK] = {
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300196 /*
197 * In fact, the EVK is shipped without
198 * remotes, but we should have something handy,
199 * to allow testing it
200 */
Mauro Carvalho Chehab15195d32011-01-24 12:18:47 -0300201 .rc_map = RC_MAP_HAUPPAUGE,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300202 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
203 },
204 [CX_HYBRID_TV] = {
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300205 .no_tx = 1, /* tx isn't wired up at all */
206 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300207 },
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300208 [MULTIFUNCTION] = {
209 .mce_gen2 = 1,
210 .ir_intfnum = 2,
211 },
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300212 [TIVO_KIT] = {
213 .mce_gen2 = 1,
214 .rc_map = RC_MAP_TIVO,
215 },
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300216};
217
Jarod Wilson66e89522010-06-01 17:32:08 -0300218static struct usb_device_id mceusb_dev_table[] = {
219 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300220 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
221 .driver_info = MCE_GEN1 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300222 /* Philips Infrared Transceiver - Sahara branded */
223 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
224 /* Philips Infrared Transceiver - HP branded */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300225 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
226 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300227 /* Philips SRM5100 */
228 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
229 /* Philips Infrared Transceiver - Omaura */
230 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
231 /* Philips Infrared Transceiver - Spinel plus */
232 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
233 /* Philips eHome Infrared Transceiver */
234 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300235 /* Philips/Spinel plus IR transceiver for ASUS */
236 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
237 /* Philips/Spinel plus IR transceiver for ASUS */
238 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilsone296e122011-04-25 14:48:18 -0300239 /* Philips IR transceiver (Dell branded) */
240 { USB_DEVICE(VENDOR_PHILIPS, 0x2093) },
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300241 /* Realtek MCE IR Receiver and card reader */
242 { USB_DEVICE(VENDOR_REALTEK, 0x0161),
243 .driver_info = MULTIFUNCTION },
Jarod Wilson66e89522010-06-01 17:32:08 -0300244 /* SMK/Toshiba G83C0004D410 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300245 { USB_DEVICE(VENDOR_SMK, 0x031d),
246 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300247 /* SMK eHome Infrared Transceiver (Sony VAIO) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300248 { USB_DEVICE(VENDOR_SMK, 0x0322),
249 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300250 /* bundled with Hauppauge PVR-150 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300251 { USB_DEVICE(VENDOR_SMK, 0x0334),
252 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300253 /* SMK eHome Infrared Transceiver */
254 { USB_DEVICE(VENDOR_SMK, 0x0338) },
Jarod Wilsonb825fe1b2011-05-26 16:03:17 -0300255 /* SMK/I-O Data GV-MC7/RCKIT Receiver */
256 { USB_DEVICE(VENDOR_SMK, 0x0353),
257 .driver_info = MCE_GEN2_NO_TX },
Jarod Wilson66e89522010-06-01 17:32:08 -0300258 /* Tatung eHome Infrared Transceiver */
259 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
260 /* Shuttle eHome Infrared Transceiver */
261 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
262 /* Shuttle eHome Infrared Transceiver */
263 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
264 /* Gateway eHome Infrared Transceiver */
265 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
266 /* Mitsumi */
267 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
268 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300269 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
270 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300271 /* Topseed HP eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300272 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
273 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300274 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300275 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
276 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300277 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300278 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
279 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300280 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300281 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
282 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300283 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300284 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
Jarod Wilson7d9a46f2011-03-04 20:20:47 -0300285 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300286 /* Ricavision internal Infrared Transceiver */
287 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
288 /* Itron ione Libra Q-11 */
289 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
290 /* FIC eHome Infrared Transceiver */
291 { USB_DEVICE(VENDOR_FIC, 0x9242) },
292 /* LG eHome Infrared Transceiver */
293 { USB_DEVICE(VENDOR_LG, 0x9803) },
294 /* Microsoft MCE Infrared Transceiver */
295 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
296 /* Formosa eHome Infrared Transceiver */
297 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
298 /* Formosa21 / eHome Infrared Receiver */
299 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
300 /* Formosa aim / Trust MCE Infrared Receiver */
Jarod Wilson2faa0ca2011-04-19 16:47:34 -0300301 { USB_DEVICE(VENDOR_FORMOSA, 0xe017),
302 .driver_info = MCE_GEN2_NO_TX },
Jarod Wilson66e89522010-06-01 17:32:08 -0300303 /* Formosa Industrial Computing / Beanbag Emulation Device */
304 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
305 /* Formosa21 / eHome Infrared Receiver */
306 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
307 /* Formosa Industrial Computing AIM IR605/A */
308 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
309 /* Formosa Industrial Computing */
310 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
Jarod Wilsonfbb1f1b2010-12-16 13:27:11 -0300311 /* Fintek eHome Infrared Transceiver (HP branded) */
312 { USB_DEVICE(VENDOR_FINTEK, 0x5168) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300313 /* Fintek eHome Infrared Transceiver */
314 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
315 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
316 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
317 /* Pinnacle Remote Kit */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300318 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
319 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300320 /* Elitegroup Computer Systems IR */
321 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
322 /* Wistron Corp. eHome Infrared Receiver */
323 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
324 /* Compro K100 */
325 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
326 /* Compro K100 v2 */
327 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
328 /* Northstar Systems, Inc. eHome Infrared Transceiver */
329 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
330 /* TiVo PC IR Receiver */
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300331 { USB_DEVICE(VENDOR_TIVO, 0x2000),
332 .driver_info = TIVO_KIT },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300333 /* Conexant Hybrid TV "Shelby" Polaris SDK */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300334 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
335 .driver_info = POLARIS_EVK },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300336 /* Conexant Hybrid TV RDU253S Polaris */
337 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
338 .driver_info = CX_HYBRID_TV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300339 /* Terminating entry */
340 { }
341};
342
Jarod Wilson66e89522010-06-01 17:32:08 -0300343/* data structure for each usb transceiver */
344struct mceusb_dev {
345 /* ir-core bits */
David Härdemand8b4b582010-10-29 16:08:23 -0300346 struct rc_dev *rc;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300347
348 /* optional features we can enable */
349 bool carrier_report_enabled;
350 bool learning_enabled;
Jarod Wilson66e89522010-06-01 17:32:08 -0300351
352 /* core device bits */
353 struct device *dev;
Jarod Wilson66e89522010-06-01 17:32:08 -0300354
355 /* usb */
356 struct usb_device *usbdev;
357 struct urb *urb_in;
358 struct usb_endpoint_descriptor *usb_ep_in;
359 struct usb_endpoint_descriptor *usb_ep_out;
360
361 /* buffers and dma */
362 unsigned char *buf_in;
363 unsigned int len_in;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300364 dma_addr_t dma_in;
365 dma_addr_t dma_out;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300366
367 enum {
368 CMD_HEADER = 0,
369 SUBCMD,
370 CMD_DATA,
371 PARSE_IRDATA,
372 } parser_state;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300373
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300374 u8 cmd, rem; /* Remaining IR data bytes in packet */
Jarod Wilson66e89522010-06-01 17:32:08 -0300375
376 struct {
377 u32 connected:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300378 u32 tx_mask_normal:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300379 u32 microsoft_gen1:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300380 u32 no_tx:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300381 } flags;
382
Jarod Wilsone23fb962010-06-16 17:55:52 -0300383 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300384 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300385 u32 carrier;
386 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300387
388 char name[128];
389 char phys[64];
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300390 enum mceusb_model_type model;
Jarod Wilson66e89522010-06-01 17:32:08 -0300391};
392
393/*
394 * MCE Device Command Strings
395 * Device command responses vary from device to device...
396 * - DEVICE_RESET resets the hardware to its default state
397 * - GET_REVISION fetches the hardware/software revision, common
398 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
399 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
400 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
401 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
402 * ((clk / frequency) - 1)
403 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
404 * response in the form of 9f 0c msb lsb
405 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
406 * the form of 9f 08 bm, where bm is the bitmask
407 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
408 * general use one or short-range learning one, in the form of
409 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
410 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
411 * - SET_TX_BITMASK sets the transmitter bitmask
412 * - SET_RX_TIMEOUT sets the receiver timeout
413 * - SET_RX_SENSOR sets which receiver sensor to use
414 */
Jarod Wilson4a883912010-10-22 14:42:54 -0300415static char DEVICE_RESET[] = {MCE_COMMAND_NULL, MCE_HW_CMD_HEADER,
416 MCE_CMD_DEVICE_RESET};
417static char GET_REVISION[] = {MCE_HW_CMD_HEADER, MCE_CMD_G_REVISION};
418static char GET_UNKNOWN[] = {MCE_HW_CMD_HEADER, MCE_CMD_UNKNOWN7};
419static char GET_UNKNOWN2[] = {MCE_COMMAND_HEADER, MCE_CMD_UNKNOWN2};
420static char GET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER, MCE_CMD_G_CARRIER};
421static char GET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TIMEOUT};
422static char GET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TXMASK};
423static char GET_RX_SENSOR[] = {MCE_COMMAND_HEADER, MCE_CMD_G_RXSENSOR};
Jarod Wilson66e89522010-06-01 17:32:08 -0300424/* sub in desired values in lower byte or bytes for full command */
425/* FIXME: make use of these for transmit.
Jarod Wilson4a883912010-10-22 14:42:54 -0300426static char SET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER,
427 MCE_CMD_S_CARRIER, 0x00, 0x00};
428static char SET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_S_TXMASK, 0x00};
429static char SET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER,
430 MCE_CMD_S_TIMEOUT, 0x00, 0x00};
431static char SET_RX_SENSOR[] = {MCE_COMMAND_HEADER,
432 MCE_CMD_S_RXSENSOR, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300433*/
434
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300435static int mceusb_cmdsize(u8 cmd, u8 subcmd)
436{
437 int datasize = 0;
438
439 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300440 case MCE_COMMAND_NULL:
441 if (subcmd == MCE_HW_CMD_HEADER)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300442 datasize = 1;
443 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300444 case MCE_HW_CMD_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300445 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300446 case MCE_CMD_G_REVISION:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300447 datasize = 2;
448 break;
449 }
Jarod Wilson4a883912010-10-22 14:42:54 -0300450 case MCE_COMMAND_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300451 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300452 case MCE_CMD_UNKNOWN:
453 case MCE_CMD_S_CARRIER:
454 case MCE_CMD_S_TIMEOUT:
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300455 case MCE_RSP_PULSE_COUNT:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300456 datasize = 2;
457 break;
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300458 case MCE_CMD_SIG_END:
Jarod Wilson4a883912010-10-22 14:42:54 -0300459 case MCE_CMD_S_TXMASK:
460 case MCE_CMD_S_RXSENSOR:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300461 datasize = 1;
462 break;
463 }
464 }
465 return datasize;
466}
467
Jarod Wilson66e89522010-06-01 17:32:08 -0300468static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300469 int offset, int len, bool out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300470{
471 char codes[USB_BUFLEN * 3 + 1];
472 char inout[9];
Jarod Wilson66e89522010-06-01 17:32:08 -0300473 u8 cmd, subcmd, data1, data2;
474 struct device *dev = ir->dev;
Jarod Wilson36e9d262010-10-22 16:49:35 -0300475 int i, start, skip = 0;
476
477 if (!debug)
478 return;
Jarod Wilson66e89522010-06-01 17:32:08 -0300479
Jarod Wilson657290b2010-06-16 17:10:05 -0300480 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
Jarod Wilson29b44942010-11-09 18:41:46 -0300481 if (ir->flags.microsoft_gen1 && !out && !offset)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300482 skip = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300483
Jarod Wilson36e9d262010-10-22 16:49:35 -0300484 if (len <= skip)
Jarod Wilson66e89522010-06-01 17:32:08 -0300485 return;
486
487 for (i = 0; i < len && i < USB_BUFLEN; i++)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300488 snprintf(codes + i * 3, 4, "%02x ", buf[i + offset] & 0xff);
Jarod Wilson66e89522010-06-01 17:32:08 -0300489
Jarod Wilson36e9d262010-10-22 16:49:35 -0300490 dev_info(dev, "%sx data: %s(length=%d)\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300491 (out ? "t" : "r"), codes, len);
492
493 if (out)
494 strcpy(inout, "Request\0");
495 else
496 strcpy(inout, "Got\0");
497
Jarod Wilson36e9d262010-10-22 16:49:35 -0300498 start = offset + skip;
499 cmd = buf[start] & 0xff;
500 subcmd = buf[start + 1] & 0xff;
501 data1 = buf[start + 2] & 0xff;
502 data2 = buf[start + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300503
504 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300505 case MCE_COMMAND_NULL:
506 if ((subcmd == MCE_HW_CMD_HEADER) &&
507 (data1 == MCE_CMD_DEVICE_RESET))
Jarod Wilson66e89522010-06-01 17:32:08 -0300508 dev_info(dev, "Device reset requested\n");
509 else
510 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
511 cmd, subcmd);
512 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300513 case MCE_HW_CMD_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300514 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300515 case MCE_CMD_G_REVISION:
Jarod Wilson66e89522010-06-01 17:32:08 -0300516 if (len == 2)
517 dev_info(dev, "Get hw/sw rev?\n");
518 else
519 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
520 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300521 buf[start + 4], buf[start + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300522 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300523 case MCE_CMD_DEVICE_RESET:
Jarod Wilson66e89522010-06-01 17:32:08 -0300524 dev_info(dev, "Device reset requested\n");
525 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300526 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300527 dev_info(dev, "Previous command not supported\n");
528 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300529 case MCE_CMD_UNKNOWN7:
530 case MCE_CMD_UNKNOWN9:
Jarod Wilson66e89522010-06-01 17:32:08 -0300531 default:
532 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
533 cmd, subcmd);
534 break;
535 }
536 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300537 case MCE_COMMAND_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300538 switch (subcmd) {
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300539 case MCE_CMD_SIG_END:
540 dev_info(dev, "End of signal\n");
541 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300542 case MCE_CMD_PING:
Jarod Wilson66e89522010-06-01 17:32:08 -0300543 dev_info(dev, "Ping\n");
544 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300545 case MCE_CMD_UNKNOWN:
Jarod Wilson66e89522010-06-01 17:32:08 -0300546 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
547 data1, data2);
548 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300549 case MCE_CMD_S_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300550 dev_info(dev, "%s carrier mode and freq of "
551 "0x%02x 0x%02x\n", inout, data1, data2);
552 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300553 case MCE_CMD_G_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300554 dev_info(dev, "Get carrier mode and freq\n");
555 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300556 case MCE_CMD_S_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300557 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
558 inout, data1);
559 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300560 case MCE_CMD_S_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300561 /* value is in units of 50us, so x*50/100 or x/2 ms */
562 dev_info(dev, "%s receive timeout of %d ms\n",
563 inout, ((data1 << 8) | data2) / 2);
564 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300565 case MCE_CMD_G_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300566 dev_info(dev, "Get receive timeout\n");
567 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300568 case MCE_CMD_G_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300569 dev_info(dev, "Get transmit blaster mask\n");
570 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300571 case MCE_CMD_S_RXSENSOR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300572 dev_info(dev, "%s %s-range receive sensor in use\n",
573 inout, data1 == 0x02 ? "short" : "long");
574 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300575 case MCE_CMD_G_RXSENSOR:
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300576 /* aka MCE_RSP_PULSE_COUNT */
577 if (out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300578 dev_info(dev, "Get receive sensor\n");
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300579 else if (ir->learning_enabled)
580 dev_info(dev, "RX pulse count: %d\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300581 ((data1 << 8) | data2));
582 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300583 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300584 dev_info(dev, "Error! Hardware is likely wedged...\n");
585 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300586 case MCE_CMD_UNKNOWN2:
587 case MCE_CMD_UNKNOWN3:
588 case MCE_CMD_UNKNOWN5:
Jarod Wilson66e89522010-06-01 17:32:08 -0300589 default:
590 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
591 cmd, subcmd);
592 break;
593 }
594 break;
595 default:
596 break;
597 }
Jarod Wilson36e9d262010-10-22 16:49:35 -0300598
599 if (cmd == MCE_IRDATA_TRAILER)
600 dev_info(dev, "End of raw IR data\n");
601 else if ((cmd != MCE_COMMAND_HEADER) &&
602 ((cmd & MCE_COMMAND_MASK) == MCE_COMMAND_IRDATA))
603 dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem);
Jarod Wilson66e89522010-06-01 17:32:08 -0300604}
605
Jarod Wilson7c294402010-08-02 18:21:06 -0300606static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300607{
608 struct mceusb_dev *ir;
609 int len;
610
611 if (!urb)
612 return;
613
614 ir = urb->context;
615 if (ir) {
616 len = urb->actual_length;
617
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300618 mce_dbg(ir->dev, "callback called (status=%d len=%d)\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300619 urb->status, len);
620
Jarod Wilson36e9d262010-10-22 16:49:35 -0300621 mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
Jarod Wilson66e89522010-06-01 17:32:08 -0300622 }
623
624}
625
626/* request incoming or send outgoing usb packet - used to initialize remote */
Jarod Wilson51ea6292011-05-10 14:09:59 -0300627static void mce_request_packet(struct mceusb_dev *ir, unsigned char *data,
628 int size, int urb_type)
Jarod Wilson66e89522010-06-01 17:32:08 -0300629{
Jarod Wilson51ea6292011-05-10 14:09:59 -0300630 int res, pipe;
Jarod Wilson66e89522010-06-01 17:32:08 -0300631 struct urb *async_urb;
632 struct device *dev = ir->dev;
633 unsigned char *async_buf;
634
635 if (urb_type == MCEUSB_TX) {
636 async_urb = usb_alloc_urb(0, GFP_KERNEL);
637 if (unlikely(!async_urb)) {
638 dev_err(dev, "Error, couldn't allocate urb!\n");
639 return;
640 }
641
642 async_buf = kzalloc(size, GFP_KERNEL);
643 if (!async_buf) {
644 dev_err(dev, "Error, couldn't allocate buf!\n");
645 usb_free_urb(async_urb);
646 return;
647 }
648
649 /* outbound data */
Jarod Wilson51ea6292011-05-10 14:09:59 -0300650 pipe = usb_sndintpipe(ir->usbdev,
651 ir->usb_ep_out->bEndpointAddress);
652 usb_fill_int_urb(async_urb, ir->usbdev, pipe,
Jarod Wilson7c294402010-08-02 18:21:06 -0300653 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson51ea6292011-05-10 14:09:59 -0300654 ir, ir->usb_ep_out->bInterval);
Jarod Wilson66e89522010-06-01 17:32:08 -0300655 memcpy(async_buf, data, size);
656
657 } else if (urb_type == MCEUSB_RX) {
658 /* standard request */
659 async_urb = ir->urb_in;
660 ir->send_flags = RECV_FLAG_IN_PROGRESS;
661
662 } else {
663 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
664 return;
665 }
666
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300667 mce_dbg(dev, "receive request called (size=%#x)\n", size);
Jarod Wilson66e89522010-06-01 17:32:08 -0300668
669 async_urb->transfer_buffer_length = size;
670 async_urb->dev = ir->usbdev;
671
672 res = usb_submit_urb(async_urb, GFP_ATOMIC);
673 if (res) {
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300674 mce_dbg(dev, "receive request FAILED! (res=%d)\n", res);
Jarod Wilson66e89522010-06-01 17:32:08 -0300675 return;
676 }
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300677 mce_dbg(dev, "receive request complete (res=%d)\n", res);
Jarod Wilson66e89522010-06-01 17:32:08 -0300678}
679
680static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
681{
Jarod Wilson51ea6292011-05-10 14:09:59 -0300682 mce_request_packet(ir, data, size, MCEUSB_TX);
Jarod Wilson66e89522010-06-01 17:32:08 -0300683}
684
Jarod Wilson3a918aa2011-05-26 14:23:18 -0300685static void mce_flush_rx_buffer(struct mceusb_dev *ir, int size)
Jarod Wilson66e89522010-06-01 17:32:08 -0300686{
Jarod Wilson3a918aa2011-05-26 14:23:18 -0300687 mce_request_packet(ir, NULL, size, MCEUSB_RX);
Jarod Wilson66e89522010-06-01 17:32:08 -0300688}
689
Jarod Wilsone23fb962010-06-16 17:55:52 -0300690/* Send data out the IR blaster port(s) */
David Härdemand8b4b582010-10-29 16:08:23 -0300691static int mceusb_tx_ir(struct rc_dev *dev, int *txbuf, u32 n)
Jarod Wilsone23fb962010-06-16 17:55:52 -0300692{
David Härdemand8b4b582010-10-29 16:08:23 -0300693 struct mceusb_dev *ir = dev->priv;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300694 int i, ret = 0;
695 int count, cmdcount = 0;
696 unsigned char *cmdbuf; /* MCE command buffer */
697 long signal_duration = 0; /* Singnal length in us */
698 struct timeval start_time, end_time;
699
700 do_gettimeofday(&start_time);
701
702 count = n / sizeof(int);
703
704 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
705 if (!cmdbuf)
706 return -ENOMEM;
707
708 /* MCE tx init header */
Jarod Wilson4a883912010-10-22 14:42:54 -0300709 cmdbuf[cmdcount++] = MCE_COMMAND_HEADER;
710 cmdbuf[cmdcount++] = MCE_CMD_S_TXMASK;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300711 cmdbuf[cmdcount++] = ir->tx_mask;
712
713 /* Generate mce packet data */
714 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
715 signal_duration += txbuf[i];
716 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
717
718 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
719
720 /* Insert mce packet header every 4th entry */
721 if ((cmdcount < MCE_CMDBUF_SIZE) &&
722 (cmdcount - MCE_TX_HEADER_LENGTH) %
723 MCE_CODE_LENGTH == 0)
Jarod Wilson4a883912010-10-22 14:42:54 -0300724 cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300725
726 /* Insert mce packet data */
727 if (cmdcount < MCE_CMDBUF_SIZE)
728 cmdbuf[cmdcount++] =
729 (txbuf[i] < MCE_PULSE_BIT ?
730 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
731 (i & 1 ? 0x00 : MCE_PULSE_BIT);
732 else {
733 ret = -EINVAL;
734 goto out;
735 }
736
737 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
738 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
739 }
740
741 /* Fix packet length in last header */
742 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
Jarod Wilson4a883912010-10-22 14:42:54 -0300743 MCE_COMMAND_IRDATA + (cmdcount - MCE_TX_HEADER_LENGTH) %
744 MCE_CODE_LENGTH - 1;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300745
746 /* Check if we have room for the empty packet at the end */
747 if (cmdcount >= MCE_CMDBUF_SIZE) {
748 ret = -EINVAL;
749 goto out;
750 }
751
752 /* All mce commands end with an empty packet (0x80) */
Jarod Wilson4a883912010-10-22 14:42:54 -0300753 cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300754
755 /* Transmit the command to the mce device */
756 mce_async_out(ir, cmdbuf, cmdcount);
757
758 /*
759 * The lircd gap calculation expects the write function to
760 * wait the time it takes for the ircommand to be sent before
761 * it returns.
762 */
763 do_gettimeofday(&end_time);
764 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
765 (end_time.tv_sec - start_time.tv_sec) * 1000000;
766
767 /* delay with the closest number of ticks */
768 set_current_state(TASK_INTERRUPTIBLE);
769 schedule_timeout(usecs_to_jiffies(signal_duration));
770
771out:
772 kfree(cmdbuf);
773 return ret ? ret : n;
774}
775
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300776/* Sets active IR outputs -- mce devices typically have two */
David Härdemand8b4b582010-10-29 16:08:23 -0300777static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
Jarod Wilson657290b2010-06-16 17:10:05 -0300778{
David Härdemand8b4b582010-10-29 16:08:23 -0300779 struct mceusb_dev *ir = dev->priv;
Jarod Wilson657290b2010-06-16 17:10:05 -0300780
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300781 if (ir->flags.tx_mask_normal)
782 ir->tx_mask = mask;
783 else
Jarod Wilson4a883912010-10-22 14:42:54 -0300784 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
785 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300786
787 return 0;
788}
789
Jarod Wilsone23fb962010-06-16 17:55:52 -0300790/* Sets the send carrier frequency and mode */
David Härdemand8b4b582010-10-29 16:08:23 -0300791static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
Jarod Wilsone23fb962010-06-16 17:55:52 -0300792{
David Härdemand8b4b582010-10-29 16:08:23 -0300793 struct mceusb_dev *ir = dev->priv;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300794 int clk = 10000000;
795 int prescaler = 0, divisor = 0;
Jarod Wilson4a883912010-10-22 14:42:54 -0300796 unsigned char cmdbuf[4] = { MCE_COMMAND_HEADER,
797 MCE_CMD_S_CARRIER, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300798
799 /* Carrier has changed */
800 if (ir->carrier != carrier) {
801
802 if (carrier == 0) {
803 ir->carrier = carrier;
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300804 cmdbuf[2] = MCE_CMD_SIG_END;
Jarod Wilson4a883912010-10-22 14:42:54 -0300805 cmdbuf[3] = MCE_IRDATA_TRAILER;
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300806 mce_dbg(ir->dev, "%s: disabling carrier "
Jarod Wilsone23fb962010-06-16 17:55:52 -0300807 "modulation\n", __func__);
808 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
809 return carrier;
810 }
811
812 for (prescaler = 0; prescaler < 4; ++prescaler) {
813 divisor = (clk >> (2 * prescaler)) / carrier;
Jarod Wilson4a883912010-10-22 14:42:54 -0300814 if (divisor <= 0xff) {
Jarod Wilsone23fb962010-06-16 17:55:52 -0300815 ir->carrier = carrier;
816 cmdbuf[2] = prescaler;
817 cmdbuf[3] = divisor;
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300818 mce_dbg(ir->dev, "%s: requesting %u HZ "
Jarod Wilsone23fb962010-06-16 17:55:52 -0300819 "carrier\n", __func__, carrier);
820
821 /* Transmit new carrier to mce device */
822 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
823 return carrier;
824 }
825 }
826
827 return -EINVAL;
828
829 }
830
831 return carrier;
832}
833
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300834/*
835 * We don't do anything but print debug spew for many of the command bits
836 * we receive from the hardware, but some of them are useful information
837 * we want to store so that we can use them.
838 */
839static void mceusb_handle_command(struct mceusb_dev *ir, int index)
840{
841 u8 hi = ir->buf_in[index + 1] & 0xff;
842 u8 lo = ir->buf_in[index + 2] & 0xff;
843
844 switch (ir->buf_in[index]) {
845 /* 2-byte return value commands */
846 case MCE_CMD_S_TIMEOUT:
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300847 ir->rc->timeout = US_TO_NS((hi << 8 | lo) / 2);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300848 break;
849
850 /* 1-byte return value commands */
851 case MCE_CMD_S_TXMASK:
852 ir->tx_mask = hi;
853 break;
854 case MCE_CMD_S_RXSENSOR:
855 ir->learning_enabled = (hi == 0x02);
856 break;
857 default:
858 break;
859 }
860}
861
Jarod Wilson66e89522010-06-01 17:32:08 -0300862static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
863{
Maxim Levitsky46519182010-10-16 19:56:28 -0300864 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300865 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300866
867 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
868 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300869 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300870
Jarod Wilson29b44942010-11-09 18:41:46 -0300871 /* if there's no data, just return now */
872 if (buf_len <= i)
873 return;
874
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300875 for (; i < buf_len; i++) {
876 switch (ir->parser_state) {
877 case SUBCMD:
878 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
Jarod Wilson36e9d262010-10-22 16:49:35 -0300879 mceusb_dev_printdata(ir, ir->buf_in, i - 1,
880 ir->rem + 2, false);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300881 mceusb_handle_command(ir, i);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300882 ir->parser_state = CMD_DATA;
883 break;
884 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300885 ir->rem--;
Jarod Wilson5bd9d732011-01-26 12:20:09 -0300886 init_ir_raw_event(&rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -0300887 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
888 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300889 * US_TO_NS(MCE_TIME_UNIT);
Jarod Wilson66e89522010-06-01 17:32:08 -0300890
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300891 mce_dbg(ir->dev, "Storing %s with duration %d\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300892 rawir.pulse ? "pulse" : "space",
893 rawir.duration);
894
David Härdemand8b4b582010-10-29 16:08:23 -0300895 ir_raw_event_store_with_filter(ir->rc, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300896 break;
897 case CMD_DATA:
898 ir->rem--;
899 break;
900 case CMD_HEADER:
901 /* decode mce packets of the form (84),AA,BB,CC,DD */
902 /* IR data packets can span USB messages - rem */
903 ir->cmd = ir->buf_in[i];
Jarod Wilson4a883912010-10-22 14:42:54 -0300904 if ((ir->cmd == MCE_COMMAND_HEADER) ||
905 ((ir->cmd & MCE_COMMAND_MASK) !=
906 MCE_COMMAND_IRDATA)) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300907 ir->parser_state = SUBCMD;
908 continue;
909 }
910 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300911 mceusb_dev_printdata(ir, ir->buf_in,
912 i, ir->rem + 1, false);
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300913 if (ir->rem)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300914 ir->parser_state = PARSE_IRDATA;
Jarod Wilson5bd9d732011-01-26 12:20:09 -0300915 else
916 ir_raw_event_reset(ir->rc);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300917 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300918 }
919
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300920 if (ir->parser_state != CMD_HEADER && !ir->rem)
921 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300922 }
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300923 mce_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
David Härdemand8b4b582010-10-29 16:08:23 -0300924 ir_raw_event_handle(ir->rc);
Jarod Wilson66e89522010-06-01 17:32:08 -0300925}
926
Jarod Wilson66e89522010-06-01 17:32:08 -0300927static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
928{
929 struct mceusb_dev *ir;
930 int buf_len;
931
932 if (!urb)
933 return;
934
935 ir = urb->context;
936 if (!ir) {
937 usb_unlink_urb(urb);
938 return;
939 }
940
941 buf_len = urb->actual_length;
942
Jarod Wilson66e89522010-06-01 17:32:08 -0300943 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
944 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300945 mce_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300946 buf_len);
947 }
948
949 switch (urb->status) {
950 /* success */
951 case 0:
952 mceusb_process_ir_data(ir, buf_len);
953 break;
954
955 case -ECONNRESET:
956 case -ENOENT:
957 case -ESHUTDOWN:
958 usb_unlink_urb(urb);
959 return;
960
961 case -EPIPE:
962 default:
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300963 mce_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300964 break;
965 }
966
967 usb_submit_urb(urb, GFP_ATOMIC);
968}
969
970static void mceusb_gen1_init(struct mceusb_dev *ir)
971{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300972 int ret;
Jarod Wilson66e89522010-06-01 17:32:08 -0300973 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300974 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300975
976 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
977 if (!data) {
978 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300979 return;
980 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300981
982 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300983 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300984 * on the receive control pipe and expect a certain value pair back
985 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300986 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
987 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300988 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300989 mce_dbg(dev, "%s - ret = %d\n", __func__, ret);
990 mce_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300991 __func__, data[0], data[1]);
992
993 /* set feature: bit rate 38400 bps */
994 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
995 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
996 0xc04e, 0x0000, NULL, 0, HZ * 3);
997
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -0300998 mce_dbg(dev, "%s - ret = %d\n", __func__, ret);
Jarod Wilson66e89522010-06-01 17:32:08 -0300999
1000 /* bRequest 4: set char length to 8 bits */
1001 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1002 4, USB_TYPE_VENDOR,
1003 0x0808, 0x0000, NULL, 0, HZ * 3);
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -03001004 mce_dbg(dev, "%s - retB = %d\n", __func__, ret);
Jarod Wilson66e89522010-06-01 17:32:08 -03001005
1006 /* bRequest 2: set handshaking to use DTR/DSR */
1007 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1008 2, USB_TYPE_VENDOR,
1009 0x0000, 0x0100, NULL, 0, HZ * 3);
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -03001010 mce_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -03001011
Jarod Wilson22b07662010-07-08 12:38:57 -03001012 /* device reset */
1013 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
Jarod Wilson22b07662010-07-08 12:38:57 -03001014
1015 /* get hw/sw revision? */
1016 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
Jarod Wilson22b07662010-07-08 12:38:57 -03001017
Jarod Wilson657290b2010-06-16 17:10:05 -03001018 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -03001019};
1020
1021static void mceusb_gen2_init(struct mceusb_dev *ir)
1022{
Jarod Wilson66e89522010-06-01 17:32:08 -03001023 /* device reset */
1024 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
Jarod Wilson66e89522010-06-01 17:32:08 -03001025
1026 /* get hw/sw revision? */
1027 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
Jarod Wilson66e89522010-06-01 17:32:08 -03001028
Jarod Wilson22b07662010-07-08 12:38:57 -03001029 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -03001030 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
Jarod Wilson22b07662010-07-08 12:38:57 -03001031 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
Jarod Wilson66e89522010-06-01 17:32:08 -03001032}
1033
Jarod Wilson22b07662010-07-08 12:38:57 -03001034static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -03001035{
Jarod Wilson66e89522010-06-01 17:32:08 -03001036 /* get the carrier and frequency */
1037 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
Jarod Wilson66e89522010-06-01 17:32:08 -03001038
Jarod Wilson3a918aa2011-05-26 14:23:18 -03001039 if (!ir->flags.no_tx)
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001040 /* get the transmitter bitmask */
1041 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
Jarod Wilson66e89522010-06-01 17:32:08 -03001042
1043 /* get receiver timeout value */
1044 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
Jarod Wilson66e89522010-06-01 17:32:08 -03001045
1046 /* get receiver sensor setting */
1047 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
Jarod Wilson66e89522010-06-01 17:32:08 -03001048}
1049
David Härdemand8b4b582010-10-29 16:08:23 -03001050static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -03001051{
Jarod Wilson66e89522010-06-01 17:32:08 -03001052 struct device *dev = ir->dev;
David Härdemand8b4b582010-10-29 16:08:23 -03001053 struct rc_dev *rc;
1054 int ret;
Jarod Wilson66e89522010-06-01 17:32:08 -03001055
David Härdemand8b4b582010-10-29 16:08:23 -03001056 rc = rc_allocate_device();
1057 if (!rc) {
1058 dev_err(dev, "remote dev allocation failed\n");
1059 goto out;
Jarod Wilson66e89522010-06-01 17:32:08 -03001060 }
1061
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001062 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
David Härdemand8b4b582010-10-29 16:08:23 -03001063 mceusb_model[ir->model].name ?
Paul Bender5ad1a552010-11-17 16:56:17 -03001064 mceusb_model[ir->model].name :
David Härdemand8b4b582010-10-29 16:08:23 -03001065 "Media Center Ed. eHome Infrared Remote Transceiver",
Jarod Wilson66e89522010-06-01 17:32:08 -03001066 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1067 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1068
Jarod Wilson66e89522010-06-01 17:32:08 -03001069 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
Jarod Wilson66e89522010-06-01 17:32:08 -03001070
David Härdemand8b4b582010-10-29 16:08:23 -03001071 rc->input_name = ir->name;
1072 rc->input_phys = ir->phys;
1073 usb_to_input_id(ir->usbdev, &rc->input_id);
1074 rc->dev.parent = dev;
1075 rc->priv = ir;
1076 rc->driver_type = RC_DRIVER_IR_RAW;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001077 rc->allowed_protos = RC_TYPE_ALL;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -03001078 rc->timeout = US_TO_NS(1000);
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001079 if (!ir->flags.no_tx) {
David Härdemand8b4b582010-10-29 16:08:23 -03001080 rc->s_tx_mask = mceusb_set_tx_mask;
1081 rc->s_tx_carrier = mceusb_set_tx_carrier;
1082 rc->tx_ir = mceusb_tx_ir;
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001083 }
David Härdemand8b4b582010-10-29 16:08:23 -03001084 rc->driver_name = DRIVER_NAME;
1085 rc->map_name = mceusb_model[ir->model].rc_map ?
1086 mceusb_model[ir->model].rc_map : RC_MAP_RC6_MCE;
Jarod Wilson66e89522010-06-01 17:32:08 -03001087
David Härdemand8b4b582010-10-29 16:08:23 -03001088 ret = rc_register_device(rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001089 if (ret < 0) {
David Härdemand8b4b582010-10-29 16:08:23 -03001090 dev_err(dev, "remote dev registration failed\n");
1091 goto out;
Jarod Wilson66e89522010-06-01 17:32:08 -03001092 }
1093
David Härdemand8b4b582010-10-29 16:08:23 -03001094 return rc;
Jarod Wilson66e89522010-06-01 17:32:08 -03001095
David Härdemand8b4b582010-10-29 16:08:23 -03001096out:
1097 rc_free_device(rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001098 return NULL;
1099}
1100
1101static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1102 const struct usb_device_id *id)
1103{
1104 struct usb_device *dev = interface_to_usbdev(intf);
1105 struct usb_host_interface *idesc;
1106 struct usb_endpoint_descriptor *ep = NULL;
1107 struct usb_endpoint_descriptor *ep_in = NULL;
1108 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001109 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -03001110 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -03001111 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001112 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001113 bool is_gen3;
1114 bool is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001115 bool tx_mask_normal;
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001116 int ir_intfnum;
Jarod Wilson66e89522010-06-01 17:32:08 -03001117
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -03001118 mce_dbg(&intf->dev, "%s called\n", __func__);
Jarod Wilson66e89522010-06-01 17:32:08 -03001119
Jarod Wilson66e89522010-06-01 17:32:08 -03001120 idesc = intf->cur_altsetting;
1121
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001122 is_gen3 = mceusb_model[model].mce_gen3;
1123 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001124 tx_mask_normal = mceusb_model[model].tx_mask_normal;
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001125 ir_intfnum = mceusb_model[model].ir_intfnum;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001126
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001127 /* There are multi-function devices with non-IR interfaces */
1128 if (idesc->desc.bInterfaceNumber != ir_intfnum)
1129 return -ENODEV;
Jarod Wilson66e89522010-06-01 17:32:08 -03001130
1131 /* step through the endpoints to find first bulk in and out endpoint */
1132 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1133 ep = &idesc->endpoint[i].desc;
1134
1135 if ((ep_in == NULL)
1136 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1137 == USB_DIR_IN)
1138 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1139 == USB_ENDPOINT_XFER_BULK)
1140 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1141 == USB_ENDPOINT_XFER_INT))) {
1142
Jarod Wilson66e89522010-06-01 17:32:08 -03001143 ep_in = ep;
1144 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001145 ep_in->bInterval = 1;
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -03001146 mce_dbg(&intf->dev, "acceptable inbound endpoint "
Jarod Wilsond732a722010-06-16 17:10:46 -03001147 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001148 }
1149
1150 if ((ep_out == NULL)
1151 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1152 == USB_DIR_OUT)
1153 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1154 == USB_ENDPOINT_XFER_BULK)
1155 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1156 == USB_ENDPOINT_XFER_INT))) {
1157
Jarod Wilson66e89522010-06-01 17:32:08 -03001158 ep_out = ep;
1159 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001160 ep_out->bInterval = 1;
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -03001161 mce_dbg(&intf->dev, "acceptable outbound endpoint "
Jarod Wilsond732a722010-06-16 17:10:46 -03001162 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001163 }
1164 }
1165 if (ep_in == NULL) {
Jarod Wilson5ae8f9a2011-05-26 15:51:11 -03001166 mce_dbg(&intf->dev, "inbound and/or endpoint not found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001167 return -ENODEV;
1168 }
1169
1170 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1171 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1172
1173 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1174 if (!ir)
1175 goto mem_alloc_fail;
1176
1177 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1178 if (!ir->buf_in)
1179 goto buf_in_alloc_fail;
1180
1181 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1182 if (!ir->urb_in)
1183 goto urb_in_alloc_fail;
1184
1185 ir->usbdev = dev;
1186 ir->dev = &intf->dev;
1187 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001188 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001189 ir->flags.tx_mask_normal = tx_mask_normal;
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001190 ir->flags.no_tx = mceusb_model[model].no_tx;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001191 ir->model = model;
1192
Jarod Wilson66e89522010-06-01 17:32:08 -03001193 /* Saving usb interface data for use by the transmitter routine */
1194 ir->usb_ep_in = ep_in;
1195 ir->usb_ep_out = ep_out;
1196
1197 if (dev->descriptor.iManufacturer
1198 && usb_string(dev, dev->descriptor.iManufacturer,
1199 buf, sizeof(buf)) > 0)
1200 strlcpy(name, buf, sizeof(name));
1201 if (dev->descriptor.iProduct
1202 && usb_string(dev, dev->descriptor.iProduct,
1203 buf, sizeof(buf)) > 0)
1204 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1205 " %s", buf);
1206
David Härdemand8b4b582010-10-29 16:08:23 -03001207 ir->rc = mceusb_init_rc_dev(ir);
1208 if (!ir->rc)
1209 goto rc_dev_fail;
Jarod Wilson66e89522010-06-01 17:32:08 -03001210
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001211 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001212 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1213 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1214 ir->urb_in->transfer_dma = ir->dma_in;
1215 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1216
Jarod Wilson3a918aa2011-05-26 14:23:18 -03001217 /* flush buffers on the device */
1218 mce_dbg(&intf->dev, "Flushing receive buffers\n");
1219 mce_flush_rx_buffer(ir, maxp);
1220
Jarod Wilson66e89522010-06-01 17:32:08 -03001221 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001222 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001223 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001224 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001225 mceusb_gen2_init(ir);
1226
Jarod Wilson22b07662010-07-08 12:38:57 -03001227 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001228
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001229 if (!ir->flags.no_tx)
David Härdemand8b4b582010-10-29 16:08:23 -03001230 mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001231
1232 usb_set_intfdata(intf, ir);
1233
1234 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1235 dev->bus->busnum, dev->devnum);
1236
1237 return 0;
1238
1239 /* Error-handling path */
David Härdemand8b4b582010-10-29 16:08:23 -03001240rc_dev_fail:
Jarod Wilson66e89522010-06-01 17:32:08 -03001241 usb_free_urb(ir->urb_in);
1242urb_in_alloc_fail:
1243 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1244buf_in_alloc_fail:
1245 kfree(ir);
1246mem_alloc_fail:
1247 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1248
1249 return -ENOMEM;
1250}
1251
1252
1253static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1254{
1255 struct usb_device *dev = interface_to_usbdev(intf);
1256 struct mceusb_dev *ir = usb_get_intfdata(intf);
1257
1258 usb_set_intfdata(intf, NULL);
1259
1260 if (!ir)
1261 return;
1262
1263 ir->usbdev = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -03001264 rc_unregister_device(ir->rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001265 usb_kill_urb(ir->urb_in);
1266 usb_free_urb(ir->urb_in);
1267 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1268
1269 kfree(ir);
1270}
1271
1272static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1273{
1274 struct mceusb_dev *ir = usb_get_intfdata(intf);
1275 dev_info(ir->dev, "suspend\n");
1276 usb_kill_urb(ir->urb_in);
1277 return 0;
1278}
1279
1280static int mceusb_dev_resume(struct usb_interface *intf)
1281{
1282 struct mceusb_dev *ir = usb_get_intfdata(intf);
1283 dev_info(ir->dev, "resume\n");
1284 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1285 return -EIO;
1286 return 0;
1287}
1288
1289static struct usb_driver mceusb_dev_driver = {
1290 .name = DRIVER_NAME,
1291 .probe = mceusb_dev_probe,
1292 .disconnect = mceusb_dev_disconnect,
1293 .suspend = mceusb_dev_suspend,
1294 .resume = mceusb_dev_resume,
1295 .reset_resume = mceusb_dev_resume,
1296 .id_table = mceusb_dev_table
1297};
1298
1299static int __init mceusb_dev_init(void)
1300{
1301 int ret;
1302
1303 ret = usb_register(&mceusb_dev_driver);
1304 if (ret < 0)
1305 printk(KERN_ERR DRIVER_NAME
1306 ": usb register failed, result = %d\n", ret);
1307
1308 return ret;
1309}
1310
1311static void __exit mceusb_dev_exit(void)
1312{
1313 usb_deregister(&mceusb_dev_driver);
1314}
1315
1316module_init(mceusb_dev_init);
1317module_exit(mceusb_dev_exit);
1318
1319MODULE_DESCRIPTION(DRIVER_DESC);
1320MODULE_AUTHOR(DRIVER_AUTHOR);
1321MODULE_LICENSE("GPL");
1322MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1323
1324module_param(debug, bool, S_IRUGO | S_IWUSR);
1325MODULE_PARM_DESC(debug, "Debug enabled or not");