1184610Salfred/* $FreeBSD$ */
2184610Salfred/*	$NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $	*/
3184610Salfred
4184610Salfred/*
5184610Salfred * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
6184610Salfred * All rights reserved.
7184610Salfred *
8184610Salfred * This code is derived from software contributed to The NetBSD Foundation
9184610Salfred * by Roland C. Dowdeswell <elric@netbsd.org>.
10184610Salfred *
11184610Salfred * Redistribution and use in source and binary forms, with or without
12184610Salfred * modification, are permitted provided that the following conditions
13184610Salfred * are met:
14184610Salfred * 1. Redistributions of source code must retain the above copyright
15184610Salfred *    notice, this list of conditions and the following disclaimer.
16184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
17184610Salfred *    notice, this list of conditions and the following disclaimer in the
18184610Salfred *    documentation and/or other materials provided with the distribution.
19184610Salfred *
20184610Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21184610Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22184610Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23184610Salfred * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30184610Salfred * POSSIBILITY OF SUCH DAMAGE.
31184610Salfred */
32184610Salfred
33184610Salfred/*
34184610Salfred * NOTE: all function names beginning like "ugensa_cfg_" can only
35184610Salfred * be called from within the config thread function !
36184610Salfred */
37184610Salfred
38194677Sthompsa#include <sys/stdint.h>
39194677Sthompsa#include <sys/stddef.h>
40194677Sthompsa#include <sys/param.h>
41194677Sthompsa#include <sys/queue.h>
42194677Sthompsa#include <sys/types.h>
43194677Sthompsa#include <sys/systm.h>
44194677Sthompsa#include <sys/kernel.h>
45194677Sthompsa#include <sys/bus.h>
46194677Sthompsa#include <sys/module.h>
47194677Sthompsa#include <sys/lock.h>
48194677Sthompsa#include <sys/mutex.h>
49194677Sthompsa#include <sys/condvar.h>
50194677Sthompsa#include <sys/sysctl.h>
51194677Sthompsa#include <sys/sx.h>
52194677Sthompsa#include <sys/unistd.h>
53194677Sthompsa#include <sys/callout.h>
54194677Sthompsa#include <sys/malloc.h>
55194677Sthompsa#include <sys/priv.h>
56194677Sthompsa
57194677Sthompsa#include <dev/usb/usb.h>
58194677Sthompsa#include <dev/usb/usbdi.h>
59188746Sthompsa#include "usbdevs.h"
60184610Salfred
61194228Sthompsa#define	USB_DEBUG_VAR usb_debug
62188942Sthompsa#include <dev/usb/usb_debug.h>
63188942Sthompsa#include <dev/usb/usb_process.h>
64184610Salfred
65188942Sthompsa#include <dev/usb/serial/usb_serial.h>
66184610Salfred
67184610Salfred#define	UGENSA_BUF_SIZE		2048	/* bytes */
68184610Salfred#define	UGENSA_CONFIG_INDEX	0
69184610Salfred#define	UGENSA_IFACE_INDEX	0
70184610Salfred#define	UGENSA_IFACE_MAX	8	/* exclusivly */
71184610Salfred
72187259Sthompsaenum {
73187259Sthompsa	UGENSA_BULK_DT_WR,
74187259Sthompsa	UGENSA_BULK_DT_RD,
75188413Sthompsa	UGENSA_N_TRANSFER,
76187259Sthompsa};
77187259Sthompsa
78184610Salfredstruct ugensa_sub_softc {
79194228Sthompsa	struct ucom_softc *sc_ucom_ptr;
80192984Sthompsa	struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
81184610Salfred};
82184610Salfred
83184610Salfredstruct ugensa_softc {
84192984Sthompsa	struct ucom_super_softc sc_super_ucom;
85192984Sthompsa	struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
86184610Salfred	struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
87184610Salfred
88184610Salfred	struct mtx sc_mtx;
89184610Salfred	uint8_t	sc_niface;
90184610Salfred};
91184610Salfred
92184610Salfred/* prototypes */
93184610Salfred
94184610Salfredstatic device_probe_t ugensa_probe;
95184610Salfredstatic device_attach_t ugensa_attach;
96184610Salfredstatic device_detach_t ugensa_detach;
97239299Shselaskystatic void ugensa_free_softc(struct ugensa_softc *);
98184610Salfred
99193045Sthompsastatic usb_callback_t ugensa_bulk_write_callback;
100193045Sthompsastatic usb_callback_t ugensa_bulk_read_callback;
101184610Salfred
102239180Shselaskystatic void	ugensa_free(struct ucom_softc *);
103192984Sthompsastatic void	ugensa_start_read(struct ucom_softc *);
104192984Sthompsastatic void	ugensa_stop_read(struct ucom_softc *);
105192984Sthompsastatic void	ugensa_start_write(struct ucom_softc *);
106192984Sthompsastatic void	ugensa_stop_write(struct ucom_softc *);
107197570Sthompsastatic void	ugensa_poll(struct ucom_softc *ucom);
108184610Salfred
109197570Sthompsastatic const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
110184610Salfred
111187259Sthompsa	[UGENSA_BULK_DT_WR] = {
112184610Salfred		.type = UE_BULK,
113184610Salfred		.endpoint = UE_ADDR_ANY,
114184610Salfred		.direction = UE_DIR_OUT,
115190734Sthompsa		.bufsize = UGENSA_BUF_SIZE,
116190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
117190734Sthompsa		.callback = &ugensa_bulk_write_callback,
118184610Salfred	},
119184610Salfred
120187259Sthompsa	[UGENSA_BULK_DT_RD] = {
121184610Salfred		.type = UE_BULK,
122184610Salfred		.endpoint = UE_ADDR_ANY,
123184610Salfred		.direction = UE_DIR_IN,
124190734Sthompsa		.bufsize = UGENSA_BUF_SIZE,
125190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
126190734Sthompsa		.callback = &ugensa_bulk_read_callback,
127184610Salfred	},
128184610Salfred};
129184610Salfred
130192984Sthompsastatic const struct ucom_callback ugensa_callback = {
131194228Sthompsa	.ucom_start_read = &ugensa_start_read,
132194228Sthompsa	.ucom_stop_read = &ugensa_stop_read,
133194228Sthompsa	.ucom_start_write = &ugensa_start_write,
134194228Sthompsa	.ucom_stop_write = &ugensa_stop_write,
135197570Sthompsa	.ucom_poll = &ugensa_poll,
136239180Shselasky	.ucom_free = &ugensa_free,
137184610Salfred};
138184610Salfred
139184610Salfredstatic device_method_t ugensa_methods[] = {
140184610Salfred	/* Device methods */
141184610Salfred	DEVMETHOD(device_probe, ugensa_probe),
142184610Salfred	DEVMETHOD(device_attach, ugensa_attach),
143184610Salfred	DEVMETHOD(device_detach, ugensa_detach),
144239180Shselasky	DEVMETHOD_END
145184610Salfred};
146184610Salfred
147184610Salfredstatic devclass_t ugensa_devclass;
148184610Salfred
149184610Salfredstatic driver_t ugensa_driver = {
150184610Salfred	.name = "ugensa",
151184610Salfred	.methods = ugensa_methods,
152184610Salfred	.size = sizeof(struct ugensa_softc),
153184610Salfred};
154184610Salfred
155189275SthompsaDRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
156188942SthompsaMODULE_DEPEND(ugensa, ucom, 1, 1, 1);
157188942SthompsaMODULE_DEPEND(ugensa, usb, 1, 1, 1);
158212122SthompsaMODULE_VERSION(ugensa, 1);
159184610Salfred
160223486Shselaskystatic const STRUCT_USB_HOST_ID ugensa_devs[] = {
161184610Salfred	{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
162184610Salfred	{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
163184610Salfred	{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
164184610Salfred	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
165184610Salfred	{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
166184610Salfred};
167184610Salfred
168184610Salfredstatic int
169184610Salfredugensa_probe(device_t dev)
170184610Salfred{
171192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
172184610Salfred
173192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST) {
174184610Salfred		return (ENXIO);
175184610Salfred	}
176184610Salfred	if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
177184610Salfred		return (ENXIO);
178184610Salfred	}
179184610Salfred	if (uaa->info.bIfaceIndex != 0) {
180184610Salfred		return (ENXIO);
181184610Salfred	}
182194228Sthompsa	return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
183184610Salfred}
184184610Salfred
185184610Salfredstatic int
186184610Salfredugensa_attach(device_t dev)
187184610Salfred{
188192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
189184610Salfred	struct ugensa_softc *sc = device_get_softc(dev);
190184610Salfred	struct ugensa_sub_softc *ssc;
191192984Sthompsa	struct usb_interface *iface;
192184610Salfred	int32_t error;
193184610Salfred	uint8_t iface_index;
194184610Salfred	int x, cnt;
195184610Salfred
196194228Sthompsa	device_set_usb_desc(dev);
197184610Salfred	mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
198239180Shselasky	ucom_ref(&sc->sc_super_ucom);
199184610Salfred
200184610Salfred	/* Figure out how many interfaces this device has got */
201184610Salfred	for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
202194228Sthompsa		if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
203194228Sthompsa		    (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
204184610Salfred			/* we have reached the end */
205184610Salfred			break;
206184610Salfred		}
207184610Salfred	}
208184610Salfred
209184610Salfred	if (cnt == 0) {
210199816Sthompsa		device_printf(dev, "No interfaces\n");
211184610Salfred		goto detach;
212184610Salfred	}
213184610Salfred	for (x = 0; x < cnt; x++) {
214194228Sthompsa		iface = usbd_get_iface(uaa->device, x);
215184610Salfred		if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
216184610Salfred			/* Not a serial port, most likely a SD reader */
217184610Salfred			continue;
218184610Salfred
219184610Salfred		ssc = sc->sc_sub + sc->sc_niface;
220194228Sthompsa		ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
221184610Salfred
222184610Salfred		iface_index = (UGENSA_IFACE_INDEX + x);
223194228Sthompsa		error = usbd_transfer_setup(uaa->device,
224184610Salfred		    &iface_index, ssc->sc_xfer, ugensa_xfer_config,
225184610Salfred		    UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
226184610Salfred
227184610Salfred		if (error) {
228184610Salfred			device_printf(dev, "allocating USB "
229199816Sthompsa			    "transfers failed\n");
230184610Salfred			goto detach;
231184610Salfred		}
232184610Salfred		/* clear stall at first run */
233189265Sthompsa		mtx_lock(&sc->sc_mtx);
234194677Sthompsa		usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
235194677Sthompsa		usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
236189265Sthompsa		mtx_unlock(&sc->sc_mtx);
237184610Salfred
238184610Salfred		/* initialize port number */
239194228Sthompsa		ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
240184610Salfred		sc->sc_niface++;
241184610Salfred		if (x != uaa->info.bIfaceIndex)
242194228Sthompsa			usbd_set_parent_iface(uaa->device, x,
243184610Salfred			    uaa->info.bIfaceIndex);
244184610Salfred	}
245184610Salfred	device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
246184610Salfred
247194228Sthompsa	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
248184610Salfred	    &ugensa_callback, &sc->sc_mtx);
249184610Salfred	if (error) {
250184610Salfred		DPRINTF("attach failed\n");
251184610Salfred		goto detach;
252184610Salfred	}
253214843Sn_hibma	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
254214843Sn_hibma
255184610Salfred	return (0);			/* success */
256184610Salfred
257184610Salfreddetach:
258184610Salfred	ugensa_detach(dev);
259184610Salfred	return (ENXIO);			/* failure */
260184610Salfred}
261184610Salfred
262184610Salfredstatic int
263184610Salfredugensa_detach(device_t dev)
264184610Salfred{
265184610Salfred	struct ugensa_softc *sc = device_get_softc(dev);
266184610Salfred	uint8_t x;
267184610Salfred
268214761Sn_hibma	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
269184610Salfred
270184610Salfred	for (x = 0; x < sc->sc_niface; x++) {
271194228Sthompsa		usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
272184610Salfred	}
273184610Salfred
274239299Shselasky	device_claim_softc(dev);
275239299Shselasky
276239299Shselasky	ugensa_free_softc(sc);
277239299Shselasky
278184610Salfred	return (0);
279184610Salfred}
280184610Salfred
281239180ShselaskyUCOM_UNLOAD_DRAIN(ugensa);
282239180Shselasky
283184610Salfredstatic void
284239299Shselaskyugensa_free_softc(struct ugensa_softc *sc)
285239180Shselasky{
286239180Shselasky	if (ucom_unref(&sc->sc_super_ucom)) {
287239299Shselasky		mtx_destroy(&sc->sc_mtx);
288239299Shselasky		device_free_softc(sc);
289239180Shselasky	}
290239180Shselasky}
291239180Shselasky
292239180Shselaskystatic void
293239180Shselaskyugensa_free(struct ucom_softc *ucom)
294239180Shselasky{
295239299Shselasky	ugensa_free_softc(ucom->sc_parent);
296239180Shselasky}
297239180Shselasky
298239180Shselaskystatic void
299194677Sthompsaugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
300184610Salfred{
301194677Sthompsa	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
302194677Sthompsa	struct usb_page_cache *pc;
303184610Salfred	uint32_t actlen;
304184610Salfred
305184610Salfred	switch (USB_GET_STATE(xfer)) {
306184610Salfred	case USB_ST_SETUP:
307184610Salfred	case USB_ST_TRANSFERRED:
308188413Sthompsatr_setup:
309194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
310194677Sthompsa		if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
311184610Salfred		    UGENSA_BUF_SIZE, &actlen)) {
312194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, actlen);
313194228Sthompsa			usbd_transfer_submit(xfer);
314184610Salfred		}
315184610Salfred		return;
316184610Salfred
317184610Salfred	default:			/* Error */
318194677Sthompsa		if (error != USB_ERR_CANCELLED) {
319188413Sthompsa			/* try to clear stall first */
320194677Sthompsa			usbd_xfer_set_stall(xfer);
321188413Sthompsa			goto tr_setup;
322184610Salfred		}
323184610Salfred		return;
324184610Salfred	}
325184610Salfred}
326184610Salfred
327184610Salfredstatic void
328194677Sthompsaugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
329184610Salfred{
330194677Sthompsa	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
331194677Sthompsa	struct usb_page_cache *pc;
332194677Sthompsa	int actlen;
333184610Salfred
334194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
335194677Sthompsa
336184610Salfred	switch (USB_GET_STATE(xfer)) {
337184610Salfred	case USB_ST_TRANSFERRED:
338194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
339194677Sthompsa		ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
340184610Salfred
341184610Salfred	case USB_ST_SETUP:
342188413Sthompsatr_setup:
343194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
344194228Sthompsa		usbd_transfer_submit(xfer);
345184610Salfred		return;
346184610Salfred
347184610Salfred	default:			/* Error */
348194677Sthompsa		if (error != USB_ERR_CANCELLED) {
349188413Sthompsa			/* try to clear stall first */
350194677Sthompsa			usbd_xfer_set_stall(xfer);
351188413Sthompsa			goto tr_setup;
352184610Salfred		}
353184610Salfred		return;
354184610Salfred	}
355184610Salfred}
356184610Salfred
357184610Salfredstatic void
358192984Sthompsaugensa_start_read(struct ucom_softc *ucom)
359184610Salfred{
360184610Salfred	struct ugensa_softc *sc = ucom->sc_parent;
361184610Salfred	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
362184610Salfred
363194228Sthompsa	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
364184610Salfred}
365184610Salfred
366184610Salfredstatic void
367192984Sthompsaugensa_stop_read(struct ucom_softc *ucom)
368184610Salfred{
369184610Salfred	struct ugensa_softc *sc = ucom->sc_parent;
370184610Salfred	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
371184610Salfred
372194228Sthompsa	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
373184610Salfred}
374184610Salfred
375184610Salfredstatic void
376192984Sthompsaugensa_start_write(struct ucom_softc *ucom)
377184610Salfred{
378184610Salfred	struct ugensa_softc *sc = ucom->sc_parent;
379184610Salfred	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
380184610Salfred
381194228Sthompsa	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
382184610Salfred}
383184610Salfred
384184610Salfredstatic void
385192984Sthompsaugensa_stop_write(struct ucom_softc *ucom)
386184610Salfred{
387184610Salfred	struct ugensa_softc *sc = ucom->sc_parent;
388184610Salfred	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
389184610Salfred
390194228Sthompsa	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
391184610Salfred}
392197570Sthompsa
393197570Sthompsastatic void
394197570Sthompsaugensa_poll(struct ucom_softc *ucom)
395197570Sthompsa{
396197570Sthompsa	struct ugensa_softc *sc = ucom->sc_parent;
397197570Sthompsa	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
398197570Sthompsa
399197570Sthompsa	usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
400197570Sthompsa}
401