[BACK]Return to umcs.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / dev / usb

Annotation of src/sys/dev/usb/umcs.c, Revision 1.15.10.2

1.15.10.2! thorpej     1: /* $NetBSD: umcs.c,v 1.15.10.1 2021/03/22 02:01:02 thorpej Exp $ */
1.1       martin      2: /* $FreeBSD: head/sys/dev/usb/serial/umcs.c 260559 2014-01-12 11:44:28Z hselasky $ */
                      3:
                      4: /*-
                      5:  * Copyright (c) 2010 Lev Serebryakov <lev@FreeBSD.org>.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
                     31:  * This driver supports several multiport USB-to-RS232 serial adapters driven
                     32:  * by MosChip mos7820 and mos7840, bridge chips.
                     33:  * The adapters are sold under many different brand names.
                     34:  *
                     35:  * Datasheets are available at MosChip www site at
                     36:  * http://www.moschip.com.  The datasheets don't contain full
                     37:  * programming information for the chip.
                     38:  *
                     39:  * It is nornal to have only two enabled ports in devices, based on
                     40:  * quad-port mos7840.
                     41:  *
                     42:  */
                     43: #include <sys/cdefs.h>
1.15.10.2! thorpej    44: __KERNEL_RCSID(0, "$NetBSD: umcs.c,v 1.15.10.1 2021/03/22 02:01:02 thorpej Exp $");
1.1       martin     45:
                     46: #include <sys/param.h>
                     47: #include <sys/systm.h>
                     48: #include <sys/atomic.h>
                     49: #include <sys/kernel.h>
                     50: #include <sys/conf.h>
                     51: #include <sys/tty.h>
                     52: #include <sys/device.h>
1.3       martin     53: #include <sys/kmem.h>
1.1       martin     54:
                     55: #include <dev/usb/usb.h>
                     56: #include <dev/usb/usbdi.h>
                     57: #include <dev/usb/usbdi_util.h>
                     58: #include <dev/usb/usbdevs.h>
                     59:
                     60: #include <dev/usb/usbdevs.h>
                     61: #include <dev/usb/ucomvar.h>
                     62:
                     63: #include "umcs.h"
                     64:
                     65: #if 0
                     66: #define        DPRINTF(ARG)    printf ARG
                     67: #else
                     68: #define        DPRINTF(ARG)
                     69: #endif
                     70:
                     71: /*
                     72:  * Two-port devices (both with 7820 chip and 7840 chip configured as two-port)
                     73:  * have ports 0 and 2, with ports 1 and 3 omitted.
                     74:  * So, PHYSICAL port numbers on two-port device will be 0 and 2.
                     75:  *
                     76:  * We use an array of the following struct, indexed by ucom port index,
                     77:  * and include the physical port number in it.
                     78:  */
                     79: struct umcs7840_softc_oneport {
                     80:        device_t sc_port_ucom;          /* ucom subdevice */
                     81:        unsigned int sc_port_phys;      /* physical port number */
                     82:        uint8_t sc_port_lcr;            /* local line control register */
                     83:        uint8_t sc_port_mcr;            /* local modem control register */
                     84: };
                     85:
                     86: struct umcs7840_softc {
                     87:        device_t sc_dev;                /* ourself */
1.14      maxv       88:        enum {
                     89:                UMCS_INIT_NONE,
                     90:                UMCS_INIT_INITED
                     91:        } sc_init_state;
1.9       skrll      92:        struct usbd_interface *sc_iface; /* the usb interface */
                     93:        struct usbd_device *sc_udev;    /* the usb device */
                     94:        struct usbd_pipe *sc_intr_pipe; /* interrupt pipe */
1.1       martin     95:        uint8_t *sc_intr_buf;           /* buffer for interrupt xfer */
1.3       martin     96:        unsigned int sc_intr_buflen;    /* size of buffer */
1.5       martin     97:        struct usb_task sc_change_task; /* async status changes */
1.6       riastrad   98:        volatile uint32_t sc_change_mask;       /* mask of port changes */
1.1       martin     99:        struct umcs7840_softc_oneport sc_ports[UMCS7840_MAX_PORTS];
                    100:                                        /* data for each port */
                    101:        uint8_t sc_numports;            /* number of ports (subunits) */
                    102:        bool sc_init_done;              /* special one time init in open */
                    103:        bool sc_dying;                  /* we have been deactivated */
                    104: };
                    105:
1.9       skrll     106: static int umcs7840_get_reg(struct umcs7840_softc *, uint8_t, uint8_t *);
                    107: static int umcs7840_set_reg(struct umcs7840_softc *, uint8_t, uint8_t);
                    108: static int umcs7840_get_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t *);
                    109: static int umcs7840_set_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t );
                    110: static int umcs7840_calc_baudrate(uint32_t, uint16_t *, uint8_t *);
                    111: static void umcs7840_dtr(struct umcs7840_softc *, int, bool);
                    112: static void umcs7840_rts(struct umcs7840_softc *, int, bool);
                    113: static void umcs7840_break(struct umcs7840_softc *, int, bool );
1.1       martin    114:
                    115: static int umcs7840_match(device_t, cfdata_t, void *);
                    116: static void umcs7840_attach(device_t, device_t, void *);
                    117: static int umcs7840_detach(device_t, int);
