1 /* $NetBSD: ustir.c,v 1.51 2022/03/12 21:15:25 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Sainty <dsainty@NetBSD.org>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.51 2022/03/12 21:15:25 riastradh Exp $");
34
35 #ifdef _KERNEL_OPT
36 #include "opt_usb.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/kmem.h>
44 #include <sys/conf.h>
45 #include <sys/file.h>
46 #include <sys/poll.h>
47 #include <sys/select.h>
48 #include <sys/proc.h>
49 #include <sys/kthread.h>
50
51 #ifdef USTIR_DEBUG_IOCTLS
52 #include <sys/ioctl.h>
53 #include <dev/usb/ustir.h>
54 #endif
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdevs.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/ustirreg.h>
61
62 #include <dev/ir/ir.h>
63 #include <dev/ir/irdaio.h>
64 #include <dev/ir/irframevar.h>
65 #include <dev/ir/sir.h>
66
67 #ifdef USTIR_DEBUG
68 #define DPRINTFN(n,x) if (ustirdebug>(n)) printf x
69 int ustirdebug = 0;
70 #else
71 #define DPRINTFN(n,x)
72 #endif
73
74 /* Max size with framing. */
75 #define MAX_USTIR_OUTPUT_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + STIR_OUTPUT_HEADER_SIZE + 4)
76
77 #define USTIR_NSPEEDS 9
78 struct ustir_speedrec {
79 unsigned int speed;
80 unsigned int config;
81 };
82
83 Static struct ustir_speedrec const ustir_speeds[USTIR_NSPEEDS] = {
84 { 4000000, STIR_BRMODE_4000000 },
85 { 1152000, STIR_BRMODE_1152000 },
86 { 576000, STIR_BRMODE_576000 },
87 { 115200, STIR_BRMODE_115200 },
88 { 57600, STIR_BRMODE_57600 },
89 { 38400, STIR_BRMODE_38400 },
90 { 19200, STIR_BRMODE_19200 },
91 { 9600, STIR_BRMODE_9600 },
92 { 2400, STIR_BRMODE_2400 }
93 };
94
95 struct ustir_softc {
96 device_t sc_dev;
97 struct usbd_device *sc_udev;
98 struct usbd_interface *sc_iface;
99 enum {
100 USTIR_INIT_NONE,
101 USTIR_INIT_INITED
102 } sc_init_state;
103
104 uint8_t *sc_ur_buf; /* Unencapsulated frame */
105 u_int sc_ur_framelen;
106
107 uint8_t *sc_rd_buf; /* Raw incoming data stream */
108 size_t sc_rd_index;
109 int sc_rd_addr;
110 struct usbd_pipe *sc_rd_pipe;
111 struct usbd_xfer *sc_rd_xfer;
112 u_int sc_rd_count;
113 int sc_rd_readinprogress;
114 u_int sc_rd_expectdataticks;
115 u_char sc_rd_err;
116 struct framestate sc_framestate;
117 struct lwp *sc_thread;
118 struct selinfo sc_rd_sel;
119
120 uint8_t *sc_wr_buf;
121 int sc_wr_addr;
122 int sc_wr_stalewrite;
123 struct usbd_xfer *sc_wr_xfer;
124 struct usbd_pipe *sc_wr_pipe;
125 struct selinfo sc_wr_sel;
126
127 enum {
128 udir_input, /* Receiving data */
129 udir_output, /* Transmitting data */
130 udir_stalled, /* Error preventing data flow */
131 udir_idle /* Neither receiving nor transmitting */
132 } sc_direction;
133
134 struct ustir_speedrec const *sc_speedrec;
135
136 device_t sc_child;
137 struct irda_params sc_params;
138
139 int sc_refcnt;
140 char sc_closing;
141 char sc_dying;
142 };
143
144 /* True if we cannot safely read data from the device */
145 #define USTIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0)
146
147 #define USTIR_WR_TIMEOUT 200
148
149 Static int ustir_open(void *, int, int, struct lwp *);
150 Static int ustir_close(void *, int, int, struct lwp *);
151 Static int ustir_read(void *, struct uio *, int);
152 Static int ustir_write(void *, struct uio *, int);
153 Static int ustir_set_params(void *, struct irda_params *);
154 Static int ustir_get_speeds(void *, int *);
155 Static int ustir_get_turnarounds(void *, int *);
156 Static int ustir_poll(void *, int, struct lwp *);
157 Static int ustir_kqfilter(void *, struct knote *);
158
159 #ifdef USTIR_DEBUG_IOCTLS
160 Static int ustir_ioctl(void *, u_long, void *, int, struct lwp *);
161 #endif
162
163 Static struct irframe_methods const ustir_methods = {
164 ustir_open, ustir_close, ustir_read, ustir_write, ustir_poll,
165 ustir_kqfilter, ustir_set_params, ustir_get_speeds,
166 ustir_get_turnarounds,
167 #ifdef USTIR_DEBUG_IOCTLS
168 ustir_ioctl
169 #endif
170 };
171
172 Static void ustir_rd_cb(struct usbd_xfer *, void *, usbd_status);
173 Static usbd_status ustir_start_read(struct ustir_softc *);
174 Static void ustir_periodic(struct ustir_softc *);
175 Static void ustir_thread(void *);
176
177 static usbd_status
ustir_read_reg(struct ustir_softc * sc,unsigned int reg,uint8_t * data)178 ustir_read_reg(struct ustir_softc *sc, unsigned int reg, uint8_t *data)
179 {
180 usb_device_request_t req;
181
182 req.bmRequestType = UT_READ_VENDOR_DEVICE;
183 req.bRequest = STIR_CMD_READMULTIREG;
184 USETW(req.wValue, 0);
185 USETW(req.wIndex, reg);
186 USETW(req.wLength, 1);
187
188 return usbd_do_request(sc->sc_udev, &req, data);
189 }
190
191 static usbd_status
ustir_write_reg(struct ustir_softc * sc,unsigned int reg,uint8_t data)192 ustir_write_reg(struct ustir_softc *sc, unsigned int reg, uint8_t data)
193 {
194 usb_device_request_t req;
195
196 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
197 req.bRequest = STIR_CMD_WRITESINGLEREG;
198 USETW(req.wValue, data);
199 USETW(req.wIndex, reg);
200 USETW(req.wLength, 0);
201
202 return usbd_do_request(sc->sc_udev, &req, NULL);
203 }
204
205 #ifdef USTIR_DEBUG
206 static void
ustir_dumpdata(uint8_t const * data,size_t dlen,char const * desc)207 ustir_dumpdata(uint8_t const *data, size_t dlen, char const *desc)
208 {
209 size_t bdindex;
210 printf("%s: (%lx)", desc, (unsigned long)dlen);
211 for (bdindex = 0; bdindex < dlen; bdindex++)
212 printf(" %02x", (unsigned int)data[bdindex]);
213 printf("\n");
214 }
215 #endif
216
217 static int ustir_match(device_t, cfdata_t, void *);
218 static void ustir_attach(device_t, device_t, void *);
219 static void ustir_childdet(device_t, device_t);
220 static int ustir_detach(device_t, int);
221 static int ustir_activate(device_t, enum devact);
222
223 CFATTACH_DECL2_NEW(ustir, sizeof(struct ustir_softc), ustir_match,
224 ustir_attach, ustir_detach, ustir_activate, NULL, ustir_childdet);
225
226 static int
ustir_match(device_t parent,cfdata_t match,void * aux)227 ustir_match(device_t parent, cfdata_t match, void *aux)
228 {
229 struct usb_attach_arg *uaa = aux;
230
231 DPRINTFN(50,("ustir_match\n"));
232
233 if (uaa->uaa_vendor == USB_VENDOR_SIGMATEL &&
234 uaa->uaa_product == USB_PRODUCT_SIGMATEL_IRDA)
235 return UMATCH_VENDOR_PRODUCT;
236
237 return UMATCH_NONE;
238 }
239
240 static void
ustir_attach(device_t parent,device_t self,void * aux)241 ustir_attach(device_t parent, device_t self, void *aux)
242 {
243 struct ustir_softc *sc = device_private(self);
244 struct usb_attach_arg *uaa = aux;
245 struct usbd_device *dev = uaa->uaa_device;
246 struct usbd_interface *iface;
247 char *devinfop;
248 usb_endpoint_descriptor_t *ed;
249 uint8_t epcount;
250 int i;
251 struct ir_attach_args ia;
252
253 DPRINTFN(10,("ustir_attach: sc=%p\n", sc));
254
255 sc->sc_dev = self;
256 sc->sc_init_state = USTIR_INIT_NONE;
257
258 aprint_naive("\n");
259 aprint_normal("\n");
260
261 devinfop = usbd_devinfo_alloc(dev, 0);
262 aprint_normal_dev(self, "%s\n", devinfop);
263 usbd_devinfo_free(devinfop);
264
265 if (usbd_set_config_index(dev, 0, 1)
266 || usbd_device2interface_handle(dev, 0, &iface)) {
267 aprint_error_dev(self, "Configuration failed\n");
268 return;
269 }
270
271 sc->sc_udev = dev;
272 sc->sc_iface = iface;
273
274 epcount = 0;
275 (void)usbd_endpoint_count(iface, &epcount);
276
277 sc->sc_rd_addr = -1;
278 sc->sc_wr_addr = -1;
279 for (i = 0; i < epcount; i++) {
280 ed = usbd_interface2endpoint_descriptor(iface, i);
281 if (ed == NULL) {
282 aprint_error_dev(self, "couldn't get ep %d\n", i);
283 return;
284 }
285 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
286 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
287 sc->sc_rd_addr = ed->bEndpointAddress;
288 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
289 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
290 sc->sc_wr_addr = ed->bEndpointAddress;
291 }
292 }
293 if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
294 aprint_error_dev(self, "missing endpoint\n");
295 return;
296 }
297
298 DPRINTFN(10, ("ustir_attach: %p\n", sc->sc_udev));
299
300 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
301
302 ia.ia_type = IR_TYPE_IRFRAME;
303 ia.ia_methods = &ustir_methods;
304 ia.ia_handle = sc;
305
306 sc->sc_child = config_found(self, &ia, ir_print, CFARGS_NONE);
307 selinit(&sc->sc_rd_sel);
308 selinit(&sc->sc_wr_sel);
309 sc->sc_init_state = USTIR_INIT_INITED;
310
311 return;
312 }
313
314 static void
ustir_childdet(device_t self,device_t child)315 ustir_childdet(device_t self, device_t child)
316 {
317 struct ustir_softc *sc = device_private(self);
318
319 KASSERT(sc->sc_child == child);
320 sc->sc_child = NULL;
321 }
322
323 static int
ustir_detach(device_t self,int flags)324 ustir_detach(device_t self, int flags)
325 {
326 struct ustir_softc *sc = device_private(self);
327 int s;
328 int rv = 0;
329
330 DPRINTFN(0, ("ustir_detach: sc=%p flags=%d\n", sc, flags));
331
332 sc->sc_closing = sc->sc_dying = 1;
333
334 wakeup(&sc->sc_thread);
335
336 while (sc->sc_thread != NULL)
337 tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
338
339 /* Abort all pipes. Causes processes waiting for transfer to wake. */
340 if (sc->sc_rd_pipe != NULL) {
341 usbd_abort_pipe(sc->sc_rd_pipe);
342 }
343 if (sc->sc_wr_pipe != NULL) {
344 usbd_abort_pipe(sc->sc_wr_pipe);
345 }
346 if (sc->sc_rd_xfer != NULL) {
347 usbd_destroy_xfer(sc->sc_rd_xfer);
348 sc->sc_rd_xfer = NULL;
349 sc->sc_rd_buf = NULL;
350 }
351 if (sc->sc_wr_xfer != NULL) {
352 usbd_destroy_xfer(sc->sc_wr_xfer);
353 sc->sc_wr_xfer = NULL;
354 sc->sc_wr_buf = NULL;
355 }
356 if (sc->sc_rd_pipe != NULL) {
357 usbd_close_pipe(sc->sc_rd_pipe);
358 sc->sc_rd_pipe = NULL;
359 }
360 if (sc->sc_wr_pipe != NULL) {
361 usbd_close_pipe(sc->sc_wr_pipe);
362 sc->sc_wr_pipe = NULL;
363 }
364 wakeup(&sc->sc_ur_framelen);
365 wakeup(&sc->sc_wr_buf);
366
367 s = splusb();
368 if (--sc->sc_refcnt >= 0) {
369 /* Wait for processes to go away. */
370 usb_detach_waitold(sc->sc_dev);
371 }
372 splx(s);
373
374 if (sc->sc_child != NULL)
375 rv = config_detach(sc->sc_child, flags);
376
377 if (sc->sc_init_state >= USTIR_INIT_INITED) {
378 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
379 sc->sc_dev);
380 seldestroy(&sc->sc_rd_sel);
381 seldestroy(&sc->sc_wr_sel);
382 }
383
384 return rv;
385 }
386
387 /* Returns 0 if more data required, 1 if a complete frame was extracted */
388 static int
deframe_rd_ur(struct ustir_softc * sc)389 deframe_rd_ur(struct ustir_softc *sc)
390 {
391 while (sc->sc_rd_index < sc->sc_rd_count) {
392 uint8_t const *buf;
393 size_t buflen;
394 enum frameresult fresult;
395
396 buf = &sc->sc_rd_buf[sc->sc_rd_index];
397 buflen = sc->sc_rd_count - sc->sc_rd_index;
398
399 fresult = deframe_process(&sc->sc_framestate, &buf, &buflen);
400
401 sc->sc_rd_index = sc->sc_rd_count - buflen;
402
403 DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult));
404
405 switch (fresult) {
406 case FR_IDLE:
407 case FR_INPROGRESS:
408 case FR_FRAMEBADFCS:
409 case FR_FRAMEMALFORMED:
410 case FR_BUFFEROVERRUN:
411 break;
412 case FR_FRAMEOK:
413 sc->sc_ur_framelen = sc->sc_framestate.bufindex;
414 wakeup(&sc->sc_ur_framelen); /* XXX should use flag */
415 selnotify(&sc->sc_rd_sel, 0, 0);
416 return 1;
417 }
418 }
419
420 /* Reset indices into USB-side buffer */
421 sc->sc_rd_index = sc->sc_rd_count = 0;
422
423 return 0;
424 }
425
426 /*
427 * Direction transitions:
428 *
429 * ustir_periodic() can switch the direction from:
430 *
431 * output -> idle
432 * output -> stalled
433 * stalled -> idle
434 * idle -> input
435 *
436 * ustir_rd_cb() can switch the direction from:
437 *
438 * input -> stalled
439 * input -> idle
440 *
441 * ustir_write() can switch the direction from:
442 *
443 * idle -> output
444 */
445 Static void
ustir_periodic(struct ustir_softc * sc)446 ustir_periodic(struct ustir_softc *sc)
447 {
448 DPRINTFN(60, ("%s: direction = %d\n",
449 __func__, sc->sc_direction));
450
451 if (sc->sc_direction == udir_output ||
452 sc->sc_direction == udir_stalled) {
453 usbd_status err;
454 uint8_t regval;
455
456 DPRINTFN(60, ("%s: reading status register\n",
457 __func__));
458
459 err = ustir_read_reg(sc, STIR_REG_STATUS,
460 ®val);
461 if (err != USBD_NORMAL_COMPLETION) {
462 aprint_error_dev(sc->sc_dev,
463 "status register read failed: %s\n",
464 usbd_errstr(err));
465 } else {
466 DPRINTFN(10, ("%s: status register = %#x\n",
467 __func__,
468 (unsigned int)regval));
469 if (sc->sc_direction == udir_output &&
470 !(regval & STIR_RSTATUS_FFDIR))
471 /* Output has completed */
472 sc->sc_direction = udir_idle;
473 if (regval & STIR_RSTATUS_FFOVER) {
474 /*
475 * On an overrun the FIFO hangs, and
476 * any data bulk transfers will stall.
477 * Reset the FIFO.
478 */
479 sc->sc_direction = udir_stalled;
480
481 DPRINTFN(10, ("%s: clearing FIFO error\n",
482 __func__));
483
484 err = ustir_write_reg(sc, STIR_REG_STATUS,
485 STIR_RSTATUS_FFCLR);
486 /* XXX if we fail partway through
487 * this, we may not recover? */
488 if (err == USBD_NORMAL_COMPLETION)
489 err = ustir_write_reg(sc,
490 STIR_REG_STATUS,
491 0);
492 if (err != USBD_NORMAL_COMPLETION) {
493 aprint_error_dev(sc->sc_dev,
494 "FIFO reset failed: %s\n",
495 usbd_errstr(err));
496 } else {
497 /* FIFO reset */
498 sc->sc_direction = udir_idle;
499 }
500 }
501 }
502 }
503
504 if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) {
505 /*
506 * In a stale write case, we need to check if the
507 * write has completed. Once that has happened, the
508 * write is no longer stale.
509 *
510 * But note that we may immediately start a read poll...
511 */
512 sc->sc_wr_stalewrite = 0;
513 wakeup(&sc->sc_wr_buf);
514 }
515
516 if (!sc->sc_rd_readinprogress &&
517 (sc->sc_direction == udir_idle ||
518 sc->sc_direction == udir_input))
519 /* Do a read poll if appropriate... */
520 ustir_start_read(sc);
521 }
522
523 Static void
ustir_thread(void * arg)524 ustir_thread(void *arg)
525 {
526 struct ustir_softc *sc = arg;
527
528 DPRINTFN(20, ("%s: starting polling thread\n", __func__));
529
530 while (!sc->sc_closing) {
531 if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
532 ustir_periodic(sc);
533
534 if (!sc->sc_closing) {
535 int error;
536 error = tsleep(&sc->sc_thread, PWAIT,
537 "ustir", hz / 10);
538 if (error == EWOULDBLOCK &&
539 sc->sc_rd_expectdataticks > 0)
540 /*
541 * After a timeout decrement the tick
542 * counter within which time we expect
543 * data to arrive if we are receiving
544 * data...
545 */
546 sc->sc_rd_expectdataticks--;
547 }
548 }
549
550 DPRINTFN(20, ("%s: exiting polling thread\n", __func__));
551
552 sc->sc_thread = NULL;
553
554 wakeup(&sc->sc_closing);
555
556 if (--sc->sc_refcnt < 0)
557 usb_detach_wakeupold(sc->sc_dev);
558
559 kthread_exit(0);
560 }
561
562 Static void
ustir_rd_cb(struct usbd_xfer * xfer,void * priv,usbd_status status)563 ustir_rd_cb(struct usbd_xfer *xfer, void *priv,
564 usbd_status status)
565 {
566 struct ustir_softc *sc = priv;
567 uint32_t size;
568
569 DPRINTFN(60, ("%s: sc=%p\n", __func__, sc));
570
571 /* Read is no longer in progress */
572 sc->sc_rd_readinprogress = 0;
573
574 if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */
575 return;
576 if (status) {
577 size = 0;
578 sc->sc_rd_err = 1;
579
580 if (sc->sc_direction == udir_input ||
581 sc->sc_direction == udir_idle) {
582 /*
583 * Receive error, probably need to clear error
584 * condition.
585 */
586 sc->sc_direction = udir_stalled;
587 }
588 } else {
589 usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
590 }
591
592 sc->sc_rd_index = 0;
593 sc->sc_rd_count = size;
594
595 DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60),
596 ("%s: sc=%p size=%u, err=%d\n", __func__,
597 sc, size, sc->sc_rd_err));
598
599 #ifdef USTIR_DEBUG
600 if (ustirdebug >= 20 && size > 0)
601 ustir_dumpdata(sc->sc_rd_buf, size, __func__);
602 #endif
603
604 if (!deframe_rd_ur(sc)) {
605 if (!deframe_isclear(&sc->sc_framestate) && size == 0 &&
606 sc->sc_rd_expectdataticks == 0) {
607 /*
608 * Expected data, but didn't get it
609 * within expected time...
610 */
611 DPRINTFN(5,("%s: incoming packet timeout\n",
612 __func__));
613 deframe_clear(&sc->sc_framestate);
614 } else if (size > 0) {
615 /*
616 * If we also received actual data, reset the
617 * data read timeout and wake up the possibly
618 * sleeping thread...
619 */
620 sc->sc_rd_expectdataticks = 2;
621 wakeup(&sc->sc_thread);
622 }
623 }
624
625 /*
626 * Check if incoming data has stopped, or that we cannot
627 * safely read any more data. In the case of the latter we
628 * must switch to idle so that a write will not block...
629 */
630 if (sc->sc_direction == udir_input &&
631 ((size == 0 && sc->sc_rd_expectdataticks == 0) ||
632 USTIR_BLOCK_RX_DATA(sc))) {
633 DPRINTFN(8,("%s: idling on packet timeout, "
634 "complete frame, or no data\n", __func__));
635 sc->sc_direction = udir_idle;
636
637 /* Wake up for possible output */
638 wakeup(&sc->sc_wr_buf);
639 selnotify(&sc->sc_wr_sel, 0, 0);
640 }
641 }
642
643 Static usbd_status
ustir_start_read(struct ustir_softc * sc)644 ustir_start_read(struct ustir_softc *sc)
645 {
646 usbd_status err;
647
648 DPRINTFN(60,("%s: sc=%p, size=%d\n", __func__, sc,
649 sc->sc_params.maxsize));
650
651 if (sc->sc_dying)
652 return USBD_IOERROR;
653
654 if (USTIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) {
655 /*
656 * Can't start reading just yet. Since we aren't
657 * going to start a read, have to switch direction to
658 * idle.
659 */
660 sc->sc_direction = udir_idle;
661 return USBD_NORMAL_COMPLETION;
662 }
663
664 /* Starting a read... */
665 sc->sc_rd_readinprogress = 1;
666 sc->sc_direction = udir_input;
667
668 if (sc->sc_rd_err) {
669 sc->sc_rd_err = 0;
670 DPRINTFN(0, ("%s: clear stall\n", __func__));
671 usbd_clear_endpoint_stall(sc->sc_rd_pipe);
672 }
673
674 usbd_setup_xfer(sc->sc_rd_xfer, sc, sc->sc_rd_buf,
675 sc->sc_params.maxsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
676 ustir_rd_cb);
677 err = usbd_transfer(sc->sc_rd_xfer);
678 if (err != USBD_IN_PROGRESS) {
679 DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err));
680 return err;
681 }
682 return USBD_NORMAL_COMPLETION;
683 }
684
685 Static int
ustir_activate(device_t self,enum devact act)686 ustir_activate(device_t self, enum devact act)
687 {
688 struct ustir_softc *sc = device_private(self);
689
690 switch (act) {
691 case DVACT_DEACTIVATE:
692 sc->sc_dying = 1;
693 return 0;
694 default:
695 return EOPNOTSUPP;
696 }
697 }
698
699 /* ARGSUSED */
700 Static int
ustir_open(void * h,int flag,int mode,struct lwp * l)701 ustir_open(void *h, int flag, int mode,
702 struct lwp *l)
703 {
704 struct ustir_softc *sc = h;
705 int error;
706 usbd_status err;
707
708 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
709
710 err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
711 if (err != USBD_NORMAL_COMPLETION) {
712 error = EIO;
713 goto bad1;
714 }
715 err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
716 if (err != USBD_NORMAL_COMPLETION) {
717 error = EIO;
718 goto bad2;
719 }
720 error = usbd_create_xfer(sc->sc_rd_pipe, IRDA_MAX_FRAME_SIZE,
721 0, 0, &sc->sc_rd_xfer);
722 if (error)
723 goto bad3;
724 sc->sc_rd_buf = usbd_get_buffer(sc->sc_rd_xfer);
725
726 error = usbd_create_xfer(sc->sc_wr_pipe,
727 IRDA_MAX_FRAME_SIZE + STIR_OUTPUT_HEADER_SIZE,
728 USBD_FORCE_SHORT_XFER, 0, &sc->sc_wr_xfer);
729 if (error)
730 goto bad4;
731 sc->sc_wr_buf = usbd_get_buffer(sc->sc_wr_xfer);
732
733 sc->sc_ur_buf = kmem_alloc(IRDA_MAX_FRAME_SIZE, KM_SLEEP);
734 sc->sc_rd_index = sc->sc_rd_count = 0;
735 sc->sc_closing = 0;
736 sc->sc_rd_readinprogress = 0;
737 sc->sc_rd_expectdataticks = 0;
738 sc->sc_ur_framelen = 0;
739 sc->sc_rd_err = 0;
740 sc->sc_wr_stalewrite = 0;
741 sc->sc_speedrec = NULL;
742 sc->sc_direction = udir_idle;
743 sc->sc_params.speed = 0;
744 sc->sc_params.ebofs = 0;
745 sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
746
747 deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
748
749 /* Increment reference for thread */
750 sc->sc_refcnt++;
751
752 error = kthread_create(PRI_NONE, 0, NULL, ustir_thread, sc,
753 &sc->sc_thread, "%s", device_xname(sc->sc_dev));
754 if (error) {
755 sc->sc_refcnt--;
756 goto bad5;
757 }
758
759 return 0;
760
761 bad5:
762 usbd_destroy_xfer(sc->sc_wr_xfer);
763 sc->sc_wr_xfer = NULL;
764 bad4:
765 usbd_destroy_xfer(sc->sc_rd_xfer);
766 sc->sc_rd_xfer = NULL;
767 bad3:
768 usbd_close_pipe(sc->sc_wr_pipe);
769 sc->sc_wr_pipe = NULL;
770 bad2:
771 usbd_close_pipe(sc->sc_rd_pipe);
772 sc->sc_rd_pipe = NULL;
773 bad1:
774 return error;
775 }
776
777 /* ARGSUSED */
778 Static int
ustir_close(void * h,int flag,int mode,struct lwp * l)779 ustir_close(void *h, int flag, int mode,
780 struct lwp *l)
781 {
782 struct ustir_softc *sc = h;
783
784 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
785
786 sc->sc_refcnt++;
787
788 sc->sc_rd_readinprogress = 1;
789 sc->sc_closing = 1;
790
791 wakeup(&sc->sc_thread);
792
793 while (sc->sc_thread != NULL)
794 tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
795
796 if (sc->sc_rd_pipe != NULL) {
797 usbd_abort_pipe(sc->sc_rd_pipe);
798 sc->sc_rd_pipe = NULL;
799 }
800 if (sc->sc_wr_pipe != NULL) {
801 usbd_abort_pipe(sc->sc_wr_pipe);
802 sc->sc_wr_pipe = NULL;
803 }
804 if (sc->sc_rd_xfer != NULL) {
805 usbd_destroy_xfer(sc->sc_rd_xfer);
806 sc->sc_rd_xfer = NULL;
807 sc->sc_rd_buf = NULL;
808 }
809 if (sc->sc_wr_xfer != NULL) {
810 usbd_destroy_xfer(sc->sc_wr_xfer);
811 sc->sc_wr_xfer = NULL;
812 sc->sc_wr_buf = NULL;
813 }
814 if (sc->sc_ur_buf != NULL) {
815 kmem_free(sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
816 sc->sc_ur_buf = NULL;
817 }
818 if (sc->sc_rd_pipe != NULL) {
819 usbd_close_pipe(sc->sc_rd_pipe);
820 sc->sc_rd_pipe = NULL;
821 }
822 if (sc->sc_wr_pipe != NULL) {
823 usbd_close_pipe(sc->sc_wr_pipe);
824 sc->sc_wr_pipe = NULL;
825 }
826
827 if (--sc->sc_refcnt < 0)
828 usb_detach_wakeupold(sc->sc_dev);
829
830 return 0;
831 }
832
833 /* ARGSUSED */
834 Static int
ustir_read(void * h,struct uio * uio,int flag)835 ustir_read(void *h, struct uio *uio, int flag)
836 {
837 struct ustir_softc *sc = h;
838 int s;
839 int error;
840 u_int uframelen;
841
842 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
843
844 if (sc->sc_dying)
845 return EIO;
846
847 #ifdef DIAGNOSTIC
848 if (sc->sc_rd_buf == NULL)
849 return EINVAL;
850 #endif
851
852 sc->sc_refcnt++;
853
854 if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
855 /* Possibly wake up polling thread */
856 wakeup(&sc->sc_thread);
857
858 do {
859 s = splusb();
860 while (sc->sc_ur_framelen == 0) {
861 DPRINTFN(5,("%s: calling tsleep()\n", __func__));
862 error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH,
863 "usirrd", 0);
864 if (sc->sc_dying)
865 error = EIO;
866 if (error) {
867 splx(s);
868 DPRINTFN(0, ("%s: tsleep() = %d\n",
869 __func__, error));
870 goto ret;
871 }
872 }
873 splx(s);
874
875 uframelen = sc->sc_ur_framelen;
876 DPRINTFN(1,("%s: sc=%p framelen=%u, hdr=0x%02x\n",
877 __func__, sc, uframelen, sc->sc_ur_buf[0]));
878 if (uframelen > uio->uio_resid)
879 error = EINVAL;
880 else
881 error = uiomove(sc->sc_ur_buf, uframelen, uio);
882 sc->sc_ur_framelen = 0;
883
884 if (!deframe_rd_ur(sc) && uframelen > 0) {
885 /*
886 * Need to wait for another read to obtain a
887 * complete frame... If we also obtained
888 * actual data, wake up the possibly sleeping
889 * thread immediately...
890 */
891 wakeup(&sc->sc_thread);
892 }
893 } while (uframelen == 0);
894
895 DPRINTFN(1,("%s: return %d\n", __func__, error));
896
897 ret:
898 if (--sc->sc_refcnt < 0)
899 usb_detach_wakeupold(sc->sc_dev);
900 return error;
901 }
902
903 /* ARGSUSED */
904 Static int
ustir_write(void * h,struct uio * uio,int flag)905 ustir_write(void *h, struct uio *uio, int flag)
906 {
907 struct ustir_softc *sc = h;
908 usbd_status err;
909 uint32_t wrlen;
910 int error, sirlength;
911 uint8_t *wrbuf;
912 int s;
913
914 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
915
916 if (sc->sc_dying)
917 return EIO;
918
919 #ifdef DIAGNOSTIC
920 if (sc->sc_wr_buf == NULL)
921 return EINVAL;
922 #endif
923
924 wrlen = uio->uio_resid;
925 if (wrlen > sc->sc_params.maxsize)
926 return EINVAL;
927
928 sc->sc_refcnt++;
929
930 if (!USTIR_BLOCK_RX_DATA(sc)) {
931 /*
932 * If reads are not blocked, determine what action we
933 * should potentially take...
934 */
935 if (sc->sc_direction == udir_output) {
936 /*
937 * If the last operation was an output, wait for the
938 * polling thread to check for incoming data.
939 */
940 sc->sc_wr_stalewrite = 1;
941 wakeup(&sc->sc_thread);
942 } else if (!sc->sc_rd_readinprogress &&
943 (sc->sc_direction == udir_idle ||
944 sc->sc_direction == udir_input)) {
945 /* If idle, check for input before outputting */
946 ustir_start_read(sc);
947 }
948 }
949
950 s = splusb();
951 while (sc->sc_wr_stalewrite ||
952 (sc->sc_direction != udir_output &&
953 sc->sc_direction != udir_idle)) {
954 DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, "
955 "calling tsleep()\n", __func__,
956 sc, sc->sc_wr_stalewrite, sc->sc_direction));
957 error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH,
958 "usirwr", 0);
959 if (sc->sc_dying)
960 error = EIO;
961 if (error) {
962 splx(s);
963 DPRINTFN(0, ("%s: tsleep() = %d\n", __func__,
964 error));
965 goto ret;
966 }
967 }
968 splx(s);
969
970 wrbuf = sc->sc_wr_buf;
971
972 /* Build header */
973 wrbuf[0] = STIR_OUTPUT_HEADER_BYTE0;
974 wrbuf[1] = STIR_OUTPUT_HEADER_BYTE1;
975
976 sirlength = irda_sir_frame(&wrbuf[STIR_OUTPUT_HEADER_SIZE],
977 MAX_USTIR_OUTPUT_FRAME -
978 STIR_OUTPUT_HEADER_SIZE,
979 uio, sc->sc_params.ebofs);
980 if (sirlength < 0) {
981 error = -sirlength;
982 } else {
983 uint32_t btlen;
984
985 DPRINTFN(1, ("%s: transfer %u bytes\n", __func__,
986 (unsigned int)wrlen));
987
988 wrbuf[2] = sirlength & 0xff;
989 wrbuf[3] = (sirlength >> 8) & 0xff;
990
991 btlen = STIR_OUTPUT_HEADER_SIZE + sirlength;
992
993 sc->sc_direction = udir_output;
994
995 #ifdef USTIR_DEBUG
996 if (ustirdebug >= 20)
997 ustir_dumpdata(wrbuf, btlen, __func__);
998 #endif
999
1000 err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
1001 USBD_FORCE_SHORT_XFER, USTIR_WR_TIMEOUT, wrbuf, &btlen);
1002 DPRINTFN(2, ("%s: err=%d\n", __func__, err));
1003 if (err != USBD_NORMAL_COMPLETION) {
1004 if (err == USBD_INTERRUPTED)
1005 error = EINTR;
1006 else if (err == USBD_TIMEOUT)
1007 error = ETIMEDOUT;
1008 else
1009 error = EIO;
1010 } else {
1011 error = 0;
1012 }
1013 }
1014
1015 ret:
1016 if (--sc->sc_refcnt < 0)
1017 usb_detach_wakeupold(sc->sc_dev);
1018
1019 DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
1020 return error;
1021 }
1022
1023 Static int
ustir_poll(void * h,int events,struct lwp * l)1024 ustir_poll(void *h, int events, struct lwp *l)
1025 {
1026 struct ustir_softc *sc = h;
1027 int revents = 0;
1028
1029 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
1030
1031 if (events & (POLLOUT | POLLWRNORM)) {
1032 if (sc->sc_direction != udir_input) {
1033 revents |= events & (POLLOUT | POLLWRNORM);
1034 } else {
1035 DPRINTFN(2,("%s: recording write select\n",
1036 __func__));
1037 selrecord(l, &sc->sc_wr_sel);
1038 }
1039 }
1040
1041 if (events & (POLLIN | POLLRDNORM)) {
1042 if (sc->sc_ur_framelen != 0) {
1043 DPRINTFN(2,("%s: have data\n", __func__));
1044 revents |= events & (POLLIN | POLLRDNORM);
1045 } else {
1046 DPRINTFN(2,("%s: recording read select\n",
1047 __func__));
1048 selrecord(l, &sc->sc_rd_sel);
1049 }
1050 }
1051
1052 return revents;
1053 }
1054
1055 static void
filt_ustirrdetach(struct knote * kn)1056 filt_ustirrdetach(struct knote *kn)
1057 {
1058 struct ustir_softc *sc = kn->kn_hook;
1059 int s;
1060
1061 s = splusb();
1062 selremove_knote(&sc->sc_rd_sel, kn);
1063 splx(s);
1064 }
1065
1066 /* ARGSUSED */
1067 static int
filt_ustirread(struct knote * kn,long hint)1068 filt_ustirread(struct knote *kn, long hint)
1069 {
1070 struct ustir_softc *sc = kn->kn_hook;
1071
1072 kn->kn_data = sc->sc_ur_framelen;
1073 return kn->kn_data > 0;
1074 }
1075
1076 static void
filt_ustirwdetach(struct knote * kn)1077 filt_ustirwdetach(struct knote *kn)
1078 {
1079 struct ustir_softc *sc = kn->kn_hook;
1080 int s;
1081
1082 s = splusb();
1083 selremove_knote(&sc->sc_wr_sel, kn);
1084 splx(s);
1085 }
1086
1087 /* ARGSUSED */
1088 static int
filt_ustirwrite(struct knote * kn,long hint)1089 filt_ustirwrite(struct knote *kn, long hint)
1090 {
1091 struct ustir_softc *sc = kn->kn_hook;
1092
1093 kn->kn_data = 0;
1094 return sc->sc_direction != udir_input;
1095 }
1096
1097 static const struct filterops ustirread_filtops = {
1098 .f_flags = FILTEROP_ISFD,
1099 .f_attach = NULL,
1100 .f_detach = filt_ustirrdetach,
1101 .f_event = filt_ustirread,
1102 };
1103
1104 static const struct filterops ustirwrite_filtops = {
1105 .f_flags = FILTEROP_ISFD,
1106 .f_attach = NULL,
1107 .f_detach = filt_ustirwdetach,
1108 .f_event = filt_ustirwrite,
1109 };
1110
1111 Static int
ustir_kqfilter(void * h,struct knote * kn)1112 ustir_kqfilter(void *h, struct knote *kn)
1113 {
1114 struct ustir_softc *sc = h;
1115 struct selinfo *sip;
1116 int s;
1117
1118 switch (kn->kn_filter) {
1119 case EVFILT_READ:
1120 sip = &sc->sc_rd_sel;
1121 kn->kn_fop = &ustirread_filtops;
1122 break;
1123 case EVFILT_WRITE:
1124 sip = &sc->sc_wr_sel;
1125 kn->kn_fop = &ustirwrite_filtops;
1126 break;
1127 default:
1128 return EINVAL;
1129 }
1130
1131 kn->kn_hook = sc;
1132
1133 s = splusb();
1134 selrecord_knote(sip, kn);
1135 splx(s);
1136
1137 return 0;
1138 }
1139
1140 #ifdef USTIR_DEBUG_IOCTLS
ustir_ioctl(void * h,u_long cmd,void * addr,int flag,struct lwp * l)1141 Static int ustir_ioctl(void *h, u_long cmd, void *addr, int flag, struct lwp *l)
1142 {
1143 struct ustir_softc *sc = h;
1144 int error;
1145 unsigned int regnum;
1146 usbd_status err;
1147 uint8_t regdata = 0;
1148
1149 if (sc->sc_dying)
1150 return EIO;
1151
1152 sc->sc_refcnt++;
1153
1154 error = 0;
1155 switch (cmd) {
1156 case USTIR_READ_REGISTER:
1157 regnum = *(unsigned int *)addr;
1158
1159 if (regnum > STIR_MAX_REG) {
1160 error = EINVAL;
1161 break;
1162 }
1163
1164 err = ustir_read_reg(sc, regnum, ®data);
1165
1166 DPRINTFN(10, ("%s: regget(%u) = %#x\n", __func__,
1167 regnum, (unsigned int)regdata));
1168
1169 *(unsigned int *)addr = regdata;
1170 if (err != USBD_NORMAL_COMPLETION) {
1171 printf("%s: register read failed: %s\n",
1172 device_xname(sc->sc_dev),
1173 usbd_errstr(err));
1174 error = EIO;
1175 }
1176 break;
1177
1178 case USTIR_WRITE_REGISTER:
1179 regnum = *(unsigned int *)addr;
1180 regdata = (regnum >> 8) & 0xff;
1181 regnum = regnum & 0xff;
1182
1183 if (regnum > STIR_MAX_REG) {
1184 error = EINVAL;
1185 break;
1186 }
1187
1188 DPRINTFN(10, ("%s: regset(%u, %#x)\n", __func__,
1189 regnum, (unsigned int)regdata));
1190
1191 err = ustir_write_reg(sc, regnum, regdata);
1192 if (err != USBD_NORMAL_COMPLETION) {
1193 printf("%s: register write failed: %s\n",
1194 device_xname(sc->sc_dev),
1195 usbd_errstr(err));
1196 error = EIO;
1197 }
1198 break;
1199
1200 case USTIR_DEBUG_LEVEL:
1201 #ifdef USTIR_DEBUG
1202 ustirdebug = *(int *)addr;
1203 #endif
1204 break;
1205
1206 case USTIR_DEBUG_OPERATION:
1207 break;
1208
1209 default:
1210 error = EINVAL;
1211 break;
1212 }
1213
1214 if (--sc->sc_refcnt < 0)
1215 usb_detach_wakeupold(sc->sc_dev);
1216
1217 return error;
1218 }
1219 #endif
1220
1221 Static int
ustir_set_params(void * h,struct irda_params * p)1222 ustir_set_params(void *h, struct irda_params *p)
1223 {
1224 struct ustir_softc *sc = h;
1225 struct ustir_speedrec const *speedblk;
1226 int i;
1227
1228 DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
1229 sc, p->speed, p->ebofs, p->maxsize));
1230
1231 if (sc->sc_dying)
1232 return EIO;
1233
1234 speedblk = NULL;
1235
1236 if (sc->sc_speedrec == NULL || p->speed != sc->sc_speedrec->speed) {
1237 /* find speed */
1238 for (i = 0; i < USTIR_NSPEEDS; i++) {
1239 if (ustir_speeds[i].speed == p->speed) {
1240 speedblk = &ustir_speeds[i];
1241 goto found2;
1242 }
1243 }
1244 /* no good value found */
1245 return EINVAL;
1246 found2:
1247 ;
1248 }
1249 if (p->maxsize != sc->sc_params.maxsize) {
1250 if (p->maxsize > IRDA_MAX_FRAME_SIZE)
1251 return EINVAL;
1252 sc->sc_params.maxsize = p->maxsize;
1253 }
1254
1255 sc->sc_params = *p;
1256
1257 if (speedblk != NULL) {
1258 usbd_status err;
1259 uint8_t regmode;
1260 uint8_t regbrate;
1261
1262 sc->sc_speedrec = speedblk;
1263
1264 regmode = STIR_BRMODE_MODEREG(speedblk->config);
1265 regbrate = STIR_BRMODE_BRATEREG(speedblk->config);
1266
1267 /*
1268 * FFSPRST must be set to enable the FIFO.
1269 */
1270 regmode |= STIR_RMODE_FFSPRST;
1271
1272 DPRINTFN(10, ("%s: setting BRATE = %x\n", __func__,
1273 (unsigned int)regbrate));
1274 err = ustir_write_reg(sc, STIR_REG_BRATE, regbrate);
1275 if (err == USBD_NORMAL_COMPLETION) {
1276 DPRINTFN(10, ("%s: setting MODE = %x\n", __func__,
1277 (unsigned int)regmode));
1278 err = ustir_write_reg(sc, STIR_REG_MODE, regmode);
1279 }
1280 if (err != USBD_NORMAL_COMPLETION) {
1281 DPRINTFN(10, ("%s: error setting register: %s\n",
1282 __func__, usbd_errstr(err)));
1283 return EIO;
1284 }
1285 }
1286
1287 return 0;
1288 }
1289
1290 Static int
ustir_get_speeds(void * h,int * speeds)1291 ustir_get_speeds(void *h, int *speeds)
1292 {
1293 struct ustir_softc *sc = h;
1294
1295 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
1296
1297 if (sc->sc_dying)
1298 return EIO;
1299
1300 /* All these speeds are supported */
1301 *speeds = IRDA_SPEED_4000000 |
1302 IRDA_SPEED_1152000 |
1303 IRDA_SPEED_576000 |
1304 IRDA_SPEED_115200 |
1305 IRDA_SPEED_57600 |
1306 IRDA_SPEED_38400 |
1307 IRDA_SPEED_19200 |
1308 IRDA_SPEED_9600 |
1309 IRDA_SPEED_2400;
1310
1311 return 0;
1312 }
1313
1314 Static int
ustir_get_turnarounds(void * h,int * turnarounds)1315 ustir_get_turnarounds(void *h, int *turnarounds)
1316 {
1317 struct ustir_softc *sc = h;
1318
1319 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
1320
1321 if (sc->sc_dying)
1322 return EIO;
1323
1324 /*
1325 * Documentation is on the light side with respect to
1326 * turnaround time for this device.
1327 */
1328 *turnarounds = IRDA_TURNT_10000;
1329
1330 return 0;
1331 }
1332