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