blob: 392ca24132dafd5456e905fa61c216d566703976 [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>
Jarod Wilson66e89522010-06-01 17:32:08 -030038#include <linux/input.h>
Paul Bender635f76b2010-12-16 13:23:07 -030039#include <linux/usb.h>
40#include <linux/usb/input.h>
Jarod Wilson66e89522010-06-01 17:32:08 -030041#include <media/ir-core.h>
Jarod Wilson66e89522010-06-01 17:32:08 -030042
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 Wilson2ee95db2010-11-12 19:49:04 -030052#define MS_TO_NS(msec) ((msec) * 1000)
Jarod Wilson66e89522010-06-01 17:32:08 -030053
54/* MCE constants */
Jarod Wilson4a883912010-10-22 14:42:54 -030055#define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
56#define MCE_TIME_UNIT 50 /* Approx 50us resolution */
57#define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
58#define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
59#define MCE_IRDATA_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
60#define MCE_IRDATA_TRAILER 0x80 /* End of IR data */
61#define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
62#define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
63#define MCE_DEFAULT_TX_MASK 0x03 /* Vals: TX1=0x01, TX2=0x02, ALL=0x03 */
64#define MCE_PULSE_BIT 0x80 /* Pulse bit, MSB set == PULSE else SPACE */
65#define MCE_PULSE_MASK 0x7f /* Pulse mask */
66#define MCE_MAX_PULSE_LENGTH 0x7f /* Longest transmittable pulse symbol */
67
68#define MCE_HW_CMD_HEADER 0xff /* MCE hardware command header */
69#define MCE_COMMAND_HEADER 0x9f /* MCE command header */
70#define MCE_COMMAND_MASK 0xe0 /* Mask out command bits */
71#define MCE_COMMAND_NULL 0x00 /* These show up various places... */
72/* if buf[i] & MCE_COMMAND_MASK == 0x80 and buf[i] != MCE_COMMAND_HEADER,
73 * then we're looking at a raw IR data sample */
74#define MCE_COMMAND_IRDATA 0x80
75#define MCE_PACKET_LENGTH_MASK 0x1f /* Packet length mask */
76
77/* Sub-commands, which follow MCE_COMMAND_HEADER or MCE_HW_CMD_HEADER */
Jarod Wilson1cd50f22010-11-09 18:41:03 -030078#define MCE_CMD_SIG_END 0x01 /* End of signal */
Jarod Wilson4a883912010-10-22 14:42:54 -030079#define MCE_CMD_PING 0x03 /* Ping device */
80#define MCE_CMD_UNKNOWN 0x04 /* Unknown */
81#define MCE_CMD_UNKNOWN2 0x05 /* Unknown */
82#define MCE_CMD_S_CARRIER 0x06 /* Set TX carrier frequency */
83#define MCE_CMD_G_CARRIER 0x07 /* Get TX carrier frequency */
84#define MCE_CMD_S_TXMASK 0x08 /* Set TX port bitmask */
85#define MCE_CMD_UNKNOWN3 0x09 /* Unknown */
86#define MCE_CMD_UNKNOWN4 0x0a /* Unknown */
87#define MCE_CMD_G_REVISION 0x0b /* Get hw/sw revision */
88#define MCE_CMD_S_TIMEOUT 0x0c /* Set RX timeout value */
89#define MCE_CMD_G_TIMEOUT 0x0d /* Get RX timeout value */
90#define MCE_CMD_UNKNOWN5 0x0e /* Unknown */
91#define MCE_CMD_UNKNOWN6 0x0f /* Unknown */
92#define MCE_CMD_G_RXPORTSTS 0x11 /* Get RX port status */
93#define MCE_CMD_G_TXMASK 0x13 /* Set TX port bitmask */
94#define MCE_CMD_S_RXSENSOR 0x14 /* Set RX sensor (std/learning) */
95#define MCE_CMD_G_RXSENSOR 0x15 /* Get RX sensor (std/learning) */
Jarod Wilson2ee95db2010-11-12 19:49:04 -030096#define MCE_RSP_PULSE_COUNT 0x15 /* RX pulse count (only if learning) */
Jarod Wilson4a883912010-10-22 14:42:54 -030097#define MCE_CMD_TX_PORTS 0x16 /* Get number of TX ports */
98#define MCE_CMD_G_WAKESRC 0x17 /* Get wake source */
99#define MCE_CMD_UNKNOWN7 0x18 /* Unknown */
100#define MCE_CMD_UNKNOWN8 0x19 /* Unknown */
101#define MCE_CMD_UNKNOWN9 0x1b /* Unknown */
102#define MCE_CMD_DEVICE_RESET 0xaa /* Reset the hardware */
103#define MCE_RSP_CMD_INVALID 0xfe /* Invalid command issued */
Jarod Wilson66e89522010-06-01 17:32:08 -0300104
105
106/* module parameters */
107#ifdef CONFIG_USB_DEBUG
108static int debug = 1;
109#else
110static int debug;
111#endif
112
113/* general constants */
114#define SEND_FLAG_IN_PROGRESS 1
115#define SEND_FLAG_COMPLETE 2
116#define RECV_FLAG_IN_PROGRESS 3
117#define RECV_FLAG_COMPLETE 4
118
119#define MCEUSB_RX 1
120#define MCEUSB_TX 2
121
122#define VENDOR_PHILIPS 0x0471
123#define VENDOR_SMK 0x0609
124#define VENDOR_TATUNG 0x1460
125#define VENDOR_GATEWAY 0x107b
126#define VENDOR_SHUTTLE 0x1308
127#define VENDOR_SHUTTLE2 0x051c
128#define VENDOR_MITSUMI 0x03ee
129#define VENDOR_TOPSEED 0x1784
130#define VENDOR_RICAVISION 0x179d
131#define VENDOR_ITRON 0x195d
132#define VENDOR_FIC 0x1509
133#define VENDOR_LG 0x043e
134#define VENDOR_MICROSOFT 0x045e
135#define VENDOR_FORMOSA 0x147a
136#define VENDOR_FINTEK 0x1934
137#define VENDOR_PINNACLE 0x2304
138#define VENDOR_ECS 0x1019
139#define VENDOR_WISTRON 0x0fb8
140#define VENDOR_COMPRO 0x185b
141#define VENDOR_NORTHSTAR 0x04eb
142#define VENDOR_REALTEK 0x0bda
143#define VENDOR_TIVO 0x105a
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -0300144#define VENDOR_CONEXANT 0x0572
Jarod Wilson66e89522010-06-01 17:32:08 -0300145
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300146enum mceusb_model_type {
147 MCE_GEN2 = 0, /* Most boards */
148 MCE_GEN1,
149 MCE_GEN3,
150 MCE_GEN2_TX_INV,
151 POLARIS_EVK,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300152 CX_HYBRID_TV,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300153};
154
155struct mceusb_model {
156 u32 mce_gen1:1;
157 u32 mce_gen2:1;
158 u32 mce_gen3:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300159 u32 tx_mask_normal:1;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300160 u32 is_polaris:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300161 u32 no_tx:1;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300162
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300163 const char *rc_map; /* Allow specify a per-board map */
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -0300164 const char *name; /* per-board name */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300165};
166
167static const struct mceusb_model mceusb_model[] = {
168 [MCE_GEN1] = {
169 .mce_gen1 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300170 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300171 },
172 [MCE_GEN2] = {
173 .mce_gen2 = 1,
174 },
175 [MCE_GEN2_TX_INV] = {
176 .mce_gen2 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300177 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300178 },
179 [MCE_GEN3] = {
180 .mce_gen3 = 1,
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300181 .tx_mask_normal = 1,
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300182 },
183 [POLARIS_EVK] = {
184 .is_polaris = 1,
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -0300185 /*
186 * In fact, the EVK is shipped without
187 * remotes, but we should have something handy,
188 * to allow testing it
189 */
190 .rc_map = RC_MAP_RC5_HAUPPAUGE_NEW,
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300191 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
192 },
193 [CX_HYBRID_TV] = {
194 .is_polaris = 1,
195 .no_tx = 1, /* tx isn't wired up at all */
196 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300197 },
198};
199
Jarod Wilson66e89522010-06-01 17:32:08 -0300200static struct usb_device_id mceusb_dev_table[] = {
201 /* Original Microsoft MCE IR Transceiver (often HP-branded) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300202 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
203 .driver_info = MCE_GEN1 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300204 /* Philips Infrared Transceiver - Sahara branded */
205 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
206 /* Philips Infrared Transceiver - HP branded */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300207 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
208 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300209 /* Philips SRM5100 */
210 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
211 /* Philips Infrared Transceiver - Omaura */
212 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
213 /* Philips Infrared Transceiver - Spinel plus */
214 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
215 /* Philips eHome Infrared Transceiver */
216 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
Jarod Wilsonc13df9c2010-08-27 18:21:14 -0300217 /* Philips/Spinel plus IR transceiver for ASUS */
218 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
219 /* Philips/Spinel plus IR transceiver for ASUS */
220 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300221 /* Realtek MCE IR Receiver */
222 { USB_DEVICE(VENDOR_REALTEK, 0x0161) },
223 /* SMK/Toshiba G83C0004D410 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300224 { USB_DEVICE(VENDOR_SMK, 0x031d),
225 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300226 /* SMK eHome Infrared Transceiver (Sony VAIO) */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300227 { USB_DEVICE(VENDOR_SMK, 0x0322),
228 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300229 /* bundled with Hauppauge PVR-150 */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300230 { USB_DEVICE(VENDOR_SMK, 0x0334),
231 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300232 /* SMK eHome Infrared Transceiver */
233 { USB_DEVICE(VENDOR_SMK, 0x0338) },
234 /* Tatung eHome Infrared Transceiver */
235 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
236 /* Shuttle eHome Infrared Transceiver */
237 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
238 /* Shuttle eHome Infrared Transceiver */
239 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
240 /* Gateway eHome Infrared Transceiver */
241 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
242 /* Mitsumi */
243 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
244 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300245 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
246 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300247 /* Topseed HP eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300248 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
249 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300250 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300251 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
252 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300253 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300254 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
255 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300256 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300257 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
258 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300259 /* Topseed eHome Infrared Transceiver */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300260 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
261 .driver_info = MCE_GEN2_TX_INV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300262 /* Ricavision internal Infrared Transceiver */
263 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
264 /* Itron ione Libra Q-11 */
265 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
266 /* FIC eHome Infrared Transceiver */
267 { USB_DEVICE(VENDOR_FIC, 0x9242) },
268 /* LG eHome Infrared Transceiver */
269 { USB_DEVICE(VENDOR_LG, 0x9803) },
270 /* Microsoft MCE Infrared Transceiver */
271 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
272 /* Formosa eHome Infrared Transceiver */
273 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
274 /* Formosa21 / eHome Infrared Receiver */
275 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
276 /* Formosa aim / Trust MCE Infrared Receiver */
277 { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
278 /* Formosa Industrial Computing / Beanbag Emulation Device */
279 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
280 /* Formosa21 / eHome Infrared Receiver */
281 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
282 /* Formosa Industrial Computing AIM IR605/A */
283 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
284 /* Formosa Industrial Computing */
285 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
Jarod Wilsonfbb1f1b2010-12-16 13:27:11 -0300286 /* Fintek eHome Infrared Transceiver (HP branded) */
287 { USB_DEVICE(VENDOR_FINTEK, 0x5168) },
Jarod Wilson66e89522010-06-01 17:32:08 -0300288 /* Fintek eHome Infrared Transceiver */
289 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
290 /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
291 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
292 /* Pinnacle Remote Kit */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300293 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
294 .driver_info = MCE_GEN3 },
Jarod Wilson66e89522010-06-01 17:32:08 -0300295 /* Elitegroup Computer Systems IR */
296 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
297 /* Wistron Corp. eHome Infrared Receiver */
298 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
299 /* Compro K100 */
300 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
301 /* Compro K100 v2 */
302 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
303 /* Northstar Systems, Inc. eHome Infrared Transceiver */
304 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
305 /* TiVo PC IR Receiver */
306 { USB_DEVICE(VENDOR_TIVO, 0x2000) },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300307 /* Conexant Hybrid TV "Shelby" Polaris SDK */
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300308 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
309 .driver_info = POLARIS_EVK },
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300310 /* Conexant Hybrid TV RDU253S Polaris */
311 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
312 .driver_info = CX_HYBRID_TV },
Jarod Wilson66e89522010-06-01 17:32:08 -0300313 /* Terminating entry */
314 { }
315};
316
Jarod Wilson66e89522010-06-01 17:32:08 -0300317/* data structure for each usb transceiver */
318struct mceusb_dev {
319 /* ir-core bits */
Jarod Wilson66e89522010-06-01 17:32:08 -0300320 struct ir_dev_props *props;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300321
322 /* optional features we can enable */
323 bool carrier_report_enabled;
324 bool learning_enabled;
Jarod Wilson66e89522010-06-01 17:32:08 -0300325
326 /* core device bits */
327 struct device *dev;
328 struct input_dev *idev;
329
330 /* usb */
331 struct usb_device *usbdev;
332 struct urb *urb_in;
333 struct usb_endpoint_descriptor *usb_ep_in;
334 struct usb_endpoint_descriptor *usb_ep_out;
335
336 /* buffers and dma */
337 unsigned char *buf_in;
338 unsigned int len_in;
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300339 dma_addr_t dma_in;
340 dma_addr_t dma_out;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300341
342 enum {
343 CMD_HEADER = 0,
344 SUBCMD,
345 CMD_DATA,
346 PARSE_IRDATA,
347 } parser_state;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300348
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300349 u8 cmd, rem; /* Remaining IR data bytes in packet */
Jarod Wilson66e89522010-06-01 17:32:08 -0300350
351 struct {
352 u32 connected:1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300353 u32 tx_mask_normal:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300354 u32 microsoft_gen1:1;
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300355 u32 no_tx:1;
Jarod Wilson66e89522010-06-01 17:32:08 -0300356 } flags;
357
Jarod Wilsone23fb962010-06-16 17:55:52 -0300358 /* transmit support */
Jarod Wilson66e89522010-06-01 17:32:08 -0300359 int send_flags;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300360 u32 carrier;
361 unsigned char tx_mask;
Jarod Wilson66e89522010-06-01 17:32:08 -0300362
363 char name[128];
364 char phys[64];
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -0300365 enum mceusb_model_type model;
Jarod Wilson66e89522010-06-01 17:32:08 -0300366};
367
368/*
369 * MCE Device Command Strings
370 * Device command responses vary from device to device...
371 * - DEVICE_RESET resets the hardware to its default state
372 * - GET_REVISION fetches the hardware/software revision, common
373 * replies are ff 0b 45 ff 1b 08 and ff 0b 50 ff 1b 42
374 * - GET_CARRIER_FREQ gets the carrier mode and frequency of the
375 * device, with replies in the form of 9f 06 MM FF, where MM is 0-3,
376 * meaning clk of 10000000, 2500000, 625000 or 156250, and FF is
377 * ((clk / frequency) - 1)
378 * - GET_RX_TIMEOUT fetches the receiver timeout in units of 50us,
379 * response in the form of 9f 0c msb lsb
380 * - GET_TX_BITMASK fetches the transmitter bitmask, replies in
381 * the form of 9f 08 bm, where bm is the bitmask
382 * - GET_RX_SENSOR fetches the RX sensor setting -- long-range
383 * general use one or short-range learning one, in the form of
384 * 9f 14 ss, where ss is either 01 for long-range or 02 for short
385 * - SET_CARRIER_FREQ sets a new carrier mode and frequency
386 * - SET_TX_BITMASK sets the transmitter bitmask
387 * - SET_RX_TIMEOUT sets the receiver timeout
388 * - SET_RX_SENSOR sets which receiver sensor to use
389 */
Jarod Wilson4a883912010-10-22 14:42:54 -0300390static char DEVICE_RESET[] = {MCE_COMMAND_NULL, MCE_HW_CMD_HEADER,
391 MCE_CMD_DEVICE_RESET};
392static char GET_REVISION[] = {MCE_HW_CMD_HEADER, MCE_CMD_G_REVISION};
393static char GET_UNKNOWN[] = {MCE_HW_CMD_HEADER, MCE_CMD_UNKNOWN7};
394static char GET_UNKNOWN2[] = {MCE_COMMAND_HEADER, MCE_CMD_UNKNOWN2};
395static char GET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER, MCE_CMD_G_CARRIER};
396static char GET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TIMEOUT};
397static char GET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_G_TXMASK};
398static char GET_RX_SENSOR[] = {MCE_COMMAND_HEADER, MCE_CMD_G_RXSENSOR};
Jarod Wilson66e89522010-06-01 17:32:08 -0300399/* sub in desired values in lower byte or bytes for full command */
400/* FIXME: make use of these for transmit.
Jarod Wilson4a883912010-10-22 14:42:54 -0300401static char SET_CARRIER_FREQ[] = {MCE_COMMAND_HEADER,
402 MCE_CMD_S_CARRIER, 0x00, 0x00};
403static char SET_TX_BITMASK[] = {MCE_COMMAND_HEADER, MCE_CMD_S_TXMASK, 0x00};
404static char SET_RX_TIMEOUT[] = {MCE_COMMAND_HEADER,
405 MCE_CMD_S_TIMEOUT, 0x00, 0x00};
406static char SET_RX_SENSOR[] = {MCE_COMMAND_HEADER,
407 MCE_CMD_S_RXSENSOR, 0x00};
Jarod Wilson66e89522010-06-01 17:32:08 -0300408*/
409
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300410static int mceusb_cmdsize(u8 cmd, u8 subcmd)
411{
412 int datasize = 0;
413
414 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300415 case MCE_COMMAND_NULL:
416 if (subcmd == MCE_HW_CMD_HEADER)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300417 datasize = 1;
418 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300419 case MCE_HW_CMD_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300420 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300421 case MCE_CMD_G_REVISION:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300422 datasize = 2;
423 break;
424 }
Jarod Wilson4a883912010-10-22 14:42:54 -0300425 case MCE_COMMAND_HEADER:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300426 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300427 case MCE_CMD_UNKNOWN:
428 case MCE_CMD_S_CARRIER:
429 case MCE_CMD_S_TIMEOUT:
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300430 case MCE_RSP_PULSE_COUNT:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300431 datasize = 2;
432 break;
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300433 case MCE_CMD_SIG_END:
Jarod Wilson4a883912010-10-22 14:42:54 -0300434 case MCE_CMD_S_TXMASK:
435 case MCE_CMD_S_RXSENSOR:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300436 datasize = 1;
437 break;
438 }
439 }
440 return datasize;
441}
442
Jarod Wilson66e89522010-06-01 17:32:08 -0300443static void mceusb_dev_printdata(struct mceusb_dev *ir, char *buf,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300444 int offset, int len, bool out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300445{
446 char codes[USB_BUFLEN * 3 + 1];
447 char inout[9];
Jarod Wilson66e89522010-06-01 17:32:08 -0300448 u8 cmd, subcmd, data1, data2;
449 struct device *dev = ir->dev;
Jarod Wilson36e9d262010-10-22 16:49:35 -0300450 int i, start, skip = 0;
451
452 if (!debug)
453 return;
Jarod Wilson66e89522010-06-01 17:32:08 -0300454
Jarod Wilson657290b2010-06-16 17:10:05 -0300455 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
Jarod Wilson29b44942010-11-09 18:41:46 -0300456 if (ir->flags.microsoft_gen1 && !out && !offset)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300457 skip = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300458
Jarod Wilson36e9d262010-10-22 16:49:35 -0300459 if (len <= skip)
Jarod Wilson66e89522010-06-01 17:32:08 -0300460 return;
461
462 for (i = 0; i < len && i < USB_BUFLEN; i++)
Jarod Wilson36e9d262010-10-22 16:49:35 -0300463 snprintf(codes + i * 3, 4, "%02x ", buf[i + offset] & 0xff);
Jarod Wilson66e89522010-06-01 17:32:08 -0300464
Jarod Wilson36e9d262010-10-22 16:49:35 -0300465 dev_info(dev, "%sx data: %s(length=%d)\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300466 (out ? "t" : "r"), codes, len);
467
468 if (out)
469 strcpy(inout, "Request\0");
470 else
471 strcpy(inout, "Got\0");
472
Jarod Wilson36e9d262010-10-22 16:49:35 -0300473 start = offset + skip;
474 cmd = buf[start] & 0xff;
475 subcmd = buf[start + 1] & 0xff;
476 data1 = buf[start + 2] & 0xff;
477 data2 = buf[start + 3] & 0xff;
Jarod Wilson66e89522010-06-01 17:32:08 -0300478
479 switch (cmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300480 case MCE_COMMAND_NULL:
481 if ((subcmd == MCE_HW_CMD_HEADER) &&
482 (data1 == MCE_CMD_DEVICE_RESET))
Jarod Wilson66e89522010-06-01 17:32:08 -0300483 dev_info(dev, "Device reset requested\n");
484 else
485 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
486 cmd, subcmd);
487 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300488 case MCE_HW_CMD_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300489 switch (subcmd) {
Jarod Wilson4a883912010-10-22 14:42:54 -0300490 case MCE_CMD_G_REVISION:
Jarod Wilson66e89522010-06-01 17:32:08 -0300491 if (len == 2)
492 dev_info(dev, "Get hw/sw rev?\n");
493 else
494 dev_info(dev, "hw/sw rev 0x%02x 0x%02x "
495 "0x%02x 0x%02x\n", data1, data2,
Jarod Wilson36e9d262010-10-22 16:49:35 -0300496 buf[start + 4], buf[start + 5]);
Jarod Wilson66e89522010-06-01 17:32:08 -0300497 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300498 case MCE_CMD_DEVICE_RESET:
Jarod Wilson66e89522010-06-01 17:32:08 -0300499 dev_info(dev, "Device reset requested\n");
500 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300501 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300502 dev_info(dev, "Previous command not supported\n");
503 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300504 case MCE_CMD_UNKNOWN7:
505 case MCE_CMD_UNKNOWN9:
Jarod Wilson66e89522010-06-01 17:32:08 -0300506 default:
507 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
508 cmd, subcmd);
509 break;
510 }
511 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300512 case MCE_COMMAND_HEADER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300513 switch (subcmd) {
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300514 case MCE_CMD_SIG_END:
515 dev_info(dev, "End of signal\n");
516 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300517 case MCE_CMD_PING:
Jarod Wilson66e89522010-06-01 17:32:08 -0300518 dev_info(dev, "Ping\n");
519 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300520 case MCE_CMD_UNKNOWN:
Jarod Wilson66e89522010-06-01 17:32:08 -0300521 dev_info(dev, "Resp to 9f 05 of 0x%02x 0x%02x\n",
522 data1, data2);
523 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300524 case MCE_CMD_S_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300525 dev_info(dev, "%s carrier mode and freq of "
526 "0x%02x 0x%02x\n", inout, data1, data2);
527 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300528 case MCE_CMD_G_CARRIER:
Jarod Wilson66e89522010-06-01 17:32:08 -0300529 dev_info(dev, "Get carrier mode and freq\n");
530 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300531 case MCE_CMD_S_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300532 dev_info(dev, "%s transmit blaster mask of 0x%02x\n",
533 inout, data1);
534 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300535 case MCE_CMD_S_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300536 /* value is in units of 50us, so x*50/100 or x/2 ms */
537 dev_info(dev, "%s receive timeout of %d ms\n",
538 inout, ((data1 << 8) | data2) / 2);
539 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300540 case MCE_CMD_G_TIMEOUT:
Jarod Wilson66e89522010-06-01 17:32:08 -0300541 dev_info(dev, "Get receive timeout\n");
542 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300543 case MCE_CMD_G_TXMASK:
Jarod Wilson66e89522010-06-01 17:32:08 -0300544 dev_info(dev, "Get transmit blaster mask\n");
545 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300546 case MCE_CMD_S_RXSENSOR:
Jarod Wilson66e89522010-06-01 17:32:08 -0300547 dev_info(dev, "%s %s-range receive sensor in use\n",
548 inout, data1 == 0x02 ? "short" : "long");
549 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300550 case MCE_CMD_G_RXSENSOR:
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300551 /* aka MCE_RSP_PULSE_COUNT */
552 if (out)
Jarod Wilson66e89522010-06-01 17:32:08 -0300553 dev_info(dev, "Get receive sensor\n");
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300554 else if (ir->learning_enabled)
555 dev_info(dev, "RX pulse count: %d\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300556 ((data1 << 8) | data2));
557 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300558 case MCE_RSP_CMD_INVALID:
Jarod Wilson66e89522010-06-01 17:32:08 -0300559 dev_info(dev, "Error! Hardware is likely wedged...\n");
560 break;
Jarod Wilson4a883912010-10-22 14:42:54 -0300561 case MCE_CMD_UNKNOWN2:
562 case MCE_CMD_UNKNOWN3:
563 case MCE_CMD_UNKNOWN5:
Jarod Wilson66e89522010-06-01 17:32:08 -0300564 default:
565 dev_info(dev, "Unknown command 0x%02x 0x%02x\n",
566 cmd, subcmd);
567 break;
568 }
569 break;
570 default:
571 break;
572 }
Jarod Wilson36e9d262010-10-22 16:49:35 -0300573
574 if (cmd == MCE_IRDATA_TRAILER)
575 dev_info(dev, "End of raw IR data\n");
576 else if ((cmd != MCE_COMMAND_HEADER) &&
577 ((cmd & MCE_COMMAND_MASK) == MCE_COMMAND_IRDATA))
578 dev_info(dev, "Raw IR data, %d pulse/space samples\n", ir->rem);
Jarod Wilson66e89522010-06-01 17:32:08 -0300579}
580
Jarod Wilson7c294402010-08-02 18:21:06 -0300581static void mce_async_callback(struct urb *urb, struct pt_regs *regs)
Jarod Wilson66e89522010-06-01 17:32:08 -0300582{
583 struct mceusb_dev *ir;
584 int len;
585
586 if (!urb)
587 return;
588
589 ir = urb->context;
590 if (ir) {
591 len = urb->actual_length;
592
593 dev_dbg(ir->dev, "callback called (status=%d len=%d)\n",
594 urb->status, len);
595
Jarod Wilson36e9d262010-10-22 16:49:35 -0300596 mceusb_dev_printdata(ir, urb->transfer_buffer, 0, len, true);
Jarod Wilson66e89522010-06-01 17:32:08 -0300597 }
598
599}
600
601/* request incoming or send outgoing usb packet - used to initialize remote */
602static void mce_request_packet(struct mceusb_dev *ir,
603 struct usb_endpoint_descriptor *ep,
604 unsigned char *data, int size, int urb_type)
605{
606 int res;
607 struct urb *async_urb;
608 struct device *dev = ir->dev;
609 unsigned char *async_buf;
610
611 if (urb_type == MCEUSB_TX) {
612 async_urb = usb_alloc_urb(0, GFP_KERNEL);
613 if (unlikely(!async_urb)) {
614 dev_err(dev, "Error, couldn't allocate urb!\n");
615 return;
616 }
617
618 async_buf = kzalloc(size, GFP_KERNEL);
619 if (!async_buf) {
620 dev_err(dev, "Error, couldn't allocate buf!\n");
621 usb_free_urb(async_urb);
622 return;
623 }
624
625 /* outbound data */
626 usb_fill_int_urb(async_urb, ir->usbdev,
627 usb_sndintpipe(ir->usbdev, ep->bEndpointAddress),
Jarod Wilson7c294402010-08-02 18:21:06 -0300628 async_buf, size, (usb_complete_t)mce_async_callback,
Jarod Wilson66e89522010-06-01 17:32:08 -0300629 ir, ep->bInterval);
630 memcpy(async_buf, data, size);
631
632 } else if (urb_type == MCEUSB_RX) {
633 /* standard request */
634 async_urb = ir->urb_in;
635 ir->send_flags = RECV_FLAG_IN_PROGRESS;
636
637 } else {
638 dev_err(dev, "Error! Unknown urb type %d\n", urb_type);
639 return;
640 }
641
642 dev_dbg(dev, "receive request called (size=%#x)\n", size);
643
644 async_urb->transfer_buffer_length = size;
645 async_urb->dev = ir->usbdev;
646
647 res = usb_submit_urb(async_urb, GFP_ATOMIC);
648 if (res) {
649 dev_dbg(dev, "receive request FAILED! (res=%d)\n", res);
650 return;
651 }
652 dev_dbg(dev, "receive request complete (res=%d)\n", res);
653}
654
655static void mce_async_out(struct mceusb_dev *ir, unsigned char *data, int size)
656{
657 mce_request_packet(ir, ir->usb_ep_out, data, size, MCEUSB_TX);
658}
659
660static void mce_sync_in(struct mceusb_dev *ir, unsigned char *data, int size)
661{
662 mce_request_packet(ir, ir->usb_ep_in, data, size, MCEUSB_RX);
663}
664
Jarod Wilsone23fb962010-06-16 17:55:52 -0300665/* Send data out the IR blaster port(s) */
666static int mceusb_tx_ir(void *priv, int *txbuf, u32 n)
667{
668 struct mceusb_dev *ir = priv;
669 int i, ret = 0;
670 int count, cmdcount = 0;
671 unsigned char *cmdbuf; /* MCE command buffer */
672 long signal_duration = 0; /* Singnal length in us */
673 struct timeval start_time, end_time;
674
675 do_gettimeofday(&start_time);
676
677 count = n / sizeof(int);
678
679 cmdbuf = kzalloc(sizeof(int) * MCE_CMDBUF_SIZE, GFP_KERNEL);
680 if (!cmdbuf)
681 return -ENOMEM;
682
683 /* MCE tx init header */
Jarod Wilson4a883912010-10-22 14:42:54 -0300684 cmdbuf[cmdcount++] = MCE_COMMAND_HEADER;
685 cmdbuf[cmdcount++] = MCE_CMD_S_TXMASK;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300686 cmdbuf[cmdcount++] = ir->tx_mask;
687
688 /* Generate mce packet data */
689 for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
690 signal_duration += txbuf[i];
691 txbuf[i] = txbuf[i] / MCE_TIME_UNIT;
692
693 do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
694
695 /* Insert mce packet header every 4th entry */
696 if ((cmdcount < MCE_CMDBUF_SIZE) &&
697 (cmdcount - MCE_TX_HEADER_LENGTH) %
698 MCE_CODE_LENGTH == 0)
Jarod Wilson4a883912010-10-22 14:42:54 -0300699 cmdbuf[cmdcount++] = MCE_IRDATA_HEADER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300700
701 /* Insert mce packet data */
702 if (cmdcount < MCE_CMDBUF_SIZE)
703 cmdbuf[cmdcount++] =
704 (txbuf[i] < MCE_PULSE_BIT ?
705 txbuf[i] : MCE_MAX_PULSE_LENGTH) |
706 (i & 1 ? 0x00 : MCE_PULSE_BIT);
707 else {
708 ret = -EINVAL;
709 goto out;
710 }
711
712 } while ((txbuf[i] > MCE_MAX_PULSE_LENGTH) &&
713 (txbuf[i] -= MCE_MAX_PULSE_LENGTH));
714 }
715
716 /* Fix packet length in last header */
717 cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
Jarod Wilson4a883912010-10-22 14:42:54 -0300718 MCE_COMMAND_IRDATA + (cmdcount - MCE_TX_HEADER_LENGTH) %
719 MCE_CODE_LENGTH - 1;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300720
721 /* Check if we have room for the empty packet at the end */
722 if (cmdcount >= MCE_CMDBUF_SIZE) {
723 ret = -EINVAL;
724 goto out;
725 }
726
727 /* All mce commands end with an empty packet (0x80) */
Jarod Wilson4a883912010-10-22 14:42:54 -0300728 cmdbuf[cmdcount++] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300729
730 /* Transmit the command to the mce device */
731 mce_async_out(ir, cmdbuf, cmdcount);
732
733 /*
734 * The lircd gap calculation expects the write function to
735 * wait the time it takes for the ircommand to be sent before
736 * it returns.
737 */
738 do_gettimeofday(&end_time);
739 signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
740 (end_time.tv_sec - start_time.tv_sec) * 1000000;
741
742 /* delay with the closest number of ticks */
743 set_current_state(TASK_INTERRUPTIBLE);
744 schedule_timeout(usecs_to_jiffies(signal_duration));
745
746out:
747 kfree(cmdbuf);
748 return ret ? ret : n;
749}
750
Jarod Wilson6f6c6252010-10-29 00:07:39 -0300751/* Sets active IR outputs -- mce devices typically have two */
Jarod Wilson657290b2010-06-16 17:10:05 -0300752static int mceusb_set_tx_mask(void *priv, u32 mask)
753{
754 struct mceusb_dev *ir = priv;
755
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -0300756 if (ir->flags.tx_mask_normal)
757 ir->tx_mask = mask;
758 else
Jarod Wilson4a883912010-10-22 14:42:54 -0300759 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
760 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
Jarod Wilson657290b2010-06-16 17:10:05 -0300761
762 return 0;
763}
764
Jarod Wilsone23fb962010-06-16 17:55:52 -0300765/* Sets the send carrier frequency and mode */
766static int mceusb_set_tx_carrier(void *priv, u32 carrier)
767{
768 struct mceusb_dev *ir = priv;
769 int clk = 10000000;
770 int prescaler = 0, divisor = 0;
Jarod Wilson4a883912010-10-22 14:42:54 -0300771 unsigned char cmdbuf[4] = { MCE_COMMAND_HEADER,
772 MCE_CMD_S_CARRIER, 0x00, 0x00 };
Jarod Wilsone23fb962010-06-16 17:55:52 -0300773
774 /* Carrier has changed */
775 if (ir->carrier != carrier) {
776
777 if (carrier == 0) {
778 ir->carrier = carrier;
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300779 cmdbuf[2] = MCE_CMD_SIG_END;
Jarod Wilson4a883912010-10-22 14:42:54 -0300780 cmdbuf[3] = MCE_IRDATA_TRAILER;
Jarod Wilsone23fb962010-06-16 17:55:52 -0300781 dev_dbg(ir->dev, "%s: disabling carrier "
782 "modulation\n", __func__);
783 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
784 return carrier;
785 }
786
787 for (prescaler = 0; prescaler < 4; ++prescaler) {
788 divisor = (clk >> (2 * prescaler)) / carrier;
Jarod Wilson4a883912010-10-22 14:42:54 -0300789 if (divisor <= 0xff) {
Jarod Wilsone23fb962010-06-16 17:55:52 -0300790 ir->carrier = carrier;
791 cmdbuf[2] = prescaler;
792 cmdbuf[3] = divisor;
793 dev_dbg(ir->dev, "%s: requesting %u HZ "
794 "carrier\n", __func__, carrier);
795
796 /* Transmit new carrier to mce device */
797 mce_async_out(ir, cmdbuf, sizeof(cmdbuf));
798 return carrier;
799 }
800 }
801
802 return -EINVAL;
803
804 }
805
806 return carrier;
807}
808
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300809/*
810 * We don't do anything but print debug spew for many of the command bits
811 * we receive from the hardware, but some of them are useful information
812 * we want to store so that we can use them.
813 */
814static void mceusb_handle_command(struct mceusb_dev *ir, int index)
815{
816 u8 hi = ir->buf_in[index + 1] & 0xff;
817 u8 lo = ir->buf_in[index + 2] & 0xff;
818
819 switch (ir->buf_in[index]) {
820 /* 2-byte return value commands */
821 case MCE_CMD_S_TIMEOUT:
822 ir->props->timeout = MS_TO_NS((hi << 8 | lo) / 2);
823 break;
824
825 /* 1-byte return value commands */
826 case MCE_CMD_S_TXMASK:
827 ir->tx_mask = hi;
828 break;
829 case MCE_CMD_S_RXSENSOR:
830 ir->learning_enabled = (hi == 0x02);
831 break;
832 default:
833 break;
834 }
835}
836
Jarod Wilson66e89522010-06-01 17:32:08 -0300837static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
838{
Maxim Levitsky46519182010-10-16 19:56:28 -0300839 DEFINE_IR_RAW_EVENT(rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300840 int i = 0;
Jarod Wilson66e89522010-06-01 17:32:08 -0300841
842 /* skip meaningless 0xb1 0x60 header bytes on orig receiver */
843 if (ir->flags.microsoft_gen1)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300844 i = 2;
Jarod Wilson66e89522010-06-01 17:32:08 -0300845
Jarod Wilson29b44942010-11-09 18:41:46 -0300846 /* if there's no data, just return now */
847 if (buf_len <= i)
848 return;
849
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300850 for (; i < buf_len; i++) {
851 switch (ir->parser_state) {
852 case SUBCMD:
853 ir->rem = mceusb_cmdsize(ir->cmd, ir->buf_in[i]);
Jarod Wilson36e9d262010-10-22 16:49:35 -0300854 mceusb_dev_printdata(ir, ir->buf_in, i - 1,
855 ir->rem + 2, false);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300856 mceusb_handle_command(ir, i);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300857 ir->parser_state = CMD_DATA;
858 break;
859 case PARSE_IRDATA:
Jarod Wilson66e89522010-06-01 17:32:08 -0300860 ir->rem--;
Jarod Wilson66e89522010-06-01 17:32:08 -0300861 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
862 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK)
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300863 * MS_TO_NS(MCE_TIME_UNIT);
Jarod Wilson66e89522010-06-01 17:32:08 -0300864
Jarod Wilson66e89522010-06-01 17:32:08 -0300865 dev_dbg(ir->dev, "Storing %s with duration %d\n",
866 rawir.pulse ? "pulse" : "space",
867 rawir.duration);
868
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300869 ir_raw_event_store_with_filter(ir->idev, &rawir);
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300870 break;
871 case CMD_DATA:
872 ir->rem--;
873 break;
874 case CMD_HEADER:
875 /* decode mce packets of the form (84),AA,BB,CC,DD */
876 /* IR data packets can span USB messages - rem */
877 ir->cmd = ir->buf_in[i];
Jarod Wilson4a883912010-10-22 14:42:54 -0300878 if ((ir->cmd == MCE_COMMAND_HEADER) ||
879 ((ir->cmd & MCE_COMMAND_MASK) !=
880 MCE_COMMAND_IRDATA)) {
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300881 ir->parser_state = SUBCMD;
882 continue;
883 }
884 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
Jarod Wilson2ee95db2010-11-12 19:49:04 -0300885 mceusb_dev_printdata(ir, ir->buf_in,
886 i, ir->rem + 1, false);
Jarod Wilson1cd50f22010-11-09 18:41:03 -0300887 if (ir->rem)
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300888 ir->parser_state = PARSE_IRDATA;
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300889 break;
Jarod Wilson66e89522010-06-01 17:32:08 -0300890 }
891
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300892 if (ir->parser_state != CMD_HEADER && !ir->rem)
893 ir->parser_state = CMD_HEADER;
Jarod Wilson66e89522010-06-01 17:32:08 -0300894 }
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300895 dev_dbg(ir->dev, "processed IR data, calling ir_raw_event_handle\n");
896 ir_raw_event_handle(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -0300897}
898
Jarod Wilson66e89522010-06-01 17:32:08 -0300899static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
900{
901 struct mceusb_dev *ir;
902 int buf_len;
903
904 if (!urb)
905 return;
906
907 ir = urb->context;
908 if (!ir) {
909 usb_unlink_urb(urb);
910 return;
911 }
912
913 buf_len = urb->actual_length;
914
Jarod Wilson66e89522010-06-01 17:32:08 -0300915 if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
916 ir->send_flags = SEND_FLAG_COMPLETE;
Jarod Wilson7a9fcb42010-07-29 18:34:52 -0300917 dev_dbg(ir->dev, "setup answer received %d bytes\n",
Jarod Wilson66e89522010-06-01 17:32:08 -0300918 buf_len);
919 }
920
921 switch (urb->status) {
922 /* success */
923 case 0:
924 mceusb_process_ir_data(ir, buf_len);
925 break;
926
927 case -ECONNRESET:
928 case -ENOENT:
929 case -ESHUTDOWN:
930 usb_unlink_urb(urb);
931 return;
932
933 case -EPIPE:
934 default:
Mauro Carvalho Chehab39dc5c32010-10-22 01:35:44 -0300935 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
Jarod Wilson66e89522010-06-01 17:32:08 -0300936 break;
937 }
938
939 usb_submit_urb(urb, GFP_ATOMIC);
940}
941
942static void mceusb_gen1_init(struct mceusb_dev *ir)
943{
Mauro Carvalho Chehabca17a4f2010-07-10 11:19:42 -0300944 int ret;
Jarod Wilson22b07662010-07-08 12:38:57 -0300945 int maxp = ir->len_in;
Jarod Wilson66e89522010-06-01 17:32:08 -0300946 struct device *dev = ir->dev;
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300947 char *data;
Jarod Wilson657290b2010-06-16 17:10:05 -0300948
949 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
950 if (!data) {
951 dev_err(dev, "%s: memory allocation failed!\n", __func__);
Jarod Wilson657290b2010-06-16 17:10:05 -0300952 return;
953 }
Jarod Wilson66e89522010-06-01 17:32:08 -0300954
955 /*
Jarod Wilsonb48592e2010-07-03 22:42:14 -0300956 * This is a strange one. Windows issues a set address to the device
Jarod Wilson66e89522010-06-01 17:32:08 -0300957 * on the receive control pipe and expect a certain value pair back
958 */
Jarod Wilson66e89522010-06-01 17:32:08 -0300959 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
960 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
Jarod Wilson657290b2010-06-16 17:10:05 -0300961 data, USB_CTRL_MSG_SZ, HZ * 3);
Jarod Wilson66e89522010-06-01 17:32:08 -0300962 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
963 dev_dbg(dev, "%s - data[0] = %d, data[1] = %d\n",
964 __func__, data[0], data[1]);
965
966 /* set feature: bit rate 38400 bps */
967 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
968 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
969 0xc04e, 0x0000, NULL, 0, HZ * 3);
970
971 dev_dbg(dev, "%s - ret = %d\n", __func__, ret);
972
973 /* bRequest 4: set char length to 8 bits */
974 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
975 4, USB_TYPE_VENDOR,
976 0x0808, 0x0000, NULL, 0, HZ * 3);
977 dev_dbg(dev, "%s - retB = %d\n", __func__, ret);
978
979 /* bRequest 2: set handshaking to use DTR/DSR */
980 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
981 2, USB_TYPE_VENDOR,
982 0x0000, 0x0100, NULL, 0, HZ * 3);
983 dev_dbg(dev, "%s - retC = %d\n", __func__, ret);
Jarod Wilson657290b2010-06-16 17:10:05 -0300984
Jarod Wilson22b07662010-07-08 12:38:57 -0300985 /* device reset */
986 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
987 mce_sync_in(ir, NULL, maxp);
988
989 /* get hw/sw revision? */
990 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
991 mce_sync_in(ir, NULL, maxp);
992
Jarod Wilson657290b2010-06-16 17:10:05 -0300993 kfree(data);
Jarod Wilson66e89522010-06-01 17:32:08 -0300994};
995
996static void mceusb_gen2_init(struct mceusb_dev *ir)
997{
998 int maxp = ir->len_in;
999
Jarod Wilson66e89522010-06-01 17:32:08 -03001000 /* device reset */
1001 mce_async_out(ir, DEVICE_RESET, sizeof(DEVICE_RESET));
1002 mce_sync_in(ir, NULL, maxp);
1003
1004 /* get hw/sw revision? */
1005 mce_async_out(ir, GET_REVISION, sizeof(GET_REVISION));
1006 mce_sync_in(ir, NULL, maxp);
1007
Jarod Wilson22b07662010-07-08 12:38:57 -03001008 /* unknown what the next two actually return... */
Jarod Wilson66e89522010-06-01 17:32:08 -03001009 mce_async_out(ir, GET_UNKNOWN, sizeof(GET_UNKNOWN));
1010 mce_sync_in(ir, NULL, maxp);
Jarod Wilson22b07662010-07-08 12:38:57 -03001011 mce_async_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
1012 mce_sync_in(ir, NULL, maxp);
Jarod Wilson66e89522010-06-01 17:32:08 -03001013}
1014
Jarod Wilson22b07662010-07-08 12:38:57 -03001015static void mceusb_get_parameters(struct mceusb_dev *ir)
Jarod Wilson66e89522010-06-01 17:32:08 -03001016{
1017 int maxp = ir->len_in;
1018
Jarod Wilson66e89522010-06-01 17:32:08 -03001019 /* get the carrier and frequency */
1020 mce_async_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
1021 mce_sync_in(ir, NULL, maxp);
1022
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001023 if (!ir->flags.no_tx) {
1024 /* get the transmitter bitmask */
1025 mce_async_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
1026 mce_sync_in(ir, NULL, maxp);
1027 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001028
1029 /* get receiver timeout value */
1030 mce_async_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
1031 mce_sync_in(ir, NULL, maxp);
1032
1033 /* get receiver sensor setting */
1034 mce_async_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
1035 mce_sync_in(ir, NULL, maxp);
1036}
1037
1038static struct input_dev *mceusb_init_input_dev(struct mceusb_dev *ir)
1039{
1040 struct input_dev *idev;
1041 struct ir_dev_props *props;
Jarod Wilson66e89522010-06-01 17:32:08 -03001042 struct device *dev = ir->dev;
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -03001043 const char *rc_map = RC_MAP_RC6_MCE;
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001044 const char *name = "Media Center Ed. eHome Infrared Remote Transceiver";
Jarod Wilson66e89522010-06-01 17:32:08 -03001045 int ret = -ENODEV;
1046
1047 idev = input_allocate_device();
1048 if (!idev) {
1049 dev_err(dev, "remote input dev allocation failed\n");
1050 goto idev_alloc_failed;
1051 }
1052
1053 ret = -ENOMEM;
1054 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
1055 if (!props) {
1056 dev_err(dev, "remote ir dev props allocation failed\n");
1057 goto props_alloc_failed;
1058 }
1059
Mauro Carvalho Chehab3459d452010-10-22 11:52:53 -03001060 if (mceusb_model[ir->model].name)
1061 name = mceusb_model[ir->model].name;
1062
1063 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1064 name,
Jarod Wilson66e89522010-06-01 17:32:08 -03001065 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1066 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1067
Jarod Wilson66e89522010-06-01 17:32:08 -03001068 idev->name = ir->name;
Jarod Wilson66e89522010-06-01 17:32:08 -03001069 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
1070 strlcat(ir->phys, "/input0", sizeof(ir->phys));
1071 idev->phys = ir->phys;
1072
Jarod Wilson66e89522010-06-01 17:32:08 -03001073 props->priv = ir;
1074 props->driver_type = RC_DRIVER_IR_RAW;
1075 props->allowed_protos = IR_TYPE_ALL;
Jarod Wilson501aaa12010-12-16 12:40:16 -03001076 props->timeout = MS_TO_NS(1000);
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001077 if (!ir->flags.no_tx) {
1078 props->s_tx_mask = mceusb_set_tx_mask;
1079 props->s_tx_carrier = mceusb_set_tx_carrier;
1080 props->tx_ir = mceusb_tx_ir;
1081 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001082
1083 ir->props = props;
Jarod Wilson66e89522010-06-01 17:32:08 -03001084
Paul Bender635f76b2010-12-16 13:23:07 -03001085 usb_to_input_id(ir->usbdev, &idev->id);
1086 idev->dev.parent = ir->dev;
1087
Mauro Carvalho Chehab17c2b1f2010-10-22 11:51:50 -03001088 if (mceusb_model[ir->model].rc_map)
1089 rc_map = mceusb_model[ir->model].rc_map;
1090
1091 ret = ir_input_register(idev, rc_map, props, DRIVER_NAME);
Jarod Wilson66e89522010-06-01 17:32:08 -03001092 if (ret < 0) {
1093 dev_err(dev, "remote input device register failed\n");
1094 goto irdev_failed;
1095 }
1096
1097 return idev;
1098
1099irdev_failed:
Jarod Wilson66e89522010-06-01 17:32:08 -03001100 kfree(props);
1101props_alloc_failed:
1102 input_free_device(idev);
1103idev_alloc_failed:
1104 return NULL;
1105}
1106
1107static int __devinit mceusb_dev_probe(struct usb_interface *intf,
1108 const struct usb_device_id *id)
1109{
1110 struct usb_device *dev = interface_to_usbdev(intf);
1111 struct usb_host_interface *idesc;
1112 struct usb_endpoint_descriptor *ep = NULL;
1113 struct usb_endpoint_descriptor *ep_in = NULL;
1114 struct usb_endpoint_descriptor *ep_out = NULL;
Jarod Wilson66e89522010-06-01 17:32:08 -03001115 struct mceusb_dev *ir = NULL;
Jarod Wilsond732a722010-06-16 17:10:46 -03001116 int pipe, maxp, i;
Jarod Wilson66e89522010-06-01 17:32:08 -03001117 char buf[63], name[128] = "";
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001118 enum mceusb_model_type model = id->driver_info;
Jarod Wilson66e89522010-06-01 17:32:08 -03001119 bool is_gen3;
1120 bool is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001121 bool tx_mask_normal;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001122 bool is_polaris;
Jarod Wilson66e89522010-06-01 17:32:08 -03001123
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001124 dev_dbg(&intf->dev, "%s called\n", __func__);
Jarod Wilson66e89522010-06-01 17:32:08 -03001125
Jarod Wilson66e89522010-06-01 17:32:08 -03001126 idesc = intf->cur_altsetting;
1127
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001128 is_gen3 = mceusb_model[model].mce_gen3;
1129 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001130 tx_mask_normal = mceusb_model[model].tx_mask_normal;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001131 is_polaris = mceusb_model[model].is_polaris;
Mauro Carvalho Chehab56e92f62010-10-18 16:32:50 -03001132
1133 if (is_polaris) {
1134 /* Interface 0 is IR */
1135 if (idesc->desc.bInterfaceNumber)
1136 return -ENODEV;
1137 }
Jarod Wilson66e89522010-06-01 17:32:08 -03001138
1139 /* step through the endpoints to find first bulk in and out endpoint */
1140 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1141 ep = &idesc->endpoint[i].desc;
1142
1143 if ((ep_in == NULL)
1144 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1145 == USB_DIR_IN)
1146 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1147 == USB_ENDPOINT_XFER_BULK)
1148 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1149 == USB_ENDPOINT_XFER_INT))) {
1150
Jarod Wilson66e89522010-06-01 17:32:08 -03001151 ep_in = ep;
1152 ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001153 ep_in->bInterval = 1;
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001154 dev_dbg(&intf->dev, "acceptable inbound endpoint "
Jarod Wilsond732a722010-06-16 17:10:46 -03001155 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001156 }
1157
1158 if ((ep_out == NULL)
1159 && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1160 == USB_DIR_OUT)
1161 && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1162 == USB_ENDPOINT_XFER_BULK)
1163 || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1164 == USB_ENDPOINT_XFER_INT))) {
1165
Jarod Wilson66e89522010-06-01 17:32:08 -03001166 ep_out = ep;
1167 ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
Jarod Wilsond732a722010-06-16 17:10:46 -03001168 ep_out->bInterval = 1;
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001169 dev_dbg(&intf->dev, "acceptable outbound endpoint "
Jarod Wilsond732a722010-06-16 17:10:46 -03001170 "found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001171 }
1172 }
1173 if (ep_in == NULL) {
Jarod Wilson1cd50f22010-11-09 18:41:03 -03001174 dev_dbg(&intf->dev, "inbound and/or endpoint not found\n");
Jarod Wilson66e89522010-06-01 17:32:08 -03001175 return -ENODEV;
1176 }
1177
1178 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1179 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1180
1181 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1182 if (!ir)
1183 goto mem_alloc_fail;
1184
1185 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1186 if (!ir->buf_in)
1187 goto buf_in_alloc_fail;
1188
1189 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1190 if (!ir->urb_in)
1191 goto urb_in_alloc_fail;
1192
1193 ir->usbdev = dev;
1194 ir->dev = &intf->dev;
1195 ir->len_in = maxp;
Jarod Wilson66e89522010-06-01 17:32:08 -03001196 ir->flags.microsoft_gen1 = is_microsoft_gen1;
Jarod Wilsond8cc7fd2010-12-15 19:20:55 -03001197 ir->flags.tx_mask_normal = tx_mask_normal;
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001198 ir->flags.no_tx = mceusb_model[model].no_tx;
Mauro Carvalho Chehab37dbd3a2010-10-22 11:50:37 -03001199 ir->model = model;
1200
Jarod Wilson66e89522010-06-01 17:32:08 -03001201 /* Saving usb interface data for use by the transmitter routine */
1202 ir->usb_ep_in = ep_in;
1203 ir->usb_ep_out = ep_out;
1204
1205 if (dev->descriptor.iManufacturer
1206 && usb_string(dev, dev->descriptor.iManufacturer,
1207 buf, sizeof(buf)) > 0)
1208 strlcpy(name, buf, sizeof(name));
1209 if (dev->descriptor.iProduct
1210 && usb_string(dev, dev->descriptor.iProduct,
1211 buf, sizeof(buf)) > 0)
1212 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1213 " %s", buf);
1214
1215 ir->idev = mceusb_init_input_dev(ir);
1216 if (!ir->idev)
1217 goto input_dev_fail;
1218
Jarod Wilsonb48592e2010-07-03 22:42:14 -03001219 /* flush buffers on the device */
1220 mce_sync_in(ir, NULL, maxp);
1221 mce_sync_in(ir, NULL, maxp);
1222
1223 /* wire up inbound data handler */
Jarod Wilson66e89522010-06-01 17:32:08 -03001224 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
1225 maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
1226 ir->urb_in->transfer_dma = ir->dma_in;
1227 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1228
Jarod Wilson66e89522010-06-01 17:32:08 -03001229 /* initialize device */
Jarod Wilson22b07662010-07-08 12:38:57 -03001230 if (ir->flags.microsoft_gen1)
Jarod Wilson66e89522010-06-01 17:32:08 -03001231 mceusb_gen1_init(ir);
Jarod Wilson22b07662010-07-08 12:38:57 -03001232 else if (!is_gen3)
Jarod Wilson66e89522010-06-01 17:32:08 -03001233 mceusb_gen2_init(ir);
1234
Jarod Wilson22b07662010-07-08 12:38:57 -03001235 mceusb_get_parameters(ir);
Jarod Wilson66e89522010-06-01 17:32:08 -03001236
Jarod Wilson6f6c6252010-10-29 00:07:39 -03001237 if (!ir->flags.no_tx)
1238 mceusb_set_tx_mask(ir, MCE_DEFAULT_TX_MASK);
Jarod Wilson66e89522010-06-01 17:32:08 -03001239
1240 usb_set_intfdata(intf, ir);
1241
1242 dev_info(&intf->dev, "Registered %s on usb%d:%d\n", name,
1243 dev->bus->busnum, dev->devnum);
1244
1245 return 0;
1246
1247 /* Error-handling path */
1248input_dev_fail:
1249 usb_free_urb(ir->urb_in);
1250urb_in_alloc_fail:
1251 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1252buf_in_alloc_fail:
1253 kfree(ir);
1254mem_alloc_fail:
1255 dev_err(&intf->dev, "%s: device setup failed!\n", __func__);
1256
1257 return -ENOMEM;
1258}
1259
1260
1261static void __devexit mceusb_dev_disconnect(struct usb_interface *intf)
1262{
1263 struct usb_device *dev = interface_to_usbdev(intf);
1264 struct mceusb_dev *ir = usb_get_intfdata(intf);
1265
1266 usb_set_intfdata(intf, NULL);
1267
1268 if (!ir)
1269 return;
1270
1271 ir->usbdev = NULL;
Jarod Wilsonbd3881b2010-06-16 17:08:32 -03001272 ir_input_unregister(ir->idev);
Jarod Wilson66e89522010-06-01 17:32:08 -03001273 usb_kill_urb(ir->urb_in);
1274 usb_free_urb(ir->urb_in);
1275 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1276
1277 kfree(ir);
1278}
1279
1280static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1281{
1282 struct mceusb_dev *ir = usb_get_intfdata(intf);
1283 dev_info(ir->dev, "suspend\n");
1284 usb_kill_urb(ir->urb_in);
1285 return 0;
1286}
1287
1288static int mceusb_dev_resume(struct usb_interface *intf)
1289{
1290 struct mceusb_dev *ir = usb_get_intfdata(intf);
1291 dev_info(ir->dev, "resume\n");
1292 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1293 return -EIO;
1294 return 0;
1295}
1296
1297static struct usb_driver mceusb_dev_driver = {
1298 .name = DRIVER_NAME,
1299 .probe = mceusb_dev_probe,
1300 .disconnect = mceusb_dev_disconnect,
1301 .suspend = mceusb_dev_suspend,
1302 .resume = mceusb_dev_resume,
1303 .reset_resume = mceusb_dev_resume,
1304 .id_table = mceusb_dev_table
1305};
1306
1307static int __init mceusb_dev_init(void)
1308{
1309 int ret;
1310
1311 ret = usb_register(&mceusb_dev_driver);
1312 if (ret < 0)
1313 printk(KERN_ERR DRIVER_NAME
1314 ": usb register failed, result = %d\n", ret);
1315
1316 return ret;
1317}
1318
1319static void __exit mceusb_dev_exit(void)
1320{
1321 usb_deregister(&mceusb_dev_driver);
1322}
1323
1324module_init(mceusb_dev_init);
1325module_exit(mceusb_dev_exit);
1326
1327MODULE_DESCRIPTION(DRIVER_DESC);
1328MODULE_AUTHOR(DRIVER_AUTHOR);
1329MODULE_LICENSE("GPL");
1330MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1331
1332module_param(debug, bool, S_IRUGO | S_IWUSR);
1333MODULE_PARM_DESC(debug, "Debug enabled or not");