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