]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/usb/ubsa.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / usb / 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/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/module.h>
70 #include <sys/bus.h>
71 #include <sys/ioccom.h>
72 #include <sys/fcntl.h>
73 #include <sys/taskqueue.h>
74 #include <sys/conf.h>
75 #include <sys/tty.h>
76 #include <sys/file.h>
77 #include <sys/selinfo.h>
78 #include <sys/proc.h>
79 #include <sys/poll.h>
80 #include <sys/sysctl.h>
81
82 #include <dev/usb/usb.h>
83 #include <dev/usb/usbcdc.h>
84
85 #include <dev/usb/usbdi.h>
86 #include <dev/usb/usbdi_util.h>
87 #include "usbdevs.h"
88 #include <dev/usb/usb_quirks.h>
89
90 #include <dev/usb/ucomvar.h>
91
92 #ifdef USB_DEBUG
93 static int      ubsadebug = 0;
94 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
95 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
96            &ubsadebug, 0, "ubsa debug level");
97
98 #define DPRINTFN(n, x)  do { \
99                                 if (ubsadebug > (n)) \
100                                         printf x; \
101                         } while (0)
102 #else
103 #define DPRINTFN(n, x)
104 #endif
105 #define DPRINTF(x) DPRINTFN(0, x)
106
107 #define UBSA_MODVER             1       /* module version */
108
109 #define UBSA_CONFIG_INDEX       1
110 #define UBSA_IFACE_INDEX        0
111
112 #define UBSA_INTR_INTERVAL      100     /* ms */
113
114 #define UBSA_SET_BAUDRATE       0x00
115 #define UBSA_SET_STOP_BITS      0x01
116 #define UBSA_SET_DATA_BITS      0x02
117 #define UBSA_SET_PARITY         0x03
118 #define UBSA_SET_DTR            0x0A
119 #define UBSA_SET_RTS            0x0B
120 #define UBSA_SET_BREAK          0x0C
121 #define UBSA_SET_FLOW_CTRL      0x10
122
123 #define UBSA_PARITY_NONE        0x00
124 #define UBSA_PARITY_EVEN        0x01
125 #define UBSA_PARITY_ODD         0x02
126 #define UBSA_PARITY_MARK        0x03
127 #define UBSA_PARITY_SPACE       0x04
128
129 #define UBSA_FLOW_NONE          0x0000
130 #define UBSA_FLOW_OCTS          0x0001
131 #define UBSA_FLOW_ODSR          0x0002
132 #define UBSA_FLOW_IDSR          0x0004
133 #define UBSA_FLOW_IDTR          0x0008
134 #define UBSA_FLOW_IRTS          0x0010
135 #define UBSA_FLOW_ORTS          0x0020
136 #define UBSA_FLOW_UNKNOWN       0x0040
137 #define UBSA_FLOW_OXON          0x0080
138 #define UBSA_FLOW_IXON          0x0100
139
140 /* line status register */
141 #define UBSA_LSR_TSRE           0x40    /* Transmitter empty: byte sent */
142 #define UBSA_LSR_TXRDY          0x20    /* Transmitter buffer empty */
143 #define UBSA_LSR_BI             0x10    /* Break detected */
144 #define UBSA_LSR_FE             0x08    /* Framing error: bad stop bit */
145 #define UBSA_LSR_PE             0x04    /* Parity error */
146 #define UBSA_LSR_OE             0x02    /* Overrun, lost incoming byte */
147 #define UBSA_LSR_RXRDY          0x01    /* Byte ready in Receive Buffer */
148 #define UBSA_LSR_RCV_MASK       0x1f    /* Mask for incoming data or error */
149
150 /* modem status register */
151 /* All deltas are from the last read of the MSR. */
152 #define UBSA_MSR_DCD            0x80    /* Current Data Carrier Detect */
153 #define UBSA_MSR_RI             0x40    /* Current Ring Indicator */
154 #define UBSA_MSR_DSR            0x20    /* Current Data Set Ready */
155 #define UBSA_MSR_CTS            0x10    /* Current Clear to Send */
156 #define UBSA_MSR_DDCD           0x08    /* DCD has changed state */
157 #define UBSA_MSR_TERI           0x04    /* RI has toggled low to high */
158 #define UBSA_MSR_DDSR           0x02    /* DSR has changed state */
159 #define UBSA_MSR_DCTS           0x01    /* CTS has changed state */
160
161 struct  ubsa_softc {
162         struct ucom_softc       sc_ucom;
163
164         int                     sc_iface_number;        /* interface number */
165
166         usbd_interface_handle   sc_intr_iface;  /* interrupt interface */
167         int                     sc_intr_number; /* interrupt number */
168         usbd_pipe_handle        sc_intr_pipe;   /* interrupt pipe */
169         u_char                  *sc_intr_buf;   /* interrupt buffer */
170         int                     sc_isize;
171
172         u_char                  sc_dtr;         /* current DTR state */
173         u_char                  sc_rts;         /* current RTS state */
174
175         u_char                  sc_lsr;         /* Local status register */
176         u_char                  sc_msr;         /* ubsa status register */
177         struct task             sc_task;
178 };
179
180 static  void ubsa_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
181 static  void ubsa_notify(void *, int count);
182
183 static  void ubsa_get_status(void *, int, u_char *, u_char *);
184 static  void ubsa_set(void *, int, int, int);
185 static  int  ubsa_param(void *, int, struct termios *);
186 static  int  ubsa_open(void *, int);
187 static  void ubsa_close(void *, int);
188
189 static  int  ubsa_request(struct ubsa_softc *, u_int8_t, u_int16_t);
190 static  void ubsa_dtr(struct ubsa_softc *, int);
191 static  void ubsa_rts(struct ubsa_softc *, int);
192 static  void ubsa_baudrate(struct ubsa_softc *, speed_t);
193 static  void ubsa_parity(struct ubsa_softc *, tcflag_t);
194 static  void ubsa_databits(struct ubsa_softc *, tcflag_t);
195 static  void ubsa_stopbits(struct ubsa_softc *, tcflag_t);
196 static  void ubsa_flow(struct ubsa_softc *, tcflag_t, tcflag_t);
197
198 struct ucom_callback ubsa_callback = {
199         ubsa_get_status,
200         ubsa_set,
201         ubsa_param,
202         NULL,
203         ubsa_open,
204         ubsa_close,
205         NULL,
206         NULL
207 };
208
209 static const struct ubsa_product {
210         uint16_t        vendor;
211         uint16_t        product;
212 } ubsa_products [] = {
213         /* AnyData ADU-500A */
214         { USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A },
215         /* AnyData ADU-E100A/H */
216         { USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X },
217         /* Axesstel MV100H */
218         { USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM },
219         /* BELKIN F5U103 */
220         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
221         /* BELKIN F5U120 */
222         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
223         /* GoHubs GO-COM232 */
224         { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
225         /* GoHubs GO-COM232 */
226         { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
227         /* Peracom */
228         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
229         { 0, 0 }
230 };
231
232 static device_probe_t ubsa_match;
233 static device_attach_t ubsa_attach;
234 static device_detach_t ubsa_detach;
235
236 static device_method_t ubsa_methods[] = {
237         /* Device interface */
238         DEVMETHOD(device_probe, ubsa_match),
239         DEVMETHOD(device_attach, ubsa_attach),
240         DEVMETHOD(device_detach, ubsa_detach),
241         { 0, 0 }
242 };
243
244 static driver_t ubsa_driver = {
245         "ucom",
246         ubsa_methods,
247         sizeof (struct ubsa_softc)
248 };
249
250 DRIVER_MODULE(ubsa, uhub, ubsa_driver, ucom_devclass, usbd_driver_load, 0);
251 MODULE_DEPEND(ubsa, usb, 1, 1, 1);
252 MODULE_DEPEND(ubsa, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
253 MODULE_VERSION(ubsa, UBSA_MODVER);
254
255 static int
256 ubsa_match(device_t self)
257 {
258         struct usb_attach_arg *uaa = device_get_ivars(self);
259         int i;
260
261         if (uaa->iface != NULL)
262                 return (UMATCH_NONE);
263
264         for (i = 0; ubsa_products[i].vendor != 0; i++) {
265                 if (ubsa_products[i].vendor == uaa->vendor &&
266                     ubsa_products[i].product == uaa->product) {
267                         return (UMATCH_VENDOR_PRODUCT);
268                 }
269         }
270         return (UMATCH_NONE);
271 }
272
273 static int
274 ubsa_attach(device_t self)
275 {
276         struct ubsa_softc *sc = device_get_softc(self);
277         struct usb_attach_arg *uaa = device_get_ivars(self);
278         usbd_device_handle dev;
279         struct ucom_softc *ucom;
280         usb_config_descriptor_t *cdesc;
281         usb_interface_descriptor_t *id;
282         usb_endpoint_descriptor_t *ed;
283         usbd_status err;
284         int i;
285
286         dev = uaa->device;
287         ucom = &sc->sc_ucom;
288
289         /*
290          * initialize rts, dtr variables to something
291          * different from boolean 0, 1
292          */
293         sc->sc_dtr = -1;
294         sc->sc_rts = -1;
295
296         ucom->sc_dev = self;
297         ucom->sc_udev = dev;
298         ucom->sc_iface = uaa->iface;
299
300         DPRINTF(("ubsa attach: sc = %p\n", sc));
301
302         /* initialize endpoints */
303         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
304         sc->sc_intr_number = -1;
305         sc->sc_intr_pipe = NULL;
306
307         /* Move the device into the configured state. */
308         err = usbd_set_config_index(dev, UBSA_CONFIG_INDEX, 1);
309         if (err) {
310                 device_printf(ucom->sc_dev, "failed to set configuration: %s\n",
311                     usbd_errstr(err));
312                 ucom->sc_dying = 1;
313                 goto error;
314         }
315
316         /* get the config descriptor */
317         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
318
319         if (cdesc == NULL) {
320                 device_printf(ucom->sc_dev,
321                     "failed to get configuration descriptor\n");
322                 ucom->sc_dying = 1;
323                 goto error;
324         }
325
326         /* get the first interface */
327         err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX,
328             &ucom->sc_iface);
329         if (err) {
330                 device_printf(ucom->sc_dev, "failed to get interface: %s\n",
331                     usbd_errstr(err));
332                 ucom->sc_dying = 1;
333                 goto error;
334         }
335
336         /* Find the endpoints */
337
338         id = usbd_get_interface_descriptor(ucom->sc_iface);
339         sc->sc_iface_number = id->bInterfaceNumber;
340
341         for (i = 0; i < id->bNumEndpoints; i++) {
342                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
343                 if (ed == NULL) {
344                         device_printf(ucom->sc_dev,
345                             "no endpoint descriptor for %d\n", i);
346                         ucom->sc_dying = 1;
347                         goto error;
348                 }
349
350                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
351                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
352                         sc->sc_intr_number = ed->bEndpointAddress;
353                         sc->sc_isize = UGETW(ed->wMaxPacketSize);
354                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
355                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
356                         ucom->sc_bulkin_no = ed->bEndpointAddress;
357                         ucom->sc_ibufsize = UGETW(ed->wMaxPacketSize);
358                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
359                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
360                         ucom->sc_bulkout_no = ed->bEndpointAddress;
361                         ucom->sc_obufsize = UGETW(ed->wMaxPacketSize);
362                 }
363         }
364
365         if (sc->sc_intr_number == -1) {
366                 device_printf(ucom->sc_dev, "Could not find interrupt in\n");
367                 ucom->sc_dying = 1;
368                 goto error;
369         }
370
371         /* keep interface for interrupt */
372         sc->sc_intr_iface = ucom->sc_iface;
373
374         if (ucom->sc_bulkin_no == -1) {
375                 device_printf(ucom->sc_dev, "Could not find data bulk in\n");
376                 ucom->sc_dying = 1;
377                 goto error;
378         }
379
380         if (ucom->sc_bulkout_no == -1) {
381                 device_printf(ucom->sc_dev, "Could not find data bulk out\n");
382                 ucom->sc_dying = 1;
383                 goto error;
384         }
385
386         ucom->sc_parent = sc;
387         ucom->sc_portno = UCOM_UNK_PORTNO;
388         /* bulkin, bulkout set above */
389         ucom->sc_ibufsize = 1024;
390         ucom->sc_obufsize = 1024;
391         ucom->sc_ibufsizepad = ucom->sc_ibufsize;
392         ucom->sc_opkthdrlen = 0;
393         ucom->sc_callback = &ubsa_callback;
394
395         DPRINTF(("ubsa: in = 0x%x, out = 0x%x, intr = 0x%x\n",
396             ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
397
398         TASK_INIT(&sc->sc_task, 0, ubsa_notify, sc); 
399         ucom_attach(ucom);
400         return 0;
401
402 error:
403         return ENXIO;
404 }
405
406 static int
407 ubsa_detach(device_t self)
408 {
409         struct ubsa_softc *sc = device_get_softc(self);
410         int rv;
411
412         DPRINTF(("ubsa_detach: sc = %p\n", sc));
413
414         if (sc->sc_intr_pipe != NULL) {
415                 usbd_abort_pipe(sc->sc_intr_pipe);
416                 usbd_close_pipe(sc->sc_intr_pipe);
417                 free(sc->sc_intr_buf, M_USBDEV);
418                 sc->sc_intr_pipe = NULL;
419         }
420
421         sc->sc_ucom.sc_dying = 1;
422 #if 0
423         taskqueue_drain(taskqueue_swi_giant);
424 #endif
425         rv = ucom_detach(&sc->sc_ucom);
426
427         return (rv);
428 }
429
430 static int
431 ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
432 {
433         usb_device_request_t req;
434         usbd_status err;
435
436         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
437         req.bRequest = request;
438         USETW(req.wValue, value);
439         USETW(req.wIndex, sc->sc_iface_number);
440         USETW(req.wLength, 0);
441
442         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
443         if (err)
444                 device_printf(sc->sc_ucom.sc_dev, "ubsa_request(%x, %x): %s\n",
445                     request, value, usbd_errstr(err));
446         return (err);
447 }
448
449 static void
450 ubsa_dtr(struct ubsa_softc *sc, int onoff)
451 {
452
453         DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
454
455         if (sc->sc_dtr == onoff)
456                 return;
457         sc->sc_dtr = onoff;
458
459         ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
460 }
461
462 static void
463 ubsa_rts(struct ubsa_softc *sc, int onoff)
464 {
465
466         DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
467
468         if (sc->sc_rts == onoff)
469                 return;
470         sc->sc_rts = onoff;
471
472         ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
473 }
474
475 static void
476 ubsa_break(struct ubsa_softc *sc, int onoff)
477 {
478
479         DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
480
481         ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
482 }
483
484 static void
485 ubsa_set(void *addr, int portno, int reg, int onoff)
486 {
487         struct ubsa_softc *sc;
488
489         sc = addr;
490         switch (reg) {
491         case UCOM_SET_DTR:
492                 ubsa_dtr(sc, onoff);
493                 break;
494         case UCOM_SET_RTS:
495                 ubsa_rts(sc, onoff);
496                 break;
497         case UCOM_SET_BREAK:
498                 ubsa_break(sc, onoff);
499                 break;
500         default:
501                 break;
502         }
503 }
504
505 static void
506 ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
507 {
508         u_int16_t value = 0;
509
510         DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
511
512         switch(speed) {
513         case B0:
514                 break;
515         case B300:
516         case B600:
517         case B1200:
518         case B2400:
519         case B4800:
520         case B9600:
521         case B19200:
522         case B38400:
523         case B57600:
524         case B115200:
525         case B230400:
526                 value = B230400 / speed;
527                 break;
528         default:
529                 device_printf(sc->sc_ucom.sc_dev,
530                     "ubsa_param: unsupported baud, forcing default of 9600\n");
531                 value = B230400 / B9600;
532                 break;
533         };
534
535         if (speed == B0) {
536                 ubsa_flow(sc, 0, 0);
537                 ubsa_dtr(sc, 0);
538                 ubsa_rts(sc, 0);
539         } else
540                 ubsa_request(sc, UBSA_SET_BAUDRATE, value);
541 }
542
543 static void
544 ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
545 {
546         int value;
547
548         DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
549
550         if (cflag & PARENB)
551                 value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
552         else
553                 value = UBSA_PARITY_NONE;
554
555         ubsa_request(sc, UBSA_SET_PARITY, value);
556 }
557
558 static void
559 ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
560 {
561         int value;
562
563         DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
564
565         switch (cflag & CSIZE) {
566         case CS5: value = 0; break;
567         case CS6: value = 1; break;
568         case CS7: value = 2; break;
569         case CS8: value = 3; break;
570         default:
571                 device_printf(sc->sc_ucom.sc_dev,
572                     "ubsa_param: unsupported databits requested, "
573                     "forcing default of 8\n");
574                 value = 3;
575         }
576
577         ubsa_request(sc, UBSA_SET_DATA_BITS, value);
578 }
579
580 static void
581 ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
582 {
583         int value;
584
585         DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
586
587         value = (cflag & CSTOPB) ? 1 : 0;
588
589         ubsa_request(sc, UBSA_SET_STOP_BITS, value);
590 }
591
592 static void
593 ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
594 {
595         int value;
596
597         DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
598
599         value = 0;
600         if (cflag & CRTSCTS)
601                 value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
602         if (iflag & (IXON|IXOFF))
603                 value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
604
605         ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
606 }
607
608 static int
609 ubsa_param(void *addr, int portno, struct termios *ti)
610 {
611         struct ubsa_softc *sc;
612
613         sc = addr;
614
615         DPRINTF(("ubsa_param: sc = %p\n", sc));
616
617         ubsa_baudrate(sc, ti->c_ospeed);
618         ubsa_parity(sc, ti->c_cflag);
619         ubsa_databits(sc, ti->c_cflag);
620         ubsa_stopbits(sc, ti->c_cflag);
621         ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
622
623         return (0);
624 }
625
626 static int
627 ubsa_open(void *addr, int portno)
628 {
629         struct ubsa_softc *sc;
630         int err;
631
632         sc = addr;
633         if (sc->sc_ucom.sc_dying)
634                 return (ENXIO);
635
636         DPRINTF(("ubsa_open: sc = %p\n", sc));
637
638         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
639                 sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
640                 err = usbd_open_pipe_intr(sc->sc_intr_iface,
641                     sc->sc_intr_number,
642                     USBD_SHORT_XFER_OK,
643                     &sc->sc_intr_pipe,
644                     sc,
645                     sc->sc_intr_buf,
646                     sc->sc_isize,
647                     ubsa_intr,
648                     UBSA_INTR_INTERVAL);
649                 if (err) {
650                         device_printf(sc->sc_ucom.sc_dev,
651                             "cannot open interrupt pipe (addr %d)\n",
652                             sc->sc_intr_number);
653                         return (EIO);
654                 }
655         }
656
657         return (0);
658 }
659
660 static void
661 ubsa_close(void *addr, int portno)
662 {
663         struct ubsa_softc *sc;
664         int err;
665
666         sc = addr;
667         if (sc->sc_ucom.sc_dying)
668                 return;
669
670         DPRINTF(("ubsa_close: close\n"));
671
672         if (sc->sc_intr_pipe != NULL) {
673                 err = usbd_abort_pipe(sc->sc_intr_pipe);
674                 if (err)
675                         device_printf(sc->sc_ucom.sc_dev,
676                             "abort interrupt pipe failed: %s\n",
677                             usbd_errstr(err));
678                 err = usbd_close_pipe(sc->sc_intr_pipe);
679                 if (err)
680                         device_printf(sc->sc_ucom.sc_dev,
681                             "close interrupt pipe failed: %s\n",
682                             usbd_errstr(err));
683                 free(sc->sc_intr_buf, M_USBDEV);
684                 sc->sc_intr_pipe = NULL;
685         }
686 }
687
688 static void
689 ubsa_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
690 {
691         struct ubsa_softc *sc;
692         u_char *buf;
693
694         sc = priv;
695         buf = sc->sc_intr_buf;
696         if (sc->sc_ucom.sc_dying)
697                 return;
698
699         if (status != USBD_NORMAL_COMPLETION) {
700                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
701                         return;
702
703                 DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
704                     device_get_nameunit(sc->sc_ucom.sc_dev),
705                     usbd_errstr(status)));
706                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
707                 return;
708         }
709
710         /* incidentally, Belkin adapter status bits match UART 16550 bits */
711         sc->sc_lsr = buf[2];
712         sc->sc_msr = buf[3];
713
714         DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
715             device_get_nameunit(sc->sc_ucom.sc_dev), sc->sc_lsr, sc->sc_msr));
716
717         taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
718 }
719
720 /* Handle delayed events. */
721 static void
722 ubsa_notify(void *arg, int count)
723 {
724         struct ubsa_softc *sc;
725
726         sc = arg;
727         ucom_status_change(&sc->sc_ucom);
728 }
729
730 static void
731 ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
732 {
733         struct ubsa_softc *sc;
734
735         DPRINTF(("ubsa_get_status\n"));
736
737         sc = addr;
738         if (lsr != NULL)
739                 *lsr = sc->sc_lsr;
740         if (msr != NULL)
741                 *msr = sc->sc_msr;
742 }