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