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