1.9       skrll     118: static void umcs7840_intr(struct usbd_xfer *, void *, usbd_status);
1.5       martin    119: static void umcs7840_change_task(void *arg);
1.1       martin    120: static void umcs7840_childdet(device_t, device_t);
                    121:
                    122: static void umcs7840_get_status(void *, int, u_char *, u_char *);
                    123: static void umcs7840_set(void *, int, int, int);
                    124: static int umcs7840_param(void *, int, struct termios *);
1.9       skrll     125: static int umcs7840_port_open(void *, int);
                    126: static void umcs7840_port_close(void *, int);
1.1       martin    127:
1.15      maxv      128: static const struct ucom_methods umcs7840_methods = {
1.2       martin    129:        .ucom_get_status = umcs7840_get_status,
                    130:        .ucom_set = umcs7840_set,
                    131:        .ucom_param = umcs7840_param,
                    132:        .ucom_open = umcs7840_port_open,
                    133:        .ucom_close = umcs7840_port_close,
1.1       martin    134: };
                    135:
                    136: static const struct usb_devno umcs7840_devs[] = {
                    137:        { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7703 },
                    138:        { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7810 },
                    139:        { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7820 },
                    140:        { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7840 },
                    141:        { USB_VENDOR_ATEN,              USB_PRODUCT_ATEN_UC2324 }
                    142: };
                    143: #define umcs7840_lookup(v, p) usb_lookup(umcs7840_devs, v, p)
                    144:
                    145: CFATTACH_DECL2_NEW(umcs, sizeof(struct umcs7840_softc), umcs7840_match,
1.13      mrg       146:     umcs7840_attach, umcs7840_detach, NULL, NULL,
1.1       martin    147:     umcs7840_childdet);
                    148:
                    149: static inline int
                    150: umcs7840_reg_sp(int phyport)
                    151: {
                    152:        KASSERT(phyport >= 0 && phyport < 4);
                    153:        switch (phyport) {
                    154:        default:
                    155:        case 0: return MCS7840_DEV_REG_SP1;
                    156:        case 1: return MCS7840_DEV_REG_SP2;
                    157:        case 2: return MCS7840_DEV_REG_SP3;
                    158:        case 3: return MCS7840_DEV_REG_SP4;
                    159:        }
                    160: }
                    161:
                    162: static inline int
                    163: umcs7840_reg_ctrl(int phyport)
                    164: {
                    165:        KASSERT(phyport >= 0 && phyport < 4);
                    166:        switch (phyport) {
                    167:        default:
                    168:        case 0: return MCS7840_DEV_REG_CONTROL1;
                    169:        case 1: return MCS7840_DEV_REG_CONTROL2;
                    170:        case 2: return MCS7840_DEV_REG_CONTROL3;
                    171:        case 3: return MCS7840_DEV_REG_CONTROL4;
                    172:        }
                    173: }
                    174:
                    175: static int
                    176: umcs7840_match(device_t dev, cfdata_t match, void *aux)
                    177: {
                    178:        struct usb_attach_arg *uaa = aux;
                    179:
1.9       skrll     180:        return umcs7840_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
                    181:                UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
1.1       martin    182: }
                    183:
                    184: static void
