This source file includes following definitions.
- mceusb_cmd_datasize
- mceusb_dev_printdata
- mceusb_defer_kevent
- mce_write_callback
- mce_write
- mce_command_out
- mceusb_tx_ir
- mceusb_set_tx_mask
- mceusb_set_tx_carrier
- mceusb_set_timeout
- mceusb_set_rx_wideband
- mceusb_set_rx_carrier_report
- mceusb_handle_command
- mceusb_process_ir_data
- mceusb_dev_recv
- mceusb_get_emulator_version
- mceusb_gen1_init
- mceusb_gen2_init
- mceusb_get_parameters
- mceusb_flash_led
- mceusb_deferred_kevent
- mceusb_init_rc_dev
- mceusb_dev_probe
- mceusb_dev_disconnect
- mceusb_dev_suspend
- mceusb_dev_resume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 #include <linux/device.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/workqueue.h>
29 #include <linux/usb.h>
30 #include <linux/usb/input.h>
31 #include <linux/pm_wakeup.h>
32 #include <media/rc-core.h>
33
34 #define DRIVER_VERSION "1.95"
35 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
36 #define DRIVER_DESC "Windows Media Center Ed. eHome Infrared Transceiver " \
37 "device driver"
38 #define DRIVER_NAME "mceusb"
39
40 #define USB_TX_TIMEOUT 1000
41 #define USB_CTRL_MSG_SZ 2
42 #define MCE_G1_INIT_MSGS 40
43
44
45 #define MCE_IRBUF_SIZE 128
46 #define MCE_TIME_UNIT 50
47 #define MCE_PACKET_SIZE 31
48 #define MCE_IRDATA_HEADER (0x80 + MCE_PACKET_SIZE - 1)
49
50 #define MCE_IRDATA_TRAILER 0x80
51 #define MCE_MAX_CHANNELS 2
52 #define MCE_DEFAULT_TX_MASK 0x03
53 #define MCE_PULSE_BIT 0x80
54 #define MCE_PULSE_MASK 0x7f
55 #define MCE_MAX_PULSE_LENGTH 0x7f
56
57
58
59
60
61
62
63
64
65
66
67
68
69 #define MCE_CMD 0x1f
70 #define MCE_PORT_IR 0x4
71 #define MCE_PORT_SYS 0x7
72 #define MCE_PORT_SER 0x6
73 #define MCE_PORT_MASK 0xe0
74
75
76 #define MCE_CMD_PORT_IR 0x9f
77 #define MCE_CMD_PORT_SYS 0xff
78
79
80 #define MCE_CMD_RESET 0xfe
81 #define MCE_CMD_RESUME 0xaa
82 #define MCE_CMD_SETIRCFS 0x06
83 #define MCE_CMD_SETIRTIMEOUT 0x0c
84 #define MCE_CMD_SETIRTXPORTS 0x08
85 #define MCE_CMD_SETIRRXPORTEN 0x14
86 #define MCE_CMD_FLASHLED 0x23
87
88
89 #define MCE_CMD_GETIRCFS 0x07
90 #define MCE_CMD_GETIRTIMEOUT 0x0d
91 #define MCE_CMD_GETIRTXPORTS 0x13
92 #define MCE_CMD_GETIRRXPORTEN 0x15
93 #define MCE_CMD_GETPORTSTATUS 0x11
94 #define MCE_CMD_GETIRNUMPORTS 0x16
95 #define MCE_CMD_GETWAKESOURCE 0x17
96 #define MCE_CMD_GETEMVER 0x22
97 #define MCE_CMD_GETDEVDETAILS 0x21
98 #define MCE_CMD_GETWAKESUPPORT 0x20
99 #define MCE_CMD_GETWAKEVERSION 0x18
100
101
102 #define MCE_CMD_NOP 0xff
103
104
105 #define MCE_RSP_EQIRCFS 0x06
106 #define MCE_RSP_EQIRTIMEOUT 0x0c
107 #define MCE_RSP_GETWAKESOURCE 0x17
108 #define MCE_RSP_EQIRTXPORTS 0x08
109 #define MCE_RSP_EQIRRXPORTEN 0x14
110 #define MCE_RSP_GETPORTSTATUS 0x11
111 #define MCE_RSP_EQIRRXCFCNT 0x15
112 #define MCE_RSP_EQIRNUMPORTS 0x16
113 #define MCE_RSP_EQWAKESUPPORT 0x20
114 #define MCE_RSP_EQWAKEVERSION 0x18
115 #define MCE_RSP_EQDEVDETAILS 0x21
116 #define MCE_RSP_EQEMVER 0x22
117 #define MCE_RSP_FLASHLED 0x23
118
119
120 #define MCE_RSP_CMD_ILLEGAL 0xfe
121 #define MCE_RSP_TX_TIMEOUT 0x81
122
123
124 #define MCE_CMD_SIG_END 0x01
125 #define MCE_CMD_PING 0x03
126 #define MCE_CMD_UNKNOWN 0x04
127 #define MCE_CMD_UNKNOWN2 0x05
128 #define MCE_CMD_UNKNOWN3 0x09
129 #define MCE_CMD_UNKNOWN4 0x0a
130 #define MCE_CMD_G_REVISION 0x0b
131 #define MCE_CMD_UNKNOWN5 0x0e
132 #define MCE_CMD_UNKNOWN6 0x0f
133 #define MCE_CMD_UNKNOWN8 0x19
134 #define MCE_CMD_UNKNOWN9 0x1b
135 #define MCE_CMD_NULL 0x00
136
137
138
139 #define MCE_COMMAND_IRDATA 0x80
140 #define MCE_PACKET_LENGTH_MASK 0x1f
141
142 #define VENDOR_PHILIPS 0x0471
143 #define VENDOR_SMK 0x0609
144 #define VENDOR_TATUNG 0x1460
145 #define VENDOR_GATEWAY 0x107b
146 #define VENDOR_SHUTTLE 0x1308
147 #define VENDOR_SHUTTLE2 0x051c
148 #define VENDOR_MITSUMI 0x03ee
149 #define VENDOR_TOPSEED 0x1784
150 #define VENDOR_RICAVISION 0x179d
151 #define VENDOR_ITRON 0x195d
152 #define VENDOR_FIC 0x1509
153 #define VENDOR_LG 0x043e
154 #define VENDOR_MICROSOFT 0x045e
155 #define VENDOR_FORMOSA 0x147a
156 #define VENDOR_FINTEK 0x1934
157 #define VENDOR_PINNACLE 0x2304
158 #define VENDOR_ECS 0x1019
159 #define VENDOR_WISTRON 0x0fb8
160 #define VENDOR_COMPRO 0x185b
161 #define VENDOR_NORTHSTAR 0x04eb
162 #define VENDOR_REALTEK 0x0bda
163 #define VENDOR_TIVO 0x105a
164 #define VENDOR_CONEXANT 0x0572
165 #define VENDOR_TWISTEDMELON 0x2596
166 #define VENDOR_HAUPPAUGE 0x2040
167 #define VENDOR_PCTV 0x2013
168 #define VENDOR_ADAPTEC 0x03f3
169
170 enum mceusb_model_type {
171 MCE_GEN2 = 0,
172 MCE_GEN1,
173 MCE_GEN3,
174 MCE_GEN3_BROKEN_IRTIMEOUT,
175 MCE_GEN2_TX_INV,
176 MCE_GEN2_TX_INV_RX_GOOD,
177 POLARIS_EVK,
178 CX_HYBRID_TV,
179 MULTIFUNCTION,
180 TIVO_KIT,
181 MCE_GEN2_NO_TX,
182 HAUPPAUGE_CX_HYBRID_TV,
183 EVROMEDIA_FULL_HYBRID_FULLHD,
184 ASTROMETA_T2HYBRID,
185 };
186
187 struct mceusb_model {
188 u32 mce_gen1:1;
189 u32 mce_gen2:1;
190 u32 mce_gen3:1;
191 u32 tx_mask_normal:1;
192 u32 no_tx:1;
193 u32 broken_irtimeout:1;
194
195
196
197
198
199
200 u32 rx2;
201
202 int ir_intfnum;
203
204 const char *rc_map;
205 const char *name;
206 };
207
208 static const struct mceusb_model mceusb_model[] = {
209 [MCE_GEN1] = {
210 .mce_gen1 = 1,
211 .tx_mask_normal = 1,
212 .rx2 = 2,
213 },
214 [MCE_GEN2] = {
215 .mce_gen2 = 1,
216 .rx2 = 2,
217 },
218 [MCE_GEN2_NO_TX] = {
219 .mce_gen2 = 1,
220 .no_tx = 1,
221 },
222 [MCE_GEN2_TX_INV] = {
223 .mce_gen2 = 1,
224 .tx_mask_normal = 1,
225 .rx2 = 1,
226 },
227 [MCE_GEN2_TX_INV_RX_GOOD] = {
228 .mce_gen2 = 1,
229 .tx_mask_normal = 1,
230 .rx2 = 2,
231 },
232 [MCE_GEN3] = {
233 .mce_gen3 = 1,
234 .tx_mask_normal = 1,
235 .rx2 = 2,
236 },
237 [MCE_GEN3_BROKEN_IRTIMEOUT] = {
238 .mce_gen3 = 1,
239 .tx_mask_normal = 1,
240 .rx2 = 2,
241 .broken_irtimeout = 1
242 },
243 [POLARIS_EVK] = {
244
245
246
247
248
249 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
250 .rx2 = 2,
251 },
252 [CX_HYBRID_TV] = {
253 .no_tx = 1,
254 .name = "Conexant Hybrid TV (cx231xx) MCE IR",
255 },
256 [HAUPPAUGE_CX_HYBRID_TV] = {
257 .no_tx = 1,
258 .name = "Conexant Hybrid TV (cx231xx) MCE IR no TX",
259 },
260 [MULTIFUNCTION] = {
261 .mce_gen2 = 1,
262 .ir_intfnum = 2,
263 .rx2 = 2,
264 },
265 [TIVO_KIT] = {
266 .mce_gen2 = 1,
267 .rc_map = RC_MAP_TIVO,
268 .rx2 = 2,
269 },
270 [EVROMEDIA_FULL_HYBRID_FULLHD] = {
271 .name = "Evromedia USB Full Hybrid Full HD",
272 .no_tx = 1,
273 .rc_map = RC_MAP_MSI_DIGIVOX_III,
274 },
275 [ASTROMETA_T2HYBRID] = {
276 .name = "Astrometa T2Hybrid",
277 .no_tx = 1,
278 .rc_map = RC_MAP_ASTROMETA_T2HYBRID,
279 }
280 };
281
282 static const struct usb_device_id mceusb_dev_table[] = {
283
284 { USB_DEVICE(VENDOR_MICROSOFT, 0x006d),
285 .driver_info = MCE_GEN1 },
286
287 { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
288
289 { USB_DEVICE(VENDOR_PHILIPS, 0x060c),
290 .driver_info = MCE_GEN2_TX_INV },
291
292 { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
293
294 { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
295
296 { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
297
298 { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
299
300 { USB_DEVICE(VENDOR_PHILIPS, 0x206c) },
301
302 { USB_DEVICE(VENDOR_PHILIPS, 0x2088) },
303
304 { USB_DEVICE(VENDOR_PHILIPS, 0x2093),
305 .driver_info = MCE_GEN2_TX_INV },
306
307 { USB_DEVICE(VENDOR_REALTEK, 0x0161),
308 .driver_info = MULTIFUNCTION },
309
310 { USB_DEVICE(VENDOR_SMK, 0x031d),
311 .driver_info = MCE_GEN2_TX_INV_RX_GOOD },
312
313 { USB_DEVICE(VENDOR_SMK, 0x0322),
314 .driver_info = MCE_GEN2_TX_INV },
315
316 { USB_DEVICE(VENDOR_SMK, 0x0334),
317 .driver_info = MCE_GEN2_TX_INV },
318
319 { USB_DEVICE(VENDOR_SMK, 0x0338) },
320
321 { USB_DEVICE(VENDOR_SMK, 0x0353),
322 .driver_info = MCE_GEN2_NO_TX },
323
324 { USB_DEVICE(VENDOR_SMK, 0x0357),
325 .driver_info = MCE_GEN2_NO_TX },
326
327 { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
328
329 { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
330
331 { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
332
333 { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
334
335 { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
336
337 { USB_DEVICE(VENDOR_TOPSEED, 0x0001),
338 .driver_info = MCE_GEN2_TX_INV },
339
340 { USB_DEVICE(VENDOR_TOPSEED, 0x0006),
341 .driver_info = MCE_GEN2_TX_INV },
342
343 { USB_DEVICE(VENDOR_TOPSEED, 0x0007),
344 .driver_info = MCE_GEN2_TX_INV },
345
346 { USB_DEVICE(VENDOR_TOPSEED, 0x0008),
347 .driver_info = MCE_GEN3 },
348
349 { USB_DEVICE(VENDOR_TOPSEED, 0x000a),
350 .driver_info = MCE_GEN2_TX_INV },
351
352 { USB_DEVICE(VENDOR_TOPSEED, 0x0011),
353 .driver_info = MCE_GEN3_BROKEN_IRTIMEOUT },
354
355 { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
356
357 { USB_DEVICE(VENDOR_ITRON, 0x7002) },
358
359 { USB_DEVICE(VENDOR_FIC, 0x9242) },
360
361 { USB_DEVICE(VENDOR_LG, 0x9803) },
362
363 { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
364
365 { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
366
367 { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
368
369 { USB_DEVICE(VENDOR_FORMOSA, 0xe017),
370 .driver_info = MCE_GEN2_NO_TX },
371
372 { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
373
374 { USB_DEVICE(VENDOR_FORMOSA, 0xe03a) },
375
376 { USB_DEVICE(VENDOR_FORMOSA, 0xe03c) },
377
378 { USB_DEVICE(VENDOR_FORMOSA, 0xe03e) },
379
380 { USB_DEVICE(VENDOR_FORMOSA, 0xe042) },
381
382 { USB_DEVICE(VENDOR_FINTEK, 0x5168),
383 .driver_info = MCE_GEN2_TX_INV },
384
385 { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
386
387 { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
388
389 { USB_DEVICE(VENDOR_PINNACLE, 0x0225),
390 .driver_info = MCE_GEN3 },
391
392 { USB_DEVICE(VENDOR_ECS, 0x0f38) },
393
394 { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
395
396 { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
397
398 { USB_DEVICE(VENDOR_COMPRO, 0x3082) },
399
400 { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
401
402 { USB_DEVICE(VENDOR_TIVO, 0x2000),
403 .driver_info = TIVO_KIT },
404
405 { USB_DEVICE(VENDOR_CONEXANT, 0x58a1),
406 .driver_info = POLARIS_EVK },
407
408 { USB_DEVICE(VENDOR_CONEXANT, 0x58a5),
409 .driver_info = CX_HYBRID_TV },
410
411 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8008) },
412
413 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8016) },
414
415 { USB_DEVICE(VENDOR_TWISTEDMELON, 0x8042) },
416
417 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb130),
418 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
419 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb131),
420 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
421 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb138),
422 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
423 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb139),
424 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
425
426 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb151),
427 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
428
429 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb123),
430 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
431
432 { USB_DEVICE(VENDOR_HAUPPAUGE, 0xb150),
433 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
434 { USB_DEVICE(VENDOR_PCTV, 0x0259),
435 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
436 { USB_DEVICE(VENDOR_PCTV, 0x025e),
437 .driver_info = HAUPPAUGE_CX_HYBRID_TV },
438
439 { USB_DEVICE(VENDOR_ADAPTEC, 0x0094) },
440
441 { USB_DEVICE(0x1b80, 0xd3b2),
442 .driver_info = EVROMEDIA_FULL_HYBRID_FULLHD },
443
444 { USB_DEVICE(0x15f4, 0x0135),
445 .driver_info = ASTROMETA_T2HYBRID },
446
447
448 { }
449 };
450
451
452 struct mceusb_dev {
453
454 struct rc_dev *rc;
455
456
457 bool carrier_report_enabled;
458 bool wideband_rx_enabled;
459
460
461 struct device *dev;
462
463
464 struct usb_device *usbdev;
465 struct usb_interface *usbintf;
466 struct urb *urb_in;
467 unsigned int pipe_in;
468 struct usb_endpoint_descriptor *usb_ep_out;
469 unsigned int pipe_out;
470
471
472 unsigned char *buf_in;
473 unsigned int len_in;
474 dma_addr_t dma_in;
475
476 enum {
477 CMD_HEADER = 0,
478 SUBCMD,
479 CMD_DATA,
480 PARSE_IRDATA,
481 } parser_state;
482
483 u8 cmd, rem;
484
485 struct {
486 u32 connected:1;
487 u32 tx_mask_normal:1;
488 u32 microsoft_gen1:1;
489 u32 no_tx:1;
490 u32 rx2;
491 } flags;
492
493
494 u32 carrier;
495 unsigned char tx_mask;
496
497 char name[128];
498 char phys[64];
499 enum mceusb_model_type model;
500
501 bool need_reset;
502 u8 emver;
503 u8 num_txports;
504 u8 num_rxports;
505 u8 txports_cabled;
506 u8 rxports_active;
507 bool learning_active;
508
509
510 u32 pulse_tunit;
511 u32 pulse_count;
512
513
514
515
516
517
518 struct work_struct kevent;
519 unsigned long kevent_flags;
520 # define EVENT_TX_HALT 0
521 # define EVENT_RX_HALT 1
522 # define EVENT_RST_PEND 31
523 };
524
525
526 static char DEVICE_RESUME[] = {MCE_CMD_NULL, MCE_CMD_PORT_SYS,
527 MCE_CMD_RESUME};
528 static char GET_REVISION[] = {MCE_CMD_PORT_SYS, MCE_CMD_G_REVISION};
529 static char GET_EMVER[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETEMVER};
530 static char GET_WAKEVERSION[] = {MCE_CMD_PORT_SYS, MCE_CMD_GETWAKEVERSION};
531 static char FLASH_LED[] = {MCE_CMD_PORT_SYS, MCE_CMD_FLASHLED};
532 static char GET_UNKNOWN2[] = {MCE_CMD_PORT_IR, MCE_CMD_UNKNOWN2};
533 static char GET_CARRIER_FREQ[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRCFS};
534 static char GET_RX_TIMEOUT[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTIMEOUT};
535 static char GET_NUM_PORTS[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRNUMPORTS};
536 static char GET_TX_BITMASK[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRTXPORTS};
537 static char GET_RX_SENSOR[] = {MCE_CMD_PORT_IR, MCE_CMD_GETIRRXPORTEN};
538
539
540
541
542
543
544
545
546
547
548
549 static int mceusb_cmd_datasize(u8 cmd, u8 subcmd)
550 {
551 int datasize = 0;
552
553 switch (cmd) {
554 case MCE_CMD_NULL:
555 if (subcmd == MCE_CMD_PORT_SYS)
556 datasize = 1;
557 break;
558 case MCE_CMD_PORT_SYS:
559 switch (subcmd) {
560 case MCE_RSP_GETPORTSTATUS:
561 datasize = 5;
562 break;
563 case MCE_RSP_EQWAKEVERSION:
564 datasize = 4;
565 break;
566 case MCE_CMD_G_REVISION:
567 datasize = 4;
568 break;
569 case MCE_RSP_EQWAKESUPPORT:
570 case MCE_RSP_GETWAKESOURCE:
571 case MCE_RSP_EQDEVDETAILS:
572 case MCE_RSP_EQEMVER:
573 datasize = 1;
574 break;
575 }
576 break;
577 case MCE_CMD_PORT_IR:
578 switch (subcmd) {
579 case MCE_CMD_UNKNOWN:
580 case MCE_RSP_EQIRCFS:
581 case MCE_RSP_EQIRTIMEOUT:
582 case MCE_RSP_EQIRRXCFCNT:
583 case MCE_RSP_EQIRNUMPORTS:
584 datasize = 2;
585 break;
586 case MCE_CMD_SIG_END:
587 case MCE_RSP_EQIRTXPORTS:
588 case MCE_RSP_EQIRRXPORTEN:
589 datasize = 1;
590 break;
591 }
592 }
593 return datasize;
594 }
595
596 static void mceusb_dev_printdata(struct mceusb_dev *ir, u8 *buf, int buf_len,
597 int offset, int len, bool out)
598 {
599 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
600 char *inout;
601 u8 cmd, subcmd, *data;
602 struct device *dev = ir->dev;
603 u32 carrier, period;
604
605 if (offset < 0 || offset >= buf_len)
606 return;
607
608 dev_dbg(dev, "%cx data[%d]: %*ph (len=%d sz=%d)",
609 (out ? 't' : 'r'), offset,
610 min(len, buf_len - offset), buf + offset, len, buf_len);
611
612 inout = out ? "Request" : "Got";
613
614 cmd = buf[offset];
615 subcmd = (offset + 1 < buf_len) ? buf[offset + 1] : 0;
616 data = &buf[offset] + 2;
617
618
619 if (ir->flags.microsoft_gen1 && !out && !offset) {
620 dev_dbg(dev, "MCE gen 1 header");
621 return;
622 }
623
624
625 if (cmd != MCE_CMD_PORT_IR &&
626 (cmd & MCE_PORT_MASK) == MCE_COMMAND_IRDATA) {
627 if (cmd == MCE_IRDATA_TRAILER)
628 dev_dbg(dev, "End of raw IR data");
629 else
630 dev_dbg(dev, "Raw IR data, %d pulse/space samples",
631 cmd & MCE_PACKET_LENGTH_MASK);
632 return;
633 }
634
635
636 if (offset + len > buf_len)
637 return;
638
639
640 switch (cmd) {
641 case MCE_CMD_NULL:
642 if (subcmd == MCE_CMD_NULL)
643 break;
644 if ((subcmd == MCE_CMD_PORT_SYS) &&
645 (data[0] == MCE_CMD_RESUME))
646 dev_dbg(dev, "Device resume requested");
647 else
648 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
649 cmd, subcmd);
650 break;
651 case MCE_CMD_PORT_SYS:
652 switch (subcmd) {
653 case MCE_RSP_EQEMVER:
654 if (!out)
655 dev_dbg(dev, "Emulator interface version %x",
656 data[0]);
657 break;
658 case MCE_CMD_G_REVISION:
659 if (len == 2)
660 dev_dbg(dev, "Get hw/sw rev?");
661 else
662 dev_dbg(dev, "hw/sw rev %*ph",
663 4, &buf[offset + 2]);
664 break;
665 case MCE_CMD_RESUME:
666 dev_dbg(dev, "Device resume requested");
667 break;
668 case MCE_RSP_CMD_ILLEGAL:
669 dev_dbg(dev, "Illegal PORT_SYS command");
670 break;
671 case MCE_RSP_EQWAKEVERSION:
672 if (!out)
673 dev_dbg(dev, "Wake version, proto: 0x%02x, payload: 0x%02x, address: 0x%02x, version: 0x%02x",
674 data[0], data[1], data[2], data[3]);
675 break;
676 case MCE_RSP_GETPORTSTATUS:
677 if (!out)
678
679 dev_dbg(dev, "TX port %d: blaster is%s connected",
680 data[0] + 1, data[3] ? " not" : "");
681 break;
682 case MCE_CMD_FLASHLED:
683 dev_dbg(dev, "Attempting to flash LED");
684 break;
685 default:
686 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
687 cmd, subcmd);
688 break;
689 }
690 break;
691 case MCE_CMD_PORT_IR:
692 switch (subcmd) {
693 case MCE_CMD_SIG_END:
694 dev_dbg(dev, "End of signal");
695 break;
696 case MCE_CMD_PING:
697 dev_dbg(dev, "Ping");
698 break;
699 case MCE_CMD_UNKNOWN:
700 dev_dbg(dev, "Resp to 9f 05 of 0x%02x 0x%02x",
701 data[0], data[1]);
702 break;
703 case MCE_RSP_EQIRCFS:
704 period = DIV_ROUND_CLOSEST((1U << data[0] * 2) *
705 (data[1] + 1), 10);
706 if (!period)
707 break;
708 carrier = (1000 * 1000) / period;
709 dev_dbg(dev, "%s carrier of %u Hz (period %uus)",
710 inout, carrier, period);
711 break;
712 case MCE_CMD_GETIRCFS:
713 dev_dbg(dev, "Get carrier mode and freq");
714 break;
715 case MCE_RSP_EQIRTXPORTS:
716 dev_dbg(dev, "%s transmit blaster mask of 0x%02x",
717 inout, data[0]);
718 break;
719 case MCE_RSP_EQIRTIMEOUT:
720
721 period = ((data[0] << 8) | data[1]) *
722 MCE_TIME_UNIT / 1000;
723 dev_dbg(dev, "%s receive timeout of %d ms",
724 inout, period);
725 break;
726 case MCE_CMD_GETIRTIMEOUT:
727 dev_dbg(dev, "Get receive timeout");
728 break;
729 case MCE_CMD_GETIRTXPORTS:
730 dev_dbg(dev, "Get transmit blaster mask");
731 break;
732 case MCE_RSP_EQIRRXPORTEN:
733 dev_dbg(dev, "%s %s-range receive sensor in use",
734 inout, data[0] == 0x02 ? "short" : "long");
735 break;
736 case MCE_CMD_GETIRRXPORTEN:
737
738 if (out)
739 dev_dbg(dev, "Get receive sensor");
740 else
741 dev_dbg(dev, "RX carrier cycle count: %d",
742 ((data[0] << 8) | data[1]));
743 break;
744 case MCE_RSP_EQIRNUMPORTS:
745 if (out)
746 break;
747 dev_dbg(dev, "Num TX ports: %x, num RX ports: %x",
748 data[0], data[1]);
749 break;
750 case MCE_RSP_CMD_ILLEGAL:
751 dev_dbg(dev, "Illegal PORT_IR command");
752 break;
753 case MCE_RSP_TX_TIMEOUT:
754 dev_dbg(dev, "IR TX timeout (TX buffer underrun)");
755 break;
756 default:
757 dev_dbg(dev, "Unknown command 0x%02x 0x%02x",
758 cmd, subcmd);
759 break;
760 }
761 break;
762 default:
763 break;
764 }
765 #endif
766 }
767
768
769
770
771
772
773
774 static void mceusb_defer_kevent(struct mceusb_dev *ir, int kevent)
775 {
776 set_bit(kevent, &ir->kevent_flags);
777
778 if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) {
779 dev_dbg(ir->dev, "kevent %d dropped pending USB Reset Device",
780 kevent);
781 return;
782 }
783
784 if (!schedule_work(&ir->kevent))
785 dev_dbg(ir->dev, "kevent %d already scheduled", kevent);
786 else
787 dev_dbg(ir->dev, "kevent %d scheduled", kevent);
788 }
789
790 static void mce_write_callback(struct urb *urb)
791 {
792 if (!urb)
793 return;
794
795 complete(urb->context);
796 }
797
798
799
800
801
802
803
804 static int mce_write(struct mceusb_dev *ir, u8 *data, int size)
805 {
806 int ret;
807 struct urb *urb;
808 struct device *dev = ir->dev;
809 unsigned char *buf_out;
810 struct completion tx_done;
811 unsigned long expire;
812 unsigned long ret_wait;
813
814 mceusb_dev_printdata(ir, data, size, 0, size, true);
815
816 urb = usb_alloc_urb(0, GFP_KERNEL);
817 if (unlikely(!urb)) {
818 dev_err(dev, "Error: mce write couldn't allocate urb");
819 return -ENOMEM;
820 }
821
822 buf_out = kmalloc(size, GFP_KERNEL);
823 if (!buf_out) {
824 usb_free_urb(urb);
825 return -ENOMEM;
826 }
827
828 init_completion(&tx_done);
829
830
831 if (usb_endpoint_xfer_int(ir->usb_ep_out))
832 usb_fill_int_urb(urb, ir->usbdev, ir->pipe_out,
833 buf_out, size, mce_write_callback, &tx_done,
834 ir->usb_ep_out->bInterval);
835 else
836 usb_fill_bulk_urb(urb, ir->usbdev, ir->pipe_out,
837 buf_out, size, mce_write_callback, &tx_done);
838 memcpy(buf_out, data, size);
839
840 ret = usb_submit_urb(urb, GFP_KERNEL);
841 if (ret) {
842 dev_err(dev, "Error: mce write submit urb error = %d", ret);
843 kfree(buf_out);
844 usb_free_urb(urb);
845 return ret;
846 }
847
848 expire = msecs_to_jiffies(USB_TX_TIMEOUT);
849 ret_wait = wait_for_completion_timeout(&tx_done, expire);
850 if (!ret_wait) {
851 dev_err(dev, "Error: mce write timed out (expire = %lu (%dms))",
852 expire, USB_TX_TIMEOUT);
853 usb_kill_urb(urb);
854 ret = (urb->status == -ENOENT ? -ETIMEDOUT : urb->status);
855 } else {
856 ret = urb->status;
857 }
858 if (ret >= 0)
859 ret = urb->actual_length;
860
861 switch (urb->status) {
862
863 case 0:
864 break;
865
866 case -ECONNRESET:
867 case -ENOENT:
868 case -EILSEQ:
869 case -ESHUTDOWN:
870 break;
871
872 case -EPIPE:
873 dev_err(ir->dev, "Error: mce write urb status = %d (TX HALT)",
874 urb->status);
875 mceusb_defer_kevent(ir, EVENT_TX_HALT);
876 break;
877
878 default:
879 dev_err(ir->dev, "Error: mce write urb status = %d",
880 urb->status);
881 break;
882 }
883
884 dev_dbg(dev, "tx done status = %d (wait = %lu, expire = %lu (%dms), urb->actual_length = %d, urb->status = %d)",
885 ret, ret_wait, expire, USB_TX_TIMEOUT,
886 urb->actual_length, urb->status);
887
888 kfree(buf_out);
889 usb_free_urb(urb);
890
891 return ret;
892 }
893
894 static void mce_command_out(struct mceusb_dev *ir, u8 *data, int size)
895 {
896 int rsize = sizeof(DEVICE_RESUME);
897
898 if (ir->need_reset) {
899 ir->need_reset = false;
900 mce_write(ir, DEVICE_RESUME, rsize);
901 msleep(10);
902 }
903
904 mce_write(ir, data, size);
905 msleep(10);
906 }
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925 static int mceusb_tx_ir(struct rc_dev *dev, unsigned *txbuf, unsigned count)
926 {
927 struct mceusb_dev *ir = dev->priv;
928 u8 cmdbuf[3] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTXPORTS, 0x00 };
929 u8 irbuf[MCE_IRBUF_SIZE];
930 int ircount = 0;
931 unsigned int irsample;
932 int i, length, ret;
933
934
935 cmdbuf[2] = ir->tx_mask;
936 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
937
938
939 for (i = 0; i < count; i++) {
940 irsample = txbuf[i] / MCE_TIME_UNIT;
941
942
943 while (irsample > 0) {
944
945 if (ircount % MCE_PACKET_SIZE == 0) {
946
947 if (ircount >= MCE_IRBUF_SIZE - 1) {
948
949 ret = mce_write(ir, irbuf, ircount);
950 if (ret < 0)
951 return ret;
952 ircount = 0;
953 }
954 irbuf[ircount++] = MCE_IRDATA_HEADER;
955 }
956
957
958 if (irsample <= MCE_MAX_PULSE_LENGTH) {
959 irbuf[ircount] = irsample;
960 irsample = 0;
961 } else {
962 irbuf[ircount] = MCE_MAX_PULSE_LENGTH;
963 irsample -= MCE_MAX_PULSE_LENGTH;
964 }
965
966
967
968
969 irbuf[ircount] |= (i & 1 ? 0 : MCE_PULSE_BIT);
970 ircount++;
971
972
973 if (ircount >= MCE_IRBUF_SIZE) {
974
975 length = ircount % MCE_PACKET_SIZE;
976 if (length > 0)
977 irbuf[ircount - length] -=
978 MCE_PACKET_SIZE - length;
979
980 ret = mce_write(ir, irbuf, ircount);
981 if (ret < 0)
982 return ret;
983 ircount = 0;
984 }
985 }
986 }
987
988
989 length = ircount % MCE_PACKET_SIZE;
990 if (length > 0)
991 irbuf[ircount - length] -= MCE_PACKET_SIZE - length;
992
993
994 irbuf[ircount++] = MCE_IRDATA_TRAILER;
995
996
997 ret = mce_write(ir, irbuf, ircount);
998 if (ret < 0)
999 return ret;
1000
1001 return count;
1002 }
1003
1004
1005 static int mceusb_set_tx_mask(struct rc_dev *dev, u32 mask)
1006 {
1007 struct mceusb_dev *ir = dev->priv;
1008
1009
1010 int emitters = ir->num_txports ? ir->num_txports : 2;
1011
1012 if (mask >= (1 << emitters))
1013 return emitters;
1014
1015 if (ir->flags.tx_mask_normal)
1016 ir->tx_mask = mask;
1017 else
1018 ir->tx_mask = (mask != MCE_DEFAULT_TX_MASK ?
1019 mask ^ MCE_DEFAULT_TX_MASK : mask) << 1;
1020
1021 return 0;
1022 }
1023
1024
1025 static int mceusb_set_tx_carrier(struct rc_dev *dev, u32 carrier)
1026 {
1027 struct mceusb_dev *ir = dev->priv;
1028 int clk = 10000000;
1029 int prescaler = 0, divisor = 0;
1030 unsigned char cmdbuf[4] = { MCE_CMD_PORT_IR,
1031 MCE_CMD_SETIRCFS, 0x00, 0x00 };
1032
1033
1034 if (ir->carrier != carrier) {
1035
1036 if (carrier == 0) {
1037 ir->carrier = carrier;
1038 cmdbuf[2] = MCE_CMD_SIG_END;
1039 cmdbuf[3] = MCE_IRDATA_TRAILER;
1040 dev_dbg(ir->dev, "disabling carrier modulation");
1041 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
1042 return 0;
1043 }
1044
1045 for (prescaler = 0; prescaler < 4; ++prescaler) {
1046 divisor = (clk >> (2 * prescaler)) / carrier;
1047 if (divisor <= 0xff) {
1048 ir->carrier = carrier;
1049 cmdbuf[2] = prescaler;
1050 cmdbuf[3] = divisor;
1051 dev_dbg(ir->dev, "requesting %u HZ carrier",
1052 carrier);
1053
1054
1055 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
1056 return 0;
1057 }
1058 }
1059
1060 return -EINVAL;
1061
1062 }
1063
1064 return 0;
1065 }
1066
1067 static int mceusb_set_timeout(struct rc_dev *dev, unsigned int timeout)
1068 {
1069 u8 cmdbuf[4] = { MCE_CMD_PORT_IR, MCE_CMD_SETIRTIMEOUT, 0, 0 };
1070 struct mceusb_dev *ir = dev->priv;
1071 unsigned int units;
1072
1073 units = DIV_ROUND_CLOSEST(timeout, US_TO_NS(MCE_TIME_UNIT));
1074
1075 cmdbuf[2] = units >> 8;
1076 cmdbuf[3] = units;
1077
1078 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
1079
1080
1081 mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
1082
1083 return 0;
1084 }
1085
1086
1087
1088
1089
1090
1091 static int mceusb_set_rx_wideband(struct rc_dev *dev, int enable)
1092 {
1093 struct mceusb_dev *ir = dev->priv;
1094 unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
1095 MCE_CMD_SETIRRXPORTEN, 0x00 };
1096
1097 dev_dbg(ir->dev, "select %s-range receive sensor",
1098 enable ? "short" : "long");
1099 if (enable) {
1100 ir->wideband_rx_enabled = true;
1101 cmdbuf[2] = 2;
1102 } else {
1103 ir->wideband_rx_enabled = false;
1104 cmdbuf[2] = 1;
1105 }
1106 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
1107
1108
1109 return 0;
1110 }
1111
1112
1113
1114
1115
1116
1117 static int mceusb_set_rx_carrier_report(struct rc_dev *dev, int enable)
1118 {
1119 struct mceusb_dev *ir = dev->priv;
1120 unsigned char cmdbuf[3] = { MCE_CMD_PORT_IR,
1121 MCE_CMD_SETIRRXPORTEN, 0x00 };
1122
1123 dev_dbg(ir->dev, "%s short-range receiver carrier reporting",
1124 enable ? "enable" : "disable");
1125 if (enable) {
1126 ir->carrier_report_enabled = true;
1127 if (!ir->learning_active) {
1128 cmdbuf[2] = 2;
1129 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
1130 }
1131 } else {
1132 ir->carrier_report_enabled = false;
1133
1134
1135
1136
1137
1138 if (ir->learning_active && !ir->wideband_rx_enabled) {
1139 cmdbuf[2] = 1;
1140 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
1141 }
1142 }
1143
1144 return 0;
1145 }
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 static void mceusb_handle_command(struct mceusb_dev *ir, u8 *buf_in)
1159 {
1160 u8 cmd = buf_in[0];
1161 u8 subcmd = buf_in[1];
1162 u8 *hi = &buf_in[2];
1163 u8 *lo = &buf_in[3];
1164 struct ir_raw_event rawir = {};
1165 u32 carrier_cycles;
1166 u32 cycles_fix;
1167
1168 if (cmd == MCE_CMD_PORT_SYS) {
1169 switch (subcmd) {
1170
1171 case MCE_RSP_GETPORTSTATUS:
1172 if (buf_in[5] == 0)
1173 ir->txports_cabled |= 1 << *hi;
1174 break;
1175
1176
1177 case MCE_RSP_EQEMVER:
1178 ir->emver = *hi;
1179 break;
1180
1181
1182 case MCE_RSP_CMD_ILLEGAL:
1183 ir->need_reset = true;
1184 break;
1185
1186 default:
1187 break;
1188 }
1189
1190 return;
1191 }
1192
1193 if (cmd != MCE_CMD_PORT_IR)
1194 return;
1195
1196 switch (subcmd) {
1197
1198 case MCE_RSP_EQIRTIMEOUT:
1199 ir->rc->timeout = US_TO_NS((*hi << 8 | *lo) * MCE_TIME_UNIT);
1200 break;
1201 case MCE_RSP_EQIRNUMPORTS:
1202 ir->num_txports = *hi;
1203 ir->num_rxports = *lo;
1204 break;
1205 case MCE_RSP_EQIRRXCFCNT:
1206
1207
1208
1209
1210
1211
1212
1213
1214 if (ir->carrier_report_enabled && ir->learning_active &&
1215 ir->pulse_tunit > 0) {
1216 carrier_cycles = (*hi << 8 | *lo);
1217
1218
1219
1220
1221 cycles_fix = ir->flags.rx2 == 2 ? ir->pulse_count : 0;
1222 rawir.carrier_report = 1;
1223 rawir.carrier = (1000000u / MCE_TIME_UNIT) *
1224 (carrier_cycles + cycles_fix) /
1225 ir->pulse_tunit;
1226 dev_dbg(ir->dev, "RX carrier frequency %u Hz (pulse count = %u, cycles = %u, duration = %u, rx2 = %u)",
1227 rawir.carrier, ir->pulse_count, carrier_cycles,
1228 ir->pulse_tunit, ir->flags.rx2);
1229 ir_raw_event_store(ir->rc, &rawir);
1230 }
1231 break;
1232
1233
1234 case MCE_RSP_EQIRTXPORTS:
1235 ir->tx_mask = *hi;
1236 break;
1237 case MCE_RSP_EQIRRXPORTEN:
1238 ir->learning_active = ((*hi & 0x02) == 0x02);
1239 if (ir->rxports_active != *hi) {
1240 dev_info(ir->dev, "%s-range (0x%x) receiver active",
1241 ir->learning_active ? "short" : "long", *hi);
1242 ir->rxports_active = *hi;
1243 }
1244 break;
1245
1246
1247 case MCE_RSP_CMD_ILLEGAL:
1248 case MCE_RSP_TX_TIMEOUT:
1249 ir->need_reset = true;
1250 break;
1251
1252 default:
1253 break;
1254 }
1255 }
1256
1257 static void mceusb_process_ir_data(struct mceusb_dev *ir, int buf_len)
1258 {
1259 struct ir_raw_event rawir = {};
1260 bool event = false;
1261 int i = 0;
1262
1263
1264 if (ir->flags.microsoft_gen1)
1265 i = 2;
1266
1267
1268 if (buf_len <= i)
1269 return;
1270
1271 for (; i < buf_len; i++) {
1272 switch (ir->parser_state) {
1273 case SUBCMD:
1274 ir->rem = mceusb_cmd_datasize(ir->cmd, ir->buf_in[i]);
1275 mceusb_dev_printdata(ir, ir->buf_in, buf_len, i - 1,
1276 ir->rem + 2, false);
1277 if (i + ir->rem < buf_len)
1278 mceusb_handle_command(ir, &ir->buf_in[i - 1]);
1279 ir->parser_state = CMD_DATA;
1280 break;
1281 case PARSE_IRDATA:
1282 ir->rem--;
1283 rawir.pulse = ((ir->buf_in[i] & MCE_PULSE_BIT) != 0);
1284 rawir.duration = (ir->buf_in[i] & MCE_PULSE_MASK);
1285 if (unlikely(!rawir.duration)) {
1286 dev_dbg(ir->dev, "nonsensical irdata %02x with duration 0",
1287 ir->buf_in[i]);
1288 break;
1289 }
1290 if (rawir.pulse) {
1291 ir->pulse_tunit += rawir.duration;
1292 ir->pulse_count++;
1293 }
1294 rawir.duration *= US_TO_NS(MCE_TIME_UNIT);
1295
1296 dev_dbg(ir->dev, "Storing %s %u ns (%02x)",
1297 rawir.pulse ? "pulse" : "space",
1298 rawir.duration, ir->buf_in[i]);
1299
1300 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
1301 event = true;
1302 break;
1303 case CMD_DATA:
1304 ir->rem--;
1305 break;
1306 case CMD_HEADER:
1307 ir->cmd = ir->buf_in[i];
1308 if ((ir->cmd == MCE_CMD_PORT_IR) ||
1309 ((ir->cmd & MCE_PORT_MASK) !=
1310 MCE_COMMAND_IRDATA)) {
1311
1312
1313
1314
1315 ir->parser_state = SUBCMD;
1316 continue;
1317 }
1318
1319
1320
1321
1322
1323 ir->rem = (ir->cmd & MCE_PACKET_LENGTH_MASK);
1324 mceusb_dev_printdata(ir, ir->buf_in, buf_len,
1325 i, ir->rem + 1, false);
1326 if (ir->rem) {
1327 ir->parser_state = PARSE_IRDATA;
1328 } else {
1329 struct ir_raw_event ev = {
1330 .timeout = 1,
1331 .duration = ir->rc->timeout
1332 };
1333
1334 if (ir_raw_event_store_with_filter(ir->rc,
1335 &ev))
1336 event = true;
1337 ir->pulse_tunit = 0;
1338 ir->pulse_count = 0;
1339 }
1340 break;
1341 }
1342
1343 if (ir->parser_state != CMD_HEADER && !ir->rem)
1344 ir->parser_state = CMD_HEADER;
1345 }
1346
1347
1348
1349
1350
1351 if (ir->parser_state != PARSE_IRDATA || !ir->rem)
1352 ir->parser_state = CMD_HEADER;
1353
1354 if (event) {
1355 dev_dbg(ir->dev, "processed IR data");
1356 ir_raw_event_handle(ir->rc);
1357 }
1358 }
1359
1360 static void mceusb_dev_recv(struct urb *urb)
1361 {
1362 struct mceusb_dev *ir;
1363
1364 if (!urb)
1365 return;
1366
1367 ir = urb->context;
1368 if (!ir) {
1369 usb_unlink_urb(urb);
1370 return;
1371 }
1372
1373 switch (urb->status) {
1374
1375 case 0:
1376 mceusb_process_ir_data(ir, urb->actual_length);
1377 break;
1378
1379 case -ECONNRESET:
1380 case -ENOENT:
1381 case -EILSEQ:
1382 case -ESHUTDOWN:
1383 usb_unlink_urb(urb);
1384 return;
1385
1386 case -EPIPE:
1387 dev_err(ir->dev, "Error: urb status = %d (RX HALT)",
1388 urb->status);
1389 mceusb_defer_kevent(ir, EVENT_RX_HALT);
1390 return;
1391
1392 default:
1393 dev_err(ir->dev, "Error: urb status = %d", urb->status);
1394 break;
1395 }
1396
1397 usb_submit_urb(urb, GFP_ATOMIC);
1398 }
1399
1400 static void mceusb_get_emulator_version(struct mceusb_dev *ir)
1401 {
1402
1403 ir->emver = 1;
1404 mce_command_out(ir, GET_EMVER, sizeof(GET_EMVER));
1405 }
1406
1407 static void mceusb_gen1_init(struct mceusb_dev *ir)
1408 {
1409 int ret;
1410 struct device *dev = ir->dev;
1411 char *data;
1412
1413 data = kzalloc(USB_CTRL_MSG_SZ, GFP_KERNEL);
1414 if (!data) {
1415 dev_err(dev, "%s: memory allocation failed!", __func__);
1416 return;
1417 }
1418
1419
1420
1421
1422
1423 ret = usb_control_msg(ir->usbdev, usb_rcvctrlpipe(ir->usbdev, 0),
1424 USB_REQ_SET_ADDRESS, USB_TYPE_VENDOR, 0, 0,
1425 data, USB_CTRL_MSG_SZ, HZ * 3);
1426 dev_dbg(dev, "set address - ret = %d", ret);
1427 dev_dbg(dev, "set address - data[0] = %d, data[1] = %d",
1428 data[0], data[1]);
1429
1430
1431 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1432 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
1433 0xc04e, 0x0000, NULL, 0, HZ * 3);
1434
1435 dev_dbg(dev, "set feature - ret = %d", ret);
1436
1437
1438 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1439 4, USB_TYPE_VENDOR,
1440 0x0808, 0x0000, NULL, 0, HZ * 3);
1441 dev_dbg(dev, "set char length - retB = %d", ret);
1442
1443
1444 ret = usb_control_msg(ir->usbdev, usb_sndctrlpipe(ir->usbdev, 0),
1445 2, USB_TYPE_VENDOR,
1446 0x0000, 0x0100, NULL, 0, HZ * 3);
1447 dev_dbg(dev, "set handshake - retC = %d", ret);
1448
1449
1450 mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1451
1452
1453 mce_command_out(ir, GET_REVISION, sizeof(GET_REVISION));
1454
1455 kfree(data);
1456 }
1457
1458 static void mceusb_gen2_init(struct mceusb_dev *ir)
1459 {
1460
1461 mce_command_out(ir, DEVICE_RESUME, sizeof(DEVICE_RESUME));
1462
1463
1464 mce_command_out(ir, GET_WAKEVERSION, sizeof(GET_WAKEVERSION));
1465
1466
1467 mce_command_out(ir, GET_UNKNOWN2, sizeof(GET_UNKNOWN2));
1468 }
1469
1470 static void mceusb_get_parameters(struct mceusb_dev *ir)
1471 {
1472 int i;
1473 unsigned char cmdbuf[3] = { MCE_CMD_PORT_SYS,
1474 MCE_CMD_GETPORTSTATUS, 0x00 };
1475
1476
1477 ir->num_txports = 2;
1478 ir->num_rxports = 2;
1479
1480
1481 mce_command_out(ir, GET_NUM_PORTS, sizeof(GET_NUM_PORTS));
1482
1483
1484 mce_command_out(ir, GET_CARRIER_FREQ, sizeof(GET_CARRIER_FREQ));
1485
1486 if (ir->num_txports && !ir->flags.no_tx)
1487
1488 mce_command_out(ir, GET_TX_BITMASK, sizeof(GET_TX_BITMASK));
1489
1490
1491 mce_command_out(ir, GET_RX_TIMEOUT, sizeof(GET_RX_TIMEOUT));
1492
1493
1494 mce_command_out(ir, GET_RX_SENSOR, sizeof(GET_RX_SENSOR));
1495
1496 for (i = 0; i < ir->num_txports; i++) {
1497 cmdbuf[2] = i;
1498 mce_command_out(ir, cmdbuf, sizeof(cmdbuf));
1499 }
1500 }
1501
1502 static void mceusb_flash_led(struct mceusb_dev *ir)
1503 {
1504 if (ir->emver < 2)
1505 return;
1506
1507 mce_command_out(ir, FLASH_LED, sizeof(FLASH_LED));
1508 }
1509
1510
1511
1512
1513
1514
1515
1516
1517 static void mceusb_deferred_kevent(struct work_struct *work)
1518 {
1519 struct mceusb_dev *ir =
1520 container_of(work, struct mceusb_dev, kevent);
1521 int status;
1522
1523 dev_err(ir->dev, "kevent handler called (flags 0x%lx)",
1524 ir->kevent_flags);
1525
1526 if (test_bit(EVENT_RST_PEND, &ir->kevent_flags)) {
1527 dev_err(ir->dev, "kevent handler canceled pending USB Reset Device");
1528 return;
1529 }
1530
1531 if (test_bit(EVENT_RX_HALT, &ir->kevent_flags)) {
1532 usb_unlink_urb(ir->urb_in);
1533 status = usb_clear_halt(ir->usbdev, ir->pipe_in);
1534 dev_err(ir->dev, "rx clear halt status = %d", status);
1535 if (status < 0) {
1536
1537
1538
1539
1540 dev_err(ir->dev,
1541 "stuck RX HALT state requires USB Reset Device to clear");
1542 usb_queue_reset_device(ir->usbintf);
1543 set_bit(EVENT_RST_PEND, &ir->kevent_flags);
1544 clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
1545
1546
1547 clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
1548 return;
1549 }
1550 clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
1551 status = usb_submit_urb(ir->urb_in, GFP_KERNEL);
1552 if (status < 0) {
1553 dev_err(ir->dev, "rx unhalt submit urb error = %d",
1554 status);
1555 }
1556 }
1557
1558 if (test_bit(EVENT_TX_HALT, &ir->kevent_flags)) {
1559 status = usb_clear_halt(ir->usbdev, ir->pipe_out);
1560 dev_err(ir->dev, "tx clear halt status = %d", status);
1561 if (status < 0) {
1562
1563
1564
1565
1566 dev_err(ir->dev,
1567 "stuck TX HALT state requires USB Reset Device to clear");
1568 usb_queue_reset_device(ir->usbintf);
1569 set_bit(EVENT_RST_PEND, &ir->kevent_flags);
1570 clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
1571
1572
1573 clear_bit(EVENT_RX_HALT, &ir->kevent_flags);
1574 return;
1575 }
1576 clear_bit(EVENT_TX_HALT, &ir->kevent_flags);
1577 }
1578 }
1579
1580 static struct rc_dev *mceusb_init_rc_dev(struct mceusb_dev *ir)
1581 {
1582 struct usb_device *udev = ir->usbdev;
1583 struct device *dev = ir->dev;
1584 struct rc_dev *rc;
1585 int ret;
1586
1587 rc = rc_allocate_device(RC_DRIVER_IR_RAW);
1588 if (!rc) {
1589 dev_err(dev, "remote dev allocation failed");
1590 goto out;
1591 }
1592
1593 snprintf(ir->name, sizeof(ir->name), "%s (%04x:%04x)",
1594 mceusb_model[ir->model].name ?
1595 mceusb_model[ir->model].name :
1596 "Media Center Ed. eHome Infrared Remote Transceiver",
1597 le16_to_cpu(ir->usbdev->descriptor.idVendor),
1598 le16_to_cpu(ir->usbdev->descriptor.idProduct));
1599
1600 usb_make_path(ir->usbdev, ir->phys, sizeof(ir->phys));
1601
1602 rc->device_name = ir->name;
1603 rc->input_phys = ir->phys;
1604 usb_to_input_id(ir->usbdev, &rc->input_id);
1605 rc->dev.parent = dev;
1606 rc->priv = ir;
1607 rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
1608 rc->min_timeout = US_TO_NS(MCE_TIME_UNIT);
1609 rc->timeout = MS_TO_NS(100);
1610 if (!mceusb_model[ir->model].broken_irtimeout) {
1611 rc->s_timeout = mceusb_set_timeout;
1612 rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
1613 } else {
1614
1615
1616
1617
1618 rc->max_timeout = rc->timeout;
1619 }
1620 if (!ir->flags.no_tx) {
1621 rc->s_tx_mask = mceusb_set_tx_mask;
1622 rc->s_tx_carrier = mceusb_set_tx_carrier;
1623 rc->tx_ir = mceusb_tx_ir;
1624 }
1625 if (ir->flags.rx2 > 0) {
1626 rc->s_learning_mode = mceusb_set_rx_wideband;
1627 rc->s_carrier_report = mceusb_set_rx_carrier_report;
1628 }
1629 rc->driver_name = DRIVER_NAME;
1630
1631 switch (le16_to_cpu(udev->descriptor.idVendor)) {
1632 case VENDOR_HAUPPAUGE:
1633 rc->map_name = RC_MAP_HAUPPAUGE;
1634 break;
1635 case VENDOR_PCTV:
1636 rc->map_name = RC_MAP_PINNACLE_PCTV_HD;
1637 break;
1638 default:
1639 rc->map_name = RC_MAP_RC6_MCE;
1640 }
1641 if (mceusb_model[ir->model].rc_map)
1642 rc->map_name = mceusb_model[ir->model].rc_map;
1643
1644 ret = rc_register_device(rc);
1645 if (ret < 0) {
1646 dev_err(dev, "remote dev registration failed");
1647 goto out;
1648 }
1649
1650 return rc;
1651
1652 out:
1653 rc_free_device(rc);
1654 return NULL;
1655 }
1656
1657 static int mceusb_dev_probe(struct usb_interface *intf,
1658 const struct usb_device_id *id)
1659 {
1660 struct usb_device *dev = interface_to_usbdev(intf);
1661 struct usb_host_interface *idesc;
1662 struct usb_endpoint_descriptor *ep = NULL;
1663 struct usb_endpoint_descriptor *ep_in = NULL;
1664 struct usb_endpoint_descriptor *ep_out = NULL;
1665 struct mceusb_dev *ir = NULL;
1666 int pipe, maxp, i, res;
1667 char buf[63], name[128] = "";
1668 enum mceusb_model_type model = id->driver_info;
1669 bool is_gen3;
1670 bool is_microsoft_gen1;
1671 bool tx_mask_normal;
1672 int ir_intfnum;
1673
1674 dev_dbg(&intf->dev, "%s called", __func__);
1675
1676 idesc = intf->cur_altsetting;
1677
1678 is_gen3 = mceusb_model[model].mce_gen3;
1679 is_microsoft_gen1 = mceusb_model[model].mce_gen1;
1680 tx_mask_normal = mceusb_model[model].tx_mask_normal;
1681 ir_intfnum = mceusb_model[model].ir_intfnum;
1682
1683
1684 if (idesc->desc.bInterfaceNumber != ir_intfnum)
1685 return -ENODEV;
1686
1687
1688 for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
1689 ep = &idesc->endpoint[i].desc;
1690
1691 if (ep_in == NULL) {
1692 if (usb_endpoint_is_bulk_in(ep)) {
1693 ep_in = ep;
1694 dev_dbg(&intf->dev, "acceptable bulk inbound endpoint found\n");
1695 } else if (usb_endpoint_is_int_in(ep)) {
1696 ep_in = ep;
1697 ep_in->bInterval = 1;
1698 dev_dbg(&intf->dev, "acceptable interrupt inbound endpoint found\n");
1699 }
1700 }
1701
1702 if (ep_out == NULL) {
1703 if (usb_endpoint_is_bulk_out(ep)) {
1704 ep_out = ep;
1705 dev_dbg(&intf->dev, "acceptable bulk outbound endpoint found\n");
1706 } else if (usb_endpoint_is_int_out(ep)) {
1707 ep_out = ep;
1708 ep_out->bInterval = 1;
1709 dev_dbg(&intf->dev, "acceptable interrupt outbound endpoint found\n");
1710 }
1711 }
1712 }
1713 if (!ep_in || !ep_out) {
1714 dev_dbg(&intf->dev, "required endpoints not found\n");
1715 return -ENODEV;
1716 }
1717
1718 if (usb_endpoint_xfer_int(ep_in))
1719 pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
1720 else
1721 pipe = usb_rcvbulkpipe(dev, ep_in->bEndpointAddress);
1722 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
1723
1724 ir = kzalloc(sizeof(struct mceusb_dev), GFP_KERNEL);
1725 if (!ir)
1726 goto mem_alloc_fail;
1727
1728 ir->pipe_in = pipe;
1729 ir->buf_in = usb_alloc_coherent(dev, maxp, GFP_ATOMIC, &ir->dma_in);
1730 if (!ir->buf_in)
1731 goto buf_in_alloc_fail;
1732
1733 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
1734 if (!ir->urb_in)
1735 goto urb_in_alloc_fail;
1736
1737 ir->usbintf = intf;
1738 ir->usbdev = usb_get_dev(dev);
1739 ir->dev = &intf->dev;
1740 ir->len_in = maxp;
1741 ir->flags.microsoft_gen1 = is_microsoft_gen1;
1742 ir->flags.tx_mask_normal = tx_mask_normal;
1743 ir->flags.no_tx = mceusb_model[model].no_tx;
1744 ir->flags.rx2 = mceusb_model[model].rx2;
1745 ir->model = model;
1746
1747
1748 ir->usb_ep_out = ep_out;
1749 if (usb_endpoint_xfer_int(ep_out))
1750 ir->pipe_out = usb_sndintpipe(ir->usbdev,
1751 ep_out->bEndpointAddress);
1752 else
1753 ir->pipe_out = usb_sndbulkpipe(ir->usbdev,
1754 ep_out->bEndpointAddress);
1755
1756 if (dev->descriptor.iManufacturer
1757 && usb_string(dev, dev->descriptor.iManufacturer,
1758 buf, sizeof(buf)) > 0)
1759 strscpy(name, buf, sizeof(name));
1760 if (dev->descriptor.iProduct
1761 && usb_string(dev, dev->descriptor.iProduct,
1762 buf, sizeof(buf)) > 0)
1763 snprintf(name + strlen(name), sizeof(name) - strlen(name),
1764 " %s", buf);
1765
1766
1767
1768
1769
1770 INIT_WORK(&ir->kevent, mceusb_deferred_kevent);
1771
1772 ir->rc = mceusb_init_rc_dev(ir);
1773 if (!ir->rc)
1774 goto rc_dev_fail;
1775
1776
1777 if (usb_endpoint_xfer_int(ep_in))
1778 usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1779 mceusb_dev_recv, ir, ep_in->bInterval);
1780 else
1781 usb_fill_bulk_urb(ir->urb_in, dev, pipe, ir->buf_in, maxp,
1782 mceusb_dev_recv, ir);
1783
1784 ir->urb_in->transfer_dma = ir->dma_in;
1785 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1786
1787
1788 dev_dbg(&intf->dev, "Flushing receive buffers");
1789 res = usb_submit_urb(ir->urb_in, GFP_KERNEL);
1790 if (res)
1791 dev_err(&intf->dev, "failed to flush buffers: %d", res);
1792
1793
1794 mceusb_get_emulator_version(ir);
1795
1796
1797 if (ir->flags.microsoft_gen1)
1798 mceusb_gen1_init(ir);
1799 else if (!is_gen3)
1800 mceusb_gen2_init(ir);
1801
1802 mceusb_get_parameters(ir);
1803
1804 mceusb_flash_led(ir);
1805
1806 if (!ir->flags.no_tx)
1807 mceusb_set_tx_mask(ir->rc, MCE_DEFAULT_TX_MASK);
1808
1809 usb_set_intfdata(intf, ir);
1810
1811
1812 device_set_wakeup_capable(ir->dev, true);
1813 device_set_wakeup_enable(ir->dev, true);
1814
1815 dev_info(&intf->dev, "Registered %s with mce emulator interface version %x",
1816 name, ir->emver);
1817 dev_info(&intf->dev, "%x tx ports (0x%x cabled) and %x rx sensors (0x%x active)",
1818 ir->num_txports, ir->txports_cabled,
1819 ir->num_rxports, ir->rxports_active);
1820
1821 return 0;
1822
1823
1824 rc_dev_fail:
1825 cancel_work_sync(&ir->kevent);
1826 usb_put_dev(ir->usbdev);
1827 usb_kill_urb(ir->urb_in);
1828 usb_free_urb(ir->urb_in);
1829 urb_in_alloc_fail:
1830 usb_free_coherent(dev, maxp, ir->buf_in, ir->dma_in);
1831 buf_in_alloc_fail:
1832 kfree(ir);
1833 mem_alloc_fail:
1834 dev_err(&intf->dev, "%s: device setup failed!", __func__);
1835
1836 return -ENOMEM;
1837 }
1838
1839
1840 static void mceusb_dev_disconnect(struct usb_interface *intf)
1841 {
1842 struct usb_device *dev = interface_to_usbdev(intf);
1843 struct mceusb_dev *ir = usb_get_intfdata(intf);
1844
1845 dev_dbg(&intf->dev, "%s called", __func__);
1846
1847 usb_set_intfdata(intf, NULL);
1848
1849 if (!ir)
1850 return;
1851
1852 ir->usbdev = NULL;
1853 cancel_work_sync(&ir->kevent);
1854 rc_unregister_device(ir->rc);
1855 usb_kill_urb(ir->urb_in);
1856 usb_free_urb(ir->urb_in);
1857 usb_free_coherent(dev, ir->len_in, ir->buf_in, ir->dma_in);
1858 usb_put_dev(dev);
1859
1860 kfree(ir);
1861 }
1862
1863 static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1864 {
1865 struct mceusb_dev *ir = usb_get_intfdata(intf);
1866 dev_info(ir->dev, "suspend");
1867 usb_kill_urb(ir->urb_in);
1868 return 0;
1869 }
1870
1871 static int mceusb_dev_resume(struct usb_interface *intf)
1872 {
1873 struct mceusb_dev *ir = usb_get_intfdata(intf);
1874 dev_info(ir->dev, "resume");
1875 if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1876 return -EIO;
1877 return 0;
1878 }
1879
1880 static struct usb_driver mceusb_dev_driver = {
1881 .name = DRIVER_NAME,
1882 .probe = mceusb_dev_probe,
1883 .disconnect = mceusb_dev_disconnect,
1884 .suspend = mceusb_dev_suspend,
1885 .resume = mceusb_dev_resume,
1886 .reset_resume = mceusb_dev_resume,
1887 .id_table = mceusb_dev_table
1888 };
1889
1890 module_usb_driver(mceusb_dev_driver);
1891
1892 MODULE_DESCRIPTION(DRIVER_DESC);
1893 MODULE_AUTHOR(DRIVER_AUTHOR);
1894 MODULE_LICENSE("GPL");
1895 MODULE_DEVICE_TABLE(usb, mceusb_dev_table);