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