1.9       skrll     185: umcs7840_attach(device_t parent, device_t self, void *aux)
1.1       martin    186: {
                    187:        struct umcs7840_softc *sc = device_private(self);
                    188:        struct usb_attach_arg *uaa = aux;
1.9       skrll     189:        struct usbd_device *dev = uaa->uaa_device;
1.1       martin    190:        usb_interface_descriptor_t *id;
                    191:        usb_endpoint_descriptor_t *ed;
                    192:        char *devinfop;
1.9       skrll     193:        struct ucom_attach_args ucaa;
1.1       martin    194:        int error, i, intr_addr;
                    195:        uint8_t data;
                    196:
                    197:        sc->sc_dev = self;
1.9       skrll     198:        sc->sc_udev = uaa->uaa_device;
1.13      mrg       199:        sc->sc_dying = false;
1.14      maxv      200:        sc->sc_init_state = UMCS_INIT_NONE;
1.1       martin    201:
                    202:        if (usbd_set_config_index(sc->sc_udev, MCS7840_CONFIG_INDEX, 1) != 0) {
                    203:                aprint_error(": could not set configuration no\n");
1.13      mrg       204:                sc->sc_dying = true;
1.1       martin    205:                return;
                    206:        }
                    207:
                    208:        /* get the first interface handle */
                    209:        error = usbd_device2interface_handle(sc->sc_udev, MCS7840_IFACE_INDEX,
                    210:            &sc->sc_iface);
                    211:        if (error != 0) {
                    212:                aprint_error(": could not get interface handle\n");
1.13      mrg       213:                sc->sc_dying = true;
1.1       martin    214:                return;
                    215:        }
                    216:
                    217:        /*
                    218:         * Get number of ports
                    219:         * Documentation (full datasheet) says, that number of ports is
                    220:         * set as MCS7840_DEV_MODE_SELECT24S bit in MODE R/Only
                    221:         * register. But vendor driver uses these undocumented
                    222:         * register & bit.
                    223:         *
                    224:         * Experiments show, that MODE register can have `0'
                    225:         * (4 ports) bit on 2-port device, so use vendor driver's way.
                    226:         *
                    227:         * Also, see notes in header file for these constants.
                    228:         */
                    229:        umcs7840_get_reg(sc, MCS7840_DEV_REG_GPIO, &data);
                    230:        if (data & MCS7840_DEV_GPIO_4PORTS) {
                    231:                sc->sc_numports = 4;
                    232:                /* physical port no are : 0, 1, 2, 3 */
                    233:        } else {
1.9       skrll     234:                if (uaa->uaa_product == USB_PRODUCT_MOSCHIP_MCS7810)
1.1       martin    235:                        sc->sc_numports = 1;
                    236:                else {
                    237:                        sc->sc_numports = 2;
                    238:                        /* physical port no are : 0 and 2 */
                    239:                }
                    240:        }
                    241:        devinfop = usbd_devinfo_alloc(dev, 0);
                    242:        aprint_normal(": %s\n", devinfop);
                    243:        usbd_devinfo_free(devinfop);
                    244:        aprint_verbose_dev(self, "found %d active ports\n", sc->sc_numports);
                    245:
                    246:        if (!umcs7840_get_reg(sc, MCS7840_DEV_REG_MODE, &data)) {
                    247:                aprint_verbose_dev(self, "On-die confguration: RST: active %s, "
                    248:                    "HRD: %s, PLL: %s, POR: %s, Ports: %s, EEPROM write %s, "
                    249:                    "IrDA is %savailable\n",
                    250:                    (data & MCS7840_DEV_MODE_RESET) ? "low" : "high",
                    251:                    (data & MCS7840_DEV_MODE_SER_PRSNT) ? "yes" : "no",
                    252:                    (data & MCS7840_DEV_MODE_PLLBYPASS) ? "bypassed" : "avail",
                    253:                    (data & MCS7840_DEV_MODE_PORBYPASS) ? "bypassed" : "avail",
                    254:                    (data & MCS7840_DEV_MODE_SELECT24S) ? "2" : "4",
                    255:                    (data & MCS7840_DEV_MODE_EEPROMWR) ? "enabled" : "disabled",
                    256:                    (data & MCS7840_DEV_MODE_IRDA) ? "" : "not ");
                    257:        }
                    258:
                    259:        /*
                    260:         * Set up the interrupt pipe
                    261:         */
                    262:        id = usbd_get_interface_descriptor(sc->sc_iface);
                    263:        intr_addr = -1;
                    264:        for (i = 0 ; i < id->bNumEndpoints ; i++) {
                    265:                ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
                    266:                if (ed == NULL) continue;
                    267:                if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN
                    268:                    || UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT)
                    269:                        continue;
1.3       martin    270:                sc->sc_intr_buflen = UGETW(ed->wMaxPacketSize);
1.1       martin    271:                intr_addr = ed->bEndpointAddress;
                    272:                break;
                    273:        }
                    274:        if (intr_addr < 0) {
                    275:                aprint_error_dev(self, "interrupt pipe not found\n");
1.13      mrg       276:                sc->sc_dying = true;
1.1       martin    277:                return;
                    278:        }
1.3       martin    279:        sc->sc_intr_buf = kmem_alloc(sc->sc_intr_buflen, KM_SLEEP);
1.1       martin    280:
                    281:        error = usbd_open_pipe_intr(sc->sc_iface, intr_addr,
                    282:                    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
1.3       martin    283:                    sc->sc_intr_buflen, umcs7840_intr, 100);
1.1       martin    284:        if (error) {
                    285:                aprint_error_dev(self, "cannot open interrupt pipe "
                    286:                    "(addr %d)\n", intr_addr);
1.13      mrg       287:                sc->sc_dying = true;
1.1       martin    288:                return;
                    289:        }
                    290:
1.5       martin    291:        usb_init_task(&sc->sc_change_task, umcs7840_change_task, sc,
                    292:            USB_TASKQ_MPSAFE);
1.1       martin    293:
1.10      msaitoh   294:        usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
1.1       martin    295:
1.14      maxv      296:        sc->sc_init_state = UMCS_INIT_INITED;
                    297:
1.9       skrll     298:        memset(&ucaa, 0, sizeof(ucaa));
                    299:        ucaa.ucaa_ibufsize = 256;
                    300:        ucaa.ucaa_obufsize = 256;
                    301:        ucaa.ucaa_ibufsizepad = 256;
                    302:        ucaa.ucaa_opkthdrlen = 0;
                    303:        ucaa.ucaa_device = sc->sc_udev;
                    304:        ucaa.ucaa_iface = sc->sc_iface;
                    305:        ucaa.ucaa_methods = &umcs7840_methods;
                    306:        ucaa.ucaa_arg = sc;
1.1       martin    307:
                    308:        for (i = 0; i < sc->sc_numports; i++) {
1.9       skrll     309:                ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
1.1       martin    310:
                    311:                /*
                    312:                 * On four port cards, endpoints are 0/1 for first,
                    313:                 * 2/3 for second, ...
                    314:                 * On two port cards, they are 0/1 for first, 4/5 for second.
                    315:                 * On single port, just 0/1 will be used.
                    316:                 */
                    317:                int phyport = i * (sc->sc_numports == 2 ? 2 : 1);
                    318:
                    319:                ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
                    320:                        phyport*2);
                    321:                if (ed == NULL) {
                    322:                        aprint_error_dev(self,
                    323:                            "no bulk in endpoint found for %d\n", i);
1.13      mrg       324:                        sc->sc_dying = true;
1.1       martin    325:                        return;
                    326:                }
