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