blob: 756f7186edb848f3190bb10ce640d1ca27239937 [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 *
18 * TODO:
19 * - add rc-core transmit support, once available
20 * - enable support for forthcoming ir-lirc-codec interface
21 *
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 *
37 */
38
39#include <linux/device.h>
40#include <linux/module.h>
41#include <linux/slab.h>
42#include <linux/usb.h>
43#include <linux/input.h>
44#include <media/ir-core.h>
45#include <media/ir-common.h>
46
47#define DRIVER_VERSION "1.91"
48#define DRIVER_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
49#define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
50 "device driver"
51#define DRIVER_NAME "mceusb"
52
53#define USB_BUFLEN 32 /* USB reception buffer length */
54#define IRBUF_SIZE 256 /* IR work buffer length */
Jarod Wilson657290b2010-06-16 17:10:05 -030055#define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
56#define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
Jarod Wilson66e89522010-06-01 17:32:08 -030057
58/* MCE constants */
59#define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
60#define MCE_TIME_UNIT 50 /* Approx 50us resolution */
61#define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
62#define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
63#define MCE_PACKET_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
64#define MCE_CONTROL_HEADER 0x9F /* MCE status header */
65#define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
66#define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
67#define MCE_DEFAULT_TX_MASK 0x03 /* Val opts: TX1=0x01, TX2=0x02, ALL=0x03 */
68#define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
69#define MCE_PULSE_MASK 0x7F /* Pulse mask */
70#define MCE_MAX_PULSE_LENGTH 0x7F /* Longest transmittable pulse symbol */
71#define MCE_PACKET_LENGTH_MASK 0xF /* Packet length mask */
72
73
74/* module parameters */
75#ifdef CONFIG_USB_DEBUG
76static int debug = 1;
77#else
78static int debug;
79#endif
80
81/* general constants */
82#define SEND_FLAG_IN_PROGRESS 1
83#define SEND_FLAG_COMPLETE 2
84#define RECV_FLAG_IN_PROGRESS 3
85#define RECV_FLAG_COMPLETE 4
86
87#define MCEUSB_RX 1
88#define MCEUSB_TX 2
89
90#define VENDOR_PHILIPS 0x0471
91#define VENDOR_SMK 0x0609
92#define VENDOR_TATUNG 0x1460
93#define VENDOR_GATEWAY 0x107b
94#define VENDOR_SHUTTLE 0x1308
95#define VENDOR_SHUTTLE2 0x051c
96#define VENDOR_MITSUMI 0x03ee
97#define VENDOR_TOPSEED 0x1784
98#define VENDOR_RICAVISION 0x179d
99#define VENDOR_ITRON 0x195d
100#define VENDOR_FIC 0x1509
101#define VENDOR_LG 0x043e
102#define VENDOR_MICROSOFT 0x045e
103#define VENDOR_FORMOSA 0x147a
104#define VENDOR_FINTEK 0x1934
105#define VENDOR_PINNACLE 0x2304
106#define VENDOR_ECS 0x1019
107#define VENDOR_WISTRON 0x0fb8
108#define VENDOR_COMPRO 0x185b
109#define VENDOR_NORTHSTAR 0x04eb
110#define VENDOR_REALTEK 0x0bda
111#define VENDOR_TIVO 0x105a
112
113static struct usb_device_id mceusb_dev_table[] = {
114 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
115 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
116 /* Philips Infrared Transceiver - Sahara branded */
117 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
118 /* Philips Infrared Transceiver - HP branded */
119 { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
120 /* Philips SRM5100 */
121 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
122 /* Philips Infrared Transceiver - Omaura */
123 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
124 /* Philips Infrared Transceiver - Spinel plus */
125 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
126 /* Philips eHome Infrared Transceiver */
127 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
128 /* Realtek MCE IR Receiver */
129 { USB_DEVICE(VENDOR_REALTEK, 0x0161) },
130 /* SMK/Toshiba G83C0004D410 */
131 { USB_DEVICE(VENDOR_SMK, 0x031d) },
132 /* SMK eHome Infrared Transceiver (Sony VAIO) */
133 { USB_DEVICE(VENDOR_SMK, 0x0322) },
134 /* bundled with Hauppauge PVR-150 */
135 { USB_DEVICE(VENDOR_SMK, 0x0334) },
136 /* SMK eHome Infrared Transceiver */
137 { USB_DEVICE(VENDOR_SMK, 0x0338) },
138 /* Tatung eHome Infrared Transceiver */
139 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
140 /* Shuttle eHome Infrared Transceiver */
141 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
142 /* Shuttle eHome Infrared Transceiver */
143 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
144 /* Gateway eHome Infrared Transceiver */
145 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
146 /* Mitsumi */
147 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
148 /* Topseed eHome Infrared Transceiver */
149 { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
150 /* Topseed HP eHome Infrared Transceiver */
151 { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
152 /* Topseed eHome Infrared Transceiver */
153 { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
154 /* Topseed eHome Infrared Transceiver */
155 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
156 /* Topseed eHome Infrared Transceiver */
157 { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
158 /* Topseed eHome Infrared Transceiver */
159 { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
160 /* Ricavision internal Infrared Transceiver */
161 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
162 /* Itron ione Libra Q-11 */
163 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
164 /* FIC eHome Infrared Transceiver */
165 { USB_DEVICE(VENDOR_FIC, 0x9242) },
166 /* LG eHome Infrared Transceiver */
167 { USB_DEVICE(VENDOR_LG, 0x9803) },
168 /* Microsoft MCE Infrared Transceiver */
169 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
170 /* Formosa eHome Infrared Transceiver */
171 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
172 /* Formosa21 / eHome Infrared Receiver */
173 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
174 /* Formosa aim / Trust MCE Infrared Receiver */
175 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
176 /* Formosa Industrial Computing / Beanbag Emulation Device */
177 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
178 /* Formosa21 / eHome Infrared Receiver */
179 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
180 /* Formosa Industrial Computing AIM IR605/A */
181 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
182 /* Formosa Industrial Computing */
183 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
184 /* Fintek eHome Infrared Transceiver */
185 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
186 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
187 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
188 /* Pinnacle Remote Kit */
189 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
190 /* Elitegroup Computer Systems IR */
191 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
192 /* Wistron Corp. eHome Infrared Receiver */
193 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
194 /* Compro K100 */
195 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
196 /* Compro K100 v2 */
197 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
198 /* Northstar Systems, Inc. eHome Infrared Transceiver */
199 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
200 /* TiVo PC IR Receiver */
201 { USB_DEVICE(VENDOR_TIVO, 0x2000) },
202 /* Terminating entry */
203 { }
204};
205
206static struct usb_device_id gen3_list[] = {
207 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
208 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
209 {}
210};
211
212static struct usb_device_id pinnacle_list[] = {
213 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
214 {}
215};
216
217static struct usb_device_id microsoft_gen1_list[] = {
218 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
219 {}
220};
221
Jarod Wilson657290b2010-06-16 17:10:05 -0300222static struct usb_device_id std_tx_mask_list[] = {
223 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
224 { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
225 { USB_DEVICE(VENDOR_SMK, 0x031d) },
226 { USB_DEVICE(VENDOR_SMK, 0x0322) },
227 { USB_DEVICE(VENDOR_SMK, 0x0334) },
228 { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
229 { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
230 { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
231 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
232 { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
233 { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
234 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
235 {}
236};
237
Jarod Wilson66e89522010-06-01 17:32:08 -0300238/* data structure for each usb transceiver */
239struct mceusb_dev {
240 /* ir-core bits */
241 struct ir_input_dev *irdev;
242 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300243 struct ir_raw_event rawir;
244
245 /* core device bits */
246 struct device *dev;
247 struct input_dev *idev;
248
249 /* usb */
250 struct usb_device *usbdev;
251 struct urb *urb_in;
252 struct usb_endpoint_descriptor *usb_ep_in;
253 struct usb_endpoint_descriptor *usb_ep_out;
254
255 /* buffers and dma */
256 unsigned char *buf_in;
257 unsigned int len_in;
258 u8 cmd; /* MCE command type */
259 u8 rem; /* Remaining IR data bytes in packet */
260 dma_addr_t dma_in;
261 dma_addr_t dma_out;
262
263 struct {
264 u32 connected:1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300265 u32 tx_mask_inverted:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300266 u32 microsoft_gen1:1;
267 u32 gen3:1;
268 u32 reserved:28;
269 } flags;
270
271 /* handle sending (init strings) */
272 int send_flags;
273 int carrier;
274
275 char name[128];
276 char phys[64];
277
Jarod Wilson657290b2010-06-16 17:10:05 -0300278 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300279};
280
281/*
282 * MCE Device Command Strings
283 * Device command responses vary from device to device...
284 * - DEVICE_RESET resets the hardware to its default state
285 * - GET_REVISION fetches the hardware/software revision, common
286 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
287 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
288 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
289 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
290 * ((clk / frequency) - 1)
291 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
292 * response in the form of 9f 0c msb lsb
293 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
294 * the form of 9f 08 bm, where bm is the bitmask
295 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
296 * general use one or short-range learning one, in the form of
297 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
298 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
299 * - SET_TX_BITMASK sets the transmitter bitmask
300 * - SET_RX_TIMEOUT sets the receiver timeout
301 * - SET_RX_SENSOR sets which receiver sensor to use
302 */
303static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
304static char GET_REVISION[] = {0xff, 0x0b};
305static char GET_UNKNOWN[] = {0xff, 0x18};
306static char GET_CARRIER_FREQ[] = {0x9f, 0x07};
307static char GET_RX_TIMEOUT[] = {0x9f, 0x0d};
308static char GET_TX_BITMASK[] = {0x9f, 0x13};
309static char GET_RX_SENSOR[] = {0x9f, 0x15};
310/* sub in desired values in lower byte or bytes for full command */
311/* FIXME: make use of these for transmit.
312static char SET_CARRIER_FREQ[] = {0x9f, 0x06, 0x00, 0x00};
313static char SET_TX_BITMASK[] = {0x9f, 0x08, 0x00};
314static char SET_RX_TIMEOUT[] = {0x9f, 0x0c, 0x00, 0x00};
315static char SET_RX_SENSOR[] = {0x9f, 0x14, 0x00};
316*/
317
318static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
319 int len, bool out)
320{
321 char codes[USB_BUFLEN * 3 + 1];
322 char inout[9];
323 int i;
324 u8 cmd, subcmd, data1, data2;
325 struct device *dev = ir->dev;
Jarod Wilson657290b2010-06-16 17:10:05 -0300326 int idx = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300327
Jarod Wilson657290b2010-06-16 17:10:05 -0300328 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
329 if (ir->flags.microsoft_gen1 && !out)
330 idx = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300331
Jarod Wilson657290b2010-06-16 17:10:05 -0300332 if (len <= idx)
Jarod Wilson66e89522010-06-01 17:32:08 -0300333 return;
334
335 for (i = 0; i < len && i < USB_BUFLEN; i++)
336 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
337
338 dev_info(dev, "%sx data: %s (length=%d)\n",
339 (out ? "t" : "r"), codes, len);
340
341 if (out)
342 strcpy(inout, "Request\0");
343 else
344 strcpy(inout, "Got\0");
345
Jarod Wilson657290b2010-06-16 17:10:05 -0300346 cmd = buf[idx] & 0xff;
347 subcmd = buf[idx + 1] & 0xff;
348 data1 = buf[idx + 2] & 0xff;
349 data2 = buf[idx + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300350
351 switch (cmd) {
352 case 0x00:
353 if (subcmd == 0xff && data1 == 0xaa)
354 dev_info(dev, "Device reset requested\n");
355 else
356 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
357 cmd, subcmd);
358 break;
359 case 0xff:
360 switch (subcmd) {
361 case 0x0b:
362 if (len == 2)
363 dev_info(dev, "Get hw/sw rev?\n");
364 else
365 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
366 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson657290b2010-06-16 17:10:05 -0300367 buf[idx + 4], buf[idx + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300368 break;
369 case 0xaa:
370 dev_info(dev, "Device reset requested\n");
371 break;
372 case 0xfe:
373 dev_info(dev, "Previous command not supported\n");
374 break;
375 case 0x18:
376 case 0x1b:
377 default:
378 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
379 cmd, subcmd);
380 break;
381 }
382 break;
383 case 0x9f:
384 switch (subcmd) {
385 case 0x03:
386 dev_info(dev, "Ping\n");
387 break;
388 case 0x04:
389 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
390 data1, data2);
391 break;
392 case 0x06:
393 dev_info(dev, "%s carrier mode and freq of "
394 "0x%02x 0x%02x\n", inout, data1, data2);
395 break;
396 case 0x07:
397 dev_info(dev, "Get carrier mode and freq\n");
398 break;
399 case 0x08:
400 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
401 inout, data1);
402 break;
403 case 0x0c:
404 /* value is in units of 50us, so x*50/100 or x/2 ms */
405 dev_info(dev, "%s receive timeout of %d ms\n",
406 inout, ((data1 << 8) | data2) / 2);
407 break;
408 case 0x0d:
409 dev_info(dev, "Get receive timeout\n");
410 break;
411 case 0x13:
412 dev_info(dev, "Get transmit blaster mask\n");
413 break;
414 case 0x14:
415 dev_info(dev, "%s %s-range receive sensor in use\n",
416 inout, data1 == 0x02 ? "short" : "long");
417 break;
418 case 0x15:
419 if (len == 2)
420 dev_info(dev, "Get receive sensor\n");
421 else
422 dev_info(dev, "Received pulse count is %d\n",
423 ((data1 << 8) | data2));
424 break;
425 case 0xfe:
426 dev_info(dev, "Error! Hardware is likely wedged...\n");
427 break;
428 case 0x05:
429 case 0x09:
430 case 0x0f:
431 default:
432 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
433 cmd, subcmd);
434 break;
435 }
436 break;
437 default:
438 break;
439 }
440}
441
442static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
443{
444 struct mceusb_dev *ir;
445 int len;
446
447 if (!urb)
448 return;
449
450 ir = urb->context;
451 if (ir) {
452 len = urb->actual_length;
453
454 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
455 urb->status, len);
456
457 if (debug)
458 mceusb_dev_printdata(ir, urb->transfer_buffer,
459 len, true);
460 }
461
462}
463
464/* request incoming or send outgoing usb packet - used to initialize remote */
465static void mce_request_packet(struct mceusb_dev *ir,
466 struct usb_endpoint_descriptor *ep,
467 unsigned char *data, int size, int urb_type)
468{
469 int res;
470 struct urb *async_urb;
471 struct device *dev = ir->dev;
472 unsigned char *async_buf;
473
474 if (urb_type == MCEUSB_TX) {
475 async_urb = usb_alloc_urb(0, GFP_KERNEL);
476 if (unlikely(!async_urb)) {
477 dev_err(dev, "Error, couldn't allocate urb!\n");
478 return;
479 }
480
481 async_buf = kzalloc(size, GFP_KERNEL);
482 if (!async_buf) {
483 dev_err(dev, "Error, couldn't allocate buf!\n");
484 usb_free_urb(async_urb);
485 return;
486 }
487
488 /* outbound data */
489 usb_fill_int_urb(async_urb, ir->usbdev,
490 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
491 async_buf, size, (usb_complete_t) usb_async_callback,
492 ir, ep->bInterval);
493 memcpy(async_buf, data, size);
494
495 } else if (urb_type == MCEUSB_RX) {
496 /* standard request */
497 async_urb = ir->urb_in;
498 ir->send_flags = RECV_FLAG_IN_PROGRESS;
499
500 } else {
501 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
502 return;
503 }
504
505 dev_dbg(dev, "receive request called (size=%#x)\n", size);
506
507 async_urb->transfer_buffer_length = size;
508 async_urb->dev = ir->usbdev;
509
510 res = usb_submit_urb(async_urb, GFP_ATOMIC);
511 if (res) {
512 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
513 return;
514 }
515 dev_dbg(dev, "receive request complete (res=%d)\n", res);
516}
517
518static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
519{
520 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
521}
522
523static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
524{
525 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
526}
527
Jarod Wilson657290b2010-06-16 17:10:05 -0300528/* Sets active IR outputs -- mce devices typically (all?) have two */
529static int mceusb_set_tx_mask(void *priv, u32 mask)
530{
531 struct mceusb_dev *ir = priv;
532
533 if (ir->flags.tx_mask_inverted)
534 ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
535 else
536 ir->tx_mask = mask;
537
538 return 0;
539}
540
Jarod Wilson66e89522010-06-01 17:32:08 -0300541static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
542{
543 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
544 int i, start_index = 0;
545
546 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
547 if (ir->flags.microsoft_gen1)
548 start_index = 2;
549
550 for (i = start_index; i < buf_len;) {
551 if (ir->rem == 0) {
552 /* decode mce packets of the form (84),AA,BB,CC,DD */
553 /* IR data packets can span USB messages - rem */
554 ir->rem = (ir->buf_in[i] & MCE_PACKET_LENGTH_MASK);
555 ir->cmd = (ir->buf_in[i] & ~MCE_PACKET_LENGTH_MASK);
556 dev_dbg(ir->dev, "New data. rem: 0x%02x, cmd: 0x%02x\n",
557 ir->rem, ir->cmd);
558 i++;
559 }
560
561 /* Only cmd 0x8<bytes> is IR data, don't process MCE commands */
562 if (ir->cmd != 0x80) {
563 ir->rem = 0;
564 return;
565 }
566
567 for (; (ir->rem > 0) && (i < buf_len); i++) {
568 ir->rem--;
569
570 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
571 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
572 * MCE_TIME_UNIT * 1000;
573
574 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
575 if (ir->rawir.pulse == rawir.pulse)
576 ir->rawir.duration += rawir.duration;
577 else {
578 ir->rawir.duration = rawir.duration;
579 ir->rawir.pulse = rawir.pulse;
580 }
581 continue;
582 }
583 rawir.duration += ir->rawir.duration;
584 ir->rawir.duration = 0;
585 ir->rawir.pulse = rawir.pulse;
586
587 dev_dbg(ir->dev, "Storing %s with duration %d\n",
588 rawir.pulse ? "pulse" : "space",
589 rawir.duration);
590
591 ir_raw_event_store(ir->idev, &rawir);
592 }
593
594 if (ir->buf_in[i] == 0x80 || ir->buf_in[i] == 0x9f)
595 ir->rem = 0;
596
597 dev_dbg(ir->dev, "calling ir_raw_event_handle\n");
598 ir_raw_event_handle(ir->idev);
599 }
600}
601
Jarod Wilson66e89522010-06-01 17:32:08 -0300602static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
603{
604 struct mceusb_dev *ir;
605 int buf_len;
606
607 if (!urb)
608 return;
609
610 ir = urb->context;
611 if (!ir) {
612 usb_unlink_urb(urb);
613 return;
614 }
615
616 buf_len = urb->actual_length;
617
Jarod Wilson66e89522010-06-01 17:32:08 -0300618 if (debug)
619 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
620
621 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
622 ir->send_flags = SEND_FLAG_COMPLETE;
623 dev_dbg(&ir->irdev->dev, "setup answer received %d bytes\n",
624 buf_len);
625 }
626
627 switch (urb->status) {
628 /* success */
629 case 0:
630 mceusb_process_ir_data(ir, buf_len);
631 break;
632
633 case -ECONNRESET:
634 case -ENOENT:
635 case -ESHUTDOWN:
636 usb_unlink_urb(urb);
637 return;
638
639 case -EPIPE:
640 default:
641 break;
642 }
643
644 usb_submit_urb(urb, GFP_ATOMIC);
645}
646
647static void mceusb_gen1_init(struct mceusb_dev *ir)
648{
649 int i, ret;
Jarod Wilson66e89522010-06-01 17:32:08 -0300650 int partial = 0;
651 struct device *dev = ir->dev;
Jarod Wilson657290b2010-06-16 17:10:05 -0300652 char *junk, *data;
653
654 junk = kmalloc(2 * USB_BUFLEN, GFP_KERNEL);
655 if (!junk) {
656 dev_err(dev, "%s: memory allocation failed!\n", __func__);
657 return;
658 }
659
660 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
661 if (!data) {
662 dev_err(dev, "%s: memory allocation failed!\n", __func__);
663 kfree(junk);
664 return;
665 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300666
667 /*
668 * Clear off the first few messages. These look like calibration
669 * or test data, I can't really tell. This also flushes in case
670 * we have random ir data queued up.
671 */
Jarod Wilson657290b2010-06-16 17:10:05 -0300672 for (i = 0; i < MCE_G1_INIT_MSGS; i++)
Jarod Wilson66e89522010-06-01 17:32:08 -0300673 usb_bulk_msg(ir->usbdev,
674 usb_rcvbulkpipe(ir->usbdev,
675 ir->usb_ep_in->bEndpointAddress),
Jarod Wilson657290b2010-06-16 17:10:05 -0300676 junk, sizeof(junk), &partial, HZ * 10);
Jarod Wilson66e89522010-06-01 17:32:08 -0300677
678 /* Get Status */
679 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
680 USB_REQ_GET_STATUS, USB_DIR_IN,
Jarod Wilson657290b2010-06-16 17:10:05 -0300681 0, 0, data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300682
683 /* ret = usb_get_status( ir->usbdev, 0, 0, data ); */
684 dev_dbg(dev, "%s - ret = %d status = 0x%x 0x%x\n", __func__,
685 ret, data[0], data[1]);
686
687 /*
688 * This is a strange one. They issue a set address to the device
689 * on the receive control pipe and expect a certain value pair back
690 */
Jarod Wilson657290b2010-06-16 17:10:05 -0300691 memset(data, 0, sizeof(data));
Jarod Wilson66e89522010-06-01 17:32:08 -0300692
693 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
694 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300695 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300696 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
697 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
698 __func__, data[0], data[1]);
699
700 /* set feature: bit rate 38400 bps */
701 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
702 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
703 0xc04e, 0x0000, NULL, 0, HZ * 3);
704
705 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
706
707 /* bRequest 4: set char length to 8 bits */
708 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
709 4, USB_TYPE_VENDOR,
710 0x0808, 0x0000, NULL, 0, HZ * 3);
711 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
712
713 /* bRequest 2: set handshaking to use DTR/DSR */
714 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
715 2, USB_TYPE_VENDOR,
716 0x0000, 0x0100, NULL, 0, HZ * 3);
717 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300718
719 kfree(data);
720 kfree(junk);
Jarod Wilson66e89522010-06-01 17:32:08 -0300721};
722
723static void mceusb_gen2_init(struct mceusb_dev *ir)
724{
725 int maxp = ir->len_in;
726
727 mce_sync_in(ir, NULL, maxp);
728 mce_sync_in(ir, NULL, maxp);
729
730 /* device reset */
731 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
732 mce_sync_in(ir, NULL, maxp);
733
734 /* get hw/sw revision? */
735 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
736 mce_sync_in(ir, NULL, maxp);
737
738 /* unknown what this actually returns... */
739 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
740 mce_sync_in(ir, NULL, maxp);
741}
742
743static void mceusb_gen3_init(struct mceusb_dev *ir)
744{
745 int maxp = ir->len_in;
746
747 mce_sync_in(ir, NULL, maxp);
748
749 /* device reset */
750 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
751 mce_sync_in(ir, NULL, maxp);
752
753 /* get the carrier and frequency */
754 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
755 mce_sync_in(ir, NULL, maxp);
756
757 /* get the transmitter bitmask */
758 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
759 mce_sync_in(ir, NULL, maxp);
760
761 /* get receiver timeout value */
762 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
763 mce_sync_in(ir, NULL, maxp);
764
765 /* get receiver sensor setting */
766 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
767 mce_sync_in(ir, NULL, maxp);
768}
769
770static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
771{
772 struct input_dev *idev;
773 struct ir_dev_props *props;
774 struct ir_input_dev *irdev;
Jarod Wilson66e89522010-06-01 17:32:08 -0300775 struct device *dev = ir->dev;
776 int ret = -ENODEV;
777
778 idev = input_allocate_device();
779 if (!idev) {
780 dev_err(dev, "remote input dev allocation failed\n");
781 goto idev_alloc_failed;
782 }
783
784 ret = -ENOMEM;
785 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
786 if (!props) {
787 dev_err(dev, "remote ir dev props allocation failed\n");
788 goto props_alloc_failed;
789 }
790
791 irdev = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL);
792 if (!irdev) {
793 dev_err(dev, "remote ir input dev allocation failed\n");
794 goto ir_dev_alloc_failed;
795 }
796
Jarod Wilson66e89522010-06-01 17:32:08 -0300797 snprintf(ir->name, sizeof(ir->name), "Media Center Edition eHome "
798 "Infrared Remote Transceiver (%04x:%04x)",
799 le16_to_cpu(ir->usbdev->descriptor.idVendor),
800 le16_to_cpu(ir->usbdev->descriptor.idProduct));
801
Jarod Wilson66e89522010-06-01 17:32:08 -0300802 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -0300803 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
804 strlcat(ir->phys, "/input0", sizeof(ir->phys));
805 idev->phys = ir->phys;
806
Jarod Wilson66e89522010-06-01 17:32:08 -0300807 props->priv = ir;
808 props->driver_type = RC_DRIVER_IR_RAW;
809 props->allowed_protos = IR_TYPE_ALL;
810
811 ir->props = props;
812 ir->irdev = irdev;
Jarod Wilson66e89522010-06-01 17:32:08 -0300813
814 input_set_drvdata(idev, irdev);
815
816 ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
817 if (ret < 0) {
818 dev_err(dev, "remote input device register failed\n");
819 goto irdev_failed;
820 }
821
822 return idev;
823
824irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -0300825 kfree(irdev);
826ir_dev_alloc_failed:
827 kfree(props);
828props_alloc_failed:
829 input_free_device(idev);
830idev_alloc_failed:
831 return NULL;
832}
833
834static int __devinit mceusb_dev_probe(struct usb_interface *intf,
835 const struct usb_device_id *id)
836{
837 struct usb_device *dev = interface_to_usbdev(intf);
838 struct usb_host_interface *idesc;
839 struct usb_endpoint_descriptor *ep = NULL;
840 struct usb_endpoint_descriptor *ep_in = NULL;
841 struct usb_endpoint_descriptor *ep_out = NULL;
842 struct usb_host_config *config;
843 struct mceusb_dev *ir = NULL;
844 int pipe, maxp;
845 int i, ret;
846 char buf[63], name[128] = "";
847 bool is_gen3;
848 bool is_microsoft_gen1;
849 bool is_pinnacle;
Jarod Wilson657290b2010-06-16 17:10:05 -0300850 bool tx_mask_inverted;
Jarod Wilson66e89522010-06-01 17:32:08 -0300851
852 dev_dbg(&intf->dev, ": %s called\n", __func__);
853
854 usb_reset_device(dev);
855
856 config = dev->actconfig;
857 idesc = intf->cur_altsetting;
858
859 is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
860 is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
861 is_pinnacle = usb_match_id(intf, pinnacle_list) ? 1 : 0;
Jarod Wilson657290b2010-06-16 17:10:05 -0300862 tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300863
864 /* step through the endpoints to find first bulk in and out endpoint */
865 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
866 ep = &idesc->endpoint[i].desc;
867
868 if ((ep_in == NULL)
869 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
870 == USB_DIR_IN)
871 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
872 == USB_ENDPOINT_XFER_BULK)
873 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
874 == USB_ENDPOINT_XFER_INT))) {
875
876 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
877 "found\n");
878 ep_in = ep;
879 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
880 if (!is_pinnacle)
881 /*
882 * Ideally, we'd use what the device offers up,
883 * but that leads to non-functioning first and
884 * second-gen devices, and many devices have an
885 * invalid bInterval of 0. Pinnacle devices
886 * don't work witha bInterval of 1 though.
887 */
888 ep_in->bInterval = 1;
889 }
890
891 if ((ep_out == NULL)
892 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
893 == USB_DIR_OUT)
894 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
895 == USB_ENDPOINT_XFER_BULK)
896 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
897 == USB_ENDPOINT_XFER_INT))) {
898
899 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
900 "found\n");
901 ep_out = ep;
902 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
903 if (!is_pinnacle)
904 /*
905 * Ideally, we'd use what the device offers up,
906 * but that leads to non-functioning first and
907 * second-gen devices, and many devices have an
908 * invalid bInterval of 0. Pinnacle devices
909 * don't work witha bInterval of 1 though.
910 */
911 ep_out->bInterval = 1;
912 }
913 }
914 if (ep_in == NULL) {
915 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
916 return -ENODEV;
917 }
918
919 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
920 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
921
922 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
923 if (!ir)
924 goto mem_alloc_fail;
925
926 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
927 if (!ir->buf_in)
928 goto buf_in_alloc_fail;
929
930 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
931 if (!ir->urb_in)
932 goto urb_in_alloc_fail;
933
934 ir->usbdev = dev;
935 ir->dev = &intf->dev;
936 ir->len_in = maxp;
937 ir->flags.gen3 = is_gen3;
938 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300939 ir->flags.tx_mask_inverted = tx_mask_inverted;
Jarod Wilson66e89522010-06-01 17:32:08 -0300940
941 /* Saving usb interface data for use by the transmitter routine */
942 ir->usb_ep_in = ep_in;
943 ir->usb_ep_out = ep_out;
944
945 if (dev->descriptor.iManufacturer
946 && usb_string(dev, dev->descriptor.iManufacturer,
947 buf, sizeof(buf)) > 0)
948 strlcpy(name, buf, sizeof(name));
949 if (dev->descriptor.iProduct
950 && usb_string(dev, dev->descriptor.iProduct,
951 buf, sizeof(buf)) > 0)
952 snprintf(name + strlen(name), sizeof(name) - strlen(name),
953 " %s", buf);
954
955 ir->idev = mceusb_init_input_dev(ir);
956 if (!ir->idev)
957 goto input_dev_fail;
958
959 /* inbound data */
960 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
961 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
962 ir->urb_in->transfer_dma = ir->dma_in;
963 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
964
965 if (is_pinnacle) {
966 /*
967 * I have no idea why but this reset seems to be crucial to
968 * getting the device to do outbound IO correctly - without
969 * this the device seems to hang, ignoring all input - although
970 * IR signals are correctly sent from the device, no input is
971 * interpreted by the device and the host never does the
972 * completion routine
973 */
974 ret = usb_reset_configuration(dev);
975 dev_info(&intf->dev, "usb reset config ret %x\n", ret);
976 }
977
978 /* initialize device */
979 if (ir->flags.gen3)
980 mceusb_gen3_init(ir);
981
982 else if (ir->flags.microsoft_gen1)
983 mceusb_gen1_init(ir);
984
985 else
986 mceusb_gen2_init(ir);
987
988 mce_sync_in(ir, NULL, maxp);
989
Jarod Wilson657290b2010-06-16 17:10:05 -0300990 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -0300991
992 usb_set_intfdata(intf, ir);
993
994 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
995 dev->bus->busnum, dev->devnum);
996
997 return 0;
998
999 /* Error-handling path */
1000input_dev_fail:
1001 usb_free_urb(ir->urb_in);
1002urb_in_alloc_fail:
1003 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1004buf_in_alloc_fail:
1005 kfree(ir);
1006mem_alloc_fail:
1007 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1008
1009 return -ENOMEM;
1010}
1011
1012
1013static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1014{
1015 struct usb_device *dev = interface_to_usbdev(intf);
1016 struct mceusb_dev *ir = usb_get_intfdata(intf);
1017
1018 usb_set_intfdata(intf, NULL);
1019
1020 if (!ir)
1021 return;
1022
1023 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001024 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001025 usb_kill_urb(ir->urb_in);
1026 usb_free_urb(ir->urb_in);
1027 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1028
1029 kfree(ir);
1030}
1031
1032static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1033{
1034 struct mceusb_dev *ir = usb_get_intfdata(intf);
1035 dev_info(ir->dev, "suspend\n");
1036 usb_kill_urb(ir->urb_in);
1037 return 0;
1038}
1039
1040static int mceusb_dev_resume(struct usb_interface *intf)
1041{
1042 struct mceusb_dev *ir = usb_get_intfdata(intf);
1043 dev_info(ir->dev, "resume\n");
1044 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1045 return -EIO;
1046 return 0;
1047}
1048
1049static struct usb_driver mceusb_dev_driver = {
1050 .name = DRIVER_NAME,
1051 .probe = mceusb_dev_probe,
1052 .disconnect = mceusb_dev_disconnect,
1053 .suspend = mceusb_dev_suspend,
1054 .resume = mceusb_dev_resume,
1055 .reset_resume = mceusb_dev_resume,
1056 .id_table = mceusb_dev_table
1057};
1058
1059static int __init mceusb_dev_init(void)
1060{
1061 int ret;
1062
1063 ret = usb_register(&mceusb_dev_driver);
1064 if (ret < 0)
1065 printk(KERN_ERR DRIVER_NAME
1066 ": usb register failed, result = %d\n", ret);
1067
1068 return ret;
1069}
1070
1071static void __exit mceusb_dev_exit(void)
1072{
1073 usb_deregister(&mceusb_dev_driver);
1074}
1075
1076module_init(mceusb_dev_init);
1077module_exit(mceusb_dev_exit);
1078
1079MODULE_DESCRIPTION(DRIVER_DESC);
1080MODULE_AUTHOR(DRIVER_AUTHOR);
1081MODULE_LICENSE("GPL");
1082MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1083
1084module_param(debug, bool, S_IRUGO | S_IWUSR);
1085MODULE_PARM_DESC(debug, "Debug enabled or not");