Home | History | Annotate | Line # | Download | only in usb
uchcom.c revision 1.24
      1 /*	$NetBSD: uchcom.c,v 1.24 2018/12/12 23:21:36 jakllsch Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Takuya SHIOZAKI (tshiozak (at) netbsd.org).
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: uchcom.c,v 1.24 2018/12/12 23:21:36 jakllsch Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_usb.h"
     37 #endif
     38 
     39 /*
     40  * driver for WinChipHead CH341/340, the worst USB-serial chip in the world.
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kmem.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/conf.h>
     49 #include <sys/tty.h>
     50 #include <sys/file.h>
     51 #include <sys/select.h>
     52 #include <sys/proc.h>
     53 #include <sys/device.h>
     54 #include <sys/poll.h>
     55 
     56 #include <dev/usb/usb.h>
     57 
     58 #include <dev/usb/usbdi.h>
     59 #include <dev/usb/usbdi_util.h>
     60 #include <dev/usb/usbdevs.h>
     61 
     62 #include <dev/usb/ucomvar.h>
     63 
     64 #ifdef UCHCOM_DEBUG
     65 #define DPRINTFN(n, x)  if (uchcomdebug > (n)) printf x
     66 int	uchcomdebug = 0;
     67 #else
     68 #define DPRINTFN(n, x)
     69 #endif
     70 #define DPRINTF(x) DPRINTFN(0, x)
     71 
     72 #define	UCHCOM_IFACE_INDEX	0
     73 #define	UCHCOM_CONFIG_INDEX	0
     74 
     75 #define UCHCOM_INPUT_BUF_SIZE	8
     76 
     77 #define UCHCOM_REQ_GET_VERSION	0x5F
     78 #define UCHCOM_REQ_READ_REG	0x95
     79 #define UCHCOM_REQ_WRITE_REG	0x9A
     80 #define UCHCOM_REQ_RESET	0xA1
     81 #define UCHCOM_REQ_SET_DTRRTS	0xA4
     82 
     83 #define UCHCOM_REG_STAT1	0x06
     84 #define UCHCOM_REG_STAT2	0x07
     85 #define UCHCOM_REG_BPS_PRE	0x12
     86 #define UCHCOM_REG_BPS_DIV	0x13
     87 #define UCHCOM_REG_BREAK1	0x05
     88 #define UCHCOM_REG_BREAK2	0x18
     89 #define UCHCOM_REG_LCR1		0x18
     90 #define UCHCOM_REG_LCR2		0x25
     91 
     92 #define UCHCOM_VER_20		0x20
     93 #define UCHCOM_VER_30		0x30
     94 
     95 #define UCHCOM_BASE_UNKNOWN	0
     96 
     97 #define UCHCOM_BPS_PRE_IMM	0x80	/* CH341: immediate RX forwarding */
     98 
     99 #define UCHCOM_DTR_MASK		0x20
    100 #define UCHCOM_RTS_MASK		0x40
    101 
    102 #define UCHCOM_BRK1_MASK	0x01
    103 #define UCHCOM_BRK2_MASK	0x40
    104 
    105 #define UCHCOM_LCR1_MASK	0xAF
    106 #define UCHCOM_LCR2_MASK	0x07
    107 #define UCHCOM_LCR1_PARENB	0x80
    108 #define UCHCOM_LCR2_PAREVEN	0x07
    109 #define UCHCOM_LCR2_PARODD	0x06
    110 #define UCHCOM_LCR2_PARMARK	0x05
    111 #define UCHCOM_LCR2_PARSPACE	0x04
    112 
    113 #define UCHCOM_INTR_STAT1	0x02
    114 #define UCHCOM_INTR_STAT2	0x03
    115 #define UCHCOM_INTR_LEAST	4
    116 
    117 #define UCHCOMIBUFSIZE 256
    118 #define UCHCOMOBUFSIZE 256
    119 
    120 struct uchcom_softc
    121 {
    122 	device_t		sc_dev;
    123 	struct usbd_device *	sc_udev;
    124 	device_t		sc_subdev;
    125 	struct usbd_interface *	sc_iface;
    126 	int			sc_dying;
    127 	/* */
    128 	int			sc_intr_endpoint;
    129 	int			sc_intr_size;
    130 	struct usbd_pipe *	sc_intr_pipe;
    131 	u_char			*sc_intr_buf;
    132 	/* */
    133 	uint8_t			sc_version;
    134 	int			sc_dtr;
    135 	int			sc_rts;
    136 	u_char			sc_lsr;
    137 	u_char			sc_msr;
    138 	int			sc_lcr1;
    139 	int			sc_lcr2;
    140 };
    141 
    142 struct uchcom_endpoints
    143 {
    144 	int		ep_bulkin;
    145 	int		ep_bulkout;
    146 	int		ep_intr;
    147 	int		ep_intr_size;
    148 };
    149 
    150 struct uchcom_divider
    151 {
    152 	uint8_t		dv_prescaler;
    153 	uint8_t		dv_div;
    154 };
    155 
    156 struct uchcom_divider_record
    157 {
    158 	uint32_t		dvr_high;
    159 	uint32_t		dvr_low;
    160 	uint32_t		dvr_base_clock;
    161 	struct uchcom_divider	dvr_divider;
    162 };
    163 
    164 static const struct uchcom_divider_record dividers[] =
    165 {
    166 	{  307200, 307200, UCHCOM_BASE_UNKNOWN, { 7, 0xD9 } },
    167 	{  921600, 921600, UCHCOM_BASE_UNKNOWN, { 7, 0xF3 } },
    168 	{ 2999999,  23530,             6000000, { 3,    0 } },
    169 	{   23529,   2942,              750000, { 2,    0 } },
    170 	{    2941,    368,               93750, { 1,    0 } },
    171 	{     367,      1,               11719, { 0,    0 } },
    172 };
    173 #define NUM_DIVIDERS	(sizeof (dividers) / sizeof (dividers[0]))
    174 
    175 static const struct usb_devno uchcom_devs[] = {
    176 	{ USB_VENDOR_QINHENG2, USB_PRODUCT_QINHENG2_CH341SER },
    177 	{ USB_VENDOR_QINHENG, USB_PRODUCT_QINHENG_CH340 },
    178 	{ USB_VENDOR_QINHENG, USB_PRODUCT_QINHENG_CH341_ASP },
    179 };
    180 #define uchcom_lookup(v, p)	usb_lookup(uchcom_devs, v, p)
    181 
    182 Static void	uchcom_get_status(void *, int, u_char *, u_char *);
    183 Static void	uchcom_set(void *, int, int, int);
    184 Static int	uchcom_param(void *, int, struct termios *);
    185 Static int	uchcom_open(void *, int);
    186 Static void	uchcom_close(void *, int);
    187 Static void	uchcom_intr(struct usbd_xfer *, void *,
    188 			    usbd_status);
    189 
    190 static int	set_config(struct uchcom_softc *);
    191 static int	find_ifaces(struct uchcom_softc *, struct usbd_interface **);
    192 static int	find_endpoints(struct uchcom_softc *,
    193 			       struct uchcom_endpoints *);
    194 static void	close_intr_pipe(struct uchcom_softc *);
    195 
    196 
    197 struct	ucom_methods uchcom_methods = {
    198 	.ucom_get_status	= uchcom_get_status,
    199 	.ucom_set		= uchcom_set,
    200 	.ucom_param		= uchcom_param,
    201 	.ucom_ioctl		= NULL,
    202 	.ucom_open		= uchcom_open,
    203 	.ucom_close		= uchcom_close,
    204 	.ucom_read		= NULL,
    205 	.ucom_write		= NULL,
    206 };
    207 
    208 int uchcom_match(device_t, cfdata_t, void *);
    209 void uchcom_attach(device_t, device_t, void *);
    210 void uchcom_childdet(device_t, device_t);
    211 int uchcom_detach(device_t, int);
    212 int uchcom_activate(device_t, enum devact);
    213 
    214 extern struct cfdriver uchcom_cd;
    215 
    216 CFATTACH_DECL2_NEW(uchcom,
    217     sizeof(struct uchcom_softc),
    218     uchcom_match,
    219     uchcom_attach,
    220     uchcom_detach,
    221     uchcom_activate,
    222     NULL,
    223     uchcom_childdet);
    224 
    225 /* ----------------------------------------------------------------------
    226  * driver entry points
    227  */
    228 
    229 int
    230 uchcom_match(device_t parent, cfdata_t match, void *aux)
    231 {
    232 	struct usb_attach_arg *uaa = aux;
    233 
    234 	return (uchcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    235 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    236 }
    237 
    238 void
    239 uchcom_attach(device_t parent, device_t self, void *aux)
    240 {
    241 	struct uchcom_softc *sc = device_private(self);
    242 	struct usb_attach_arg *uaa = aux;
    243 	struct usbd_device *dev = uaa->uaa_device;
    244 	char *devinfop;
    245 	struct uchcom_endpoints endpoints;
    246 	struct ucom_attach_args ucaa;
    247 
    248 	aprint_naive("\n");
    249 	aprint_normal("\n");
    250 
    251 	devinfop = usbd_devinfo_alloc(dev, 0);
    252 	aprint_normal_dev(self, "%s\n", devinfop);
    253 	usbd_devinfo_free(devinfop);
    254 
    255 	sc->sc_dev = self;
    256         sc->sc_udev = dev;
    257 	sc->sc_dying = 0;
    258 	sc->sc_dtr = sc->sc_rts = -1;
    259 	sc->sc_lsr = sc->sc_msr = 0;
    260 
    261 	DPRINTF(("\n\nuchcom attach: sc=%p\n", sc));
    262 
    263 	if (set_config(sc))
    264 		goto failed;
    265 
    266 	if (find_ifaces(sc, &sc->sc_iface))
    267 		goto failed;
    268 
    269 	if (find_endpoints(sc, &endpoints))
    270 		goto failed;
    271 
    272 	sc->sc_intr_endpoint = endpoints.ep_intr;
    273 	sc->sc_intr_size = endpoints.ep_intr_size;
    274 
    275 	/* setup ucom layer */
    276 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    277 	ucaa.ucaa_bulkin = endpoints.ep_bulkin;
    278 	ucaa.ucaa_bulkout = endpoints.ep_bulkout;
    279 	ucaa.ucaa_ibufsize = UCHCOMIBUFSIZE;
    280 	ucaa.ucaa_obufsize = UCHCOMOBUFSIZE;
    281 	ucaa.ucaa_ibufsizepad = UCHCOMIBUFSIZE;
    282 	ucaa.ucaa_opkthdrlen = 0;
    283 	ucaa.ucaa_device = dev;
    284 	ucaa.ucaa_iface = sc->sc_iface;
    285 	ucaa.ucaa_methods = &uchcom_methods;
    286 	ucaa.ucaa_arg = sc;
    287 	ucaa.ucaa_info = NULL;
    288 
    289 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    290 
    291 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    292 					    ucomprint, ucomsubmatch);
    293 
    294 	return;
    295 
    296 failed:
    297 	sc->sc_dying = 1;
    298 	return;
    299 }
    300 
    301 void
    302 uchcom_childdet(device_t self, device_t child)
    303 {
    304 	struct uchcom_softc *sc = device_private(self);
    305 
    306 	KASSERT(sc->sc_subdev == child);
    307 	sc->sc_subdev = NULL;
    308 }
    309 
    310 int
    311 uchcom_detach(device_t self, int flags)
    312 {
    313 	struct uchcom_softc *sc = device_private(self);
    314 	int rv = 0;
    315 
    316 	DPRINTF(("uchcom_detach: sc=%p flags=%d\n", sc, flags));
    317 
    318 	close_intr_pipe(sc);
    319 
    320 	sc->sc_dying = 1;
    321 
    322 	if (sc->sc_subdev != NULL)
    323 		rv = config_detach(sc->sc_subdev, flags);
    324 
    325 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    326 
    327 	return rv;
    328 }
    329 
    330 int
    331 uchcom_activate(device_t self, enum devact act)
    332 {
    333 	struct uchcom_softc *sc = device_private(self);
    334 
    335 	switch (act) {
    336 	case DVACT_DEACTIVATE:
    337 		close_intr_pipe(sc);
    338 		sc->sc_dying = 1;
    339 		return 0;
    340 	default:
    341 		return EOPNOTSUPP;
    342 	}
    343 }
    344 
    345 static int
    346 set_config(struct uchcom_softc *sc)
    347 {
    348 	usbd_status err;
    349 
    350 	err = usbd_set_config_index(sc->sc_udev, UCHCOM_CONFIG_INDEX, 1);
    351 	if (err) {
    352 		aprint_error_dev(sc->sc_dev,
    353 		    "failed to set configuration: %s\n", usbd_errstr(err));
    354 		return -1;
    355 	}
    356 
    357 	return 0;
    358 }
    359 
    360 static int
    361 find_ifaces(struct uchcom_softc *sc, struct usbd_interface **riface)
    362 {
    363 	usbd_status err;
    364 
    365 	err = usbd_device2interface_handle(sc->sc_udev, UCHCOM_IFACE_INDEX,
    366 					   riface);
    367 	if (err) {
    368 		aprint_error("\n%s: failed to get interface: %s\n",
    369 			device_xname(sc->sc_dev), usbd_errstr(err));
    370 		return -1;
    371 	}
    372 
    373 	return 0;
    374 }
    375 
    376 static int
    377 find_endpoints(struct uchcom_softc *sc, struct uchcom_endpoints *endpoints)
    378 {
    379 	int i, bin=-1, bout=-1, intr=-1, isize=0;
    380 	usb_interface_descriptor_t *id;
    381 	usb_endpoint_descriptor_t *ed;
    382 
    383 	id = usbd_get_interface_descriptor(sc->sc_iface);
    384 
    385 	for (i = 0; i < id->bNumEndpoints; i++) {
    386 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    387 		if (ed == NULL) {
    388 			aprint_error_dev(sc->sc_dev,
    389 			    "no endpoint descriptor for %d\n", i);
    390 			return -1;
    391 		}
    392 
    393 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    394 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    395 			intr = ed->bEndpointAddress;
    396 			isize = UGETW(ed->wMaxPacketSize);
    397 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    398 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    399 			bin = ed->bEndpointAddress;
    400 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    401 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    402 			bout = ed->bEndpointAddress;
    403 		}
    404 	}
    405 
    406 	if (intr == -1 || bin == -1 || bout == -1) {
    407 		if (intr == -1) {
    408 			aprint_error_dev(sc->sc_dev,
    409 			    "no interrupt end point\n");
    410 		}
    411 		if (bin == -1) {
    412 			aprint_error_dev(sc->sc_dev,
    413 			    "no data bulk in end point\n");
    414 		}
    415 		if (bout == -1) {
    416 			aprint_error_dev(sc->sc_dev,
    417 			    "no data bulk out end point\n");
    418 		}
    419 		return -1;
    420 	}
    421 	if (isize < UCHCOM_INTR_LEAST) {
    422 		aprint_error_dev(sc->sc_dev, "intr pipe is too short\n");
    423 		return -1;
    424 	}
    425 
    426 	DPRINTF(("%s: bulkin=%d, bulkout=%d, intr=%d, isize=%d\n",
    427 		 device_xname(sc->sc_dev), bin, bout, intr, isize));
    428 
    429 	endpoints->ep_intr = intr;
    430 	endpoints->ep_intr_size = isize;
    431 	endpoints->ep_bulkin = bin;
    432 	endpoints->ep_bulkout = bout;
    433 
    434 	return 0;
    435 }
    436 
    437 
    438 /* ----------------------------------------------------------------------
    439  * low level i/o
    440  */
    441 
    442 static __inline usbd_status
    443 generic_control_out(struct uchcom_softc *sc, uint8_t reqno,
    444 		    uint16_t value, uint16_t index)
    445 {
    446 	usb_device_request_t req;
    447 
    448 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    449 	req.bRequest = reqno;
    450 	USETW(req.wValue, value);
    451 	USETW(req.wIndex, index);
    452 	USETW(req.wLength, 0);
    453 
    454 	return usbd_do_request(sc->sc_udev, &req, 0);
    455 }
    456 
    457 static __inline usbd_status
    458 generic_control_in(struct uchcom_softc *sc, uint8_t reqno,
    459 		   uint16_t value, uint16_t index, void *buf, int buflen,
    460 		   int *actlen)
    461 {
    462 	usb_device_request_t req;
    463 
    464 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    465 	req.bRequest = reqno;
    466 	USETW(req.wValue, value);
    467 	USETW(req.wIndex, index);
    468 	USETW(req.wLength, (uint16_t)buflen);
    469 
    470 	return usbd_do_request_flags(sc->sc_udev, &req, buf,
    471 				     USBD_SHORT_XFER_OK, actlen,
    472 				     USBD_DEFAULT_TIMEOUT);
    473 }
    474 
    475 static __inline usbd_status
    476 write_reg(struct uchcom_softc *sc,
    477 	  uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
    478 {
    479 	DPRINTF(("%s: write reg 0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
    480 		 device_xname(sc->sc_dev),
    481 		 (unsigned)reg1, (unsigned)val1,
    482 		 (unsigned)reg2, (unsigned)val2));
    483 	return generic_control_out(
    484 		sc, UCHCOM_REQ_WRITE_REG,
    485 		reg1|((uint16_t)reg2<<8), val1|((uint16_t)val2<<8));
    486 }
    487 
    488 static __inline usbd_status
    489 read_reg(struct uchcom_softc *sc,
    490 	 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
    491 {
    492 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
    493 	usbd_status err;
    494 	int actin;
    495 
    496 	err = generic_control_in(
    497 		sc, UCHCOM_REQ_READ_REG,
    498 		reg1|((uint16_t)reg2<<8), 0, buf, sizeof(buf), &actin);
    499 	if (err)
    500 		return err;
    501 
    502 	DPRINTF(("%s: read reg 0x%02X->0x%02X, 0x%02X->0x%02X\n",
    503 		 device_xname(sc->sc_dev),
    504 		 (unsigned)reg1, (unsigned)buf[0],
    505 		 (unsigned)reg2, (unsigned)buf[1]));
    506 
    507 	if (rval1) *rval1 = buf[0];
    508 	if (rval2) *rval2 = buf[1];
    509 
    510 	return USBD_NORMAL_COMPLETION;
    511 }
    512 
    513 static __inline usbd_status
    514 get_version(struct uchcom_softc *sc, uint8_t *rver)
    515 {
    516 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
    517 	usbd_status err;
    518 	int actin;
    519 
    520 	err = generic_control_in(
    521 		sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf), &actin);
    522 	if (err)
    523 		return err;
    524 
    525 	if (rver) *rver = buf[0];
    526 
    527 	return USBD_NORMAL_COMPLETION;
    528 }
    529 
    530 static __inline usbd_status
    531 get_status(struct uchcom_softc *sc, uint8_t *rval)
    532 {
    533 	return read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
    534 }
    535 
    536 static __inline usbd_status
    537 set_dtrrts_10(struct uchcom_softc *sc, uint8_t val)
    538 {
    539 	return write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
    540 }
    541 
    542 static __inline usbd_status
    543 set_dtrrts_20(struct uchcom_softc *sc, uint8_t val)
    544 {
    545 	return generic_control_out(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
    546 }
    547 
    548 
    549 /* ----------------------------------------------------------------------
    550  * middle layer
    551  */
    552 
    553 static int
    554 update_version(struct uchcom_softc *sc)
    555 {
    556 	usbd_status err;
    557 
    558 	err = get_version(sc, &sc->sc_version);
    559 	if (err) {
    560 		device_printf(sc->sc_dev, "cannot get version: %s\n",
    561 		    usbd_errstr(err));
    562 		return EIO;
    563 	}
    564 	DPRINTF(("%s: update_version %d\n", device_xname(sc->sc_dev), sc->sc_version));
    565 
    566 	return 0;
    567 }
    568 
    569 static void
    570 convert_status(struct uchcom_softc *sc, uint8_t cur)
    571 {
    572 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
    573 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
    574 
    575 	cur = ~cur & 0x0F;
    576 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
    577 }
    578 
    579 static int
    580 update_status(struct uchcom_softc *sc)
    581 {
    582 	usbd_status err;
    583 	uint8_t cur;
    584 
    585 	err = get_status(sc, &cur);
    586 	if (err) {
    587 		device_printf(sc->sc_dev,
    588 		    "cannot update status: %s\n", usbd_errstr(err));
    589 		return EIO;
    590 	}
    591 	convert_status(sc, cur);
    592 
    593 	return 0;
    594 }
    595 
    596 
    597 static int
    598 set_dtrrts(struct uchcom_softc *sc, int dtr, int rts)
    599 {
    600 	usbd_status err;
    601 	uint8_t val = 0;
    602 
    603 	if (dtr) val |= UCHCOM_DTR_MASK;
    604 	if (rts) val |= UCHCOM_RTS_MASK;
    605 
    606 	if (sc->sc_version < UCHCOM_VER_20)
    607 		err = set_dtrrts_10(sc, ~val);
    608 	else
    609 		err = set_dtrrts_20(sc, ~val);
    610 
    611 	if (err) {
    612 		device_printf(sc->sc_dev, "cannot set DTR/RTS: %s\n",
    613 		    usbd_errstr(err));
    614 		return EIO;
    615 	}
    616 
    617 	return 0;
    618 }
    619 
    620 static int
    621 set_break(struct uchcom_softc *sc, int onoff)
    622 {
    623 	usbd_status err;
    624 	uint8_t brk1, brk2;
    625 
    626 	err = read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
    627 	if (err)
    628 		return EIO;
    629 	if (onoff) {
    630 		/* on - clear bits */
    631 		brk1 &= ~UCHCOM_BRK1_MASK;
    632 		brk2 &= ~UCHCOM_BRK2_MASK;
    633 	} else {
    634 		/* off - set bits */
    635 		brk1 |= UCHCOM_BRK1_MASK;
    636 		brk2 |= UCHCOM_BRK2_MASK;
    637 	}
    638 	err = write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
    639 	if (err)
    640 		return EIO;
    641 
    642 	return 0;
    643 }
    644 
    645 static int
    646 calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
    647 {
    648 	int i;
    649 	const struct uchcom_divider_record *rp;
    650 	uint32_t div, rem;
    651 
    652 	/* find record */
    653 	for (i=0; i<NUM_DIVIDERS; i++) {
    654 		if (dividers[i].dvr_high >= rate &&
    655 		    dividers[i].dvr_low <= rate) {
    656 			rp = &dividers[i];
    657 			goto found;
    658 		}
    659 	}
    660 	return -1;
    661 
    662 found:
    663 	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
    664 	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
    665 		dp->dv_div = rp->dvr_divider.dv_div;
    666 	else {
    667 		div = rp->dvr_base_clock / rate;
    668 		rem = rp->dvr_base_clock % rate;
    669 		if (div==0 || div>=0xFF)
    670 			return -1;
    671 		if ((rem<<1) >= rate)
    672 			div += 1;
    673 		dp->dv_div = (uint8_t)-div;
    674 	}
    675 
    676 	return 0;
    677 }
    678 
    679 static int
    680 set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
    681 {
    682 	usbd_status err;
    683 	struct uchcom_divider dv;
    684 
    685 	if (calc_divider_settings(&dv, rate))
    686 		return EINVAL;
    687 
    688 	if ((err = write_reg(sc,
    689 			     UCHCOM_REG_BPS_PRE,
    690 			     dv.dv_prescaler | UCHCOM_BPS_PRE_IMM,
    691 			     UCHCOM_REG_BPS_DIV, dv.dv_div))) {
    692 		device_printf(sc->sc_dev, "cannot set DTE rate: %s\n",
    693 		    usbd_errstr(err));
    694 		return EIO;
    695 	}
    696 
    697 	return 0;
    698 }
    699 
    700 static int
    701 set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
    702 {
    703 	if (sc->sc_version < UCHCOM_VER_30) {
    704 		usbd_status err;
    705 		uint8_t lcr1val = 0, lcr2val = 0;
    706 
    707 		err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
    708 		if (err) {
    709 			device_printf(sc->sc_dev, "cannot get LCR: %s\n",
    710 			    usbd_errstr(err));
    711 			return EIO;
    712 		}
    713 
    714 		lcr1val &= ~UCHCOM_LCR1_MASK;
    715 		lcr2val &= ~UCHCOM_LCR2_MASK;
    716 
    717 		/*
    718 		 * XXX: it is difficult to handle the line control appropriately:
    719 		 *   - CS8, !CSTOPB and any parity mode seems ok, but
    720 		 *   - the chip doesn't have the function to calculate parity
    721 		 *     in !CS8 mode.
    722 		 *   - it is unclear that the chip supports CS5,6 mode.
    723 		 *   - it is unclear how to handle stop bits.
    724 		 */
    725 
    726 		switch (ISSET(cflag, CSIZE)) {
    727 		case CS5:
    728 		case CS6:
    729 		case CS7:
    730 			return EINVAL;
    731 		case CS8:
    732 			break;
    733 		}
    734 
    735 		if (ISSET(cflag, PARENB)) {
    736 			lcr1val |= UCHCOM_LCR1_PARENB;
    737 			if (ISSET(cflag, PARODD))
    738 				lcr2val |= UCHCOM_LCR2_PARODD;
    739 			else
    740 				lcr2val |= UCHCOM_LCR2_PAREVEN;
    741 		}
    742 
    743 		err = write_reg(sc, UCHCOM_REG_LCR1, lcr1val, UCHCOM_REG_LCR2, lcr2val);
    744 		if (err) {
    745 			device_printf(sc->sc_dev, "cannot set LCR: %s\n",
    746 			    usbd_errstr(err));
    747 			return EIO;
    748 		}
    749 	}
    750 
    751 	return 0;
    752 }
    753 
    754 static int
    755 clear_chip(struct uchcom_softc *sc)
    756 {
    757 	usbd_status err;
    758 
    759 	DPRINTF(("%s: clear\n", device_xname(sc->sc_dev)));
    760 	err = generic_control_out(sc, UCHCOM_REQ_RESET, 0, 0);
    761 	if (err) {
    762 		device_printf(sc->sc_dev, "cannot clear: %s\n",
    763 		    usbd_errstr(err));
    764 		return EIO;
    765 	}
    766 
    767 	return 0;
    768 }
    769 
    770 static int
    771 reset_chip(struct uchcom_softc *sc)
    772 {
    773 	usbd_status err;
    774 	uint8_t lcr1val, lcr2val, pre, div;
    775 	uint16_t val=0, idx=0;
    776 
    777 	err = read_reg(sc, UCHCOM_REG_LCR1, &lcr1val, UCHCOM_REG_LCR2, &lcr2val);
    778 	if (err)
    779 		goto failed;
    780 
    781 	err = read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div);
    782 	if (err)
    783 		goto failed;
    784 
    785 	val |= (uint16_t)(lcr1val&0xF0) << 8;
    786 	val |= 0x01;
    787 	val |= (uint16_t)(lcr2val&0x0F) << 8;
    788 	val |= 0x02;
    789 	idx |= pre & 0x07;
    790 	val |= 0x04;
    791 	idx |= (uint16_t)div << 8;
    792 	val |= 0x08;
    793 	val |= 0x10;
    794 
    795 	DPRINTF(("%s: reset v=0x%04X, i=0x%04X\n",
    796 		 device_xname(sc->sc_dev), val, idx));
    797 
    798 	err = generic_control_out(sc, UCHCOM_REQ_RESET, val, idx);
    799 	if (err)
    800 		goto failed;
    801 
    802 	return 0;
    803 
    804 failed:
    805 	printf("%s: cannot reset: %s\n",
    806 	       device_xname(sc->sc_dev), usbd_errstr(err));
    807 	return EIO;
    808 }
    809 
    810 static int
    811 setup_comm(struct uchcom_softc *sc)
    812 {
    813 	int ret;
    814 
    815 	ret = update_version(sc);
    816 	if (ret)
    817 		return ret;
    818 
    819 	ret = clear_chip(sc);
    820 	if (ret)
    821 		return ret;
    822 
    823 	ret = set_dte_rate(sc, TTYDEF_SPEED);
    824 	if (ret)
    825 		return ret;
    826 
    827 	ret = set_line_control(sc, CS8);
    828 	if (ret)
    829 		return ret;
    830 
    831 	ret = update_status(sc);
    832 	if (ret)
    833 		return ret;
    834 
    835 	ret = reset_chip(sc);
    836 	if (ret)
    837 		return ret;
    838 
    839 	ret = set_dte_rate(sc, TTYDEF_SPEED); /* XXX */
    840 	if (ret)
    841 		return ret;
    842 
    843 	sc->sc_dtr = sc->sc_rts = 1;
    844 	ret = set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    845 	if (ret)
    846 		return ret;
    847 
    848 	return 0;
    849 }
    850 
    851 static int
    852 setup_intr_pipe(struct uchcom_softc *sc)
    853 {
    854 	usbd_status err;
    855 
    856 	if (sc->sc_intr_endpoint != -1 && sc->sc_intr_pipe == NULL) {
    857 		sc->sc_intr_buf = kmem_alloc(sc->sc_intr_size, KM_SLEEP);
    858 		err = usbd_open_pipe_intr(sc->sc_iface,
    859 					  sc->sc_intr_endpoint,
    860 					  USBD_SHORT_XFER_OK,
    861 					  &sc->sc_intr_pipe, sc,
    862 					  sc->sc_intr_buf,
    863 					  sc->sc_intr_size,
    864 					  uchcom_intr, USBD_DEFAULT_INTERVAL);
    865 		if (err) {
    866 			device_printf(sc->sc_dev,
    867 			    "cannot open interrupt pipe: %s\n",
    868 			    usbd_errstr(err));
    869 			return EIO;
    870 		}
    871 	}
    872 	return 0;
    873 }
    874 
    875 static void
    876 close_intr_pipe(struct uchcom_softc *sc)
    877 {
    878 	usbd_status err;
    879 
    880 	if (sc->sc_dying)
    881 		return;
    882 
    883 	if (sc->sc_intr_pipe != NULL) {
    884 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    885 		if (err)
    886 			device_printf(sc->sc_dev,
    887 			    "abort interrupt pipe failed: %s\n",
    888 			    usbd_errstr(err));
    889 		err = usbd_close_pipe(sc->sc_intr_pipe);
    890 		if (err)
    891 			device_printf(sc->sc_dev,
    892 			    "close interrupt pipe failed: %s\n",
    893 			    usbd_errstr(err));
    894 		kmem_free(sc->sc_intr_buf, sc->sc_intr_size);
    895 		sc->sc_intr_pipe = NULL;
    896 	}
    897 }
    898 
    899 
    900 /* ----------------------------------------------------------------------
    901  * methods for ucom
    902  */
    903 void
    904 uchcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr)
    905 {
    906 	struct uchcom_softc *sc = arg;
    907 
    908 	if (sc->sc_dying)
    909 		return;
    910 
    911 	*rlsr = sc->sc_lsr;
    912 	*rmsr = sc->sc_msr;
    913 }
    914 
    915 void
    916 uchcom_set(void *arg, int portno, int reg, int onoff)
    917 {
    918 	struct uchcom_softc *sc = arg;
    919 
    920 	if (sc->sc_dying)
    921 		return;
    922 
    923 	switch (reg) {
    924 	case UCOM_SET_DTR:
    925 		sc->sc_dtr = !!onoff;
    926 		set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    927 		break;
    928 	case UCOM_SET_RTS:
    929 		sc->sc_rts = !!onoff;
    930 		set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    931 		break;
    932 	case UCOM_SET_BREAK:
    933 		set_break(sc, onoff);
    934 		break;
    935 	}
    936 }
    937 
    938 int
    939 uchcom_param(void *arg, int portno, struct termios *t)
    940 {
    941 	struct uchcom_softc *sc = arg;
    942 	int ret;
    943 
    944 	if (sc->sc_dying)
    945 		return 0;
    946 
    947 	ret = set_line_control(sc, t->c_cflag);
    948 	if (ret)
    949 		return ret;
    950 
    951 	ret = set_dte_rate(sc, t->c_ospeed);
    952 	if (ret)
    953 		return ret;
    954 
    955 	return 0;
    956 }
    957 
    958 int
    959 uchcom_open(void *arg, int portno)
    960 {
    961 	int ret;
    962 	struct uchcom_softc *sc = arg;
    963 
    964 	if (sc->sc_dying)
    965 		return EIO;
    966 
    967 	ret = setup_intr_pipe(sc);
    968 	if (ret)
    969 		return ret;
    970 
    971 	ret = setup_comm(sc);
    972 	if (ret)
    973 		return ret;
    974 
    975 	return 0;
    976 }
    977 
    978 void
    979 uchcom_close(void *arg, int portno)
    980 {
    981 	struct uchcom_softc *sc = arg;
    982 
    983 	if (sc->sc_dying)
    984 		return;
    985 
    986 	close_intr_pipe(sc);
    987 }
    988 
    989 
    990 /* ----------------------------------------------------------------------
    991  * callback when the modem status is changed.
    992  */
    993 void
    994 uchcom_intr(struct usbd_xfer *xfer, void * priv,
    995 	    usbd_status status)
    996 {
    997 	struct uchcom_softc *sc = priv;
    998 	u_char *buf = sc->sc_intr_buf;
    999 
   1000 	if (sc->sc_dying)
   1001 		return;
   1002 
   1003 	if (status != USBD_NORMAL_COMPLETION) {
   1004 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1005 			return;
   1006 
   1007 		DPRINTF(("%s: abnormal status: %s\n",
   1008 			 device_xname(sc->sc_dev), usbd_errstr(status)));
   1009 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
   1010 		return;
   1011 	}
   1012 	DPRINTF(("%s: intr: 0x%02X 0x%02X 0x%02X 0x%02X "
   1013 		 "0x%02X 0x%02X 0x%02X 0x%02X\n",
   1014 		 device_xname(sc->sc_dev),
   1015 		 (unsigned)buf[0], (unsigned)buf[1],
   1016 		 (unsigned)buf[2], (unsigned)buf[3],
   1017 		 (unsigned)buf[4], (unsigned)buf[5],
   1018 		 (unsigned)buf[6], (unsigned)buf[7]));
   1019 
   1020 	convert_status(sc, buf[UCHCOM_INTR_STAT1]);
   1021 	ucom_status_change(device_private(sc->sc_subdev));
   1022 }
   1023