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