1 /* $NetBSD: ustir.c,v 1.27 2009/09/23 19:07:19 plunky Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Sainty <David.Sainty@dtsp.co.nz>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.27 2009/09/23 19:07:19 plunky Exp $");
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>
43 #include <sys/select.h>
45 #include <sys/kthread.h>
47 #ifdef USTIR_DEBUG_IOCTLS
48 #include <sys/ioctl.h>
49 #include <dev/usb/ustir.h>
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>
58 #include <dev/ir/ir.h>
59 #include <dev/ir/irdaio.h>
60 #include <dev/ir/irframevar.h>
61 #include <dev/ir/sir.h>
64 #define DPRINTFN(n,x) if (ustirdebug>(n)) logprintf x
70 /* Max size with framing. */
71 #define MAX_USTIR_OUTPUT_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + STIR_OUTPUT_HEADER_SIZE + 4)
73 #define USTIR_NSPEEDS 9
74 struct ustir_speedrec
{
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
}
92 unsigned int bof_count
;
98 unsigned int eof_count
;
101 unsigned int fcs_count
;
103 u_int32_t fcs_correct
;
105 u_int32_t (*fcs_calc
)(u_int32_t
, u_int8_t
const*, size_t);
108 Static u_int32_t
crc_ccitt_16(u_int32_t
, u_int8_t
const*, size_t);
110 struct framedefn
const framedef_sir
= {
120 FSTATE_START_OF_FRAME
,
135 struct framedefn
const *definition
;
141 enum framefsmstate fsmstate
;
146 #define deframe_isclear(fs) ((fs)->fsmstate == FSTATE_END_OF_FRAME)
148 Static
void deframe_clear(struct framestate
*);
149 Static
void deframe_init(struct framestate
*, struct framedefn
const *,
151 Static
enum frameresult
deframe_process(struct framestate
*, u_int8_t
const **,
155 USBBASEDEVICE sc_dev
;
156 usbd_device_handle sc_udev
;
157 usbd_interface_handle sc_iface
;
159 u_int8_t
*sc_ur_buf
; /* Unencapsulated frame */
160 u_int sc_ur_framelen
;
162 u_int8_t
*sc_rd_buf
; /* Raw incoming data stream */
165 usbd_pipe_handle sc_rd_pipe
;
166 usbd_xfer_handle sc_rd_xfer
;
168 int sc_rd_readinprogress
;
169 u_int sc_rd_expectdataticks
;
171 struct framestate sc_framestate
;
172 struct lwp
*sc_thread
;
173 struct selinfo sc_rd_sel
;
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
;
183 udir_input
, /* Receiving data */
184 udir_output
, /* Transmitting data */
185 udir_stalled
, /* Error preventing data flow */
186 udir_idle
/* Neither receiving nor transmitting */
189 struct ustir_speedrec
const *sc_speedrec
;
192 struct irda_params sc_params
;
199 /* True if we cannot safely read data from the device */
200 #define USTIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0)
202 #define USTIR_WR_TIMEOUT 200
204 Static
int ustir_activate(device_ptr_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
);
215 #ifdef USTIR_DEBUG_IOCTLS
216 Static
int ustir_ioctl(void *h
, u_long cmd
, void *addr
, int flag
, struct lwp
*l
);
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
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 *);
234 crc_ccitt_16(u_int32_t crcinit
, u_int8_t
const *buf
, size_t blen
)
239 crcinit
= updateFCS(crcinit
, chr
);
245 ustir_read_reg(struct ustir_softc
*sc
, unsigned int reg
, u_int8_t
*data
)
247 usb_device_request_t req
;
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);
255 return usbd_do_request(sc
->sc_udev
, &req
, data
);
259 ustir_write_reg(struct ustir_softc
*sc
, unsigned int reg
, u_int8_t data
)
261 usb_device_request_t req
;
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);
269 return usbd_do_request(sc
->sc_udev
, &req
, NULL
);
274 ustir_dumpdata(u_int8_t
const *data
, size_t dlen
, char const *desc
)
277 printf("%s: (%lx)", desc
, (unsigned long)dlen
);
278 for (bdindex
= 0; bdindex
< dlen
; bdindex
++)
279 printf(" %02x", (unsigned int)data
[bdindex
]);
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
);
295 USB_MATCH_START(ustir
, uaa
);
297 DPRINTFN(50,("ustir_match\n"));
299 if (uaa
->vendor
== USB_VENDOR_SIGMATEL
&&
300 uaa
->product
== USB_PRODUCT_SIGMATEL_IRDA
)
301 return UMATCH_VENDOR_PRODUCT
;
308 USB_ATTACH_START(ustir
, sc
, uaa
);
309 usbd_device_handle dev
= uaa
->device
;
310 usbd_interface_handle iface
;
312 usb_endpoint_descriptor_t
*ed
;
315 struct ir_attach_args ia
;
317 DPRINTFN(10,("ustir_attach: sc=%p\n", sc
));
324 devinfop
= usbd_devinfo_alloc(dev
, 0);
325 aprint_normal_dev(self
, "%s\n", devinfop
);
326 usbd_devinfo_free(devinfop
);
328 if (usbd_set_config_index(dev
, 0, 1)
329 || usbd_device2interface_handle(dev
, 0, &iface
)) {
330 aprint_error_dev(self
, "Configuration failed\n");
331 USB_ATTACH_ERROR_RETURN
;
335 sc
->sc_iface
= iface
;
338 (void)usbd_endpoint_count(iface
, &epcount
);
342 for (i
= 0; i
< epcount
; i
++) {
343 ed
= usbd_interface2endpoint_descriptor(iface
, i
);
345 aprint_error_dev(self
, "couldn't get ep %d\n", i
);
346 USB_ATTACH_ERROR_RETURN
;
348 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
349 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
350 sc
->sc_rd_addr
= ed
->bEndpointAddress
;
351 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
352 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
353 sc
->sc_wr_addr
= ed
->bEndpointAddress
;
356 if (sc
->sc_rd_addr
== -1 || sc
->sc_wr_addr
== -1) {
357 aprint_error_dev(self
, "missing endpoint\n");
358 USB_ATTACH_ERROR_RETURN
;
361 DPRINTFN(10, ("ustir_attach: %p\n", sc
->sc_udev
));
363 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, sc
->sc_udev
,
366 ia
.ia_type
= IR_TYPE_IRFRAME
;
367 ia
.ia_methods
= &ustir_methods
;
370 sc
->sc_child
= config_found(self
, &ia
, ir_print
);
371 selinit(&sc
->sc_rd_sel
);
372 selinit(&sc
->sc_wr_sel
);
374 USB_ATTACH_SUCCESS_RETURN
;
378 ustir_childdet(device_t self
, device_t child
)
380 struct ustir_softc
*sc
= device_private(self
);
382 KASSERT(sc
->sc_child
== child
);
388 USB_DETACH_START(ustir
, sc
);
392 DPRINTFN(0, ("ustir_detach: sc=%p flags=%d\n", sc
, flags
));
394 sc
->sc_closing
= sc
->sc_dying
= 1;
396 wakeup(&sc
->sc_thread
);
398 while (sc
->sc_thread
!= NULL
)
399 tsleep(&sc
->sc_closing
, PWAIT
, "usircl", 0);
401 /* Abort all pipes. Causes processes waiting for transfer to wake. */
402 if (sc
->sc_rd_pipe
!= NULL
) {
403 usbd_abort_pipe(sc
->sc_rd_pipe
);
404 usbd_close_pipe(sc
->sc_rd_pipe
);
405 sc
->sc_rd_pipe
= NULL
;
407 if (sc
->sc_wr_pipe
!= NULL
) {
408 usbd_abort_pipe(sc
->sc_wr_pipe
);
409 usbd_close_pipe(sc
->sc_wr_pipe
);
410 sc
->sc_wr_pipe
= NULL
;
412 wakeup(&sc
->sc_ur_framelen
);
413 wakeup(&sc
->sc_wr_buf
);
416 if (--sc
->sc_refcnt
>= 0) {
417 /* Wait for processes to go away. */
418 usb_detach_wait(USBDEV(sc
->sc_dev
));
422 if (sc
->sc_child
!= NULL
)
423 rv
= config_detach(sc
->sc_child
, flags
);
425 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->sc_udev
,
428 seldestroy(&sc
->sc_rd_sel
);
429 seldestroy(&sc
->sc_wr_sel
);
435 deframe_clear(struct framestate
*fstate
)
437 fstate
->bufindex
= 0;
438 fstate
->fsmstate
= FSTATE_END_OF_FRAME
;
443 deframe_init(struct framestate
*fstate
, struct framedefn
const *definition
,
444 u_int8_t
*buf
, size_t buflen
)
446 fstate
->definition
= definition
;
447 fstate
->buffer
= buf
;
448 fstate
->buflen
= buflen
;
450 deframe_clear(fstate
);
453 Static
enum frameresult
454 deframe_process(struct framestate
*fstate
, u_int8_t
const **bptr
, size_t *blen
)
456 struct framedefn
const *definition
;
457 u_int8_t
const *cptr
;
459 size_t ibuflen
, obufindex
, obuflen
;
460 enum framefsmstate fsmstate
;
461 enum frameresult result
;
464 fsmstate
= fstate
->fsmstate
;
465 definition
= fstate
->definition
;
466 escchr
= definition
->esc_byte
;
467 obufindex
= fstate
->bufindex
;
468 obuflen
= fstate
->buflen
;
471 while (ibuflen
-- > 0) {
476 if (fstate
->escaped
) {
478 chr
^= definition
->esc_xor
;
479 } else if (chr
== escchr
) {
486 if (chr
== definition
->eof_byte
) {
487 fsmstate
= FSTATE_IN_END
;
488 fstate
->state_index
= definition
->eof_count
;
491 if (obufindex
>= obuflen
) {
492 result
= FR_BUFFEROVERRUN
;
493 fsmstate
= FSTATE_END_OF_FRAME
;
496 fstate
->buffer
[obufindex
++] = chr
;
503 if (--fstate
->state_index
== 0) {
507 fsmstate
= FSTATE_END_OF_FRAME
;
509 fcslen
= definition
->fcs_count
;
511 if (obufindex
< fcslen
) {
512 result
= FR_FRAMEMALFORMED
;
517 fcs_calc(definition
->fcs_init
,
518 fstate
->buffer
, obufindex
);
520 /* Remove check bytes from buffer length */
523 if (crc
== definition
->fcs_correct
)
526 result
= FR_FRAMEBADFCS
;
532 case FSTATE_END_OF_FRAME
:
533 if (chr
!= definition
->bof_byte
)
536 fsmstate
= FSTATE_START_OF_FRAME
;
537 fstate
->state_index
= definition
->bof_count
;
539 case FSTATE_START_OF_FRAME
:
540 if (--fstate
->state_index
== 0) {
541 fsmstate
= FSTATE_IN_DATA
;
548 result
= (fsmstate
== FSTATE_END_OF_FRAME
) ? FR_IDLE
: FR_INPROGRESS
;
551 fstate
->bufindex
= obufindex
;
552 fstate
->fsmstate
= fsmstate
;
558 /* Returns 0 if more data required, 1 if a complete frame was extracted */
560 deframe_rd_ur(struct ustir_softc
*sc
)
562 while (sc
->sc_rd_index
< sc
->sc_rd_count
) {
565 enum frameresult fresult
;
567 buf
= &sc
->sc_rd_buf
[sc
->sc_rd_index
];
568 buflen
= sc
->sc_rd_count
- sc
->sc_rd_index
;
570 fresult
= deframe_process(&sc
->sc_framestate
, &buf
, &buflen
);
572 sc
->sc_rd_index
= sc
->sc_rd_count
- buflen
;
574 DPRINTFN(1,("%s: result=%d\n", __func__
, (int)fresult
));
580 case FR_FRAMEMALFORMED
:
581 case FR_BUFFEROVERRUN
:
584 sc
->sc_ur_framelen
= sc
->sc_framestate
.bufindex
;
585 wakeup(&sc
->sc_ur_framelen
); /* XXX should use flag */
586 selnotify(&sc
->sc_rd_sel
, 0, 0);
591 /* Reset indices into USB-side buffer */
592 sc
->sc_rd_index
= sc
->sc_rd_count
= 0;
598 * Direction transitions:
600 * ustir_periodic() can switch the direction from:
607 * ustir_rd_cb() can switch the direction from:
612 * ustir_write() can switch the direction from:
617 ustir_periodic(struct ustir_softc
*sc
)
619 DPRINTFN(60, ("%s: direction = %d\n",
620 __func__
, sc
->sc_direction
));
622 if (sc
->sc_direction
== udir_output
||
623 sc
->sc_direction
== udir_stalled
) {
627 DPRINTFN(60, ("%s: reading status register\n",
630 err
= ustir_read_reg(sc
, STIR_REG_STATUS
,
632 if (err
!= USBD_NORMAL_COMPLETION
) {
633 aprint_error_dev(sc
->sc_dev
,
634 "status register read failed: %s\n",
637 DPRINTFN(10, ("%s: status register = 0x%x\n",
639 (unsigned int)regval
));
640 if (sc
->sc_direction
== udir_output
&&
641 !(regval
& STIR_RSTATUS_FFDIR
))
642 /* Output has completed */
643 sc
->sc_direction
= udir_idle
;
644 if (regval
& STIR_RSTATUS_FFOVER
) {
646 * On an overrun the FIFO hangs, and
647 * any data bulk transfers will stall.
650 sc
->sc_direction
= udir_stalled
;
652 DPRINTFN(10, ("%s: clearing FIFO error\n",
655 err
= ustir_write_reg(sc
, STIR_REG_STATUS
,
657 /* XXX if we fail partway through
658 * this, we may not recover? */
659 if (err
== USBD_NORMAL_COMPLETION
)
660 err
= ustir_write_reg(sc
,
663 if (err
!= USBD_NORMAL_COMPLETION
) {
664 aprint_error_dev(sc
->sc_dev
,
665 "FIFO reset failed: %s\n",
669 sc
->sc_direction
= udir_idle
;
675 if (sc
->sc_wr_stalewrite
&& sc
->sc_direction
== udir_idle
) {
677 * In a stale write case, we need to check if the
678 * write has completed. Once that has happened, the
679 * write is no longer stale.
681 * But note that we may immediately start a read poll...
683 sc
->sc_wr_stalewrite
= 0;
684 wakeup(&sc
->sc_wr_buf
);
687 if (!sc
->sc_rd_readinprogress
&&
688 (sc
->sc_direction
== udir_idle
||
689 sc
->sc_direction
== udir_input
))
690 /* Do a read poll if appropriate... */
691 ustir_start_read(sc
);
695 ustir_thread(void *arg
)
697 struct ustir_softc
*sc
= arg
;
699 DPRINTFN(20, ("%s: starting polling thread\n", __func__
));
701 while (!sc
->sc_closing
) {
702 if (!sc
->sc_rd_readinprogress
&& !USTIR_BLOCK_RX_DATA(sc
))
705 if (!sc
->sc_closing
) {
707 error
= tsleep(&sc
->sc_thread
, PWAIT
,
709 if (error
== EWOULDBLOCK
&&
710 sc
->sc_rd_expectdataticks
> 0)
712 * After a timeout decrement the tick
713 * counter within which time we expect
714 * data to arrive if we are receiving
717 sc
->sc_rd_expectdataticks
--;
721 DPRINTFN(20, ("%s: exiting polling thread\n", __func__
));
723 sc
->sc_thread
= NULL
;
725 wakeup(&sc
->sc_closing
);
727 if (--sc
->sc_refcnt
< 0)
728 usb_detach_wakeup(USBDEV(sc
->sc_dev
));
734 ustir_rd_cb(usbd_xfer_handle xfer
, usbd_private_handle priv
,
737 struct ustir_softc
*sc
= priv
;
740 DPRINTFN(60, ("%s: sc=%p\n", __func__
, sc
));
742 /* Read is no longer in progress */
743 sc
->sc_rd_readinprogress
= 0;
745 if (status
== USBD_CANCELLED
|| sc
->sc_closing
) /* this is normal */
751 if (sc
->sc_direction
== udir_input
||
752 sc
->sc_direction
== udir_idle
) {
754 * Receive error, probably need to clear error
757 sc
->sc_direction
= udir_stalled
;
760 usbd_get_xfer_status(xfer
, NULL
, NULL
, &size
, NULL
);
764 sc
->sc_rd_count
= size
;
766 DPRINTFN(((size
> 0 || sc
->sc_rd_err
!= 0) ? 20 : 60),
767 ("%s: sc=%p size=%u, err=%d\n", __func__
,
768 sc
, size
, sc
->sc_rd_err
));
771 if (ustirdebug
>= 20 && size
> 0)
772 ustir_dumpdata(sc
->sc_rd_buf
, size
, __func__
);
775 if (!deframe_rd_ur(sc
)) {
776 if (!deframe_isclear(&sc
->sc_framestate
) && size
== 0 &&
777 sc
->sc_rd_expectdataticks
== 0) {
779 * Expected data, but didn't get it
780 * within expected time...
782 DPRINTFN(5,("%s: incoming packet timeout\n",
784 deframe_clear(&sc
->sc_framestate
);
785 } else if (size
> 0) {
787 * If we also received actual data, reset the
788 * data read timeout and wake up the possibly
791 sc
->sc_rd_expectdataticks
= 2;
792 wakeup(&sc
->sc_thread
);
797 * Check if incoming data has stopped, or that we cannot
798 * safely read any more data. In the case of the latter we
799 * must switch to idle so that a write will not block...
801 if (sc
->sc_direction
== udir_input
&&
802 ((size
== 0 && sc
->sc_rd_expectdataticks
== 0) ||
803 USTIR_BLOCK_RX_DATA(sc
))) {
804 DPRINTFN(8,("%s: idling on packet timeout, "
805 "complete frame, or no data\n", __func__
));
806 sc
->sc_direction
= udir_idle
;
808 /* Wake up for possible output */
809 wakeup(&sc
->sc_wr_buf
);
810 selnotify(&sc
->sc_wr_sel
, 0, 0);
815 ustir_start_read(struct ustir_softc
*sc
)
819 DPRINTFN(60,("%s: sc=%p, size=%d\n", __func__
, sc
,
820 sc
->sc_params
.maxsize
));
825 if (USTIR_BLOCK_RX_DATA(sc
) || deframe_rd_ur(sc
)) {
827 * Can't start reading just yet. Since we aren't
828 * going to start a read, have to switch direction to
831 sc
->sc_direction
= udir_idle
;
832 return USBD_NORMAL_COMPLETION
;
835 /* Starting a read... */
836 sc
->sc_rd_readinprogress
= 1;
837 sc
->sc_direction
= udir_input
;
841 DPRINTFN(0, ("%s: clear stall\n", __func__
));
842 usbd_clear_endpoint_stall(sc
->sc_rd_pipe
);
845 usbd_setup_xfer(sc
->sc_rd_xfer
, sc
->sc_rd_pipe
, sc
, sc
->sc_rd_buf
,
846 sc
->sc_params
.maxsize
,
847 USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
848 USBD_NO_TIMEOUT
, ustir_rd_cb
);
849 err
= usbd_transfer(sc
->sc_rd_xfer
);
850 if (err
!= USBD_IN_PROGRESS
) {
851 DPRINTFN(0, ("%s: err=%d\n", __func__
, (int)err
));
854 return USBD_NORMAL_COMPLETION
;
858 ustir_activate(device_t self
, enum devact act
)
860 struct ustir_softc
*sc
= device_private(self
);
863 case DVACT_DEACTIVATE
:
873 ustir_open(void *h
, int flag
, int mode
,
876 struct ustir_softc
*sc
= h
;
880 DPRINTFN(0, ("%s: sc=%p\n", __func__
, sc
));
882 err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_rd_addr
, 0, &sc
->sc_rd_pipe
);
883 if (err
!= USBD_NORMAL_COMPLETION
) {
887 err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_wr_addr
, 0, &sc
->sc_wr_pipe
);
888 if (err
!= USBD_NORMAL_COMPLETION
) {
892 sc
->sc_rd_xfer
= usbd_alloc_xfer(sc
->sc_udev
);
893 if (sc
->sc_rd_xfer
== NULL
) {
897 sc
->sc_wr_xfer
= usbd_alloc_xfer(sc
->sc_udev
);
898 if (sc
->sc_wr_xfer
== NULL
) {
902 sc
->sc_rd_buf
= usbd_alloc_buffer(sc
->sc_rd_xfer
,
903 IRDA_MAX_FRAME_SIZE
);
904 if (sc
->sc_rd_buf
== NULL
) {
908 sc
->sc_wr_buf
= usbd_alloc_buffer(sc
->sc_wr_xfer
,
909 IRDA_MAX_FRAME_SIZE
+ STIR_OUTPUT_HEADER_SIZE
);
910 if (sc
->sc_wr_buf
== NULL
) {
914 sc
->sc_ur_buf
= malloc(IRDA_MAX_FRAME_SIZE
, M_USBDEV
, M_NOWAIT
);
915 if (sc
->sc_ur_buf
== NULL
) {
920 sc
->sc_rd_index
= sc
->sc_rd_count
= 0;
922 sc
->sc_rd_readinprogress
= 0;
923 sc
->sc_rd_expectdataticks
= 0;
924 sc
->sc_ur_framelen
= 0;
926 sc
->sc_wr_stalewrite
= 0;
927 sc
->sc_speedrec
= NULL
;
928 sc
->sc_direction
= udir_idle
;
929 sc
->sc_params
.speed
= 0;
930 sc
->sc_params
.ebofs
= 0;
931 sc
->sc_params
.maxsize
= IRDA_MAX_FRAME_SIZE
;
933 deframe_init(&sc
->sc_framestate
, &framedef_sir
, sc
->sc_ur_buf
,
934 IRDA_MAX_FRAME_SIZE
);
936 /* Increment reference for thread */
939 error
= kthread_create(PRI_NONE
, 0, NULL
, ustir_thread
, sc
,
940 &sc
->sc_thread
, "%s", device_xname(sc
->sc_dev
));
949 usbd_free_xfer(sc
->sc_wr_xfer
);
950 sc
->sc_wr_xfer
= NULL
;
952 usbd_free_xfer(sc
->sc_rd_xfer
);
953 sc
->sc_rd_xfer
= NULL
;
955 usbd_close_pipe(sc
->sc_wr_pipe
);
956 sc
->sc_wr_pipe
= NULL
;
958 usbd_close_pipe(sc
->sc_rd_pipe
);
959 sc
->sc_rd_pipe
= NULL
;
966 ustir_close(void *h
, int flag
, int mode
,
969 struct ustir_softc
*sc
= h
;
971 DPRINTFN(0, ("%s: sc=%p\n", __func__
, sc
));
975 sc
->sc_rd_readinprogress
= 1;
978 wakeup(&sc
->sc_thread
);
980 while (sc
->sc_thread
!= NULL
)
981 tsleep(&sc
->sc_closing
, PWAIT
, "usircl", 0);
983 if (sc
->sc_rd_pipe
!= NULL
) {
984 usbd_abort_pipe(sc
->sc_rd_pipe
);
985 usbd_close_pipe(sc
->sc_rd_pipe
);
986 sc
->sc_rd_pipe
= NULL
;
988 if (sc
->sc_wr_pipe
!= NULL
) {
989 usbd_abort_pipe(sc
->sc_wr_pipe
);
990 usbd_close_pipe(sc
->sc_wr_pipe
);
991 sc
->sc_wr_pipe
= NULL
;
993 if (sc
->sc_rd_xfer
!= NULL
) {
994 usbd_free_xfer(sc
->sc_rd_xfer
);
995 sc
->sc_rd_xfer
= NULL
;
996 sc
->sc_rd_buf
= NULL
;
998 if (sc
->sc_wr_xfer
!= NULL
) {
999 usbd_free_xfer(sc
->sc_wr_xfer
);
1000 sc
->sc_wr_xfer
= NULL
;
1001 sc
->sc_wr_buf
= NULL
;
1003 if (sc
->sc_ur_buf
!= NULL
) {
1004 free(sc
->sc_ur_buf
, M_USBDEV
);
1005 sc
->sc_ur_buf
= NULL
;
1008 if (--sc
->sc_refcnt
< 0)
1009 usb_detach_wakeup(USBDEV(sc
->sc_dev
));
1016 ustir_read(void *h
, struct uio
*uio
, int flag
)
1018 struct ustir_softc
*sc
= h
;
1023 DPRINTFN(1,("%s: sc=%p\n", __func__
, sc
));
1029 if (sc
->sc_rd_buf
== NULL
)
1035 if (!sc
->sc_rd_readinprogress
&& !USTIR_BLOCK_RX_DATA(sc
))
1036 /* Possibly wake up polling thread */
1037 wakeup(&sc
->sc_thread
);
1041 while (sc
->sc_ur_framelen
== 0) {
1042 DPRINTFN(5,("%s: calling tsleep()\n", __func__
));
1043 error
= tsleep(&sc
->sc_ur_framelen
, PZERO
| PCATCH
,
1049 DPRINTFN(0, ("%s: tsleep() = %d\n",
1056 uframelen
= sc
->sc_ur_framelen
;
1057 DPRINTFN(1,("%s: sc=%p framelen=%u, hdr=0x%02x\n",
1058 __func__
, sc
, uframelen
, sc
->sc_ur_buf
[0]));
1059 if (uframelen
> uio
->uio_resid
)
1062 error
= uiomove(sc
->sc_ur_buf
, uframelen
, uio
);
1063 sc
->sc_ur_framelen
= 0;
1065 if (!deframe_rd_ur(sc
) && uframelen
> 0) {
1067 * Need to wait for another read to obtain a
1068 * complete frame... If we also obtained
1069 * actual data, wake up the possibly sleeping
1070 * thread immediately...
1072 wakeup(&sc
->sc_thread
);
1074 } while (uframelen
== 0);
1076 DPRINTFN(1,("%s: return %d\n", __func__
, error
));
1079 if (--sc
->sc_refcnt
< 0)
1080 usb_detach_wakeup(USBDEV(sc
->sc_dev
));
1086 ustir_write(void *h
, struct uio
*uio
, int flag
)
1088 struct ustir_softc
*sc
= h
;
1091 int error
, sirlength
;
1095 DPRINTFN(1,("%s: sc=%p\n", __func__
, sc
));
1101 if (sc
->sc_wr_buf
== NULL
)
1105 wrlen
= uio
->uio_resid
;
1106 if (wrlen
> sc
->sc_params
.maxsize
)
1111 if (!USTIR_BLOCK_RX_DATA(sc
)) {
1113 * If reads are not blocked, determine what action we
1114 * should potentially take...
1116 if (sc
->sc_direction
== udir_output
) {
1118 * If the last operation was an output, wait for the
1119 * polling thread to check for incoming data.
1121 sc
->sc_wr_stalewrite
= 1;
1122 wakeup(&sc
->sc_thread
);
1123 } else if (!sc
->sc_rd_readinprogress
&&
1124 (sc
->sc_direction
== udir_idle
||
1125 sc
->sc_direction
== udir_input
)) {
1126 /* If idle, check for input before outputting */
1127 ustir_start_read(sc
);
1132 while (sc
->sc_wr_stalewrite
||
1133 (sc
->sc_direction
!= udir_output
&&
1134 sc
->sc_direction
!= udir_idle
)) {
1135 DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, "
1136 "calling tsleep()\n", __func__
,
1137 sc
, sc
->sc_wr_stalewrite
, sc
->sc_direction
));
1138 error
= tsleep(&sc
->sc_wr_buf
, PZERO
| PCATCH
,
1144 DPRINTFN(0, ("%s: tsleep() = %d\n", __func__
,
1151 wrbuf
= sc
->sc_wr_buf
;
1154 wrbuf
[0] = STIR_OUTPUT_HEADER_BYTE0
;
1155 wrbuf
[1] = STIR_OUTPUT_HEADER_BYTE1
;
1157 sirlength
= irda_sir_frame(&wrbuf
[STIR_OUTPUT_HEADER_SIZE
],
1158 MAX_USTIR_OUTPUT_FRAME
-
1159 STIR_OUTPUT_HEADER_SIZE
,
1160 uio
, sc
->sc_params
.ebofs
);
1161 if (sirlength
< 0) {
1166 DPRINTFN(1, ("%s: transfer %u bytes\n", __func__
,
1167 (unsigned int)wrlen
));
1169 wrbuf
[2] = sirlength
& 0xff;
1170 wrbuf
[3] = (sirlength
>> 8) & 0xff;
1172 btlen
= STIR_OUTPUT_HEADER_SIZE
+ sirlength
;
1174 sc
->sc_direction
= udir_output
;
1177 if (ustirdebug
>= 20)
1178 ustir_dumpdata(wrbuf
, btlen
, __func__
);
1181 err
= usbd_bulk_transfer(sc
->sc_wr_xfer
, sc
->sc_wr_pipe
,
1182 USBD_FORCE_SHORT_XFER
| USBD_NO_COPY
,
1184 wrbuf
, &btlen
, "ustiwr");
1185 DPRINTFN(2, ("%s: err=%d\n", __func__
, err
));
1186 if (err
!= USBD_NORMAL_COMPLETION
) {
1187 if (err
== USBD_INTERRUPTED
)
1189 else if (err
== USBD_TIMEOUT
)
1199 if (--sc
->sc_refcnt
< 0)
1200 usb_detach_wakeup(USBDEV(sc
->sc_dev
));
1202 DPRINTFN(1,("%s: sc=%p done\n", __func__
, sc
));
1207 ustir_poll(void *h
, int events
, struct lwp
*l
)
1209 struct ustir_softc
*sc
= h
;
1212 DPRINTFN(1,("%s: sc=%p\n", __func__
, sc
));
1214 if (events
& (POLLOUT
| POLLWRNORM
)) {
1215 if (sc
->sc_direction
!= udir_input
) {
1216 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1218 DPRINTFN(2,("%s: recording write select\n",
1220 selrecord(l
, &sc
->sc_wr_sel
);
1224 if (events
& (POLLIN
| POLLRDNORM
)) {
1225 if (sc
->sc_ur_framelen
!= 0) {
1226 DPRINTFN(2,("%s: have data\n", __func__
));
1227 revents
|= events
& (POLLIN
| POLLRDNORM
);
1229 DPRINTFN(2,("%s: recording read select\n",
1231 selrecord(l
, &sc
->sc_rd_sel
);
1239 filt_ustirrdetach(struct knote
*kn
)
1241 struct ustir_softc
*sc
= kn
->kn_hook
;
1245 SLIST_REMOVE(&sc
->sc_rd_sel
.sel_klist
, kn
, knote
, kn_selnext
);
1251 filt_ustirread(struct knote
*kn
, long hint
)
1253 struct ustir_softc
*sc
= kn
->kn_hook
;
1255 kn
->kn_data
= sc
->sc_ur_framelen
;
1256 return (kn
->kn_data
> 0);
1260 filt_ustirwdetach(struct knote
*kn
)
1262 struct ustir_softc
*sc
= kn
->kn_hook
;
1266 SLIST_REMOVE(&sc
->sc_wr_sel
.sel_klist
, kn
, knote
, kn_selnext
);
1272 filt_ustirwrite(struct knote
*kn
, long hint
)
1274 struct ustir_softc
*sc
= kn
->kn_hook
;
1277 return (sc
->sc_direction
!= udir_input
);
1280 static const struct filterops ustirread_filtops
=
1281 { 1, NULL
, filt_ustirrdetach
, filt_ustirread
};
1282 static const struct filterops ustirwrite_filtops
=
1283 { 1, NULL
, filt_ustirwdetach
, filt_ustirwrite
};
1286 ustir_kqfilter(void *h
, struct knote
*kn
)
1288 struct ustir_softc
*sc
= h
;
1289 struct klist
*klist
;
1292 switch (kn
->kn_filter
) {
1294 klist
= &sc
->sc_rd_sel
.sel_klist
;
1295 kn
->kn_fop
= &ustirread_filtops
;
1298 klist
= &sc
->sc_wr_sel
.sel_klist
;
1299 kn
->kn_fop
= &ustirwrite_filtops
;
1308 SLIST_INSERT_HEAD(klist
, kn
, kn_selnext
);
1314 #ifdef USTIR_DEBUG_IOCTLS
1315 Static
int ustir_ioctl(void *h
, u_long cmd
, void *addr
, int flag
, struct lwp
*l
)
1317 struct ustir_softc
*sc
= h
;
1319 unsigned int regnum
;
1330 case USTIR_READ_REGISTER
:
1331 regnum
= *(unsigned int *)addr
;
1333 if (regnum
> STIR_MAX_REG
) {
1338 err
= ustir_read_reg(sc
, regnum
, ®data
);
1340 DPRINTFN(10, ("%s: regget(%u) = 0x%x\n", __func__
,
1341 regnum
, (unsigned int)regdata
));
1343 *(unsigned int *)addr
= regdata
;
1344 if (err
!= USBD_NORMAL_COMPLETION
) {
1345 printf("%s: register read failed: %s\n",
1346 USBDEVNAME(sc
->sc_dev
),
1352 case USTIR_WRITE_REGISTER
:
1353 regnum
= *(unsigned int *)addr
;
1354 regdata
= (regnum
>> 8) & 0xff;
1355 regnum
= regnum
& 0xff;
1357 if (regnum
> STIR_MAX_REG
) {
1362 DPRINTFN(10, ("%s: regset(%u, 0x%x)\n", __func__
,
1363 regnum
, (unsigned int)regdata
));
1365 err
= ustir_write_reg(sc
, regnum
, regdata
);
1366 if (err
!= USBD_NORMAL_COMPLETION
) {
1367 printf("%s: register write failed: %s\n",
1368 USBDEVNAME(sc
->sc_dev
),
1374 case USTIR_DEBUG_LEVEL
:
1376 ustirdebug
= *(int *)addr
;
1380 case USTIR_DEBUG_OPERATION
:
1388 if (--sc
->sc_refcnt
< 0)
1389 usb_detach_wakeup(USBDEV(sc
->sc_dev
));
1396 ustir_set_params(void *h
, struct irda_params
*p
)
1398 struct ustir_softc
*sc
= h
;
1399 struct ustir_speedrec
const *speedblk
;
1402 DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__
,
1403 sc
, p
->speed
, p
->ebofs
, p
->maxsize
));
1410 if (sc
->sc_speedrec
== NULL
|| p
->speed
!= sc
->sc_speedrec
->speed
) {
1412 for (i
= 0; i
< USTIR_NSPEEDS
; i
++) {
1413 if (ustir_speeds
[i
].speed
== p
->speed
) {
1414 speedblk
= &ustir_speeds
[i
];
1418 /* no good value found */
1423 if (p
->maxsize
!= sc
->sc_params
.maxsize
) {
1424 if (p
->maxsize
> IRDA_MAX_FRAME_SIZE
)
1426 sc
->sc_params
.maxsize
= p
->maxsize
;
1431 if (speedblk
!= NULL
) {
1436 sc
->sc_speedrec
= speedblk
;
1438 regmode
= STIR_BRMODE_MODEREG(speedblk
->config
);
1439 regbrate
= STIR_BRMODE_BRATEREG(speedblk
->config
);
1442 * FFSPRST must be set to enable the FIFO.
1444 regmode
|= STIR_RMODE_FFSPRST
;
1446 DPRINTFN(10, ("%s: setting BRATE = %x\n", __func__
,
1447 (unsigned int)regbrate
));
1448 err
= ustir_write_reg(sc
, STIR_REG_BRATE
, regbrate
);
1449 if (err
== USBD_NORMAL_COMPLETION
) {
1450 DPRINTFN(10, ("%s: setting MODE = %x\n", __func__
,
1451 (unsigned int)regmode
));
1452 err
= ustir_write_reg(sc
, STIR_REG_MODE
, regmode
);
1454 if (err
!= USBD_NORMAL_COMPLETION
) {
1455 DPRINTFN(10, ("%s: error setting register: %s\n",
1456 __func__
, usbd_errstr(err
)));
1465 ustir_get_speeds(void *h
, int *speeds
)
1467 struct ustir_softc
*sc
= h
;
1469 DPRINTFN(0, ("%s: sc=%p\n", __func__
, sc
));
1474 /* All these speeds are supported */
1475 *speeds
= IRDA_SPEED_4000000
|
1476 IRDA_SPEED_1152000
|
1489 ustir_get_turnarounds(void *h
, int *turnarounds
)
1491 struct ustir_softc
*sc
= h
;
1493 DPRINTFN(0, ("%s: sc=%p\n", __func__
, sc
));
1499 * Documentation is on the light side with respect to
1500 * turnaround time for this device.
1502 *turnarounds
= IRDA_TURNT_10000
;