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