Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/usb Add umcs(4) - a driver for moschip 78{1, 2, 4}0 ba...



details:   https://anonhg.NetBSD.org/src/rev/ac9d07531f41
branches:  trunk
changeset: 327754:ac9d07531f41
user:      martin <martin%NetBSD.org@localhost>
date:      Sun Mar 16 09:34:45 2014 +0000

description:
Add umcs(4) - a driver for moschip 78{1,2,4}0 based multiport serial
adapters. Ported from FreeBSD.

diffstat:

 sys/dev/usb/files.usb         |    7 +-
 sys/dev/usb/umcs.c            |  944 ++++++++++++++++++++++++++++++++++++++++++
 sys/dev/usb/umcs.h            |  648 ++++++++++++++++++++++++++++
 sys/dev/usb/usbdevices.config |   10 +-
 4 files changed, 1605 insertions(+), 4 deletions(-)

diffs (truncated from 1645 to 300 lines):

diff -r 2d47474854f0 -r ac9d07531f41 sys/dev/usb/files.usb
--- a/sys/dev/usb/files.usb     Sun Mar 16 09:28:43 2014 +0000
+++ b/sys/dev/usb/files.usb     Sun Mar 16 09:34:45 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.usb,v 1.130 2013/10/26 09:16:20 nonaka Exp $
+#      $NetBSD: files.usb,v 1.131 2014/03/16 09:34:45 martin Exp $
 #
 # Config file and device description for machine-independent USB code.
 # Included by ports that need it.  Ports that use it must provide
@@ -327,6 +327,11 @@
 attach moscom at usbdevif
 file   dev/usb/moscom.c                moscom
 
+# andother driver for the same chip
+device umcs: ucombus
+attach umcs at usbdevif
+file   dev/usb/umcs.c                  umcs
+
 # SUNTAC Slipper U VS-10U driver
 device uvscom: ucombus
 attach uvscom at usbdevif
