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