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