ustir.c revision 1.33.10.9 1 /* $NetBSD: ustir.c,v 1.33.10.9 2015/10/06 21:32:15 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.33.10.9 2015/10/06 21:32:15 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 usbd_close_pipe(sc->sc_rd_pipe);
335 sc->sc_rd_pipe = NULL;
336 }
337 if (sc->sc_wr_pipe != NULL) {
338 usbd_abort_pipe(sc->sc_wr_pipe);
339 usbd_close_pipe(sc->sc_wr_pipe);
340 sc->sc_wr_pipe = NULL;
341 }
342 wakeup(&sc->sc_ur_framelen);
343 wakeup(&sc->sc_wr_buf);
344
345 s = splusb();
346 if (--sc->sc_refcnt >= 0) {
347 /* Wait for processes to go away. */
348 usb_detach_waitold(sc->sc_dev);
349 }
350 splx(s);
351
352 if (sc->sc_child != NULL)
353 rv = config_detach(sc->sc_child, flags);
354
355 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
356 sc->sc_dev);
357
358 seldestroy(&sc->sc_rd_sel);
359 seldestroy(&sc->sc_wr_sel);
360
361 return rv;
362 }
363
364 /* Returns 0 if more data required, 1 if a complete frame was extracted */
365 static int
366 deframe_rd_ur(struct ustir_softc *sc)
367 {
368 while (sc->sc_rd_index < sc->sc_rd_count) {
369 uint8_t const *buf;
370 size_t buflen;
371 enum frameresult fresult;
372
373 buf = &sc->sc_rd_buf[sc->sc_rd_index];
374 buflen = sc->sc_rd_count - sc->sc_rd_index;
375
376 fresult = deframe_process(&sc->sc_framestate, &buf, &buflen);
377
378 sc->sc_rd_index = sc->sc_rd_count - buflen;
379
380 DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult));
381
382 switch (fresult) {
383 case FR_IDLE:
384 case FR_INPROGRESS:
385 case FR_FRAMEBADFCS:
386 case FR_FRAMEMALFORMED:
387 case FR_BUFFEROVERRUN:
388 break;
389 case FR_FRAMEOK:
390 sc->sc_ur_framelen = sc->sc_framestate.bufindex;
391 wakeup(&sc->sc_ur_framelen); /* XXX should use flag */
392 selnotify(&sc->sc_rd_sel, 0, 0);
393 return 1;
394 }
395 }
396
397 /* Reset indices into USB-side buffer */
398 sc->sc_rd_index = sc->sc_rd_count = 0;
399
400 return 0;
401 }
402
403 /*
404 * Direction transitions:
405 *
406 * ustir_periodic() can switch the direction from:
407 *
408 * output -> idle
409 * output -> stalled
410 * stalled -> idle
411 * idle -> input
412 *
413 * ustir_rd_cb() can switch the direction from:
414 *
415 * input -> stalled
416 * input -> idle
417 *
418 * ustir_write() can switch the direction from:
419 *
420 * idle -> output
421 */
422 Static void
423 ustir_periodic(struct ustir_softc *sc)
424 {
425 DPRINTFN(60, ("%s: direction = %d\n",
426 __func__, sc->sc_direction));
427
428 if (sc->sc_direction == udir_output ||
429 sc->sc_direction == udir_stalled) {
430 usbd_status err;
431 uint8_t regval;
432
433 DPRINTFN(60, ("%s: reading status register\n",
434 __func__));
435
436 err = ustir_read_reg(sc, STIR_REG_STATUS,
437 ®val);
438 if (err != USBD_NORMAL_COMPLETION) {
439 aprint_error_dev(sc->sc_dev,
440 "status register read failed: %s\n",
441 usbd_errstr(err));
442 } else {
443 DPRINTFN(10, ("%s: status register = 0x%x\n",
444 __func__,
445 (unsigned int)regval));
446 if (sc->sc_direction == udir_output &&
447 !(regval & STIR_RSTATUS_FFDIR))
448 /* Output has completed */
449 sc->sc_direction = udir_idle;
450 if (regval & STIR_RSTATUS_FFOVER) {
451 /*
452 * On an overrun the FIFO hangs, and
453 * any data bulk transfers will stall.
454 * Reset the FIFO.
455 */
456 sc->sc_direction = udir_stalled;
457
458 DPRINTFN(10, ("%s: clearing FIFO error\n",
459 __func__));
460
461 err = ustir_write_reg(sc, STIR_REG_STATUS,
462 STIR_RSTATUS_FFCLR);
463 /* XXX if we fail partway through
464 * this, we may not recover? */
465 if (err == USBD_NORMAL_COMPLETION)
466 err = ustir_write_reg(sc,
467 STIR_REG_STATUS,
468 0);
469 if (err != USBD_NORMAL_COMPLETION) {
470 aprint_error_dev(sc->sc_dev,
471 "FIFO reset failed: %s\n",
472 usbd_errstr(err));
473 } else {
474 /* FIFO reset */
475 sc->sc_direction = udir_idle;
476 }
477 }
478 }
479 }
480
481 if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) {
482 /*
483 * In a stale write case, we need to check if the
484 * write has completed. Once that has happened, the
485 * write is no longer stale.
486 *
487 * But note that we may immediately start a read poll...
488 */
489 sc->sc_wr_stalewrite = 0;
490 wakeup(&sc->sc_wr_buf);
491 }
492
493 if (!sc->sc_rd_readinprogress &&
494 (sc->sc_direction == udir_idle ||
495 sc->sc_direction == udir_input))
496 /* Do a read poll if appropriate... */
497 ustir_start_read(sc);
498 }
499
500 Static void
501 ustir_thread(void *arg)
502 {
503 struct ustir_softc *sc = arg;
504
505 DPRINTFN(20, ("%s: starting polling thread\n", __func__));
506
507 while (!sc->sc_closing) {
508 if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
509 ustir_periodic(sc);
510
511 if (!sc->sc_closing) {
512 int error;
513 error = tsleep(&sc->sc_thread, PWAIT,
514 "ustir", hz / 10);
515 if (error == EWOULDBLOCK &&
516 sc->sc_rd_expectdataticks > 0)
517 /*
518 * After a timeout decrement the tick
519 * counter within which time we expect
520 * data to arrive if we are receiving
521 * data...
522 */
523 sc->sc_rd_expectdataticks--;
524 }
525 }
526
527 DPRINTFN(20, ("%s: exiting polling thread\n", __func__));
528
529 sc->sc_thread = NULL;
530
531 wakeup(&sc->sc_closing);
532
533 if (--sc->sc_refcnt < 0)
534 usb_detach_wakeupold(sc->sc_dev);
535
536 kthread_exit(0);
537 }
538
539 Static void
540 ustir_rd_cb(struct usbd_xfer *xfer, void *priv,
541 usbd_status status)
542 {
543 struct ustir_softc *sc = priv;
544 uint32_t size;
545
546 DPRINTFN(60, ("%s: sc=%p\n", __func__, sc));
547
548 /* Read is no longer in progress */
549 sc->sc_rd_readinprogress = 0;
550
551 if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */
552 return;
553 if (status) {
554 size = 0;
555 sc->sc_rd_err = 1;
556
557 if (sc->sc_direction == udir_input ||
558 sc->sc_direction == udir_idle) {
559 /*
560 * Receive error, probably need to clear error
561 * condition.
562 */
563 sc->sc_direction = udir_stalled;
564 }
565 } else {
566 usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
567 }
568
569 sc->sc_rd_index = 0;
570 sc->sc_rd_count = size;
571
572 DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60),
573 ("%s: sc=%p size=%u, err=%d\n", __func__,
574 sc, size, sc->sc_rd_err));
575
576 #ifdef USTIR_DEBUG
577 if (ustirdebug >= 20 && size > 0)
578 ustir_dumpdata(sc->sc_rd_buf, size, __func__);
579 #endif
580
581 if (!deframe_rd_ur(sc)) {
582 if (!deframe_isclear(&sc->sc_framestate) && size == 0 &&
583 sc->sc_rd_expectdataticks == 0) {
584 /*
585 * Expected data, but didn't get it
586 * within expected time...
587 */
588 DPRINTFN(5,("%s: incoming packet timeout\n",
589 __func__));
590 deframe_clear(&sc->sc_framestate);
591 } else if (size > 0) {
592 /*
593 * If we also received actual data, reset the
594 * data read timeout and wake up the possibly
595 * sleeping thread...
596 */
597 sc->sc_rd_expectdataticks = 2;
598 wakeup(&sc->sc_thread);
599 }
600 }
601
602 /*
603 * Check if incoming data has stopped, or that we cannot
604 * safely read any more data. In the case of the latter we
605 * must switch to idle so that a write will not block...
606 */
607 if (sc->sc_direction == udir_input &&
608 ((size == 0 && sc->sc_rd_expectdataticks == 0) ||
609 USTIR_BLOCK_RX_DATA(sc))) {
610 DPRINTFN(8,("%s: idling on packet timeout, "
611 "complete frame, or no data\n", __func__));
612 sc->sc_direction = udir_idle;
613
614 /* Wake up for possible output */
615 wakeup(&sc->sc_wr_buf);
616 selnotify(&sc->sc_wr_sel, 0, 0);
617 }
618 }
619
620 Static usbd_status
621 ustir_start_read(struct ustir_softc *sc)
622 {
623 usbd_status err;
624
625 DPRINTFN(60,("%s: sc=%p, size=%d\n", __func__, sc,
626 sc->sc_params.maxsize));
627
628 if (sc->sc_dying)
629 return USBD_IOERROR;
630
631 if (USTIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) {
632 /*
633 * Can't start reading just yet. Since we aren't
634 * going to start a read, have to switch direction to
635 * idle.
636 */
637 sc->sc_direction = udir_idle;
638 return USBD_NORMAL_COMPLETION;
639 }
640
641 /* Starting a read... */
642 sc->sc_rd_readinprogress = 1;
643 sc->sc_direction = udir_input;
644
645 if (sc->sc_rd_err) {
646 sc->sc_rd_err = 0;
647 DPRINTFN(0, ("%s: clear stall\n", __func__));
648 usbd_clear_endpoint_stall(sc->sc_rd_pipe);
649 }
650
651 usbd_setup_xfer(sc->sc_rd_xfer, sc, sc->sc_rd_buf,
652 sc->sc_params.maxsize, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT,
653 ustir_rd_cb);
654 err = usbd_transfer(sc->sc_rd_xfer);
655 if (err != USBD_IN_PROGRESS) {
656 DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err));
657 return err;
658 }
659 return USBD_NORMAL_COMPLETION;
660 }
661
662 Static int
663 ustir_activate(device_t self, enum devact act)
664 {
665 struct ustir_softc *sc = device_private(self);
666
667 switch (act) {
668 case DVACT_DEACTIVATE:
669 sc->sc_dying = 1;
670 return 0;
671 default:
672 return EOPNOTSUPP;
673 }
674 }
675
676 /* ARGSUSED */
677 Static int
678 ustir_open(void *h, int flag, int mode,
679 struct lwp *l)
680 {
681 struct ustir_softc *sc = h;
682 int error;
683 usbd_status err;
684
685 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
686
687 err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
688 if (err != USBD_NORMAL_COMPLETION) {
689 error = EIO;
690 goto bad1;
691 }
692 err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
693 if (err != USBD_NORMAL_COMPLETION) {
694 error = EIO;
695 goto bad2;
696 }
697 error = usbd_create_xfer(sc->sc_rd_pipe, IRDA_MAX_FRAME_SIZE,
698 USBD_SHORT_XFER_OK, 0, &sc->sc_rd_xfer);
699 if (error)
700 goto bad3;
701 sc->sc_rd_buf = usbd_get_buffer(sc->sc_rd_xfer);
702
703 error = usbd_create_xfer(sc->sc_wr_pipe,
704 IRDA_MAX_FRAME_SIZE + STIR_OUTPUT_HEADER_SIZE,
705 USBD_FORCE_SHORT_XFER, 0, &sc->sc_wr_xfer);
706 if (error)
707 goto bad4;
708 sc->sc_wr_buf = usbd_get_buffer(sc->sc_wr_xfer);
709
710 sc->sc_ur_buf = kmem_alloc(IRDA_MAX_FRAME_SIZE, KM_SLEEP);
711 if (sc->sc_ur_buf == NULL) {
712 error = ENOMEM;
713 goto bad5;
714 }
715
716 sc->sc_rd_index = sc->sc_rd_count = 0;
717 sc->sc_closing = 0;
718 sc->sc_rd_readinprogress = 0;
719 sc->sc_rd_expectdataticks = 0;
720 sc->sc_ur_framelen = 0;
721 sc->sc_rd_err = 0;
722 sc->sc_wr_stalewrite = 0;
723 sc->sc_speedrec = NULL;
724 sc->sc_direction = udir_idle;
725 sc->sc_params.speed = 0;
726 sc->sc_params.ebofs = 0;
727 sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
728
729 deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
730
731 /* Increment reference for thread */
732 sc->sc_refcnt++;
733
734 error = kthread_create(PRI_NONE, 0, NULL, ustir_thread, sc,
735 &sc->sc_thread, "%s", device_xname(sc->sc_dev));
736 if (error) {
737 sc->sc_refcnt--;
738 goto bad5;
739 }
740
741 return 0;
742
743 bad5:
744 usbd_destroy_xfer(sc->sc_wr_xfer);
745 sc->sc_wr_xfer = NULL;
746 bad4:
747 usbd_destroy_xfer(sc->sc_rd_xfer);
748 sc->sc_rd_xfer = NULL;
749 bad3:
750 usbd_close_pipe(sc->sc_wr_pipe);
751 sc->sc_wr_pipe = NULL;
752 bad2:
753 usbd_close_pipe(sc->sc_rd_pipe);
754 sc->sc_rd_pipe = NULL;
755 bad1:
756 return error;
757 }
758
759 /* ARGSUSED */
760 Static int
761 ustir_close(void *h, int flag, int mode,
762 struct lwp *l)
763 {
764 struct ustir_softc *sc = h;
765
766 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
767
768 sc->sc_refcnt++;
769
770 sc->sc_rd_readinprogress = 1;
771 sc->sc_closing = 1;
772
773 wakeup(&sc->sc_thread);
774
775 while (sc->sc_thread != NULL)
776 tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
777
778 if (sc->sc_rd_pipe != NULL) {
779 usbd_abort_pipe(sc->sc_rd_pipe);
780 usbd_close_pipe(sc->sc_rd_pipe);
781 sc->sc_rd_pipe = NULL;
782 }
783 if (sc->sc_wr_pipe != NULL) {
784 usbd_abort_pipe(sc->sc_wr_pipe);
785 usbd_close_pipe(sc->sc_wr_pipe);
786 sc->sc_wr_pipe = NULL;
787 }
788 if (sc->sc_rd_xfer != NULL) {
789 usbd_destroy_xfer(sc->sc_rd_xfer);
790 sc->sc_rd_xfer = NULL;
791 sc->sc_rd_buf = NULL;
792 }
793 if (sc->sc_wr_xfer != NULL) {
794 usbd_destroy_xfer(sc->sc_wr_xfer);
795 sc->sc_wr_xfer = NULL;
796 sc->sc_wr_buf = NULL;
797 }
798 if (sc->sc_ur_buf != NULL) {
799 kmem_free(sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
800 sc->sc_ur_buf = NULL;
801 }
802
803 if (--sc->sc_refcnt < 0)
804 usb_detach_wakeupold(sc->sc_dev);
805
806 return 0;
807 }
808
809 /* ARGSUSED */
810 Static int
811 ustir_read(void *h, struct uio *uio, int flag)
812 {
813 struct ustir_softc *sc = h;
814 int s;
815 int error;
816 u_int uframelen;
817
818 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
819
820 if (sc->sc_dying)
821 return EIO;
822
823 #ifdef DIAGNOSTIC
824 if (sc->sc_rd_buf == NULL)
825 return EINVAL;
826 #endif
827
828 sc->sc_refcnt++;
829
830 if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
831 /* Possibly wake up polling thread */
832 wakeup(&sc->sc_thread);
833
834 do {
835 s = splusb();
836 while (sc->sc_ur_framelen == 0) {
837 DPRINTFN(5,("%s: calling tsleep()\n", __func__));
838 error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH,
839 "usirrd", 0);
840 if (sc->sc_dying)
841 error = EIO;
842 if (error) {
843 splx(s);
844 DPRINTFN(0, ("%s: tsleep() = %d\n",
845 __func__, error));
846 goto ret;
847 }
848 }
849 splx(s);
850
851 uframelen = sc->sc_ur_framelen;
852 DPRINTFN(1,("%s: sc=%p framelen=%u, hdr=0x%02x\n",
853 __func__, sc, uframelen, sc->sc_ur_buf[0]));
854 if (uframelen > uio->uio_resid)
855 error = EINVAL;
856 else
857 error = uiomove(sc->sc_ur_buf, uframelen, uio);
858 sc->sc_ur_framelen = 0;
859
860 if (!deframe_rd_ur(sc) && uframelen > 0) {
861 /*
862 * Need to wait for another read to obtain a
863 * complete frame... If we also obtained
864 * actual data, wake up the possibly sleeping
865 * thread immediately...
866 */
867 wakeup(&sc->sc_thread);
868 }
869 } while (uframelen == 0);
870
871 DPRINTFN(1,("%s: return %d\n", __func__, error));
872
873 ret:
874 if (--sc->sc_refcnt < 0)
875 usb_detach_wakeupold(sc->sc_dev);
876 return error;
877 }
878
879 /* ARGSUSED */
880 Static int
881 ustir_write(void *h, struct uio *uio, int flag)
882 {
883 struct ustir_softc *sc = h;
884 usbd_status err;
885 uint32_t wrlen;
886 int error, sirlength;
887 uint8_t *wrbuf;
888 int s;
889
890 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
891
892 if (sc->sc_dying)
893 return EIO;
894
895 #ifdef DIAGNOSTIC
896 if (sc->sc_wr_buf == NULL)
897 return EINVAL;
898 #endif
899
900 wrlen = uio->uio_resid;
901 if (wrlen > sc->sc_params.maxsize)
902 return EINVAL;
903
904 sc->sc_refcnt++;
905
906 if (!USTIR_BLOCK_RX_DATA(sc)) {
907 /*
908 * If reads are not blocked, determine what action we
909 * should potentially take...
910 */
911 if (sc->sc_direction == udir_output) {
912 /*
913 * If the last operation was an output, wait for the
914 * polling thread to check for incoming data.
915 */
916 sc->sc_wr_stalewrite = 1;
917 wakeup(&sc->sc_thread);
918 } else if (!sc->sc_rd_readinprogress &&
919 (sc->sc_direction == udir_idle ||
920 sc->sc_direction == udir_input)) {
921 /* If idle, check for input before outputting */
922 ustir_start_read(sc);
923 }
924 }
925
926 s = splusb();
927 while (sc->sc_wr_stalewrite ||
928 (sc->sc_direction != udir_output &&
929 sc->sc_direction != udir_idle)) {
930 DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, "
931 "calling tsleep()\n", __func__,
932 sc, sc->sc_wr_stalewrite, sc->sc_direction));
933 error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH,
934 "usirwr", 0);
935 if (sc->sc_dying)
936 error = EIO;
937 if (error) {
938 splx(s);
939 DPRINTFN(0, ("%s: tsleep() = %d\n", __func__,
940 error));
941 goto ret;
942 }
943 }
944 splx(s);
945
946 wrbuf = sc->sc_wr_buf;
947
948 /* Build header */
949 wrbuf[0] = STIR_OUTPUT_HEADER_BYTE0;
950 wrbuf[1] = STIR_OUTPUT_HEADER_BYTE1;
951
952 sirlength = irda_sir_frame(&wrbuf[STIR_OUTPUT_HEADER_SIZE],
953 MAX_USTIR_OUTPUT_FRAME -
954 STIR_OUTPUT_HEADER_SIZE,
955 uio, sc->sc_params.ebofs);
956 if (sirlength < 0) {
957 error = -sirlength;
958 } else {
959 uint32_t btlen;
960
961 DPRINTFN(1, ("%s: transfer %u bytes\n", __func__,
962 (unsigned int)wrlen));
963
964 wrbuf[2] = sirlength & 0xff;
965 wrbuf[3] = (sirlength >> 8) & 0xff;
966
967 btlen = STIR_OUTPUT_HEADER_SIZE + sirlength;
968
969 sc->sc_direction = udir_output;
970
971 #ifdef USTIR_DEBUG
972 if (ustirdebug >= 20)
973 ustir_dumpdata(wrbuf, btlen, __func__);
974 #endif
975
976 err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
977 USBD_FORCE_SHORT_XFER, USTIR_WR_TIMEOUT, wrbuf, &btlen);
978 DPRINTFN(2, ("%s: err=%d\n", __func__, err));
979 if (err != USBD_NORMAL_COMPLETION) {
980 if (err == USBD_INTERRUPTED)
981 error = EINTR;
982 else if (err == USBD_TIMEOUT)
983 error = ETIMEDOUT;
984 else
985 error = EIO;
986 } else {
987 error = 0;
988 }
989 }
990
991 ret:
992 if (--sc->sc_refcnt < 0)
993 usb_detach_wakeupold(sc->sc_dev);
994
995 DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
996 return error;
997 }
998
999 Static int
1000 ustir_poll(void *h, int events, struct lwp *l)
1001 {
1002 struct ustir_softc *sc = h;
1003 int revents = 0;
1004
1005 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
1006
1007 if (events & (POLLOUT | POLLWRNORM)) {
1008 if (sc->sc_direction != udir_input) {
1009 revents |= events & (POLLOUT | POLLWRNORM);
1010 } else {
1011 DPRINTFN(2,("%s: recording write select\n",
1012 __func__));
1013 selrecord(l, &sc->sc_wr_sel);
1014 }
1015 }
1016
1017 if (events & (POLLIN | POLLRDNORM)) {
1018 if (sc->sc_ur_framelen != 0) {
1019 DPRINTFN(2,("%s: have data\n", __func__));
1020 revents |= events & (POLLIN | POLLRDNORM);
1021 } else {
1022 DPRINTFN(2,("%s: recording read select\n",
1023 __func__));
1024 selrecord(l, &sc->sc_rd_sel);
1025 }
1026 }
1027
1028 return revents;
1029 }
1030
1031 static void
1032 filt_ustirrdetach(struct knote *kn)
1033 {
1034 struct ustir_softc *sc = kn->kn_hook;
1035 int s;
1036
1037 s = splusb();
1038 SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
1039 splx(s);
1040 }
1041
1042 /* ARGSUSED */
1043 static int
1044 filt_ustirread(struct knote *kn, long hint)
1045 {
1046 struct ustir_softc *sc = kn->kn_hook;
1047
1048 kn->kn_data = sc->sc_ur_framelen;
1049 return kn->kn_data > 0;
1050 }
1051
1052 static void
1053 filt_ustirwdetach(struct knote *kn)
1054 {
1055 struct ustir_softc *sc = kn->kn_hook;
1056 int s;
1057
1058 s = splusb();
1059 SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext);
1060 splx(s);
1061 }
1062
1063 /* ARGSUSED */
1064 static int
1065 filt_ustirwrite(struct knote *kn, long hint)
1066 {
1067 struct ustir_softc *sc = kn->kn_hook;
1068
1069 kn->kn_data = 0;
1070 return sc->sc_direction != udir_input;
1071 }
1072
1073 static const struct filterops ustirread_filtops =
1074 { 1, NULL, filt_ustirrdetach, filt_ustirread };
1075 static const struct filterops ustirwrite_filtops =
1076 { 1, NULL, filt_ustirwdetach, filt_ustirwrite };
1077
1078 Static int
1079 ustir_kqfilter(void *h, struct knote *kn)
1080 {
1081 struct ustir_softc *sc = h;
1082 struct klist *klist;
1083 int s;
1084
1085 switch (kn->kn_filter) {
1086 case EVFILT_READ:
1087 klist = &sc->sc_rd_sel.sel_klist;
1088 kn->kn_fop = &ustirread_filtops;
1089 break;
1090 case EVFILT_WRITE:
1091 klist = &sc->sc_wr_sel.sel_klist;
1092 kn->kn_fop = &ustirwrite_filtops;
1093 break;
1094 default:
1095 return EINVAL;
1096 }
1097
1098 kn->kn_hook = sc;
1099
1100 s = splusb();
1101 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1102 splx(s);
1103
1104 return 0;
1105 }
1106
1107 #ifdef USTIR_DEBUG_IOCTLS
1108 Static int ustir_ioctl(void *h, u_long cmd, void *addr, int flag, struct lwp *l)
1109 {
1110 struct ustir_softc *sc = h;
1111 int error;
1112 unsigned int regnum;
1113 usbd_status err;
1114 uint8_t regdata;
1115
1116 if (sc->sc_dying)
1117 return EIO;
1118
1119 sc->sc_refcnt++;
1120
1121 error = 0;
1122 switch (cmd) {
1123 case USTIR_READ_REGISTER:
1124 regnum = *(unsigned int *)addr;
1125
1126 if (regnum > STIR_MAX_REG) {
1127 error = EINVAL;
1128 break;
1129 }
1130
1131 err = ustir_read_reg(sc, regnum, ®data);
1132
1133 DPRINTFN(10, ("%s: regget(%u) = 0x%x\n", __func__,
1134 regnum, (unsigned int)regdata));
1135
1136 *(unsigned int *)addr = regdata;
1137 if (err != USBD_NORMAL_COMPLETION) {
1138 printf("%s: register read failed: %s\n",
1139 device_xname(sc->sc_dev),
1140 usbd_errstr(err));
1141 error = EIO;
1142 }
1143 break;
1144
1145 case USTIR_WRITE_REGISTER:
1146 regnum = *(unsigned int *)addr;
1147 regdata = (regnum >> 8) & 0xff;
1148 regnum = regnum & 0xff;
1149
1150 if (regnum > STIR_MAX_REG) {
1151 error = EINVAL;
1152 break;
1153 }
1154
1155 DPRINTFN(10, ("%s: regset(%u, 0x%x)\n", __func__,
1156 regnum, (unsigned int)regdata));
1157
1158 err = ustir_write_reg(sc, regnum, regdata);
1159 if (err != USBD_NORMAL_COMPLETION) {
1160 printf("%s: register write failed: %s\n",
1161 device_xname(sc->sc_dev),
1162 usbd_errstr(err));
1163 error = EIO;
1164 }
1165 break;
1166
1167 case USTIR_DEBUG_LEVEL:
1168 #ifdef USTIR_DEBUG
1169 ustirdebug = *(int *)addr;
1170 #endif
1171 break;
1172
1173 case USTIR_DEBUG_OPERATION:
1174 break;
1175
1176 default:
1177 error = EINVAL;
1178 break;
1179 }
1180
1181 if (--sc->sc_refcnt < 0)
1182 usb_detach_wakeupold(sc->sc_dev);
1183
1184 return error;
1185 }
1186 #endif
1187
1188 Static int
1189 ustir_set_params(void *h, struct irda_params *p)
1190 {
1191 struct ustir_softc *sc = h;
1192 struct ustir_speedrec const *speedblk;
1193 int i;
1194
1195 DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
1196 sc, p->speed, p->ebofs, p->maxsize));
1197
1198 if (sc->sc_dying)
1199 return EIO;
1200
1201 speedblk = NULL;
1202
1203 if (sc->sc_speedrec == NULL || p->speed != sc->sc_speedrec->speed) {
1204 /* find speed */
1205 for (i = 0; i < USTIR_NSPEEDS; i++) {
1206 if (ustir_speeds[i].speed == p->speed) {
1207 speedblk = &ustir_speeds[i];
1208 goto found2;
1209 }
1210 }
1211 /* no good value found */
1212 return EINVAL;
1213 found2:
1214 ;
1215 }
1216 if (p->maxsize != sc->sc_params.maxsize) {
1217 if (p->maxsize > IRDA_MAX_FRAME_SIZE)
1218 return EINVAL;
1219 sc->sc_params.maxsize = p->maxsize;
1220 }
1221
1222 sc->sc_params = *p;
1223
1224 if (speedblk != NULL) {
1225 usbd_status err;
1226 uint8_t regmode;
1227 uint8_t regbrate;
1228
1229 sc->sc_speedrec = speedblk;
1230
1231 regmode = STIR_BRMODE_MODEREG(speedblk->config);
1232 regbrate = STIR_BRMODE_BRATEREG(speedblk->config);
1233
1234 /*
1235 * FFSPRST must be set to enable the FIFO.
1236 */
1237 regmode |= STIR_RMODE_FFSPRST;
1238
1239 DPRINTFN(10, ("%s: setting BRATE = %x\n", __func__,
1240 (unsigned int)regbrate));
1241 err = ustir_write_reg(sc, STIR_REG_BRATE, regbrate);
1242 if (err == USBD_NORMAL_COMPLETION) {
1243 DPRINTFN(10, ("%s: setting MODE = %x\n", __func__,
1244 (unsigned int)regmode));
1245 err = ustir_write_reg(sc, STIR_REG_MODE, regmode);
1246 }
1247 if (err != USBD_NORMAL_COMPLETION) {
1248 DPRINTFN(10, ("%s: error setting register: %s\n",
1249 __func__, usbd_errstr(err)));
1250 return EIO;
1251 }
1252 }
1253
1254 return 0;
1255 }
1256
1257 Static int
1258 ustir_get_speeds(void *h, int *speeds)
1259 {
1260 struct ustir_softc *sc = h;
1261
1262 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
1263
1264 if (sc->sc_dying)
1265 return EIO;
1266
1267 /* All these speeds are supported */
1268 *speeds = IRDA_SPEED_4000000 |
1269 IRDA_SPEED_1152000 |
1270 IRDA_SPEED_576000 |
1271 IRDA_SPEED_115200 |
1272 IRDA_SPEED_57600 |
1273 IRDA_SPEED_38400 |
1274 IRDA_SPEED_19200 |
1275 IRDA_SPEED_9600 |
1276 IRDA_SPEED_2400;
1277
1278 return 0;
1279 }
1280
1281 Static int
1282 ustir_get_turnarounds(void *h, int *turnarounds)
1283 {
1284 struct ustir_softc *sc = h;
1285
1286 DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
1287
1288 if (sc->sc_dying)
1289 return EIO;
1290
1291 /*
1292 * Documentation is on the light side with respect to
1293 * turnaround time for this device.
1294 */
1295 *turnarounds = IRDA_TURNT_10000;
1296
1297 return 0;
1298 }
1299