Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.60
      1 /*	$NetBSD: uplcom.c,v 1.60 2008/02/18 05:24:24 dyoung Exp $	*/
      2 /*
      3  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * General information: http://www.prolific.com.tw/fr_pl2303.htm
     40  * http://www.hitachi-hitec.com/jyouhou/prolific/2303.pdf
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: uplcom.c,v 1.60 2008/02/18 05:24:24 dyoung Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/malloc.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/conf.h>
     52 #include <sys/tty.h>
     53 #include <sys/file.h>
     54 #include <sys/select.h>
     55 #include <sys/proc.h>
     56 #include <sys/device.h>
     57 #include <sys/poll.h>
     58 
     59 #include <dev/usb/usb.h>
     60 #include <dev/usb/usbcdc.h>
     61 
     62 #include <dev/usb/usbdi.h>
     63 #include <dev/usb/usbdi_util.h>
     64 #include <dev/usb/usbdevs.h>
     65 #include <dev/usb/usb_quirks.h>
     66 
     67 #include <dev/usb/ucomvar.h>
     68 
     69 #ifdef UPLCOM_DEBUG
     70 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
     71 int	uplcomdebug = 0;
     72 #else
     73 #define DPRINTFN(n, x)
     74 #endif
     75 #define DPRINTF(x) DPRINTFN(0, x)
     76 
     77 #define	UPLCOM_CONFIG_INDEX	0
     78 #define	UPLCOM_IFACE_INDEX	0
     79 #define	UPLCOM_SECOND_IFACE_INDEX	1
     80 
     81 #define	UPLCOM_SET_REQUEST	0x01
     82 #define	UPLCOM_SET_CRTSCTS_0	0x41
     83 #define	UPLCOM_SET_CRTSCTS_HX	0x61
     84 #define RSAQ_STATUS_DSR		0x02
     85 #define RSAQ_STATUS_DCD		0x01
     86 
     87 enum  pl2303_type {
     88 	UPLCOM_TYPE_0,
     89 	UPLCOM_TYPE_HX,
     90 };
     91 
     92 struct	uplcom_softc {
     93 	USBBASEDEVICE		sc_dev;		/* base device */
     94 	usbd_device_handle	sc_udev;	/* USB device */
     95 	usbd_interface_handle	sc_iface;	/* interface */
     96 	int			sc_iface_number;	/* interface number */
     97 
     98 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
     99 	int			sc_intr_number;	/* interrupt number */
    100 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
    101 	u_char			*sc_intr_buf;	/* interrupt buffer */
    102 	int			sc_isize;
    103 
    104 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
    105 	int			sc_dtr;		/* current DTR state */
    106 	int			sc_rts;		/* current RTS state */
    107 
    108 	device_ptr_t		sc_subdev;	/* ucom device */
    109 
    110 	u_char			sc_dying;	/* disconnecting */
    111 
    112 	u_char			sc_lsr;		/* Local status register */
    113 	u_char			sc_msr;		/* uplcom status register */
    114 
    115 	enum pl2303_type	sc_type;	/* PL2303 chip type */
    116 };
    117 
    118 /*
    119  * These are the maximum number of bytes transferred per frame.
    120  * The output buffer size cannot be increased due to the size encoding.
    121  */
    122 #define UPLCOMIBUFSIZE 256
    123 #define UPLCOMOBUFSIZE 256
    124 
    125 Static	usbd_status uplcom_reset(struct uplcom_softc *);
    126 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
    127 					   usb_cdc_line_state_t *state);
    128 Static	usbd_status uplcom_set_crtscts(struct uplcom_softc *);
    129 Static	void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    130 
    131 Static	void uplcom_set(void *, int, int, int);
    132 Static	void uplcom_dtr(struct uplcom_softc *, int);
    133 Static	void uplcom_rts(struct uplcom_softc *, int);
    134 Static	void uplcom_break(struct uplcom_softc *, int);
    135 Static	void uplcom_set_line_state(struct uplcom_softc *);
    136 Static	void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
    137 #if TODO
    138 Static	int  uplcom_ioctl(void *, int, u_long, void *, int, usb_proc_ptr );
    139 #endif
    140 Static	int  uplcom_param(void *, int, struct termios *);
    141 Static	int  uplcom_open(void *, int);
    142 Static	void uplcom_close(void *, int);
    143 Static usbd_status uplcom_vendor_control_write(usbd_device_handle, u_int16_t, u_int16_t);
    144 
    145 struct	ucom_methods uplcom_methods = {
    146 	uplcom_get_status,
    147 	uplcom_set,
    148 	uplcom_param,
    149 	NULL, /* uplcom_ioctl, TODO */
    150 	uplcom_open,
    151 	uplcom_close,
    152 	NULL,
    153 	NULL,
    154 };
    155 
    156 static const struct uplcom_type {
    157       struct usb_devno uplcom_dev;
    158       int32_t	  release;
    159       enum pl2303_type chiptype;
    160 } uplcom_devs[] = {
    161 	/* I/O DATA USB-RSAQ2 */
    162 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
    163 		-1, UPLCOM_TYPE_0 },
    164 	/* I/O DATA USB-RSAQ3 */
    165 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ3 },
    166 		-1, UPLCOM_TYPE_HX },
    167 	/* I/O DATA USB-RSAQ */
    168 	{ { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
    169 		-1, UPLCOM_TYPE_0 },
    170 	/* I/O DATA USB-RSAQ5 */
    171 	{ { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ5 },
    172 		-1, UPLCOM_TYPE_HX },
    173 	/* PLANEX USB-RS232 URS-03 */
    174 	{ { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
    175 		-1, UPLCOM_TYPE_0 },
    176 	/* TrendNet TU-S9 */
    177 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    178 		0x400, UPLCOM_TYPE_HX },
    179 	/* ST Lab USB-SERIAL-4 */
    180 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    181 		0x300, UPLCOM_TYPE_HX },
    182 	/* IOGEAR/ATEN UC-232A */
    183 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    184 		-1, UPLCOM_TYPE_0 },
    185 	/* SMART Technologies USB to serial */
    186 	{ { USB_VENDOR_PROLIFIC2, USB_PRODUCT_PROLIFIC2_PL2303 },
    187 		-1, UPLCOM_TYPE_HX },
    188 	/* IOGEAR/ATENTRIPPLITE */
    189 	{ { USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
    190 		-1, UPLCOM_TYPE_0 },
    191 	/* ELECOM UC-SGT */
    192 	{ { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
    193 		-1, UPLCOM_TYPE_0 },
    194 	/* ELECOM UC-SGT0 */
    195 	{ { USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT0 },
    196 		-1, UPLCOM_TYPE_0 },
    197 	/* Panasonic 50" Touch Panel */
    198 	{ { USB_VENDOR_PANASONIC, USB_PRODUCT_PANASONIC_TYTP50P6S },
    199 		-1, UPLCOM_TYPE_0 },
    200 	/* RATOC REX-USB60 */
    201 	{ { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
    202 		-1, UPLCOM_TYPE_0 },
    203 	/* TDK USB-PHS Adapter UHA6400 */
    204 	{ { USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
    205 		-1, UPLCOM_TYPE_0 },
    206 	/* TDK USB-PDC Adapter UPA9664 */
    207 	{ { USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
    208 		-1, UPLCOM_TYPE_0 },
    209 	/* Sony Ericsson USB Cable */
    210 	{ { USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
    211 		-1, UPLCOM_TYPE_0 },
    212 	/* SOURCENEXT KeikaiDenwa 8 */
    213 	{ { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
    214 		-1, UPLCOM_TYPE_0 },
    215 	/* SOURCENEXT KeikaiDenwa 8 with charger */
    216 	{ { USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
    217 		-1, UPLCOM_TYPE_0 },
    218 	/* HAL Corporation Crossam2+USB */
    219 	{ { USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
    220 		-1, UPLCOM_TYPE_0 },
    221 	/* Sitecom USB to serial cable */
    222 	{ { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
    223 		-1, UPLCOM_TYPE_0 },
    224 	/* Pharos USB GPS - Microsoft version */
    225 	{ { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303X },
    226 		-1, UPLCOM_TYPE_0 },
    227 	/* Willcom WS002IN (DD) */
    228 	{ { USB_VENDOR_NETINDEX, USB_PRODUCT_NETINDEX_WS002IN },
    229 		-1, UPLCOM_TYPE_HX },
    230 };
    231 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
    232 
    233 int uplcom_match(device_t, struct cfdata *, void *);
    234 void uplcom_attach(device_t, device_t, void *);
    235 void uplcom_childdet(device_t, device_t);
    236 int uplcom_detach(device_t, int);
    237 int uplcom_activate(device_t, enum devact);
    238 extern struct cfdriver uplcom_cd;
    239 CFATTACH_DECL2(uplcom, sizeof(struct uplcom_softc), uplcom_match,
    240     uplcom_attach, uplcom_detach, uplcom_activate, NULL, uplcom_childdet);
    241 
    242 USB_MATCH(uplcom)
    243 {
    244 	USB_MATCH_START(uplcom, uaa);
    245 
    246 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
    247 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    248 }
    249 
    250 USB_ATTACH(uplcom)
    251 {
    252 	USB_ATTACH_START(uplcom, sc, uaa);
    253 	usbd_device_handle dev = uaa->device;
    254 	usb_config_descriptor_t *cdesc;
    255 	usb_interface_descriptor_t *id;
    256 	usb_endpoint_descriptor_t *ed;
    257 	char *devinfop;
    258 	char *devname = USBDEVNAME(sc->sc_dev);
    259 	usbd_status err;
    260 	int i;
    261 	struct ucom_attach_args uca;
    262 
    263 	devinfop = usbd_devinfo_alloc(dev, 0);
    264 	USB_ATTACH_SETUP;
    265 	printf("%s: %s\n", devname, devinfop);
    266 	usbd_devinfo_free(devinfop);
    267 
    268         sc->sc_udev = dev;
    269 
    270 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    271 
    272 	/* initialize endpoints */
    273 	uca.bulkin = uca.bulkout = -1;
    274 	sc->sc_intr_number = -1;
    275 	sc->sc_intr_pipe = NULL;
    276 
    277 	/* Move the device into the configured state. */
    278 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    279 	if (err) {
    280 		printf("\n%s: failed to set configuration, err=%s\n",
    281 			devname, usbd_errstr(err));
    282 		sc->sc_dying = 1;
    283 		USB_ATTACH_ERROR_RETURN;
    284 	}
    285 
    286 	/* determine chip type */
    287 	for (i = 0; uplcom_devs[i].uplcom_dev.ud_vendor != 0; i++) {
    288 		if (uplcom_devs[i].uplcom_dev.ud_vendor == uaa->vendor &&
    289 		    uplcom_devs[i].uplcom_dev.ud_product == uaa->product &&
    290 		    (uplcom_devs[i].release == uaa->release ||
    291 		    uplcom_devs[i].release == 1)) {
    292 			sc->sc_type = uplcom_devs[i].chiptype;
    293 			break;
    294 		}
    295 	}
    296 
    297 #ifdef USB_DEBUG
    298 	/* print the chip type */
    299 	if (sc->sc_type == UPLCOM_TYPE_HX) {
    300 		DPRINTF(("uplcom_attach: chiptype HX\n"));
    301 	} else {
    302 		DPRINTF(("uplcom_attach: chiptype 0\n"));
    303 	}
    304 #endif
    305 
    306 	/* Move the device into the configured state. */
    307 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    308 	if (err) {
    309 		printf("%s: failed to set configuration: %s\n",
    310 			devname, usbd_errstr(err));
    311 		sc->sc_dying = 1;
    312 		USB_ATTACH_ERROR_RETURN;
    313 	}
    314 
    315 	/* get the config descriptor */
    316 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    317 
    318 	if (cdesc == NULL) {
    319 		printf("%s: failed to get configuration descriptor\n",
    320 			USBDEVNAME(sc->sc_dev));
    321 		sc->sc_dying = 1;
    322 		USB_ATTACH_ERROR_RETURN;
    323 	}
    324 
    325 	/* get the (first/common) interface */
    326 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
    327 							&sc->sc_iface);
    328 	if (err) {
    329 		printf("\n%s: failed to get interface, err=%s\n",
    330 			devname, usbd_errstr(err));
    331 		sc->sc_dying = 1;
    332 		USB_ATTACH_ERROR_RETURN;
    333 	}
    334 
    335 	/* Find the interrupt endpoints */
    336 
    337 	id = usbd_get_interface_descriptor(sc->sc_iface);
    338 	sc->sc_iface_number = id->bInterfaceNumber;
    339 
    340 	for (i = 0; i < id->bNumEndpoints; i++) {
    341 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    342 		if (ed == NULL) {
    343 			printf("%s: no endpoint descriptor for %d\n",
    344 				USBDEVNAME(sc->sc_dev), i);
    345 			sc->sc_dying = 1;
    346 			USB_ATTACH_ERROR_RETURN;
    347 		}
    348 
    349 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    350 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    351 			sc->sc_intr_number = ed->bEndpointAddress;
    352 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    353 		}
    354 	}
    355 
    356 	if (sc->sc_intr_number== -1) {
    357 		printf("%s: Could not find interrupt in\n",
    358 			USBDEVNAME(sc->sc_dev));
    359 		sc->sc_dying = 1;
    360 		USB_ATTACH_ERROR_RETURN;
    361 	}
    362 
    363 	/* keep interface for interrupt */
    364 	sc->sc_intr_iface = sc->sc_iface;
    365 
    366 	/*
    367 	 * USB-RSAQ1 has two interface
    368 	 *
    369 	 *  USB-RSAQ1       | USB-RSAQ2
    370  	 * -----------------+-----------------
    371 	 * Interface 0      |Interface 0
    372 	 *  Interrupt(0x81) | Interrupt(0x81)
    373 	 * -----------------+ BulkIN(0x02)
    374 	 * Interface 1	    | BulkOUT(0x83)
    375 	 *   BulkIN(0x02)   |
    376 	 *   BulkOUT(0x83)  |
    377 	 */
    378 	if (cdesc->bNumInterface == 2) {
    379 		err = usbd_device2interface_handle(dev,
    380 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
    381 		if (err) {
    382 			printf("\n%s: failed to get second interface, err=%s\n",
    383 							devname, usbd_errstr(err));
    384 			sc->sc_dying = 1;
    385 			USB_ATTACH_ERROR_RETURN;
    386 		}
    387 	}
    388 
    389 	/* Find the bulk{in,out} endpoints */
    390 
    391 	id = usbd_get_interface_descriptor(sc->sc_iface);
    392 	sc->sc_iface_number = id->bInterfaceNumber;
    393 
    394 	for (i = 0; i < id->bNumEndpoints; i++) {
    395 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    396 		if (ed == NULL) {
    397 			printf("%s: no endpoint descriptor for %d\n",
    398 				USBDEVNAME(sc->sc_dev), i);
    399 			sc->sc_dying = 1;
    400 			USB_ATTACH_ERROR_RETURN;
    401 		}
    402 
    403 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    404 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    405 			uca.bulkin = ed->bEndpointAddress;
    406 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    407 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    408 			uca.bulkout = ed->bEndpointAddress;
    409 		}
    410 	}
    411 
    412 	if (uca.bulkin == -1) {
    413 		printf("%s: Could not find data bulk in\n",
    414 			USBDEVNAME(sc->sc_dev));
    415 		sc->sc_dying = 1;
    416 		USB_ATTACH_ERROR_RETURN;
    417 	}
    418 
    419 	if (uca.bulkout == -1) {
    420 		printf("%s: Could not find data bulk out\n",
    421 			USBDEVNAME(sc->sc_dev));
    422 		sc->sc_dying = 1;
    423 		USB_ATTACH_ERROR_RETURN;
    424 	}
    425 
    426 	sc->sc_dtr = sc->sc_rts = -1;
    427 	uca.portno = UCOM_UNK_PORTNO;
    428 	/* bulkin, bulkout set above */
    429 	uca.ibufsize = UPLCOMIBUFSIZE;
    430 	uca.obufsize = UPLCOMOBUFSIZE;
    431 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    432 	uca.opkthdrlen = 0;
    433 	uca.device = dev;
    434 	uca.iface = sc->sc_iface;
    435 	uca.methods = &uplcom_methods;
    436 	uca.arg = sc;
    437 	uca.info = NULL;
    438 
    439 	err = uplcom_reset(sc);
    440 
    441 	if (err) {
    442 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    443 			usbd_errstr(err));
    444 		sc->sc_dying = 1;
    445 		USB_ATTACH_ERROR_RETURN;
    446 	}
    447 
    448 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    449 			   USBDEV(sc->sc_dev));
    450 
    451 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
    452 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    453 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    454 					    ucomprint, ucomsubmatch);
    455 
    456 	USB_ATTACH_SUCCESS_RETURN;
    457 }
    458 
    459 void
    460 uplcom_childdet(device_t self, device_t child)
    461 {
    462 	struct uplcom_softc *sc = device_private(self);
    463 
    464 	KASSERT(sc->sc_subdev == child);
    465 	sc->sc_subdev = NULL;
    466 }
    467 
    468 USB_DETACH(uplcom)
    469 {
    470 	USB_DETACH_START(uplcom, sc);
    471 	int rv = 0;
    472 
    473 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    474 
    475         if (sc->sc_intr_pipe != NULL) {
    476                 usbd_abort_pipe(sc->sc_intr_pipe);
    477                 usbd_close_pipe(sc->sc_intr_pipe);
    478 		free(sc->sc_intr_buf, M_USBDEV);
    479                 sc->sc_intr_pipe = NULL;
    480         }
    481 
    482 	sc->sc_dying = 1;
    483 	if (sc->sc_subdev != NULL)
    484 		rv = config_detach(sc->sc_subdev, flags);
    485 
    486 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    487 			   USBDEV(sc->sc_dev));
    488 
    489 	return (rv);
    490 }
    491 
    492 int
    493 uplcom_activate(device_t self, enum devact act)
    494 {
    495 	struct uplcom_softc *sc = device_private(self);
    496 	int rv = 0;
    497 
    498 	switch (act) {
    499 	case DVACT_ACTIVATE:
    500 		return (EOPNOTSUPP);
    501 
    502 	case DVACT_DEACTIVATE:
    503 		if (sc->sc_subdev != NULL)
    504 			rv = config_deactivate(sc->sc_subdev);
    505 		sc->sc_dying = 1;
    506 		break;
    507 	}
    508 	return (rv);
    509 }
    510 
    511 usbd_status
    512 uplcom_reset(struct uplcom_softc *sc)
    513 {
    514         usb_device_request_t req;
    515 	usbd_status err;
    516 
    517         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    518         req.bRequest = UPLCOM_SET_REQUEST;
    519         USETW(req.wValue, 0);
    520         USETW(req.wIndex, sc->sc_iface_number);
    521         USETW(req.wLength, 0);
    522 
    523         err = usbd_do_request(sc->sc_udev, &req, 0);
    524 	if (err)
    525 		return (EIO);
    526 
    527 	return (0);
    528 }
    529 
    530 struct pl2303x_init {
    531 	uint8_t		req_type;
    532 	uint8_t		request;
    533 	uint16_t	value;
    534 	uint16_t	index;
    535 	uint16_t	length;
    536 };
    537 
    538 static const struct pl2303x_init pl2303x[] = {
    539 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
    540 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    0, 0 },
    541 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
    542 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
    543 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
    544 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST, 0x0404,    1, 0 },
    545 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8484,    0, 0 },
    546 	{ UT_READ_VENDOR_DEVICE,  UPLCOM_SET_REQUEST, 0x8383,    0, 0 },
    547 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      0,    1, 0 },
    548 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      1,    0, 0 },
    549 	{ UT_WRITE_VENDOR_DEVICE, UPLCOM_SET_REQUEST,      2, 0x44, 0 }
    550 };
    551 #define N_PL2302X_INIT  (sizeof(pl2303x)/sizeof(pl2303x[0]))
    552 
    553 static usbd_status
    554 uplcom_pl2303x_init(struct uplcom_softc *sc)
    555 {
    556 	usb_device_request_t req;
    557 	usbd_status err;
    558 	int i;
    559 
    560 	for (i = 0; i < N_PL2302X_INIT; i++) {
    561 		req.bmRequestType = pl2303x[i].req_type;
    562 		req.bRequest = pl2303x[i].request;
    563 		USETW(req.wValue, pl2303x[i].value);
    564 		USETW(req.wIndex, pl2303x[i].index);
    565 		USETW(req.wLength, pl2303x[i].length);
    566 
    567 		err = usbd_do_request(sc->sc_udev, &req, 0);
    568 		if (err) {
    569 			printf("%s: uplcom_pl2303x_init failed: %s\n",
    570 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    571 			return (EIO);
    572 		}
    573 	}
    574 
    575 	return (0);
    576 }
    577 
    578 void
    579 uplcom_set_line_state(struct uplcom_softc *sc)
    580 {
    581 	usb_device_request_t req;
    582 	int ls;
    583 
    584 	/* make sure we have initialized state for sc_dtr and sc_rts */
    585 	if (sc->sc_dtr == -1)
    586 		sc->sc_dtr = 0;
    587 	if (sc->sc_rts == -1)
    588 		sc->sc_rts = 0;
    589 
    590 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    591 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    592 
    593 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    594 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    595 	USETW(req.wValue, ls);
    596 	USETW(req.wIndex, sc->sc_iface_number);
    597 	USETW(req.wLength, 0);
    598 
    599 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    600 }
    601 
    602 void
    603 uplcom_set(void *addr, int portno, int reg, int onoff)
    604 {
    605 	struct uplcom_softc *sc = addr;
    606 
    607 	switch (reg) {
    608 	case UCOM_SET_DTR:
    609 		uplcom_dtr(sc, onoff);
    610 		break;
    611 	case UCOM_SET_RTS:
    612 		uplcom_rts(sc, onoff);
    613 		break;
    614 	case UCOM_SET_BREAK:
    615 		uplcom_break(sc, onoff);
    616 		break;
    617 	default:
    618 		break;
    619 	}
    620 }
    621 
    622 void
    623 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    624 {
    625 
    626 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
    627 
    628 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
    629 		return;
    630 
    631 	sc->sc_dtr = !!onoff;
    632 
    633 	uplcom_set_line_state(sc);
    634 }
    635 
    636 void
    637 uplcom_rts(struct uplcom_softc *sc, int onoff)
    638 {
    639 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    640 
    641 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
    642 		return;
    643 
    644 	sc->sc_rts = !!onoff;
    645 
    646 	uplcom_set_line_state(sc);
    647 }
    648 
    649 void
    650 uplcom_break(struct uplcom_softc *sc, int onoff)
    651 {
    652 	usb_device_request_t req;
    653 
    654 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    655 
    656 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    657 	req.bRequest = UCDC_SEND_BREAK;
    658 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    659 	USETW(req.wIndex, sc->sc_iface_number);
    660 	USETW(req.wLength, 0);
    661 
    662 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    663 }
    664 
    665 usbd_status
    666 uplcom_set_crtscts(struct uplcom_softc *sc)
    667 {
    668 	usb_device_request_t req;
    669 	usbd_status err;
    670 
    671 	DPRINTF(("uplcom_set_crtscts: on\n"));
    672 
    673 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    674 	req.bRequest = UPLCOM_SET_REQUEST;
    675 	USETW(req.wValue, 0);
    676 	if (sc->sc_type == UPLCOM_TYPE_HX)
    677 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_HX);
    678 	else
    679 		USETW(req.wIndex, UPLCOM_SET_CRTSCTS_0);
    680 	USETW(req.wLength, 0);
    681 
    682 	err = usbd_do_request(sc->sc_udev, &req, 0);
    683 	if (err) {
    684 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
    685 			usbd_errstr(err)));
    686 		return (err);
    687 	}
    688 
    689 	return (USBD_NORMAL_COMPLETION);
    690 }
    691 
    692 usbd_status
    693 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    694 {
    695 	usb_device_request_t req;
    696 	usbd_status err;
    697 
    698 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    699 		UGETDW(state->dwDTERate), state->bCharFormat,
    700 		state->bParityType, state->bDataBits));
    701 
    702 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    703 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    704 		return (USBD_NORMAL_COMPLETION);
    705 	}
    706 
    707 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    708 	req.bRequest = UCDC_SET_LINE_CODING;
    709 	USETW(req.wValue, 0);
    710 	USETW(req.wIndex, sc->sc_iface_number);
    711 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    712 
    713 	err = usbd_do_request(sc->sc_udev, &req, state);
    714 	if (err) {
    715 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    716 			usbd_errstr(err)));
    717 		return (err);
    718 	}
    719 
    720 	sc->sc_line_state = *state;
    721 
    722 	return (USBD_NORMAL_COMPLETION);
    723 }
    724 
    725 int
    726 uplcom_param(void *addr, int portno, struct termios *t)
    727 {
    728 	struct uplcom_softc *sc = addr;
    729 	usbd_status err;
    730 	usb_cdc_line_state_t ls;
    731 
    732 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    733 
    734 	USETDW(ls.dwDTERate, t->c_ospeed);
    735 	if (ISSET(t->c_cflag, CSTOPB))
    736 		ls.bCharFormat = UCDC_STOP_BIT_2;
    737 	else
    738 		ls.bCharFormat = UCDC_STOP_BIT_1;
    739 	if (ISSET(t->c_cflag, PARENB)) {
    740 		if (ISSET(t->c_cflag, PARODD))
    741 			ls.bParityType = UCDC_PARITY_ODD;
    742 		else
    743 			ls.bParityType = UCDC_PARITY_EVEN;
    744 	} else
    745 		ls.bParityType = UCDC_PARITY_NONE;
    746 	switch (ISSET(t->c_cflag, CSIZE)) {
    747 	case CS5:
    748 		ls.bDataBits = 5;
    749 		break;
    750 	case CS6:
    751 		ls.bDataBits = 6;
    752 		break;
    753 	case CS7:
    754 		ls.bDataBits = 7;
    755 		break;
    756 	case CS8:
    757 		ls.bDataBits = 8;
    758 		break;
    759 	}
    760 
    761 	err = uplcom_set_line_coding(sc, &ls);
    762 	if (err) {
    763 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    764 		return (EIO);
    765 	}
    766 
    767 	if (ISSET(t->c_cflag, CRTSCTS))
    768 		uplcom_set_crtscts(sc);
    769 
    770 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
    771 		uplcom_set_line_state(sc);
    772 
    773 	if (err) {
    774 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    775 		return (EIO);
    776 	}
    777 
    778 	return (0);
    779 }
    780 
    781 Static usbd_status
    782 uplcom_vendor_control_write(usbd_device_handle dev, u_int16_t value, u_int16_t index)
    783 {
    784 	usb_device_request_t req;
    785 	usbd_status err;
    786 
    787 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    788 	req.bRequest = UPLCOM_SET_REQUEST;
    789 	USETW(req.wValue, value);
    790 	USETW(req.wIndex, index);
    791 	USETW(req.wLength, 0);
    792 
    793 	err = usbd_do_request(dev, &req, NULL);
    794 
    795 	if (err) {
    796 		DPRINTF(("uplcom_open: vendor write failed, err=%s (%d)\n",
    797 			    usbd_errstr(err), err));
    798 	}
    799 
    800 	return err;
    801 }
    802 
    803 int
    804 uplcom_open(void *addr, int portno)
    805 {
    806 	struct uplcom_softc *sc = addr;
    807 	usbd_status err;
    808 
    809 	if (sc->sc_dying)
    810 		return (EIO);
    811 
    812 	DPRINTF(("uplcom_open: sc=%p\n", sc));
    813 
    814 	/* Some unknown device frobbing. */
    815 	if (sc->sc_type == UPLCOM_TYPE_HX)
    816 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x44);
    817 	else
    818 		uplcom_vendor_control_write(sc->sc_udev, 2, 0x24);
    819 
    820 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    821 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    822 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
    823 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    824 			sc->sc_intr_buf, sc->sc_isize,
    825 			uplcom_intr, USBD_DEFAULT_INTERVAL);
    826 		if (err) {
    827 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    828 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
    829 					return (EIO);
    830 		}
    831 	}
    832 
    833 	if (sc->sc_type == UPLCOM_TYPE_HX)
    834 		return (uplcom_pl2303x_init(sc));
    835 
    836 	return (0);
    837 }
    838 
    839 void
    840 uplcom_close(void *addr, int portno)
    841 {
    842 	struct uplcom_softc *sc = addr;
    843 	int err;
    844 
    845 	if (sc->sc_dying)
    846 		return;
    847 
    848 	DPRINTF(("uplcom_close: close\n"));
    849 
    850 	if (sc->sc_intr_pipe != NULL) {
    851 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    852 		if (err)
    853 			printf("%s: abort interrupt pipe failed: %s\n",
    854 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    855 		err = usbd_close_pipe(sc->sc_intr_pipe);
    856 		if (err)
    857 			printf("%s: close interrupt pipe failed: %s\n",
    858 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    859 		free(sc->sc_intr_buf, M_USBDEV);
    860 		sc->sc_intr_pipe = NULL;
    861 	}
    862 }
    863 
    864 void
    865 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
    866     usbd_status status)
    867 {
    868 	struct uplcom_softc *sc = priv;
    869 	u_char *buf = sc->sc_intr_buf;
    870 	u_char pstatus;
    871 
    872 	if (sc->sc_dying)
    873 		return;
    874 
    875 	if (status != USBD_NORMAL_COMPLETION) {
    876 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    877 			return;
    878 
    879 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    880 			usbd_errstr(status)));
    881 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    882 		return;
    883 	}
    884 
    885 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
    886 
    887 	sc->sc_lsr = sc->sc_msr = 0;
    888 	pstatus = buf[8];
    889 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
    890 		sc->sc_msr |= UMSR_DSR;
    891 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
    892 		sc->sc_msr |= UMSR_DCD;
    893 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
    894 }
    895 
    896 void
    897 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    898 {
    899 	struct uplcom_softc *sc = addr;
    900 
    901 	DPRINTF(("uplcom_get_status:\n"));
    902 
    903 	if (lsr != NULL)
    904 		*lsr = sc->sc_lsr;
    905 	if (msr != NULL)
    906 		*msr = sc->sc_msr;
    907 }
    908 
    909 #if TODO
    910 int
    911 uplcom_ioctl(void *addr, int portno, u_long cmd, void *data, int flag,
    912 	     usb_proc_ptr p)
    913 {
    914 	struct uplcom_softc *sc = addr;
    915 	int error = 0;
    916 
    917 	if (sc->sc_dying)
    918 		return (EIO);
    919 
    920 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
    921 
    922 	switch (cmd) {
    923 	case TIOCNOTTY:
    924 	case TIOCMGET:
    925 	case TIOCMSET:
    926 	case USB_GET_CM_OVER_DATA:
    927 	case USB_SET_CM_OVER_DATA:
    928 		break;
    929 
    930 	default:
    931 		DPRINTF(("uplcom_ioctl: unknown\n"));
    932 		error = ENOTTY;
    933 		break;
    934 	}
    935 
    936 	return (error);
    937 }
    938 #endif
    939