Add support for HandyTech's Braille displays into ubsa(4) (ID found in
[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.15 2007/08/16 07:03:30 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/usbdevs.h>
88 #include <bus/usb/usb_quirks.h>
89
90 #include "../ucom/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                                         kprintf 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 };
178
179 static  void ubsa_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
180 static  void ubsa_notify(void *);
181
182 static  void ubsa_get_status(void *, int, u_char *, u_char *);
183 static  void ubsa_set(void *, int, int, int);
184 static  int  ubsa_param(void *, int, struct termios *);
185 static  int  ubsa_open(void *, int);
186 static  void ubsa_close(void *, int);
187
188 static  int  ubsa_request(struct ubsa_softc *, u_int8_t, u_int16_t);
189 static  void ubsa_dtr(struct ubsa_softc *, int);
190 static  void ubsa_rts(struct ubsa_softc *, int);
191 static  void ubsa_baudrate(struct ubsa_softc *, speed_t);
192 static  void ubsa_parity(struct ubsa_softc *, tcflag_t);
193 static  void ubsa_databits(struct ubsa_softc *, tcflag_t);
194 static  void ubsa_stopbits(struct ubsa_softc *, tcflag_t);
195 static  void ubsa_flow(struct ubsa_softc *, tcflag_t, tcflag_t);
196
197 struct ucom_callback ubsa_callback = {
198         ubsa_get_status,
199         ubsa_set,
200         ubsa_param,
201         NULL,
202         ubsa_open,
203         ubsa_close,
204         NULL,
205         NULL
206 };
207
208 static const struct ubsa_product {
209         uint16_t        vendor;
210         uint16_t        product;
211 } ubsa_products [] = {
212         /* BELKIN F5U103 */
213         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
214         /* BELKIN F5U120 */
215         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
216         /* GoHubs GO-COM232 */
217         { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
218         /* GoHubs GO-COM232 */
219         { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
220         /* HandyTech's Braille displays */
221         { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_HANDYLINK },
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         "ucom",
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 static int
251 ubsa_match(device_t self)
252 {
253         struct usb_attach_arg *uaa = device_get_ivars(self);
254         int i;
255
256         if (uaa->iface != NULL)
257                 return (UMATCH_NONE);
258
259         for (i = 0; ubsa_products[i].vendor != 0; i++) {
260                 if (ubsa_products[i].vendor == uaa->vendor &&
261                     ubsa_products[i].product == uaa->product) {
262                         return (UMATCH_VENDOR_PRODUCT);
263                 }
264         }
265         return (UMATCH_NONE);
266 }
267
268 static int
269 ubsa_attach(device_t self)
270 {
271         struct ubsa_softc *sc = device_get_softc(self);
272         struct usb_attach_arg *uaa = device_get_ivars(self);
273         usbd_device_handle dev;
274         struct ucom_softc *ucom;
275         usb_config_descriptor_t *cdesc;
276         usb_interface_descriptor_t *id;
277         usb_endpoint_descriptor_t *ed;
278         char *devinfo;
279         const char *devname;
280         usbd_status err;
281         int i;
282
283         dev = uaa->device;
284         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
285         ucom = &sc->sc_ucom;
286
287         bzero(sc, sizeof (struct ubsa_softc));
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         usbd_devinfo(dev, 0, devinfo);
297         ucom->sc_dev = self;
298         device_set_desc_copy(self, devinfo);
299
300         ucom->sc_udev = dev;
301         ucom->sc_iface = uaa->iface;
302
303         devname = device_get_nameunit(ucom->sc_dev);
304         kprintf("%s: %s\n", devname, devinfo);
305
306         DPRINTF(("ubsa attach: sc = %p\n", sc));
307
308         /* initialize endpoints */
309         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
310         sc->sc_intr_number = -1;
311         sc->sc_intr_pipe = NULL;
312
313         /* Move the device into the configured state. */
314         err = usbd_set_config_index(dev, UBSA_CONFIG_INDEX, 1);
315         if (err) {
316                 kprintf("%s: failed to set configuration: %s\n",
317                     devname, usbd_errstr(err));
318                 ucom->sc_dying = 1;
319                 goto error;
320         }
321
322         /* get the config descriptor */
323         cdesc = usbd_get_config_descriptor(ucom->sc_udev);
324
325         if (cdesc == NULL) {
326                 kprintf("%s: failed to get configuration descriptor\n",
327                     device_get_nameunit(ucom->sc_dev));
328                 ucom->sc_dying = 1;
329                 goto error;
330         }
331
332         /* get the first interface */
333         err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX,
334             &ucom->sc_iface);
335         if (err) {
336                 kprintf("%s: failed to get interface: %s\n",
337                         devname, usbd_errstr(err));
338                 ucom->sc_dying = 1;
339                 goto error;
340         }
341
342         /* Find the endpoints */
343
344         id = usbd_get_interface_descriptor(ucom->sc_iface);
345         sc->sc_iface_number = id->bInterfaceNumber;
346
347         for (i = 0; i < id->bNumEndpoints; i++) {
348                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
349                 if (ed == NULL) {
350                         kprintf("%s: no endpoint descriptor for %d\n",
351                             device_get_nameunit(ucom->sc_dev), i);
352                         ucom->sc_dying = 1;
353                         goto error;
354                 }
355
356                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
357                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
358                         sc->sc_intr_number = ed->bEndpointAddress;
359                         sc->sc_isize = UGETW(ed->wMaxPacketSize);
360                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
361                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
362                         ucom->sc_bulkin_no = ed->bEndpointAddress;
363                         ucom->sc_ibufsize = UGETW(ed->wMaxPacketSize);
364                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
365                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
366                         ucom->sc_bulkout_no = ed->bEndpointAddress;
367                         ucom->sc_obufsize = UGETW(ed->wMaxPacketSize);
368                 }
369         }
370
371         if (sc->sc_intr_number == -1) {
372                 kprintf("%s: Could not find interrupt in\n",
373                     device_get_nameunit(ucom->sc_dev));
374                 ucom->sc_dying = 1;
375                 goto error;
376         }
377
378         /* keep interface for interrupt */
379         sc->sc_intr_iface = ucom->sc_iface;
380
381         if (ucom->sc_bulkin_no == -1) {
382                 kprintf("%s: Could not find data bulk in\n",
383                     device_get_nameunit(ucom->sc_dev));
384                 ucom->sc_dying = 1;
385                 goto error;
386         }
387
388         if (ucom->sc_bulkout_no == -1) {
389                 kprintf("%s: Could not find data bulk out\n",
390                     device_get_nameunit(ucom->sc_dev));
391                 ucom->sc_dying = 1;
392                 goto error;
393         }
394
395         ucom->sc_parent = sc;
396         ucom->sc_portno = UCOM_UNK_PORTNO;
397         /* bulkin, bulkout set above */
398         ucom->sc_ibufsizepad = ucom->sc_ibufsize;
399         ucom->sc_opkthdrlen = 0;
400         ucom->sc_callback = &ubsa_callback;
401
402         DPRINTF(("ubsa: in = 0x%x, out = 0x%x, intr = 0x%x\n",
403             ucom->sc_bulkin_no, ucom->sc_bulkout_no, sc->sc_intr_number));
404
405         ucom_attach(ucom);
406
407         kfree(devinfo, M_USBDEV);
408         return 0;
409
410 error:
411         kfree(devinfo, M_USBDEV);
412         return ENXIO;
413 }
414
415 static int
416 ubsa_detach(device_t self)
417 {
418         struct ubsa_softc *sc = device_get_softc(self);
419         int rv;
420
421
422         DPRINTF(("ubsa_detach: sc = %p\n", sc));
423
424         if (sc->sc_intr_pipe != NULL) {
425                 usbd_abort_pipe(sc->sc_intr_pipe);
426                 usbd_close_pipe(sc->sc_intr_pipe);
427                 kfree(sc->sc_intr_buf, M_USBDEV);
428                 sc->sc_intr_pipe = NULL;
429         }
430
431         sc->sc_ucom.sc_dying = 1;
432
433         rv = ucom_detach(&sc->sc_ucom);
434
435         return (rv);
436 }
437
438 static int
439 ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
440 {
441         usb_device_request_t req;
442         usbd_status err;
443
444         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
445         req.bRequest = request;
446         USETW(req.wValue, value);
447         USETW(req.wIndex, sc->sc_iface_number);
448         USETW(req.wLength, 0);
449
450         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
451         if (err)
452                 kprintf("%s: ubsa_request: %s\n",
453                     device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
454         return (err);
455 }
456
457 static void
458 ubsa_dtr(struct ubsa_softc *sc, int onoff)
459 {
460
461         DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
462
463         if (sc->sc_dtr == onoff)
464                 return;
465         sc->sc_dtr = onoff;
466
467         ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
468 }
469
470 static void
471 ubsa_rts(struct ubsa_softc *sc, int onoff)
472 {
473
474         DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
475
476         if (sc->sc_rts == onoff)
477                 return;
478         sc->sc_rts = onoff;
479
480         ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
481 }
482
483 static void
484 ubsa_break(struct ubsa_softc *sc, int onoff)
485 {
486
487         DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
488
489         ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
490 }
491
492 static void
493 ubsa_set(void *addr, int portno, int reg, int onoff)
494 {
495         struct ubsa_softc *sc;
496
497         sc = addr;
498         switch (reg) {
499         case UCOM_SET_DTR:
500                 ubsa_dtr(sc, onoff);
501                 break;
502         case UCOM_SET_RTS:
503                 ubsa_rts(sc, onoff);
504                 break;
505         case UCOM_SET_BREAK:
506                 ubsa_break(sc, onoff);
507                 break;
508         default:
509                 break;
510         }
511 }
512
513 static void
514 ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
515 {
516         u_int16_t value = 0;
517
518         DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
519
520         switch(speed) {
521         case B0:
522                 break;
523         case B300:
524         case B600:
525         case B1200:
526         case B2400:
527         case B4800:
528         case B9600:
529         case B19200:
530         case B38400:
531         case B57600:
532         case B115200:
533         case B230400:
534                 value = B230400 / speed;
535                 break;
536         default:
537                 kprintf("%s: ubsa_param: unsupported baudrate, "
538                     "forcing default of 9600\n",
539                     device_get_nameunit(sc->sc_ucom.sc_dev));
540                 value = B230400 / B9600;
541                 break;
542         };
543
544         if (speed == B0) {
545                 ubsa_flow(sc, 0, 0);
546                 ubsa_dtr(sc, 0);
547                 ubsa_rts(sc, 0);
548         } else
549                 ubsa_request(sc, UBSA_SET_BAUDRATE, value);
550 }
551
552 static void
553 ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
554 {
555         int value;
556
557         DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
558
559         if (cflag & PARENB)
560                 value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
561         else
562                 value = UBSA_PARITY_NONE;
563
564         ubsa_request(sc, UBSA_SET_PARITY, value);
565 }
566
567 static void
568 ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
569 {
570         int value;
571
572         DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
573
574         switch (cflag & CSIZE) {
575         case CS5: value = 0; break;
576         case CS6: value = 1; break;
577         case CS7: value = 2; break;
578         case CS8: value = 3; break;
579         default:
580                 kprintf("%s: ubsa_param: unsupported databits requested, "
581                     "forcing default of 8\n",
582                     device_get_nameunit(sc->sc_ucom.sc_dev));
583                 value = 3;
584         }
585
586         ubsa_request(sc, UBSA_SET_DATA_BITS, value);
587 }
588
589 static void
590 ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
591 {
592         int value;
593
594         DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
595
596         value = (cflag & CSTOPB) ? 1 : 0;
597
598         ubsa_request(sc, UBSA_SET_STOP_BITS, value);
599 }
600
601 static void
602 ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
603 {
604         int value;
605
606         DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
607
608         value = 0;
609         if (cflag & CRTSCTS)
610                 value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
611         if (iflag & (IXON|IXOFF))
612                 value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
613
614         ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
615 }
616
617 static int
618 ubsa_param(void *addr, int portno, struct termios *ti)
619 {
620         struct ubsa_softc *sc;
621
622         sc = addr;
623
624         DPRINTF(("ubsa_param: sc = %p\n", sc));
625
626         ubsa_baudrate(sc, ti->c_ospeed);
627         ubsa_parity(sc, ti->c_cflag);
628         ubsa_databits(sc, ti->c_cflag);
629         ubsa_stopbits(sc, ti->c_cflag);
630         ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
631
632         return (0);
633 }
634
635 static int
636 ubsa_open(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 (ENXIO);
644
645         DPRINTF(("ubsa_open: sc = %p\n", sc));
646
647         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
648                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
649                 err = usbd_open_pipe_intr(sc->sc_intr_iface,
650                     sc->sc_intr_number,
651                     USBD_SHORT_XFER_OK,
652                     &sc->sc_intr_pipe,
653                     sc,
654                     sc->sc_intr_buf,
655                     sc->sc_isize,
656                     ubsa_intr,
657                     UBSA_INTR_INTERVAL);
658                 if (err) {
659                         kprintf("%s: cannot open interrupt pipe (addr %d)\n",
660                             device_get_nameunit(sc->sc_ucom.sc_dev),
661                             sc->sc_intr_number);
662                         return (EIO);
663                 }
664         }
665
666         return (0);
667 }
668
669 static void
670 ubsa_close(void *addr, int portno)
671 {
672         struct ubsa_softc *sc;
673         int err;
674
675         sc = addr;
676         if (sc->sc_ucom.sc_dying)
677                 return;
678
679         DPRINTF(("ubsa_close: close\n"));
680
681         if (sc->sc_intr_pipe != NULL) {
682                 err = usbd_abort_pipe(sc->sc_intr_pipe);
683                 if (err)
684                         kprintf("%s: abort interrupt pipe failed: %s\n",
685                             device_get_nameunit(sc->sc_ucom.sc_dev),
686                             usbd_errstr(err));
687                 err = usbd_close_pipe(sc->sc_intr_pipe);
688                 if (err)
689                         kprintf("%s: close interrupt pipe failed: %s\n",
690                             device_get_nameunit(sc->sc_ucom.sc_dev),
691                             usbd_errstr(err));
692                 kfree(sc->sc_intr_buf, M_USBDEV);
693                 sc->sc_intr_pipe = NULL;
694         }
695 }
696
697 static void
698 ubsa_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
699 {
700         struct ubsa_softc *sc;
701         u_char *buf;
702
703         sc = priv;
704         buf = sc->sc_intr_buf;
705         if (sc->sc_ucom.sc_dying)
706                 return;
707
708         if (status != USBD_NORMAL_COMPLETION) {
709                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
710                         return;
711
712                 DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
713                     device_get_nameunit(sc->sc_ucom.sc_dev),
714                     usbd_errstr(status)));
715                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
716                 return;
717         }
718
719         /* incidentally, Belkin adapter status bits match UART 16550 bits */
720         sc->sc_lsr = buf[2];
721         sc->sc_msr = buf[3];
722
723         DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
724             device_get_nameunit(sc->sc_ucom.sc_dev), sc->sc_lsr, sc->sc_msr));
725
726         ubsa_notify(sc);
727 }
728
729 /* Handle delayed events. */
730 static void
731 ubsa_notify(void *arg)
732 {
733         struct ubsa_softc *sc;
734
735         sc = arg;
736         ucom_status_change(&sc->sc_ucom);
737 }
738
739 static void
740 ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
741 {
742         struct ubsa_softc *sc;
743
744         DPRINTF(("ubsa_get_status\n"));
745
746         sc = addr;
747         if (lsr != NULL)
748                 *lsr = sc->sc_lsr;
749         if (msr != NULL)
750                 *msr = sc->sc_msr;
751 }