]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/dev/usb/serial/ugensa.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.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 static void     ugensa_poll(struct ucom_softc *ucom);
114
115 static const struct usb_config 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         .ucom_poll = &ugensa_poll,
142 };
143
144 static device_method_t ugensa_methods[] = {
145         /* Device methods */
146         DEVMETHOD(device_probe, ugensa_probe),
147         DEVMETHOD(device_attach, ugensa_attach),
148         DEVMETHOD(device_detach, ugensa_detach),
149         {0, 0}
150 };
151
152 static devclass_t ugensa_devclass;
153
154 static driver_t ugensa_driver = {
155         .name = "ugensa",
156         .methods = ugensa_methods,
157         .size = sizeof(struct ugensa_softc),
158 };
159
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);
163 MODULE_VERSION(ugensa, 1);
164
165 static const struct usb_device_id ugensa_devs[] = {
166         {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
167         {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
168         {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
169         {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
170         {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
171 };
172
173 static int
174 ugensa_probe(device_t dev)
175 {
176         struct usb_attach_arg *uaa = device_get_ivars(dev);
177
178         if (uaa->usb_mode != USB_MODE_HOST) {
179                 return (ENXIO);
180         }
181         if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
182                 return (ENXIO);
183         }
184         if (uaa->info.bIfaceIndex != 0) {
185                 return (ENXIO);
186         }
187         return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
188 }
189
190 static int
191 ugensa_attach(device_t dev)
192 {
193         struct usb_attach_arg *uaa = device_get_ivars(dev);
194         struct ugensa_softc *sc = device_get_softc(dev);
195         struct ugensa_sub_softc *ssc;
196         struct usb_interface *iface;
197         int32_t error;
198         uint8_t iface_index;
199         int x, cnt;
200
201         device_set_usb_desc(dev);
202         mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
203
204         /* Figure out how many interfaces this device has got */
205         for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
206                 if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
207                     (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
208                         /* we have reached the end */
209                         break;
210                 }
211         }
212
213         if (cnt == 0) {
214                 device_printf(dev, "No interfaces\n");
215                 goto detach;
216         }
217         for (x = 0; x < cnt; x++) {
218                 iface = usbd_get_iface(uaa->device, x);
219                 if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
220                         /* Not a serial port, most likely a SD reader */
221                         continue;
222
223                 ssc = sc->sc_sub + sc->sc_niface;
224                 ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
225
226                 iface_index = (UGENSA_IFACE_INDEX + x);
227                 error = usbd_transfer_setup(uaa->device,
228                     &iface_index, ssc->sc_xfer, ugensa_xfer_config,
229                     UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
230
231                 if (error) {
232                         device_printf(dev, "allocating USB "
233                             "transfers failed\n");
234                         goto detach;
235                 }
236                 /* clear stall at first run */
237                 mtx_lock(&sc->sc_mtx);
238                 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
239                 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
240                 mtx_unlock(&sc->sc_mtx);
241
242                 /* initialize port number */
243                 ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
244                 sc->sc_niface++;
245                 if (x != uaa->info.bIfaceIndex)
246                         usbd_set_parent_iface(uaa->device, x,
247                             uaa->info.bIfaceIndex);
248         }
249         device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
250
251         error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
252             &ugensa_callback, &sc->sc_mtx);
253         if (error) {
254                 DPRINTF("attach failed\n");
255                 goto detach;
256         }
257         return (0);                     /* success */
258
259 detach:
260         ugensa_detach(dev);
261         return (ENXIO);                 /* failure */
262 }
263
264 static int
265 ugensa_detach(device_t dev)
266 {
267         struct ugensa_softc *sc = device_get_softc(dev);
268         uint8_t x;
269
270         ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface);
271
272         for (x = 0; x < sc->sc_niface; x++) {
273                 usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
274         }
275         mtx_destroy(&sc->sc_mtx);
276
277         return (0);
278 }
279
280 static void
281 ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
282 {
283         struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
284         struct usb_page_cache *pc;
285         uint32_t actlen;
286
287         switch (USB_GET_STATE(xfer)) {
288         case USB_ST_SETUP:
289         case USB_ST_TRANSFERRED:
290 tr_setup:
291                 pc = usbd_xfer_get_frame(xfer, 0);
292                 if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
293                     UGENSA_BUF_SIZE, &actlen)) {
294                         usbd_xfer_set_frame_len(xfer, 0, actlen);
295                         usbd_transfer_submit(xfer);
296                 }
297                 return;
298
299         default:                        /* Error */
300                 if (error != USB_ERR_CANCELLED) {
301                         /* try to clear stall first */
302                         usbd_xfer_set_stall(xfer);
303                         goto tr_setup;
304                 }
305                 return;
306         }
307 }
308
309 static void
310 ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
311 {
312         struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
313         struct usb_page_cache *pc;
314         int actlen;
315
316         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
317
318         switch (USB_GET_STATE(xfer)) {
319         case USB_ST_TRANSFERRED:
320                 pc = usbd_xfer_get_frame(xfer, 0);
321                 ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
322
323         case USB_ST_SETUP:
324 tr_setup:
325                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
326                 usbd_transfer_submit(xfer);
327                 return;
328
329         default:                        /* Error */
330                 if (error != USB_ERR_CANCELLED) {
331                         /* try to clear stall first */
332                         usbd_xfer_set_stall(xfer);
333                         goto tr_setup;
334                 }
335                 return;
336         }
337 }
338
339 static void
340 ugensa_start_read(struct ucom_softc *ucom)
341 {
342         struct ugensa_softc *sc = ucom->sc_parent;
343         struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
344
345         usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
346 }
347
348 static void
349 ugensa_stop_read(struct ucom_softc *ucom)
350 {
351         struct ugensa_softc *sc = ucom->sc_parent;
352         struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
353
354         usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
355 }
356
357 static void
358 ugensa_start_write(struct ucom_softc *ucom)
359 {
360         struct ugensa_softc *sc = ucom->sc_parent;
361         struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
362
363         usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
364 }
365
366 static void
367 ugensa_stop_write(struct ucom_softc *ucom)
368 {
369         struct ugensa_softc *sc = ucom->sc_parent;
370         struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
371
372         usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
373 }
374
375 static void
376 ugensa_poll(struct ucom_softc *ucom)
377 {
378         struct ugensa_softc *sc = ucom->sc_parent;
379         struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
380
381         usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
382 }