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