]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/usb/serial/ugensa.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / dev / usb / serial / ugensa.c
1 /* $FreeBSD$ */
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 <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>
52 #include <sys/bus.h>
53 #include <sys/linker_set.h>
54 #include <sys/module.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/sx.h>
60 #include <sys/unistd.h>
61 #include <sys/callout.h>
62 #include <sys/malloc.h>
63 #include <sys/priv.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include "usbdevs.h"
68
69 #define USB_DEBUG_VAR usb_debug
70 #include <dev/usb/usb_debug.h>
71 #include <dev/usb/usb_process.h>
72
73 #include <dev/usb/serial/usb_serial.h>
74
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 */
79
80 enum {
81         UGENSA_BULK_DT_WR,
82         UGENSA_BULK_DT_RD,
83         UGENSA_N_TRANSFER,
84 };
85
86 struct ugensa_sub_softc {
87         struct ucom_softc *sc_ucom_ptr;
88         struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
89 };
90
91 struct ugensa_softc {
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];
95
96         struct mtx sc_mtx;
97         uint8_t sc_niface;
98 };
99
100 /* prototypes */
101
102 static device_probe_t ugensa_probe;
103 static device_attach_t ugensa_attach;
104 static device_detach_t ugensa_detach;
105
106 static usb_callback_t ugensa_bulk_write_callback;
107 static usb_callback_t ugensa_bulk_read_callback;
108
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
114 static const struct usb_config
115         ugensa_xfer_config[UGENSA_N_TRANSFER] = {
116
117         [UGENSA_BULK_DT_WR] = {
118                 .type = UE_BULK,
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,
124         },
125
126         [UGENSA_BULK_DT_RD] = {
127                 .type = UE_BULK,
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,
133         },
134 };
135
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 };
142
143 static device_method_t ugensa_methods[] = {
144         /* Device methods */
145         DEVMETHOD(device_probe, ugensa_probe),
146         DEVMETHOD(device_attach, ugensa_attach),
147         DEVMETHOD(device_detach, ugensa_detach),
148         {0, 0}
149 };
150
151 static devclass_t ugensa_devclass;
152
153 static driver_t ugensa_driver = {
154         .name = "ugensa",
155         .methods = ugensa_methods,
156         .size = sizeof(struct ugensa_softc),
157 };
158
159 DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
160 MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
161 MODULE_DEPEND(ugensa, usb, 1, 1, 1);
162
163 static const struct usb_device_id ugensa_devs[] = {
164         {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
165         {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
166         {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
167         {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
168         {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
169 };
170
171 static int
172 ugensa_probe(device_t dev)
173 {
174         struct usb_attach_arg *uaa = device_get_ivars(dev);
175
176         if (uaa->usb_mode != USB_MODE_HOST) {
177                 return (ENXIO);
178         }
179         if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
180                 return (ENXIO);
181         }
182         if (uaa->info.bIfaceIndex != 0) {
183                 return (ENXIO);
184         }
185         return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
186 }
187
188 static int
189 ugensa_attach(device_t dev)
190 {
191         struct usb_attach_arg *uaa = device_get_ivars(dev);
192         struct ugensa_softc *sc = device_get_softc(dev);
193         struct ugensa_sub_softc *ssc;
194         struct usb_interface *iface;
195         int32_t error;
196         uint8_t iface_index;
197         int x, cnt;
198
199         device_set_usb_desc(dev);
200         mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
201
202         /* Figure out how many interfaces this device has got */
203         for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
204                 if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
205                     (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
206                         /* we have reached the end */
207                         break;
208                 }
209         }
210
211         if (cnt == 0) {
212                 device_printf(dev, "No interfaces!\n");
213                 goto detach;
214         }
215         for (x = 0; x < cnt; x++) {
216                 iface = usbd_get_iface(uaa->device, x);
217                 if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
218                         /* Not a serial port, most likely a SD reader */
219                         continue;
220
221                 ssc = sc->sc_sub + sc->sc_niface;
222                 ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
223
224                 iface_index = (UGENSA_IFACE_INDEX + x);
225                 error = usbd_transfer_setup(uaa->device,
226                     &iface_index, ssc->sc_xfer, ugensa_xfer_config,
227                     UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
228
229                 if (error) {
230                         device_printf(dev, "allocating USB "
231                             "transfers failed!\n");
232                         goto detach;
233                 }
234                 /* clear stall at first run */
235                 mtx_lock(&sc->sc_mtx);
236                 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
237                 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
238                 mtx_unlock(&sc->sc_mtx);
239
240                 /* initialize port number */
241                 ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
242                 sc->sc_niface++;
243                 if (x != uaa->info.bIfaceIndex)
244                         usbd_set_parent_iface(uaa->device, x,
245                             uaa->info.bIfaceIndex);
246         }
247         device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
248
249         error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
250             &ugensa_callback, &sc->sc_mtx);
251         if (error) {
252                 DPRINTF("attach failed\n");
253                 goto detach;
254         }
255         return (0);                     /* success */
256
257 detach:
258         ugensa_detach(dev);
259         return (ENXIO);                 /* failure */
260 }
261
262 static int
263 ugensa_detach(device_t dev)
264 {
265         struct ugensa_softc *sc = device_get_softc(dev);
266         uint8_t x;
267
268         ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface);
269
270         for (x = 0; x < sc->sc_niface; x++) {
271                 usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
272         }
273         mtx_destroy(&sc->sc_mtx);
274
275         return (0);
276 }
277
278 static void
279 ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
280 {
281         struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
282         struct usb_page_cache *pc;
283         uint32_t actlen;
284
285         switch (USB_GET_STATE(xfer)) {
286         case USB_ST_SETUP:
287         case USB_ST_TRANSFERRED:
288 tr_setup:
289                 pc = usbd_xfer_get_frame(xfer, 0);
290                 if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
291                     UGENSA_BUF_SIZE, &actlen)) {
292                         usbd_xfer_set_frame_len(xfer, 0, actlen);
293                         usbd_transfer_submit(xfer);
294                 }
295                 return;
296
297         default:                        /* Error */
298                 if (error != USB_ERR_CANCELLED) {
299                         /* try to clear stall first */
300                         usbd_xfer_set_stall(xfer);
301                         goto tr_setup;
302                 }
303                 return;
304         }
305 }
306
307 static void
308 ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
309 {
310         struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
311         struct usb_page_cache *pc;
312         int actlen;
313
314         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
315
316         switch (USB_GET_STATE(xfer)) {
317         case USB_ST_TRANSFERRED:
318                 pc = usbd_xfer_get_frame(xfer, 0);
319                 ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
320
321         case USB_ST_SETUP:
322 tr_setup:
323                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
324                 usbd_transfer_submit(xfer);
325                 return;
326
327         default:                        /* Error */
328                 if (error != USB_ERR_CANCELLED) {
329                         /* try to clear stall first */
330                         usbd_xfer_set_stall(xfer);
331                         goto tr_setup;
332                 }
333                 return;
334         }
335 }
336
337 static void
338 ugensa_start_read(struct ucom_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         usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
344 }
345
346 static void
347 ugensa_stop_read(struct ucom_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         usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
353 }
354
355 static void
356 ugensa_start_write(struct ucom_softc *ucom)
357 {
358         struct ugensa_softc *sc = ucom->sc_parent;
359         struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
360
361         usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
362 }
363
364 static void
365 ugensa_stop_write(struct ucom_softc *ucom)
366 {
367         struct ugensa_softc *sc = ucom->sc_parent;
368         struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
369
370         usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
371 }