xref: /netbsd-src/sys/dev/usb/ubsa.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: ubsa.c,v 1.30 2012/02/24 06:48:24 mrg 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.30 2012/02/24 06:48:24 mrg Exp $");
58 
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.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 	ubsa_get_status,
99 	ubsa_set,
100 	ubsa_param,
101 	NULL,
102 	ubsa_open,
103 	ubsa_close,
104 	NULL,
105 	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->vendor, 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 	usbd_device_handle dev = 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 uca;
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->vendor == USB_VENDOR_OPTIONNV) {
188 		switch (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 	uca.bulkin = uca.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 			uca.bulkin = ed->bEndpointAddress;
252 			uca.ibufsize = UGETW(ed->wMaxPacketSize);
253 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
254 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
255 			uca.bulkout = ed->bEndpointAddress;
256 			uca.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 (uca.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 (uca.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 	uca.portno = 0;
279 	/* bulkin, bulkout set above */
280 	uca.ibufsizepad = uca.ibufsize;
281 	uca.opkthdrlen = 0;
282 	uca.device = dev;
283 	uca.iface = sc->sc_iface[0];
284 	uca.methods = &ubsa_methods;
285 	uca.arg = sc;
286 	uca.info = NULL;
287 	DPRINTF(("ubsa: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
288     		i, uca.bulkin, uca.bulkout, sc->sc_intr_number));
289 	sc->sc_subdevs[0] = config_found_sm_loc(self, "ucombus", NULL, &uca,
290 				    ucomprint, ucomsubmatch);
291 
292 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
293 			   sc->sc_dev);
294 
295 	return;
296 
297 error:
298 	return;
299 }
300 
301 
302 void
303 ubsa_childdet(device_t self, device_t child)
304 {
305 	int i;
306 	struct ubsa_softc *sc = device_private(self);
307 
308 	for (i = 0; i < sc->sc_numif; i++) {
309 		if (sc->sc_subdevs[i] == child)
310 			break;
311 	}
312 	KASSERT(i < sc->sc_numif);
313 		sc->sc_subdevs[i] = NULL;
314 }
315 
316 int
317 ubsa_detach(device_t self, int flags)
318 {
319 	struct ubsa_softc *sc = device_private(self);
320 	int i;
321 	int rv = 0;
322 
323 
324 	DPRINTF(("ubsa_detach: sc = %p\n", sc));
325 
326 	if (sc->sc_intr_pipe != NULL) {
327 		usbd_abort_pipe(sc->sc_intr_pipe);
328 		usbd_close_pipe(sc->sc_intr_pipe);
329 		free(sc->sc_intr_buf, M_USBDEV);
330 		sc->sc_intr_pipe = NULL;
331 	}
332 
333 	sc->sc_dying = 1;
334 	for (i = 0; i < sc->sc_numif; i++) {
335 		if (sc->sc_subdevs[i] != NULL)
336 			rv |= config_detach(sc->sc_subdevs[i], flags);
337 	}
338 
339 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
340 			   sc->sc_dev);
341 
342 	return (rv);
343 }
344 
345 int
346 ubsa_activate(device_t self, enum devact act)
347 {
348 	struct ubsa_softc *sc = device_private(self);
349 
350 	switch (act) {
351 	case DVACT_DEACTIVATE:
352 		sc->sc_dying = 1;
353 		return 0;
354 	default:
355 		return EOPNOTSUPP;
356 	}
357 }
358