blob: c890cd521b204bd8cc863da9bdc75118e7b60952 [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
111static struct usb_device_id mceusb_dev_table[] = {
112 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
113 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
114 /* Philips Infrared Transceiver - Sahara branded */
115 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
116 /* Philips Infrared Transceiver - HP branded */
117 { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
118 /* Philips SRM5100 */
119 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
120 /* Philips Infrared Transceiver - Omaura */
121 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
122 /* Philips Infrared Transceiver - Spinel plus */
123 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
124 /* Philips eHome Infrared Transceiver */
125 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300126 /* Philips/Spinel plus IR transceiver for ASUS */
127 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
128 /* Philips/Spinel plus IR transceiver for ASUS */
129 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300130 /* Realtek MCE IR Receiver */
131 { USB_DEVICE(VENDOR_REALTEK, 0x0161) },
132 /* SMK/Toshiba G83C0004D410 */
133 { USB_DEVICE(VENDOR_SMK, 0x031d) },
134 /* SMK eHome Infrared Transceiver (Sony VAIO) */
135 { USB_DEVICE(VENDOR_SMK, 0x0322) },
136 /* bundled with Hauppauge PVR-150 */
137 { USB_DEVICE(VENDOR_SMK, 0x0334) },
138 /* SMK eHome Infrared Transceiver */
139 { USB_DEVICE(VENDOR_SMK, 0x0338) },
140 /* Tatung eHome Infrared Transceiver */
141 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
142 /* Shuttle eHome Infrared Transceiver */
143 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
144 /* Shuttle eHome Infrared Transceiver */
145 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
146 /* Gateway eHome Infrared Transceiver */
147 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
148 /* Mitsumi */
149 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
150 /* Topseed eHome Infrared Transceiver */
151 { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
152 /* Topseed HP eHome Infrared Transceiver */
153 { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
154 /* Topseed eHome Infrared Transceiver */
155 { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
156 /* Topseed eHome Infrared Transceiver */
157 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
158 /* Topseed eHome Infrared Transceiver */
159 { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
160 /* Topseed eHome Infrared Transceiver */
161 { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
162 /* Ricavision internal Infrared Transceiver */
163 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
164 /* Itron ione Libra Q-11 */
165 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
166 /* FIC eHome Infrared Transceiver */
167 { USB_DEVICE(VENDOR_FIC, 0x9242) },
168 /* LG eHome Infrared Transceiver */
169 { USB_DEVICE(VENDOR_LG, 0x9803) },
170 /* Microsoft MCE Infrared Transceiver */
171 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
172 /* Formosa eHome Infrared Transceiver */
173 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
174 /* Formosa21 / eHome Infrared Receiver */
175 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
176 /* Formosa aim / Trust MCE Infrared Receiver */
177 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
178 /* Formosa Industrial Computing / Beanbag Emulation Device */
179 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
180 /* Formosa21 / eHome Infrared Receiver */
181 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
182 /* Formosa Industrial Computing AIM IR605/A */
183 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
184 /* Formosa Industrial Computing */
185 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
186 /* Fintek eHome Infrared Transceiver */
187 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
188 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
189 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
190 /* Pinnacle Remote Kit */
191 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
192 /* Elitegroup Computer Systems IR */
193 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
194 /* Wistron Corp. eHome Infrared Receiver */
195 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
196 /* Compro K100 */
197 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
198 /* Compro K100 v2 */
199 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
200 /* Northstar Systems, Inc. eHome Infrared Transceiver */
201 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
202 /* TiVo PC IR Receiver */
203 { USB_DEVICE(VENDOR_TIVO, 0x2000) },
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300204 /* Conexant SDK */
205 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300206 /* Terminating entry */
207 { }
208};
209
210static struct usb_device_id gen3_list[] = {
211 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
212 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
213 {}
214};
215
Jarod Wilson66e89522010-06-01 17:32:08 -0300216static struct usb_device_id microsoft_gen1_list[] = {
217 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
218 {}
219};
220
Jarod Wilson657290b2010-06-16 17:10:05 -0300221static struct usb_device_id std_tx_mask_list[] = {
222 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
223 { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
224 { USB_DEVICE(VENDOR_SMK, 0x031d) },
225 { USB_DEVICE(VENDOR_SMK, 0x0322) },
226 { USB_DEVICE(VENDOR_SMK, 0x0334) },
227 { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
228 { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
229 { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
230 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
231 { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
232 { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
233 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
234 {}
235};
236
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300237static struct usb_device_id cx_polaris_list[] = {
238 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1) },
239 {}
240};
241
Jarod Wilson66e89522010-06-01 17:32:08 -0300242/* data structure for each usb transceiver */
243struct mceusb_dev {
244 /* ir-core bits */
Jarod Wilson66e89522010-06-01 17:32:08 -0300245 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300246 struct ir_raw_event rawir;
247
248 /* core device bits */
249 struct device *dev;
250 struct input_dev *idev;
251
252 /* usb */
253 struct usb_device *usbdev;
254 struct urb *urb_in;
255 struct usb_endpoint_descriptor *usb_ep_in;
256 struct usb_endpoint_descriptor *usb_ep_out;
257
258 /* buffers and dma */
259 unsigned char *buf_in;
260 unsigned int len_in;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300261
262 enum {
263 CMD_HEADER = 0,
264 SUBCMD,
265 CMD_DATA,
266 PARSE_IRDATA,
267 } parser_state;
268 u8 cmd, rem; /* Remaining IR data bytes in packet */
269
Jarod Wilson66e89522010-06-01 17:32:08 -0300270 dma_addr_t dma_in;
271 dma_addr_t dma_out;
272
273 struct {
274 u32 connected:1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300275 u32 tx_mask_inverted:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300276 u32 microsoft_gen1:1;
Jarod Wilson22b07662010-07-08 12:38:57 -0300277 u32 reserved:29;
Jarod Wilson66e89522010-06-01 17:32:08 -0300278 } flags;
279
Jarod Wilsone23fb962010-06-16 17:55:52 -0300280 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300281 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300282 u32 carrier;
283 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300284
285 char name[128];
286 char phys[64];
Jarod Wilson66e89522010-06-01 17:32:08 -0300287};
288
289/*
290 * MCE Device Command Strings
291 * Device command responses vary from device to device...
292 * - DEVICE_RESET resets the hardware to its default state
293 * - GET_REVISION fetches the hardware/software revision, common
294 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
295 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
296 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
297 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
298 * ((clk / frequency) - 1)
299 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
300 * response in the form of 9f 0c msb lsb
301 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
302 * the form of 9f 08 bm, where bm is the bitmask
303 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
304 * general use one or short-range learning one, in the form of
305 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
306 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
307 * - SET_TX_BITMASK sets the transmitter bitmask
308 * - SET_RX_TIMEOUT sets the receiver timeout
309 * - SET_RX_SENSOR sets which receiver sensor to use
310 */
311static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
312static char GET_REVISION[] = {0xff, 0x0b};
313static char GET_UNKNOWN[] = {0xff, 0x18};
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300314static char GET_UNKNOWN2[] = {MCE_CONTROL_HEADER, 0x05};
315static char GET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x07};
316static char GET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0d};
317static char GET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x13};
318static char GET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x15};
Jarod Wilson66e89522010-06-01 17:32:08 -0300319/* sub in desired values in lower byte or bytes for full command */
320/* FIXME: make use of these for transmit.
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300321static char SET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x06, 0x00, 0x00};
322static char SET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x08, 0x00};
323static char SET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0c, 0x00, 0x00};
324static char SET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x14, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300325*/
326
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300327static int mceusb_cmdsize(u8 cmd, u8 subcmd)
328{
329 int datasize = 0;
330
331 switch (cmd) {
332 case 0x00:
333 if (subcmd == 0xff)
334 datasize = 1;
335 break;
336 case 0xff:
337 switch (subcmd) {
338 case 0x0b:
339 datasize = 2;
340 break;
341 }
342 case MCE_CONTROL_HEADER:
343 switch (subcmd) {
344 case 0x04:
345 case 0x06:
346 case 0x0c:
347 case 0x15:
348 datasize = 2;
349 break;
350 case 0x08:
351 case 0x14:
352 datasize = 1;
353 break;
354 }
355 }
356 return datasize;
357}
358
Jarod Wilson66e89522010-06-01 17:32:08 -0300359static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
360 int len, bool out)
361{
362 char codes[USB_BUFLEN * 3 + 1];
363 char inout[9];
364 int i;
365 u8 cmd, subcmd, data1, data2;
366 struct device *dev = ir->dev;
Jarod Wilson657290b2010-06-16 17:10:05 -0300367 int idx = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300368
Jarod Wilson657290b2010-06-16 17:10:05 -0300369 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
370 if (ir->flags.microsoft_gen1 && !out)
371 idx = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300372
Jarod Wilson657290b2010-06-16 17:10:05 -0300373 if (len <= idx)
Jarod Wilson66e89522010-06-01 17:32:08 -0300374 return;
375
376 for (i = 0; i < len && i < USB_BUFLEN; i++)
377 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
378
379 dev_info(dev, "%sx data: %s (length=%d)\n",
380 (out ? "t" : "r"), codes, len);
381
382 if (out)
383 strcpy(inout, "Request\0");
384 else
385 strcpy(inout, "Got\0");
386
Jarod Wilson657290b2010-06-16 17:10:05 -0300387 cmd = buf[idx] & 0xff;
388 subcmd = buf[idx + 1] & 0xff;
389 data1 = buf[idx + 2] & 0xff;
390 data2 = buf[idx + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300391
392 switch (cmd) {
393 case 0x00:
394 if (subcmd == 0xff && data1 == 0xaa)
395 dev_info(dev, "Device reset requested\n");
396 else
397 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
398 cmd, subcmd);
399 break;
400 case 0xff:
401 switch (subcmd) {
402 case 0x0b:
403 if (len == 2)
404 dev_info(dev, "Get hw/sw rev?\n");
405 else
406 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
407 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson657290b2010-06-16 17:10:05 -0300408 buf[idx + 4], buf[idx + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300409 break;
410 case 0xaa:
411 dev_info(dev, "Device reset requested\n");
412 break;
413 case 0xfe:
414 dev_info(dev, "Previous command not supported\n");
415 break;
416 case 0x18:
417 case 0x1b:
418 default:
419 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
420 cmd, subcmd);
421 break;
422 }
423 break;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300424 case MCE_CONTROL_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300425 switch (subcmd) {
426 case 0x03:
427 dev_info(dev, "Ping\n");
428 break;
429 case 0x04:
430 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
431 data1, data2);
432 break;
433 case 0x06:
434 dev_info(dev, "%s carrier mode and freq of "
435 "0x%02x 0x%02x\n", inout, data1, data2);
436 break;
437 case 0x07:
438 dev_info(dev, "Get carrier mode and freq\n");
439 break;
440 case 0x08:
441 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
442 inout, data1);
443 break;
444 case 0x0c:
445 /* value is in units of 50us, so x*50/100 or x/2 ms */
446 dev_info(dev, "%s receive timeout of %d ms\n",
447 inout, ((data1 << 8) | data2) / 2);
448 break;
449 case 0x0d:
450 dev_info(dev, "Get receive timeout\n");
451 break;
452 case 0x13:
453 dev_info(dev, "Get transmit blaster mask\n");
454 break;
455 case 0x14:
456 dev_info(dev, "%s %s-range receive sensor in use\n",
457 inout, data1 == 0x02 ? "short" : "long");
458 break;
459 case 0x15:
460 if (len == 2)
461 dev_info(dev, "Get receive sensor\n");
462 else
463 dev_info(dev, "Received pulse count is %d\n",
464 ((data1 << 8) | data2));
465 break;
466 case 0xfe:
467 dev_info(dev, "Error! Hardware is likely wedged...\n");
468 break;
469 case 0x05:
470 case 0x09:
471 case 0x0f:
472 default:
473 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
474 cmd, subcmd);
475 break;
476 }
477 break;
478 default:
479 break;
480 }
481}
482
Jarod Wilson7c294402010-08-02 18:21:06 -0300483static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300484{
485 struct mceusb_dev *ir;
486 int len;
487
488 if (!urb)
489 return;
490
491 ir = urb->context;
492 if (ir) {
493 len = urb->actual_length;
494
495 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
496 urb->status, len);
497
498 if (debug)
499 mceusb_dev_printdata(ir, urb->transfer_buffer,
500 len, true);
501 }
502
503}
504
505/* request incoming or send outgoing usb packet - used to initialize remote */
506static void mce_request_packet(struct mceusb_dev *ir,
507 struct usb_endpoint_descriptor *ep,
508 unsigned char *data, int size, int urb_type)
509{
510 int res;
511 struct urb *async_urb;
512 struct device *dev = ir->dev;
513 unsigned char *async_buf;
514
515 if (urb_type == MCEUSB_TX) {
516 async_urb = usb_alloc_urb(0, GFP_KERNEL);
517 if (unlikely(!async_urb)) {
518 dev_err(dev, "Error, couldn't allocate urb!\n");
519 return;
520 }
521
522 async_buf = kzalloc(size, GFP_KERNEL);
523 if (!async_buf) {
524 dev_err(dev, "Error, couldn't allocate buf!\n");
525 usb_free_urb(async_urb);
526 return;
527 }
528
529 /* outbound data */
530 usb_fill_int_urb(async_urb, ir->usbdev,
531 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300532 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300533 ir, ep->bInterval);
534 memcpy(async_buf, data, size);
535
536 } else if (urb_type == MCEUSB_RX) {
537 /* standard request */
538 async_urb = ir->urb_in;
539 ir->send_flags = RECV_FLAG_IN_PROGRESS;
540
541 } else {
542 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
543 return;
544 }
545
546 dev_dbg(dev, "receive request called (size=%#x)\n", size);
547
548 async_urb->transfer_buffer_length = size;
549 async_urb->dev = ir->usbdev;
550
551 res = usb_submit_urb(async_urb, GFP_ATOMIC);
552 if (res) {
553 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
554 return;
555 }
556 dev_dbg(dev, "receive request complete (res=%d)\n", res);
557}
558
559static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
560{
561 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
562}
563
564static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
565{
566 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
567}
568
Jarod Wilsone23fb962010-06-16 17:55:52 -0300569/* Send data out the IR blaster port(s) */
570static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
571{
572 struct mceusb_dev *ir = priv;
573 int i, ret = 0;
574 int count, cmdcount = 0;
575 unsigned char *cmdbuf; /* MCE command buffer */
576 long signal_duration = 0; /* Singnal length in us */
577 struct timeval start_time, end_time;
578
579 do_gettimeofday(&start_time);
580
581 count = n / sizeof(int);
582
583 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
584 if (!cmdbuf)
585 return -ENOMEM;
586
587 /* MCE tx init header */
588 cmdbuf[cmdcount++] = MCE_CONTROL_HEADER;
589 cmdbuf[cmdcount++] = 0x08;
590 cmdbuf[cmdcount++] = ir->tx_mask;
591
592 /* Generate mce packet data */
593 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
594 signal_duration += txbuf[i];
595 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
596
597 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
598
599 /* Insert mce packet header every 4th entry */
600 if ((cmdcount < MCE_CMDBUF_SIZE) &&
601 (cmdcount - MCE_TX_HEADER_LENGTH) %
602 MCE_CODE_LENGTH == 0)
603 cmdbuf[cmdcount++] = MCE_PACKET_HEADER;
604
605 /* Insert mce packet data */
606 if (cmdcount < MCE_CMDBUF_SIZE)
607 cmdbuf[cmdcount++] =
608 (txbuf[i] < MCE_PULSE_BIT ?
609 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
610 (i & 1 ? 0x00 : MCE_PULSE_BIT);
611 else {
612 ret = -EINVAL;
613 goto out;
614 }
615
616 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
617 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
618 }
619
620 /* Fix packet length in last header */
621 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
622 0x80 + (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH - 1;
623
624 /* Check if we have room for the empty packet at the end */
625 if (cmdcount >= MCE_CMDBUF_SIZE) {
626 ret = -EINVAL;
627 goto out;
628 }
629
630 /* All mce commands end with an empty packet (0x80) */
631 cmdbuf[cmdcount++] = 0x80;
632
633 /* Transmit the command to the mce device */
634 mce_async_out(ir, cmdbuf, cmdcount);
635
636 /*
637 * The lircd gap calculation expects the write function to
638 * wait the time it takes for the ircommand to be sent before
639 * it returns.
640 */
641 do_gettimeofday(&end_time);
642 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
643 (end_time.tv_sec - start_time.tv_sec) * 1000000;
644
645 /* delay with the closest number of ticks */
646 set_current_state(TASK_INTERRUPTIBLE);
647 schedule_timeout(usecs_to_jiffies(signal_duration));
648
649out:
650 kfree(cmdbuf);
651 return ret ? ret : n;
652}
653
Jarod Wilson657290b2010-06-16 17:10:05 -0300654/* Sets active IR outputs -- mce devices typically (all?) have two */
655static int mceusb_set_tx_mask(void *priv, u32 mask)
656{
657 struct mceusb_dev *ir = priv;
658
659 if (ir->flags.tx_mask_inverted)
660 ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
661 else
662 ir->tx_mask = mask;
663
664 return 0;
665}
666
Jarod Wilsone23fb962010-06-16 17:55:52 -0300667/* Sets the send carrier frequency and mode */
668static int mceusb_set_tx_carrier(void *priv, u32 carrier)
669{
670 struct mceusb_dev *ir = priv;
671 int clk = 10000000;
672 int prescaler = 0, divisor = 0;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300673 unsigned char cmdbuf[4] = { MCE_CONTROL_HEADER, 0x06, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300674
675 /* Carrier has changed */
676 if (ir->carrier != carrier) {
677
678 if (carrier == 0) {
679 ir->carrier = carrier;
680 cmdbuf[2] = 0x01;
681 cmdbuf[3] = 0x80;
682 dev_dbg(ir->dev, "%s: disabling carrier "
683 "modulation\n", __func__);
684 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
685 return carrier;
686 }
687
688 for (prescaler = 0; prescaler < 4; ++prescaler) {
689 divisor = (clk >> (2 * prescaler)) / carrier;
690 if (divisor <= 0xFF) {
691 ir->carrier = carrier;
692 cmdbuf[2] = prescaler;
693 cmdbuf[3] = divisor;
694 dev_dbg(ir->dev, "%s: requesting %u HZ "
695 "carrier\n", __func__, carrier);
696
697 /* Transmit new carrier to mce device */
698 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
699 return carrier;
700 }
701 }
702
703 return -EINVAL;
704
705 }
706
707 return carrier;
708}
709
Jarod Wilson66e89522010-06-01 17:32:08 -0300710static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
711{
Maxim Levitsky46519182010-10-16 19:56:28 -0300712 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300713 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300714
715 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
716 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300717 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300718
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300719 for (; i < buf_len; i++) {
720 switch (ir->parser_state) {
721 case SUBCMD:
722 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
723 ir->parser_state = CMD_DATA;
724 break;
725 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300726 ir->rem--;
Jarod Wilson66e89522010-06-01 17:32:08 -0300727 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
728 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
729 * MCE_TIME_UNIT * 1000;
730
731 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300732 if (ir->rawir.pulse == rawir.pulse) {
Jarod Wilson66e89522010-06-01 17:32:08 -0300733 ir->rawir.duration += rawir.duration;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300734 } else {
Jarod Wilson66e89522010-06-01 17:32:08 -0300735 ir->rawir.duration = rawir.duration;
736 ir->rawir.pulse = rawir.pulse;
737 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300738 if (ir->rem)
739 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300740 }
741 rawir.duration += ir->rawir.duration;
742 ir->rawir.duration = 0;
743 ir->rawir.pulse = rawir.pulse;
744
745 dev_dbg(ir->dev, "Storing %s with duration %d\n",
746 rawir.pulse ? "pulse" : "space",
747 rawir.duration);
748
749 ir_raw_event_store(ir->idev, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300750 break;
751 case CMD_DATA:
752 ir->rem--;
753 break;
754 case CMD_HEADER:
755 /* decode mce packets of the form (84),AA,BB,CC,DD */
756 /* IR data packets can span USB messages - rem */
757 ir->cmd = ir->buf_in[i];
758 if ((ir->cmd == MCE_CONTROL_HEADER) ||
759 ((ir->cmd & MCE_COMMAND_MASK) != MCE_COMMAND_IRDATA)) {
760 ir->parser_state = SUBCMD;
761 continue;
762 }
763 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
764 dev_dbg(ir->dev, "Processing RX data: len = %d\n",
765 ir->rem);
766 if (ir->rem) {
767 ir->parser_state = PARSE_IRDATA;
768 break;
769 }
770 /*
771 * a package with len=0 (e. g. 0x80) means end of
772 * data. We could use it to do the call to
773 * ir_raw_event_handle(). For now, we don't need to
774 * use it.
775 */
776 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300777 }
778
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300779 if (ir->parser_state != CMD_HEADER && !ir->rem)
780 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300781 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300782 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
783 ir_raw_event_handle(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -0300784}
785
Jarod Wilson66e89522010-06-01 17:32:08 -0300786static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
787{
788 struct mceusb_dev *ir;
789 int buf_len;
790
791 if (!urb)
792 return;
793
794 ir = urb->context;
795 if (!ir) {
796 usb_unlink_urb(urb);
797 return;
798 }
799
800 buf_len = urb->actual_length;
801
Jarod Wilson66e89522010-06-01 17:32:08 -0300802 if (debug)
803 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
804
805 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
806 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300807 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300808 buf_len);
809 }
810
811 switch (urb->status) {
812 /* success */
813 case 0:
814 mceusb_process_ir_data(ir, buf_len);
815 break;
816
817 case -ECONNRESET:
818 case -ENOENT:
819 case -ESHUTDOWN:
820 usb_unlink_urb(urb);
821 return;
822
823 case -EPIPE:
824 default:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300825 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300826 break;
827 }
828
829 usb_submit_urb(urb, GFP_ATOMIC);
830}
831
832static void mceusb_gen1_init(struct mceusb_dev *ir)
833{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300834 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300835 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300836 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300837 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300838
839 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
840 if (!data) {
841 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300842 return;
843 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300844
845 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300846 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300847 * on the receive control pipe and expect a certain value pair back
848 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300849 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
850 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300851 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300852 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
853 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
854 __func__, data[0], data[1]);
855
856 /* set feature: bit rate 38400 bps */
857 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
858 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
859 0xc04e, 0x0000, NULL, 0, HZ * 3);
860
861 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
862
863 /* bRequest 4: set char length to 8 bits */
864 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
865 4, USB_TYPE_VENDOR,
866 0x0808, 0x0000, NULL, 0, HZ * 3);
867 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
868
869 /* bRequest 2: set handshaking to use DTR/DSR */
870 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
871 2, USB_TYPE_VENDOR,
872 0x0000, 0x0100, NULL, 0, HZ * 3);
873 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300874
Jarod Wilson22b07662010-07-08 12:38:57 -0300875 /* device reset */
876 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
877 mce_sync_in(ir, NULL, maxp);
878
879 /* get hw/sw revision? */
880 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
881 mce_sync_in(ir, NULL, maxp);
882
Jarod Wilson657290b2010-06-16 17:10:05 -0300883 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -0300884};
885
886static void mceusb_gen2_init(struct mceusb_dev *ir)
887{
888 int maxp = ir->len_in;
889
Jarod Wilson66e89522010-06-01 17:32:08 -0300890 /* device reset */
891 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
892 mce_sync_in(ir, NULL, maxp);
893
894 /* get hw/sw revision? */
895 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
896 mce_sync_in(ir, NULL, maxp);
897
Jarod Wilson22b07662010-07-08 12:38:57 -0300898 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -0300899 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
900 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -0300901 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
902 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -0300903}
904
Jarod Wilson22b07662010-07-08 12:38:57 -0300905static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -0300906{
907 int maxp = ir->len_in;
908
Jarod Wilson66e89522010-06-01 17:32:08 -0300909 /* get the carrier and frequency */
910 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
911 mce_sync_in(ir, NULL, maxp);
912
913 /* get the transmitter bitmask */
914 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
915 mce_sync_in(ir, NULL, maxp);
916
917 /* get receiver timeout value */
918 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
919 mce_sync_in(ir, NULL, maxp);
920
921 /* get receiver sensor setting */
922 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
923 mce_sync_in(ir, NULL, maxp);
924}
925
926static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
927{
928 struct input_dev *idev;
929 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300930 struct device *dev = ir->dev;
931 int ret = -ENODEV;
932
933 idev = input_allocate_device();
934 if (!idev) {
935 dev_err(dev, "remote input dev allocation failed\n");
936 goto idev_alloc_failed;
937 }
938
939 ret = -ENOMEM;
940 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
941 if (!props) {
942 dev_err(dev, "remote ir dev props allocation failed\n");
943 goto props_alloc_failed;
944 }
945
Jarod Wilsone23fb962010-06-16 17:55:52 -0300946 snprintf(ir->name, sizeof(ir->name), "Media Center Ed. eHome "
Jarod Wilson66e89522010-06-01 17:32:08 -0300947 "Infrared Remote Transceiver (%04x:%04x)",
948 le16_to_cpu(ir->usbdev->descriptor.idVendor),
949 le16_to_cpu(ir->usbdev->descriptor.idProduct));
950
Jarod Wilson66e89522010-06-01 17:32:08 -0300951 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -0300952 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
953 strlcat(ir->phys, "/input0", sizeof(ir->phys));
954 idev->phys = ir->phys;
955
Jarod Wilson66e89522010-06-01 17:32:08 -0300956 props->priv = ir;
957 props->driver_type = RC_DRIVER_IR_RAW;
958 props->allowed_protos = IR_TYPE_ALL;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300959 props->s_tx_mask = mceusb_set_tx_mask;
960 props->s_tx_carrier = mceusb_set_tx_carrier;
961 props->tx_ir = mceusb_tx_ir;
Jarod Wilson66e89522010-06-01 17:32:08 -0300962
963 ir->props = props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300964
965 ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
966 if (ret < 0) {
967 dev_err(dev, "remote input device register failed\n");
968 goto irdev_failed;
969 }
970
971 return idev;
972
973irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -0300974 kfree(props);
975props_alloc_failed:
976 input_free_device(idev);
977idev_alloc_failed:
978 return NULL;
979}
980
981static int __devinit mceusb_dev_probe(struct usb_interface *intf,
982 const struct usb_device_id *id)
983{
984 struct usb_device *dev = interface_to_usbdev(intf);
985 struct usb_host_interface *idesc;
986 struct usb_endpoint_descriptor *ep = NULL;
987 struct usb_endpoint_descriptor *ep_in = NULL;
988 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -0300989 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -0300990 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -0300991 char buf[63], name[128] = "";
992 bool is_gen3;
993 bool is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300994 bool tx_mask_inverted;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300995 bool is_polaris;
Jarod Wilson66e89522010-06-01 17:32:08 -0300996
997 dev_dbg(&intf->dev, ": %s called\n", __func__);
998
Jarod Wilson66e89522010-06-01 17:32:08 -0300999 idesc = intf->cur_altsetting;
1000
1001 is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
1002 is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
Jarod Wilson657290b2010-06-16 17:10:05 -03001003 tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001004 is_polaris = usb_match_id(intf, cx_polaris_list) ? 1 : 0;
1005
1006 if (is_polaris) {
1007 /* Interface 0 is IR */
1008 if (idesc->desc.bInterfaceNumber)
1009 return -ENODEV;
1010 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001011
1012 /* step through the endpoints to find first bulk in and out endpoint */
1013 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1014 ep = &idesc->endpoint[i].desc;
1015
1016 if ((ep_in == NULL)
1017 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1018 == USB_DIR_IN)
1019 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1020 == USB_ENDPOINT_XFER_BULK)
1021 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1022 == USB_ENDPOINT_XFER_INT))) {
1023
Jarod Wilson66e89522010-06-01 17:32:08 -03001024 ep_in = ep;
1025 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001026 ep_in->bInterval = 1;
1027 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
1028 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001029 }
1030
1031 if ((ep_out == NULL)
1032 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1033 == USB_DIR_OUT)
1034 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1035 == USB_ENDPOINT_XFER_BULK)
1036 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1037 == USB_ENDPOINT_XFER_INT))) {
1038
Jarod Wilson66e89522010-06-01 17:32:08 -03001039 ep_out = ep;
1040 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001041 ep_out->bInterval = 1;
1042 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
1043 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001044 }
1045 }
1046 if (ep_in == NULL) {
1047 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
1048 return -ENODEV;
1049 }
1050
1051 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1052 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1053
1054 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1055 if (!ir)
1056 goto mem_alloc_fail;
1057
1058 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1059 if (!ir->buf_in)
1060 goto buf_in_alloc_fail;
1061
1062 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1063 if (!ir->urb_in)
1064 goto urb_in_alloc_fail;
1065
1066 ir->usbdev = dev;
1067 ir->dev = &intf->dev;
1068 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001069 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001070 ir->flags.tx_mask_inverted = tx_mask_inverted;
Maxim Levitsky46519182010-10-16 19:56:28 -03001071 init_ir_raw_event(&ir->rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001072
1073 /* Saving usb interface data for use by the transmitter routine */
1074 ir->usb_ep_in = ep_in;
1075 ir->usb_ep_out = ep_out;
1076
1077 if (dev->descriptor.iManufacturer
1078 && usb_string(dev, dev->descriptor.iManufacturer,
1079 buf, sizeof(buf)) > 0)
1080 strlcpy(name, buf, sizeof(name));
1081 if (dev->descriptor.iProduct
1082 && usb_string(dev, dev->descriptor.iProduct,
1083 buf, sizeof(buf)) > 0)
1084 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1085 " %s", buf);
1086
1087 ir->idev = mceusb_init_input_dev(ir);
1088 if (!ir->idev)
1089 goto input_dev_fail;
1090
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001091 /* flush buffers on the device */
1092 mce_sync_in(ir, NULL, maxp);
1093 mce_sync_in(ir, NULL, maxp);
1094
1095 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001096 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1097 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1098 ir->urb_in->transfer_dma = ir->dma_in;
1099 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1100
Jarod Wilson66e89522010-06-01 17:32:08 -03001101 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001102 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001103 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001104 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001105 mceusb_gen2_init(ir);
1106
Jarod Wilson22b07662010-07-08 12:38:57 -03001107 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001108
Jarod Wilson657290b2010-06-16 17:10:05 -03001109 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001110
1111 usb_set_intfdata(intf, ir);
1112
1113 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1114 dev->bus->busnum, dev->devnum);
1115
1116 return 0;
1117
1118 /* Error-handling path */
1119input_dev_fail:
1120 usb_free_urb(ir->urb_in);
1121urb_in_alloc_fail:
1122 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1123buf_in_alloc_fail:
1124 kfree(ir);
1125mem_alloc_fail:
1126 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1127
1128 return -ENOMEM;
1129}
1130
1131
1132static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1133{
1134 struct usb_device *dev = interface_to_usbdev(intf);
1135 struct mceusb_dev *ir = usb_get_intfdata(intf);
1136
1137 usb_set_intfdata(intf, NULL);
1138
1139 if (!ir)
1140 return;
1141
1142 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001143 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001144 usb_kill_urb(ir->urb_in);
1145 usb_free_urb(ir->urb_in);
1146 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1147
1148 kfree(ir);
1149}
1150
1151static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1152{
1153 struct mceusb_dev *ir = usb_get_intfdata(intf);
1154 dev_info(ir->dev, "suspend\n");
1155 usb_kill_urb(ir->urb_in);
1156 return 0;
1157}
1158
1159static int mceusb_dev_resume(struct usb_interface *intf)
1160{
1161 struct mceusb_dev *ir = usb_get_intfdata(intf);
1162 dev_info(ir->dev, "resume\n");
1163 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1164 return -EIO;
1165 return 0;
1166}
1167
1168static struct usb_driver mceusb_dev_driver = {
1169 .name = DRIVER_NAME,
1170 .probe = mceusb_dev_probe,
1171 .disconnect = mceusb_dev_disconnect,
1172 .suspend = mceusb_dev_suspend,
1173 .resume = mceusb_dev_resume,
1174 .reset_resume = mceusb_dev_resume,
1175 .id_table = mceusb_dev_table
1176};
1177
1178static int __init mceusb_dev_init(void)
1179{
1180 int ret;
1181
1182 ret = usb_register(&mceusb_dev_driver);
1183 if (ret < 0)
1184 printk(KERN_ERR DRIVER_NAME
1185 ": usb register failed, result = %d\n", ret);
1186
1187 return ret;
1188}
1189
1190static void __exit mceusb_dev_exit(void)
1191{
1192 usb_deregister(&mceusb_dev_driver);
1193}
1194
1195module_init(mceusb_dev_init);
1196module_exit(mceusb_dev_exit);
1197
1198MODULE_DESCRIPTION(DRIVER_DESC);
1199MODULE_AUTHOR(DRIVER_AUTHOR);
1200MODULE_LICENSE("GPL");
1201MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1202
1203module_param(debug, bool, S_IRUGO | S_IWUSR);
1204MODULE_PARM_DESC(debug, "Debug enabled or not");