2 /* $NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $ */
5 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Roland C. Dowdeswell <elric@netbsd.org>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
41 * NOTE: all function names beginning like "ugensa_cfg_" can only
42 * be called from within the config thread function !
45 #include <sys/stdint.h>
46 #include <sys/stddef.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/linker_set.h>
54 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
69 #define USB_DEBUG_VAR usb_debug
70 #include <dev/usb/usb_debug.h>
71 #include <dev/usb/usb_process.h>
73 #include <dev/usb/serial/usb_serial.h>
75 #define UGENSA_BUF_SIZE 2048 /* bytes */
76 #define UGENSA_CONFIG_INDEX 0
77 #define UGENSA_IFACE_INDEX 0
78 #define UGENSA_IFACE_MAX 8 /* exclusivly */
86 struct ugensa_sub_softc {
87 struct ucom_softc *sc_ucom_ptr;
88 struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
92 struct ucom_super_softc sc_super_ucom;
93 struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
94 struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
102 static device_probe_t ugensa_probe;
103 static device_attach_t ugensa_attach;
104 static device_detach_t ugensa_detach;
106 static usb_callback_t ugensa_bulk_write_callback;
107 static usb_callback_t ugensa_bulk_read_callback;
109 static void ugensa_start_read(struct ucom_softc *);
110 static void ugensa_stop_read(struct ucom_softc *);
111 static void ugensa_start_write(struct ucom_softc *);
112 static void ugensa_stop_write(struct ucom_softc *);
113 static void ugensa_poll(struct ucom_softc *ucom);
115 static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
117 [UGENSA_BULK_DT_WR] = {
119 .endpoint = UE_ADDR_ANY,
120 .direction = UE_DIR_OUT,
121 .bufsize = UGENSA_BUF_SIZE,
122 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
123 .callback = &ugensa_bulk_write_callback,
126 [UGENSA_BULK_DT_RD] = {
128 .endpoint = UE_ADDR_ANY,
129 .direction = UE_DIR_IN,
130 .bufsize = UGENSA_BUF_SIZE,
131 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
132 .callback = &ugensa_bulk_read_callback,
136 static const struct ucom_callback ugensa_callback = {
137 .ucom_start_read = &ugensa_start_read,
138 .ucom_stop_read = &ugensa_stop_read,
139 .ucom_start_write = &ugensa_start_write,
140 .ucom_stop_write = &ugensa_stop_write,
141 .ucom_poll = &ugensa_poll,
144 static device_method_t ugensa_methods[] = {
146 DEVMETHOD(device_probe, ugensa_probe),
147 DEVMETHOD(device_attach, ugensa_attach),
148 DEVMETHOD(device_detach, ugensa_detach),
152 static devclass_t ugensa_devclass;
154 static driver_t ugensa_driver = {
156 .methods = ugensa_methods,
157 .size = sizeof(struct ugensa_softc),
160 DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
161 MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
162 MODULE_DEPEND(ugensa, usb, 1, 1, 1);
164 static const struct usb_device_id ugensa_devs[] = {
165 {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
166 {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
167 {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
168 {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
169 {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
173 ugensa_probe(device_t dev)
175 struct usb_attach_arg *uaa = device_get_ivars(dev);
177 if (uaa->usb_mode != USB_MODE_HOST) {
180 if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
183 if (uaa->info.bIfaceIndex != 0) {
186 return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
190 ugensa_attach(device_t dev)
192 struct usb_attach_arg *uaa = device_get_ivars(dev);
193 struct ugensa_softc *sc = device_get_softc(dev);
194 struct ugensa_sub_softc *ssc;
195 struct usb_interface *iface;
200 device_set_usb_desc(dev);
201 mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
203 /* Figure out how many interfaces this device has got */
204 for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
205 if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
206 (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
207 /* we have reached the end */
213 device_printf(dev, "No interfaces\n");
216 for (x = 0; x < cnt; x++) {
217 iface = usbd_get_iface(uaa->device, x);
218 if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
219 /* Not a serial port, most likely a SD reader */
222 ssc = sc->sc_sub + sc->sc_niface;
223 ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
225 iface_index = (UGENSA_IFACE_INDEX + x);
226 error = usbd_transfer_setup(uaa->device,
227 &iface_index, ssc->sc_xfer, ugensa_xfer_config,
228 UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
231 device_printf(dev, "allocating USB "
232 "transfers failed\n");
235 /* clear stall at first run */
236 mtx_lock(&sc->sc_mtx);
237 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
238 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
239 mtx_unlock(&sc->sc_mtx);
241 /* initialize port number */
242 ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
244 if (x != uaa->info.bIfaceIndex)
245 usbd_set_parent_iface(uaa->device, x,
246 uaa->info.bIfaceIndex);
248 device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
250 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
251 &ugensa_callback, &sc->sc_mtx);
253 DPRINTF("attach failed\n");
256 return (0); /* success */
260 return (ENXIO); /* failure */
264 ugensa_detach(device_t dev)
266 struct ugensa_softc *sc = device_get_softc(dev);
269 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface);
271 for (x = 0; x < sc->sc_niface; x++) {
272 usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
274 mtx_destroy(&sc->sc_mtx);
280 ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
282 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
283 struct usb_page_cache *pc;
286 switch (USB_GET_STATE(xfer)) {
288 case USB_ST_TRANSFERRED:
290 pc = usbd_xfer_get_frame(xfer, 0);
291 if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
292 UGENSA_BUF_SIZE, &actlen)) {
293 usbd_xfer_set_frame_len(xfer, 0, actlen);
294 usbd_transfer_submit(xfer);
299 if (error != USB_ERR_CANCELLED) {
300 /* try to clear stall first */
301 usbd_xfer_set_stall(xfer);
309 ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
311 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
312 struct usb_page_cache *pc;
315 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
317 switch (USB_GET_STATE(xfer)) {
318 case USB_ST_TRANSFERRED:
319 pc = usbd_xfer_get_frame(xfer, 0);
320 ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
324 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
325 usbd_transfer_submit(xfer);
329 if (error != USB_ERR_CANCELLED) {
330 /* try to clear stall first */
331 usbd_xfer_set_stall(xfer);
339 ugensa_start_read(struct ucom_softc *ucom)
341 struct ugensa_softc *sc = ucom->sc_parent;
342 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
344 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
348 ugensa_stop_read(struct ucom_softc *ucom)
350 struct ugensa_softc *sc = ucom->sc_parent;
351 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
353 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
357 ugensa_start_write(struct ucom_softc *ucom)
359 struct ugensa_softc *sc = ucom->sc_parent;
360 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
362 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
366 ugensa_stop_write(struct ucom_softc *ucom)
368 struct ugensa_softc *sc = ucom->sc_parent;
369 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
371 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
375 ugensa_poll(struct ucom_softc *ucom)
377 struct ugensa_softc *sc = ucom->sc_parent;
378 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
380 usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);