diff -r 2d47474854f0 -r ac9d07531f41 sys/dev/usb/umcs.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/usb/umcs.c        Sun Mar 16 09:34:45 2014 +0000
@@ -0,0 +1,944 @@
+/* $NetBSD: umcs.c,v 1.1 2014/03/16 09:34:45 martin Exp $ */
+/* $FreeBSD: head/sys/dev/usb/serial/umcs.c 260559 2014-01-12 11:44:28Z hselasky $ */
+
+/*-
+ * Copyright (c) 2010 Lev Serebryakov <lev%FreeBSD.org@localhost>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * This driver supports several multiport USB-to-RS232 serial adapters driven
+ * by MosChip mos7820 and mos7840, bridge chips.
+ * The adapters are sold under many different brand names.
+ *
+ * Datasheets are available at MosChip www site at
+ * http://www.moschip.com.  The datasheets don't contain full
+ * programming information for the chip.
+ *
+ * It is nornal to have only two enabled ports in devices, based on
+ * quad-port mos7840.
+ *
+ */
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: umcs.c,v 1.1 2014/03/16 09:34:45 martin Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/atomic.h>
+#include <sys/kernel.h>
+#include <sys/conf.h>
+#include <sys/tty.h>
+#include <sys/device.h>
+#include <sys/malloc.h>
+#include <sys/workqueue.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+
+#include <dev/usb/usbdevs.h>
+#include <dev/usb/ucomvar.h>
+
+#include "umcs.h"
+
+#if 0
+#define        DPRINTF(ARG)    printf ARG
+#else
+#define        DPRINTF(ARG)
+#endif
+
+/*
+ * Two-port devices (both with 7820 chip and 7840 chip configured as two-port)
+ * have ports 0 and 2, with ports 1 and 3 omitted.
+ * So, PHYSICAL port numbers on two-port device will be 0 and 2.
+ *
+ * We use an array of the following struct, indexed by ucom port index,
+ * and include the physical port number in it.
+ */
+struct umcs7840_softc_oneport {
+       device_t sc_port_ucom;          /* ucom subdevice */
+       uint32_t sc_port_changed;       /* call ucom_status_change() */
+       unsigned int sc_port_phys;      /* physical port number */
+       uint8_t sc_port_lcr;            /* local line control register */
+       uint8_t sc_port_mcr;            /* local modem control register */
+};
+
+struct umcs7840_softc {
+       device_t sc_dev;                /* ourself */
+       usbd_interface_handle sc_iface; /* the usb interface */
+       usbd_device_handle sc_udev;     /* the usb device */
+       usbd_pipe_handle sc_intr_pipe;  /* interrupt pipe */
+       usbd_xfer_handle sc_intr_xfer;  /* and preallocated xfer */
+       uint8_t *sc_intr_buf;           /* buffer for interrupt xfer */
+       struct workqueue *sc_change_wq; /* workqueue for status changes */
+       struct work sc_work;            /* work for said workqueue.  */
+       struct umcs7840_softc_oneport sc_ports[UMCS7840_MAX_PORTS];
+                                       /* data for each port */
+       uint8_t sc_numports;            /* number of ports (subunits) */
+       bool sc_init_done;              /* special one time init in open */
+       bool sc_dying;                  /* we have been deactivated */
+};
+
+static int umcs7840_get_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t *data);
+static int umcs7840_set_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t data);
+static int umcs7840_get_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t *data);
+static int umcs7840_set_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t data);
+static int umcs7840_calc_baudrate(uint32_t rate, uint16_t *divisor, uint8_t *clk);
+static void umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff);
+static void umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff);
+static void umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff);
+
+static int umcs7840_match(device_t, cfdata_t, void *);
+static void umcs7840_attach(device_t, device_t, void *);
+static int umcs7840_detach(device_t, int);
+static void umcs7840_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status);
+static void umcs7840_change_worker(struct work *w, void *arg);
+static int umcs7840_activate(device_t, enum devact); 
+static void umcs7840_childdet(device_t, device_t);
+
+static void umcs7840_get_status(void *, int, u_char *, u_char *);
+static void umcs7840_set(void *, int, int, int);
+static int umcs7840_param(void *, int, struct termios *);
+static int umcs7840_port_open(void *sc, int portno);
+static void umcs7840_port_close(void *sc, int portno);
+
+struct ucom_methods umcs7840_methods = {
+       umcs7840_get_status,
+       umcs7840_set,
+       umcs7840_param,
+       NULL,
+       umcs7840_port_open,
+       umcs7840_port_close,
+};
+
+static const struct usb_devno umcs7840_devs[] = {
+       { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7703 },
+       { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7810 },
+       { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7820 },
+       { USB_VENDOR_MOSCHIP,           USB_PRODUCT_MOSCHIP_MCS7840 },
+       { USB_VENDOR_ATEN,              USB_PRODUCT_ATEN_UC2324 }
+};
+#define umcs7840_lookup(v, p) usb_lookup(umcs7840_devs, v, p)
+
+CFATTACH_DECL2_NEW(umcs, sizeof(struct umcs7840_softc), umcs7840_match,
+    umcs7840_attach, umcs7840_detach, umcs7840_activate, NULL,
+    umcs7840_childdet);
+
+static inline int
+umcs7840_reg_sp(int phyport)
+{
+       KASSERT(phyport >= 0 && phyport < 4);
+       switch (phyport) {
+       default:
+       case 0: return MCS7840_DEV_REG_SP1;
+       case 1: return MCS7840_DEV_REG_SP2;
+       case 2: return MCS7840_DEV_REG_SP3;
+       case 3: return MCS7840_DEV_REG_SP4;
+       }
+}
+
+static inline int
+umcs7840_reg_ctrl(int phyport)
+{
+       KASSERT(phyport >= 0 && phyport < 4);
+       switch (phyport) {
+       default:
+       case 0: return MCS7840_DEV_REG_CONTROL1;
+       case 1: return MCS7840_DEV_REG_CONTROL2;
+       case 2: return MCS7840_DEV_REG_CONTROL3;
+       case 3: return MCS7840_DEV_REG_CONTROL4;
+       }
+}
+
+static inline int
+umcs7840_reg_dcr0(int phyport)
+{
+       KASSERT(phyport >= 0 && phyport < 4);
+       switch (phyport) {
+       default:
+       case 0: return MCS7840_DEV_REG_DCR0_1;
+       case 1: return MCS7840_DEV_REG_DCR0_2;
+       case 2: return MCS7840_DEV_REG_DCR0_3;
+       case 3: return MCS7840_DEV_REG_DCR0_4;
+       }
+}
+
+static int
+umcs7840_match(device_t dev, cfdata_t match, void *aux)
+{
+       struct usb_attach_arg *uaa = aux;
+
+       return (umcs7840_lookup(uaa->vendor, uaa->product) != NULL ?
+               UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
+}
+
+static void
+umcs7840_attach(device_t parent, device_t self, void * aux)
+{
+       struct umcs7840_softc *sc = device_private(self);
+       struct usb_attach_arg *uaa = aux;
+       usbd_device_handle dev = uaa->device;
+       usb_interface_descriptor_t *id;
+       usb_endpoint_descriptor_t *ed;
+       char *devinfop;
+       struct ucom_attach_args uca;
+       int error, i, intr_addr;
+       uint8_t data;
+       uint16_t isize;
+
+       sc->sc_dev = self;
+       sc->sc_udev = uaa->device;
+
+       if (usbd_set_config_index(sc->sc_udev, MCS7840_CONFIG_INDEX, 1) != 0) {
+               aprint_error(": could not set configuration no\n");
+               return;
+       }
+
+       /* get the first interface handle */
+       error = usbd_device2interface_handle(sc->sc_udev, MCS7840_IFACE_INDEX,
+           &sc->sc_iface);
+       if (error != 0) {
+               aprint_error(": could not get interface handle\n");
+               return;
+       }
+
+       /*
+        * Get number of ports
+        * Documentation (full datasheet) says, that number of ports is
+        * set as MCS7840_DEV_MODE_SELECT24S bit in MODE R/Only
+        * register. But vendor driver uses these undocumented
+        * register & bit.
+        *
+        * Experiments show, that MODE register can have `0'
+        * (4 ports) bit on 2-port device, so use vendor driver's way.
+        *
+        * Also, see notes in header file for these constants.
+        */
+       umcs7840_get_reg(sc, MCS7840_DEV_REG_GPIO, &data);
+       if (data & MCS7840_DEV_GPIO_4PORTS) {
+               sc->sc_numports = 4;
+               /* physical port no are : 0, 1, 2, 3 */
+       } else {
+               if (uaa->product == USB_PRODUCT_MOSCHIP_MCS7810)
+                       sc->sc_numports = 1;
+               else {
+                       sc->sc_numports = 2;
+                       /* physical port no are : 0 and 2 */
+               }
+       }
+       devinfop = usbd_devinfo_alloc(dev, 0);
+       aprint_normal(": %s\n", devinfop);
+       usbd_devinfo_free(devinfop);
+       aprint_verbose_dev(self, "found %d active ports\n", sc->sc_numports);
+
+       if (!umcs7840_get_reg(sc, MCS7840_DEV_REG_MODE, &data)) {
+               aprint_verbose_dev(self, "On-die confguration: RST: active %s, "
+                   "HRD: %s, PLL: %s, POR: %s, Ports: %s, EEPROM write %s, "
+                   "IrDA is %savailable\n",
+                   (data & MCS7840_DEV_MODE_RESET) ? "low" : "high",
+                   (data & MCS7840_DEV_MODE_SER_PRSNT) ? "yes" : "no",
+                   (data & MCS7840_DEV_MODE_PLLBYPASS) ? "bypassed" : "avail",
+                   (data & MCS7840_DEV_MODE_PORBYPASS) ? "bypassed" : "avail",
+                   (data & MCS7840_DEV_MODE_SELECT24S) ? "2" : "4",
+                   (data & MCS7840_DEV_MODE_EEPROMWR) ? "enabled" : "disabled",
+                   (data & MCS7840_DEV_MODE_IRDA) ? "" : "not ");
+       }
+
+       /*
+        * Set up the interrupt pipe
+        */
+       id = usbd_get_interface_descriptor(sc->sc_iface);
+       isize = 0;
+       intr_addr = -1;
+       for (i = 0 ; i < id->bNumEndpoints ; i++) {



Home | Main Index | Thread Index | Old Index