Home | History | Annotate | Line # | Download | only in usb
umodem_common.c revision 1.4
      1  1.4  augustss /*	$NetBSD: umodem_common.c,v 1.4 2005/05/11 10:02:28 augustss Exp $	*/
      2  1.1     itohy 
      3  1.1     itohy /*
      4  1.1     itohy  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1     itohy  * All rights reserved.
      6  1.1     itohy  *
      7  1.1     itohy  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     itohy  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  1.1     itohy  * Carlstedt Research & Technology.
     10  1.1     itohy  *
     11  1.1     itohy  * Redistribution and use in source and binary forms, with or without
     12  1.1     itohy  * modification, are permitted provided that the following conditions
     13  1.1     itohy  * are met:
     14  1.1     itohy  * 1. Redistributions of source code must retain the above copyright
     15  1.1     itohy  *    notice, this list of conditions and the following disclaimer.
     16  1.1     itohy  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1     itohy  *    notice, this list of conditions and the following disclaimer in the
     18  1.1     itohy  *    documentation and/or other materials provided with the distribution.
     19  1.1     itohy  * 3. All advertising materials mentioning features or use of this software
     20  1.1     itohy  *    must display the following acknowledgement:
     21  1.1     itohy  *        This product includes software developed by the NetBSD
     22  1.1     itohy  *        Foundation, Inc. and its contributors.
     23  1.1     itohy  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1     itohy  *    contributors may be used to endorse or promote products derived
     25  1.1     itohy  *    from this software without specific prior written permission.
     26  1.1     itohy  *
     27  1.1     itohy  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1     itohy  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1     itohy  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1     itohy  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1     itohy  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1     itohy  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1     itohy  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1     itohy  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1     itohy  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1     itohy  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1     itohy  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1     itohy  */
     39  1.1     itohy 
     40  1.1     itohy /*
     41  1.1     itohy  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
     42  1.1     itohy  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
     43  1.1     itohy  */
     44  1.1     itohy 
     45  1.1     itohy /*
     46  1.1     itohy  * TODO:
     47  1.1     itohy  * - Add error recovery in various places; the big problem is what
     48  1.1     itohy  *   to do in a callback if there is an error.
     49  1.1     itohy  * - Implement a Call Device for modems without multiplexed commands.
     50  1.1     itohy  *
     51  1.1     itohy  */
     52  1.1     itohy 
     53  1.1     itohy #include <sys/cdefs.h>
     54  1.4  augustss __KERNEL_RCSID(0, "$NetBSD: umodem_common.c,v 1.4 2005/05/11 10:02:28 augustss Exp $");
     55  1.1     itohy 
     56  1.1     itohy #include <sys/param.h>
     57  1.1     itohy #include <sys/systm.h>
     58  1.1     itohy #include <sys/kernel.h>
     59  1.1     itohy #include <sys/ioctl.h>
     60  1.1     itohy #include <sys/conf.h>
     61  1.1     itohy #include <sys/tty.h>
     62  1.1     itohy #include <sys/file.h>
     63  1.1     itohy #include <sys/select.h>
     64  1.1     itohy #include <sys/proc.h>
     65  1.1     itohy #include <sys/vnode.h>
     66  1.1     itohy #include <sys/device.h>
     67  1.1     itohy #include <sys/poll.h>
     68  1.1     itohy 
     69  1.1     itohy #include <dev/usb/usb.h>
     70  1.1     itohy #include <dev/usb/usbcdc.h>
     71  1.1     itohy 
     72  1.1     itohy #include <dev/usb/usbdi.h>
     73  1.1     itohy #include <dev/usb/usbdi_util.h>
     74  1.1     itohy #include <dev/usb/usbdevs.h>
     75  1.1     itohy #include <dev/usb/usb_quirks.h>
     76  1.1     itohy 
     77  1.1     itohy #include <dev/usb/usbdevs.h>
     78  1.1     itohy #include <dev/usb/ucomvar.h>
     79  1.1     itohy #include <dev/usb/umodemvar.h>
     80  1.1     itohy 
     81  1.1     itohy #ifdef UMODEM_DEBUG
     82  1.1     itohy #define DPRINTFN(n, x)	if (umodemdebug > (n)) logprintf x
     83  1.1     itohy int	umodemdebug = 0;
     84  1.1     itohy #else
     85  1.1     itohy #define DPRINTFN(n, x)
     86  1.1     itohy #endif
     87  1.1     itohy #define DPRINTF(x) DPRINTFN(0, x)
     88  1.1     itohy 
     89  1.1     itohy /*
     90  1.1     itohy  * These are the maximum number of bytes transferred per frame.
     91  1.1     itohy  * If some really high speed devices should use this driver they
     92  1.1     itohy  * may need to be increased, but this is good enough for normal modems.
     93  1.1     itohy  */
     94  1.1     itohy #define UMODEMIBUFSIZE 64
     95  1.1     itohy #define UMODEMOBUFSIZE 256
     96  1.1     itohy 
     97  1.1     itohy Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
     98  1.1     itohy 					   int feature, int state);
     99  1.1     itohy Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
    100  1.1     itohy 					  usb_cdc_line_state_t *state);
    101  1.1     itohy 
    102  1.1     itohy Static void	umodem_dtr(struct umodem_softc *, int);
    103  1.1     itohy Static void	umodem_rts(struct umodem_softc *, int);
    104  1.1     itohy Static void	umodem_break(struct umodem_softc *, int);
    105  1.1     itohy Static void	umodem_set_line_state(struct umodem_softc *);
    106  1.1     itohy Static void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    107  1.1     itohy 
    108  1.1     itohy int
    109  1.1     itohy umodem_common_attach(device_ptr_t self, struct umodem_softc *sc,
    110  1.1     itohy 		     struct usb_attach_arg *uaa, struct ucom_attach_args *uca)
    111  1.1     itohy {
    112  1.1     itohy 	usbd_device_handle dev = uaa->device;
    113  1.1     itohy 	usb_interface_descriptor_t *id;
    114  1.1     itohy 	usb_endpoint_descriptor_t *ed;
    115  1.4  augustss 	char *devinfop;
    116  1.1     itohy 	usbd_status err;
    117  1.1     itohy 	int data_ifcno;
    118  1.1     itohy 	int i;
    119  1.1     itohy 
    120  1.4  augustss 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
    121  1.1     itohy 	USB_ATTACH_SETUP;
    122  1.1     itohy 
    123  1.1     itohy 	sc->sc_udev = dev;
    124  1.1     itohy 	sc->sc_ctl_iface = uaa->iface;
    125  1.1     itohy 
    126  1.1     itohy 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    127  1.1     itohy 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    128  1.4  augustss 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    129  1.4  augustss 	usbd_devinfo_free(devinfop);
    130  1.1     itohy 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
    131  1.1     itohy 
    132  1.2     itohy 	/* Get the data interface no. */
    133  1.2     itohy 	sc->sc_data_iface_no = data_ifcno =
    134  1.2     itohy 	    umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
    135  1.1     itohy 
    136  1.2     itohy 	if (data_ifcno == -1) {
    137  1.2     itohy 		printf("%s: no pointer to data interface\n",
    138  1.2     itohy 		       USBDEVNAME(sc->sc_dev));
    139  1.1     itohy 		goto bad;
    140  1.1     itohy 	}
    141  1.1     itohy 
    142  1.1     itohy 	printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
    143  1.1     itohy 	       USBDEVNAME(sc->sc_dev), data_ifcno,
    144  1.1     itohy 	       sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
    145  1.1     itohy 	       sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
    146  1.1     itohy 
    147  1.1     itohy 	/* Get the data interface too. */
    148  1.1     itohy 	for (i = 0; i < uaa->nifaces; i++) {
    149  1.1     itohy 		if (uaa->ifaces[i] != NULL) {
    150  1.1     itohy 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
    151  1.1     itohy 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
    152  1.1     itohy 				sc->sc_data_iface = uaa->ifaces[i];
    153  1.1     itohy 				uaa->ifaces[i] = NULL;
    154  1.1     itohy 			}
    155  1.1     itohy 		}
    156  1.1     itohy 	}
    157  1.1     itohy 	if (sc->sc_data_iface == NULL) {
    158  1.1     itohy 		printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
    159  1.1     itohy 		goto bad;
    160  1.1     itohy 	}
    161  1.1     itohy 
    162  1.1     itohy 	/*
    163  1.1     itohy 	 * Find the bulk endpoints.
    164  1.1     itohy 	 * Iterate over all endpoints in the data interface and take note.
    165  1.1     itohy 	 */
    166  1.1     itohy 	uca->bulkin = uca->bulkout = -1;
    167  1.1     itohy 
    168  1.1     itohy 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
    169  1.1     itohy 	for (i = 0; i < id->bNumEndpoints; i++) {
    170  1.1     itohy 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
    171  1.1     itohy 		if (ed == NULL) {
    172  1.1     itohy 			printf("%s: no endpoint descriptor for %d\n",
    173  1.1     itohy 				USBDEVNAME(sc->sc_dev), i);
    174  1.1     itohy 			goto bad;
    175  1.1     itohy 		}
    176  1.1     itohy 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    177  1.1     itohy 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    178  1.3     itohy 			uca->bulkin = ed->bEndpointAddress;
    179  1.3     itohy 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    180  1.1     itohy 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    181  1.3     itohy 			uca->bulkout = ed->bEndpointAddress;
    182  1.3     itohy 		}
    183  1.3     itohy 	}
    184  1.1     itohy 
    185  1.1     itohy 	if (uca->bulkin == -1) {
    186  1.1     itohy 		printf("%s: Could not find data bulk in\n",
    187  1.1     itohy 		       USBDEVNAME(sc->sc_dev));
    188  1.1     itohy 		goto bad;
    189  1.1     itohy 	}
    190  1.1     itohy 	if (uca->bulkout == -1) {
    191  1.1     itohy 		printf("%s: Could not find data bulk out\n",
    192  1.1     itohy 			USBDEVNAME(sc->sc_dev));
    193  1.1     itohy 		goto bad;
    194  1.1     itohy 	}
    195  1.1     itohy 
    196  1.1     itohy 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
    197  1.1     itohy 		sc->sc_cm_over_data = 1;
    198  1.1     itohy 	} else {
    199  1.1     itohy 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
    200  1.1     itohy 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
    201  1.1     itohy 				err = umodem_set_comm_feature(sc,
    202  1.1     itohy 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
    203  1.1     itohy 			else
    204  1.1     itohy 				err = 0;
    205  1.1     itohy 			if (err) {
    206  1.1     itohy 				printf("%s: could not set data multiplex mode\n",
    207  1.1     itohy 				       USBDEVNAME(sc->sc_dev));
    208  1.1     itohy 				goto bad;
    209  1.1     itohy 			}
    210  1.1     itohy 			sc->sc_cm_over_data = 1;
    211  1.1     itohy 		}
    212  1.1     itohy 	}
    213  1.1     itohy 
    214  1.1     itohy 	/*
    215  1.1     itohy 	 * The standard allows for notification messages (to indicate things
    216  1.1     itohy 	 * like a modem hangup) to come in via an interrupt endpoint
    217  1.1     itohy 	 * off of the control interface.  Iterate over the endpoints on
    218  1.1     itohy 	 * the control interface and see if there are any interrupt
    219  1.1     itohy 	 * endpoints; if there are, then register it.
    220  1.1     itohy 	 */
    221  1.1     itohy 
    222  1.1     itohy 	sc->sc_ctl_notify = -1;
    223  1.1     itohy 	sc->sc_notify_pipe = NULL;
    224  1.1     itohy 
    225  1.1     itohy 	for (i = 0; i < id->bNumEndpoints; i++) {
    226  1.1     itohy 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
    227  1.1     itohy 		if (ed == NULL)
    228  1.1     itohy 			continue;
    229  1.1     itohy 
    230  1.1     itohy 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    231  1.1     itohy 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    232  1.1     itohy 			printf("%s: status change notification available\n",
    233  1.1     itohy 			       USBDEVNAME(sc->sc_dev));
    234  1.1     itohy 			sc->sc_ctl_notify = ed->bEndpointAddress;
    235  1.1     itohy 		}
    236  1.1     itohy 	}
    237  1.1     itohy 
    238  1.1     itohy 	sc->sc_dtr = -1;
    239  1.1     itohy 
    240  1.1     itohy 	/* bulkin, bulkout set above */
    241  1.1     itohy 	uca->ibufsize = UMODEMIBUFSIZE;
    242  1.1     itohy 	uca->obufsize = UMODEMOBUFSIZE;
    243  1.1     itohy 	uca->ibufsizepad = UMODEMIBUFSIZE;
    244  1.1     itohy 	uca->opkthdrlen = 0;
    245  1.1     itohy 	uca->device = sc->sc_udev;
    246  1.1     itohy 	uca->iface = sc->sc_data_iface;
    247  1.1     itohy 	uca->arg = sc;
    248  1.1     itohy 
    249  1.1     itohy 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    250  1.1     itohy 			   USBDEV(sc->sc_dev));
    251  1.1     itohy 
    252  1.1     itohy 	DPRINTF(("umodem_common_attach: sc=%p\n", sc));
    253  1.1     itohy 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, uca,
    254  1.1     itohy 					    ucomprint, ucomsubmatch);
    255  1.1     itohy 
    256  1.1     itohy 	return 0;
    257  1.1     itohy 
    258  1.1     itohy  bad:
    259  1.1     itohy 	sc->sc_dying = 1;
    260  1.1     itohy 	return 1;
    261  1.1     itohy }
    262  1.1     itohy 
    263  1.1     itohy int
    264  1.1     itohy umodem_open(void *addr, int portno)
    265  1.1     itohy {
    266  1.1     itohy 	struct umodem_softc *sc = addr;
    267  1.1     itohy 	int err;
    268  1.1     itohy 
    269  1.1     itohy 	DPRINTF(("umodem_open: sc=%p\n", sc));
    270  1.1     itohy 
    271  1.1     itohy 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
    272  1.1     itohy 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
    273  1.1     itohy 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
    274  1.1     itohy 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
    275  1.1     itohy 		    umodem_intr, USBD_DEFAULT_INTERVAL);
    276  1.1     itohy 
    277  1.1     itohy 		if (err) {
    278  1.1     itohy 			DPRINTF(("Failed to establish notify pipe: %s\n",
    279  1.1     itohy 				usbd_errstr(err)));
    280  1.1     itohy 			return EIO;
    281  1.1     itohy 		}
    282  1.1     itohy 	}
    283  1.1     itohy 
    284  1.1     itohy 	return 0;
    285  1.1     itohy }
    286  1.1     itohy 
    287  1.1     itohy void
    288  1.1     itohy umodem_close(void *addr, int portno)
    289  1.1     itohy {
    290  1.1     itohy 	struct umodem_softc *sc = addr;
    291  1.1     itohy 	int err;
    292  1.1     itohy 
    293  1.1     itohy 	DPRINTF(("umodem_close: sc=%p\n", sc));
    294  1.1     itohy 
    295  1.1     itohy 	if (sc->sc_notify_pipe != NULL) {
    296  1.1     itohy 		err = usbd_abort_pipe(sc->sc_notify_pipe);
    297  1.1     itohy 		if (err)
    298  1.1     itohy 			printf("%s: abort notify pipe failed: %s\n",
    299  1.1     itohy 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    300  1.1     itohy 		err = usbd_close_pipe(sc->sc_notify_pipe);
    301  1.1     itohy 		if (err)
    302  1.1     itohy 			printf("%s: close notify pipe failed: %s\n",
    303  1.1     itohy 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    304  1.1     itohy 		sc->sc_notify_pipe = NULL;
    305  1.1     itohy 	}
    306  1.1     itohy }
    307  1.1     itohy 
    308  1.1     itohy Static void
    309  1.1     itohy umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    310  1.1     itohy {
    311  1.1     itohy 	struct umodem_softc *sc = priv;
    312  1.1     itohy 	u_char mstatus;
    313  1.1     itohy 
    314  1.1     itohy 	if (sc->sc_dying)
    315  1.1     itohy 		return;
    316  1.1     itohy 
    317  1.1     itohy 	if (status != USBD_NORMAL_COMPLETION) {
    318  1.1     itohy 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    319  1.1     itohy 			return;
    320  1.1     itohy 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
    321  1.1     itohy 		       usbd_errstr(status));
    322  1.1     itohy 		return;
    323  1.1     itohy 	}
    324  1.1     itohy 
    325  1.1     itohy 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
    326  1.1     itohy 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
    327  1.1     itohy 			 USBDEVNAME(sc->sc_dev),
    328  1.1     itohy 			 sc->sc_notify_buf.bmRequestType));
    329  1.1     itohy 		return;
    330  1.1     itohy 	}
    331  1.1     itohy 
    332  1.1     itohy 	switch (sc->sc_notify_buf.bNotification) {
    333  1.1     itohy 	case UCDC_N_SERIAL_STATE:
    334  1.1     itohy 		/*
    335  1.1     itohy 		 * Set the serial state in ucom driver based on
    336  1.1     itohy 		 * the bits from the notify message
    337  1.1     itohy 		 */
    338  1.1     itohy 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
    339  1.1     itohy 			printf("%s: Invalid notification length! (%d)\n",
    340  1.1     itohy 			       USBDEVNAME(sc->sc_dev),
    341  1.1     itohy 			       UGETW(sc->sc_notify_buf.wLength));
    342  1.1     itohy 			break;
    343  1.1     itohy 		}
    344  1.1     itohy 		DPRINTF(("%s: notify bytes = %02x%02x\n",
    345  1.1     itohy 			 USBDEVNAME(sc->sc_dev),
    346  1.1     itohy 			 sc->sc_notify_buf.data[0],
    347  1.1     itohy 			 sc->sc_notify_buf.data[1]));
    348  1.1     itohy 		/* Currently, lsr is always zero. */
    349  1.1     itohy 		sc->sc_lsr = sc->sc_msr = 0;
    350  1.1     itohy 		mstatus = sc->sc_notify_buf.data[0];
    351  1.1     itohy 
    352  1.1     itohy 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
    353  1.1     itohy 			sc->sc_msr |= UMSR_RI;
    354  1.1     itohy 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
    355  1.1     itohy 			sc->sc_msr |= UMSR_DSR;
    356  1.1     itohy 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
    357  1.1     itohy 			sc->sc_msr |= UMSR_DCD;
    358  1.1     itohy 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
    359  1.1     itohy 		break;
    360  1.1     itohy 	default:
    361  1.1     itohy 		DPRINTF(("%s: unknown notify message: %02x\n",
    362  1.1     itohy 			 USBDEVNAME(sc->sc_dev),
    363  1.1     itohy 			 sc->sc_notify_buf.bNotification));
    364  1.1     itohy 		break;
    365  1.1     itohy 	}
    366  1.1     itohy }
    367  1.1     itohy 
    368  1.2     itohy int
    369  1.1     itohy umodem_get_caps(usbd_device_handle dev, int *cm, int *acm,
    370  1.1     itohy 		usb_interface_descriptor_t *id)
    371  1.1     itohy {
    372  1.1     itohy 	const usb_cdc_cm_descriptor_t *cmd;
    373  1.1     itohy 	const usb_cdc_acm_descriptor_t *cad;
    374  1.2     itohy 	const usb_cdc_union_descriptor_t *cud;
    375  1.1     itohy 
    376  1.1     itohy 	*cm = *acm = 0;
    377  1.1     itohy 
    378  1.1     itohy 	cmd = (usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
    379  1.1     itohy 							  UDESC_CS_INTERFACE,
    380  1.1     itohy 							  UDESCSUB_CDC_CM, id);
    381  1.1     itohy 	if (cmd == NULL) {
    382  1.3     itohy 		DPRINTF(("umodem_get_caps: no CM desc\n"));
    383  1.2     itohy 	} else {
    384  1.2     itohy 		*cm = cmd->bmCapabilities;
    385  1.1     itohy 	}
    386  1.1     itohy 
    387  1.1     itohy 	cad = (usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
    388  1.1     itohy 							   UDESC_CS_INTERFACE,
    389  1.1     itohy 							   UDESCSUB_CDC_ACM,
    390  1.1     itohy 							   id);
    391  1.1     itohy 	if (cad == NULL) {
    392  1.3     itohy 		DPRINTF(("umodem_get_caps: no ACM desc\n"));
    393  1.2     itohy 	} else {
    394  1.2     itohy 		*acm = cad->bmCapabilities;
    395  1.2     itohy 	}
    396  1.2     itohy 
    397  1.2     itohy 	cud = (usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
    398  1.2     itohy 							     UDESC_CS_INTERFACE,
    399  1.2     itohy 							     UDESCSUB_CDC_UNION,
    400  1.2     itohy 							     id);
    401  1.2     itohy 	if (cud == NULL) {
    402  1.3     itohy 		DPRINTF(("umodem_get_caps: no UNION desc\n"));
    403  1.1     itohy 	}
    404  1.3     itohy 
    405  1.2     itohy 	return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
    406  1.1     itohy }
    407  1.1     itohy 
    408  1.1     itohy void
    409  1.1     itohy umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
    410  1.1     itohy {
    411  1.1     itohy 	struct umodem_softc *sc = addr;
    412  1.1     itohy 
    413  1.1     itohy 	DPRINTF(("umodem_get_status:\n"));
    414  1.1     itohy 
    415  1.1     itohy 	if (lsr != NULL)
    416  1.1     itohy 		*lsr = sc->sc_lsr;
    417  1.1     itohy 	if (msr != NULL)
    418  1.1     itohy 		*msr = sc->sc_msr;
    419  1.1     itohy }
    420  1.1     itohy 
    421  1.1     itohy int
    422  1.1     itohy umodem_param(void *addr, int portno, struct termios *t)
    423  1.1     itohy {
    424  1.1     itohy 	struct umodem_softc *sc = addr;
    425  1.1     itohy 	usbd_status err;
    426  1.1     itohy 	usb_cdc_line_state_t ls;
    427  1.1     itohy 
    428  1.1     itohy 	DPRINTF(("umodem_param: sc=%p\n", sc));
    429  1.1     itohy 
    430  1.1     itohy 	USETDW(ls.dwDTERate, t->c_ospeed);
    431  1.1     itohy 	if (ISSET(t->c_cflag, CSTOPB))
    432  1.1     itohy 		ls.bCharFormat = UCDC_STOP_BIT_2;
    433  1.1     itohy 	else
    434  1.1     itohy 		ls.bCharFormat = UCDC_STOP_BIT_1;
    435  1.1     itohy 	if (ISSET(t->c_cflag, PARENB)) {
    436  1.1     itohy 		if (ISSET(t->c_cflag, PARODD))
    437  1.1     itohy 			ls.bParityType = UCDC_PARITY_ODD;
    438  1.1     itohy 		else
    439  1.1     itohy 			ls.bParityType = UCDC_PARITY_EVEN;
    440  1.1     itohy 	} else
    441  1.1     itohy 		ls.bParityType = UCDC_PARITY_NONE;
    442  1.1     itohy 	switch (ISSET(t->c_cflag, CSIZE)) {
    443  1.1     itohy 	case CS5:
    444  1.1     itohy 		ls.bDataBits = 5;
    445  1.1     itohy 		break;
    446  1.1     itohy 	case CS6:
    447  1.1     itohy 		ls.bDataBits = 6;
    448  1.1     itohy 		break;
    449  1.1     itohy 	case CS7:
    450  1.1     itohy 		ls.bDataBits = 7;
    451  1.1     itohy 		break;
    452  1.1     itohy 	case CS8:
    453  1.1     itohy 		ls.bDataBits = 8;
    454  1.1     itohy 		break;
    455  1.1     itohy 	}
    456  1.1     itohy 
    457  1.1     itohy 	err = umodem_set_line_coding(sc, &ls);
    458  1.1     itohy 	if (err) {
    459  1.1     itohy 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
    460  1.1     itohy 		return (EPASSTHROUGH);
    461  1.1     itohy 	}
    462  1.1     itohy 	return (0);
    463  1.1     itohy }
    464  1.1     itohy 
    465  1.1     itohy int
    466  1.1     itohy umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
    467  1.1     itohy 	     usb_proc_ptr p)
    468  1.1     itohy {
    469  1.1     itohy 	struct umodem_softc *sc = addr;
    470  1.1     itohy 	int error = 0;
    471  1.1     itohy 
    472  1.1     itohy 	if (sc->sc_dying)
    473  1.1     itohy 		return (EIO);
    474  1.1     itohy 
    475  1.3     itohy 	DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
    476  1.1     itohy 
    477  1.1     itohy 	switch (cmd) {
    478  1.1     itohy 	case USB_GET_CM_OVER_DATA:
    479  1.1     itohy 		*(int *)data = sc->sc_cm_over_data;
    480  1.1     itohy 		break;
    481  1.1     itohy 
    482  1.1     itohy 	case USB_SET_CM_OVER_DATA:
    483  1.1     itohy 		if (*(int *)data != sc->sc_cm_over_data) {
    484  1.1     itohy 			/* XXX change it */
    485  1.1     itohy 		}
    486  1.1     itohy 		break;
    487  1.1     itohy 
    488  1.1     itohy 	default:
    489  1.3     itohy 		DPRINTF(("umodem_ioctl: unknown\n"));
    490  1.1     itohy 		error = EPASSTHROUGH;
    491  1.1     itohy 		break;
    492  1.1     itohy 	}
    493  1.1     itohy 
    494  1.1     itohy 	return (error);
    495  1.1     itohy }
    496  1.1     itohy 
    497  1.1     itohy void
    498  1.1     itohy umodem_dtr(struct umodem_softc *sc, int onoff)
    499  1.1     itohy {
    500  1.3     itohy 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
    501  1.1     itohy 
    502  1.1     itohy 	if (sc->sc_dtr == onoff)
    503  1.1     itohy 		return;
    504  1.1     itohy 	sc->sc_dtr = onoff;
    505  1.1     itohy 
    506  1.1     itohy 	umodem_set_line_state(sc);
    507  1.1     itohy }
    508  1.1     itohy 
    509  1.1     itohy void
    510  1.1     itohy umodem_rts(struct umodem_softc *sc, int onoff)
    511  1.1     itohy {
    512  1.3     itohy 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
    513  1.1     itohy 
    514  1.1     itohy 	if (sc->sc_rts == onoff)
    515  1.1     itohy 		return;
    516  1.1     itohy 	sc->sc_rts = onoff;
    517  1.1     itohy 
    518  1.1     itohy 	umodem_set_line_state(sc);
    519  1.1     itohy }
    520  1.1     itohy 
    521  1.1     itohy void
    522  1.1     itohy umodem_set_line_state(struct umodem_softc *sc)
    523  1.1     itohy {
    524  1.1     itohy 	usb_device_request_t req;
    525  1.1     itohy 	int ls;
    526  1.1     itohy 
    527  1.1     itohy 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    528  1.1     itohy 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
    529  1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    530  1.1     itohy 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    531  1.1     itohy 	USETW(req.wValue, ls);
    532  1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    533  1.1     itohy 	USETW(req.wLength, 0);
    534  1.1     itohy 
    535  1.1     itohy 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    536  1.1     itohy 
    537  1.1     itohy }
    538  1.1     itohy 
    539  1.1     itohy void
    540  1.1     itohy umodem_break(struct umodem_softc *sc, int onoff)
    541  1.1     itohy {
    542  1.1     itohy 	usb_device_request_t req;
    543  1.1     itohy 
    544  1.1     itohy 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
    545  1.1     itohy 
    546  1.1     itohy 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
    547  1.1     itohy 		return;
    548  1.1     itohy 
    549  1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    550  1.1     itohy 	req.bRequest = UCDC_SEND_BREAK;
    551  1.1     itohy 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    552  1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    553  1.1     itohy 	USETW(req.wLength, 0);
    554  1.1     itohy 
    555  1.1     itohy 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    556  1.1     itohy }
    557  1.1     itohy 
    558  1.1     itohy void
    559  1.1     itohy umodem_set(void *addr, int portno, int reg, int onoff)
    560  1.1     itohy {
    561  1.1     itohy 	struct umodem_softc *sc = addr;
    562  1.1     itohy 
    563  1.1     itohy 	switch (reg) {
    564  1.1     itohy 	case UCOM_SET_DTR:
    565  1.1     itohy 		umodem_dtr(sc, onoff);
    566  1.1     itohy 		break;
    567  1.1     itohy 	case UCOM_SET_RTS:
    568  1.1     itohy 		umodem_rts(sc, onoff);
    569  1.1     itohy 		break;
    570  1.1     itohy 	case UCOM_SET_BREAK:
    571  1.1     itohy 		umodem_break(sc, onoff);
    572  1.1     itohy 		break;
    573  1.1     itohy 	default:
    574  1.1     itohy 		break;
    575  1.1     itohy 	}
    576  1.1     itohy }
    577  1.1     itohy 
    578  1.1     itohy usbd_status
    579  1.1     itohy umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
    580  1.1     itohy {
    581  1.1     itohy 	usb_device_request_t req;
    582  1.1     itohy 	usbd_status err;
    583  1.1     itohy 
    584  1.1     itohy 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    585  1.1     itohy 		 UGETDW(state->dwDTERate), state->bCharFormat,
    586  1.1     itohy 		 state->bParityType, state->bDataBits));
    587  1.1     itohy 
    588  1.1     itohy 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    589  1.1     itohy 		DPRINTF(("umodem_set_line_coding: already set\n"));
    590  1.1     itohy 		return (USBD_NORMAL_COMPLETION);
    591  1.1     itohy 	}
    592  1.1     itohy 
    593  1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    594  1.1     itohy 	req.bRequest = UCDC_SET_LINE_CODING;
    595  1.1     itohy 	USETW(req.wValue, 0);
    596  1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    597  1.1     itohy 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    598  1.1     itohy 
    599  1.1     itohy 	err = usbd_do_request(sc->sc_udev, &req, state);
    600  1.1     itohy 	if (err) {
    601  1.1     itohy 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
    602  1.1     itohy 			 usbd_errstr(err)));
    603  1.1     itohy 		return (err);
    604  1.1     itohy 	}
    605  1.1     itohy 
    606  1.1     itohy 	sc->sc_line_state = *state;
    607  1.1     itohy 
    608  1.1     itohy 	return (USBD_NORMAL_COMPLETION);
    609  1.1     itohy }
    610  1.1     itohy 
    611  1.1     itohy usbd_status
    612  1.1     itohy umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
    613  1.1     itohy {
    614  1.1     itohy 	usb_device_request_t req;
    615  1.1     itohy 	usbd_status err;
    616  1.1     itohy 	usb_cdc_abstract_state_t ast;
    617  1.1     itohy 
    618  1.1     itohy 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
    619  1.1     itohy 		 state));
    620  1.1     itohy 
    621  1.1     itohy 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    622  1.1     itohy 	req.bRequest = UCDC_SET_COMM_FEATURE;
    623  1.1     itohy 	USETW(req.wValue, feature);
    624  1.1     itohy 	USETW(req.wIndex, sc->sc_ctl_iface_no);
    625  1.1     itohy 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
    626  1.1     itohy 	USETW(ast.wState, state);
    627  1.1     itohy 
    628  1.1     itohy 	err = usbd_do_request(sc->sc_udev, &req, &ast);
    629  1.1     itohy 	if (err) {
    630  1.1     itohy 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
    631  1.1     itohy 			 feature, usbd_errstr(err)));
    632  1.1     itohy 		return (err);
    633  1.1     itohy 	}
    634  1.1     itohy 
    635  1.1     itohy 	return (USBD_NORMAL_COMPLETION);
    636  1.1     itohy }
    637  1.1     itohy 
    638  1.1     itohy int
    639  1.1     itohy umodem_common_activate(struct umodem_softc *sc, enum devact act)
    640  1.1     itohy {
    641  1.1     itohy 	int rv = 0;
    642  1.1     itohy 
    643  1.1     itohy 	switch (act) {
    644  1.1     itohy 	case DVACT_ACTIVATE:
    645  1.1     itohy 		return (EOPNOTSUPP);
    646  1.1     itohy 
    647  1.1     itohy 	case DVACT_DEACTIVATE:
    648  1.1     itohy 		sc->sc_dying = 1;
    649  1.1     itohy 		if (sc->sc_subdev)
    650  1.1     itohy 			rv = config_deactivate(sc->sc_subdev);
    651  1.1     itohy 		break;
    652  1.1     itohy 	}
    653  1.1     itohy 	return (rv);
    654  1.1     itohy }
    655  1.1     itohy 
    656  1.1     itohy int
    657  1.1     itohy umodem_common_detach(struct umodem_softc *sc, int flags)
    658  1.1     itohy {
    659  1.1     itohy 	int rv = 0;
    660  1.1     itohy 
    661  1.1     itohy 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
    662  1.1     itohy 
    663  1.1     itohy 	sc->sc_dying = 1;
    664  1.1     itohy 
    665  1.1     itohy 	if (sc->sc_subdev != NULL)
    666  1.1     itohy 		rv = config_detach(sc->sc_subdev, flags);
    667  1.1     itohy 
    668  1.1     itohy 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    669  1.1     itohy 			   USBDEV(sc->sc_dev));
    670  1.1     itohy 
    671  1.1     itohy 	return (rv);
    672  1.1     itohy }
    673