ustir.c revision 1.22 1 /* $NetBSD: ustir.c,v 1.22 2008/02/18 05:24:24 dyoung 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.22 2008/02/18 05:24:24 dyoung 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 lwp *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, struct lwp *l);
213 Static int ustir_close(void *h, int flag, int mode, struct lwp *l);
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, struct lwp *l);
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, void *addr, int flag, struct lwp *l);
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 int ustir_match(device_t, struct cfdata *, void *);
292 void ustir_attach(device_t, device_t, void *);
293 void ustir_childdet(device_t, device_t);
294 int ustir_detach(device_t, int);
295 int ustir_activate(device_t, enum devact);
296 extern struct cfdriver ustir_cd;
297 CFATTACH_DECL2(ustir, sizeof(struct ustir_softc), ustir_match,
298 ustir_attach, ustir_detach, ustir_activate, NULL, ustir_childdet);
299
300 USB_MATCH(ustir)
301 {
302 USB_MATCH_START(ustir, uaa);
303
304 DPRINTFN(50,("ustir_match\n"));
305
306 if (uaa->vendor == USB_VENDOR_SIGMATEL &&
307 uaa->product == USB_PRODUCT_SIGMATEL_IRDA)
308 return UMATCH_VENDOR_PRODUCT;
309
310 return UMATCH_NONE;
311 }
312
313 USB_ATTACH(ustir)
314 {
315 USB_ATTACH_START(ustir, sc, uaa);
316 usbd_device_handle dev = uaa->device;
317 usbd_interface_handle iface;
318 char *devinfop;
319 usb_endpoint_descriptor_t *ed;
320 u_int8_t epcount;
321 int i;
322 struct ir_attach_args ia;
323
324 DPRINTFN(10,("ustir_attach: sc=%p\n", sc));
325
326 devinfop = usbd_devinfo_alloc(dev, 0);
327 USB_ATTACH_SETUP;
328 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
329 usbd_devinfo_free(devinfop);
330
331 if (usbd_set_config_index(dev, 0, 1)
332 || usbd_device2interface_handle(dev, 0, &iface)) {
333 printf("%s: Configuration failed\n", USBDEVNAME(sc->sc_dev));
334 USB_ATTACH_ERROR_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 printf("%s: couldn't get ep %d\n",
349 USBDEVNAME(sc->sc_dev), i);
350 USB_ATTACH_ERROR_RETURN;
351 }
352 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
353 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
354 sc->sc_rd_addr = ed->bEndpointAddress;
355 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
356 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
357 sc->sc_wr_addr = ed->bEndpointAddress;
358 }
359 }
360 if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
361 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
362 USB_ATTACH_ERROR_RETURN;
363 }
364
365 DPRINTFN(10, ("ustir_attach: %p\n", sc->sc_udev));
366
367 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
368 USBDEV(sc->sc_dev));
369
370 ia.ia_type = IR_TYPE_IRFRAME;
371 ia.ia_methods = &ustir_methods;
372 ia.ia_handle = sc;
373
374 sc->sc_child = config_found(self, &ia, ir_print);
375
376 USB_ATTACH_SUCCESS_RETURN;
377 }
378
379 void
380 ustir_childdet(device_t self, device_t child)
381 {
382 struct ustir_softc *sc = device_private(self);
383
384 KASSERT(sc->sc_child == child);
385 sc->sc_child = NULL;
386 }
387
388 USB_DETACH(ustir)
389 {
390 USB_DETACH_START(ustir, sc);
391 int s;
392 int rv = 0;
393
394 DPRINTFN(0, ("ustir_detach: sc=%p flags=%d\n", sc, flags));
395
396 sc->sc_closing = sc->sc_dying = 1;
397
398 wakeup(&sc->sc_thread);
399
400 while (sc->sc_thread != NULL)
401 tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
402
403 /* Abort all pipes. Causes processes waiting for transfer to wake. */
404 if (sc->sc_rd_pipe != NULL) {
405 usbd_abort_pipe(sc->sc_rd_pipe);
406 usbd_close_pipe(sc->sc_rd_pipe);
407 sc->sc_rd_pipe = NULL;
408 }
409 if (sc->sc_wr_pipe != NULL) {
410 usbd_abort_pipe(sc->sc_wr_pipe);
411 usbd_close_pipe(sc->sc_wr_pipe);
412 sc->sc_wr_pipe = NULL;
413 }
414 wakeup(&sc->sc_ur_framelen);
415 wakeup(&sc->sc_wr_buf);
416
417 s = splusb();
418 if (--sc->sc_refcnt >= 0) {
419 /* Wait for processes to go away. */
420 usb_detach_wait(USBDEV(sc->sc_dev));
421 }
422 splx(s);
423
424 if (sc->sc_child != NULL)
425 rv = config_detach(sc->sc_child, flags);
426
427 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
428 USBDEV(sc->sc_dev));
429
430 return rv;
431 }
432
433 Static void
434 deframe_clear(struct framestate *fstate)
435 {
436 fstate->bufindex = 0;
437 fstate->fsmstate = FSTATE_END_OF_FRAME;
438 fstate->escaped = 0;
439 }
440
441 Static void
442 deframe_init(struct framestate *fstate, struct framedefn const *definition,
443 u_int8_t *buf, size_t buflen)
444 {
445 fstate->definition = definition;
446 fstate->buffer = buf;
447 fstate->buflen = buflen;
448
449 deframe_clear(fstate);
450 }
451
452 Static enum frameresult
453 deframe_process(struct framestate *fstate, u_int8_t const **bptr, size_t *blen)
454 {
455 struct framedefn const *definition;
456 u_int8_t const *cptr;
457 u_int8_t escchr;
458 size_t ibuflen, obufindex, obuflen;
459 enum framefsmstate fsmstate;
460 enum frameresult result;
461
462 cptr = *bptr;
463 fsmstate = fstate->fsmstate;
464 definition = fstate->definition;
465 escchr = definition->esc_byte;
466 obufindex = fstate->bufindex;
467 obuflen = fstate->buflen;
468 ibuflen = *blen;
469
470 while (ibuflen-- > 0) {
471 u_int8_t chr;
472
473 chr = *cptr++;
474
475 if (fstate->escaped) {
476 fstate->escaped = 0;
477 chr ^= definition->esc_xor;
478 } else if (chr == escchr) {
479 fstate->escaped = 1;
480 continue;
481 }
482
483 switch (fsmstate) {
484 case FSTATE_IN_DATA:
485 if (chr == definition->eof_byte) {
486 fsmstate = FSTATE_IN_END;
487 fstate->state_index = definition->eof_count;
488 goto state_in_end;
489 }
490 if (obufindex >= obuflen) {
491 result = FR_BUFFEROVERRUN;
492 fsmstate = FSTATE_END_OF_FRAME;
493 goto complete;
494 }
495 fstate->buffer[obufindex++] = chr;
496 break;
497
498 state_in_end:
499 /* FALLTHROUGH */
500
501 case FSTATE_IN_END:
502 if (--fstate->state_index == 0) {
503 u_int32_t crc;
504 size_t fcslen;
505
506 fsmstate = FSTATE_END_OF_FRAME;
507
508 fcslen = definition->fcs_count;
509
510 if (obufindex < fcslen) {
511 result = FR_FRAMEMALFORMED;
512 goto complete;
513 }
514
515 crc = definition->
516 fcs_calc(definition->fcs_init,
517 fstate->buffer, obufindex);
518
519 /* Remove check bytes from buffer length */
520 obufindex -= fcslen;
521
522 if (crc == definition->fcs_correct)
523 result = FR_FRAMEOK;
524 else
525 result = FR_FRAMEBADFCS;
526
527 goto complete;
528 }
529 break;
530
531 case FSTATE_END_OF_FRAME:
532 if (chr != definition->bof_byte)
533 break;
534
535 fsmstate = FSTATE_START_OF_FRAME;
536 fstate->state_index = definition->bof_count;
537 /* FALLTHROUGH */
538 case FSTATE_START_OF_FRAME:
539 if (--fstate->state_index == 0) {
540 fsmstate = FSTATE_IN_DATA;
541 obufindex = 0;
542 }
543 break;
544 }
545 }
546
547 result = (fsmstate == FSTATE_END_OF_FRAME) ? FR_IDLE : FR_INPROGRESS;
548
549 complete:
550 fstate->bufindex = obufindex;
551 fstate->fsmstate = fsmstate;
552 *blen = ibuflen;
553
554 return result;
555 }
556
557 /* Returns 0 if more data required, 1 if a complete frame was extracted */
558 static int
559 deframe_rd_ur(struct ustir_softc *sc)
560 {
561 while (sc->sc_rd_index < sc->sc_rd_count) {
562 u_int8_t const *buf;
563 size_t buflen;
564 enum frameresult fresult;
565
566 buf = &sc->sc_rd_buf[sc->sc_rd_index];
567 buflen = sc->sc_rd_count - sc->sc_rd_index;
568
569 fresult = deframe_process(&sc->sc_framestate, &buf, &buflen);
570
571 sc->sc_rd_index = sc->sc_rd_count - buflen;
572
573 DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult));
574
575 switch (fresult) {
576 case FR_IDLE:
577 case FR_INPROGRESS:
578 case FR_FRAMEBADFCS:
579 case FR_FRAMEMALFORMED:
580 case FR_BUFFEROVERRUN:
581 break;
582 case FR_FRAMEOK:
583 sc->sc_ur_framelen = sc->sc_framestate.bufindex;
584 wakeup(&sc->sc_ur_framelen); /* XXX should use flag */
585 selnotify(&sc->sc_rd_sel, 0);
586 return 1;
587 }
588 }
589
590 /* Reset indices into USB-side buffer */
591 sc->sc_rd_index = sc->sc_rd_count = 0;
592
593 return 0;
594 }
595
596 /*
597 * Direction transitions:
598 *
599 * ustir_periodic() can switch the direction from:
600 *
601 * output -> idle
602 * output -> stalled
603 * stalled -> idle
604 * idle -> input
605 *
606 * ustir_rd_cb() can switch the direction from:
607 *
608 * input -> stalled
609 * input -> idle
610 *
611 * ustir_write() can switch the direction from:
612 *
613 * idle -> output
614 */
615 Static void
616 ustir_periodic(struct ustir_softc *sc)
617 {
618 DPRINTFN(60, ("%s: direction = %d\n",
619 __func__, sc->sc_direction));
620
621 if (sc->sc_direction == udir_output ||
622 sc->sc_direction == udir_stalled) {
623 usbd_status err;
624 u_int8_t regval;
625
626 DPRINTFN(60, ("%s: reading status register\n",
627 __func__));
628
629 err = ustir_read_reg(sc, STIR_REG_STATUS,
630 ®val);
631 if (err != USBD_NORMAL_COMPLETION) {
632 printf("%s: status register read failed: %s\n",
633 USBDEVNAME(sc->sc_dev),
634 usbd_errstr(err));
635 } else {
636 DPRINTFN(10, ("%s: status register = 0x%x\n",
637 __func__,
638 (unsigned int)regval));
639 if (sc->sc_direction == udir_output &&
640 !(regval & STIR_RSTATUS_FFDIR))
641 /* Output has completed */
642 sc->sc_direction = udir_idle;
643 if (regval & STIR_RSTATUS_FFOVER) {
644 /*
645 * On an overrun the FIFO hangs, and
646 * any data bulk transfers will stall.
647 * Reset the FIFO.
648 */
649 sc->sc_direction = udir_stalled;
650
651 DPRINTFN(10, ("%s: clearing FIFO error\n",
652 __func__));
653
654 err = ustir_write_reg(sc, STIR_REG_STATUS,
655 STIR_RSTATUS_FFCLR);
656 /* XXX if we fail partway through
657 * this, we may not recover? */
658 if (err == USBD_NORMAL_COMPLETION)
659 err = ustir_write_reg(sc,
660 STIR_REG_STATUS,
661 0);
662 if (err != USBD_NORMAL_COMPLETION) {
663 printf("%s: FIFO reset failed: %s\n",
664 USBDEVNAME(sc->sc_dev),
665 usbd_errstr(err));
666 } else {
667 /* FIFO reset */
668 sc->sc_direction = udir_idle;
669 }
670 }
671 }
672 }
673
674 if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) {
675 /*
676 * In a stale write case, we need to check if the
677 * write has completed. Once that has happened, the
678 * write is no longer stale.
679 *
680 * But note that we may immediately start a read poll...
681 */
682 sc->sc_wr_stalewrite = 0;
683 wakeup(&sc->sc_wr_buf);
684 }
685
686 if (!sc->sc_rd_readinprogress &&
687 (sc->sc_direction == udir_idle ||
688 sc->sc_direction == udir_input))
689 /* Do a read poll if appropriate... */
690 ustir_start_read(sc);
691 }
692
693 Static void
694 ustir_thread(void *arg)
695 {
696 struct ustir_softc *sc = arg;
697
698 DPRINTFN(20, ("%s: starting polling thread\n", __func__));
699
700 while (!sc->sc_closing) {
701 if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
702 ustir_periodic(sc);
703
704 if (!sc->sc_closing) {
705 int error;
706 error = tsleep(&sc->sc_thread, PWAIT,
707 "ustir", hz / 10);
708 if (error == EWOULDBLOCK &&
709 sc->sc_rd_expectdataticks > 0)
710 /*
711 * After a timeout decrement the tick
712 * counter within which time we expect
713 * data to arrive if we are receiving
714 * data...
715 */
716 sc->sc_rd_expectdataticks--;
717 }
718 }
719
720 DPRINTFN(20, ("%s: exiting polling thread\n", __func__));
721
722 sc->sc_thread = NULL;
723
724 wakeup(&sc->sc_closing);
725
726 if (--sc->sc_refcnt < 0)
727 usb_detach_wakeup(USBDEV(sc->sc_dev));
728
729 kthread_exit(0);
730 }
731
732 Static void
733 ustir_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
734 usbd_status status)
735 {
736 struct ustir_softc *sc = priv;
737 u_int32_t size;
738
739 DPRINTFN(60, ("%s: sc=%p\n", __func__, sc));
740
741 /* Read is no longer in progress */
742 sc->sc_rd_readinprogress = 0;
743
744 if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */
745 return;
746 if (status) {
747 size = 0;
748 sc->sc_rd_err = 1;
749
750 if (sc->sc_direction == udir_input ||
751 sc->sc_direction == udir_idle) {
752 /*
753 * Receive error, probably need to clear error
754 * condition.
755 */
756 sc->sc_direction = udir_stalled;
757 }
758 } else {
759 usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
760 }
761
762 sc->sc_rd_index = 0;
763 sc->sc_rd_count = size;
764
765 DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60),
766 ("%s: sc=%p size=%u, err=%d\n", __func__,
767 sc, size, sc->sc_rd_err));
768
769 #ifdef USTIR_DEBUG
770 if (ustirdebug >= 20 && size > 0)
771 ustir_dumpdata(sc->sc_rd_buf, size, __func__);
772 #endif
773
774 if (!deframe_rd_ur(sc)) {
775 if (!deframe_isclear(&sc->sc_framestate) && size == 0 &&
776 sc->sc_rd_expectdataticks == 0) {
777 /*
778 * Expected data, but didn't get it
779 * within expected time...
780 */
781 DPRINTFN(5,("%s: incoming packet timeout\n",
782 __func__));
783 deframe_clear(&sc->sc_framestate);
784 } else if (size > 0) {
785 /*
786 * If we also received actual data, reset the
787 * data read timeout and wake up the possibly
788 * sleeping thread...
789 */
790 sc->sc_rd_expectdataticks = 2;
791 wakeup(&sc->sc_thread);
792 }
793 }
794
795 /*
796 * Check if incoming data has stopped, or that we cannot
797 * safely read any more data. In the case of the latter we
798 * must switch to idle so that a write will not block...
799 */
800 if (sc->sc_direction == udir_input &&
801 ((size == 0 && sc->sc_rd_expectdataticks == 0) ||
802 USTIR_BLOCK_RX_DATA(sc))) {
803 DPRINTFN(8,("%s: idling on packet timeout, "
804 "complete frame, or no data\n", __func__));
805 sc->sc_direction = udir_idle;
806
807 /* Wake up for possible output */
808 wakeup(&sc->sc_wr_buf);
809 selnotify(&sc->sc_wr_sel, 0);
810 }
811 }
812
813 Static usbd_status
814 ustir_start_read(struct ustir_softc *sc)
815 {
816 usbd_status err;
817
818 DPRINTFN(60,("%s: sc=%p, size=%d\n", __func__, sc,
819 sc->sc_params.maxsize));
820
821 if (sc->sc_dying)
822 return USBD_IOERROR;
823
824 if (USTIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) {
825 /*
826 * Can't start reading just yet. Since we aren't
827 * going to start a read, have to switch direction to
828 * idle.
829 */
830 sc->sc_direction = udir_idle;
831 return USBD_NORMAL_COMPLETION;
832 }
833
834 /* Starting a read... */
835 sc->sc_rd_readinprogress = 1;
836 sc->sc_direction = udir_input;
837
838 if (sc->sc_rd_err) {
839 sc->sc_rd_err = 0;
840 DPRINTFN(0, ("%s: clear stall\n", __func__));
841 usbd_clear_endpoint_stall(sc->sc_rd_pipe);
842 }
843
844 usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
845 sc->sc_params.maxsize,
846 USBD_SHORT_XFER_OK | USBD_NO_COPY,
847 USBD_NO_TIMEOUT, ustir_rd_cb);
848 err = usbd_transfer(sc->sc_rd_xfer);
849 if (err != USBD_IN_PROGRESS) {
850 DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err));
851 return err;
852 }
853 return USBD_NORMAL_COMPLETION;
854 }
855
856 Static int
857 ustir_activate(device_t self, enum devact act)
858 {
859 struct ustir_softc *sc = device_private(self);
860 int error = 0;
861
862 switch (act) {
863 case DVACT_ACTIVATE:
864 return EOPNOTSUPP;
865
866 case DVACT_DEACTIVATE:
867 sc->sc_dying = 1;
868 if (sc->sc_child != NULL)
869 error = config_deactivate(sc->sc_child);
870 break;
871 }
872 return error;
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", sc->sc_dev.dv_xname);
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_wakeup(USBDEV(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_wakeup(USBDEV(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_wakeup(USBDEV(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 USBDEVNAME(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 USBDEVNAME(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_wakeup(USBDEV(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