1.9       skrll     327:                ucaa.ucaa_bulkin = ed->bEndpointAddress;
1.1       martin    328:
                    329:                ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
                    330:                        phyport*2 + 1);
                    331:                if (ed == NULL) {
                    332:                        aprint_error_dev(self,
                    333:                            "no bulk out endpoint found for %d\n", i);
                    334:                        return;
                    335:                }
1.9       skrll     336:                ucaa.ucaa_bulkout = ed->bEndpointAddress;
                    337:                ucaa.ucaa_portno = i;
1.1       martin    338:                DPRINTF(("port %d physical port %d bulk-in %d bulk-out %d\n",
1.9       skrll     339:                    i, phyport, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout));
1.1       martin    340:
                    341:                sc->sc_ports[i].sc_port_phys = phyport;
                    342:                sc->sc_ports[i].sc_port_ucom =
1.15.10.1  thorpej   343:                    config_found(self, &ucaa, ucomprint,
                    344:                                 CFARG_SUBMATCH, ucomsubmatch,
                    345:                                 CFARG_EOL);
1.1       martin    346:        }
                    347: }
                    348:
                    349: static int
                    350: umcs7840_get_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t *data)
                    351: {
                    352:        usb_device_request_t req;
                    353:        int err;
                    354:
                    355:        req.bmRequestType = UT_READ_VENDOR_DEVICE;
                    356:        req.bRequest = MCS7840_RDREQ;
                    357:        USETW(req.wValue, 0);
                    358:        USETW(req.wIndex, reg);
                    359:        USETW(req.wLength, UMCS7840_READ_LENGTH);
                    360:
                    361:        err = usbd_do_request(sc->sc_udev, &req, data);
                    362:        if (err)
                    363:                aprint_normal_dev(sc->sc_dev,
                    364:                    "Reading register %d failed: %s\n", reg, usbd_errstr(err));
                    365:        return err;
                    366: }
                    367:
                    368: static int
                    369: umcs7840_set_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t data)
                    370: {
                    371:        usb_device_request_t req;
                    372:        int err;
                    373:
                    374:        req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
                    375:        req.bRequest = MCS7840_WRREQ;
                    376:        USETW(req.wValue, data);
                    377:        USETW(req.wIndex, reg);
                    378:        USETW(req.wLength, 0);
                    379:
                    380:        err = usbd_do_request(sc->sc_udev, &req, 0);
                    381:        if (err)
                    382:                aprint_normal_dev(sc->sc_dev, "Writing register %d failed: %s\n", reg, usbd_errstr(err));
                    383:
                    384:        return err;
                    385: }
                    386:
                    387: static int
                    388: umcs7840_get_UART_reg(struct umcs7840_softc *sc, uint8_t portno,
                    389:        uint8_t reg, uint8_t *data)
                    390: {
                    391:        usb_device_request_t req;
                    392:        uint16_t wVal;
                    393:        int err;
                    394:
                    395:        /* portno is port number */
                    396:        wVal = ((uint16_t)(portno + 1)) << 8;
                    397:
                    398:        req.bmRequestType = UT_READ_VENDOR_DEVICE;
                    399:        req.bRequest = MCS7840_RDREQ;
                    400:        USETW(req.wValue, wVal);
                    401:        USETW(req.wIndex, reg);
                    402:        USETW(req.wLength, UMCS7840_READ_LENGTH);
                    403:
                    404:        err = usbd_do_request(sc->sc_udev, &req, data);
                    405:        if (err)
                    406:                aprint_normal_dev(sc->sc_dev, "Reading UART %d register %d failed: %s\n", portno, reg, usbd_errstr(err));
                    407:        return err;
                    408: }
                    409:
                    410: static int
                    411: umcs7840_set_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t data)
                    412: {
                    413:        usb_device_request_t req;
                    414:        int err;
                    415:        uint16_t wVal;
                    416:
                    417:        /* portno is the physical port number */
                    418:        wVal = ((uint16_t)(portno + 1)) << 8 | data;
                    419:
                    420:        req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
                    421:        req.bRequest = MCS7840_WRREQ;
                    422:        USETW(req.wValue, wVal);
                    423:        USETW(req.wIndex, reg);
                    424:        USETW(req.wLength, 0);
                    425:
                    426:        err = usbd_do_request(sc->sc_udev, &req, NULL);
                    427:        if (err)
1.3       martin    428:                aprint_error_dev(sc->sc_dev,
                    429:                    "Writing UART %d register %d failed: %s\n",
                    430:                    portno, reg, usbd_errstr(err));
1.1       martin    431:        return err;
                    432: }
                    433:
                    434: static int
1.8       martin    435: umcs7840_set_baudrate(struct umcs7840_softc *sc, uint8_t portno,
                    436:        uint32_t rate)
