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