xref: /netbsd-src/sys/dev/usb/ubsa.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: ubsa.c,v 1.20 2008/01/21 11:36:47 ichiro 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  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *        This product includes software developed by the NetBSD
45  *        Foundation, Inc. and its contributors.
46  * 4. Neither the name of The NetBSD Foundation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: ubsa.c,v 1.20 2008/01/21 11:36:47 ichiro Exp $");
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #ifdef __FreeBSD__
71 #include <sys/bus.h>
72 #endif
73 #include <sys/ioccom.h>
74 #include <sys/fcntl.h>
75 #include <sys/conf.h>
76 #include <sys/tty.h>
77 #include <sys/file.h>
78 #if __FreeBSD_version >= 500014
79 #include <sys/selinfo.h>
80 #else
81 #include <sys/select.h>
82 #endif
83 #include <sys/proc.h>
84 #include <sys/device.h>
85 #include <sys/poll.h>
86 #include <sys/sysctl.h>
87 
88 #include <dev/usb/usb.h>
89 #include <dev/usb/usbcdc.h>
90 
91 #include <dev/usb/usbdi.h>
92 #include <dev/usb/usbdi_util.h>
93 #include <dev/usb/usbdevs.h>
94 #include <dev/usb/usb_quirks.h>
95 
96 #include <dev/usb/ucomvar.h>
97 #include <dev/usb/ubsavar.h>
98 
99 #ifdef UBSA_DEBUG
100 Static int	ubsadebug = 0;
101 #ifdef __FreeBSD__
102 SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
103 SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
104 	   &ubsadebug, 0, "ubsa debug level");
105 #endif
106 
107 #define	DPRINTFN(n, x)	do { \
108 				if (ubsadebug > (n)) \
109 					logprintf x; \
110 			} while (0)
111 #else
112 #define	DPRINTFN(n, x)
113 #endif
114 #define	DPRINTF(x) DPRINTFN(0, x)
115 
116 struct	ucom_methods ubsa_methods = {
117 	ubsa_get_status,
118 	ubsa_set,
119 	ubsa_param,
120 	NULL,
121 	ubsa_open,
122 	ubsa_close,
123 	NULL,
124 	NULL
125 };
126 
127 Static const struct usb_devno ubsa_devs[] = {
128 	/* BELKIN F5U103 */
129 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103 },
130 	/* BELKIN F5U120 */
131 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120 },
132 	/* GoHubs GO-COM232 */
133 	{ USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM },
134 	/* GoHubs GO-COM232 */
135 	{ USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232 },
136 	/* Peracom */
137 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1 },
138 	/* Option N.V. */
139 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MC3G },
140 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS2 },
141 	{ USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_QUADUMTS },
142 	/* AnyDATA ADU-E100H */
143 	{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100H },
144 };
145 #define ubsa_lookup(v, p) usb_lookup(ubsa_devs, v, p)
146 
147 USB_DECLARE_DRIVER(ubsa);
148 
149 USB_MATCH(ubsa)
150 {
151 	USB_MATCH_START(ubsa, uaa);
152 
153 	return (ubsa_lookup(uaa->vendor, uaa->product) != NULL ?
154 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
155 }
156 
157 USB_ATTACH(ubsa)
158 {
159 	USB_ATTACH_START(ubsa, sc, uaa);
160 	usbd_device_handle dev = uaa->device;
161 	usb_config_descriptor_t *cdesc;
162 	usb_interface_descriptor_t *id;
163 	usb_endpoint_descriptor_t *ed;
164 	char *devinfop;
165 	const char *devname = USBDEVNAME(sc->sc_dev);
166 	usbd_status err;
167 	struct ucom_attach_args uca;
168 	int i;
169 
170 	devinfop = usbd_devinfo_alloc(dev, 0);
171 	USB_ATTACH_SETUP;
172 	printf("%s: %s\n", devname, 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->vendor == USB_VENDOR_OPTIONNV) {
192 		switch (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 		printf("%s: failed to set configuration: %s\n",
206 		    devname, usbd_errstr(err));
207 		sc->sc_dying = 1;
208 		goto error;
209 	}
210 
211 	/* get the config descriptor */
212 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
213 
214 	if (cdesc == NULL) {
215 		printf("%s: failed to get configuration descriptor\n",
216 		    devname);
217 		sc->sc_dying = 1;
218 		goto error;
219 	}
220 
221 	sc->sc_intr_number = -1;
222 	sc->sc_intr_pipe = NULL;
223 
224 	/* get the interfaces */
225 	err = usbd_device2interface_handle(dev, UBSA_IFACE_INDEX_OFFSET,
226 			 &sc->sc_iface[0]);
227 	if (err) {
228 		/* can not get main interface */
229 		sc->sc_dying = 1;
230 		goto error;
231 	}
232 
233 	/* Find the endpoints */
234 	id = usbd_get_interface_descriptor(sc->sc_iface[0]);
235 	sc->sc_iface_number[0] = id->bInterfaceNumber;
236 
237 	/* initialize endpoints */
238 	uca.bulkin = uca.bulkout = -1;
239 
240 	for (i = 0; i < id->bNumEndpoints; i++) {
241 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface[0], i);
242 		if (ed == NULL) {
243 			printf("%s: no endpoint descriptor for %d\n",
244 		    		USBDEVNAME(sc->sc_dev), i);
245 			break;
246 		}
247 
248 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
249 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
250 			sc->sc_intr_number = ed->bEndpointAddress;
251 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
252 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
253 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
254 			uca.bulkin = ed->bEndpointAddress;
255 			uca.ibufsize = UGETW(ed->wMaxPacketSize);
256 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
257 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
258 			uca.bulkout = ed->bEndpointAddress;
259 			uca.obufsize = UGETW(ed->wMaxPacketSize);
260 		}
261 	} /* end of Endpoint loop */
262 
263 	if (sc->sc_intr_number == -1) {
264 		printf("%s: Could not find interrupt in\n", devname);
265 		sc->sc_dying = 1;
266 		goto error;
267 	}
268 
269 	if (uca.bulkin == -1) {
270 		printf("%s: Could not find data bulk in\n", devname);
271 		sc->sc_dying = 1;
272 		goto error;
273 	}
274 
275 	if (uca.bulkout == -1) {
276 		printf("%s: Could not find data bulk out\n", devname);
277 		sc->sc_dying = 1;
278 		goto error;
279 	}
280 
281 	uca.portno = 0;
282 	/* bulkin, bulkout set above */
283 	uca.ibufsizepad = uca.ibufsize;
284 	uca.opkthdrlen = 0;
285 	uca.device = dev;
286 	uca.iface = sc->sc_iface[0];
287 	uca.methods = &ubsa_methods;
288 	uca.arg = sc;
289 	uca.info = NULL;
290 	DPRINTF(("ubsa: int#=%d, in = 0x%x, out = 0x%x, intr = 0x%x\n",
291     		i, uca.bulkin, uca.bulkout, sc->sc_intr_number));
292 	sc->sc_subdevs[0] = config_found_sm_loc(self, "ucombus", NULL, &uca,
293 				    ucomprint, ucomsubmatch);
294 
295 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
296 			   USBDEV(sc->sc_dev));
297 
298 	USB_ATTACH_SUCCESS_RETURN;
299 
300 error:
301 	USB_ATTACH_ERROR_RETURN;
302 }
303 
304 USB_DETACH(ubsa)
305 {
306 	USB_DETACH_START(ubsa, sc);
307 	int i;
308 	int rv = 0;
309 
310 
311 	DPRINTF(("ubsa_detach: sc = %p\n", sc));
312 
313 	if (sc->sc_intr_pipe != NULL) {
314 		usbd_abort_pipe(sc->sc_intr_pipe);
315 		usbd_close_pipe(sc->sc_intr_pipe);
316 		free(sc->sc_intr_buf, M_USBDEV);
317 		sc->sc_intr_pipe = NULL;
318 	}
319 
320 	sc->sc_dying = 1;
321 	for (i = 0; i < sc->sc_numif; i++) {
322 		if (sc->sc_subdevs[i] != NULL) {
323 			rv |= config_detach(sc->sc_subdevs[i], flags);
324 			sc->sc_subdevs[i] = NULL;
325 		}
326 	}
327 
328 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
329 			   USBDEV(sc->sc_dev));
330 
331 	return (rv);
332 }
333 
334 int
335 ubsa_activate(device_ptr_t self, enum devact act)
336 {
337 	struct ubsa_softc *sc = (struct ubsa_softc *)self;
338 	int rv = 0;
339 	int i;
340 
341 	switch (act) {
342 	case DVACT_ACTIVATE:
343 		return (EOPNOTSUPP);
344 
345 	case DVACT_DEACTIVATE:
346 		for (i = 0; i < sc->sc_numif; i++) {
347 			if (sc->sc_subdevs[i] != NULL)
348 				rv |= config_deactivate(sc->sc_subdevs[i]);
349 		}
350 		sc->sc_dying = 1;
351 		break;
352 	}
353 	return (rv);
354 }
355 
356