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