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