blob: 712f93f26093ed02598630b5ee3ed2af35a6ba52 [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>
38#include <linux/usb.h>
39#include <linux/input.h>
40#include <media/ir-core.h>
41#include <media/ir-common.h>
42
43#define DRIVER_VERSION "1.91"
44#define DRIVER_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
45#define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
46 "device driver"
47#define DRIVER_NAME "mceusb"
48
49#define USB_BUFLEN 32 /* USB reception buffer length */
Jarod Wilson657290b2010-06-16 17:10:05 -030050#define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
51#define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
Jarod Wilson66e89522010-06-01 17:32:08 -030052
53/* MCE constants */
54#define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
55#define MCE_TIME_UNIT 50 /* Approx 50us resolution */
56#define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
57#define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
58#define MCE_PACKET_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -030059#define MCE_CONTROL_HEADER 0x9f /* MCE status header */
Jarod Wilson66e89522010-06-01 17:32:08 -030060#define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
61#define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
62#define MCE_DEFAULT_TX_MASK 0x03 /* Val opts: TX1=0x01, TX2=0x02, ALL=0x03 */
63#define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -030064#define MCE_PULSE_MASK 0x7f /* Pulse mask */
65#define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
66#define MCE_COMMAND_MASK 0xe0 /* Mask out command bits */
67#define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
68#define MCE_COMMAND_IRDATA 0x80 /* buf & MCE_COMMAND_MASK == 0x80 -> IR data */
Jarod Wilson66e89522010-06-01 17:32:08 -030069
70
71/* module parameters */
72#ifdef CONFIG_USB_DEBUG
73static int debug = 1;
74#else
75static int debug;
76#endif
77
78/* general constants */
79#define SEND_FLAG_IN_PROGRESS 1
80#define SEND_FLAG_COMPLETE 2
81#define RECV_FLAG_IN_PROGRESS 3
82#define RECV_FLAG_COMPLETE 4
83
84#define MCEUSB_RX 1
85#define MCEUSB_TX 2
86
87#define VENDOR_PHILIPS 0x0471
88#define VENDOR_SMK 0x0609
89#define VENDOR_TATUNG 0x1460
90#define VENDOR_GATEWAY 0x107b
91#define VENDOR_SHUTTLE 0x1308
92#define VENDOR_SHUTTLE2 0x051c
93#define VENDOR_MITSUMI 0x03ee
94#define VENDOR_TOPSEED 0x1784
95#define VENDOR_RICAVISION 0x179d
96#define VENDOR_ITRON 0x195d
97#define VENDOR_FIC 0x1509
98#define VENDOR_LG 0x043e
99#define VENDOR_MICROSOFT 0x045e
100#define VENDOR_FORMOSA 0x147a
101#define VENDOR_FINTEK 0x1934
102#define VENDOR_PINNACLE 0x2304
103#define VENDOR_ECS 0x1019
104#define VENDOR_WISTRON 0x0fb8
105#define VENDOR_COMPRO 0x185b
106#define VENDOR_NORTHSTAR 0x04eb
107#define VENDOR_REALTEK 0x0bda
108#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300109#define VENDOR_CONEXANT 0x0572
Jarod Wilson66e89522010-06-01 17:32:08 -0300110
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300111enum mceusb_model_type {
112 MCE_GEN2 = 0, /* Most boards */
113 MCE_GEN1,
114 MCE_GEN3,
115 MCE_GEN2_TX_INV,
116 POLARIS_EVK,
117};
118
119struct mceusb_model {
120 u32 mce_gen1:1;
121 u32 mce_gen2:1;
122 u32 mce_gen3:1;
123 u32 tx_mask_inverted:1;
124 u32 is_polaris:1;
125
126 /*
127 * Allow specify a per-board extra data, like
128 * device names, and per-device rc_maps
129 */
130};
131
132static const struct mceusb_model mceusb_model[] = {
133 [MCE_GEN1] = {
134 .mce_gen1 = 1,
135 .tx_mask_inverted = 1,
136 },
137 [MCE_GEN2] = {
138 .mce_gen2 = 1,
139 },
140 [MCE_GEN2_TX_INV] = {
141 .mce_gen2 = 1,
142 .tx_mask_inverted = 1,
143 },
144 [MCE_GEN3] = {
145 .mce_gen3 = 1,
146 .tx_mask_inverted = 1,
147 },
148 [POLARIS_EVK] = {
149 .is_polaris = 1,
150 },
151};
152
Jarod Wilson66e89522010-06-01 17:32:08 -0300153static struct usb_device_id mceusb_dev_table[] = {
154 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300155 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
156 .driver_info = MCE_GEN1 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300157 /* Philips Infrared Transceiver - Sahara branded */
158 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
159 /* Philips Infrared Transceiver - HP branded */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300160 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
161 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300162 /* Philips SRM5100 */
163 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
164 /* Philips Infrared Transceiver - Omaura */
165 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
166 /* Philips Infrared Transceiver - Spinel plus */
167 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
168 /* Philips eHome Infrared Transceiver */
169 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300170 /* Philips/Spinel plus IR transceiver for ASUS */
171 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
172 /* Philips/Spinel plus IR transceiver for ASUS */
173 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300174 /* Realtek MCE IR Receiver */
175 { USB_DEVICE(VENDOR_REALTEK, 0x0161) },
176 /* SMK/Toshiba G83C0004D410 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300177 { USB_DEVICE(VENDOR_SMK, 0x031d),
178 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300179 /* SMK eHome Infrared Transceiver (Sony VAIO) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300180 { USB_DEVICE(VENDOR_SMK, 0x0322),
181 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300182 /* bundled with Hauppauge PVR-150 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300183 { USB_DEVICE(VENDOR_SMK, 0x0334),
184 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300185 /* SMK eHome Infrared Transceiver */
186 { USB_DEVICE(VENDOR_SMK, 0x0338) },
187 /* Tatung eHome Infrared Transceiver */
188 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
189 /* Shuttle eHome Infrared Transceiver */
190 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
191 /* Shuttle eHome Infrared Transceiver */
192 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
193 /* Gateway eHome Infrared Transceiver */
194 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
195 /* Mitsumi */
196 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
197 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300198 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
199 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300200 /* Topseed HP eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300201 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
202 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300203 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300204 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
205 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300206 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300207 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
208 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300209 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300210 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
211 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300212 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300213 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
214 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300215 /* Ricavision internal Infrared Transceiver */
216 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
217 /* Itron ione Libra Q-11 */
218 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
219 /* FIC eHome Infrared Transceiver */
220 { USB_DEVICE(VENDOR_FIC, 0x9242) },
221 /* LG eHome Infrared Transceiver */
222 { USB_DEVICE(VENDOR_LG, 0x9803) },
223 /* Microsoft MCE Infrared Transceiver */
224 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
225 /* Formosa eHome Infrared Transceiver */
226 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
227 /* Formosa21 / eHome Infrared Receiver */
228 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
229 /* Formosa aim / Trust MCE Infrared Receiver */
230 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
231 /* Formosa Industrial Computing / Beanbag Emulation Device */
232 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
233 /* Formosa21 / eHome Infrared Receiver */
234 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
235 /* Formosa Industrial Computing AIM IR605/A */
236 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
237 /* Formosa Industrial Computing */
238 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
239 /* Fintek eHome Infrared Transceiver */
240 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
241 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
242 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
243 /* Pinnacle Remote Kit */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300244 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
245 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300246 /* Elitegroup Computer Systems IR */
247 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
248 /* Wistron Corp. eHome Infrared Receiver */
249 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
250 /* Compro K100 */
251 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
252 /* Compro K100 v2 */
253 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
254 /* Northstar Systems, Inc. eHome Infrared Transceiver */
255 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
256 /* TiVo PC IR Receiver */
257 { USB_DEVICE(VENDOR_TIVO, 0x2000) },
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300258 /* Conexant SDK */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300259 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
260 .driver_info = POLARIS_EVK },
Jarod Wilson66e89522010-06-01 17:32:08 -0300261 /* Terminating entry */
262 { }
263};
264
Jarod Wilson66e89522010-06-01 17:32:08 -0300265/* data structure for each usb transceiver */
266struct mceusb_dev {
267 /* ir-core bits */
Jarod Wilson66e89522010-06-01 17:32:08 -0300268 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300269 struct ir_raw_event rawir;
270
271 /* core device bits */
272 struct device *dev;
273 struct input_dev *idev;
274
275 /* usb */
276 struct usb_device *usbdev;
277 struct urb *urb_in;
278 struct usb_endpoint_descriptor *usb_ep_in;
279 struct usb_endpoint_descriptor *usb_ep_out;
280
281 /* buffers and dma */
282 unsigned char *buf_in;
283 unsigned int len_in;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300284
285 enum {
286 CMD_HEADER = 0,
287 SUBCMD,
288 CMD_DATA,
289 PARSE_IRDATA,
290 } parser_state;
291 u8 cmd, rem; /* Remaining IR data bytes in packet */
292
Jarod Wilson66e89522010-06-01 17:32:08 -0300293 dma_addr_t dma_in;
294 dma_addr_t dma_out;
295
296 struct {
297 u32 connected:1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300298 u32 tx_mask_inverted:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300299 u32 microsoft_gen1:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300300 } flags;
301
Jarod Wilsone23fb962010-06-16 17:55:52 -0300302 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300303 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300304 u32 carrier;
305 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300306
307 char name[128];
308 char phys[64];
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300309 enum mceusb_model_type model;
Jarod Wilson66e89522010-06-01 17:32:08 -0300310};
311
312/*
313 * MCE Device Command Strings
314 * Device command responses vary from device to device...
315 * - DEVICE_RESET resets the hardware to its default state
316 * - GET_REVISION fetches the hardware/software revision, common
317 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
318 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
319 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
320 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
321 * ((clk / frequency) - 1)
322 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
323 * response in the form of 9f 0c msb lsb
324 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
325 * the form of 9f 08 bm, where bm is the bitmask
326 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
327 * general use one or short-range learning one, in the form of
328 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
329 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
330 * - SET_TX_BITMASK sets the transmitter bitmask
331 * - SET_RX_TIMEOUT sets the receiver timeout
332 * - SET_RX_SENSOR sets which receiver sensor to use
333 */
334static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
335static char GET_REVISION[] = {0xff, 0x0b};
336static char GET_UNKNOWN[] = {0xff, 0x18};
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300337static char GET_UNKNOWN2[] = {MCE_CONTROL_HEADER, 0x05};
338static char GET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x07};
339static char GET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0d};
340static char GET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x13};
341static char GET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x15};
Jarod Wilson66e89522010-06-01 17:32:08 -0300342/* sub in desired values in lower byte or bytes for full command */
343/* FIXME: make use of these for transmit.
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300344static char SET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x06, 0x00, 0x00};
345static char SET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x08, 0x00};
346static char SET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0c, 0x00, 0x00};
347static char SET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x14, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300348*/
349
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300350static int mceusb_cmdsize(u8 cmd, u8 subcmd)
351{
352 int datasize = 0;
353
354 switch (cmd) {
355 case 0x00:
356 if (subcmd == 0xff)
357 datasize = 1;
358 break;
359 case 0xff:
360 switch (subcmd) {
361 case 0x0b:
362 datasize = 2;
363 break;
364 }
365 case MCE_CONTROL_HEADER:
366 switch (subcmd) {
367 case 0x04:
368 case 0x06:
369 case 0x0c:
370 case 0x15:
371 datasize = 2;
372 break;
373 case 0x08:
374 case 0x14:
375 datasize = 1;
376 break;
377 }
378 }
379 return datasize;
380}
381
Jarod Wilson66e89522010-06-01 17:32:08 -0300382static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
383 int len, bool out)
384{
385 char codes[USB_BUFLEN * 3 + 1];
386 char inout[9];
387 int i;
388 u8 cmd, subcmd, data1, data2;
389 struct device *dev = ir->dev;
Jarod Wilson657290b2010-06-16 17:10:05 -0300390 int idx = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300391
Jarod Wilson657290b2010-06-16 17:10:05 -0300392 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
393 if (ir->flags.microsoft_gen1 && !out)
394 idx = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300395
Jarod Wilson657290b2010-06-16 17:10:05 -0300396 if (len <= idx)
Jarod Wilson66e89522010-06-01 17:32:08 -0300397 return;
398
399 for (i = 0; i < len && i < USB_BUFLEN; i++)
400 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
401
402 dev_info(dev, "%sx data: %s (length=%d)\n",
403 (out ? "t" : "r"), codes, len);
404
405 if (out)
406 strcpy(inout, "Request\0");
407 else
408 strcpy(inout, "Got\0");
409
Jarod Wilson657290b2010-06-16 17:10:05 -0300410 cmd = buf[idx] & 0xff;
411 subcmd = buf[idx + 1] & 0xff;
412 data1 = buf[idx + 2] & 0xff;
413 data2 = buf[idx + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300414
415 switch (cmd) {
416 case 0x00:
417 if (subcmd == 0xff && data1 == 0xaa)
418 dev_info(dev, "Device reset requested\n");
419 else
420 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
421 cmd, subcmd);
422 break;
423 case 0xff:
424 switch (subcmd) {
425 case 0x0b:
426 if (len == 2)
427 dev_info(dev, "Get hw/sw rev?\n");
428 else
429 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
430 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson657290b2010-06-16 17:10:05 -0300431 buf[idx + 4], buf[idx + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300432 break;
433 case 0xaa:
434 dev_info(dev, "Device reset requested\n");
435 break;
436 case 0xfe:
437 dev_info(dev, "Previous command not supported\n");
438 break;
439 case 0x18:
440 case 0x1b:
441 default:
442 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
443 cmd, subcmd);
444 break;
445 }
446 break;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300447 case MCE_CONTROL_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300448 switch (subcmd) {
449 case 0x03:
450 dev_info(dev, "Ping\n");
451 break;
452 case 0x04:
453 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
454 data1, data2);
455 break;
456 case 0x06:
457 dev_info(dev, "%s carrier mode and freq of "
458 "0x%02x 0x%02x\n", inout, data1, data2);
459 break;
460 case 0x07:
461 dev_info(dev, "Get carrier mode and freq\n");
462 break;
463 case 0x08:
464 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
465 inout, data1);
466 break;
467 case 0x0c:
468 /* value is in units of 50us, so x*50/100 or x/2 ms */
469 dev_info(dev, "%s receive timeout of %d ms\n",
470 inout, ((data1 << 8) | data2) / 2);
471 break;
472 case 0x0d:
473 dev_info(dev, "Get receive timeout\n");
474 break;
475 case 0x13:
476 dev_info(dev, "Get transmit blaster mask\n");
477 break;
478 case 0x14:
479 dev_info(dev, "%s %s-range receive sensor in use\n",
480 inout, data1 == 0x02 ? "short" : "long");
481 break;
482 case 0x15:
483 if (len == 2)
484 dev_info(dev, "Get receive sensor\n");
485 else
486 dev_info(dev, "Received pulse count is %d\n",
487 ((data1 << 8) | data2));
488 break;
489 case 0xfe:
490 dev_info(dev, "Error! Hardware is likely wedged...\n");
491 break;
492 case 0x05:
493 case 0x09:
494 case 0x0f:
495 default:
496 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
497 cmd, subcmd);
498 break;
499 }
500 break;
501 default:
502 break;
503 }
504}
505
Jarod Wilson7c294402010-08-02 18:21:06 -0300506static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300507{
508 struct mceusb_dev *ir;
509 int len;
510
511 if (!urb)
512 return;
513
514 ir = urb->context;
515 if (ir) {
516 len = urb->actual_length;
517
518 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
519 urb->status, len);
520
521 if (debug)
522 mceusb_dev_printdata(ir, urb->transfer_buffer,
523 len, true);
524 }
525
526}
527
528/* request incoming or send outgoing usb packet - used to initialize remote */
529static void mce_request_packet(struct mceusb_dev *ir,
530 struct usb_endpoint_descriptor *ep,
531 unsigned char *data, int size, int urb_type)
532{
533 int res;
534 struct urb *async_urb;
535 struct device *dev = ir->dev;
536 unsigned char *async_buf;
537
538 if (urb_type == MCEUSB_TX) {
539 async_urb = usb_alloc_urb(0, GFP_KERNEL);
540 if (unlikely(!async_urb)) {
541 dev_err(dev, "Error, couldn't allocate urb!\n");
542 return;
543 }
544
545 async_buf = kzalloc(size, GFP_KERNEL);
546 if (!async_buf) {
547 dev_err(dev, "Error, couldn't allocate buf!\n");
548 usb_free_urb(async_urb);
549 return;
550 }
551
552 /* outbound data */
553 usb_fill_int_urb(async_urb, ir->usbdev,
554 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300555 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300556 ir, ep->bInterval);
557 memcpy(async_buf, data, size);
558
559 } else if (urb_type == MCEUSB_RX) {
560 /* standard request */
561 async_urb = ir->urb_in;
562 ir->send_flags = RECV_FLAG_IN_PROGRESS;
563
564 } else {
565 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
566 return;
567 }
568
569 dev_dbg(dev, "receive request called (size=%#x)\n", size);
570
571 async_urb->transfer_buffer_length = size;
572 async_urb->dev = ir->usbdev;
573
574 res = usb_submit_urb(async_urb, GFP_ATOMIC);
575 if (res) {
576 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
577 return;
578 }
579 dev_dbg(dev, "receive request complete (res=%d)\n", res);
580}
581
582static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
583{
584 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
585}
586
587static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
588{
589 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
590}
591
Jarod Wilsone23fb962010-06-16 17:55:52 -0300592/* Send data out the IR blaster port(s) */
593static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
594{
595 struct mceusb_dev *ir = priv;
596 int i, ret = 0;
597 int count, cmdcount = 0;
598 unsigned char *cmdbuf; /* MCE command buffer */
599 long signal_duration = 0; /* Singnal length in us */
600 struct timeval start_time, end_time;
601
602 do_gettimeofday(&start_time);
603
604 count = n / sizeof(int);
605
606 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
607 if (!cmdbuf)
608 return -ENOMEM;
609
610 /* MCE tx init header */
611 cmdbuf[cmdcount++] = MCE_CONTROL_HEADER;
612 cmdbuf[cmdcount++] = 0x08;
613 cmdbuf[cmdcount++] = ir->tx_mask;
614
615 /* Generate mce packet data */
616 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
617 signal_duration += txbuf[i];
618 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
619
620 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
621
622 /* Insert mce packet header every 4th entry */
623 if ((cmdcount < MCE_CMDBUF_SIZE) &&
624 (cmdcount - MCE_TX_HEADER_LENGTH) %
625 MCE_CODE_LENGTH == 0)
626 cmdbuf[cmdcount++] = MCE_PACKET_HEADER;
627
628 /* Insert mce packet data */
629 if (cmdcount < MCE_CMDBUF_SIZE)
630 cmdbuf[cmdcount++] =
631 (txbuf[i] < MCE_PULSE_BIT ?
632 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
633 (i & 1 ? 0x00 : MCE_PULSE_BIT);
634 else {
635 ret = -EINVAL;
636 goto out;
637 }
638
639 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
640 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
641 }
642
643 /* Fix packet length in last header */
644 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
645 0x80 + (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH - 1;
646
647 /* Check if we have room for the empty packet at the end */
648 if (cmdcount >= MCE_CMDBUF_SIZE) {
649 ret = -EINVAL;
650 goto out;
651 }
652
653 /* All mce commands end with an empty packet (0x80) */
654 cmdbuf[cmdcount++] = 0x80;
655
656 /* Transmit the command to the mce device */
657 mce_async_out(ir, cmdbuf, cmdcount);
658
659 /*
660 * The lircd gap calculation expects the write function to
661 * wait the time it takes for the ircommand to be sent before
662 * it returns.
663 */
664 do_gettimeofday(&end_time);
665 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
666 (end_time.tv_sec - start_time.tv_sec) * 1000000;
667
668 /* delay with the closest number of ticks */
669 set_current_state(TASK_INTERRUPTIBLE);
670 schedule_timeout(usecs_to_jiffies(signal_duration));
671
672out:
673 kfree(cmdbuf);
674 return ret ? ret : n;
675}
676
Jarod Wilson657290b2010-06-16 17:10:05 -0300677/* Sets active IR outputs -- mce devices typically (all?) have two */
678static int mceusb_set_tx_mask(void *priv, u32 mask)
679{
680 struct mceusb_dev *ir = priv;
681
682 if (ir->flags.tx_mask_inverted)
683 ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
684 else
685 ir->tx_mask = mask;
686
687 return 0;
688}
689
Jarod Wilsone23fb962010-06-16 17:55:52 -0300690/* Sets the send carrier frequency and mode */
691static int mceusb_set_tx_carrier(void *priv, u32 carrier)
692{
693 struct mceusb_dev *ir = priv;
694 int clk = 10000000;
695 int prescaler = 0, divisor = 0;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300696 unsigned char cmdbuf[4] = { MCE_CONTROL_HEADER, 0x06, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300697
698 /* Carrier has changed */
699 if (ir->carrier != carrier) {
700
701 if (carrier == 0) {
702 ir->carrier = carrier;
703 cmdbuf[2] = 0x01;
704 cmdbuf[3] = 0x80;
705 dev_dbg(ir->dev, "%s: disabling carrier "
706 "modulation\n", __func__);
707 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
708 return carrier;
709 }
710
711 for (prescaler = 0; prescaler < 4; ++prescaler) {
712 divisor = (clk >> (2 * prescaler)) / carrier;
713 if (divisor <= 0xFF) {
714 ir->carrier = carrier;
715 cmdbuf[2] = prescaler;
716 cmdbuf[3] = divisor;
717 dev_dbg(ir->dev, "%s: requesting %u HZ "
718 "carrier\n", __func__, carrier);
719
720 /* Transmit new carrier to mce device */
721 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
722 return carrier;
723 }
724 }
725
726 return -EINVAL;
727
728 }
729
730 return carrier;
731}
732
Jarod Wilson66e89522010-06-01 17:32:08 -0300733static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
734{
Maxim Levitsky46519182010-10-16 19:56:28 -0300735 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300736 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300737
738 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
739 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300740 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300741
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300742 for (; i < buf_len; i++) {
743 switch (ir->parser_state) {
744 case SUBCMD:
745 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
746 ir->parser_state = CMD_DATA;
747 break;
748 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300749 ir->rem--;
Jarod Wilson66e89522010-06-01 17:32:08 -0300750 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
751 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
752 * MCE_TIME_UNIT * 1000;
753
754 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300755 if (ir->rawir.pulse == rawir.pulse) {
Jarod Wilson66e89522010-06-01 17:32:08 -0300756 ir->rawir.duration += rawir.duration;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300757 } else {
Jarod Wilson66e89522010-06-01 17:32:08 -0300758 ir->rawir.duration = rawir.duration;
759 ir->rawir.pulse = rawir.pulse;
760 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300761 if (ir->rem)
762 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300763 }
764 rawir.duration += ir->rawir.duration;
765 ir->rawir.duration = 0;
766 ir->rawir.pulse = rawir.pulse;
767
768 dev_dbg(ir->dev, "Storing %s with duration %d\n",
769 rawir.pulse ? "pulse" : "space",
770 rawir.duration);
771
772 ir_raw_event_store(ir->idev, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300773 break;
774 case CMD_DATA:
775 ir->rem--;
776 break;
777 case CMD_HEADER:
778 /* decode mce packets of the form (84),AA,BB,CC,DD */
779 /* IR data packets can span USB messages - rem */
780 ir->cmd = ir->buf_in[i];
781 if ((ir->cmd == MCE_CONTROL_HEADER) ||
782 ((ir->cmd & MCE_COMMAND_MASK) != MCE_COMMAND_IRDATA)) {
783 ir->parser_state = SUBCMD;
784 continue;
785 }
786 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
787 dev_dbg(ir->dev, "Processing RX data: len = %d\n",
788 ir->rem);
789 if (ir->rem) {
790 ir->parser_state = PARSE_IRDATA;
791 break;
792 }
793 /*
794 * a package with len=0 (e. g. 0x80) means end of
795 * data. We could use it to do the call to
796 * ir_raw_event_handle(). For now, we don't need to
797 * use it.
798 */
799 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300800 }
801
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300802 if (ir->parser_state != CMD_HEADER && !ir->rem)
803 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300804 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300805 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
806 ir_raw_event_handle(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -0300807}
808
Jarod Wilson66e89522010-06-01 17:32:08 -0300809static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
810{
811 struct mceusb_dev *ir;
812 int buf_len;
813
814 if (!urb)
815 return;
816
817 ir = urb->context;
818 if (!ir) {
819 usb_unlink_urb(urb);
820 return;
821 }
822
823 buf_len = urb->actual_length;
824
Jarod Wilson66e89522010-06-01 17:32:08 -0300825 if (debug)
826 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
827
828 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
829 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300830 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300831 buf_len);
832 }
833
834 switch (urb->status) {
835 /* success */
836 case 0:
837 mceusb_process_ir_data(ir, buf_len);
838 break;
839
840 case -ECONNRESET:
841 case -ENOENT:
842 case -ESHUTDOWN:
843 usb_unlink_urb(urb);
844 return;
845
846 case -EPIPE:
847 default:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300848 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300849 break;
850 }
851
852 usb_submit_urb(urb, GFP_ATOMIC);
853}
854
855static void mceusb_gen1_init(struct mceusb_dev *ir)
856{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300857 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300858 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300859 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300860 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300861
862 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
863 if (!data) {
864 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300865 return;
866 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300867
868 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300869 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300870 * on the receive control pipe and expect a certain value pair back
871 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300872 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
873 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300874 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300875 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
876 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
877 __func__, data[0], data[1]);
878
879 /* set feature: bit rate 38400 bps */
880 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
881 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
882 0xc04e, 0x0000, NULL, 0, HZ * 3);
883
884 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
885
886 /* bRequest 4: set char length to 8 bits */
887 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
888 4, USB_TYPE_VENDOR,
889 0x0808, 0x0000, NULL, 0, HZ * 3);
890 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
891
892 /* bRequest 2: set handshaking to use DTR/DSR */
893 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
894 2, USB_TYPE_VENDOR,
895 0x0000, 0x0100, NULL, 0, HZ * 3);
896 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300897
Jarod Wilson22b07662010-07-08 12:38:57 -0300898 /* device reset */
899 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
900 mce_sync_in(ir, NULL, maxp);
901
902 /* get hw/sw revision? */
903 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
904 mce_sync_in(ir, NULL, maxp);
905
Jarod Wilson657290b2010-06-16 17:10:05 -0300906 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -0300907};
908
909static void mceusb_gen2_init(struct mceusb_dev *ir)
910{
911 int maxp = ir->len_in;
912
Jarod Wilson66e89522010-06-01 17:32:08 -0300913 /* device reset */
914 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
915 mce_sync_in(ir, NULL, maxp);
916
917 /* get hw/sw revision? */
918 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
919 mce_sync_in(ir, NULL, maxp);
920
Jarod Wilson22b07662010-07-08 12:38:57 -0300921 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -0300922 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
923 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -0300924 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
925 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -0300926}
927
Jarod Wilson22b07662010-07-08 12:38:57 -0300928static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -0300929{
930 int maxp = ir->len_in;
931
Jarod Wilson66e89522010-06-01 17:32:08 -0300932 /* get the carrier and frequency */
933 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
934 mce_sync_in(ir, NULL, maxp);
935
936 /* get the transmitter bitmask */
937 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
938 mce_sync_in(ir, NULL, maxp);
939
940 /* get receiver timeout value */
941 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
942 mce_sync_in(ir, NULL, maxp);
943
944 /* get receiver sensor setting */
945 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
946 mce_sync_in(ir, NULL, maxp);
947}
948
949static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
950{
951 struct input_dev *idev;
952 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300953 struct device *dev = ir->dev;
954 int ret = -ENODEV;
955
956 idev = input_allocate_device();
957 if (!idev) {
958 dev_err(dev, "remote input dev allocation failed\n");
959 goto idev_alloc_failed;
960 }
961
962 ret = -ENOMEM;
963 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
964 if (!props) {
965 dev_err(dev, "remote ir dev props allocation failed\n");
966 goto props_alloc_failed;
967 }
968
Jarod Wilsone23fb962010-06-16 17:55:52 -0300969 snprintf(ir->name, sizeof(ir->name), "Media Center Ed. eHome "
Jarod Wilson66e89522010-06-01 17:32:08 -0300970 "Infrared Remote Transceiver (%04x:%04x)",
971 le16_to_cpu(ir->usbdev->descriptor.idVendor),
972 le16_to_cpu(ir->usbdev->descriptor.idProduct));
973
Jarod Wilson66e89522010-06-01 17:32:08 -0300974 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -0300975 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
976 strlcat(ir->phys, "/input0", sizeof(ir->phys));
977 idev->phys = ir->phys;
978
Jarod Wilson66e89522010-06-01 17:32:08 -0300979 props->priv = ir;
980 props->driver_type = RC_DRIVER_IR_RAW;
981 props->allowed_protos = IR_TYPE_ALL;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300982 props->s_tx_mask = mceusb_set_tx_mask;
983 props->s_tx_carrier = mceusb_set_tx_carrier;
984 props->tx_ir = mceusb_tx_ir;
Jarod Wilson66e89522010-06-01 17:32:08 -0300985
986 ir->props = props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300987
988 ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
989 if (ret < 0) {
990 dev_err(dev, "remote input device register failed\n");
991 goto irdev_failed;
992 }
993
994 return idev;
995
996irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -0300997 kfree(props);
998props_alloc_failed:
999 input_free_device(idev);
1000idev_alloc_failed:
1001 return NULL;
1002}
1003
1004static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1005 const struct usb_device_id *id)
1006{
1007 struct usb_device *dev = interface_to_usbdev(intf);
1008 struct usb_host_interface *idesc;
1009 struct usb_endpoint_descriptor *ep = NULL;
1010 struct usb_endpoint_descriptor *ep_in = NULL;
1011 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001012 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -03001013 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -03001014 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001015 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001016 bool is_gen3;
1017 bool is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001018 bool tx_mask_inverted;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001019 bool is_polaris;
Jarod Wilson66e89522010-06-01 17:32:08 -03001020
1021 dev_dbg(&intf->dev, ": %s called\n", __func__);
1022
Jarod Wilson66e89522010-06-01 17:32:08 -03001023 idesc = intf->cur_altsetting;
1024
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001025 is_gen3 = mceusb_model[model].mce_gen3;
1026 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1027 tx_mask_inverted = mceusb_model[model].tx_mask_inverted;
1028 is_polaris = mceusb_model[model].is_polaris;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001029
1030 if (is_polaris) {
1031 /* Interface 0 is IR */
1032 if (idesc->desc.bInterfaceNumber)
1033 return -ENODEV;
1034 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001035
1036 /* step through the endpoints to find first bulk in and out endpoint */
1037 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1038 ep = &idesc->endpoint[i].desc;
1039
1040 if ((ep_in == NULL)
1041 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1042 == USB_DIR_IN)
1043 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1044 == USB_ENDPOINT_XFER_BULK)
1045 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1046 == USB_ENDPOINT_XFER_INT))) {
1047
Jarod Wilson66e89522010-06-01 17:32:08 -03001048 ep_in = ep;
1049 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001050 ep_in->bInterval = 1;
1051 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
1052 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001053 }
1054
1055 if ((ep_out == NULL)
1056 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1057 == USB_DIR_OUT)
1058 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1059 == USB_ENDPOINT_XFER_BULK)
1060 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1061 == USB_ENDPOINT_XFER_INT))) {
1062
Jarod Wilson66e89522010-06-01 17:32:08 -03001063 ep_out = ep;
1064 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001065 ep_out->bInterval = 1;
1066 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
1067 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001068 }
1069 }
1070 if (ep_in == NULL) {
1071 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
1072 return -ENODEV;
1073 }
1074
1075 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1076 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1077
1078 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1079 if (!ir)
1080 goto mem_alloc_fail;
1081
1082 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1083 if (!ir->buf_in)
1084 goto buf_in_alloc_fail;
1085
1086 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1087 if (!ir->urb_in)
1088 goto urb_in_alloc_fail;
1089
1090 ir->usbdev = dev;
1091 ir->dev = &intf->dev;
1092 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001093 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001094 ir->flags.tx_mask_inverted = tx_mask_inverted;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001095 ir->model = model;
1096
Maxim Levitsky46519182010-10-16 19:56:28 -03001097 init_ir_raw_event(&ir->rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001098
1099 /* Saving usb interface data for use by the transmitter routine */
1100 ir->usb_ep_in = ep_in;
1101 ir->usb_ep_out = ep_out;
1102
1103 if (dev->descriptor.iManufacturer
1104 && usb_string(dev, dev->descriptor.iManufacturer,
1105 buf, sizeof(buf)) > 0)
1106 strlcpy(name, buf, sizeof(name));
1107 if (dev->descriptor.iProduct
1108 && usb_string(dev, dev->descriptor.iProduct,
1109 buf, sizeof(buf)) > 0)
1110 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1111 " %s", buf);
1112
1113 ir->idev = mceusb_init_input_dev(ir);
1114 if (!ir->idev)
1115 goto input_dev_fail;
1116
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001117 /* flush buffers on the device */
1118 mce_sync_in(ir, NULL, maxp);
1119 mce_sync_in(ir, NULL, maxp);
1120
1121 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001122 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1123 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1124 ir->urb_in->transfer_dma = ir->dma_in;
1125 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1126
Jarod Wilson66e89522010-06-01 17:32:08 -03001127 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001128 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001129 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001130 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001131 mceusb_gen2_init(ir);
1132
Jarod Wilson22b07662010-07-08 12:38:57 -03001133 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001134
Jarod Wilson657290b2010-06-16 17:10:05 -03001135 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001136
1137 usb_set_intfdata(intf, ir);
1138
1139 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1140 dev->bus->busnum, dev->devnum);
1141
1142 return 0;
1143
1144 /* Error-handling path */
1145input_dev_fail:
1146 usb_free_urb(ir->urb_in);
1147urb_in_alloc_fail:
1148 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1149buf_in_alloc_fail:
1150 kfree(ir);
1151mem_alloc_fail:
1152 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1153
1154 return -ENOMEM;
1155}
1156
1157
1158static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1159{
1160 struct usb_device *dev = interface_to_usbdev(intf);
1161 struct mceusb_dev *ir = usb_get_intfdata(intf);
1162
1163 usb_set_intfdata(intf, NULL);
1164
1165 if (!ir)
1166 return;
1167
1168 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001169 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001170 usb_kill_urb(ir->urb_in);
1171 usb_free_urb(ir->urb_in);
1172 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1173
1174 kfree(ir);
1175}
1176
1177static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1178{
1179 struct mceusb_dev *ir = usb_get_intfdata(intf);
1180 dev_info(ir->dev, "suspend\n");
1181 usb_kill_urb(ir->urb_in);
1182 return 0;
1183}
1184
1185static int mceusb_dev_resume(struct usb_interface *intf)
1186{
1187 struct mceusb_dev *ir = usb_get_intfdata(intf);
1188 dev_info(ir->dev, "resume\n");
1189 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1190 return -EIO;
1191 return 0;
1192}
1193
1194static struct usb_driver mceusb_dev_driver = {
1195 .name = DRIVER_NAME,
1196 .probe = mceusb_dev_probe,
1197 .disconnect = mceusb_dev_disconnect,
1198 .suspend = mceusb_dev_suspend,
1199 .resume = mceusb_dev_resume,
1200 .reset_resume = mceusb_dev_resume,
1201 .id_table = mceusb_dev_table
1202};
1203
1204static int __init mceusb_dev_init(void)
1205{
1206 int ret;
1207
1208 ret = usb_register(&mceusb_dev_driver);
1209 if (ret < 0)
1210 printk(KERN_ERR DRIVER_NAME
1211 ": usb register failed, result = %d\n", ret);
1212
1213 return ret;
1214}
1215
1216static void __exit mceusb_dev_exit(void)
1217{
1218 usb_deregister(&mceusb_dev_driver);
1219}
1220
1221module_init(mceusb_dev_init);
1222module_exit(mceusb_dev_exit);
1223
1224MODULE_DESCRIPTION(DRIVER_DESC);
1225MODULE_AUTHOR(DRIVER_AUTHOR);
1226MODULE_LICENSE("GPL");
1227MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1228
1229module_param(debug, bool, S_IRUGO | S_IWUSR);
1230MODULE_PARM_DESC(debug, "Debug enabled or not");