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