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