]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/usb/serial/ubsa.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / dev / usb / serial / ubsa.c
1 /*-
2  * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 /*-
30  * Copyright (c) 2001 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to The NetBSD Foundation
34  * by Ichiro FUKUHARA (ichiro@ichiro.org).
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *        This product includes software developed by the NetBSD
47  *        Foundation, Inc. and its contributors.
48  * 4. Neither the name of The NetBSD Foundation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  */
64
65 #include <sys/stdint.h>
66 #include <sys/stddef.h>
67 #include <sys/param.h>
68 #include <sys/queue.h>
69 #include <sys/types.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/bus.h>
73 #include <sys/linker_set.h>
74 #include <sys/module.h>
75 #include <sys/lock.h>
76 #include <sys/mutex.h>
77 #include <sys/condvar.h>
78 #include <sys/sysctl.h>
79 #include <sys/sx.h>
80 #include <sys/unistd.h>
81 #include <sys/callout.h>
82 #include <sys/malloc.h>
83 #include <sys/priv.h>
84
85 #include <dev/usb/usb.h>
86 #include <dev/usb/usbdi.h>
87 #include <dev/usb/usbdi_util.h>
88 #include "usbdevs.h"
89
90 #define USB_DEBUG_VAR ubsa_debug
91 #include <dev/usb/usb_debug.h>
92 #include <dev/usb/usb_process.h>
93
94 #include <dev/usb/serial/usb_serial.h>
95
96 #if USB_DEBUG
97 static int ubsa_debug = 0;
98
99 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
100 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
101     &ubsa_debug, 0, "ubsa debug level");
102 #endif
103
104 #define UBSA_BSIZE             1024     /* bytes */
105
106 #define UBSA_CONFIG_INDEX       0
107 #define UBSA_IFACE_INDEX        0
108
109 #define UBSA_REG_BAUDRATE       0x00
110 #define UBSA_REG_STOP_BITS      0x01
111 #define UBSA_REG_DATA_BITS      0x02
112 #define UBSA_REG_PARITY         0x03
113 #define UBSA_REG_DTR            0x0A
114 #define UBSA_REG_RTS            0x0B
115 #define UBSA_REG_BREAK          0x0C
116 #define UBSA_REG_FLOW_CTRL      0x10
117
118 #define UBSA_PARITY_NONE        0x00
119 #define UBSA_PARITY_EVEN        0x01
120 #define UBSA_PARITY_ODD         0x02
121 #define UBSA_PARITY_MARK        0x03
122 #define UBSA_PARITY_SPACE       0x04
123
124 #define UBSA_FLOW_NONE          0x0000
125 #define UBSA_FLOW_OCTS          0x0001
126 #define UBSA_FLOW_ODSR          0x0002
127 #define UBSA_FLOW_IDSR          0x0004
128 #define UBSA_FLOW_IDTR          0x0008
129 #define UBSA_FLOW_IRTS          0x0010
130 #define UBSA_FLOW_ORTS          0x0020
131 #define UBSA_FLOW_UNKNOWN       0x0040
132 #define UBSA_FLOW_OXON          0x0080
133 #define UBSA_FLOW_IXON          0x0100
134
135 /* line status register */
136 #define UBSA_LSR_TSRE           0x40    /* Transmitter empty: byte sent */
137 #define UBSA_LSR_TXRDY          0x20    /* Transmitter buffer empty */
138 #define UBSA_LSR_BI             0x10    /* Break detected */
139 #define UBSA_LSR_FE             0x08    /* Framing error: bad stop bit */
140 #define UBSA_LSR_PE             0x04    /* Parity error */
141 #define UBSA_LSR_OE             0x02    /* Overrun, lost incoming byte */
142 #define UBSA_LSR_RXRDY          0x01    /* Byte ready in Receive Buffer */
143 #define UBSA_LSR_RCV_MASK       0x1f    /* Mask for incoming data or error */
144
145 /* modem status register */
146 /* All deltas are from the last read of the MSR. */
147 #define UBSA_MSR_DCD            0x80    /* Current Data Carrier Detect */
148 #define UBSA_MSR_RI             0x40    /* Current Ring Indicator */
149 #define UBSA_MSR_DSR            0x20    /* Current Data Set Ready */
150 #define UBSA_MSR_CTS            0x10    /* Current Clear to Send */
151 #define UBSA_MSR_DDCD           0x08    /* DCD has changed state */
152 #define UBSA_MSR_TERI           0x04    /* RI has toggled low to high */
153 #define UBSA_MSR_DDSR           0x02    /* DSR has changed state */
154 #define UBSA_MSR_DCTS           0x01    /* CTS has changed state */
155
156 enum {
157         UBSA_BULK_DT_WR,
158         UBSA_BULK_DT_RD,
159         UBSA_INTR_DT_RD,
160         UBSA_N_TRANSFER,
161 };
162
163 struct ubsa_softc {
164         struct ucom_super_softc sc_super_ucom;
165         struct ucom_softc sc_ucom;
166
167         struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
168         struct usb_device *sc_udev;
169         struct mtx sc_mtx;
170
171         uint8_t sc_iface_no;            /* interface number */
172         uint8_t sc_iface_index;         /* interface index */
173         uint8_t sc_lsr;                 /* local status register */
174         uint8_t sc_msr;                 /* UBSA status register */
175 };
176
177 static device_probe_t ubsa_probe;
178 static device_attach_t ubsa_attach;
179 static device_detach_t ubsa_detach;
180
181 static usb_callback_t ubsa_write_callback;
182 static usb_callback_t ubsa_read_callback;
183 static usb_callback_t ubsa_intr_callback;
184
185 static void     ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
186 static void     ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
187 static void     ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
188 static void     ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
189 static int      ubsa_pre_param(struct ucom_softc *, struct termios *);
190 static void     ubsa_cfg_param(struct ucom_softc *, struct termios *);
191 static void     ubsa_start_read(struct ucom_softc *);
192 static void     ubsa_stop_read(struct ucom_softc *);
193 static void     ubsa_start_write(struct ucom_softc *);
194 static void     ubsa_stop_write(struct ucom_softc *);
195 static void     ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
196                     uint8_t *);
197
198 static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
199
200         [UBSA_BULK_DT_WR] = {
201                 .type = UE_BULK,
202                 .endpoint = UE_ADDR_ANY,
203                 .direction = UE_DIR_OUT,
204                 .bufsize = UBSA_BSIZE,  /* bytes */
205                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
206                 .callback = &ubsa_write_callback,
207         },
208
209         [UBSA_BULK_DT_RD] = {
210                 .type = UE_BULK,
211                 .endpoint = UE_ADDR_ANY,
212                 .direction = UE_DIR_IN,
213                 .bufsize = UBSA_BSIZE,  /* bytes */
214                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
215                 .callback = &ubsa_read_callback,
216         },
217
218         [UBSA_INTR_DT_RD] = {
219                 .type = UE_INTERRUPT,
220                 .endpoint = UE_ADDR_ANY,
221                 .direction = UE_DIR_IN,
222                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
223                 .bufsize = 0,   /* use wMaxPacketSize */
224                 .callback = &ubsa_intr_callback,
225         },
226 };
227
228 static const struct ucom_callback ubsa_callback = {
229         .ucom_cfg_get_status = &ubsa_cfg_get_status,
230         .ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
231         .ucom_cfg_set_rts = &ubsa_cfg_set_rts,
232         .ucom_cfg_set_break = &ubsa_cfg_set_break,
233         .ucom_cfg_param = &ubsa_cfg_param,
234         .ucom_pre_param = &ubsa_pre_param,
235         .ucom_start_read = &ubsa_start_read,
236         .ucom_stop_read = &ubsa_stop_read,
237         .ucom_start_write = &ubsa_start_write,
238         .ucom_stop_write = &ubsa_stop_write,
239 };
240
241 static const struct usb_device_id ubsa_devs[] = {
242         /* AnyData ADU-500A */
243         {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
244         /* AnyData ADU-E100A/H */
245         {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
246         /* Axesstel MV100H */
247         {USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
248         /* BELKIN F5U103 */
249         {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
250         /* BELKIN F5U120 */
251         {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
252         /* GoHubs GO-COM232 */
253         {USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
254         /* GoHubs GO-COM232 */
255         {USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
256         /* Peracom */
257         {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
258 };
259
260 static device_method_t ubsa_methods[] = {
261         DEVMETHOD(device_probe, ubsa_probe),
262         DEVMETHOD(device_attach, ubsa_attach),
263         DEVMETHOD(device_detach, ubsa_detach),
264         {0, 0}
265 };
266
267 static devclass_t ubsa_devclass;
268
269 static driver_t ubsa_driver = {
270         .name = "ubsa",
271         .methods = ubsa_methods,
272         .size = sizeof(struct ubsa_softc),
273 };
274
275 DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
276 MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
277 MODULE_DEPEND(ubsa, usb, 1, 1, 1);
278
279 static int
280 ubsa_probe(device_t dev)
281 {
282         struct usb_attach_arg *uaa = device_get_ivars(dev);
283
284         if (uaa->usb_mode != USB_MODE_HOST) {
285                 return (ENXIO);
286         }
287         if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
288                 return (ENXIO);
289         }
290         if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
291                 return (ENXIO);
292         }
293         return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
294 }
295
296 static int
297 ubsa_attach(device_t dev)
298 {
299         struct usb_attach_arg *uaa = device_get_ivars(dev);
300         struct ubsa_softc *sc = device_get_softc(dev);
301         int error;
302
303         DPRINTF("sc=%p\n", sc);
304
305         device_set_usb_desc(dev);
306         mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
307
308         sc->sc_udev = uaa->device;
309         sc->sc_iface_no = uaa->info.bIfaceNum;
310         sc->sc_iface_index = UBSA_IFACE_INDEX;
311
312         error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
313             sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
314
315         if (error) {
316                 DPRINTF("could not allocate all pipes\n");
317                 goto detach;
318         }
319         /* clear stall at first run */
320         mtx_lock(&sc->sc_mtx);
321         usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
322         usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
323         mtx_unlock(&sc->sc_mtx);
324
325         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
326             &ubsa_callback, &sc->sc_mtx);
327         if (error) {
328                 DPRINTF("ucom_attach failed\n");
329                 goto detach;
330         }
331         return (0);
332
333 detach:
334         ubsa_detach(dev);
335         return (ENXIO);
336 }
337
338 static int
339 ubsa_detach(device_t dev)
340 {
341         struct ubsa_softc *sc = device_get_softc(dev);
342
343         DPRINTF("sc=%p\n", sc);
344
345         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
346         usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
347         mtx_destroy(&sc->sc_mtx);
348
349         return (0);
350 }
351
352 static void
353 ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
354 {
355         struct usb_device_request req;
356         usb_error_t err;
357
358         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
359         req.bRequest = index;
360         USETW(req.wValue, value);
361         req.wIndex[0] = sc->sc_iface_no;
362         req.wIndex[1] = 0;
363         USETW(req.wLength, 0);
364
365         err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
366             &req, NULL, 0, 1000);
367         if (err) {
368                 DPRINTFN(0, "device request failed, err=%s "
369                     "(ignored)\n", usbd_errstr(err));
370         }
371 }
372
373 static void
374 ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
375 {
376         struct ubsa_softc *sc = ucom->sc_parent;
377
378         DPRINTF("onoff = %d\n", onoff);
379
380         ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
381 }
382
383 static void
384 ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
385 {
386         struct ubsa_softc *sc = ucom->sc_parent;
387
388         DPRINTF("onoff = %d\n", onoff);
389
390         ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
391 }
392
393 static void
394 ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
395 {
396         struct ubsa_softc *sc = ucom->sc_parent;
397
398         DPRINTF("onoff = %d\n", onoff);
399
400         ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
401 }
402
403 static int
404 ubsa_pre_param(struct ucom_softc *ucom, struct termios *t)
405 {
406         struct ubsa_softc *sc = ucom->sc_parent;
407
408         DPRINTF("sc = %p\n", sc);
409
410         switch (t->c_ospeed) {
411         case B0:
412         case B300:
413         case B600:
414         case B1200:
415         case B2400:
416         case B4800:
417         case B9600:
418         case B19200:
419         case B38400:
420         case B57600:
421         case B115200:
422         case B230400:
423                 break;
424         default:
425                 return (EINVAL);
426         }
427         return (0);
428 }
429
430 static void
431 ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t)
432 {
433         struct ubsa_softc *sc = ucom->sc_parent;
434         uint16_t value = 0;
435
436         DPRINTF("sc = %p\n", sc);
437
438         switch (t->c_ospeed) {
439         case B0:
440                 ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
441                 ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
442                 ubsa_cfg_set_rts(&sc->sc_ucom, 0);
443                 break;
444         case B300:
445         case B600:
446         case B1200:
447         case B2400:
448         case B4800:
449         case B9600:
450         case B19200:
451         case B38400:
452         case B57600:
453         case B115200:
454         case B230400:
455                 value = B230400 / t->c_ospeed;
456                 ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
457                 break;
458         default:
459                 return;
460         }
461
462         if (t->c_cflag & PARENB)
463                 value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
464         else
465                 value = UBSA_PARITY_NONE;
466
467         ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
468
469         switch (t->c_cflag & CSIZE) {
470         case CS5:
471                 value = 0;
472                 break;
473         case CS6:
474                 value = 1;
475                 break;
476         case CS7:
477                 value = 2;
478                 break;
479         default:
480         case CS8:
481                 value = 3;
482                 break;
483         }
484
485         ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
486
487         value = (t->c_cflag & CSTOPB) ? 1 : 0;
488
489         ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
490
491         value = 0;
492         if (t->c_cflag & CRTSCTS)
493                 value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
494
495         if (t->c_iflag & (IXON | IXOFF))
496                 value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
497
498         ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
499 }
500
501 static void
502 ubsa_start_read(struct ucom_softc *ucom)
503 {
504         struct ubsa_softc *sc = ucom->sc_parent;
505
506         /* start interrupt endpoint */
507         usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
508
509         /* start read endpoint */
510         usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
511 }
512
513 static void
514 ubsa_stop_read(struct ucom_softc *ucom)
515 {
516         struct ubsa_softc *sc = ucom->sc_parent;
517
518         /* stop interrupt endpoint */
519         usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
520
521         /* stop read endpoint */
522         usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
523 }
524
525 static void
526 ubsa_start_write(struct ucom_softc *ucom)
527 {
528         struct ubsa_softc *sc = ucom->sc_parent;
529
530         usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
531 }
532
533 static void
534 ubsa_stop_write(struct ucom_softc *ucom)
535 {
536         struct ubsa_softc *sc = ucom->sc_parent;
537
538         usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
539 }
540
541 static void
542 ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
543 {
544         struct ubsa_softc *sc = ucom->sc_parent;
545
546         DPRINTF("\n");
547
548         *lsr = sc->sc_lsr;
549         *msr = sc->sc_msr;
550 }
551
552 static void
553 ubsa_write_callback(struct usb_xfer *xfer, usb_error_t error)
554 {
555         struct ubsa_softc *sc = usbd_xfer_softc(xfer);
556         struct usb_page_cache *pc;
557         uint32_t actlen;
558
559         switch (USB_GET_STATE(xfer)) {
560         case USB_ST_SETUP:
561         case USB_ST_TRANSFERRED:
562 tr_setup:
563                 pc = usbd_xfer_get_frame(xfer, 0);
564                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
565                     UBSA_BSIZE, &actlen)) {
566
567                         usbd_xfer_set_frame_len(xfer, 0, actlen);
568                         usbd_transfer_submit(xfer);
569                 }
570                 return;
571
572         default:                        /* Error */
573                 if (error != USB_ERR_CANCELLED) {
574                         /* try to clear stall first */
575                         usbd_xfer_set_stall(xfer);
576                         goto tr_setup;
577                 }
578                 return;
579
580         }
581 }
582
583 static void
584 ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error)
585 {
586         struct ubsa_softc *sc = usbd_xfer_softc(xfer);
587         struct usb_page_cache *pc;
588         int actlen;
589
590         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
591
592         switch (USB_GET_STATE(xfer)) {
593         case USB_ST_TRANSFERRED:
594                 pc = usbd_xfer_get_frame(xfer, 0);
595                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
596
597         case USB_ST_SETUP:
598 tr_setup:
599                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
600                 usbd_transfer_submit(xfer);
601                 return;
602
603         default:                        /* Error */
604                 if (error != USB_ERR_CANCELLED) {
605                         /* try to clear stall first */
606                         usbd_xfer_set_stall(xfer);
607                         goto tr_setup;
608                 }
609                 return;
610
611         }
612 }
613
614 static void
615 ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error)
616 {
617         struct ubsa_softc *sc = usbd_xfer_softc(xfer);
618         struct usb_page_cache *pc;
619         uint8_t buf[4];
620         int actlen;
621
622         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
623
624         switch (USB_GET_STATE(xfer)) {
625         case USB_ST_TRANSFERRED:
626
627                 if (actlen >= sizeof(buf)) {
628                         pc = usbd_xfer_get_frame(xfer, 0);
629                         usbd_copy_out(pc, 0, buf, sizeof(buf));
630
631                         /*
632                          * incidentally, Belkin adapter status bits match
633                          * UART 16550 bits
634                          */
635                         sc->sc_lsr = buf[2];
636                         sc->sc_msr = buf[3];
637
638                         DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
639                             sc->sc_lsr, sc->sc_msr);
640
641                         ucom_status_change(&sc->sc_ucom);
642                 } else {
643                         DPRINTF("ignoring short packet, %d bytes\n", actlen);
644                 }
645
646         case USB_ST_SETUP:
647 tr_setup:
648                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
649                 usbd_transfer_submit(xfer);
650                 return;
651
652         default:                        /* Error */
653                 if (error != USB_ERR_CANCELLED) {
654                         /* try to clear stall first */
655                         usbd_xfer_set_stall(xfer);
656                         goto tr_setup;
657                 }
658                 return;
659
660         }
661 }