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