ugensa.c revision 192499
1/* $FreeBSD: head/sys/dev/usb/serial/ugensa.c 192499 2009-05-21 00:04:17Z thompsa $ */
2/*	$NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $	*/
3
4/*
5 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Roland C. Dowdeswell <elric@netbsd.org>.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *        This product includes software developed by the NetBSD
22 *        Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * NOTE: all function names beginning like "ugensa_cfg_" can only
42 * be called from within the config thread function !
43 */
44
45#include "usbdevs.h"
46#include <dev/usb/usb.h>
47#include <dev/usb/usb_mfunc.h>
48#include <dev/usb/usb_error.h>
49#include <dev/usb/usb_cdc.h>
50
51#define	USB_DEBUG_VAR usb2_debug
52
53#include <dev/usb/usb_core.h>
54#include <dev/usb/usb_debug.h>
55#include <dev/usb/usb_process.h>
56#include <dev/usb/usb_request.h>
57#include <dev/usb/usb_lookup.h>
58#include <dev/usb/usb_util.h>
59#include <dev/usb/usb_device.h>
60
61#include <dev/usb/serial/usb_serial.h>
62
63#define	UGENSA_BUF_SIZE		2048	/* bytes */
64#define	UGENSA_CONFIG_INDEX	0
65#define	UGENSA_IFACE_INDEX	0
66#define	UGENSA_IFACE_MAX	8	/* exclusivly */
67
68enum {
69	UGENSA_BULK_DT_WR,
70	UGENSA_BULK_DT_RD,
71	UGENSA_N_TRANSFER,
72};
73
74struct ugensa_sub_softc {
75	struct usb2_com_softc *sc_usb2_com_ptr;
76	struct usb2_xfer *sc_xfer[UGENSA_N_TRANSFER];
77};
78
79struct ugensa_softc {
80	struct usb2_com_super_softc sc_super_ucom;
81	struct usb2_com_softc sc_ucom[UGENSA_IFACE_MAX];
82	struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
83
84	struct mtx sc_mtx;
85	uint8_t	sc_niface;
86};
87
88/* prototypes */
89
90static device_probe_t ugensa_probe;
91static device_attach_t ugensa_attach;
92static device_detach_t ugensa_detach;
93
94static usb2_callback_t ugensa_bulk_write_callback;
95static usb2_callback_t ugensa_bulk_read_callback;
96
97static void	ugensa_start_read(struct usb2_com_softc *);
98static void	ugensa_stop_read(struct usb2_com_softc *);
99static void	ugensa_start_write(struct usb2_com_softc *);
100static void	ugensa_stop_write(struct usb2_com_softc *);
101
102static const struct usb2_config
103	ugensa_xfer_config[UGENSA_N_TRANSFER] = {
104
105	[UGENSA_BULK_DT_WR] = {
106		.type = UE_BULK,
107		.endpoint = UE_ADDR_ANY,
108		.direction = UE_DIR_OUT,
109		.bufsize = UGENSA_BUF_SIZE,
110		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
111		.callback = &ugensa_bulk_write_callback,
112	},
113
114	[UGENSA_BULK_DT_RD] = {
115		.type = UE_BULK,
116		.endpoint = UE_ADDR_ANY,
117		.direction = UE_DIR_IN,
118		.bufsize = UGENSA_BUF_SIZE,
119		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
120		.callback = &ugensa_bulk_read_callback,
121	},
122};
123
124static const struct usb2_com_callback ugensa_callback = {
125	.usb2_com_start_read = &ugensa_start_read,
126	.usb2_com_stop_read = &ugensa_stop_read,
127	.usb2_com_start_write = &ugensa_start_write,
128	.usb2_com_stop_write = &ugensa_stop_write,
129};
130
131static device_method_t ugensa_methods[] = {
132	/* Device methods */
133	DEVMETHOD(device_probe, ugensa_probe),
134	DEVMETHOD(device_attach, ugensa_attach),
135	DEVMETHOD(device_detach, ugensa_detach),
136	{0, 0}
137};
138
139static devclass_t ugensa_devclass;
140
141static driver_t ugensa_driver = {
142	.name = "ugensa",
143	.methods = ugensa_methods,
144	.size = sizeof(struct ugensa_softc),
145};
146
147DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
148MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
149MODULE_DEPEND(ugensa, usb, 1, 1, 1);
150
151static const struct usb2_device_id ugensa_devs[] = {
152	{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
153	{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
154	{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
155	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
156	{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
157};
158
159static int
160ugensa_probe(device_t dev)
161{
162	struct usb2_attach_arg *uaa = device_get_ivars(dev);
163
164	if (uaa->usb_mode != USB_MODE_HOST) {
165		return (ENXIO);
166	}
167	if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
168		return (ENXIO);
169	}
170	if (uaa->info.bIfaceIndex != 0) {
171		return (ENXIO);
172	}
173	return (usb2_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
174}
175
176static int
177ugensa_attach(device_t dev)
178{
179	struct usb2_attach_arg *uaa = device_get_ivars(dev);
180	struct ugensa_softc *sc = device_get_softc(dev);
181	struct ugensa_sub_softc *ssc;
182	struct usb2_interface *iface;
183	int32_t error;
184	uint8_t iface_index;
185	int x, cnt;
186
187	device_set_usb2_desc(dev);
188	mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
189
190	/* Figure out how many interfaces this device has got */
191	for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
192		if ((usb2_get_pipe(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
193		    (usb2_get_pipe(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
194			/* we have reached the end */
195			break;
196		}
197	}
198
199	if (cnt == 0) {
200		device_printf(dev, "No interfaces!\n");
201		goto detach;
202	}
203	for (x = 0; x < cnt; x++) {
204		iface = usb2_get_iface(uaa->device, x);
205		if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
206			/* Not a serial port, most likely a SD reader */
207			continue;
208
209		ssc = sc->sc_sub + sc->sc_niface;
210		ssc->sc_usb2_com_ptr = sc->sc_ucom + sc->sc_niface;
211
212		iface_index = (UGENSA_IFACE_INDEX + x);
213		error = usb2_transfer_setup(uaa->device,
214		    &iface_index, ssc->sc_xfer, ugensa_xfer_config,
215		    UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
216
217		if (error) {
218			device_printf(dev, "allocating USB "
219			    "transfers failed!\n");
220			goto detach;
221		}
222		/* clear stall at first run */
223		mtx_lock(&sc->sc_mtx);
224		usb2_transfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
225		usb2_transfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
226		mtx_unlock(&sc->sc_mtx);
227
228		/* initialize port number */
229		ssc->sc_usb2_com_ptr->sc_portno = sc->sc_niface;
230		sc->sc_niface++;
231		if (x != uaa->info.bIfaceIndex)
232			usb2_set_parent_iface(uaa->device, x,
233			    uaa->info.bIfaceIndex);
234	}
235	device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
236
237	error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
238	    &ugensa_callback, &sc->sc_mtx);
239	if (error) {
240		DPRINTF("attach failed\n");
241		goto detach;
242	}
243	return (0);			/* success */
244
245detach:
246	ugensa_detach(dev);
247	return (ENXIO);			/* failure */
248}
249
250static int
251ugensa_detach(device_t dev)
252{
253	struct ugensa_softc *sc = device_get_softc(dev);
254	uint8_t x;
255
256	usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface);
257
258	for (x = 0; x < sc->sc_niface; x++) {
259		usb2_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
260	}
261	mtx_destroy(&sc->sc_mtx);
262
263	return (0);
264}
265
266static void
267ugensa_bulk_write_callback(struct usb2_xfer *xfer)
268{
269	struct ugensa_sub_softc *ssc = xfer->priv_sc;
270	uint32_t actlen;
271
272	switch (USB_GET_STATE(xfer)) {
273	case USB_ST_SETUP:
274	case USB_ST_TRANSFERRED:
275tr_setup:
276		if (usb2_com_get_data(ssc->sc_usb2_com_ptr, xfer->frbuffers, 0,
277		    UGENSA_BUF_SIZE, &actlen)) {
278			xfer->frlengths[0] = actlen;
279			usb2_start_hardware(xfer);
280		}
281		return;
282
283	default:			/* Error */
284		if (xfer->error != USB_ERR_CANCELLED) {
285			/* try to clear stall first */
286			xfer->flags.stall_pipe = 1;
287			goto tr_setup;
288		}
289		return;
290	}
291}
292
293static void
294ugensa_bulk_read_callback(struct usb2_xfer *xfer)
295{
296	struct ugensa_sub_softc *ssc = xfer->priv_sc;
297
298	switch (USB_GET_STATE(xfer)) {
299	case USB_ST_TRANSFERRED:
300		usb2_com_put_data(ssc->sc_usb2_com_ptr, xfer->frbuffers, 0,
301		    xfer->actlen);
302
303	case USB_ST_SETUP:
304tr_setup:
305		xfer->frlengths[0] = xfer->max_data_length;
306		usb2_start_hardware(xfer);
307		return;
308
309	default:			/* Error */
310		if (xfer->error != USB_ERR_CANCELLED) {
311			/* try to clear stall first */
312			xfer->flags.stall_pipe = 1;
313			goto tr_setup;
314		}
315		return;
316	}
317}
318
319static void
320ugensa_start_read(struct usb2_com_softc *ucom)
321{
322	struct ugensa_softc *sc = ucom->sc_parent;
323	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
324
325	usb2_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
326}
327
328static void
329ugensa_stop_read(struct usb2_com_softc *ucom)
330{
331	struct ugensa_softc *sc = ucom->sc_parent;
332	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
333
334	usb2_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
335}
336
337static void
338ugensa_start_write(struct usb2_com_softc *ucom)
339{
340	struct ugensa_softc *sc = ucom->sc_parent;
341	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
342
343	usb2_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
344}
345
346static void
347ugensa_stop_write(struct usb2_com_softc *ucom)
348{
349	struct ugensa_softc *sc = ucom->sc_parent;
350	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
351
352	usb2_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
353}
354