Home | History | Annotate | Line # | Download | only in usb
uchcom.c revision 1.28
      1 /*	$NetBSD: uchcom.c,v 1.28 2019/01/31 18:21:21 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.28 2019/01/31 18:21:21 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_BREAK	0x05
     88 #define UCHCOM_REG_LCR		0x18
     89 #define UCHCOM_REG_LCR2		0x25
     90 
     91 #define UCHCOM_VER_20		0x20
     92 #define UCHCOM_VER_30		0x30
     93 
     94 #define UCHCOM_BPS_PRE_IMM	0x80	/* CH341: immediate RX forwarding */
     95 
     96 #define UCHCOM_DTR_MASK		0x20
     97 #define UCHCOM_RTS_MASK		0x40
     98 
     99 #define UCHCOM_BREAK_MASK	0x01
    100 
    101 #define UCHCOM_LCR_CS5		0x00
    102 #define UCHCOM_LCR_CS6		0x01
    103 #define UCHCOM_LCR_CS7		0x02
    104 #define UCHCOM_LCR_CS8		0x03
    105 #define UCHCOM_LCR_STOPB	0x04
    106 #define UCHCOM_LCR_PARENB	0x08
    107 #define UCHCOM_LCR_PARODD	0x00
    108 #define UCHCOM_LCR_PAREVEN	0x10
    109 #define UCHCOM_LCR_PARMARK	0x20
    110 #define UCHCOM_LCR_PARSPACE	0x30
    111 #define UCHCOM_LCR_TXE		0x40
    112 #define UCHCOM_LCR_RXE		0x80
    113 
    114 #define UCHCOM_INTR_STAT1	0x02
    115 #define UCHCOM_INTR_STAT2	0x03
    116 #define UCHCOM_INTR_LEAST	4
    117 
    118 #define UCHCOMIBUFSIZE 256
    119 #define UCHCOMOBUFSIZE 256
    120 
    121 #define UCHCOM_RESET_VALUE	0x501F
    122 #define UCHCOM_RESET_INDEX	0xD90A
    123 
    124 struct uchcom_softc
    125 {
    126 	device_t		sc_dev;
    127 	struct usbd_device *	sc_udev;
    128 	device_t		sc_subdev;
    129 	struct usbd_interface *	sc_iface;
    130 	int			sc_dying;
    131 	/* */
    132 	int			sc_intr_endpoint;
    133 	int			sc_intr_size;
    134 	struct usbd_pipe *	sc_intr_pipe;
    135 	u_char			*sc_intr_buf;
    136 	/* */
    137 	uint8_t			sc_version;
    138 	int			sc_dtr;
    139 	int			sc_rts;
    140 	u_char			sc_lsr;
    141 	u_char			sc_msr;
    142 };
    143 
    144 struct uchcom_endpoints
    145 {
    146 	int		ep_bulkin;
    147 	int		ep_bulkout;
    148 	int		ep_intr;
    149 	int		ep_intr_size;
    150 };
    151 
    152 struct uchcom_divider
    153 {
    154 	uint8_t		dv_prescaler;
    155 	uint8_t		dv_div;
    156 };
    157 
    158 static const uint32_t rates4x[8] = {
    159 	[0] = 4 * 12000000 / 1024,
    160 	[1] = 4 * 12000000 / 128,
    161 	[2] = 4 * 12000000 / 16,
    162 	[3] = 4 * 12000000 / 2,
    163 	[7] = 4 * 12000000,
    164 };
    165 
    166 static const struct usb_devno uchcom_devs[] = {
    167 	{ USB_VENDOR_QINHENG2, USB_PRODUCT_QINHENG2_CH341SER },
    168 	{ USB_VENDOR_QINHENG, USB_PRODUCT_QINHENG_CH340 },
    169 	{ USB_VENDOR_QINHENG, USB_PRODUCT_QINHENG_CH341_ASP },
    170 };
    171 #define uchcom_lookup(v, p)	usb_lookup(uchcom_devs, v, p)
    172 
    173 Static void	uchcom_get_status(void *, int, u_char *, u_char *);
    174 Static void	uchcom_set(void *, int, int, int);
    175 Static int	uchcom_param(void *, int, struct termios *);
    176 Static int	uchcom_open(void *, int);
    177 Static void	uchcom_close(void *, int);
    178 Static void	uchcom_intr(struct usbd_xfer *, void *,
    179 			    usbd_status);
    180 
    181 static int	set_config(struct uchcom_softc *);
    182 static int	find_ifaces(struct uchcom_softc *, struct usbd_interface **);
    183 static int	find_endpoints(struct uchcom_softc *,
    184 			       struct uchcom_endpoints *);
    185 static void	close_intr_pipe(struct uchcom_softc *);
    186 
    187 
    188 struct	ucom_methods uchcom_methods = {
    189 	.ucom_get_status	= uchcom_get_status,
    190 	.ucom_set		= uchcom_set,
    191 	.ucom_param		= uchcom_param,
    192 	.ucom_ioctl		= NULL,
    193 	.ucom_open		= uchcom_open,
    194 	.ucom_close		= uchcom_close,
    195 	.ucom_read		= NULL,
    196 	.ucom_write		= NULL,
    197 };
    198 
    199 int uchcom_match(device_t, cfdata_t, void *);
    200 void uchcom_attach(device_t, device_t, void *);
    201 void uchcom_childdet(device_t, device_t);
    202 int uchcom_detach(device_t, int);
    203 int uchcom_activate(device_t, enum devact);
    204 
    205 extern struct cfdriver uchcom_cd;
    206 
    207 CFATTACH_DECL2_NEW(uchcom,
    208     sizeof(struct uchcom_softc),
    209     uchcom_match,
    210     uchcom_attach,
    211     uchcom_detach,
    212     uchcom_activate,
    213     NULL,
    214     uchcom_childdet);
    215 
    216 /* ----------------------------------------------------------------------
    217  * driver entry points
    218  */
    219 
    220 int
    221 uchcom_match(device_t parent, cfdata_t match, void *aux)
    222 {
    223 	struct usb_attach_arg *uaa = aux;
    224 
    225 	return (uchcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    226 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    227 }
    228 
    229 void
    230 uchcom_attach(device_t parent, device_t self, void *aux)
    231 {
    232 	struct uchcom_softc *sc = device_private(self);
    233 	struct usb_attach_arg *uaa = aux;
    234 	struct usbd_device *dev = uaa->uaa_device;
    235 	char *devinfop;
    236 	struct uchcom_endpoints endpoints;
    237 	struct ucom_attach_args ucaa;
    238 
    239 	aprint_naive("\n");
    240 	aprint_normal("\n");
    241 
    242 	devinfop = usbd_devinfo_alloc(dev, 0);
    243 	aprint_normal_dev(self, "%s\n", devinfop);
    244 	usbd_devinfo_free(devinfop);
    245 
    246 	sc->sc_dev = self;
    247         sc->sc_udev = dev;
    248 	sc->sc_dying = 0;
    249 	sc->sc_dtr = sc->sc_rts = -1;
    250 	sc->sc_lsr = sc->sc_msr = 0;
    251 
    252 	DPRINTF(("\n\nuchcom attach: sc=%p\n", sc));
    253 
    254 	if (set_config(sc))
    255 		goto failed;
    256 
    257 	if (find_ifaces(sc, &sc->sc_iface))
    258 		goto failed;
    259 
    260 	if (find_endpoints(sc, &endpoints))
    261 		goto failed;
    262 
    263 	sc->sc_intr_endpoint = endpoints.ep_intr;
    264 	sc->sc_intr_size = endpoints.ep_intr_size;
    265 
    266 	/* setup ucom layer */
    267 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    268 	ucaa.ucaa_bulkin = endpoints.ep_bulkin;
    269 	ucaa.ucaa_bulkout = endpoints.ep_bulkout;
    270 	ucaa.ucaa_ibufsize = UCHCOMIBUFSIZE;
    271 	ucaa.ucaa_obufsize = UCHCOMOBUFSIZE;
    272 	ucaa.ucaa_ibufsizepad = UCHCOMIBUFSIZE;
    273 	ucaa.ucaa_opkthdrlen = 0;
    274 	ucaa.ucaa_device = dev;
    275 	ucaa.ucaa_iface = sc->sc_iface;
    276 	ucaa.ucaa_methods = &uchcom_methods;
    277 	ucaa.ucaa_arg = sc;
    278 	ucaa.ucaa_info = NULL;
    279 
    280 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    281 
    282 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
    283 					    ucomprint, ucomsubmatch);
    284 
    285 	return;
    286 
    287 failed:
    288 	sc->sc_dying = 1;
    289 	return;
    290 }
    291 
    292 void
    293 uchcom_childdet(device_t self, device_t child)
    294 {
    295 	struct uchcom_softc *sc = device_private(self);
    296 
    297 	KASSERT(sc->sc_subdev == child);
    298 	sc->sc_subdev = NULL;
    299 }
    300 
    301 int
    302 uchcom_detach(device_t self, int flags)
    303 {
    304 	struct uchcom_softc *sc = device_private(self);
    305 	int rv = 0;
    306 
    307 	DPRINTF(("uchcom_detach: sc=%p flags=%d\n", sc, flags));
    308 
    309 	close_intr_pipe(sc);
    310 
    311 	sc->sc_dying = 1;
    312 
    313 	if (sc->sc_subdev != NULL)
    314 		rv = config_detach(sc->sc_subdev, flags);
    315 
    316 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    317 
    318 	return rv;
    319 }
    320 
    321 int
    322 uchcom_activate(device_t self, enum devact act)
    323 {
    324 	struct uchcom_softc *sc = device_private(self);
    325 
    326 	switch (act) {
    327 	case DVACT_DEACTIVATE:
    328 		close_intr_pipe(sc);
    329 		sc->sc_dying = 1;
    330 		return 0;
    331 	default:
    332 		return EOPNOTSUPP;
    333 	}
    334 }
    335 
    336 static int
    337 set_config(struct uchcom_softc *sc)
    338 {
    339 	usbd_status err;
    340 
    341 	err = usbd_set_config_index(sc->sc_udev, UCHCOM_CONFIG_INDEX, 1);
    342 	if (err) {
    343 		aprint_error_dev(sc->sc_dev,
    344 		    "failed to set configuration: %s\n", usbd_errstr(err));
    345 		return -1;
    346 	}
    347 
    348 	return 0;
    349 }
    350 
    351 static int
    352 find_ifaces(struct uchcom_softc *sc, struct usbd_interface **riface)
    353 {
    354 	usbd_status err;
    355 
    356 	err = usbd_device2interface_handle(sc->sc_udev, UCHCOM_IFACE_INDEX,
    357 					   riface);
    358 	if (err) {
    359 		aprint_error("\n%s: failed to get interface: %s\n",
    360 			device_xname(sc->sc_dev), usbd_errstr(err));
    361 		return -1;
    362 	}
    363 
    364 	return 0;
    365 }
    366 
    367 static int
    368 find_endpoints(struct uchcom_softc *sc, struct uchcom_endpoints *endpoints)
    369 {
    370 	int i, bin=-1, bout=-1, intr=-1, isize=0;
    371 	usb_interface_descriptor_t *id;
    372 	usb_endpoint_descriptor_t *ed;
    373 
    374 	id = usbd_get_interface_descriptor(sc->sc_iface);
    375 
    376 	for (i = 0; i < id->bNumEndpoints; i++) {
    377 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    378 		if (ed == NULL) {
    379 			aprint_error_dev(sc->sc_dev,
    380 			    "no endpoint descriptor for %d\n", i);
    381 			return -1;
    382 		}
    383 
    384 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    385 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    386 			intr = ed->bEndpointAddress;
    387 			isize = UGETW(ed->wMaxPacketSize);
    388 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    389 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    390 			bin = ed->bEndpointAddress;
    391 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    392 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    393 			bout = ed->bEndpointAddress;
    394 		}
    395 	}
    396 
    397 	if (intr == -1 || bin == -1 || bout == -1) {
    398 		if (intr == -1) {
    399 			aprint_error_dev(sc->sc_dev,
    400 			    "no interrupt end point\n");
    401 		}
    402 		if (bin == -1) {
    403 			aprint_error_dev(sc->sc_dev,
    404 			    "no data bulk in end point\n");
    405 		}
    406 		if (bout == -1) {
    407 			aprint_error_dev(sc->sc_dev,
    408 			    "no data bulk out end point\n");
    409 		}
    410 		return -1;
    411 	}
    412 	if (isize < UCHCOM_INTR_LEAST) {
    413 		aprint_error_dev(sc->sc_dev, "intr pipe is too short\n");
    414 		return -1;
    415 	}
    416 
    417 	DPRINTF(("%s: bulkin=%d, bulkout=%d, intr=%d, isize=%d\n",
    418 		 device_xname(sc->sc_dev), bin, bout, intr, isize));
    419 
    420 	endpoints->ep_intr = intr;
    421 	endpoints->ep_intr_size = isize;
    422 	endpoints->ep_bulkin = bin;
    423 	endpoints->ep_bulkout = bout;
    424 
    425 	return 0;
    426 }
    427 
    428 
    429 /* ----------------------------------------------------------------------
    430  * low level i/o
    431  */
    432 
    433 static __inline usbd_status
    434 generic_control_out(struct uchcom_softc *sc, uint8_t reqno,
    435 		    uint16_t value, uint16_t index)
    436 {
    437 	usb_device_request_t req;
    438 
    439 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    440 	req.bRequest = reqno;
    441 	USETW(req.wValue, value);
    442 	USETW(req.wIndex, index);
    443 	USETW(req.wLength, 0);
    444 
    445 	return usbd_do_request(sc->sc_udev, &req, 0);
    446 }
    447 
    448 static __inline usbd_status
    449 generic_control_in(struct uchcom_softc *sc, uint8_t reqno,
    450 		   uint16_t value, uint16_t index, void *buf, int buflen,
    451 		   int *actlen)
    452 {
    453 	usb_device_request_t req;
    454 
    455 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    456 	req.bRequest = reqno;
    457 	USETW(req.wValue, value);
    458 	USETW(req.wIndex, index);
    459 	USETW(req.wLength, (uint16_t)buflen);
    460 
    461 	return usbd_do_request_flags(sc->sc_udev, &req, buf,
    462 				     USBD_SHORT_XFER_OK, actlen,
    463 				     USBD_DEFAULT_TIMEOUT);
    464 }
    465 
    466 static __inline usbd_status
    467 write_reg(struct uchcom_softc *sc,
    468 	  uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
    469 {
    470 	DPRINTF(("%s: write reg 0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
    471 		 device_xname(sc->sc_dev),
    472 		 (unsigned)reg1, (unsigned)val1,
    473 		 (unsigned)reg2, (unsigned)val2));
    474 	return generic_control_out(
    475 		sc, UCHCOM_REQ_WRITE_REG,
    476 		reg1|((uint16_t)reg2<<8), val1|((uint16_t)val2<<8));
    477 }
    478 
    479 static __inline usbd_status
    480 read_reg(struct uchcom_softc *sc,
    481 	 uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
    482 {
    483 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
    484 	usbd_status err;
    485 	int actin;
    486 
    487 	err = generic_control_in(
    488 		sc, UCHCOM_REQ_READ_REG,
    489 		reg1|((uint16_t)reg2<<8), 0, buf, sizeof(buf), &actin);
    490 	if (err)
    491 		return err;
    492 
    493 	DPRINTF(("%s: read reg 0x%02X->0x%02X, 0x%02X->0x%02X\n",
    494 		 device_xname(sc->sc_dev),
    495 		 (unsigned)reg1, (unsigned)buf[0],
    496 		 (unsigned)reg2, (unsigned)buf[1]));
    497 
    498 	if (rval1) *rval1 = buf[0];
    499 	if (rval2) *rval2 = buf[1];
    500 
    501 	return USBD_NORMAL_COMPLETION;
    502 }
    503 
    504 static __inline usbd_status
    505 get_version(struct uchcom_softc *sc, uint8_t *rver)
    506 {
    507 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
    508 	usbd_status err;
    509 	int actin;
    510 
    511 	err = generic_control_in(
    512 		sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf), &actin);
    513 	if (err)
    514 		return err;
    515 
    516 	if (rver) *rver = buf[0];
    517 
    518 	return USBD_NORMAL_COMPLETION;
    519 }
    520 
    521 static __inline usbd_status
    522 get_status(struct uchcom_softc *sc, uint8_t *rval)
    523 {
    524 	return read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
    525 }
    526 
    527 static __inline usbd_status
    528 set_dtrrts_10(struct uchcom_softc *sc, uint8_t val)
    529 {
    530 	return write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
    531 }
    532 
    533 static __inline usbd_status
    534 set_dtrrts_20(struct uchcom_softc *sc, uint8_t val)
    535 {
    536 	return generic_control_out(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
    537 }
    538 
    539 
    540 /* ----------------------------------------------------------------------
    541  * middle layer
    542  */
    543 
    544 static int
    545 update_version(struct uchcom_softc *sc)
    546 {
    547 	usbd_status err;
    548 
    549 	err = get_version(sc, &sc->sc_version);
    550 	if (err) {
    551 		device_printf(sc->sc_dev, "cannot get version: %s\n",
    552 		    usbd_errstr(err));
    553 		return EIO;
    554 	}
    555 	DPRINTF(("%s: update_version %d\n", device_xname(sc->sc_dev), sc->sc_version));
    556 
    557 	return 0;
    558 }
    559 
    560 static void
    561 convert_status(struct uchcom_softc *sc, uint8_t cur)
    562 {
    563 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
    564 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
    565 
    566 	cur = ~cur & 0x0F;
    567 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
    568 }
    569 
    570 static int
    571 update_status(struct uchcom_softc *sc)
    572 {
    573 	usbd_status err;
    574 	uint8_t cur;
    575 
    576 	err = get_status(sc, &cur);
    577 	if (err) {
    578 		device_printf(sc->sc_dev,
    579 		    "cannot update status: %s\n", usbd_errstr(err));
    580 		return EIO;
    581 	}
    582 	convert_status(sc, cur);
    583 
    584 	return 0;
    585 }
    586 
    587 
    588 static int
    589 set_dtrrts(struct uchcom_softc *sc, int dtr, int rts)
    590 {
    591 	usbd_status err;
    592 	uint8_t val = 0;
    593 
    594 	if (dtr) val |= UCHCOM_DTR_MASK;
    595 	if (rts) val |= UCHCOM_RTS_MASK;
    596 
    597 	if (sc->sc_version < UCHCOM_VER_20)
    598 		err = set_dtrrts_10(sc, ~val);
    599 	else
    600 		err = set_dtrrts_20(sc, ~val);
    601 
    602 	if (err) {
    603 		device_printf(sc->sc_dev, "cannot set DTR/RTS: %s\n",
    604 		    usbd_errstr(err));
    605 		return EIO;
    606 	}
    607 
    608 	return 0;
    609 }
    610 
    611 static int
    612 set_break(struct uchcom_softc *sc, int onoff)
    613 {
    614 	usbd_status err;
    615 	uint8_t brk, lcr;
    616 
    617 	err = read_reg(sc, UCHCOM_REG_BREAK, &brk, UCHCOM_REG_LCR, &lcr);
    618 	if (err)
    619 		return EIO;
    620 	if (onoff) {
    621 		/* on - clear bits */
    622 		brk &= ~UCHCOM_BREAK_MASK;
    623 		lcr &= ~UCHCOM_LCR_TXE;
    624 	} else {
    625 		/* off - set bits */
    626 		brk |= UCHCOM_BREAK_MASK;
    627 		lcr |= UCHCOM_LCR_TXE;
    628 	}
    629 	err = write_reg(sc, UCHCOM_REG_BREAK, brk, UCHCOM_REG_LCR, lcr);
    630 	if (err)
    631 		return EIO;
    632 
    633 	return 0;
    634 }
    635 
    636 static int
    637 calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
    638 {
    639 	size_t i;
    640 	uint32_t best, div, pre;
    641 	const uint32_t rate4x = rate * 4U;
    642 
    643 	if (rate == 0 || rate > 3000000)
    644 		return -1;
    645 
    646 	pre = __arraycount(rates4x);
    647 	best = UINT32_MAX;
    648 
    649 	for (i = 0; i < __arraycount(rates4x); i++) {
    650 		uint32_t score, try;
    651 		try = rates4x[i] * 2 / rate4x;
    652 		try = (try / 2) + (try & 1);
    653 		if (try < 2)
    654 			continue;
    655 		if (try > 255)
    656 			try = 255;
    657 		score = abs((int)rate4x - rates4x[i] / try);
    658 		if (score < best) {
    659 			best = score;
    660 			pre = i;
    661 			div = try;
    662 		}
    663 	}
    664 
    665 	if (pre >= __arraycount(rates4x))
    666 		return -1;
    667 	if ((rates4x[pre] / div / 4) < (rate * 99 / 100))
    668 		return -1;
    669 	if ((rates4x[pre] / div / 4) > (rate * 101 / 100))
    670 		return -1;
    671 
    672 	dp->dv_prescaler = pre;
    673 	dp->dv_div = (uint8_t)-div;
    674 
    675 	return 0;
    676 }
    677 
    678 static int
    679 set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
    680 {
    681 	usbd_status err;
    682 	struct uchcom_divider dv;
    683 
    684 	if (calc_divider_settings(&dv, rate))
    685 		return EINVAL;
    686 
    687 	if ((err = write_reg(sc,
    688 			     UCHCOM_REG_BPS_PRE,
    689 			     dv.dv_prescaler | UCHCOM_BPS_PRE_IMM,
    690 			     UCHCOM_REG_BPS_DIV, dv.dv_div))) {
    691 		device_printf(sc->sc_dev, "cannot set DTE rate: %s\n",
    692 		    usbd_errstr(err));
    693 		return EIO;
    694 	}
    695 
    696 	return 0;
    697 }
    698 
    699 static int
    700 set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
    701 {
    702 	usbd_status err;
    703 	uint8_t lcr = 0, lcr2 = 0;
    704 
    705 	err = read_reg(sc, UCHCOM_REG_LCR, &lcr, UCHCOM_REG_LCR2, &lcr2);
    706 	if (err) {
    707 		device_printf(sc->sc_dev, "cannot get LCR: %s\n",
    708 		    usbd_errstr(err));
    709 		return EIO;
    710 	}
    711 
    712 	lcr = UCHCOM_LCR_RXE | UCHCOM_LCR_TXE;
    713 
    714 	switch (ISSET(cflag, CSIZE)) {
    715 	case CS5:
    716 		lcr |= UCHCOM_LCR_CS5;
    717 		break;
    718 	case CS6:
    719 		lcr |= UCHCOM_LCR_CS6;
    720 		break;
    721 	case CS7:
    722 		lcr |= UCHCOM_LCR_CS7;
    723 		break;
    724 	case CS8:
    725 		lcr |= UCHCOM_LCR_CS8;
    726 		break;
    727 	}
    728 
    729 	if (ISSET(cflag, PARENB)) {
    730 		lcr |= UCHCOM_LCR_PARENB;
    731 		if (!ISSET(cflag, PARODD))
    732 			lcr |= UCHCOM_LCR_PAREVEN;
    733 	}
    734 
    735 	if (ISSET(cflag, CSTOPB)) {
    736 		lcr |= UCHCOM_LCR_STOPB;
    737 	}
    738 
    739 	err = write_reg(sc, UCHCOM_REG_LCR, lcr, UCHCOM_REG_LCR2, lcr2);
    740 	if (err) {
    741 		device_printf(sc->sc_dev, "cannot set LCR: %s\n",
    742 		    usbd_errstr(err));
    743 		return EIO;
    744 	}
    745 
    746 	return 0;
    747 }
    748 
    749 static int
    750 clear_chip(struct uchcom_softc *sc)
    751 {
    752 	usbd_status err;
    753 
    754 	DPRINTF(("%s: clear\n", device_xname(sc->sc_dev)));
    755 	err = generic_control_out(sc, UCHCOM_REQ_RESET, 0, 0);
    756 	if (err) {
    757 		device_printf(sc->sc_dev, "cannot clear: %s\n",
    758 		    usbd_errstr(err));
    759 		return EIO;
    760 	}
    761 
    762 	return 0;
    763 }
    764 
    765 static int
    766 reset_chip(struct uchcom_softc *sc)
    767 {
    768 	usbd_status err;
    769 
    770 	err = generic_control_out(sc, UCHCOM_REQ_RESET,
    771 	    UCHCOM_RESET_VALUE, UCHCOM_RESET_INDEX);
    772 	if (err)
    773 		goto failed;
    774 
    775 	return 0;
    776 
    777 failed:
    778 	printf("%s: cannot reset: %s\n",
    779 	       device_xname(sc->sc_dev), usbd_errstr(err));
    780 	return EIO;
    781 }
    782 
    783 static int
    784 setup_comm(struct uchcom_softc *sc)
    785 {
    786 	int ret;
    787 
    788 	ret = update_version(sc);
    789 	if (ret)
    790 		return ret;
    791 
    792 	ret = clear_chip(sc);
    793 	if (ret)
    794 		return ret;
    795 
    796 	ret = set_dte_rate(sc, TTYDEF_SPEED);
    797 	if (ret)
    798 		return ret;
    799 
    800 	ret = set_line_control(sc, CS8);
    801 	if (ret)
    802 		return ret;
    803 
    804 	ret = update_status(sc);
    805 	if (ret)
    806 		return ret;
    807 
    808 	ret = reset_chip(sc);
    809 	if (ret)
    810 		return ret;
    811 
    812 	ret = set_dte_rate(sc, TTYDEF_SPEED); /* XXX */
    813 	if (ret)
    814 		return ret;
    815 
    816 	sc->sc_dtr = sc->sc_rts = 1;
    817 	ret = set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    818 	if (ret)
    819 		return ret;
    820 
    821 	return 0;
    822 }
    823 
    824 static int
    825 setup_intr_pipe(struct uchcom_softc *sc)
    826 {
    827 	usbd_status err;
    828 
    829 	if (sc->sc_intr_endpoint != -1 && sc->sc_intr_pipe == NULL) {
    830 		sc->sc_intr_buf = kmem_alloc(sc->sc_intr_size, KM_SLEEP);
    831 		err = usbd_open_pipe_intr(sc->sc_iface,
    832 					  sc->sc_intr_endpoint,
    833 					  USBD_SHORT_XFER_OK,
    834 					  &sc->sc_intr_pipe, sc,
    835 					  sc->sc_intr_buf,
    836 					  sc->sc_intr_size,
    837 					  uchcom_intr, USBD_DEFAULT_INTERVAL);
    838 		if (err) {
    839 			device_printf(sc->sc_dev,
    840 			    "cannot open interrupt pipe: %s\n",
    841 			    usbd_errstr(err));
    842 			return EIO;
    843 		}
    844 	}
    845 	return 0;
    846 }
    847 
    848 static void
    849 close_intr_pipe(struct uchcom_softc *sc)
    850 {
    851 	usbd_status err;
    852 
    853 	if (sc->sc_dying)
    854 		return;
    855 
    856 	if (sc->sc_intr_pipe != NULL) {
    857 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    858 		if (err)
    859 			device_printf(sc->sc_dev,
    860 			    "abort interrupt pipe failed: %s\n",
    861 			    usbd_errstr(err));
    862 		err = usbd_close_pipe(sc->sc_intr_pipe);
    863 		if (err)
    864 			device_printf(sc->sc_dev,
    865 			    "close interrupt pipe failed: %s\n",
    866 			    usbd_errstr(err));
    867 		kmem_free(sc->sc_intr_buf, sc->sc_intr_size);
    868 		sc->sc_intr_pipe = NULL;
    869 	}
    870 }
    871 
    872 
    873 /* ----------------------------------------------------------------------
    874  * methods for ucom
    875  */
    876 void
    877 uchcom_get_status(void *arg, int portno, u_char *rlsr, u_char *rmsr)
    878 {
    879 	struct uchcom_softc *sc = arg;
    880 
    881 	if (sc->sc_dying)
    882 		return;
    883 
    884 	*rlsr = sc->sc_lsr;
    885 	*rmsr = sc->sc_msr;
    886 }
    887 
    888 void
    889 uchcom_set(void *arg, int portno, int reg, int onoff)
    890 {
    891 	struct uchcom_softc *sc = arg;
    892 
    893 	if (sc->sc_dying)
    894 		return;
    895 
    896 	switch (reg) {
    897 	case UCOM_SET_DTR:
    898 		sc->sc_dtr = !!onoff;
    899 		set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    900 		break;
    901 	case UCOM_SET_RTS:
    902 		sc->sc_rts = !!onoff;
    903 		set_dtrrts(sc, sc->sc_dtr, sc->sc_rts);
    904 		break;
    905 	case UCOM_SET_BREAK:
    906 		set_break(sc, onoff);
    907 		break;
    908 	}
    909 }
    910 
    911 int
    912 uchcom_param(void *arg, int portno, struct termios *t)
    913 {
    914 	struct uchcom_softc *sc = arg;
    915 	int ret;
    916 
    917 	if (sc->sc_dying)
    918 		return 0;
    919 
    920 	ret = set_line_control(sc, t->c_cflag);
    921 	if (ret)
    922 		return ret;
    923 
    924 	ret = set_dte_rate(sc, t->c_ospeed);
    925 	if (ret)
    926 		return ret;
    927 
    928 	return 0;
    929 }
    930 
    931 int
    932 uchcom_open(void *arg, int portno)
    933 {
    934 	int ret;
    935 	struct uchcom_softc *sc = arg;
    936 
    937 	if (sc->sc_dying)
    938 		return EIO;
    939 
    940 	ret = setup_intr_pipe(sc);
    941 	if (ret)
    942 		return ret;
    943 
    944 	ret = setup_comm(sc);
    945 	if (ret)
    946 		return ret;
    947 
    948 	return 0;
    949 }
    950 
    951 void
    952 uchcom_close(void *arg, int portno)
    953 {
    954 	struct uchcom_softc *sc = arg;
    955 
    956 	if (sc->sc_dying)
    957 		return;
    958 
    959 	close_intr_pipe(sc);
    960 }
    961 
    962 
    963 /* ----------------------------------------------------------------------
    964  * callback when the modem status is changed.
    965  */
    966 void
    967 uchcom_intr(struct usbd_xfer *xfer, void * priv,
    968 	    usbd_status status)
    969 {
    970 	struct uchcom_softc *sc = priv;
    971 	u_char *buf = sc->sc_intr_buf;
    972 
    973 	if (sc->sc_dying)
    974 		return;
    975 
    976 	if (status != USBD_NORMAL_COMPLETION) {
    977 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    978 			return;
    979 
    980 		DPRINTF(("%s: abnormal status: %s\n",
    981 			 device_xname(sc->sc_dev), usbd_errstr(status)));
    982 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    983 		return;
    984 	}
    985 	DPRINTF(("%s: intr: 0x%02X 0x%02X 0x%02X 0x%02X "
    986 		 "0x%02X 0x%02X 0x%02X 0x%02X\n",
    987 		 device_xname(sc->sc_dev),
    988 		 (unsigned)buf[0], (unsigned)buf[1],
    989 		 (unsigned)buf[2], (unsigned)buf[3],
    990 		 (unsigned)buf[4], (unsigned)buf[5],
    991 		 (unsigned)buf[6], (unsigned)buf[7]));
    992 
    993 	convert_status(sc, buf[UCHCOM_INTR_STAT1]);
    994 	ucom_status_change(device_private(sc->sc_subdev));
    995 }
    996