Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.37
      1 /*	$NetBSD: uplcom.c,v 1.37 2004/05/20 09:23:33 martin 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.37 2004/05/20 09:23:33 martin 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_SONYERICSSON, USB_PRODUCT_SONYERICSSON_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 };
    185 #define uplcom_lookup(v, p) usb_lookup(uplcom_devs, v, p)
    186 
    187 
    188 USB_DECLARE_DRIVER(uplcom);
    189 
    190 USB_MATCH(uplcom)
    191 {
    192 	USB_MATCH_START(uplcom, uaa);
    193 
    194 	if (uaa->iface != NULL)
    195 		return (UMATCH_NONE);
    196 
    197 	return (uplcom_lookup(uaa->vendor, uaa->product) != NULL ?
    198 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    199 }
    200 
    201 USB_ATTACH(uplcom)
    202 {
    203 	USB_ATTACH_START(uplcom, sc, uaa);
    204 	usbd_device_handle dev = uaa->device;
    205 	usb_config_descriptor_t *cdesc;
    206 	usb_interface_descriptor_t *id;
    207 	usb_endpoint_descriptor_t *ed;
    208 
    209 	char devinfo[1024];
    210 	char *devname = USBDEVNAME(sc->sc_dev);
    211 	usbd_status err;
    212 	int i;
    213 	struct ucom_attach_args uca;
    214 
    215         usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
    216         USB_ATTACH_SETUP;
    217         printf("%s: %s\n", devname, devinfo);
    218 
    219         sc->sc_udev = dev;
    220 
    221 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    222 
    223 	/* initialize endpoints */
    224 	uca.bulkin = uca.bulkout = -1;
    225 	sc->sc_intr_number = -1;
    226 	sc->sc_intr_pipe = NULL;
    227 
    228 	/* Move the device into the configured state. */
    229 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    230 	if (err) {
    231 		printf("\n%s: failed to set configuration, err=%s\n",
    232 			devname, usbd_errstr(err));
    233 		sc->sc_dying = 1;
    234 		USB_ATTACH_ERROR_RETURN;
    235 	}
    236 
    237 	/* get the config descriptor */
    238 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    239 
    240 	if (cdesc == NULL) {
    241 		printf("%s: failed to get configuration descriptor\n",
    242 			USBDEVNAME(sc->sc_dev));
    243 		sc->sc_dying = 1;
    244 		USB_ATTACH_ERROR_RETURN;
    245 	}
    246 
    247 	/* get the (first/common) interface */
    248 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
    249 							&sc->sc_iface);
    250 	if (err) {
    251 		printf("\n%s: failed to get interface, err=%s\n",
    252 			devname, usbd_errstr(err));
    253 		sc->sc_dying = 1;
    254 		USB_ATTACH_ERROR_RETURN;
    255 	}
    256 
    257 	/* Find the interrupt endpoints */
    258 
    259 	id = usbd_get_interface_descriptor(sc->sc_iface);
    260 	sc->sc_iface_number = id->bInterfaceNumber;
    261 
    262 	for (i = 0; i < id->bNumEndpoints; i++) {
    263 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    264 		if (ed == NULL) {
    265 			printf("%s: no endpoint descriptor for %d\n",
    266 				USBDEVNAME(sc->sc_dev), i);
    267 			sc->sc_dying = 1;
    268 			USB_ATTACH_ERROR_RETURN;
    269 		}
    270 
    271 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    272 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    273 			sc->sc_intr_number = ed->bEndpointAddress;
    274 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
    275 		}
    276 	}
    277 
    278 	if (sc->sc_intr_number== -1) {
    279 		printf("%s: Could not find interrupt in\n",
    280 			USBDEVNAME(sc->sc_dev));
    281 		sc->sc_dying = 1;
    282 		USB_ATTACH_ERROR_RETURN;
    283 	}
    284 
    285 	/* keep interface for interrupt */
    286 	sc->sc_intr_iface = sc->sc_iface;
    287 
    288 	/*
    289 	 * USB-RSAQ1 has two interface
    290 	 *
    291 	 *  USB-RSAQ1       | USB-RSAQ2
    292  	 * -----------------+-----------------
    293 	 * Interface 0      |Interface 0
    294 	 *  Interrupt(0x81) | Interrupt(0x81)
    295 	 * -----------------+ BulkIN(0x02)
    296 	 * Interface 1	    | BulkOUT(0x83)
    297 	 *   BulkIN(0x02)   |
    298 	 *   BulkOUT(0x83)  |
    299 	 */
    300 	if (cdesc->bNumInterface == 2) {
    301 		err = usbd_device2interface_handle(dev,
    302 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_iface);
    303 		if (err) {
    304 			printf("\n%s: failed to get second interface, err=%s\n",
    305 							devname, usbd_errstr(err));
    306 			sc->sc_dying = 1;
    307 			USB_ATTACH_ERROR_RETURN;
    308 		}
    309 	}
    310 
    311 	/* Find the bulk{in,out} endpoints */
    312 
    313 	id = usbd_get_interface_descriptor(sc->sc_iface);
    314 	sc->sc_iface_number = id->bInterfaceNumber;
    315 
    316 	for (i = 0; i < id->bNumEndpoints; i++) {
    317 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    318 		if (ed == NULL) {
    319 			printf("%s: no endpoint descriptor for %d\n",
    320 				USBDEVNAME(sc->sc_dev), i);
    321 			sc->sc_dying = 1;
    322 			USB_ATTACH_ERROR_RETURN;
    323 		}
    324 
    325 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    326 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    327 			uca.bulkin = ed->bEndpointAddress;
    328 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    329 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    330 			uca.bulkout = ed->bEndpointAddress;
    331 		}
    332 	}
    333 
    334 	if (uca.bulkin == -1) {
    335 		printf("%s: Could not find data bulk in\n",
    336 			USBDEVNAME(sc->sc_dev));
    337 		sc->sc_dying = 1;
    338 		USB_ATTACH_ERROR_RETURN;
    339 	}
    340 
    341 	if (uca.bulkout == -1) {
    342 		printf("%s: Could not find data bulk out\n",
    343 			USBDEVNAME(sc->sc_dev));
    344 		sc->sc_dying = 1;
    345 		USB_ATTACH_ERROR_RETURN;
    346 	}
    347 
    348 	sc->sc_dtr = sc->sc_rts = -1;
    349 	uca.portno = UCOM_UNK_PORTNO;
    350 	/* bulkin, bulkout set above */
    351 	uca.ibufsize = UPLCOMIBUFSIZE;
    352 	uca.obufsize = UPLCOMOBUFSIZE;
    353 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    354 	uca.opkthdrlen = 0;
    355 	uca.device = dev;
    356 	uca.iface = sc->sc_iface;
    357 	uca.methods = &uplcom_methods;
    358 	uca.arg = sc;
    359 	uca.info = NULL;
    360 
    361 	err = uplcom_reset(sc);
    362 
    363 	if (err) {
    364 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    365 			usbd_errstr(err));
    366 		sc->sc_dying = 1;
    367 		USB_ATTACH_ERROR_RETURN;
    368 	}
    369 
    370 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    371 			   USBDEV(sc->sc_dev));
    372 
    373 	DPRINTF(("uplcom: in=0x%x out=0x%x intr=0x%x\n",
    374 			uca.bulkin, uca.bulkout, sc->sc_intr_number ));
    375 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    376 
    377 	USB_ATTACH_SUCCESS_RETURN;
    378 }
    379 
    380 USB_DETACH(uplcom)
    381 {
    382 	USB_DETACH_START(uplcom, sc);
    383 	int rv = 0;
    384 
    385 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    386 
    387         if (sc->sc_intr_pipe != NULL) {
    388                 usbd_abort_pipe(sc->sc_intr_pipe);
    389                 usbd_close_pipe(sc->sc_intr_pipe);
    390 		free(sc->sc_intr_buf, M_USBDEV);
    391                 sc->sc_intr_pipe = NULL;
    392         }
    393 
    394 	sc->sc_dying = 1;
    395 	if (sc->sc_subdev != NULL) {
    396 		rv = config_detach(sc->sc_subdev, flags);
    397 		sc->sc_subdev = NULL;
    398 	}
    399 
    400 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    401 			   USBDEV(sc->sc_dev));
    402 
    403 	return (rv);
    404 }
    405 
    406 int
    407 uplcom_activate(device_ptr_t self, enum devact act)
    408 {
    409 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    410 	int rv = 0;
    411 
    412 	switch (act) {
    413 	case DVACT_ACTIVATE:
    414 		return (EOPNOTSUPP);
    415 
    416 	case DVACT_DEACTIVATE:
    417 		if (sc->sc_subdev != NULL)
    418 			rv = config_deactivate(sc->sc_subdev);
    419 		sc->sc_dying = 1;
    420 		break;
    421 	}
    422 	return (rv);
    423 }
    424 
    425 usbd_status
    426 uplcom_reset(struct uplcom_softc *sc)
    427 {
    428         usb_device_request_t req;
    429 	usbd_status err;
    430 
    431         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    432         req.bRequest = UPLCOM_SET_REQUEST;
    433         USETW(req.wValue, 0);
    434         USETW(req.wIndex, sc->sc_iface_number);
    435         USETW(req.wLength, 0);
    436 
    437         err = usbd_do_request(sc->sc_udev, &req, 0);
    438 	if (err)
    439 		return (EIO);
    440 
    441 	return (0);
    442 }
    443 
    444 void
    445 uplcom_set_line_state(struct uplcom_softc *sc)
    446 {
    447 	usb_device_request_t req;
    448 	int ls;
    449 
    450 	/* make sure we have initialized state for sc_dtr and sc_rts */
    451 	if (sc->sc_dtr == -1)
    452 		sc->sc_dtr = 0;
    453 	if (sc->sc_rts == -1)
    454 		sc->sc_rts = 0;
    455 
    456 	ls = (sc->sc_dtr ? UPLCOM_FLOW_OUT_DSR : 0) |
    457 		(sc->sc_rts ? UPLCOM_FLOW_OUT_CTS : 0);
    458 
    459 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    460 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    461 	USETW(req.wValue, ls);
    462 	USETW(req.wIndex, sc->sc_iface_number);
    463 	USETW(req.wLength, 0);
    464 
    465 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    466 
    467 }
    468 
    469 void
    470 uplcom_set(void *addr, int portno, int reg, int onoff)
    471 {
    472 	struct uplcom_softc *sc = addr;
    473 
    474 	switch (reg) {
    475 	case UCOM_SET_DTR:
    476 		uplcom_dtr(sc, onoff);
    477 		break;
    478 	case UCOM_SET_RTS:
    479 		uplcom_rts(sc, onoff);
    480 		break;
    481 	case UCOM_SET_BREAK:
    482 		uplcom_break(sc, onoff);
    483 		break;
    484 	default:
    485 		break;
    486 	}
    487 }
    488 
    489 void
    490 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    491 {
    492 
    493 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
    494 
    495 	if (sc->sc_dtr != -1 && !sc->sc_dtr == !onoff)
    496 		return;
    497 
    498 	sc->sc_dtr = !!onoff;
    499 
    500 	uplcom_set_line_state(sc);
    501 }
    502 
    503 void
    504 uplcom_rts(struct uplcom_softc *sc, int onoff)
    505 {
    506 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    507 
    508 	if (sc->sc_rts != -1 && !sc->sc_rts == !onoff)
    509 		return;
    510 
    511 	sc->sc_rts = !!onoff;
    512 
    513 	uplcom_set_line_state(sc);
    514 }
    515 
    516 void
    517 uplcom_break(struct uplcom_softc *sc, int onoff)
    518 {
    519 	usb_device_request_t req;
    520 
    521 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    522 
    523 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    524 	req.bRequest = UCDC_SEND_BREAK;
    525 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    526 	USETW(req.wIndex, sc->sc_iface_number);
    527 	USETW(req.wLength, 0);
    528 
    529 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    530 }
    531 
    532 usbd_status
    533 uplcom_set_crtscts(struct uplcom_softc *sc)
    534 {
    535 	usb_device_request_t req;
    536 	usbd_status err;
    537 
    538 	DPRINTF(("uplcom_set_crtscts: on\n"));
    539 
    540 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    541 	req.bRequest = UPLCOM_SET_REQUEST;
    542 	USETW(req.wValue, 0);
    543 	USETW(req.wIndex, UPLCOM_SET_CRTSCTS);
    544 	USETW(req.wLength, 0);
    545 
    546 	err = usbd_do_request(sc->sc_udev, &req, 0);
    547 	if (err) {
    548 		DPRINTF(("uplcom_set_crtscts: failed, err=%s\n",
    549 			usbd_errstr(err)));
    550 		return (err);
    551 	}
    552 
    553 	return (USBD_NORMAL_COMPLETION);
    554 }
    555 
    556 usbd_status
    557 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    558 {
    559 	usb_device_request_t req;
    560 	usbd_status err;
    561 
    562 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    563 		UGETDW(state->dwDTERate), state->bCharFormat,
    564 		state->bParityType, state->bDataBits));
    565 
    566 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    567 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    568 		return (USBD_NORMAL_COMPLETION);
    569 	}
    570 
    571 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    572 	req.bRequest = UCDC_SET_LINE_CODING;
    573 	USETW(req.wValue, 0);
    574 	USETW(req.wIndex, sc->sc_iface_number);
    575 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    576 
    577 	err = usbd_do_request(sc->sc_udev, &req, state);
    578 	if (err) {
    579 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    580 			usbd_errstr(err)));
    581 		return (err);
    582 	}
    583 
    584 	sc->sc_line_state = *state;
    585 
    586 	return (USBD_NORMAL_COMPLETION);
    587 }
    588 
    589 int
    590 uplcom_param(void *addr, int portno, struct termios *t)
    591 {
    592 	struct uplcom_softc *sc = addr;
    593 	usbd_status err;
    594 	usb_cdc_line_state_t ls;
    595 
    596 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    597 
    598 	USETDW(ls.dwDTERate, t->c_ospeed);
    599 	if (ISSET(t->c_cflag, CSTOPB))
    600 		ls.bCharFormat = UCDC_STOP_BIT_2;
    601 	else
    602 		ls.bCharFormat = UCDC_STOP_BIT_1;
    603 	if (ISSET(t->c_cflag, PARENB)) {
    604 		if (ISSET(t->c_cflag, PARODD))
    605 			ls.bParityType = UCDC_PARITY_ODD;
    606 		else
    607 			ls.bParityType = UCDC_PARITY_EVEN;
    608 	} else
    609 		ls.bParityType = UCDC_PARITY_NONE;
    610 	switch (ISSET(t->c_cflag, CSIZE)) {
    611 	case CS5:
    612 		ls.bDataBits = 5;
    613 		break;
    614 	case CS6:
    615 		ls.bDataBits = 6;
    616 		break;
    617 	case CS7:
    618 		ls.bDataBits = 7;
    619 		break;
    620 	case CS8:
    621 		ls.bDataBits = 8;
    622 		break;
    623 	}
    624 
    625 	err = uplcom_set_line_coding(sc, &ls);
    626 	if (err) {
    627 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    628 		return (EIO);
    629 	}
    630 
    631 	if (ISSET(t->c_cflag, CRTSCTS))
    632 		uplcom_set_crtscts(sc);
    633 
    634 	if (sc->sc_rts == -1 || sc->sc_dtr == -1)
    635 		uplcom_set_line_state(sc);
    636 
    637 	if (err) {
    638 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    639 		return (EIO);
    640 	}
    641 
    642 	return (0);
    643 }
    644 
    645 int
    646 uplcom_open(void *addr, int portno)
    647 {
    648 	struct uplcom_softc *sc = addr;
    649 	int err;
    650 
    651 	if (sc->sc_dying)
    652 		return (EIO);
    653 
    654 	DPRINTF(("uplcom_open: sc=%p\n", sc));
    655 
    656 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    657 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    658 		err = usbd_open_pipe_intr(sc->sc_intr_iface, sc->sc_intr_number,
    659 			USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
    660 			sc->sc_intr_buf, sc->sc_isize,
    661 			uplcom_intr, USBD_DEFAULT_INTERVAL);
    662 		if (err) {
    663 			DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
    664 				USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
    665 					return (EIO);
    666 		}
    667 	}
    668 
    669 	return (0);
    670 }
    671 
    672 void
    673 uplcom_close(void *addr, int portno)
    674 {
    675 	struct uplcom_softc *sc = addr;
    676 	int err;
    677 
    678 	if (sc->sc_dying)
    679 		return;
    680 
    681 	DPRINTF(("uplcom_close: close\n"));
    682 
    683 	if (sc->sc_intr_pipe != NULL) {
    684 		err = usbd_abort_pipe(sc->sc_intr_pipe);
    685 		if (err)
    686 			printf("%s: abort interrupt pipe failed: %s\n",
    687 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    688 		err = usbd_close_pipe(sc->sc_intr_pipe);
    689 		if (err)
    690 			printf("%s: close interrupt pipe failed: %s\n",
    691 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    692 		free(sc->sc_intr_buf, M_USBDEV);
    693 		sc->sc_intr_pipe = NULL;
    694 	}
    695 }
    696 
    697 void
    698 uplcom_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    699 {
    700 	struct uplcom_softc *sc = priv;
    701 	u_char *buf = sc->sc_intr_buf;
    702 	u_char pstatus;
    703 
    704 	if (sc->sc_dying)
    705 		return;
    706 
    707 	if (status != USBD_NORMAL_COMPLETION) {
    708 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    709 			return;
    710 
    711 		DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    712 			usbd_errstr(status)));
    713 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
    714 		return;
    715 	}
    716 
    717 	DPRINTF(("%s: uplcom status = %02x\n", USBDEVNAME(sc->sc_dev), buf[8]));
    718 
    719 	sc->sc_lsr = sc->sc_msr = 0;
    720 	pstatus = buf[8];
    721 	if (ISSET(pstatus, RSAQ_STATUS_DSR))
    722 		sc->sc_msr |= UMSR_DSR;
    723 	if (ISSET(pstatus, RSAQ_STATUS_DCD))
    724 		sc->sc_msr |= UMSR_DCD;
    725 	ucom_status_change((struct ucom_softc *) sc->sc_subdev);
    726 }
    727 
    728 void
    729 uplcom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    730 {
    731 	struct uplcom_softc *sc = addr;
    732 
    733 	DPRINTF(("uplcom_get_status:\n"));
    734 
    735 	if (lsr != NULL)
    736 		*lsr = sc->sc_lsr;
    737 	if (msr != NULL)
    738 		*msr = sc->sc_msr;
    739 }
    740 
    741 #if TODO
    742 int
    743 uplcom_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
    744 	     usb_proc_ptr p)
    745 {
    746 	struct uplcom_softc *sc = addr;
    747 	int error = 0;
    748 
    749 	if (sc->sc_dying)
    750 		return (EIO);
    751 
    752 	DPRINTF(("uplcom_ioctl: cmd=0x%08lx\n", cmd));
    753 
    754 	switch (cmd) {
    755 	case TIOCNOTTY:
    756 	case TIOCMGET:
    757 	case TIOCMSET:
    758 	case USB_GET_CM_OVER_DATA:
    759 	case USB_SET_CM_OVER_DATA:
    760 		break;
    761 
    762 	default:
    763 		DPRINTF(("uplcom_ioctl: unknown\n"));
    764 		error = ENOTTY;
    765 		break;
    766 	}
    767 
    768 	return (error);
    769 }
    770 #endif
    771