Home | History | Annotate | Line # | Download | only in usb
uhmodem.c revision 1.22.6.2
      1  1.22.6.2   thorpej /*	$NetBSD: uhmodem.c,v 1.22.6.2 2021/03/22 16:23:46 thorpej 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  *
     60       1.1    ichiro  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     61       1.1    ichiro  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     62       1.1    ichiro  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     63       1.1    ichiro  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     64       1.1    ichiro  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     65       1.1    ichiro  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     66       1.1    ichiro  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     67       1.1    ichiro  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     68       1.1    ichiro  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     69       1.1    ichiro  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     70       1.1    ichiro  * POSSIBILITY OF SUCH DAMAGE.
     71       1.1    ichiro  */
     72       1.1    ichiro 
     73       1.1    ichiro #include <sys/cdefs.h>
     74  1.22.6.2   thorpej __KERNEL_RCSID(0, "$NetBSD: uhmodem.c,v 1.22.6.2 2021/03/22 16:23:46 thorpej Exp $");
     75      1.15     skrll 
     76      1.15     skrll #ifdef _KERNEL_OPT
     77      1.15     skrll #include "opt_usb.h"
     78      1.15     skrll #endif
     79       1.1    ichiro 
     80       1.1    ichiro #include <sys/param.h>
     81       1.1    ichiro #include <sys/systm.h>
     82       1.1    ichiro #include <sys/kernel.h>
     83      1.14     skrll #include <sys/kmem.h>
     84       1.1    ichiro #include <sys/ioccom.h>
     85       1.1    ichiro #include <sys/fcntl.h>
     86       1.1    ichiro #include <sys/conf.h>
     87       1.1    ichiro #include <sys/tty.h>
     88       1.1    ichiro #include <sys/file.h>
     89       1.1    ichiro #include <sys/select.h>
     90       1.1    ichiro #include <sys/proc.h>
     91       1.1    ichiro #include <sys/device.h>
     92       1.1    ichiro #include <sys/poll.h>
     93       1.1    ichiro #include <sys/sysctl.h>
     94       1.1    ichiro #include <sys/bus.h>
     95       1.1    ichiro 
     96       1.1    ichiro #include <dev/usb/usb.h>
     97       1.1    ichiro #include <dev/usb/usbdi.h>
     98       1.1    ichiro #include <dev/usb/usbdi_util.h>
     99       1.1    ichiro #include <dev/usb/usbdivar.h>
    100       1.1    ichiro 
    101       1.1    ichiro #include <dev/usb/usbcdc.h>
    102       1.1    ichiro #include <dev/usb/usbdevs.h>
    103       1.1    ichiro #include <dev/usb/usb_quirks.h>
    104       1.1    ichiro #include <dev/usb/ucomvar.h>
    105       1.1    ichiro #include <dev/usb/ubsavar.h>
    106       1.1    ichiro 
    107       1.2    ichiro /* vendor specific bRequest */
    108       1.2    ichiro #define	UHMODEM_REGWRITE	0x20
    109       1.2    ichiro #define	UHMODEM_REGREAD		0x21
    110       1.2    ichiro #define UHMODEM_SETUP		0x22
    111       1.1    ichiro 
    112       1.1    ichiro #define UHMODEMIBUFSIZE	4096
    113       1.1    ichiro #define UHMODEMOBUFSIZE	4096
    114       1.1    ichiro 
    115       1.1    ichiro #ifdef UHMODEM_DEBUG
    116      1.17       mrg static int	uhmodemdebug = 0;
    117       1.1    ichiro #define DPRINTFN(n, x)  do { \
    118       1.1    ichiro 				if (uhmodemdebug > (n)) \
    119      1.10    dyoung 					printf x; \
    120       1.1    ichiro 			} while (0)
    121       1.1    ichiro #else
    122       1.1    ichiro #define DPRINTFN(n, x)
    123       1.1    ichiro #endif
    124       1.1    ichiro #define DPRINTF(x) DPRINTFN(0, x)
    125       1.1    ichiro 
    126      1.17       mrg static int	uhmodem_open(void *, int);
    127      1.17       mrg static usbd_status e220_modechange_request(struct usbd_device *);
    128      1.17       mrg static usbd_status uhmodem_endpointhalt(struct ubsa_softc *, int);
    129      1.17       mrg static usbd_status uhmodem_regwrite(struct usbd_device *, uint8_t *, size_t);
    130      1.17       mrg static usbd_status uhmodem_regread(struct usbd_device *, uint8_t *, size_t);
    131      1.17       mrg static usbd_status a2502_init(struct usbd_device *);
    132       1.1    ichiro #if 0
    133      1.17       mrg static usbd_status uhmodem_regsetup(struct usbd_device *, uint16_t);
    134      1.17       mrg static usbd_status e220_init(struct usbd_device *);
    135       1.1    ichiro #endif
    136       1.1    ichiro 
    137      1.17       mrg /* this driver uses the ubsa_softc and methods directly as-is. */
    138       1.1    ichiro struct	ucom_methods uhmodem_methods = {
    139      1.14     skrll 	.ucom_get_status = ubsa_get_status,
    140      1.14     skrll 	.ucom_set = ubsa_set,
    141      1.14     skrll 	.ucom_param = ubsa_param,
    142      1.14     skrll 	.ucom_open = uhmodem_open,
    143      1.14     skrll 	.ucom_close = ubsa_close,
    144       1.1    ichiro };
    145       1.1    ichiro 
    146       1.2    ichiro struct uhmodem_type {
    147       1.2    ichiro 	struct usb_devno	uhmodem_dev;
    148      1.14     skrll 	uint16_t		uhmodem_coms;	/* number of serial interfaces on the device */
    149      1.14     skrll 	uint16_t		uhmodem_flags;
    150       1.2    ichiro #define	E220	0x0001
    151       1.2    ichiro #define	A2502	0x0002
    152       1.4    ichiro #define	E620	0x0004		/* XXX */
    153       1.4    ichiro 				/* Whether or not it is a device different from E220 is not clear. */
    154       1.2    ichiro };
    155       1.2    ichiro 
    156      1.17       mrg static const struct uhmodem_type uhmodem_devs[] = {
    157       1.1    ichiro 	/* HUAWEI E220 / Emobile D0[12]HW */
    158       1.2    ichiro 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 }, 2,	E220},
    159       1.2    ichiro 	/* ANYDATA / NTT DoCoMo A2502 */
    160       1.2    ichiro 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_A2502 }, 3,	A2502},
    161       1.4    ichiro 	/* HUAWEI E620 */
    162       1.4    ichiro 	{{ USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE }, 3,   E620},
    163       1.1    ichiro };
    164       1.2    ichiro #define uhmodem_lookup(v, p) ((const struct uhmodem_type *)usb_lookup(uhmodem_devs, v, p))
    165       1.1    ichiro 
    166      1.17       mrg static int	uhmodem_match(device_t, cfdata_t, void *);
    167      1.17       mrg static void	uhmodem_attach(device_t, device_t, void *);
    168      1.17       mrg static void	uhmodem_childdet(device_t, device_t);
    169      1.17       mrg static int	uhmodem_detach(device_t, int);
    170      1.17       mrg 
    171      1.17       mrg CFATTACH_DECL2_NEW(uhmodem, sizeof(struct ubsa_softc), uhmodem_match,
    172      1.19       mrg     uhmodem_attach, uhmodem_detach, NULL, NULL, uhmodem_childdet);
    173       1.1    ichiro 
    174      1.17       mrg static int
    175      1.10    dyoung uhmodem_match(device_t parent, cfdata_t match, void *aux)
    176       1.1    ichiro {
    177      1.14     skrll 	struct usbif_attach_arg *uiaa = aux;
    178       1.1    ichiro 
    179      1.14     skrll 	if (uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL)
    180       1.1    ichiro 		/* XXX interface# 0,1 provide modem function, but this driver
    181       1.1    ichiro 		   handles all modem in single device.  */
    182      1.14     skrll 		if (uiaa->uiaa_ifaceno == 0)
    183      1.14     skrll 			return UMATCH_VENDOR_PRODUCT;
    184      1.14     skrll 	return UMATCH_NONE;
    185       1.1    ichiro }
    186       1.1    ichiro 
    187      1.17       mrg static void
    188      1.10    dyoung uhmodem_attach(device_t parent, device_t self, void *aux)
    189       1.1    ichiro {
    190      1.17       mrg 	struct ubsa_softc *sc = device_private(self);
    191      1.14     skrll 	struct usbif_attach_arg *uiaa = aux;
    192      1.14     skrll 	struct usbd_device *dev = uiaa->uiaa_device;
    193       1.1    ichiro 	usb_config_descriptor_t *cdesc;
    194       1.1    ichiro 	usb_interface_descriptor_t *id;
    195       1.1    ichiro 	usb_endpoint_descriptor_t *ed;
    196       1.1    ichiro 	char *devinfop;
    197       1.1    ichiro 	usbd_status err;
    198      1.14     skrll 	struct ucom_attach_args ucaa;
    199       1.1    ichiro 	int i;
    200       1.1    ichiro 	int j;
    201       1.1    ichiro 	char comname[16];
    202       1.1    ichiro 
    203       1.8    plunky 	aprint_naive("\n");
    204       1.8    plunky 	aprint_normal("\n");
    205       1.8    plunky 
    206       1.1    ichiro 	devinfop = usbd_devinfo_alloc(dev, 0);
    207       1.7      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    208       1.1    ichiro 	usbd_devinfo_free(devinfop);
    209       1.1    ichiro 
    210      1.17       mrg 	sc->sc_dev = self;
    211      1.17       mrg 	sc->sc_udev = dev;
    212      1.17       mrg 	sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
    213      1.19       mrg 	sc->sc_numif = 1; /* default device has one interface */
    214      1.19       mrg 	sc->sc_dying = false;
    215       1.1    ichiro 
    216       1.1    ichiro 	/* Hauwei E220 need special request to change its mode to modem */
    217      1.14     skrll 	if ((uiaa->uiaa_ifaceno == 0) && (uiaa->uiaa_class != 255)) {
    218       1.1    ichiro 		err = e220_modechange_request(dev);
    219       1.1    ichiro 		if (err) {
    220      1.14     skrll 			aprint_error_dev(self, "failed to change mode: %s\n",
    221       1.7      cube 				usbd_errstr(err));
    222       1.1    ichiro 			goto error;
    223       1.1    ichiro 		}
    224       1.7      cube 		aprint_error_dev(self,
    225       1.7      cube 		    "mass storage only mode, reattach to enable modem\n");
    226       1.1    ichiro 		goto error;
    227       1.1    ichiro 	}
    228       1.1    ichiro 
    229       1.1    ichiro 	/*
    230       1.1    ichiro 	 * initialize rts, dtr variables to something
    231       1.1    ichiro 	 * different from boolean 0, 1
    232       1.1    ichiro 	 */
    233      1.17       mrg 	sc->sc_dtr = -1;
    234      1.17       mrg 	sc->sc_rts = -1;
    235       1.1    ichiro 
    236      1.17       mrg 	sc->sc_quadumts = 1;
    237      1.17       mrg 	sc->sc_config_index = 0;
    238      1.17       mrg 	sc->sc_numif = uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product)->uhmodem_coms;
    239      1.17       mrg 	sc->sc_devflags = uhmodem_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product)->uhmodem_flags;
    240       1.1    ichiro 
    241       1.1    ichiro 	DPRINTF(("uhmodem attach: sc = %p\n", sc));
    242       1.1    ichiro 
    243       1.1    ichiro 	/* Move the device into the configured state. */
    244      1.17       mrg 	err = usbd_set_config_index(dev, sc->sc_config_index, 1);
    245       1.1    ichiro 	if (err) {
    246       1.7      cube 		aprint_error_dev(self, "failed to set configuration: %s\n",
    247       1.7      cube 		    usbd_errstr(err));
    248       1.1    ichiro 		goto error;
    249       1.1    ichiro 	}
    250       1.1    ichiro 
    251       1.1    ichiro 	/* get the config descriptor */
    252      1.17       mrg 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
    253       1.1    ichiro 	if (cdesc == NULL) {
    254       1.7      cube 		aprint_error_dev(self,
    255       1.7      cube 		    "failed to get configuration descriptor\n");
    256       1.1    ichiro 		goto error;
    257       1.1    ichiro 	}
    258       1.1    ichiro 
    259      1.17       mrg 	sc->sc_intr_number = -1;
    260      1.17       mrg 	sc->sc_intr_pipe = NULL;
    261       1.1    ichiro 
    262       1.1    ichiro 	/* get the interfaces */
    263      1.17       mrg 	for (i = 0; i < sc->sc_numif; i++) {
    264       1.1    ichiro 		err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET+i,
    265      1.17       mrg 				 &sc->sc_iface[i]);
    266       1.1    ichiro 		if (err) {
    267       1.1    ichiro 			if (i == 0){
    268       1.1    ichiro 				/* can not get main interface */
    269       1.1    ichiro 				goto error;
    270       1.1    ichiro 			} else
    271       1.1    ichiro 				break;
    272       1.1    ichiro 		}
    273       1.1    ichiro 
    274       1.1    ichiro 		/* Find the endpoints */
    275      1.17       mrg 		id = usbd_get_interface_descriptor(sc->sc_iface[i]);
    276      1.17       mrg 		sc->sc_iface_number[i] = id->bInterfaceNumber;
    277       1.1    ichiro 
    278       1.1    ichiro 		/* initialize endpoints */
    279      1.14     skrll 		ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
    280       1.1    ichiro 
    281       1.1    ichiro 		for (j = 0; j < id->bNumEndpoints; j++) {
    282       1.1    ichiro 			ed = usbd_interface2endpoint_descriptor(
    283      1.17       mrg 				sc->sc_iface[i], j);
    284       1.1    ichiro 			if (ed == NULL) {
    285       1.7      cube 				aprint_error_dev(self,
    286       1.7      cube 				    "no endpoint descriptor for %d "
    287       1.7      cube 				    "(interface: %d)\n", j, i);
    288       1.1    ichiro 				break;
    289       1.1    ichiro 			}
    290       1.1    ichiro 
    291       1.1    ichiro 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    292       1.1    ichiro 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    293      1.17       mrg 				sc->sc_intr_number = ed->bEndpointAddress;
    294      1.17       mrg 				sc->sc_isize = UGETW(ed->wMaxPacketSize);
    295       1.1    ichiro 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    296       1.1    ichiro 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    297      1.14     skrll 				ucaa.ucaa_bulkin = ed->bEndpointAddress;
    298       1.1    ichiro 			} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    299       1.1    ichiro 			    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    300      1.14     skrll 				ucaa.ucaa_bulkout = ed->bEndpointAddress;
    301       1.1    ichiro 			}
    302       1.1    ichiro 		} /* end of Endpoint loop */
    303       1.1    ichiro 
    304      1.17       mrg 		if (sc->sc_intr_number == -1) {
    305       1.7      cube 			aprint_error_dev(self, "HUAWEI E220 need to re-attach "
    306       1.7      cube 			    "to enable modem function\n");
    307       1.1    ichiro 			if (i == 0) {
    308       1.1    ichiro 				/* could not get intr for main tty */
    309       1.1    ichiro 				goto error;
    310       1.1    ichiro 			} else
    311       1.1    ichiro 				break;
    312       1.1    ichiro 		}
    313      1.14     skrll 		if (ucaa.ucaa_bulkin == -1) {
    314       1.7      cube 			aprint_error_dev(self,
    315       1.7      cube 			    "Could not find data bulk in\n");
    316       1.1    ichiro 			goto error;
    317       1.1    ichiro 		}
    318       1.1    ichiro 
    319      1.14     skrll 		if (ucaa.ucaa_bulkout == -1) {
    320       1.7      cube 			aprint_error_dev(self,
    321       1.7      cube 			    "Could not find data bulk out\n");
    322       1.1    ichiro 			goto error;
    323       1.1    ichiro 		}
    324       1.1    ichiro 
    325       1.1    ichiro 		switch (i) {
    326       1.1    ichiro 		case 0:
    327       1.5    cegger 			snprintf(comname, sizeof(comname), "modem");
    328       1.1    ichiro 			break;
    329       1.1    ichiro 		case 1:
    330       1.5    cegger 			snprintf(comname, sizeof(comname), "alt#1");
    331       1.1    ichiro 			break;
    332       1.1    ichiro 		case 2:
    333       1.5    cegger 			snprintf(comname, sizeof(comname), "alt#2");
    334       1.1    ichiro 			break;
    335       1.1    ichiro 		default:
    336       1.5    cegger 			snprintf(comname, sizeof(comname), "int#%d", i);
    337       1.5    cegger 			break;
    338       1.1    ichiro 		}
    339       1.1    ichiro 
    340      1.14     skrll 		ucaa.ucaa_portno = i;
    341      1.14     skrll 		/* ucaa_bulkin, ucaa_bulkout set above */
    342      1.14     skrll 		ucaa.ucaa_ibufsize = UHMODEMIBUFSIZE;
    343      1.14     skrll 		ucaa.ucaa_obufsize = UHMODEMOBUFSIZE;
    344      1.14     skrll 		ucaa.ucaa_ibufsizepad = UHMODEMIBUFSIZE;
    345      1.14     skrll 		ucaa.ucaa_opkthdrlen = 0;
    346      1.14     skrll 		ucaa.ucaa_device = dev;
    347      1.17       mrg 		ucaa.ucaa_iface = sc->sc_iface[i];
    348      1.14     skrll 		ucaa.ucaa_methods = &uhmodem_methods;
    349      1.18       mrg 		ucaa.ucaa_arg = sc;
    350      1.14     skrll 		ucaa.ucaa_info = comname;
    351      1.22  christos 		DPRINTF(("uhmodem: int#=%d, in = %#x, out = %#x, intr = %#x\n",
    352      1.14     skrll 		    i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
    353      1.17       mrg 		    sc->sc_intr_number));
    354  1.22.6.1   thorpej 		sc->sc_subdevs[i] = config_found(self, &ucaa, ucomprint,
    355  1.22.6.1   thorpej 						 CFARG_SUBMATCH, ucomsubmatch,
    356  1.22.6.1   thorpej 						 CFARG_EOL);
    357       1.2    ichiro 
    358       1.2    ichiro 		/* issue endpoint halt to each interface */
    359      1.18       mrg 		err = uhmodem_endpointhalt(sc, i);
    360      1.14     skrll 		if (err)
    361       1.7      cube 			aprint_error("%s: endpointhalt fail\n", __func__);
    362       1.2    ichiro 		else
    363      1.17       mrg 			usbd_delay_ms(sc->sc_udev, 50);
    364       1.1    ichiro 	} /* end of Interface loop */
    365       1.1    ichiro 
    366      1.17       mrg 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    367      1.17       mrg 			   sc->sc_dev);
    368       1.1    ichiro 
    369      1.20      maya 	if (!pmf_device_register(self, NULL, NULL))
    370      1.20      maya 		aprint_error_dev(self, "couldn't establish power handler\n");
    371      1.20      maya 
    372      1.10    dyoung 	return;
    373       1.1    ichiro 
    374       1.1    ichiro error:
    375      1.19       mrg 	sc->sc_dying = true;
    376      1.10    dyoung 	return;
    377       1.1    ichiro }
    378       1.1    ichiro 
    379      1.17       mrg static void
    380       1.3    dyoung uhmodem_childdet(device_t self, device_t child)
    381       1.3    dyoung {
    382       1.3    dyoung 	int i;
    383      1.17       mrg 	struct ubsa_softc *sc = device_private(self);
    384       1.3    dyoung 
    385      1.17       mrg 	for (i = 0; i < sc->sc_numif; i++) {
    386      1.17       mrg 		if (sc->sc_subdevs[i] == child)
    387       1.3    dyoung 			break;
    388       1.3    dyoung 	}
    389      1.17       mrg 	KASSERT(i < sc->sc_numif);
    390      1.17       mrg 	sc->sc_subdevs[i] = NULL;
    391       1.3    dyoung }
    392       1.3    dyoung 
    393      1.17       mrg static int
    394      1.10    dyoung uhmodem_detach(device_t self, int flags)
    395       1.1    ichiro {
    396      1.17       mrg 	struct ubsa_softc *sc = device_private(self);
    397       1.1    ichiro 	int i;
    398       1.1    ichiro 	int rv = 0;
    399       1.1    ichiro 
    400       1.1    ichiro 	DPRINTF(("uhmodem_detach: sc = %p\n", sc));
    401       1.1    ichiro 
    402      1.19       mrg 	sc->sc_dying = true;
    403      1.19       mrg 
    404      1.19       mrg 	ubsa_close_pipe(sc);
    405       1.1    ichiro 
    406      1.17       mrg 	for (i = 0; i < sc->sc_numif; i++) {
    407      1.19       mrg 		if (sc->sc_subdevs[i] != NULL) {
    408      1.17       mrg 			rv |= config_detach(sc->sc_subdevs[i], flags);
    409      1.19       mrg 			sc->sc_subdevs[i] = NULL;
    410      1.19       mrg 		}
    411       1.1    ichiro 	}
    412       1.1    ichiro 
    413      1.17       mrg 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    414      1.17       mrg 			   sc->sc_dev);
    415       1.1    ichiro 
    416      1.14     skrll 	return rv;
    417       1.1    ichiro }
    418       1.1    ichiro 
    419      1.17       mrg static int
    420       1.1    ichiro uhmodem_open(void *addr, int portno)
    421       1.1    ichiro {
    422       1.1    ichiro 	struct ubsa_softc *sc = addr;
    423       1.1    ichiro 	usbd_status err;
    424       1.1    ichiro 
    425      1.19       mrg 	DPRINTF(("%s: sc = %p\n", __func__, sc));
    426      1.19       mrg 
    427       1.1    ichiro 	if (sc->sc_dying)
    428      1.19       mrg 		return EIO;
    429       1.1    ichiro 
    430       1.2    ichiro 	err = uhmodem_endpointhalt(sc, 0);
    431      1.19       mrg 	if (err) {
    432       1.7      cube 		aprint_error("%s: endpointhalt fail\n", __func__);
    433      1.19       mrg 		return EIO;
    434      1.19       mrg 	} else
    435       1.1    ichiro 		usbd_delay_ms(sc->sc_udev, 50);
    436       1.2    ichiro 
    437       1.2    ichiro 	if (sc->sc_devflags & A2502) {
    438       1.2    ichiro 		err = a2502_init(sc->sc_udev);
    439      1.19       mrg 		if (err) {
    440       1.7      cube 			aprint_error("%s: a2502init fail\n", __func__);
    441      1.19       mrg 			return EIO;
    442      1.19       mrg 		} else
    443       1.2    ichiro 			usbd_delay_ms(sc->sc_udev, 50);
    444       1.2    ichiro 	}
    445       1.2    ichiro #if 0 /* currently disabled */
    446       1.2    ichiro 	if (sc->sc_devflags & E220) {
    447       1.2    ichiro 		err = e220_init(sc->sc_udev);
    448      1.19       mrg 		if (err) {
    449       1.7      cube 			aprint_error("%s: e220init fail\n", __func__);
    450      1.19       mrg 			return EIO;
    451      1.19       mrg 		} else
    452       1.2    ichiro 			usbd_delay_ms(sc->sc_udev, 50);
    453       1.2    ichiro 	}
    454       1.1    ichiro #endif
    455       1.1    ichiro 	if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
    456      1.14     skrll 		sc->sc_intr_buf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    457       1.1    ichiro 		/* XXX only iface# = 0 has intr line */
    458       1.1    ichiro 		/* XXX E220 specific? need to check */
    459       1.1    ichiro 		err = usbd_open_pipe_intr(sc->sc_iface[0],
    460       1.1    ichiro 		    sc->sc_intr_number,
    461       1.1    ichiro 		    USBD_SHORT_XFER_OK,
    462       1.1    ichiro 		    &sc->sc_intr_pipe,
    463       1.1    ichiro 		    sc,
    464       1.1    ichiro 		    sc->sc_intr_buf,
    465       1.1    ichiro 		    sc->sc_isize,
    466       1.1    ichiro 		    ubsa_intr,
    467       1.1    ichiro 		    UBSA_INTR_INTERVAL);
    468       1.1    ichiro 		if (err) {
    469       1.7      cube 			aprint_error_dev(sc->sc_dev,
    470       1.7      cube 			    "cannot open interrupt pipe (addr %d)\n",
    471       1.1    ichiro 			    sc->sc_intr_number);
    472      1.14     skrll 			return EIO;
    473       1.1    ichiro 		}
    474       1.1    ichiro 	}
    475       1.1    ichiro 
    476      1.14     skrll 	return 0;
    477       1.1    ichiro }
    478       1.1    ichiro 
    479       1.1    ichiro /*
    480       1.1    ichiro  * Hauwei E220 needs special request to enable modem function.
    481       1.1    ichiro  * -- DEVICE_REMOTE_WAKEUP ruquest to endpoint 2.
    482       1.1    ichiro  */
    483      1.17       mrg static  usbd_status
    484      1.14     skrll e220_modechange_request(struct usbd_device *dev)
    485       1.1    ichiro {
    486       1.1    ichiro #define E220_MODE_CHANGE_REQUEST 0x2
    487       1.1    ichiro 	usb_device_request_t req;
    488       1.1    ichiro 	usbd_status err;
    489       1.1    ichiro 
    490       1.1    ichiro 	req.bmRequestType = UT_WRITE_DEVICE;
    491       1.1    ichiro 	req.bRequest = UR_SET_FEATURE;
    492       1.1    ichiro 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
    493       1.1    ichiro 	USETW(req.wIndex, E220_MODE_CHANGE_REQUEST);
    494       1.1    ichiro 	USETW(req.wLength, 0);
    495       1.1    ichiro 
    496       1.1    ichiro 	DPRINTF(("%s: send e220 mode change request\n", __func__));
    497       1.1    ichiro 	err = usbd_do_request(dev, &req, 0);
    498       1.1    ichiro 	if (err) {
    499       1.1    ichiro 		DPRINTF(("%s: E220 mode change fail\n", __func__));
    500      1.14     skrll 		return EIO;
    501       1.1    ichiro 	}
    502       1.1    ichiro 
    503      1.14     skrll 	return 0;
    504       1.1    ichiro #undef E220_MODE_CHANGE_REQUEST
    505       1.1    ichiro }
    506       1.1    ichiro 
    507      1.17       mrg static  usbd_status
    508       1.2    ichiro uhmodem_endpointhalt(struct ubsa_softc *sc, int iface)
    509       1.2    ichiro {
    510       1.2    ichiro 	usb_endpoint_descriptor_t *ed;
    511       1.2    ichiro 	usb_interface_descriptor_t *id;
    512       1.2    ichiro 	usbd_status err;
    513       1.2    ichiro 	int i;
    514       1.2    ichiro 
    515       1.2    ichiro 	/* Find the endpoints */
    516       1.2    ichiro 	id = usbd_get_interface_descriptor(sc->sc_iface[iface]);
    517       1.2    ichiro 
    518       1.2    ichiro 	for (i = 0; i < id->bNumEndpoints; i++) {
    519       1.2    ichiro 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[iface], i);
    520      1.14     skrll 		if (ed == NULL)
    521      1.14     skrll 			return EIO;
    522       1.2    ichiro 
    523       1.2    ichiro 		if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    524      1.21      maxv 			err = usbd_clear_endpoint_feature(sc->sc_udev,
    525      1.21      maxv 			    ed->bEndpointAddress, UF_ENDPOINT_HALT);
    526       1.2    ichiro 			if (err) {
    527      1.14     skrll 				DPRINTF(("%s: ENDPOINT_HALT to EP:%d fail\n",
    528       1.2    ichiro 					__func__, ed->bEndpointAddress));
    529      1.14     skrll 				return EIO;
    530       1.2    ichiro 			}
    531       1.2    ichiro 
    532       1.2    ichiro 		}
    533       1.2    ichiro 	} /* end of Endpoint loop */
    534       1.2    ichiro 
    535      1.14     skrll 	return 0;
    536       1.2    ichiro }
    537       1.2    ichiro 
    538      1.17       mrg static usbd_status
    539      1.14     skrll uhmodem_regwrite(struct usbd_device *dev, uint8_t *data, size_t len)
    540       1.2    ichiro {
    541       1.2    ichiro 	usb_device_request_t req;
    542       1.2    ichiro 	usbd_status err;
    543       1.2    ichiro 
    544       1.2    ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    545       1.2    ichiro 	req.bRequest = UHMODEM_REGWRITE;
    546       1.2    ichiro 	USETW(req.wValue, 0x0000);
    547       1.2    ichiro 	USETW(req.wIndex, 0x0000);
    548       1.2    ichiro 	USETW(req.wLength, len);
    549       1.2    ichiro 	err = usbd_do_request(dev, &req, data);
    550      1.14     skrll 	if (err)
    551       1.2    ichiro 		return err;
    552       1.2    ichiro 
    553       1.2    ichiro 	return 0;
    554       1.2    ichiro }
    555       1.2    ichiro 
    556      1.17       mrg static usbd_status
    557      1.14     skrll uhmodem_regread(struct usbd_device *dev, uint8_t *data, size_t len)
    558       1.2    ichiro {
    559       1.2    ichiro 	usb_device_request_t req;
    560       1.2    ichiro 	usbd_status err;
    561       1.2    ichiro 
    562       1.2    ichiro 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    563       1.2    ichiro 	req.bRequest = UHMODEM_REGREAD;
    564       1.2    ichiro 	USETW(req.wValue, 0x0000);
    565       1.2    ichiro 	USETW(req.wIndex, 0x0000);
    566       1.2    ichiro 	USETW(req.wLength, len);
    567       1.2    ichiro 	err = usbd_do_request(dev, &req, data);
    568       1.2    ichiro 
    569       1.2    ichiro 	if (err)
    570       1.2    ichiro 		return err;
    571       1.2    ichiro 
    572       1.2    ichiro 	return 0;
    573       1.2    ichiro }
    574       1.2    ichiro 
    575       1.2    ichiro #if 0
    576      1.17       mrg static usbd_status
    577      1.14     skrll uhmodem_regsetup(struct usbd_device *dev, uint16_t cmd)
    578       1.1    ichiro {
    579       1.1    ichiro 	usb_device_request_t req;
    580       1.1    ichiro 	usbd_status err;
    581       1.1    ichiro 
    582       1.2    ichiro 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    583       1.2    ichiro 	req.bRequest = UHMODEM_SETUP;
    584       1.2    ichiro 	USETW(req.wValue, cmd);
    585       1.2    ichiro 	USETW(req.wIndex, 0x0000);
    586       1.1    ichiro 	USETW(req.wLength, 0);
    587       1.1    ichiro 	err = usbd_do_request(dev, &req, 0);
    588       1.2    ichiro 
    589       1.2    ichiro 	if (err)
    590       1.2    ichiro 		return err;
    591       1.2    ichiro 
    592       1.2    ichiro 	return 0;
    593       1.2    ichiro }
    594       1.2    ichiro #endif
    595       1.2    ichiro 
    596      1.17       mrg static  usbd_status
    597      1.14     skrll a2502_init(struct usbd_device *dev)
    598       1.2    ichiro {
    599       1.2    ichiro 	uint8_t data[8];
    600       1.2    ichiro 	static uint8_t init_cmd[] = {0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08};
    601       1.2    ichiro #ifdef UHMODEM_DEBUG
    602       1.2    ichiro 	int i;
    603       1.2    ichiro #endif
    604       1.2    ichiro 	if (uhmodem_regread(dev, data, 7)) {
    605       1.2    ichiro 		DPRINTF(("%s: read fail\n", __func__));
    606       1.2    ichiro 		return EIO;
    607       1.1    ichiro 	}
    608       1.2    ichiro #ifdef UHMODEM_DEBUG
    609       1.2    ichiro 	printf("%s: readdata: ", __func__);
    610       1.2    ichiro 	for (i = 0; i < 7; i++)
    611      1.22  christos 		printf("%#x ", data[i]);
    612       1.2    ichiro #endif
    613       1.2    ichiro 	if (uhmodem_regwrite(dev, init_cmd, sizeof(init_cmd)) ) {
    614       1.2    ichiro 		DPRINTF(("%s: write fail\n", __func__));
    615       1.2    ichiro 		return EIO;
    616       1.1    ichiro 	}
    617       1.1    ichiro 
    618      1.14     skrll 	if (uhmodem_regread(dev, data, 7)) {
    619       1.2    ichiro 		DPRINTF(("%s: read fail\n", __func__));
    620       1.2    ichiro 		return EIO;
    621       1.2    ichiro 	}
    622       1.2    ichiro #ifdef UHMODEM_DEBUG
    623       1.2    ichiro 	printf("%s: readdata: ", __func__);
    624       1.2    ichiro 	printf(" => ");
    625       1.2    ichiro 	for (i = 0; i < 7; i++)
    626      1.22  christos 		printf("%#x ", data[i]);
    627       1.2    ichiro 	printf("\n");
    628       1.2    ichiro #endif
    629       1.2    ichiro 	return 0;
    630       1.1    ichiro }
    631       1.1    ichiro 
    632       1.2    ichiro 
    633       1.1    ichiro #if 0
    634      1.14     skrll /*
    635       1.1    ichiro  * Windows device driver send these sequens of USB requests.
    636       1.1    ichiro  * However currently I can't understand what the messege is,
    637       1.1    ichiro  * disable this code when I get more information about it.
    638      1.14     skrll  */
    639      1.17       mrg static  usbd_status
    640      1.14     skrll e220_init(struct usbd_device *dev)
    641       1.1    ichiro {
    642       1.1    ichiro 	uint8_t data[8];
    643       1.1    ichiro 	usb_device_request_t req;
    644       1.1    ichiro 	int i;
    645       1.1    ichiro 
    646       1.2    ichiro 	/* vendor specific unknown request */
    647       1.1    ichiro 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    648       1.1    ichiro 	req.bRequest = 0x02;
    649       1.1    ichiro 	USETW(req.wValue, 0x0001);
    650       1.1    ichiro 	USETW(req.wIndex, 0x0000);
    651       1.1    ichiro 	USETW(req.wLength, 2);
    652       1.1    ichiro 	data[0] = 0x0;
    653       1.1    ichiro 	data[1] = 0x0;
    654       1.2    ichiro 	if (usbd_do_request(dev, &req, data))
    655       1.1    ichiro 		goto error;
    656       1.1    ichiro 
    657       1.1    ichiro 	/* vendor specific unknown sequence */
    658      1.14     skrll 	if(uhmodem_regsetup(dev, 0x1))
    659       1.1    ichiro 		goto error;
    660       1.1    ichiro 
    661       1.2    ichiro 	if (uhmodem_regread(dev, data, 7))
    662       1.1    ichiro 		goto error;
    663       1.1    ichiro 
    664       1.1    ichiro 	data[1] = 0x8;
    665       1.1    ichiro 	data[2] = 0x7;
    666       1.2    ichiro 	if (uhmodem_regwrite(dev, data, sizeof(data)) )
    667       1.1    ichiro 		goto error;
    668       1.1    ichiro 
    669       1.2    ichiro 	if (uhmodem_regread(dev, data, 7))
    670       1.1    ichiro 		goto error;
    671       1.2    ichiro 		/* XXX should verify the read data ? */
    672       1.1    ichiro 
    673       1.2    ichiro 	if (uhmodem_regsetup(dev, 0x3))
    674       1.1    ichiro 		goto error;
    675       1.1    ichiro 
    676      1.14     skrll 	return 0;
    677       1.1    ichiro error:
    678       1.2    ichiro 	DPRINTF(("%s: E220 init request fail\n", __func__));
    679      1.14     skrll 	return EIO;
    680       1.1    ichiro }
    681       1.1    ichiro #endif
    682       1.1    ichiro 
    683