1 | /* $NetBSD: ubsa.c,v 1.32 2016/07/07 06:55:42 msaitoh 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@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.32 2016/07/07 06:55:42 msaitoh Exp $" ); |
58 | |
59 | #include <sys/param.h> |
60 | #include <sys/systm.h> |
61 | #include <sys/kernel.h> |
62 | #include <sys/kmem.h> |
63 | #include <sys/ioccom.h> |
64 | #include <sys/fcntl.h> |
65 | #include <sys/conf.h> |
66 | #include <sys/tty.h> |
67 | #include <sys/file.h> |
68 | #include <sys/select.h> |
69 | #include <sys/proc.h> |
70 | #include <sys/device.h> |
71 | #include <sys/poll.h> |
72 | #include <sys/sysctl.h> |
73 | |
74 | #include <dev/usb/usb.h> |
75 | #include <dev/usb/usbcdc.h> |
76 | |
77 | #include <dev/usb/usbdi.h> |
78 | #include <dev/usb/usbdi_util.h> |
79 | #include <dev/usb/usbdevs.h> |
80 | #include <dev/usb/usb_quirks.h> |
81 | |
82 | #include <dev/usb/ucomvar.h> |
83 | #include <dev/usb/ubsavar.h> |
84 | |
85 | #ifdef UBSA_DEBUG |
86 | int ubsadebug = 0; |
87 | |
88 | #define DPRINTFN(n, x) do { \ |
89 | if (ubsadebug > (n)) \ |
90 | printf x; \ |
91 | } while (0) |
92 | #else |
93 | #define DPRINTFN(n, x) |
94 | #endif |
95 | #define DPRINTF(x) DPRINTFN(0, x) |
96 | |
97 | struct ucom_methods ubsa_methods = { |
98 | .ucom_get_status = ubsa_get_status, |
99 | .ucom_set = ubsa_set, |
100 | .ucom_param = ubsa_param, |
101 | .ucom_ioctl = NULL, |
102 | .ucom_open = ubsa_open, |
103 | .ucom_close = ubsa_close, |
104 | .ucom_read = NULL, |
105 | .ucom_write = NULL |
106 | }; |
107 | |
108 | Static const struct usb_devno ubsa_devs[] = { |
109 | /* BELKIN F5U103 */ |
110 | { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 }, |
111 | /* BELKIN F5U120 */ |
112 | { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 }, |
113 | /* GoHubs GO-COM232 */ |
114 | { USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM }, |
115 | /* GoHubs GO-COM232 */ |
116 | { USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 }, |
117 | /* Peracom */ |
118 | { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 }, |
119 | /* Option N.V. */ |
120 | { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MC3G }, |
121 | { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS2 }, |
122 | { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS }, |
123 | /* AnyDATA ADU-E100H */ |
124 | { USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100H }, |
125 | }; |
126 | #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p) |
127 | |
128 | int ubsa_match(device_t, cfdata_t, void *); |
129 | void ubsa_attach(device_t, device_t, void *); |
130 | void ubsa_childdet(device_t, device_t); |
131 | int ubsa_detach(device_t, int); |
132 | int ubsa_activate(device_t, enum devact); |
133 | |
134 | extern struct cfdriver ubsa_cd; |
135 | |
136 | CFATTACH_DECL2_NEW(ubsa, sizeof(struct ubsa_softc), |
137 | ubsa_match, ubsa_attach, ubsa_detach, ubsa_activate, NULL, ubsa_childdet); |
138 | |
139 | int |
140 | ubsa_match(device_t parent, cfdata_t match, void *aux) |
141 | { |
142 | struct usb_attach_arg *uaa = aux; |
143 | |
144 | return (ubsa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? |
145 | UMATCH_VENDOR_PRODUCT : UMATCH_NONE); |
146 | } |
147 | |
148 | void |
149 | ubsa_attach(device_t parent, device_t self, void *aux) |
150 | { |
151 | struct ubsa_softc *sc = device_private(self); |
152 | struct usb_attach_arg *uaa = aux; |
153 | struct usbd_device *dev = uaa->uaa_device; |
154 | usb_config_descriptor_t *cdesc; |
155 | usb_interface_descriptor_t *id; |
156 | usb_endpoint_descriptor_t *ed; |
157 | char *devinfop; |
158 | usbd_status err; |
159 | struct ucom_attach_args ucaa; |
160 | int i; |
161 | |
162 | sc->sc_dev = self; |
163 | |
164 | aprint_naive("\n" ); |
165 | aprint_normal("\n" ); |
166 | |
167 | devinfop = usbd_devinfo_alloc(dev, 0); |
168 | aprint_normal_dev(self, "%s\n" , devinfop); |
169 | usbd_devinfo_free(devinfop); |
170 | |
171 | sc->sc_udev = dev; |
172 | sc->sc_config_index = UBSA_DEFAULT_CONFIG_INDEX; |
173 | sc->sc_numif = 1; /* default device has one interface */ |
174 | |
175 | /* |
176 | * initialize rts, dtr variables to something |
177 | * different from boolean 0, 1 |
178 | */ |
179 | sc->sc_dtr = -1; |
180 | sc->sc_rts = -1; |
181 | |
182 | /* |
183 | * Quad UMTS cards use different requests to |
184 | * control com settings and only some. |
185 | */ |
186 | sc->sc_quadumts = 0; |
187 | if (uaa->uaa_vendor == USB_VENDOR_OPTIONNV) { |
188 | switch (uaa->uaa_product) { |
189 | case USB_PRODUCT_OPTIONNV_QUADUMTS: |
190 | case USB_PRODUCT_OPTIONNV_QUADUMTS2: |
191 | sc->sc_quadumts = 1; |
192 | break; |
193 | } |
194 | } |
195 | |
196 | DPRINTF(("ubsa attach: sc = %p\n" , sc)); |
197 | |
198 | /* Move the device into the configured state. */ |
199 | err = usbd_set_config_index(dev, sc->sc_config_index, 1); |
200 | if (err) { |
201 | aprint_error_dev(self, |
202 | "failed to set configuration: %s\n" , |
203 | usbd_errstr(err)); |
204 | sc->sc_dying = 1; |
205 | goto error; |
206 | } |
207 | |
208 | /* get the config descriptor */ |
209 | cdesc = usbd_get_config_descriptor(sc->sc_udev); |
210 | |
211 | if (cdesc == NULL) { |
212 | aprint_error_dev(self, |
213 | "failed to get configuration descriptor\n" ); |
214 | sc->sc_dying = 1; |
215 | goto error; |
216 | } |
217 | |
218 | sc->sc_intr_number = -1; |
219 | sc->sc_intr_pipe = NULL; |
220 | |
221 | /* get the interfaces */ |
222 | err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET, |
223 | &sc->sc_iface[0]); |
224 | if (err) { |
225 | /* can not get main interface */ |
226 | sc->sc_dying = 1; |
227 | goto error; |
228 | } |
229 | |
230 | /* Find the endpoints */ |
231 | id = usbd_get_interface_descriptor(sc->sc_iface[0]); |
232 | sc->sc_iface_number[0] = id->bInterfaceNumber; |
233 | |
234 | /* initialize endpoints */ |
235 | ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1; |
236 | |
237 | for (i = 0; i < id->bNumEndpoints; i++) { |
238 | ed = usbd_interface2endpoint_descriptor(sc->sc_iface[0], i); |
239 | if (ed == NULL) { |
240 | aprint_error_dev(self, |
241 | "no endpoint descriptor for %d\n" , i); |
242 | break; |
243 | } |
244 | |
245 | if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && |
246 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { |
247 | sc->sc_intr_number = ed->bEndpointAddress; |
248 | sc->sc_isize = UGETW(ed->wMaxPacketSize); |
249 | } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && |
250 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
251 | ucaa.ucaa_bulkin = ed->bEndpointAddress; |
252 | ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize); |
253 | } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && |
254 | UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { |
255 | ucaa.ucaa_bulkout = ed->bEndpointAddress; |
256 | ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize); |
257 | } |
258 | } /* end of Endpoint loop */ |
259 | |
260 | if (sc->sc_intr_number == -1) { |
261 | aprint_error_dev(self, "Could not find interrupt in\n" ); |
262 | sc->sc_dying = 1; |
263 | goto error; |
264 | } |
265 | |
266 | if (ucaa.ucaa_bulkin == -1) { |
267 | aprint_error_dev(self, "Could not find data bulk in\n" ); |
268 | sc->sc_dying = 1; |
269 | goto error; |
270 | } |
271 | |
272 | if (ucaa.ucaa_bulkout == -1) { |
273 | aprint_error_dev(self, "Could not find data bulk out\n" ); |
274 | sc->sc_dying = 1; |
275 | goto error; |
276 | } |
277 | |
278 | ucaa.ucaa_portno = 0; |
279 | /* bulkin, bulkout set above */ |
280 | ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize; |
281 | ucaa.ucaa_opkthdrlen = 0; |
282 | ucaa.ucaa_device = dev; |
283 | ucaa.ucaa_iface = sc->sc_iface[0]; |
284 | ucaa.ucaa_methods = &ubsa_methods; |
285 | ucaa.ucaa_arg = sc; |
286 | ucaa.ucaa_info = NULL; |
287 | DPRINTF(("ubsa: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n" , |
288 | i, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout, sc->sc_intr_number)); |
289 | sc->sc_subdevs[0] = config_found_sm_loc(self, "ucombus" , NULL, &ucaa, |
290 | ucomprint, ucomsubmatch); |
291 | |
292 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); |
293 | |
294 | return; |
295 | |
296 | error: |
297 | return; |
298 | } |
299 | |
300 | |
301 | void |
302 | ubsa_childdet(device_t self, device_t child) |
303 | { |
304 | int i; |
305 | struct ubsa_softc *sc = device_private(self); |
306 | |
307 | for (i = 0; i < sc->sc_numif; i++) { |
308 | if (sc->sc_subdevs[i] == child) |
309 | break; |
310 | } |
311 | KASSERT(i < sc->sc_numif); |
312 | sc->sc_subdevs[i] = NULL; |
313 | } |
314 | |
315 | int |
316 | ubsa_detach(device_t self, int flags) |
317 | { |
318 | struct ubsa_softc *sc = device_private(self); |
319 | int i; |
320 | int rv = 0; |
321 | |
322 | |
323 | DPRINTF(("ubsa_detach: sc = %p\n" , sc)); |
324 | |
325 | if (sc->sc_intr_pipe != NULL) { |
326 | usbd_abort_pipe(sc->sc_intr_pipe); |
327 | usbd_close_pipe(sc->sc_intr_pipe); |
328 | kmem_free(sc->sc_intr_buf, sc->sc_isize); |
329 | sc->sc_intr_pipe = NULL; |
330 | } |
331 | |
332 | sc->sc_dying = 1; |
333 | for (i = 0; i < sc->sc_numif; i++) { |
334 | if (sc->sc_subdevs[i] != NULL) |
335 | rv |= config_detach(sc->sc_subdevs[i], flags); |
336 | } |
337 | |
338 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); |
339 | |
340 | return (rv); |
341 | } |
342 | |
343 | int |
344 | ubsa_activate(device_t self, enum devact act) |
345 | { |
346 | struct ubsa_softc *sc = device_private(self); |
347 | |
348 | switch (act) { |
349 | case DVACT_DEACTIVATE: |
350 | sc->sc_dying = 1; |
351 | return 0; |
352 | default: |
353 | return EOPNOTSUPP; |
354 | } |
355 | } |
356 | |