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