Nuke USB_MATCH*, USB_ATTACH* and USB_DETACH* macros.
[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.13 2007/07/01 21:24:03 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/vnode.h>
80 #include <sys/poll.h>
81 #include <sys/sysctl.h>
82
83 #include <bus/usb/usb.h>
84 #include <bus/usb/usbcdc.h>
85
86 #include <bus/usb/usbdi.h>
87 #include <bus/usb/usbdi_util.h>
88 #include <bus/usb/usbdevs.h>
89 #include <bus/usb/usb_quirks.h>
90
91 #include "../ucom/ucomvar.h"
92
93 #ifdef USB_DEBUG
94 static int      ubsadebug = 0;
95 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
96 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
97            &ubsadebug, 0, "ubsa debug level");
98
99 #define DPRINTFN(n, x)  do { \
100                                 if (ubsadebug > (n)) \
101                                         kprintf x; \
102                         } while (0)
103 #else
104 #define DPRINTFN(n, x)
105 #endif
106 #define DPRINTF(x) DPRINTFN(0, x)
107
108 #define UBSA_MODVER             1       /* module version */
109
110 #define UBSA_CONFIG_INDEX       1
111 #define UBSA_IFACE_INDEX        0
112
113 #define UBSA_INTR_INTERVAL      100     /* ms */
114
115 #define UBSA_SET_BAUDRATE       0x00
116 #define UBSA_SET_STOP_BITS      0x01
117 #define UBSA_SET_DATA_BITS      0x02
118 #define UBSA_SET_PARITY         0x03
119 #define UBSA_SET_DTR            0x0A
120 #define UBSA_SET_RTS            0x0B
121 #define UBSA_SET_BREAK          0x0C
122 #define UBSA_SET_FLOW_CTRL      0x10
123
124 #define UBSA_PARITY_NONE        0x00
125 #define UBSA_PARITY_EVEN        0x01
126 #define UBSA_PARITY_ODD         0x02
127 #define UBSA_PARITY_MARK        0x03
128 #define UBSA_PARITY_SPACE       0x04
129
130 #define UBSA_FLOW_NONE          0x0000
131 #define UBSA_FLOW_OCTS          0x0001
132 #define UBSA_FLOW_ODSR          0x0002
133 #define UBSA_FLOW_IDSR          0x0004
134 #define UBSA_FLOW_IDTR          0x0008
135 #define UBSA_FLOW_IRTS          0x0010
136 #define UBSA_FLOW_ORTS          0x0020
137 #define UBSA_FLOW_UNKNOWN       0x0040
138 #define UBSA_FLOW_OXON          0x0080
139 #define UBSA_FLOW_IXON          0x0100
140
141 /* line status register */
142 #define UBSA_LSR_TSRE           0x40    /* Transmitter empty: byte sent */
143 #define UBSA_LSR_TXRDY          0x20    /* Transmitter buffer empty */
144 #define UBSA_LSR_BI             0x10    /* Break detected */
145 #define UBSA_LSR_FE             0x08    /* Framing error: bad stop bit */
146 #define UBSA_LSR_PE             0x04    /* Parity error */
147 #define UBSA_LSR_OE             0x02    /* Overrun, lost incoming byte */
148 #define UBSA_LSR_RXRDY          0x01    /* Byte ready in Receive Buffer */
149 #define UBSA_LSR_RCV_MASK       0x1f    /* Mask for incoming data or error */
150
151 /* modem status register */
152 /* All deltas are from the last read of the MSR. */
153 #define UBSA_MSR_DCD            0x80    /* Current Data Carrier Detect */
154 #define UBSA_MSR_RI             0x40    /* Current Ring Indicator */
155 #define UBSA_MSR_DSR            0x20    /* Current Data Set Ready */
156 #define UBSA_MSR_CTS            0x10    /* Current Clear to Send */
157 #define UBSA_MSR_DDCD           0x08    /* DCD has changed state */
158 #define UBSA_MSR_TERI           0x04    /* RI has toggled low to high */
159 #define UBSA_MSR_DDSR           0x02    /* DSR has changed state */
160 #define UBSA_MSR_DCTS           0x01    /* CTS has changed state */
161
162 struct  ubsa_softc {
163         struct ucom_softc       sc_ucom;
164
165         int                     sc_iface_number;        /* interface number */
166
167         usbd_interface_handle   sc_intr_iface;  /* interrupt interface */
168         int                     sc_intr_number; /* interrupt number */
169         usbd_pipe_handle        sc_intr_pipe;   /* interrupt pipe */
170         u_char                  *sc_intr_buf;   /* interrupt buffer */
171         int                     sc_isize;
172
173         u_char                  sc_dtr;         /* current DTR state */
174         u_char                  sc_rts;         /* current RTS state */
175
176         u_char                  sc_lsr;         /* Local status register */
177         u_char                  sc_msr;         /* ubsa status register */
178 };
179
180 static  void ubsa_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
181 static  void ubsa_notify(void *);
182
183 static  void ubsa_get_status(void *, int, u_char *, u_char *);
184 static  void ubsa_set(void *, int, int, int);
185 static  int  ubsa_param(void *, int, struct termios *);
186 static  int  ubsa_open(void *, int);
187 static  void ubsa_close(void *, int);
188
189 static  int  ubsa_request(struct ubsa_softc *, u_int8_t, u_int16_t);
190 static  void ubsa_dtr(struct ubsa_softc *, int);
191 static  void ubsa_rts(struct ubsa_softc *, int);
192 static  void ubsa_baudrate(struct ubsa_softc *, speed_t);
193 static  void ubsa_parity(struct ubsa_softc *, tcflag_t);
194 static  void ubsa_databits(struct ubsa_softc *, tcflag_t);
195 static  void ubsa_stopbits(struct ubsa_softc *, tcflag_t);
196 static  void ubsa_flow(struct ubsa_softc *, tcflag_t, tcflag_t);
197
198 struct ucom_callback ubsa_callback = {
199         ubsa_get_status,
200         ubsa_set,
201         ubsa_param,
202         NULL,
203         ubsa_open,
204         ubsa_close,
205         NULL,
206         NULL
207 };
208
209 static const struct ubsa_product {
210         uint16_t        vendor;
211         uint16_t        product;
212 } ubsa_products [] = {
213         /* BELKIN F5U103 */
214         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
215         /* BELKIN F5U120 */
216         { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
217         /* GoHubs GO-COM232 */
218         { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
219         /* GoHubs GO-COM232 */
220         { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
221         /* Peracom */
222         { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
223         { 0, 0 }
224 };
225
226 static device_probe_t ubsa_match;
227 static device_attach_t ubsa_attach;
228 static device_detach_t ubsa_detach;
229
230 static device_method_t ubsa_methods[] = {
231         /* Device interface */
232         DEVMETHOD(device_probe, ubsa_match),
233         DEVMETHOD(device_attach, ubsa_attach),
234         DEVMETHOD(device_detach, ubsa_detach),
235         { 0, 0 }
236 };
237
238 static driver_t ubsa_driver = {
239         "ucom",
240         ubsa_methods,
241         sizeof (struct ubsa_softc)
242 };
243
244 DRIVER_MODULE(ubsa, uhub, ubsa_driver, ucom_devclass, usbd_driver_load, 0);
245 MODULE_DEPEND(ubsa, usb, 1, 1, 1);
246 MODULE_DEPEND(ubsa, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
247 MODULE_VERSION(ubsa, UBSA_MODVER);
248
249 static int
250 ubsa_match(device_t self)
251 {
252         struct usb_attach_arg *uaa = device_get_ivars(self);
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 static int
268 ubsa_attach(device_t self)
269 {
270         struct ubsa_softc *sc = device_get_softc(self);
271         struct usb_attach_arg *uaa = device_get_ivars(self);
272         usbd_device_handle dev;
273         struct ucom_softc *ucom;
274         usb_config_descriptor_t *cdesc;
275         usb_interface_descriptor_t *id;
276         usb_endpoint_descriptor_t *ed;
277         char *devinfo;
278         const char *devname;
279         usbd_status err;
280         int i;
281
282         dev = uaa->device;
283         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
284         ucom = &sc->sc_ucom;
285
286         bzero(sc, sizeof (struct ubsa_softc));
287
288         /*
289          * initialize rts, dtr variables to something
290          * different from boolean 0, 1
291          */
292         sc->sc_dtr = -1;
293         sc->sc_rts = -1;
294
295         usbd_devinfo(dev, 0, devinfo);
296         ucom->sc_dev = self;
297         device_set_desc_copy(self, devinfo);
298
299         ucom->sc_udev = dev;
300         ucom->sc_iface = uaa->iface;
301
302         devname = device_get_nameunit(ucom->sc_dev);
303         kprintf("%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                 kprintf("%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                 kprintf("%s: failed to get configuration descriptor\n",
326                     device_get_nameunit(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                 kprintf("%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                         kprintf("%s: no endpoint descriptor for %d\n",
350                             device_get_nameunit(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                 kprintf("%s: Could not find interrupt in\n",
372                     device_get_nameunit(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                 kprintf("%s: Could not find data bulk in\n",
382                     device_get_nameunit(ucom->sc_dev));
383                 ucom->sc_dying = 1;
384                 goto error;
385         }
386
387         if (ucom->sc_bulkout_no == -1) {
388                 kprintf("%s: Could not find data bulk out\n",
389                     device_get_nameunit(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         kfree(devinfo, M_USBDEV);
407         return 0;
408
409 error:
410         kfree(devinfo, M_USBDEV);
411         return ENXIO;
412 }
413
414 static int
415 ubsa_detach(device_t self)
416 {
417         struct ubsa_softc *sc = device_get_softc(self);
418         int rv;
419
420
421         DPRINTF(("ubsa_detach: sc = %p\n", sc));
422
423         if (sc->sc_intr_pipe != NULL) {
424                 usbd_abort_pipe(sc->sc_intr_pipe);
425                 usbd_close_pipe(sc->sc_intr_pipe);
426                 kfree(sc->sc_intr_buf, M_USBDEV);
427                 sc->sc_intr_pipe = NULL;
428         }
429
430         sc->sc_ucom.sc_dying = 1;
431
432         rv = ucom_detach(&sc->sc_ucom);
433
434         return (rv);
435 }
436
437 static int
438 ubsa_request(struct ubsa_softc *sc, u_int8_t request, u_int16_t value)
439 {
440         usb_device_request_t req;
441         usbd_status err;
442
443         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
444         req.bRequest = request;
445         USETW(req.wValue, value);
446         USETW(req.wIndex, sc->sc_iface_number);
447         USETW(req.wLength, 0);
448
449         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
450         if (err)
451                 kprintf("%s: ubsa_request: %s\n",
452                     device_get_nameunit(sc->sc_ucom.sc_dev), usbd_errstr(err));
453         return (err);
454 }
455
456 static void
457 ubsa_dtr(struct ubsa_softc *sc, int onoff)
458 {
459
460         DPRINTF(("ubsa_dtr: onoff = %d\n", onoff));
461
462         if (sc->sc_dtr == onoff)
463                 return;
464         sc->sc_dtr = onoff;
465
466         ubsa_request(sc, UBSA_SET_DTR, onoff ? 1 : 0);
467 }
468
469 static void
470 ubsa_rts(struct ubsa_softc *sc, int onoff)
471 {
472
473         DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
474
475         if (sc->sc_rts == onoff)
476                 return;
477         sc->sc_rts = onoff;
478
479         ubsa_request(sc, UBSA_SET_RTS, onoff ? 1 : 0);
480 }
481
482 static void
483 ubsa_break(struct ubsa_softc *sc, int onoff)
484 {
485
486         DPRINTF(("ubsa_rts: onoff = %d\n", onoff));
487
488         ubsa_request(sc, UBSA_SET_BREAK, onoff ? 1 : 0);
489 }
490
491 static void
492 ubsa_set(void *addr, int portno, int reg, int onoff)
493 {
494         struct ubsa_softc *sc;
495
496         sc = addr;
497         switch (reg) {
498         case UCOM_SET_DTR:
499                 ubsa_dtr(sc, onoff);
500                 break;
501         case UCOM_SET_RTS:
502                 ubsa_rts(sc, onoff);
503                 break;
504         case UCOM_SET_BREAK:
505                 ubsa_break(sc, onoff);
506                 break;
507         default:
508                 break;
509         }
510 }
511
512 static void
513 ubsa_baudrate(struct ubsa_softc *sc, speed_t speed)
514 {
515         u_int16_t value = 0;
516
517         DPRINTF(("ubsa_baudrate: speed = %d\n", speed));
518
519         switch(speed) {
520         case B0:
521                 break;
522         case B300:
523         case B600:
524         case B1200:
525         case B2400:
526         case B4800:
527         case B9600:
528         case B19200:
529         case B38400:
530         case B57600:
531         case B115200:
532         case B230400:
533                 value = B230400 / speed;
534                 break;
535         default:
536                 kprintf("%s: ubsa_param: unsupported baudrate, "
537                     "forcing default of 9600\n",
538                     device_get_nameunit(sc->sc_ucom.sc_dev));
539                 value = B230400 / B9600;
540                 break;
541         };
542
543         if (speed == B0) {
544                 ubsa_flow(sc, 0, 0);
545                 ubsa_dtr(sc, 0);
546                 ubsa_rts(sc, 0);
547         } else
548                 ubsa_request(sc, UBSA_SET_BAUDRATE, value);
549 }
550
551 static void
552 ubsa_parity(struct ubsa_softc *sc, tcflag_t cflag)
553 {
554         int value;
555
556         DPRINTF(("ubsa_parity: cflag = 0x%x\n", cflag));
557
558         if (cflag & PARENB)
559                 value = (cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
560         else
561                 value = UBSA_PARITY_NONE;
562
563         ubsa_request(sc, UBSA_SET_PARITY, value);
564 }
565
566 static void
567 ubsa_databits(struct ubsa_softc *sc, tcflag_t cflag)
568 {
569         int value;
570
571         DPRINTF(("ubsa_databits: cflag = 0x%x\n", cflag));
572
573         switch (cflag & CSIZE) {
574         case CS5: value = 0; break;
575         case CS6: value = 1; break;
576         case CS7: value = 2; break;
577         case CS8: value = 3; break;
578         default:
579                 kprintf("%s: ubsa_param: unsupported databits requested, "
580                     "forcing default of 8\n",
581                     device_get_nameunit(sc->sc_ucom.sc_dev));
582                 value = 3;
583         }
584
585         ubsa_request(sc, UBSA_SET_DATA_BITS, value);
586 }
587
588 static void
589 ubsa_stopbits(struct ubsa_softc *sc, tcflag_t cflag)
590 {
591         int value;
592
593         DPRINTF(("ubsa_stopbits: cflag = 0x%x\n", cflag));
594
595         value = (cflag & CSTOPB) ? 1 : 0;
596
597         ubsa_request(sc, UBSA_SET_STOP_BITS, value);
598 }
599
600 static void
601 ubsa_flow(struct ubsa_softc *sc, tcflag_t cflag, tcflag_t iflag)
602 {
603         int value;
604
605         DPRINTF(("ubsa_flow: cflag = 0x%x, iflag = 0x%x\n", cflag, iflag));
606
607         value = 0;
608         if (cflag & CRTSCTS)
609                 value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
610         if (iflag & (IXON|IXOFF))
611                 value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
612
613         ubsa_request(sc, UBSA_SET_FLOW_CTRL, value);
614 }
615
616 static int
617 ubsa_param(void *addr, int portno, struct termios *ti)
618 {
619         struct ubsa_softc *sc;
620
621         sc = addr;
622
623         DPRINTF(("ubsa_param: sc = %p\n", sc));
624
625         ubsa_baudrate(sc, ti->c_ospeed);
626         ubsa_parity(sc, ti->c_cflag);
627         ubsa_databits(sc, ti->c_cflag);
628         ubsa_stopbits(sc, ti->c_cflag);
629         ubsa_flow(sc, ti->c_cflag, ti->c_iflag);
630
631         return (0);
632 }
633
634 static int
635 ubsa_open(void *addr, int portno)
636 {
637         struct ubsa_softc *sc;
638         int err;
639
640         sc = addr;
641         if (sc->sc_ucom.sc_dying)
642                 return (ENXIO);
643
644         DPRINTF(("ubsa_open: sc = %p\n", sc));
645
646         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
647                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
648                 err = usbd_open_pipe_intr(sc->sc_intr_iface,
649                     sc->sc_intr_number,
650                     USBD_SHORT_XFER_OK,
651                     &sc->sc_intr_pipe,
652                     sc,
653                     sc->sc_intr_buf,
654                     sc->sc_isize,
655                     ubsa_intr,
656                     UBSA_INTR_INTERVAL);
657                 if (err) {
658                         kprintf("%s: cannot open interrupt pipe (addr %d)\n",
659                             device_get_nameunit(sc->sc_ucom.sc_dev),
660                             sc->sc_intr_number);
661                         return (EIO);
662                 }
663         }
664
665         return (0);
666 }
667
668 static void
669 ubsa_close(void *addr, int portno)
670 {
671         struct ubsa_softc *sc;
672         int err;
673
674         sc = addr;
675         if (sc->sc_ucom.sc_dying)
676                 return;
677
678         DPRINTF(("ubsa_close: close\n"));
679
680         if (sc->sc_intr_pipe != NULL) {
681                 err = usbd_abort_pipe(sc->sc_intr_pipe);
682                 if (err)
683                         kprintf("%s: abort interrupt pipe failed: %s\n",
684                             device_get_nameunit(sc->sc_ucom.sc_dev),
685                             usbd_errstr(err));
686                 err = usbd_close_pipe(sc->sc_intr_pipe);
687                 if (err)
688                         kprintf("%s: close interrupt pipe failed: %s\n",
689                             device_get_nameunit(sc->sc_ucom.sc_dev),
690                             usbd_errstr(err));
691                 kfree(sc->sc_intr_buf, M_USBDEV);
692                 sc->sc_intr_pipe = NULL;
693         }
694 }
695
696 static void
697 ubsa_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
698 {
699         struct ubsa_softc *sc;
700         u_char *buf;
701
702         sc = priv;
703         buf = sc->sc_intr_buf;
704         if (sc->sc_ucom.sc_dying)
705                 return;
706
707         if (status != USBD_NORMAL_COMPLETION) {
708                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
709                         return;
710
711                 DPRINTF(("%s: ubsa_intr: abnormal status: %s\n",
712                     device_get_nameunit(sc->sc_ucom.sc_dev),
713                     usbd_errstr(status)));
714                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
715                 return;
716         }
717
718         /* incidentally, Belkin adapter status bits match UART 16550 bits */
719         sc->sc_lsr = buf[2];
720         sc->sc_msr = buf[3];
721
722         DPRINTF(("%s: ubsa lsr = 0x%02x, msr = 0x%02x\n",
723             device_get_nameunit(sc->sc_ucom.sc_dev), sc->sc_lsr, sc->sc_msr));
724
725         ubsa_notify(sc);
726 }
727
728 /* Handle delayed events. */
729 static void
730 ubsa_notify(void *arg)
731 {
732         struct ubsa_softc *sc;
733
734         sc = arg;
735         ucom_status_change(&sc->sc_ucom);
736 }
737
738 static void
739 ubsa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
740 {
741         struct ubsa_softc *sc;
742
743         DPRINTF(("ubsa_get_status\n"));
744
745         sc = addr;
746         if (lsr != NULL)
747                 *lsr = sc->sc_lsr;
748         if (msr != NULL)
749                 *msr = sc->sc_msr;
750 }