]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/IR/mceusb.c
V4L/DVB: IR/mceusb: kill pinnacle-device-specific nonsense
[karo-tx-linux.git] / drivers / media / IR / mceusb.c
1 /*
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 */
55 #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 */
57
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  0x1F /* Packet length mask */
72
73
74 /* module parameters */
75 #ifdef CONFIG_USB_DEBUG
76 static int debug = 1;
77 #else
78 static 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
113 static 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
206 static struct usb_device_id gen3_list[] = {
207         { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
208         { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
209         {}
210 };
211
212 static struct usb_device_id microsoft_gen1_list[] = {
213         { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
214         {}
215 };
216
217 static struct usb_device_id std_tx_mask_list[] = {
218         { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
219         { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
220         { USB_DEVICE(VENDOR_SMK, 0x031d) },
221         { USB_DEVICE(VENDOR_SMK, 0x0322) },
222         { USB_DEVICE(VENDOR_SMK, 0x0334) },
223         { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
224         { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
225         { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
226         { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
227         { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
228         { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
229         { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
230         {}
231 };
232
233 /* data structure for each usb transceiver */
234 struct mceusb_dev {
235         /* ir-core bits */
236         struct ir_input_dev *irdev;
237         struct ir_dev_props *props;
238         struct ir_raw_event rawir;
239
240         /* core device bits */
241         struct device *dev;
242         struct input_dev *idev;
243
244         /* usb */
245         struct usb_device *usbdev;
246         struct urb *urb_in;
247         struct usb_endpoint_descriptor *usb_ep_in;
248         struct usb_endpoint_descriptor *usb_ep_out;
249
250         /* buffers and dma */
251         unsigned char *buf_in;
252         unsigned int len_in;
253         u8 cmd;         /* MCE command type */
254         u8 rem;         /* Remaining IR data bytes in packet */
255         dma_addr_t dma_in;
256         dma_addr_t dma_out;
257
258         struct {
259                 u32 connected:1;
260                 u32 tx_mask_inverted:1;
261                 u32 microsoft_gen1:1;
262                 u32 gen3:1;
263                 u32 reserved:28;
264         } flags;
265
266         /* handle sending (init strings) */
267         int send_flags;
268         int carrier;
269
270         char name[128];
271         char phys[64];
272
273         unsigned char tx_mask;
274 };
275
276 /*
277  * MCE Device Command Strings
278  * Device command responses vary from device to device...
279  * - DEVICE_RESET resets the hardware to its default state
280  * - GET_REVISION fetches the hardware/software revision, common
281  *   replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
282  * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
283  *   device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
284  *   meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
285  *   ((clk / frequency) - 1)
286  * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
287  *   response in the form of 9f 0c msb lsb
288  * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
289  *   the form of 9f 08 bm, where bm is the bitmask
290  * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
291  *   general use one or short-range learning one, in the form of
292  *   9f 14 ss, where ss is either 01 for long-range or 02 for short
293  * - SET_CARRIER_FREQ sets a new carrier mode and frequency
294  * - SET_TX_BITMASK sets the transmitter bitmask
295  * - SET_RX_TIMEOUT sets the receiver timeout
296  * - SET_RX_SENSOR sets which receiver sensor to use
297  */
298 static char DEVICE_RESET[]      = {0x00, 0xff, 0xaa};
299 static char GET_REVISION[]      = {0xff, 0x0b};
300 static char GET_UNKNOWN[]       = {0xff, 0x18};
301 static char GET_CARRIER_FREQ[]  = {0x9f, 0x07};
302 static char GET_RX_TIMEOUT[]    = {0x9f, 0x0d};
303 static char GET_TX_BITMASK[]    = {0x9f, 0x13};
304 static char GET_RX_SENSOR[]     = {0x9f, 0x15};
305 /* sub in desired values in lower byte or bytes for full command */
306 /* FIXME: make use of these for transmit.
307 static char SET_CARRIER_FREQ[]  = {0x9f, 0x06, 0x00, 0x00};
308 static char SET_TX_BITMASK[]    = {0x9f, 0x08, 0x00};
309 static char SET_RX_TIMEOUT[]    = {0x9f, 0x0c, 0x00, 0x00};
310 static char SET_RX_SENSOR[]     = {0x9f, 0x14, 0x00};
311 */
312
313 static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
314                                  int len, bool out)
315 {
316         char codes[USB_BUFLEN * 3 + 1];
317         char inout[9];
318         int i;
319         u8 cmd, subcmd, data1, data2;
320         struct device *dev = ir->dev;
321         int idx = 0;
322
323         /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
324         if (ir->flags.microsoft_gen1 && !out)
325                 idx = 2;
326
327         if (len <= idx)
328                 return;
329
330         for (i = 0; i < len && i < USB_BUFLEN; i++)
331                 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
332
333         dev_info(dev, "%sx data: %s (length=%d)\n",
334                  (out ? "t" : "r"), codes, len);
335
336         if (out)
337                 strcpy(inout, "Request\0");
338         else
339                 strcpy(inout, "Got\0");
340
341         cmd    = buf[idx] & 0xff;
342         subcmd = buf[idx + 1] & 0xff;
343         data1  = buf[idx + 2] & 0xff;
344         data2  = buf[idx + 3] & 0xff;
345
346         switch (cmd) {
347         case 0x00:
348                 if (subcmd == 0xff && data1 == 0xaa)
349                         dev_info(dev, "Device reset requested\n");
350                 else
351                         dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
352                                  cmd, subcmd);
353                 break;
354         case 0xff:
355                 switch (subcmd) {
356                 case 0x0b:
357                         if (len == 2)
358                                 dev_info(dev, "Get hw/sw rev?\n");
359                         else
360                                 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
361                                          "0x%02x 0x%02x\n", data1, data2,
362                                          buf[idx + 4], buf[idx + 5]);
363                         break;
364                 case 0xaa:
365                         dev_info(dev, "Device reset requested\n");
366                         break;
367                 case 0xfe:
368                         dev_info(dev, "Previous command not supported\n");
369                         break;
370                 case 0x18:
371                 case 0x1b:
372                 default:
373                         dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
374                                  cmd, subcmd);
375                         break;
376                 }
377                 break;
378         case 0x9f:
379                 switch (subcmd) {
380                 case 0x03:
381                         dev_info(dev, "Ping\n");
382                         break;
383                 case 0x04:
384                         dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
385                                  data1, data2);
386                         break;
387                 case 0x06:
388                         dev_info(dev, "%s carrier mode and freq of "
389                                  "0x%02x 0x%02x\n", inout, data1, data2);
390                         break;
391                 case 0x07:
392                         dev_info(dev, "Get carrier mode and freq\n");
393                         break;
394                 case 0x08:
395                         dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
396                                  inout, data1);
397                         break;
398                 case 0x0c:
399                         /* value is in units of 50us, so x*50/100 or x/2 ms */
400                         dev_info(dev, "%s receive timeout of %d ms\n",
401                                  inout, ((data1 << 8) | data2) / 2);
402                         break;
403                 case 0x0d:
404                         dev_info(dev, "Get receive timeout\n");
405                         break;
406                 case 0x13:
407                         dev_info(dev, "Get transmit blaster mask\n");
408                         break;
409                 case 0x14:
410                         dev_info(dev, "%s %s-range receive sensor in use\n",
411                                  inout, data1 == 0x02 ? "short" : "long");
412                         break;
413                 case 0x15:
414                         if (len == 2)
415                                 dev_info(dev, "Get receive sensor\n");
416                         else
417                                 dev_info(dev, "Received pulse count is %d\n",
418                                          ((data1 << 8) | data2));
419                         break;
420                 case 0xfe:
421                         dev_info(dev, "Error! Hardware is likely wedged...\n");
422                         break;
423                 case 0x05:
424                 case 0x09:
425                 case 0x0f:
426                 default:
427                         dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
428                                  cmd, subcmd);
429                         break;
430                 }
431                 break;
432         default:
433                 break;
434         }
435 }
436
437 static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
438 {
439         struct mceusb_dev *ir;
440         int len;
441
442         if (!urb)
443                 return;
444
445         ir = urb->context;
446         if (ir) {
447                 len = urb->actual_length;
448
449                 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
450                         urb->status, len);
451
452                 if (debug)
453                         mceusb_dev_printdata(ir, urb->transfer_buffer,
454                                              len, true);
455         }
456
457 }
458
459 /* request incoming or send outgoing usb packet - used to initialize remote */
460 static void mce_request_packet(struct mceusb_dev *ir,
461                                struct usb_endpoint_descriptor *ep,
462                                unsigned char *data, int size, int urb_type)
463 {
464         int res;
465         struct urb *async_urb;
466         struct device *dev = ir->dev;
467         unsigned char *async_buf;
468
469         if (urb_type == MCEUSB_TX) {
470                 async_urb = usb_alloc_urb(0, GFP_KERNEL);
471                 if (unlikely(!async_urb)) {
472                         dev_err(dev, "Error, couldn't allocate urb!\n");
473                         return;
474                 }
475
476                 async_buf = kzalloc(size, GFP_KERNEL);
477                 if (!async_buf) {
478                         dev_err(dev, "Error, couldn't allocate buf!\n");
479                         usb_free_urb(async_urb);
480                         return;
481                 }
482
483                 /* outbound data */
484                 usb_fill_int_urb(async_urb, ir->usbdev,
485                         usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
486                         async_buf, size, (usb_complete_t) usb_async_callback,
487                         ir, ep->bInterval);
488                 memcpy(async_buf, data, size);
489
490         } else if (urb_type == MCEUSB_RX) {
491                 /* standard request */
492                 async_urb = ir->urb_in;
493                 ir->send_flags = RECV_FLAG_IN_PROGRESS;
494
495         } else {
496                 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
497                 return;
498         }
499
500         dev_dbg(dev, "receive request called (size=%#x)\n", size);
501
502         async_urb->transfer_buffer_length = size;
503         async_urb->dev = ir->usbdev;
504
505         res = usb_submit_urb(async_urb, GFP_ATOMIC);
506         if (res) {
507                 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
508                 return;
509         }
510         dev_dbg(dev, "receive request complete (res=%d)\n", res);
511 }
512
513 static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
514 {
515         mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
516 }
517
518 static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
519 {
520         mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
521 }
522
523 /* Sets active IR outputs -- mce devices typically (all?) have two */
524 static int mceusb_set_tx_mask(void *priv, u32 mask)
525 {
526         struct mceusb_dev *ir = priv;
527
528         if (ir->flags.tx_mask_inverted)
529                 ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
530         else
531                 ir->tx_mask = mask;
532
533         return 0;
534 }
535
536 static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
537 {
538         struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
539         int i, start_index = 0;
540         u8 hdr = MCE_CONTROL_HEADER;
541
542         /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
543         if (ir->flags.microsoft_gen1)
544                 start_index = 2;
545
546         for (i = start_index; i < buf_len;) {
547                 if (ir->rem == 0) {
548                         /* decode mce packets of the form (84),AA,BB,CC,DD */
549                         /* IR data packets can span USB messages - rem */
550                         hdr = ir->buf_in[i];
551                         ir->rem = (hdr & MCE_PACKET_LENGTH_MASK);
552                         ir->cmd = (hdr & ~MCE_PACKET_LENGTH_MASK);
553                         dev_dbg(ir->dev, "New data. rem: 0x%02x, cmd: 0x%02x\n",
554                                 ir->rem, ir->cmd);
555                         i++;
556                 }
557
558                 /* don't process MCE commands */
559                 if (hdr == MCE_CONTROL_HEADER || hdr == 0xff) {
560                         ir->rem = 0;
561                         return;
562                 }
563
564                 for (; (ir->rem > 0) && (i < buf_len); i++) {
565                         ir->rem--;
566
567                         rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
568                         rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
569                                          * MCE_TIME_UNIT * 1000;
570
571                         if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
572                                 if (ir->rawir.pulse == rawir.pulse)
573                                         ir->rawir.duration += rawir.duration;
574                                 else {
575                                         ir->rawir.duration = rawir.duration;
576                                         ir->rawir.pulse = rawir.pulse;
577                                 }
578                                 continue;
579                         }
580                         rawir.duration += ir->rawir.duration;
581                         ir->rawir.duration = 0;
582                         ir->rawir.pulse = rawir.pulse;
583
584                         dev_dbg(ir->dev, "Storing %s with duration %d\n",
585                                 rawir.pulse ? "pulse" : "space",
586                                 rawir.duration);
587
588                         ir_raw_event_store(ir->idev, &rawir);
589                 }
590
591                 if (ir->buf_in[i] == 0x80 || ir->buf_in[i] == 0x9f)
592                         ir->rem = 0;
593
594                 dev_dbg(ir->dev, "calling ir_raw_event_handle\n");
595                 ir_raw_event_handle(ir->idev);
596         }
597 }
598
599 static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
600 {
601         struct mceusb_dev *ir;
602         int buf_len;
603
604         if (!urb)
605                 return;
606
607         ir = urb->context;
608         if (!ir) {
609                 usb_unlink_urb(urb);
610                 return;
611         }
612
613         buf_len = urb->actual_length;
614
615         if (debug)
616                 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
617
618         if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
619                 ir->send_flags = SEND_FLAG_COMPLETE;
620                 dev_dbg(&ir->irdev->dev, "setup answer received %d bytes\n",
621                         buf_len);
622         }
623
624         switch (urb->status) {
625         /* success */
626         case 0:
627                 mceusb_process_ir_data(ir, buf_len);
628                 break;
629
630         case -ECONNRESET:
631         case -ENOENT:
632         case -ESHUTDOWN:
633                 usb_unlink_urb(urb);
634                 return;
635
636         case -EPIPE:
637         default:
638                 break;
639         }
640
641         usb_submit_urb(urb, GFP_ATOMIC);
642 }
643
644 static void mceusb_gen1_init(struct mceusb_dev *ir)
645 {
646         int i, ret;
647         int partial = 0;
648         struct device *dev = ir->dev;
649         char *junk, *data;
650
651         junk = kmalloc(2 * USB_BUFLEN, GFP_KERNEL);
652         if (!junk) {
653                 dev_err(dev, "%s: memory allocation failed!\n", __func__);
654                 return;
655         }
656
657         data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
658         if (!data) {
659                 dev_err(dev, "%s: memory allocation failed!\n", __func__);
660                 kfree(junk);
661                 return;
662         }
663
664         /*
665          * Clear off the first few messages. These look like calibration
666          * or test data, I can't really tell. This also flushes in case
667          * we have random ir data queued up.
668          */
669         for (i = 0; i < MCE_G1_INIT_MSGS; i++)
670                 usb_bulk_msg(ir->usbdev,
671                         usb_rcvbulkpipe(ir->usbdev,
672                                 ir->usb_ep_in->bEndpointAddress),
673                         junk, sizeof(junk), &partial, HZ * 10);
674
675         /* Get Status */
676         ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
677                               USB_REQ_GET_STATUS, USB_DIR_IN,
678                               0, 0, data, USB_CTRL_MSG_SZ, HZ * 3);
679
680         /*    ret = usb_get_status( ir->usbdev, 0, 0, data ); */
681         dev_dbg(dev, "%s - ret = %d status = 0x%x 0x%x\n", __func__,
682                 ret, data[0], data[1]);
683
684         /*
685          * This is a strange one. They issue a set address to the device
686          * on the receive control pipe and expect a certain value pair back
687          */
688         memset(data, 0, sizeof(data));
689
690         ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
691                               USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
692                               data, USB_CTRL_MSG_SZ, HZ * 3);
693         dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
694         dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
695                 __func__, data[0], data[1]);
696
697         /* set feature: bit rate 38400 bps */
698         ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
699                               USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
700                               0xc04e, 0x0000, NULL, 0, HZ * 3);
701
702         dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
703
704         /* bRequest 4: set char length to 8 bits */
705         ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
706                               4, USB_TYPE_VENDOR,
707                               0x0808, 0x0000, NULL, 0, HZ * 3);
708         dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
709
710         /* bRequest 2: set handshaking to use DTR/DSR */
711         ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
712                               2, USB_TYPE_VENDOR,
713                               0x0000, 0x0100, NULL, 0, HZ * 3);
714         dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
715
716         kfree(data);
717         kfree(junk);
718 };
719
720 static void mceusb_gen2_init(struct mceusb_dev *ir)
721 {
722         int maxp = ir->len_in;
723
724         mce_sync_in(ir, NULL, maxp);
725         mce_sync_in(ir, NULL, maxp);
726
727         /* device reset */
728         mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
729         mce_sync_in(ir, NULL, maxp);
730
731         /* get hw/sw revision? */
732         mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
733         mce_sync_in(ir, NULL, maxp);
734
735         /* unknown what this actually returns... */
736         mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
737         mce_sync_in(ir, NULL, maxp);
738 }
739
740 static void mceusb_gen3_init(struct mceusb_dev *ir)
741 {
742         int maxp = ir->len_in;
743
744         mce_sync_in(ir, NULL, maxp);
745
746         /* device reset */
747         mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
748         mce_sync_in(ir, NULL, maxp);
749
750         /* get the carrier and frequency */
751         mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
752         mce_sync_in(ir, NULL, maxp);
753
754         /* get the transmitter bitmask */
755         mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
756         mce_sync_in(ir, NULL, maxp);
757
758         /* get receiver timeout value */
759         mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
760         mce_sync_in(ir, NULL, maxp);
761
762         /* get receiver sensor setting */
763         mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
764         mce_sync_in(ir, NULL, maxp);
765 }
766
767 static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
768 {
769         struct input_dev *idev;
770         struct ir_dev_props *props;
771         struct ir_input_dev *irdev;
772         struct device *dev = ir->dev;
773         int ret = -ENODEV;
774
775         idev = input_allocate_device();
776         if (!idev) {
777                 dev_err(dev, "remote input dev allocation failed\n");
778                 goto idev_alloc_failed;
779         }
780
781         ret = -ENOMEM;
782         props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
783         if (!props) {
784                 dev_err(dev, "remote ir dev props allocation failed\n");
785                 goto props_alloc_failed;
786         }
787
788         irdev = kzalloc(sizeof(struct ir_input_dev), GFP_KERNEL);
789         if (!irdev) {
790                 dev_err(dev, "remote ir input dev allocation failed\n");
791                 goto ir_dev_alloc_failed;
792         }
793
794         snprintf(ir->name, sizeof(ir->name), "Media Center Edition eHome "
795                  "Infrared Remote Transceiver (%04x:%04x)",
796                  le16_to_cpu(ir->usbdev->descriptor.idVendor),
797                  le16_to_cpu(ir->usbdev->descriptor.idProduct));
798
799         idev->name = ir->name;
800         usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
801         strlcat(ir->phys, "/input0", sizeof(ir->phys));
802         idev->phys = ir->phys;
803
804         props->priv = ir;
805         props->driver_type = RC_DRIVER_IR_RAW;
806         props->allowed_protos = IR_TYPE_ALL;
807
808         ir->props = props;
809         ir->irdev = irdev;
810
811         input_set_drvdata(idev, irdev);
812
813         ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
814         if (ret < 0) {
815                 dev_err(dev, "remote input device register failed\n");
816                 goto irdev_failed;
817         }
818
819         return idev;
820
821 irdev_failed:
822         kfree(irdev);
823 ir_dev_alloc_failed:
824         kfree(props);
825 props_alloc_failed:
826         input_free_device(idev);
827 idev_alloc_failed:
828         return NULL;
829 }
830
831 static int __devinit mceusb_dev_probe(struct usb_interface *intf,
832                                       const struct usb_device_id *id)
833 {
834         struct usb_device *dev = interface_to_usbdev(intf);
835         struct usb_host_interface *idesc;
836         struct usb_endpoint_descriptor *ep = NULL;
837         struct usb_endpoint_descriptor *ep_in = NULL;
838         struct usb_endpoint_descriptor *ep_out = NULL;
839         struct usb_host_config *config;
840         struct mceusb_dev *ir = NULL;
841         int pipe, maxp, i;
842         char buf[63], name[128] = "";
843         bool is_gen3;
844         bool is_microsoft_gen1;
845         bool tx_mask_inverted;
846
847         dev_dbg(&intf->dev, ": %s called\n", __func__);
848
849         usb_reset_device(dev);
850
851         config = dev->actconfig;
852         idesc  = intf->cur_altsetting;
853
854         is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
855         is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
856         tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1;
857
858         /* step through the endpoints to find first bulk in and out endpoint */
859         for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
860                 ep = &idesc->endpoint[i].desc;
861
862                 if ((ep_in == NULL)
863                         && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
864                             == USB_DIR_IN)
865                         && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
866                             == USB_ENDPOINT_XFER_BULK)
867                         || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
868                             == USB_ENDPOINT_XFER_INT))) {
869
870                         ep_in = ep;
871                         ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
872                         ep_in->bInterval = 1;
873                         dev_dbg(&intf->dev, ": acceptable inbound endpoint "
874                                 "found\n");
875                 }
876
877                 if ((ep_out == NULL)
878                         && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
879                             == USB_DIR_OUT)
880                         && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
881                             == USB_ENDPOINT_XFER_BULK)
882                         || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
883                             == USB_ENDPOINT_XFER_INT))) {
884
885                         ep_out = ep;
886                         ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
887                         ep_out->bInterval = 1;
888                         dev_dbg(&intf->dev, ": acceptable outbound endpoint "
889                                 "found\n");
890                 }
891         }
892         if (ep_in == NULL) {
893                 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
894                 return -ENODEV;
895         }
896
897         pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
898         maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
899
900         ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
901         if (!ir)
902                 goto mem_alloc_fail;
903
904         ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
905         if (!ir->buf_in)
906                 goto buf_in_alloc_fail;
907
908         ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
909         if (!ir->urb_in)
910                 goto urb_in_alloc_fail;
911
912         ir->usbdev = dev;
913         ir->dev = &intf->dev;
914         ir->len_in = maxp;
915         ir->flags.gen3 = is_gen3;
916         ir->flags.microsoft_gen1 = is_microsoft_gen1;
917         ir->flags.tx_mask_inverted = tx_mask_inverted;
918
919         /* Saving usb interface data for use by the transmitter routine */
920         ir->usb_ep_in = ep_in;
921         ir->usb_ep_out = ep_out;
922
923         if (dev->descriptor.iManufacturer
924             && usb_string(dev, dev->descriptor.iManufacturer,
925                           buf, sizeof(buf)) > 0)
926                 strlcpy(name, buf, sizeof(name));
927         if (dev->descriptor.iProduct
928             && usb_string(dev, dev->descriptor.iProduct,
929                           buf, sizeof(buf)) > 0)
930                 snprintf(name + strlen(name), sizeof(name) - strlen(name),
931                          " %s", buf);
932
933         ir->idev = mceusb_init_input_dev(ir);
934         if (!ir->idev)
935                 goto input_dev_fail;
936
937         /* inbound data */
938         usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
939                 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
940         ir->urb_in->transfer_dma = ir->dma_in;
941         ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
942
943         /* initialize device */
944         if (ir->flags.gen3)
945                 mceusb_gen3_init(ir);
946
947         else if (ir->flags.microsoft_gen1)
948                 mceusb_gen1_init(ir);
949
950         else
951                 mceusb_gen2_init(ir);
952
953         mce_sync_in(ir, NULL, maxp);
954
955         mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
956
957         usb_set_intfdata(intf, ir);
958
959         dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
960                  dev->bus->busnum, dev->devnum);
961
962         return 0;
963
964         /* Error-handling path */
965 input_dev_fail:
966         usb_free_urb(ir->urb_in);
967 urb_in_alloc_fail:
968         usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
969 buf_in_alloc_fail:
970         kfree(ir);
971 mem_alloc_fail:
972         dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
973
974         return -ENOMEM;
975 }
976
977
978 static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
979 {
980         struct usb_device *dev = interface_to_usbdev(intf);
981         struct mceusb_dev *ir = usb_get_intfdata(intf);
982
983         usb_set_intfdata(intf, NULL);
984
985         if (!ir)
986                 return;
987
988         ir->usbdev = NULL;
989         ir_input_unregister(ir->idev);
990         usb_kill_urb(ir->urb_in);
991         usb_free_urb(ir->urb_in);
992         usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
993
994         kfree(ir);
995 }
996
997 static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
998 {
999         struct mceusb_dev *ir = usb_get_intfdata(intf);
1000         dev_info(ir->dev, "suspend\n");
1001         usb_kill_urb(ir->urb_in);
1002         return 0;
1003 }
1004
1005 static int mceusb_dev_resume(struct usb_interface *intf)
1006 {
1007         struct mceusb_dev *ir = usb_get_intfdata(intf);
1008         dev_info(ir->dev, "resume\n");
1009         if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1010                 return -EIO;
1011         return 0;
1012 }
1013
1014 static struct usb_driver mceusb_dev_driver = {
1015         .name =         DRIVER_NAME,
1016         .probe =        mceusb_dev_probe,
1017         .disconnect =   mceusb_dev_disconnect,
1018         .suspend =      mceusb_dev_suspend,
1019         .resume =       mceusb_dev_resume,
1020         .reset_resume = mceusb_dev_resume,
1021         .id_table =     mceusb_dev_table
1022 };
1023
1024 static int __init mceusb_dev_init(void)
1025 {
1026         int ret;
1027
1028         ret = usb_register(&mceusb_dev_driver);
1029         if (ret < 0)
1030                 printk(KERN_ERR DRIVER_NAME
1031                        ": usb register failed, result = %d\n", ret);
1032
1033         return ret;
1034 }
1035
1036 static void __exit mceusb_dev_exit(void)
1037 {
1038         usb_deregister(&mceusb_dev_driver);
1039 }
1040
1041 module_init(mceusb_dev_init);
1042 module_exit(mceusb_dev_exit);
1043
1044 MODULE_DESCRIPTION(DRIVER_DESC);
1045 MODULE_AUTHOR(DRIVER_AUTHOR);
1046 MODULE_LICENSE("GPL");
1047 MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1048
1049 module_param(debug, bool, S_IRUGO | S_IWUSR);
1050 MODULE_PARM_DESC(debug, "Debug enabled or not");