]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/usb/serial/u3g.c
MFC r271874:
[FreeBSD/stable/10.git] / sys / dev / usb / serial / u3g.c
1 /*
2  * Copyright (c) 2008 AnyWi Technologies
3  * Author: Andrea Guzzo <aguzzo@anywi.com>
4  * * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
5  * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * $FreeBSD$
20  */
21
22 /*
23  * NOTE:
24  *
25  * - The detour through the tty layer is ridiculously expensive wrt
26  *   buffering due to the high speeds.
27  *
28  *   We should consider adding a simple r/w device which allows
29  *   attaching of PPP in a more efficient way.
30  *
31  */
32
33
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/sx.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
51 #include <sys/priv.h>
52
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usb_cdc.h>
57 #include "usbdevs.h"
58
59 #define USB_DEBUG_VAR u3g_debug
60 #include <dev/usb/usb_debug.h>
61 #include <dev/usb/usb_process.h>
62 #include <dev/usb/usb_msctest.h>
63
64 #include <dev/usb/serial/usb_serial.h>
65 #include <dev/usb/quirk/usb_quirk.h>
66
67 #ifdef USB_DEBUG
68 static int u3g_debug = 0;
69
70 static SYSCTL_NODE(_hw_usb, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g");
71 SYSCTL_INT(_hw_usb_u3g, OID_AUTO, debug, CTLFLAG_RW,
72     &u3g_debug, 0, "Debug level");
73 #endif
74
75 #define U3G_MAXPORTS            12
76 #define U3G_CONFIG_INDEX        0
77 #define U3G_BSIZE               2048
78 #define U3G_TXSIZE              (U3G_BSIZE / U3G_TXFRAMES)
79 #define U3G_TXFRAMES            4
80
81 #define U3GSP_GPRS              0
82 #define U3GSP_EDGE              1
83 #define U3GSP_CDMA              2
84 #define U3GSP_UMTS              3
85 #define U3GSP_HSDPA             4
86 #define U3GSP_HSUPA             5
87 #define U3GSP_HSPA              6
88 #define U3GSP_MAX               7
89
90 /* Eject methods; See also usb_quirks.h:UQ_MSC_EJECT_* */
91 #define U3GINIT_HUAWEI          1       /* Requires Huawei init command */
92 #define U3GINIT_SIERRA          2       /* Requires Sierra init command */
93 #define U3GINIT_SCSIEJECT       3       /* Requires SCSI eject command */
94 #define U3GINIT_REZERO          4       /* Requires SCSI rezero command */
95 #define U3GINIT_ZTESTOR         5       /* Requires ZTE SCSI command */
96 #define U3GINIT_CMOTECH         6       /* Requires CMOTECH SCSI command */
97 #define U3GINIT_WAIT            7       /* Device reappears after a delay */
98 #define U3GINIT_SAEL_M460       8       /* Requires vendor init */
99 #define U3GINIT_HUAWEISCSI      9       /* Requires Huawei SCSI init command */
100 #define U3GINIT_TCT             10      /* Requires TCT Mobile init command */
101
102 enum {
103         U3G_BULK_WR,
104         U3G_BULK_RD,
105         U3G_INTR,
106         U3G_N_TRANSFER,
107 };
108
109 struct u3g_softc {
110         struct ucom_super_softc sc_super_ucom;
111         struct ucom_softc sc_ucom[U3G_MAXPORTS];
112
113         struct usb_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER];
114         uint8_t sc_iface[U3G_MAXPORTS];                 /* local status register */
115         uint8_t sc_lsr[U3G_MAXPORTS];                   /* local status register */
116         uint8_t sc_msr[U3G_MAXPORTS];                   /* u3g status register */
117         uint16_t sc_line[U3G_MAXPORTS];                 /* line status */
118
119         struct usb_device *sc_udev;
120         struct mtx sc_mtx;
121
122         uint8_t sc_numports;
123 };
124
125 static device_probe_t u3g_probe;
126 static device_attach_t u3g_attach;
127 static device_detach_t u3g_detach;
128 static void u3g_free_softc(struct u3g_softc *);
129
130 static usb_callback_t u3g_write_callback;
131 static usb_callback_t u3g_read_callback;
132 static usb_callback_t u3g_intr_callback;
133
134 static void u3g_cfg_get_status(struct ucom_softc *, uint8_t *, uint8_t *);
135 static void u3g_cfg_set_dtr(struct ucom_softc *, uint8_t);
136 static void u3g_cfg_set_rts(struct ucom_softc *, uint8_t);
137 static void u3g_start_read(struct ucom_softc *ucom);
138 static void u3g_stop_read(struct ucom_softc *ucom);
139 static void u3g_start_write(struct ucom_softc *ucom);
140 static void u3g_stop_write(struct ucom_softc *ucom);
141 static void u3g_poll(struct ucom_softc *ucom);
142 static void u3g_free(struct ucom_softc *ucom);
143
144
145 static void u3g_test_autoinst(void *, struct usb_device *,
146                 struct usb_attach_arg *);
147 static int u3g_driver_loaded(struct module *mod, int what, void *arg);
148
149 static eventhandler_tag u3g_etag;
150
151 static const struct usb_config u3g_config[U3G_N_TRANSFER] = {
152
153         [U3G_BULK_WR] = {
154                 .type = UE_BULK,
155                 .endpoint = UE_ADDR_ANY,
156                 .direction = UE_DIR_OUT,
157                 .bufsize = U3G_BSIZE,/* bytes */
158                 .frames = U3G_TXFRAMES,
159                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
160                 .callback = &u3g_write_callback,
161         },
162
163         [U3G_BULK_RD] = {
164                 .type = UE_BULK,
165                 .endpoint = UE_ADDR_ANY,
166                 .direction = UE_DIR_IN,
167                 .bufsize = U3G_BSIZE,/* bytes */
168                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
169                 .callback = &u3g_read_callback,
170         },
171
172         [U3G_INTR] = {
173                 .type = UE_INTERRUPT,
174                 .endpoint = UE_ADDR_ANY,
175                 .direction = UE_DIR_IN,
176                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
177                 .bufsize = 0,   /* use wMaxPacketSize */
178                 .callback = &u3g_intr_callback,
179         },
180 };
181
182 static const struct ucom_callback u3g_callback = {
183         .ucom_cfg_get_status = &u3g_cfg_get_status,
184         .ucom_cfg_set_dtr = &u3g_cfg_set_dtr,
185         .ucom_cfg_set_rts = &u3g_cfg_set_rts,
186         .ucom_start_read = &u3g_start_read,
187         .ucom_stop_read = &u3g_stop_read,
188         .ucom_start_write = &u3g_start_write,
189         .ucom_stop_write = &u3g_stop_write,
190         .ucom_poll = &u3g_poll,
191         .ucom_free = &u3g_free,
192 };
193
194 static device_method_t u3g_methods[] = {
195         DEVMETHOD(device_probe, u3g_probe),
196         DEVMETHOD(device_attach, u3g_attach),
197         DEVMETHOD(device_detach, u3g_detach),
198         DEVMETHOD_END
199 };
200
201 static devclass_t u3g_devclass;
202
203 static driver_t u3g_driver = {
204         .name = "u3g",
205         .methods = u3g_methods,
206         .size = sizeof(struct u3g_softc),
207 };
208
209 DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0);
210 MODULE_DEPEND(u3g, ucom, 1, 1, 1);
211 MODULE_DEPEND(u3g, usb, 1, 1, 1);
212 MODULE_VERSION(u3g, 1);
213
214 static const STRUCT_USB_HOST_ID u3g_devs[] = {
215 #define U3G_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
216         U3G_DEV(ACERP, H10, 0),
217         U3G_DEV(AIRPLUS, MCD650, 0),
218         U3G_DEV(AIRPRIME, PC5220, 0),
219         U3G_DEV(AIRPRIME, AC313U, 0),
220         U3G_DEV(ALINK, 3G, 0),
221         U3G_DEV(ALINK, 3GU, 0),
222         U3G_DEV(ALINK, DWM652U5, 0),
223         U3G_DEV(AMOI, H01, 0),
224         U3G_DEV(AMOI, H01A, 0),
225         U3G_DEV(AMOI, H02, 0),
226         U3G_DEV(ANYDATA, ADU_500A, 0),
227         U3G_DEV(ANYDATA, ADU_620UW, 0),
228         U3G_DEV(ANYDATA, ADU_E100X, 0),
229         U3G_DEV(AXESSTEL, DATAMODEM, 0),
230         U3G_DEV(CMOTECH, CDMA_MODEM1, 0),
231         U3G_DEV(CMOTECH, CGU628, U3GINIT_CMOTECH),
232         U3G_DEV(DELL, U5500, 0),
233         U3G_DEV(DELL, U5505, 0),
234         U3G_DEV(DELL, U5510, 0),
235         U3G_DEV(DELL, U5520, 0),
236         U3G_DEV(DELL, U5520_2, 0),
237         U3G_DEV(DELL, U5520_3, 0),
238         U3G_DEV(DELL, U5700, 0),
239         U3G_DEV(DELL, U5700_2, 0),
240         U3G_DEV(DELL, U5700_3, 0),
241         U3G_DEV(DELL, U5700_4, 0),
242         U3G_DEV(DELL, U5720, 0),
243         U3G_DEV(DELL, U5720_2, 0),
244         U3G_DEV(DELL, U5730, 0),
245         U3G_DEV(DELL, U5730_2, 0),
246         U3G_DEV(DELL, U5730_3, 0),
247         U3G_DEV(DELL, U740, 0),
248         U3G_DEV(DLINK, DWR510_CD, U3GINIT_SCSIEJECT),
249         U3G_DEV(DLINK, DWR510, 0),
250         U3G_DEV(DLINK3, DWM652, 0),
251         U3G_DEV(HP, EV2200, 0),
252         U3G_DEV(HP, HS2300, 0),
253         U3G_DEV(HP, UN2420_QDL, 0),
254         U3G_DEV(HP, UN2420, 0),
255         U3G_DEV(HUAWEI, E1401, U3GINIT_HUAWEI),
256         U3G_DEV(HUAWEI, E1402, U3GINIT_HUAWEI),
257         U3G_DEV(HUAWEI, E1403, U3GINIT_HUAWEI),
258         U3G_DEV(HUAWEI, E1404, U3GINIT_HUAWEI),
259         U3G_DEV(HUAWEI, E1405, U3GINIT_HUAWEI),
260         U3G_DEV(HUAWEI, E1406, U3GINIT_HUAWEI),
261         U3G_DEV(HUAWEI, E1407, U3GINIT_HUAWEI),
262         U3G_DEV(HUAWEI, E1408, U3GINIT_HUAWEI),
263         U3G_DEV(HUAWEI, E1409, U3GINIT_HUAWEI),
264         U3G_DEV(HUAWEI, E140A, U3GINIT_HUAWEI),
265         U3G_DEV(HUAWEI, E140B, U3GINIT_HUAWEI),
266         U3G_DEV(HUAWEI, E140D, U3GINIT_HUAWEI),
267         U3G_DEV(HUAWEI, E140E, U3GINIT_HUAWEI),
268         U3G_DEV(HUAWEI, E140F, U3GINIT_HUAWEI),
269         U3G_DEV(HUAWEI, E1410, U3GINIT_HUAWEI),
270         U3G_DEV(HUAWEI, E1411, U3GINIT_HUAWEI),
271         U3G_DEV(HUAWEI, E1412, U3GINIT_HUAWEI),
272         U3G_DEV(HUAWEI, E1413, U3GINIT_HUAWEI),
273         U3G_DEV(HUAWEI, E1414, U3GINIT_HUAWEI),
274         U3G_DEV(HUAWEI, E1415, U3GINIT_HUAWEI),
275         U3G_DEV(HUAWEI, E1416, U3GINIT_HUAWEI),
276         U3G_DEV(HUAWEI, E1417, U3GINIT_HUAWEI),
277         U3G_DEV(HUAWEI, E1418, U3GINIT_HUAWEI),
278         U3G_DEV(HUAWEI, E1419, U3GINIT_HUAWEI),
279         U3G_DEV(HUAWEI, E141A, U3GINIT_HUAWEI),
280         U3G_DEV(HUAWEI, E141B, U3GINIT_HUAWEI),
281         U3G_DEV(HUAWEI, E141C, U3GINIT_HUAWEI),
282         U3G_DEV(HUAWEI, E141D, U3GINIT_HUAWEI),
283         U3G_DEV(HUAWEI, E141E, U3GINIT_HUAWEI),
284         U3G_DEV(HUAWEI, E141F, U3GINIT_HUAWEI),
285         U3G_DEV(HUAWEI, E1420, U3GINIT_HUAWEI),
286         U3G_DEV(HUAWEI, E1421, U3GINIT_HUAWEI),
287         U3G_DEV(HUAWEI, E1422, U3GINIT_HUAWEI),
288         U3G_DEV(HUAWEI, E1423, U3GINIT_HUAWEI),
289         U3G_DEV(HUAWEI, E1424, U3GINIT_HUAWEI),
290         U3G_DEV(HUAWEI, E1425, U3GINIT_HUAWEI),
291         U3G_DEV(HUAWEI, E1426, U3GINIT_HUAWEI),
292         U3G_DEV(HUAWEI, E1427, U3GINIT_HUAWEI),
293         U3G_DEV(HUAWEI, E1428, U3GINIT_HUAWEI),
294         U3G_DEV(HUAWEI, E1429, U3GINIT_HUAWEI),
295         U3G_DEV(HUAWEI, E142A, U3GINIT_HUAWEI),
296         U3G_DEV(HUAWEI, E142B, U3GINIT_HUAWEI),
297         U3G_DEV(HUAWEI, E142C, U3GINIT_HUAWEI),
298         U3G_DEV(HUAWEI, E142D, U3GINIT_HUAWEI),
299         U3G_DEV(HUAWEI, E142E, U3GINIT_HUAWEI),
300         U3G_DEV(HUAWEI, E142F, U3GINIT_HUAWEI),
301         U3G_DEV(HUAWEI, E1430, U3GINIT_HUAWEI),
302         U3G_DEV(HUAWEI, E1431, U3GINIT_HUAWEI),
303         U3G_DEV(HUAWEI, E1432, U3GINIT_HUAWEI),
304         U3G_DEV(HUAWEI, E1433, U3GINIT_HUAWEI),
305         U3G_DEV(HUAWEI, E1434, U3GINIT_HUAWEI),
306         U3G_DEV(HUAWEI, E1435, U3GINIT_HUAWEI),
307         U3G_DEV(HUAWEI, E1436, U3GINIT_HUAWEI),
308         U3G_DEV(HUAWEI, E1437, U3GINIT_HUAWEI),
309         U3G_DEV(HUAWEI, E1438, U3GINIT_HUAWEI),
310         U3G_DEV(HUAWEI, E1439, U3GINIT_HUAWEI),
311         U3G_DEV(HUAWEI, E143A, U3GINIT_HUAWEI),
312         U3G_DEV(HUAWEI, E143B, U3GINIT_HUAWEI),
313         U3G_DEV(HUAWEI, E143C, U3GINIT_HUAWEI),
314         U3G_DEV(HUAWEI, E143D, U3GINIT_HUAWEI),
315         U3G_DEV(HUAWEI, E143E, U3GINIT_HUAWEI),
316         U3G_DEV(HUAWEI, E143F, U3GINIT_HUAWEI),
317         U3G_DEV(HUAWEI, E173, 0),
318         U3G_DEV(HUAWEI, E173_INIT, U3GINIT_HUAWEISCSI),
319         U3G_DEV(HUAWEI, E3131, 0),
320         U3G_DEV(HUAWEI, E3131_INIT, U3GINIT_HUAWEISCSI),
321         U3G_DEV(HUAWEI, E180V, U3GINIT_HUAWEI),
322         U3G_DEV(HUAWEI, E220, U3GINIT_HUAWEI),
323         U3G_DEV(HUAWEI, E220BIS, U3GINIT_HUAWEI),
324         U3G_DEV(HUAWEI, E392, U3GINIT_HUAWEISCSI),
325         U3G_DEV(HUAWEI, MOBILE, U3GINIT_HUAWEI),
326         U3G_DEV(HUAWEI, E1752, U3GINIT_HUAWEISCSI),
327         U3G_DEV(HUAWEI, E1820, U3GINIT_HUAWEISCSI),
328         U3G_DEV(HUAWEI, K3765, U3GINIT_HUAWEI),
329         U3G_DEV(HUAWEI, K3765_INIT, U3GINIT_HUAWEISCSI),
330         U3G_DEV(HUAWEI, K3770, U3GINIT_HUAWEI),
331         U3G_DEV(HUAWEI, K3770_INIT, U3GINIT_HUAWEISCSI),
332         U3G_DEV(HUAWEI, K4505, U3GINIT_HUAWEI),
333         U3G_DEV(HUAWEI, K4505_INIT, U3GINIT_HUAWEISCSI),
334         U3G_DEV(HUAWEI, ETS2055, U3GINIT_HUAWEI),
335         U3G_DEV(KYOCERA2, CDMA_MSM_K, 0),
336         U3G_DEV(KYOCERA2, KPC680, 0),
337         U3G_DEV(LONGCHEER, WM66, U3GINIT_HUAWEI),
338         U3G_DEV(LONGCHEER, DISK, U3GINIT_TCT),
339         U3G_DEV(LONGCHEER, W14, 0),
340         U3G_DEV(LONGCHEER, XSSTICK, 0),
341         U3G_DEV(MERLIN, V620, 0),
342         U3G_DEV(NEOTEL, PRIME, 0),
343         U3G_DEV(NOVATEL, E725, 0),
344         U3G_DEV(NOVATEL, ES620, 0),
345         U3G_DEV(NOVATEL, ES620_2, 0),
346         U3G_DEV(NOVATEL, EU730, 0),
347         U3G_DEV(NOVATEL, EU740, 0),
348         U3G_DEV(NOVATEL, EU870D, 0),
349         U3G_DEV(NOVATEL, MC760, 0),
350         U3G_DEV(NOVATEL, MC547, 0),
351         U3G_DEV(NOVATEL, MC679, 0),
352         U3G_DEV(NOVATEL, MC950D, 0),
353         U3G_DEV(NOVATEL, MIFI2200, U3GINIT_SCSIEJECT),
354         U3G_DEV(NOVATEL, MIFI2200V, U3GINIT_SCSIEJECT),
355         U3G_DEV(NOVATEL, U720, 0),
356         U3G_DEV(NOVATEL, U727, 0),
357         U3G_DEV(NOVATEL, U727_2, 0),
358         U3G_DEV(NOVATEL, U740, 0),
359         U3G_DEV(NOVATEL, U740_2, 0),
360         U3G_DEV(NOVATEL, U760, U3GINIT_SCSIEJECT),
361         U3G_DEV(NOVATEL, U870, 0),
362         U3G_DEV(NOVATEL, V620, 0),
363         U3G_DEV(NOVATEL, V640, 0),
364         U3G_DEV(NOVATEL, V720, 0),
365         U3G_DEV(NOVATEL, V740, 0),
366         U3G_DEV(NOVATEL, X950D, 0),
367         U3G_DEV(NOVATEL, XU870, 0),
368         U3G_DEV(MOTOROLA2, MB886, U3GINIT_SCSIEJECT),
369         U3G_DEV(OPTION, E6500, 0),
370         U3G_DEV(OPTION, E6501, 0),
371         U3G_DEV(OPTION, E6601, 0),
372         U3G_DEV(OPTION, E6721, 0),
373         U3G_DEV(OPTION, E6741, 0),
374         U3G_DEV(OPTION, E6761, 0),
375         U3G_DEV(OPTION, E6800, 0),
376         U3G_DEV(OPTION, E7021, 0),
377         U3G_DEV(OPTION, E7041, 0),
378         U3G_DEV(OPTION, E7061, 0),
379         U3G_DEV(OPTION, E7100, 0),
380         U3G_DEV(OPTION, GE40X, 0),
381         U3G_DEV(OPTION, GT3G, 0),
382         U3G_DEV(OPTION, GT3GPLUS, 0),
383         U3G_DEV(OPTION, GT3GQUAD, 0),
384         U3G_DEV(OPTION, GT3G_1, 0),
385         U3G_DEV(OPTION, GT3G_2, 0),
386         U3G_DEV(OPTION, GT3G_3, 0),
387         U3G_DEV(OPTION, GT3G_4, 0),
388         U3G_DEV(OPTION, GT3G_5, 0),
389         U3G_DEV(OPTION, GT3G_6, 0),
390         U3G_DEV(OPTION, GTHSDPA, 0),
391         U3G_DEV(OPTION, GTM380, 0),
392         U3G_DEV(OPTION, GTMAX36, 0),
393         U3G_DEV(OPTION, GTMAX380HSUPAE, 0),
394         U3G_DEV(OPTION, GTMAXHSUPA, 0),
395         U3G_DEV(OPTION, GTMAXHSUPAE, 0),
396         U3G_DEV(OPTION, VODAFONEMC3G, 0),
397         U3G_DEV(QISDA, H20_1, 0),
398         U3G_DEV(QISDA, H20_2, 0),
399         U3G_DEV(QISDA, H21_1, 0),
400         U3G_DEV(QISDA, H21_2, 0),
401         U3G_DEV(QUALCOMM, NTT_L02C_MODEM, U3GINIT_SCSIEJECT),
402         U3G_DEV(QUALCOMM2, AC8700, 0),
403         U3G_DEV(QUALCOMM2, MF330, 0),
404         U3G_DEV(QUALCOMM2, SIM5218, 0),
405         U3G_DEV(QUALCOMM2, VW110L, U3GINIT_SCSIEJECT),
406         U3G_DEV(QUALCOMM2, GOBI2000_QDL, 0),
407         U3G_DEV(QUALCOMM2, GOBI2000, 0),
408         U3G_DEV(QUALCOMMINC, AC2726, 0),
409         U3G_DEV(QUALCOMMINC, AC8700, 0),
410         U3G_DEV(QUALCOMMINC, AC8710, 0),
411         U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GINIT_SCSIEJECT),
412         U3G_DEV(QUALCOMMINC, E0002, 0),
413         U3G_DEV(QUALCOMMINC, E0003, 0),
414         U3G_DEV(QUALCOMMINC, E0004, 0),
415         U3G_DEV(QUALCOMMINC, E0005, 0),
416         U3G_DEV(QUALCOMMINC, E0006, 0),
417         U3G_DEV(QUALCOMMINC, E0007, 0),
418         U3G_DEV(QUALCOMMINC, E0008, 0),
419         U3G_DEV(QUALCOMMINC, E0009, 0),
420         U3G_DEV(QUALCOMMINC, E000A, 0),
421         U3G_DEV(QUALCOMMINC, E000B, 0),
422         U3G_DEV(QUALCOMMINC, E000C, 0),
423         U3G_DEV(QUALCOMMINC, E000D, 0),
424         U3G_DEV(QUALCOMMINC, E000E, 0),
425         U3G_DEV(QUALCOMMINC, E000F, 0),
426         U3G_DEV(QUALCOMMINC, E0010, 0),
427         U3G_DEV(QUALCOMMINC, E0011, 0),
428         U3G_DEV(QUALCOMMINC, E0012, 0),
429         U3G_DEV(QUALCOMMINC, E0013, 0),
430         U3G_DEV(QUALCOMMINC, E0014, 0),
431         U3G_DEV(QUALCOMMINC, E0017, 0),
432         U3G_DEV(QUALCOMMINC, E0018, 0),
433         U3G_DEV(QUALCOMMINC, E0019, 0),
434         U3G_DEV(QUALCOMMINC, E0020, 0),
435         U3G_DEV(QUALCOMMINC, E0021, 0),
436         U3G_DEV(QUALCOMMINC, E0022, 0),
437         U3G_DEV(QUALCOMMINC, E0023, 0),
438         U3G_DEV(QUALCOMMINC, E0024, 0),
439         U3G_DEV(QUALCOMMINC, E0025, 0),
440         U3G_DEV(QUALCOMMINC, E0026, 0),
441         U3G_DEV(QUALCOMMINC, E0027, 0),
442         U3G_DEV(QUALCOMMINC, E0028, 0),
443         U3G_DEV(QUALCOMMINC, E0029, 0),
444         U3G_DEV(QUALCOMMINC, E0030, 0),
445         U3G_DEV(QUALCOMMINC, E0032, 0),
446         U3G_DEV(QUALCOMMINC, E0033, 0),
447         U3G_DEV(QUALCOMMINC, E0037, 0),
448         U3G_DEV(QUALCOMMINC, E0039, 0),
449         U3G_DEV(QUALCOMMINC, E0042, 0),
450         U3G_DEV(QUALCOMMINC, E0043, 0),
451         U3G_DEV(QUALCOMMINC, E0048, 0),
452         U3G_DEV(QUALCOMMINC, E0049, 0),
453         U3G_DEV(QUALCOMMINC, E0051, 0),
454         U3G_DEV(QUALCOMMINC, E0052, 0),
455         U3G_DEV(QUALCOMMINC, E0054, 0),
456         U3G_DEV(QUALCOMMINC, E0055, 0),
457         U3G_DEV(QUALCOMMINC, E0057, 0),
458         U3G_DEV(QUALCOMMINC, E0058, 0),
459         U3G_DEV(QUALCOMMINC, E0059, 0),
460         U3G_DEV(QUALCOMMINC, E0060, 0),
461         U3G_DEV(QUALCOMMINC, E0061, 0),
462         U3G_DEV(QUALCOMMINC, E0062, 0),
463         U3G_DEV(QUALCOMMINC, E0063, 0),
464         U3G_DEV(QUALCOMMINC, E0064, 0),
465         U3G_DEV(QUALCOMMINC, E0066, 0),
466         U3G_DEV(QUALCOMMINC, E0069, 0),
467         U3G_DEV(QUALCOMMINC, E0070, 0),
468         U3G_DEV(QUALCOMMINC, E0073, 0),
469         U3G_DEV(QUALCOMMINC, E0076, 0),
470         U3G_DEV(QUALCOMMINC, E0078, 0),
471         U3G_DEV(QUALCOMMINC, E0082, 0),
472         U3G_DEV(QUALCOMMINC, E0086, 0),
473         U3G_DEV(QUALCOMMINC, SURFSTICK, 0),
474         U3G_DEV(QUALCOMMINC, E2002, 0),
475         U3G_DEV(QUALCOMMINC, E2003, 0),
476         U3G_DEV(QUALCOMMINC, K3772_Z, U3GINIT_SCSIEJECT),
477         U3G_DEV(QUALCOMMINC, MF626, 0),
478         U3G_DEV(QUALCOMMINC, MF628, 0),
479         U3G_DEV(QUALCOMMINC, MF633R, 0),
480         U3G_DEV(QUANTA, GKE, 0),
481         U3G_DEV(QUANTA, GLE, 0),
482         U3G_DEV(QUANTA, GLX, 0),
483         U3G_DEV(QUANTA, Q101, 0),
484         U3G_DEV(QUANTA, Q111, 0),
485         U3G_DEV(SIERRA, AC402, 0),
486         U3G_DEV(SIERRA, AC595U, 0),
487         U3G_DEV(SIERRA, AC313U, 0),
488         U3G_DEV(SIERRA, AC597E, 0),
489         U3G_DEV(SIERRA, AC875E, 0),
490         U3G_DEV(SIERRA, AC875U, 0),
491         U3G_DEV(SIERRA, AC875U_2, 0),
492         U3G_DEV(SIERRA, AC880, 0),
493         U3G_DEV(SIERRA, AC880E, 0),
494         U3G_DEV(SIERRA, AC880U, 0),
495         U3G_DEV(SIERRA, AC881, 0),
496         U3G_DEV(SIERRA, AC881E, 0),
497         U3G_DEV(SIERRA, AC881U, 0),
498         U3G_DEV(SIERRA, AC885E, 0),
499         U3G_DEV(SIERRA, AC885E_2, 0),
500         U3G_DEV(SIERRA, AC885U, 0),
501         U3G_DEV(SIERRA, AIRCARD580, 0),
502         U3G_DEV(SIERRA, AIRCARD595, 0),
503         U3G_DEV(SIERRA, AIRCARD875, 0),
504         U3G_DEV(SIERRA, C22, 0),
505         U3G_DEV(SIERRA, C597, 0),
506         U3G_DEV(SIERRA, C888, 0),
507         U3G_DEV(SIERRA, E0029, 0),
508         U3G_DEV(SIERRA, E6892, 0),
509         U3G_DEV(SIERRA, E6893, 0),
510         U3G_DEV(SIERRA, EM5625, 0),
511         U3G_DEV(SIERRA, EM5725, 0),
512         U3G_DEV(SIERRA, MC5720, 0),
513         U3G_DEV(SIERRA, MC5720_2, 0),
514         U3G_DEV(SIERRA, MC5725, 0),
515         U3G_DEV(SIERRA, MC5727, 0),
516         U3G_DEV(SIERRA, MC5727_2, 0),
517         U3G_DEV(SIERRA, MC5728, 0),
518         U3G_DEV(SIERRA, MC8700, 0),
519         U3G_DEV(SIERRA, MC8755, 0),
520         U3G_DEV(SIERRA, MC8755_2, 0),
521         U3G_DEV(SIERRA, MC8755_3, 0),
522         U3G_DEV(SIERRA, MC8755_4, 0),
523         U3G_DEV(SIERRA, MC8765, 0),
524         U3G_DEV(SIERRA, MC8765_2, 0),
525         U3G_DEV(SIERRA, MC8765_3, 0),
526         U3G_DEV(SIERRA, MC8775, 0),
527         U3G_DEV(SIERRA, MC8775_2, 0),
528         U3G_DEV(SIERRA, MC8780, 0),
529         U3G_DEV(SIERRA, MC8780_2, 0),
530         U3G_DEV(SIERRA, MC8780_3, 0),
531         U3G_DEV(SIERRA, MC8781, 0),
532         U3G_DEV(SIERRA, MC8781_2, 0),
533         U3G_DEV(SIERRA, MC8781_3, 0),
534         U3G_DEV(SIERRA, MC8785, 0),
535         U3G_DEV(SIERRA, MC8785_2, 0),
536         U3G_DEV(SIERRA, MC8790, 0),
537         U3G_DEV(SIERRA, MC8791, 0),
538         U3G_DEV(SIERRA, MC8792, 0),
539         U3G_DEV(SIERRA, MINI5725, 0),
540         U3G_DEV(SIERRA, T11, 0),
541         U3G_DEV(SIERRA, T598, 0),
542         U3G_DEV(SILABS, SAEL, U3GINIT_SAEL_M460),
543         U3G_DEV(STELERA, C105, 0),
544         U3G_DEV(STELERA, E1003, 0),
545         U3G_DEV(STELERA, E1004, 0),
546         U3G_DEV(STELERA, E1005, 0),
547         U3G_DEV(STELERA, E1006, 0),
548         U3G_DEV(STELERA, E1007, 0),
549         U3G_DEV(STELERA, E1008, 0),
550         U3G_DEV(STELERA, E1009, 0),
551         U3G_DEV(STELERA, E100A, 0),
552         U3G_DEV(STELERA, E100B, 0),
553         U3G_DEV(STELERA, E100C, 0),
554         U3G_DEV(STELERA, E100D, 0),
555         U3G_DEV(STELERA, E100E, 0),
556         U3G_DEV(STELERA, E100F, 0),
557         U3G_DEV(STELERA, E1010, 0),
558         U3G_DEV(STELERA, E1011, 0),
559         U3G_DEV(STELERA, E1012, 0),
560         U3G_DEV(TCTMOBILE, X060S, 0),
561         U3G_DEV(TCTMOBILE, X080S, U3GINIT_TCT),
562         U3G_DEV(TELIT, UC864E, 0),
563         U3G_DEV(TELIT, UC864G, 0),
564         U3G_DEV(TLAYTECH, TEU800, 0),
565         U3G_DEV(TOSHIBA, G450, 0),
566         U3G_DEV(TOSHIBA, HSDPA, 0),
567         U3G_DEV(YISO, C893, 0),
568         U3G_DEV(WETELECOM, WM_D200, 0),
569         /* Autoinstallers */
570         U3G_DEV(NOVATEL, ZEROCD, U3GINIT_SCSIEJECT),
571         U3G_DEV(OPTION, GTICON322, U3GINIT_REZERO),
572         U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GINIT_ZTESTOR),
573         U3G_DEV(QUALCOMMINC, ZTE_STOR2, U3GINIT_SCSIEJECT),
574         U3G_DEV(QUANTA, Q101_STOR, U3GINIT_SCSIEJECT),
575         U3G_DEV(SIERRA, TRUINSTALL, U3GINIT_SIERRA),
576 #undef  U3G_DEV
577 };
578
579 static int
580 u3g_sierra_init(struct usb_device *udev)
581 {
582         struct usb_device_request req;
583
584         req.bmRequestType = UT_VENDOR;
585         req.bRequest = UR_SET_INTERFACE;
586         USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
587         USETW(req.wIndex, UHF_PORT_CONNECTION);
588         USETW(req.wLength, 0);
589
590         if (usbd_do_request_flags(udev, NULL, &req,
591             NULL, 0, NULL, USB_MS_HZ)) {
592                 /* ignore any errors */
593         }
594         return (0);
595 }
596
597 static int
598 u3g_huawei_init(struct usb_device *udev)
599 {
600         struct usb_device_request req;
601
602         req.bmRequestType = UT_WRITE_DEVICE;
603         req.bRequest = UR_SET_FEATURE;
604         USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
605         USETW(req.wIndex, UHF_PORT_SUSPEND);
606         USETW(req.wLength, 0);
607
608         if (usbd_do_request_flags(udev, NULL, &req,
609             NULL, 0, NULL, USB_MS_HZ)) {
610                 /* ignore any errors */
611         }
612         return (0);
613 }
614
615 static void
616 u3g_sael_m460_init(struct usb_device *udev)
617 {
618         static const uint8_t setup[][24] = {
619              { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
620              { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
621              { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 
622                0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
623                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
624              { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
625              { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
626              { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
627              { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
628              { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
629              { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
630              { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
631              { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
632                0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
633              { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 
634                0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
635                0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
636              { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
637              { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
638              { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
639              { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
640              { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
641                0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
642              { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
643                0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 
644                0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
645              { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
646         };
647
648         struct usb_device_request req;
649         usb_error_t err;
650         uint16_t len;
651         uint8_t buf[0x300];
652         uint8_t n;
653
654         DPRINTFN(1, "\n");
655
656         if (usbd_req_set_alt_interface_no(udev, NULL, 0, 0)) {
657                 DPRINTFN(0, "Alt setting 0 failed\n");
658                 return;
659         }
660
661         for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {
662
663                 memcpy(&req, setup[n], sizeof(req));
664
665                 len = UGETW(req.wLength);
666                 if (req.bmRequestType & UE_DIR_IN) {
667                         if (len > sizeof(buf)) {
668                                 DPRINTFN(0, "too small buffer\n");
669                                 continue;
670                         }
671                         err = usbd_do_request(udev, NULL, &req, buf);
672                 } else {
673                         if (len > (sizeof(setup[0]) - 8)) {
674                                 DPRINTFN(0, "too small buffer\n");
675                                 continue;
676                         }
677                         err = usbd_do_request(udev, NULL, &req, 
678                             __DECONST(uint8_t *, &setup[n][8]));
679                 }
680                 if (err) {
681                         DPRINTFN(1, "request %u failed\n",
682                             (unsigned int)n);
683                         /*
684                          * Some of the requests will fail. Stop doing
685                          * requests when we are getting timeouts so
686                          * that we don't block the explore/attach
687                          * thread forever.
688                          */
689                         if (err == USB_ERR_TIMEOUT)
690                                 break;
691                 }
692         }
693 }
694
695 /*
696  * The following function handles 3G modem devices (E220, Mobile,
697  * etc.) with auto-install flash disks for Windows/MacOSX on the first
698  * interface.  After some command or some delay they change appearance
699  * to a modem.
700  */
701 static void
702 u3g_test_autoinst(void *arg, struct usb_device *udev,
703     struct usb_attach_arg *uaa)
704 {
705         struct usb_interface *iface;
706         struct usb_interface_descriptor *id;
707         int error;
708         unsigned long method;
709
710         if (uaa->dev_state != UAA_DEV_READY)
711                 return;
712
713         iface = usbd_get_iface(udev, 0);
714         if (iface == NULL)
715                 return;
716         id = iface->idesc;
717         if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
718                 return;
719
720         if (usb_test_quirk(uaa, UQ_MSC_EJECT_HUAWEI))
721                 method = U3GINIT_HUAWEI;
722         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_SIERRA))
723                 method = U3GINIT_SIERRA;
724         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_SCSIEJECT))
725                 method = U3GINIT_SCSIEJECT;
726         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_REZERO))
727                 method = U3GINIT_REZERO;
728         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_ZTESTOR))
729                 method = U3GINIT_ZTESTOR;
730         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_CMOTECH))
731                 method = U3GINIT_CMOTECH;
732         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_WAIT))
733                 method = U3GINIT_WAIT;
734         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_HUAWEISCSI))
735                 method = U3GINIT_HUAWEISCSI;
736         else if (usb_test_quirk(uaa, UQ_MSC_EJECT_TCT))
737                 method = U3GINIT_TCT;
738         else if (usbd_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa) == 0)
739                 method = USB_GET_DRIVER_INFO(uaa);
740         else
741                 return;         /* no device match */
742
743         if (bootverbose) {
744                 printf("Ejecting %s %s using method %ld\n",
745                        usb_get_manufacturer(udev),
746                        usb_get_product(udev), method);
747         }
748
749         switch (method) {
750                 case U3GINIT_HUAWEI:
751                         error = u3g_huawei_init(udev);
752                         break;
753                 case U3GINIT_HUAWEISCSI:
754                         error = usb_msc_eject(udev, 0, MSC_EJECT_HUAWEI);
755                         break;
756                 case U3GINIT_SCSIEJECT:
757                         error = usb_msc_eject(udev, 0, MSC_EJECT_STOPUNIT);
758                         break;
759                 case U3GINIT_REZERO:
760                         error = usb_msc_eject(udev, 0, MSC_EJECT_REZERO);
761                         break;
762                 case U3GINIT_ZTESTOR:
763                         error = usb_msc_eject(udev, 0, MSC_EJECT_STOPUNIT);
764                         error |= usb_msc_eject(udev, 0, MSC_EJECT_ZTESTOR);
765                         break;
766                 case U3GINIT_CMOTECH:
767                         error = usb_msc_eject(udev, 0, MSC_EJECT_CMOTECH);
768                         break;
769                 case U3GINIT_TCT:
770                         error = usb_msc_eject(udev, 0, MSC_EJECT_TCT);
771                         break;
772                 case U3GINIT_SIERRA:
773                         error = u3g_sierra_init(udev);
774                         break;
775                 case U3GINIT_WAIT:
776                         /* Just pretend we ejected, the card will timeout */
777                         error = 0;
778                         break;
779                 default:
780                         /* no 3G eject quirks */
781                         error = EOPNOTSUPP;
782                         break;
783         }
784         if (error == 0) {
785                 /* success, mark the udev as disappearing */
786                 uaa->dev_state = UAA_DEV_EJECTING;
787         }
788 }
789
790 static int
791 u3g_driver_loaded(struct module *mod, int what, void *arg)
792 {
793         switch (what) {
794         case MOD_LOAD:
795                 /* register our autoinstall handler */
796                 u3g_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
797                     u3g_test_autoinst, NULL, EVENTHANDLER_PRI_ANY);
798                 break;
799         case MOD_UNLOAD:
800                 EVENTHANDLER_DEREGISTER(usb_dev_configured, u3g_etag);
801                 break;
802         default:
803                 return (EOPNOTSUPP);
804         }
805         return (0);
806 }
807
808 static int
809 u3g_probe(device_t self)
810 {
811         struct usb_attach_arg *uaa = device_get_ivars(self);
812
813         if (uaa->usb_mode != USB_MODE_HOST) {
814                 return (ENXIO);
815         }
816         if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
817                 return (ENXIO);
818         }
819         if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
820                 return (ENXIO);
821         }
822         return (usbd_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
823 }
824
825 static int
826 u3g_attach(device_t dev)
827 {
828         struct usb_config u3g_config_tmp[U3G_N_TRANSFER];
829         struct usb_attach_arg *uaa = device_get_ivars(dev);
830         struct u3g_softc *sc = device_get_softc(dev);
831         struct usb_interface *iface;
832         struct usb_interface_descriptor *id;
833         uint32_t iface_valid;
834         int error, type, nports;
835         int ep, n;
836         uint8_t i;
837
838         DPRINTF("sc=%p\n", sc);
839
840         type = USB_GET_DRIVER_INFO(uaa);
841         if (type == U3GINIT_SAEL_M460
842             || usb_test_quirk(uaa, UQ_MSC_EJECT_SAEL_M460)) {
843                 u3g_sael_m460_init(uaa->device);
844         }
845
846         /* copy in USB config */
847         for (n = 0; n != U3G_N_TRANSFER; n++) 
848                 u3g_config_tmp[n] = u3g_config[n];
849
850         device_set_usb_desc(dev);
851         mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
852         ucom_ref(&sc->sc_super_ucom);
853
854         sc->sc_udev = uaa->device;
855
856         /* Claim all interfaces on the device */
857         iface_valid = 0;
858         for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) {
859                 iface = usbd_get_iface(uaa->device, i);
860                 if (iface == NULL)
861                         break;
862                 id = usbd_get_interface_descriptor(iface);
863                 if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR)
864                         continue;
865                 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
866                 iface_valid |= (1<<i);
867         }
868
869         i = 0;          /* interface index */
870         ep = 0;         /* endpoint index */
871         nports = 0;     /* number of ports */
872         while (i < USB_IFACE_MAX) {
873                 if ((iface_valid & (1<<i)) == 0) {
874                         i++;
875                         continue;
876                 }
877
878                 /* update BULK endpoint index */
879                 for (n = 0; n < U3G_N_TRANSFER; n++)
880                         u3g_config_tmp[n].ep_index = ep;
881
882                 /* try to allocate a set of BULK endpoints */
883                 error = usbd_transfer_setup(uaa->device, &i,
884                     sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER,
885                     &sc->sc_ucom[nports], &sc->sc_mtx);
886                 if (error) {
887                         /* next interface */
888                         i++;
889                         ep = 0;
890                         continue;
891                 }
892
893                 iface = usbd_get_iface(uaa->device, i);
894                 id = usbd_get_interface_descriptor(iface);
895                 sc->sc_iface[nports] = id->bInterfaceNumber;
896
897                 if (bootverbose && sc->sc_xfer[nports][U3G_INTR]) {
898                         device_printf(dev, "port %d supports modem control",
899                                       nports);
900                 }
901
902                 /* set stall by default */
903                 mtx_lock(&sc->sc_mtx);
904                 usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_WR]);
905                 usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_RD]);
906                 mtx_unlock(&sc->sc_mtx);
907
908                 nports++;       /* found one port */
909                 ep++;
910                 if (nports == U3G_MAXPORTS)
911                         break;
912         }
913         if (nports == 0) {
914                 device_printf(dev, "no ports found\n");
915                 goto detach;
916         }
917         sc->sc_numports = nports;
918
919         error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
920             sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
921         if (error) {
922                 DPRINTF("ucom_attach failed\n");
923                 goto detach;
924         }
925         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
926         device_printf(dev, "Found %u port%s.\n", sc->sc_numports,
927             sc->sc_numports > 1 ? "s":"");
928
929         return (0);
930
931 detach:
932         u3g_detach(dev);
933         return (ENXIO);
934 }
935
936 static int
937 u3g_detach(device_t dev)
938 {
939         struct u3g_softc *sc = device_get_softc(dev);
940         uint8_t subunit;
941
942         DPRINTF("sc=%p\n", sc);
943
944         /* NOTE: It is not dangerous to detach more ports than attached! */
945         ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
946
947         for (subunit = 0; subunit != U3G_MAXPORTS; subunit++)
948                 usbd_transfer_unsetup(sc->sc_xfer[subunit], U3G_N_TRANSFER);
949
950         device_claim_softc(dev);
951
952         u3g_free_softc(sc);
953
954         return (0);
955 }
956
957 UCOM_UNLOAD_DRAIN(u3g);
958
959 static void
960 u3g_free_softc(struct u3g_softc *sc)
961 {
962         if (ucom_unref(&sc->sc_super_ucom)) {
963                 mtx_destroy(&sc->sc_mtx);
964                 device_free_softc(sc);
965         }
966 }
967
968 static void
969 u3g_free(struct ucom_softc *ucom)
970 {
971         u3g_free_softc(ucom->sc_parent);
972 }
973
974 static void
975 u3g_start_read(struct ucom_softc *ucom)
976 {
977         struct u3g_softc *sc = ucom->sc_parent;
978
979         /* start interrupt endpoint (if configured) */
980         usbd_transfer_start(sc->sc_xfer[ucom->sc_subunit][U3G_INTR]);
981
982         /* start read endpoint */
983         usbd_transfer_start(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_RD]);
984 }
985
986 static void
987 u3g_stop_read(struct ucom_softc *ucom)
988 {
989         struct u3g_softc *sc = ucom->sc_parent;
990
991         /* stop interrupt endpoint (if configured) */
992         usbd_transfer_stop(sc->sc_xfer[ucom->sc_subunit][U3G_INTR]);
993
994         /* stop read endpoint */
995         usbd_transfer_stop(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_RD]);
996 }
997
998 static void
999 u3g_start_write(struct ucom_softc *ucom)
1000 {
1001         struct u3g_softc *sc = ucom->sc_parent;
1002
1003         usbd_transfer_start(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_WR]);
1004 }
1005
1006 static void
1007 u3g_stop_write(struct ucom_softc *ucom)
1008 {
1009         struct u3g_softc *sc = ucom->sc_parent;
1010
1011         usbd_transfer_stop(sc->sc_xfer[ucom->sc_subunit][U3G_BULK_WR]);
1012 }
1013
1014 static void
1015 u3g_write_callback(struct usb_xfer *xfer, usb_error_t error)
1016 {
1017         struct ucom_softc *ucom = usbd_xfer_softc(xfer);
1018         struct usb_page_cache *pc;
1019         uint32_t actlen;
1020         uint32_t frame;
1021
1022         switch (USB_GET_STATE(xfer)) {
1023         case USB_ST_TRANSFERRED:
1024         case USB_ST_SETUP:
1025 tr_setup:
1026                 for (frame = 0; frame != U3G_TXFRAMES; frame++) {
1027                         usbd_xfer_set_frame_offset(xfer, frame * U3G_TXSIZE, frame);
1028
1029                         pc = usbd_xfer_get_frame(xfer, frame);
1030                         if (ucom_get_data(ucom, pc, 0, U3G_TXSIZE, &actlen) == 0)
1031                                 break;
1032                         usbd_xfer_set_frame_len(xfer, frame, actlen);
1033                 }
1034                 if (frame != 0) {
1035                         usbd_xfer_set_frames(xfer, frame);
1036                         usbd_transfer_submit(xfer);
1037                 }
1038                 break;
1039
1040         default:                        /* Error */
1041                 if (error != USB_ERR_CANCELLED) {
1042                         /* do a builtin clear-stall */
1043                         usbd_xfer_set_stall(xfer);
1044                         goto tr_setup;
1045                 }
1046                 break;
1047         }
1048 }
1049
1050 static void
1051 u3g_read_callback(struct usb_xfer *xfer, usb_error_t error)
1052 {
1053         struct ucom_softc *ucom = usbd_xfer_softc(xfer);
1054         struct usb_page_cache *pc;
1055         int actlen;
1056
1057         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1058
1059         switch (USB_GET_STATE(xfer)) {
1060         case USB_ST_TRANSFERRED:
1061                 pc = usbd_xfer_get_frame(xfer, 0);
1062                 ucom_put_data(ucom, pc, 0, actlen);
1063
1064         case USB_ST_SETUP:
1065 tr_setup:
1066                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1067                 usbd_transfer_submit(xfer);
1068                 break;
1069
1070         default:                        /* Error */
1071                 if (error != USB_ERR_CANCELLED) {
1072                         /* do a builtin clear-stall */
1073                         usbd_xfer_set_stall(xfer);
1074                         goto tr_setup;
1075                 }
1076                 break;
1077         }
1078 }
1079
1080 static void
1081 u3g_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
1082 {
1083         struct u3g_softc *sc = ucom->sc_parent;
1084
1085         *lsr = sc->sc_lsr[ucom->sc_subunit];
1086         *msr = sc->sc_msr[ucom->sc_subunit];
1087 }
1088
1089 static void
1090 u3g_cfg_set_line(struct ucom_softc *ucom)
1091 {
1092         struct u3g_softc *sc = ucom->sc_parent;
1093         struct usb_device_request req;
1094
1095         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1096         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1097         USETW(req.wValue, sc->sc_line[ucom->sc_subunit]);
1098         req.wIndex[0] = sc->sc_iface[ucom->sc_subunit];
1099         req.wIndex[1] = 0;
1100         USETW(req.wLength, 0);
1101
1102         ucom_cfg_do_request(sc->sc_udev, ucom,
1103             &req, NULL, 0, 1000);
1104 }
1105
1106 static void
1107 u3g_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
1108 {
1109         struct u3g_softc *sc = ucom->sc_parent;
1110
1111         DPRINTF("onoff = %d\n", onoff);
1112
1113         if (onoff)
1114                 sc->sc_line[ucom->sc_subunit] |= UCDC_LINE_DTR;
1115         else
1116                 sc->sc_line[ucom->sc_subunit] &= ~UCDC_LINE_DTR;
1117
1118         u3g_cfg_set_line(ucom);
1119 }
1120
1121 static void
1122 u3g_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
1123 {
1124         struct u3g_softc *sc = ucom->sc_parent;
1125
1126         DPRINTF("onoff = %d\n", onoff);
1127
1128         if (onoff)
1129                 sc->sc_line[ucom->sc_subunit] |= UCDC_LINE_RTS;
1130         else
1131                 sc->sc_line[ucom->sc_subunit] &= ~UCDC_LINE_RTS;
1132
1133         u3g_cfg_set_line(ucom);
1134 }
1135
1136 static void
1137 u3g_intr_callback(struct usb_xfer *xfer, usb_error_t error)
1138 {
1139         struct ucom_softc *ucom = usbd_xfer_softc(xfer);
1140         struct u3g_softc *sc = ucom->sc_parent;
1141         struct usb_page_cache *pc;
1142         struct usb_cdc_notification pkt;
1143         int actlen;
1144         uint16_t wLen;
1145         uint8_t mstatus;
1146
1147         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1148
1149         switch (USB_GET_STATE(xfer)) {
1150         case USB_ST_TRANSFERRED:
1151                 if (actlen < 8) {       /* usb_cdc_notification with 2 data bytes */
1152                         DPRINTF("message too short (expected 8, received %d)\n", actlen);
1153                         goto tr_setup;
1154                 }
1155                 pc = usbd_xfer_get_frame(xfer, 0);
1156                 usbd_copy_out(pc, 0, &pkt, actlen);
1157
1158                 wLen = UGETW(pkt.wLength);
1159                 if (wLen < 2) {
1160                         DPRINTF("message too short (expected 2 data bytes, received %d)\n", wLen);
1161                         goto tr_setup;
1162                 }
1163
1164                 if (pkt.bmRequestType == UCDC_NOTIFICATION
1165                     && pkt.bNotification == UCDC_N_SERIAL_STATE) {
1166                         /*
1167                          * Set the serial state in ucom driver based on
1168                          * the bits from the notify message
1169                          */
1170                         DPRINTF("notify bytes = 0x%02x, 0x%02x\n",
1171                             pkt.data[0], pkt.data[1]);
1172
1173                         /* currently, lsr is always zero. */
1174                         sc->sc_lsr[ucom->sc_subunit] = 0;
1175                         sc->sc_msr[ucom->sc_subunit] = 0;
1176
1177                         mstatus = pkt.data[0];
1178
1179                         if (mstatus & UCDC_N_SERIAL_RI)
1180                                 sc->sc_msr[ucom->sc_subunit] |= SER_RI;
1181                         if (mstatus & UCDC_N_SERIAL_DSR)
1182                                 sc->sc_msr[ucom->sc_subunit] |= SER_DSR;
1183                         if (mstatus & UCDC_N_SERIAL_DCD)
1184                                 sc->sc_msr[ucom->sc_subunit] |= SER_DCD;
1185                         ucom_status_change(ucom);
1186                 }
1187
1188         case USB_ST_SETUP:
1189 tr_setup:
1190                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1191                 usbd_transfer_submit(xfer);
1192                 return;
1193
1194         default:                        /* Error */
1195                 if (error != USB_ERR_CANCELLED) {
1196                         /* try to clear stall first */
1197                         usbd_xfer_set_stall(xfer);
1198                         goto tr_setup;
1199                 }
1200                 return;
1201         }
1202 }
1203
1204 static void
1205 u3g_poll(struct ucom_softc *ucom)
1206 {
1207         struct u3g_softc *sc = ucom->sc_parent;
1208         usbd_transfer_poll(sc->sc_xfer[ucom->sc_subunit], U3G_N_TRANSFER);
1209 }