blob: 0a2981ac742903e7a195592bddaa3a98fc731dc6 [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,
424 int len, bool out)
425{
426 char codes[USB_BUFLEN * 3 + 1];
427 char inout[9];
428 int i;
429 u8 cmd, subcmd, data1, data2;
430 struct device *dev = ir->dev;
Jarod Wilson657290b2010-06-16 17:10:05 -0300431 int idx = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300432
Jarod Wilson657290b2010-06-16 17:10:05 -0300433 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
434 if (ir->flags.microsoft_gen1 && !out)
435 idx = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300436
Jarod Wilson657290b2010-06-16 17:10:05 -0300437 if (len <= idx)
Jarod Wilson66e89522010-06-01 17:32:08 -0300438 return;
439
440 for (i = 0; i < len && i < USB_BUFLEN; i++)
Jarod Wilson4a883912010-10-22 14:42:54 -0300441 snprintf(codes + i * 3, 4, "%02x ", buf[i] & 0xff);
Jarod Wilson66e89522010-06-01 17:32:08 -0300442
443 dev_info(dev, "%sx data: %s (length=%d)\n",
444 (out ? "t" : "r"), codes, len);
445
446 if (out)
447 strcpy(inout, "Request\0");
448 else
449 strcpy(inout, "Got\0");
450
Jarod Wilson657290b2010-06-16 17:10:05 -0300451 cmd = buf[idx] & 0xff;
452 subcmd = buf[idx + 1] & 0xff;
453 data1 = buf[idx + 2] & 0xff;
454 data2 = buf[idx + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300455
456 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300457 case MCE_COMMAND_NULL:
458 if ((subcmd == MCE_HW_CMD_HEADER) &&
459 (data1 == MCE_CMD_DEVICE_RESET))
Jarod Wilson66e89522010-06-01 17:32:08 -0300460 dev_info(dev, "Device reset requested\n");
461 else
462 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
463 cmd, subcmd);
464 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300465 case MCE_HW_CMD_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300466 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300467 case MCE_CMD_G_REVISION:
Jarod Wilson66e89522010-06-01 17:32:08 -0300468 if (len == 2)
469 dev_info(dev, "Get hw/sw rev?\n");
470 else
471 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
472 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson657290b2010-06-16 17:10:05 -0300473 buf[idx + 4], buf[idx + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300474 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300475 case MCE_CMD_DEVICE_RESET:
Jarod Wilson66e89522010-06-01 17:32:08 -0300476 dev_info(dev, "Device reset requested\n");
477 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300478 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300479 dev_info(dev, "Previous command not supported\n");
480 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300481 case MCE_CMD_UNKNOWN7:
482 case MCE_CMD_UNKNOWN9:
Jarod Wilson66e89522010-06-01 17:32:08 -0300483 default:
484 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
485 cmd, subcmd);
486 break;
487 }
488 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300489 case MCE_COMMAND_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300490 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300491 case MCE_CMD_PING:
Jarod Wilson66e89522010-06-01 17:32:08 -0300492 dev_info(dev, "Ping\n");
493 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300494 case MCE_CMD_UNKNOWN:
Jarod Wilson66e89522010-06-01 17:32:08 -0300495 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
496 data1, data2);
497 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300498 case MCE_CMD_S_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300499 dev_info(dev, "%s carrier mode and freq of "
500 "0x%02x 0x%02x\n", inout, data1, data2);
501 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300502 case MCE_CMD_G_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300503 dev_info(dev, "Get carrier mode and freq\n");
504 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300505 case MCE_CMD_S_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300506 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
507 inout, data1);
508 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300509 case MCE_CMD_S_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300510 /* value is in units of 50us, so x*50/100 or x/2 ms */
511 dev_info(dev, "%s receive timeout of %d ms\n",
512 inout, ((data1 << 8) | data2) / 2);
513 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300514 case MCE_CMD_G_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300515 dev_info(dev, "Get receive timeout\n");
516 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300517 case MCE_CMD_G_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300518 dev_info(dev, "Get transmit blaster mask\n");
519 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300520 case MCE_CMD_S_RXSENSOR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300521 dev_info(dev, "%s %s-range receive sensor in use\n",
522 inout, data1 == 0x02 ? "short" : "long");
523 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300524 case MCE_CMD_G_RXSENSOR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300525 if (len == 2)
526 dev_info(dev, "Get receive sensor\n");
527 else
528 dev_info(dev, "Received pulse count is %d\n",
529 ((data1 << 8) | data2));
530 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300531 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300532 dev_info(dev, "Error! Hardware is likely wedged...\n");
533 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300534 case MCE_CMD_UNKNOWN2:
535 case MCE_CMD_UNKNOWN3:
536 case MCE_CMD_UNKNOWN5:
Jarod Wilson66e89522010-06-01 17:32:08 -0300537 default:
538 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
539 cmd, subcmd);
540 break;
541 }
542 break;
543 default:
544 break;
545 }
546}
547
Jarod Wilson7c294402010-08-02 18:21:06 -0300548static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300549{
550 struct mceusb_dev *ir;
551 int len;
552
553 if (!urb)
554 return;
555
556 ir = urb->context;
557 if (ir) {
558 len = urb->actual_length;
559
560 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
561 urb->status, len);
562
563 if (debug)
564 mceusb_dev_printdata(ir, urb->transfer_buffer,
565 len, true);
566 }
567
568}
569
570/* request incoming or send outgoing usb packet - used to initialize remote */
571static void mce_request_packet(struct mceusb_dev *ir,
572 struct usb_endpoint_descriptor *ep,
573 unsigned char *data, int size, int urb_type)
574{
575 int res;
576 struct urb *async_urb;
577 struct device *dev = ir->dev;
578 unsigned char *async_buf;
579
580 if (urb_type == MCEUSB_TX) {
581 async_urb = usb_alloc_urb(0, GFP_KERNEL);
582 if (unlikely(!async_urb)) {
583 dev_err(dev, "Error, couldn't allocate urb!\n");
584 return;
585 }
586
587 async_buf = kzalloc(size, GFP_KERNEL);
588 if (!async_buf) {
589 dev_err(dev, "Error, couldn't allocate buf!\n");
590 usb_free_urb(async_urb);
591 return;
592 }
593
594 /* outbound data */
595 usb_fill_int_urb(async_urb, ir->usbdev,
596 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300597 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300598 ir, ep->bInterval);
599 memcpy(async_buf, data, size);
600
601 } else if (urb_type == MCEUSB_RX) {
602 /* standard request */
603 async_urb = ir->urb_in;
604 ir->send_flags = RECV_FLAG_IN_PROGRESS;
605
606 } else {
607 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
608 return;
609 }
610
611 dev_dbg(dev, "receive request called (size=%#x)\n", size);
612
613 async_urb->transfer_buffer_length = size;
614 async_urb->dev = ir->usbdev;
615
616 res = usb_submit_urb(async_urb, GFP_ATOMIC);
617 if (res) {
618 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
619 return;
620 }
621 dev_dbg(dev, "receive request complete (res=%d)\n", res);
622}
623
624static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
625{
626 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
627}
628
629static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
630{
631 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
632}
633
Jarod Wilsone23fb962010-06-16 17:55:52 -0300634/* Send data out the IR blaster port(s) */
635static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
636{
637 struct mceusb_dev *ir = priv;
638 int i, ret = 0;
639 int count, cmdcount = 0;
640 unsigned char *cmdbuf; /* MCE command buffer */
641 long signal_duration = 0; /* Singnal length in us */
642 struct timeval start_time, end_time;
643
644 do_gettimeofday(&start_time);
645
646 count = n / sizeof(int);
647
648 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
649 if (!cmdbuf)
650 return -ENOMEM;
651
652 /* MCE tx init header */
Jarod Wilson4a883912010-10-22 14:42:54 -0300653 cmdbuf[cmdcount++] = MCE_COMMAND_HEADER;
654 cmdbuf[cmdcount++] = MCE_CMD_S_TXMASK;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300655 cmdbuf[cmdcount++] = ir->tx_mask;
656
657 /* Generate mce packet data */
658 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
659 signal_duration += txbuf[i];
660 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
661
662 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
663
664 /* Insert mce packet header every 4th entry */
665 if ((cmdcount < MCE_CMDBUF_SIZE) &&
666 (cmdcount - MCE_TX_HEADER_LENGTH) %
667 MCE_CODE_LENGTH == 0)
Jarod Wilson4a883912010-10-22 14:42:54 -0300668 cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300669
670 /* Insert mce packet data */
671 if (cmdcount < MCE_CMDBUF_SIZE)
672 cmdbuf[cmdcount++] =
673 (txbuf[i] < MCE_PULSE_BIT ?
674 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
675 (i & 1 ? 0x00 : MCE_PULSE_BIT);
676 else {
677 ret = -EINVAL;
678 goto out;
679 }
680
681 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
682 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
683 }
684
685 /* Fix packet length in last header */
686 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
Jarod Wilson4a883912010-10-22 14:42:54 -0300687 MCE_COMMAND_IRDATA + (cmdcount - MCE_TX_HEADER_LENGTH) %
688 MCE_CODE_LENGTH - 1;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300689
690 /* Check if we have room for the empty packet at the end */
691 if (cmdcount >= MCE_CMDBUF_SIZE) {
692 ret = -EINVAL;
693 goto out;
694 }
695
696 /* All mce commands end with an empty packet (0x80) */
Jarod Wilson4a883912010-10-22 14:42:54 -0300697 cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300698
699 /* Transmit the command to the mce device */
700 mce_async_out(ir, cmdbuf, cmdcount);
701
702 /*
703 * The lircd gap calculation expects the write function to
704 * wait the time it takes for the ircommand to be sent before
705 * it returns.
706 */
707 do_gettimeofday(&end_time);
708 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
709 (end_time.tv_sec - start_time.tv_sec) * 1000000;
710
711 /* delay with the closest number of ticks */
712 set_current_state(TASK_INTERRUPTIBLE);
713 schedule_timeout(usecs_to_jiffies(signal_duration));
714
715out:
716 kfree(cmdbuf);
717 return ret ? ret : n;
718}
719
Jarod Wilson657290b2010-06-16 17:10:05 -0300720/* Sets active IR outputs -- mce devices typically (all?) have two */
721static int mceusb_set_tx_mask(void *priv, u32 mask)
722{
723 struct mceusb_dev *ir = priv;
724
725 if (ir->flags.tx_mask_inverted)
Jarod Wilson4a883912010-10-22 14:42:54 -0300726 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
727 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300728 else
729 ir->tx_mask = mask;
730
731 return 0;
732}
733
Jarod Wilsone23fb962010-06-16 17:55:52 -0300734/* Sets the send carrier frequency and mode */
735static int mceusb_set_tx_carrier(void *priv, u32 carrier)
736{
737 struct mceusb_dev *ir = priv;
738 int clk = 10000000;
739 int prescaler = 0, divisor = 0;
Jarod Wilson4a883912010-10-22 14:42:54 -0300740 unsigned char cmdbuf[4] = { MCE_COMMAND_HEADER,
741 MCE_CMD_S_CARRIER, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300742
743 /* Carrier has changed */
744 if (ir->carrier != carrier) {
745
746 if (carrier == 0) {
747 ir->carrier = carrier;
748 cmdbuf[2] = 0x01;
Jarod Wilson4a883912010-10-22 14:42:54 -0300749 cmdbuf[3] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300750 dev_dbg(ir->dev, "%s: disabling carrier "
751 "modulation\n", __func__);
752 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
753 return carrier;
754 }
755
756 for (prescaler = 0; prescaler < 4; ++prescaler) {
757 divisor = (clk >> (2 * prescaler)) / carrier;
Jarod Wilson4a883912010-10-22 14:42:54 -0300758 if (divisor <= 0xff) {
Jarod Wilsone23fb962010-06-16 17:55:52 -0300759 ir->carrier = carrier;
760 cmdbuf[2] = prescaler;
761 cmdbuf[3] = divisor;
762 dev_dbg(ir->dev, "%s: requesting %u HZ "
763 "carrier\n", __func__, carrier);
764
765 /* Transmit new carrier to mce device */
766 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
767 return carrier;
768 }
769 }
770
771 return -EINVAL;
772
773 }
774
775 return carrier;
776}
777
Jarod Wilson66e89522010-06-01 17:32:08 -0300778static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
779{
Maxim Levitsky46519182010-10-16 19:56:28 -0300780 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300781 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300782
783 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
784 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300785 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300786
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300787 for (; i < buf_len; i++) {
788 switch (ir->parser_state) {
789 case SUBCMD:
790 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
791 ir->parser_state = CMD_DATA;
792 break;
793 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300794 ir->rem--;
Jarod Wilson66e89522010-06-01 17:32:08 -0300795 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
796 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
797 * MCE_TIME_UNIT * 1000;
798
799 if ((ir->buf_in[i] & MCE_PULSE_MASK) == 0x7f) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300800 if (ir->rawir.pulse == rawir.pulse) {
Jarod Wilson66e89522010-06-01 17:32:08 -0300801 ir->rawir.duration += rawir.duration;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300802 } else {
Jarod Wilson66e89522010-06-01 17:32:08 -0300803 ir->rawir.duration = rawir.duration;
804 ir->rawir.pulse = rawir.pulse;
805 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300806 if (ir->rem)
807 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300808 }
809 rawir.duration += ir->rawir.duration;
810 ir->rawir.duration = 0;
811 ir->rawir.pulse = rawir.pulse;
812
813 dev_dbg(ir->dev, "Storing %s with duration %d\n",
814 rawir.pulse ? "pulse" : "space",
815 rawir.duration);
816
817 ir_raw_event_store(ir->idev, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300818 break;
819 case CMD_DATA:
820 ir->rem--;
821 break;
822 case CMD_HEADER:
823 /* decode mce packets of the form (84),AA,BB,CC,DD */
824 /* IR data packets can span USB messages - rem */
825 ir->cmd = ir->buf_in[i];
Jarod Wilson4a883912010-10-22 14:42:54 -0300826 if ((ir->cmd == MCE_COMMAND_HEADER) ||
827 ((ir->cmd & MCE_COMMAND_MASK) !=
828 MCE_COMMAND_IRDATA)) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300829 ir->parser_state = SUBCMD;
830 continue;
831 }
832 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
833 dev_dbg(ir->dev, "Processing RX data: len = %d\n",
834 ir->rem);
835 if (ir->rem) {
836 ir->parser_state = PARSE_IRDATA;
837 break;
838 }
839 /*
840 * a package with len=0 (e. g. 0x80) means end of
841 * data. We could use it to do the call to
842 * ir_raw_event_handle(). For now, we don't need to
843 * use it.
844 */
845 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300846 }
847
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300848 if (ir->parser_state != CMD_HEADER && !ir->rem)
849 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300850 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300851 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
852 ir_raw_event_handle(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -0300853}
854
Jarod Wilson66e89522010-06-01 17:32:08 -0300855static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
856{
857 struct mceusb_dev *ir;
858 int buf_len;
859
860 if (!urb)
861 return;
862
863 ir = urb->context;
864 if (!ir) {
865 usb_unlink_urb(urb);
866 return;
867 }
868
869 buf_len = urb->actual_length;
870
Jarod Wilson66e89522010-06-01 17:32:08 -0300871 if (debug)
872 mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len, false);
873
874 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
875 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300876 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300877 buf_len);
878 }
879
880 switch (urb->status) {
881 /* success */
882 case 0:
883 mceusb_process_ir_data(ir, buf_len);
884 break;
885
886 case -ECONNRESET:
887 case -ENOENT:
888 case -ESHUTDOWN:
889 usb_unlink_urb(urb);
890 return;
891
892 case -EPIPE:
893 default:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300894 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300895 break;
896 }
897
898 usb_submit_urb(urb, GFP_ATOMIC);
899}
900
901static void mceusb_gen1_init(struct mceusb_dev *ir)
902{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300903 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300904 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300905 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300906 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300907
908 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
909 if (!data) {
910 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300911 return;
912 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300913
914 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300915 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300916 * on the receive control pipe and expect a certain value pair back
917 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300918 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
919 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300920 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300921 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
922 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
923 __func__, data[0], data[1]);
924
925 /* set feature: bit rate 38400 bps */
926 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
927 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
928 0xc04e, 0x0000, NULL, 0, HZ * 3);
929
930 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
931
932 /* bRequest 4: set char length to 8 bits */
933 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
934 4, USB_TYPE_VENDOR,
935 0x0808, 0x0000, NULL, 0, HZ * 3);
936 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
937
938 /* bRequest 2: set handshaking to use DTR/DSR */
939 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
940 2, USB_TYPE_VENDOR,
941 0x0000, 0x0100, NULL, 0, HZ * 3);
942 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300943
Jarod Wilson22b07662010-07-08 12:38:57 -0300944 /* device reset */
945 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
946 mce_sync_in(ir, NULL, maxp);
947
948 /* get hw/sw revision? */
949 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
950 mce_sync_in(ir, NULL, maxp);
951
Jarod Wilson657290b2010-06-16 17:10:05 -0300952 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -0300953};
954
955static void mceusb_gen2_init(struct mceusb_dev *ir)
956{
957 int maxp = ir->len_in;
958
Jarod Wilson66e89522010-06-01 17:32:08 -0300959 /* device reset */
960 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
961 mce_sync_in(ir, NULL, maxp);
962
963 /* get hw/sw revision? */
964 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
965 mce_sync_in(ir, NULL, maxp);
966
Jarod Wilson22b07662010-07-08 12:38:57 -0300967 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -0300968 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
969 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -0300970 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
971 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -0300972}
973
Jarod Wilson22b07662010-07-08 12:38:57 -0300974static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -0300975{
976 int maxp = ir->len_in;
977
Jarod Wilson66e89522010-06-01 17:32:08 -0300978 /* get the carrier and frequency */
979 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
980 mce_sync_in(ir, NULL, maxp);
981
982 /* get the transmitter bitmask */
983 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
984 mce_sync_in(ir, NULL, maxp);
985
986 /* get receiver timeout value */
987 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
988 mce_sync_in(ir, NULL, maxp);
989
990 /* get receiver sensor setting */
991 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
992 mce_sync_in(ir, NULL, maxp);
993}
994
995static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
996{
997 struct input_dev *idev;
998 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -0300999 struct device *dev = ir->dev;
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -03001000 const char *rc_map = RC_MAP_RC6_MCE;
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001001 const char *name = "Media Center Ed. eHome Infrared Remote Transceiver";
Jarod Wilson66e89522010-06-01 17:32:08 -03001002 int ret = -ENODEV;
1003
1004 idev = input_allocate_device();
1005 if (!idev) {
1006 dev_err(dev, "remote input dev allocation failed\n");
1007 goto idev_alloc_failed;
1008 }
1009
1010 ret = -ENOMEM;
1011 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
1012 if (!props) {
1013 dev_err(dev, "remote ir dev props allocation failed\n");
1014 goto props_alloc_failed;
1015 }
1016
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001017 if (mceusb_model[ir->model].name)
1018 name = mceusb_model[ir->model].name;
1019
1020 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1021 name,
Jarod Wilson66e89522010-06-01 17:32:08 -03001022 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1023 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1024
Jarod Wilson66e89522010-06-01 17:32:08 -03001025 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -03001026 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
1027 strlcat(ir->phys, "/input0", sizeof(ir->phys));
1028 idev->phys = ir->phys;
1029
Jarod Wilson66e89522010-06-01 17:32:08 -03001030 props->priv = ir;
1031 props->driver_type = RC_DRIVER_IR_RAW;
1032 props->allowed_protos = IR_TYPE_ALL;
Jarod Wilsone23fb962010-06-16 17:55:52 -03001033 props->s_tx_mask = mceusb_set_tx_mask;
1034 props->s_tx_carrier = mceusb_set_tx_carrier;
1035 props->tx_ir = mceusb_tx_ir;
Jarod Wilson66e89522010-06-01 17:32:08 -03001036
1037 ir->props = props;
Jarod Wilson66e89522010-06-01 17:32:08 -03001038
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -03001039 if (mceusb_model[ir->model].rc_map)
1040 rc_map = mceusb_model[ir->model].rc_map;
1041
1042 ret = ir_input_register(idev, rc_map, props, DRIVER_NAME);
Jarod Wilson66e89522010-06-01 17:32:08 -03001043 if (ret < 0) {
1044 dev_err(dev, "remote input device register failed\n");
1045 goto irdev_failed;
1046 }
1047
1048 return idev;
1049
1050irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -03001051 kfree(props);
1052props_alloc_failed:
1053 input_free_device(idev);
1054idev_alloc_failed:
1055 return NULL;
1056}
1057
1058static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1059 const struct usb_device_id *id)
1060{
1061 struct usb_device *dev = interface_to_usbdev(intf);
1062 struct usb_host_interface *idesc;
1063 struct usb_endpoint_descriptor *ep = NULL;
1064 struct usb_endpoint_descriptor *ep_in = NULL;
1065 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001066 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -03001067 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -03001068 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001069 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001070 bool is_gen3;
1071 bool is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001072 bool tx_mask_inverted;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001073 bool is_polaris;
Jarod Wilson66e89522010-06-01 17:32:08 -03001074
1075 dev_dbg(&intf->dev, ": %s called\n", __func__);
1076
Jarod Wilson66e89522010-06-01 17:32:08 -03001077 idesc = intf->cur_altsetting;
1078
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001079 is_gen3 = mceusb_model[model].mce_gen3;
1080 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1081 tx_mask_inverted = mceusb_model[model].tx_mask_inverted;
1082 is_polaris = mceusb_model[model].is_polaris;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001083
1084 if (is_polaris) {
1085 /* Interface 0 is IR */
1086 if (idesc->desc.bInterfaceNumber)
1087 return -ENODEV;
1088 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001089
1090 /* step through the endpoints to find first bulk in and out endpoint */
1091 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1092 ep = &idesc->endpoint[i].desc;
1093
1094 if ((ep_in == NULL)
1095 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1096 == USB_DIR_IN)
1097 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1098 == USB_ENDPOINT_XFER_BULK)
1099 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1100 == USB_ENDPOINT_XFER_INT))) {
1101
Jarod Wilson66e89522010-06-01 17:32:08 -03001102 ep_in = ep;
1103 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001104 ep_in->bInterval = 1;
1105 dev_dbg(&intf->dev, ": acceptable inbound endpoint "
1106 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001107 }
1108
1109 if ((ep_out == NULL)
1110 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1111 == USB_DIR_OUT)
1112 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1113 == USB_ENDPOINT_XFER_BULK)
1114 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1115 == USB_ENDPOINT_XFER_INT))) {
1116
Jarod Wilson66e89522010-06-01 17:32:08 -03001117 ep_out = ep;
1118 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001119 ep_out->bInterval = 1;
1120 dev_dbg(&intf->dev, ": acceptable outbound endpoint "
1121 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001122 }
1123 }
1124 if (ep_in == NULL) {
1125 dev_dbg(&intf->dev, ": inbound and/or endpoint not found\n");
1126 return -ENODEV;
1127 }
1128
1129 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1130 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1131
1132 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1133 if (!ir)
1134 goto mem_alloc_fail;
1135
1136 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1137 if (!ir->buf_in)
1138 goto buf_in_alloc_fail;
1139
1140 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1141 if (!ir->urb_in)
1142 goto urb_in_alloc_fail;
1143
1144 ir->usbdev = dev;
1145 ir->dev = &intf->dev;
1146 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001147 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilson657290b2010-06-16 17:10:05 -03001148 ir->flags.tx_mask_inverted = tx_mask_inverted;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001149 ir->model = model;
1150
Maxim Levitsky46519182010-10-16 19:56:28 -03001151 init_ir_raw_event(&ir->rawir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001152
1153 /* Saving usb interface data for use by the transmitter routine */
1154 ir->usb_ep_in = ep_in;
1155 ir->usb_ep_out = ep_out;
1156
1157 if (dev->descriptor.iManufacturer
1158 && usb_string(dev, dev->descriptor.iManufacturer,
1159 buf, sizeof(buf)) > 0)
1160 strlcpy(name, buf, sizeof(name));
1161 if (dev->descriptor.iProduct
1162 && usb_string(dev, dev->descriptor.iProduct,
1163 buf, sizeof(buf)) > 0)
1164 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1165 " %s", buf);
1166
1167 ir->idev = mceusb_init_input_dev(ir);
1168 if (!ir->idev)
1169 goto input_dev_fail;
1170
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001171 /* flush buffers on the device */
1172 mce_sync_in(ir, NULL, maxp);
1173 mce_sync_in(ir, NULL, maxp);
1174
1175 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001176 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1177 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1178 ir->urb_in->transfer_dma = ir->dma_in;
1179 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1180
Jarod Wilson66e89522010-06-01 17:32:08 -03001181 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001182 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001183 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001184 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001185 mceusb_gen2_init(ir);
1186
Jarod Wilson22b07662010-07-08 12:38:57 -03001187 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001188
Jarod Wilson657290b2010-06-16 17:10:05 -03001189 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001190
1191 usb_set_intfdata(intf, ir);
1192
1193 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1194 dev->bus->busnum, dev->devnum);
1195
1196 return 0;
1197
1198 /* Error-handling path */
1199input_dev_fail:
1200 usb_free_urb(ir->urb_in);
1201urb_in_alloc_fail:
1202 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1203buf_in_alloc_fail:
1204 kfree(ir);
1205mem_alloc_fail:
1206 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1207
1208 return -ENOMEM;
1209}
1210
1211
1212static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1213{
1214 struct usb_device *dev = interface_to_usbdev(intf);
1215 struct mceusb_dev *ir = usb_get_intfdata(intf);
1216
1217 usb_set_intfdata(intf, NULL);
1218
1219 if (!ir)
1220 return;
1221
1222 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001223 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001224 usb_kill_urb(ir->urb_in);
1225 usb_free_urb(ir->urb_in);
1226 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1227
1228 kfree(ir);
1229}
1230
1231static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1232{
1233 struct mceusb_dev *ir = usb_get_intfdata(intf);
1234 dev_info(ir->dev, "suspend\n");
1235 usb_kill_urb(ir->urb_in);
1236 return 0;
1237}
1238
1239static int mceusb_dev_resume(struct usb_interface *intf)
1240{
1241 struct mceusb_dev *ir = usb_get_intfdata(intf);
1242 dev_info(ir->dev, "resume\n");
1243 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1244 return -EIO;
1245 return 0;
1246}
1247
1248static struct usb_driver mceusb_dev_driver = {
1249 .name = DRIVER_NAME,
1250 .probe = mceusb_dev_probe,
1251 .disconnect = mceusb_dev_disconnect,
1252 .suspend = mceusb_dev_suspend,
1253 .resume = mceusb_dev_resume,
1254 .reset_resume = mceusb_dev_resume,
1255 .id_table = mceusb_dev_table
1256};
1257
1258static int __init mceusb_dev_init(void)
1259{
1260 int ret;
1261
1262 ret = usb_register(&mceusb_dev_driver);
1263 if (ret < 0)
1264 printk(KERN_ERR DRIVER_NAME
1265 ": usb register failed, result = %d\n", ret);
1266
1267 return ret;
1268}
1269
1270static void __exit mceusb_dev_exit(void)
1271{
1272 usb_deregister(&mceusb_dev_driver);
1273}
1274
1275module_init(mceusb_dev_init);
1276module_exit(mceusb_dev_exit);
1277
1278MODULE_DESCRIPTION(DRIVER_DESC);
1279MODULE_AUTHOR(DRIVER_AUTHOR);
1280MODULE_LICENSE("GPL");
1281MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1282
1283module_param(debug, bool, S_IRUGO | S_IWUSR);
1284MODULE_PARM_DESC(debug, "Debug enabled or not");