1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/* $NetBSD: ubsa.c,v 1.43 2021/08/07 16:19:17 thorpej Exp $ */
/*-
* Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Ichiro FUKUHARA (ichiro@ichiro.org).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ubsa.c,v 1.43 2021/08/07 16:19:17 thorpej Exp $");
#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/ioccom.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/select.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/poll.h>
#include <sys/sysctl.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbcdc.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usb_quirks.h>
#include <dev/usb/ucomvar.h>
#include <dev/usb/ubsavar.h>
#ifdef UBSA_DEBUG
int ubsadebug = 0;
#define DPRINTFN(n, x) do { \
if (ubsadebug > (n)) \
printf x; \
} while (0)
#else
#define DPRINTFN(n, x)
#endif
#define DPRINTF(x) DPRINTFN(0, x)
static const struct ucom_methods ubsa_methods = {
.ucom_get_status = ubsa_get_status,
.ucom_set = ubsa_set,
.ucom_param = ubsa_param,
.ucom_open = ubsa_open,
.ucom_close = ubsa_close,
};
Static const struct ubsa_type {
struct usb_devno ubsa_dev;
int ubsa_quadumts;
} ubsa_devs[] = {
/* BELKIN F5U103 */
{ { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 }, 0 },
/* BELKIN F5U120 */
{ { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 }, 0 },
/* GoHubs GO-COM232 */
{ { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM }, 0 },
/* GoHubs GO-COM232 */
{ { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 }, 0 },
/* Peracom */
{ { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 }, 0 },
/* Option N.V. */
{ { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MC3G }, 0 },
{ { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS2 }, 1 },
{ { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS }, 1 },
{ { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADPLUSUMTS }, 1 },
{ { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_HSDPA }, 1 },
};
#define ubsa_lookup(v, p) ((const struct ubsa_type *)usb_lookup(ubsa_devs, v, p))
int ubsa_match(device_t, cfdata_t, void *);
void ubsa_attach(device_t, device_t, void *);
void ubsa_childdet(device_t, device_t);
int ubsa_detach(device_t, int);
CFATTACH_DECL2_NEW(ubsa, sizeof(struct ubsa_softc),
ubsa_match, ubsa_attach, ubsa_detach, NULL, NULL, ubsa_childdet);
int
ubsa_match(device_t parent, cfdata_t match, void *aux)
{
struct usb_attach_arg *uaa = aux;
return (ubsa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
}
void
ubsa_attach(device_t parent, device_t self, void *aux)
{
struct ubsa_softc *sc = device_private(self);
struct usb_attach_arg *uaa = aux;
struct usbd_device *dev = uaa->uaa_device;
usb_config_descriptor_t *cdesc;
usb_interface_descriptor_t *id;
usb_endpoint_descriptor_t *ed;
char *devinfop;
usbd_status err;
struct ucom_attach_args ucaa;
int i;
sc->sc_dev = self;
sc->sc_dying = false;
aprint_naive("\n");
aprint_normal("\n");
devinfop = usbd_devinfo_alloc(dev, 0);
aprint_normal_dev(self, "%s\n", devinfop);
usbd_devinfo_free(devinfop);
sc->sc_udev = dev;
sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX;
sc->sc_numif = 1; /* default device has one interface */
/*
* initialize rts, dtr variables to something
* different from boolean 0, 1
*/
sc->sc_dtr = -1;
sc->sc_rts = -1;
/*
* Quad UMTS cards use different requests to
* control com settings and only some.
*/
sc->sc_quadumts = ubsa_lookup(uaa->uaa_vendor, uaa->uaa_product)->ubsa_quadumts;
DPRINTF(("ubsa attach: sc = %p\n", sc));
/* Move the device into the configured state. */
err = usbd_set_config_index(dev, sc->sc_config_index, 1);
if (err) {
aprint_error_dev(self,
"failed to set configuration: %s\n",
usbd_errstr(err));
goto error;
}
/* get the config descriptor */
cdesc = usbd_get_config_descriptor(sc->sc_udev);
if (cdesc == NULL) {
aprint_error_dev(self,
"failed to get configuration descriptor\n");
goto error;
}
sc->sc_intr_number = -1;
sc->sc_intr_pipe = NULL;
/* get the interfaces */
err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET,
&sc->sc_iface[0]);
if (err) {
/* can not get main interface */
goto error;
}
/* Find the endpoints */
id = usbd_get_interface_descriptor(sc->sc_iface[0]);
sc->sc_iface_number[0] = id->bInterfaceNumber;
/* initialize endpoints */
ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
for (i = 0; i < id->bNumEndpoints; i++) {
ed = usbd_interface2endpoint_descriptor(sc->sc_iface[0], i);
if (ed == NULL) {
aprint_error_dev(self,
"no endpoint descriptor for %d\n", i);
break;
}
if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
sc->sc_intr_number = ed->bEndpointAddress;
sc->sc_isize = UGETW(ed->wMaxPacketSize);
} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
ucaa.ucaa_bulkin = ed->bEndpointAddress;
ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
ucaa.ucaa_bulkout = ed->bEndpointAddress;
ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize);
}
} /* end of Endpoint loop */
if (sc->sc_intr_number == -1) {
aprint_error_dev(self, "Could not find interrupt in\n");
goto error;
}
if (ucaa.ucaa_bulkin == -1) {
aprint_error_dev(self, "Could not find data bulk in\n");
goto error;
}
if (ucaa.ucaa_bulkout == -1) {
aprint_error_dev(self, "Could not find data bulk out\n");
goto error;
}
ucaa.ucaa_portno = 0;
/* bulkin, bulkout set above */
ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
ucaa.ucaa_opkthdrlen = 0;
ucaa.ucaa_device = dev;
ucaa.ucaa_iface = sc->sc_iface[0];
ucaa.ucaa_methods = &ubsa_methods;
ucaa.ucaa_arg = sc;
ucaa.ucaa_info = NULL;
DPRINTF(("ubsa: int#=%d, in = %#x, out = %#x, intr = %#x\n",
i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number));
sc->sc_subdevs[0] = config_found(self, &ucaa, ucomprint,
CFARGS(.submatch = ucomsubmatch));
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
return;
error:
sc->sc_dying = true;
return;
}
void
ubsa_childdet(device_t self, device_t child)
{
int i;
struct ubsa_softc *sc = device_private(self);
for (i = 0; i < sc->sc_numif; i++) {
if (sc->sc_subdevs[i] == child)
break;
}
KASSERT(i < sc->sc_numif);
sc->sc_subdevs[i] = NULL;
}
int
ubsa_detach(device_t self, int flags)
{
struct ubsa_softc *sc = device_private(self);
int i;
int rv = 0;
DPRINTF(("ubsa_detach: sc = %p\n", sc));
sc->sc_dying = true;
ubsa_close_pipe(sc);
for (i = 0; i < sc->sc_numif; i++) {
if (sc->sc_subdevs[i] != NULL) {
rv |= config_detach(sc->sc_subdevs[i], flags);
sc->sc_subdevs[i] = NULL;
}
}
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
return rv;
}
|