102ac6454SAndrew Thompson #include <sys/cdefs.h> 202ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 302ac6454SAndrew Thompson 402ac6454SAndrew Thompson /*- 5718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 6718cf2ccSPedro F. Giffuni * 702ac6454SAndrew Thompson * Copyright (c) 2003 Scott Long 802ac6454SAndrew Thompson * All rights reserved. 902ac6454SAndrew Thompson * 1002ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 1102ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 1202ac6454SAndrew Thompson * are met: 1302ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1402ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1502ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1602ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1702ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1802ac6454SAndrew Thompson * 1902ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2002ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2102ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2202ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2302ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2402ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2502ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2602ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2702ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2802ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2902ac6454SAndrew Thompson * SUCH DAMAGE. 3002ac6454SAndrew Thompson * 3102ac6454SAndrew Thompson */ 3202ac6454SAndrew Thompson 3302ac6454SAndrew Thompson /* 3402ac6454SAndrew Thompson * Driver for the MCT (Magic Control Technology) USB-RS232 Converter. 3502ac6454SAndrew Thompson * Based on the superb documentation from the linux mct_u232 driver by 3602ac6454SAndrew Thompson * Wolfgang Grandeggar <wolfgang@cec.ch>. 3702ac6454SAndrew Thompson * This device smells a lot like the Belkin F5U103, except that it has 3802ac6454SAndrew Thompson * suffered some mild brain-damage. This driver is based off of the ubsa.c 395d38a4d4SEd Schouten * driver from Alexander Kabaev <kan@FreeBSD.org>. Merging the two together 4002ac6454SAndrew Thompson * might be useful, though the subtle differences might lead to lots of 4102ac6454SAndrew Thompson * #ifdef's. 4202ac6454SAndrew Thompson */ 4302ac6454SAndrew Thompson 4402ac6454SAndrew Thompson /* 4502ac6454SAndrew Thompson * NOTE: all function names beginning like "umct_cfg_" can only 4602ac6454SAndrew Thompson * be called from within the config thread function ! 4702ac6454SAndrew Thompson */ 4802ac6454SAndrew Thompson 49ed6d949aSAndrew Thompson #include <sys/stdint.h> 50ed6d949aSAndrew Thompson #include <sys/stddef.h> 51ed6d949aSAndrew Thompson #include <sys/param.h> 52ed6d949aSAndrew Thompson #include <sys/queue.h> 53ed6d949aSAndrew Thompson #include <sys/types.h> 54ed6d949aSAndrew Thompson #include <sys/systm.h> 55ed6d949aSAndrew Thompson #include <sys/kernel.h> 56ed6d949aSAndrew Thompson #include <sys/bus.h> 57ed6d949aSAndrew Thompson #include <sys/module.h> 58ed6d949aSAndrew Thompson #include <sys/lock.h> 59ed6d949aSAndrew Thompson #include <sys/mutex.h> 60ed6d949aSAndrew Thompson #include <sys/condvar.h> 61ed6d949aSAndrew Thompson #include <sys/sysctl.h> 62ed6d949aSAndrew Thompson #include <sys/sx.h> 63ed6d949aSAndrew Thompson #include <sys/unistd.h> 64ed6d949aSAndrew Thompson #include <sys/callout.h> 65ed6d949aSAndrew Thompson #include <sys/malloc.h> 66ed6d949aSAndrew Thompson #include <sys/priv.h> 67ed6d949aSAndrew Thompson 6802ac6454SAndrew Thompson #include <dev/usb/usb.h> 69ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 70ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 71ed6d949aSAndrew Thompson #include "usbdevs.h" 7202ac6454SAndrew Thompson 73a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_debug 7402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 7502ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 7602ac6454SAndrew Thompson 7702ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h> 7802ac6454SAndrew Thompson 7902ac6454SAndrew Thompson /* The UMCT advertises the standard 8250 UART registers */ 8002ac6454SAndrew Thompson #define UMCT_GET_MSR 2 /* Get Modem Status Register */ 8102ac6454SAndrew Thompson #define UMCT_GET_MSR_SIZE 1 8202ac6454SAndrew Thompson #define UMCT_GET_LCR 6 /* Get Line Control Register */ 8302ac6454SAndrew Thompson #define UMCT_GET_LCR_SIZE 1 8402ac6454SAndrew Thompson #define UMCT_SET_BAUD 5 /* Set the Baud Rate Divisor */ 8502ac6454SAndrew Thompson #define UMCT_SET_BAUD_SIZE 4 8602ac6454SAndrew Thompson #define UMCT_SET_LCR 7 /* Set Line Control Register */ 8702ac6454SAndrew Thompson #define UMCT_SET_LCR_SIZE 1 8802ac6454SAndrew Thompson #define UMCT_SET_MCR 10 /* Set Modem Control Register */ 8902ac6454SAndrew Thompson #define UMCT_SET_MCR_SIZE 1 9002ac6454SAndrew Thompson 91d88e7bb0SIan Lepore #define UMCT_MSR_CTS_CHG 0x01 92d88e7bb0SIan Lepore #define UMCT_MSR_DSR_CHG 0x02 93d88e7bb0SIan Lepore #define UMCT_MSR_RI_CHG 0x04 94d88e7bb0SIan Lepore #define UMCT_MSR_CD_CHG 0x08 95d88e7bb0SIan Lepore #define UMCT_MSR_CTS 0x10 96d88e7bb0SIan Lepore #define UMCT_MSR_RTS 0x20 97d88e7bb0SIan Lepore #define UMCT_MSR_RI 0x40 98d88e7bb0SIan Lepore #define UMCT_MSR_CD 0x80 99d88e7bb0SIan Lepore 10002ac6454SAndrew Thompson #define UMCT_INTR_INTERVAL 100 10102ac6454SAndrew Thompson #define UMCT_IFACE_INDEX 0 102774651eeSAndrew Thompson #define UMCT_CONFIG_INDEX 0 10302ac6454SAndrew Thompson 10402ac6454SAndrew Thompson enum { 10502ac6454SAndrew Thompson UMCT_BULK_DT_WR, 10602ac6454SAndrew Thompson UMCT_BULK_DT_RD, 10702ac6454SAndrew Thompson UMCT_INTR_DT_RD, 10802ac6454SAndrew Thompson UMCT_N_TRANSFER, 10902ac6454SAndrew Thompson }; 11002ac6454SAndrew Thompson 11102ac6454SAndrew Thompson struct umct_softc { 112760bc48eSAndrew Thompson struct ucom_super_softc sc_super_ucom; 113760bc48eSAndrew Thompson struct ucom_softc sc_ucom; 11402ac6454SAndrew Thompson 115760bc48eSAndrew Thompson struct usb_device *sc_udev; 116760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UMCT_N_TRANSFER]; 117deefe583SAndrew Thompson struct mtx sc_mtx; 11802ac6454SAndrew Thompson 11902ac6454SAndrew Thompson uint32_t sc_unit; 12002ac6454SAndrew Thompson 12102ac6454SAndrew Thompson uint16_t sc_obufsize; 12202ac6454SAndrew Thompson 12302ac6454SAndrew Thompson uint8_t sc_lsr; 12402ac6454SAndrew Thompson uint8_t sc_msr; 12502ac6454SAndrew Thompson uint8_t sc_lcr; 12602ac6454SAndrew Thompson uint8_t sc_mcr; 12702ac6454SAndrew Thompson uint8_t sc_iface_no; 128585b13f2SAndrew Thompson uint8_t sc_swap_cb; 12902ac6454SAndrew Thompson }; 13002ac6454SAndrew Thompson 13102ac6454SAndrew Thompson /* prototypes */ 13202ac6454SAndrew Thompson 13302ac6454SAndrew Thompson static device_probe_t umct_probe; 13402ac6454SAndrew Thompson static device_attach_t umct_attach; 13502ac6454SAndrew Thompson static device_detach_t umct_detach; 136c01fc06eSHans Petter Selasky static void umct_free_softc(struct umct_softc *); 13702ac6454SAndrew Thompson 138e0a69b51SAndrew Thompson static usb_callback_t umct_intr_callback; 139585b13f2SAndrew Thompson static usb_callback_t umct_intr_callback_sub; 140e0a69b51SAndrew Thompson static usb_callback_t umct_read_callback; 141585b13f2SAndrew Thompson static usb_callback_t umct_read_callback_sub; 142585b13f2SAndrew Thompson static usb_callback_t umct_write_callback; 14302ac6454SAndrew Thompson 14402ac6454SAndrew Thompson static void umct_cfg_do_request(struct umct_softc *sc, uint8_t request, 14502ac6454SAndrew Thompson uint16_t len, uint32_t value); 1465805d178SHans Petter Selasky static void umct_free(struct ucom_softc *); 147760bc48eSAndrew Thompson static void umct_cfg_get_status(struct ucom_softc *, uint8_t *, 14802ac6454SAndrew Thompson uint8_t *); 149760bc48eSAndrew Thompson static void umct_cfg_set_break(struct ucom_softc *, uint8_t); 150760bc48eSAndrew Thompson static void umct_cfg_set_dtr(struct ucom_softc *, uint8_t); 151760bc48eSAndrew Thompson static void umct_cfg_set_rts(struct ucom_softc *, uint8_t); 15202ac6454SAndrew Thompson static uint8_t umct_calc_baud(uint32_t); 153760bc48eSAndrew Thompson static int umct_pre_param(struct ucom_softc *, struct termios *); 154760bc48eSAndrew Thompson static void umct_cfg_param(struct ucom_softc *, struct termios *); 155760bc48eSAndrew Thompson static void umct_start_read(struct ucom_softc *); 156760bc48eSAndrew Thompson static void umct_stop_read(struct ucom_softc *); 157760bc48eSAndrew Thompson static void umct_start_write(struct ucom_softc *); 158760bc48eSAndrew Thompson static void umct_stop_write(struct ucom_softc *); 159655dc9d0SAndrew Thompson static void umct_poll(struct ucom_softc *ucom); 16002ac6454SAndrew Thompson 161760bc48eSAndrew Thompson static const struct usb_config umct_config[UMCT_N_TRANSFER] = { 16202ac6454SAndrew Thompson [UMCT_BULK_DT_WR] = { 16302ac6454SAndrew Thompson .type = UE_BULK, 16402ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 16502ac6454SAndrew Thompson .direction = UE_DIR_OUT, 1664eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 1674eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 1684eae601eSAndrew Thompson .callback = &umct_write_callback, 16902ac6454SAndrew Thompson }, 17002ac6454SAndrew Thompson 17102ac6454SAndrew Thompson [UMCT_BULK_DT_RD] = { 17202ac6454SAndrew Thompson .type = UE_INTERRUPT, 17302ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 17402ac6454SAndrew Thompson .direction = UE_DIR_IN, 1754eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 1764eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 1774eae601eSAndrew Thompson .callback = &umct_read_callback, 17802ac6454SAndrew Thompson .ep_index = 0, /* first interrupt endpoint */ 17902ac6454SAndrew Thompson }, 18002ac6454SAndrew Thompson 18102ac6454SAndrew Thompson [UMCT_INTR_DT_RD] = { 18202ac6454SAndrew Thompson .type = UE_INTERRUPT, 18302ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 18402ac6454SAndrew Thompson .direction = UE_DIR_IN, 1854eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 1864eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 1874eae601eSAndrew Thompson .callback = &umct_intr_callback, 18802ac6454SAndrew Thompson .ep_index = 1, /* second interrupt endpoint */ 18902ac6454SAndrew Thompson }, 19002ac6454SAndrew Thompson }; 19102ac6454SAndrew Thompson 192760bc48eSAndrew Thompson static const struct ucom_callback umct_callback = { 193a593f6b8SAndrew Thompson .ucom_cfg_get_status = &umct_cfg_get_status, 194a593f6b8SAndrew Thompson .ucom_cfg_set_dtr = &umct_cfg_set_dtr, 195a593f6b8SAndrew Thompson .ucom_cfg_set_rts = &umct_cfg_set_rts, 196a593f6b8SAndrew Thompson .ucom_cfg_set_break = &umct_cfg_set_break, 197a593f6b8SAndrew Thompson .ucom_cfg_param = &umct_cfg_param, 198a593f6b8SAndrew Thompson .ucom_pre_param = &umct_pre_param, 199a593f6b8SAndrew Thompson .ucom_start_read = &umct_start_read, 200a593f6b8SAndrew Thompson .ucom_stop_read = &umct_stop_read, 201a593f6b8SAndrew Thompson .ucom_start_write = &umct_start_write, 202a593f6b8SAndrew Thompson .ucom_stop_write = &umct_stop_write, 203655dc9d0SAndrew Thompson .ucom_poll = &umct_poll, 2045805d178SHans Petter Selasky .ucom_free = &umct_free, 20502ac6454SAndrew Thompson }; 20602ac6454SAndrew Thompson 207f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID umct_devs[] = { 20802ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)}, 20902ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)}, 21002ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)}, 21102ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)}, 21202ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)}, 21302ac6454SAndrew Thompson }; 21402ac6454SAndrew Thompson 21502ac6454SAndrew Thompson static device_method_t umct_methods[] = { 21602ac6454SAndrew Thompson DEVMETHOD(device_probe, umct_probe), 21702ac6454SAndrew Thompson DEVMETHOD(device_attach, umct_attach), 21802ac6454SAndrew Thompson DEVMETHOD(device_detach, umct_detach), 2195805d178SHans Petter Selasky DEVMETHOD_END 22002ac6454SAndrew Thompson }; 22102ac6454SAndrew Thompson 22202ac6454SAndrew Thompson static driver_t umct_driver = { 22302ac6454SAndrew Thompson .name = "umct", 22402ac6454SAndrew Thompson .methods = umct_methods, 22502ac6454SAndrew Thompson .size = sizeof(struct umct_softc), 22602ac6454SAndrew Thompson }; 22702ac6454SAndrew Thompson 228*bc9372d7SJohn Baldwin DRIVER_MODULE(umct, uhub, umct_driver, NULL, NULL); 22902ac6454SAndrew Thompson MODULE_DEPEND(umct, ucom, 1, 1, 1); 23002ac6454SAndrew Thompson MODULE_DEPEND(umct, usb, 1, 1, 1); 231910cb8feSAndrew Thompson MODULE_VERSION(umct, 1); 232f809f280SWarner Losh USB_PNP_HOST_INFO(umct_devs); 23302ac6454SAndrew Thompson 23402ac6454SAndrew Thompson static int 23502ac6454SAndrew Thompson umct_probe(device_t dev) 23602ac6454SAndrew Thompson { 237760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 23802ac6454SAndrew Thompson 239f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) { 24002ac6454SAndrew Thompson return (ENXIO); 24102ac6454SAndrew Thompson } 24202ac6454SAndrew Thompson if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) { 24302ac6454SAndrew Thompson return (ENXIO); 24402ac6454SAndrew Thompson } 24502ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) { 24602ac6454SAndrew Thompson return (ENXIO); 24702ac6454SAndrew Thompson } 248a593f6b8SAndrew Thompson return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa)); 24902ac6454SAndrew Thompson } 25002ac6454SAndrew Thompson 25102ac6454SAndrew Thompson static int 25202ac6454SAndrew Thompson umct_attach(device_t dev) 25302ac6454SAndrew Thompson { 254760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 25502ac6454SAndrew Thompson struct umct_softc *sc = device_get_softc(dev); 25602ac6454SAndrew Thompson int32_t error; 257585b13f2SAndrew Thompson uint16_t maxp; 25802ac6454SAndrew Thompson uint8_t iface_index; 25902ac6454SAndrew Thompson 26002ac6454SAndrew Thompson sc->sc_udev = uaa->device; 26102ac6454SAndrew Thompson sc->sc_unit = device_get_unit(dev); 26202ac6454SAndrew Thompson 263a593f6b8SAndrew Thompson device_set_usb_desc(dev); 264deefe583SAndrew Thompson mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF); 2655805d178SHans Petter Selasky ucom_ref(&sc->sc_super_ucom); 26602ac6454SAndrew Thompson 26702ac6454SAndrew Thompson sc->sc_iface_no = uaa->info.bIfaceNum; 26802ac6454SAndrew Thompson 26902ac6454SAndrew Thompson iface_index = UMCT_IFACE_INDEX; 270a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device, &iface_index, 271deefe583SAndrew Thompson sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx); 27202ac6454SAndrew Thompson 27302ac6454SAndrew Thompson if (error) { 27402ac6454SAndrew Thompson device_printf(dev, "allocating USB " 275767cb2e2SAndrew Thompson "transfers failed\n"); 27602ac6454SAndrew Thompson goto detach; 27702ac6454SAndrew Thompson } 278585b13f2SAndrew Thompson 27902ac6454SAndrew Thompson /* 28002ac6454SAndrew Thompson * The real bulk-in endpoint is also marked as an interrupt. 28102ac6454SAndrew Thompson * The only way to differentiate it from the real interrupt 28202ac6454SAndrew Thompson * endpoint is to look at the wMaxPacketSize field. 28302ac6454SAndrew Thompson */ 284585b13f2SAndrew Thompson maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]); 28502ac6454SAndrew Thompson if (maxp == 0x2) { 28602ac6454SAndrew Thompson /* guessed wrong - switch around endpoints */ 28702ac6454SAndrew Thompson 288760bc48eSAndrew Thompson struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD]; 28902ac6454SAndrew Thompson 29002ac6454SAndrew Thompson sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD]; 29102ac6454SAndrew Thompson sc->sc_xfer[UMCT_BULK_DT_RD] = temp; 292585b13f2SAndrew Thompson sc->sc_swap_cb = 1; 29302ac6454SAndrew Thompson } 294585b13f2SAndrew Thompson 295ed6d949aSAndrew Thompson sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]); 29602ac6454SAndrew Thompson 29702ac6454SAndrew Thompson if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) { 29802ac6454SAndrew Thompson if (sc->sc_obufsize > 16) { 29902ac6454SAndrew Thompson sc->sc_obufsize = 16; 30002ac6454SAndrew Thompson } 30102ac6454SAndrew Thompson } 302a593f6b8SAndrew Thompson error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc, 303deefe583SAndrew Thompson &umct_callback, &sc->sc_mtx); 30402ac6454SAndrew Thompson if (error) { 30502ac6454SAndrew Thompson goto detach; 30602ac6454SAndrew Thompson } 3076416c259SNick Hibma ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev); 3086416c259SNick Hibma 30902ac6454SAndrew Thompson return (0); /* success */ 31002ac6454SAndrew Thompson 31102ac6454SAndrew Thompson detach: 31202ac6454SAndrew Thompson umct_detach(dev); 31302ac6454SAndrew Thompson return (ENXIO); /* failure */ 31402ac6454SAndrew Thompson } 31502ac6454SAndrew Thompson 31602ac6454SAndrew Thompson static int 31702ac6454SAndrew Thompson umct_detach(device_t dev) 31802ac6454SAndrew Thompson { 31902ac6454SAndrew Thompson struct umct_softc *sc = device_get_softc(dev); 32002ac6454SAndrew Thompson 321015bb88fSNick Hibma ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom); 322a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER); 32302ac6454SAndrew Thompson 324c01fc06eSHans Petter Selasky device_claim_softc(dev); 325c01fc06eSHans Petter Selasky 326c01fc06eSHans Petter Selasky umct_free_softc(sc); 327c01fc06eSHans Petter Selasky 32802ac6454SAndrew Thompson return (0); 32902ac6454SAndrew Thompson } 33002ac6454SAndrew Thompson 3315805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(umct); 3325805d178SHans Petter Selasky 3335805d178SHans Petter Selasky static void 334c01fc06eSHans Petter Selasky umct_free_softc(struct umct_softc *sc) 3355805d178SHans Petter Selasky { 3365805d178SHans Petter Selasky if (ucom_unref(&sc->sc_super_ucom)) { 3375805d178SHans Petter Selasky mtx_destroy(&sc->sc_mtx); 338c01fc06eSHans Petter Selasky device_free_softc(sc); 3395805d178SHans Petter Selasky } 3405805d178SHans Petter Selasky } 3415805d178SHans Petter Selasky 3425805d178SHans Petter Selasky static void 3435805d178SHans Petter Selasky umct_free(struct ucom_softc *ucom) 3445805d178SHans Petter Selasky { 345c01fc06eSHans Petter Selasky umct_free_softc(ucom->sc_parent); 3465805d178SHans Petter Selasky } 3475805d178SHans Petter Selasky 34802ac6454SAndrew Thompson static void 34902ac6454SAndrew Thompson umct_cfg_do_request(struct umct_softc *sc, uint8_t request, 35002ac6454SAndrew Thompson uint16_t len, uint32_t value) 35102ac6454SAndrew Thompson { 352760bc48eSAndrew Thompson struct usb_device_request req; 353e0a69b51SAndrew Thompson usb_error_t err; 35402ac6454SAndrew Thompson uint8_t temp[4]; 35502ac6454SAndrew Thompson 35602ac6454SAndrew Thompson if (len > 4) 35702ac6454SAndrew Thompson len = 4; 35802ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 35902ac6454SAndrew Thompson req.bRequest = request; 36002ac6454SAndrew Thompson USETW(req.wValue, 0); 36102ac6454SAndrew Thompson req.wIndex[0] = sc->sc_iface_no; 36202ac6454SAndrew Thompson req.wIndex[1] = 0; 36302ac6454SAndrew Thompson USETW(req.wLength, len); 36402ac6454SAndrew Thompson USETDW(temp, value); 36502ac6454SAndrew Thompson 366a593f6b8SAndrew Thompson err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 36702ac6454SAndrew Thompson &req, temp, 0, 1000); 36802ac6454SAndrew Thompson if (err) { 36902ac6454SAndrew Thompson DPRINTFN(0, "device request failed, err=%s " 370a593f6b8SAndrew Thompson "(ignored)\n", usbd_errstr(err)); 37102ac6454SAndrew Thompson } 37202ac6454SAndrew Thompson return; 37302ac6454SAndrew Thompson } 37402ac6454SAndrew Thompson 37502ac6454SAndrew Thompson static void 376585b13f2SAndrew Thompson umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error) 37702ac6454SAndrew Thompson { 378ed6d949aSAndrew Thompson struct umct_softc *sc = usbd_xfer_softc(xfer); 379ed6d949aSAndrew Thompson struct usb_page_cache *pc; 38002ac6454SAndrew Thompson uint8_t buf[2]; 381ed6d949aSAndrew Thompson int actlen; 382ed6d949aSAndrew Thompson 383ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 38402ac6454SAndrew Thompson 38502ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 38602ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 387ed6d949aSAndrew Thompson if (actlen < 2) { 38802ac6454SAndrew Thompson DPRINTF("too short message\n"); 38902ac6454SAndrew Thompson goto tr_setup; 39002ac6454SAndrew Thompson } 391ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 392ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, buf, sizeof(buf)); 39302ac6454SAndrew Thompson 394d88e7bb0SIan Lepore /* 395d88e7bb0SIan Lepore * MSR bits need translation from ns16550 to SER_* values. 396d88e7bb0SIan Lepore * LSR bits are ns16550 in hardware and ucom. 397d88e7bb0SIan Lepore */ 398d88e7bb0SIan Lepore sc->sc_msr = 0; 399d88e7bb0SIan Lepore if (buf[0] & UMCT_MSR_CTS) 400d88e7bb0SIan Lepore sc->sc_msr |= SER_CTS; 401d88e7bb0SIan Lepore if (buf[0] & UMCT_MSR_CD) 402d88e7bb0SIan Lepore sc->sc_msr |= SER_DCD; 403d88e7bb0SIan Lepore if (buf[0] & UMCT_MSR_RI) 404d88e7bb0SIan Lepore sc->sc_msr |= SER_RI; 405d88e7bb0SIan Lepore if (buf[0] & UMCT_MSR_RTS) 406d88e7bb0SIan Lepore sc->sc_msr |= SER_DSR; 40702ac6454SAndrew Thompson sc->sc_lsr = buf[1]; 40802ac6454SAndrew Thompson 409a593f6b8SAndrew Thompson ucom_status_change(&sc->sc_ucom); 410d88e7bb0SIan Lepore /* FALLTHROUGH */ 41102ac6454SAndrew Thompson case USB_ST_SETUP: 41202ac6454SAndrew Thompson tr_setup: 413ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 414a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 41502ac6454SAndrew Thompson return; 41602ac6454SAndrew Thompson 41702ac6454SAndrew Thompson default: /* Error */ 418ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 41902ac6454SAndrew Thompson /* try to clear stall first */ 420ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 42102ac6454SAndrew Thompson goto tr_setup; 42202ac6454SAndrew Thompson } 42302ac6454SAndrew Thompson return; 42402ac6454SAndrew Thompson } 42502ac6454SAndrew Thompson } 42602ac6454SAndrew Thompson 42702ac6454SAndrew Thompson static void 428760bc48eSAndrew Thompson umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr) 42902ac6454SAndrew Thompson { 43002ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 43102ac6454SAndrew Thompson 43202ac6454SAndrew Thompson *lsr = sc->sc_lsr; 43302ac6454SAndrew Thompson *msr = sc->sc_msr; 43402ac6454SAndrew Thompson } 43502ac6454SAndrew Thompson 43602ac6454SAndrew Thompson static void 437760bc48eSAndrew Thompson umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff) 43802ac6454SAndrew Thompson { 43902ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 44002ac6454SAndrew Thompson 44102ac6454SAndrew Thompson if (onoff) 44202ac6454SAndrew Thompson sc->sc_lcr |= 0x40; 44302ac6454SAndrew Thompson else 44402ac6454SAndrew Thompson sc->sc_lcr &= ~0x40; 44502ac6454SAndrew Thompson 44602ac6454SAndrew Thompson umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr); 44702ac6454SAndrew Thompson } 44802ac6454SAndrew Thompson 44902ac6454SAndrew Thompson static void 450760bc48eSAndrew Thompson umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff) 45102ac6454SAndrew Thompson { 45202ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 45302ac6454SAndrew Thompson 45402ac6454SAndrew Thompson if (onoff) 45502ac6454SAndrew Thompson sc->sc_mcr |= 0x01; 45602ac6454SAndrew Thompson else 45702ac6454SAndrew Thompson sc->sc_mcr &= ~0x01; 45802ac6454SAndrew Thompson 45902ac6454SAndrew Thompson umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr); 46002ac6454SAndrew Thompson } 46102ac6454SAndrew Thompson 46202ac6454SAndrew Thompson static void 463760bc48eSAndrew Thompson umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff) 46402ac6454SAndrew Thompson { 46502ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 46602ac6454SAndrew Thompson 46702ac6454SAndrew Thompson if (onoff) 46802ac6454SAndrew Thompson sc->sc_mcr |= 0x02; 46902ac6454SAndrew Thompson else 47002ac6454SAndrew Thompson sc->sc_mcr &= ~0x02; 47102ac6454SAndrew Thompson 47202ac6454SAndrew Thompson umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr); 47302ac6454SAndrew Thompson } 47402ac6454SAndrew Thompson 47502ac6454SAndrew Thompson static uint8_t 47602ac6454SAndrew Thompson umct_calc_baud(uint32_t baud) 47702ac6454SAndrew Thompson { 47802ac6454SAndrew Thompson switch (baud) { 4791645a4aeSWarner Losh case B300: 4801645a4aeSWarner Losh return (0x1); 48102ac6454SAndrew Thompson case B600: 48202ac6454SAndrew Thompson return (0x2); 48302ac6454SAndrew Thompson case B1200: 48402ac6454SAndrew Thompson return (0x3); 48502ac6454SAndrew Thompson case B2400: 48602ac6454SAndrew Thompson return (0x4); 48702ac6454SAndrew Thompson case B4800: 48802ac6454SAndrew Thompson return (0x6); 48902ac6454SAndrew Thompson case B9600: 49002ac6454SAndrew Thompson return (0x8); 49102ac6454SAndrew Thompson case B19200: 49202ac6454SAndrew Thompson return (0x9); 49302ac6454SAndrew Thompson case B38400: 49402ac6454SAndrew Thompson return (0xa); 49502ac6454SAndrew Thompson case B57600: 49602ac6454SAndrew Thompson return (0xb); 49702ac6454SAndrew Thompson case 115200: 49802ac6454SAndrew Thompson return (0xc); 49902ac6454SAndrew Thompson case B0: 50002ac6454SAndrew Thompson default: 50102ac6454SAndrew Thompson break; 50202ac6454SAndrew Thompson } 50302ac6454SAndrew Thompson return (0x0); 50402ac6454SAndrew Thompson } 50502ac6454SAndrew Thompson 50602ac6454SAndrew Thompson static int 507760bc48eSAndrew Thompson umct_pre_param(struct ucom_softc *ucom, struct termios *t) 50802ac6454SAndrew Thompson { 50902ac6454SAndrew Thompson return (0); /* we accept anything */ 51002ac6454SAndrew Thompson } 51102ac6454SAndrew Thompson 51202ac6454SAndrew Thompson static void 513760bc48eSAndrew Thompson umct_cfg_param(struct ucom_softc *ucom, struct termios *t) 51402ac6454SAndrew Thompson { 51502ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 51602ac6454SAndrew Thompson uint32_t value; 51702ac6454SAndrew Thompson 51802ac6454SAndrew Thompson value = umct_calc_baud(t->c_ospeed); 51902ac6454SAndrew Thompson umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value); 52002ac6454SAndrew Thompson 52102ac6454SAndrew Thompson value = (sc->sc_lcr & 0x40); 52202ac6454SAndrew Thompson 52302ac6454SAndrew Thompson switch (t->c_cflag & CSIZE) { 52402ac6454SAndrew Thompson case CS5: 52502ac6454SAndrew Thompson value |= 0x0; 52602ac6454SAndrew Thompson break; 52702ac6454SAndrew Thompson case CS6: 52802ac6454SAndrew Thompson value |= 0x1; 52902ac6454SAndrew Thompson break; 53002ac6454SAndrew Thompson case CS7: 53102ac6454SAndrew Thompson value |= 0x2; 53202ac6454SAndrew Thompson break; 53302ac6454SAndrew Thompson default: 53402ac6454SAndrew Thompson case CS8: 53502ac6454SAndrew Thompson value |= 0x3; 53602ac6454SAndrew Thompson break; 53702ac6454SAndrew Thompson } 53802ac6454SAndrew Thompson 53902ac6454SAndrew Thompson value |= (t->c_cflag & CSTOPB) ? 0x4 : 0; 54002ac6454SAndrew Thompson if (t->c_cflag & PARENB) { 54102ac6454SAndrew Thompson value |= 0x8; 54202ac6454SAndrew Thompson value |= (t->c_cflag & PARODD) ? 0x0 : 0x10; 54302ac6454SAndrew Thompson } 54402ac6454SAndrew Thompson /* 54502ac6454SAndrew Thompson * XXX There doesn't seem to be a way to tell the device 54602ac6454SAndrew Thompson * to use flow control. 54702ac6454SAndrew Thompson */ 54802ac6454SAndrew Thompson 54902ac6454SAndrew Thompson sc->sc_lcr = value; 55002ac6454SAndrew Thompson umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value); 55102ac6454SAndrew Thompson } 55202ac6454SAndrew Thompson 55302ac6454SAndrew Thompson static void 554760bc48eSAndrew Thompson umct_start_read(struct ucom_softc *ucom) 55502ac6454SAndrew Thompson { 55602ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 55702ac6454SAndrew Thompson 55802ac6454SAndrew Thompson /* start interrupt endpoint */ 559a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]); 56002ac6454SAndrew Thompson 56102ac6454SAndrew Thompson /* start read endpoint */ 562a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]); 56302ac6454SAndrew Thompson } 56402ac6454SAndrew Thompson 56502ac6454SAndrew Thompson static void 566760bc48eSAndrew Thompson umct_stop_read(struct ucom_softc *ucom) 56702ac6454SAndrew Thompson { 56802ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 56902ac6454SAndrew Thompson 57002ac6454SAndrew Thompson /* stop interrupt endpoint */ 571a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]); 57202ac6454SAndrew Thompson 57302ac6454SAndrew Thompson /* stop read endpoint */ 574a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]); 57502ac6454SAndrew Thompson } 57602ac6454SAndrew Thompson 57702ac6454SAndrew Thompson static void 578760bc48eSAndrew Thompson umct_start_write(struct ucom_softc *ucom) 57902ac6454SAndrew Thompson { 58002ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 58102ac6454SAndrew Thompson 582a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]); 58302ac6454SAndrew Thompson } 58402ac6454SAndrew Thompson 58502ac6454SAndrew Thompson static void 586760bc48eSAndrew Thompson umct_stop_write(struct ucom_softc *ucom) 58702ac6454SAndrew Thompson { 58802ac6454SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 58902ac6454SAndrew Thompson 590a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]); 59102ac6454SAndrew Thompson } 59202ac6454SAndrew Thompson 59302ac6454SAndrew Thompson static void 594585b13f2SAndrew Thompson umct_read_callback(struct usb_xfer *xfer, usb_error_t error) 595585b13f2SAndrew Thompson { 596585b13f2SAndrew Thompson struct umct_softc *sc = usbd_xfer_softc(xfer); 597585b13f2SAndrew Thompson 598585b13f2SAndrew Thompson if (sc->sc_swap_cb) 599585b13f2SAndrew Thompson umct_intr_callback_sub(xfer, error); 600585b13f2SAndrew Thompson else 601585b13f2SAndrew Thompson umct_read_callback_sub(xfer, error); 602585b13f2SAndrew Thompson } 603585b13f2SAndrew Thompson 604585b13f2SAndrew Thompson static void 605585b13f2SAndrew Thompson umct_intr_callback(struct usb_xfer *xfer, usb_error_t error) 606585b13f2SAndrew Thompson { 607585b13f2SAndrew Thompson struct umct_softc *sc = usbd_xfer_softc(xfer); 608585b13f2SAndrew Thompson 609585b13f2SAndrew Thompson if (sc->sc_swap_cb) 610585b13f2SAndrew Thompson umct_read_callback_sub(xfer, error); 611585b13f2SAndrew Thompson else 612585b13f2SAndrew Thompson umct_intr_callback_sub(xfer, error); 613585b13f2SAndrew Thompson } 614585b13f2SAndrew Thompson 615585b13f2SAndrew Thompson static void 616ed6d949aSAndrew Thompson umct_write_callback(struct usb_xfer *xfer, usb_error_t error) 61702ac6454SAndrew Thompson { 618ed6d949aSAndrew Thompson struct umct_softc *sc = usbd_xfer_softc(xfer); 619ed6d949aSAndrew Thompson struct usb_page_cache *pc; 62002ac6454SAndrew Thompson uint32_t actlen; 62102ac6454SAndrew Thompson 62202ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 62302ac6454SAndrew Thompson case USB_ST_SETUP: 62402ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 62502ac6454SAndrew Thompson tr_setup: 626ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 627ed6d949aSAndrew Thompson if (ucom_get_data(&sc->sc_ucom, pc, 0, 62802ac6454SAndrew Thompson sc->sc_obufsize, &actlen)) { 629ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, actlen); 630a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 63102ac6454SAndrew Thompson } 63202ac6454SAndrew Thompson return; 63302ac6454SAndrew Thompson 63402ac6454SAndrew Thompson default: /* Error */ 635ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 63602ac6454SAndrew Thompson /* try to clear stall first */ 637ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 63802ac6454SAndrew Thompson goto tr_setup; 63902ac6454SAndrew Thompson } 64002ac6454SAndrew Thompson return; 64102ac6454SAndrew Thompson } 64202ac6454SAndrew Thompson } 64302ac6454SAndrew Thompson 64402ac6454SAndrew Thompson static void 645585b13f2SAndrew Thompson umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error) 64602ac6454SAndrew Thompson { 647ed6d949aSAndrew Thompson struct umct_softc *sc = usbd_xfer_softc(xfer); 648ed6d949aSAndrew Thompson struct usb_page_cache *pc; 649ed6d949aSAndrew Thompson int actlen; 650ed6d949aSAndrew Thompson 651ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 65202ac6454SAndrew Thompson 65302ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 65402ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 655ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 656ed6d949aSAndrew Thompson ucom_put_data(&sc->sc_ucom, pc, 0, actlen); 65702ac6454SAndrew Thompson 65802ac6454SAndrew Thompson case USB_ST_SETUP: 65902ac6454SAndrew Thompson tr_setup: 660ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 661a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 66202ac6454SAndrew Thompson return; 66302ac6454SAndrew Thompson 66402ac6454SAndrew Thompson default: /* Error */ 665ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 66602ac6454SAndrew Thompson /* try to clear stall first */ 667ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 66802ac6454SAndrew Thompson goto tr_setup; 66902ac6454SAndrew Thompson } 67002ac6454SAndrew Thompson return; 67102ac6454SAndrew Thompson } 67202ac6454SAndrew Thompson } 673655dc9d0SAndrew Thompson 674655dc9d0SAndrew Thompson static void 675655dc9d0SAndrew Thompson umct_poll(struct ucom_softc *ucom) 676655dc9d0SAndrew Thompson { 677655dc9d0SAndrew Thompson struct umct_softc *sc = ucom->sc_parent; 678655dc9d0SAndrew Thompson usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER); 679655dc9d0SAndrew Thompson } 680