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