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