u3g.c revision 1.6.6.5 1 1.6.6.5 yamt /* $NetBSD: u3g.c,v 1.6.6.5 2010/08/11 22:54:15 yamt Exp $ */
2 1.6.6.4 yamt
3 1.6.6.4 yamt /*-
4 1.6.6.4 yamt * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.6.6.4 yamt * All rights reserved.
6 1.6.6.4 yamt *
7 1.6.6.4 yamt * This code is derived from software contributed to The NetBSD Foundation.
8 1.6.6.4 yamt *
9 1.6.6.4 yamt * Redistribution and use in source and binary forms, with or without
10 1.6.6.4 yamt * modification, are permitted provided that the following conditions
11 1.6.6.4 yamt * are met:
12 1.6.6.4 yamt * 1. Redistributions of source code must retain the above copyright
13 1.6.6.4 yamt * notice, this list of conditions and the following disclaimer.
14 1.6.6.4 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.6.6.4 yamt * notice, this list of conditions and the following disclaimer in the
16 1.6.6.4 yamt * documentation and/or other materials provided with the distribution.
17 1.6.6.4 yamt *
18 1.6.6.4 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.6.6.4 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.6.6.4 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.6.6.4 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.6.6.4 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.6.6.4 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.6.6.4 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.6.6.4 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.6.6.4 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.6.6.4 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.6.6.4 yamt * POSSIBILITY OF SUCH DAMAGE.
29 1.6.6.4 yamt */
30 1.6.6.3 yamt
31 1.6.6.2 yamt /*
32 1.6.6.2 yamt * Copyright (c) 2008 AnyWi Technologies
33 1.6.6.2 yamt * Author: Andrea Guzzo <aguzzo (at) anywi.com>
34 1.6.6.2 yamt * * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
35 1.6.6.2 yamt * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
36 1.6.6.2 yamt *
37 1.6.6.2 yamt * Permission to use, copy, modify, and distribute this software for any
38 1.6.6.2 yamt * purpose with or without fee is hereby granted, provided that the above
39 1.6.6.2 yamt * copyright notice and this permission notice appear in all copies.
40 1.6.6.2 yamt *
41 1.6.6.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 1.6.6.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 1.6.6.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 1.6.6.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 1.6.6.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46 1.6.6.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47 1.6.6.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 1.6.6.2 yamt *
49 1.6.6.2 yamt * $FreeBSD$
50 1.6.6.2 yamt */
51 1.6.6.2 yamt
52 1.6.6.3 yamt #include <sys/cdefs.h>
53 1.6.6.5 yamt __KERNEL_RCSID(0, "$NetBSD: u3g.c,v 1.6.6.5 2010/08/11 22:54:15 yamt Exp $");
54 1.6.6.3 yamt
55 1.6.6.2 yamt #include <sys/param.h>
56 1.6.6.2 yamt #include <sys/systm.h>
57 1.6.6.2 yamt #include <sys/kernel.h>
58 1.6.6.2 yamt #include <sys/malloc.h>
59 1.6.6.2 yamt #include <sys/bus.h>
60 1.6.6.2 yamt #include <sys/conf.h>
61 1.6.6.2 yamt #include <sys/tty.h>
62 1.6.6.2 yamt
63 1.6.6.2 yamt #include <dev/usb/usb.h>
64 1.6.6.2 yamt #include <dev/usb/usbdi.h>
65 1.6.6.2 yamt #include <dev/usb/usbdivar.h>
66 1.6.6.2 yamt #include <dev/usb/usbdi_util.h>
67 1.6.6.2 yamt
68 1.6.6.2 yamt #include <dev/usb/ucomvar.h>
69 1.6.6.2 yamt
70 1.6.6.2 yamt #include "usbdevs.h"
71 1.6.6.2 yamt
72 1.6.6.4 yamt /*
73 1.6.6.4 yamt * We read/write data from/to the device in 4KB chunks to maximise
74 1.6.6.4 yamt * performance.
75 1.6.6.4 yamt */
76 1.6.6.4 yamt #define U3G_BUFF_SIZE 4096
77 1.6.6.4 yamt
78 1.6.6.4 yamt /*
79 1.6.6.4 yamt * Some 3G devices (the Huawei E160/E220 springs to mind here) buffer up
80 1.6.6.4 yamt * data internally even when the USB pipes are closed. So on first open,
81 1.6.6.4 yamt * we can receive a large chunk of stale data.
82 1.6.6.4 yamt *
83 1.6.6.4 yamt * This causes a real problem because the default TTYDEF_LFLAG (applied
84 1.6.6.4 yamt * on first open) has the ECHO flag set, resulting in all the stale data
85 1.6.6.4 yamt * being echoed straight back to the device by the tty(4) layer. Some
86 1.6.6.4 yamt * devices (again, the Huawei E160/E220 for example) react to this spew
87 1.6.6.4 yamt * by going catatonic.
88 1.6.6.4 yamt *
89 1.6.6.4 yamt * All this happens before the application gets a chance to disable ECHO.
90 1.6.6.4 yamt *
91 1.6.6.4 yamt * We work around this by ignoring all data received from the device for
92 1.6.6.4 yamt * a period of two seconds, or until the application starts sending data -
93 1.6.6.4 yamt * whichever comes first.
94 1.6.6.4 yamt */
95 1.6.6.4 yamt #define U3G_PURGE_SECS 2
96 1.6.6.4 yamt
97 1.6.6.4 yamt /*
98 1.6.6.4 yamt * Define bits for the virtual modem control pins.
99 1.6.6.4 yamt * The input pin states are reported via the interrupt pipe on some devices.
100 1.6.6.4 yamt */
101 1.6.6.4 yamt #define U3G_OUTPIN_DTR (1u << 0)
102 1.6.6.4 yamt #define U3G_OUTPIN_RTS (1u << 1)
103 1.6.6.4 yamt #define U3G_INPIN_DCD (1u << 0)
104 1.6.6.4 yamt #define U3G_INPIN_DSR (1u << 1)
105 1.6.6.4 yamt #define U3G_INPIN_RI (1u << 3)
106 1.6.6.4 yamt
107 1.6.6.4 yamt /*
108 1.6.6.4 yamt * USB request to set the output pin status
109 1.6.6.4 yamt */
110 1.6.6.4 yamt #define U3G_SET_PIN 0x22
111 1.6.6.2 yamt
112 1.6.6.2 yamt struct u3g_softc {
113 1.6.6.4 yamt device_t sc_dev;
114 1.6.6.4 yamt usbd_device_handle sc_udev;
115 1.6.6.4 yamt bool sc_dying; /* We're going away */
116 1.6.6.4 yamt
117 1.6.6.4 yamt device_t sc_ucom; /* Child ucom(4) handle */
118 1.6.6.4 yamt int sc_ifaceno; /* Device interface number */
119 1.6.6.4 yamt
120 1.6.6.4 yamt bool sc_open; /* Device is in use */
121 1.6.6.4 yamt bool sc_purging; /* Purging stale data */
122 1.6.6.4 yamt struct timeval sc_purge_start; /* Control duration of purge */
123 1.6.6.4 yamt
124 1.6.6.4 yamt u_char sc_msr; /* Emulated 'msr' */
125 1.6.6.4 yamt uint16_t sc_outpins; /* Output pin state */
126 1.6.6.4 yamt
127 1.6.6.4 yamt usbd_pipe_handle sc_intr_pipe; /* Interrupt pipe */
128 1.6.6.4 yamt u_char *sc_intr_buff; /* Interrupt buffer */
129 1.6.6.2 yamt };
130 1.6.6.2 yamt
131 1.6.6.4 yamt /*
132 1.6.6.4 yamt * The device driver has two personalities. The first uses the 'usbdevif'
133 1.6.6.4 yamt * interface attribute so that a match will claim the entire USB device
134 1.6.6.4 yamt * for itself. This is used for when a device needs to be mode-switched
135 1.6.6.4 yamt * and ensures any other interfaces present cannot be claimed by other
136 1.6.6.4 yamt * drivers while the mode-switch is in progress.
137 1.6.6.4 yamt *
138 1.6.6.4 yamt * The second personality uses the 'usbifif' interface attribute so that
139 1.6.6.4 yamt * it can claim the 3G modem interfaces for itself, leaving others (such
140 1.6.6.4 yamt * as the mass storage interfaces on some devices) for other drivers.
141 1.6.6.4 yamt */
142 1.6.6.4 yamt static int u3ginit_match(device_t, cfdata_t, void *);
143 1.6.6.4 yamt static void u3ginit_attach(device_t, device_t, void *);
144 1.6.6.4 yamt static int u3ginit_detach(device_t, int);
145 1.6.6.4 yamt
146 1.6.6.4 yamt CFATTACH_DECL2_NEW(u3ginit, 0, u3ginit_match,
147 1.6.6.4 yamt u3ginit_attach, u3ginit_detach, NULL, NULL, NULL);
148 1.6.6.4 yamt
149 1.6.6.4 yamt
150 1.6.6.4 yamt static int u3g_match(device_t, cfdata_t, void *);
151 1.6.6.4 yamt static void u3g_attach(device_t, device_t, void *);
152 1.6.6.4 yamt static int u3g_detach(device_t, int);
153 1.6.6.4 yamt static int u3g_activate(device_t, enum devact);
154 1.6.6.4 yamt static void u3g_childdet(device_t, device_t);
155 1.6.6.4 yamt
156 1.6.6.4 yamt CFATTACH_DECL2_NEW(u3g, sizeof(struct u3g_softc), u3g_match,
157 1.6.6.4 yamt u3g_attach, u3g_detach, u3g_activate, NULL, u3g_childdet);
158 1.6.6.4 yamt
159 1.6.6.4 yamt
160 1.6.6.4 yamt static void u3g_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
161 1.6.6.4 yamt static void u3g_get_status(void *, int, u_char *, u_char *);
162 1.6.6.4 yamt static void u3g_set(void *, int, int, int);
163 1.6.6.4 yamt static int u3g_open(void *, int);
164 1.6.6.4 yamt static void u3g_close(void *, int);
165 1.6.6.4 yamt static void u3g_read(void *, int, u_char **, uint32_t *);
166 1.6.6.4 yamt static void u3g_write(void *, int, u_char *, u_char *, u_int32_t *);
167 1.6.6.4 yamt
168 1.6.6.2 yamt struct ucom_methods u3g_methods = {
169 1.6.6.4 yamt u3g_get_status,
170 1.6.6.4 yamt u3g_set,
171 1.6.6.2 yamt NULL,
172 1.6.6.2 yamt NULL,
173 1.6.6.4 yamt u3g_open,
174 1.6.6.4 yamt u3g_close,
175 1.6.6.4 yamt u3g_read,
176 1.6.6.4 yamt u3g_write,
177 1.6.6.2 yamt };
178 1.6.6.2 yamt
179 1.6.6.4 yamt /*
180 1.6.6.4 yamt * Allegedly supported devices
181 1.6.6.4 yamt */
182 1.6.6.2 yamt static const struct usb_devno u3g_devs[] = {
183 1.6.6.5 yamt { USB_VENDOR_DELL, USB_PRODUCT_DELL_W5500 },
184 1.6.6.2 yamt /* OEM: Huawei */
185 1.6.6.2 yamt { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220 },
186 1.6.6.4 yamt { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_K3765 },
187 1.6.6.5 yamt { USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE },
188 1.6.6.5 yamt /* OEM: Merlin */
189 1.6.6.5 yamt { USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620 },
190 1.6.6.2 yamt /* OEM: Novatel */
191 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 },
192 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D },
193 1.6.6.5 yamt #if 0
194 1.6.6.5 yamt /* These are matched in u3ginit_match() */
195 1.6.6.5 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MC950D_DRIVER },
196 1.6.6.5 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760_DRIVER },
197 1.6.6.5 yamt #endif
198 1.6.6.5 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINU740 },
199 1.6.6.5 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 },
200 1.6.6.5 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 },
201 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 },
202 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U727 },
203 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U740_2 },
204 1.6.6.5 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U760 },
205 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U870 },
206 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_V740 },
207 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_X950D },
208 1.6.6.2 yamt { USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 },
209 1.6.6.5 yamt /* OEM: Option N.V. */
210 1.6.6.5 yamt { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS },
211 1.6.6.5 yamt { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA },
212 1.6.6.5 yamt { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTMAXHSUPA },
213 1.6.6.5 yamt /* OEM: Qualcomm, Inc. */
214 1.6.6.5 yamt { USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_CDMA_MSM },
215 1.6.6.5 yamt { USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_ZTE_MF626 },
216 1.6.6.2 yamt
217 1.6.6.2 yamt /* OEM: Sierra Wireless: */
218 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U },
219 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E },
220 1.6.6.5 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U },
221 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880 },
222 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E },
223 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U },
224 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881 },
225 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E },
226 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U },
227 1.6.6.5 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 },
228 1.6.6.5 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 },
229 1.6.6.5 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 },
230 1.6.6.5 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597 },
231 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 },
232 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 },
233 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2 },
234 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 },
235 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 },
236 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 },
237 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 },
238 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 },
239 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2 },
240 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780 },
241 1.6.6.2 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781 },
242 1.6.6.5 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725 },
243 1.6.6.5 yamt { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 },
244 1.6.6.2 yamt };
245 1.6.6.2 yamt
246 1.6.6.2 yamt static int
247 1.6.6.4 yamt send_bulkmsg(usbd_device_handle dev, char *cmd, size_t cmdlen)
248 1.6.6.2 yamt {
249 1.6.6.2 yamt usbd_interface_handle iface;
250 1.6.6.2 yamt usb_interface_descriptor_t *id;
251 1.6.6.2 yamt usb_endpoint_descriptor_t *ed;
252 1.6.6.2 yamt usbd_pipe_handle pipe;
253 1.6.6.2 yamt usbd_xfer_handle xfer;
254 1.6.6.2 yamt int err, i;
255 1.6.6.2 yamt
256 1.6.6.2 yamt /* Move the device into the configured state. */
257 1.6.6.4 yamt err = usbd_set_config_index(dev, 0, 0);
258 1.6.6.2 yamt if (err) {
259 1.6.6.2 yamt aprint_error("u3g: failed to set configuration index\n");
260 1.6.6.2 yamt return UMATCH_NONE;
261 1.6.6.2 yamt }
262 1.6.6.2 yamt
263 1.6.6.4 yamt err = usbd_device2interface_handle(dev, 0, &iface);
264 1.6.6.2 yamt if (err != 0) {
265 1.6.6.4 yamt aprint_error("u3ginit: failed to get interface\n");
266 1.6.6.2 yamt return UMATCH_NONE;
267 1.6.6.2 yamt }
268 1.6.6.2 yamt
269 1.6.6.2 yamt id = usbd_get_interface_descriptor(iface);
270 1.6.6.2 yamt ed = NULL;
271 1.6.6.2 yamt for (i = 0 ; i < id->bNumEndpoints ; i++) {
272 1.6.6.2 yamt ed = usbd_interface2endpoint_descriptor(iface, i);
273 1.6.6.2 yamt if (ed == NULL)
274 1.6.6.2 yamt continue;
275 1.6.6.2 yamt if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_OUT)
276 1.6.6.2 yamt continue;
277 1.6.6.2 yamt if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK)
278 1.6.6.2 yamt break;
279 1.6.6.2 yamt }
280 1.6.6.2 yamt
281 1.6.6.2 yamt if (i == id->bNumEndpoints)
282 1.6.6.2 yamt return UMATCH_NONE;
283 1.6.6.2 yamt
284 1.6.6.4 yamt err = usbd_open_pipe(iface, ed->bEndpointAddress,
285 1.6.6.4 yamt USBD_EXCLUSIVE_USE, &pipe);
286 1.6.6.2 yamt if (err != 0) {
287 1.6.6.4 yamt aprint_error("u3ginit: failed to open bulk transfer pipe %d\n",
288 1.6.6.2 yamt ed->bEndpointAddress);
289 1.6.6.2 yamt return UMATCH_NONE;
290 1.6.6.2 yamt }
291 1.6.6.2 yamt
292 1.6.6.4 yamt xfer = usbd_alloc_xfer(dev);
293 1.6.6.2 yamt if (xfer != NULL) {
294 1.6.6.4 yamt usbd_setup_xfer(xfer, pipe, NULL, cmd, cmdlen,
295 1.6.6.2 yamt USBD_SYNCHRONOUS, USBD_DEFAULT_TIMEOUT, NULL);
296 1.6.6.2 yamt
297 1.6.6.2 yamt err = usbd_transfer(xfer);
298 1.6.6.4 yamt
299 1.6.6.4 yamt #if 0 /* XXXpooka: at least my huawei "fails" this always, but still detaches */
300 1.6.6.2 yamt if (err)
301 1.6.6.4 yamt aprint_error("u3ginit: transfer failed\n");
302 1.6.6.4 yamt #else
303 1.6.6.4 yamt err = 0;
304 1.6.6.4 yamt #endif
305 1.6.6.2 yamt usbd_free_xfer(xfer);
306 1.6.6.2 yamt } else {
307 1.6.6.4 yamt aprint_error("u3ginit: failed to allocate xfer\n");
308 1.6.6.2 yamt err = USBD_NOMEM;
309 1.6.6.2 yamt }
310 1.6.6.2 yamt
311 1.6.6.2 yamt usbd_abort_pipe(pipe);
312 1.6.6.2 yamt usbd_close_pipe(pipe);
313 1.6.6.2 yamt
314 1.6.6.2 yamt return (err == USBD_NORMAL_COMPLETION ? UMATCH_HIGHEST : UMATCH_NONE);
315 1.6.6.2 yamt }
316 1.6.6.2 yamt
317 1.6.6.2 yamt static int
318 1.6.6.4 yamt u3g_novatel_reinit(usbd_device_handle dev)
319 1.6.6.4 yamt {
320 1.6.6.4 yamt unsigned char cmd[31];
321 1.6.6.4 yamt
322 1.6.6.4 yamt memset(cmd, 0, sizeof(cmd));
323 1.6.6.4 yamt /* Byte 0..3: Command Block Wrapper (CBW) signature */
324 1.6.6.4 yamt cmd[0] = 0x55;
325 1.6.6.4 yamt cmd[1] = 0x53;
326 1.6.6.4 yamt cmd[2] = 0x42;
327 1.6.6.4 yamt cmd[3] = 0x43;
328 1.6.6.4 yamt /* 4..7: CBW Tag, has to unique, but only a single transfer used. */
329 1.6.6.4 yamt cmd[4] = 0x01;
330 1.6.6.4 yamt /* 8..11: CBW Transfer Length, no data here */
331 1.6.6.4 yamt /* 12: CBW Flag: output, so 0 */
332 1.6.6.4 yamt /* 13: CBW Lun: 0 */
333 1.6.6.4 yamt /* 14: CBW Length */
334 1.6.6.4 yamt cmd[14] = 0x06;
335 1.6.6.4 yamt /* Rest is the SCSI payload */
336 1.6.6.4 yamt /* 0: SCSI START/STOP opcode */
337 1.6.6.4 yamt cmd[15] = 0x1b;
338 1.6.6.4 yamt /* 1..3 unused */
339 1.6.6.4 yamt /* 4 Load/Eject command */
340 1.6.6.4 yamt cmd[19] = 0x02;
341 1.6.6.4 yamt /* 5: unused */
342 1.6.6.4 yamt
343 1.6.6.4 yamt return send_bulkmsg(dev, cmd, sizeof(cmd));
344 1.6.6.4 yamt }
345 1.6.6.4 yamt
346 1.6.6.4 yamt static int
347 1.6.6.2 yamt u3g_huawei_reinit(usbd_device_handle dev)
348 1.6.6.2 yamt {
349 1.6.6.4 yamt /*
350 1.6.6.4 yamt * The Huawei device presents itself as a umass device with Windows
351 1.6.6.2 yamt * drivers on it. After installation of the driver, it reinits into a
352 1.6.6.2 yamt * 3G serial device.
353 1.6.6.2 yamt */
354 1.6.6.2 yamt usb_device_request_t req;
355 1.6.6.2 yamt usb_config_descriptor_t *cdesc;
356 1.6.6.2 yamt
357 1.6.6.2 yamt /* Get the config descriptor */
358 1.6.6.2 yamt cdesc = usbd_get_config_descriptor(dev);
359 1.6.6.4 yamt if (cdesc == NULL) {
360 1.6.6.4 yamt usb_device_descriptor_t dd;
361 1.6.6.4 yamt
362 1.6.6.4 yamt if (usbd_get_device_desc(dev, &dd) != 0)
363 1.6.6.4 yamt return (UMATCH_NONE);
364 1.6.6.4 yamt
365 1.6.6.4 yamt if (dd.bNumConfigurations != 1)
366 1.6.6.4 yamt return (UMATCH_NONE);
367 1.6.6.4 yamt
368 1.6.6.4 yamt if (usbd_set_config_index(dev, 0, 1) != 0)
369 1.6.6.4 yamt return (UMATCH_NONE);
370 1.6.6.4 yamt
371 1.6.6.4 yamt cdesc = usbd_get_config_descriptor(dev);
372 1.6.6.2 yamt
373 1.6.6.4 yamt if (cdesc == NULL)
374 1.6.6.4 yamt return (UMATCH_NONE);
375 1.6.6.4 yamt }
376 1.6.6.4 yamt
377 1.6.6.4 yamt /*
378 1.6.6.4 yamt * One iface means umass mode, more than 1 (4 usually) means 3G mode.
379 1.6.6.4 yamt *
380 1.6.6.4 yamt * XXX: We should check the first interface's device class just to be
381 1.6.6.4 yamt * sure. If it's a mass storage device, then we can be fairly certain
382 1.6.6.4 yamt * it needs a mode-switch.
383 1.6.6.4 yamt */
384 1.6.6.2 yamt if (cdesc->bNumInterface > 1)
385 1.6.6.4 yamt return (UMATCH_NONE);
386 1.6.6.2 yamt
387 1.6.6.2 yamt req.bmRequestType = UT_WRITE_DEVICE;
388 1.6.6.2 yamt req.bRequest = UR_SET_FEATURE;
389 1.6.6.2 yamt USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
390 1.6.6.2 yamt USETW(req.wIndex, UHF_PORT_SUSPEND);
391 1.6.6.2 yamt USETW(req.wLength, 0);
392 1.6.6.2 yamt
393 1.6.6.2 yamt (void) usbd_do_request(dev, &req, 0);
394 1.6.6.2 yamt
395 1.6.6.4 yamt return (UMATCH_HIGHEST); /* Prevent umass from attaching */
396 1.6.6.4 yamt }
397 1.6.6.2 yamt
398 1.6.6.4 yamt static int
399 1.6.6.4 yamt u3g_huawei_k3765_reinit(usbd_device_handle dev)
400 1.6.6.4 yamt {
401 1.6.6.4 yamt unsigned char cmd[31];
402 1.6.6.4 yamt
403 1.6.6.4 yamt /* magic string adapted from some webpage */
404 1.6.6.4 yamt memset(cmd, 0, sizeof(cmd));
405 1.6.6.4 yamt cmd[0] = 0x55;
406 1.6.6.4 yamt cmd[1] = 0x53;
407 1.6.6.4 yamt cmd[2] = 0x42;
408 1.6.6.4 yamt cmd[3] = 0x43;
409 1.6.6.4 yamt cmd[15]= 0x11;
410 1.6.6.4 yamt cmd[16]= 0x06;
411 1.6.6.4 yamt
412 1.6.6.4 yamt return send_bulkmsg(dev, cmd, sizeof(cmd));
413 1.6.6.2 yamt }
414 1.6.6.2 yamt
415 1.6.6.2 yamt static int
416 1.6.6.2 yamt u3g_sierra_reinit(usbd_device_handle dev)
417 1.6.6.2 yamt {
418 1.6.6.2 yamt /* Some Sierra devices presents themselves as a umass device with
419 1.6.6.2 yamt * Windows drivers on it. After installation of the driver, it
420 1.6.6.2 yamt * reinits into a * 3G serial device.
421 1.6.6.2 yamt */
422 1.6.6.2 yamt usb_device_request_t req;
423 1.6.6.2 yamt
424 1.6.6.2 yamt req.bmRequestType = UT_VENDOR;
425 1.6.6.2 yamt req.bRequest = UR_SET_INTERFACE;
426 1.6.6.2 yamt USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
427 1.6.6.2 yamt USETW(req.wIndex, UHF_PORT_CONNECTION);
428 1.6.6.2 yamt USETW(req.wLength, 0);
429 1.6.6.2 yamt
430 1.6.6.2 yamt (void) usbd_do_request(dev, &req, 0);
431 1.6.6.2 yamt
432 1.6.6.2 yamt return (UMATCH_HIGHEST); /* Match to prevent umass from attaching */
433 1.6.6.2 yamt }
434 1.6.6.2 yamt
435 1.6.6.4 yamt /*
436 1.6.6.4 yamt * First personality:
437 1.6.6.4 yamt *
438 1.6.6.4 yamt * Claim the entire device if a mode-switch is required.
439 1.6.6.4 yamt */
440 1.6.6.4 yamt
441 1.6.6.2 yamt static int
442 1.6.6.4 yamt u3ginit_match(device_t parent, cfdata_t match, void *aux)
443 1.6.6.2 yamt {
444 1.6.6.2 yamt struct usb_attach_arg *uaa = aux;
445 1.6.6.2 yamt
446 1.6.6.4 yamt /*
447 1.6.6.4 yamt * Huawei changes product when it is configured as a modem.
448 1.6.6.4 yamt */
449 1.6.6.5 yamt switch (uaa->vendor) {
450 1.6.6.5 yamt case USB_VENDOR_HUAWEI:
451 1.6.6.4 yamt if (uaa->product == USB_PRODUCT_HUAWEI_K3765)
452 1.6.6.4 yamt return UMATCH_NONE;
453 1.6.6.4 yamt
454 1.6.6.5 yamt switch (uaa->product) {
455 1.6.6.5 yamt case USB_PRODUCT_HUAWEI_E1750INIT:
456 1.6.6.5 yamt case USB_PRODUCT_HUAWEI_K3765INIT:
457 1.6.6.4 yamt return u3g_huawei_k3765_reinit(uaa->device);
458 1.6.6.5 yamt break;
459 1.6.6.5 yamt default:
460 1.6.6.4 yamt return u3g_huawei_reinit(uaa->device);
461 1.6.6.5 yamt break;
462 1.6.6.5 yamt }
463 1.6.6.5 yamt break;
464 1.6.6.2 yamt
465 1.6.6.5 yamt case USB_VENDOR_NOVATEL2:
466 1.6.6.5 yamt switch (uaa->product){
467 1.6.6.5 yamt case USB_PRODUCT_NOVATEL2_MC950D_DRIVER:
468 1.6.6.5 yamt case USB_PRODUCT_NOVATEL2_U760_DRIVER:
469 1.6.6.5 yamt return u3g_novatel_reinit(uaa->device);
470 1.6.6.5 yamt break;
471 1.6.6.5 yamt default:
472 1.6.6.5 yamt break;
473 1.6.6.5 yamt }
474 1.6.6.5 yamt break;
475 1.6.6.5 yamt
476 1.6.6.5 yamt case USB_VENDOR_SIERRA:
477 1.6.6.5 yamt if (uaa->product == USB_PRODUCT_SIERRA_INSTALLER)
478 1.6.6.5 yamt return u3g_sierra_reinit(uaa->device);
479 1.6.6.5 yamt break;
480 1.6.6.5 yamt
481 1.6.6.5 yamt case USB_VENDOR_QUALCOMMINC:
482 1.6.6.5 yamt if (uaa->product == USB_PRODUCT_QUALCOMMINC_ZTE_STOR)
483 1.6.6.5 yamt return u3g_novatel_reinit(uaa->device);
484 1.6.6.5 yamt break;
485 1.6.6.5 yamt
486 1.6.6.5 yamt default:
487 1.6.6.5 yamt break;
488 1.6.6.5 yamt }
489 1.6.6.2 yamt
490 1.6.6.2 yamt return UMATCH_NONE;
491 1.6.6.2 yamt }
492 1.6.6.2 yamt
493 1.6.6.2 yamt static void
494 1.6.6.4 yamt u3ginit_attach(device_t parent, device_t self, void *aux)
495 1.6.6.2 yamt {
496 1.6.6.2 yamt struct usb_attach_arg *uaa = aux;
497 1.6.6.2 yamt
498 1.6.6.2 yamt aprint_naive("\n");
499 1.6.6.4 yamt aprint_normal(": Switching to 3G mode\n");
500 1.6.6.2 yamt
501 1.6.6.5 yamt if (uaa->vendor == USB_VENDOR_NOVATEL2) {
502 1.6.6.5 yamt switch (uaa->product) {
503 1.6.6.5 yamt case USB_PRODUCT_NOVATEL2_MC950D_DRIVER:
504 1.6.6.5 yamt case USB_PRODUCT_NOVATEL2_U760_DRIVER:
505 1.6.6.5 yamt /* About to disappear... */
506 1.6.6.5 yamt return;
507 1.6.6.5 yamt break;
508 1.6.6.5 yamt default:
509 1.6.6.5 yamt break;
510 1.6.6.5 yamt }
511 1.6.6.2 yamt }
512 1.6.6.2 yamt
513 1.6.6.2 yamt /* Move the device into the configured state. */
514 1.6.6.4 yamt (void) usbd_set_config_index(uaa->device, 0, 1);
515 1.6.6.4 yamt }
516 1.6.6.2 yamt
517 1.6.6.4 yamt static int
518 1.6.6.4 yamt u3ginit_detach(device_t self, int flags)
519 1.6.6.4 yamt {
520 1.6.6.2 yamt
521 1.6.6.4 yamt return (0);
522 1.6.6.4 yamt }
523 1.6.6.4 yamt
524 1.6.6.4 yamt
525 1.6.6.4 yamt /*
526 1.6.6.4 yamt * Second personality:
527 1.6.6.4 yamt *
528 1.6.6.4 yamt * Claim only those interfaces required for 3G modem operation.
529 1.6.6.4 yamt */
530 1.6.6.4 yamt
531 1.6.6.4 yamt static int
532 1.6.6.4 yamt u3g_match(device_t parent, cfdata_t match, void *aux)
533 1.6.6.4 yamt {
534 1.6.6.4 yamt struct usbif_attach_arg *uaa = aux;
535 1.6.6.4 yamt usbd_interface_handle iface;
536 1.6.6.4 yamt usb_interface_descriptor_t *id;
537 1.6.6.4 yamt usbd_status error;
538 1.6.6.4 yamt
539 1.6.6.4 yamt if (!usb_lookup(u3g_devs, uaa->vendor, uaa->product))
540 1.6.6.4 yamt return (UMATCH_NONE);
541 1.6.6.4 yamt
542 1.6.6.4 yamt error = usbd_device2interface_handle(uaa->device, uaa->ifaceno, &iface);
543 1.6.6.4 yamt if (error) {
544 1.6.6.4 yamt printf("u3g_match: failed to get interface, err=%s\n",
545 1.6.6.4 yamt usbd_errstr(error));
546 1.6.6.4 yamt return (UMATCH_NONE);
547 1.6.6.2 yamt }
548 1.6.6.2 yamt
549 1.6.6.4 yamt id = usbd_get_interface_descriptor(iface);
550 1.6.6.4 yamt if (id == NULL) {
551 1.6.6.4 yamt printf("u3g_match: failed to get interface descriptor\n");
552 1.6.6.4 yamt return (UMATCH_NONE);
553 1.6.6.2 yamt }
554 1.6.6.2 yamt
555 1.6.6.4 yamt /*
556 1.6.6.4 yamt * 3G modems generally report vendor-specific class
557 1.6.6.4 yamt *
558 1.6.6.4 yamt * XXX: this may be too generalised.
559 1.6.6.4 yamt */
560 1.6.6.4 yamt return ((id->bInterfaceClass == UICLASS_VENDOR) ?
561 1.6.6.4 yamt UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
562 1.6.6.4 yamt }
563 1.6.6.4 yamt
564 1.6.6.4 yamt static void
565 1.6.6.4 yamt u3g_attach(device_t parent, device_t self, void *aux)
566 1.6.6.4 yamt {
567 1.6.6.4 yamt struct u3g_softc *sc = device_private(self);
568 1.6.6.4 yamt struct usbif_attach_arg *uaa = aux;
569 1.6.6.4 yamt usbd_device_handle dev = uaa->device;
570 1.6.6.4 yamt usbd_interface_handle iface;
571 1.6.6.4 yamt usb_interface_descriptor_t *id;
572 1.6.6.4 yamt usb_endpoint_descriptor_t *ed;
573 1.6.6.4 yamt struct ucom_attach_args uca;
574 1.6.6.4 yamt usbd_status error;
575 1.6.6.4 yamt int n, intr_address, intr_size;
576 1.6.6.4 yamt
577 1.6.6.4 yamt aprint_naive("\n");
578 1.6.6.4 yamt aprint_normal("\n");
579 1.6.6.4 yamt
580 1.6.6.4 yamt sc->sc_dev = self;
581 1.6.6.4 yamt sc->sc_dying = false;
582 1.6.6.4 yamt sc->sc_udev = dev;
583 1.6.6.4 yamt
584 1.6.6.4 yamt error = usbd_device2interface_handle(dev, uaa->ifaceno, &iface);
585 1.6.6.4 yamt if (error) {
586 1.6.6.4 yamt aprint_error_dev(self, "failed to get interface, err=%s\n",
587 1.6.6.4 yamt usbd_errstr(error));
588 1.6.6.2 yamt return;
589 1.6.6.2 yamt }
590 1.6.6.2 yamt
591 1.6.6.4 yamt id = usbd_get_interface_descriptor(iface);
592 1.6.6.2 yamt
593 1.6.6.4 yamt uca.info = "3G Modem";
594 1.6.6.4 yamt uca.ibufsize = U3G_BUFF_SIZE;
595 1.6.6.4 yamt uca.obufsize = U3G_BUFF_SIZE;
596 1.6.6.4 yamt uca.ibufsizepad = U3G_BUFF_SIZE;
597 1.6.6.4 yamt uca.portno = uaa->ifaceno;
598 1.6.6.4 yamt uca.opkthdrlen = 0;
599 1.6.6.4 yamt uca.device = dev;
600 1.6.6.4 yamt uca.iface = iface;
601 1.6.6.4 yamt uca.methods = &u3g_methods;
602 1.6.6.4 yamt uca.arg = sc;
603 1.6.6.4 yamt uca.bulkin = uca.bulkout = -1;
604 1.6.6.4 yamt
605 1.6.6.4 yamt sc->sc_outpins = 0;
606 1.6.6.4 yamt sc->sc_msr = UMSR_DSR | UMSR_CTS | UMSR_DCD;
607 1.6.6.4 yamt sc->sc_ifaceno = uaa->ifaceno;
608 1.6.6.4 yamt sc->sc_open = false;
609 1.6.6.4 yamt sc->sc_purging = false;
610 1.6.6.4 yamt
611 1.6.6.4 yamt intr_address = -1;
612 1.6.6.4 yamt intr_size = 0;
613 1.6.6.4 yamt
614 1.6.6.4 yamt for (n = 0; n < id->bNumEndpoints; n++) {
615 1.6.6.4 yamt ed = usbd_interface2endpoint_descriptor(iface, n);
616 1.6.6.4 yamt if (ed == NULL) {
617 1.6.6.4 yamt aprint_error_dev(self, "no endpoint descriptor "
618 1.6.6.4 yamt "for %d (interface: %d)\n", n, sc->sc_ifaceno);
619 1.6.6.4 yamt sc->sc_dying = true;
620 1.6.6.2 yamt return;
621 1.6.6.2 yamt }
622 1.6.6.2 yamt
623 1.6.6.4 yamt if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
624 1.6.6.4 yamt UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
625 1.6.6.4 yamt intr_address = ed->bEndpointAddress;
626 1.6.6.4 yamt intr_size = UGETW(ed->wMaxPacketSize);
627 1.6.6.4 yamt } else
628 1.6.6.4 yamt if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
629 1.6.6.4 yamt UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
630 1.6.6.4 yamt uca.bulkin = ed->bEndpointAddress;
631 1.6.6.4 yamt } else
632 1.6.6.4 yamt if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
633 1.6.6.4 yamt UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
634 1.6.6.4 yamt uca.bulkout = ed->bEndpointAddress;
635 1.6.6.2 yamt }
636 1.6.6.4 yamt }
637 1.6.6.2 yamt
638 1.6.6.4 yamt if (uca.bulkin == -1) {
639 1.6.6.4 yamt aprint_error_dev(self, "Missing bulk in for interface %d\n",
640 1.6.6.4 yamt sc->sc_ifaceno);
641 1.6.6.4 yamt sc->sc_dying = true;
642 1.6.6.4 yamt return;
643 1.6.6.2 yamt }
644 1.6.6.2 yamt
645 1.6.6.4 yamt if (uca.bulkout == -1) {
646 1.6.6.4 yamt aprint_error_dev(self, "Missing bulk out for interface %d\n",
647 1.6.6.4 yamt sc->sc_ifaceno);
648 1.6.6.4 yamt sc->sc_dying = true;
649 1.6.6.4 yamt return;
650 1.6.6.4 yamt }
651 1.6.6.4 yamt
652 1.6.6.4 yamt sc->sc_ucom = config_found_sm_loc(self, "ucombus",
653 1.6.6.4 yamt NULL, &uca, ucomprint, ucomsubmatch);
654 1.6.6.4 yamt
655 1.6.6.4 yamt /*
656 1.6.6.4 yamt * If the interface has an interrupt pipe, open it immediately so
657 1.6.6.4 yamt * that we can track input pin state changes regardless of whether
658 1.6.6.4 yamt * the tty(4) device is open or not.
659 1.6.6.4 yamt */
660 1.6.6.4 yamt if (intr_address != -1) {
661 1.6.6.4 yamt sc->sc_intr_buff = malloc(intr_size, M_USBDEV, M_WAITOK);
662 1.6.6.4 yamt error = usbd_open_pipe_intr(iface, intr_address,
663 1.6.6.4 yamt USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buff,
664 1.6.6.4 yamt intr_size, u3g_intr, 100);
665 1.6.6.2 yamt if (error) {
666 1.6.6.4 yamt aprint_error_dev(self, "cannot open interrupt pipe "
667 1.6.6.4 yamt "(addr %d)\n", intr_address);
668 1.6.6.2 yamt return;
669 1.6.6.2 yamt }
670 1.6.6.4 yamt } else {
671 1.6.6.4 yamt sc->sc_intr_pipe = NULL;
672 1.6.6.4 yamt sc->sc_intr_buff = NULL;
673 1.6.6.2 yamt }
674 1.6.6.2 yamt
675 1.6.6.2 yamt if (!pmf_device_register(self, NULL, NULL))
676 1.6.6.2 yamt aprint_error_dev(self, "couldn't establish power handler\n");
677 1.6.6.2 yamt }
678 1.6.6.2 yamt
679 1.6.6.2 yamt static int
680 1.6.6.2 yamt u3g_detach(device_t self, int flags)
681 1.6.6.2 yamt {
682 1.6.6.2 yamt struct u3g_softc *sc = device_private(self);
683 1.6.6.4 yamt int rv;
684 1.6.6.2 yamt
685 1.6.6.4 yamt if (sc->sc_dying)
686 1.6.6.2 yamt return 0;
687 1.6.6.2 yamt
688 1.6.6.2 yamt pmf_device_deregister(self);
689 1.6.6.2 yamt
690 1.6.6.4 yamt if (sc->sc_ucom != NULL) {
691 1.6.6.4 yamt rv = config_detach(sc->sc_ucom, flags);
692 1.6.6.4 yamt if (rv != 0) {
693 1.6.6.4 yamt aprint_verbose_dev(self, "Can't deallocate "
694 1.6.6.4 yamt "port (%d)", rv);
695 1.6.6.2 yamt }
696 1.6.6.2 yamt }
697 1.6.6.2 yamt
698 1.6.6.2 yamt if (sc->sc_intr_pipe != NULL) {
699 1.6.6.4 yamt (void) usbd_abort_pipe(sc->sc_intr_pipe);
700 1.6.6.4 yamt (void) usbd_close_pipe(sc->sc_intr_pipe);
701 1.6.6.2 yamt sc->sc_intr_pipe = NULL;
702 1.6.6.2 yamt }
703 1.6.6.4 yamt if (sc->sc_intr_buff != NULL) {
704 1.6.6.4 yamt free(sc->sc_intr_buff, M_USBDEV);
705 1.6.6.4 yamt sc->sc_intr_buff = NULL;
706 1.6.6.4 yamt }
707 1.6.6.2 yamt
708 1.6.6.4 yamt return (0);
709 1.6.6.2 yamt }
710 1.6.6.2 yamt
711 1.6.6.2 yamt static void
712 1.6.6.2 yamt u3g_childdet(device_t self, device_t child)
713 1.6.6.2 yamt {
714 1.6.6.2 yamt struct u3g_softc *sc = device_private(self);
715 1.6.6.2 yamt
716 1.6.6.4 yamt if (sc->sc_ucom == child)
717 1.6.6.4 yamt sc->sc_ucom = NULL;
718 1.6.6.2 yamt }
719 1.6.6.2 yamt
720 1.6.6.2 yamt static int
721 1.6.6.2 yamt u3g_activate(device_t self, enum devact act)
722 1.6.6.2 yamt {
723 1.6.6.2 yamt struct u3g_softc *sc = device_private(self);
724 1.6.6.4 yamt int rv;
725 1.6.6.2 yamt
726 1.6.6.2 yamt switch (act) {
727 1.6.6.4 yamt case DVACT_DEACTIVATE:
728 1.6.6.4 yamt if (sc->sc_ucom != NULL && config_deactivate(sc->sc_ucom))
729 1.6.6.4 yamt rv = -1;
730 1.6.6.4 yamt else
731 1.6.6.4 yamt rv = 0;
732 1.6.6.2 yamt break;
733 1.6.6.2 yamt
734 1.6.6.4 yamt default:
735 1.6.6.4 yamt rv = 0;
736 1.6.6.2 yamt break;
737 1.6.6.2 yamt }
738 1.6.6.4 yamt
739 1.6.6.2 yamt return (rv);
740 1.6.6.2 yamt }
741 1.6.6.2 yamt
742 1.6.6.4 yamt static void
743 1.6.6.4 yamt u3g_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
744 1.6.6.4 yamt {
745 1.6.6.4 yamt struct u3g_softc *sc = (struct u3g_softc *)priv;
746 1.6.6.4 yamt u_char *buf;
747 1.6.6.4 yamt
748 1.6.6.4 yamt if (sc->sc_dying)
749 1.6.6.4 yamt return;
750 1.6.6.4 yamt
751 1.6.6.4 yamt if (status != USBD_NORMAL_COMPLETION) {
752 1.6.6.4 yamt if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
753 1.6.6.4 yamt return;
754 1.6.6.4 yamt usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
755 1.6.6.4 yamt return;
756 1.6.6.4 yamt }
757 1.6.6.4 yamt
758 1.6.6.4 yamt buf = sc->sc_intr_buff;
759 1.6.6.4 yamt if (buf[0] == 0xa1 && buf[1] == 0x20) {
760 1.6.6.4 yamt u_char msr;
761 1.6.6.4 yamt
762 1.6.6.4 yamt msr = sc->sc_msr & ~(UMSR_DCD | UMSR_DSR | UMSR_RI);
763 1.6.6.4 yamt
764 1.6.6.4 yamt if (buf[8] & U3G_INPIN_DCD)
765 1.6.6.4 yamt msr |= UMSR_DCD;
766 1.6.6.4 yamt
767 1.6.6.4 yamt if (buf[8] & U3G_INPIN_DSR)
768 1.6.6.4 yamt msr |= UMSR_DSR;
769 1.6.6.4 yamt
770 1.6.6.4 yamt if (buf[8] & U3G_INPIN_RI)
771 1.6.6.4 yamt msr |= UMSR_RI;
772 1.6.6.4 yamt
773 1.6.6.4 yamt if (msr != sc->sc_msr) {
774 1.6.6.4 yamt sc->sc_msr = msr;
775 1.6.6.4 yamt if (sc->sc_open)
776 1.6.6.4 yamt ucom_status_change(device_private(sc->sc_ucom));
777 1.6.6.4 yamt }
778 1.6.6.4 yamt }
779 1.6.6.4 yamt }
780 1.6.6.4 yamt
781 1.6.6.4 yamt /*ARGSUSED*/
782 1.6.6.4 yamt static void
783 1.6.6.4 yamt u3g_get_status(void *arg, int portno, u_char *lsr, u_char *msr)
784 1.6.6.4 yamt {
785 1.6.6.4 yamt struct u3g_softc *sc = arg;
786 1.6.6.4 yamt
787 1.6.6.4 yamt if (lsr != NULL)
788 1.6.6.4 yamt *lsr = 0; /* LSR isn't supported */
789 1.6.6.4 yamt if (msr != NULL)
790 1.6.6.4 yamt *msr = sc->sc_msr;
791 1.6.6.4 yamt }
792 1.6.6.4 yamt
793 1.6.6.4 yamt /*ARGSUSED*/
794 1.6.6.4 yamt static void
795 1.6.6.4 yamt u3g_set(void *arg, int portno, int reg, int onoff)
796 1.6.6.4 yamt {
797 1.6.6.4 yamt struct u3g_softc *sc = arg;
798 1.6.6.4 yamt usb_device_request_t req;
799 1.6.6.4 yamt uint16_t mask, new_state;
800 1.6.6.4 yamt usbd_status err;
801 1.6.6.4 yamt
802 1.6.6.4 yamt if (sc->sc_dying)
803 1.6.6.4 yamt return;
804 1.6.6.4 yamt
805 1.6.6.4 yamt switch (reg) {
806 1.6.6.4 yamt case UCOM_SET_DTR:
807 1.6.6.4 yamt mask = U3G_OUTPIN_DTR;
808 1.6.6.4 yamt break;
809 1.6.6.4 yamt case UCOM_SET_RTS:
810 1.6.6.4 yamt mask = U3G_OUTPIN_RTS;
811 1.6.6.4 yamt break;
812 1.6.6.4 yamt default:
813 1.6.6.4 yamt return;
814 1.6.6.4 yamt }
815 1.6.6.4 yamt
816 1.6.6.4 yamt new_state = sc->sc_outpins & ~mask;
817 1.6.6.4 yamt if (onoff)
818 1.6.6.4 yamt new_state |= mask;
819 1.6.6.4 yamt
820 1.6.6.4 yamt if (new_state == sc->sc_outpins)
821 1.6.6.4 yamt return;
822 1.6.6.4 yamt
823 1.6.6.4 yamt sc->sc_outpins = new_state;
824 1.6.6.4 yamt
825 1.6.6.4 yamt req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
826 1.6.6.4 yamt req.bRequest = U3G_SET_PIN;
827 1.6.6.4 yamt USETW(req.wValue, new_state);
828 1.6.6.4 yamt USETW(req.wIndex, sc->sc_ifaceno);
829 1.6.6.4 yamt USETW(req.wLength, 0);
830 1.6.6.4 yamt
831 1.6.6.4 yamt err = usbd_do_request(sc->sc_udev, &req, 0);
832 1.6.6.4 yamt if (err == USBD_STALLED)
833 1.6.6.4 yamt usbd_clear_endpoint_stall(sc->sc_udev->default_pipe);
834 1.6.6.4 yamt }
835 1.6.6.4 yamt
836 1.6.6.4 yamt /*ARGSUSED*/
837 1.6.6.4 yamt static int
838 1.6.6.4 yamt u3g_open(void *arg, int portno)
839 1.6.6.4 yamt {
840 1.6.6.4 yamt struct u3g_softc *sc = arg;
841 1.6.6.4 yamt usb_device_request_t req;
842 1.6.6.4 yamt usb_endpoint_descriptor_t *ed;
843 1.6.6.4 yamt usb_interface_descriptor_t *id;
844 1.6.6.4 yamt usbd_interface_handle ih;
845 1.6.6.4 yamt usbd_status err;
846 1.6.6.4 yamt int i;
847 1.6.6.4 yamt
848 1.6.6.4 yamt if (sc->sc_dying)
849 1.6.6.4 yamt return (0);
850 1.6.6.4 yamt
851 1.6.6.4 yamt err = usbd_device2interface_handle(sc->sc_udev, portno, &ih);
852 1.6.6.4 yamt if (err)
853 1.6.6.4 yamt return (EIO);
854 1.6.6.4 yamt
855 1.6.6.4 yamt id = usbd_get_interface_descriptor(ih);
856 1.6.6.4 yamt
857 1.6.6.4 yamt for (i = 0; i < id->bNumEndpoints; i++) {
858 1.6.6.4 yamt ed = usbd_interface2endpoint_descriptor(ih, i);
859 1.6.6.4 yamt if (ed == NULL)
860 1.6.6.4 yamt return (EIO);
861 1.6.6.4 yamt
862 1.6.6.4 yamt if (UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
863 1.6.6.4 yamt /* Issue ENDPOINT_HALT request */
864 1.6.6.4 yamt req.bmRequestType = UT_WRITE_ENDPOINT;
865 1.6.6.4 yamt req.bRequest = UR_CLEAR_FEATURE;
866 1.6.6.4 yamt USETW(req.wValue, UF_ENDPOINT_HALT);
867 1.6.6.4 yamt USETW(req.wIndex, ed->bEndpointAddress);
868 1.6.6.4 yamt USETW(req.wLength, 0);
869 1.6.6.4 yamt err = usbd_do_request(sc->sc_udev, &req, 0);
870 1.6.6.4 yamt if (err)
871 1.6.6.4 yamt return (EIO);
872 1.6.6.4 yamt }
873 1.6.6.4 yamt }
874 1.6.6.4 yamt
875 1.6.6.4 yamt sc->sc_open = true;
876 1.6.6.4 yamt sc->sc_purging = true;
877 1.6.6.4 yamt getmicrotime(&sc->sc_purge_start);
878 1.6.6.4 yamt
879 1.6.6.4 yamt return (0);
880 1.6.6.4 yamt }
881 1.6.6.4 yamt
882 1.6.6.4 yamt /*ARGSUSED*/
883 1.6.6.4 yamt static void
884 1.6.6.4 yamt u3g_close(void *arg, int portno)
885 1.6.6.4 yamt {
886 1.6.6.4 yamt struct u3g_softc *sc = arg;
887 1.6.6.4 yamt
888 1.6.6.4 yamt sc->sc_open = false;
889 1.6.6.4 yamt }
890 1.6.6.4 yamt
891 1.6.6.4 yamt /*ARGSUSED*/
892 1.6.6.4 yamt static void
893 1.6.6.4 yamt u3g_read(void *arg, int portno, u_char **cpp, uint32_t *ccp)
894 1.6.6.4 yamt {
895 1.6.6.4 yamt struct u3g_softc *sc = arg;
896 1.6.6.4 yamt struct timeval curr_tv, diff_tv;
897 1.6.6.4 yamt
898 1.6.6.4 yamt /*
899 1.6.6.4 yamt * If we're not purging input data following first open, do nothing.
900 1.6.6.4 yamt */
901 1.6.6.4 yamt if (sc->sc_purging == false)
902 1.6.6.4 yamt return;
903 1.6.6.4 yamt
904 1.6.6.4 yamt /*
905 1.6.6.4 yamt * Otherwise check if the purge timeout has expired
906 1.6.6.4 yamt */
907 1.6.6.4 yamt getmicrotime(&curr_tv);
908 1.6.6.4 yamt timersub(&curr_tv, &sc->sc_purge_start, &diff_tv);
909 1.6.6.4 yamt
910 1.6.6.4 yamt if (diff_tv.tv_sec >= U3G_PURGE_SECS) {
911 1.6.6.4 yamt /* Timeout expired. */
912 1.6.6.4 yamt sc->sc_purging = false;
913 1.6.6.4 yamt } else {
914 1.6.6.4 yamt /* Still purging. Adjust the caller's byte count. */
915 1.6.6.4 yamt *ccp = 0;
916 1.6.6.4 yamt }
917 1.6.6.4 yamt }
918 1.6.6.4 yamt
919 1.6.6.4 yamt /*ARGSUSED*/
920 1.6.6.4 yamt static void
921 1.6.6.4 yamt u3g_write(void *arg, int portno, u_char *to, u_char *from, u_int32_t *count)
922 1.6.6.4 yamt {
923 1.6.6.4 yamt struct u3g_softc *sc = arg;
924 1.6.6.4 yamt
925 1.6.6.4 yamt /*
926 1.6.6.4 yamt * Stop purging as soon as the first data is written to the device.
927 1.6.6.4 yamt */
928 1.6.6.4 yamt sc->sc_purging = false;
929 1.6.6.4 yamt memcpy(to, from, *count);
930 1.6.6.4 yamt }
931