1.1       martin    437: {
                    438:        int err;
                    439:        uint16_t divisor;
                    440:        uint8_t clk;
                    441:        uint8_t data;
                    442:        uint8_t physport = sc->sc_ports[portno].sc_port_phys;
                    443:        int spreg = umcs7840_reg_sp(physport);
                    444:
                    445:        if (umcs7840_calc_baudrate(rate, &divisor, &clk)) {
                    446:                DPRINTF(("Port %d bad speed: %d\n", portno, rate));
1.9       skrll     447:                return -1;
1.1       martin    448:        }
                    449:        if (divisor == 0 || (clk & MCS7840_DEV_SPx_CLOCK_MASK) != clk) {
1.8       martin    450:                DPRINTF(("Port %d bad speed calculation: %d\n", portno,
                    451:                    rate));
1.9       skrll     452:                return -1;
1.1       martin    453:        }
                    454:        DPRINTF(("Port %d set speed: %d (%02x / %d)\n", portno, rate, clk, divisor));
                    455:
                    456:        /* Set clock source for standard BAUD frequences */
                    457:        err = umcs7840_get_reg(sc, spreg, &data);
                    458:        if (err)
                    459:                return err;
                    460:        data &= MCS7840_DEV_SPx_CLOCK_MASK;
                    461:        data |= clk;
                    462:        err = umcs7840_set_reg(sc, spreg, data);
                    463:        if (err)
                    464:                return err;
                    465:
                    466:        /* Set divider */
                    467:        sc->sc_ports[portno].sc_port_lcr |= MCS7840_UART_LCR_DIVISORS;
                    468:        err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
                    469:        if (err)
                    470:                return err;
                    471:
                    472:        err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLL, (uint8_t)(divisor & 0xff));
                    473:        if (err)
                    474:                return err;
                    475:        err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLM, (uint8_t)((divisor >> 8) & 0xff));
                    476:        if (err)
                    477:                return err;
                    478:
                    479:        /* Turn off access to DLL/DLM registers of UART */
                    480:        sc->sc_ports[portno].sc_port_lcr &= ~MCS7840_UART_LCR_DIVISORS;
                    481:        err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
                    482:        if (err)
                    483:                return err;
1.9       skrll     484:        return 0;
1.1       martin    485: }
                    486:
                    487: static int
                    488: umcs7840_calc_baudrate(uint32_t rate, uint16_t *divisor, uint8_t *clk)
                    489: {
                    490:        /* Maximum speeds for standard frequences, when PLL is not used */
                    491:        static const uint32_t umcs7840_baudrate_divisors[] =
                    492:            {0, 115200, 230400, 403200, 460800, 806400, 921600,
                    493:             1572864, 3145728,};
                    494:        static const uint8_t umcs7840_baudrate_divisors_len =
                    495:             __arraycount(umcs7840_baudrate_divisors);
                    496:        uint8_t i = 0;
                    497:
                    498:        if (rate > umcs7840_baudrate_divisors[umcs7840_baudrate_divisors_len - 1])
1.9       skrll     499:                return -1;
1.1       martin    500:
                    501:        for (i = 0; i < umcs7840_baudrate_divisors_len - 1
                    502:             && !(rate > umcs7840_baudrate_divisors[i]
                    503:             && rate <= umcs7840_baudrate_divisors[i + 1]); ++i);
                    504:        *divisor = umcs7840_baudrate_divisors[i + 1] / rate;
                    505:        /* 0x00 .. 0x70 */
                    506:        *clk = i << MCS7840_DEV_SPx_CLOCK_SHIFT;
1.9       skrll     507:        return 0;
1.1       martin    508: }
                    509:
