102ac6454SAndrew Thompson /*- 202ac6454SAndrew Thompson * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>. 302ac6454SAndrew Thompson * All rights reserved. 402ac6454SAndrew Thompson * 502ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 602ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 702ac6454SAndrew Thompson * are met: 802ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 902ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1002ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1202ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1502ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1602ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1702ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1802ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1902ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2002ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2102ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2202ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2302ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2402ac6454SAndrew Thompson * SUCH DAMAGE. 2502ac6454SAndrew Thompson */ 2602ac6454SAndrew Thompson 2702ac6454SAndrew Thompson #include <sys/cdefs.h> 2802ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 2902ac6454SAndrew Thompson /*- 3002ac6454SAndrew Thompson * Copyright (c) 2001 The NetBSD Foundation, Inc. 3102ac6454SAndrew Thompson * All rights reserved. 3202ac6454SAndrew Thompson * 3302ac6454SAndrew Thompson * This code is derived from software contributed to The NetBSD Foundation 3402ac6454SAndrew Thompson * by Ichiro FUKUHARA (ichiro@ichiro.org). 3502ac6454SAndrew Thompson * 3602ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 3702ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 3802ac6454SAndrew Thompson * are met: 3902ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 4002ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 4102ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 4202ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 4302ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 4402ac6454SAndrew Thompson * 3. All advertising materials mentioning features or use of this software 4502ac6454SAndrew Thompson * must display the following acknowledgement: 4602ac6454SAndrew Thompson * This product includes software developed by the NetBSD 4702ac6454SAndrew Thompson * Foundation, Inc. and its contributors. 4802ac6454SAndrew Thompson * 4. Neither the name of The NetBSD Foundation nor the names of its 4902ac6454SAndrew Thompson * contributors may be used to endorse or promote products derived 5002ac6454SAndrew Thompson * from this software without specific prior written permission. 5102ac6454SAndrew Thompson * 5202ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 5302ac6454SAndrew Thompson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 5402ac6454SAndrew Thompson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 5502ac6454SAndrew Thompson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 5602ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5702ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5802ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5902ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 6002ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 6102ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 6202ac6454SAndrew Thompson * POSSIBILITY OF SUCH DAMAGE. 6302ac6454SAndrew Thompson */ 6402ac6454SAndrew Thompson 65ed6d949aSAndrew Thompson #include <sys/stdint.h> 66ed6d949aSAndrew Thompson #include <sys/stddef.h> 67ed6d949aSAndrew Thompson #include <sys/param.h> 68ed6d949aSAndrew Thompson #include <sys/queue.h> 69ed6d949aSAndrew Thompson #include <sys/types.h> 70ed6d949aSAndrew Thompson #include <sys/systm.h> 71ed6d949aSAndrew Thompson #include <sys/kernel.h> 72ed6d949aSAndrew Thompson #include <sys/bus.h> 73ed6d949aSAndrew Thompson #include <sys/linker_set.h> 74ed6d949aSAndrew Thompson #include <sys/module.h> 75ed6d949aSAndrew Thompson #include <sys/lock.h> 76ed6d949aSAndrew Thompson #include <sys/mutex.h> 77ed6d949aSAndrew Thompson #include <sys/condvar.h> 78ed6d949aSAndrew Thompson #include <sys/sysctl.h> 79ed6d949aSAndrew Thompson #include <sys/sx.h> 80ed6d949aSAndrew Thompson #include <sys/unistd.h> 81ed6d949aSAndrew Thompson #include <sys/callout.h> 82ed6d949aSAndrew Thompson #include <sys/malloc.h> 83ed6d949aSAndrew Thompson #include <sys/priv.h> 84ed6d949aSAndrew Thompson 8502ac6454SAndrew Thompson #include <dev/usb/usb.h> 86ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 87ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 88ed6d949aSAndrew Thompson #include "usbdevs.h" 8902ac6454SAndrew Thompson 9002ac6454SAndrew Thompson #define USB_DEBUG_VAR ubsa_debug 9102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 9202ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 9302ac6454SAndrew Thompson 9402ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h> 9502ac6454SAndrew Thompson 96b850ecc1SAndrew Thompson #ifdef USB_DEBUG 9702ac6454SAndrew Thompson static int ubsa_debug = 0; 9802ac6454SAndrew Thompson 999360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa"); 1009360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW, 10102ac6454SAndrew Thompson &ubsa_debug, 0, "ubsa debug level"); 10202ac6454SAndrew Thompson #endif 10302ac6454SAndrew Thompson 10402ac6454SAndrew Thompson #define UBSA_BSIZE 1024 /* bytes */ 10502ac6454SAndrew Thompson 10602ac6454SAndrew Thompson #define UBSA_CONFIG_INDEX 0 10702ac6454SAndrew Thompson #define UBSA_IFACE_INDEX 0 10802ac6454SAndrew Thompson 10902ac6454SAndrew Thompson #define UBSA_REG_BAUDRATE 0x00 11002ac6454SAndrew Thompson #define UBSA_REG_STOP_BITS 0x01 11102ac6454SAndrew Thompson #define UBSA_REG_DATA_BITS 0x02 11202ac6454SAndrew Thompson #define UBSA_REG_PARITY 0x03 11302ac6454SAndrew Thompson #define UBSA_REG_DTR 0x0A 11402ac6454SAndrew Thompson #define UBSA_REG_RTS 0x0B 11502ac6454SAndrew Thompson #define UBSA_REG_BREAK 0x0C 11602ac6454SAndrew Thompson #define UBSA_REG_FLOW_CTRL 0x10 11702ac6454SAndrew Thompson 11802ac6454SAndrew Thompson #define UBSA_PARITY_NONE 0x00 11902ac6454SAndrew Thompson #define UBSA_PARITY_EVEN 0x01 12002ac6454SAndrew Thompson #define UBSA_PARITY_ODD 0x02 12102ac6454SAndrew Thompson #define UBSA_PARITY_MARK 0x03 12202ac6454SAndrew Thompson #define UBSA_PARITY_SPACE 0x04 12302ac6454SAndrew Thompson 12402ac6454SAndrew Thompson #define UBSA_FLOW_NONE 0x0000 12502ac6454SAndrew Thompson #define UBSA_FLOW_OCTS 0x0001 12602ac6454SAndrew Thompson #define UBSA_FLOW_ODSR 0x0002 12702ac6454SAndrew Thompson #define UBSA_FLOW_IDSR 0x0004 12802ac6454SAndrew Thompson #define UBSA_FLOW_IDTR 0x0008 12902ac6454SAndrew Thompson #define UBSA_FLOW_IRTS 0x0010 13002ac6454SAndrew Thompson #define UBSA_FLOW_ORTS 0x0020 13102ac6454SAndrew Thompson #define UBSA_FLOW_UNKNOWN 0x0040 13202ac6454SAndrew Thompson #define UBSA_FLOW_OXON 0x0080 13302ac6454SAndrew Thompson #define UBSA_FLOW_IXON 0x0100 13402ac6454SAndrew Thompson 13502ac6454SAndrew Thompson /* line status register */ 13602ac6454SAndrew Thompson #define UBSA_LSR_TSRE 0x40 /* Transmitter empty: byte sent */ 13702ac6454SAndrew Thompson #define UBSA_LSR_TXRDY 0x20 /* Transmitter buffer empty */ 13802ac6454SAndrew Thompson #define UBSA_LSR_BI 0x10 /* Break detected */ 13902ac6454SAndrew Thompson #define UBSA_LSR_FE 0x08 /* Framing error: bad stop bit */ 14002ac6454SAndrew Thompson #define UBSA_LSR_PE 0x04 /* Parity error */ 14102ac6454SAndrew Thompson #define UBSA_LSR_OE 0x02 /* Overrun, lost incoming byte */ 14202ac6454SAndrew Thompson #define UBSA_LSR_RXRDY 0x01 /* Byte ready in Receive Buffer */ 14302ac6454SAndrew Thompson #define UBSA_LSR_RCV_MASK 0x1f /* Mask for incoming data or error */ 14402ac6454SAndrew Thompson 14502ac6454SAndrew Thompson /* modem status register */ 14602ac6454SAndrew Thompson /* All deltas are from the last read of the MSR. */ 14702ac6454SAndrew Thompson #define UBSA_MSR_DCD 0x80 /* Current Data Carrier Detect */ 14802ac6454SAndrew Thompson #define UBSA_MSR_RI 0x40 /* Current Ring Indicator */ 14902ac6454SAndrew Thompson #define UBSA_MSR_DSR 0x20 /* Current Data Set Ready */ 15002ac6454SAndrew Thompson #define UBSA_MSR_CTS 0x10 /* Current Clear to Send */ 15102ac6454SAndrew Thompson #define UBSA_MSR_DDCD 0x08 /* DCD has changed state */ 15202ac6454SAndrew Thompson #define UBSA_MSR_TERI 0x04 /* RI has toggled low to high */ 15302ac6454SAndrew Thompson #define UBSA_MSR_DDSR 0x02 /* DSR has changed state */ 15402ac6454SAndrew Thompson #define UBSA_MSR_DCTS 0x01 /* CTS has changed state */ 15502ac6454SAndrew Thompson 15602ac6454SAndrew Thompson enum { 15702ac6454SAndrew Thompson UBSA_BULK_DT_WR, 15802ac6454SAndrew Thompson UBSA_BULK_DT_RD, 15902ac6454SAndrew Thompson UBSA_INTR_DT_RD, 16002ac6454SAndrew Thompson UBSA_N_TRANSFER, 16102ac6454SAndrew Thompson }; 16202ac6454SAndrew Thompson 16302ac6454SAndrew Thompson struct ubsa_softc { 164760bc48eSAndrew Thompson struct ucom_super_softc sc_super_ucom; 165760bc48eSAndrew Thompson struct ucom_softc sc_ucom; 16602ac6454SAndrew Thompson 167760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UBSA_N_TRANSFER]; 168760bc48eSAndrew Thompson struct usb_device *sc_udev; 169deefe583SAndrew Thompson struct mtx sc_mtx; 17002ac6454SAndrew Thompson 17102ac6454SAndrew Thompson uint8_t sc_iface_no; /* interface number */ 17202ac6454SAndrew Thompson uint8_t sc_iface_index; /* interface index */ 17302ac6454SAndrew Thompson uint8_t sc_lsr; /* local status register */ 17402ac6454SAndrew Thompson uint8_t sc_msr; /* UBSA status register */ 17502ac6454SAndrew Thompson }; 17602ac6454SAndrew Thompson 17702ac6454SAndrew Thompson static device_probe_t ubsa_probe; 17802ac6454SAndrew Thompson static device_attach_t ubsa_attach; 17902ac6454SAndrew Thompson static device_detach_t ubsa_detach; 18002ac6454SAndrew Thompson 181e0a69b51SAndrew Thompson static usb_callback_t ubsa_write_callback; 182e0a69b51SAndrew Thompson static usb_callback_t ubsa_read_callback; 183e0a69b51SAndrew Thompson static usb_callback_t ubsa_intr_callback; 18402ac6454SAndrew Thompson 18502ac6454SAndrew Thompson static void ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t); 186760bc48eSAndrew Thompson static void ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t); 187760bc48eSAndrew Thompson static void ubsa_cfg_set_rts(struct ucom_softc *, uint8_t); 188760bc48eSAndrew Thompson static void ubsa_cfg_set_break(struct ucom_softc *, uint8_t); 189760bc48eSAndrew Thompson static int ubsa_pre_param(struct ucom_softc *, struct termios *); 190760bc48eSAndrew Thompson static void ubsa_cfg_param(struct ucom_softc *, struct termios *); 191760bc48eSAndrew Thompson static void ubsa_start_read(struct ucom_softc *); 192760bc48eSAndrew Thompson static void ubsa_stop_read(struct ucom_softc *); 193760bc48eSAndrew Thompson static void ubsa_start_write(struct ucom_softc *); 194760bc48eSAndrew Thompson static void ubsa_stop_write(struct ucom_softc *); 195760bc48eSAndrew Thompson static void ubsa_cfg_get_status(struct ucom_softc *, uint8_t *, 19602ac6454SAndrew Thompson uint8_t *); 197655dc9d0SAndrew Thompson static void ubsa_poll(struct ucom_softc *ucom); 19802ac6454SAndrew Thompson 199760bc48eSAndrew Thompson static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = { 20002ac6454SAndrew Thompson 20102ac6454SAndrew Thompson [UBSA_BULK_DT_WR] = { 20202ac6454SAndrew Thompson .type = UE_BULK, 20302ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 20402ac6454SAndrew Thompson .direction = UE_DIR_OUT, 2054eae601eSAndrew Thompson .bufsize = UBSA_BSIZE, /* bytes */ 2064eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 2074eae601eSAndrew Thompson .callback = &ubsa_write_callback, 20802ac6454SAndrew Thompson }, 20902ac6454SAndrew Thompson 21002ac6454SAndrew Thompson [UBSA_BULK_DT_RD] = { 21102ac6454SAndrew Thompson .type = UE_BULK, 21202ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 21302ac6454SAndrew Thompson .direction = UE_DIR_IN, 2144eae601eSAndrew Thompson .bufsize = UBSA_BSIZE, /* bytes */ 2154eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 2164eae601eSAndrew Thompson .callback = &ubsa_read_callback, 21702ac6454SAndrew Thompson }, 21802ac6454SAndrew Thompson 21902ac6454SAndrew Thompson [UBSA_INTR_DT_RD] = { 22002ac6454SAndrew Thompson .type = UE_INTERRUPT, 22102ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 22202ac6454SAndrew Thompson .direction = UE_DIR_IN, 2234eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 2244eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 2254eae601eSAndrew Thompson .callback = &ubsa_intr_callback, 22602ac6454SAndrew Thompson }, 22702ac6454SAndrew Thompson }; 22802ac6454SAndrew Thompson 229760bc48eSAndrew Thompson static const struct ucom_callback ubsa_callback = { 230a593f6b8SAndrew Thompson .ucom_cfg_get_status = &ubsa_cfg_get_status, 231a593f6b8SAndrew Thompson .ucom_cfg_set_dtr = &ubsa_cfg_set_dtr, 232a593f6b8SAndrew Thompson .ucom_cfg_set_rts = &ubsa_cfg_set_rts, 233a593f6b8SAndrew Thompson .ucom_cfg_set_break = &ubsa_cfg_set_break, 234a593f6b8SAndrew Thompson .ucom_cfg_param = &ubsa_cfg_param, 235a593f6b8SAndrew Thompson .ucom_pre_param = &ubsa_pre_param, 236a593f6b8SAndrew Thompson .ucom_start_read = &ubsa_start_read, 237a593f6b8SAndrew Thompson .ucom_stop_read = &ubsa_stop_read, 238a593f6b8SAndrew Thompson .ucom_start_write = &ubsa_start_write, 239a593f6b8SAndrew Thompson .ucom_stop_write = &ubsa_stop_write, 240655dc9d0SAndrew Thompson .ucom_poll = &ubsa_poll, 24102ac6454SAndrew Thompson }; 24202ac6454SAndrew Thompson 243760bc48eSAndrew Thompson static const struct usb_device_id ubsa_devs[] = { 24402ac6454SAndrew Thompson /* AnyData ADU-500A */ 24502ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)}, 24602ac6454SAndrew Thompson /* AnyData ADU-E100A/H */ 24702ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)}, 24802ac6454SAndrew Thompson /* Axesstel MV100H */ 24902ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)}, 25002ac6454SAndrew Thompson /* BELKIN F5U103 */ 25102ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)}, 25202ac6454SAndrew Thompson /* BELKIN F5U120 */ 25302ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)}, 25402ac6454SAndrew Thompson /* GoHubs GO-COM232 */ 25502ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)}, 25602ac6454SAndrew Thompson /* GoHubs GO-COM232 */ 25702ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)}, 25802ac6454SAndrew Thompson /* Peracom */ 25902ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)}, 26002ac6454SAndrew Thompson }; 26102ac6454SAndrew Thompson 26202ac6454SAndrew Thompson static device_method_t ubsa_methods[] = { 26302ac6454SAndrew Thompson DEVMETHOD(device_probe, ubsa_probe), 26402ac6454SAndrew Thompson DEVMETHOD(device_attach, ubsa_attach), 26502ac6454SAndrew Thompson DEVMETHOD(device_detach, ubsa_detach), 26602ac6454SAndrew Thompson {0, 0} 26702ac6454SAndrew Thompson }; 26802ac6454SAndrew Thompson 26902ac6454SAndrew Thompson static devclass_t ubsa_devclass; 27002ac6454SAndrew Thompson 27102ac6454SAndrew Thompson static driver_t ubsa_driver = { 27202ac6454SAndrew Thompson .name = "ubsa", 27302ac6454SAndrew Thompson .methods = ubsa_methods, 27402ac6454SAndrew Thompson .size = sizeof(struct ubsa_softc), 27502ac6454SAndrew Thompson }; 27602ac6454SAndrew Thompson 2779aef556dSAndrew Thompson DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0); 27802ac6454SAndrew Thompson MODULE_DEPEND(ubsa, ucom, 1, 1, 1); 27902ac6454SAndrew Thompson MODULE_DEPEND(ubsa, usb, 1, 1, 1); 280910cb8feSAndrew Thompson MODULE_VERSION(ubsa, 1); 28102ac6454SAndrew Thompson 28202ac6454SAndrew Thompson static int 28302ac6454SAndrew Thompson ubsa_probe(device_t dev) 28402ac6454SAndrew Thompson { 285760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 28602ac6454SAndrew Thompson 287f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) { 28802ac6454SAndrew Thompson return (ENXIO); 28902ac6454SAndrew Thompson } 29002ac6454SAndrew Thompson if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) { 29102ac6454SAndrew Thompson return (ENXIO); 29202ac6454SAndrew Thompson } 29302ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) { 29402ac6454SAndrew Thompson return (ENXIO); 29502ac6454SAndrew Thompson } 296a593f6b8SAndrew Thompson return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa)); 29702ac6454SAndrew Thompson } 29802ac6454SAndrew Thompson 29902ac6454SAndrew Thompson static int 30002ac6454SAndrew Thompson ubsa_attach(device_t dev) 30102ac6454SAndrew Thompson { 302760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 30302ac6454SAndrew Thompson struct ubsa_softc *sc = device_get_softc(dev); 30402ac6454SAndrew Thompson int error; 30502ac6454SAndrew Thompson 30602ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc); 30702ac6454SAndrew Thompson 308a593f6b8SAndrew Thompson device_set_usb_desc(dev); 309deefe583SAndrew Thompson mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF); 31002ac6454SAndrew Thompson 31102ac6454SAndrew Thompson sc->sc_udev = uaa->device; 31202ac6454SAndrew Thompson sc->sc_iface_no = uaa->info.bIfaceNum; 31302ac6454SAndrew Thompson sc->sc_iface_index = UBSA_IFACE_INDEX; 31402ac6454SAndrew Thompson 315a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index, 316deefe583SAndrew Thompson sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx); 31702ac6454SAndrew Thompson 31802ac6454SAndrew Thompson if (error) { 31902ac6454SAndrew Thompson DPRINTF("could not allocate all pipes\n"); 32002ac6454SAndrew Thompson goto detach; 32102ac6454SAndrew Thompson } 32202ac6454SAndrew Thompson /* clear stall at first run */ 323deefe583SAndrew Thompson mtx_lock(&sc->sc_mtx); 324ed6d949aSAndrew Thompson usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]); 325ed6d949aSAndrew Thompson usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]); 326deefe583SAndrew Thompson mtx_unlock(&sc->sc_mtx); 32702ac6454SAndrew Thompson 328a593f6b8SAndrew Thompson error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 329deefe583SAndrew Thompson &ubsa_callback, &sc->sc_mtx); 33002ac6454SAndrew Thompson if (error) { 331a593f6b8SAndrew Thompson DPRINTF("ucom_attach failed\n"); 33202ac6454SAndrew Thompson goto detach; 33302ac6454SAndrew Thompson } 33402ac6454SAndrew Thompson return (0); 33502ac6454SAndrew Thompson 33602ac6454SAndrew Thompson detach: 33702ac6454SAndrew Thompson ubsa_detach(dev); 33802ac6454SAndrew Thompson return (ENXIO); 33902ac6454SAndrew Thompson } 34002ac6454SAndrew Thompson 34102ac6454SAndrew Thompson static int 34202ac6454SAndrew Thompson ubsa_detach(device_t dev) 34302ac6454SAndrew Thompson { 34402ac6454SAndrew Thompson struct ubsa_softc *sc = device_get_softc(dev); 34502ac6454SAndrew Thompson 34602ac6454SAndrew Thompson DPRINTF("sc=%p\n", sc); 34702ac6454SAndrew Thompson 348*015bb88fSNick Hibma ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 349a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER); 350deefe583SAndrew Thompson mtx_destroy(&sc->sc_mtx); 35102ac6454SAndrew Thompson 35202ac6454SAndrew Thompson return (0); 35302ac6454SAndrew Thompson } 35402ac6454SAndrew Thompson 35502ac6454SAndrew Thompson static void 35602ac6454SAndrew Thompson ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value) 35702ac6454SAndrew Thompson { 358760bc48eSAndrew Thompson struct usb_device_request req; 359e0a69b51SAndrew Thompson usb_error_t err; 36002ac6454SAndrew Thompson 36102ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 36202ac6454SAndrew Thompson req.bRequest = index; 36302ac6454SAndrew Thompson USETW(req.wValue, value); 36402ac6454SAndrew Thompson req.wIndex[0] = sc->sc_iface_no; 36502ac6454SAndrew Thompson req.wIndex[1] = 0; 36602ac6454SAndrew Thompson USETW(req.wLength, 0); 36702ac6454SAndrew Thompson 368a593f6b8SAndrew Thompson err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 36902ac6454SAndrew Thompson &req, NULL, 0, 1000); 37002ac6454SAndrew Thompson if (err) { 37102ac6454SAndrew Thompson DPRINTFN(0, "device request failed, err=%s " 372a593f6b8SAndrew Thompson "(ignored)\n", usbd_errstr(err)); 37302ac6454SAndrew Thompson } 37402ac6454SAndrew Thompson } 37502ac6454SAndrew Thompson 37602ac6454SAndrew Thompson static void 377760bc48eSAndrew Thompson ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 37802ac6454SAndrew Thompson { 37902ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 38002ac6454SAndrew Thompson 38102ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff); 38202ac6454SAndrew Thompson 38302ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0); 38402ac6454SAndrew Thompson } 38502ac6454SAndrew Thompson 38602ac6454SAndrew Thompson static void 387760bc48eSAndrew Thompson ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 38802ac6454SAndrew Thompson { 38902ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 39002ac6454SAndrew Thompson 39102ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff); 39202ac6454SAndrew Thompson 39302ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0); 39402ac6454SAndrew Thompson } 39502ac6454SAndrew Thompson 39602ac6454SAndrew Thompson static void 397760bc48eSAndrew Thompson ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 39802ac6454SAndrew Thompson { 39902ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 40002ac6454SAndrew Thompson 40102ac6454SAndrew Thompson DPRINTF("onoff = %d\n", onoff); 40202ac6454SAndrew Thompson 40302ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0); 40402ac6454SAndrew Thompson } 40502ac6454SAndrew Thompson 40602ac6454SAndrew Thompson static int 407760bc48eSAndrew Thompson ubsa_pre_param(struct ucom_softc *ucom, struct termios *t) 40802ac6454SAndrew Thompson { 40902ac6454SAndrew Thompson 410b850ecc1SAndrew Thompson DPRINTF("sc = %p\n", ucom->sc_parent); 41102ac6454SAndrew Thompson 41202ac6454SAndrew Thompson switch (t->c_ospeed) { 41302ac6454SAndrew Thompson case B0: 41402ac6454SAndrew Thompson case B300: 41502ac6454SAndrew Thompson case B600: 41602ac6454SAndrew Thompson case B1200: 41702ac6454SAndrew Thompson case B2400: 41802ac6454SAndrew Thompson case B4800: 41902ac6454SAndrew Thompson case B9600: 42002ac6454SAndrew Thompson case B19200: 42102ac6454SAndrew Thompson case B38400: 42202ac6454SAndrew Thompson case B57600: 42302ac6454SAndrew Thompson case B115200: 42402ac6454SAndrew Thompson case B230400: 42502ac6454SAndrew Thompson break; 42602ac6454SAndrew Thompson default: 42702ac6454SAndrew Thompson return (EINVAL); 42802ac6454SAndrew Thompson } 42902ac6454SAndrew Thompson return (0); 43002ac6454SAndrew Thompson } 43102ac6454SAndrew Thompson 43202ac6454SAndrew Thompson static void 433760bc48eSAndrew Thompson ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t) 43402ac6454SAndrew Thompson { 43502ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 43602ac6454SAndrew Thompson uint16_t value = 0; 43702ac6454SAndrew Thompson 43802ac6454SAndrew Thompson DPRINTF("sc = %p\n", sc); 43902ac6454SAndrew Thompson 44002ac6454SAndrew Thompson switch (t->c_ospeed) { 44102ac6454SAndrew Thompson case B0: 44202ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0); 44302ac6454SAndrew Thompson ubsa_cfg_set_dtr(&sc->sc_ucom, 0); 44402ac6454SAndrew Thompson ubsa_cfg_set_rts(&sc->sc_ucom, 0); 44502ac6454SAndrew Thompson break; 44602ac6454SAndrew Thompson case B300: 44702ac6454SAndrew Thompson case B600: 44802ac6454SAndrew Thompson case B1200: 44902ac6454SAndrew Thompson case B2400: 45002ac6454SAndrew Thompson case B4800: 45102ac6454SAndrew Thompson case B9600: 45202ac6454SAndrew Thompson case B19200: 45302ac6454SAndrew Thompson case B38400: 45402ac6454SAndrew Thompson case B57600: 45502ac6454SAndrew Thompson case B115200: 45602ac6454SAndrew Thompson case B230400: 45702ac6454SAndrew Thompson value = B230400 / t->c_ospeed; 45802ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value); 45902ac6454SAndrew Thompson break; 46002ac6454SAndrew Thompson default: 46102ac6454SAndrew Thompson return; 46202ac6454SAndrew Thompson } 46302ac6454SAndrew Thompson 46402ac6454SAndrew Thompson if (t->c_cflag & PARENB) 46502ac6454SAndrew Thompson value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN; 46602ac6454SAndrew Thompson else 46702ac6454SAndrew Thompson value = UBSA_PARITY_NONE; 46802ac6454SAndrew Thompson 46902ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_PARITY, value); 47002ac6454SAndrew Thompson 47102ac6454SAndrew Thompson switch (t->c_cflag & CSIZE) { 47202ac6454SAndrew Thompson case CS5: 47302ac6454SAndrew Thompson value = 0; 47402ac6454SAndrew Thompson break; 47502ac6454SAndrew Thompson case CS6: 47602ac6454SAndrew Thompson value = 1; 47702ac6454SAndrew Thompson break; 47802ac6454SAndrew Thompson case CS7: 47902ac6454SAndrew Thompson value = 2; 48002ac6454SAndrew Thompson break; 48102ac6454SAndrew Thompson default: 48202ac6454SAndrew Thompson case CS8: 48302ac6454SAndrew Thompson value = 3; 48402ac6454SAndrew Thompson break; 48502ac6454SAndrew Thompson } 48602ac6454SAndrew Thompson 48702ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value); 48802ac6454SAndrew Thompson 48902ac6454SAndrew Thompson value = (t->c_cflag & CSTOPB) ? 1 : 0; 49002ac6454SAndrew Thompson 49102ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value); 49202ac6454SAndrew Thompson 49302ac6454SAndrew Thompson value = 0; 49402ac6454SAndrew Thompson if (t->c_cflag & CRTSCTS) 49502ac6454SAndrew Thompson value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS; 49602ac6454SAndrew Thompson 49702ac6454SAndrew Thompson if (t->c_iflag & (IXON | IXOFF)) 49802ac6454SAndrew Thompson value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON; 49902ac6454SAndrew Thompson 50002ac6454SAndrew Thompson ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value); 50102ac6454SAndrew Thompson } 50202ac6454SAndrew Thompson 50302ac6454SAndrew Thompson static void 504760bc48eSAndrew Thompson ubsa_start_read(struct ucom_softc *ucom) 50502ac6454SAndrew Thompson { 50602ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 50702ac6454SAndrew Thompson 50802ac6454SAndrew Thompson /* start interrupt endpoint */ 509a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]); 51002ac6454SAndrew Thompson 51102ac6454SAndrew Thompson /* start read endpoint */ 512a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]); 51302ac6454SAndrew Thompson } 51402ac6454SAndrew Thompson 51502ac6454SAndrew Thompson static void 516760bc48eSAndrew Thompson ubsa_stop_read(struct ucom_softc *ucom) 51702ac6454SAndrew Thompson { 51802ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 51902ac6454SAndrew Thompson 52002ac6454SAndrew Thompson /* stop interrupt endpoint */ 521a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]); 52202ac6454SAndrew Thompson 52302ac6454SAndrew Thompson /* stop read endpoint */ 524a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]); 52502ac6454SAndrew Thompson } 52602ac6454SAndrew Thompson 52702ac6454SAndrew Thompson static void 528760bc48eSAndrew Thompson ubsa_start_write(struct ucom_softc *ucom) 52902ac6454SAndrew Thompson { 53002ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 53102ac6454SAndrew Thompson 532a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]); 53302ac6454SAndrew Thompson } 53402ac6454SAndrew Thompson 53502ac6454SAndrew Thompson static void 536760bc48eSAndrew Thompson ubsa_stop_write(struct ucom_softc *ucom) 53702ac6454SAndrew Thompson { 53802ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 53902ac6454SAndrew Thompson 540a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]); 54102ac6454SAndrew Thompson } 54202ac6454SAndrew Thompson 54302ac6454SAndrew Thompson static void 544760bc48eSAndrew Thompson ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 54502ac6454SAndrew Thompson { 54602ac6454SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 54702ac6454SAndrew Thompson 54802ac6454SAndrew Thompson DPRINTF("\n"); 54902ac6454SAndrew Thompson 55002ac6454SAndrew Thompson *lsr = sc->sc_lsr; 55102ac6454SAndrew Thompson *msr = sc->sc_msr; 55202ac6454SAndrew Thompson } 55302ac6454SAndrew Thompson 55402ac6454SAndrew Thompson static void 555ed6d949aSAndrew Thompson ubsa_write_callback(struct usb_xfer *xfer, usb_error_t error) 55602ac6454SAndrew Thompson { 557ed6d949aSAndrew Thompson struct ubsa_softc *sc = usbd_xfer_softc(xfer); 558ed6d949aSAndrew Thompson struct usb_page_cache *pc; 55902ac6454SAndrew Thompson uint32_t actlen; 56002ac6454SAndrew Thompson 56102ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 56202ac6454SAndrew Thompson case USB_ST_SETUP: 56302ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 56402ac6454SAndrew Thompson tr_setup: 565ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 566ed6d949aSAndrew Thompson if (ucom_get_data(&sc->sc_ucom, pc, 0, 56702ac6454SAndrew Thompson UBSA_BSIZE, &actlen)) { 56802ac6454SAndrew Thompson 569ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, actlen); 570a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 57102ac6454SAndrew Thompson } 57202ac6454SAndrew Thompson return; 57302ac6454SAndrew Thompson 57402ac6454SAndrew Thompson default: /* Error */ 575ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 57602ac6454SAndrew Thompson /* try to clear stall first */ 577ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 57802ac6454SAndrew Thompson goto tr_setup; 57902ac6454SAndrew Thompson } 58002ac6454SAndrew Thompson return; 58102ac6454SAndrew Thompson 58202ac6454SAndrew Thompson } 58302ac6454SAndrew Thompson } 58402ac6454SAndrew Thompson 58502ac6454SAndrew Thompson static void 586ed6d949aSAndrew Thompson ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error) 58702ac6454SAndrew Thompson { 588ed6d949aSAndrew Thompson struct ubsa_softc *sc = usbd_xfer_softc(xfer); 589ed6d949aSAndrew Thompson struct usb_page_cache *pc; 590ed6d949aSAndrew Thompson int actlen; 591ed6d949aSAndrew Thompson 592ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 59302ac6454SAndrew Thompson 59402ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 59502ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 596ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 597ed6d949aSAndrew Thompson ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 59802ac6454SAndrew Thompson 59902ac6454SAndrew Thompson case USB_ST_SETUP: 60002ac6454SAndrew Thompson tr_setup: 601ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 602a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 60302ac6454SAndrew Thompson return; 60402ac6454SAndrew Thompson 60502ac6454SAndrew Thompson default: /* Error */ 606ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 60702ac6454SAndrew Thompson /* try to clear stall first */ 608ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 60902ac6454SAndrew Thompson goto tr_setup; 61002ac6454SAndrew Thompson } 61102ac6454SAndrew Thompson return; 61202ac6454SAndrew Thompson 61302ac6454SAndrew Thompson } 61402ac6454SAndrew Thompson } 61502ac6454SAndrew Thompson 61602ac6454SAndrew Thompson static void 617ed6d949aSAndrew Thompson ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error) 61802ac6454SAndrew Thompson { 619ed6d949aSAndrew Thompson struct ubsa_softc *sc = usbd_xfer_softc(xfer); 620ed6d949aSAndrew Thompson struct usb_page_cache *pc; 62102ac6454SAndrew Thompson uint8_t buf[4]; 622ed6d949aSAndrew Thompson int actlen; 623ed6d949aSAndrew Thompson 624ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 62502ac6454SAndrew Thompson 62602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 62702ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 62802ac6454SAndrew Thompson 629ed6d949aSAndrew Thompson if (actlen >= sizeof(buf)) { 630ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 631ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, buf, sizeof(buf)); 63202ac6454SAndrew Thompson 63302ac6454SAndrew Thompson /* 63402ac6454SAndrew Thompson * incidentally, Belkin adapter status bits match 63502ac6454SAndrew Thompson * UART 16550 bits 63602ac6454SAndrew Thompson */ 63702ac6454SAndrew Thompson sc->sc_lsr = buf[2]; 63802ac6454SAndrew Thompson sc->sc_msr = buf[3]; 63902ac6454SAndrew Thompson 64002ac6454SAndrew Thompson DPRINTF("lsr = 0x%02x, msr = 0x%02x\n", 64102ac6454SAndrew Thompson sc->sc_lsr, sc->sc_msr); 64202ac6454SAndrew Thompson 643a593f6b8SAndrew Thompson ucom_status_change(&sc->sc_ucom); 64402ac6454SAndrew Thompson } else { 645ed6d949aSAndrew Thompson DPRINTF("ignoring short packet, %d bytes\n", actlen); 64602ac6454SAndrew Thompson } 64702ac6454SAndrew Thompson 64802ac6454SAndrew Thompson case USB_ST_SETUP: 64902ac6454SAndrew Thompson tr_setup: 650ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 651a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 65202ac6454SAndrew Thompson return; 65302ac6454SAndrew Thompson 65402ac6454SAndrew Thompson default: /* Error */ 655ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 65602ac6454SAndrew Thompson /* try to clear stall first */ 657ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 65802ac6454SAndrew Thompson goto tr_setup; 65902ac6454SAndrew Thompson } 66002ac6454SAndrew Thompson return; 66102ac6454SAndrew Thompson 66202ac6454SAndrew Thompson } 66302ac6454SAndrew Thompson } 664655dc9d0SAndrew Thompson 665655dc9d0SAndrew Thompson static void 666655dc9d0SAndrew Thompson ubsa_poll(struct ucom_softc *ucom) 667655dc9d0SAndrew Thompson { 668655dc9d0SAndrew Thompson struct ubsa_softc *sc = ucom->sc_parent; 669655dc9d0SAndrew Thompson usbd_transfer_poll(sc->sc_xfer, UBSA_N_TRANSFER); 670655dc9d0SAndrew Thompson 671655dc9d0SAndrew Thompson } 672