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