Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.40
      1 /*	$NetBSD: uplcom.c,v 1.40 2004/10/22 09:34:55 augustss 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.40 2004/10/22 09:34:55 augustss 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/usbdevs.h>
     68 #include <dev/usb/ucomvar.h>
     69 
     70 #ifdef UPLCOM_DEBUG
     71 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
     72 int	uplcomdebug = 0;
     73 #else
     74 #define DPRINTFN(n, x)
     75 #endif
     76 #define DPRINTF(x) DPRINTFN(0, x)
     77 
     78 #define	UPLCOM_CONFIG_INDEX	0
     79 #define	UPLCOM_IFACE_INDEX	0
     80 #define	UPLCOM_SECOND_IFACE_INDEX	1
     81 
     82 #define	UPLCOM_SET_REQUEST	0x01
     83 #define	UPLCOM_SET_CRTSCTS	0x41
     84 #define RSAQ_STATUS_DSR		0x02
     85 #define RSAQ_STATUS_DCD		0x01
     86 
     87 #define	UPLCOM_FLOW_OUT_CTS	0x0001
     88 #define	UPLCOM_FLOW_OUT_DSR	0x0002
     89 #define	UPLCOM_FLOW_IN_DSR	0x0004
     90 #define	UPLCOM_FLOW_IN_DTR	0x0008
     91 #define	UPLCOM_FLOW_IN_RTS	0x0010
     92 #define	UPLCOM_FLOW_OUT_RTS	0x0020
     93 #define	UPLCOM_FLOW_OUT_XON	0x0080
     94 #define	UPLCOM_FLOW_IN_XON	0x0100
     95 
     96 struct	uplcom_softc {
     97 	USBBASEDEVICE		sc_dev;		/* base device */
     98 	usbd_device_handle	sc_udev;	/* USB device */
     99 	usbd_interface_handle	sc_iface;	/* interface */
    100 	int			sc_iface_number;	/* interface number */
    101 
    102 	usbd_interface_handle	sc_intr_iface;	/* interrupt interface */
    103 	int			sc_intr_number;	/* interrupt number */
    104 	usbd_pipe_handle	sc_intr_pipe;	/* interrupt pipe */
    105 	u_char			*sc_intr_buf;	/* interrupt buffer */
    106 	int			sc_isize;
    107 
    108 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
    109 	int			sc_dtr;		/* current DTR state */
    110 	int			sc_rts;		/* current RTS state */
    111 
    112 	device_ptr_t		sc_subdev;	/* ucom device */
    113 
    114 	u_char			sc_dying;	/* disconnecting */
    115 
    116 	u_char			sc_lsr;		/* Local status register */
    117 	u_char			sc_msr;		/* uplcom status register */
    118 };
    119 
    120 /*
    121  * These are the maximum number of bytes transferred per frame.
    122  * The output buffer size cannot be increased due to the size encoding.
    123  */
    124 #define UPLCOMIBUFSIZE 256
    125 #define UPLCOMOBUFSIZE 256
    126 
    127 Static	usbd_status uplcom_reset(struct uplcom_softc *);
    128 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
    129 					   usb_cdc_line_state_t *state);
    130 Static	usbd_status uplcom_set_crtscts(struct uplcom_softc *);
    131 Static	void uplcom_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    132 
    133 Static	void uplcom_set(void *, int, int, int);
    134 Static	void uplcom_dtr(struct uplcom_softc *, int);
    135 Static	void uplcom_rts(struct uplcom_softc *, int);
    136 Static	void uplcom_break(struct uplcom_softc *, int);
    137 Static	void uplcom_set_line_state(struct uplcom_softc *);
    138 Static	void uplcom_get_status(void *, int portno, u_char *lsr, u_char *msr);
    139 #if TODO
    140 Static	int  uplcom_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
    141 #endif
    142 Static	int  uplcom_param(void *, int, struct termios *);
    143 Static	int  uplcom_open(void *, int);
    144 Static	void uplcom_close(void *, int);
    145 
    146 struct	ucom_methods uplcom_methods = {
    147 	uplcom_get_status,
    148 	uplcom_set,
    149 	uplcom_param,
    150 	NULL, /* uplcom_ioctl, TODO */
    151 	uplcom_open,
    152 	uplcom_close,
    153 	NULL,
    154 	NULL,
    155 };
    156 
    157 static const struct usb_devno uplcom_devs[] = {
    158 	/* I/O DATA USB-RSAQ2 */
    159 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_RSAQ2 },
    160 	/* I/O DATA USB-RSAQ */
    161 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
    162 	/* PLANEX USB-RS232 URS-03 */
    163 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC232A },
    164 	/* IOGEAR/ATEN UC-232A */
    165 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    166 	/* IOGEAR/ATENTRIPPLITE */
    167 	{ USB_VENDOR_TRIPPLITE, USB_PRODUCT_TRIPPLITE_U209 },
    168 	/* ELECOM UC-SGT */
    169 	{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_UCSGT },
    170 	/* RATOC REX-USB60 */
    171 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60 },
    172 	/* TDK USB-PHS Adapter UHA6400 */
    173 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UHA6400 },
    174 	/* TDK USB-PDC Adapter UPA9664 */
    175 	{ USB_VENDOR_TDK, USB_PRODUCT_TDK_UPA9664 },
    176 	/* Sony Ericsson USB Cable */
    177 	{ USB_VENDOR_SUSTEEN, USB_PRODUCT_SUSTEEN_DCU10 },
    178 	/* SOURCENEXT KeikaiDenwa 8 */
    179 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8 },
    180 	/* SOURCENEXT KeikaiDenwa 8 with charger */
    181 	{ USB_VENDOR_SOURCENEXT, USB_PRODUCT_SOURCENEXT_KEIKAI8_CHG },
    182 	/* HAL Corporation Crossam2+USB */
    183 	{ USB_VENDOR_HAL, USB_PRODUCT_HAL_IMR001 },
    184 	/* Sitecom USB to serial cable */
    185 	{ USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_CN104 },
    186 };
    187 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
    188 
    189 
    190 USB_DECLARE_DRIVER(uplcom);
    191 
    192 USB_MATCH(uplcom)
    193 {
    194 	USB_MATCH_START(uplcom, uaa);
    195 
    196 	if (uaa->iface != NULL)
    197 		return (UMATCH_NONE);
    198 
    199 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
    200 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    201 }
    202 
    203 USB_ATTACH(uplcom)
    204 {
    205 	USB_ATTACH_START(uplcom, sc, uaa);
    206 	usbd_device_handle dev = uaa->device;
    207 	usb_config_descriptor_t *cdesc;
    208 	usb_interface_descriptor_t *id;
    209 	usb_endpoint_descriptor_t *ed;
    210 
    211 	char devinfo[1024];
    212 	char *devname = USBDEVNAME(sc->sc_dev);
    213 	usbd_status err;
    214 	int i;
    215 	struct ucom_attach_args uca;
    216 
    217         usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
    218         USB_ATTACH_SETUP;
    219         printf("%s: %s\n", devname, devinfo);
    220 
    221         sc->sc_udev = dev;
    222 
    223 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    224 
    225 	/* initialize endpoints */
    226 	uca.bulkin = uca.bulkout = -1;
    227 	sc->sc_intr_number = -1;
    228 	sc->sc_intr_pipe = NULL;
    229 
    230 	/* Move the device into the configured state. */
    231 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    232 	if (err) {
    233 		printf("\n%s: failed to set configuration, err=%s\n",
    234 			devname, usbd_errstr(err));
    235 		sc->sc_dying = 1;
    236 		USB_ATTACH_ERROR_RETURN;
    237 	}
    238 
    239 	/* get the config descriptor */
    240 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    241 
    242 	if (cdesc == NULL) {
    243 		printf("%s: failed to get configuration descriptor\n",
    244 			USBDEVNAME(sc->sc_dev));
    245 		sc->sc_dying = 1;
    246 		USB_ATTACH_ERROR_RETURN;
    247 	}
    248 
    249 	/* get the (first/common) interface */
    250 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
    251 							&sc->sc_iface);
    252 	if (err) {
    253 		printf("\n%s: failed to get interface, err=%s\n",
    254 			devname, usbd_errstr(err));
    255 		sc->sc_dying = 1;
    256 		USB_ATTACH_ERROR_RETURN;
    257 	}
    258 
    259 	/* Find the interrupt endpoints */
    260 
    261 	id = usbd_get_interface_descriptor(sc->sc_iface);
    262 	sc->sc_iface_number = id->bInterfaceNumber;
    263 
    264 	for (i = 0; i < id->bNumEndpoints; i++) {
    265 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    266 		if (ed == NULL) {
    267 			printf("%s: no endpoint descriptor for %d\n",
    268 				USBDEVNAME(sc->sc_dev), i);
    269 			sc->sc_dying = 1;
    270 			USB_ATTACH_ERROR_RETURN;
    271 		}
    272 
    273 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    274 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    275 			sc->sc_intr_number = ed->bEndpointAddress;
    276 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    277 		}
    278 	}
    279 
    280 	if (sc->sc_intr_number== -1) {
    281 		printf("%s: Could not find interrupt in\n",
    282 			USBDEVNAME(sc->sc_dev));
    283 		sc->sc_dying = 1;
    284 		USB_ATTACH_ERROR_RETURN;
    285 	}
    286 
    287 	/* keep interface for interrupt */
    288 	sc->sc_intr_iface = sc->sc_iface;
    289 
    290 	/*
    291 	 * USB-RSAQ1 has two interface
    292 	 *
    293 	 *  USB-RSAQ1       | USB-RSAQ2
    294  	 * -----------------+-----------------
    295 	 * Interface 0      |Interface 0
    296 	 *  Interrupt(0x81) | Interrupt(0x81)
    297 	 * -----------------+ BulkIN(0x02)
    298 	 * Interface 1	    | BulkOUT(0x83)
    299 	 *   BulkIN(0x02)   |
    300 	 *   BulkOUT(0x83)  |
    301 	 */
    302 	if (cdesc->bNumInterface == 2) {
    303 		err = usbd_device2interface_handle(dev,
    304 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
    305 		if (err) {
    306 			printf("\n%s: failed to get second interface, err=%s\n",
    307 							devname, usbd_errstr(err));
    308 			sc->sc_dying = 1;
    309 			USB_ATTACH_ERROR_RETURN;
    310 		}
    311 	}
    312 
    313 	/* Find the bulk{in,out} endpoints */
    314 
    315 	id = usbd_get_interface_descriptor(sc->sc_iface);
    316 	sc->sc_iface_number = id->bInterfaceNumber;
    317 
    318 	for (i = 0; i < id->bNumEndpoints; i++) {
    319 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    320 		if (ed == NULL) {
    321 			printf("%s: no endpoint descriptor for %d\n",
    322 				USBDEVNAME(sc->sc_dev), i);
    323 			sc->sc_dying = 1;
    324 			USB_ATTACH_ERROR_RETURN;
    325 		}
    326 
    327 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    328 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    329 			uca.bulkin = ed->bEndpointAddress;
    330 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    331 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    332 			uca.bulkout = ed->bEndpointAddress;
    333 		}
    334 	}
    335 
    336 	if (uca.bulkin == -1) {
    337 		printf("%s: Could not find data bulk in\n",
    338 			USBDEVNAME(sc->sc_dev));
    339 		sc->sc_dying = 1;
    340 		USB_ATTACH_ERROR_RETURN;
    341 	}
    342 
    343 	if (uca.bulkout == -1) {
    344 		printf("%s: Could not find data bulk out\n",
    345 			USBDEVNAME(sc->sc_dev));
    346 		sc->sc_dying = 1;
    347 		USB_ATTACH_ERROR_RETURN;
    348 	}
    349 
    350 	sc->sc_dtr = sc->sc_rts = -1;
    351 	uca.portno = UCOM_UNK_PORTNO;
    352 	/* bulkin, bulkout set above */
    353 	uca.ibufsize = UPLCOMIBUFSIZE;
    354 	uca.obufsize = UPLCOMOBUFSIZE;
    355 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    356 	uca.opkthdrlen = 0;
    357 	uca.device = dev;
    358 	uca.iface = sc->sc_iface;
    359 	uca.methods = &uplcom_methods;
    360 	uca.arg = sc;
    361 	uca.info = NULL;
    362 
    363 	err = uplcom_reset(sc);
    364 
    365 	if (err) {
    366 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    367 			usbd_errstr(err));
    368 		sc->sc_dying = 1;
    369 		USB_ATTACH_ERROR_RETURN;
    370 	}
    371 
    372 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    373 			   USBDEV(sc->sc_dev));
    374 
    375 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
    376 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    377 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
    378 					    ucomprint, ucomsubmatch);
    379 
    380 	USB_ATTACH_SUCCESS_RETURN;
    381 }
    382 
    383 USB_DETACH(uplcom)
    384 {
    385 	USB_DETACH_START(uplcom, sc);
    386 	int rv = 0;
    387 
    388 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    389 
    390         if (sc->sc_intr_pipe != NULL) {
    391                 usbd_abort_pipe(sc->sc_intr_pipe);
    392                 usbd_close_pipe(sc->sc_intr_pipe);
    393 		free(sc->sc_intr_buf, M_USBDEV);
    394                 sc->sc_intr_pipe = NULL;
    395         }
    396 
    397 	sc->sc_dying = 1;
    398 	if (sc->sc_subdev != NULL) {
    399 		rv = config_detach(sc->sc_subdev, flags);
    400 		sc->sc_subdev = NULL;
    401 	}
    402 
    403 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    404 			   USBDEV(sc->sc_dev));
    405 
    406 	return (rv);
    407 }
    408 
    409 int
    410 uplcom_activate(device_ptr_t self, enum devact act)
    411 {
    412 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    413 	int rv = 0;
    414 
    415 	switch (act) {
    416 	case DVACT_ACTIVATE:
    417 		return (EOPNOTSUPP);
    418 
    419 	case DVACT_DEACTIVATE:
    420 		if (sc->sc_subdev != NULL)
    421 			rv = config_deactivate(sc->sc_subdev);
    422 		sc->sc_dying = 1;
    423 		break;
    424 	}
    425 	return (rv);
    426 }
    427 
    428 usbd_status
    429 uplcom_reset(struct uplcom_softc *sc)
    430 {
    431         usb_device_request_t req;
    432 	usbd_status err;
    433 
    434         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    435         req.bRequest = UPLCOM_SET_REQUEST;
    436         USETW(req.wValue, 0);
    437         USETW(req.wIndex, sc->sc_iface_number);
    438         USETW(req.wLength, 0);
    439 
    440         err = usbd_do_request(sc->sc_udev, &req, 0);
    441 	if (err)
    442 		return (EIO);
    443 
    444 	return (0);
    445 }
    446 
    447 void
    448 uplcom_set_line_state(struct uplcom_softc *sc)
    449 {
    450 	usb_device_request_t req;
    451 	int ls;
    452 
    453 	/* make sure we have initialized state for sc_dtr and sc_rts */
    454 	if (sc->sc_dtr == -1)
    455 		sc->sc_dtr = 0;
    456 	if (sc->sc_rts == -1)
    457 		sc->sc_rts = 0;
    458 
    459 	ls = (sc->sc_dtr ? UPLCOM_FLOW_OUT_DSR : 0) |
    460 		(sc->sc_rts ? UPLCOM_FLOW_OUT_CTS : 0);
    461 
    462 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    463 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    464 	USETW(req.wValue, ls);
    465 	USETW(req.wIndex, sc->sc_iface_number);
    466 	USETW(req.wLength, 0);
    467 
    468 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    469 
    470 }
    471 
    472 void
    473 uplcom_set(void *addr, int portno, int reg, int onoff)
    474 {
    475 	struct uplcom_softc *sc = addr;
    476 
    477 	switch (reg) {
    478 	case UCOM_SET_DTR:
    479 		uplcom_dtr(sc, onoff);
    480 		break;
    481 	case UCOM_SET_RTS:
    482 		uplcom_rts(sc, onoff);
    483 		break;
    484 	case UCOM_SET_BREAK:
    485 		uplcom_break(sc, onoff);
    486 		break;
    487 	default:
    488 		break;
    489 	}
    490 }
    491 
    492 void
    493 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    494 {
    495 
    496 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
    497 
    498 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
    499 		return;
    500 
    501 	sc->sc_dtr = !!onoff;
    502 
    503 	uplcom_set_line_state(sc);
    504 }
    505 
    506 void
    507 uplcom_rts(struct uplcom_softc *sc, int onoff)
    508 {
    509 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    510 
    511 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
    512 		return;
    513 
    514 	sc->sc_rts = !!onoff;
    515 
    516 	uplcom_set_line_state(sc);
    517 }
    518 
    519 void
    520 uplcom_break(struct uplcom_softc *sc, int onoff)
    521 {
    522 	usb_device_request_t req;
    523 
    524 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    525 
    526 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    527 	req.bRequest = UCDC_SEND_BREAK;
    528 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    529 	USETW(req.wIndex, sc->sc_iface_number);
    530 	USETW(req.wLength, 0);
    531 
    532 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    533 }
    534 
    535 usbd_status
    536 uplcom_set_crtscts(struct uplcom_softc *sc)
    537 {
    538 	usb_device_request_t req;
    539 	usbd_status err;
    540 
    541 	DPRINTF(("uplcom_set_crtscts: on\n"));
    542 
    543 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    544 	req.bRequest = UPLCOM_SET_REQUEST;
    545 	USETW(req.wValue, 0);
    546 	USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
    547 	USETW(req.wLength, 0);
    548 
    549 	err = usbd_do_request(sc->sc_udev, &req, 0);
    550 	if (err) {
    551 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
    552 			usbd_errstr(err)));
    553 		return (err);
    554 	}
    555 
    556 	return (USBD_NORMAL_COMPLETION);
    557 }
    558 
    559 usbd_status
    560 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    561 {
    562 	usb_device_request_t req;
    563 	usbd_status err;
    564 
    565 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    566 		UGETDW(state->dwDTERate), state->bCharFormat,
    567 		state->bParityType, state->bDataBits));
    568 
    569 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    570 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    571 		return (USBD_NORMAL_COMPLETION);
    572 	}
    573 
    574 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    575 	req.bRequest = UCDC_SET_LINE_CODING;
    576 	USETW(req.wValue, 0);
    577 	USETW(req.wIndex, sc->sc_iface_number);
    578 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    579 
    580 	err = usbd_do_request(sc->sc_udev, &req, state);
    581 	if (err) {
    582 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    583 			usbd_errstr(err)));
    584 		return (err);
    585 	}
    586 
    587 	sc->sc_line_state = *state;
    588 
    589 	return (USBD_NORMAL_COMPLETION);
    590 }
    591 
    592 int
    593 uplcom_param(void *addr, int portno, struct termios *t)
    594 {
    595 	struct uplcom_softc *sc = addr;
    596 	usbd_status err;
    597 	usb_cdc_line_state_t ls;
    598 
    599 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    600 
    601 	USETDW(ls.dwDTERate, t->c_ospeed);
    602 	if (ISSET(t->c_cflag, CSTOPB))
    603 		ls.bCharFormat = UCDC_STOP_BIT_2;
    604 	else
    605 		ls.bCharFormat = UCDC_STOP_BIT_1;
    606 	if (ISSET(t->c_cflag, PARENB)) {
    607 		if (ISSET(t->c_cflag, PARODD))
    608 			ls.bParityType = UCDC_PARITY_ODD;
    609 		else
    610 			ls.bParityType = UCDC_PARITY_EVEN;
    611 	} else
    612 		ls.bParityType = UCDC_PARITY_NONE;
    613 	switch (ISSET(t->c_cflag, CSIZE)) {
    614 	case CS5:
    615 		ls.bDataBits = 5;
    616 		break;
    617 	case CS6:
    618 		ls.bDataBits = 6;
    619 		break;
    620 	case CS7:
    621 		ls.bDataBits = 7;
    622 		break;
    623 	case CS8:
    624 		ls.bDataBits = 8;
    625 		break;
    626 	}
    627 
    628 	err = uplcom_set_line_coding(sc, &ls);
    629 	if (err) {
    630 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    631 		return (EIO);
    632 	}
    633 
    634 	if (ISSET(t->c_cflag, CRTSCTS))
    635 		uplcom_set_crtscts(sc);
    636 
    637 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
    638 		uplcom_set_line_state(sc);
    639 
    640 	if (err) {
    641 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    642 		return (EIO);
    643 	}
    644 
    645 	return (0);
    646 }
    647 
    648 int
    649 uplcom_open(void *addr, int portno)
    650 {
    651 	struct uplcom_softc *sc = addr;
    652 	int err;
    653 
    654 	if (sc->sc_dying)
    655 		return (EIO);
    656 
    657 	DPRINTF(("uplcom_open: sc=%p\n", sc));
    658 
    659 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    660 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    661 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
    662 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    663 			sc->sc_intr_buf, sc->sc_isize,
    664 			uplcom_intr, USBD_DEFAULT_INTERVAL);
    665 		if (err) {
    666 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    667 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
    668 					return (EIO);
    669 		}
    670 	}
    671 
    672 	return (0);
    673 }
    674 
    675 void
    676 uplcom_close(void *addr, int portno)
    677 {
    678 	struct uplcom_softc *sc = addr;
    679 	int err;
    680 
    681 	if (sc->sc_dying)
    682 		return;
    683 
    684 	DPRINTF(("uplcom_close: close\n"));
    685 
    686 	if (sc->sc_intr_pipe != NULL) {
    687 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    688 		if (err)
    689 			printf("%s: abort interrupt pipe failed: %s\n",
    690 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    691 		err = usbd_close_pipe(sc->sc_intr_pipe);
    692 		if (err)
    693 			printf("%s: close interrupt pipe failed: %s\n",
    694 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    695 		free(sc->sc_intr_buf, M_USBDEV);
    696 		sc->sc_intr_pipe = NULL;
    697 	}
    698 }
    699 
    700 void
    701 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    702 {
    703 	struct uplcom_softc *sc = priv;
    704 	u_char *buf = sc->sc_intr_buf;
    705 	u_char pstatus;
    706 
    707 	if (sc->sc_dying)
    708 		return;
    709 
    710 	if (status != USBD_NORMAL_COMPLETION) {
    711 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    712 			return;
    713 
    714 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    715 			usbd_errstr(status)));
    716 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    717 		return;
    718 	}
    719 
    720 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
    721 
    722 	sc->sc_lsr = sc->sc_msr = 0;
    723 	pstatus = buf[8];
    724 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
    725 		sc->sc_msr |= UMSR_DSR;
    726 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
    727 		sc->sc_msr |= UMSR_DCD;
    728 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
    729 }
    730 
    731 void
    732 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    733 {
    734 	struct uplcom_softc *sc = addr;
    735 
    736 	DPRINTF(("uplcom_get_status:\n"));
    737 
    738 	if (lsr != NULL)
    739 		*lsr = sc->sc_lsr;
    740 	if (msr != NULL)
    741 		*msr = sc->sc_msr;
    742 }
    743 
    744 #if TODO
    745 int
    746 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
    747 	     usb_proc_ptr p)
    748 {
    749 	struct uplcom_softc *sc = addr;
    750 	int error = 0;
    751 
    752 	if (sc->sc_dying)
    753 		return (EIO);
    754 
    755 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
    756 
    757 	switch (cmd) {
    758 	case TIOCNOTTY:
    759 	case TIOCMGET:
    760 	case TIOCMSET:
    761 	case USB_GET_CM_OVER_DATA:
    762 	case USB_SET_CM_OVER_DATA:
    763 		break;
    764 
    765 	default:
    766 		DPRINTF(("uplcom_ioctl: unknown\n"));
    767 		error = ENOTTY;
    768 		break;
    769 	}
    770 
    771 	return (error);
    772 }
    773 #endif
    774