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