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