blob: 9145be9c0f04cd6756b9ab61621abcad908671ca [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
111/* general constants */
112#define SEND_FLAG_IN_PROGRESS 1
113#define SEND_FLAG_COMPLETE 2
114#define RECV_FLAG_IN_PROGRESS 3
115#define RECV_FLAG_COMPLETE 4
116
117#define MCEUSB_RX 1
118#define MCEUSB_TX 2
119
120#define VENDOR_PHILIPS 0x0471
121#define VENDOR_SMK 0x0609
122#define VENDOR_TATUNG 0x1460
123#define VENDOR_GATEWAY 0x107b
124#define VENDOR_SHUTTLE 0x1308
125#define VENDOR_SHUTTLE2 0x051c
126#define VENDOR_MITSUMI 0x03ee
127#define VENDOR_TOPSEED 0x1784
128#define VENDOR_RICAVISION 0x179d
129#define VENDOR_ITRON 0x195d
130#define VENDOR_FIC 0x1509
131#define VENDOR_LG 0x043e
132#define VENDOR_MICROSOFT 0x045e
133#define VENDOR_FORMOSA 0x147a
134#define VENDOR_FINTEK 0x1934
135#define VENDOR_PINNACLE 0x2304
136#define VENDOR_ECS 0x1019
137#define VENDOR_WISTRON 0x0fb8
138#define VENDOR_COMPRO 0x185b
139#define VENDOR_NORTHSTAR 0x04eb
140#define VENDOR_REALTEK 0x0bda
141#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300142#define VENDOR_CONEXANT 0x0572
Jarod Wilson66e89522010-06-01 17:32:08 -0300143
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300144enum mceusb_model_type {
145 MCE_GEN2 = 0, /* Most boards */
146 MCE_GEN1,
147 MCE_GEN3,
148 MCE_GEN2_TX_INV,
149 POLARIS_EVK,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300150 CX_HYBRID_TV,
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300151 MULTIFUNCTION,
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300152 TIVO_KIT,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300153};
154
155struct mceusb_model {
156 u32 mce_gen1:1;
157 u32 mce_gen2:1;
158 u32 mce_gen3:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300159 u32 tx_mask_normal:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300160 u32 no_tx:1;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300161
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300162 int ir_intfnum;
163
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300164 const char *rc_map; /* Allow specify a per-board map */
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300165 const char *name; /* per-board name */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300166};
167
168static const struct mceusb_model mceusb_model[] = {
169 [MCE_GEN1] = {
170 .mce_gen1 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300171 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300172 },
173 [MCE_GEN2] = {
174 .mce_gen2 = 1,
175 },
176 [MCE_GEN2_TX_INV] = {
177 .mce_gen2 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300178 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300179 },
180 [MCE_GEN3] = {
181 .mce_gen3 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300182 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300183 },
184 [POLARIS_EVK] = {
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300185 /*
186 * In fact, the EVK is shipped without
187 * remotes, but we should have something handy,
188 * to allow testing it
189 */
Mauro Carvalho Chehab15195d32011-01-24 12:18:47 -0300190 .rc_map = RC_MAP_HAUPPAUGE,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300191 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
192 },
193 [CX_HYBRID_TV] = {
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300194 .no_tx = 1, /* tx isn't wired up at all */
195 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300196 },
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300197 [MULTIFUNCTION] = {
198 .mce_gen2 = 1,
199 .ir_intfnum = 2,
200 },
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300201 [TIVO_KIT] = {
202 .mce_gen2 = 1,
203 .rc_map = RC_MAP_TIVO,
204 },
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300205};
206
Jarod Wilson66e89522010-06-01 17:32:08 -0300207static struct usb_device_id mceusb_dev_table[] = {
208 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300209 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
210 .driver_info = MCE_GEN1 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300211 /* Philips Infrared Transceiver - Sahara branded */
212 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
213 /* Philips Infrared Transceiver - HP branded */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300214 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
215 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300216 /* Philips SRM5100 */
217 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
218 /* Philips Infrared Transceiver - Omaura */
219 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
220 /* Philips Infrared Transceiver - Spinel plus */
221 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
222 /* Philips eHome Infrared Transceiver */
223 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300224 /* Philips/Spinel plus IR transceiver for ASUS */
225 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
226 /* Philips/Spinel plus IR transceiver for ASUS */
227 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilsone296e122011-04-25 14:48:18 -0300228 /* Philips IR transceiver (Dell branded) */
229 { USB_DEVICE(VENDOR_PHILIPS, 0x2093) },
Jarod Wilsona6994eb2011-03-01 12:38:28 -0300230 /* Realtek MCE IR Receiver and card reader */
231 { USB_DEVICE(VENDOR_REALTEK, 0x0161),
232 .driver_info = MULTIFUNCTION },
Jarod Wilson66e89522010-06-01 17:32:08 -0300233 /* SMK/Toshiba G83C0004D410 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300234 { USB_DEVICE(VENDOR_SMK, 0x031d),
235 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300236 /* SMK eHome Infrared Transceiver (Sony VAIO) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300237 { USB_DEVICE(VENDOR_SMK, 0x0322),
238 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300239 /* bundled with Hauppauge PVR-150 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300240 { USB_DEVICE(VENDOR_SMK, 0x0334),
241 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300242 /* SMK eHome Infrared Transceiver */
243 { USB_DEVICE(VENDOR_SMK, 0x0338) },
244 /* Tatung eHome Infrared Transceiver */
245 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
246 /* Shuttle eHome Infrared Transceiver */
247 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
248 /* Shuttle eHome Infrared Transceiver */
249 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
250 /* Gateway eHome Infrared Transceiver */
251 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
252 /* Mitsumi */
253 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
254 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300255 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
256 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300257 /* Topseed HP eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300258 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
259 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300260 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300261 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
262 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300263 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300264 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
265 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300266 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300267 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
268 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300269 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300270 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
Jarod Wilson7d9a46f2011-03-04 20:20:47 -0300271 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300272 /* Ricavision internal Infrared Transceiver */
273 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
274 /* Itron ione Libra Q-11 */
275 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
276 /* FIC eHome Infrared Transceiver */
277 { USB_DEVICE(VENDOR_FIC, 0x9242) },
278 /* LG eHome Infrared Transceiver */
279 { USB_DEVICE(VENDOR_LG, 0x9803) },
280 /* Microsoft MCE Infrared Transceiver */
281 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
282 /* Formosa eHome Infrared Transceiver */
283 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
284 /* Formosa21 / eHome Infrared Receiver */
285 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
286 /* Formosa aim / Trust MCE Infrared Receiver */
287 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
288 /* Formosa Industrial Computing / Beanbag Emulation Device */
289 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
290 /* Formosa21 / eHome Infrared Receiver */
291 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
292 /* Formosa Industrial Computing AIM IR605/A */
293 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
294 /* Formosa Industrial Computing */
295 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
Jarod Wilsonfbb1f1b2010-12-16 13:27:11 -0300296 /* Fintek eHome Infrared Transceiver (HP branded) */
297 { USB_DEVICE(VENDOR_FINTEK, 0x5168) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300298 /* Fintek eHome Infrared Transceiver */
299 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
300 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
301 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
302 /* Pinnacle Remote Kit */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300303 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
304 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300305 /* Elitegroup Computer Systems IR */
306 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
307 /* Wistron Corp. eHome Infrared Receiver */
308 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
309 /* Compro K100 */
310 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
311 /* Compro K100 v2 */
312 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
313 /* Northstar Systems, Inc. eHome Infrared Transceiver */
314 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
315 /* TiVo PC IR Receiver */
Jarod Wilsond8ee99e2011-03-24 12:59:10 -0300316 { USB_DEVICE(VENDOR_TIVO, 0x2000),
317 .driver_info = TIVO_KIT },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300318 /* Conexant Hybrid TV "Shelby" Polaris SDK */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300319 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
320 .driver_info = POLARIS_EVK },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300321 /* Conexant Hybrid TV RDU253S Polaris */
322 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
323 .driver_info = CX_HYBRID_TV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300324 /* Terminating entry */
325 { }
326};
327
Jarod Wilson66e89522010-06-01 17:32:08 -0300328/* data structure for each usb transceiver */
329struct mceusb_dev {
330 /* ir-core bits */
David Härdemand8b4b582010-10-29 16:08:23 -0300331 struct rc_dev *rc;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300332
333 /* optional features we can enable */
334 bool carrier_report_enabled;
335 bool learning_enabled;
Jarod Wilson66e89522010-06-01 17:32:08 -0300336
337 /* core device bits */
338 struct device *dev;
Jarod Wilson66e89522010-06-01 17:32:08 -0300339
340 /* usb */
341 struct usb_device *usbdev;
342 struct urb *urb_in;
343 struct usb_endpoint_descriptor *usb_ep_in;
344 struct usb_endpoint_descriptor *usb_ep_out;
345
346 /* buffers and dma */
347 unsigned char *buf_in;
348 unsigned int len_in;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300349 dma_addr_t dma_in;
350 dma_addr_t dma_out;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300351
352 enum {
353 CMD_HEADER = 0,
354 SUBCMD,
355 CMD_DATA,
356 PARSE_IRDATA,
357 } parser_state;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300358
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300359 u8 cmd, rem; /* Remaining IR data bytes in packet */
Jarod Wilson66e89522010-06-01 17:32:08 -0300360
361 struct {
362 u32 connected:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300363 u32 tx_mask_normal:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300364 u32 microsoft_gen1:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300365 u32 no_tx:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300366 } flags;
367
Jarod Wilsone23fb962010-06-16 17:55:52 -0300368 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300369 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300370 u32 carrier;
371 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300372
373 char name[128];
374 char phys[64];
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300375 enum mceusb_model_type model;
Jarod Wilson66e89522010-06-01 17:32:08 -0300376};
377
378/*
379 * MCE Device Command Strings
380 * Device command responses vary from device to device...
381 * - DEVICE_RESET resets the hardware to its default state
382 * - GET_REVISION fetches the hardware/software revision, common
383 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
384 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
385 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
386 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
387 * ((clk / frequency) - 1)
388 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
389 * response in the form of 9f 0c msb lsb
390 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
391 * the form of 9f 08 bm, where bm is the bitmask
392 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
393 * general use one or short-range learning one, in the form of
394 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
395 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
396 * - SET_TX_BITMASK sets the transmitter bitmask
397 * - SET_RX_TIMEOUT sets the receiver timeout
398 * - SET_RX_SENSOR sets which receiver sensor to use
399 */
Jarod Wilson4a883912010-10-22 14:42:54 -0300400static char DEVICE_RESET[] = {MCE_COMMAND_NULL, MCE_HW_CMD_HEADER,
401 MCE_CMD_DEVICE_RESET};
402static char GET_REVISION[] = {MCE_HW_CMD_HEADER, MCE_CMD_G_REVISION};
403static char GET_UNKNOWN[] = {MCE_HW_CMD_HEADER, MCE_CMD_UNKNOWN7};
404static char GET_UNKNOWN2[] = {MCE_COMMAND_HEADER, MCE_CMD_UNKNOWN2};
405static char GET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER, MCE_CMD_G_CARRIER};
406static char GET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TIMEOUT};
407static char GET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TXMASK};
408static char GET_RX_SENSOR[] = {MCE_COMMAND_HEADER, MCE_CMD_G_RXSENSOR};
Jarod Wilson66e89522010-06-01 17:32:08 -0300409/* sub in desired values in lower byte or bytes for full command */
410/* FIXME: make use of these for transmit.
Jarod Wilson4a883912010-10-22 14:42:54 -0300411static char SET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER,
412 MCE_CMD_S_CARRIER, 0x00, 0x00};
413static char SET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_S_TXMASK, 0x00};
414static char SET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER,
415 MCE_CMD_S_TIMEOUT, 0x00, 0x00};
416static char SET_RX_SENSOR[] = {MCE_COMMAND_HEADER,
417 MCE_CMD_S_RXSENSOR, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300418*/
419
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300420static int mceusb_cmdsize(u8 cmd, u8 subcmd)
421{
422 int datasize = 0;
423
424 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300425 case MCE_COMMAND_NULL:
426 if (subcmd == MCE_HW_CMD_HEADER)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300427 datasize = 1;
428 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300429 case MCE_HW_CMD_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300430 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300431 case MCE_CMD_G_REVISION:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300432 datasize = 2;
433 break;
434 }
Jarod Wilson4a883912010-10-22 14:42:54 -0300435 case MCE_COMMAND_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300436 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300437 case MCE_CMD_UNKNOWN:
438 case MCE_CMD_S_CARRIER:
439 case MCE_CMD_S_TIMEOUT:
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300440 case MCE_RSP_PULSE_COUNT:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300441 datasize = 2;
442 break;
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300443 case MCE_CMD_SIG_END:
Jarod Wilson4a883912010-10-22 14:42:54 -0300444 case MCE_CMD_S_TXMASK:
445 case MCE_CMD_S_RXSENSOR:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300446 datasize = 1;
447 break;
448 }
449 }
450 return datasize;
451}
452
Jarod Wilson66e89522010-06-01 17:32:08 -0300453static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300454 int offset, int len, bool out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300455{
456 char codes[USB_BUFLEN * 3 + 1];
457 char inout[9];
Jarod Wilson66e89522010-06-01 17:32:08 -0300458 u8 cmd, subcmd, data1, data2;
459 struct device *dev = ir->dev;
Jarod Wilson36e9d262010-10-22 16:49:35 -0300460 int i, start, skip = 0;
461
462 if (!debug)
463 return;
Jarod Wilson66e89522010-06-01 17:32:08 -0300464
Jarod Wilson657290b2010-06-16 17:10:05 -0300465 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
Jarod Wilson29b44942010-11-09 18:41:46 -0300466 if (ir->flags.microsoft_gen1 && !out && !offset)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300467 skip = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300468
Jarod Wilson36e9d262010-10-22 16:49:35 -0300469 if (len <= skip)
Jarod Wilson66e89522010-06-01 17:32:08 -0300470 return;
471
472 for (i = 0; i < len && i < USB_BUFLEN; i++)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300473 snprintf(codes + i * 3, 4, "%02x ", buf[i + offset] & 0xff);
Jarod Wilson66e89522010-06-01 17:32:08 -0300474
Jarod Wilson36e9d262010-10-22 16:49:35 -0300475 dev_info(dev, "%sx data: %s(length=%d)\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300476 (out ? "t" : "r"), codes, len);
477
478 if (out)
479 strcpy(inout, "Request\0");
480 else
481 strcpy(inout, "Got\0");
482
Jarod Wilson36e9d262010-10-22 16:49:35 -0300483 start = offset + skip;
484 cmd = buf[start] & 0xff;
485 subcmd = buf[start + 1] & 0xff;
486 data1 = buf[start + 2] & 0xff;
487 data2 = buf[start + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300488
489 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300490 case MCE_COMMAND_NULL:
491 if ((subcmd == MCE_HW_CMD_HEADER) &&
492 (data1 == MCE_CMD_DEVICE_RESET))
Jarod Wilson66e89522010-06-01 17:32:08 -0300493 dev_info(dev, "Device reset requested\n");
494 else
495 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
496 cmd, subcmd);
497 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300498 case MCE_HW_CMD_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300499 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300500 case MCE_CMD_G_REVISION:
Jarod Wilson66e89522010-06-01 17:32:08 -0300501 if (len == 2)
502 dev_info(dev, "Get hw/sw rev?\n");
503 else
504 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
505 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300506 buf[start + 4], buf[start + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300507 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300508 case MCE_CMD_DEVICE_RESET:
Jarod Wilson66e89522010-06-01 17:32:08 -0300509 dev_info(dev, "Device reset requested\n");
510 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300511 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300512 dev_info(dev, "Previous command not supported\n");
513 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300514 case MCE_CMD_UNKNOWN7:
515 case MCE_CMD_UNKNOWN9:
Jarod Wilson66e89522010-06-01 17:32:08 -0300516 default:
517 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
518 cmd, subcmd);
519 break;
520 }
521 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300522 case MCE_COMMAND_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300523 switch (subcmd) {
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300524 case MCE_CMD_SIG_END:
525 dev_info(dev, "End of signal\n");
526 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300527 case MCE_CMD_PING:
Jarod Wilson66e89522010-06-01 17:32:08 -0300528 dev_info(dev, "Ping\n");
529 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300530 case MCE_CMD_UNKNOWN:
Jarod Wilson66e89522010-06-01 17:32:08 -0300531 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
532 data1, data2);
533 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300534 case MCE_CMD_S_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300535 dev_info(dev, "%s carrier mode and freq of "
536 "0x%02x 0x%02x\n", inout, data1, data2);
537 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300538 case MCE_CMD_G_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300539 dev_info(dev, "Get carrier mode and freq\n");
540 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300541 case MCE_CMD_S_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300542 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
543 inout, data1);
544 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300545 case MCE_CMD_S_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300546 /* value is in units of 50us, so x*50/100 or x/2 ms */
547 dev_info(dev, "%s receive timeout of %d ms\n",
548 inout, ((data1 << 8) | data2) / 2);
549 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300550 case MCE_CMD_G_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300551 dev_info(dev, "Get receive timeout\n");
552 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300553 case MCE_CMD_G_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300554 dev_info(dev, "Get transmit blaster mask\n");
555 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300556 case MCE_CMD_S_RXSENSOR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300557 dev_info(dev, "%s %s-range receive sensor in use\n",
558 inout, data1 == 0x02 ? "short" : "long");
559 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300560 case MCE_CMD_G_RXSENSOR:
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300561 /* aka MCE_RSP_PULSE_COUNT */
562 if (out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300563 dev_info(dev, "Get receive sensor\n");
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300564 else if (ir->learning_enabled)
565 dev_info(dev, "RX pulse count: %d\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300566 ((data1 << 8) | data2));
567 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300568 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300569 dev_info(dev, "Error! Hardware is likely wedged...\n");
570 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300571 case MCE_CMD_UNKNOWN2:
572 case MCE_CMD_UNKNOWN3:
573 case MCE_CMD_UNKNOWN5:
Jarod Wilson66e89522010-06-01 17:32:08 -0300574 default:
575 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
576 cmd, subcmd);
577 break;
578 }
579 break;
580 default:
581 break;
582 }
Jarod Wilson36e9d262010-10-22 16:49:35 -0300583
584 if (cmd == MCE_IRDATA_TRAILER)
585 dev_info(dev, "End of raw IR data\n");
586 else if ((cmd != MCE_COMMAND_HEADER) &&
587 ((cmd & MCE_COMMAND_MASK) == MCE_COMMAND_IRDATA))
588 dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem);
Jarod Wilson66e89522010-06-01 17:32:08 -0300589}
590
Jarod Wilson7c294402010-08-02 18:21:06 -0300591static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300592{
593 struct mceusb_dev *ir;
594 int len;
595
596 if (!urb)
597 return;
598
599 ir = urb->context;
600 if (ir) {
601 len = urb->actual_length;
602
603 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
604 urb->status, len);
605
Jarod Wilson36e9d262010-10-22 16:49:35 -0300606 mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
Jarod Wilson66e89522010-06-01 17:32:08 -0300607 }
608
609}
610
611/* request incoming or send outgoing usb packet - used to initialize remote */
612static void mce_request_packet(struct mceusb_dev *ir,
613 struct usb_endpoint_descriptor *ep,
614 unsigned char *data, int size, int urb_type)
615{
616 int res;
617 struct urb *async_urb;
618 struct device *dev = ir->dev;
619 unsigned char *async_buf;
620
621 if (urb_type == MCEUSB_TX) {
622 async_urb = usb_alloc_urb(0, GFP_KERNEL);
623 if (unlikely(!async_urb)) {
624 dev_err(dev, "Error, couldn't allocate urb!\n");
625 return;
626 }
627
628 async_buf = kzalloc(size, GFP_KERNEL);
629 if (!async_buf) {
630 dev_err(dev, "Error, couldn't allocate buf!\n");
631 usb_free_urb(async_urb);
632 return;
633 }
634
635 /* outbound data */
636 usb_fill_int_urb(async_urb, ir->usbdev,
637 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300638 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300639 ir, ep->bInterval);
640 memcpy(async_buf, data, size);
641
642 } else if (urb_type == MCEUSB_RX) {
643 /* standard request */
644 async_urb = ir->urb_in;
645 ir->send_flags = RECV_FLAG_IN_PROGRESS;
646
647 } else {
648 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
649 return;
650 }
651
652 dev_dbg(dev, "receive request called (size=%#x)\n", size);
653
654 async_urb->transfer_buffer_length = size;
655 async_urb->dev = ir->usbdev;
656
657 res = usb_submit_urb(async_urb, GFP_ATOMIC);
658 if (res) {
659 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
660 return;
661 }
662 dev_dbg(dev, "receive request complete (res=%d)\n", res);
663}
664
665static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
666{
667 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
668}
669
670static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
671{
672 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
673}
674
Jarod Wilsone23fb962010-06-16 17:55:52 -0300675/* Send data out the IR blaster port(s) */
David Härdemand8b4b582010-10-29 16:08:23 -0300676static int mceusb_tx_ir(struct rc_dev *dev, int *txbuf, u32 n)
Jarod Wilsone23fb962010-06-16 17:55:52 -0300677{
David Härdemand8b4b582010-10-29 16:08:23 -0300678 struct mceusb_dev *ir = dev->priv;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300679 int i, ret = 0;
680 int count, cmdcount = 0;
681 unsigned char *cmdbuf; /* MCE command buffer */
682 long signal_duration = 0; /* Singnal length in us */
683 struct timeval start_time, end_time;
684
685 do_gettimeofday(&start_time);
686
687 count = n / sizeof(int);
688
689 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
690 if (!cmdbuf)
691 return -ENOMEM;
692
693 /* MCE tx init header */
Jarod Wilson4a883912010-10-22 14:42:54 -0300694 cmdbuf[cmdcount++] = MCE_COMMAND_HEADER;
695 cmdbuf[cmdcount++] = MCE_CMD_S_TXMASK;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300696 cmdbuf[cmdcount++] = ir->tx_mask;
697
698 /* Generate mce packet data */
699 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
700 signal_duration += txbuf[i];
701 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
702
703 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
704
705 /* Insert mce packet header every 4th entry */
706 if ((cmdcount < MCE_CMDBUF_SIZE) &&
707 (cmdcount - MCE_TX_HEADER_LENGTH) %
708 MCE_CODE_LENGTH == 0)
Jarod Wilson4a883912010-10-22 14:42:54 -0300709 cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300710
711 /* Insert mce packet data */
712 if (cmdcount < MCE_CMDBUF_SIZE)
713 cmdbuf[cmdcount++] =
714 (txbuf[i] < MCE_PULSE_BIT ?
715 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
716 (i & 1 ? 0x00 : MCE_PULSE_BIT);
717 else {
718 ret = -EINVAL;
719 goto out;
720 }
721
722 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
723 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
724 }
725
726 /* Fix packet length in last header */
727 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
Jarod Wilson4a883912010-10-22 14:42:54 -0300728 MCE_COMMAND_IRDATA + (cmdcount - MCE_TX_HEADER_LENGTH) %
729 MCE_CODE_LENGTH - 1;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300730
731 /* Check if we have room for the empty packet at the end */
732 if (cmdcount >= MCE_CMDBUF_SIZE) {
733 ret = -EINVAL;
734 goto out;
735 }
736
737 /* All mce commands end with an empty packet (0x80) */
Jarod Wilson4a883912010-10-22 14:42:54 -0300738 cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300739
740 /* Transmit the command to the mce device */
741 mce_async_out(ir, cmdbuf, cmdcount);
742
743 /*
744 * The lircd gap calculation expects the write function to
745 * wait the time it takes for the ircommand to be sent before
746 * it returns.
747 */
748 do_gettimeofday(&end_time);
749 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
750 (end_time.tv_sec - start_time.tv_sec) * 1000000;
751
752 /* delay with the closest number of ticks */
753 set_current_state(TASK_INTERRUPTIBLE);
754 schedule_timeout(usecs_to_jiffies(signal_duration));
755
756out:
757 kfree(cmdbuf);
758 return ret ? ret : n;
759}
760
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300761/* Sets active IR outputs -- mce devices typically have two */
David Härdemand8b4b582010-10-29 16:08:23 -0300762static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
Jarod Wilson657290b2010-06-16 17:10:05 -0300763{
David Härdemand8b4b582010-10-29 16:08:23 -0300764 struct mceusb_dev *ir = dev->priv;
Jarod Wilson657290b2010-06-16 17:10:05 -0300765
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300766 if (ir->flags.tx_mask_normal)
767 ir->tx_mask = mask;
768 else
Jarod Wilson4a883912010-10-22 14:42:54 -0300769 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
770 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300771
772 return 0;
773}
774
Jarod Wilsone23fb962010-06-16 17:55:52 -0300775/* Sets the send carrier frequency and mode */
David Härdemand8b4b582010-10-29 16:08:23 -0300776static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
Jarod Wilsone23fb962010-06-16 17:55:52 -0300777{
David Härdemand8b4b582010-10-29 16:08:23 -0300778 struct mceusb_dev *ir = dev->priv;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300779 int clk = 10000000;
780 int prescaler = 0, divisor = 0;
Jarod Wilson4a883912010-10-22 14:42:54 -0300781 unsigned char cmdbuf[4] = { MCE_COMMAND_HEADER,
782 MCE_CMD_S_CARRIER, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300783
784 /* Carrier has changed */
785 if (ir->carrier != carrier) {
786
787 if (carrier == 0) {
788 ir->carrier = carrier;
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300789 cmdbuf[2] = MCE_CMD_SIG_END;
Jarod Wilson4a883912010-10-22 14:42:54 -0300790 cmdbuf[3] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300791 dev_dbg(ir->dev, "%s: disabling carrier "
792 "modulation\n", __func__);
793 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
794 return carrier;
795 }
796
797 for (prescaler = 0; prescaler < 4; ++prescaler) {
798 divisor = (clk >> (2 * prescaler)) / carrier;
Jarod Wilson4a883912010-10-22 14:42:54 -0300799 if (divisor <= 0xff) {
Jarod Wilsone23fb962010-06-16 17:55:52 -0300800 ir->carrier = carrier;
801 cmdbuf[2] = prescaler;
802 cmdbuf[3] = divisor;
803 dev_dbg(ir->dev, "%s: requesting %u HZ "
804 "carrier\n", __func__, carrier);
805
806 /* Transmit new carrier to mce device */
807 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
808 return carrier;
809 }
810 }
811
812 return -EINVAL;
813
814 }
815
816 return carrier;
817}
818
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300819/*
820 * We don't do anything but print debug spew for many of the command bits
821 * we receive from the hardware, but some of them are useful information
822 * we want to store so that we can use them.
823 */
824static void mceusb_handle_command(struct mceusb_dev *ir, int index)
825{
826 u8 hi = ir->buf_in[index + 1] & 0xff;
827 u8 lo = ir->buf_in[index + 2] & 0xff;
828
829 switch (ir->buf_in[index]) {
830 /* 2-byte return value commands */
831 case MCE_CMD_S_TIMEOUT:
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300832 ir->rc->timeout = US_TO_NS((hi << 8 | lo) / 2);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300833 break;
834
835 /* 1-byte return value commands */
836 case MCE_CMD_S_TXMASK:
837 ir->tx_mask = hi;
838 break;
839 case MCE_CMD_S_RXSENSOR:
840 ir->learning_enabled = (hi == 0x02);
841 break;
842 default:
843 break;
844 }
845}
846
Jarod Wilson66e89522010-06-01 17:32:08 -0300847static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
848{
Maxim Levitsky46519182010-10-16 19:56:28 -0300849 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300850 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300851
852 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
853 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300854 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300855
Jarod Wilson29b44942010-11-09 18:41:46 -0300856 /* if there's no data, just return now */
857 if (buf_len <= i)
858 return;
859
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300860 for (; i < buf_len; i++) {
861 switch (ir->parser_state) {
862 case SUBCMD:
863 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
Jarod Wilson36e9d262010-10-22 16:49:35 -0300864 mceusb_dev_printdata(ir, ir->buf_in, i - 1,
865 ir->rem + 2, false);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300866 mceusb_handle_command(ir, i);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300867 ir->parser_state = CMD_DATA;
868 break;
869 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300870 ir->rem--;
Jarod Wilson5bd9d732011-01-26 12:20:09 -0300871 init_ir_raw_event(&rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -0300872 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
873 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
Jarod Wilsonb4608fa2011-01-18 17:31:24 -0300874 * US_TO_NS(MCE_TIME_UNIT);
Jarod Wilson66e89522010-06-01 17:32:08 -0300875
Jarod Wilson66e89522010-06-01 17:32:08 -0300876 dev_dbg(ir->dev, "Storing %s with duration %d\n",
877 rawir.pulse ? "pulse" : "space",
878 rawir.duration);
879
David Härdemand8b4b582010-10-29 16:08:23 -0300880 ir_raw_event_store_with_filter(ir->rc, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300881 break;
882 case CMD_DATA:
883 ir->rem--;
884 break;
885 case CMD_HEADER:
886 /* decode mce packets of the form (84),AA,BB,CC,DD */
887 /* IR data packets can span USB messages - rem */
888 ir->cmd = ir->buf_in[i];
Jarod Wilson4a883912010-10-22 14:42:54 -0300889 if ((ir->cmd == MCE_COMMAND_HEADER) ||
890 ((ir->cmd & MCE_COMMAND_MASK) !=
891 MCE_COMMAND_IRDATA)) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300892 ir->parser_state = SUBCMD;
893 continue;
894 }
895 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300896 mceusb_dev_printdata(ir, ir->buf_in,
897 i, ir->rem + 1, false);
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300898 if (ir->rem)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300899 ir->parser_state = PARSE_IRDATA;
Jarod Wilson5bd9d732011-01-26 12:20:09 -0300900 else
901 ir_raw_event_reset(ir->rc);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300902 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300903 }
904
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300905 if (ir->parser_state != CMD_HEADER && !ir->rem)
906 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300907 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300908 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
David Härdemand8b4b582010-10-29 16:08:23 -0300909 ir_raw_event_handle(ir->rc);
Jarod Wilson66e89522010-06-01 17:32:08 -0300910}
911
Jarod Wilson66e89522010-06-01 17:32:08 -0300912static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
913{
914 struct mceusb_dev *ir;
915 int buf_len;
916
917 if (!urb)
918 return;
919
920 ir = urb->context;
921 if (!ir) {
922 usb_unlink_urb(urb);
923 return;
924 }
925
926 buf_len = urb->actual_length;
927
Jarod Wilson66e89522010-06-01 17:32:08 -0300928 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
929 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300930 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300931 buf_len);
932 }
933
934 switch (urb->status) {
935 /* success */
936 case 0:
937 mceusb_process_ir_data(ir, buf_len);
938 break;
939
940 case -ECONNRESET:
941 case -ENOENT:
942 case -ESHUTDOWN:
943 usb_unlink_urb(urb);
944 return;
945
946 case -EPIPE:
947 default:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300948 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300949 break;
950 }
951
952 usb_submit_urb(urb, GFP_ATOMIC);
953}
954
955static void mceusb_gen1_init(struct mceusb_dev *ir)
956{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300957 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300958 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300959 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300960 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300961
962 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
963 if (!data) {
964 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300965 return;
966 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300967
968 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300969 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300970 * on the receive control pipe and expect a certain value pair back
971 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300972 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
973 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300974 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300975 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
976 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
977 __func__, data[0], data[1]);
978
979 /* set feature: bit rate 38400 bps */
980 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
981 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
982 0xc04e, 0x0000, NULL, 0, HZ * 3);
983
984 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
985
986 /* bRequest 4: set char length to 8 bits */
987 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
988 4, USB_TYPE_VENDOR,
989 0x0808, 0x0000, NULL, 0, HZ * 3);
990 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
991
992 /* bRequest 2: set handshaking to use DTR/DSR */
993 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
994 2, USB_TYPE_VENDOR,
995 0x0000, 0x0100, NULL, 0, HZ * 3);
996 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300997
Jarod Wilson22b07662010-07-08 12:38:57 -0300998 /* device reset */
999 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
1000 mce_sync_in(ir, NULL, maxp);
1001
1002 /* get hw/sw revision? */
1003 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
1004 mce_sync_in(ir, NULL, maxp);
1005
Jarod Wilson657290b2010-06-16 17:10:05 -03001006 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -03001007};
1008
1009static void mceusb_gen2_init(struct mceusb_dev *ir)
1010{
1011 int maxp = ir->len_in;
1012
Jarod Wilson66e89522010-06-01 17:32:08 -03001013 /* device reset */
1014 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
1015 mce_sync_in(ir, NULL, maxp);
1016
1017 /* get hw/sw revision? */
1018 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
1019 mce_sync_in(ir, NULL, maxp);
1020
Jarod Wilson22b07662010-07-08 12:38:57 -03001021 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -03001022 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
1023 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -03001024 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
1025 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -03001026}
1027
Jarod Wilson22b07662010-07-08 12:38:57 -03001028static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -03001029{
1030 int maxp = ir->len_in;
1031
Jarod Wilson66e89522010-06-01 17:32:08 -03001032 /* get the carrier and frequency */
1033 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
1034 mce_sync_in(ir, NULL, maxp);
1035
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001036 if (!ir->flags.no_tx) {
1037 /* get the transmitter bitmask */
1038 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
1039 mce_sync_in(ir, NULL, maxp);
1040 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001041
1042 /* get receiver timeout value */
1043 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
1044 mce_sync_in(ir, NULL, maxp);
1045
1046 /* get receiver sensor setting */
1047 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
1048 mce_sync_in(ir, NULL, maxp);
1049}
1050
David Härdemand8b4b582010-10-29 16:08:23 -03001051static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -03001052{
Jarod Wilson66e89522010-06-01 17:32:08 -03001053 struct device *dev = ir->dev;
David Härdemand8b4b582010-10-29 16:08:23 -03001054 struct rc_dev *rc;
1055 int ret;
Jarod Wilson66e89522010-06-01 17:32:08 -03001056
David Härdemand8b4b582010-10-29 16:08:23 -03001057 rc = rc_allocate_device();
1058 if (!rc) {
1059 dev_err(dev, "remote dev allocation failed\n");
1060 goto out;
Jarod Wilson66e89522010-06-01 17:32:08 -03001061 }
1062
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001063 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
David Härdemand8b4b582010-10-29 16:08:23 -03001064 mceusb_model[ir->model].name ?
Paul Bender5ad1a552010-11-17 16:56:17 -03001065 mceusb_model[ir->model].name :
David Härdemand8b4b582010-10-29 16:08:23 -03001066 "Media Center Ed. eHome Infrared Remote Transceiver",
Jarod Wilson66e89522010-06-01 17:32:08 -03001067 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1068 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1069
Jarod Wilson66e89522010-06-01 17:32:08 -03001070 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
Jarod Wilson66e89522010-06-01 17:32:08 -03001071
David Härdemand8b4b582010-10-29 16:08:23 -03001072 rc->input_name = ir->name;
1073 rc->input_phys = ir->phys;
1074 usb_to_input_id(ir->usbdev, &rc->input_id);
1075 rc->dev.parent = dev;
1076 rc->priv = ir;
1077 rc->driver_type = RC_DRIVER_IR_RAW;
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -03001078 rc->allowed_protos = RC_TYPE_ALL;
Jarod Wilsonb4608fa2011-01-18 17:31:24 -03001079 rc->timeout = US_TO_NS(1000);
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001080 if (!ir->flags.no_tx) {
David Härdemand8b4b582010-10-29 16:08:23 -03001081 rc->s_tx_mask = mceusb_set_tx_mask;
1082 rc->s_tx_carrier = mceusb_set_tx_carrier;
1083 rc->tx_ir = mceusb_tx_ir;
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001084 }
David Härdemand8b4b582010-10-29 16:08:23 -03001085 rc->driver_name = DRIVER_NAME;
1086 rc->map_name = mceusb_model[ir->model].rc_map ?
1087 mceusb_model[ir->model].rc_map : RC_MAP_RC6_MCE;
Jarod Wilson66e89522010-06-01 17:32:08 -03001088
David Härdemand8b4b582010-10-29 16:08:23 -03001089 ret = rc_register_device(rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001090 if (ret < 0) {
David Härdemand8b4b582010-10-29 16:08:23 -03001091 dev_err(dev, "remote dev registration failed\n");
1092 goto out;
Jarod Wilson66e89522010-06-01 17:32:08 -03001093 }
1094
David Härdemand8b4b582010-10-29 16:08:23 -03001095 return rc;
Jarod Wilson66e89522010-06-01 17:32:08 -03001096
David Härdemand8b4b582010-10-29 16:08:23 -03001097out:
1098 rc_free_device(rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001099 return NULL;
1100}
1101
1102static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1103 const struct usb_device_id *id)
1104{
1105 struct usb_device *dev = interface_to_usbdev(intf);
1106 struct usb_host_interface *idesc;
1107 struct usb_endpoint_descriptor *ep = NULL;
1108 struct usb_endpoint_descriptor *ep_in = NULL;
1109 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001110 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -03001111 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -03001112 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001113 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001114 bool is_gen3;
1115 bool is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001116 bool tx_mask_normal;
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001117 int ir_intfnum;
Jarod Wilson66e89522010-06-01 17:32:08 -03001118
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001119 dev_dbg(&intf->dev, "%s called\n", __func__);
Jarod Wilson66e89522010-06-01 17:32:08 -03001120
Jarod Wilson66e89522010-06-01 17:32:08 -03001121 idesc = intf->cur_altsetting;
1122
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001123 is_gen3 = mceusb_model[model].mce_gen3;
1124 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001125 tx_mask_normal = mceusb_model[model].tx_mask_normal;
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001126 ir_intfnum = mceusb_model[model].ir_intfnum;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001127
Jarod Wilsona6994eb2011-03-01 12:38:28 -03001128 /* There are multi-function devices with non-IR interfaces */
1129 if (idesc->desc.bInterfaceNumber != ir_intfnum)
1130 return -ENODEV;
Jarod Wilson66e89522010-06-01 17:32:08 -03001131
1132 /* step through the endpoints to find first bulk in and out endpoint */
1133 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1134 ep = &idesc->endpoint[i].desc;
1135
1136 if ((ep_in == NULL)
1137 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1138 == USB_DIR_IN)
1139 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1140 == USB_ENDPOINT_XFER_BULK)
1141 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1142 == USB_ENDPOINT_XFER_INT))) {
1143
Jarod Wilson66e89522010-06-01 17:32:08 -03001144 ep_in = ep;
1145 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001146 ep_in->bInterval = 1;
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001147 dev_dbg(&intf->dev, "acceptable inbound endpoint "
Jarod Wilsond732a722010-06-16 17:10:46 -03001148 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001149 }
1150
1151 if ((ep_out == NULL)
1152 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1153 == USB_DIR_OUT)
1154 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1155 == USB_ENDPOINT_XFER_BULK)
1156 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1157 == USB_ENDPOINT_XFER_INT))) {
1158
Jarod Wilson66e89522010-06-01 17:32:08 -03001159 ep_out = ep;
1160 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001161 ep_out->bInterval = 1;
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001162 dev_dbg(&intf->dev, "acceptable outbound endpoint "
Jarod Wilsond732a722010-06-16 17:10:46 -03001163 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001164 }
1165 }
1166 if (ep_in == NULL) {
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001167 dev_dbg(&intf->dev, "inbound and/or endpoint not found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001168 return -ENODEV;
1169 }
1170
1171 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1172 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1173
1174 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1175 if (!ir)
1176 goto mem_alloc_fail;
1177
1178 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1179 if (!ir->buf_in)
1180 goto buf_in_alloc_fail;
1181
1182 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1183 if (!ir->urb_in)
1184 goto urb_in_alloc_fail;
1185
1186 ir->usbdev = dev;
1187 ir->dev = &intf->dev;
1188 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001189 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001190 ir->flags.tx_mask_normal = tx_mask_normal;
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001191 ir->flags.no_tx = mceusb_model[model].no_tx;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001192 ir->model = model;
1193
Jarod Wilson66e89522010-06-01 17:32:08 -03001194 /* Saving usb interface data for use by the transmitter routine */
1195 ir->usb_ep_in = ep_in;
1196 ir->usb_ep_out = ep_out;
1197
1198 if (dev->descriptor.iManufacturer
1199 && usb_string(dev, dev->descriptor.iManufacturer,
1200 buf, sizeof(buf)) > 0)
1201 strlcpy(name, buf, sizeof(name));
1202 if (dev->descriptor.iProduct
1203 && usb_string(dev, dev->descriptor.iProduct,
1204 buf, sizeof(buf)) > 0)
1205 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1206 " %s", buf);
1207
David Härdemand8b4b582010-10-29 16:08:23 -03001208 ir->rc = mceusb_init_rc_dev(ir);
1209 if (!ir->rc)
1210 goto rc_dev_fail;
Jarod Wilson66e89522010-06-01 17:32:08 -03001211
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001212 /* flush buffers on the device */
1213 mce_sync_in(ir, NULL, maxp);
1214 mce_sync_in(ir, NULL, maxp);
1215
1216 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001217 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1218 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1219 ir->urb_in->transfer_dma = ir->dma_in;
1220 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1221
Jarod Wilson66e89522010-06-01 17:32:08 -03001222 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001223 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001224 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001225 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001226 mceusb_gen2_init(ir);
1227
Jarod Wilson22b07662010-07-08 12:38:57 -03001228 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001229
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001230 if (!ir->flags.no_tx)
David Härdemand8b4b582010-10-29 16:08:23 -03001231 mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001232
1233 usb_set_intfdata(intf, ir);
1234
1235 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1236 dev->bus->busnum, dev->devnum);
1237
1238 return 0;
1239
1240 /* Error-handling path */
David Härdemand8b4b582010-10-29 16:08:23 -03001241rc_dev_fail:
Jarod Wilson66e89522010-06-01 17:32:08 -03001242 usb_free_urb(ir->urb_in);
1243urb_in_alloc_fail:
1244 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1245buf_in_alloc_fail:
1246 kfree(ir);
1247mem_alloc_fail:
1248 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1249
1250 return -ENOMEM;
1251}
1252
1253
1254static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1255{
1256 struct usb_device *dev = interface_to_usbdev(intf);
1257 struct mceusb_dev *ir = usb_get_intfdata(intf);
1258
1259 usb_set_intfdata(intf, NULL);
1260
1261 if (!ir)
1262 return;
1263
1264 ir->usbdev = NULL;
David Härdemand8b4b582010-10-29 16:08:23 -03001265 rc_unregister_device(ir->rc);
Jarod Wilson66e89522010-06-01 17:32:08 -03001266 usb_kill_urb(ir->urb_in);
1267 usb_free_urb(ir->urb_in);
1268 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1269
1270 kfree(ir);
1271}
1272
1273static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1274{
1275 struct mceusb_dev *ir = usb_get_intfdata(intf);
1276 dev_info(ir->dev, "suspend\n");
1277 usb_kill_urb(ir->urb_in);
1278 return 0;
1279}
1280
1281static int mceusb_dev_resume(struct usb_interface *intf)
1282{
1283 struct mceusb_dev *ir = usb_get_intfdata(intf);
1284 dev_info(ir->dev, "resume\n");
1285 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1286 return -EIO;
1287 return 0;
1288}
1289
1290static struct usb_driver mceusb_dev_driver = {
1291 .name = DRIVER_NAME,
1292 .probe = mceusb_dev_probe,
1293 .disconnect = mceusb_dev_disconnect,
1294 .suspend = mceusb_dev_suspend,
1295 .resume = mceusb_dev_resume,
1296 .reset_resume = mceusb_dev_resume,
1297 .id_table = mceusb_dev_table
1298};
1299
1300static int __init mceusb_dev_init(void)
1301{
1302 int ret;
1303
1304 ret = usb_register(&mceusb_dev_driver);
1305 if (ret < 0)
1306 printk(KERN_ERR DRIVER_NAME
1307 ": usb register failed, result = %d\n", ret);
1308
1309 return ret;
1310}
1311
1312static void __exit mceusb_dev_exit(void)
1313{
1314 usb_deregister(&mceusb_dev_driver);
1315}
1316
1317module_init(mceusb_dev_init);
1318module_exit(mceusb_dev_exit);
1319
1320MODULE_DESCRIPTION(DRIVER_DESC);
1321MODULE_AUTHOR(DRIVER_AUTHOR);
1322MODULE_LICENSE("GPL");
1323MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1324
1325module_param(debug, bool, S_IRUGO | S_IWUSR);
1326MODULE_PARM_DESC(debug, "Debug enabled or not");