blob: 6c39188672577e21b4317bc5077c4323afad116a [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 */
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -030059#define MCE_CONTROL_HEADER 0x9f /* MCE status header */
Jarod Wilson66e89522010-06-01 17:32:08 -030060#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 */
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -030064#define MCE_PULSE_MASK 0x7f /* Pulse mask */
65#define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
66#define MCE_COMMAND_MASK 0xe0 /* Mask out command bits */
67#define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
68#define MCE_COMMAND_IRDATA 0x80 /* buf & MCE_COMMAND_MASK == 0x80 -> IR data */
Jarod Wilson66e89522010-06-01 17:32:08 -030069
70
71/* module parameters */
72#ifdef CONFIG_USB_DEBUG
73static int debug = 1;
74#else
75static int debug;
76#endif
77
78/* general constants */
79#define SEND_FLAG_IN_PROGRESS 1
80#define SEND_FLAG_COMPLETE 2
81#define RECV_FLAG_IN_PROGRESS 3
82#define RECV_FLAG_COMPLETE 4
83
84#define MCEUSB_RX 1
85#define MCEUSB_TX 2
86
87#define VENDOR_PHILIPS 0x0471
88#define VENDOR_SMK 0x0609
89#define VENDOR_TATUNG 0x1460
90#define VENDOR_GATEWAY 0x107b
91#define VENDOR_SHUTTLE 0x1308
92#define VENDOR_SHUTTLE2 0x051c
93#define VENDOR_MITSUMI 0x03ee
94#define VENDOR_TOPSEED 0x1784
95#define VENDOR_RICAVISION 0x179d
96#define VENDOR_ITRON 0x195d
97#define VENDOR_FIC 0x1509
98#define VENDOR_LG 0x043e
99#define VENDOR_MICROSOFT 0x045e
100#define VENDOR_FORMOSA 0x147a
101#define VENDOR_FINTEK 0x1934
102#define VENDOR_PINNACLE 0x2304
103#define VENDOR_ECS 0x1019
104#define VENDOR_WISTRON 0x0fb8
105#define VENDOR_COMPRO 0x185b
106#define VENDOR_NORTHSTAR 0x04eb
107#define VENDOR_REALTEK 0x0bda
108#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300109#define VENDOR_CONEXANT 0x0572
Jarod Wilson66e89522010-06-01 17:32:08 -0300110
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300111enum mceusb_model_type {
112 MCE_GEN2 = 0, /* Most boards */
113 MCE_GEN1,
114 MCE_GEN3,
115 MCE_GEN2_TX_INV,
116 POLARIS_EVK,
117};
118
119struct mceusb_model {
120 u32 mce_gen1:1;
121 u32 mce_gen2:1;
122 u32 mce_gen3:1;
123 u32 tx_mask_inverted:1;
124 u32 is_polaris:1;
125
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300126 const char *rc_map; /* Allow specify a per-board map */
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300127 const char *name; /* per-board name */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300128};
129
130static const struct mceusb_model mceusb_model[] = {
131 [MCE_GEN1] = {
132 .mce_gen1 = 1,
133 .tx_mask_inverted = 1,
134 },
135 [MCE_GEN2] = {
136 .mce_gen2 = 1,
137 },
138 [MCE_GEN2_TX_INV] = {
139 .mce_gen2 = 1,
140 .tx_mask_inverted = 1,
141 },
142 [MCE_GEN3] = {
143 .mce_gen3 = 1,
144 .tx_mask_inverted = 1,
145 },
146 [POLARIS_EVK] = {
147 .is_polaris = 1,
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300148 /*
149 * In fact, the EVK is shipped without
150 * remotes, but we should have something handy,
151 * to allow testing it
152 */
153 .rc_map = RC_MAP_RC5_HAUPPAUGE_NEW,
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300154 .name = "cx231xx MCE IR",
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300155 },
156};
157
Jarod Wilson66e89522010-06-01 17:32:08 -0300158static struct usb_device_id mceusb_dev_table[] = {
159 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300160 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
161 .driver_info = MCE_GEN1 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300162 /* Philips Infrared Transceiver - Sahara branded */
163 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
164 /* Philips Infrared Transceiver - HP branded */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300165 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
166 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300167 /* Philips SRM5100 */
168 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
169 /* Philips Infrared Transceiver - Omaura */
170 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
171 /* Philips Infrared Transceiver - Spinel plus */
172 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
173 /* Philips eHome Infrared Transceiver */
174 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300175 /* Philips/Spinel plus IR transceiver for ASUS */
176 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
177 /* Philips/Spinel plus IR transceiver for ASUS */
178 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300179 /* Realtek MCE IR Receiver */
180 { USB_DEVICE(VENDOR_REALTEK, 0x0161) },
181 /* SMK/Toshiba G83C0004D410 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300182 { USB_DEVICE(VENDOR_SMK, 0x031d),
183 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300184 /* SMK eHome Infrared Transceiver (Sony VAIO) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300185 { USB_DEVICE(VENDOR_SMK, 0x0322),
186 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300187 /* bundled with Hauppauge PVR-150 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300188 { USB_DEVICE(VENDOR_SMK, 0x0334),
189 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300190 /* SMK eHome Infrared Transceiver */
191 { USB_DEVICE(VENDOR_SMK, 0x0338) },
192 /* Tatung eHome Infrared Transceiver */
193 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
194 /* Shuttle eHome Infrared Transceiver */
195 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
196 /* Shuttle eHome Infrared Transceiver */
197 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
198 /* Gateway eHome Infrared Transceiver */
199 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
200 /* Mitsumi */
201 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
202 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300203 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
204 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300205 /* Topseed HP eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300206 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
207 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300208 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300209 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
210 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300211 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300212 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
213 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300214 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300215 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
216 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300217 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300218 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
219 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300220 /* Ricavision internal Infrared Transceiver */
221 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
222 /* Itron ione Libra Q-11 */
223 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
224 /* FIC eHome Infrared Transceiver */
225 { USB_DEVICE(VENDOR_FIC, 0x9242) },
226 /* LG eHome Infrared Transceiver */
227 { USB_DEVICE(VENDOR_LG, 0x9803) },
228 /* Microsoft MCE Infrared Transceiver */
229 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
230 /* Formosa eHome Infrared Transceiver */
231 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
232 /* Formosa21 / eHome Infrared Receiver */
233 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
234 /* Formosa aim / Trust MCE Infrared Receiver */
235 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
236 /* Formosa Industrial Computing / Beanbag Emulation Device */
237 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
238 /* Formosa21 / eHome Infrared Receiver */
239 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
240 /* Formosa Industrial Computing AIM IR605/A */
241 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
242 /* Formosa Industrial Computing */
243 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
244 /* Fintek eHome Infrared Transceiver */
245 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
246 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
247 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
248 /* Pinnacle Remote Kit */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300249 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
250 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300251 /* Elitegroup Computer Systems IR */
252 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
253 /* Wistron Corp. eHome Infrared Receiver */
254 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
255 /* Compro K100 */
256 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
257 /* Compro K100 v2 */
258 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
259 /* Northstar Systems, Inc. eHome Infrared Transceiver */
260 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
261 /* TiVo PC IR Receiver */
262 { USB_DEVICE(VENDOR_TIVO, 0x2000) },
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300263 /* Conexant SDK */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300264 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
265 .driver_info = POLARIS_EVK },
Jarod Wilson66e89522010-06-01 17:32:08 -0300266 /* Terminating entry */
267 { }
268};
269
Jarod Wilson66e89522010-06-01 17:32:08 -0300270/* data structure for each usb transceiver */
271struct mceusb_dev {
272 /* ir-core bits */
Jarod Wilson66e89522010-06-01 17:32:08 -0300273 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300274 struct ir_raw_event rawir;
275
276 /* core device bits */
277 struct device *dev;
278 struct input_dev *idev;
279
280 /* usb */
281 struct usb_device *usbdev;
282 struct urb *urb_in;
283 struct usb_endpoint_descriptor *usb_ep_in;
284 struct usb_endpoint_descriptor *usb_ep_out;
285
286 /* buffers and dma */
287 unsigned char *buf_in;
288 unsigned int len_in;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300289
290 enum {
291 CMD_HEADER = 0,
292 SUBCMD,
293 CMD_DATA,
294 PARSE_IRDATA,
295 } parser_state;
296 u8 cmd, rem; /* Remaining IR data bytes in packet */
297
Jarod Wilson66e89522010-06-01 17:32:08 -0300298 dma_addr_t dma_in;
299 dma_addr_t dma_out;
300
301 struct {
302 u32 connected:1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300303 u32 tx_mask_inverted:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300304 u32 microsoft_gen1:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300305 } flags;
306
Jarod Wilsone23fb962010-06-16 17:55:52 -0300307 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300308 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300309 u32 carrier;
310 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300311
312 char name[128];
313 char phys[64];
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300314 enum mceusb_model_type model;
Jarod Wilson66e89522010-06-01 17:32:08 -0300315};
316
317/*
318 * MCE Device Command Strings
319 * Device command responses vary from device to device...
320 * - DEVICE_RESET resets the hardware to its default state
321 * - GET_REVISION fetches the hardware/software revision, common
322 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
323 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
324 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
325 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
326 * ((clk / frequency) - 1)
327 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
328 * response in the form of 9f 0c msb lsb
329 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
330 * the form of 9f 08 bm, where bm is the bitmask
331 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
332 * general use one or short-range learning one, in the form of
333 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
334 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
335 * - SET_TX_BITMASK sets the transmitter bitmask
336 * - SET_RX_TIMEOUT sets the receiver timeout
337 * - SET_RX_SENSOR sets which receiver sensor to use
338 */
339static char DEVICE_RESET[] = {0x00, 0xff, 0xaa};
340static char GET_REVISION[] = {0xff, 0x0b};
341static char GET_UNKNOWN[] = {0xff, 0x18};
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300342static char GET_UNKNOWN2[] = {MCE_CONTROL_HEADER, 0x05};
343static char GET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x07};
344static char GET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0d};
345static char GET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x13};
346static char GET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x15};
Jarod Wilson66e89522010-06-01 17:32:08 -0300347/* sub in desired values in lower byte or bytes for full command */
348/* FIXME: make use of these for transmit.
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300349static char SET_CARRIER_FREQ[] = {MCE_CONTROL_HEADER, 0x06, 0x00, 0x00};
350static char SET_TX_BITMASK[] = {MCE_CONTROL_HEADER, 0x08, 0x00};
351static char SET_RX_TIMEOUT[] = {MCE_CONTROL_HEADER, 0x0c, 0x00, 0x00};
352static char SET_RX_SENSOR[] = {MCE_CONTROL_HEADER, 0x14, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300353*/
354
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300355static int mceusb_cmdsize(u8 cmd, u8 subcmd)
356{
357 int datasize = 0;
358
359 switch (cmd) {
360 case 0x00:
361 if (subcmd == 0xff)
362 datasize = 1;
363 break;
364 case 0xff:
365 switch (subcmd) {
366 case 0x0b:
367 datasize = 2;
368 break;
369 }
370 case MCE_CONTROL_HEADER:
371 switch (subcmd) {
372 case 0x04:
373 case 0x06:
374 case 0x0c:
375 case 0x15:
376 datasize = 2;
377 break;
378 case 0x08:
379 case 0x14:
380 datasize = 1;
381 break;
382 }
383 }
384 return datasize;
385}
386
Jarod Wilson66e89522010-06-01 17:32:08 -0300387static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
388 int len, bool out)
389{
390 char codes[USB_BUFLEN * 3 + 1];
391 char inout[9];
392 int i;
393 u8 cmd, subcmd, data1, data2;
394 struct device *dev = ir->dev;
Jarod Wilson657290b2010-06-16 17:10:05 -0300395 int idx = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300396
Jarod Wilson657290b2010-06-16 17:10:05 -0300397 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
398 if (ir->flags.microsoft_gen1 && !out)
399 idx = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300400
Jarod Wilson657290b2010-06-16 17:10:05 -0300401 if (len <= idx)
Jarod Wilson66e89522010-06-01 17:32:08 -0300402 return;
403
404 for (i = 0; i < len && i < USB_BUFLEN; i++)
405 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xFF);
406
407 dev_info(dev, "%sx data: %s (length=%d)\n",
408 (out ? "t" : "r"), codes, len);
409
410 if (out)
411 strcpy(inout, "Request\0");
412 else
413 strcpy(inout, "Got\0");
414
Jarod Wilson657290b2010-06-16 17:10:05 -0300415 cmd = buf[idx] & 0xff;
416 subcmd = buf[idx + 1] & 0xff;
417 data1 = buf[idx + 2] & 0xff;
418 data2 = buf[idx + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300419
420 switch (cmd) {
421 case 0x00:
422 if (subcmd == 0xff && data1 == 0xaa)
423 dev_info(dev, "Device reset requested\n");
424 else
425 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
426 cmd, subcmd);
427 break;
428 case 0xff:
429 switch (subcmd) {
430 case 0x0b:
431 if (len == 2)
432 dev_info(dev, "Get hw/sw rev?\n");
433 else
434 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
435 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson657290b2010-06-16 17:10:05 -0300436 buf[idx + 4], buf[idx + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300437 break;
438 case 0xaa:
439 dev_info(dev, "Device reset requested\n");
440 break;
441 case 0xfe:
442 dev_info(dev, "Previous command not supported\n");
443 break;
444 case 0x18:
445 case 0x1b:
446 default:
447 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
448 cmd, subcmd);
449 break;
450 }
451 break;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300452 case MCE_CONTROL_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300453 switch (subcmd) {
454 case 0x03:
455 dev_info(dev, "Ping\n");
456 break;
457 case 0x04:
458 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
459 data1, data2);
460 break;
461 case 0x06:
462 dev_info(dev, "%s carrier mode and freq of "
463 "0x%02x 0x%02x\n", inout, data1, data2);
464 break;
465 case 0x07:
466 dev_info(dev, "Get carrier mode and freq\n");
467 break;
468 case 0x08:
469 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
470 inout, data1);
471 break;
472 case 0x0c:
473 /* value is in units of 50us, so x*50/100 or x/2 ms */
474 dev_info(dev, "%s receive timeout of %d ms\n",
475 inout, ((data1 << 8) | data2) / 2);
476 break;
477 case 0x0d:
478 dev_info(dev, "Get receive timeout\n");
479 break;
480 case 0x13:
481 dev_info(dev, "Get transmit blaster mask\n");
482 break;
483 case 0x14:
484 dev_info(dev, "%s %s-range receive sensor in use\n",
485 inout, data1 == 0x02 ? "short" : "long");
486 break;
487 case 0x15:
488 if (len == 2)
489 dev_info(dev, "Get receive sensor\n");
490 else
491 dev_info(dev, "Received pulse count is %d\n",
492 ((data1 << 8) | data2));
493 break;
494 case 0xfe:
495 dev_info(dev, "Error! Hardware is likely wedged...\n");
496 break;
497 case 0x05:
498 case 0x09:
499 case 0x0f:
500 default:
501 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
502 cmd, subcmd);
503 break;
504 }
505 break;
506 default:
507 break;
508 }
509}
510
Jarod Wilson7c294402010-08-02 18:21:06 -0300511static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300512{
513 struct mceusb_dev *ir;
514 int len;
515
516 if (!urb)
517 return;
518
519 ir = urb->context;
520 if (ir) {
521 len = urb->actual_length;
522
523 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
524 urb->status, len);
525
526 if (debug)
527 mceusb_dev_printdata(ir, urb->transfer_buffer,
528 len, true);
529 }
530
531}
532
533/* request incoming or send outgoing usb packet - used to initialize remote */
534static void mce_request_packet(struct mceusb_dev *ir,
535 struct usb_endpoint_descriptor *ep,
536 unsigned char *data, int size, int urb_type)
537{
538 int res;
539 struct urb *async_urb;
540 struct device *dev = ir->dev;
541 unsigned char *async_buf;
542
543 if (urb_type == MCEUSB_TX) {
544 async_urb = usb_alloc_urb(0, GFP_KERNEL);
545 if (unlikely(!async_urb)) {
546 dev_err(dev, "Error, couldn't allocate urb!\n");
547 return;
548 }
549
550 async_buf = kzalloc(size, GFP_KERNEL);
551 if (!async_buf) {
552 dev_err(dev, "Error, couldn't allocate buf!\n");
553 usb_free_urb(async_urb);
554 return;
555 }
556
557 /* outbound data */
558 usb_fill_int_urb(async_urb, ir->usbdev,
559 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300560 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300561 ir, ep->bInterval);
562 memcpy(async_buf, data, size);
563
564 } else if (urb_type == MCEUSB_RX) {
565 /* standard request */
566 async_urb = ir->urb_in;
567 ir->send_flags = RECV_FLAG_IN_PROGRESS;
568
569 } else {
570 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
571 return;
572 }
573
574 dev_dbg(dev, "receive request called (size=%#x)\n", size);
575
576 async_urb->transfer_buffer_length = size;
577 async_urb->dev = ir->usbdev;
578
579 res = usb_submit_urb(async_urb, GFP_ATOMIC);
580 if (res) {
581 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
582 return;
583 }
584 dev_dbg(dev, "receive request complete (res=%d)\n", res);
585}
586
587static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
588{
589 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
590}
591
592static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
593{
594 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
595}
596
Jarod Wilsone23fb962010-06-16 17:55:52 -0300597/* Send data out the IR blaster port(s) */
598static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
599{
600 struct mceusb_dev *ir = priv;
601 int i, ret = 0;
602 int count, cmdcount = 0;
603 unsigned char *cmdbuf; /* MCE command buffer */
604 long signal_duration = 0; /* Singnal length in us */
605 struct timeval start_time, end_time;
606
607 do_gettimeofday(&start_time);
608
609 count = n / sizeof(int);
610
611 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
612 if (!cmdbuf)
613 return -ENOMEM;
614
615 /* MCE tx init header */
616 cmdbuf[cmdcount++] = MCE_CONTROL_HEADER;
617 cmdbuf[cmdcount++] = 0x08;
618 cmdbuf[cmdcount++] = ir->tx_mask;
619
620 /* Generate mce packet data */
621 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
622 signal_duration += txbuf[i];
623 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
624
625 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
626
627 /* Insert mce packet header every 4th entry */
628 if ((cmdcount < MCE_CMDBUF_SIZE) &&
629 (cmdcount - MCE_TX_HEADER_LENGTH) %
630 MCE_CODE_LENGTH == 0)
631 cmdbuf[cmdcount++] = MCE_PACKET_HEADER;
632
633 /* Insert mce packet data */
634 if (cmdcount < MCE_CMDBUF_SIZE)
635 cmdbuf[cmdcount++] =
636 (txbuf[i] < MCE_PULSE_BIT ?
637 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
638 (i & 1 ? 0x00 : MCE_PULSE_BIT);
639 else {
640 ret = -EINVAL;
641 goto out;
642 }
643
644 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
645 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
646 }
647
648 /* Fix packet length in last header */
649 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
650 0x80 + (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH - 1;
651
652 /* Check if we have room for the empty packet at the end */
653 if (cmdcount >= MCE_CMDBUF_SIZE) {
654 ret = -EINVAL;
655 goto out;
656 }
657
658 /* All mce commands end with an empty packet (0x80) */
659 cmdbuf[cmdcount++] = 0x80;
660
661 /* Transmit the command to the mce device */
662 mce_async_out(ir, cmdbuf, cmdcount);
663
664 /*
665 * The lircd gap calculation expects the write function to
666 * wait the time it takes for the ircommand to be sent before
667 * it returns.
668 */
669 do_gettimeofday(&end_time);
670 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
671 (end_time.tv_sec - start_time.tv_sec) * 1000000;
672
673 /* delay with the closest number of ticks */
674 set_current_state(TASK_INTERRUPTIBLE);
675 schedule_timeout(usecs_to_jiffies(signal_duration));
676
677out:
678 kfree(cmdbuf);
679 return ret ? ret : n;
680}
681
Jarod Wilson657290b2010-06-16 17:10:05 -0300682/* Sets active IR outputs -- mce devices typically (all?) have two */
683static int mceusb_set_tx_mask(void *priv, u32 mask)
684{
685 struct mceusb_dev *ir = priv;
686
687 if (ir->flags.tx_mask_inverted)
688 ir->tx_mask = (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
689 else
690 ir->tx_mask = mask;
691
692 return 0;
693}
694
Jarod Wilsone23fb962010-06-16 17:55:52 -0300695/* Sets the send carrier frequency and mode */
696static int mceusb_set_tx_carrier(void *priv, u32 carrier)
697{
698 struct mceusb_dev *ir = priv;
699 int clk = 10000000;
700 int prescaler = 0, divisor = 0;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300701 unsigned char cmdbuf[4] = { MCE_CONTROL_HEADER, 0x06, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300702
703 /* Carrier has changed */
704 if (ir->carrier != carrier) {
705
706 if (carrier == 0) {
707 ir->carrier = carrier;
708 cmdbuf[2] = 0x01;
709 cmdbuf[3] = 0x80;
710 dev_dbg(ir->dev, "%s: disabling carrier "
711 "modulation\n", __func__);
712 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
713 return carrier;
714 }
715
716 for (prescaler = 0; prescaler < 4; ++prescaler) {
717 divisor = (clk >> (2 * prescaler)) / carrier;
718 if (divisor <= 0xFF) {
719 ir->carrier = carrier;
720 cmdbuf[2] = prescaler;
721 cmdbuf[3] = divisor;
722 dev_dbg(ir->dev, "%s: requesting %u HZ "
723 "carrier\n", __func__, carrier);
724
725 /* Transmit new carrier to mce device */
726 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
727 return carrier;
728 }
729 }
730
731 return -EINVAL;
732
733 }
734
735 return carrier;
736}
737
Jarod Wilson66e89522010-06-01 17:32:08 -0300738static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
739{
Maxim Levitsky46519182010-10-16 19:56:28 -0300740 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300741 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300742
743 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
744 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300745 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300746
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300747 for (; i < buf_len; i++) {
748 switch (ir->parser_state) {
749 case SUBCMD:
750 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
751 ir->parser_state = CMD_DATA;
752 break;
753 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300754 ir->rem--;
Jarod Wilson66e89522010-06-01 17:32:08 -0300755 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
756 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
757 * MCE_TIME_UNIT * 1000;
758
759 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300760 if (ir->rawir.pulse == rawir.pulse) {
Jarod Wilson66e89522010-06-01 17:32:08 -0300761 ir->rawir.duration += rawir.duration;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300762 } else {
Jarod Wilson66e89522010-06-01 17:32:08 -0300763 ir->rawir.duration = rawir.duration;
764 ir->rawir.pulse = rawir.pulse;
765 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300766 if (ir->rem)
767 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300768 }
769 rawir.duration += ir->rawir.duration;
770 ir->rawir.duration = 0;
771 ir->rawir.pulse = rawir.pulse;
772
773 dev_dbg(ir->dev, "Storing %s with duration %d\n",
774 rawir.pulse ? "pulse" : "space",
775 rawir.duration);
776
777 ir_raw_event_store(ir->idev, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300778 break;
779 case CMD_DATA:
780 ir->rem--;
781 break;
782 case CMD_HEADER:
783 /* decode mce packets of the form (84),AA,BB,CC,DD */
784 /* IR data packets can span USB messages - rem */
785 ir->cmd = ir->buf_in[i];
786 if ((ir->cmd == MCE_CONTROL_HEADER) ||
787 ((ir->cmd & MCE_COMMAND_MASK) != MCE_COMMAND_IRDATA)) {
788 ir->parser_state = SUBCMD;
789 continue;
790 }
791 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
792 dev_dbg(ir->dev, "Processing RX data: len = %d\n",
793 ir->rem);
794 if (ir->rem) {
795 ir->parser_state = PARSE_IRDATA;
796 break;
797 }
798 /*
799 * a package with len=0 (e. g. 0x80) means end of
800 * data. We could use it to do the call to
801 * ir_raw_event_handle(). For now, we don't need to
802 * use it.
803 */
804 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300805 }
806
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300807 if (ir->parser_state != CMD_HEADER && !ir->rem)
808 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300809 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300810 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
811 ir_raw_event_handle(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -0300812}
813
Jarod Wilson66e89522010-06-01 17:32:08 -0300814static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
815{
816 struct mceusb_dev *ir;
817 int buf_len;
818
819 if (!urb)
820 return;
821
822 ir = urb->context;
823 if (!ir) {
824 usb_unlink_urb(urb);
825 return;
826 }
827
828 buf_len = urb->actual_length;
829
Jarod Wilson66e89522010-06-01 17:32:08 -0300830 if (debug)
831 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
832
833 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
834 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300835 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300836 buf_len);
837 }
838
839 switch (urb->status) {
840 /* success */
841 case 0:
842 mceusb_process_ir_data(ir, buf_len);
843 break;
844
845 case -ECONNRESET:
846 case -ENOENT:
847 case -ESHUTDOWN:
848 usb_unlink_urb(urb);
849 return;
850
851 case -EPIPE:
852 default:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300853 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300854 break;
855 }
856
857 usb_submit_urb(urb, GFP_ATOMIC);
858}
859
860static void mceusb_gen1_init(struct mceusb_dev *ir)
861{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300862 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300863 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300864 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300865 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300866
867 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
868 if (!data) {
869 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300870 return;
871 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300872
873 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300874 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300875 * on the receive control pipe and expect a certain value pair back
876 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300877 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
878 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300879 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300880 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
881 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
882 __func__, data[0], data[1]);
883
884 /* set feature: bit rate 38400 bps */
885 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
886 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
887 0xc04e, 0x0000, NULL, 0, HZ * 3);
888
889 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
890
891 /* bRequest 4: set char length to 8 bits */
892 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
893 4, USB_TYPE_VENDOR,
894 0x0808, 0x0000, NULL, 0, HZ * 3);
895 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
896
897 /* bRequest 2: set handshaking to use DTR/DSR */
898 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
899 2, USB_TYPE_VENDOR,
900 0x0000, 0x0100, NULL, 0, HZ * 3);
901 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300902
Jarod Wilson22b07662010-07-08 12:38:57 -0300903 /* device reset */
904 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
905 mce_sync_in(ir, NULL, maxp);
906
907 /* get hw/sw revision? */
908 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
909 mce_sync_in(ir, NULL, maxp);
910
Jarod Wilson657290b2010-06-16 17:10:05 -0300911 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -0300912};
913
914static void mceusb_gen2_init(struct mceusb_dev *ir)
915{
916 int maxp = ir->len_in;
917
Jarod Wilson66e89522010-06-01 17:32:08 -0300918 /* device reset */
919 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
920 mce_sync_in(ir, NULL, maxp);
921
922 /* get hw/sw revision? */
923 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
924 mce_sync_in(ir, NULL, maxp);
925
Jarod Wilson22b07662010-07-08 12:38:57 -0300926 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -0300927 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
928 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -0300929 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
930 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -0300931}
932
Jarod Wilson22b07662010-07-08 12:38:57 -0300933static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -0300934{
935 int maxp = ir->len_in;
936
Jarod Wilson66e89522010-06-01 17:32:08 -0300937 /* get the carrier and frequency */
938 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
939 mce_sync_in(ir, NULL, maxp);
940
941 /* get the transmitter bitmask */
942 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
943 mce_sync_in(ir, NULL, maxp);
944
945 /* get receiver timeout value */
946 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
947 mce_sync_in(ir, NULL, maxp);
948
949 /* get receiver sensor setting */
950 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
951 mce_sync_in(ir, NULL, maxp);
952}
953
954static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
955{
956 struct input_dev *idev;
957 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300958 struct device *dev = ir->dev;
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300959 const char *rc_map = RC_MAP_RC6_MCE;
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300960 const char *name = "Media Center Ed. eHome Infrared Remote Transceiver";
Jarod Wilson66e89522010-06-01 17:32:08 -0300961 int ret = -ENODEV;
962
963 idev = input_allocate_device();
964 if (!idev) {
965 dev_err(dev, "remote input dev allocation failed\n");
966 goto idev_alloc_failed;
967 }
968
969 ret = -ENOMEM;
970 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
971 if (!props) {
972 dev_err(dev, "remote ir dev props allocation failed\n");
973 goto props_alloc_failed;
974 }
975
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300976 if (mceusb_model[ir->model].name)
977 name = mceusb_model[ir->model].name;
978
979 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
980 name,
Jarod Wilson66e89522010-06-01 17:32:08 -0300981 le16_to_cpu(ir->usbdev->descriptor.idVendor),
982 le16_to_cpu(ir->usbdev->descriptor.idProduct));
983
Jarod Wilson66e89522010-06-01 17:32:08 -0300984 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -0300985 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
986 strlcat(ir->phys, "/input0", sizeof(ir->phys));
987 idev->phys = ir->phys;
988
Jarod Wilson66e89522010-06-01 17:32:08 -0300989 props->priv = ir;
990 props->driver_type = RC_DRIVER_IR_RAW;
991 props->allowed_protos = IR_TYPE_ALL;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300992 props->s_tx_mask = mceusb_set_tx_mask;
993 props->s_tx_carrier = mceusb_set_tx_carrier;
994 props->tx_ir = mceusb_tx_ir;
Jarod Wilson66e89522010-06-01 17:32:08 -0300995
996 ir->props = props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300997
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300998 if (mceusb_model[ir->model].rc_map)
999 rc_map = mceusb_model[ir->model].rc_map;
1000
1001 ret = ir_input_register(idev, rc_map, props, DRIVER_NAME);
Jarod Wilson66e89522010-06-01 17:32:08 -03001002 if (ret < 0) {
1003 dev_err(dev, "remote input device register failed\n");
1004 goto irdev_failed;
1005 }
1006
1007 return idev;
1008
1009irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -03001010 kfree(props);
1011props_alloc_failed:
1012 input_free_device(idev);
1013idev_alloc_failed:
1014 return NULL;
1015}
1016
1017static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1018 const struct usb_device_id *id)
1019{
1020 struct usb_device *dev = interface_to_usbdev(intf);
1021 struct usb_host_interface *idesc;
1022 struct usb_endpoint_descriptor *ep = NULL;
1023 struct usb_endpoint_descriptor *ep_in = NULL;
1024 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001025 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -03001026 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -03001027 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001028 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001029 bool is_gen3;
1030 bool is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001031 bool tx_mask_inverted;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001032 bool is_polaris;
Jarod Wilson66e89522010-06-01 17:32:08 -03001033
1034 dev_dbg(&intf->dev, ": %s called\n", __func__);
1035
Jarod Wilson66e89522010-06-01 17:32:08 -03001036 idesc = intf->cur_altsetting;
1037
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001038 is_gen3 = mceusb_model[model].mce_gen3;
1039 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1040 tx_mask_inverted = mceusb_model[model].tx_mask_inverted;
1041 is_polaris = mceusb_model[model].is_polaris;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001042
1043 if (is_polaris) {
1044 /* Interface 0 is IR */
1045 if (idesc->desc.bInterfaceNumber)
1046 return -ENODEV;
1047 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001048
1049 /* step through the endpoints to find first bulk in and out endpoint */
1050 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1051 ep = &idesc->endpoint[i].desc;
1052
1053 if ((ep_in == NULL)
1054 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1055 == USB_DIR_IN)
1056 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1057 == USB_ENDPOINT_XFER_BULK)
1058 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1059 == USB_ENDPOINT_XFER_INT))) {
1060
Jarod Wilson66e89522010-06-01 17:32:08 -03001061 ep_in = ep;
1062 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001063 ep_in->bInterval = 1;
1064 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
1065 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001066 }
1067
1068 if ((ep_out == NULL)
1069 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1070 == USB_DIR_OUT)
1071 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1072 == USB_ENDPOINT_XFER_BULK)
1073 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1074 == USB_ENDPOINT_XFER_INT))) {
1075
Jarod Wilson66e89522010-06-01 17:32:08 -03001076 ep_out = ep;
1077 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001078 ep_out->bInterval = 1;
1079 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
1080 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001081 }
1082 }
1083 if (ep_in == NULL) {
1084 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
1085 return -ENODEV;
1086 }
1087
1088 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1089 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1090
1091 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1092 if (!ir)
1093 goto mem_alloc_fail;
1094
1095 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1096 if (!ir->buf_in)
1097 goto buf_in_alloc_fail;
1098
1099 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1100 if (!ir->urb_in)
1101 goto urb_in_alloc_fail;
1102
1103 ir->usbdev = dev;
1104 ir->dev = &intf->dev;
1105 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001106 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001107 ir->flags.tx_mask_inverted = tx_mask_inverted;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001108 ir->model = model;
1109
Maxim Levitsky46519182010-10-16 19:56:28 -03001110 init_ir_raw_event(&ir->rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001111
1112 /* Saving usb interface data for use by the transmitter routine */
1113 ir->usb_ep_in = ep_in;
1114 ir->usb_ep_out = ep_out;
1115
1116 if (dev->descriptor.iManufacturer
1117 && usb_string(dev, dev->descriptor.iManufacturer,
1118 buf, sizeof(buf)) > 0)
1119 strlcpy(name, buf, sizeof(name));
1120 if (dev->descriptor.iProduct
1121 && usb_string(dev, dev->descriptor.iProduct,
1122 buf, sizeof(buf)) > 0)
1123 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1124 " %s", buf);
1125
1126 ir->idev = mceusb_init_input_dev(ir);
1127 if (!ir->idev)
1128 goto input_dev_fail;
1129
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001130 /* flush buffers on the device */
1131 mce_sync_in(ir, NULL, maxp);
1132 mce_sync_in(ir, NULL, maxp);
1133
1134 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001135 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1136 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1137 ir->urb_in->transfer_dma = ir->dma_in;
1138 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1139
Jarod Wilson66e89522010-06-01 17:32:08 -03001140 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001141 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001142 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001143 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001144 mceusb_gen2_init(ir);
1145
Jarod Wilson22b07662010-07-08 12:38:57 -03001146 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001147
Jarod Wilson657290b2010-06-16 17:10:05 -03001148 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001149
1150 usb_set_intfdata(intf, ir);
1151
1152 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1153 dev->bus->busnum, dev->devnum);
1154
1155 return 0;
1156
1157 /* Error-handling path */
1158input_dev_fail:
1159 usb_free_urb(ir->urb_in);
1160urb_in_alloc_fail:
1161 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1162buf_in_alloc_fail:
1163 kfree(ir);
1164mem_alloc_fail:
1165 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1166
1167 return -ENOMEM;
1168}
1169
1170
1171static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1172{
1173 struct usb_device *dev = interface_to_usbdev(intf);
1174 struct mceusb_dev *ir = usb_get_intfdata(intf);
1175
1176 usb_set_intfdata(intf, NULL);
1177
1178 if (!ir)
1179 return;
1180
1181 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001182 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001183 usb_kill_urb(ir->urb_in);
1184 usb_free_urb(ir->urb_in);
1185 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1186
1187 kfree(ir);
1188}
1189
1190static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1191{
1192 struct mceusb_dev *ir = usb_get_intfdata(intf);
1193 dev_info(ir->dev, "suspend\n");
1194 usb_kill_urb(ir->urb_in);
1195 return 0;
1196}
1197
1198static int mceusb_dev_resume(struct usb_interface *intf)
1199{
1200 struct mceusb_dev *ir = usb_get_intfdata(intf);
1201 dev_info(ir->dev, "resume\n");
1202 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1203 return -EIO;
1204 return 0;
1205}
1206
1207static struct usb_driver mceusb_dev_driver = {
1208 .name = DRIVER_NAME,
1209 .probe = mceusb_dev_probe,
1210 .disconnect = mceusb_dev_disconnect,
1211 .suspend = mceusb_dev_suspend,
1212 .resume = mceusb_dev_resume,
1213 .reset_resume = mceusb_dev_resume,
1214 .id_table = mceusb_dev_table
1215};
1216
1217static int __init mceusb_dev_init(void)
1218{
1219 int ret;
1220
1221 ret = usb_register(&mceusb_dev_driver);
1222 if (ret < 0)
1223 printk(KERN_ERR DRIVER_NAME
1224 ": usb register failed, result = %d\n", ret);
1225
1226 return ret;
1227}
1228
1229static void __exit mceusb_dev_exit(void)
1230{
1231 usb_deregister(&mceusb_dev_driver);
1232}
1233
1234module_init(mceusb_dev_init);
1235module_exit(mceusb_dev_exit);
1236
1237MODULE_DESCRIPTION(DRIVER_DESC);
1238MODULE_AUTHOR(DRIVER_AUTHOR);
1239MODULE_LICENSE("GPL");
1240MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1241
1242module_param(debug, bool, S_IRUGO | S_IWUSR);
1243MODULE_PARM_DESC(debug, "Debug enabled or not");