2 * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 * Copyright (c) 2001 The NetBSD Foundation, Inc.
31 * All rights reserved.
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Ichiro FUKUHARA (ichiro@ichiro.org).
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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.
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usb_mfunc.h>
68 #include <dev/usb/usb_error.h>
69 #include <dev/usb/usb_cdc.h>
71 #define USB_DEBUG_VAR ubsa_debug
73 #include <dev/usb/usb_core.h>
74 #include <dev/usb/usb_debug.h>
75 #include <dev/usb/usb_process.h>
76 #include <dev/usb/usb_request.h>
77 #include <dev/usb/usb_lookup.h>
78 #include <dev/usb/usb_util.h>
79 #include <dev/usb/usb_busdma.h>
81 #include <dev/usb/serial/usb_serial.h>
84 static int ubsa_debug = 0;
86 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
87 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
88 &ubsa_debug, 0, "ubsa debug level");
91 #define UBSA_BSIZE 1024 /* bytes */
93 #define UBSA_CONFIG_INDEX 0
94 #define UBSA_IFACE_INDEX 0
96 #define UBSA_REG_BAUDRATE 0x00
97 #define UBSA_REG_STOP_BITS 0x01
98 #define UBSA_REG_DATA_BITS 0x02
99 #define UBSA_REG_PARITY 0x03
100 #define UBSA_REG_DTR 0x0A
101 #define UBSA_REG_RTS 0x0B
102 #define UBSA_REG_BREAK 0x0C
103 #define UBSA_REG_FLOW_CTRL 0x10
105 #define UBSA_PARITY_NONE 0x00
106 #define UBSA_PARITY_EVEN 0x01
107 #define UBSA_PARITY_ODD 0x02
108 #define UBSA_PARITY_MARK 0x03
109 #define UBSA_PARITY_SPACE 0x04
111 #define UBSA_FLOW_NONE 0x0000
112 #define UBSA_FLOW_OCTS 0x0001
113 #define UBSA_FLOW_ODSR 0x0002
114 #define UBSA_FLOW_IDSR 0x0004
115 #define UBSA_FLOW_IDTR 0x0008
116 #define UBSA_FLOW_IRTS 0x0010
117 #define UBSA_FLOW_ORTS 0x0020
118 #define UBSA_FLOW_UNKNOWN 0x0040
119 #define UBSA_FLOW_OXON 0x0080
120 #define UBSA_FLOW_IXON 0x0100
122 /* line status register */
123 #define UBSA_LSR_TSRE 0x40 /* Transmitter empty: byte sent */
124 #define UBSA_LSR_TXRDY 0x20 /* Transmitter buffer empty */
125 #define UBSA_LSR_BI 0x10 /* Break detected */
126 #define UBSA_LSR_FE 0x08 /* Framing error: bad stop bit */
127 #define UBSA_LSR_PE 0x04 /* Parity error */
128 #define UBSA_LSR_OE 0x02 /* Overrun, lost incoming byte */
129 #define UBSA_LSR_RXRDY 0x01 /* Byte ready in Receive Buffer */
130 #define UBSA_LSR_RCV_MASK 0x1f /* Mask for incoming data or error */
132 /* modem status register */
133 /* All deltas are from the last read of the MSR. */
134 #define UBSA_MSR_DCD 0x80 /* Current Data Carrier Detect */
135 #define UBSA_MSR_RI 0x40 /* Current Ring Indicator */
136 #define UBSA_MSR_DSR 0x20 /* Current Data Set Ready */
137 #define UBSA_MSR_CTS 0x10 /* Current Clear to Send */
138 #define UBSA_MSR_DDCD 0x08 /* DCD has changed state */
139 #define UBSA_MSR_TERI 0x04 /* RI has toggled low to high */
140 #define UBSA_MSR_DDSR 0x02 /* DSR has changed state */
141 #define UBSA_MSR_DCTS 0x01 /* CTS has changed state */
151 struct usb2_com_super_softc sc_super_ucom;
152 struct usb2_com_softc sc_ucom;
154 struct usb2_xfer *sc_xfer[UBSA_N_TRANSFER];
155 struct usb2_device *sc_udev;
158 uint8_t sc_iface_no; /* interface number */
159 uint8_t sc_iface_index; /* interface index */
160 uint8_t sc_lsr; /* local status register */
161 uint8_t sc_msr; /* UBSA status register */
164 static device_probe_t ubsa_probe;
165 static device_attach_t ubsa_attach;
166 static device_detach_t ubsa_detach;
168 static usb2_callback_t ubsa_write_callback;
169 static usb2_callback_t ubsa_read_callback;
170 static usb2_callback_t ubsa_intr_callback;
172 static void ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
173 static void ubsa_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
174 static void ubsa_cfg_set_rts(struct usb2_com_softc *, uint8_t);
175 static void ubsa_cfg_set_break(struct usb2_com_softc *, uint8_t);
176 static int ubsa_pre_param(struct usb2_com_softc *, struct termios *);
177 static void ubsa_cfg_param(struct usb2_com_softc *, struct termios *);
178 static void ubsa_start_read(struct usb2_com_softc *);
179 static void ubsa_stop_read(struct usb2_com_softc *);
180 static void ubsa_start_write(struct usb2_com_softc *);
181 static void ubsa_stop_write(struct usb2_com_softc *);
182 static void ubsa_cfg_get_status(struct usb2_com_softc *, uint8_t *,
185 static const struct usb2_config ubsa_config[UBSA_N_TRANSFER] = {
187 [UBSA_BULK_DT_WR] = {
189 .endpoint = UE_ADDR_ANY,
190 .direction = UE_DIR_OUT,
191 .bufsize = UBSA_BSIZE, /* bytes */
192 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
193 .callback = &ubsa_write_callback,
196 [UBSA_BULK_DT_RD] = {
198 .endpoint = UE_ADDR_ANY,
199 .direction = UE_DIR_IN,
200 .bufsize = UBSA_BSIZE, /* bytes */
201 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
202 .callback = &ubsa_read_callback,
205 [UBSA_INTR_DT_RD] = {
206 .type = UE_INTERRUPT,
207 .endpoint = UE_ADDR_ANY,
208 .direction = UE_DIR_IN,
209 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
210 .bufsize = 0, /* use wMaxPacketSize */
211 .callback = &ubsa_intr_callback,
215 static const struct usb2_com_callback ubsa_callback = {
216 .usb2_com_cfg_get_status = &ubsa_cfg_get_status,
217 .usb2_com_cfg_set_dtr = &ubsa_cfg_set_dtr,
218 .usb2_com_cfg_set_rts = &ubsa_cfg_set_rts,
219 .usb2_com_cfg_set_break = &ubsa_cfg_set_break,
220 .usb2_com_cfg_param = &ubsa_cfg_param,
221 .usb2_com_pre_param = &ubsa_pre_param,
222 .usb2_com_start_read = &ubsa_start_read,
223 .usb2_com_stop_read = &ubsa_stop_read,
224 .usb2_com_start_write = &ubsa_start_write,
225 .usb2_com_stop_write = &ubsa_stop_write,
228 static const struct usb2_device_id ubsa_devs[] = {
229 /* AnyData ADU-500A */
230 {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
231 /* AnyData ADU-E100A/H */
232 {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
233 /* Axesstel MV100H */
234 {USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
236 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
238 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
239 /* GoHubs GO-COM232 */
240 {USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
241 /* GoHubs GO-COM232 */
242 {USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
244 {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
247 static device_method_t ubsa_methods[] = {
248 DEVMETHOD(device_probe, ubsa_probe),
249 DEVMETHOD(device_attach, ubsa_attach),
250 DEVMETHOD(device_detach, ubsa_detach),
254 static devclass_t ubsa_devclass;
256 static driver_t ubsa_driver = {
258 .methods = ubsa_methods,
259 .size = sizeof(struct ubsa_softc),
262 DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
263 MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
264 MODULE_DEPEND(ubsa, usb, 1, 1, 1);
267 ubsa_probe(device_t dev)
269 struct usb2_attach_arg *uaa = device_get_ivars(dev);
271 if (uaa->usb_mode != USB_MODE_HOST) {
274 if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
277 if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
280 return (usb2_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
284 ubsa_attach(device_t dev)
286 struct usb2_attach_arg *uaa = device_get_ivars(dev);
287 struct ubsa_softc *sc = device_get_softc(dev);
290 DPRINTF("sc=%p\n", sc);
292 device_set_usb2_desc(dev);
293 mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
295 sc->sc_udev = uaa->device;
296 sc->sc_iface_no = uaa->info.bIfaceNum;
297 sc->sc_iface_index = UBSA_IFACE_INDEX;
299 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
300 sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
303 DPRINTF("could not allocate all pipes\n");
306 /* clear stall at first run */
307 mtx_lock(&sc->sc_mtx);
308 usb2_transfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
309 usb2_transfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
310 mtx_unlock(&sc->sc_mtx);
312 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
313 &ubsa_callback, &sc->sc_mtx);
315 DPRINTF("usb2_com_attach failed\n");
326 ubsa_detach(device_t dev)
328 struct ubsa_softc *sc = device_get_softc(dev);
330 DPRINTF("sc=%p\n", sc);
332 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
333 usb2_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
334 mtx_destroy(&sc->sc_mtx);
340 ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
342 struct usb2_device_request req;
345 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
346 req.bRequest = index;
347 USETW(req.wValue, value);
348 req.wIndex[0] = sc->sc_iface_no;
350 USETW(req.wLength, 0);
352 err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
353 &req, NULL, 0, 1000);
355 DPRINTFN(0, "device request failed, err=%s "
356 "(ignored)\n", usb2_errstr(err));
361 ubsa_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
363 struct ubsa_softc *sc = ucom->sc_parent;
365 DPRINTF("onoff = %d\n", onoff);
367 ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
371 ubsa_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
373 struct ubsa_softc *sc = ucom->sc_parent;
375 DPRINTF("onoff = %d\n", onoff);
377 ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
381 ubsa_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
383 struct ubsa_softc *sc = ucom->sc_parent;
385 DPRINTF("onoff = %d\n", onoff);
387 ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
391 ubsa_pre_param(struct usb2_com_softc *ucom, struct termios *t)
393 struct ubsa_softc *sc = ucom->sc_parent;
395 DPRINTF("sc = %p\n", sc);
397 switch (t->c_ospeed) {
418 ubsa_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
420 struct ubsa_softc *sc = ucom->sc_parent;
423 DPRINTF("sc = %p\n", sc);
425 switch (t->c_ospeed) {
427 ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
428 ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
429 ubsa_cfg_set_rts(&sc->sc_ucom, 0);
442 value = B230400 / t->c_ospeed;
443 ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
449 if (t->c_cflag & PARENB)
450 value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
452 value = UBSA_PARITY_NONE;
454 ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
456 switch (t->c_cflag & CSIZE) {
472 ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
474 value = (t->c_cflag & CSTOPB) ? 1 : 0;
476 ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
479 if (t->c_cflag & CRTSCTS)
480 value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
482 if (t->c_iflag & (IXON | IXOFF))
483 value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
485 ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
489 ubsa_start_read(struct usb2_com_softc *ucom)
491 struct ubsa_softc *sc = ucom->sc_parent;
493 /* start interrupt endpoint */
494 usb2_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
496 /* start read endpoint */
497 usb2_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
501 ubsa_stop_read(struct usb2_com_softc *ucom)
503 struct ubsa_softc *sc = ucom->sc_parent;
505 /* stop interrupt endpoint */
506 usb2_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
508 /* stop read endpoint */
509 usb2_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
513 ubsa_start_write(struct usb2_com_softc *ucom)
515 struct ubsa_softc *sc = ucom->sc_parent;
517 usb2_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
521 ubsa_stop_write(struct usb2_com_softc *ucom)
523 struct ubsa_softc *sc = ucom->sc_parent;
525 usb2_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
529 ubsa_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
531 struct ubsa_softc *sc = ucom->sc_parent;
540 ubsa_write_callback(struct usb2_xfer *xfer)
542 struct ubsa_softc *sc = xfer->priv_sc;
545 switch (USB_GET_STATE(xfer)) {
547 case USB_ST_TRANSFERRED:
549 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
550 UBSA_BSIZE, &actlen)) {
552 xfer->frlengths[0] = actlen;
553 usb2_start_hardware(xfer);
558 if (xfer->error != USB_ERR_CANCELLED) {
559 /* try to clear stall first */
560 xfer->flags.stall_pipe = 1;
569 ubsa_read_callback(struct usb2_xfer *xfer)
571 struct ubsa_softc *sc = xfer->priv_sc;
573 switch (USB_GET_STATE(xfer)) {
574 case USB_ST_TRANSFERRED:
575 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
579 xfer->frlengths[0] = xfer->max_data_length;
580 usb2_start_hardware(xfer);
584 if (xfer->error != USB_ERR_CANCELLED) {
585 /* try to clear stall first */
586 xfer->flags.stall_pipe = 1;
595 ubsa_intr_callback(struct usb2_xfer *xfer)
597 struct ubsa_softc *sc = xfer->priv_sc;
600 switch (USB_GET_STATE(xfer)) {
601 case USB_ST_TRANSFERRED:
603 if (xfer->actlen >= sizeof(buf)) {
605 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
608 * incidentally, Belkin adapter status bits match
614 DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
615 sc->sc_lsr, sc->sc_msr);
617 usb2_com_status_change(&sc->sc_ucom);
619 DPRINTF("ignoring short packet, %d bytes\n",
625 xfer->frlengths[0] = xfer->max_data_length;
626 usb2_start_hardware(xfer);
630 if (xfer->error != USB_ERR_CANCELLED) {
631 /* try to clear stall first */
632 xfer->flags.stall_pipe = 1;