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