Deleted Added
full compact
ubser.c (192499) ubser.c (192502)
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/usb/serial/ubser.c 192499 2009-05-21 00:04:17Z thompsa $");
73__FBSDID("$FreeBSD: head/sys/dev/usb/serial/ubser.c 192502 2009-05-21 01:48:42Z thompsa $");
74
75/*
76 * BWCT serial adapter driver
77 */
78
79#include <dev/usb/usb.h>
80#include <dev/usb/usb_mfunc.h>
81#include <dev/usb/usb_error.h>
82#include <dev/usb/usb_cdc.h>
83
84#define USB_DEBUG_VAR ubser_debug
85
86#include <dev/usb/usb_core.h>
87#include <dev/usb/usb_debug.h>
88#include <dev/usb/usb_process.h>
89#include <dev/usb/usb_request.h>
90#include <dev/usb/usb_lookup.h>
91#include <dev/usb/usb_util.h>
92#include <dev/usb/usb_busdma.h>
93#include <dev/usb/usb_device.h>
94
95#include <dev/usb/serial/usb_serial.h>
96
97#define UBSER_UNIT_MAX 32
98
99/* Vendor Interface Requests */
100#define VENDOR_GET_NUMSER 0x01
101#define VENDOR_SET_BREAK 0x02
102#define VENDOR_CLEAR_BREAK 0x03
103
104#if USB_DEBUG
105static int ubser_debug = 0;
106
74
75/*
76 * BWCT serial adapter driver
77 */
78
79#include <dev/usb/usb.h>
80#include <dev/usb/usb_mfunc.h>
81#include <dev/usb/usb_error.h>
82#include <dev/usb/usb_cdc.h>
83
84#define USB_DEBUG_VAR ubser_debug
85
86#include <dev/usb/usb_core.h>
87#include <dev/usb/usb_debug.h>
88#include <dev/usb/usb_process.h>
89#include <dev/usb/usb_request.h>
90#include <dev/usb/usb_lookup.h>
91#include <dev/usb/usb_util.h>
92#include <dev/usb/usb_busdma.h>
93#include <dev/usb/usb_device.h>
94
95#include <dev/usb/serial/usb_serial.h>
96
97#define UBSER_UNIT_MAX 32
98
99/* Vendor Interface Requests */
100#define VENDOR_GET_NUMSER 0x01
101#define VENDOR_SET_BREAK 0x02
102#define VENDOR_CLEAR_BREAK 0x03
103
104#if USB_DEBUG
105static int ubser_debug = 0;
106
107SYSCTL_NODE(_hw_usb2, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
108SYSCTL_INT(_hw_usb2_ubser, OID_AUTO, debug, CTLFLAG_RW,
107SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
108SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RW,
109 &ubser_debug, 0, "ubser debug level");
110#endif
111
112enum {
113 UBSER_BULK_DT_WR,
114 UBSER_BULK_DT_RD,
115 UBSER_N_TRANSFER,
116};
117
118struct ubser_softc {
119 struct usb2_com_super_softc sc_super_ucom;
120 struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX];
121
122 struct usb2_xfer *sc_xfer[UBSER_N_TRANSFER];
123 struct usb2_device *sc_udev;
124 struct mtx sc_mtx;
125
126 uint16_t sc_tx_size;
127
128 uint8_t sc_numser;
129 uint8_t sc_iface_no;
130 uint8_t sc_iface_index;
131 uint8_t sc_curr_tx_unit;
132 uint8_t sc_name[16];
133};
134
135/* prototypes */
136
137static device_probe_t ubser_probe;
138static device_attach_t ubser_attach;
139static device_detach_t ubser_detach;
140
141static usb2_callback_t ubser_write_callback;
142static usb2_callback_t ubser_read_callback;
143
144static int ubser_pre_param(struct usb2_com_softc *, struct termios *);
145static void ubser_cfg_set_break(struct usb2_com_softc *, uint8_t);
146static void ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *,
147 uint8_t *);
148static void ubser_start_read(struct usb2_com_softc *);
149static void ubser_stop_read(struct usb2_com_softc *);
150static void ubser_start_write(struct usb2_com_softc *);
151static void ubser_stop_write(struct usb2_com_softc *);
152
153static const struct usb2_config ubser_config[UBSER_N_TRANSFER] = {
154
155 [UBSER_BULK_DT_WR] = {
156 .type = UE_BULK,
157 .endpoint = UE_ADDR_ANY,
158 .direction = UE_DIR_OUT,
159 .bufsize = 0, /* use wMaxPacketSize */
160 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
161 .callback = &ubser_write_callback,
162 },
163
164 [UBSER_BULK_DT_RD] = {
165 .type = UE_BULK,
166 .endpoint = UE_ADDR_ANY,
167 .direction = UE_DIR_IN,
168 .bufsize = 0, /* use wMaxPacketSize */
169 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
170 .callback = &ubser_read_callback,
171 },
172};
173
174static const struct usb2_com_callback ubser_callback = {
175 .usb2_com_cfg_set_break = &ubser_cfg_set_break,
176 .usb2_com_cfg_get_status = &ubser_cfg_get_status,
177 .usb2_com_pre_param = &ubser_pre_param,
178 .usb2_com_start_read = &ubser_start_read,
179 .usb2_com_stop_read = &ubser_stop_read,
180 .usb2_com_start_write = &ubser_start_write,
181 .usb2_com_stop_write = &ubser_stop_write,
182};
183
184static device_method_t ubser_methods[] = {
185 DEVMETHOD(device_probe, ubser_probe),
186 DEVMETHOD(device_attach, ubser_attach),
187 DEVMETHOD(device_detach, ubser_detach),
188 {0, 0}
189};
190
191static devclass_t ubser_devclass;
192
193static driver_t ubser_driver = {
194 .name = "ubser",
195 .methods = ubser_methods,
196 .size = sizeof(struct ubser_softc),
197};
198
199DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0);
200MODULE_DEPEND(ubser, ucom, 1, 1, 1);
201MODULE_DEPEND(ubser, usb, 1, 1, 1);
202
203static int
204ubser_probe(device_t dev)
205{
206 struct usb2_attach_arg *uaa = device_get_ivars(dev);
207
208 if (uaa->usb_mode != USB_MODE_HOST) {
209 return (ENXIO);
210 }
211 /* check if this is a BWCT vendor specific ubser interface */
212 if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) &&
213 (uaa->info.bInterfaceClass == 0xff) &&
214 (uaa->info.bInterfaceSubClass == 0x00))
215 return (0);
216
217 return (ENXIO);
218}
219
220static int
221ubser_attach(device_t dev)
222{
223 struct usb2_attach_arg *uaa = device_get_ivars(dev);
224 struct ubser_softc *sc = device_get_softc(dev);
225 struct usb2_device_request req;
226 uint8_t n;
227 int error;
228
229 device_set_usb2_desc(dev);
230 mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
231
232 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
233 device_get_nameunit(dev));
234
235 sc->sc_iface_no = uaa->info.bIfaceNum;
236 sc->sc_iface_index = uaa->info.bIfaceIndex;
237 sc->sc_udev = uaa->device;
238
239 /* get number of serials */
240 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
241 req.bRequest = VENDOR_GET_NUMSER;
242 USETW(req.wValue, 0);
243 req.wIndex[0] = sc->sc_iface_no;
244 req.wIndex[1] = 0;
245 USETW(req.wLength, 1);
246 error = usb2_do_request_flags(uaa->device, NULL,
247 &req, &sc->sc_numser,
248 0, NULL, USB_DEFAULT_TIMEOUT);
249
250 if (error || (sc->sc_numser == 0)) {
251 device_printf(dev, "failed to get number "
252 "of serial ports: %s\n",
253 usb2_errstr(error));
254 goto detach;
255 }
256 if (sc->sc_numser > UBSER_UNIT_MAX)
257 sc->sc_numser = UBSER_UNIT_MAX;
258
259 device_printf(dev, "found %i serials\n", sc->sc_numser);
260
261 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
262 sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
263 if (error) {
264 goto detach;
265 }
266 sc->sc_tx_size = sc->sc_xfer[UBSER_BULK_DT_WR]->max_data_length;
267
268 if (sc->sc_tx_size == 0) {
269 DPRINTFN(0, "invalid tx_size!\n");
270 goto detach;
271 }
272 /* initialize port numbers */
273
274 for (n = 0; n < sc->sc_numser; n++) {
275 sc->sc_ucom[n].sc_portno = n;
276 }
277
278 error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
279 sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
280 if (error) {
281 goto detach;
282 }
283
284 mtx_lock(&sc->sc_mtx);
285 usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
286 usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
287 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
288 mtx_unlock(&sc->sc_mtx);
289
290 return (0); /* success */
291
292detach:
293 ubser_detach(dev);
294 return (ENXIO); /* failure */
295}
296
297static int
298ubser_detach(device_t dev)
299{
300 struct ubser_softc *sc = device_get_softc(dev);
301
302 DPRINTF("\n");
303
304 usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser);
305 usb2_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
306 mtx_destroy(&sc->sc_mtx);
307
308 return (0);
309}
310
311static int
312ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t)
313{
314 DPRINTF("\n");
315
316 /*
317 * The firmware on our devices can only do 8n1@9600bps
318 * without handshake.
319 * We refuse to accept other configurations.
320 */
321
322 /* ensure 9600bps */
323 switch (t->c_ospeed) {
324 case 9600:
325 break;
326 default:
327 return (EINVAL);
328 }
329
330 /* 2 stop bits not possible */
331 if (t->c_cflag & CSTOPB)
332 return (EINVAL);
333
334 /* XXX parity handling not possible with current firmware */
335 if (t->c_cflag & PARENB)
336 return (EINVAL);
337
338 /* we can only do 8 data bits */
339 switch (t->c_cflag & CSIZE) {
340 case CS8:
341 break;
342 default:
343 return (EINVAL);
344 }
345
346 /* we can't do any kind of hardware handshaking */
347 if ((t->c_cflag &
348 (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
349 return (EINVAL);
350
351 /*
352 * XXX xon/xoff not supported by the firmware!
353 * This is handled within FreeBSD only and may overflow buffers
354 * because of delayed reaction due to device buffering.
355 */
356
357 return (0);
358}
359
360static __inline void
361ubser_inc_tx_unit(struct ubser_softc *sc)
362{
363 sc->sc_curr_tx_unit++;
364 if (sc->sc_curr_tx_unit >= sc->sc_numser) {
365 sc->sc_curr_tx_unit = 0;
366 }
367}
368
369static void
370ubser_write_callback(struct usb2_xfer *xfer)
371{
372 struct ubser_softc *sc = xfer->priv_sc;
373 uint8_t buf[1];
374 uint8_t first_unit = sc->sc_curr_tx_unit;
375 uint32_t actlen;
376
377 switch (USB_GET_STATE(xfer)) {
378 case USB_ST_SETUP:
379 case USB_ST_TRANSFERRED:
380tr_setup:
381 do {
382 if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
383 xfer->frbuffers, 1, sc->sc_tx_size - 1,
384 &actlen)) {
385
386 buf[0] = sc->sc_curr_tx_unit;
387
388 usb2_copy_in(xfer->frbuffers, 0, buf, 1);
389
390 xfer->frlengths[0] = actlen + 1;
391 usb2_start_hardware(xfer);
392
393 ubser_inc_tx_unit(sc); /* round robin */
394
395 break;
396 }
397 ubser_inc_tx_unit(sc);
398
399 } while (sc->sc_curr_tx_unit != first_unit);
400
401 return;
402
403 default: /* Error */
404 if (xfer->error != USB_ERR_CANCELLED) {
405 /* try to clear stall first */
406 xfer->flags.stall_pipe = 1;
407 goto tr_setup;
408 }
409 return;
410
411 }
412}
413
414static void
415ubser_read_callback(struct usb2_xfer *xfer)
416{
417 struct ubser_softc *sc = xfer->priv_sc;
418 uint8_t buf[1];
419
420 switch (USB_GET_STATE(xfer)) {
421 case USB_ST_TRANSFERRED:
422 if (xfer->actlen < 1) {
423 DPRINTF("invalid actlen=0!\n");
424 goto tr_setup;
425 }
426 usb2_copy_out(xfer->frbuffers, 0, buf, 1);
427
428 if (buf[0] >= sc->sc_numser) {
429 DPRINTF("invalid serial number!\n");
430 goto tr_setup;
431 }
432 usb2_com_put_data(sc->sc_ucom + buf[0],
433 xfer->frbuffers, 1, xfer->actlen - 1);
434
435 case USB_ST_SETUP:
436tr_setup:
437 xfer->frlengths[0] = xfer->max_data_length;
438 usb2_start_hardware(xfer);
439 return;
440
441 default: /* Error */
442 if (xfer->error != USB_ERR_CANCELLED) {
443 /* try to clear stall first */
444 xfer->flags.stall_pipe = 1;
445 goto tr_setup;
446 }
447 return;
448
449 }
450}
451
452static void
453ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
454{
455 struct ubser_softc *sc = ucom->sc_parent;
456 uint8_t x = ucom->sc_portno;
457 struct usb2_device_request req;
458 usb2_error_t err;
459
460 if (onoff) {
461
462 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
463 req.bRequest = VENDOR_SET_BREAK;
464 req.wValue[0] = x;
465 req.wValue[1] = 0;
466 req.wIndex[0] = sc->sc_iface_no;
467 req.wIndex[1] = 0;
468 USETW(req.wLength, 0);
469
470 err = usb2_com_cfg_do_request(sc->sc_udev, ucom,
471 &req, NULL, 0, 1000);
472 if (err) {
473 DPRINTFN(0, "send break failed, error=%s\n",
474 usb2_errstr(err));
475 }
476 }
477}
478
479static void
480ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
481{
482 /* fake status bits */
483 *lsr = 0;
484 *msr = SER_DCD;
485}
486
487static void
488ubser_start_read(struct usb2_com_softc *ucom)
489{
490 struct ubser_softc *sc = ucom->sc_parent;
491
492 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
493}
494
495static void
496ubser_stop_read(struct usb2_com_softc *ucom)
497{
498 struct ubser_softc *sc = ucom->sc_parent;
499
500 usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
501}
502
503static void
504ubser_start_write(struct usb2_com_softc *ucom)
505{
506 struct ubser_softc *sc = ucom->sc_parent;
507
508 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
509}
510
511static void
512ubser_stop_write(struct usb2_com_softc *ucom)
513{
514 struct ubser_softc *sc = ucom->sc_parent;
515
516 usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
517}
109 &ubser_debug, 0, "ubser debug level");
110#endif
111
112enum {
113 UBSER_BULK_DT_WR,
114 UBSER_BULK_DT_RD,
115 UBSER_N_TRANSFER,
116};
117
118struct ubser_softc {
119 struct usb2_com_super_softc sc_super_ucom;
120 struct usb2_com_softc sc_ucom[UBSER_UNIT_MAX];
121
122 struct usb2_xfer *sc_xfer[UBSER_N_TRANSFER];
123 struct usb2_device *sc_udev;
124 struct mtx sc_mtx;
125
126 uint16_t sc_tx_size;
127
128 uint8_t sc_numser;
129 uint8_t sc_iface_no;
130 uint8_t sc_iface_index;
131 uint8_t sc_curr_tx_unit;
132 uint8_t sc_name[16];
133};
134
135/* prototypes */
136
137static device_probe_t ubser_probe;
138static device_attach_t ubser_attach;
139static device_detach_t ubser_detach;
140
141static usb2_callback_t ubser_write_callback;
142static usb2_callback_t ubser_read_callback;
143
144static int ubser_pre_param(struct usb2_com_softc *, struct termios *);
145static void ubser_cfg_set_break(struct usb2_com_softc *, uint8_t);
146static void ubser_cfg_get_status(struct usb2_com_softc *, uint8_t *,
147 uint8_t *);
148static void ubser_start_read(struct usb2_com_softc *);
149static void ubser_stop_read(struct usb2_com_softc *);
150static void ubser_start_write(struct usb2_com_softc *);
151static void ubser_stop_write(struct usb2_com_softc *);
152
153static const struct usb2_config ubser_config[UBSER_N_TRANSFER] = {
154
155 [UBSER_BULK_DT_WR] = {
156 .type = UE_BULK,
157 .endpoint = UE_ADDR_ANY,
158 .direction = UE_DIR_OUT,
159 .bufsize = 0, /* use wMaxPacketSize */
160 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
161 .callback = &ubser_write_callback,
162 },
163
164 [UBSER_BULK_DT_RD] = {
165 .type = UE_BULK,
166 .endpoint = UE_ADDR_ANY,
167 .direction = UE_DIR_IN,
168 .bufsize = 0, /* use wMaxPacketSize */
169 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
170 .callback = &ubser_read_callback,
171 },
172};
173
174static const struct usb2_com_callback ubser_callback = {
175 .usb2_com_cfg_set_break = &ubser_cfg_set_break,
176 .usb2_com_cfg_get_status = &ubser_cfg_get_status,
177 .usb2_com_pre_param = &ubser_pre_param,
178 .usb2_com_start_read = &ubser_start_read,
179 .usb2_com_stop_read = &ubser_stop_read,
180 .usb2_com_start_write = &ubser_start_write,
181 .usb2_com_stop_write = &ubser_stop_write,
182};
183
184static device_method_t ubser_methods[] = {
185 DEVMETHOD(device_probe, ubser_probe),
186 DEVMETHOD(device_attach, ubser_attach),
187 DEVMETHOD(device_detach, ubser_detach),
188 {0, 0}
189};
190
191static devclass_t ubser_devclass;
192
193static driver_t ubser_driver = {
194 .name = "ubser",
195 .methods = ubser_methods,
196 .size = sizeof(struct ubser_softc),
197};
198
199DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, NULL, 0);
200MODULE_DEPEND(ubser, ucom, 1, 1, 1);
201MODULE_DEPEND(ubser, usb, 1, 1, 1);
202
203static int
204ubser_probe(device_t dev)
205{
206 struct usb2_attach_arg *uaa = device_get_ivars(dev);
207
208 if (uaa->usb_mode != USB_MODE_HOST) {
209 return (ENXIO);
210 }
211 /* check if this is a BWCT vendor specific ubser interface */
212 if ((strcmp(uaa->device->manufacturer, "BWCT") == 0) &&
213 (uaa->info.bInterfaceClass == 0xff) &&
214 (uaa->info.bInterfaceSubClass == 0x00))
215 return (0);
216
217 return (ENXIO);
218}
219
220static int
221ubser_attach(device_t dev)
222{
223 struct usb2_attach_arg *uaa = device_get_ivars(dev);
224 struct ubser_softc *sc = device_get_softc(dev);
225 struct usb2_device_request req;
226 uint8_t n;
227 int error;
228
229 device_set_usb2_desc(dev);
230 mtx_init(&sc->sc_mtx, "ubser", NULL, MTX_DEF);
231
232 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
233 device_get_nameunit(dev));
234
235 sc->sc_iface_no = uaa->info.bIfaceNum;
236 sc->sc_iface_index = uaa->info.bIfaceIndex;
237 sc->sc_udev = uaa->device;
238
239 /* get number of serials */
240 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
241 req.bRequest = VENDOR_GET_NUMSER;
242 USETW(req.wValue, 0);
243 req.wIndex[0] = sc->sc_iface_no;
244 req.wIndex[1] = 0;
245 USETW(req.wLength, 1);
246 error = usb2_do_request_flags(uaa->device, NULL,
247 &req, &sc->sc_numser,
248 0, NULL, USB_DEFAULT_TIMEOUT);
249
250 if (error || (sc->sc_numser == 0)) {
251 device_printf(dev, "failed to get number "
252 "of serial ports: %s\n",
253 usb2_errstr(error));
254 goto detach;
255 }
256 if (sc->sc_numser > UBSER_UNIT_MAX)
257 sc->sc_numser = UBSER_UNIT_MAX;
258
259 device_printf(dev, "found %i serials\n", sc->sc_numser);
260
261 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
262 sc->sc_xfer, ubser_config, UBSER_N_TRANSFER, sc, &sc->sc_mtx);
263 if (error) {
264 goto detach;
265 }
266 sc->sc_tx_size = sc->sc_xfer[UBSER_BULK_DT_WR]->max_data_length;
267
268 if (sc->sc_tx_size == 0) {
269 DPRINTFN(0, "invalid tx_size!\n");
270 goto detach;
271 }
272 /* initialize port numbers */
273
274 for (n = 0; n < sc->sc_numser; n++) {
275 sc->sc_ucom[n].sc_portno = n;
276 }
277
278 error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
279 sc->sc_numser, sc, &ubser_callback, &sc->sc_mtx);
280 if (error) {
281 goto detach;
282 }
283
284 mtx_lock(&sc->sc_mtx);
285 usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_WR]);
286 usb2_transfer_set_stall(sc->sc_xfer[UBSER_BULK_DT_RD]);
287 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
288 mtx_unlock(&sc->sc_mtx);
289
290 return (0); /* success */
291
292detach:
293 ubser_detach(dev);
294 return (ENXIO); /* failure */
295}
296
297static int
298ubser_detach(device_t dev)
299{
300 struct ubser_softc *sc = device_get_softc(dev);
301
302 DPRINTF("\n");
303
304 usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_numser);
305 usb2_transfer_unsetup(sc->sc_xfer, UBSER_N_TRANSFER);
306 mtx_destroy(&sc->sc_mtx);
307
308 return (0);
309}
310
311static int
312ubser_pre_param(struct usb2_com_softc *ucom, struct termios *t)
313{
314 DPRINTF("\n");
315
316 /*
317 * The firmware on our devices can only do 8n1@9600bps
318 * without handshake.
319 * We refuse to accept other configurations.
320 */
321
322 /* ensure 9600bps */
323 switch (t->c_ospeed) {
324 case 9600:
325 break;
326 default:
327 return (EINVAL);
328 }
329
330 /* 2 stop bits not possible */
331 if (t->c_cflag & CSTOPB)
332 return (EINVAL);
333
334 /* XXX parity handling not possible with current firmware */
335 if (t->c_cflag & PARENB)
336 return (EINVAL);
337
338 /* we can only do 8 data bits */
339 switch (t->c_cflag & CSIZE) {
340 case CS8:
341 break;
342 default:
343 return (EINVAL);
344 }
345
346 /* we can't do any kind of hardware handshaking */
347 if ((t->c_cflag &
348 (CRTS_IFLOW | CDTR_IFLOW | CDSR_OFLOW | CCAR_OFLOW)) != 0)
349 return (EINVAL);
350
351 /*
352 * XXX xon/xoff not supported by the firmware!
353 * This is handled within FreeBSD only and may overflow buffers
354 * because of delayed reaction due to device buffering.
355 */
356
357 return (0);
358}
359
360static __inline void
361ubser_inc_tx_unit(struct ubser_softc *sc)
362{
363 sc->sc_curr_tx_unit++;
364 if (sc->sc_curr_tx_unit >= sc->sc_numser) {
365 sc->sc_curr_tx_unit = 0;
366 }
367}
368
369static void
370ubser_write_callback(struct usb2_xfer *xfer)
371{
372 struct ubser_softc *sc = xfer->priv_sc;
373 uint8_t buf[1];
374 uint8_t first_unit = sc->sc_curr_tx_unit;
375 uint32_t actlen;
376
377 switch (USB_GET_STATE(xfer)) {
378 case USB_ST_SETUP:
379 case USB_ST_TRANSFERRED:
380tr_setup:
381 do {
382 if (usb2_com_get_data(sc->sc_ucom + sc->sc_curr_tx_unit,
383 xfer->frbuffers, 1, sc->sc_tx_size - 1,
384 &actlen)) {
385
386 buf[0] = sc->sc_curr_tx_unit;
387
388 usb2_copy_in(xfer->frbuffers, 0, buf, 1);
389
390 xfer->frlengths[0] = actlen + 1;
391 usb2_start_hardware(xfer);
392
393 ubser_inc_tx_unit(sc); /* round robin */
394
395 break;
396 }
397 ubser_inc_tx_unit(sc);
398
399 } while (sc->sc_curr_tx_unit != first_unit);
400
401 return;
402
403 default: /* Error */
404 if (xfer->error != USB_ERR_CANCELLED) {
405 /* try to clear stall first */
406 xfer->flags.stall_pipe = 1;
407 goto tr_setup;
408 }
409 return;
410
411 }
412}
413
414static void
415ubser_read_callback(struct usb2_xfer *xfer)
416{
417 struct ubser_softc *sc = xfer->priv_sc;
418 uint8_t buf[1];
419
420 switch (USB_GET_STATE(xfer)) {
421 case USB_ST_TRANSFERRED:
422 if (xfer->actlen < 1) {
423 DPRINTF("invalid actlen=0!\n");
424 goto tr_setup;
425 }
426 usb2_copy_out(xfer->frbuffers, 0, buf, 1);
427
428 if (buf[0] >= sc->sc_numser) {
429 DPRINTF("invalid serial number!\n");
430 goto tr_setup;
431 }
432 usb2_com_put_data(sc->sc_ucom + buf[0],
433 xfer->frbuffers, 1, xfer->actlen - 1);
434
435 case USB_ST_SETUP:
436tr_setup:
437 xfer->frlengths[0] = xfer->max_data_length;
438 usb2_start_hardware(xfer);
439 return;
440
441 default: /* Error */
442 if (xfer->error != USB_ERR_CANCELLED) {
443 /* try to clear stall first */
444 xfer->flags.stall_pipe = 1;
445 goto tr_setup;
446 }
447 return;
448
449 }
450}
451
452static void
453ubser_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
454{
455 struct ubser_softc *sc = ucom->sc_parent;
456 uint8_t x = ucom->sc_portno;
457 struct usb2_device_request req;
458 usb2_error_t err;
459
460 if (onoff) {
461
462 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
463 req.bRequest = VENDOR_SET_BREAK;
464 req.wValue[0] = x;
465 req.wValue[1] = 0;
466 req.wIndex[0] = sc->sc_iface_no;
467 req.wIndex[1] = 0;
468 USETW(req.wLength, 0);
469
470 err = usb2_com_cfg_do_request(sc->sc_udev, ucom,
471 &req, NULL, 0, 1000);
472 if (err) {
473 DPRINTFN(0, "send break failed, error=%s\n",
474 usb2_errstr(err));
475 }
476 }
477}
478
479static void
480ubser_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
481{
482 /* fake status bits */
483 *lsr = 0;
484 *msr = SER_DCD;
485}
486
487static void
488ubser_start_read(struct usb2_com_softc *ucom)
489{
490 struct ubser_softc *sc = ucom->sc_parent;
491
492 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_RD]);
493}
494
495static void
496ubser_stop_read(struct usb2_com_softc *ucom)
497{
498 struct ubser_softc *sc = ucom->sc_parent;
499
500 usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_RD]);
501}
502
503static void
504ubser_start_write(struct usb2_com_softc *ucom)
505{
506 struct ubser_softc *sc = ucom->sc_parent;
507
508 usb2_transfer_start(sc->sc_xfer[UBSER_BULK_DT_WR]);
509}
510
511static void
512ubser_stop_write(struct usb2_com_softc *ucom)
513{
514 struct ubser_softc *sc = ucom->sc_parent;
515
516 usb2_transfer_stop(sc->sc_xfer[UBSER_BULK_DT_WR]);
517}