blob: 9dce684fd23113d13a3d445995983089cff56c60 [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
Jarod Wilson4a883912010-10-22 14:42:54 -030049#define USB_BUFLEN 32 /* USB reception buffer length */
50#define USB_CTRL_MSG_SZ 2 /* Size of usb ctrl msg on gen1 hw */
51#define MCE_G1_INIT_MSGS 40 /* Init messages on gen1 hw to throw out */
Jarod Wilson66e89522010-06-01 17:32:08 -030052
53/* MCE constants */
Jarod Wilson4a883912010-10-22 14:42:54 -030054#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_IRDATA_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
59#define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
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 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
63#define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
64#define MCE_PULSE_MASK 0x7f /* Pulse mask */
65#define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
66
67#define MCE_HW_CMD_HEADER 0xff /* MCE hardware command header */
68#define MCE_COMMAND_HEADER 0x9f /* MCE command header */
69#define MCE_COMMAND_MASK 0xe0 /* Mask out command bits */
70#define MCE_COMMAND_NULL 0x00 /* These show up various places... */
71/* if buf[i] & MCE_COMMAND_MASK == 0x80 and buf[i] != MCE_COMMAND_HEADER,
72 * then we're looking at a raw IR data sample */
73#define MCE_COMMAND_IRDATA 0x80
74#define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
75
76/* Sub-commands, which follow MCE_COMMAND_HEADER or MCE_HW_CMD_HEADER */
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_CMD_TX_PORTS 0x16 /* Get number of TX ports */
95#define MCE_CMD_G_WAKESRC 0x17 /* Get wake source */
96#define MCE_CMD_UNKNOWN7 0x18 /* Unknown */
97#define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
98#define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
99#define MCE_CMD_DEVICE_RESET 0xaa /* Reset the hardware */
100#define MCE_RSP_CMD_INVALID 0xfe /* Invalid command issued */
Jarod Wilson66e89522010-06-01 17:32:08 -0300101
102
103/* module parameters */
104#ifdef CONFIG_USB_DEBUG
105static int debug = 1;
106#else
107static int debug;
108#endif
109
110/* general constants */
111#define SEND_FLAG_IN_PROGRESS 1
112#define SEND_FLAG_COMPLETE 2
113#define RECV_FLAG_IN_PROGRESS 3
114#define RECV_FLAG_COMPLETE 4
115
116#define MCEUSB_RX 1
117#define MCEUSB_TX 2
118
119#define VENDOR_PHILIPS 0x0471
120#define VENDOR_SMK 0x0609
121#define VENDOR_TATUNG 0x1460
122#define VENDOR_GATEWAY 0x107b
123#define VENDOR_SHUTTLE 0x1308
124#define VENDOR_SHUTTLE2 0x051c
125#define VENDOR_MITSUMI 0x03ee
126#define VENDOR_TOPSEED 0x1784
127#define VENDOR_RICAVISION 0x179d
128#define VENDOR_ITRON 0x195d
129#define VENDOR_FIC 0x1509
130#define VENDOR_LG 0x043e
131#define VENDOR_MICROSOFT 0x045e
132#define VENDOR_FORMOSA 0x147a
133#define VENDOR_FINTEK 0x1934
134#define VENDOR_PINNACLE 0x2304
135#define VENDOR_ECS 0x1019
136#define VENDOR_WISTRON 0x0fb8
137#define VENDOR_COMPRO 0x185b
138#define VENDOR_NORTHSTAR 0x04eb
139#define VENDOR_REALTEK 0x0bda
140#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300141#define VENDOR_CONEXANT 0x0572
Jarod Wilson66e89522010-06-01 17:32:08 -0300142
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300143enum mceusb_model_type {
144 MCE_GEN2 = 0, /* Most boards */
145 MCE_GEN1,
146 MCE_GEN3,
147 MCE_GEN2_TX_INV,
148 POLARIS_EVK,
149};
150
151struct mceusb_model {
152 u32 mce_gen1:1;
153 u32 mce_gen2:1;
154 u32 mce_gen3:1;
155 u32 tx_mask_inverted:1;
156 u32 is_polaris:1;
157
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300158 const char *rc_map; /* Allow specify a per-board map */
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300159 const char *name; /* per-board name */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300160};
161
162static const struct mceusb_model mceusb_model[] = {
163 [MCE_GEN1] = {
164 .mce_gen1 = 1,
165 .tx_mask_inverted = 1,
166 },
167 [MCE_GEN2] = {
168 .mce_gen2 = 1,
169 },
170 [MCE_GEN2_TX_INV] = {
171 .mce_gen2 = 1,
172 .tx_mask_inverted = 1,
173 },
174 [MCE_GEN3] = {
175 .mce_gen3 = 1,
176 .tx_mask_inverted = 1,
177 },
178 [POLARIS_EVK] = {
179 .is_polaris = 1,
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300180 /*
181 * In fact, the EVK is shipped without
182 * remotes, but we should have something handy,
183 * to allow testing it
184 */
185 .rc_map = RC_MAP_RC5_HAUPPAUGE_NEW,
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300186 .name = "cx231xx MCE IR",
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300187 },
188};
189
Jarod Wilson66e89522010-06-01 17:32:08 -0300190static struct usb_device_id mceusb_dev_table[] = {
191 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300192 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
193 .driver_info = MCE_GEN1 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300194 /* Philips Infrared Transceiver - Sahara branded */
195 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
196 /* Philips Infrared Transceiver - HP branded */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300197 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
198 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300199 /* Philips SRM5100 */
200 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
201 /* Philips Infrared Transceiver - Omaura */
202 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
203 /* Philips Infrared Transceiver - Spinel plus */
204 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
205 /* Philips eHome Infrared Transceiver */
206 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300207 /* Philips/Spinel plus IR transceiver for ASUS */
208 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
209 /* Philips/Spinel plus IR transceiver for ASUS */
210 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300211 /* Realtek MCE IR Receiver */
212 { USB_DEVICE(VENDOR_REALTEK, 0x0161) },
213 /* SMK/Toshiba G83C0004D410 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300214 { USB_DEVICE(VENDOR_SMK, 0x031d),
215 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300216 /* SMK eHome Infrared Transceiver (Sony VAIO) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300217 { USB_DEVICE(VENDOR_SMK, 0x0322),
218 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300219 /* bundled with Hauppauge PVR-150 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300220 { USB_DEVICE(VENDOR_SMK, 0x0334),
221 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300222 /* SMK eHome Infrared Transceiver */
223 { USB_DEVICE(VENDOR_SMK, 0x0338) },
224 /* Tatung eHome Infrared Transceiver */
225 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
226 /* Shuttle eHome Infrared Transceiver */
227 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
228 /* Shuttle eHome Infrared Transceiver */
229 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
230 /* Gateway eHome Infrared Transceiver */
231 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
232 /* Mitsumi */
233 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
234 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300235 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
236 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300237 /* Topseed HP eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300238 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
239 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300240 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300241 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
242 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300243 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300244 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
245 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300246 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300247 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
248 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300249 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300250 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
251 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300252 /* Ricavision internal Infrared Transceiver */
253 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
254 /* Itron ione Libra Q-11 */
255 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
256 /* FIC eHome Infrared Transceiver */
257 { USB_DEVICE(VENDOR_FIC, 0x9242) },
258 /* LG eHome Infrared Transceiver */
259 { USB_DEVICE(VENDOR_LG, 0x9803) },
260 /* Microsoft MCE Infrared Transceiver */
261 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
262 /* Formosa eHome Infrared Transceiver */
263 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
264 /* Formosa21 / eHome Infrared Receiver */
265 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
266 /* Formosa aim / Trust MCE Infrared Receiver */
267 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
268 /* Formosa Industrial Computing / Beanbag Emulation Device */
269 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
270 /* Formosa21 / eHome Infrared Receiver */
271 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
272 /* Formosa Industrial Computing AIM IR605/A */
273 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
274 /* Formosa Industrial Computing */
275 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
276 /* Fintek eHome Infrared Transceiver */
277 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
278 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
279 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
280 /* Pinnacle Remote Kit */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300281 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
282 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300283 /* Elitegroup Computer Systems IR */
284 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
285 /* Wistron Corp. eHome Infrared Receiver */
286 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
287 /* Compro K100 */
288 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
289 /* Compro K100 v2 */
290 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
291 /* Northstar Systems, Inc. eHome Infrared Transceiver */
292 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
293 /* TiVo PC IR Receiver */
294 { USB_DEVICE(VENDOR_TIVO, 0x2000) },
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300295 /* Conexant SDK */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300296 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
297 .driver_info = POLARIS_EVK },
Jarod Wilson66e89522010-06-01 17:32:08 -0300298 /* Terminating entry */
299 { }
300};
301
Jarod Wilson66e89522010-06-01 17:32:08 -0300302/* data structure for each usb transceiver */
303struct mceusb_dev {
304 /* ir-core bits */
Jarod Wilson66e89522010-06-01 17:32:08 -0300305 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300306 struct ir_raw_event rawir;
307
308 /* core device bits */
309 struct device *dev;
310 struct input_dev *idev;
311
312 /* usb */
313 struct usb_device *usbdev;
314 struct urb *urb_in;
315 struct usb_endpoint_descriptor *usb_ep_in;
316 struct usb_endpoint_descriptor *usb_ep_out;
317
318 /* buffers and dma */
319 unsigned char *buf_in;
320 unsigned int len_in;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300321
322 enum {
323 CMD_HEADER = 0,
324 SUBCMD,
325 CMD_DATA,
326 PARSE_IRDATA,
327 } parser_state;
328 u8 cmd, rem; /* Remaining IR data bytes in packet */
329
Jarod Wilson66e89522010-06-01 17:32:08 -0300330 dma_addr_t dma_in;
331 dma_addr_t dma_out;
332
333 struct {
334 u32 connected:1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300335 u32 tx_mask_inverted:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300336 u32 microsoft_gen1:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300337 } flags;
338
Jarod Wilsone23fb962010-06-16 17:55:52 -0300339 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300340 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300341 u32 carrier;
342 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300343
344 char name[128];
345 char phys[64];
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300346 enum mceusb_model_type model;
Jarod Wilson66e89522010-06-01 17:32:08 -0300347};
348
349/*
350 * MCE Device Command Strings
351 * Device command responses vary from device to device...
352 * - DEVICE_RESET resets the hardware to its default state
353 * - GET_REVISION fetches the hardware/software revision, common
354 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
355 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
356 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
357 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
358 * ((clk / frequency) - 1)
359 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
360 * response in the form of 9f 0c msb lsb
361 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
362 * the form of 9f 08 bm, where bm is the bitmask
363 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
364 * general use one or short-range learning one, in the form of
365 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
366 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
367 * - SET_TX_BITMASK sets the transmitter bitmask
368 * - SET_RX_TIMEOUT sets the receiver timeout
369 * - SET_RX_SENSOR sets which receiver sensor to use
370 */
Jarod Wilson4a883912010-10-22 14:42:54 -0300371static char DEVICE_RESET[] = {MCE_COMMAND_NULL, MCE_HW_CMD_HEADER,
372 MCE_CMD_DEVICE_RESET};
373static char GET_REVISION[] = {MCE_HW_CMD_HEADER, MCE_CMD_G_REVISION};
374static char GET_UNKNOWN[] = {MCE_HW_CMD_HEADER, MCE_CMD_UNKNOWN7};
375static char GET_UNKNOWN2[] = {MCE_COMMAND_HEADER, MCE_CMD_UNKNOWN2};
376static char GET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER, MCE_CMD_G_CARRIER};
377static char GET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TIMEOUT};
378static char GET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TXMASK};
379static char GET_RX_SENSOR[] = {MCE_COMMAND_HEADER, MCE_CMD_G_RXSENSOR};
Jarod Wilson66e89522010-06-01 17:32:08 -0300380/* sub in desired values in lower byte or bytes for full command */
381/* FIXME: make use of these for transmit.
Jarod Wilson4a883912010-10-22 14:42:54 -0300382static char SET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER,
383 MCE_CMD_S_CARRIER, 0x00, 0x00};
384static char SET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_S_TXMASK, 0x00};
385static char SET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER,
386 MCE_CMD_S_TIMEOUT, 0x00, 0x00};
387static char SET_RX_SENSOR[] = {MCE_COMMAND_HEADER,
388 MCE_CMD_S_RXSENSOR, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300389*/
390
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300391static int mceusb_cmdsize(u8 cmd, u8 subcmd)
392{
393 int datasize = 0;
394
395 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300396 case MCE_COMMAND_NULL:
397 if (subcmd == MCE_HW_CMD_HEADER)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300398 datasize = 1;
399 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300400 case MCE_HW_CMD_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300401 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300402 case MCE_CMD_G_REVISION:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300403 datasize = 2;
404 break;
405 }
Jarod Wilson4a883912010-10-22 14:42:54 -0300406 case MCE_COMMAND_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300407 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300408 case MCE_CMD_UNKNOWN:
409 case MCE_CMD_S_CARRIER:
410 case MCE_CMD_S_TIMEOUT:
411 case MCE_CMD_G_RXSENSOR:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300412 datasize = 2;
413 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300414 case MCE_CMD_S_TXMASK:
415 case MCE_CMD_S_RXSENSOR:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300416 datasize = 1;
417 break;
418 }
419 }
420 return datasize;
421}
422
Jarod Wilson66e89522010-06-01 17:32:08 -0300423static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300424 int offset, int len, bool out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300425{
426 char codes[USB_BUFLEN * 3 + 1];
427 char inout[9];
Jarod Wilson66e89522010-06-01 17:32:08 -0300428 u8 cmd, subcmd, data1, data2;
429 struct device *dev = ir->dev;
Jarod Wilson36e9d262010-10-22 16:49:35 -0300430 int i, start, skip = 0;
431
432 if (!debug)
433 return;
Jarod Wilson66e89522010-06-01 17:32:08 -0300434
Jarod Wilson657290b2010-06-16 17:10:05 -0300435 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
436 if (ir->flags.microsoft_gen1 && !out)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300437 skip = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300438
Jarod Wilson36e9d262010-10-22 16:49:35 -0300439 if (len <= skip)
Jarod Wilson66e89522010-06-01 17:32:08 -0300440 return;
441
442 for (i = 0; i < len && i < USB_BUFLEN; i++)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300443 snprintf(codes + i * 3, 4, "%02x ", buf[i + offset] & 0xff);
Jarod Wilson66e89522010-06-01 17:32:08 -0300444
Jarod Wilson36e9d262010-10-22 16:49:35 -0300445 dev_info(dev, "%sx data: %s(length=%d)\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300446 (out ? "t" : "r"), codes, len);
447
448 if (out)
449 strcpy(inout, "Request\0");
450 else
451 strcpy(inout, "Got\0");
452
Jarod Wilson36e9d262010-10-22 16:49:35 -0300453 start = offset + skip;
454 cmd = buf[start] & 0xff;
455 subcmd = buf[start + 1] & 0xff;
456 data1 = buf[start + 2] & 0xff;
457 data2 = buf[start + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300458
459 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300460 case MCE_COMMAND_NULL:
461 if ((subcmd == MCE_HW_CMD_HEADER) &&
462 (data1 == MCE_CMD_DEVICE_RESET))
Jarod Wilson66e89522010-06-01 17:32:08 -0300463 dev_info(dev, "Device reset requested\n");
464 else
465 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
466 cmd, subcmd);
467 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300468 case MCE_HW_CMD_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300469 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300470 case MCE_CMD_G_REVISION:
Jarod Wilson66e89522010-06-01 17:32:08 -0300471 if (len == 2)
472 dev_info(dev, "Get hw/sw rev?\n");
473 else
474 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
475 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300476 buf[start + 4], buf[start + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300477 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300478 case MCE_CMD_DEVICE_RESET:
Jarod Wilson66e89522010-06-01 17:32:08 -0300479 dev_info(dev, "Device reset requested\n");
480 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300481 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300482 dev_info(dev, "Previous command not supported\n");
483 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300484 case MCE_CMD_UNKNOWN7:
485 case MCE_CMD_UNKNOWN9:
Jarod Wilson66e89522010-06-01 17:32:08 -0300486 default:
487 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
488 cmd, subcmd);
489 break;
490 }
491 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300492 case MCE_COMMAND_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300493 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300494 case MCE_CMD_PING:
Jarod Wilson66e89522010-06-01 17:32:08 -0300495 dev_info(dev, "Ping\n");
496 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300497 case MCE_CMD_UNKNOWN:
Jarod Wilson66e89522010-06-01 17:32:08 -0300498 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
499 data1, data2);
500 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300501 case MCE_CMD_S_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300502 dev_info(dev, "%s carrier mode and freq of "
503 "0x%02x 0x%02x\n", inout, data1, data2);
504 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300505 case MCE_CMD_G_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300506 dev_info(dev, "Get carrier mode and freq\n");
507 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300508 case MCE_CMD_S_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300509 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
510 inout, data1);
511 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300512 case MCE_CMD_S_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300513 /* value is in units of 50us, so x*50/100 or x/2 ms */
514 dev_info(dev, "%s receive timeout of %d ms\n",
515 inout, ((data1 << 8) | data2) / 2);
516 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300517 case MCE_CMD_G_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300518 dev_info(dev, "Get receive timeout\n");
519 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300520 case MCE_CMD_G_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300521 dev_info(dev, "Get transmit blaster mask\n");
522 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300523 case MCE_CMD_S_RXSENSOR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300524 dev_info(dev, "%s %s-range receive sensor in use\n",
525 inout, data1 == 0x02 ? "short" : "long");
526 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300527 case MCE_CMD_G_RXSENSOR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300528 if (len == 2)
529 dev_info(dev, "Get receive sensor\n");
530 else
531 dev_info(dev, "Received pulse count is %d\n",
532 ((data1 << 8) | data2));
533 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300534 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300535 dev_info(dev, "Error! Hardware is likely wedged...\n");
536 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300537 case MCE_CMD_UNKNOWN2:
538 case MCE_CMD_UNKNOWN3:
539 case MCE_CMD_UNKNOWN5:
Jarod Wilson66e89522010-06-01 17:32:08 -0300540 default:
541 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
542 cmd, subcmd);
543 break;
544 }
545 break;
546 default:
547 break;
548 }
Jarod Wilson36e9d262010-10-22 16:49:35 -0300549
550 if (cmd == MCE_IRDATA_TRAILER)
551 dev_info(dev, "End of raw IR data\n");
552 else if ((cmd != MCE_COMMAND_HEADER) &&
553 ((cmd & MCE_COMMAND_MASK) == MCE_COMMAND_IRDATA))
554 dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem);
Jarod Wilson66e89522010-06-01 17:32:08 -0300555}
556
Jarod Wilson7c294402010-08-02 18:21:06 -0300557static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300558{
559 struct mceusb_dev *ir;
560 int len;
561
562 if (!urb)
563 return;
564
565 ir = urb->context;
566 if (ir) {
567 len = urb->actual_length;
568
569 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
570 urb->status, len);
571
Jarod Wilson36e9d262010-10-22 16:49:35 -0300572 mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
Jarod Wilson66e89522010-06-01 17:32:08 -0300573 }
574
575}
576
577/* request incoming or send outgoing usb packet - used to initialize remote */
578static void mce_request_packet(struct mceusb_dev *ir,
579 struct usb_endpoint_descriptor *ep,
580 unsigned char *data, int size, int urb_type)
581{
582 int res;
583 struct urb *async_urb;
584 struct device *dev = ir->dev;
585 unsigned char *async_buf;
586
587 if (urb_type == MCEUSB_TX) {
588 async_urb = usb_alloc_urb(0, GFP_KERNEL);
589 if (unlikely(!async_urb)) {
590 dev_err(dev, "Error, couldn't allocate urb!\n");
591 return;
592 }
593
594 async_buf = kzalloc(size, GFP_KERNEL);
595 if (!async_buf) {
596 dev_err(dev, "Error, couldn't allocate buf!\n");
597 usb_free_urb(async_urb);
598 return;
599 }
600
601 /* outbound data */
602 usb_fill_int_urb(async_urb, ir->usbdev,
603 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300604 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300605 ir, ep->bInterval);
606 memcpy(async_buf, data, size);
607
608 } else if (urb_type == MCEUSB_RX) {
609 /* standard request */
610 async_urb = ir->urb_in;
611 ir->send_flags = RECV_FLAG_IN_PROGRESS;
612
613 } else {
614 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
615 return;
616 }
617
618 dev_dbg(dev, "receive request called (size=%#x)\n", size);
619
620 async_urb->transfer_buffer_length = size;
621 async_urb->dev = ir->usbdev;
622
623 res = usb_submit_urb(async_urb, GFP_ATOMIC);
624 if (res) {
625 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
626 return;
627 }
628 dev_dbg(dev, "receive request complete (res=%d)\n", res);
629}
630
631static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
632{
633 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
634}
635
636static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
637{
638 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
639}
640
Jarod Wilsone23fb962010-06-16 17:55:52 -0300641/* Send data out the IR blaster port(s) */
642static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
643{
644 struct mceusb_dev *ir = priv;
645 int i, ret = 0;
646 int count, cmdcount = 0;
647 unsigned char *cmdbuf; /* MCE command buffer */
648 long signal_duration = 0; /* Singnal length in us */
649 struct timeval start_time, end_time;
650
651 do_gettimeofday(&start_time);
652
653 count = n / sizeof(int);
654
655 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
656 if (!cmdbuf)
657 return -ENOMEM;
658
659 /* MCE tx init header */
Jarod Wilson4a883912010-10-22 14:42:54 -0300660 cmdbuf[cmdcount++] = MCE_COMMAND_HEADER;
661 cmdbuf[cmdcount++] = MCE_CMD_S_TXMASK;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300662 cmdbuf[cmdcount++] = ir->tx_mask;
663
664 /* Generate mce packet data */
665 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
666 signal_duration += txbuf[i];
667 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
668
669 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
670
671 /* Insert mce packet header every 4th entry */
672 if ((cmdcount < MCE_CMDBUF_SIZE) &&
673 (cmdcount - MCE_TX_HEADER_LENGTH) %
674 MCE_CODE_LENGTH == 0)
Jarod Wilson4a883912010-10-22 14:42:54 -0300675 cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300676
677 /* Insert mce packet data */
678 if (cmdcount < MCE_CMDBUF_SIZE)
679 cmdbuf[cmdcount++] =
680 (txbuf[i] < MCE_PULSE_BIT ?
681 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
682 (i & 1 ? 0x00 : MCE_PULSE_BIT);
683 else {
684 ret = -EINVAL;
685 goto out;
686 }
687
688 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
689 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
690 }
691
692 /* Fix packet length in last header */
693 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
Jarod Wilson4a883912010-10-22 14:42:54 -0300694 MCE_COMMAND_IRDATA + (cmdcount - MCE_TX_HEADER_LENGTH) %
695 MCE_CODE_LENGTH - 1;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300696
697 /* Check if we have room for the empty packet at the end */
698 if (cmdcount >= MCE_CMDBUF_SIZE) {
699 ret = -EINVAL;
700 goto out;
701 }
702
703 /* All mce commands end with an empty packet (0x80) */
Jarod Wilson4a883912010-10-22 14:42:54 -0300704 cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300705
706 /* Transmit the command to the mce device */
707 mce_async_out(ir, cmdbuf, cmdcount);
708
709 /*
710 * The lircd gap calculation expects the write function to
711 * wait the time it takes for the ircommand to be sent before
712 * it returns.
713 */
714 do_gettimeofday(&end_time);
715 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
716 (end_time.tv_sec - start_time.tv_sec) * 1000000;
717
718 /* delay with the closest number of ticks */
719 set_current_state(TASK_INTERRUPTIBLE);
720 schedule_timeout(usecs_to_jiffies(signal_duration));
721
722out:
723 kfree(cmdbuf);
724 return ret ? ret : n;
725}
726
Jarod Wilson657290b2010-06-16 17:10:05 -0300727/* Sets active IR outputs -- mce devices typically (all?) have two */
728static int mceusb_set_tx_mask(void *priv, u32 mask)
729{
730 struct mceusb_dev *ir = priv;
731
732 if (ir->flags.tx_mask_inverted)
Jarod Wilson4a883912010-10-22 14:42:54 -0300733 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
734 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300735 else
736 ir->tx_mask = mask;
737
738 return 0;
739}
740
Jarod Wilsone23fb962010-06-16 17:55:52 -0300741/* Sets the send carrier frequency and mode */
742static int mceusb_set_tx_carrier(void *priv, u32 carrier)
743{
744 struct mceusb_dev *ir = priv;
745 int clk = 10000000;
746 int prescaler = 0, divisor = 0;
Jarod Wilson4a883912010-10-22 14:42:54 -0300747 unsigned char cmdbuf[4] = { MCE_COMMAND_HEADER,
748 MCE_CMD_S_CARRIER, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300749
750 /* Carrier has changed */
751 if (ir->carrier != carrier) {
752
753 if (carrier == 0) {
754 ir->carrier = carrier;
755 cmdbuf[2] = 0x01;
Jarod Wilson4a883912010-10-22 14:42:54 -0300756 cmdbuf[3] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300757 dev_dbg(ir->dev, "%s: disabling carrier "
758 "modulation\n", __func__);
759 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
760 return carrier;
761 }
762
763 for (prescaler = 0; prescaler < 4; ++prescaler) {
764 divisor = (clk >> (2 * prescaler)) / carrier;
Jarod Wilson4a883912010-10-22 14:42:54 -0300765 if (divisor <= 0xff) {
Jarod Wilsone23fb962010-06-16 17:55:52 -0300766 ir->carrier = carrier;
767 cmdbuf[2] = prescaler;
768 cmdbuf[3] = divisor;
769 dev_dbg(ir->dev, "%s: requesting %u HZ "
770 "carrier\n", __func__, carrier);
771
772 /* Transmit new carrier to mce device */
773 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
774 return carrier;
775 }
776 }
777
778 return -EINVAL;
779
780 }
781
782 return carrier;
783}
784
Jarod Wilson66e89522010-06-01 17:32:08 -0300785static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
786{
Maxim Levitsky46519182010-10-16 19:56:28 -0300787 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300788 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300789
790 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
791 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300792 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300793
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300794 for (; i < buf_len; i++) {
795 switch (ir->parser_state) {
796 case SUBCMD:
797 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
Jarod Wilson36e9d262010-10-22 16:49:35 -0300798 mceusb_dev_printdata(ir, ir->buf_in, i - 1,
799 ir->rem + 2, false);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300800 ir->parser_state = CMD_DATA;
801 break;
802 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300803 ir->rem--;
Jarod Wilson66e89522010-06-01 17:32:08 -0300804 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
805 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
806 * MCE_TIME_UNIT * 1000;
807
808 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300809 if (ir->rawir.pulse == rawir.pulse) {
Jarod Wilson66e89522010-06-01 17:32:08 -0300810 ir->rawir.duration += rawir.duration;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300811 } else {
Jarod Wilson66e89522010-06-01 17:32:08 -0300812 ir->rawir.duration = rawir.duration;
813 ir->rawir.pulse = rawir.pulse;
814 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300815 if (ir->rem)
816 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300817 }
818 rawir.duration += ir->rawir.duration;
819 ir->rawir.duration = 0;
820 ir->rawir.pulse = rawir.pulse;
821
822 dev_dbg(ir->dev, "Storing %s with duration %d\n",
823 rawir.pulse ? "pulse" : "space",
824 rawir.duration);
825
826 ir_raw_event_store(ir->idev, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300827 break;
828 case CMD_DATA:
829 ir->rem--;
830 break;
831 case CMD_HEADER:
832 /* decode mce packets of the form (84),AA,BB,CC,DD */
833 /* IR data packets can span USB messages - rem */
834 ir->cmd = ir->buf_in[i];
Jarod Wilson4a883912010-10-22 14:42:54 -0300835 if ((ir->cmd == MCE_COMMAND_HEADER) ||
836 ((ir->cmd & MCE_COMMAND_MASK) !=
837 MCE_COMMAND_IRDATA)) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300838 ir->parser_state = SUBCMD;
839 continue;
840 }
841 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
Jarod Wilson36e9d262010-10-22 16:49:35 -0300842 mceusb_dev_printdata(ir, ir->buf_in, i, ir->rem + 1, false);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300843 if (ir->rem) {
844 ir->parser_state = PARSE_IRDATA;
845 break;
846 }
847 /*
848 * a package with len=0 (e. g. 0x80) means end of
849 * data. We could use it to do the call to
850 * ir_raw_event_handle(). For now, we don't need to
851 * use it.
852 */
853 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300854 }
855
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300856 if (ir->parser_state != CMD_HEADER && !ir->rem)
857 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300858 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300859 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
860 ir_raw_event_handle(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -0300861}
862
Jarod Wilson66e89522010-06-01 17:32:08 -0300863static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
864{
865 struct mceusb_dev *ir;
866 int buf_len;
867
868 if (!urb)
869 return;
870
871 ir = urb->context;
872 if (!ir) {
873 usb_unlink_urb(urb);
874 return;
875 }
876
877 buf_len = urb->actual_length;
878
Jarod Wilson66e89522010-06-01 17:32:08 -0300879 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
880 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300881 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300882 buf_len);
883 }
884
885 switch (urb->status) {
886 /* success */
887 case 0:
888 mceusb_process_ir_data(ir, buf_len);
889 break;
890
891 case -ECONNRESET:
892 case -ENOENT:
893 case -ESHUTDOWN:
894 usb_unlink_urb(urb);
895 return;
896
897 case -EPIPE:
898 default:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300899 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300900 break;
901 }
902
903 usb_submit_urb(urb, GFP_ATOMIC);
904}
905
906static void mceusb_gen1_init(struct mceusb_dev *ir)
907{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300908 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300909 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300910 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300911 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300912
913 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
914 if (!data) {
915 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300916 return;
917 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300918
919 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300920 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300921 * on the receive control pipe and expect a certain value pair back
922 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300923 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
924 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300925 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300926 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
927 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
928 __func__, data[0], data[1]);
929
930 /* set feature: bit rate 38400 bps */
931 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
932 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
933 0xc04e, 0x0000, NULL, 0, HZ * 3);
934
935 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
936
937 /* bRequest 4: set char length to 8 bits */
938 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
939 4, USB_TYPE_VENDOR,
940 0x0808, 0x0000, NULL, 0, HZ * 3);
941 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
942
943 /* bRequest 2: set handshaking to use DTR/DSR */
944 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
945 2, USB_TYPE_VENDOR,
946 0x0000, 0x0100, NULL, 0, HZ * 3);
947 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300948
Jarod Wilson22b07662010-07-08 12:38:57 -0300949 /* device reset */
950 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
951 mce_sync_in(ir, NULL, maxp);
952
953 /* get hw/sw revision? */
954 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
955 mce_sync_in(ir, NULL, maxp);
956
Jarod Wilson657290b2010-06-16 17:10:05 -0300957 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -0300958};
959
960static void mceusb_gen2_init(struct mceusb_dev *ir)
961{
962 int maxp = ir->len_in;
963
Jarod Wilson66e89522010-06-01 17:32:08 -0300964 /* device reset */
965 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
966 mce_sync_in(ir, NULL, maxp);
967
968 /* get hw/sw revision? */
969 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
970 mce_sync_in(ir, NULL, maxp);
971
Jarod Wilson22b07662010-07-08 12:38:57 -0300972 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -0300973 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
974 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -0300975 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
976 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -0300977}
978
Jarod Wilson22b07662010-07-08 12:38:57 -0300979static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -0300980{
981 int maxp = ir->len_in;
982
Jarod Wilson66e89522010-06-01 17:32:08 -0300983 /* get the carrier and frequency */
984 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
985 mce_sync_in(ir, NULL, maxp);
986
987 /* get the transmitter bitmask */
988 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
989 mce_sync_in(ir, NULL, maxp);
990
991 /* get receiver timeout value */
992 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
993 mce_sync_in(ir, NULL, maxp);
994
995 /* get receiver sensor setting */
996 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
997 mce_sync_in(ir, NULL, maxp);
998}
999
1000static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
1001{
1002 struct input_dev *idev;
1003 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -03001004 struct device *dev = ir->dev;
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -03001005 const char *rc_map = RC_MAP_RC6_MCE;
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001006 const char *name = "Media Center Ed. eHome Infrared Remote Transceiver";
Jarod Wilson66e89522010-06-01 17:32:08 -03001007 int ret = -ENODEV;
1008
1009 idev = input_allocate_device();
1010 if (!idev) {
1011 dev_err(dev, "remote input dev allocation failed\n");
1012 goto idev_alloc_failed;
1013 }
1014
1015 ret = -ENOMEM;
1016 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
1017 if (!props) {
1018 dev_err(dev, "remote ir dev props allocation failed\n");
1019 goto props_alloc_failed;
1020 }
1021
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001022 if (mceusb_model[ir->model].name)
1023 name = mceusb_model[ir->model].name;
1024
1025 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1026 name,
Jarod Wilson66e89522010-06-01 17:32:08 -03001027 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1028 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1029
Jarod Wilson66e89522010-06-01 17:32:08 -03001030 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -03001031 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
1032 strlcat(ir->phys, "/input0", sizeof(ir->phys));
1033 idev->phys = ir->phys;
1034
Jarod Wilson66e89522010-06-01 17:32:08 -03001035 props->priv = ir;
1036 props->driver_type = RC_DRIVER_IR_RAW;
1037 props->allowed_protos = IR_TYPE_ALL;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001038 props->s_tx_mask = mceusb_set_tx_mask;
1039 props->s_tx_carrier = mceusb_set_tx_carrier;
1040 props->tx_ir = mceusb_tx_ir;
Jarod Wilson66e89522010-06-01 17:32:08 -03001041
1042 ir->props = props;
Jarod Wilson66e89522010-06-01 17:32:08 -03001043
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -03001044 if (mceusb_model[ir->model].rc_map)
1045 rc_map = mceusb_model[ir->model].rc_map;
1046
1047 ret = ir_input_register(idev, rc_map, props, DRIVER_NAME);
Jarod Wilson66e89522010-06-01 17:32:08 -03001048 if (ret < 0) {
1049 dev_err(dev, "remote input device register failed\n");
1050 goto irdev_failed;
1051 }
1052
1053 return idev;
1054
1055irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -03001056 kfree(props);
1057props_alloc_failed:
1058 input_free_device(idev);
1059idev_alloc_failed:
1060 return NULL;
1061}
1062
1063static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1064 const struct usb_device_id *id)
1065{
1066 struct usb_device *dev = interface_to_usbdev(intf);
1067 struct usb_host_interface *idesc;
1068 struct usb_endpoint_descriptor *ep = NULL;
1069 struct usb_endpoint_descriptor *ep_in = NULL;
1070 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001071 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -03001072 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -03001073 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001074 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001075 bool is_gen3;
1076 bool is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001077 bool tx_mask_inverted;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001078 bool is_polaris;
Jarod Wilson66e89522010-06-01 17:32:08 -03001079
1080 dev_dbg(&intf->dev, ": %s called\n", __func__);
1081
Jarod Wilson66e89522010-06-01 17:32:08 -03001082 idesc = intf->cur_altsetting;
1083
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001084 is_gen3 = mceusb_model[model].mce_gen3;
1085 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1086 tx_mask_inverted = mceusb_model[model].tx_mask_inverted;
1087 is_polaris = mceusb_model[model].is_polaris;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001088
1089 if (is_polaris) {
1090 /* Interface 0 is IR */
1091 if (idesc->desc.bInterfaceNumber)
1092 return -ENODEV;
1093 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001094
1095 /* step through the endpoints to find first bulk in and out endpoint */
1096 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1097 ep = &idesc->endpoint[i].desc;
1098
1099 if ((ep_in == NULL)
1100 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1101 == USB_DIR_IN)
1102 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1103 == USB_ENDPOINT_XFER_BULK)
1104 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1105 == USB_ENDPOINT_XFER_INT))) {
1106
Jarod Wilson66e89522010-06-01 17:32:08 -03001107 ep_in = ep;
1108 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001109 ep_in->bInterval = 1;
1110 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
1111 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001112 }
1113
1114 if ((ep_out == NULL)
1115 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1116 == USB_DIR_OUT)
1117 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1118 == USB_ENDPOINT_XFER_BULK)
1119 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1120 == USB_ENDPOINT_XFER_INT))) {
1121
Jarod Wilson66e89522010-06-01 17:32:08 -03001122 ep_out = ep;
1123 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001124 ep_out->bInterval = 1;
1125 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
1126 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001127 }
1128 }
1129 if (ep_in == NULL) {
1130 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
1131 return -ENODEV;
1132 }
1133
1134 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1135 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1136
1137 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1138 if (!ir)
1139 goto mem_alloc_fail;
1140
1141 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1142 if (!ir->buf_in)
1143 goto buf_in_alloc_fail;
1144
1145 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1146 if (!ir->urb_in)
1147 goto urb_in_alloc_fail;
1148
1149 ir->usbdev = dev;
1150 ir->dev = &intf->dev;
1151 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001152 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001153 ir->flags.tx_mask_inverted = tx_mask_inverted;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001154 ir->model = model;
1155
Maxim Levitsky46519182010-10-16 19:56:28 -03001156 init_ir_raw_event(&ir->rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001157
1158 /* Saving usb interface data for use by the transmitter routine */
1159 ir->usb_ep_in = ep_in;
1160 ir->usb_ep_out = ep_out;
1161
1162 if (dev->descriptor.iManufacturer
1163 && usb_string(dev, dev->descriptor.iManufacturer,
1164 buf, sizeof(buf)) > 0)
1165 strlcpy(name, buf, sizeof(name));
1166 if (dev->descriptor.iProduct
1167 && usb_string(dev, dev->descriptor.iProduct,
1168 buf, sizeof(buf)) > 0)
1169 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1170 " %s", buf);
1171
1172 ir->idev = mceusb_init_input_dev(ir);
1173 if (!ir->idev)
1174 goto input_dev_fail;
1175
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001176 /* flush buffers on the device */
1177 mce_sync_in(ir, NULL, maxp);
1178 mce_sync_in(ir, NULL, maxp);
1179
1180 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001181 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1182 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1183 ir->urb_in->transfer_dma = ir->dma_in;
1184 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1185
Jarod Wilson66e89522010-06-01 17:32:08 -03001186 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001187 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001188 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001189 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001190 mceusb_gen2_init(ir);
1191
Jarod Wilson22b07662010-07-08 12:38:57 -03001192 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001193
Jarod Wilson657290b2010-06-16 17:10:05 -03001194 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001195
1196 usb_set_intfdata(intf, ir);
1197
1198 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1199 dev->bus->busnum, dev->devnum);
1200
1201 return 0;
1202
1203 /* Error-handling path */
1204input_dev_fail:
1205 usb_free_urb(ir->urb_in);
1206urb_in_alloc_fail:
1207 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1208buf_in_alloc_fail:
1209 kfree(ir);
1210mem_alloc_fail:
1211 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1212
1213 return -ENOMEM;
1214}
1215
1216
1217static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1218{
1219 struct usb_device *dev = interface_to_usbdev(intf);
1220 struct mceusb_dev *ir = usb_get_intfdata(intf);
1221
1222 usb_set_intfdata(intf, NULL);
1223
1224 if (!ir)
1225 return;
1226
1227 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001228 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001229 usb_kill_urb(ir->urb_in);
1230 usb_free_urb(ir->urb_in);
1231 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1232
1233 kfree(ir);
1234}
1235
1236static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1237{
1238 struct mceusb_dev *ir = usb_get_intfdata(intf);
1239 dev_info(ir->dev, "suspend\n");
1240 usb_kill_urb(ir->urb_in);
1241 return 0;
1242}
1243
1244static int mceusb_dev_resume(struct usb_interface *intf)
1245{
1246 struct mceusb_dev *ir = usb_get_intfdata(intf);
1247 dev_info(ir->dev, "resume\n");
1248 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1249 return -EIO;
1250 return 0;
1251}
1252
1253static struct usb_driver mceusb_dev_driver = {
1254 .name = DRIVER_NAME,
1255 .probe = mceusb_dev_probe,
1256 .disconnect = mceusb_dev_disconnect,
1257 .suspend = mceusb_dev_suspend,
1258 .resume = mceusb_dev_resume,
1259 .reset_resume = mceusb_dev_resume,
1260 .id_table = mceusb_dev_table
1261};
1262
1263static int __init mceusb_dev_init(void)
1264{
1265 int ret;
1266
1267 ret = usb_register(&mceusb_dev_driver);
1268 if (ret < 0)
1269 printk(KERN_ERR DRIVER_NAME
1270 ": usb register failed, result = %d\n", ret);
1271
1272 return ret;
1273}
1274
1275static void __exit mceusb_dev_exit(void)
1276{
1277 usb_deregister(&mceusb_dev_driver);
1278}
1279
1280module_init(mceusb_dev_init);
1281module_exit(mceusb_dev_exit);
1282
1283MODULE_DESCRIPTION(DRIVER_DESC);
1284MODULE_AUTHOR(DRIVER_AUTHOR);
1285MODULE_LICENSE("GPL");
1286MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1287
1288module_param(debug, bool, S_IRUGO | S_IWUSR);
1289MODULE_PARM_DESC(debug, "Debug enabled or not");