Home | History | Annotate | Line # | Download | only in usb
uhmodem.c revision 1.1
      1  1.1  ichiro /*	$NetBSD: uhmodem.c,v 1.1 2008/01/21 11:36:47 ichiro Exp $	*/
      2  1.1  ichiro 
      3  1.1  ichiro /*
      4  1.1  ichiro  * Copyright (c) 2008 Yojiro UO <yuo (at) nui.org>.
      5  1.1  ichiro  * All rights reserved.
      6  1.1  ichiro  *
      7  1.1  ichiro  * Permission to use, copy, modify, and distribute this software for any
      8  1.1  ichiro  * purpose with or without fee is hereby granted, provided that the above
      9  1.1  ichiro  * copyright notice and this permission notice appear in all copies.
     10  1.1  ichiro  *
     11  1.1  ichiro  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  1.1  ichiro  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  ichiro  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  1.1  ichiro  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  ichiro  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
     16  1.1  ichiro  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1  ichiro  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  ichiro  */
     19  1.1  ichiro /*-
     20  1.1  ichiro  * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
     21  1.1  ichiro  * All rights reserved.
     22  1.1  ichiro  *
     23  1.1  ichiro  * Redistribution and use in source and binary forms, with or without
     24  1.1  ichiro  * modification, are permitted provided that the following conditions
     25  1.1  ichiro  * are met:
     26  1.1  ichiro  * 1. Redistributions of source code must retain the above copyright
     27  1.1  ichiro  *    notice, this list of conditions and the following disclaimer.
     28  1.1  ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     29  1.1  ichiro  *    notice, this list of conditions and the following disclaimer in the
     30  1.1  ichiro  *    documentation and/or other materials provided with the distribution.
     31  1.1  ichiro  *
     32  1.1  ichiro  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     33  1.1  ichiro  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  1.1  ichiro  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  1.1  ichiro  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     36  1.1  ichiro  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.1  ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.1  ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.1  ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  1.1  ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  1.1  ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  1.1  ichiro  * SUCH DAMAGE.
     43  1.1  ichiro  */
     44  1.1  ichiro /*
     45  1.1  ichiro  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     46  1.1  ichiro  * All rights reserved.
     47  1.1  ichiro  *
     48  1.1  ichiro  * This code is derived from software contributed to The NetBSD Foundation
     49  1.1  ichiro  * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
     50  1.1  ichiro  *
     51  1.1  ichiro  * Redistribution and use in source and binary forms, with or without
     52  1.1  ichiro  * modification, are permitted provided that the following conditions
     53  1.1  ichiro  * are met:
     54  1.1  ichiro  * 1. Redistributions of source code must retain the above copyright
     55  1.1  ichiro  *    notice, this list of conditions and the following disclaimer.
     56  1.1  ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     57  1.1  ichiro  *    notice, this list of conditions and the following disclaimer in the
     58  1.1  ichiro  *    documentation and/or other materials provided with the distribution.
     59  1.1  ichiro  * 3. All advertising materials mentioning features or use of this software
     60  1.1  ichiro  *    must display the following acknowledgement:
     61  1.1  ichiro  *        This product includes software developed by the NetBSD
     62  1.1  ichiro  *        Foundation, Inc. and its contributors.
     63  1.1  ichiro  * 4. Neither the name of The NetBSD Foundation nor the names of its
     64  1.1  ichiro  *    contributors may be used to endorse or promote products derived
     65  1.1  ichiro  *    from this software without specific prior written permission.
     66  1.1  ichiro  *
     67  1.1  ichiro  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     68  1.1  ichiro  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     69  1.1  ichiro  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     70  1.1  ichiro  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     71  1.1  ichiro  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     72  1.1  ichiro  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     73  1.1  ichiro  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     74  1.1  ichiro  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     75  1.1  ichiro  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     76  1.1  ichiro  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     77  1.1  ichiro  * POSSIBILITY OF SUCH DAMAGE.
     78  1.1  ichiro  */
     79  1.1  ichiro 
     80  1.1  ichiro #include <sys/cdefs.h>
     81  1.1  ichiro __KERNEL_RCSID(0, "$NetBSD: uhmodem.c,v 1.1 2008/01/21 11:36:47 ichiro Exp $");
     82  1.1  ichiro 
     83  1.1  ichiro #include <sys/param.h>
     84  1.1  ichiro #include <sys/systm.h>
     85  1.1  ichiro #include <sys/kernel.h>
     86  1.1  ichiro #include <sys/malloc.h>
     87  1.1  ichiro #include <sys/ioccom.h>
     88  1.1  ichiro #include <sys/fcntl.h>
     89  1.1  ichiro #include <sys/conf.h>
     90  1.1  ichiro #include <sys/tty.h>
     91  1.1  ichiro #include <sys/file.h>
     92  1.1  ichiro #include <sys/select.h>
     93  1.1  ichiro #include <sys/proc.h>
     94  1.1  ichiro #include <sys/device.h>
     95  1.1  ichiro #include <sys/poll.h>
     96  1.1  ichiro #include <sys/sysctl.h>
     97  1.1  ichiro #include <sys/bus.h>
     98  1.1  ichiro 
     99  1.1  ichiro #include <dev/usb/usb.h>
    100  1.1  ichiro #include <dev/usb/usbdi.h>
    101  1.1  ichiro #include <dev/usb/usbdi_util.h>
    102  1.1  ichiro #include <dev/usb/usbdivar.h>
    103  1.1  ichiro 
    104  1.1  ichiro #include <dev/usb/usbcdc.h>
    105  1.1  ichiro #include <dev/usb/usbdevs.h>
    106  1.1  ichiro #include <dev/usb/usb_quirks.h>
    107  1.1  ichiro #include <dev/usb/ucomvar.h>
    108  1.1  ichiro #include <dev/usb/ubsavar.h>
    109  1.1  ichiro 
    110  1.1  ichiro 
    111  1.1  ichiro #define UHMODEMIBUFSIZE	4096
    112  1.1  ichiro #define UHMODEMOBUFSIZE	4096
    113  1.1  ichiro 
    114  1.1  ichiro #ifdef UHMODEM_DEBUG
    115  1.1  ichiro Static int	uhmodemdebug = 0;
    116  1.1  ichiro #define DPRINTFN(n, x)  do { \
    117  1.1  ichiro 				if (uhmodemdebug > (n)) \
    118  1.1  ichiro 					logprintf x; \
    119  1.1  ichiro 			} while (0)
    120  1.1  ichiro #else
    121  1.1  ichiro #define DPRINTFN(n, x)
    122  1.1  ichiro #endif
    123  1.1  ichiro #define DPRINTF(x) DPRINTFN(0, x)
    124  1.1  ichiro 
    125  1.1  ichiro Static int uhmodem_open(void *, int);
    126  1.1  ichiro Static  usbd_status e220_modechange_request(usbd_device_handle);
    127  1.1  ichiro Static	usbd_status e220_endpointhalt(usbd_device_handle);
    128  1.1  ichiro #if 0
    129  1.1  ichiro Static  usbd_status e220_testreq(usbd_device_handle);
    130  1.1  ichiro #endif
    131  1.1  ichiro void e220_modechange_request_test(usbd_device_handle);
    132  1.1  ichiro 
    133  1.1  ichiro #define UHMODEM_MAXCONN		2
    134  1.1  ichiro 
    135  1.1  ichiro struct	uhmodem_softc {
    136  1.1  ichiro 	struct ubsa_softc	sc_ubsa;
    137  1.1  ichiro };
    138  1.1  ichiro 
    139  1.1  ichiro struct	ucom_methods uhmodem_methods = {
    140  1.1  ichiro 	ubsa_get_status,
    141  1.1  ichiro 	ubsa_set,
    142  1.1  ichiro 	ubsa_param,
    143  1.1  ichiro 	NULL,
    144  1.1  ichiro 	uhmodem_open,
    145  1.1  ichiro 	ubsa_close,
    146  1.1  ichiro 	NULL,
    147  1.1  ichiro 	NULL
    148  1.1  ichiro };
    149  1.1  ichiro 
    150  1.1  ichiro Static const struct usb_devno uhmodem_devs[] = {
    151  1.1  ichiro 	/* HUAWEI E220 / Emobile D0[12]HW */
    152  1.1  ichiro 	{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 },
    153  1.1  ichiro };
    154  1.1  ichiro #define uhmodem_lookup(v, p) usb_lookup(uhmodem_devs, v, p)
    155  1.1  ichiro 
    156  1.1  ichiro USB_DECLARE_DRIVER(uhmodem);
    157  1.1  ichiro 
    158  1.1  ichiro USB_MATCH(uhmodem)
    159  1.1  ichiro {
    160  1.1  ichiro 	USB_IFMATCH_START(uhmodem, uaa);
    161  1.1  ichiro 
    162  1.1  ichiro 	if (uhmodem_lookup(uaa->vendor, uaa->product) != NULL)
    163  1.1  ichiro 		/* XXX interface# 0,1 provide modem function, but this driver
    164  1.1  ichiro 		   handles all modem in single device.  */
    165  1.1  ichiro 		if (uaa->ifaceno == 0)
    166  1.1  ichiro 			return (UMATCH_VENDOR_PRODUCT);
    167  1.1  ichiro 	return (UMATCH_NONE);
    168  1.1  ichiro }
    169  1.1  ichiro 
    170  1.1  ichiro USB_ATTACH(uhmodem)
    171  1.1  ichiro {
    172  1.1  ichiro 	USB_IFATTACH_START(uhmodem, sc, uaa);
    173  1.1  ichiro 	usbd_device_handle dev = uaa->device;
    174  1.1  ichiro 	usb_config_descriptor_t *cdesc;
    175  1.1  ichiro 	usb_interface_descriptor_t *id;
    176  1.1  ichiro 	usb_endpoint_descriptor_t *ed;
    177  1.1  ichiro 	char *devinfop;
    178  1.1  ichiro 	const char *devname = USBDEVNAME(sc->sc_ubsa.sc_dev);
    179  1.1  ichiro 	usbd_status err;
    180  1.1  ichiro 	struct ucom_attach_args uca;
    181  1.1  ichiro 	int i;
    182  1.1  ichiro 	int j;
    183  1.1  ichiro 	char comname[16];
    184  1.1  ichiro 
    185  1.1  ichiro 	devinfop = usbd_devinfo_alloc(dev, 0);
    186  1.1  ichiro 	USB_ATTACH_SETUP;
    187  1.1  ichiro 	printf("%s: %s\n", devname, devinfop);
    188  1.1  ichiro 	usbd_devinfo_free(devinfop);
    189  1.1  ichiro 
    190  1.1  ichiro         sc->sc_ubsa.sc_udev = dev;
    191  1.1  ichiro 	sc->sc_ubsa.sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
    192  1.1  ichiro 	sc->sc_ubsa.sc_numif = 1; /* defaut device has one interface */
    193  1.1  ichiro 
    194  1.1  ichiro 	/* Hauwei E220 need special request to change its mode to modem */
    195  1.1  ichiro 	if ((uaa->ifaceno == 0) && (uaa->class != 255)) {
    196  1.1  ichiro 		err = e220_modechange_request(dev);
    197  1.1  ichiro 		if (err) {
    198  1.1  ichiro 			printf("%s: failed to change mode: %s\n",
    199  1.1  ichiro 				devname, usbd_errstr(err));
    200  1.1  ichiro 			sc->sc_ubsa.sc_dying = 1;
    201  1.1  ichiro 			goto error;
    202  1.1  ichiro 		}
    203  1.1  ichiro 		printf("%s: mass storage only mode, reattach to enable modem\n",
    204  1.1  ichiro 			devname);
    205  1.1  ichiro 		sc->sc_ubsa.sc_dying = 1;
    206  1.1  ichiro 		goto error;
    207  1.1  ichiro 	}
    208  1.1  ichiro 
    209  1.1  ichiro 	/*
    210  1.1  ichiro 	 * initialize rts, dtr variables to something
    211  1.1  ichiro 	 * different from boolean 0, 1
    212  1.1  ichiro 	 */
    213  1.1  ichiro 	sc->sc_ubsa.sc_dtr = -1;
    214  1.1  ichiro 	sc->sc_ubsa.sc_rts = -1;
    215  1.1  ichiro 
    216  1.1  ichiro 	sc->sc_ubsa.sc_quadumts = 1;
    217  1.1  ichiro 	sc->sc_ubsa.sc_config_index = 0;
    218  1.1  ichiro 	sc->sc_ubsa.sc_numif = 2; /* E220 has 2coms */
    219  1.1  ichiro 
    220  1.1  ichiro 	DPRINTF(("uhmodem attach: sc = %p\n", sc));
    221  1.1  ichiro 
    222  1.1  ichiro 	/* Move the device into the configured state. */
    223  1.1  ichiro 	err = usbd_set_config_index(dev, sc->sc_ubsa.sc_config_index, 1);
    224  1.1  ichiro 	if (err) {
    225  1.1  ichiro 		printf("%s: failed to set configuration: %s\n",
    226  1.1  ichiro 		    devname, usbd_errstr(err));
    227  1.1  ichiro 		sc->sc_ubsa.sc_dying = 1;
    228  1.1  ichiro 		goto error;
    229  1.1  ichiro 	}
    230  1.1  ichiro 
    231  1.1  ichiro 	/* get the config descriptor */
    232  1.1  ichiro 	cdesc = usbd_get_config_descriptor(sc->sc_ubsa.sc_udev);
    233  1.1  ichiro 	if (cdesc == NULL) {
    234  1.1  ichiro 		printf("%s: failed to get configuration descriptor\n",
    235  1.1  ichiro 		    devname);
    236  1.1  ichiro 		sc->sc_ubsa.sc_dying = 1;
    237  1.1  ichiro 		goto error;
    238  1.1  ichiro 	}
    239  1.1  ichiro 
    240  1.1  ichiro 	sc->sc_ubsa.sc_intr_number = -1;
    241  1.1  ichiro 	sc->sc_ubsa.sc_intr_pipe = NULL;
    242  1.1  ichiro 
    243  1.1  ichiro 	/* get the interfaces */
    244  1.1  ichiro 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    245  1.1  ichiro 		err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET+i,
    246  1.1  ichiro 				 &sc->sc_ubsa.sc_iface[i]);
    247  1.1  ichiro 		if (err) {
    248  1.1  ichiro 			if (i == 0){
    249  1.1  ichiro 				/* can not get main interface */
    250  1.1  ichiro 				sc->sc_ubsa.sc_dying = 1;
    251  1.1  ichiro 				goto error;
    252  1.1  ichiro 			} else
    253  1.1  ichiro 				break;
    254  1.1  ichiro 		}
    255  1.1  ichiro 
    256  1.1  ichiro 		/* Find the endpoints */
    257  1.1  ichiro 		id = usbd_get_interface_descriptor(sc->sc_ubsa.sc_iface[i]);
    258  1.1  ichiro 		sc->sc_ubsa.sc_iface_number[i] = id->bInterfaceNumber;
    259  1.1  ichiro 
    260  1.1  ichiro 		/* initialize endpoints */
    261  1.1  ichiro 		uca.bulkin = uca.bulkout = -1;
    262  1.1  ichiro 
    263  1.1  ichiro 		for (j = 0; j < id->bNumEndpoints; j++) {
    264  1.1  ichiro 			ed = usbd_interface2endpoint_descriptor(
    265  1.1  ichiro 				sc->sc_ubsa.sc_iface[i], j);
    266  1.1  ichiro 			if (ed == NULL) {
    267  1.1  ichiro 				printf("%s: no endpoint descriptor for %d (interface: %d)\n",
    268  1.1  ichiro 			    		devname, j, i);
    269  1.1  ichiro 				break;
    270  1.1  ichiro 			}
    271  1.1  ichiro 
    272  1.1  ichiro 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    273  1.1  ichiro 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    274  1.1  ichiro 				sc->sc_ubsa.sc_intr_number = ed->bEndpointAddress;
    275  1.1  ichiro 				sc->sc_ubsa.sc_isize = UGETW(ed->wMaxPacketSize);
    276  1.1  ichiro 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    277  1.1  ichiro 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    278  1.1  ichiro 				uca.bulkin = ed->bEndpointAddress;
    279  1.1  ichiro 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    280  1.1  ichiro 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    281  1.1  ichiro 				uca.bulkout = ed->bEndpointAddress;
    282  1.1  ichiro 			}
    283  1.1  ichiro 		} /* end of Endpoint loop */
    284  1.1  ichiro 
    285  1.1  ichiro 		if (sc->sc_ubsa.sc_intr_number == -1) {
    286  1.1  ichiro 			printf("%s: HUAWEI E220 need to re-attach to enable modem function\n", devname);
    287  1.1  ichiro 			if (i == 0) {
    288  1.1  ichiro 				/* could not get intr for main tty */
    289  1.1  ichiro 				sc->sc_ubsa.sc_dying = 1;
    290  1.1  ichiro 				goto error;
    291  1.1  ichiro 			} else
    292  1.1  ichiro 				break;
    293  1.1  ichiro 		}
    294  1.1  ichiro 		if (uca.bulkin == -1) {
    295  1.1  ichiro 			printf("%s: Could not find data bulk in\n", devname);
    296  1.1  ichiro 			sc->sc_ubsa.sc_dying = 1;
    297  1.1  ichiro 			goto error;
    298  1.1  ichiro 		}
    299  1.1  ichiro 
    300  1.1  ichiro 		if (uca.bulkout == -1) {
    301  1.1  ichiro 			printf("%s: Could not find data bulk out\n", devname);
    302  1.1  ichiro 			sc->sc_ubsa.sc_dying = 1;
    303  1.1  ichiro 			goto error;
    304  1.1  ichiro 		}
    305  1.1  ichiro 
    306  1.1  ichiro 		switch (i) {
    307  1.1  ichiro 		case 0:
    308  1.1  ichiro 			sprintf(comname, "modem");
    309  1.1  ichiro 			break;
    310  1.1  ichiro 		case 1:
    311  1.1  ichiro 			sprintf(comname, "monitor");
    312  1.1  ichiro 			break;
    313  1.1  ichiro 		case 2:
    314  1.1  ichiro 			sprintf(comname, "unknown");
    315  1.1  ichiro 			break;
    316  1.1  ichiro 		default:
    317  1.1  ichiro 			sprintf(comname, "int#%d", i);
    318  1.1  ichiro 		}
    319  1.1  ichiro 
    320  1.1  ichiro 		uca.portno = i;
    321  1.1  ichiro 		/* bulkin, bulkout set above */
    322  1.1  ichiro 		uca.ibufsize = UHMODEMIBUFSIZE;
    323  1.1  ichiro 		uca.obufsize = UHMODEMOBUFSIZE;
    324  1.1  ichiro 		uca.ibufsizepad = UHMODEMIBUFSIZE;
    325  1.1  ichiro 		uca.opkthdrlen = 0;
    326  1.1  ichiro 		uca.device = dev;
    327  1.1  ichiro 		uca.iface = sc->sc_ubsa.sc_iface[i];
    328  1.1  ichiro 		uca.methods = &uhmodem_methods;
    329  1.1  ichiro 		uca.arg = &sc->sc_ubsa;
    330  1.1  ichiro 		uca.info = comname;
    331  1.1  ichiro 		DPRINTF(("uhmodem: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
    332  1.1  ichiro 	    		i, uca.bulkin, uca.bulkout, sc->sc_ubsa.sc_intr_number));
    333  1.1  ichiro 		sc->sc_ubsa.sc_subdevs[i] = config_found_sm_loc(self, "ucombus", NULL,
    334  1.1  ichiro 				 &uca, ucomprint, ucomsubmatch);
    335  1.1  ichiro 	} /* end of Interface loop */
    336  1.1  ichiro 
    337  1.1  ichiro 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_ubsa.sc_udev,
    338  1.1  ichiro 			   USBDEV(sc->sc_ubsa.sc_dev));
    339  1.1  ichiro 
    340  1.1  ichiro 	USB_ATTACH_SUCCESS_RETURN;
    341  1.1  ichiro 
    342  1.1  ichiro error:
    343  1.1  ichiro 	USB_ATTACH_ERROR_RETURN;
    344  1.1  ichiro }
    345  1.1  ichiro 
    346  1.1  ichiro USB_DETACH(uhmodem)
    347  1.1  ichiro {
    348  1.1  ichiro 	USB_DETACH_START(uhmodem, sc);
    349  1.1  ichiro 	int i;
    350  1.1  ichiro 	int rv = 0;
    351  1.1  ichiro 
    352  1.1  ichiro 	DPRINTF(("uhmodem_detach: sc = %p\n", sc));
    353  1.1  ichiro 
    354  1.1  ichiro 	if (sc->sc_ubsa.sc_intr_pipe != NULL) {
    355  1.1  ichiro 		usbd_abort_pipe(sc->sc_ubsa.sc_intr_pipe);
    356  1.1  ichiro 		usbd_close_pipe(sc->sc_ubsa.sc_intr_pipe);
    357  1.1  ichiro 		free(sc->sc_ubsa.sc_intr_buf, M_USBDEV);
    358  1.1  ichiro 		sc->sc_ubsa.sc_intr_pipe = NULL;
    359  1.1  ichiro 	}
    360  1.1  ichiro 
    361  1.1  ichiro 	sc->sc_ubsa.sc_dying = 1;
    362  1.1  ichiro 	for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    363  1.1  ichiro 		if (sc->sc_ubsa.sc_subdevs[i] != NULL) {
    364  1.1  ichiro 			rv |= config_detach(sc->sc_ubsa.sc_subdevs[i], flags);
    365  1.1  ichiro 			sc->sc_ubsa.sc_subdevs[i] = NULL;
    366  1.1  ichiro 		}
    367  1.1  ichiro 	}
    368  1.1  ichiro 
    369  1.1  ichiro 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ubsa.sc_udev,
    370  1.1  ichiro 			   USBDEV(sc->sc_ubsa.sc_dev));
    371  1.1  ichiro 
    372  1.1  ichiro 	return (rv);
    373  1.1  ichiro }
    374  1.1  ichiro 
    375  1.1  ichiro int
    376  1.1  ichiro uhmodem_activate(device_ptr_t self, enum devact act)
    377  1.1  ichiro {
    378  1.1  ichiro 	struct uhmodem_softc *sc = (struct uhmodem_softc *)self;
    379  1.1  ichiro 	int rv = 0;
    380  1.1  ichiro 	int i;
    381  1.1  ichiro 
    382  1.1  ichiro 	switch (act) {
    383  1.1  ichiro 	case DVACT_ACTIVATE:
    384  1.1  ichiro 		return (EOPNOTSUPP);
    385  1.1  ichiro 
    386  1.1  ichiro 	case DVACT_DEACTIVATE:
    387  1.1  ichiro 		for (i = 0; i < sc->sc_ubsa.sc_numif; i++) {
    388  1.1  ichiro 			if (sc->sc_ubsa.sc_subdevs[i] != NULL)
    389  1.1  ichiro 				rv |= config_deactivate(sc->sc_ubsa.sc_subdevs[i]);
    390  1.1  ichiro 		}
    391  1.1  ichiro 		sc->sc_ubsa.sc_dying = 1;
    392  1.1  ichiro 		break;
    393  1.1  ichiro 	}
    394  1.1  ichiro 	return (rv);
    395  1.1  ichiro }
    396  1.1  ichiro 
    397  1.1  ichiro Static int
    398  1.1  ichiro uhmodem_open(void *addr, int portno)
    399  1.1  ichiro {
    400  1.1  ichiro 	struct ubsa_softc *sc = addr;
    401  1.1  ichiro 	usbd_status err;
    402  1.1  ichiro 
    403  1.1  ichiro 	if (sc->sc_dying)
    404  1.1  ichiro 		return (ENXIO);
    405  1.1  ichiro 
    406  1.1  ichiro 	DPRINTF(("%s: sc = %p\n", __func__, sc));
    407  1.1  ichiro 
    408  1.1  ichiro 	err = e220_endpointhalt(sc->sc_udev);
    409  1.1  ichiro 	if (err)
    410  1.1  ichiro 		printf("%s: endpointhalt fail\n", __func__);
    411  1.1  ichiro 	else
    412  1.1  ichiro 		usbd_delay_ms(sc->sc_udev, 50);
    413  1.1  ichiro #if 0 /* currenly disable */
    414  1.1  ichiro 	err = e220_testreq(sc->sc_udev);
    415  1.1  ichiro 	if (err)
    416  1.1  ichiro 		printf("%s: send testreq fail\n", __func__);
    417  1.1  ichiro 	else
    418  1.1  ichiro 		usbd_delay_ms(sc->sc_udev, 50);
    419  1.1  ichiro #endif
    420  1.1  ichiro 
    421  1.1  ichiro 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    422  1.1  ichiro 		sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    423  1.1  ichiro 		/* XXX only iface# = 0 has intr line */
    424  1.1  ichiro 		/* XXX E220 specific? need to check */
    425  1.1  ichiro 		err = usbd_open_pipe_intr(sc->sc_iface[0],
    426  1.1  ichiro 		    sc->sc_intr_number,
    427  1.1  ichiro 		    USBD_SHORT_XFER_OK,
    428  1.1  ichiro 		    &sc->sc_intr_pipe,
    429  1.1  ichiro 		    sc,
    430  1.1  ichiro 		    sc->sc_intr_buf,
    431  1.1  ichiro 		    sc->sc_isize,
    432  1.1  ichiro 		    ubsa_intr,
    433  1.1  ichiro 		    UBSA_INTR_INTERVAL);
    434  1.1  ichiro 		if (err) {
    435  1.1  ichiro 			printf("%s: cannot open interrupt pipe (addr %d)\n",
    436  1.1  ichiro 			    USBDEVNAME(sc->sc_dev),
    437  1.1  ichiro 			    sc->sc_intr_number);
    438  1.1  ichiro 			return (EIO);
    439  1.1  ichiro 		}
    440  1.1  ichiro 	}
    441  1.1  ichiro 
    442  1.1  ichiro 	return (0);
    443  1.1  ichiro }
    444  1.1  ichiro 
    445  1.1  ichiro /*
    446  1.1  ichiro  * Hauwei E220 needs special request to enable modem function.
    447  1.1  ichiro  * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2.
    448  1.1  ichiro  */
    449  1.1  ichiro Static  usbd_status
    450  1.1  ichiro e220_modechange_request(usbd_device_handle dev)
    451  1.1  ichiro {
    452  1.1  ichiro #define E220_MODE_CHANGE_REQUEST 0x2
    453  1.1  ichiro 	usb_device_request_t req;
    454  1.1  ichiro 	usbd_status err;
    455  1.1  ichiro 
    456  1.1  ichiro 	req.bmRequestType = UT_WRITE_DEVICE;
    457  1.1  ichiro 	req.bRequest = UR_SET_FEATURE;
    458  1.1  ichiro 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    459  1.1  ichiro 	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
    460  1.1  ichiro 	USETW(req.wLength, 0);
    461  1.1  ichiro 
    462  1.1  ichiro 	DPRINTF(("%s: send e220 mode change request\n", __func__));
    463  1.1  ichiro 	err = usbd_do_request(dev, &req, 0);
    464  1.1  ichiro 	if (err) {
    465  1.1  ichiro 		DPRINTF(("%s: E220 mode change fail\n", __func__));
    466  1.1  ichiro 		return (EIO);
    467  1.1  ichiro 	}
    468  1.1  ichiro 
    469  1.1  ichiro 	return (0);
    470  1.1  ichiro #undef E220_MODE_CHANGE_REQUEST
    471  1.1  ichiro }
    472  1.1  ichiro 
    473  1.1  ichiro Static  usbd_status
    474  1.1  ichiro e220_endpointhalt(usbd_device_handle dev)
    475  1.1  ichiro {
    476  1.1  ichiro 	usb_device_request_t req;
    477  1.1  ichiro 	usbd_status err;
    478  1.1  ichiro 
    479  1.1  ichiro 	/* CLEAR feature / endpoint halt to modem i/o */
    480  1.1  ichiro 	req.bmRequestType = UT_WRITE_ENDPOINT;
    481  1.1  ichiro 	req.bRequest = UR_CLEAR_FEATURE;
    482  1.1  ichiro 	USETW(req.wValue, UF_ENDPOINT_HALT);
    483  1.1  ichiro 	USETW(req.wIndex, 0x0082); /* should get value from softc etc.*/
    484  1.1  ichiro 	USETW(req.wLength, 0);
    485  1.1  ichiro 	err = usbd_do_request(dev, &req, 0);
    486  1.1  ichiro 	if (err) {
    487  1.1  ichiro 		DPRINTF(("%s: E220 request test ENDPOINT_HALT fail\n", __func__));
    488  1.1  ichiro 		return (EIO);
    489  1.1  ichiro 	}
    490  1.1  ichiro 	req.bmRequestType = UT_WRITE_ENDPOINT;
    491  1.1  ichiro 	req.bRequest = UR_CLEAR_FEATURE;
    492  1.1  ichiro 	USETW(req.wValue, UF_ENDPOINT_HALT);
    493  1.1  ichiro 	USETW(req.wIndex, 0x0002); /* should get value from softc etc.*/
    494  1.1  ichiro 	USETW(req.wLength, 0);
    495  1.1  ichiro 	err = usbd_do_request(dev, &req, 0);
    496  1.1  ichiro 	if (err) {
    497  1.1  ichiro 		DPRINTF(("%s: E220 request test ENDPOINT_HALT fail\n", __func__));
    498  1.1  ichiro 		return (EIO);
    499  1.1  ichiro 	}
    500  1.1  ichiro 
    501  1.1  ichiro 	return (0);
    502  1.1  ichiro }
    503  1.1  ichiro 
    504  1.1  ichiro #if 0
    505  1.1  ichiro /*
    506  1.1  ichiro  * Windows device driver send these sequens of USB requests.
    507  1.1  ichiro  * However currently I can't understand what the messege is,
    508  1.1  ichiro  * disable this code when I get more information about it.
    509  1.1  ichiro  */
    510  1.1  ichiro Static  usbd_status
    511  1.1  ichiro e220_testreq(usbd_device_handle dev)
    512  1.1  ichiro {
    513  1.1  ichiro 	uint8_t data[8];
    514  1.1  ichiro 	usb_device_request_t req;
    515  1.1  ichiro 	usbd_status err;
    516  1.1  ichiro 	int i;
    517  1.1  ichiro 
    518  1.1  ichiro 	/* vendor specific unknown requres */
    519  1.1  ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    520  1.1  ichiro 	req.bRequest = 0x02;
    521  1.1  ichiro 	USETW(req.wValue, 0x0001);
    522  1.1  ichiro 	USETW(req.wIndex, 0x0000);
    523  1.1  ichiro 	USETW(req.wLength, 2);
    524  1.1  ichiro 	data[0] = 0x0;
    525  1.1  ichiro 	data[1] = 0x0;
    526  1.1  ichiro 	err = usbd_do_request(dev, &req, data);
    527  1.1  ichiro 	if (err)
    528  1.1  ichiro 		goto error;
    529  1.1  ichiro 
    530  1.1  ichiro 	/* vendor specific unknown sequence */
    531  1.1  ichiro #define E220_CLASS_SETUP	0x22
    532  1.1  ichiro #define	E220_CLASS_READ		0x21
    533  1.1  ichiro #define	E220_CLASS_WRITE	0x20
    534  1.1  ichiro 
    535  1.1  ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    536  1.1  ichiro 	req.bRequest = E220_CLASS_SETUP;	// 0x22
    537  1.1  ichiro 	USETW(req.wValue, 0x0001);
    538  1.1  ichiro 	USETW(req.wIndex, 0x0000);
    539  1.1  ichiro 	USETW(req.wLength, 0);
    540  1.1  ichiro 	err = usbd_do_request(dev, &req, 0);
    541  1.1  ichiro 	if (err)
    542  1.1  ichiro 		goto error;
    543  1.1  ichiro 
    544  1.1  ichiro 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    545  1.1  ichiro 	req.bRequest = E220_CLASS_READ;
    546  1.1  ichiro 	USETW(req.wValue, 0x0000);
    547  1.1  ichiro 	USETW(req.wIndex, 0x0000);
    548  1.1  ichiro 	USETW(req.wLength, 7);
    549  1.1  ichiro 	err = usbd_do_request(dev, &req, &data);
    550  1.1  ichiro 	if (err)
    551  1.1  ichiro 		goto error;
    552  1.1  ichiro 
    553  1.1  ichiro 	data[1] = 0x8;
    554  1.1  ichiro 	data[2] = 0x7;
    555  1.1  ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    556  1.1  ichiro 	req.bRequest = E220_CLASS_WRITE;
    557  1.1  ichiro 	USETW(req.wValue, 0x0000);
    558  1.1  ichiro 	USETW(req.wIndex, 0x0000);
    559  1.1  ichiro 	USETW(req.wLength, 7);
    560  1.1  ichiro 	err = usbd_do_request(dev, &req, data);
    561  1.1  ichiro 	if (err)
    562  1.1  ichiro 		goto error;
    563  1.1  ichiro 
    564  1.1  ichiro 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    565  1.1  ichiro 	req.bRequest = E220_CLASS_READ;
    566  1.1  ichiro 	USETW(req.wValue, 0x0000);
    567  1.1  ichiro 	USETW(req.wIndex, 0x0000);
    568  1.1  ichiro 	USETW(req.wLength, 7);
    569  1.1  ichiro 	err = usbd_do_request(dev, &req, &data);
    570  1.1  ichiro 	if (err)
    571  1.1  ichiro 		goto error;
    572  1.1  ichiro 	printf("%s(0x21):", __func__);
    573  1.1  ichiro 
    574  1.1  ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    575  1.1  ichiro 	req.bRequest = E220_CLASS_SETUP;	// 0x22
    576  1.1  ichiro 	USETW(req.wValue, 0x0003);
    577  1.1  ichiro 	USETW(req.wIndex, 0x0000);
    578  1.1  ichiro 	USETW(req.wLength, 0);
    579  1.1  ichiro 	err = usbd_do_request(dev, &req, 0);
    580  1.1  ichiro 	if (err)
    581  1.1  ichiro 		goto error;
    582  1.1  ichiro 
    583  1.1  ichiro 	return (0);
    584  1.1  ichiro error:
    585  1.1  ichiro 	DPRINTF(("%s: E220 request test SETUP fail\n", __func__));
    586  1.1  ichiro 	return (EIO);
    587  1.1  ichiro }
    588  1.1  ichiro #endif
    589  1.1  ichiro 
    590