Deleted Added
full compact
ubser.c (185950) ubser.c (187176)
1/*-
2 * Copyright (c) 2004 Bernd Walter <ticso@freebsd.org>
3 *
4 * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
5 * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
6 * $Author: ticso $
7 * $Rev: 1127 $
8 */
9
10/*-
11 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*-
37 * Copyright (c) 2000 The NetBSD Foundation, Inc.
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Lennart Augustsson (lennart@augustsson.net).
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the NetBSD
54 * Foundation, Inc. and its contributors.
55 * 4. Neither the name of The NetBSD Foundation nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2004 Bernd Walter <ticso@freebsd.org>
3 *
4 * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
5 * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
6 * $Author: ticso $
7 * $Rev: 1127 $
8 */
9
10/*-
11 * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*-
37 * Copyright (c) 2000 The NetBSD Foundation, Inc.
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Lennart Augustsson (lennart@augustsson.net).
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the NetBSD
54 * Foundation, Inc. and its contributors.
55 * 4. Neither the name of The NetBSD Foundation nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72#include <sys/cdefs.h>
73__FBSDID("$FreeBSD: head/sys/dev/usb2/serial/ubser2.c 185950 2008-12-11 23:17:48Z thompsa $");
73__FBSDID("$FreeBSD: head/sys/dev/usb2/serial/ubser2.c 187176 2009-01-13 19:03:47Z thompsa $");
74
75/*
76 * BWCT serial adapter driver
77 */
78
79#include <dev/usb2/include/usb2_standard.h>
80#include <dev/usb2/include/usb2_mfunc.h>
81#include <dev/usb2/include/usb2_error.h>
82#include <dev/usb2/include/usb2_cdc.h>
83#include <dev/usb2/include/usb2_defs.h>
84
85#define USB_DEBUG_VAR ubser_debug
86
87#include <dev/usb2/core/usb2_core.h>
88#include <dev/usb2/core/usb2_debug.h>
89#include <dev/usb2/core/usb2_process.h>
74
75/*
76 * BWCT serial adapter driver
77 */
78
79#include <dev/usb2/include/usb2_standard.h>
80#include <dev/usb2/include/usb2_mfunc.h>
81#include <dev/usb2/include/usb2_error.h>
82#include <dev/usb2/include/usb2_cdc.h>
83#include <dev/usb2/include/usb2_defs.h>
84
85#define USB_DEBUG_VAR ubser_debug
86
87#include <dev/usb2/core/usb2_core.h>
88#include <dev/usb2/core/usb2_debug.h>
89#include <dev/usb2/core/usb2_process.h>
90#include <dev/usb2/core/usb2_config_td.h>
91#include <dev/usb2/core/usb2_request.h>
92#include <dev/usb2/core/usb2_lookup.h>
93#include <dev/usb2/core/usb2_util.h>
94#include <dev/usb2/core/usb2_busdma.h>
95#include <dev/usb2/core/usb2_device.h>
96
97#include <dev/usb2/serial/usb2_serial.h>
98
99#define UBSER_UNIT_MAX 32
100
101/* Vendor Interface Requests */
102#define VENDOR_GET_NUMSER 0x01
103#define VENDOR_SET_BREAK 0x02
104#define VENDOR_CLEAR_BREAK 0x03
105
106#if USB_DEBUG
107static int ubser_debug = 0;
108
109SYSCTL_NODE(_hw_usb2, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
110SYSCTL_INT(_hw_usb2_ubser, OID_AUTO, debug, CTLFLAG_RW,
111 &ubser_debug, 0, "ubser debug level");
112#endif
113
114#define UBSER_TR_DT_WRITE 0
115#define UBSER_TR_DT_READ 1
116#define UBSER_TR_CS_WRITE 2
117#define UBSER_TR_CS_READ 3
118#define UBSER_TR_MAX 4
119
120struct ubser_softc {
121 struct usb2_com_super_softc sc_super_ucom;
122 struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX];
123
124 struct usb2_xfer *sc_xfer[UBSER_TR_MAX];
125 struct usb2_device *sc_udev;
126
127 uint16_t sc_tx_size;
128
129 uint8_t sc_numser;
130 uint8_t sc_flags;
131#define UBSER_FLAG_READ_STALL 0x01
132#define UBSER_FLAG_WRITE_STALL 0x02
133
134 uint8_t sc_iface_no;
135 uint8_t sc_iface_index;
136 uint8_t sc_curr_tx_unit;
137 uint8_t sc_name[16];
138};
139
140/* prototypes */
141
142static device_probe_t ubser_probe;
143static device_attach_t ubser_attach;
144static device_detach_t ubser_detach;
145
146static usb2_callback_t ubser_write_clear_stall_callback;
147static usb2_callback_t ubser_write_callback;
148static usb2_callback_t ubser_read_clear_stall_callback;
149static usb2_callback_t ubser_read_callback;
150
151static int ubser_pre_param(struct usb2_com_softc *, struct termios *);
152static void ubser_cfg_set_break(struct usb2_com_softc *, uint8_t);
153static void ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *,
154 uint8_t *);
155static void ubser_start_read(struct usb2_com_softc *);
156static void ubser_stop_read(struct usb2_com_softc *);
157static void ubser_start_write(struct usb2_com_softc *);
158static void ubser_stop_write(struct usb2_com_softc *);
159
160static const struct usb2_config ubser_config[UBSER_TR_MAX] = {
161
162 [UBSER_TR_DT_WRITE] = {
163 .type = UE_BULK,
164 .endpoint = UE_ADDR_ANY,
165 .direction = UE_DIR_OUT,
166 .mh.bufsize = 0, /* use wMaxPacketSize */
167 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
168 .mh.callback = &ubser_write_callback,
169 },
170
171 [UBSER_TR_DT_READ] = {
172 .type = UE_BULK,
173 .endpoint = UE_ADDR_ANY,
174 .direction = UE_DIR_IN,
175 .mh.bufsize = 0, /* use wMaxPacketSize */
176 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
177 .mh.callback = &ubser_read_callback,
178 },
179
180 [UBSER_TR_CS_WRITE] = {
181 .type = UE_CONTROL,
182 .endpoint = 0x00, /* Control pipe */
183 .direction = UE_DIR_ANY,
184 .mh.bufsize = sizeof(struct usb2_device_request),
185 .mh.flags = {},
186 .mh.callback = &ubser_write_clear_stall_callback,
187 .mh.timeout = 1000, /* 1 second */
188 .mh.interval = 50, /* 50ms */
189 },
190
191 [UBSER_TR_CS_READ] = {
192 .type = UE_CONTROL,
193 .endpoint = 0x00, /* Control pipe */
194 .direction = UE_DIR_ANY,
195 .mh.bufsize = sizeof(struct usb2_device_request),
196 .mh.flags = {},
197 .mh.callback = &ubser_read_clear_stall_callback,
198 .mh.timeout = 1000, /* 1 second */
199 .mh.interval = 50, /* 50ms */
200 },
201};
202
203static const struct usb2_com_callback ubser_callback = {
204 .usb2_com_cfg_set_break = &ubser_cfg_set_break,
205 .usb2_com_cfg_get_status = &ubser_cfg_get_status,
206 .usb2_com_pre_param = &ubser_pre_param,
207 .usb2_com_start_read = &ubser_start_read,
208 .usb2_com_stop_read = &ubser_stop_read,
209 .usb2_com_start_write = &ubser_start_write,
210 .usb2_com_stop_write = &ubser_stop_write,
211};
212
213static device_method_t ubser_methods[] = {
214 DEVMETHOD(device_probe, ubser_probe),
215 DEVMETHOD(device_attach, ubser_attach),
216 DEVMETHOD(device_detach, ubser_detach),
217 {0, 0}
218};
219
220static devclass_t ubser_devclass;
221
222static driver_t ubser_driver = {
223 .name = "ubser",
224 .methods = ubser_methods,
225 .size = sizeof(struct ubser_softc),
226};
227
228DRIVER_MODULE(ubser, ushub, ubser_driver, ubser_devclass, NULL, 0);
229MODULE_DEPEND(ubser, usb2_serial, 1, 1, 1);
230MODULE_DEPEND(ubser, usb2_core, 1, 1, 1);
231
232static int
233ubser_probe(device_t dev)
234{
235 struct usb2_attach_arg *uaa = device_get_ivars(dev);
236
237 if (uaa->usb2_mode != USB_MODE_HOST) {
238 return (ENXIO);
239 }
240 /* check if this is a BWCT vendor specific ubser interface */
241 if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) &&
242 (uaa->info.bInterfaceClass == 0xff) &&
243 (uaa->info.bInterfaceSubClass == 0x00))
244 return (0);
245
246 return (ENXIO);
247}
248
249static int
250ubser_attach(device_t dev)
251{
252 struct usb2_attach_arg *uaa = device_get_ivars(dev);
253 struct ubser_softc *sc = device_get_softc(dev);
254 struct usb2_device_request req;
255 uint8_t n;
256 int error;
257
258 if (sc == NULL) {
259 return (ENOMEM);
260 }
261 device_set_usb2_desc(dev);
262
263 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
264 device_get_nameunit(dev));
265
266 sc->sc_iface_no = uaa->info.bIfaceNum;
267 sc->sc_iface_index = uaa->info.bIfaceIndex;
268 sc->sc_udev = uaa->device;
269
270 /* get number of serials */
271 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
272 req.bRequest = VENDOR_GET_NUMSER;
273 USETW(req.wValue, 0);
274 req.wIndex[0] = sc->sc_iface_no;
275 req.wIndex[1] = 0;
276 USETW(req.wLength, 1);
277 error = usb2_do_request_flags
278 (uaa->device, &Giant, &req, &sc->sc_numser,
279 0, NULL, USB_DEFAULT_TIMEOUT);
280
281 if (error || (sc->sc_numser == 0)) {
282 device_printf(dev, "failed to get number "
283 "of serial ports: %s\n",
284 usb2_errstr(error));
285 goto detach;
286 }
287 if (sc->sc_numser > UBSER_UNIT_MAX)
288 sc->sc_numser = UBSER_UNIT_MAX;
289
290 device_printf(dev, "found %i serials\n", sc->sc_numser);
291
292 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
293 sc->sc_xfer, ubser_config, UBSER_TR_MAX, sc, &Giant);
294 if (error) {
295 goto detach;
296 }
297 sc->sc_tx_size = sc->sc_xfer[UBSER_TR_DT_WRITE]->max_data_length;
298
299 if (sc->sc_tx_size == 0) {
300 DPRINTFN(0, "invalid tx_size!\n");
301 goto detach;
302 }
303 /* initialize port numbers */
304
305 for (n = 0; n < sc->sc_numser; n++) {
306 sc->sc_ucom[n].sc_portno = n;
307 }
308
309 error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
310 sc->sc_numser, sc, &ubser_callback, &Giant);
311 if (error) {
312 goto detach;
313 }
314 mtx_lock(&Giant);
315
316 sc->sc_flags |= (UBSER_FLAG_READ_STALL |
317 UBSER_FLAG_WRITE_STALL);
318
319 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_READ]);
320
321 mtx_unlock(&Giant);
322
323 return (0); /* success */
324
325detach:
326 ubser_detach(dev);
327 return (ENXIO); /* failure */
328}
329
330static int
331ubser_detach(device_t dev)
332{
333 struct ubser_softc *sc = device_get_softc(dev);
334 uint8_t n;
335
336 DPRINTF("\n");
337
338 usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser);
339
340 /*
341 * need to stop all transfers atomically, hence when clear stall
342 * completes, it might start other transfers !
343 */
344 mtx_lock(&Giant);
345 for (n = 0; n < UBSER_TR_MAX; n++) {
346 usb2_transfer_stop(sc->sc_xfer[n]);
347 }
348 mtx_unlock(&Giant);
349
350 usb2_transfer_unsetup(sc->sc_xfer, UBSER_TR_MAX);
351
352 return (0);
353}
354
355static int
356ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t)
357{
358 DPRINTF("\n");
359
360 /*
361 * The firmware on our devices can only do 8n1@9600bps
362 * without handshake.
363 * We refuse to accept other configurations.
364 */
365
366 /* ensure 9600bps */
367 switch (t->c_ospeed) {
368 case 9600:
369 break;
370 default:
371 return (EINVAL);
372 }
373
374 /* 2 stop bits not possible */
375 if (t->c_cflag & CSTOPB)
376 return (EINVAL);
377
378 /* XXX parity handling not possible with current firmware */
379 if (t->c_cflag & PARENB)
380 return (EINVAL);
381
382 /* we can only do 8 data bits */
383 switch (t->c_cflag & CSIZE) {
384 case CS8:
385 break;
386 default:
387 return (EINVAL);
388 }
389
390 /* we can't do any kind of hardware handshaking */
391 if ((t->c_cflag &
392 (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
393 return (EINVAL);
394
395 /*
396 * XXX xon/xoff not supported by the firmware!
397 * This is handled within FreeBSD only and may overflow buffers
398 * because of delayed reaction due to device buffering.
399 */
400
401 return (0);
402}
403
404static __inline void
405ubser_inc_tx_unit(struct ubser_softc *sc)
406{
407 sc->sc_curr_tx_unit++;
408 if (sc->sc_curr_tx_unit >= sc->sc_numser) {
409 sc->sc_curr_tx_unit = 0;
410 }
411}
412
413static void
414ubser_write_clear_stall_callback(struct usb2_xfer *xfer)
415{
416 struct ubser_softc *sc = xfer->priv_sc;
417 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_WRITE];
418
419 if (usb2_clear_stall_callback(xfer, xfer_other)) {
420 DPRINTF("stall cleared\n");
421 sc->sc_flags &= ~UBSER_FLAG_WRITE_STALL;
422 usb2_transfer_start(xfer_other);
423 }
424}
425
426static void
427ubser_write_callback(struct usb2_xfer *xfer)
428{
429 struct ubser_softc *sc = xfer->priv_sc;
430 uint8_t buf[1];
431 uint8_t first_unit = sc->sc_curr_tx_unit;
432 uint32_t actlen;
433
434 switch (USB_GET_STATE(xfer)) {
435 case USB_ST_SETUP:
436 case USB_ST_TRANSFERRED:
437 if (sc->sc_flags & UBSER_FLAG_WRITE_STALL) {
438 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]);
439 return;
440 }
441 do {
442 if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
443 xfer->frbuffers, 1, sc->sc_tx_size - 1,
444 &actlen)) {
445
446 buf[0] = sc->sc_curr_tx_unit;
447
448 usb2_copy_in(xfer->frbuffers, 0, buf, 1);
449
450 xfer->frlengths[0] = actlen + 1;
451 usb2_start_hardware(xfer);
452
453 ubser_inc_tx_unit(sc); /* round robin */
454
455 break;
456 }
457 ubser_inc_tx_unit(sc);
458
459 } while (sc->sc_curr_tx_unit != first_unit);
460
461 return;
462
463 default: /* Error */
464 if (xfer->error != USB_ERR_CANCELLED) {
465 sc->sc_flags |= UBSER_FLAG_WRITE_STALL;
466 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]);
467 }
468 return;
469
470 }
471}
472
473static void
474ubser_read_clear_stall_callback(struct usb2_xfer *xfer)
475{
476 struct ubser_softc *sc = xfer->priv_sc;
477 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_READ];
478
479 if (usb2_clear_stall_callback(xfer, xfer_other)) {
480 DPRINTF("stall cleared\n");
481 sc->sc_flags &= ~UBSER_FLAG_READ_STALL;
482 usb2_transfer_start(xfer_other);
483 }
484}
485
486static void
487ubser_read_callback(struct usb2_xfer *xfer)
488{
489 struct ubser_softc *sc = xfer->priv_sc;
490 uint8_t buf[1];
491
492 switch (USB_GET_STATE(xfer)) {
493 case USB_ST_TRANSFERRED:
494 if (xfer->actlen < 1) {
495 DPRINTF("invalid actlen=0!\n");
496 goto tr_setup;
497 }
498 usb2_copy_out(xfer->frbuffers, 0, buf, 1);
499
500 if (buf[0] >= sc->sc_numser) {
501 DPRINTF("invalid serial number!\n");
502 goto tr_setup;
503 }
504 usb2_com_put_data(sc->sc_ucom + buf[0],
505 xfer->frbuffers, 1, xfer->actlen - 1);
506
507 case USB_ST_SETUP:
508tr_setup:
509 if (sc->sc_flags & UBSER_FLAG_READ_STALL) {
510 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]);
511 } else {
512 xfer->frlengths[0] = xfer->max_data_length;
513 usb2_start_hardware(xfer);
514 }
515 return;
516
517 default: /* Error */
518 if (xfer->error != USB_ERR_CANCELLED) {
519 sc->sc_flags |= UBSER_FLAG_READ_STALL;
520 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]);
521 }
522 return;
523
524 }
525}
526
527static void
528ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
529{
530 struct ubser_softc *sc = ucom->sc_parent;
531 uint8_t x = ucom->sc_portno;
532 struct usb2_device_request req;
533 usb2_error_t err;
534
535 if (onoff) {
536
537 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
538 req.bRequest = VENDOR_SET_BREAK;
539 req.wValue[0] = x;
540 req.wValue[1] = 0;
541 req.wIndex[0] = sc->sc_iface_no;
542 req.wIndex[1] = 0;
543 USETW(req.wLength, 0);
544
545 err = usb2_do_request_flags
546 (sc->sc_udev, &Giant, &req, NULL, 0, NULL, 1000);
547
548 if (err) {
549 DPRINTFN(0, "send break failed, error=%s\n",
550 usb2_errstr(err));
551 }
552 }
553}
554
555static void
556ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
557{
558 /* fake status bits */
559 *lsr = 0;
560 *msr = SER_DCD;
561}
562
563static void
564ubser_start_read(struct usb2_com_softc *ucom)
565{
566 struct ubser_softc *sc = ucom->sc_parent;
567
568 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_READ]);
569}
570
571static void
572ubser_stop_read(struct usb2_com_softc *ucom)
573{
574 struct ubser_softc *sc = ucom->sc_parent;
575
576 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_READ]);
577 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_READ]);
578}
579
580static void
581ubser_start_write(struct usb2_com_softc *ucom)
582{
583 struct ubser_softc *sc = ucom->sc_parent;
584
585 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_WRITE]);
586}
587
588static void
589ubser_stop_write(struct usb2_com_softc *ucom)
590{
591 struct ubser_softc *sc = ucom->sc_parent;
592
593 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_WRITE]);
594 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_WRITE]);
595}
90#include <dev/usb2/core/usb2_request.h>
91#include <dev/usb2/core/usb2_lookup.h>
92#include <dev/usb2/core/usb2_util.h>
93#include <dev/usb2/core/usb2_busdma.h>
94#include <dev/usb2/core/usb2_device.h>
95
96#include <dev/usb2/serial/usb2_serial.h>
97
98#define UBSER_UNIT_MAX 32
99
100/* Vendor Interface Requests */
101#define VENDOR_GET_NUMSER 0x01
102#define VENDOR_SET_BREAK 0x02
103#define VENDOR_CLEAR_BREAK 0x03
104
105#if USB_DEBUG
106static int ubser_debug = 0;
107
108SYSCTL_NODE(_hw_usb2, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
109SYSCTL_INT(_hw_usb2_ubser, OID_AUTO, debug, CTLFLAG_RW,
110 &ubser_debug, 0, "ubser debug level");
111#endif
112
113#define UBSER_TR_DT_WRITE 0
114#define UBSER_TR_DT_READ 1
115#define UBSER_TR_CS_WRITE 2
116#define UBSER_TR_CS_READ 3
117#define UBSER_TR_MAX 4
118
119struct ubser_softc {
120 struct usb2_com_super_softc sc_super_ucom;
121 struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX];
122
123 struct usb2_xfer *sc_xfer[UBSER_TR_MAX];
124 struct usb2_device *sc_udev;
125
126 uint16_t sc_tx_size;
127
128 uint8_t sc_numser;
129 uint8_t sc_flags;
130#define UBSER_FLAG_READ_STALL 0x01
131#define UBSER_FLAG_WRITE_STALL 0x02
132
133 uint8_t sc_iface_no;
134 uint8_t sc_iface_index;
135 uint8_t sc_curr_tx_unit;
136 uint8_t sc_name[16];
137};
138
139/* prototypes */
140
141static device_probe_t ubser_probe;
142static device_attach_t ubser_attach;
143static device_detach_t ubser_detach;
144
145static usb2_callback_t ubser_write_clear_stall_callback;
146static usb2_callback_t ubser_write_callback;
147static usb2_callback_t ubser_read_clear_stall_callback;
148static usb2_callback_t ubser_read_callback;
149
150static int ubser_pre_param(struct usb2_com_softc *, struct termios *);
151static void ubser_cfg_set_break(struct usb2_com_softc *, uint8_t);
152static void ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *,
153 uint8_t *);
154static void ubser_start_read(struct usb2_com_softc *);
155static void ubser_stop_read(struct usb2_com_softc *);
156static void ubser_start_write(struct usb2_com_softc *);
157static void ubser_stop_write(struct usb2_com_softc *);
158
159static const struct usb2_config ubser_config[UBSER_TR_MAX] = {
160
161 [UBSER_TR_DT_WRITE] = {
162 .type = UE_BULK,
163 .endpoint = UE_ADDR_ANY,
164 .direction = UE_DIR_OUT,
165 .mh.bufsize = 0, /* use wMaxPacketSize */
166 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
167 .mh.callback = &ubser_write_callback,
168 },
169
170 [UBSER_TR_DT_READ] = {
171 .type = UE_BULK,
172 .endpoint = UE_ADDR_ANY,
173 .direction = UE_DIR_IN,
174 .mh.bufsize = 0, /* use wMaxPacketSize */
175 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
176 .mh.callback = &ubser_read_callback,
177 },
178
179 [UBSER_TR_CS_WRITE] = {
180 .type = UE_CONTROL,
181 .endpoint = 0x00, /* Control pipe */
182 .direction = UE_DIR_ANY,
183 .mh.bufsize = sizeof(struct usb2_device_request),
184 .mh.flags = {},
185 .mh.callback = &ubser_write_clear_stall_callback,
186 .mh.timeout = 1000, /* 1 second */
187 .mh.interval = 50, /* 50ms */
188 },
189
190 [UBSER_TR_CS_READ] = {
191 .type = UE_CONTROL,
192 .endpoint = 0x00, /* Control pipe */
193 .direction = UE_DIR_ANY,
194 .mh.bufsize = sizeof(struct usb2_device_request),
195 .mh.flags = {},
196 .mh.callback = &ubser_read_clear_stall_callback,
197 .mh.timeout = 1000, /* 1 second */
198 .mh.interval = 50, /* 50ms */
199 },
200};
201
202static const struct usb2_com_callback ubser_callback = {
203 .usb2_com_cfg_set_break = &ubser_cfg_set_break,
204 .usb2_com_cfg_get_status = &ubser_cfg_get_status,
205 .usb2_com_pre_param = &ubser_pre_param,
206 .usb2_com_start_read = &ubser_start_read,
207 .usb2_com_stop_read = &ubser_stop_read,
208 .usb2_com_start_write = &ubser_start_write,
209 .usb2_com_stop_write = &ubser_stop_write,
210};
211
212static device_method_t ubser_methods[] = {
213 DEVMETHOD(device_probe, ubser_probe),
214 DEVMETHOD(device_attach, ubser_attach),
215 DEVMETHOD(device_detach, ubser_detach),
216 {0, 0}
217};
218
219static devclass_t ubser_devclass;
220
221static driver_t ubser_driver = {
222 .name = "ubser",
223 .methods = ubser_methods,
224 .size = sizeof(struct ubser_softc),
225};
226
227DRIVER_MODULE(ubser, ushub, ubser_driver, ubser_devclass, NULL, 0);
228MODULE_DEPEND(ubser, usb2_serial, 1, 1, 1);
229MODULE_DEPEND(ubser, usb2_core, 1, 1, 1);
230
231static int
232ubser_probe(device_t dev)
233{
234 struct usb2_attach_arg *uaa = device_get_ivars(dev);
235
236 if (uaa->usb2_mode != USB_MODE_HOST) {
237 return (ENXIO);
238 }
239 /* check if this is a BWCT vendor specific ubser interface */
240 if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) &&
241 (uaa->info.bInterfaceClass == 0xff) &&
242 (uaa->info.bInterfaceSubClass == 0x00))
243 return (0);
244
245 return (ENXIO);
246}
247
248static int
249ubser_attach(device_t dev)
250{
251 struct usb2_attach_arg *uaa = device_get_ivars(dev);
252 struct ubser_softc *sc = device_get_softc(dev);
253 struct usb2_device_request req;
254 uint8_t n;
255 int error;
256
257 if (sc == NULL) {
258 return (ENOMEM);
259 }
260 device_set_usb2_desc(dev);
261
262 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
263 device_get_nameunit(dev));
264
265 sc->sc_iface_no = uaa->info.bIfaceNum;
266 sc->sc_iface_index = uaa->info.bIfaceIndex;
267 sc->sc_udev = uaa->device;
268
269 /* get number of serials */
270 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
271 req.bRequest = VENDOR_GET_NUMSER;
272 USETW(req.wValue, 0);
273 req.wIndex[0] = sc->sc_iface_no;
274 req.wIndex[1] = 0;
275 USETW(req.wLength, 1);
276 error = usb2_do_request_flags
277 (uaa->device, &Giant, &req, &sc->sc_numser,
278 0, NULL, USB_DEFAULT_TIMEOUT);
279
280 if (error || (sc->sc_numser == 0)) {
281 device_printf(dev, "failed to get number "
282 "of serial ports: %s\n",
283 usb2_errstr(error));
284 goto detach;
285 }
286 if (sc->sc_numser > UBSER_UNIT_MAX)
287 sc->sc_numser = UBSER_UNIT_MAX;
288
289 device_printf(dev, "found %i serials\n", sc->sc_numser);
290
291 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
292 sc->sc_xfer, ubser_config, UBSER_TR_MAX, sc, &Giant);
293 if (error) {
294 goto detach;
295 }
296 sc->sc_tx_size = sc->sc_xfer[UBSER_TR_DT_WRITE]->max_data_length;
297
298 if (sc->sc_tx_size == 0) {
299 DPRINTFN(0, "invalid tx_size!\n");
300 goto detach;
301 }
302 /* initialize port numbers */
303
304 for (n = 0; n < sc->sc_numser; n++) {
305 sc->sc_ucom[n].sc_portno = n;
306 }
307
308 error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
309 sc->sc_numser, sc, &ubser_callback, &Giant);
310 if (error) {
311 goto detach;
312 }
313 mtx_lock(&Giant);
314
315 sc->sc_flags |= (UBSER_FLAG_READ_STALL |
316 UBSER_FLAG_WRITE_STALL);
317
318 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_READ]);
319
320 mtx_unlock(&Giant);
321
322 return (0); /* success */
323
324detach:
325 ubser_detach(dev);
326 return (ENXIO); /* failure */
327}
328
329static int
330ubser_detach(device_t dev)
331{
332 struct ubser_softc *sc = device_get_softc(dev);
333 uint8_t n;
334
335 DPRINTF("\n");
336
337 usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser);
338
339 /*
340 * need to stop all transfers atomically, hence when clear stall
341 * completes, it might start other transfers !
342 */
343 mtx_lock(&Giant);
344 for (n = 0; n < UBSER_TR_MAX; n++) {
345 usb2_transfer_stop(sc->sc_xfer[n]);
346 }
347 mtx_unlock(&Giant);
348
349 usb2_transfer_unsetup(sc->sc_xfer, UBSER_TR_MAX);
350
351 return (0);
352}
353
354static int
355ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t)
356{
357 DPRINTF("\n");
358
359 /*
360 * The firmware on our devices can only do 8n1@9600bps
361 * without handshake.
362 * We refuse to accept other configurations.
363 */
364
365 /* ensure 9600bps */
366 switch (t->c_ospeed) {
367 case 9600:
368 break;
369 default:
370 return (EINVAL);
371 }
372
373 /* 2 stop bits not possible */
374 if (t->c_cflag & CSTOPB)
375 return (EINVAL);
376
377 /* XXX parity handling not possible with current firmware */
378 if (t->c_cflag & PARENB)
379 return (EINVAL);
380
381 /* we can only do 8 data bits */
382 switch (t->c_cflag & CSIZE) {
383 case CS8:
384 break;
385 default:
386 return (EINVAL);
387 }
388
389 /* we can't do any kind of hardware handshaking */
390 if ((t->c_cflag &
391 (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
392 return (EINVAL);
393
394 /*
395 * XXX xon/xoff not supported by the firmware!
396 * This is handled within FreeBSD only and may overflow buffers
397 * because of delayed reaction due to device buffering.
398 */
399
400 return (0);
401}
402
403static __inline void
404ubser_inc_tx_unit(struct ubser_softc *sc)
405{
406 sc->sc_curr_tx_unit++;
407 if (sc->sc_curr_tx_unit >= sc->sc_numser) {
408 sc->sc_curr_tx_unit = 0;
409 }
410}
411
412static void
413ubser_write_clear_stall_callback(struct usb2_xfer *xfer)
414{
415 struct ubser_softc *sc = xfer->priv_sc;
416 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_WRITE];
417
418 if (usb2_clear_stall_callback(xfer, xfer_other)) {
419 DPRINTF("stall cleared\n");
420 sc->sc_flags &= ~UBSER_FLAG_WRITE_STALL;
421 usb2_transfer_start(xfer_other);
422 }
423}
424
425static void
426ubser_write_callback(struct usb2_xfer *xfer)
427{
428 struct ubser_softc *sc = xfer->priv_sc;
429 uint8_t buf[1];
430 uint8_t first_unit = sc->sc_curr_tx_unit;
431 uint32_t actlen;
432
433 switch (USB_GET_STATE(xfer)) {
434 case USB_ST_SETUP:
435 case USB_ST_TRANSFERRED:
436 if (sc->sc_flags & UBSER_FLAG_WRITE_STALL) {
437 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]);
438 return;
439 }
440 do {
441 if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
442 xfer->frbuffers, 1, sc->sc_tx_size - 1,
443 &actlen)) {
444
445 buf[0] = sc->sc_curr_tx_unit;
446
447 usb2_copy_in(xfer->frbuffers, 0, buf, 1);
448
449 xfer->frlengths[0] = actlen + 1;
450 usb2_start_hardware(xfer);
451
452 ubser_inc_tx_unit(sc); /* round robin */
453
454 break;
455 }
456 ubser_inc_tx_unit(sc);
457
458 } while (sc->sc_curr_tx_unit != first_unit);
459
460 return;
461
462 default: /* Error */
463 if (xfer->error != USB_ERR_CANCELLED) {
464 sc->sc_flags |= UBSER_FLAG_WRITE_STALL;
465 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_WRITE]);
466 }
467 return;
468
469 }
470}
471
472static void
473ubser_read_clear_stall_callback(struct usb2_xfer *xfer)
474{
475 struct ubser_softc *sc = xfer->priv_sc;
476 struct usb2_xfer *xfer_other = sc->sc_xfer[UBSER_TR_DT_READ];
477
478 if (usb2_clear_stall_callback(xfer, xfer_other)) {
479 DPRINTF("stall cleared\n");
480 sc->sc_flags &= ~UBSER_FLAG_READ_STALL;
481 usb2_transfer_start(xfer_other);
482 }
483}
484
485static void
486ubser_read_callback(struct usb2_xfer *xfer)
487{
488 struct ubser_softc *sc = xfer->priv_sc;
489 uint8_t buf[1];
490
491 switch (USB_GET_STATE(xfer)) {
492 case USB_ST_TRANSFERRED:
493 if (xfer->actlen < 1) {
494 DPRINTF("invalid actlen=0!\n");
495 goto tr_setup;
496 }
497 usb2_copy_out(xfer->frbuffers, 0, buf, 1);
498
499 if (buf[0] >= sc->sc_numser) {
500 DPRINTF("invalid serial number!\n");
501 goto tr_setup;
502 }
503 usb2_com_put_data(sc->sc_ucom + buf[0],
504 xfer->frbuffers, 1, xfer->actlen - 1);
505
506 case USB_ST_SETUP:
507tr_setup:
508 if (sc->sc_flags & UBSER_FLAG_READ_STALL) {
509 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]);
510 } else {
511 xfer->frlengths[0] = xfer->max_data_length;
512 usb2_start_hardware(xfer);
513 }
514 return;
515
516 default: /* Error */
517 if (xfer->error != USB_ERR_CANCELLED) {
518 sc->sc_flags |= UBSER_FLAG_READ_STALL;
519 usb2_transfer_start(sc->sc_xfer[UBSER_TR_CS_READ]);
520 }
521 return;
522
523 }
524}
525
526static void
527ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
528{
529 struct ubser_softc *sc = ucom->sc_parent;
530 uint8_t x = ucom->sc_portno;
531 struct usb2_device_request req;
532 usb2_error_t err;
533
534 if (onoff) {
535
536 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
537 req.bRequest = VENDOR_SET_BREAK;
538 req.wValue[0] = x;
539 req.wValue[1] = 0;
540 req.wIndex[0] = sc->sc_iface_no;
541 req.wIndex[1] = 0;
542 USETW(req.wLength, 0);
543
544 err = usb2_do_request_flags
545 (sc->sc_udev, &Giant, &req, NULL, 0, NULL, 1000);
546
547 if (err) {
548 DPRINTFN(0, "send break failed, error=%s\n",
549 usb2_errstr(err));
550 }
551 }
552}
553
554static void
555ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
556{
557 /* fake status bits */
558 *lsr = 0;
559 *msr = SER_DCD;
560}
561
562static void
563ubser_start_read(struct usb2_com_softc *ucom)
564{
565 struct ubser_softc *sc = ucom->sc_parent;
566
567 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_READ]);
568}
569
570static void
571ubser_stop_read(struct usb2_com_softc *ucom)
572{
573 struct ubser_softc *sc = ucom->sc_parent;
574
575 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_READ]);
576 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_READ]);
577}
578
579static void
580ubser_start_write(struct usb2_com_softc *ucom)
581{
582 struct ubser_softc *sc = ucom->sc_parent;
583
584 usb2_transfer_start(sc->sc_xfer[UBSER_TR_DT_WRITE]);
585}
586
587static void
588ubser_stop_write(struct usb2_com_softc *ucom)
589{
590 struct ubser_softc *sc = ucom->sc_parent;
591
592 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_CS_WRITE]);
593 usb2_transfer_stop(sc->sc_xfer[UBSER_TR_DT_WRITE]);
594}