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