Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.11
      1  1.11    ichiro /*	$NetBSD: uplcom.c,v 1.11 2001/01/30 13:17:43 ichiro 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.1    ichiro  * by FUKUHARA ichiro (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.1    ichiro #include <sys/ioctl.h>
     50   1.1    ichiro #include <sys/conf.h>
     51   1.1    ichiro #include <sys/tty.h>
     52   1.1    ichiro #include <sys/file.h>
     53   1.1    ichiro #include <sys/select.h>
     54   1.1    ichiro #include <sys/proc.h>
     55   1.1    ichiro #include <sys/vnode.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/usbdevs.h>
     68   1.1    ichiro #include <dev/usb/ucomvar.h>
     69   1.1    ichiro 
     70   1.1    ichiro #ifdef UPLCOM_DEBUG
     71   1.1    ichiro #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
     72   1.1    ichiro int	uplcomdebug = 0xff;
     73   1.1    ichiro #else
     74   1.1    ichiro #define DPRINTFN(n, x)
     75   1.1    ichiro #endif
     76   1.1    ichiro #define DPRINTF(x) DPRINTFN(0, x)
     77   1.1    ichiro 
     78   1.1    ichiro #define	UPLCOM_CONFIG_INDEX	0
     79  1.10    ichiro #define	UPLCOM_IFACE_INDEX	0
     80  1.10    ichiro #define	UPLCOM_SECOND_IFACE_INDEX	1
     81   1.1    ichiro #define	UPLCOM_RESET		0
     82   1.1    ichiro 
     83   1.1    ichiro struct	uplcom_softc {
     84   1.1    ichiro 	USBBASEDEVICE		sc_dev;		/* base device */
     85   1.1    ichiro 	usbd_device_handle	sc_udev;	/* USB device */
     86  1.10    ichiro 	usbd_interface_handle	sc_iface;	/* first interface */
     87  1.10    ichiro 	usbd_interface_handle	sc_sec_iface;	/* second interface */
     88   1.1    ichiro 	int			sc_iface_number;	/* interface number */
     89   1.1    ichiro 
     90   1.1    ichiro 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
     91   1.1    ichiro 	u_char			sc_dtr;		/* current DTR state */
     92   1.1    ichiro 	u_char			sc_rts;		/* current RTS state */
     93   1.1    ichiro 
     94   1.1    ichiro 	device_ptr_t		sc_subdev;
     95   1.1    ichiro 
     96   1.1    ichiro 	u_char			sc_dying;
     97   1.1    ichiro };
     98   1.1    ichiro 
     99   1.1    ichiro /*
    100   1.1    ichiro  * These are the maximum number of bytes transferred per frame.
    101   1.1    ichiro  * The output buffer size cannot be increased due to the size encoding.
    102   1.1    ichiro  */
    103   1.6  augustss #define UPLCOMIBUFSIZE 256
    104   1.6  augustss #define UPLCOMOBUFSIZE 256
    105   1.1    ichiro 
    106  1.11    ichiro Static	usbd_status uplcom_reset(struct uplcom_softc *);
    107   1.1    ichiro Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
    108   1.6  augustss 					   usb_cdc_line_state_t *state);
    109   1.1    ichiro 
    110   1.1    ichiro Static	void	uplcom_set(void *, int, int, int);
    111   1.1    ichiro Static	void	uplcom_dtr(struct uplcom_softc *, int);
    112   1.1    ichiro Static	void	uplcom_rts(struct uplcom_softc *, int);
    113   1.1    ichiro Static	void	uplcom_break(struct uplcom_softc *, int);
    114   1.1    ichiro Static	void	uplcom_set_line_state(struct uplcom_softc *);
    115   1.1    ichiro Static	int	uplcom_param(void *, int, struct termios *);
    116  1.11    ichiro Static	int	uplcom_open(void *, int);
    117  1.11    ichiro Static	void	uplcom_close(void *, int);
    118   1.1    ichiro 
    119   1.1    ichiro struct	ucom_methods uplcom_methods = {
    120   1.1    ichiro 	NULL,
    121   1.1    ichiro 	uplcom_set,
    122   1.1    ichiro 	uplcom_param,
    123   1.1    ichiro 	NULL,
    124  1.11    ichiro 	uplcom_open,
    125  1.11    ichiro 	uplcom_close,
    126   1.1    ichiro 	NULL,
    127   1.1    ichiro 	NULL,
    128   1.1    ichiro };
    129   1.1    ichiro 
    130   1.1    ichiro static const struct uplcom_product {
    131   1.1    ichiro 	uint16_t	vendor;
    132   1.1    ichiro 	uint16_t	product;
    133   1.1    ichiro } uplcom_products [] = {
    134   1.1    ichiro 	/* I/O DATA USB-RSAQ2 */
    135   1.1    ichiro 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    136  1.10    ichiro 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBRSAQ },
    137   1.1    ichiro 
    138   1.1    ichiro 	{ 0, 0 }
    139   1.1    ichiro };
    140   1.1    ichiro 
    141   1.1    ichiro USB_DECLARE_DRIVER(uplcom);
    142   1.1    ichiro 
    143   1.1    ichiro USB_MATCH(uplcom)
    144   1.1    ichiro {
    145   1.1    ichiro 	USB_MATCH_START(uplcom, uaa);
    146   1.1    ichiro 	int i;
    147   1.1    ichiro 
    148   1.6  augustss 	if (uaa->iface != NULL)
    149   1.1    ichiro 		return (UMATCH_NONE);
    150   1.1    ichiro 
    151   1.1    ichiro 	for (i = 0; uplcom_products[i].vendor != 0; i++) {
    152   1.1    ichiro 		if (uplcom_products[i].vendor == uaa->vendor &&
    153   1.1    ichiro  		    uplcom_products[i].product == uaa->product) {
    154   1.1    ichiro 			return (UMATCH_VENDOR_PRODUCT);
    155   1.1    ichiro 		}
    156   1.1    ichiro 	}
    157   1.1    ichiro 	return (UMATCH_NONE);
    158   1.1    ichiro }
    159   1.1    ichiro 
    160   1.1    ichiro USB_ATTACH(uplcom)
    161   1.1    ichiro {
    162   1.1    ichiro 	USB_ATTACH_START(uplcom, sc, uaa);
    163   1.1    ichiro 	usbd_device_handle dev = uaa->device;
    164   1.1    ichiro 	usbd_interface_handle iface;
    165  1.10    ichiro 	usb_config_descriptor_t *cdesc;
    166   1.1    ichiro 	usb_interface_descriptor_t *id;
    167   1.1    ichiro 	usb_endpoint_descriptor_t *ed;
    168   1.1    ichiro 
    169   1.1    ichiro 	char devinfo[1024];
    170   1.1    ichiro 	char *devname = USBDEVNAME(sc->sc_dev);
    171   1.1    ichiro 	usbd_status err;
    172   1.1    ichiro 	int i;
    173   1.1    ichiro 	struct ucom_attach_args uca;
    174   1.1    ichiro 
    175  1.10    ichiro         usbd_devinfo(dev, 0, devinfo);
    176  1.10    ichiro         USB_ATTACH_SETUP;
    177  1.10    ichiro         printf("%s: %s\n", devname, devinfo);
    178  1.10    ichiro 
    179  1.10    ichiro         sc->sc_udev = dev;
    180  1.10    ichiro 
    181   1.1    ichiro 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    182   1.1    ichiro 
    183   1.6  augustss 	/* Move the device into the configured state. */
    184   1.1    ichiro 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    185   1.1    ichiro 	if (err) {
    186   1.1    ichiro 		printf("\n%s: failed to set configuration, err=%s\n",
    187   1.1    ichiro 			devname, usbd_errstr(err));
    188   1.1    ichiro 		USB_ATTACH_ERROR_RETURN;
    189   1.1    ichiro 	}
    190   1.1    ichiro 
    191  1.10    ichiro 	/* get the config descriptor */
    192  1.10    ichiro 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    193  1.10    ichiro 
    194  1.10    ichiro 	if (cdesc == NULL) {
    195  1.10    ichiro 		printf("%s: failed to get configuration descriptor\n",
    196  1.10    ichiro 			USBDEVNAME(sc->sc_dev));
    197  1.10    ichiro 		USB_ATTACH_ERROR_RETURN;
    198  1.10    ichiro 	}
    199  1.10    ichiro 
    200  1.10    ichiro 	/* get the (first) interface */
    201  1.10    ichiro 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX,
    202  1.10    ichiro 							&sc->sc_iface);
    203   1.1    ichiro 	if (err) {
    204   1.1    ichiro 		printf("\n%s: failed to get interface, err=%s\n",
    205   1.1    ichiro 			devname, usbd_errstr(err));
    206   1.1    ichiro 		USB_ATTACH_ERROR_RETURN;
    207   1.1    ichiro 	}
    208  1.10    ichiro 	/*
    209  1.10    ichiro 	 * check second of interface
    210  1.10    ichiro 	 * USB-RSAQ has two interface
    211  1.10    ichiro 	 */
    212  1.10    ichiro 	if (cdesc->bNumInterface == 2) {
    213  1.10    ichiro 		err = usbd_device2interface_handle(dev,
    214  1.10    ichiro 				UPLCOM_SECOND_IFACE_INDEX, &sc->sc_sec_iface);
    215  1.10    ichiro 		if (err) {
    216  1.10    ichiro 			printf("\n%s: failed to get second interface, err=%s\n",
    217  1.10    ichiro 							devname, usbd_errstr(err));
    218  1.10    ichiro 			USB_ATTACH_ERROR_RETURN;
    219  1.10    ichiro 		}
    220  1.10    ichiro 		iface = sc->sc_sec_iface;
    221  1.10    ichiro 	} else
    222  1.10    ichiro 		iface = sc->sc_iface;
    223   1.1    ichiro 
    224  1.10    ichiro 	/* Find the bulk endpoints */
    225   1.1    ichiro 
    226   1.1    ichiro 	id = usbd_get_interface_descriptor(iface);
    227   1.1    ichiro 	sc->sc_iface_number = id->bInterfaceNumber;
    228   1.1    ichiro 
    229   1.1    ichiro 	uca.bulkin = uca.bulkout = -1;
    230   1.1    ichiro 	for (i = 0; i < id->bNumEndpoints; i++) {
    231   1.1    ichiro 		ed = usbd_interface2endpoint_descriptor(iface, i);
    232   1.1    ichiro 		if (ed == NULL) {
    233   1.1    ichiro 			printf("%s: no endpoint descriptor for %d\n",
    234   1.1    ichiro 				USBDEVNAME(sc->sc_dev), i);
    235   1.1    ichiro 			USB_ATTACH_ERROR_RETURN;
    236   1.1    ichiro 		}
    237   1.1    ichiro 
    238   1.1    ichiro 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    239   1.1    ichiro 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    240   1.1    ichiro 			uca.bulkin = ed->bEndpointAddress;
    241   1.1    ichiro 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    242   1.1    ichiro 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    243   1.3    ichiro 			DPRINTF(("interrupt endpoint addr = 0x%x\n",
    244   1.2  augustss 				 ed->bEndpointAddress));
    245   1.1    ichiro 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    246   1.1    ichiro 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    247   1.1    ichiro 			uca.bulkout = ed->bEndpointAddress;
    248   1.1    ichiro 		}
    249   1.1    ichiro 	}
    250   1.1    ichiro 
    251   1.1    ichiro 	if (uca.bulkin == -1) {
    252   1.1    ichiro 		printf("%s: Could not find data bulk in\n",
    253   1.1    ichiro 			USBDEVNAME(sc->sc_dev));
    254   1.1    ichiro 		USB_ATTACH_ERROR_RETURN;
    255   1.1    ichiro 	}
    256   1.1    ichiro 
    257   1.1    ichiro 	if (uca.bulkout == -1) {
    258   1.1    ichiro 		printf("%s: Could not find data bulk out\n",
    259   1.1    ichiro 			USBDEVNAME(sc->sc_dev));
    260   1.1    ichiro 		USB_ATTACH_ERROR_RETURN;
    261   1.1    ichiro 	}
    262   1.1    ichiro 
    263   1.1    ichiro 	sc->sc_dtr = -1;
    264   1.1    ichiro 	uca.portno = UCOM_UNK_PORTNO;
    265   1.1    ichiro 	/* bulkin, bulkout set above */
    266   1.1    ichiro 	uca.ibufsize = UPLCOMIBUFSIZE;
    267   1.1    ichiro 	uca.obufsize = UPLCOMOBUFSIZE;
    268   1.1    ichiro 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    269   1.4    ichiro 	uca.opkthdrlen = 0;
    270   1.1    ichiro 	uca.device = dev;
    271   1.1    ichiro 	uca.iface = iface;
    272   1.1    ichiro 	uca.methods = &uplcom_methods;
    273   1.1    ichiro 	uca.arg = sc;
    274   1.8  augustss 	uca.info = NULL;
    275   1.1    ichiro 
    276  1.11    ichiro 	err = uplcom_reset(sc);
    277  1.11    ichiro 
    278   1.1    ichiro 	if (err) {
    279  1.11    ichiro 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    280   1.1    ichiro 			usbd_errstr(err));
    281   1.1    ichiro 		USB_ATTACH_ERROR_RETURN;
    282   1.1    ichiro 	}
    283   1.1    ichiro 
    284   1.7  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    285   1.7  augustss 			   USBDEV(sc->sc_dev));
    286   1.7  augustss 
    287   1.1    ichiro 	DPRINTF(("uplcom: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    288   1.1    ichiro 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    289   1.1    ichiro 
    290   1.1    ichiro 	USB_ATTACH_SUCCESS_RETURN;
    291   1.1    ichiro }
    292   1.1    ichiro 
    293   1.1    ichiro USB_DETACH(uplcom)
    294   1.1    ichiro {
    295   1.1    ichiro 	USB_DETACH_START(uplcom, sc);
    296   1.1    ichiro 	int rv = 0;
    297   1.1    ichiro 
    298   1.1    ichiro 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    299   1.1    ichiro 	sc->sc_dying = 1;
    300   1.6  augustss 	if (sc->sc_subdev != NULL) {
    301   1.1    ichiro 		rv = config_detach(sc->sc_subdev, flags);
    302   1.6  augustss 		sc->sc_subdev = NULL;
    303   1.6  augustss 	}
    304   1.7  augustss 
    305   1.7  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    306   1.7  augustss 			   USBDEV(sc->sc_dev));
    307   1.1    ichiro 
    308   1.1    ichiro 	return (rv);
    309   1.1    ichiro }
    310   1.1    ichiro 
    311   1.1    ichiro int
    312   1.1    ichiro uplcom_activate(device_ptr_t self, enum devact act)
    313   1.1    ichiro {
    314   1.1    ichiro 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    315   1.1    ichiro 	int rv = 0;
    316   1.1    ichiro 
    317   1.1    ichiro 	switch (act) {
    318   1.1    ichiro 	case DVACT_ACTIVATE:
    319   1.1    ichiro 		return (EOPNOTSUPP);
    320   1.1    ichiro 		break;
    321   1.1    ichiro 
    322   1.1    ichiro 	case DVACT_DEACTIVATE:
    323   1.1    ichiro 		if (sc->sc_subdev != NULL)
    324   1.1    ichiro 			rv = config_deactivate(sc->sc_subdev);
    325   1.1    ichiro 		sc->sc_dying = 1;
    326   1.1    ichiro 		break;
    327   1.1    ichiro 	}
    328   1.1    ichiro 	return (rv);
    329   1.1    ichiro }
    330   1.1    ichiro 
    331   1.1    ichiro 
    332   1.1    ichiro usbd_status
    333  1.11    ichiro uplcom_reset(struct uplcom_softc *sc)
    334   1.1    ichiro {
    335   1.1    ichiro         usb_device_request_t req;
    336   1.1    ichiro 	usbd_status err;
    337   1.1    ichiro 
    338   1.1    ichiro         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    339   1.1    ichiro         req.bRequest = UPLCOM_RESET;
    340   1.1    ichiro         USETW(req.wValue, UPLCOM_RESET);
    341   1.1    ichiro         USETW(req.wIndex, sc->sc_iface_number);
    342   1.1    ichiro         USETW(req.wLength, 0);
    343   1.1    ichiro 
    344   1.1    ichiro         err = usbd_do_request(sc->sc_udev, &req, 0);
    345   1.1    ichiro 	if (err)
    346   1.1    ichiro 		return (EIO);
    347   1.1    ichiro 
    348   1.1    ichiro 	return (0);
    349   1.1    ichiro }
    350   1.1    ichiro 
    351   1.1    ichiro void
    352   1.1    ichiro uplcom_set_line_state(struct uplcom_softc *sc)
    353   1.1    ichiro {
    354   1.1    ichiro 	usb_device_request_t req;
    355   1.1    ichiro 	int ls;
    356   1.1    ichiro 
    357   1.1    ichiro 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    358   1.1    ichiro 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    359   1.1    ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    360   1.1    ichiro 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    361   1.1    ichiro 	USETW(req.wValue, ls);
    362   1.1    ichiro 	USETW(req.wIndex, sc->sc_iface_number);
    363   1.1    ichiro 	USETW(req.wLength, 0);
    364   1.1    ichiro 
    365   1.1    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    366   1.1    ichiro 
    367   1.1    ichiro }
    368   1.1    ichiro 
    369   1.1    ichiro void
    370   1.1    ichiro uplcom_set(void *addr, int portno, int reg, int onoff)
    371   1.1    ichiro {
    372   1.1    ichiro 	struct uplcom_softc *sc = addr;
    373   1.1    ichiro 
    374   1.1    ichiro 	switch (reg) {
    375   1.1    ichiro 	case UCOM_SET_DTR:
    376   1.1    ichiro 		uplcom_dtr(sc, onoff);
    377   1.1    ichiro 		break;
    378   1.1    ichiro 	case UCOM_SET_RTS:
    379   1.1    ichiro 		uplcom_rts(sc, onoff);
    380   1.1    ichiro 		break;
    381   1.1    ichiro 	case UCOM_SET_BREAK:
    382   1.1    ichiro 		uplcom_break(sc, onoff);
    383   1.1    ichiro 		break;
    384   1.1    ichiro 	default:
    385   1.1    ichiro 		break;
    386   1.1    ichiro 	}
    387   1.1    ichiro }
    388   1.1    ichiro 
    389   1.1    ichiro void
    390   1.1    ichiro uplcom_dtr(struct uplcom_softc *sc, int onoff)
    391   1.1    ichiro {
    392   1.1    ichiro 
    393  1.11    ichiro 	DPRINTF(("uplcom_dtr: onoff=%d\n", onoff));
    394   1.1    ichiro 
    395   1.1    ichiro 	if (sc->sc_dtr == onoff)
    396   1.1    ichiro 		return;
    397   1.1    ichiro 	sc->sc_dtr = onoff;
    398   1.1    ichiro 
    399   1.1    ichiro 	uplcom_set_line_state(sc);
    400   1.1    ichiro }
    401   1.1    ichiro 
    402   1.1    ichiro void
    403   1.1    ichiro uplcom_rts(struct uplcom_softc *sc, int onoff)
    404   1.1    ichiro {
    405   1.1    ichiro 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    406   1.1    ichiro 
    407   1.1    ichiro 	if (sc->sc_rts == onoff)
    408   1.1    ichiro 		return;
    409   1.1    ichiro 	sc->sc_rts = onoff;
    410   1.1    ichiro 
    411   1.1    ichiro 	uplcom_set_line_state(sc);
    412   1.4    ichiro }
    413   1.4    ichiro 
    414   1.4    ichiro void
    415   1.4    ichiro uplcom_break(struct uplcom_softc *sc, int onoff)
    416   1.4    ichiro {
    417   1.4    ichiro 	usb_device_request_t req;
    418   1.4    ichiro 
    419   1.4    ichiro 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    420   1.4    ichiro 
    421   1.4    ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    422   1.4    ichiro 	req.bRequest = UCDC_SEND_BREAK;
    423   1.4    ichiro 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    424   1.4    ichiro 	USETW(req.wIndex, sc->sc_iface_number);
    425   1.4    ichiro 	USETW(req.wLength, 0);
    426   1.4    ichiro 
    427   1.4    ichiro 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    428   1.1    ichiro }
    429   1.1    ichiro 
    430   1.1    ichiro usbd_status
    431   1.1    ichiro uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    432   1.1    ichiro {
    433   1.1    ichiro 	usb_device_request_t req;
    434   1.1    ichiro 	usbd_status err;
    435   1.1    ichiro 
    436   1.1    ichiro 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    437   1.1    ichiro 		UGETDW(state->dwDTERate), state->bCharFormat,
    438   1.1    ichiro 		state->bParityType, state->bDataBits));
    439   1.1    ichiro 
    440   1.1    ichiro 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    441   1.1    ichiro 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    442   1.1    ichiro 		return (USBD_NORMAL_COMPLETION);
    443   1.1    ichiro 	}
    444   1.1    ichiro 
    445   1.1    ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    446   1.1    ichiro 	req.bRequest = UCDC_SET_LINE_CODING;
    447   1.1    ichiro 	USETW(req.wValue, 0);
    448   1.1    ichiro 	USETW(req.wIndex, sc->sc_iface_number);
    449   1.1    ichiro 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    450   1.1    ichiro 
    451   1.1    ichiro 	err = usbd_do_request(sc->sc_udev, &req, state);
    452   1.1    ichiro 	if (err) {
    453   1.1    ichiro 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    454   1.1    ichiro 			usbd_errstr(err)));
    455   1.1    ichiro 		return (err);
    456   1.1    ichiro 	}
    457   1.1    ichiro 
    458   1.1    ichiro 	sc->sc_line_state = *state;
    459   1.1    ichiro 
    460   1.1    ichiro 	return (USBD_NORMAL_COMPLETION);
    461   1.1    ichiro }
    462   1.1    ichiro 
    463   1.1    ichiro int
    464   1.1    ichiro uplcom_param(void *addr, int portno, struct termios *t)
    465   1.1    ichiro {
    466   1.1    ichiro 	struct uplcom_softc *sc = addr;
    467   1.1    ichiro 	usbd_status err;
    468   1.1    ichiro 	usb_cdc_line_state_t ls;
    469   1.1    ichiro 
    470   1.1    ichiro 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    471   1.1    ichiro 
    472   1.1    ichiro 	USETDW(ls.dwDTERate, t->c_ospeed);
    473   1.1    ichiro 	if (ISSET(t->c_cflag, CSTOPB))
    474   1.1    ichiro 		ls.bCharFormat = UCDC_STOP_BIT_2;
    475   1.1    ichiro 	else
    476   1.1    ichiro 		ls.bCharFormat = UCDC_STOP_BIT_1;
    477   1.1    ichiro 	if (ISSET(t->c_cflag, PARENB)) {
    478   1.1    ichiro 		if (ISSET(t->c_cflag, PARODD))
    479   1.1    ichiro 			ls.bParityType = UCDC_PARITY_ODD;
    480   1.1    ichiro 		else
    481   1.1    ichiro 			ls.bParityType = UCDC_PARITY_EVEN;
    482   1.1    ichiro 	} else
    483   1.1    ichiro 		ls.bParityType = UCDC_PARITY_NONE;
    484   1.1    ichiro 	switch (ISSET(t->c_cflag, CSIZE)) {
    485   1.1    ichiro 	case CS5:
    486   1.1    ichiro 		ls.bDataBits = 5;
    487   1.1    ichiro 		break;
    488   1.1    ichiro 	case CS6:
    489   1.1    ichiro 		ls.bDataBits = 6;
    490   1.1    ichiro 		break;
    491   1.1    ichiro 	case CS7:
    492   1.1    ichiro 		ls.bDataBits = 7;
    493   1.1    ichiro 		break;
    494   1.1    ichiro 	case CS8:
    495   1.1    ichiro 		ls.bDataBits = 8;
    496   1.1    ichiro 		break;
    497   1.1    ichiro 	}
    498   1.1    ichiro 
    499   1.1    ichiro 	err = uplcom_set_line_coding(sc, &ls);
    500   1.1    ichiro 	if (err) {
    501   1.1    ichiro 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    502   1.1    ichiro 		return (EIO);
    503   1.1    ichiro 	}
    504   1.1    ichiro 	return (0);
    505  1.11    ichiro }
    506  1.11    ichiro 
    507  1.11    ichiro int
    508  1.11    ichiro uplcom_open(void *addr, int portno)
    509  1.11    ichiro {
    510  1.11    ichiro 	struct uplcom_softc *sc = addr;
    511  1.11    ichiro 	usbd_status err;
    512  1.11    ichiro 
    513  1.11    ichiro 	if (sc->sc_dying)
    514  1.11    ichiro 		return (EIO);
    515  1.11    ichiro 
    516  1.11    ichiro 	err = uplcom_reset(sc);
    517  1.11    ichiro         if (err) {
    518  1.11    ichiro                 printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    519  1.11    ichiro                         usbd_errstr(err));
    520  1.11    ichiro         }
    521  1.11    ichiro 
    522  1.11    ichiro 	DPRINTF(("uplcom_open: open\n"));
    523  1.11    ichiro 
    524  1.11    ichiro 	return (0);
    525  1.11    ichiro }
    526  1.11    ichiro 
    527  1.11    ichiro void
    528  1.11    ichiro uplcom_close(void *addr, int portno)
    529  1.11    ichiro {
    530  1.11    ichiro 	struct uplcom_softc *sc = addr;
    531  1.11    ichiro 	usbd_status err;
    532  1.11    ichiro 
    533  1.11    ichiro 	if (sc->sc_dying)
    534  1.11    ichiro 		return;
    535  1.11    ichiro 
    536  1.11    ichiro 	err = uplcom_reset(sc);
    537  1.11    ichiro 	if (err) {
    538  1.11    ichiro 		printf("%s: reset failed, %s\n", USBDEVNAME(sc->sc_dev),
    539  1.11    ichiro 			usbd_errstr(err));
    540  1.11    ichiro 	}
    541  1.11    ichiro 
    542  1.11    ichiro 	DPRINTF(("uplcom_close: close\n"));
    543   1.1    ichiro }
    544