sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / media / rc / mceusb.c
blob044fb7a382d6bd888898cc889b78cdba15eeec90
1 /*
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/usb/input.h>
40 #include <media/rc-core.h>
42 #define DRIVER_VERSION "1.91"
43 #define DRIVER_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
44 #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
45 "device driver"
46 #define DRIVER_NAME "mceusb"
48 #define USB_BUFLEN 32 /* USB reception buffer length */
49 #define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
50 #define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
52 /* MCE constants */
53 #define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
54 #define MCE_TIME_UNIT 50 /* Approx 50us resolution */
55 #define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
56 #define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
57 #define MCE_IRDATA_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
58 #define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
59 #define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
60 #define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
61 #define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
62 #define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
63 #define MCE_PULSE_MASK 0x7f /* Pulse mask */
64 #define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
66 #define MCE_HW_CMD_HEADER 0xff /* MCE hardware command header */
67 #define MCE_COMMAND_HEADER 0x9f /* MCE command header */
68 #define MCE_COMMAND_MASK 0xe0 /* Mask out command bits */
69 #define MCE_COMMAND_NULL 0x00 /* These show up various places... */
70 /* if buf[i] & MCE_COMMAND_MASK == 0x80 and buf[i] != MCE_COMMAND_HEADER,
71 * then we're looking at a raw IR data sample */
72 #define MCE_COMMAND_IRDATA 0x80
73 #define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
75 /* Sub-commands, which follow MCE_COMMAND_HEADER or MCE_HW_CMD_HEADER */
76 #define MCE_CMD_SIG_END 0x01 /* End of signal */
77 #define MCE_CMD_PING 0x03 /* Ping device */
78 #define MCE_CMD_UNKNOWN 0x04 /* Unknown */
79 #define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
80 #define MCE_CMD_S_CARRIER 0x06 /* Set TX carrier frequency */
81 #define MCE_CMD_G_CARRIER 0x07 /* Get TX carrier frequency */
82 #define MCE_CMD_S_TXMASK 0x08 /* Set TX port bitmask */
83 #define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
84 #define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
85 #define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
86 #define MCE_CMD_S_TIMEOUT 0x0c /* Set RX timeout value */
87 #define MCE_CMD_G_TIMEOUT 0x0d /* Get RX timeout value */
88 #define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
89 #define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
90 #define MCE_CMD_G_RXPORTSTS 0x11 /* Get RX port status */
91 #define MCE_CMD_G_TXMASK 0x13 /* Set TX port bitmask */
92 #define MCE_CMD_S_RXSENSOR 0x14 /* Set RX sensor (std/learning) */
93 #define MCE_CMD_G_RXSENSOR 0x15 /* Get RX sensor (std/learning) */
94 #define MCE_RSP_PULSE_COUNT 0x15 /* RX pulse count (only if learning) */
95 #define MCE_CMD_TX_PORTS 0x16 /* Get number of TX ports */
96 #define MCE_CMD_G_WAKESRC 0x17 /* Get wake source */
97 #define MCE_CMD_UNKNOWN7 0x18 /* Unknown */
98 #define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
99 #define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
100 #define MCE_CMD_DEVICE_RESET 0xaa /* Reset the hardware */
101 #define MCE_RSP_CMD_INVALID 0xfe /* Invalid command issued */
104 /* module parameters */
105 #ifdef CONFIG_USB_DEBUG
106 static int debug = 1;
107 #else
108 static int debug;
109 #endif
111 /* general constants */
112 #define SEND_FLAG_IN_PROGRESS 1
113 #define SEND_FLAG_COMPLETE 2
114 #define RECV_FLAG_IN_PROGRESS 3
115 #define RECV_FLAG_COMPLETE 4
117 #define MCEUSB_RX 1
118 #define MCEUSB_TX 2
120 #define VENDOR_PHILIPS 0x0471
121 #define VENDOR_SMK 0x0609
122 #define VENDOR_TATUNG 0x1460
123 #define VENDOR_GATEWAY 0x107b
124 #define VENDOR_SHUTTLE 0x1308
125 #define VENDOR_SHUTTLE2 0x051c
126 #define VENDOR_MITSUMI 0x03ee
127 #define VENDOR_TOPSEED 0x1784
128 #define VENDOR_RICAVISION 0x179d
129 #define VENDOR_ITRON 0x195d
130 #define VENDOR_FIC 0x1509
131 #define VENDOR_LG 0x043e
132 #define VENDOR_MICROSOFT 0x045e
133 #define VENDOR_FORMOSA 0x147a
134 #define VENDOR_FINTEK 0x1934
135 #define VENDOR_PINNACLE 0x2304
136 #define VENDOR_ECS 0x1019
137 #define VENDOR_WISTRON 0x0fb8
138 #define VENDOR_COMPRO 0x185b
139 #define VENDOR_NORTHSTAR 0x04eb
140 #define VENDOR_REALTEK 0x0bda
141 #define VENDOR_TIVO 0x105a
142 #define VENDOR_CONEXANT 0x0572
144 enum mceusb_model_type {
145 MCE_GEN2 = 0, /* Most boards */
146 MCE_GEN1,
147 MCE_GEN3,
148 MCE_GEN2_TX_INV,
149 POLARIS_EVK,
150 CX_HYBRID_TV,
151 MULTIFUNCTION,
154 struct mceusb_model {
155 u32 mce_gen1:1;
156 u32 mce_gen2:1;
157 u32 mce_gen3:1;
158 u32 tx_mask_normal:1;
159 u32 no_tx:1;
161 int ir_intfnum;
163 const char *rc_map; /* Allow specify a per-board map */
164 const char *name; /* per-board name */
167 static const struct mceusb_model mceusb_model[] = {
168 [MCE_GEN1] = {
169 .mce_gen1 = 1,
170 .tx_mask_normal = 1,
172 [MCE_GEN2] = {
173 .mce_gen2 = 1,
175 [MCE_GEN2_TX_INV] = {
176 .mce_gen2 = 1,
177 .tx_mask_normal = 1,
179 [MCE_GEN3] = {
180 .mce_gen3 = 1,
181 .tx_mask_normal = 1,
183 [POLARIS_EVK] = {
185 * In fact, the EVK is shipped without
186 * remotes, but we should have something handy,
187 * to allow testing it
189 .rc_map = RC_MAP_HAUPPAUGE,
190 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
192 [CX_HYBRID_TV] = {
193 .no_tx = 1, /* tx isn't wired up at all */
194 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
196 [MULTIFUNCTION] = {
197 .mce_gen2 = 1,
198 .ir_intfnum = 2,
202 static struct usb_device_id mceusb_dev_table[] = {
203 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
204 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
205 .driver_info = MCE_GEN1 },
206 /* Philips Infrared Transceiver - Sahara branded */
207 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
208 /* Philips Infrared Transceiver - HP branded */
209 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
210 .driver_info = MCE_GEN2_TX_INV },
211 /* Philips SRM5100 */
212 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
213 /* Philips Infrared Transceiver - Omaura */
214 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
215 /* Philips Infrared Transceiver - Spinel plus */
216 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
217 /* Philips eHome Infrared Transceiver */
218 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
219 /* Philips/Spinel plus IR transceiver for ASUS */
220 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
221 /* Philips/Spinel plus IR transceiver for ASUS */
222 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
223 /* Realtek MCE IR Receiver and card reader */
224 { USB_DEVICE(VENDOR_REALTEK, 0x0161),
225 .driver_info = MULTIFUNCTION },
226 /* SMK/Toshiba G83C0004D410 */
227 { USB_DEVICE(VENDOR_SMK, 0x031d),
228 .driver_info = MCE_GEN2_TX_INV },
229 /* SMK eHome Infrared Transceiver (Sony VAIO) */
230 { USB_DEVICE(VENDOR_SMK, 0x0322),
231 .driver_info = MCE_GEN2_TX_INV },
232 /* bundled with Hauppauge PVR-150 */
233 { USB_DEVICE(VENDOR_SMK, 0x0334),
234 .driver_info = MCE_GEN2_TX_INV },
235 /* SMK eHome Infrared Transceiver */
236 { USB_DEVICE(VENDOR_SMK, 0x0338) },
237 /* Tatung eHome Infrared Transceiver */
238 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
239 /* Shuttle eHome Infrared Transceiver */
240 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
241 /* Shuttle eHome Infrared Transceiver */
242 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
243 /* Gateway eHome Infrared Transceiver */
244 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
245 /* Mitsumi */
246 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
247 /* Topseed eHome Infrared Transceiver */
248 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
249 .driver_info = MCE_GEN2_TX_INV },
250 /* Topseed HP eHome Infrared Transceiver */
251 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
252 .driver_info = MCE_GEN2_TX_INV },
253 /* Topseed eHome Infrared Transceiver */
254 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
255 .driver_info = MCE_GEN2_TX_INV },
256 /* Topseed eHome Infrared Transceiver */
257 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
258 .driver_info = MCE_GEN3 },
259 /* Topseed eHome Infrared Transceiver */
260 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
261 .driver_info = MCE_GEN2_TX_INV },
262 /* Topseed eHome Infrared Transceiver */
263 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
264 .driver_info = MCE_GEN3 },
265 /* Ricavision internal Infrared Transceiver */
266 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
267 /* Itron ione Libra Q-11 */
268 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
269 /* FIC eHome Infrared Transceiver */
270 { USB_DEVICE(VENDOR_FIC, 0x9242) },
271 /* LG eHome Infrared Transceiver */
272 { USB_DEVICE(VENDOR_LG, 0x9803) },
273 /* Microsoft MCE Infrared Transceiver */
274 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
275 /* Formosa eHome Infrared Transceiver */
276 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
277 /* Formosa21 / eHome Infrared Receiver */
278 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
279 /* Formosa aim / Trust MCE Infrared Receiver */
280 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
281 /* Formosa Industrial Computing / Beanbag Emulation Device */
282 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
283 /* Formosa21 / eHome Infrared Receiver */
284 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
285 /* Formosa Industrial Computing AIM IR605/A */
286 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
287 /* Formosa Industrial Computing */
288 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
289 /* Fintek eHome Infrared Transceiver (HP branded) */
290 { USB_DEVICE(VENDOR_FINTEK, 0x5168) },
291 /* Fintek eHome Infrared Transceiver */
292 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
293 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
294 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
295 /* Pinnacle Remote Kit */
296 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
297 .driver_info = MCE_GEN3 },
298 /* Elitegroup Computer Systems IR */
299 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
300 /* Wistron Corp. eHome Infrared Receiver */
301 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
302 /* Compro K100 */
303 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
304 /* Compro K100 v2 */
305 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
306 /* Northstar Systems, Inc. eHome Infrared Transceiver */
307 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
308 /* TiVo PC IR Receiver */
309 { USB_DEVICE(VENDOR_TIVO, 0x2000) },
310 /* Conexant Hybrid TV "Shelby" Polaris SDK */
311 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
312 .driver_info = POLARIS_EVK },
313 /* Conexant Hybrid TV RDU253S Polaris */
314 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
315 .driver_info = CX_HYBRID_TV },
316 /* Terminating entry */
320 /* data structure for each usb transceiver */
321 struct mceusb_dev {
322 /* ir-core bits */
323 struct rc_dev *rc;
325 /* optional features we can enable */
326 bool carrier_report_enabled;
327 bool learning_enabled;
329 /* core device bits */
330 struct device *dev;
332 /* usb */
333 struct usb_device *usbdev;
334 struct urb *urb_in;
335 struct usb_endpoint_descriptor *usb_ep_in;
336 struct usb_endpoint_descriptor *usb_ep_out;
338 /* buffers and dma */
339 unsigned char *buf_in;
340 unsigned int len_in;
341 dma_addr_t dma_in;
342 dma_addr_t dma_out;
344 enum {
345 CMD_HEADER = 0,
346 SUBCMD,
347 CMD_DATA,
348 PARSE_IRDATA,
349 } parser_state;
351 u8 cmd, rem; /* Remaining IR data bytes in packet */
353 struct {
354 u32 connected:1;
355 u32 tx_mask_normal:1;
356 u32 microsoft_gen1:1;
357 u32 no_tx:1;
358 } flags;
360 /* transmit support */
361 int send_flags;
362 u32 carrier;
363 unsigned char tx_mask;
365 char name[128];
366 char phys[64];
367 enum mceusb_model_type model;
371 * MCE Device Command Strings
372 * Device command responses vary from device to device...
373 * - DEVICE_RESET resets the hardware to its default state
374 * - GET_REVISION fetches the hardware/software revision, common
375 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
376 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
377 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
378 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
379 * ((clk / frequency) - 1)
380 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
381 * response in the form of 9f 0c msb lsb
382 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
383 * the form of 9f 08 bm, where bm is the bitmask
384 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
385 * general use one or short-range learning one, in the form of
386 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
387 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
388 * - SET_TX_BITMASK sets the transmitter bitmask
389 * - SET_RX_TIMEOUT sets the receiver timeout
390 * - SET_RX_SENSOR sets which receiver sensor to use
392 static char DEVICE_RESET[] = {MCE_COMMAND_NULL, MCE_HW_CMD_HEADER,
393 MCE_CMD_DEVICE_RESET};
394 static char GET_REVISION[] = {MCE_HW_CMD_HEADER, MCE_CMD_G_REVISION};
395 static char GET_UNKNOWN[] = {MCE_HW_CMD_HEADER, MCE_CMD_UNKNOWN7};
396 static char GET_UNKNOWN2[] = {MCE_COMMAND_HEADER, MCE_CMD_UNKNOWN2};
397 static char GET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER, MCE_CMD_G_CARRIER};
398 static char GET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TIMEOUT};
399 static char GET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TXMASK};
400 static char GET_RX_SENSOR[] = {MCE_COMMAND_HEADER, MCE_CMD_G_RXSENSOR};
401 /* sub in desired values in lower byte or bytes for full command */
402 /* FIXME: make use of these for transmit.
403 static char SET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER,
404 MCE_CMD_S_CARRIER, 0x00, 0x00};
405 static char SET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_S_TXMASK, 0x00};
406 static char SET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER,
407 MCE_CMD_S_TIMEOUT, 0x00, 0x00};
408 static char SET_RX_SENSOR[] = {MCE_COMMAND_HEADER,
409 MCE_CMD_S_RXSENSOR, 0x00};
412 static int mceusb_cmdsize(u8 cmd, u8 subcmd)
414 int datasize = 0;
416 switch (cmd) {
417 case MCE_COMMAND_NULL:
418 if (subcmd == MCE_HW_CMD_HEADER)
419 datasize = 1;
420 break;
421 case MCE_HW_CMD_HEADER:
422 switch (subcmd) {
423 case MCE_CMD_G_REVISION:
424 datasize = 2;
425 break;
427 case MCE_COMMAND_HEADER:
428 switch (subcmd) {
429 case MCE_CMD_UNKNOWN:
430 case MCE_CMD_S_CARRIER:
431 case MCE_CMD_S_TIMEOUT:
432 case MCE_RSP_PULSE_COUNT:
433 datasize = 2;
434 break;
435 case MCE_CMD_SIG_END:
436 case MCE_CMD_S_TXMASK:
437 case MCE_CMD_S_RXSENSOR:
438 datasize = 1;
439 break;
442 return datasize;
445 static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
446 int offset, int len, bool out)
448 char codes[USB_BUFLEN * 3 + 1];
449 char inout[9];
450 u8 cmd, subcmd, data1, data2;
451 struct device *dev = ir->dev;
452 int i, start, skip = 0;
454 if (!debug)
455 return;
457 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
458 if (ir->flags.microsoft_gen1 && !out && !offset)
459 skip = 2;
461 if (len <= skip)
462 return;
464 for (i = 0; i < len && i < USB_BUFLEN; i++)
465 snprintf(codes + i * 3, 4, "%02x ", buf[i + offset] & 0xff);
467 dev_info(dev, "%sx data: %s(length=%d)\n",
468 (out ? "t" : "r"), codes, len);
470 if (out)
471 strcpy(inout, "Request\0");
472 else
473 strcpy(inout, "Got\0");
475 start = offset + skip;
476 cmd = buf[start] & 0xff;
477 subcmd = buf[start + 1] & 0xff;
478 data1 = buf[start + 2] & 0xff;
479 data2 = buf[start + 3] & 0xff;
481 switch (cmd) {
482 case MCE_COMMAND_NULL:
483 if ((subcmd == MCE_HW_CMD_HEADER) &&
484 (data1 == MCE_CMD_DEVICE_RESET))
485 dev_info(dev, "Device reset requested\n");
486 else
487 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
488 cmd, subcmd);
489 break;
490 case MCE_HW_CMD_HEADER:
491 switch (subcmd) {
492 case MCE_CMD_G_REVISION:
493 if (len == 2)
494 dev_info(dev, "Get hw/sw rev?\n");
495 else
496 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
497 "0x%02x 0x%02x\n", data1, data2,
498 buf[start + 4], buf[start + 5]);
499 break;
500 case MCE_CMD_DEVICE_RESET:
501 dev_info(dev, "Device reset requested\n");
502 break;
503 case MCE_RSP_CMD_INVALID:
504 dev_info(dev, "Previous command not supported\n");
505 break;
506 case MCE_CMD_UNKNOWN7:
507 case MCE_CMD_UNKNOWN9:
508 default:
509 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
510 cmd, subcmd);
511 break;
513 break;
514 case MCE_COMMAND_HEADER:
515 switch (subcmd) {
516 case MCE_CMD_SIG_END:
517 dev_info(dev, "End of signal\n");
518 break;
519 case MCE_CMD_PING:
520 dev_info(dev, "Ping\n");
521 break;
522 case MCE_CMD_UNKNOWN:
523 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
524 data1, data2);
525 break;
526 case MCE_CMD_S_CARRIER:
527 dev_info(dev, "%s carrier mode and freq of "
528 "0x%02x 0x%02x\n", inout, data1, data2);
529 break;
530 case MCE_CMD_G_CARRIER:
531 dev_info(dev, "Get carrier mode and freq\n");
532 break;
533 case MCE_CMD_S_TXMASK:
534 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
535 inout, data1);
536 break;
537 case MCE_CMD_S_TIMEOUT:
538 /* value is in units of 50us, so x*50/100 or x/2 ms */
539 dev_info(dev, "%s receive timeout of %d ms\n",
540 inout, ((data1 << 8) | data2) / 2);
541 break;
542 case MCE_CMD_G_TIMEOUT:
543 dev_info(dev, "Get receive timeout\n");
544 break;
545 case MCE_CMD_G_TXMASK:
546 dev_info(dev, "Get transmit blaster mask\n");
547 break;
548 case MCE_CMD_S_RXSENSOR:
549 dev_info(dev, "%s %s-range receive sensor in use\n",
550 inout, data1 == 0x02 ? "short" : "long");
551 break;
552 case MCE_CMD_G_RXSENSOR:
553 /* aka MCE_RSP_PULSE_COUNT */
554 if (out)
555 dev_info(dev, "Get receive sensor\n");
556 else if (ir->learning_enabled)
557 dev_info(dev, "RX pulse count: %d\n",
558 ((data1 << 8) | data2));
559 break;
560 case MCE_RSP_CMD_INVALID:
561 dev_info(dev, "Error! Hardware is likely wedged...\n");
562 break;
563 case MCE_CMD_UNKNOWN2:
564 case MCE_CMD_UNKNOWN3:
565 case MCE_CMD_UNKNOWN5:
566 default:
567 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
568 cmd, subcmd);
569 break;
571 break;
572 default:
573 break;
576 if (cmd == MCE_IRDATA_TRAILER)
577 dev_info(dev, "End of raw IR data\n");
578 else if ((cmd != MCE_COMMAND_HEADER) &&
579 ((cmd & MCE_COMMAND_MASK) == MCE_COMMAND_IRDATA))
580 dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem);
583 static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
585 struct mceusb_dev *ir;
586 int len;
588 if (!urb)
589 return;
591 ir = urb->context;
592 if (ir) {
593 len = urb->actual_length;
595 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
596 urb->status, len);
598 mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
603 /* request incoming or send outgoing usb packet - used to initialize remote */
604 static void mce_request_packet(struct mceusb_dev *ir,
605 struct usb_endpoint_descriptor *ep,
606 unsigned char *data, int size, int urb_type)
608 int res;
609 struct urb *async_urb;
610 struct device *dev = ir->dev;
611 unsigned char *async_buf;
613 if (urb_type == MCEUSB_TX) {
614 async_urb = usb_alloc_urb(0, GFP_KERNEL);
615 if (unlikely(!async_urb)) {
616 dev_err(dev, "Error, couldn't allocate urb!\n");
617 return;
620 async_buf = kzalloc(size, GFP_KERNEL);
621 if (!async_buf) {
622 dev_err(dev, "Error, couldn't allocate buf!\n");
623 usb_free_urb(async_urb);
624 return;
627 /* outbound data */
628 usb_fill_int_urb(async_urb, ir->usbdev,
629 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
630 async_buf, size, (usb_complete_t)mce_async_callback,
631 ir, ep->bInterval);
632 memcpy(async_buf, data, size);
634 } else if (urb_type == MCEUSB_RX) {
635 /* standard request */
636 async_urb = ir->urb_in;
637 ir->send_flags = RECV_FLAG_IN_PROGRESS;
639 } else {
640 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
641 return;
644 dev_dbg(dev, "receive request called (size=%#x)\n", size);
646 async_urb->transfer_buffer_length = size;
647 async_urb->dev = ir->usbdev;
649 res = usb_submit_urb(async_urb, GFP_ATOMIC);
650 if (res) {
651 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
652 return;
654 dev_dbg(dev, "receive request complete (res=%d)\n", res);
657 static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
659 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
662 static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
664 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
667 /* Send data out the IR blaster port(s) */
668 static int mceusb_tx_ir(struct rc_dev *dev, int *txbuf, u32 n)
670 struct mceusb_dev *ir = dev->priv;
671 int i, ret = 0;
672 int count, cmdcount = 0;
673 unsigned char *cmdbuf; /* MCE command buffer */
674 long signal_duration = 0; /* Singnal length in us */
675 struct timeval start_time, end_time;
677 do_gettimeofday(&start_time);
679 count = n / sizeof(int);
681 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
682 if (!cmdbuf)
683 return -ENOMEM;
685 /* MCE tx init header */
686 cmdbuf[cmdcount++] = MCE_COMMAND_HEADER;
687 cmdbuf[cmdcount++] = MCE_CMD_S_TXMASK;
688 cmdbuf[cmdcount++] = ir->tx_mask;
690 /* Generate mce packet data */
691 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
692 signal_duration += txbuf[i];
693 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
695 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
697 /* Insert mce packet header every 4th entry */
698 if ((cmdcount < MCE_CMDBUF_SIZE) &&
699 (cmdcount - MCE_TX_HEADER_LENGTH) %
700 MCE_CODE_LENGTH == 0)
701 cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
703 /* Insert mce packet data */
704 if (cmdcount < MCE_CMDBUF_SIZE)
705 cmdbuf[cmdcount++] =
706 (txbuf[i] < MCE_PULSE_BIT ?
707 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
708 (i & 1 ? 0x00 : MCE_PULSE_BIT);
709 else {
710 ret = -EINVAL;
711 goto out;
714 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
715 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
718 /* Fix packet length in last header */
719 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
720 MCE_COMMAND_IRDATA + (cmdcount - MCE_TX_HEADER_LENGTH) %
721 MCE_CODE_LENGTH - 1;
723 /* Check if we have room for the empty packet at the end */
724 if (cmdcount >= MCE_CMDBUF_SIZE) {
725 ret = -EINVAL;
726 goto out;
729 /* All mce commands end with an empty packet (0x80) */
730 cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
732 /* Transmit the command to the mce device */
733 mce_async_out(ir, cmdbuf, cmdcount);
736 * The lircd gap calculation expects the write function to
737 * wait the time it takes for the ircommand to be sent before
738 * it returns.
740 do_gettimeofday(&end_time);
741 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
742 (end_time.tv_sec - start_time.tv_sec) * 1000000;
744 /* delay with the closest number of ticks */
745 set_current_state(TASK_INTERRUPTIBLE);
746 schedule_timeout(usecs_to_jiffies(signal_duration));
748 out:
749 kfree(cmdbuf);
750 return ret ? ret : n;
753 /* Sets active IR outputs -- mce devices typically have two */
754 static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
756 struct mceusb_dev *ir = dev->priv;
758 if (ir->flags.tx_mask_normal)
759 ir->tx_mask = mask;
760 else
761 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
762 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
764 return 0;
767 /* Sets the send carrier frequency and mode */
768 static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
770 struct mceusb_dev *ir = dev->priv;
771 int clk = 10000000;
772 int prescaler = 0, divisor = 0;
773 unsigned char cmdbuf[4] = { MCE_COMMAND_HEADER,
774 MCE_CMD_S_CARRIER, 0x00, 0x00 };
776 /* Carrier has changed */
777 if (ir->carrier != carrier) {
779 if (carrier == 0) {
780 ir->carrier = carrier;
781 cmdbuf[2] = MCE_CMD_SIG_END;
782 cmdbuf[3] = MCE_IRDATA_TRAILER;
783 dev_dbg(ir->dev, "%s: disabling carrier "
784 "modulation\n", __func__);
785 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
786 return carrier;
789 for (prescaler = 0; prescaler < 4; ++prescaler) {
790 divisor = (clk >> (2 * prescaler)) / carrier;
791 if (divisor <= 0xff) {
792 ir->carrier = carrier;
793 cmdbuf[2] = prescaler;
794 cmdbuf[3] = divisor;
795 dev_dbg(ir->dev, "%s: requesting %u HZ "
796 "carrier\n", __func__, carrier);
798 /* Transmit new carrier to mce device */
799 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
800 return carrier;
804 return -EINVAL;
808 return carrier;
812 * We don't do anything but print debug spew for many of the command bits
813 * we receive from the hardware, but some of them are useful information
814 * we want to store so that we can use them.
816 static void mceusb_handle_command(struct mceusb_dev *ir, int index)
818 u8 hi = ir->buf_in[index + 1] & 0xff;
819 u8 lo = ir->buf_in[index + 2] & 0xff;
821 switch (ir->buf_in[index]) {
822 /* 2-byte return value commands */
823 case MCE_CMD_S_TIMEOUT:
824 ir->rc->timeout = US_TO_NS((hi << 8 | lo) / 2);
825 break;
827 /* 1-byte return value commands */
828 case MCE_CMD_S_TXMASK:
829 ir->tx_mask = hi;
830 break;
831 case MCE_CMD_S_RXSENSOR:
832 ir->learning_enabled = (hi == 0x02);
833 break;
834 default:
835 break;
839 static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
841 DEFINE_IR_RAW_EVENT(rawir);
842 int i = 0;
844 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
845 if (ir->flags.microsoft_gen1)
846 i = 2;
848 /* if there's no data, just return now */
849 if (buf_len <= i)
850 return;
852 for (; i < buf_len; i++) {
853 switch (ir->parser_state) {
854 case SUBCMD:
855 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
856 mceusb_dev_printdata(ir, ir->buf_in, i - 1,
857 ir->rem + 2, false);
858 mceusb_handle_command(ir, i);
859 ir->parser_state = CMD_DATA;
860 break;
861 case PARSE_IRDATA:
862 ir->rem--;
863 init_ir_raw_event(&rawir);
864 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
865 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
866 * US_TO_NS(MCE_TIME_UNIT);
868 dev_dbg(ir->dev, "Storing %s with duration %d\n",
869 rawir.pulse ? "pulse" : "space",
870 rawir.duration);
872 ir_raw_event_store_with_filter(ir->rc, &rawir);
873 break;
874 case CMD_DATA:
875 ir->rem--;
876 break;
877 case CMD_HEADER:
878 /* decode mce packets of the form (84),AA,BB,CC,DD */
879 /* IR data packets can span USB messages - rem */
880 ir->cmd = ir->buf_in[i];
881 if ((ir->cmd == MCE_COMMAND_HEADER) ||
882 ((ir->cmd & MCE_COMMAND_MASK) !=
883 MCE_COMMAND_IRDATA)) {
884 ir->parser_state = SUBCMD;
885 continue;
887 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
888 mceusb_dev_printdata(ir, ir->buf_in,
889 i, ir->rem + 1, false);
890 if (ir->rem)
891 ir->parser_state = PARSE_IRDATA;
892 else
893 ir_raw_event_reset(ir->rc);
894 break;
897 if (ir->parser_state != CMD_HEADER && !ir->rem)
898 ir->parser_state = CMD_HEADER;
900 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
901 ir_raw_event_handle(ir->rc);
904 static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
906 struct mceusb_dev *ir;
907 int buf_len;
909 if (!urb)
910 return;
912 ir = urb->context;
913 if (!ir) {
914 usb_unlink_urb(urb);
915 return;
918 buf_len = urb->actual_length;
920 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
921 ir->send_flags = SEND_FLAG_COMPLETE;
922 dev_dbg(ir->dev, "setup answer received %d bytes\n",
923 buf_len);
926 switch (urb->status) {
927 /* success */
928 case 0:
929 mceusb_process_ir_data(ir, buf_len);
930 break;
932 case -ECONNRESET:
933 case -ENOENT:
934 case -ESHUTDOWN:
935 usb_unlink_urb(urb);
936 return;
938 case -EPIPE:
939 default:
940 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
941 break;
944 usb_submit_urb(urb, GFP_ATOMIC);
947 static void mceusb_gen1_init(struct mceusb_dev *ir)
949 int ret;
950 int maxp = ir->len_in;
951 struct device *dev = ir->dev;
952 char *data;
954 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
955 if (!data) {
956 dev_err(dev, "%s: memory allocation failed!\n", __func__);
957 return;
961 * This is a strange one. Windows issues a set address to the device
962 * on the receive control pipe and expect a certain value pair back
964 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
965 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
966 data, USB_CTRL_MSG_SZ, HZ * 3);
967 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
968 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
969 __func__, data[0], data[1]);
971 /* set feature: bit rate 38400 bps */
972 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
973 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
974 0xc04e, 0x0000, NULL, 0, HZ * 3);
976 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
978 /* bRequest 4: set char length to 8 bits */
979 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
980 4, USB_TYPE_VENDOR,
981 0x0808, 0x0000, NULL, 0, HZ * 3);
982 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
984 /* bRequest 2: set handshaking to use DTR/DSR */
985 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
986 2, USB_TYPE_VENDOR,
987 0x0000, 0x0100, NULL, 0, HZ * 3);
988 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
990 /* device reset */
991 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
992 mce_sync_in(ir, NULL, maxp);
994 /* get hw/sw revision? */
995 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
996 mce_sync_in(ir, NULL, maxp);
998 kfree(data);
1001 static void mceusb_gen2_init(struct mceusb_dev *ir)
1003 int maxp = ir->len_in;
1005 /* device reset */
1006 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
1007 mce_sync_in(ir, NULL, maxp);
1009 /* get hw/sw revision? */
1010 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
1011 mce_sync_in(ir, NULL, maxp);
1013 /* unknown what the next two actually return... */
1014 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
1015 mce_sync_in(ir, NULL, maxp);
1016 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
1017 mce_sync_in(ir, NULL, maxp);
1020 static void mceusb_get_parameters(struct mceusb_dev *ir)
1022 int maxp = ir->len_in;
1024 /* get the carrier and frequency */
1025 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
1026 mce_sync_in(ir, NULL, maxp);
1028 if (!ir->flags.no_tx) {
1029 /* get the transmitter bitmask */
1030 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
1031 mce_sync_in(ir, NULL, maxp);
1034 /* get receiver timeout value */
1035 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
1036 mce_sync_in(ir, NULL, maxp);
1038 /* get receiver sensor setting */
1039 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
1040 mce_sync_in(ir, NULL, maxp);
1043 static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
1045 struct device *dev = ir->dev;
1046 struct rc_dev *rc;
1047 int ret;
1049 rc = rc_allocate_device();
1050 if (!rc) {
1051 dev_err(dev, "remote dev allocation failed\n");
1052 goto out;
1055 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1056 mceusb_model[ir->model].name ?
1057 mceusb_model[ir->model].name :
1058 "Media Center Ed. eHome Infrared Remote Transceiver",
1059 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1060 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1062 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
1064 rc->input_name = ir->name;
1065 rc->input_phys = ir->phys;
1066 usb_to_input_id(ir->usbdev, &rc->input_id);
1067 rc->dev.parent = dev;
1068 rc->priv = ir;
1069 rc->driver_type = RC_DRIVER_IR_RAW;
1070 rc->allowed_protos = RC_TYPE_ALL;
1071 rc->timeout = US_TO_NS(1000);
1072 if (!ir->flags.no_tx) {
1073 rc->s_tx_mask = mceusb_set_tx_mask;
1074 rc->s_tx_carrier = mceusb_set_tx_carrier;
1075 rc->tx_ir = mceusb_tx_ir;
1077 rc->driver_name = DRIVER_NAME;
1078 rc->map_name = mceusb_model[ir->model].rc_map ?
1079 mceusb_model[ir->model].rc_map : RC_MAP_RC6_MCE;
1081 ret = rc_register_device(rc);
1082 if (ret < 0) {
1083 dev_err(dev, "remote dev registration failed\n");
1084 goto out;
1087 return rc;
1089 out:
1090 rc_free_device(rc);
1091 return NULL;
1094 static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1095 const struct usb_device_id *id)
1097 struct usb_device *dev = interface_to_usbdev(intf);
1098 struct usb_host_interface *idesc;
1099 struct usb_endpoint_descriptor *ep = NULL;
1100 struct usb_endpoint_descriptor *ep_in = NULL;
1101 struct usb_endpoint_descriptor *ep_out = NULL;
1102 struct mceusb_dev *ir = NULL;
1103 int pipe, maxp, i;
1104 char buf[63], name[128] = "";
1105 enum mceusb_model_type model = id->driver_info;
1106 bool is_gen3;
1107 bool is_microsoft_gen1;
1108 bool tx_mask_normal;
1109 int ir_intfnum;
1111 dev_dbg(&intf->dev, "%s called\n", __func__);
1113 idesc = intf->cur_altsetting;
1115 is_gen3 = mceusb_model[model].mce_gen3;
1116 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1117 tx_mask_normal = mceusb_model[model].tx_mask_normal;
1118 ir_intfnum = mceusb_model[model].ir_intfnum;
1120 /* There are multi-function devices with non-IR interfaces */
1121 if (idesc->desc.bInterfaceNumber != ir_intfnum)
1122 return -ENODEV;
1124 /* step through the endpoints to find first bulk in and out endpoint */
1125 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1126 ep = &idesc->endpoint[i].desc;
1128 if ((ep_in == NULL)
1129 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1130 == USB_DIR_IN)
1131 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1132 == USB_ENDPOINT_XFER_BULK)
1133 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1134 == USB_ENDPOINT_XFER_INT))) {
1136 ep_in = ep;
1137 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
1138 ep_in->bInterval = 1;
1139 dev_dbg(&intf->dev, "acceptable inbound endpoint "
1140 "found\n");
1143 if ((ep_out == NULL)
1144 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1145 == USB_DIR_OUT)
1146 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1147 == USB_ENDPOINT_XFER_BULK)
1148 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1149 == USB_ENDPOINT_XFER_INT))) {
1151 ep_out = ep;
1152 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
1153 ep_out->bInterval = 1;
1154 dev_dbg(&intf->dev, "acceptable outbound endpoint "
1155 "found\n");
1158 if (ep_in == NULL) {
1159 dev_dbg(&intf->dev, "inbound and/or endpoint not found\n");
1160 return -ENODEV;
1163 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1164 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1166 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1167 if (!ir)
1168 goto mem_alloc_fail;
1170 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1171 if (!ir->buf_in)
1172 goto buf_in_alloc_fail;
1174 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1175 if (!ir->urb_in)
1176 goto urb_in_alloc_fail;
1178 ir->usbdev = dev;
1179 ir->dev = &intf->dev;
1180 ir->len_in = maxp;
1181 ir->flags.microsoft_gen1 = is_microsoft_gen1;
1182 ir->flags.tx_mask_normal = tx_mask_normal;
1183 ir->flags.no_tx = mceusb_model[model].no_tx;
1184 ir->model = model;
1186 /* Saving usb interface data for use by the transmitter routine */
1187 ir->usb_ep_in = ep_in;
1188 ir->usb_ep_out = ep_out;
1190 if (dev->descriptor.iManufacturer
1191 && usb_string(dev, dev->descriptor.iManufacturer,
1192 buf, sizeof(buf)) > 0)
1193 strlcpy(name, buf, sizeof(name));
1194 if (dev->descriptor.iProduct
1195 && usb_string(dev, dev->descriptor.iProduct,
1196 buf, sizeof(buf)) > 0)
1197 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1198 " %s", buf);
1200 ir->rc = mceusb_init_rc_dev(ir);
1201 if (!ir->rc)
1202 goto rc_dev_fail;
1204 /* flush buffers on the device */
1205 mce_sync_in(ir, NULL, maxp);
1206 mce_sync_in(ir, NULL, maxp);
1208 /* wire up inbound data handler */
1209 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1210 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1211 ir->urb_in->transfer_dma = ir->dma_in;
1212 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1214 /* initialize device */
1215 if (ir->flags.microsoft_gen1)
1216 mceusb_gen1_init(ir);
1217 else if (!is_gen3)
1218 mceusb_gen2_init(ir);
1220 mceusb_get_parameters(ir);
1222 if (!ir->flags.no_tx)
1223 mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
1225 usb_set_intfdata(intf, ir);
1227 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1228 dev->bus->busnum, dev->devnum);
1230 return 0;
1232 /* Error-handling path */
1233 rc_dev_fail:
1234 usb_free_urb(ir->urb_in);
1235 urb_in_alloc_fail:
1236 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1237 buf_in_alloc_fail:
1238 kfree(ir);
1239 mem_alloc_fail:
1240 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1242 return -ENOMEM;
1246 static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1248 struct usb_device *dev = interface_to_usbdev(intf);
1249 struct mceusb_dev *ir = usb_get_intfdata(intf);
1251 usb_set_intfdata(intf, NULL);
1253 if (!ir)
1254 return;
1256 ir->usbdev = NULL;
1257 rc_unregister_device(ir->rc);
1258 usb_kill_urb(ir->urb_in);
1259 usb_free_urb(ir->urb_in);
1260 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1262 kfree(ir);
1265 static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1267 struct mceusb_dev *ir = usb_get_intfdata(intf);
1268 dev_info(ir->dev, "suspend\n");
1269 usb_kill_urb(ir->urb_in);
1270 return 0;
1273 static int mceusb_dev_resume(struct usb_interface *intf)
1275 struct mceusb_dev *ir = usb_get_intfdata(intf);
1276 dev_info(ir->dev, "resume\n");
1277 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1278 return -EIO;
1279 return 0;
1282 static struct usb_driver mceusb_dev_driver = {
1283 .name = DRIVER_NAME,
1284 .probe = mceusb_dev_probe,
1285 .disconnect = mceusb_dev_disconnect,
1286 .suspend = mceusb_dev_suspend,
1287 .resume = mceusb_dev_resume,
1288 .reset_resume = mceusb_dev_resume,
1289 .id_table = mceusb_dev_table
1292 static int __init mceusb_dev_init(void)
1294 int ret;
1296 ret = usb_register(&mceusb_dev_driver);
1297 if (ret < 0)
1298 printk(KERN_ERR DRIVER_NAME
1299 ": usb register failed, result = %d\n", ret);
1301 return ret;
1304 static void __exit mceusb_dev_exit(void)
1306 usb_deregister(&mceusb_dev_driver);
1309 module_init(mceusb_dev_init);
1310 module_exit(mceusb_dev_exit);
1312 MODULE_DESCRIPTION(DRIVER_DESC);
1313 MODULE_AUTHOR(DRIVER_AUTHOR);
1314 MODULE_LICENSE("GPL");
1315 MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1317 module_param(debug, bool, S_IRUGO | S_IWUSR);
1318 MODULE_PARM_DESC(debug, "Debug enabled or not");