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