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