2 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
4 * Copyright (c) 2010 by Jarod Wilson <jarod@redhat.com>
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.
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.
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.
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
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>
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 " \
47 #define DRIVER_NAME "mceusb"
49 #define USB_BUFLEN 32 /* USB reception buffer length */
50 #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 */
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 */
59 #define MCE_CONTROL_HEADER 0x9f /* MCE status header */
60 #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 */
64 #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 */
71 /* module parameters */
72 #ifdef CONFIG_USB_DEBUG
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
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
109 #define VENDOR_CONEXANT 0x0572
111 static 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) },
126 /* 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) },
130 /* 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) },
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) },
197 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
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) },
205 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1) },
206 /* Terminating entry */
210 static struct usb_device_id gen3_list[] = {
211 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
212 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
216 static struct usb_device_id microsoft_gen1_list[] = {
217 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
221 static 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) },
237 static struct usb_device_id cx_polaris_list[] = {
238 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1) },
242 /* data structure for each usb transceiver */
245 struct ir_dev_props *props;
246 struct ir_raw_event rawir;
248 /* core device bits */
250 struct input_dev *idev;
253 struct usb_device *usbdev;
255 struct usb_endpoint_descriptor *usb_ep_in;
256 struct usb_endpoint_descriptor *usb_ep_out;
258 /* buffers and dma */
259 unsigned char *buf_in;
268 u8 cmd, rem; /* Remaining IR data bytes in packet */
275 u32 tx_mask_inverted:1;
276 u32 microsoft_gen1:1;
280 /* transmit support */
283 unsigned char tx_mask;
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
311 static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
312 static char GET_REVISION[] = {0xff, 0x0b};
313 static char GET_UNKNOWN[] = {0xff, 0x18};
314 static char GET_UNKNOWN2[] = {MCE_CONTROL_HEADER, 0x05};
315 static char GET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x07};
316 static char GET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0d};
317 static char GET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x13};
318 static char GET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x15};
319 /* sub in desired values in lower byte or bytes for full command */
320 /* FIXME: make use of these for transmit.
321 static char SET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x06, 0x00, 0x00};
322 static char SET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x08, 0x00};
323 static char SET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0c, 0x00, 0x00};
324 static char SET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x14, 0x00};
327 static int mceusb_cmdsize(u8 cmd, u8 subcmd)
342 case MCE_CONTROL_HEADER:
359 static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
362 char codes[USB_BUFLEN * 3 + 1];
365 u8 cmd, subcmd, data1, data2;
366 struct device *dev = ir->dev;
369 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
370 if (ir->flags.microsoft_gen1 && !out)
376 for (i = 0; i < len && i < USB_BUFLEN; i++)
377 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
379 dev_info(dev, "%sx data: %s (length=%d)\n",
380 (out ? "t" : "r"), codes, len);
383 strcpy(inout, "Request\0");
385 strcpy(inout, "Got\0");
387 cmd = buf[idx] & 0xff;
388 subcmd = buf[idx + 1] & 0xff;
389 data1 = buf[idx + 2] & 0xff;
390 data2 = buf[idx + 3] & 0xff;
394 if (subcmd == 0xff && data1 == 0xaa)
395 dev_info(dev, "Device reset requested\n");
397 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
404 dev_info(dev, "Get hw/sw rev?\n");
406 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
407 "0x%02x 0x%02x\n", data1, data2,
408 buf[idx + 4], buf[idx + 5]);
411 dev_info(dev, "Device reset requested\n");
414 dev_info(dev, "Previous command not supported\n");
419 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
424 case MCE_CONTROL_HEADER:
427 dev_info(dev, "Ping\n");
430 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
434 dev_info(dev, "%s carrier mode and freq of "
435 "0x%02x 0x%02x\n", inout, data1, data2);
438 dev_info(dev, "Get carrier mode and freq\n");
441 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
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);
450 dev_info(dev, "Get receive timeout\n");
453 dev_info(dev, "Get transmit blaster mask\n");
456 dev_info(dev, "%s %s-range receive sensor in use\n",
457 inout, data1 == 0x02 ? "short" : "long");
461 dev_info(dev, "Get receive sensor\n");
463 dev_info(dev, "Received pulse count is %d\n",
464 ((data1 << 8) | data2));
467 dev_info(dev, "Error! Hardware is likely wedged...\n");
473 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
483 static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
485 struct mceusb_dev *ir;
493 len = urb->actual_length;
495 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
499 mceusb_dev_printdata(ir, urb->transfer_buffer,
505 /* request incoming or send outgoing usb packet - used to initialize remote */
506 static void mce_request_packet(struct mceusb_dev *ir,
507 struct usb_endpoint_descriptor *ep,
508 unsigned char *data, int size, int urb_type)
511 struct urb *async_urb;
512 struct device *dev = ir->dev;
513 unsigned char *async_buf;
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");
522 async_buf = kzalloc(size, GFP_KERNEL);
524 dev_err(dev, "Error, couldn't allocate buf!\n");
525 usb_free_urb(async_urb);
530 usb_fill_int_urb(async_urb, ir->usbdev,
531 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
532 async_buf, size, (usb_complete_t)mce_async_callback,
534 memcpy(async_buf, data, size);
536 } else if (urb_type == MCEUSB_RX) {
537 /* standard request */
538 async_urb = ir->urb_in;
539 ir->send_flags = RECV_FLAG_IN_PROGRESS;
542 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
546 dev_dbg(dev, "receive request called (size=%#x)\n", size);
548 async_urb->transfer_buffer_length = size;
549 async_urb->dev = ir->usbdev;
551 res = usb_submit_urb(async_urb, GFP_ATOMIC);
553 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
556 dev_dbg(dev, "receive request complete (res=%d)\n", res);
559 static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
561 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
564 static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
566 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
569 /* Send data out the IR blaster port(s) */
570 static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
572 struct mceusb_dev *ir = priv;
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;
579 do_gettimeofday(&start_time);
581 count = n / sizeof(int);
583 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
587 /* MCE tx init header */
588 cmdbuf[cmdcount++] = MCE_CONTROL_HEADER;
589 cmdbuf[cmdcount++] = 0x08;
590 cmdbuf[cmdcount++] = ir->tx_mask;
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;
597 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
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;
605 /* Insert mce packet data */
606 if (cmdcount < MCE_CMDBUF_SIZE)
608 (txbuf[i] < MCE_PULSE_BIT ?
609 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
610 (i & 1 ? 0x00 : MCE_PULSE_BIT);
616 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
617 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
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;
624 /* Check if we have room for the empty packet at the end */
625 if (cmdcount >= MCE_CMDBUF_SIZE) {
630 /* All mce commands end with an empty packet (0x80) */
631 cmdbuf[cmdcount++] = 0x80;
633 /* Transmit the command to the mce device */
634 mce_async_out(ir, cmdbuf, cmdcount);
637 * The lircd gap calculation expects the write function to
638 * wait the time it takes for the ircommand to be sent before
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;
645 /* delay with the closest number of ticks */
646 set_current_state(TASK_INTERRUPTIBLE);
647 schedule_timeout(usecs_to_jiffies(signal_duration));
651 return ret ? ret : n;
654 /* Sets active IR outputs -- mce devices typically (all?) have two */
655 static int mceusb_set_tx_mask(void *priv, u32 mask)
657 struct mceusb_dev *ir = priv;
659 if (ir->flags.tx_mask_inverted)
660 ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
667 /* Sets the send carrier frequency and mode */
668 static int mceusb_set_tx_carrier(void *priv, u32 carrier)
670 struct mceusb_dev *ir = priv;
672 int prescaler = 0, divisor = 0;
673 unsigned char cmdbuf[4] = { MCE_CONTROL_HEADER, 0x06, 0x00, 0x00 };
675 /* Carrier has changed */
676 if (ir->carrier != carrier) {
679 ir->carrier = carrier;
682 dev_dbg(ir->dev, "%s: disabling carrier "
683 "modulation\n", __func__);
684 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
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;
694 dev_dbg(ir->dev, "%s: requesting %u HZ "
695 "carrier\n", __func__, carrier);
697 /* Transmit new carrier to mce device */
698 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
710 static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
712 DEFINE_IR_RAW_EVENT(rawir);
715 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
716 if (ir->flags.microsoft_gen1)
719 for (; i < buf_len; i++) {
720 switch (ir->parser_state) {
722 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
723 ir->parser_state = CMD_DATA;
727 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;
731 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
732 if (ir->rawir.pulse == rawir.pulse) {
733 ir->rawir.duration += rawir.duration;
735 ir->rawir.duration = rawir.duration;
736 ir->rawir.pulse = rawir.pulse;
741 rawir.duration += ir->rawir.duration;
742 ir->rawir.duration = 0;
743 ir->rawir.pulse = rawir.pulse;
745 dev_dbg(ir->dev, "Storing %s with duration %d\n",
746 rawir.pulse ? "pulse" : "space",
749 ir_raw_event_store(ir->idev, &rawir);
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;
763 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
764 dev_dbg(ir->dev, "Processing RX data: len = %d\n",
767 ir->parser_state = PARSE_IRDATA;
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
779 if (ir->parser_state != CMD_HEADER && !ir->rem)
780 ir->parser_state = CMD_HEADER;
782 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
783 ir_raw_event_handle(ir->idev);
786 static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
788 struct mceusb_dev *ir;
800 buf_len = urb->actual_length;
803 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
805 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
806 ir->send_flags = SEND_FLAG_COMPLETE;
807 dev_dbg(ir->dev, "setup answer received %d bytes\n",
811 switch (urb->status) {
814 mceusb_process_ir_data(ir, buf_len);
825 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
829 usb_submit_urb(urb, GFP_ATOMIC);
832 static void mceusb_gen1_init(struct mceusb_dev *ir)
835 int maxp = ir->len_in;
836 struct device *dev = ir->dev;
839 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
841 dev_err(dev, "%s: memory allocation failed!\n", __func__);
846 * This is a strange one. Windows issues a set address to the device
847 * on the receive control pipe and expect a certain value pair back
849 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
850 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
851 data, USB_CTRL_MSG_SZ, HZ * 3);
852 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]);
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);
861 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
863 /* bRequest 4: set char length to 8 bits */
864 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
866 0x0808, 0x0000, NULL, 0, HZ * 3);
867 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
869 /* bRequest 2: set handshaking to use DTR/DSR */
870 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
872 0x0000, 0x0100, NULL, 0, HZ * 3);
873 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
876 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
877 mce_sync_in(ir, NULL, maxp);
879 /* get hw/sw revision? */
880 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
881 mce_sync_in(ir, NULL, maxp);
886 static void mceusb_gen2_init(struct mceusb_dev *ir)
888 int maxp = ir->len_in;
891 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
892 mce_sync_in(ir, NULL, maxp);
894 /* get hw/sw revision? */
895 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
896 mce_sync_in(ir, NULL, maxp);
898 /* unknown what the next two actually return... */
899 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
900 mce_sync_in(ir, NULL, maxp);
901 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
902 mce_sync_in(ir, NULL, maxp);
905 static void mceusb_get_parameters(struct mceusb_dev *ir)
907 int maxp = ir->len_in;
909 /* get the carrier and frequency */
910 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
911 mce_sync_in(ir, NULL, maxp);
913 /* get the transmitter bitmask */
914 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
915 mce_sync_in(ir, NULL, maxp);
917 /* get receiver timeout value */
918 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
919 mce_sync_in(ir, NULL, maxp);
921 /* get receiver sensor setting */
922 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
923 mce_sync_in(ir, NULL, maxp);
926 static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
928 struct input_dev *idev;
929 struct ir_dev_props *props;
930 struct device *dev = ir->dev;
933 idev = input_allocate_device();
935 dev_err(dev, "remote input dev allocation failed\n");
936 goto idev_alloc_failed;
940 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
942 dev_err(dev, "remote ir dev props allocation failed\n");
943 goto props_alloc_failed;
946 snprintf(ir->name, sizeof(ir->name), "Media Center Ed. eHome "
947 "Infrared Remote Transceiver (%04x:%04x)",
948 le16_to_cpu(ir->usbdev->descriptor.idVendor),
949 le16_to_cpu(ir->usbdev->descriptor.idProduct));
951 idev->name = ir->name;
952 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
953 strlcat(ir->phys, "/input0", sizeof(ir->phys));
954 idev->phys = ir->phys;
957 props->driver_type = RC_DRIVER_IR_RAW;
958 props->allowed_protos = IR_TYPE_ALL;
959 props->s_tx_mask = mceusb_set_tx_mask;
960 props->s_tx_carrier = mceusb_set_tx_carrier;
961 props->tx_ir = mceusb_tx_ir;
965 ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
967 dev_err(dev, "remote input device register failed\n");
976 input_free_device(idev);
981 static int __devinit mceusb_dev_probe(struct usb_interface *intf,
982 const struct usb_device_id *id)
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;
989 struct mceusb_dev *ir = NULL;
991 char buf[63], name[128] = "";
993 bool is_microsoft_gen1;
994 bool tx_mask_inverted;
997 dev_dbg(&intf->dev, ": %s called\n", __func__);
999 idesc = intf->cur_altsetting;
1001 is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
1002 is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
1003 tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1;
1004 is_polaris = usb_match_id(intf, cx_polaris_list) ? 1 : 0;
1007 /* Interface 0 is IR */
1008 if (idesc->desc.bInterfaceNumber)
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;
1017 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1019 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1020 == USB_ENDPOINT_XFER_BULK)
1021 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1022 == USB_ENDPOINT_XFER_INT))) {
1025 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
1026 ep_in->bInterval = 1;
1027 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
1031 if ((ep_out == NULL)
1032 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1034 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1035 == USB_ENDPOINT_XFER_BULK)
1036 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1037 == USB_ENDPOINT_XFER_INT))) {
1040 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
1041 ep_out->bInterval = 1;
1042 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
1046 if (ep_in == NULL) {
1047 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
1051 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1052 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1054 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1056 goto mem_alloc_fail;
1058 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1060 goto buf_in_alloc_fail;
1062 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1064 goto urb_in_alloc_fail;
1067 ir->dev = &intf->dev;
1069 ir->flags.microsoft_gen1 = is_microsoft_gen1;
1070 ir->flags.tx_mask_inverted = tx_mask_inverted;
1071 init_ir_raw_event(&ir->rawir);
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;
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),
1087 ir->idev = mceusb_init_input_dev(ir);
1089 goto input_dev_fail;
1091 /* flush buffers on the device */
1092 mce_sync_in(ir, NULL, maxp);
1093 mce_sync_in(ir, NULL, maxp);
1095 /* wire up inbound data handler */
1096 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;
1101 /* initialize device */
1102 if (ir->flags.microsoft_gen1)
1103 mceusb_gen1_init(ir);
1105 mceusb_gen2_init(ir);
1107 mceusb_get_parameters(ir);
1109 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
1111 usb_set_intfdata(intf, ir);
1113 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1114 dev->bus->busnum, dev->devnum);
1118 /* Error-handling path */
1120 usb_free_urb(ir->urb_in);
1122 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1126 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1132 static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1134 struct usb_device *dev = interface_to_usbdev(intf);
1135 struct mceusb_dev *ir = usb_get_intfdata(intf);
1137 usb_set_intfdata(intf, NULL);
1143 ir_input_unregister(ir->idev);
1144 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);
1151 static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1153 struct mceusb_dev *ir = usb_get_intfdata(intf);
1154 dev_info(ir->dev, "suspend\n");
1155 usb_kill_urb(ir->urb_in);
1159 static int mceusb_dev_resume(struct usb_interface *intf)
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))
1168 static 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
1178 static int __init mceusb_dev_init(void)
1182 ret = usb_register(&mceusb_dev_driver);
1184 printk(KERN_ERR DRIVER_NAME
1185 ": usb register failed, result = %d\n", ret);
1190 static void __exit mceusb_dev_exit(void)
1192 usb_deregister(&mceusb_dev_driver);
1195 module_init(mceusb_dev_init);
1196 module_exit(mceusb_dev_exit);
1198 MODULE_DESCRIPTION(DRIVER_DESC);
1199 MODULE_AUTHOR(DRIVER_AUTHOR);
1200 MODULE_LICENSE("GPL");
1201 MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1203 module_param(debug, bool, S_IRUGO | S_IWUSR);
1204 MODULE_PARM_DESC(debug, "Debug enabled or not");