blob: a726f63ecfeaac37fae04566597d72fc5d183d4c [file] [log] [blame]
Jarod Wilson66e89522010-06-01 17:32:08 -03001/*
2 * Driver for USB Windows Media Center Ed. eHome Infrared Transceivers
3 *
4 * Copyright (c) 2010 by Jarod Wilson <jarod@redhat.com>
5 *
6 * Based on the original lirc_mceusb and lirc_mceusb2 drivers, by Dan
7 * Conti, Martin Blatter and Daniel Melander, the latter of which was
8 * in turn also based on the lirc_atiusb driver by Paul Miller. The
9 * two mce drivers were merged into one by Jarod Wilson, with transmit
10 * support for the 1st-gen device added primarily by Patrick Calhoun,
11 * with a bit of tweaks by Jarod. Debugging improvements and proper
12 * support for what appears to be 3rd-gen hardware added by Jarod.
13 * Initial port from lirc driver to ir-core drivery by Jarod, based
14 * partially on a port to an earlier proposed IR infrastructure by
15 * Jon Smirl, which included enhancements and simplifications to the
16 * incoming IR buffer parsing routines.
17 *
Jarod Wilson66e89522010-06-01 17:32:08 -030018 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 *
33 */
34
35#include <linux/device.h>
36#include <linux/module.h>
37#include <linux/slab.h>
38#include <linux/usb.h>
39#include <linux/input.h>
40#include <media/ir-core.h>
41#include <media/ir-common.h>
42
43#define DRIVER_VERSION "1.91"
44#define DRIVER_AUTHOR "Jarod Wilson <jarod@wilsonet.com>"
45#define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
46 "device driver"
47#define DRIVER_NAME "mceusb"
48
49#define USB_BUFLEN 32 /* USB reception buffer length */
Jarod Wilson657290b2010-06-16 17:10:05 -030050#define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
51#define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
Jarod Wilson66e89522010-06-01 17:32:08 -030052
53/* MCE constants */
54#define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
55#define MCE_TIME_UNIT 50 /* Approx 50us resolution */
56#define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
57#define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
58#define MCE_PACKET_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
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 */
Jarod Wilsond732a722010-06-16 17:10:46 -030066#define MCE_PACKET_LENGTH_MASK 0x1F /* Packet length mask */
Jarod Wilson66e89522010-06-01 17:32:08 -030067
68
69/* module parameters */
70#ifdef CONFIG_USB_DEBUG
71static int debug = 1;
72#else
73static int debug;
74#endif
75
76/* general constants */
77#define SEND_FLAG_IN_PROGRESS 1
78#define SEND_FLAG_COMPLETE 2
79#define RECV_FLAG_IN_PROGRESS 3
80#define RECV_FLAG_COMPLETE 4
81
82#define MCEUSB_RX 1
83#define MCEUSB_TX 2
84
85#define VENDOR_PHILIPS 0x0471
86#define VENDOR_SMK 0x0609
87#define VENDOR_TATUNG 0x1460
88#define VENDOR_GATEWAY 0x107b
89#define VENDOR_SHUTTLE 0x1308
90#define VENDOR_SHUTTLE2 0x051c
91#define VENDOR_MITSUMI 0x03ee
92#define VENDOR_TOPSEED 0x1784
93#define VENDOR_RICAVISION 0x179d
94#define VENDOR_ITRON 0x195d
95#define VENDOR_FIC 0x1509
96#define VENDOR_LG 0x043e
97#define VENDOR_MICROSOFT 0x045e
98#define VENDOR_FORMOSA 0x147a
99#define VENDOR_FINTEK 0x1934
100#define VENDOR_PINNACLE 0x2304
101#define VENDOR_ECS 0x1019
102#define VENDOR_WISTRON 0x0fb8
103#define VENDOR_COMPRO 0x185b
104#define VENDOR_NORTHSTAR 0x04eb
105#define VENDOR_REALTEK 0x0bda
106#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300107#define VENDOR_CONEXANT 0x0572
Jarod Wilson66e89522010-06-01 17:32:08 -0300108
109static struct usb_device_id mceusb_dev_table[] = {
110 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
111 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
112 /* Philips Infrared Transceiver - Sahara branded */
113 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
114 /* Philips Infrared Transceiver - HP branded */
115 { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
116 /* Philips SRM5100 */
117 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
118 /* Philips Infrared Transceiver - Omaura */
119 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
120 /* Philips Infrared Transceiver - Spinel plus */
121 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
122 /* Philips eHome Infrared Transceiver */
123 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300124 /* Philips/Spinel plus IR transceiver for ASUS */
125 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
126 /* Philips/Spinel plus IR transceiver for ASUS */
127 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300128 /* 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) },
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300202 /* Conexant SDK */
203 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300204 /* Terminating entry */
205 { }
206};
207
208static struct usb_device_id gen3_list[] = {
209 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
210 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
211 {}
212};
213
Jarod Wilson66e89522010-06-01 17:32:08 -0300214static struct usb_device_id microsoft_gen1_list[] = {
215 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
216 {}
217};
218
Jarod Wilson657290b2010-06-16 17:10:05 -0300219static struct usb_device_id std_tx_mask_list[] = {
220 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d) },
221 { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
222 { USB_DEVICE(VENDOR_SMK, 0x031d) },
223 { USB_DEVICE(VENDOR_SMK, 0x0322) },
224 { USB_DEVICE(VENDOR_SMK, 0x0334) },
225 { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
226 { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
227 { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
228 { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
229 { USB_DEVICE(VENDOR_TOPSEED, 0x000a) },
230 { USB_DEVICE(VENDOR_TOPSEED, 0x0011) },
231 { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
232 {}
233};
234
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300235static struct usb_device_id cx_polaris_list[] = {
236 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1) },
237 {}
238};
239
Jarod Wilson66e89522010-06-01 17:32:08 -0300240/* data structure for each usb transceiver */
241struct mceusb_dev {
242 /* ir-core bits */
Jarod Wilson66e89522010-06-01 17:32:08 -0300243 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300244 struct ir_raw_event rawir;
245
246 /* core device bits */
247 struct device *dev;
248 struct input_dev *idev;
249
250 /* usb */
251 struct usb_device *usbdev;
252 struct urb *urb_in;
253 struct usb_endpoint_descriptor *usb_ep_in;
254 struct usb_endpoint_descriptor *usb_ep_out;
255
256 /* buffers and dma */
257 unsigned char *buf_in;
258 unsigned int len_in;
259 u8 cmd; /* MCE command type */
260 u8 rem; /* Remaining IR data bytes in packet */
261 dma_addr_t dma_in;
262 dma_addr_t dma_out;
263
264 struct {
265 u32 connected:1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300266 u32 tx_mask_inverted:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300267 u32 microsoft_gen1:1;
Jarod Wilson22b07662010-07-08 12:38:57 -0300268 u32 reserved:29;
Jarod Wilson66e89522010-06-01 17:32:08 -0300269 } flags;
270
Jarod Wilsone23fb962010-06-16 17:55:52 -0300271 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300272 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300273 u32 carrier;
274 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300275
276 char name[128];
277 char phys[64];
Jarod Wilson66e89522010-06-01 17:32:08 -0300278};
279
280/*
281 * MCE Device Command Strings
282 * Device command responses vary from device to device...
283 * - DEVICE_RESET resets the hardware to its default state
284 * - GET_REVISION fetches the hardware/software revision, common
285 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
286 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
287 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
288 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
289 * ((clk / frequency) - 1)
290 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
291 * response in the form of 9f 0c msb lsb
292 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
293 * the form of 9f 08 bm, where bm is the bitmask
294 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
295 * general use one or short-range learning one, in the form of
296 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
297 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
298 * - SET_TX_BITMASK sets the transmitter bitmask
299 * - SET_RX_TIMEOUT sets the receiver timeout
300 * - SET_RX_SENSOR sets which receiver sensor to use
301 */
302static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
303static char GET_REVISION[] = {0xff, 0x0b};
304static char GET_UNKNOWN[] = {0xff, 0x18};
Jarod Wilson22b07662010-07-08 12:38:57 -0300305static char GET_UNKNOWN2[] = {0x9f, 0x05};
Jarod Wilson66e89522010-06-01 17:32:08 -0300306static char GET_CARRIER_FREQ[] = {0x9f, 0x07};
307static char GET_RX_TIMEOUT[] = {0x9f, 0x0d};
308static char GET_TX_BITMASK[] = {0x9f, 0x13};
309static char GET_RX_SENSOR[] = {0x9f, 0x15};
310/* sub in desired values in lower byte or bytes for full command */
311/* FIXME: make use of these for transmit.
312static char SET_CARRIER_FREQ[] = {0x9f, 0x06, 0x00, 0x00};
313static char SET_TX_BITMASK[] = {0x9f, 0x08, 0x00};
314static char SET_RX_TIMEOUT[] = {0x9f, 0x0c, 0x00, 0x00};
315static char SET_RX_SENSOR[] = {0x9f, 0x14, 0x00};
316*/
317
318static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
319 int len, bool out)
320{
321 char codes[USB_BUFLEN * 3 + 1];
322 char inout[9];
323 int i;
324 u8 cmd, subcmd, data1, data2;
325 struct device *dev = ir->dev;
Jarod Wilson657290b2010-06-16 17:10:05 -0300326 int idx = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300327
Jarod Wilson657290b2010-06-16 17:10:05 -0300328 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
329 if (ir->flags.microsoft_gen1 && !out)
330 idx = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300331
Jarod Wilson657290b2010-06-16 17:10:05 -0300332 if (len <= idx)
Jarod Wilson66e89522010-06-01 17:32:08 -0300333 return;
334
335 for (i = 0; i < len && i < USB_BUFLEN; i++)
336 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
337
338 dev_info(dev, "%sx data: %s (length=%d)\n",
339 (out ? "t" : "r"), codes, len);
340
341 if (out)
342 strcpy(inout, "Request\0");
343 else
344 strcpy(inout, "Got\0");
345
Jarod Wilson657290b2010-06-16 17:10:05 -0300346 cmd = buf[idx] & 0xff;
347 subcmd = buf[idx + 1] & 0xff;
348 data1 = buf[idx + 2] & 0xff;
349 data2 = buf[idx + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300350
351 switch (cmd) {
352 case 0x00:
353 if (subcmd == 0xff && data1 == 0xaa)
354 dev_info(dev, "Device reset requested\n");
355 else
356 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
357 cmd, subcmd);
358 break;
359 case 0xff:
360 switch (subcmd) {
361 case 0x0b:
362 if (len == 2)
363 dev_info(dev, "Get hw/sw rev?\n");
364 else
365 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
366 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson657290b2010-06-16 17:10:05 -0300367 buf[idx + 4], buf[idx + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300368 break;
369 case 0xaa:
370 dev_info(dev, "Device reset requested\n");
371 break;
372 case 0xfe:
373 dev_info(dev, "Previous command not supported\n");
374 break;
375 case 0x18:
376 case 0x1b:
377 default:
378 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
379 cmd, subcmd);
380 break;
381 }
382 break;
383 case 0x9f:
384 switch (subcmd) {
385 case 0x03:
386 dev_info(dev, "Ping\n");
387 break;
388 case 0x04:
389 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
390 data1, data2);
391 break;
392 case 0x06:
393 dev_info(dev, "%s carrier mode and freq of "
394 "0x%02x 0x%02x\n", inout, data1, data2);
395 break;
396 case 0x07:
397 dev_info(dev, "Get carrier mode and freq\n");
398 break;
399 case 0x08:
400 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
401 inout, data1);
402 break;
403 case 0x0c:
404 /* value is in units of 50us, so x*50/100 or x/2 ms */
405 dev_info(dev, "%s receive timeout of %d ms\n",
406 inout, ((data1 << 8) | data2) / 2);
407 break;
408 case 0x0d:
409 dev_info(dev, "Get receive timeout\n");
410 break;
411 case 0x13:
412 dev_info(dev, "Get transmit blaster mask\n");
413 break;
414 case 0x14:
415 dev_info(dev, "%s %s-range receive sensor in use\n",
416 inout, data1 == 0x02 ? "short" : "long");
417 break;
418 case 0x15:
419 if (len == 2)
420 dev_info(dev, "Get receive sensor\n");
421 else
422 dev_info(dev, "Received pulse count is %d\n",
423 ((data1 << 8) | data2));
424 break;
425 case 0xfe:
426 dev_info(dev, "Error! Hardware is likely wedged...\n");
427 break;
428 case 0x05:
429 case 0x09:
430 case 0x0f:
431 default:
432 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
433 cmd, subcmd);
434 break;
435 }
436 break;
437 default:
438 break;
439 }
440}
441
Jarod Wilson7c294402010-08-02 18:21:06 -0300442static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300443{
444 struct mceusb_dev *ir;
445 int len;
446
447 if (!urb)
448 return;
449
450 ir = urb->context;
451 if (ir) {
452 len = urb->actual_length;
453
454 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
455 urb->status, len);
456
457 if (debug)
458 mceusb_dev_printdata(ir, urb->transfer_buffer,
459 len, true);
460 }
461
462}
463
464/* request incoming or send outgoing usb packet - used to initialize remote */
465static void mce_request_packet(struct mceusb_dev *ir,
466 struct usb_endpoint_descriptor *ep,
467 unsigned char *data, int size, int urb_type)
468{
469 int res;
470 struct urb *async_urb;
471 struct device *dev = ir->dev;
472 unsigned char *async_buf;
473
474 if (urb_type == MCEUSB_TX) {
475 async_urb = usb_alloc_urb(0, GFP_KERNEL);
476 if (unlikely(!async_urb)) {
477 dev_err(dev, "Error, couldn't allocate urb!\n");
478 return;
479 }
480
481 async_buf = kzalloc(size, GFP_KERNEL);
482 if (!async_buf) {
483 dev_err(dev, "Error, couldn't allocate buf!\n");
484 usb_free_urb(async_urb);
485 return;
486 }
487
488 /* outbound data */
489 usb_fill_int_urb(async_urb, ir->usbdev,
490 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300491 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300492 ir, ep->bInterval);
493 memcpy(async_buf, data, size);
494
495 } else if (urb_type == MCEUSB_RX) {
496 /* standard request */
497 async_urb = ir->urb_in;
498 ir->send_flags = RECV_FLAG_IN_PROGRESS;
499
500 } else {
501 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
502 return;
503 }
504
505 dev_dbg(dev, "receive request called (size=%#x)\n", size);
506
507 async_urb->transfer_buffer_length = size;
508 async_urb->dev = ir->usbdev;
509
510 res = usb_submit_urb(async_urb, GFP_ATOMIC);
511 if (res) {
512 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
513 return;
514 }
515 dev_dbg(dev, "receive request complete (res=%d)\n", res);
516}
517
518static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
519{
520 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
521}
522
523static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
524{
525 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
526}
527
Jarod Wilsone23fb962010-06-16 17:55:52 -0300528/* Send data out the IR blaster port(s) */
529static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
530{
531 struct mceusb_dev *ir = priv;
532 int i, ret = 0;
533 int count, cmdcount = 0;
534 unsigned char *cmdbuf; /* MCE command buffer */
535 long signal_duration = 0; /* Singnal length in us */
536 struct timeval start_time, end_time;
537
538 do_gettimeofday(&start_time);
539
540 count = n / sizeof(int);
541
542 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
543 if (!cmdbuf)
544 return -ENOMEM;
545
546 /* MCE tx init header */
547 cmdbuf[cmdcount++] = MCE_CONTROL_HEADER;
548 cmdbuf[cmdcount++] = 0x08;
549 cmdbuf[cmdcount++] = ir->tx_mask;
550
551 /* Generate mce packet data */
552 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
553 signal_duration += txbuf[i];
554 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
555
556 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
557
558 /* Insert mce packet header every 4th entry */
559 if ((cmdcount < MCE_CMDBUF_SIZE) &&
560 (cmdcount - MCE_TX_HEADER_LENGTH) %
561 MCE_CODE_LENGTH == 0)
562 cmdbuf[cmdcount++] = MCE_PACKET_HEADER;
563
564 /* Insert mce packet data */
565 if (cmdcount < MCE_CMDBUF_SIZE)
566 cmdbuf[cmdcount++] =
567 (txbuf[i] < MCE_PULSE_BIT ?
568 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
569 (i & 1 ? 0x00 : MCE_PULSE_BIT);
570 else {
571 ret = -EINVAL;
572 goto out;
573 }
574
575 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
576 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
577 }
578
579 /* Fix packet length in last header */
580 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
581 0x80 + (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH - 1;
582
583 /* Check if we have room for the empty packet at the end */
584 if (cmdcount >= MCE_CMDBUF_SIZE) {
585 ret = -EINVAL;
586 goto out;
587 }
588
589 /* All mce commands end with an empty packet (0x80) */
590 cmdbuf[cmdcount++] = 0x80;
591
592 /* Transmit the command to the mce device */
593 mce_async_out(ir, cmdbuf, cmdcount);
594
595 /*
596 * The lircd gap calculation expects the write function to
597 * wait the time it takes for the ircommand to be sent before
598 * it returns.
599 */
600 do_gettimeofday(&end_time);
601 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
602 (end_time.tv_sec - start_time.tv_sec) * 1000000;
603
604 /* delay with the closest number of ticks */
605 set_current_state(TASK_INTERRUPTIBLE);
606 schedule_timeout(usecs_to_jiffies(signal_duration));
607
608out:
609 kfree(cmdbuf);
610 return ret ? ret : n;
611}
612
Jarod Wilson657290b2010-06-16 17:10:05 -0300613/* Sets active IR outputs -- mce devices typically (all?) have two */
614static int mceusb_set_tx_mask(void *priv, u32 mask)
615{
616 struct mceusb_dev *ir = priv;
617
618 if (ir->flags.tx_mask_inverted)
619 ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
620 else
621 ir->tx_mask = mask;
622
623 return 0;
624}
625
Jarod Wilsone23fb962010-06-16 17:55:52 -0300626/* Sets the send carrier frequency and mode */
627static int mceusb_set_tx_carrier(void *priv, u32 carrier)
628{
629 struct mceusb_dev *ir = priv;
630 int clk = 10000000;
631 int prescaler = 0, divisor = 0;
632 unsigned char cmdbuf[4] = { 0x9f, 0x06, 0x00, 0x00 };
633
634 /* Carrier has changed */
635 if (ir->carrier != carrier) {
636
637 if (carrier == 0) {
638 ir->carrier = carrier;
639 cmdbuf[2] = 0x01;
640 cmdbuf[3] = 0x80;
641 dev_dbg(ir->dev, "%s: disabling carrier "
642 "modulation\n", __func__);
643 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
644 return carrier;
645 }
646
647 for (prescaler = 0; prescaler < 4; ++prescaler) {
648 divisor = (clk >> (2 * prescaler)) / carrier;
649 if (divisor <= 0xFF) {
650 ir->carrier = carrier;
651 cmdbuf[2] = prescaler;
652 cmdbuf[3] = divisor;
653 dev_dbg(ir->dev, "%s: requesting %u HZ "
654 "carrier\n", __func__, carrier);
655
656 /* Transmit new carrier to mce device */
657 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
658 return carrier;
659 }
660 }
661
662 return -EINVAL;
663
664 }
665
666 return carrier;
667}
668
Jarod Wilson66e89522010-06-01 17:32:08 -0300669static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
670{
Maxim Levitsky46519182010-10-16 19:56:28 -0300671 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -0300672 int i, start_index = 0;
Jarod Wilsond732a722010-06-16 17:10:46 -0300673 u8 hdr = MCE_CONTROL_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300674
675 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
676 if (ir->flags.microsoft_gen1)
677 start_index = 2;
678
679 for (i = start_index; i < buf_len;) {
680 if (ir->rem == 0) {
681 /* decode mce packets of the form (84),AA,BB,CC,DD */
682 /* IR data packets can span USB messages - rem */
Jarod Wilsond732a722010-06-16 17:10:46 -0300683 hdr = ir->buf_in[i];
684 ir->rem = (hdr & MCE_PACKET_LENGTH_MASK);
685 ir->cmd = (hdr & ~MCE_PACKET_LENGTH_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -0300686 dev_dbg(ir->dev, "New data. rem: 0x%02x, cmd: 0x%02x\n",
687 ir->rem, ir->cmd);
688 i++;
689 }
690
Jarod Wilsond732a722010-06-16 17:10:46 -0300691 /* don't process MCE commands */
692 if (hdr == MCE_CONTROL_HEADER || hdr == 0xff) {
Jarod Wilson66e89522010-06-01 17:32:08 -0300693 ir->rem = 0;
694 return;
695 }
696
697 for (; (ir->rem > 0) && (i < buf_len); i++) {
698 ir->rem--;
699
700 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
701 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
702 * MCE_TIME_UNIT * 1000;
703
704 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
705 if (ir->rawir.pulse == rawir.pulse)
706 ir->rawir.duration += rawir.duration;
707 else {
708 ir->rawir.duration = rawir.duration;
709 ir->rawir.pulse = rawir.pulse;
710 }
711 continue;
712 }
713 rawir.duration += ir->rawir.duration;
714 ir->rawir.duration = 0;
715 ir->rawir.pulse = rawir.pulse;
716
717 dev_dbg(ir->dev, "Storing %s with duration %d\n",
718 rawir.pulse ? "pulse" : "space",
719 rawir.duration);
720
721 ir_raw_event_store(ir->idev, &rawir);
722 }
723
724 if (ir->buf_in[i] == 0x80 || ir->buf_in[i] == 0x9f)
725 ir->rem = 0;
726
727 dev_dbg(ir->dev, "calling ir_raw_event_handle\n");
728 ir_raw_event_handle(ir->idev);
729 }
730}
731
Jarod Wilson66e89522010-06-01 17:32:08 -0300732static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
733{
734 struct mceusb_dev *ir;
735 int buf_len;
736
737 if (!urb)
738 return;
739
740 ir = urb->context;
741 if (!ir) {
742 usb_unlink_urb(urb);
743 return;
744 }
745
746 buf_len = urb->actual_length;
747
Jarod Wilson66e89522010-06-01 17:32:08 -0300748 if (debug)
749 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
750
751 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
752 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300753 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300754 buf_len);
755 }
756
757 switch (urb->status) {
758 /* success */
759 case 0:
760 mceusb_process_ir_data(ir, buf_len);
761 break;
762
763 case -ECONNRESET:
764 case -ENOENT:
765 case -ESHUTDOWN:
766 usb_unlink_urb(urb);
767 return;
768
769 case -EPIPE:
770 default:
771 break;
772 }
773
774 usb_submit_urb(urb, GFP_ATOMIC);
775}
776
777static void mceusb_gen1_init(struct mceusb_dev *ir)
778{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300779 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300780 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300781 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300782 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300783
784 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
785 if (!data) {
786 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300787 return;
788 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300789
790 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300791 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300792 * on the receive control pipe and expect a certain value pair back
793 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300794 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
795 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300796 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300797 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
798 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
799 __func__, data[0], data[1]);
800
801 /* set feature: bit rate 38400 bps */
802 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
803 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
804 0xc04e, 0x0000, NULL, 0, HZ * 3);
805
806 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
807
808 /* bRequest 4: set char length to 8 bits */
809 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
810 4, USB_TYPE_VENDOR,
811 0x0808, 0x0000, NULL, 0, HZ * 3);
812 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
813
814 /* bRequest 2: set handshaking to use DTR/DSR */
815 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
816 2, USB_TYPE_VENDOR,
817 0x0000, 0x0100, NULL, 0, HZ * 3);
818 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300819
Jarod Wilson22b07662010-07-08 12:38:57 -0300820 /* device reset */
821 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
822 mce_sync_in(ir, NULL, maxp);
823
824 /* get hw/sw revision? */
825 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
826 mce_sync_in(ir, NULL, maxp);
827
Jarod Wilson657290b2010-06-16 17:10:05 -0300828 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -0300829};
830
831static void mceusb_gen2_init(struct mceusb_dev *ir)
832{
833 int maxp = ir->len_in;
834
Jarod Wilson66e89522010-06-01 17:32:08 -0300835 /* device reset */
836 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
837 mce_sync_in(ir, NULL, maxp);
838
839 /* get hw/sw revision? */
840 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
841 mce_sync_in(ir, NULL, maxp);
842
Jarod Wilson22b07662010-07-08 12:38:57 -0300843 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -0300844 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
845 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -0300846 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
847 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -0300848}
849
Jarod Wilson22b07662010-07-08 12:38:57 -0300850static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -0300851{
852 int maxp = ir->len_in;
853
Jarod Wilson66e89522010-06-01 17:32:08 -0300854 /* get the carrier and frequency */
855 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
856 mce_sync_in(ir, NULL, maxp);
857
858 /* get the transmitter bitmask */
859 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
860 mce_sync_in(ir, NULL, maxp);
861
862 /* get receiver timeout value */
863 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
864 mce_sync_in(ir, NULL, maxp);
865
866 /* get receiver sensor setting */
867 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
868 mce_sync_in(ir, NULL, maxp);
869}
870
871static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
872{
873 struct input_dev *idev;
874 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300875 struct device *dev = ir->dev;
876 int ret = -ENODEV;
877
878 idev = input_allocate_device();
879 if (!idev) {
880 dev_err(dev, "remote input dev allocation failed\n");
881 goto idev_alloc_failed;
882 }
883
884 ret = -ENOMEM;
885 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
886 if (!props) {
887 dev_err(dev, "remote ir dev props allocation failed\n");
888 goto props_alloc_failed;
889 }
890
Jarod Wilsone23fb962010-06-16 17:55:52 -0300891 snprintf(ir->name, sizeof(ir->name), "Media Center Ed. eHome "
Jarod Wilson66e89522010-06-01 17:32:08 -0300892 "Infrared Remote Transceiver (%04x:%04x)",
893 le16_to_cpu(ir->usbdev->descriptor.idVendor),
894 le16_to_cpu(ir->usbdev->descriptor.idProduct));
895
Jarod Wilson66e89522010-06-01 17:32:08 -0300896 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -0300897 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
898 strlcat(ir->phys, "/input0", sizeof(ir->phys));
899 idev->phys = ir->phys;
900
Jarod Wilson66e89522010-06-01 17:32:08 -0300901 props->priv = ir;
902 props->driver_type = RC_DRIVER_IR_RAW;
903 props->allowed_protos = IR_TYPE_ALL;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300904 props->s_tx_mask = mceusb_set_tx_mask;
905 props->s_tx_carrier = mceusb_set_tx_carrier;
906 props->tx_ir = mceusb_tx_ir;
Jarod Wilson66e89522010-06-01 17:32:08 -0300907
908 ir->props = props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300909
910 ret = ir_input_register(idev, RC_MAP_RC6_MCE, props, DRIVER_NAME);
911 if (ret < 0) {
912 dev_err(dev, "remote input device register failed\n");
913 goto irdev_failed;
914 }
915
916 return idev;
917
918irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -0300919 kfree(props);
920props_alloc_failed:
921 input_free_device(idev);
922idev_alloc_failed:
923 return NULL;
924}
925
926static int __devinit mceusb_dev_probe(struct usb_interface *intf,
927 const struct usb_device_id *id)
928{
929 struct usb_device *dev = interface_to_usbdev(intf);
930 struct usb_host_interface *idesc;
931 struct usb_endpoint_descriptor *ep = NULL;
932 struct usb_endpoint_descriptor *ep_in = NULL;
933 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -0300934 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -0300935 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -0300936 char buf[63], name[128] = "";
937 bool is_gen3;
938 bool is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300939 bool tx_mask_inverted;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300940 bool is_polaris;
Jarod Wilson66e89522010-06-01 17:32:08 -0300941
942 dev_dbg(&intf->dev, ": %s called\n", __func__);
943
Jarod Wilson66e89522010-06-01 17:32:08 -0300944 idesc = intf->cur_altsetting;
945
946 is_gen3 = usb_match_id(intf, gen3_list) ? 1 : 0;
947 is_microsoft_gen1 = usb_match_id(intf, microsoft_gen1_list) ? 1 : 0;
Jarod Wilson657290b2010-06-16 17:10:05 -0300948 tx_mask_inverted = usb_match_id(intf, std_tx_mask_list) ? 0 : 1;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300949 is_polaris = usb_match_id(intf, cx_polaris_list) ? 1 : 0;
950
951 if (is_polaris) {
952 /* Interface 0 is IR */
953 if (idesc->desc.bInterfaceNumber)
954 return -ENODEV;
955 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300956
957 /* step through the endpoints to find first bulk in and out endpoint */
958 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
959 ep = &idesc->endpoint[i].desc;
960
961 if ((ep_in == NULL)
962 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
963 == USB_DIR_IN)
964 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
965 == USB_ENDPOINT_XFER_BULK)
966 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
967 == USB_ENDPOINT_XFER_INT))) {
968
Jarod Wilson66e89522010-06-01 17:32:08 -0300969 ep_in = ep;
970 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -0300971 ep_in->bInterval = 1;
972 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
973 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -0300974 }
975
976 if ((ep_out == NULL)
977 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
978 == USB_DIR_OUT)
979 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
980 == USB_ENDPOINT_XFER_BULK)
981 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
982 == USB_ENDPOINT_XFER_INT))) {
983
Jarod Wilson66e89522010-06-01 17:32:08 -0300984 ep_out = ep;
985 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -0300986 ep_out->bInterval = 1;
987 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
988 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -0300989 }
990 }
991 if (ep_in == NULL) {
992 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
993 return -ENODEV;
994 }
995
996 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
997 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
998
999 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1000 if (!ir)
1001 goto mem_alloc_fail;
1002
1003 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1004 if (!ir->buf_in)
1005 goto buf_in_alloc_fail;
1006
1007 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1008 if (!ir->urb_in)
1009 goto urb_in_alloc_fail;
1010
1011 ir->usbdev = dev;
1012 ir->dev = &intf->dev;
1013 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001014 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001015 ir->flags.tx_mask_inverted = tx_mask_inverted;
Maxim Levitsky46519182010-10-16 19:56:28 -03001016 init_ir_raw_event(&ir->rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001017
1018 /* Saving usb interface data for use by the transmitter routine */
1019 ir->usb_ep_in = ep_in;
1020 ir->usb_ep_out = ep_out;
1021
1022 if (dev->descriptor.iManufacturer
1023 && usb_string(dev, dev->descriptor.iManufacturer,
1024 buf, sizeof(buf)) > 0)
1025 strlcpy(name, buf, sizeof(name));
1026 if (dev->descriptor.iProduct
1027 && usb_string(dev, dev->descriptor.iProduct,
1028 buf, sizeof(buf)) > 0)
1029 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1030 " %s", buf);
1031
1032 ir->idev = mceusb_init_input_dev(ir);
1033 if (!ir->idev)
1034 goto input_dev_fail;
1035
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001036 /* flush buffers on the device */
1037 mce_sync_in(ir, NULL, maxp);
1038 mce_sync_in(ir, NULL, maxp);
1039
1040 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001041 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1042 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1043 ir->urb_in->transfer_dma = ir->dma_in;
1044 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1045
Jarod Wilson66e89522010-06-01 17:32:08 -03001046 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001047 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001048 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001049 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001050 mceusb_gen2_init(ir);
1051
Jarod Wilson22b07662010-07-08 12:38:57 -03001052 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001053
Jarod Wilson657290b2010-06-16 17:10:05 -03001054 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001055
1056 usb_set_intfdata(intf, ir);
1057
1058 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1059 dev->bus->busnum, dev->devnum);
1060
1061 return 0;
1062
1063 /* Error-handling path */
1064input_dev_fail:
1065 usb_free_urb(ir->urb_in);
1066urb_in_alloc_fail:
1067 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1068buf_in_alloc_fail:
1069 kfree(ir);
1070mem_alloc_fail:
1071 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1072
1073 return -ENOMEM;
1074}
1075
1076
1077static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1078{
1079 struct usb_device *dev = interface_to_usbdev(intf);
1080 struct mceusb_dev *ir = usb_get_intfdata(intf);
1081
1082 usb_set_intfdata(intf, NULL);
1083
1084 if (!ir)
1085 return;
1086
1087 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001088 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001089 usb_kill_urb(ir->urb_in);
1090 usb_free_urb(ir->urb_in);
1091 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1092
1093 kfree(ir);
1094}
1095
1096static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1097{
1098 struct mceusb_dev *ir = usb_get_intfdata(intf);
1099 dev_info(ir->dev, "suspend\n");
1100 usb_kill_urb(ir->urb_in);
1101 return 0;
1102}
1103
1104static int mceusb_dev_resume(struct usb_interface *intf)
1105{
1106 struct mceusb_dev *ir = usb_get_intfdata(intf);
1107 dev_info(ir->dev, "resume\n");
1108 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1109 return -EIO;
1110 return 0;
1111}
1112
1113static struct usb_driver mceusb_dev_driver = {
1114 .name = DRIVER_NAME,
1115 .probe = mceusb_dev_probe,
1116 .disconnect = mceusb_dev_disconnect,
1117 .suspend = mceusb_dev_suspend,
1118 .resume = mceusb_dev_resume,
1119 .reset_resume = mceusb_dev_resume,
1120 .id_table = mceusb_dev_table
1121};
1122
1123static int __init mceusb_dev_init(void)
1124{
1125 int ret;
1126
1127 ret = usb_register(&mceusb_dev_driver);
1128 if (ret < 0)
1129 printk(KERN_ERR DRIVER_NAME
1130 ": usb register failed, result = %d\n", ret);
1131
1132 return ret;
1133}
1134
1135static void __exit mceusb_dev_exit(void)
1136{
1137 usb_deregister(&mceusb_dev_driver);
1138}
1139
1140module_init(mceusb_dev_init);
1141module_exit(mceusb_dev_exit);
1142
1143MODULE_DESCRIPTION(DRIVER_DESC);
1144MODULE_AUTHOR(DRIVER_AUTHOR);
1145MODULE_LICENSE("GPL");
1146MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1147
1148module_param(debug, bool, S_IRUGO | S_IWUSR);
1149MODULE_PARM_DESC(debug, "Debug enabled or not");