1.9       skrll     510: static int
1.1       martin    511: umcs7840_detach(device_t self, int flags)
                    512: {
                    513:        struct umcs7840_softc *sc = device_private(self);
                    514:        int rv = 0, i;
                    515:
1.4       martin    516:        sc->sc_dying = true;
1.3       martin    517:
1.1       martin    518:        /* close interrupt pipe */
                    519:        if (sc->sc_intr_pipe != NULL) {
1.13      mrg       520:                usbd_abort_pipe(sc->sc_intr_pipe);
                    521:                usbd_close_pipe(sc->sc_intr_pipe);
                    522:                sc->sc_intr_pipe = NULL;
                    523:        }
                    524:        if (sc->sc_intr_buf != NULL) {
1.3       martin    525:                kmem_free(sc->sc_intr_buf, sc->sc_intr_buflen);
1.13      mrg       526:                sc->sc_intr_buf = NULL;
1.1       martin    527:        }
1.14      maxv      528:
                    529:        if (sc->sc_init_state < UMCS_INIT_INITED)
                    530:                return 0;
                    531:
1.12      skrll     532:        usb_rem_task_wait(sc->sc_udev, &sc->sc_change_task, USB_TASKQ_DRIVER,
                    533:            NULL);
1.1       martin    534:
1.4       martin    535:        /* detach children */
                    536:        for (i = 0; i < sc->sc_numports; i++) {
                    537:                if (sc->sc_ports[i].sc_port_ucom) {
1.13      mrg       538:                        rv |= config_detach(sc->sc_ports[i].sc_port_ucom,
1.4       martin    539:                            flags);
1.13      mrg       540:                        sc->sc_ports[i].sc_port_ucom = NULL;
1.4       martin    541:                }
                    542:        }
                    543:
1.10      msaitoh   544:        usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
1.1       martin    545:
                    546:        return rv;
                    547: }
                    548:
                    549: static void
                    550: umcs7840_childdet(device_t self, device_t child)
                    551: {
                    552:        struct umcs7840_softc *sc = device_private(self);
                    553:        int i;
                    554:
                    555:        for (i = 0; i < sc->sc_numports; i++) {
                    556:                if (child == sc->sc_ports[i].sc_port_ucom) {
                    557:                        sc->sc_ports[i].sc_port_ucom = NULL;
                    558:                        return;
                    559:                }
                    560:        }
                    561: }
                    562:
                    563: static void
                    564: umcs7840_get_status(void *self, int portno, u_char *lsr, u_char *msr)
                    565: {
                    566:        struct umcs7840_softc *sc = self;
                    567:        uint8_t pn = sc->sc_ports[portno].sc_port_phys;
                    568:        uint8_t hw_lsr = 0;     /* local line status register */
                    569:        uint8_t hw_msr = 0;     /* local modem status register */
                    570:
1.4       martin    571:        if (sc->sc_dying)
                    572:                return;
                    573:
1.1       martin    574:        /* Read LSR & MSR */
                    575:        umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LSR, &hw_lsr);
                    576:        umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_MSR, &hw_msr);
                    577:
                    578:        *lsr = hw_lsr;
                    579:        *msr = hw_msr;
                    580: }
                    581:
                    582: static void
                    583: umcs7840_set(void *self, int portno, int reg, int onoff)
                    584: {
                    585:        struct umcs7840_softc *sc = self;
                    586:
                    587:        if (sc->sc_dying)
                    588:                return;
                    589:
                    590:        switch (reg) {
                    591:        case UCOM_SET_DTR:
1.8       martin    592:                umcs7840_dtr(sc, portno, onoff);
1.1       martin    593:                break;
                    594:        case UCOM_SET_RTS:
1.8       martin    595:                umcs7840_rts(sc, portno, onoff);
1.1       martin    596:                break;
                    597:        case UCOM_SET_BREAK:
1.8       martin    598:                umcs7840_break(sc, portno, onoff);
1.1       martin    599:                break;
                    600:        default:
                    601:                break;
                    602:        }
                    603: }
                    604:
                    605: static int
                    606: umcs7840_param(void *self, int portno, struct termios *t)
                    607: {
                    608:        struct umcs7840_softc *sc = self;
                    609:        int pn = sc->sc_ports[portno].sc_port_phys;
                    610:        uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
                    611:        uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
                    612:
1.13      mrg       613:        if (sc->sc_dying)
                    614:                return EIO;
                    615:
1.1       martin    616:        if (t->c_cflag & CSTOPB) {
                    617:                lcr |= MCS7840_UART_LCR_STOPB2;
                    618:        } else {
                    619:                lcr |= MCS7840_UART_LCR_STOPB1;
                    620:        }
                    621:
                    622:        lcr &= ~MCS7840_UART_LCR_PARITYMASK;
                    623:        if (t->c_cflag & PARENB) {
                    624:                lcr |= MCS7840_UART_LCR_PARITYON;
                    625:                if (t->c_cflag & PARODD) {
                    626:                        lcr = MCS7840_UART_LCR_PARITYODD;
                    627:                } else {
                    628:                        lcr = MCS7840_UART_LCR_PARITYEVEN;
                    629:                }
                    630:        } else {
                    631:                lcr &= ~MCS7840_UART_LCR_PARITYON;
                    632:        }
                    633:
                    634:        lcr &= ~MCS7840_UART_LCR_DATALENMASK;
                    635:        switch (t->c_cflag & CSIZE) {
                    636:        case CS5:
                    637:                lcr |= MCS7840_UART_LCR_DATALEN5;
                    638:                break;
                    639:        case CS6:
                    640:                lcr |= MCS7840_UART_LCR_DATALEN6;
                    641:                break;
                    642:        case CS7:
                    643:                lcr |= MCS7840_UART_LCR_DATALEN7;
                    644:                break;
                    645:        case CS8:
                    646:                lcr |= MCS7840_UART_LCR_DATALEN8;
                    647:                break;
                    648:        }
                    649:
                    650:        if (t->c_cflag & CRTSCTS)
                    651:                mcr |= MCS7840_UART_MCR_CTSRTS;
                    652:        else
                    653:                mcr &= ~MCS7840_UART_MCR_CTSRTS;
                    654:
                    655:        if (t->c_cflag & CLOCAL)
                    656:                mcr &= ~MCS7840_UART_MCR_DTRDSR;
                    657:        else
                    658:                mcr |= MCS7840_UART_MCR_DTRDSR;
                    659:
                    660:        sc->sc_ports[portno].sc_port_lcr = lcr;
                    661:        umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
                    662:            sc->sc_ports[pn].sc_port_lcr);
                    663:
                    664:        sc->sc_ports[portno].sc_port_mcr = mcr;
                    665:        umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
                    666:            sc->sc_ports[pn].sc_port_mcr);
                    667:
