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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
/* $NetBSD: irmce.c,v 1.9 2023/05/10 00:12:28 riastradh Exp $ */
/*-
* Copyright (c) 2011 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* IR receiver/transceiver for Windows Media Center
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: irmce.c,v 1.9 2023/05/10 00:12:28 riastradh Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/select.h>
#include <sys/module.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/ir/ir.h>
#include <dev/ir/cirio.h>
#include <dev/ir/cirvar.h>
enum irmce_state {
IRMCE_STATE_HEADER,
IRMCE_STATE_IRDATA,
IRMCE_STATE_CMDHEADER,
IRMCE_STATE_CMDDATA,
};
struct irmce_softc {
device_t sc_dev;
device_t sc_cirdev;
struct usbd_device * sc_udev;
struct usbd_interface * sc_iface;
int sc_bulkin_ep;
uint16_t sc_bulkin_maxpktsize;
struct usbd_pipe * sc_bulkin_pipe;
struct usbd_xfer * sc_bulkin_xfer;
uint8_t * sc_bulkin_buffer;
int sc_bulkout_ep;
uint16_t sc_bulkout_maxpktsize;
struct usbd_pipe * sc_bulkout_pipe;
struct usbd_xfer * sc_bulkout_xfer;
uint8_t * sc_bulkout_buffer;
bool sc_raw;
uint8_t sc_ir_buf[16];
size_t sc_ir_bufused;
size_t sc_ir_resid;
enum irmce_state sc_ir_state;
uint8_t sc_ir_header;
bool sc_rc6_hb[256];
size_t sc_rc6_nhb;
};
static int irmce_match(device_t, cfdata_t, void *);
static void irmce_attach(device_t, device_t, void *);
static int irmce_detach(device_t, int);
static void irmce_childdet(device_t, device_t);
static int irmce_activate(device_t, enum devact);
static int irmce_rescan(device_t, const char *, const int *);
static int irmce_print(void *, const char *);
static int irmce_reset(struct irmce_softc *);
static int irmce_open(void *, int, int, struct proc *);
static int irmce_close(void *, int, int, struct proc *);
static int irmce_read(void *, struct uio *, int);
static int irmce_write(void *, struct uio *, int);
static int irmce_setparams(void *, struct cir_params *);
static const struct cir_methods irmce_cir_methods = {
.im_open = irmce_open,
.im_close = irmce_close,
.im_read = irmce_read,
.im_write = irmce_write,
.im_setparams = irmce_setparams,
};
static const struct {
uint16_t vendor;
uint16_t product;
} irmce_devices[] = {
{ USB_VENDOR_SMK, USB_PRODUCT_SMK_MCE_IR },
};
CFATTACH_DECL2_NEW(irmce, sizeof(struct irmce_softc),
irmce_match, irmce_attach, irmce_detach, irmce_activate,
irmce_rescan, irmce_childdet);
static int
irmce_match(device_t parent, cfdata_t match, void *opaque)
{
struct usbif_attach_arg *uiaa = opaque;
unsigned int i;
for (i = 0; i < __arraycount(irmce_devices); i++) {
if (irmce_devices[i].vendor == uiaa->uiaa_vendor &&
irmce_devices[i].product == uiaa->uiaa_product)
return UMATCH_VENDOR_PRODUCT;
}
return UMATCH_NONE;
}
static void
irmce_attach(device_t parent, device_t self, void *opaque)
{
struct irmce_softc *sc = device_private(self);
struct usbif_attach_arg *uiaa = opaque;
usb_endpoint_descriptor_t *ed;
char *devinfop;
unsigned int i;
uint8_t nep;
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
aprint_naive("\n");
devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
aprint_normal(": %s\n", devinfop);
usbd_devinfo_free(devinfop);
sc->sc_dev = self;
sc->sc_udev = uiaa->uiaa_device;
sc->sc_iface = uiaa->uiaa_iface;
nep = 0;
usbd_endpoint_count(sc->sc_iface, &nep);
sc->sc_bulkin_ep = sc->sc_bulkout_ep = -1;
for (i = 0; i < nep; i++) {
int dir, type;
ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
if (ed == NULL) {
aprint_error_dev(self,
"couldn't read endpoint descriptor %d\n", i);
continue;
}
dir = UE_GET_DIR(ed->bEndpointAddress);
type = UE_GET_XFERTYPE(ed->bmAttributes);
if (type != UE_BULK)
continue;
if (dir == UE_DIR_IN && sc->sc_bulkin_ep == -1) {
sc->sc_bulkin_ep = ed->bEndpointAddress;
sc->sc_bulkin_maxpktsize =
UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
(UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
}
if (dir == UE_DIR_OUT && sc->sc_bulkout_ep == -1) {
sc->sc_bulkout_ep = ed->bEndpointAddress;
sc->sc_bulkout_maxpktsize =
UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
(UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
}
}
aprint_debug_dev(self, "in 0x%02x/%d out 0x%02x/%d\n",
sc->sc_bulkin_ep, sc->sc_bulkin_maxpktsize,
sc->sc_bulkout_ep, sc->sc_bulkout_maxpktsize);
if (sc->sc_bulkin_maxpktsize < 16 || sc->sc_bulkout_maxpktsize < 16) {
aprint_error_dev(self, "bad maxpktsize\n");
return;
}
usbd_status err;
err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_ep,
USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
if (err) {
aprint_error_dev(sc->sc_dev,
"couldn't open bulk-in pipe: %s\n", usbd_errstr(err));
return;
}
err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_ep,
USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
if (err) {
aprint_error_dev(sc->sc_dev,
"couldn't open bulk-out pipe: %s\n", usbd_errstr(err));
usbd_close_pipe(sc->sc_bulkin_pipe);
sc->sc_bulkin_pipe = NULL;
return;
}
int error;
error = usbd_create_xfer(sc->sc_bulkin_pipe, sc->sc_bulkin_maxpktsize,
0, 0, &sc->sc_bulkin_xfer);
if (error) {
goto fail;
}
error = usbd_create_xfer(sc->sc_bulkout_pipe,
sc->sc_bulkout_maxpktsize, USBD_FORCE_SHORT_XFER, 0,
&sc->sc_bulkout_xfer);
if (error) {
goto fail;
}
sc->sc_bulkin_buffer = usbd_get_buffer(sc->sc_bulkin_xfer);
sc->sc_bulkout_buffer = usbd_get_buffer(sc->sc_bulkout_xfer);
irmce_rescan(self, NULL, NULL);
return;
fail:
if (sc->sc_bulkin_xfer)
usbd_destroy_xfer(sc->sc_bulkin_xfer);
if (sc->sc_bulkout_xfer)
usbd_destroy_xfer(sc->sc_bulkout_xfer);
}
static int
irmce_detach(device_t self, int flags)
{
struct irmce_softc *sc = device_private(self);
int error;
error = config_detach_children(self, flags);
if (error)
return error;
if (sc->sc_bulkin_pipe) {
usbd_abort_pipe(sc->sc_bulkin_pipe);
}
if (sc->sc_bulkout_pipe) {
usbd_abort_pipe(sc->sc_bulkout_pipe);
}
if (sc->sc_bulkin_xfer) {
usbd_destroy_xfer(sc->sc_bulkin_xfer);
sc->sc_bulkin_buffer = NULL;
sc->sc_bulkin_xfer = NULL;
}
if (sc->sc_bulkout_xfer) {
usbd_destroy_xfer(sc->sc_bulkout_xfer);
sc->sc_bulkout_buffer = NULL;
sc->sc_bulkout_xfer = NULL;
}
if (sc->sc_bulkin_pipe) {
usbd_close_pipe(sc->sc_bulkin_pipe);
sc->sc_bulkin_pipe = NULL;
}
if (sc->sc_bulkout_pipe) {
usbd_close_pipe(sc->sc_bulkout_pipe);
sc->sc_bulkout_pipe = NULL;
}
pmf_device_deregister(self);
return 0;
}
static int
irmce_activate(device_t self, enum devact act)
{
return 0;
}
static int
irmce_rescan(device_t self, const char *ifattr, const int *locators)
{
struct irmce_softc *sc = device_private(self);
struct ir_attach_args iaa;
if (sc->sc_cirdev == NULL) {
iaa.ia_type = IR_TYPE_CIR;
iaa.ia_methods = &irmce_cir_methods;
iaa.ia_handle = sc;
sc->sc_cirdev =
config_found(self, &iaa, irmce_print, CFARGS_NONE);
}
return 0;
}
static int
irmce_print(void *priv, const char *pnp)
{
if (pnp)
aprint_normal("cir at %s", pnp);
return UNCONF;
}
static void
irmce_childdet(device_t self, device_t child)
{
struct irmce_softc *sc = device_private(self);
if (sc->sc_cirdev == child)
sc->sc_cirdev = NULL;
}
static int
irmce_reset(struct irmce_softc *sc)
{
static const uint8_t reset_cmd[] = { 0x00, 0xff, 0xaa };
uint8_t *p = sc->sc_bulkout_buffer;
usbd_status err;
uint32_t wlen;
unsigned int n;
for (n = 0; n < __arraycount(reset_cmd); n++)
*p++ = reset_cmd[n];
wlen = sizeof(reset_cmd);
err = usbd_bulk_transfer(sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
USBD_FORCE_SHORT_XFER, USBD_DEFAULT_TIMEOUT,
sc->sc_bulkout_buffer, &wlen);
if (err != USBD_NORMAL_COMPLETION) {
if (err == USBD_INTERRUPTED)
return EINTR;
else if (err == USBD_TIMEOUT)
return ETIMEDOUT;
else
return EIO;
}
return 0;
}
static int
irmce_open(void *priv, int flag, int mode, struct proc *p)
{
struct irmce_softc *sc = priv;
int err = irmce_reset(sc);
if (err) {
aprint_error_dev(sc->sc_dev,
"couldn't reset device: %s\n", usbd_errstr(err));
}
sc->sc_ir_state = IRMCE_STATE_HEADER;
sc->sc_rc6_nhb = 0;
return 0;
}
static int
irmce_close(void *priv, int flag, int mode, struct proc *p)
{
struct irmce_softc *sc = priv;
if (sc->sc_bulkin_pipe) {
usbd_abort_pipe(sc->sc_bulkin_pipe);
}
if (sc->sc_bulkout_pipe) {
usbd_abort_pipe(sc->sc_bulkout_pipe);
}
return 0;
}
static int
irmce_rc6_decode(struct irmce_softc *sc, uint8_t *buf, size_t buflen,
struct uio *uio)
{
bool *hb = &sc->sc_rc6_hb[0];
unsigned int n;
int state, pulse;
uint32_t data;
uint8_t mode;
bool idle = false;
for (n = 0; n < buflen; n++) {
state = (buf[n] & 0x80) ? 1 : 0;
pulse = (buf[n] & 0x7f) * 50;
if (pulse >= 300 && pulse <= 600) {
hb[sc->sc_rc6_nhb++] = state;
} else if (pulse >= 680 && pulse <= 1080) {
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
} else if (pulse >= 1150 && pulse <= 1450) {
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
} else if (pulse >= 2400 && pulse <= 2800) {
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
hb[sc->sc_rc6_nhb++] = state;
} else if (pulse > 3000) {
if (sc->sc_rc6_nhb & 1)
hb[sc->sc_rc6_nhb++] = state;
idle = true;
break;
} else {
aprint_debug_dev(sc->sc_dev,
"error parsing RC6 stream (pulse=%d)\n", pulse);
return EIO;
}
}
if (!idle)
return 0;
if (sc->sc_rc6_nhb < 20) {
aprint_debug_dev(sc->sc_dev, "not enough RC6 data\n");
return EIO;
}
/* RC6 leader 11111100 */
if (!hb[0] || !hb[1] || !hb[2] || !hb[3] || !hb[4] || !hb[5] ||
hb[6] || hb[7]) {
aprint_debug_dev(sc->sc_dev, "bad RC6 leader\n");
return EIO;
}
/* start bit 10 */
if (!hb[8] || hb[9]) {
aprint_debug_dev(sc->sc_dev, "missing RC6 start bit\n");
return EIO;
}
/* mode info */
mode = 0x00;
for (n = 10; n < 15; n += 2) {
if (hb[n] && !hb[n + 1])
mode = (mode << 1) | 1;
else if (!hb[n] && hb[n + 1])
mode = (mode << 1) | 0;
else {
aprint_debug_dev(sc->sc_dev, "bad RC6 mode bits\n");
return EIO;
}
}
data = 0;
for (n = 20; n < sc->sc_rc6_nhb; n += 2) {
if (hb[n] && !hb[n + 1])
data = (data << 1) | 1;
else if (!hb[n] && hb[n + 1])
data = (data << 1) | 0;
else {
aprint_debug_dev(sc->sc_dev, "bad RC6 data bits\n");
return EIO;
}
}
sc->sc_rc6_nhb = 0;
return uiomove(&data, sizeof(data), uio);
}
static int
irmce_process(struct irmce_softc *sc, uint8_t *buf, size_t buflen,
struct uio *uio)
{
uint8_t *p = buf;
uint8_t data, cmd;
int error;
while (p - buf < (ssize_t)buflen) {
switch (sc->sc_ir_state) {
case IRMCE_STATE_HEADER:
sc->sc_ir_header = data = *p++;
if ((data & 0xe0) == 0x80 && (data & 0x1f) != 0x1f) {
sc->sc_ir_bufused = 0;
sc->sc_ir_resid = data & 0x1f;
sc->sc_ir_state = IRMCE_STATE_IRDATA;
if (sc->sc_ir_resid > sizeof(sc->sc_ir_buf))
return EIO;
if (sc->sc_ir_resid == 0)
sc->sc_ir_state = IRMCE_STATE_HEADER;
} else {
sc->sc_ir_state = IRMCE_STATE_CMDHEADER;
}
break;
case IRMCE_STATE_CMDHEADER:
cmd = *p++;
data = sc->sc_ir_header;
if (data == 0x00 && cmd == 0x9f)
sc->sc_ir_resid = 1;
else if (data == 0xff && cmd == 0x0b)
sc->sc_ir_resid = 2;
else if (data == 0x9f) {
if (cmd == 0x04 || cmd == 0x06 ||
cmd == 0x0c || cmd == 0x15) {
sc->sc_ir_resid = 2;
} else if (cmd == 0x01 || cmd == 0x08 ||
cmd == 0x14) {
sc->sc_ir_resid = 1;
}
}
if (sc->sc_ir_resid > 0)
sc->sc_ir_state = IRMCE_STATE_CMDDATA;
else
sc->sc_ir_state = IRMCE_STATE_HEADER;
break;
case IRMCE_STATE_IRDATA:
sc->sc_ir_resid--;
sc->sc_ir_buf[sc->sc_ir_bufused++] = *p;
p++;
if (sc->sc_ir_resid == 0) {
sc->sc_ir_state = IRMCE_STATE_HEADER;
error = irmce_rc6_decode(sc,
sc->sc_ir_buf, sc->sc_ir_bufused, uio);
if (error)
sc->sc_rc6_nhb = 0;
}
break;
case IRMCE_STATE_CMDDATA:
p++;
sc->sc_ir_resid--;
if (sc->sc_ir_resid == 0)
sc->sc_ir_state = IRMCE_STATE_HEADER;
break;
}
}
return 0;
}
static int
irmce_read(void *priv, struct uio *uio, int flag)
{
struct irmce_softc *sc = priv;
usbd_status err;
uint32_t rlen;
int error = 0;
while (uio->uio_resid > 0) {
rlen = sc->sc_bulkin_maxpktsize;
err = usbd_bulk_transfer(sc->sc_bulkin_xfer,
sc->sc_bulkin_pipe, USBD_SHORT_XFER_OK,
USBD_DEFAULT_TIMEOUT, sc->sc_bulkin_buffer, &rlen);
if (err != USBD_NORMAL_COMPLETION) {
if (err == USBD_INTERRUPTED)
return EINTR;
else if (err == USBD_TIMEOUT)
continue;
else
return EIO;
}
if (sc->sc_raw) {
error = uiomove(sc->sc_bulkin_buffer, rlen, uio);
break;
} else {
error = irmce_process(sc, sc->sc_bulkin_buffer,
rlen, uio);
if (error)
break;
}
}
return error;
}
static int
irmce_write(void *priv, struct uio *uio, int flag)
{
return EIO;
}
static int
irmce_setparams(void *priv, struct cir_params *params)
{
struct irmce_softc *sc = priv;
if (params->raw > 1)
return EINVAL;
sc->sc_raw = params->raw;
return 0;
}
MODULE(MODULE_CLASS_DRIVER, irmce, NULL);
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
irmce_modcmd(modcmd_t cmd, void *opaque)
{
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
return config_init_component(cfdriver_ioconf_irmce,
cfattach_ioconf_irmce, cfdata_ioconf_irmce);
#else
return 0;
#endif
case MODULE_CMD_FINI:
#ifdef _MODULE
return config_fini_component(cfdriver_ioconf_irmce,
cfattach_ioconf_irmce, cfdata_ioconf_irmce);
#else
return 0;
#endif
default:
return ENOTTY;
}
}
|