Deleted Added
full compact
ugensa.c (204632) ugensa.c (212122)
1/* $FreeBSD: head/sys/dev/usb/serial/ugensa.c 204632 2010-03-03 10:18:03Z joel $ */
1/* $FreeBSD: head/sys/dev/usb/serial/ugensa.c 212122 2010-09-01 23:47:53Z 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * NOTE: all function names beginning like "ugensa_cfg_" can only
35 * be called from within the config thread function !
36 */
37
38#include <sys/stdint.h>
39#include <sys/stddef.h>
40#include <sys/param.h>
41#include <sys/queue.h>
42#include <sys/types.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/bus.h>
46#include <sys/linker_set.h>
47#include <sys/module.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/condvar.h>
51#include <sys/sysctl.h>
52#include <sys/sx.h>
53#include <sys/unistd.h>
54#include <sys/callout.h>
55#include <sys/malloc.h>
56#include <sys/priv.h>
57
58#include <dev/usb/usb.h>
59#include <dev/usb/usbdi.h>
60#include "usbdevs.h"
61
62#define USB_DEBUG_VAR usb_debug
63#include <dev/usb/usb_debug.h>
64#include <dev/usb/usb_process.h>
65
66#include <dev/usb/serial/usb_serial.h>
67
68#define UGENSA_BUF_SIZE 2048 /* bytes */
69#define UGENSA_CONFIG_INDEX 0
70#define UGENSA_IFACE_INDEX 0
71#define UGENSA_IFACE_MAX 8 /* exclusivly */
72
73enum {
74 UGENSA_BULK_DT_WR,
75 UGENSA_BULK_DT_RD,
76 UGENSA_N_TRANSFER,
77};
78
79struct ugensa_sub_softc {
80 struct ucom_softc *sc_ucom_ptr;
81 struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
82};
83
84struct ugensa_softc {
85 struct ucom_super_softc sc_super_ucom;
86 struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
87 struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
88
89 struct mtx sc_mtx;
90 uint8_t sc_niface;
91};
92
93/* prototypes */
94
95static device_probe_t ugensa_probe;
96static device_attach_t ugensa_attach;
97static device_detach_t ugensa_detach;
98
99static usb_callback_t ugensa_bulk_write_callback;
100static usb_callback_t ugensa_bulk_read_callback;
101
102static void ugensa_start_read(struct ucom_softc *);
103static void ugensa_stop_read(struct ucom_softc *);
104static void ugensa_start_write(struct ucom_softc *);
105static void ugensa_stop_write(struct ucom_softc *);
106static void ugensa_poll(struct ucom_softc *ucom);
107
108static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
109
110 [UGENSA_BULK_DT_WR] = {
111 .type = UE_BULK,
112 .endpoint = UE_ADDR_ANY,
113 .direction = UE_DIR_OUT,
114 .bufsize = UGENSA_BUF_SIZE,
115 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
116 .callback = &ugensa_bulk_write_callback,
117 },
118
119 [UGENSA_BULK_DT_RD] = {
120 .type = UE_BULK,
121 .endpoint = UE_ADDR_ANY,
122 .direction = UE_DIR_IN,
123 .bufsize = UGENSA_BUF_SIZE,
124 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
125 .callback = &ugensa_bulk_read_callback,
126 },
127};
128
129static const struct ucom_callback ugensa_callback = {
130 .ucom_start_read = &ugensa_start_read,
131 .ucom_stop_read = &ugensa_stop_read,
132 .ucom_start_write = &ugensa_start_write,
133 .ucom_stop_write = &ugensa_stop_write,
134 .ucom_poll = &ugensa_poll,
135};
136
137static device_method_t ugensa_methods[] = {
138 /* Device methods */
139 DEVMETHOD(device_probe, ugensa_probe),
140 DEVMETHOD(device_attach, ugensa_attach),
141 DEVMETHOD(device_detach, ugensa_detach),
142 {0, 0}
143};
144
145static devclass_t ugensa_devclass;
146
147static driver_t ugensa_driver = {
148 .name = "ugensa",
149 .methods = ugensa_methods,
150 .size = sizeof(struct ugensa_softc),
151};
152
153DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
154MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
155MODULE_DEPEND(ugensa, usb, 1, 1, 1);
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * NOTE: all function names beginning like "ugensa_cfg_" can only
35 * be called from within the config thread function !
36 */
37
38#include <sys/stdint.h>
39#include <sys/stddef.h>
40#include <sys/param.h>
41#include <sys/queue.h>
42#include <sys/types.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/bus.h>
46#include <sys/linker_set.h>
47#include <sys/module.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/condvar.h>
51#include <sys/sysctl.h>
52#include <sys/sx.h>
53#include <sys/unistd.h>
54#include <sys/callout.h>
55#include <sys/malloc.h>
56#include <sys/priv.h>
57
58#include <dev/usb/usb.h>
59#include <dev/usb/usbdi.h>
60#include "usbdevs.h"
61
62#define USB_DEBUG_VAR usb_debug
63#include <dev/usb/usb_debug.h>
64#include <dev/usb/usb_process.h>
65
66#include <dev/usb/serial/usb_serial.h>
67
68#define UGENSA_BUF_SIZE 2048 /* bytes */
69#define UGENSA_CONFIG_INDEX 0
70#define UGENSA_IFACE_INDEX 0
71#define UGENSA_IFACE_MAX 8 /* exclusivly */
72
73enum {
74 UGENSA_BULK_DT_WR,
75 UGENSA_BULK_DT_RD,
76 UGENSA_N_TRANSFER,
77};
78
79struct ugensa_sub_softc {
80 struct ucom_softc *sc_ucom_ptr;
81 struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
82};
83
84struct ugensa_softc {
85 struct ucom_super_softc sc_super_ucom;
86 struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
87 struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
88
89 struct mtx sc_mtx;
90 uint8_t sc_niface;
91};
92
93/* prototypes */
94
95static device_probe_t ugensa_probe;
96static device_attach_t ugensa_attach;
97static device_detach_t ugensa_detach;
98
99static usb_callback_t ugensa_bulk_write_callback;
100static usb_callback_t ugensa_bulk_read_callback;
101
102static void ugensa_start_read(struct ucom_softc *);
103static void ugensa_stop_read(struct ucom_softc *);
104static void ugensa_start_write(struct ucom_softc *);
105static void ugensa_stop_write(struct ucom_softc *);
106static void ugensa_poll(struct ucom_softc *ucom);
107
108static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
109
110 [UGENSA_BULK_DT_WR] = {
111 .type = UE_BULK,
112 .endpoint = UE_ADDR_ANY,
113 .direction = UE_DIR_OUT,
114 .bufsize = UGENSA_BUF_SIZE,
115 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
116 .callback = &ugensa_bulk_write_callback,
117 },
118
119 [UGENSA_BULK_DT_RD] = {
120 .type = UE_BULK,
121 .endpoint = UE_ADDR_ANY,
122 .direction = UE_DIR_IN,
123 .bufsize = UGENSA_BUF_SIZE,
124 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
125 .callback = &ugensa_bulk_read_callback,
126 },
127};
128
129static const struct ucom_callback ugensa_callback = {
130 .ucom_start_read = &ugensa_start_read,
131 .ucom_stop_read = &ugensa_stop_read,
132 .ucom_start_write = &ugensa_start_write,
133 .ucom_stop_write = &ugensa_stop_write,
134 .ucom_poll = &ugensa_poll,
135};
136
137static device_method_t ugensa_methods[] = {
138 /* Device methods */
139 DEVMETHOD(device_probe, ugensa_probe),
140 DEVMETHOD(device_attach, ugensa_attach),
141 DEVMETHOD(device_detach, ugensa_detach),
142 {0, 0}
143};
144
145static devclass_t ugensa_devclass;
146
147static driver_t ugensa_driver = {
148 .name = "ugensa",
149 .methods = ugensa_methods,
150 .size = sizeof(struct ugensa_softc),
151};
152
153DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
154MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
155MODULE_DEPEND(ugensa, usb, 1, 1, 1);
156MODULE_VERSION(ugensa, 1);
156
157static const struct usb_device_id ugensa_devs[] = {
158 {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
159 {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
160 {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
161 {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
162 {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
163};
164
165static int
166ugensa_probe(device_t dev)
167{
168 struct usb_attach_arg *uaa = device_get_ivars(dev);
169
170 if (uaa->usb_mode != USB_MODE_HOST) {
171 return (ENXIO);
172 }
173 if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
174 return (ENXIO);
175 }
176 if (uaa->info.bIfaceIndex != 0) {
177 return (ENXIO);
178 }
179 return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
180}
181
182static int
183ugensa_attach(device_t dev)
184{
185 struct usb_attach_arg *uaa = device_get_ivars(dev);
186 struct ugensa_softc *sc = device_get_softc(dev);
187 struct ugensa_sub_softc *ssc;
188 struct usb_interface *iface;
189 int32_t error;
190 uint8_t iface_index;
191 int x, cnt;
192
193 device_set_usb_desc(dev);
194 mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
195
196 /* Figure out how many interfaces this device has got */
197 for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
198 if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
199 (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
200 /* we have reached the end */
201 break;
202 }
203 }
204
205 if (cnt == 0) {
206 device_printf(dev, "No interfaces\n");
207 goto detach;
208 }
209 for (x = 0; x < cnt; x++) {
210 iface = usbd_get_iface(uaa->device, x);
211 if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
212 /* Not a serial port, most likely a SD reader */
213 continue;
214
215 ssc = sc->sc_sub + sc->sc_niface;
216 ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
217
218 iface_index = (UGENSA_IFACE_INDEX + x);
219 error = usbd_transfer_setup(uaa->device,
220 &iface_index, ssc->sc_xfer, ugensa_xfer_config,
221 UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
222
223 if (error) {
224 device_printf(dev, "allocating USB "
225 "transfers failed\n");
226 goto detach;
227 }
228 /* clear stall at first run */
229 mtx_lock(&sc->sc_mtx);
230 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
231 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
232 mtx_unlock(&sc->sc_mtx);
233
234 /* initialize port number */
235 ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
236 sc->sc_niface++;
237 if (x != uaa->info.bIfaceIndex)
238 usbd_set_parent_iface(uaa->device, x,
239 uaa->info.bIfaceIndex);
240 }
241 device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
242
243 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
244 &ugensa_callback, &sc->sc_mtx);
245 if (error) {
246 DPRINTF("attach failed\n");
247 goto detach;
248 }
249 return (0); /* success */
250
251detach:
252 ugensa_detach(dev);
253 return (ENXIO); /* failure */
254}
255
256static int
257ugensa_detach(device_t dev)
258{
259 struct ugensa_softc *sc = device_get_softc(dev);
260 uint8_t x;
261
262 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface);
263
264 for (x = 0; x < sc->sc_niface; x++) {
265 usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
266 }
267 mtx_destroy(&sc->sc_mtx);
268
269 return (0);
270}
271
272static void
273ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
274{
275 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
276 struct usb_page_cache *pc;
277 uint32_t actlen;
278
279 switch (USB_GET_STATE(xfer)) {
280 case USB_ST_SETUP:
281 case USB_ST_TRANSFERRED:
282tr_setup:
283 pc = usbd_xfer_get_frame(xfer, 0);
284 if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
285 UGENSA_BUF_SIZE, &actlen)) {
286 usbd_xfer_set_frame_len(xfer, 0, actlen);
287 usbd_transfer_submit(xfer);
288 }
289 return;
290
291 default: /* Error */
292 if (error != USB_ERR_CANCELLED) {
293 /* try to clear stall first */
294 usbd_xfer_set_stall(xfer);
295 goto tr_setup;
296 }
297 return;
298 }
299}
300
301static void
302ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
303{
304 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
305 struct usb_page_cache *pc;
306 int actlen;
307
308 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
309
310 switch (USB_GET_STATE(xfer)) {
311 case USB_ST_TRANSFERRED:
312 pc = usbd_xfer_get_frame(xfer, 0);
313 ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
314
315 case USB_ST_SETUP:
316tr_setup:
317 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
318 usbd_transfer_submit(xfer);
319 return;
320
321 default: /* Error */
322 if (error != USB_ERR_CANCELLED) {
323 /* try to clear stall first */
324 usbd_xfer_set_stall(xfer);
325 goto tr_setup;
326 }
327 return;
328 }
329}
330
331static void
332ugensa_start_read(struct ucom_softc *ucom)
333{
334 struct ugensa_softc *sc = ucom->sc_parent;
335 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
336
337 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
338}
339
340static void
341ugensa_stop_read(struct ucom_softc *ucom)
342{
343 struct ugensa_softc *sc = ucom->sc_parent;
344 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
345
346 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
347}
348
349static void
350ugensa_start_write(struct ucom_softc *ucom)
351{
352 struct ugensa_softc *sc = ucom->sc_parent;
353 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
354
355 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
356}
357
358static void
359ugensa_stop_write(struct ucom_softc *ucom)
360{
361 struct ugensa_softc *sc = ucom->sc_parent;
362 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
363
364 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
365}
366
367static void
368ugensa_poll(struct ucom_softc *ucom)
369{
370 struct ugensa_softc *sc = ucom->sc_parent;
371 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
372
373 usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
374}
157
158static const struct usb_device_id ugensa_devs[] = {
159 {USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
160 {USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
161 {USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
162 {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
163 {USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
164};
165
166static int
167ugensa_probe(device_t dev)
168{
169 struct usb_attach_arg *uaa = device_get_ivars(dev);
170
171 if (uaa->usb_mode != USB_MODE_HOST) {
172 return (ENXIO);
173 }
174 if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
175 return (ENXIO);
176 }
177 if (uaa->info.bIfaceIndex != 0) {
178 return (ENXIO);
179 }
180 return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
181}
182
183static int
184ugensa_attach(device_t dev)
185{
186 struct usb_attach_arg *uaa = device_get_ivars(dev);
187 struct ugensa_softc *sc = device_get_softc(dev);
188 struct ugensa_sub_softc *ssc;
189 struct usb_interface *iface;
190 int32_t error;
191 uint8_t iface_index;
192 int x, cnt;
193
194 device_set_usb_desc(dev);
195 mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
196
197 /* Figure out how many interfaces this device has got */
198 for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
199 if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
200 (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
201 /* we have reached the end */
202 break;
203 }
204 }
205
206 if (cnt == 0) {
207 device_printf(dev, "No interfaces\n");
208 goto detach;
209 }
210 for (x = 0; x < cnt; x++) {
211 iface = usbd_get_iface(uaa->device, x);
212 if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
213 /* Not a serial port, most likely a SD reader */
214 continue;
215
216 ssc = sc->sc_sub + sc->sc_niface;
217 ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
218
219 iface_index = (UGENSA_IFACE_INDEX + x);
220 error = usbd_transfer_setup(uaa->device,
221 &iface_index, ssc->sc_xfer, ugensa_xfer_config,
222 UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
223
224 if (error) {
225 device_printf(dev, "allocating USB "
226 "transfers failed\n");
227 goto detach;
228 }
229 /* clear stall at first run */
230 mtx_lock(&sc->sc_mtx);
231 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
232 usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
233 mtx_unlock(&sc->sc_mtx);
234
235 /* initialize port number */
236 ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
237 sc->sc_niface++;
238 if (x != uaa->info.bIfaceIndex)
239 usbd_set_parent_iface(uaa->device, x,
240 uaa->info.bIfaceIndex);
241 }
242 device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
243
244 error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
245 &ugensa_callback, &sc->sc_mtx);
246 if (error) {
247 DPRINTF("attach failed\n");
248 goto detach;
249 }
250 return (0); /* success */
251
252detach:
253 ugensa_detach(dev);
254 return (ENXIO); /* failure */
255}
256
257static int
258ugensa_detach(device_t dev)
259{
260 struct ugensa_softc *sc = device_get_softc(dev);
261 uint8_t x;
262
263 ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface);
264
265 for (x = 0; x < sc->sc_niface; x++) {
266 usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
267 }
268 mtx_destroy(&sc->sc_mtx);
269
270 return (0);
271}
272
273static void
274ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
275{
276 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
277 struct usb_page_cache *pc;
278 uint32_t actlen;
279
280 switch (USB_GET_STATE(xfer)) {
281 case USB_ST_SETUP:
282 case USB_ST_TRANSFERRED:
283tr_setup:
284 pc = usbd_xfer_get_frame(xfer, 0);
285 if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
286 UGENSA_BUF_SIZE, &actlen)) {
287 usbd_xfer_set_frame_len(xfer, 0, actlen);
288 usbd_transfer_submit(xfer);
289 }
290 return;
291
292 default: /* Error */
293 if (error != USB_ERR_CANCELLED) {
294 /* try to clear stall first */
295 usbd_xfer_set_stall(xfer);
296 goto tr_setup;
297 }
298 return;
299 }
300}
301
302static void
303ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
304{
305 struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
306 struct usb_page_cache *pc;
307 int actlen;
308
309 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
310
311 switch (USB_GET_STATE(xfer)) {
312 case USB_ST_TRANSFERRED:
313 pc = usbd_xfer_get_frame(xfer, 0);
314 ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
315
316 case USB_ST_SETUP:
317tr_setup:
318 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
319 usbd_transfer_submit(xfer);
320 return;
321
322 default: /* Error */
323 if (error != USB_ERR_CANCELLED) {
324 /* try to clear stall first */
325 usbd_xfer_set_stall(xfer);
326 goto tr_setup;
327 }
328 return;
329 }
330}
331
332static void
333ugensa_start_read(struct ucom_softc *ucom)
334{
335 struct ugensa_softc *sc = ucom->sc_parent;
336 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
337
338 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
339}
340
341static void
342ugensa_stop_read(struct ucom_softc *ucom)
343{
344 struct ugensa_softc *sc = ucom->sc_parent;
345 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
346
347 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
348}
349
350static void
351ugensa_start_write(struct ucom_softc *ucom)
352{
353 struct ugensa_softc *sc = ucom->sc_parent;
354 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
355
356 usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
357}
358
359static void
360ugensa_stop_write(struct ucom_softc *ucom)
361{
362 struct ugensa_softc *sc = ucom->sc_parent;
363 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
364
365 usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
366}
367
368static void
369ugensa_poll(struct ucom_softc *ucom)
370{
371 struct ugensa_softc *sc = ucom->sc_parent;
372 struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
373
374 usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
375}