1.8       martin    668:        if (umcs7840_set_baudrate(sc, portno, t->c_ospeed))
1.1       martin    669:                return EIO;
                    670:
                    671:        return 0;
                    672: }
                    673:
                    674: static void
                    675: umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff)
                    676: {
                    677:        int pn = sc->sc_ports[portno].sc_port_phys;
                    678:        uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
                    679:
                    680:        if (onoff)
                    681:                mcr |= MCS7840_UART_MCR_DTR;
                    682:        else
                    683:                mcr &= ~MCS7840_UART_MCR_DTR;
                    684:
                    685:        sc->sc_ports[portno].sc_port_mcr = mcr;
                    686:        umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
                    687:            sc->sc_ports[pn].sc_port_mcr);
                    688: }
                    689:
                    690: static void
                    691: umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff)
                    692: {
                    693:        int pn = sc->sc_ports[portno].sc_port_phys;
                    694:        uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
                    695:
                    696:        if (onoff)
                    697:                mcr |= MCS7840_UART_MCR_RTS;
                    698:        else
                    699:                mcr &= ~MCS7840_UART_MCR_RTS;
                    700:
                    701:        sc->sc_ports[portno].sc_port_mcr = mcr;
                    702:        umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
                    703:            sc->sc_ports[pn].sc_port_mcr);
                    704: }
                    705:
                    706: static void
                    707: umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff)
                    708: {
                    709:        int pn = sc->sc_ports[portno].sc_port_phys;
                    710:        uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
                    711:
                    712:        if (onoff)
                    713:                lcr |= MCS7840_UART_LCR_BREAK;
                    714:        else
                    715:                lcr &= ~MCS7840_UART_LCR_BREAK;
                    716:
                    717:        sc->sc_ports[portno].sc_port_lcr = lcr;
                    718:        umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
                    719:            sc->sc_ports[pn].sc_port_lcr);
                    720: }
                    721:
                    722: static int
                    723: umcs7840_port_open(void *self, int portno)
                    724: {
                    725:        struct umcs7840_softc *sc = self;
                    726:        int pn = sc->sc_ports[portno].sc_port_phys;
                    727:        int spreg = umcs7840_reg_sp(pn);
                    728:        int ctrlreg = umcs7840_reg_ctrl(pn);
                    729:        uint8_t data;
                    730:
                    731:        if (sc->sc_dying)
                    732:                return EIO;
                    733:
                    734:        /* If it very first open, finish global configuration */
                    735:        if (!sc->sc_init_done) {
                    736:                if (umcs7840_get_reg(sc, MCS7840_DEV_REG_CONTROL1, &data))
                    737:                        return EIO;
                    738:                data |= MCS7840_DEV_CONTROL1_DRIVER_DONE;
                    739:                if (umcs7840_set_reg(sc, MCS7840_DEV_REG_CONTROL1, data))
                    740:                        return EIO;
                    741:                sc->sc_init_done = 1;
                    742:        }
                    743:
                    744:        /* Toggle reset bit on-off */
                    745:        if (umcs7840_get_reg(sc, spreg, &data))
                    746:                return EIO;
                    747:        data |= MCS7840_DEV_SPx_UART_RESET;
                    748:        if (umcs7840_set_reg(sc, spreg, data))
                    749:                return EIO;
                    750:        data &= ~MCS7840_DEV_SPx_UART_RESET;
                    751:        if (umcs7840_set_reg(sc, spreg, data))
                    752:                return EIO;
                    753:
                    754:        /* Set RS-232 mode */
                    755:        if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_SCRATCHPAD,
                    756:            MCS7840_UART_SCRATCHPAD_RS232))
                    757:                return EIO;
                    758:
                    759:        /* Disable RX on time of initialization */
                    760:        if (umcs7840_get_reg(sc, ctrlreg, &data))
                    761:                return EIO;
                    762:        data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
                    763:        if (umcs7840_set_reg(sc, ctrlreg, data))
                    764:                return EIO;
                    765:
                    766:        /* Disable all interrupts */
                    767:        if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0))
                    768:                return EIO;
                    769:
                    770:        /* Reset FIFO -- documented */
                    771:        if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR, 0))
                    772:                return EIO;
                    773:        if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR,
                    774:            MCS7840_UART_FCR_ENABLE | MCS7840_UART_FCR_FLUSHRHR |
                    775:            MCS7840_UART_FCR_FLUSHTHR | MCS7840_UART_FCR_RTL_1_14))
                    776:                return EIO;
                    777:
                    778:        /* Set 8 bit, no parity, 1 stop bit -- documented */
                    779:        sc->sc_ports[pn].sc_port_lcr =
                    780:            MCS7840_UART_LCR_DATALEN8 | MCS7840_UART_LCR_STOPB1;
                    781:        if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
                    782:            sc->sc_ports[pn].sc_port_lcr))
                    783:                return EIO;
                    784:
                    785:        /*
                    786:         * Enable DTR/RTS on modem control, enable modem interrupts --
                    787:         * documented
                    788:         */
                    789:        sc->sc_ports[pn].sc_port_mcr = MCS7840_UART_MCR_DTR
                    790:            | MCS7840_UART_MCR_RTS | MCS7840_UART_MCR_IE;
                    791:        if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
                    792:            sc->sc_ports[pn].sc_port_mcr))
                    793:                return EIO;
                    794:
                    795:        /* Clearing Bulkin and Bulkout FIFO */
                    796:        if (umcs7840_get_reg(sc, spreg, &data))
                    797:                return EIO;
                    798:        data |= MCS7840_DEV_SPx_RESET_OUT_FIFO | MCS7840_DEV_SPx_RESET_IN_FIFO;
                    799:        if (umcs7840_set_reg(sc, spreg, data))
                    800:                return EIO;
                    801:        data &= ~(MCS7840_DEV_SPx_RESET_OUT_FIFO
                    802:            | MCS7840_DEV_SPx_RESET_IN_FIFO);
                    803:        if (umcs7840_set_reg(sc, spreg, data))
                    804:                return EIO;
                    805:
                    806:        /* Set speed 9600 */
