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