1/*-
2 * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/usb/serial/ubsa.c 212122 2010-09-01 23:47:53Z thompsa $");
29/*-
30 * Copyright (c) 2001 The NetBSD Foundation, Inc.
31 * All rights reserved.
32 *
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Ichiro FUKUHARA (ichiro@ichiro.org).
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the NetBSD
47 * Foundation, Inc. and its contributors.
48 * 4. Neither the name of The NetBSD Foundation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
63 */
64
65#include <sys/stdint.h>
66#include <sys/stddef.h>
67#include <sys/param.h>
68#include <sys/queue.h>
69#include <sys/types.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/bus.h>
73#include <sys/linker_set.h>
74#include <sys/module.h>
75#include <sys/lock.h>
76#include <sys/mutex.h>
77#include <sys/condvar.h>
78#include <sys/sysctl.h>
79#include <sys/sx.h>
80#include <sys/unistd.h>
81#include <sys/callout.h>
82#include <sys/malloc.h>
83#include <sys/priv.h>
84
85#include <dev/usb/usb.h>
86#include <dev/usb/usbdi.h>
87#include <dev/usb/usbdi_util.h>
88#include "usbdevs.h"
89
90#define USB_DEBUG_VAR ubsa_debug
91#include <dev/usb/usb_debug.h>
92#include <dev/usb/usb_process.h>
93
94#include <dev/usb/serial/usb_serial.h>
95
96#ifdef USB_DEBUG
97static int ubsa_debug = 0;
98
99SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
100SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
101 &ubsa_debug, 0, "ubsa debug level");
102#endif
103
104#define UBSA_BSIZE 1024 /* bytes */
105
106#define UBSA_CONFIG_INDEX 0
107#define UBSA_IFACE_INDEX 0
108
109#define UBSA_REG_BAUDRATE 0x00
110#define UBSA_REG_STOP_BITS 0x01
111#define UBSA_REG_DATA_BITS 0x02
112#define UBSA_REG_PARITY 0x03
113#define UBSA_REG_DTR 0x0A
114#define UBSA_REG_RTS 0x0B
115#define UBSA_REG_BREAK 0x0C
116#define UBSA_REG_FLOW_CTRL 0x10
117
118#define UBSA_PARITY_NONE 0x00
119#define UBSA_PARITY_EVEN 0x01
120#define UBSA_PARITY_ODD 0x02
121#define UBSA_PARITY_MARK 0x03
122#define UBSA_PARITY_SPACE 0x04
123
124#define UBSA_FLOW_NONE 0x0000
125#define UBSA_FLOW_OCTS 0x0001
126#define UBSA_FLOW_ODSR 0x0002
127#define UBSA_FLOW_IDSR 0x0004
128#define UBSA_FLOW_IDTR 0x0008
129#define UBSA_FLOW_IRTS 0x0010
130#define UBSA_FLOW_ORTS 0x0020
131#define UBSA_FLOW_UNKNOWN 0x0040
132#define UBSA_FLOW_OXON 0x0080
133#define UBSA_FLOW_IXON 0x0100
134
135/* line status register */
136#define UBSA_LSR_TSRE 0x40 /* Transmitter empty: byte sent */
137#define UBSA_LSR_TXRDY 0x20 /* Transmitter buffer empty */
138#define UBSA_LSR_BI 0x10 /* Break detected */
139#define UBSA_LSR_FE 0x08 /* Framing error: bad stop bit */
140#define UBSA_LSR_PE 0x04 /* Parity error */
141#define UBSA_LSR_OE 0x02 /* Overrun, lost incoming byte */
142#define UBSA_LSR_RXRDY 0x01 /* Byte ready in Receive Buffer */
143#define UBSA_LSR_RCV_MASK 0x1f /* Mask for incoming data or error */
144
145/* modem status register */
146/* All deltas are from the last read of the MSR. */
147#define UBSA_MSR_DCD 0x80 /* Current Data Carrier Detect */
148#define UBSA_MSR_RI 0x40 /* Current Ring Indicator */
149#define UBSA_MSR_DSR 0x20 /* Current Data Set Ready */
150#define UBSA_MSR_CTS 0x10 /* Current Clear to Send */
151#define UBSA_MSR_DDCD 0x08 /* DCD has changed state */
152#define UBSA_MSR_TERI 0x04 /* RI has toggled low to high */
153#define UBSA_MSR_DDSR 0x02 /* DSR has changed state */
154#define UBSA_MSR_DCTS 0x01 /* CTS has changed state */
155
156enum {
157 UBSA_BULK_DT_WR,
158 UBSA_BULK_DT_RD,
159 UBSA_INTR_DT_RD,
160 UBSA_N_TRANSFER,
161};
162
163struct ubsa_softc {
164 struct ucom_super_softc sc_super_ucom;
165 struct ucom_softc sc_ucom;
166
167 struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
168 struct usb_device *sc_udev;
169 struct mtx sc_mtx;
170
171 uint8_t sc_iface_no; /* interface number */
172 uint8_t sc_iface_index; /* interface index */
173 uint8_t sc_lsr; /* local status register */
174 uint8_t sc_msr; /* UBSA status register */
175};
176
177static device_probe_t ubsa_probe;
178static device_attach_t ubsa_attach;
179static device_detach_t ubsa_detach;
180
181static usb_callback_t ubsa_write_callback;
182static usb_callback_t ubsa_read_callback;
183static usb_callback_t ubsa_intr_callback;
184
185static void ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
186static void ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
187static void ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
188static void ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
189static int ubsa_pre_param(struct ucom_softc *, struct termios *);
190static void ubsa_cfg_param(struct ucom_softc *, struct termios *);
191static void ubsa_start_read(struct ucom_softc *);
192static void ubsa_stop_read(struct ucom_softc *);
193static void ubsa_start_write(struct ucom_softc *);
194static void ubsa_stop_write(struct ucom_softc *);
195static void ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
196 uint8_t *);
197static void ubsa_poll(struct ucom_softc *ucom);
198
199static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
200
201 [UBSA_BULK_DT_WR] = {
202 .type = UE_BULK,
203 .endpoint = UE_ADDR_ANY,
204 .direction = UE_DIR_OUT,
205 .bufsize = UBSA_BSIZE, /* bytes */
206 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
207 .callback = &ubsa_write_callback,
208 },
209
210 [UBSA_BULK_DT_RD] = {
211 .type = UE_BULK,
212 .endpoint = UE_ADDR_ANY,
213 .direction = UE_DIR_IN,
214 .bufsize = UBSA_BSIZE, /* bytes */
215 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
216 .callback = &ubsa_read_callback,
217 },
218
219 [UBSA_INTR_DT_RD] = {
220 .type = UE_INTERRUPT,
221 .endpoint = UE_ADDR_ANY,
222 .direction = UE_DIR_IN,
223 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
224 .bufsize = 0, /* use wMaxPacketSize */
225 .callback = &ubsa_intr_callback,
226 },
227};
228
229static const struct ucom_callback ubsa_callback = {
230 .ucom_cfg_get_status = &ubsa_cfg_get_status,
231 .ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
232 .ucom_cfg_set_rts = &ubsa_cfg_set_rts,
233 .ucom_cfg_set_break = &ubsa_cfg_set_break,
234 .ucom_cfg_param = &ubsa_cfg_param,
235 .ucom_pre_param = &ubsa_pre_param,
236 .ucom_start_read = &ubsa_start_read,
237 .ucom_stop_read = &ubsa_stop_read,
238 .ucom_start_write = &ubsa_start_write,
239 .ucom_stop_write = &ubsa_stop_write,
240 .ucom_poll = &ubsa_poll,
241};
242
243static const struct usb_device_id ubsa_devs[] = {
244 /* AnyData ADU-500A */
245 {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
246 /* AnyData ADU-E100A/H */
247 {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
248 /* Axesstel MV100H */
249 {USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
250 /* BELKIN F5U103 */
251 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
252 /* BELKIN F5U120 */
253 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
254 /* GoHubs GO-COM232 */
255 {USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
256 /* GoHubs GO-COM232 */
257 {USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
258 /* Peracom */
259 {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
260};
261
262static device_method_t ubsa_methods[] = {
263 DEVMETHOD(device_probe, ubsa_probe),
264 DEVMETHOD(device_attach, ubsa_attach),
265 DEVMETHOD(device_detach, ubsa_detach),
266 {0, 0}
267};
268
269static devclass_t ubsa_devclass;
270
271static driver_t ubsa_driver = {
272 .name = "ubsa",
273 .methods = ubsa_methods,
274 .size = sizeof(struct ubsa_softc),
275};
276
277DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
278MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
279MODULE_DEPEND(ubsa, usb, 1, 1, 1);
280MODULE_VERSION(ubsa, 1);
281
282static int
283ubsa_probe(device_t dev)
284{
285 struct usb_attach_arg *uaa = device_get_ivars(dev);
286
287 if (uaa->usb_mode != USB_MODE_HOST) {
288 return (ENXIO);
289 }
290 if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
291 return (ENXIO);
292 }
293 if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
294 return (ENXIO);
295 }
296 return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
297}
298
299static int
300ubsa_attach(device_t dev)
301{
302 struct usb_attach_arg *uaa = device_get_ivars(dev);
303 struct ubsa_softc *sc = device_get_softc(dev);
304 int error;
305
306 DPRINTF("sc=%p\n", sc);
307
308 device_set_usb_desc(dev);
309 mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
310
311 sc->sc_udev = uaa->device;
312 sc->sc_iface_no = uaa->info.bIfaceNum;
313 sc->sc_iface_index = UBSA_IFACE_INDEX;
314
315 error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
316 sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
317
318 if (error) {
319 DPRINTF("could not allocate all pipes\n");
320 goto detach;
321 }
322 /* clear stall at first run */
323 mtx_lock(&sc->sc_mtx);
324 usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
325 usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
326 mtx_unlock(&sc->sc_mtx);
327
328 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
329 &ubsa_callback, &sc->sc_mtx);
330 if (error) {
331 DPRINTF("ucom_attach failed\n");
332 goto detach;
333 }
334 return (0);
335
336detach:
337 ubsa_detach(dev);
338 return (ENXIO);
339}
340
341static int
342ubsa_detach(device_t dev)
343{
344 struct ubsa_softc *sc = device_get_softc(dev);
345
346 DPRINTF("sc=%p\n", sc);
347
348 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
349 usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
350 mtx_destroy(&sc->sc_mtx);
351
352 return (0);
353}
354
355static void
356ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
357{
358 struct usb_device_request req;
359 usb_error_t err;
360
361 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
362 req.bRequest = index;
363 USETW(req.wValue, value);
364 req.wIndex[0] = sc->sc_iface_no;
365 req.wIndex[1] = 0;
366 USETW(req.wLength, 0);
367
368 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
369 &req, NULL, 0, 1000);
370 if (err) {
371 DPRINTFN(0, "device request failed, err=%s "
372 "(ignored)\n", usbd_errstr(err));
373 }
374}
375
376static void
377ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
378{
379 struct ubsa_softc *sc = ucom->sc_parent;
380
381 DPRINTF("onoff = %d\n", onoff);
382
383 ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
384}
385
386static void
387ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
388{
389 struct ubsa_softc *sc = ucom->sc_parent;
390
391 DPRINTF("onoff = %d\n", onoff);
392
393 ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
394}
395
396static void
397ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
398{
399 struct ubsa_softc *sc = ucom->sc_parent;
400
401 DPRINTF("onoff = %d\n", onoff);
402
403 ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
404}
405
406static int
407ubsa_pre_param(struct ucom_softc *ucom, struct termios *t)
408{
409
410 DPRINTF("sc = %p\n", ucom->sc_parent);
411
412 switch (t->c_ospeed) {
413 case B0:
414 case B300:
415 case B600:
416 case B1200:
417 case B2400:
418 case B4800:
419 case B9600:
420 case B19200:
421 case B38400:
422 case B57600:
423 case B115200:
424 case B230400:
425 break;
426 default:
427 return (EINVAL);
428 }
429 return (0);
430}
431
432static void
433ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t)
434{
435 struct ubsa_softc *sc = ucom->sc_parent;
436 uint16_t value = 0;
437
438 DPRINTF("sc = %p\n", sc);
439
440 switch (t->c_ospeed) {
441 case B0:
442 ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
443 ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
444 ubsa_cfg_set_rts(&sc->sc_ucom, 0);
445 break;
446 case B300:
447 case B600:
448 case B1200:
449 case B2400:
450 case B4800:
451 case B9600:
452 case B19200:
453 case B38400:
454 case B57600:
455 case B115200:
456 case B230400:
457 value = B230400 / t->c_ospeed;
458 ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
459 break;
460 default:
461 return;
462 }
463
464 if (t->c_cflag & PARENB)
465 value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
466 else
467 value = UBSA_PARITY_NONE;
468
469 ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
470
471 switch (t->c_cflag & CSIZE) {
472 case CS5:
473 value = 0;
474 break;
475 case CS6:
476 value = 1;
477 break;
478 case CS7:
479 value = 2;
480 break;
481 default:
482 case CS8:
483 value = 3;
484 break;
485 }
486
487 ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
488
489 value = (t->c_cflag & CSTOPB) ? 1 : 0;
490
491 ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
492
493 value = 0;
494 if (t->c_cflag & CRTSCTS)
495 value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
496
497 if (t->c_iflag & (IXON | IXOFF))
498 value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
499
500 ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
501}
502
503static void
504ubsa_start_read(struct ucom_softc *ucom)
505{
506 struct ubsa_softc *sc = ucom->sc_parent;
507
508 /* start interrupt endpoint */
509 usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
510
511 /* start read endpoint */
512 usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
513}
514
515static void
516ubsa_stop_read(struct ucom_softc *ucom)
517{
518 struct ubsa_softc *sc = ucom->sc_parent;
519
520 /* stop interrupt endpoint */
521 usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
522
523 /* stop read endpoint */
524 usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
525}
526
527static void
528ubsa_start_write(struct ucom_softc *ucom)
529{
530 struct ubsa_softc *sc = ucom->sc_parent;
531
532 usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
533}
534
535static void
536ubsa_stop_write(struct ucom_softc *ucom)
537{
538 struct ubsa_softc *sc = ucom->sc_parent;
539
540 usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
541}
542
543static void
544ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
545{
546 struct ubsa_softc *sc = ucom->sc_parent;
547
548 DPRINTF("\n");
549
550 *lsr = sc->sc_lsr;
551 *msr = sc->sc_msr;
552}
553
554static void
555ubsa_write_callback(struct usb_xfer *xfer, usb_error_t error)
556{
557 struct ubsa_softc *sc = usbd_xfer_softc(xfer);
558 struct usb_page_cache *pc;
559 uint32_t actlen;
560
561 switch (USB_GET_STATE(xfer)) {
562 case USB_ST_SETUP:
563 case USB_ST_TRANSFERRED:
564tr_setup:
565 pc = usbd_xfer_get_frame(xfer, 0);
566 if (ucom_get_data(&sc->sc_ucom, pc, 0,
567 UBSA_BSIZE, &actlen)) {
568
569 usbd_xfer_set_frame_len(xfer, 0, actlen);
570 usbd_transfer_submit(xfer);
571 }
572 return;
573
574 default: /* Error */
575 if (error != USB_ERR_CANCELLED) {
576 /* try to clear stall first */
577 usbd_xfer_set_stall(xfer);
578 goto tr_setup;
579 }
580 return;
581
582 }
583}
584
585static void
586ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error)
587{
588 struct ubsa_softc *sc = usbd_xfer_softc(xfer);
589 struct usb_page_cache *pc;
590 int actlen;
591
592 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
593
594 switch (USB_GET_STATE(xfer)) {
595 case USB_ST_TRANSFERRED:
596 pc = usbd_xfer_get_frame(xfer, 0);
597 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
598
599 case USB_ST_SETUP:
600tr_setup:
601 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
602 usbd_transfer_submit(xfer);
603 return;
604
605 default: /* Error */
606 if (error != USB_ERR_CANCELLED) {
607 /* try to clear stall first */
608 usbd_xfer_set_stall(xfer);
609 goto tr_setup;
610 }
611 return;
612
613 }
614}
615
616static void
617ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error)
618{
619 struct ubsa_softc *sc = usbd_xfer_softc(xfer);
620 struct usb_page_cache *pc;
621 uint8_t buf[4];
622 int actlen;
623
624 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
625
626 switch (USB_GET_STATE(xfer)) {
627 case USB_ST_TRANSFERRED:
628
629 if (actlen >= sizeof(buf)) {
630 pc = usbd_xfer_get_frame(xfer, 0);
631 usbd_copy_out(pc, 0, buf, sizeof(buf));
632
633 /*
634 * incidentally, Belkin adapter status bits match
635 * UART 16550 bits
636 */
637 sc->sc_lsr = buf[2];
638 sc->sc_msr = buf[3];
639
640 DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
641 sc->sc_lsr, sc->sc_msr);
642
643 ucom_status_change(&sc->sc_ucom);
644 } else {
645 DPRINTF("ignoring short packet, %d bytes\n", actlen);
646 }
647
648 case USB_ST_SETUP:
649tr_setup:
650 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
651 usbd_transfer_submit(xfer);
652 return;
653
654 default: /* Error */
655 if (error != USB_ERR_CANCELLED) {
656 /* try to clear stall first */
657 usbd_xfer_set_stall(xfer);
658 goto tr_setup;
659 }
660 return;
661
662 }
663}
664
665static void
666ubsa_poll(struct ucom_softc *ucom)
667{
668 struct ubsa_softc *sc = ucom->sc_parent;
669 usbd_transfer_poll(sc->sc_xfer, UBSA_N_TRANSFER);
670
671}
2 * Copyright (c) 2002, Alexander Kabaev <kan.FreeBSD.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/usb/serial/ubsa.c 212122 2010-09-01 23:47:53Z thompsa $");
29/*-
30 * Copyright (c) 2001 The NetBSD Foundation, Inc.
31 * All rights reserved.
32 *
33 * This code is derived from software contributed to The NetBSD Foundation
34 * by Ichiro FUKUHARA (ichiro@ichiro.org).
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the NetBSD
47 * Foundation, Inc. and its contributors.
48 * 4. Neither the name of The NetBSD Foundation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
53 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
56 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
63 */
64
65#include <sys/stdint.h>
66#include <sys/stddef.h>
67#include <sys/param.h>
68#include <sys/queue.h>
69#include <sys/types.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/bus.h>
73#include <sys/linker_set.h>
74#include <sys/module.h>
75#include <sys/lock.h>
76#include <sys/mutex.h>
77#include <sys/condvar.h>
78#include <sys/sysctl.h>
79#include <sys/sx.h>
80#include <sys/unistd.h>
81#include <sys/callout.h>
82#include <sys/malloc.h>
83#include <sys/priv.h>
84
85#include <dev/usb/usb.h>
86#include <dev/usb/usbdi.h>
87#include <dev/usb/usbdi_util.h>
88#include "usbdevs.h"
89
90#define USB_DEBUG_VAR ubsa_debug
91#include <dev/usb/usb_debug.h>
92#include <dev/usb/usb_process.h>
93
94#include <dev/usb/serial/usb_serial.h>
95
96#ifdef USB_DEBUG
97static int ubsa_debug = 0;
98
99SYSCTL_NODE(_hw_usb, OID_AUTO, ubsa, CTLFLAG_RW, 0, "USB ubsa");
100SYSCTL_INT(_hw_usb_ubsa, OID_AUTO, debug, CTLFLAG_RW,
101 &ubsa_debug, 0, "ubsa debug level");
102#endif
103
104#define UBSA_BSIZE 1024 /* bytes */
105
106#define UBSA_CONFIG_INDEX 0
107#define UBSA_IFACE_INDEX 0
108
109#define UBSA_REG_BAUDRATE 0x00
110#define UBSA_REG_STOP_BITS 0x01
111#define UBSA_REG_DATA_BITS 0x02
112#define UBSA_REG_PARITY 0x03
113#define UBSA_REG_DTR 0x0A
114#define UBSA_REG_RTS 0x0B
115#define UBSA_REG_BREAK 0x0C
116#define UBSA_REG_FLOW_CTRL 0x10
117
118#define UBSA_PARITY_NONE 0x00
119#define UBSA_PARITY_EVEN 0x01
120#define UBSA_PARITY_ODD 0x02
121#define UBSA_PARITY_MARK 0x03
122#define UBSA_PARITY_SPACE 0x04
123
124#define UBSA_FLOW_NONE 0x0000
125#define UBSA_FLOW_OCTS 0x0001
126#define UBSA_FLOW_ODSR 0x0002
127#define UBSA_FLOW_IDSR 0x0004
128#define UBSA_FLOW_IDTR 0x0008
129#define UBSA_FLOW_IRTS 0x0010
130#define UBSA_FLOW_ORTS 0x0020
131#define UBSA_FLOW_UNKNOWN 0x0040
132#define UBSA_FLOW_OXON 0x0080
133#define UBSA_FLOW_IXON 0x0100
134
135/* line status register */
136#define UBSA_LSR_TSRE 0x40 /* Transmitter empty: byte sent */
137#define UBSA_LSR_TXRDY 0x20 /* Transmitter buffer empty */
138#define UBSA_LSR_BI 0x10 /* Break detected */
139#define UBSA_LSR_FE 0x08 /* Framing error: bad stop bit */
140#define UBSA_LSR_PE 0x04 /* Parity error */
141#define UBSA_LSR_OE 0x02 /* Overrun, lost incoming byte */
142#define UBSA_LSR_RXRDY 0x01 /* Byte ready in Receive Buffer */
143#define UBSA_LSR_RCV_MASK 0x1f /* Mask for incoming data or error */
144
145/* modem status register */
146/* All deltas are from the last read of the MSR. */
147#define UBSA_MSR_DCD 0x80 /* Current Data Carrier Detect */
148#define UBSA_MSR_RI 0x40 /* Current Ring Indicator */
149#define UBSA_MSR_DSR 0x20 /* Current Data Set Ready */
150#define UBSA_MSR_CTS 0x10 /* Current Clear to Send */
151#define UBSA_MSR_DDCD 0x08 /* DCD has changed state */
152#define UBSA_MSR_TERI 0x04 /* RI has toggled low to high */
153#define UBSA_MSR_DDSR 0x02 /* DSR has changed state */
154#define UBSA_MSR_DCTS 0x01 /* CTS has changed state */
155
156enum {
157 UBSA_BULK_DT_WR,
158 UBSA_BULK_DT_RD,
159 UBSA_INTR_DT_RD,
160 UBSA_N_TRANSFER,
161};
162
163struct ubsa_softc {
164 struct ucom_super_softc sc_super_ucom;
165 struct ucom_softc sc_ucom;
166
167 struct usb_xfer *sc_xfer[UBSA_N_TRANSFER];
168 struct usb_device *sc_udev;
169 struct mtx sc_mtx;
170
171 uint8_t sc_iface_no; /* interface number */
172 uint8_t sc_iface_index; /* interface index */
173 uint8_t sc_lsr; /* local status register */
174 uint8_t sc_msr; /* UBSA status register */
175};
176
177static device_probe_t ubsa_probe;
178static device_attach_t ubsa_attach;
179static device_detach_t ubsa_detach;
180
181static usb_callback_t ubsa_write_callback;
182static usb_callback_t ubsa_read_callback;
183static usb_callback_t ubsa_intr_callback;
184
185static void ubsa_cfg_request(struct ubsa_softc *, uint8_t, uint16_t);
186static void ubsa_cfg_set_dtr(struct ucom_softc *, uint8_t);
187static void ubsa_cfg_set_rts(struct ucom_softc *, uint8_t);
188static void ubsa_cfg_set_break(struct ucom_softc *, uint8_t);
189static int ubsa_pre_param(struct ucom_softc *, struct termios *);
190static void ubsa_cfg_param(struct ucom_softc *, struct termios *);
191static void ubsa_start_read(struct ucom_softc *);
192static void ubsa_stop_read(struct ucom_softc *);
193static void ubsa_start_write(struct ucom_softc *);
194static void ubsa_stop_write(struct ucom_softc *);
195static void ubsa_cfg_get_status(struct ucom_softc *, uint8_t *,
196 uint8_t *);
197static void ubsa_poll(struct ucom_softc *ucom);
198
199static const struct usb_config ubsa_config[UBSA_N_TRANSFER] = {
200
201 [UBSA_BULK_DT_WR] = {
202 .type = UE_BULK,
203 .endpoint = UE_ADDR_ANY,
204 .direction = UE_DIR_OUT,
205 .bufsize = UBSA_BSIZE, /* bytes */
206 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
207 .callback = &ubsa_write_callback,
208 },
209
210 [UBSA_BULK_DT_RD] = {
211 .type = UE_BULK,
212 .endpoint = UE_ADDR_ANY,
213 .direction = UE_DIR_IN,
214 .bufsize = UBSA_BSIZE, /* bytes */
215 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
216 .callback = &ubsa_read_callback,
217 },
218
219 [UBSA_INTR_DT_RD] = {
220 .type = UE_INTERRUPT,
221 .endpoint = UE_ADDR_ANY,
222 .direction = UE_DIR_IN,
223 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
224 .bufsize = 0, /* use wMaxPacketSize */
225 .callback = &ubsa_intr_callback,
226 },
227};
228
229static const struct ucom_callback ubsa_callback = {
230 .ucom_cfg_get_status = &ubsa_cfg_get_status,
231 .ucom_cfg_set_dtr = &ubsa_cfg_set_dtr,
232 .ucom_cfg_set_rts = &ubsa_cfg_set_rts,
233 .ucom_cfg_set_break = &ubsa_cfg_set_break,
234 .ucom_cfg_param = &ubsa_cfg_param,
235 .ucom_pre_param = &ubsa_pre_param,
236 .ucom_start_read = &ubsa_start_read,
237 .ucom_stop_read = &ubsa_stop_read,
238 .ucom_start_write = &ubsa_start_write,
239 .ucom_stop_write = &ubsa_stop_write,
240 .ucom_poll = &ubsa_poll,
241};
242
243static const struct usb_device_id ubsa_devs[] = {
244 /* AnyData ADU-500A */
245 {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A, 0)},
246 /* AnyData ADU-E100A/H */
247 {USB_VPI(USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_E100X, 0)},
248 /* Axesstel MV100H */
249 {USB_VPI(USB_VENDOR_AXESSTEL, USB_PRODUCT_AXESSTEL_DATAMODEM, 0)},
250 /* BELKIN F5U103 */
251 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U103, 0)},
252 /* BELKIN F5U120 */
253 {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U120, 0)},
254 /* GoHubs GO-COM232 */
255 {USB_VPI(USB_VENDOR_ETEK, USB_PRODUCT_ETEK_1COM, 0)},
256 /* GoHubs GO-COM232 */
257 {USB_VPI(USB_VENDOR_GOHUBS, USB_PRODUCT_GOHUBS_GOCOM232, 0)},
258 /* Peracom */
259 {USB_VPI(USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_SERIAL1, 0)},
260};
261
262static device_method_t ubsa_methods[] = {
263 DEVMETHOD(device_probe, ubsa_probe),
264 DEVMETHOD(device_attach, ubsa_attach),
265 DEVMETHOD(device_detach, ubsa_detach),
266 {0, 0}
267};
268
269static devclass_t ubsa_devclass;
270
271static driver_t ubsa_driver = {
272 .name = "ubsa",
273 .methods = ubsa_methods,
274 .size = sizeof(struct ubsa_softc),
275};
276
277DRIVER_MODULE(ubsa, uhub, ubsa_driver, ubsa_devclass, NULL, 0);
278MODULE_DEPEND(ubsa, ucom, 1, 1, 1);
279MODULE_DEPEND(ubsa, usb, 1, 1, 1);
280MODULE_VERSION(ubsa, 1);
281
282static int
283ubsa_probe(device_t dev)
284{
285 struct usb_attach_arg *uaa = device_get_ivars(dev);
286
287 if (uaa->usb_mode != USB_MODE_HOST) {
288 return (ENXIO);
289 }
290 if (uaa->info.bConfigIndex != UBSA_CONFIG_INDEX) {
291 return (ENXIO);
292 }
293 if (uaa->info.bIfaceIndex != UBSA_IFACE_INDEX) {
294 return (ENXIO);
295 }
296 return (usbd_lookup_id_by_uaa(ubsa_devs, sizeof(ubsa_devs), uaa));
297}
298
299static int
300ubsa_attach(device_t dev)
301{
302 struct usb_attach_arg *uaa = device_get_ivars(dev);
303 struct ubsa_softc *sc = device_get_softc(dev);
304 int error;
305
306 DPRINTF("sc=%p\n", sc);
307
308 device_set_usb_desc(dev);
309 mtx_init(&sc->sc_mtx, "ubsa", NULL, MTX_DEF);
310
311 sc->sc_udev = uaa->device;
312 sc->sc_iface_no = uaa->info.bIfaceNum;
313 sc->sc_iface_index = UBSA_IFACE_INDEX;
314
315 error = usbd_transfer_setup(uaa->device, &sc->sc_iface_index,
316 sc->sc_xfer, ubsa_config, UBSA_N_TRANSFER, sc, &sc->sc_mtx);
317
318 if (error) {
319 DPRINTF("could not allocate all pipes\n");
320 goto detach;
321 }
322 /* clear stall at first run */
323 mtx_lock(&sc->sc_mtx);
324 usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_WR]);
325 usbd_xfer_set_stall(sc->sc_xfer[UBSA_BULK_DT_RD]);
326 mtx_unlock(&sc->sc_mtx);
327
328 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
329 &ubsa_callback, &sc->sc_mtx);
330 if (error) {
331 DPRINTF("ucom_attach failed\n");
332 goto detach;
333 }
334 return (0);
335
336detach:
337 ubsa_detach(dev);
338 return (ENXIO);
339}
340
341static int
342ubsa_detach(device_t dev)
343{
344 struct ubsa_softc *sc = device_get_softc(dev);
345
346 DPRINTF("sc=%p\n", sc);
347
348 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
349 usbd_transfer_unsetup(sc->sc_xfer, UBSA_N_TRANSFER);
350 mtx_destroy(&sc->sc_mtx);
351
352 return (0);
353}
354
355static void
356ubsa_cfg_request(struct ubsa_softc *sc, uint8_t index, uint16_t value)
357{
358 struct usb_device_request req;
359 usb_error_t err;
360
361 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
362 req.bRequest = index;
363 USETW(req.wValue, value);
364 req.wIndex[0] = sc->sc_iface_no;
365 req.wIndex[1] = 0;
366 USETW(req.wLength, 0);
367
368 err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
369 &req, NULL, 0, 1000);
370 if (err) {
371 DPRINTFN(0, "device request failed, err=%s "
372 "(ignored)\n", usbd_errstr(err));
373 }
374}
375
376static void
377ubsa_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
378{
379 struct ubsa_softc *sc = ucom->sc_parent;
380
381 DPRINTF("onoff = %d\n", onoff);
382
383 ubsa_cfg_request(sc, UBSA_REG_DTR, onoff ? 1 : 0);
384}
385
386static void
387ubsa_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
388{
389 struct ubsa_softc *sc = ucom->sc_parent;
390
391 DPRINTF("onoff = %d\n", onoff);
392
393 ubsa_cfg_request(sc, UBSA_REG_RTS, onoff ? 1 : 0);
394}
395
396static void
397ubsa_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
398{
399 struct ubsa_softc *sc = ucom->sc_parent;
400
401 DPRINTF("onoff = %d\n", onoff);
402
403 ubsa_cfg_request(sc, UBSA_REG_BREAK, onoff ? 1 : 0);
404}
405
406static int
407ubsa_pre_param(struct ucom_softc *ucom, struct termios *t)
408{
409
410 DPRINTF("sc = %p\n", ucom->sc_parent);
411
412 switch (t->c_ospeed) {
413 case B0:
414 case B300:
415 case B600:
416 case B1200:
417 case B2400:
418 case B4800:
419 case B9600:
420 case B19200:
421 case B38400:
422 case B57600:
423 case B115200:
424 case B230400:
425 break;
426 default:
427 return (EINVAL);
428 }
429 return (0);
430}
431
432static void
433ubsa_cfg_param(struct ucom_softc *ucom, struct termios *t)
434{
435 struct ubsa_softc *sc = ucom->sc_parent;
436 uint16_t value = 0;
437
438 DPRINTF("sc = %p\n", sc);
439
440 switch (t->c_ospeed) {
441 case B0:
442 ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, 0);
443 ubsa_cfg_set_dtr(&sc->sc_ucom, 0);
444 ubsa_cfg_set_rts(&sc->sc_ucom, 0);
445 break;
446 case B300:
447 case B600:
448 case B1200:
449 case B2400:
450 case B4800:
451 case B9600:
452 case B19200:
453 case B38400:
454 case B57600:
455 case B115200:
456 case B230400:
457 value = B230400 / t->c_ospeed;
458 ubsa_cfg_request(sc, UBSA_REG_BAUDRATE, value);
459 break;
460 default:
461 return;
462 }
463
464 if (t->c_cflag & PARENB)
465 value = (t->c_cflag & PARODD) ? UBSA_PARITY_ODD : UBSA_PARITY_EVEN;
466 else
467 value = UBSA_PARITY_NONE;
468
469 ubsa_cfg_request(sc, UBSA_REG_PARITY, value);
470
471 switch (t->c_cflag & CSIZE) {
472 case CS5:
473 value = 0;
474 break;
475 case CS6:
476 value = 1;
477 break;
478 case CS7:
479 value = 2;
480 break;
481 default:
482 case CS8:
483 value = 3;
484 break;
485 }
486
487 ubsa_cfg_request(sc, UBSA_REG_DATA_BITS, value);
488
489 value = (t->c_cflag & CSTOPB) ? 1 : 0;
490
491 ubsa_cfg_request(sc, UBSA_REG_STOP_BITS, value);
492
493 value = 0;
494 if (t->c_cflag & CRTSCTS)
495 value |= UBSA_FLOW_OCTS | UBSA_FLOW_IRTS;
496
497 if (t->c_iflag & (IXON | IXOFF))
498 value |= UBSA_FLOW_OXON | UBSA_FLOW_IXON;
499
500 ubsa_cfg_request(sc, UBSA_REG_FLOW_CTRL, value);
501}
502
503static void
504ubsa_start_read(struct ucom_softc *ucom)
505{
506 struct ubsa_softc *sc = ucom->sc_parent;
507
508 /* start interrupt endpoint */
509 usbd_transfer_start(sc->sc_xfer[UBSA_INTR_DT_RD]);
510
511 /* start read endpoint */
512 usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_RD]);
513}
514
515static void
516ubsa_stop_read(struct ucom_softc *ucom)
517{
518 struct ubsa_softc *sc = ucom->sc_parent;
519
520 /* stop interrupt endpoint */
521 usbd_transfer_stop(sc->sc_xfer[UBSA_INTR_DT_RD]);
522
523 /* stop read endpoint */
524 usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_RD]);
525}
526
527static void
528ubsa_start_write(struct ucom_softc *ucom)
529{
530 struct ubsa_softc *sc = ucom->sc_parent;
531
532 usbd_transfer_start(sc->sc_xfer[UBSA_BULK_DT_WR]);
533}
534
535static void
536ubsa_stop_write(struct ucom_softc *ucom)
537{
538 struct ubsa_softc *sc = ucom->sc_parent;
539
540 usbd_transfer_stop(sc->sc_xfer[UBSA_BULK_DT_WR]);
541}
542
543static void
544ubsa_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
545{
546 struct ubsa_softc *sc = ucom->sc_parent;
547
548 DPRINTF("\n");
549
550 *lsr = sc->sc_lsr;
551 *msr = sc->sc_msr;
552}
553
554static void
555ubsa_write_callback(struct usb_xfer *xfer, usb_error_t error)
556{
557 struct ubsa_softc *sc = usbd_xfer_softc(xfer);
558 struct usb_page_cache *pc;
559 uint32_t actlen;
560
561 switch (USB_GET_STATE(xfer)) {
562 case USB_ST_SETUP:
563 case USB_ST_TRANSFERRED:
564tr_setup:
565 pc = usbd_xfer_get_frame(xfer, 0);
566 if (ucom_get_data(&sc->sc_ucom, pc, 0,
567 UBSA_BSIZE, &actlen)) {
568
569 usbd_xfer_set_frame_len(xfer, 0, actlen);
570 usbd_transfer_submit(xfer);
571 }
572 return;
573
574 default: /* Error */
575 if (error != USB_ERR_CANCELLED) {
576 /* try to clear stall first */
577 usbd_xfer_set_stall(xfer);
578 goto tr_setup;
579 }
580 return;
581
582 }
583}
584
585static void
586ubsa_read_callback(struct usb_xfer *xfer, usb_error_t error)
587{
588 struct ubsa_softc *sc = usbd_xfer_softc(xfer);
589 struct usb_page_cache *pc;
590 int actlen;
591
592 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
593
594 switch (USB_GET_STATE(xfer)) {
595 case USB_ST_TRANSFERRED:
596 pc = usbd_xfer_get_frame(xfer, 0);
597 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
598
599 case USB_ST_SETUP:
600tr_setup:
601 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
602 usbd_transfer_submit(xfer);
603 return;
604
605 default: /* Error */
606 if (error != USB_ERR_CANCELLED) {
607 /* try to clear stall first */
608 usbd_xfer_set_stall(xfer);
609 goto tr_setup;
610 }
611 return;
612
613 }
614}
615
616static void
617ubsa_intr_callback(struct usb_xfer *xfer, usb_error_t error)
618{
619 struct ubsa_softc *sc = usbd_xfer_softc(xfer);
620 struct usb_page_cache *pc;
621 uint8_t buf[4];
622 int actlen;
623
624 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
625
626 switch (USB_GET_STATE(xfer)) {
627 case USB_ST_TRANSFERRED:
628
629 if (actlen >= sizeof(buf)) {
630 pc = usbd_xfer_get_frame(xfer, 0);
631 usbd_copy_out(pc, 0, buf, sizeof(buf));
632
633 /*
634 * incidentally, Belkin adapter status bits match
635 * UART 16550 bits
636 */
637 sc->sc_lsr = buf[2];
638 sc->sc_msr = buf[3];
639
640 DPRINTF("lsr = 0x%02x, msr = 0x%02x\n",
641 sc->sc_lsr, sc->sc_msr);
642
643 ucom_status_change(&sc->sc_ucom);
644 } else {
645 DPRINTF("ignoring short packet, %d bytes\n", actlen);
646 }
647
648 case USB_ST_SETUP:
649tr_setup:
650 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
651 usbd_transfer_submit(xfer);
652 return;
653
654 default: /* Error */
655 if (error != USB_ERR_CANCELLED) {
656 /* try to clear stall first */
657 usbd_xfer_set_stall(xfer);
658 goto tr_setup;
659 }
660 return;
661
662 }
663}
664
665static void
666ubsa_poll(struct ucom_softc *ucom)
667{
668 struct ubsa_softc *sc = ucom->sc_parent;
669 usbd_transfer_poll(sc->sc_xfer, UBSA_N_TRANSFER);
670
671}