1.8       martin    807:        if (umcs7840_set_baudrate(sc, portno, 9600))
1.1       martin    808:                return EIO;
                    809:
                    810:
                    811:        /* Finally enable all interrupts -- documented */
                    812:        /*
                    813:         * Copied from vendor driver, I don't know why we should read LCR
                    814:         * here
                    815:         */
                    816:        if (umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
                    817:            &sc->sc_ports[pn].sc_port_lcr))
                    818:                return EIO;
                    819:        if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER,
                    820:            MCS7840_UART_IER_RXSTAT | MCS7840_UART_IER_MODEM))
                    821:                return EIO;
                    822:
                    823:        /* Enable RX */
                    824:        if (umcs7840_get_reg(sc, ctrlreg, &data))
                    825:                return EIO;
                    826:        data &= ~MCS7840_DEV_CONTROLx_RX_DISABLE;
                    827:        if (umcs7840_set_reg(sc, ctrlreg, data))
                    828:                return EIO;
                    829:        return 0;
                    830: }
                    831:
                    832: static void
                    833: umcs7840_port_close(void *self, int portno)
                    834: {
                    835:        struct umcs7840_softc *sc = self;
                    836:        int pn = sc->sc_ports[portno].sc_port_phys;
                    837:        int ctrlreg = umcs7840_reg_ctrl(pn);
                    838:        uint8_t data;
                    839:
1.4       martin    840:        if (sc->sc_dying)
                    841:                return;
                    842:
1.1       martin    843:        umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR, 0);
                    844:        umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0);
                    845:
                    846:        /* Disable RX */
                    847:        if (umcs7840_get_reg(sc, ctrlreg, &data))
                    848:                return;
                    849:        data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
                    850:        if (umcs7840_set_reg(sc, ctrlreg, data))
                    851:                return;
                    852: }
                    853:
                    854: static void
1.9       skrll     855: umcs7840_intr(struct usbd_xfer *xfer, void *priv,
1.1       martin    856:     usbd_status status)
                    857: {
                    858:        struct umcs7840_softc *sc = priv;
                    859:        u_char *buf = sc->sc_intr_buf;
                    860:        int actlen;
1.5       martin    861:        int subunit;
1.1       martin    862:
1.13      mrg       863:        if (sc->sc_dying)
                    864:                return;
                    865:
1.4       martin    866:        if (status == USBD_NOT_STARTED || status == USBD_CANCELLED
                    867:            || status == USBD_IOERROR)
1.1       martin    868:                return;
                    869:
                    870:        if (status != USBD_NORMAL_COMPLETION) {
                    871:                aprint_error_dev(sc->sc_dev,
                    872:                    "umcs7840_intr: abnormal status: %s\n",
                    873:                    usbd_errstr(status));
                    874:                usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
                    875:                return;
                    876:        }
                    877:
                    878:        usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
                    879:        if (actlen == 5 || actlen == 13) {
1.6       riastrad  880:                uint32_t change_mask = 0;
1.1       martin    881:                /* Check status of all ports */
                    882:                for (subunit = 0; subunit < sc->sc_numports; subunit++) {
                    883:                        uint8_t pn = sc->sc_ports[subunit].sc_port_phys;
                    884:                        if (buf[pn] & MCS7840_UART_ISR_NOPENDING)
                    885:                                continue;
                    886:                        DPRINTF(("Port %d has pending interrupt: %02x "
                    887:                            "(FIFO: %02x)\n", pn,
                    888:                            buf[pn] & MCS7840_UART_ISR_INTMASK,
                    889:                            buf[pn] & (~MCS7840_UART_ISR_INTMASK)));
                    890:                        switch (buf[pn] & MCS7840_UART_ISR_INTMASK) {
                    891:                        case MCS7840_UART_ISR_RXERR:
                    892:                        case MCS7840_UART_ISR_RXHASDATA:
                    893:                        case MCS7840_UART_ISR_RXTIMEOUT:
                    894:                        case MCS7840_UART_ISR_MSCHANGE:
1.6       riastrad  895:                                change_mask |= (1U << subunit);
1.1       martin    896:                                break;
                    897:                        default:
                    898:                                /* Do nothing */
                    899:                                break;
                    900:                        }
                    901:                }
1.5       martin    902:
1.6       riastrad  903:                if (change_mask != 0) {
                    904:                        atomic_or_32(&sc->sc_change_mask, change_mask);
                    905:                        usb_add_task(sc->sc_udev, &sc->sc_change_task,
                    906:                            USB_TASKQ_DRIVER);
                    907:                }
1.1       martin    908:        } else {
1.3       martin    909:                aprint_error_dev(sc->sc_dev,
                    910:                   "Invalid interrupt data length %d", actlen);
1.1       martin    911:        }
                    912: }
                    913:
                    914: static void
1.5       martin    915: umcs7840_change_task(void *arg)
1.1       martin    916: {
                    917:        struct umcs7840_softc *sc = arg;
1.6       riastrad  918:        uint32_t change_mask;
1.1       martin    919:        int i;
                    920:
1.6       riastrad  921:        change_mask = atomic_swap_32(&sc->sc_change_mask, 0);
1.1       martin    922:        for (i = 0; i < sc->sc_numports; i++) {
1.6       riastrad  923:                if (ISSET(change_mask, (1U << i)))
1.1       martin    924:                        ucom_status_change(device_private(
                    925:                            sc->sc_ports[i].sc_port_ucom));
                    926:        }
                    927: }

CVSweb <webmaster@jp.NetBSD.org>