Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.92
      1 /*	$NetBSD: uplcom.c,v 1.92 2022/07/06 06:00:40 nat Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Ichiro FUKUHARA (ichiro (at) ichiro.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 /*
     33  * General information: http://www.prolific.com.tw/fr_pl2303.htm
     34  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.92 2022/07/06 06:00:40 nat Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_usb.h"
     42 #endif
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/kmem.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/conf.h>
     50 #include <sys/tty.h>
     51 #include <sys/file.h>
     52 #include <sys/select.h>
     53 #include <sys/proc.h>
     54 #include <sys/device.h>
     55 #include <sys/poll.h>
     56 #include <sys/sysctl.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbcdc.h>
     60 
     61 #include <dev/usb/usbdi.h>
     62 #include <dev/usb/usbdi_util.h>
     63 #include <dev/usb/usbdevs.h>
     64 #include <dev/usb/usb_quirks.h>
     65 #include <dev/usb/usbhist.h>
     66 
     67 #include <dev/usb/ucomvar.h>
     68 
     69 #ifdef USB_DEBUG
     70 #ifndef UPLCOM_DEBUG
     71 #define uplcomdebug 0
     72 #else
     73 int	uplcomdebug = 0;
     74 
     75 SYSCTL_SETUP(sysctl_hw_uplcom_setup, "sysctl hw.uplcom setup")
     76 {
     77 	int err;
     78 	const struct sysctlnode *rnode;
     79 	const struct sysctlnode *cnode;
     80 
     81 	err = sysctl_createv(clog, 0, NULL, &rnode,
     82 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "uplcom",
     83 	    SYSCTL_DESCR("uplcom global controls"),
     84 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     85 
     86 	if (err)
     87 		goto fail;
     88 
     89 	/* control debugging printfs */
     90 	err = sysctl_createv(clog, 0, &rnode, &cnode,
     91 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
     92 	    "debug", SYSCTL_DESCR("Enable debugging output"),
     93 	    NULL, 0, &uplcomdebug, sizeof(uplcomdebug), CTL_CREATE, CTL_EOL);
     94 	if (err)
     95 		goto fail;
     96 
     97 	return;
     98 fail:
     99 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    100 }
    101 
    102 #endif /* UCOM_DEBUG */
    103 #endif /* USB_DEBUG */
    104 
    105 
    106 #define DPRINTF(FMT,A,B,C,D)    USBHIST_LOGN(uplcomdebug,1,FMT,A,B,C,D)
    107 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(uplcomdebug,N,FMT,A,B,C,D)
    108 #define UPLCOMHIST_FUNC()       USBHIST_FUNC()
    109 #define UPLCOMHIST_CALLED(name) USBHIST_CALLED(uplcomdebug)
    110 
    111 #define	UPLCOM_CONFIG_INDEX	0
    112 #define	UPLCOM_IFACE_INDEX	0
    113 #define	UPLCOM_SECOND_IFACE_INDEX	1
    114 
    115 #define	UPLCOM_SET_REQUEST		0x01
    116 #define	UPLCOM_SET_CRTSCTS_0		0x41
    117 #define	UPLCOM_SET_CRTSCTS_HX		0x61
    118 
    119 #define	UPLCOM_N_SERIAL_CTS		0x80
    120 
    121 #define UPLCOM_HXN_SET_REQUEST		0x80
    122 #define UPLCOM_HXN_SET_CRTSCTS_REG	0x0A
    123 #define UPLCOM_HXN_SET_CRTSCTS		0xFA
    124 #define UPLCOM_HX_STATUS_REG		0x8080
    125 
    126 enum  pl2303_type {
    127 	UPLCOM_TYPE_0,	/* we use this for all non-HX variants */
    128 	UPLCOM_TYPE_HX,
    129 	UPLCOM_TYPE_HXN,
    130 };
    131 
    132 struct	uplcom_softc {
    133 	device_t		sc_dev;		/* base device */
    134 	struct usbd_device *	sc_udev;	/* USB device */
    135 	struct usbd_interface *	sc_iface;	/* interface */
    136 	int			sc_iface_number;	/* interface number */
    137 
    138 	struct usbd_interface *	sc_intr_iface;	/* interrupt interface */
    139 	int			sc_intr_number;	/* interrupt number */
    140 	struct usbd_pipe *	sc_intr_pipe;	/* interrupt pipe */
    141 	u_char			*sc_intr_buf;	/* interrupt buffer */
    142 	int			sc_isize;
    143 
    144 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
    145 	int			sc_dtr;		/* current DTR state */
    146 	int			sc_rts;		/* current RTS state */
    147 
    148 	device_t		sc_subdev;	/* ucom device */
    149 
    150 	bool			sc_dying;	/* disconnecting */
    151 
    152 	u_char			sc_lsr;		/* Local status register */
    153 	u_char			sc_msr;		/* uplcom status register */
    154 
    155 	enum pl2303_type	sc_type;	/* PL2303 chip type */
    156 };
    157 
    158 /*
    159  * These are the maximum number of bytes transferred per frame.
    160  * The output buffer size cannot be increased due to the size encoding.
    161  */
    162 #define UPLCOMIBUFSIZE 256
    163 #define UPLCOMOBUFSIZE 256
    164 
    165 static	usbd_status uplcom_reset(struct uplcom_softc *);
    166 static	usbd_status uplcom_set_line_coding(struct uplcom_softc *,
    167 					   usb_cdc_line_state_t *);
    168 static	usbd_status uplcom_set_crtscts(struct uplcom_softc *);
    169 static	void uplcom_intr(struct usbd_xfer *, void *, usbd_status);
    170 
    171 static	void uplcom_set(void *, int, int, int);
    172 static	void uplcom_dtr(struct uplcom_softc *, int);
    173 static	void uplcom_rts(struct uplcom_softc *, int);
    174 static	void uplcom_break(struct uplcom_softc *, int);
    175 static	void uplcom_set_line_state(struct uplcom_softc *);
    176 static	void uplcom_get_status(void *, int, u_char *, u_char *);
    177 #if TODO
    178 static	int  uplcom_ioctl(void *, int, u_long, void *, int, proc_t *);
    179 #endif
    180 static	int  uplcom_param(void *, int, struct termios *);
    181 static	int  uplcom_open(void *, int);
    182 static	void uplcom_close(void *, int);
    183 static usbd_status uplcom_vendor_control_write(struct usbd_device *, uint16_t, uint16_t);
    184 static void uplcom_close_pipe(struct uplcom_softc *);
    185 
    186 static const struct	ucom_methods uplcom_methods = {
    187 	.ucom_get_status = uplcom_get_status,
    188 	.ucom_set = uplcom_set,
    189 	.ucom_param = uplcom_param,
    190 	.ucom_ioctl = NULL,	/* TODO */
    191 	.ucom_open = uplcom_open,
    192 	.ucom_close = uplcom_close,
    193 };
    194 
    195 static const struct usb_devno uplcom_devs[] = {
    196 	/* I/O DATA USB-RSAQ2 */
    197 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
    198 	/* I/O DATA USB-RSAQ3 */
    199 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
    200 	/* I/O DATA USB-RSAQ */
    201 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
    202 	/* I/O DATA USB-RSAQ5 */
    203 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
    204 	/* PLANEX USB-RS232 URS-03 */
    205 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
    206 	/* various */
    207 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    208 	/* SMART Technologies USB to serial */
    209 	{ USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
    210 	/* IOGEAR/ATENTRIPPLITE */
    211 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
    212 	/* ELECOM UC-SGT */
    213 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
    214 	/* ELECOM UC-SGT0 */
    215 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
    216 	/* Panasonic 50" Touch Panel */
    217 	{ USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
    218 	/* RATOC REX-USB60 */
    219 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
    220 	/* TDK USB-PHS Adapter UHA6400 */
    221 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
    222 	/* TDK USB-PDC Adapter UPA9664 */
    223 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
    224 	/* Sony Ericsson USB Cable */
    225 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
    226 	/* SOURCENEXT KeikaiDenwa 8 */
    227 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
    228 	/* SOURCENEXT KeikaiDenwa 8 with charger */
    229 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
    230 	/* HAL Corporation Crossam2+USB */
    231 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
    232 	/* Sitecom USB to serial cable */
    233 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
    234 	/* Pharos USB GPS - Microsoft version */
    235 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
    236 	/* Willcom WS002IN (DD) */
    237 	{ USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
    238 	/* COREGA CG-USBRS232R */
    239 	{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGUSBRS232R },
    240 	/* Sharp CE-175TU (USB to Zaurus option port 15 adapter) */
    241 	{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_CE175TU },
    242 	/* Various */
    243 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GB },
    244 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GC },
    245 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GE },
    246 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GL },
    247 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GS },
    248 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303GT },
    249 };
    250 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
    251 
    252 static int uplcom_match(device_t, cfdata_t, void *);
    253 static void uplcom_attach(device_t, device_t, void *);
    254 static void uplcom_childdet(device_t, device_t);
    255 static int uplcom_detach(device_t, int);
    256 
    257 CFATTACH_DECL2_NEW(uplcom, sizeof(struct uplcom_softc), uplcom_match,
    258     uplcom_attach, uplcom_detach, NULL, NULL, uplcom_childdet);
    259 
    260 static int
    261 uplcom_match(device_t parent, cfdata_t match, void *aux)
    262 {
    263 	struct usb_attach_arg *uaa = aux;
    264 
    265 	return uplcom_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    266 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    267 }
    268 
    269 static void
    270 uplcom_attach(device_t parent, device_t self, void *aux)
    271 {
    272 	struct uplcom_softc *sc = device_private(self);
    273 	struct usb_attach_arg *uaa = aux;
    274 	struct usbd_device *dev = uaa->uaa_device;
    275 	usb_device_descriptor_t *ddesc;
    276 	usb_config_descriptor_t *cdesc;
    277 	usb_interface_descriptor_t *id;
    278 	usb_endpoint_descriptor_t *ed;
    279 	usb_device_request_t req;
    280 	char *devinfop;
    281 	const char *devname = device_xname(self);
    282 	usbd_status err;
    283 	uint8_t val;
    284 	int i;
    285 	struct ucom_attach_args ucaa;
    286 
    287 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    288 	DPRINTF("sc=%#jx", (uintptr_t)sc, 0, 0, 0);
    289 
    290 	sc->sc_dev = self;
    291 	sc->sc_dying = false;
    292 
    293 	aprint_naive("\n");
    294 	aprint_normal("\n");
    295 
    296 	devinfop = usbd_devinfo_alloc(dev, 0);
    297 	aprint_normal_dev(self, "%s\n", devinfop);
    298 	usbd_devinfo_free(devinfop);
    299 
    300 	sc->sc_udev = dev;
    301 
    302 	/* initialize endpoints */
    303 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    304 	sc->sc_intr_number = -1;
    305 	sc->sc_intr_pipe = NULL;
    306 
    307 	/* Move the device into the configured state. */
    308 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    309 	if (err) {
    310 		aprint_error("\n%s: failed to set configuration, err=%s\n",
    311 			devname, usbd_errstr(err));
    312 		sc->sc_dying = true;
    313 		return;
    314 	}
    315 
    316 	/* determine chip type */
    317 	ddesc = usbd_get_device_descriptor(dev);
    318 	if (ddesc->bDeviceClass != UDCLASS_COMM &&
    319 	    ddesc->bMaxPacketSize == 0x40) {
    320 		sc->sc_type = UPLCOM_TYPE_HX;
    321 	}
    322 
    323 	if (sc->sc_type == UPLCOM_TYPE_HX) {
    324 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    325 		req.bRequest = UPLCOM_SET_REQUEST;
    326 		USETW(req.wValue, UPLCOM_HX_STATUS_REG);
    327 		USETW(req.wIndex, sc->sc_iface_number);
    328 		USETW(req.wLength, 1);
    329 
    330 		err = usbd_do_request(sc->sc_udev, &req, &val);
    331 		if (err)
    332 			sc->sc_type = UPLCOM_TYPE_HXN;
    333 	}
    334 
    335 #ifdef UPLCOM_DEBUG
    336 	/* print the chip type */
    337 	if (sc->sc_type == UPLCOM_TYPE_HXN) {
    338 		DPRINTF("chiptype HXN", 0, 0, 0, 0);
    339 	else if (sc->sc_type == UPLCOM_TYPE_HX) {
    340 		DPRINTF("chiptype HX", 0, 0, 0, 0);
    341 	} else {
    342 		DPRINTF("chiptype 0", 0, 0, 0, 0);
    343 	}
    344 #endif
    345 
    346 	/* Move the device into the configured state. */
    347 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    348 	if (err) {
    349 		aprint_error_dev(self, "failed to set configuration: %s\n",
    350 		    usbd_errstr(err));
    351 		sc->sc_dying = true;
    352 		return;
    353 	}
    354 
    355 	/* get the config descriptor */
    356 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    357 
    358 	if (cdesc == NULL) {
    359 		aprint_error_dev(self,
    360 		    "failed to get configuration descriptor\n");
    361 		sc->sc_dying = true;
    362 		return;
    363 	}
    364 
    365 	/* get the (first/common) interface */
    366 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
    367 							&sc->sc_iface);
    368 	if (err) {
    369 		aprint_error("\n%s: failed to get interface, err=%s\n",
    370 			devname, usbd_errstr(err));
    371 		sc->sc_dying = true;
    372 		return;
    373 	}
    374 
    375 	/* Find the interrupt endpoints */
    376 
    377 	id = usbd_get_interface_descriptor(sc->sc_iface);
    378 	sc->sc_iface_number = id->bInterfaceNumber;
    379 
    380 	for (i = 0; i < id->bNumEndpoints; i++) {
    381 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    382 		if (ed == NULL) {
    383 			aprint_error_dev(self,
    384 			    "no endpoint descriptor for %d\n", i);
    385 			sc->sc_dying = true;
    386 			return;
    387 		}
    388 
    389 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    390 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    391 			sc->sc_intr_number = ed->bEndpointAddress;
    392 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    393 		}
    394 	}
    395 
    396 	if (sc->sc_intr_number== -1) {
    397 		aprint_error_dev(self, "Could not find interrupt in\n");
    398 		sc->sc_dying = true;
    399 		return;
    400 	}
    401 
    402 	/* keep interface for interrupt */
    403 	sc->sc_intr_iface = sc->sc_iface;
    404 
    405 	/*
    406 	 * USB-RSAQ1 has two interface
    407 	 *
    408 	 *  USB-RSAQ1       | USB-RSAQ2
    409 	 * -----------------+-----------------
    410 	 * Interface 0      |Interface 0
    411 	 *  Interrupt(0x81) | Interrupt(0x81)
    412 	 * -----------------+ BulkIN(0x02)
    413 	 * Interface 1	    | BulkOUT(0x83)
    414 	 *   BulkIN(0x02)   |
    415 	 *   BulkOUT(0x83)  |
    416 	 */
    417 	if (cdesc->bNumInterface == 2) {
    418 		err = usbd_device2interface_handle(dev,
    419 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
    420 		if (err) {
    421 			aprint_error("\n%s: failed to get second interface, "
    422 			    "err=%s\n", devname, usbd_errstr(err));
    423 			sc->sc_dying = true;
    424 			return;
    425 		}
    426 	}
    427 
    428 	/* Find the bulk{in,out} endpoints */
    429 
    430 	id = usbd_get_interface_descriptor(sc->sc_iface);
    431 	sc->sc_iface_number = id->bInterfaceNumber;
    432 
    433 	for (i = 0; i < id->bNumEndpoints; i++) {
    434 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    435 		if (ed == NULL) {
    436 			aprint_error_dev(self,
    437 			    "no endpoint descriptor for %d\n", i);
    438 			sc->sc_dying = true;
    439 			return;
    440 		}
    441 
    442 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    443 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    444 			ucaa.ucaa_bulkin = ed->bEndpointAddress;
    445 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    446 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    447 			ucaa.ucaa_bulkout = ed->bEndpointAddress;
    448 		}
    449 	}
    450 
    451 	if (ucaa.ucaa_bulkin == -1) {
    452 		aprint_error_dev(self, "Could not find data bulk in\n");
    453 		sc->sc_dying = true;
    454 		return;
    455 	}
    456 
    457 	if (ucaa.ucaa_bulkout == -1) {
    458 		aprint_error_dev(self, "Could not find data bulk out\n");
    459 		sc->sc_dying = true;
    460 		return;
    461 	}
    462 
    463 	sc->sc_dtr = sc->sc_rts = -1;
    464 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
    465 	/* ucaa_bulkin, ucaa_bulkout set above */
    466 	ucaa.ucaa_ibufsize = UPLCOMIBUFSIZE;
    467 	ucaa.ucaa_obufsize = UPLCOMOBUFSIZE;
    468 	ucaa.ucaa_ibufsizepad = UPLCOMIBUFSIZE;
    469 	ucaa.ucaa_opkthdrlen = 0;
    470 	ucaa.ucaa_device = dev;
    471 	ucaa.ucaa_iface = sc->sc_iface;
    472 	ucaa.ucaa_methods = &uplcom_methods;
    473 	ucaa.ucaa_arg = sc;
    474 	ucaa.ucaa_info = NULL;
    475 
    476 	err = uplcom_reset(sc);
    477 
    478 	if (err) {
    479 		aprint_error_dev(self, "reset failed, %s\n", usbd_errstr(err));
    480 		sc->sc_dying = true;
    481 		return;
    482 	}
    483 
    484 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    485 
    486 	DPRINTF("in=%#jx out=%#jx intr=%#jx",
    487 	    ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number, 0);
    488 	sc->sc_subdev = config_found(self, &ucaa, ucomprint,
    489 	    CFARGS(.submatch = ucomsubmatch));
    490 
    491 	if (!pmf_device_register(self, NULL, NULL))
    492 		aprint_error_dev(self, "couldn't establish power handler\n");
    493 
    494 	return;
    495 }
    496 
    497 static void
    498 uplcom_childdet(device_t self, device_t child)
    499 {
    500 	struct uplcom_softc *sc = device_private(self);
    501 
    502 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    503 
    504 	KASSERT(sc->sc_subdev == child);
    505 	sc->sc_subdev = NULL;
    506 }
    507 
    508 static void
    509 uplcom_close_pipe(struct uplcom_softc *sc)
    510 {
    511 
    512 	if (sc->sc_intr_pipe != NULL) {
    513 		usbd_abort_pipe(sc->sc_intr_pipe);
    514 		usbd_close_pipe(sc->sc_intr_pipe);
    515 		sc->sc_intr_pipe = NULL;
    516 	}
    517 	if (sc->sc_intr_buf != NULL) {
    518 		kmem_free(sc->sc_intr_buf, sc->sc_isize);
    519 		sc->sc_intr_buf = NULL;
    520 	}
    521 }
    522 
    523 static int
    524 uplcom_detach(device_t self, int flags)
    525 {
    526 	struct uplcom_softc *sc = device_private(self);
    527 	int rv = 0;
    528 
    529 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    530 	DPRINTF("sc=%#jx flags=%jd", (uintptr_t)sc, flags, 0, 0);
    531 
    532 	sc->sc_dying = true;
    533 
    534 	uplcom_close_pipe(sc);
    535 
    536 	if (sc->sc_subdev != NULL) {
    537 		rv = config_detach(sc->sc_subdev, flags);
    538 		sc->sc_subdev = NULL;
    539 	}
    540 
    541 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    542 
    543 	pmf_device_deregister(self);
    544 
    545 	return rv;
    546 }
    547 
    548 usbd_status
    549 uplcom_reset(struct uplcom_softc *sc)
    550 {
    551 	usb_device_request_t req;
    552 	usbd_status err;
    553 
    554 	if (sc->sc_type == UPLCOM_TYPE_HXN)
    555 		return 0;
    556 
    557 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    558 	req.bRequest = UPLCOM_SET_REQUEST;
    559 	USETW(req.wValue, 0);
    560 	USETW(req.wIndex, sc->sc_iface_number);
    561 	USETW(req.wLength, 0);
    562 
    563 	err = usbd_do_request(sc->sc_udev, &req, 0);
    564 	if (err)
    565 		return EIO;
    566 
    567 	return 0;
    568 }
    569 
    570 struct pl2303x_init {
    571 	uint8_t		req_type;
    572 	uint8_t		request;
    573 	uint16_t	value;
    574 	uint16_t	index;
    575 };
    576 
    577 static const struct pl2303x_init pl2303x[] = {
    578 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0 },
    579 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    0 },
    580 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0 },
    581 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0 },
    582 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0 },
    583 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    1 },
    584 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0 },
    585 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0 },
    586 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      0,    1 },
    587 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      1,    0 },
    588 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      2, 0x44 }
    589 };
    590 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
    591 
    592 static usbd_status
    593 uplcom_pl2303x_init(struct uplcom_softc *sc)
    594 {
    595 	usb_device_request_t req;
    596 	usbd_status err;
    597 	int i;
    598 
    599 	for (i = 0; i < N_PL2302X_INIT; i++) {
    600 		char buf[1];
    601 		void *b;
    602 
    603 		req.bmRequestType = pl2303x[i].req_type;
    604 		req.bRequest = pl2303x[i].request;
    605 		USETW(req.wValue, pl2303x[i].value);
    606 		USETW(req.wIndex, pl2303x[i].index);
    607 		if (UT_GET_DIR(req.bmRequestType) == UT_READ) {
    608 			b = buf;
    609 			USETW(req.wLength, sizeof(buf));
    610 		} else {
    611 			b = NULL;
    612 			USETW(req.wLength, 0);
    613 		}
    614 
    615 		err = usbd_do_request(sc->sc_udev, &req, b);
    616 		if (err) {
    617 			aprint_error_dev(sc->sc_dev,
    618 			    "uplcom_pl2303x_init failed: %s\n",
    619 			    usbd_errstr(err));
    620 			return EIO;
    621 		}
    622 	}
    623 
    624 	return 0;
    625 }
    626 
    627 static void
    628 uplcom_set_line_state(struct uplcom_softc *sc)
    629 {
    630 	usb_device_request_t req;
    631 	int ls;
    632 
    633 	/* make sure we have initialized state for sc_dtr and sc_rts */
    634 	if (sc->sc_dtr == -1)
    635 		sc->sc_dtr = 0;
    636 	if (sc->sc_rts == -1)
    637 		sc->sc_rts = 0;
    638 
    639 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    640 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    641 
    642 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    643 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    644 	USETW(req.wValue, ls);
    645 	USETW(req.wIndex, sc->sc_iface_number);
    646 	USETW(req.wLength, 0);
    647 
    648 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    649 }
    650 
    651 static void
    652 uplcom_set(void *addr, int portno, int reg, int onoff)
    653 {
    654 	struct uplcom_softc *sc = addr;
    655 
    656 	if (sc->sc_dying)
    657 		return;
    658 
    659 	switch (reg) {
    660 	case UCOM_SET_DTR:
    661 		uplcom_dtr(sc, onoff);
    662 		break;
    663 	case UCOM_SET_RTS:
    664 		uplcom_rts(sc, onoff);
    665 		break;
    666 	case UCOM_SET_BREAK:
    667 		uplcom_break(sc, onoff);
    668 		break;
    669 	default:
    670 		break;
    671 	}
    672 }
    673 
    674 static void
    675 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    676 {
    677 
    678 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    679 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
    680 
    681 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
    682 		return;
    683 
    684 	sc->sc_dtr = !!onoff;
    685 
    686 	uplcom_set_line_state(sc);
    687 }
    688 
    689 static void
    690 uplcom_rts(struct uplcom_softc *sc, int onoff)
    691 {
    692 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    693 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
    694 
    695 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
    696 		return;
    697 
    698 	sc->sc_rts = !!onoff;
    699 
    700 	uplcom_set_line_state(sc);
    701 }
    702 
    703 static void
    704 uplcom_break(struct uplcom_softc *sc, int onoff)
    705 {
    706 	usb_device_request_t req;
    707 
    708 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    709 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
    710 
    711 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    712 	req.bRequest = UCDC_SEND_BREAK;
    713 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    714 	USETW(req.wIndex, sc->sc_iface_number);
    715 	USETW(req.wLength, 0);
    716 
    717 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    718 }
    719 
    720 static usbd_status
    721 uplcom_set_crtscts(struct uplcom_softc *sc)
    722 {
    723 	usb_device_request_t req;
    724 	usbd_status err;
    725 
    726 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    727 
    728 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    729 	if (sc->sc_type == UPLCOM_TYPE_HXN) {
    730 		req.bRequest = UPLCOM_HXN_SET_REQUEST;
    731 		USETW(req.wValue, UPLCOM_HXN_SET_CRTSCTS_REG);
    732 	} else {
    733 		req.bRequest = UPLCOM_SET_REQUEST;
    734 		USETW(req.wValue, 0);
    735 	}
    736 
    737 	if (sc->sc_type == UPLCOM_TYPE_HXN)
    738 		USETW(req.wIndex, UPLCOM_HXN_SET_CRTSCTS);
    739 	else if (sc->sc_type == UPLCOM_TYPE_HX)
    740 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
    741 	else
    742 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
    743 	USETW(req.wLength, 0);
    744 
    745 	err = usbd_do_request(sc->sc_udev, &req, 0);
    746 	if (err) {
    747 		DPRINTF("failed, err=%jd", err, 0, 0, 0);
    748 		return err;
    749 	}
    750 
    751 	return USBD_NORMAL_COMPLETION;
    752 }
    753 
    754 static usbd_status
    755 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    756 {
    757 	usb_device_request_t req;
    758 	usbd_status err;
    759 
    760 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    761 
    762 	DPRINTF("rate=%jd fmt=%jd parity=%jd bits=%jd",
    763 		UGETDW(state->dwDTERate), state->bCharFormat,
    764 		state->bParityType, state->bDataBits);
    765 
    766 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    767 		DPRINTF("already set", 0, 0, 0, 0);
    768 		return USBD_NORMAL_COMPLETION;
    769 	}
    770 
    771 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    772 	req.bRequest = UCDC_SET_LINE_CODING;
    773 	USETW(req.wValue, 0);
    774 	USETW(req.wIndex, sc->sc_iface_number);
    775 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    776 
    777 	err = usbd_do_request(sc->sc_udev, &req, state);
    778 	if (err) {
    779 		DPRINTF("failed, err=%ju", err, 0, 0, 0);
    780 		return err;
    781 	}
    782 
    783 	sc->sc_line_state = *state;
    784 
    785 	return USBD_NORMAL_COMPLETION;
    786 }
    787 
    788 static int
    789 uplcom_param(void *addr, int portno, struct termios *t)
    790 {
    791 	struct uplcom_softc *sc = addr;
    792 	usbd_status err;
    793 	usb_cdc_line_state_t ls;
    794 
    795 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    796 	DPRINTF("sc=%#jx", (uintptr_t)sc, 0, 0, 0);
    797 
    798 	if (sc->sc_dying)
    799 		return EIO;
    800 
    801 	USETDW(ls.dwDTERate, t->c_ospeed);
    802 	if (ISSET(t->c_cflag, CSTOPB))
    803 		ls.bCharFormat = UCDC_STOP_BIT_2;
    804 	else
    805 		ls.bCharFormat = UCDC_STOP_BIT_1;
    806 	if (ISSET(t->c_cflag, PARENB)) {
    807 		if (ISSET(t->c_cflag, PARODD))
    808 			ls.bParityType = UCDC_PARITY_ODD;
    809 		else
    810 			ls.bParityType = UCDC_PARITY_EVEN;
    811 	} else
    812 		ls.bParityType = UCDC_PARITY_NONE;
    813 	switch (ISSET(t->c_cflag, CSIZE)) {
    814 	case CS5:
    815 		ls.bDataBits = 5;
    816 		break;
    817 	case CS6:
    818 		ls.bDataBits = 6;
    819 		break;
    820 	case CS7:
    821 		ls.bDataBits = 7;
    822 		break;
    823 	case CS8:
    824 		ls.bDataBits = 8;
    825 		break;
    826 	}
    827 
    828 	err = uplcom_set_line_coding(sc, &ls);
    829 	if (err) {
    830 		DPRINTF("err=%jd", err, 0, 0, 0);
    831 		return EIO;
    832 	}
    833 
    834 	if (ISSET(t->c_cflag, CRTSCTS))
    835 		uplcom_set_crtscts(sc);
    836 
    837 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
    838 		uplcom_set_line_state(sc);
    839 
    840 	if (err) {
    841 		DPRINTF("err=%jd", err, 0, 0, 0);
    842 		return EIO;
    843 	}
    844 
    845 	return 0;
    846 }
    847 
    848 static usbd_status
    849 uplcom_vendor_control_write(struct usbd_device *dev, uint16_t value,
    850     uint16_t index)
    851 {
    852 	usb_device_request_t req;
    853 	usbd_status err;
    854 
    855 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    856 
    857 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    858 	req.bRequest = UPLCOM_SET_REQUEST;
    859 	USETW(req.wValue, value);
    860 	USETW(req.wIndex, index);
    861 	USETW(req.wLength, 0);
    862 
    863 	err = usbd_do_request(dev, &req, NULL);
    864 
    865 	if (err) {
    866 		DPRINTF("vendor write failed, err=%jd", err, 0, 0, 0);
    867 	}
    868 
    869 	return err;
    870 }
    871 
    872 static int
    873 uplcom_open(void *addr, int portno)
    874 {
    875 	struct uplcom_softc *sc = addr;
    876 	usbd_status err = 0;
    877 
    878 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    879 	DPRINTF("sc=%#jx", (uintptr_t)sc, 0, 0, 0);
    880 
    881 	if (sc->sc_dying)
    882 		return EIO;
    883 
    884 	/* Some unknown device frobbing. */
    885 	if (sc->sc_type == UPLCOM_TYPE_HX)
    886 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
    887 	else
    888 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
    889 
    890 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    891 		sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    892 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
    893 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    894 			sc->sc_intr_buf, sc->sc_isize,
    895 			uplcom_intr, USBD_DEFAULT_INTERVAL);
    896 		if (err) {
    897 			DPRINTF("cannot open interrupt pipe (addr %jd)",
    898 				sc->sc_intr_number, 0, 0, 0);
    899 		}
    900 	}
    901 
    902 	if (err == 0 && sc->sc_type == UPLCOM_TYPE_HX)
    903 		err = uplcom_pl2303x_init(sc);
    904 
    905 	return err;
    906 }
    907 
    908 static void
    909 uplcom_close(void *addr, int portno)
    910 {
    911 	struct uplcom_softc *sc = addr;
    912 
    913 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    914 	DPRINTF("sc=%#jx", (uintptr_t)sc, 0, 0, 0);
    915 
    916 	if (sc->sc_dying)
    917 		return;
    918 
    919 	uplcom_close_pipe(sc);
    920 }
    921 
    922 static void
    923 uplcom_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
    924 {
    925 	struct uplcom_softc *sc = priv;
    926 	u_char *buf = sc->sc_intr_buf;
    927 	u_char pstatus;
    928 
    929 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    930 
    931 	if (sc->sc_dying)
    932 		return;
    933 
    934 	if (status != USBD_NORMAL_COMPLETION) {
    935 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    936 			return;
    937 
    938 		DPRINTF("abnormal status: %ju", status, 0, 0, 0);
    939 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    940 		return;
    941 	}
    942 
    943 	DPRINTF("uplcom status = %02jx", buf[8], 0, 0, 0);
    944 
    945 	sc->sc_lsr = sc->sc_msr = 0;
    946 	pstatus = buf[8];
    947 	if (ISSET(pstatus, UPLCOM_N_SERIAL_CTS))
    948 		sc->sc_msr |= UMSR_CTS;
    949 	if (ISSET(pstatus, UCDC_N_SERIAL_RI))
    950 		sc->sc_msr |= UMSR_RI;
    951 	if (ISSET(pstatus, UCDC_N_SERIAL_DSR))
    952 		sc->sc_msr |= UMSR_DSR;
    953 	if (ISSET(pstatus, UCDC_N_SERIAL_DCD))
    954 		sc->sc_msr |= UMSR_DCD;
    955 	ucom_status_change(device_private(sc->sc_subdev));
    956 }
    957 
    958 static void
    959 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    960 {
    961 	struct uplcom_softc *sc = addr;
    962 
    963 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    964 
    965 	if (sc->sc_dying)
    966 		return;
    967 
    968 	*lsr = sc->sc_lsr;
    969 	*msr = sc->sc_msr;
    970 }
    971 
    972 #if TODO
    973 static int
    974 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
    975 	     proc_t *p)
    976 {
    977 	struct uplcom_softc *sc = addr;
    978 	int error = 0;
    979 
    980 	UPLCOMHIST_FUNC(); UPLCOMHIST_CALLED();
    981 
    982 	if (sc->sc_dying)
    983 		return EIO;
    984 
    985 	DPRINTF("cmd=0x%08lx", cmd, 0, 0, 0);
    986 
    987 	switch (cmd) {
    988 	case TIOCNOTTY:
    989 	case TIOCMGET:
    990 	case TIOCMSET:
    991 	case USB_GET_CM_OVER_DATA:
    992 	case USB_SET_CM_OVER_DATA:
    993 		break;
    994 
    995 	default:
    996 		DPRINTF("unknown", 0, 0, 0, 0);
    997 		error = ENOTTY;
    998 		break;
    999 	}
   1000 
   1001 	return error;
   1002 }
   1003 #endif
   1004