1 /* $NetBSD: usb/uvscom.c,v 1.1 2002/03/19 15:08:42 augustss Exp $ */
7 * Copyright (c) 2001-2003, 2005 Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * uvscom: SUNTAC Slipper U VS-10U driver.
35 * Slipper U is a PC Card to USB converter for data communication card
36 * adapter. It supports DDI Pocket's Air H" C@rd, C@rd H" 64, NTT's P-in,
37 * P-in m@ater and various data communication card adapters.
41 #include <dev/usb/usb.h>
42 #include <dev/usb/usb_mfunc.h>
43 #include <dev/usb/usb_error.h>
44 #include <dev/usb/usb_cdc.h>
46 #define USB_DEBUG_VAR uvscom_debug
48 #include <dev/usb/usb_core.h>
49 #include <dev/usb/usb_debug.h>
50 #include <dev/usb/usb_process.h>
51 #include <dev/usb/usb_request.h>
52 #include <dev/usb/usb_lookup.h>
53 #include <dev/usb/usb_util.h>
54 #include <dev/usb/usb_busdma.h>
56 #include <dev/usb/serial/usb_serial.h>
59 static int uvscom_debug = 0;
61 SYSCTL_NODE(_hw_usb2, OID_AUTO, uvscom, CTLFLAG_RW, 0, "USB uvscom");
62 SYSCTL_INT(_hw_usb2_uvscom, OID_AUTO, debug, CTLFLAG_RW,
63 &uvscom_debug, 0, "Debug level");
66 #define UVSCOM_MODVER 1 /* module version */
68 #define UVSCOM_CONFIG_INDEX 0
69 #define UVSCOM_IFACE_INDEX 0
72 #define UVSCOM_SET_SPEED 0x10
73 #define UVSCOM_LINE_CTL 0x11
74 #define UVSCOM_SET_PARAM 0x12
75 #define UVSCOM_READ_STATUS 0xd0
76 #define UVSCOM_SHUTDOWN 0xe0
78 /* UVSCOM_SET_SPEED parameters */
79 #define UVSCOM_SPEED_150BPS 0x00
80 #define UVSCOM_SPEED_300BPS 0x01
81 #define UVSCOM_SPEED_600BPS 0x02
82 #define UVSCOM_SPEED_1200BPS 0x03
83 #define UVSCOM_SPEED_2400BPS 0x04
84 #define UVSCOM_SPEED_4800BPS 0x05
85 #define UVSCOM_SPEED_9600BPS 0x06
86 #define UVSCOM_SPEED_19200BPS 0x07
87 #define UVSCOM_SPEED_38400BPS 0x08
88 #define UVSCOM_SPEED_57600BPS 0x09
89 #define UVSCOM_SPEED_115200BPS 0x0a
91 /* UVSCOM_LINE_CTL parameters */
92 #define UVSCOM_BREAK 0x40
93 #define UVSCOM_RTS 0x02
94 #define UVSCOM_DTR 0x01
95 #define UVSCOM_LINE_INIT 0x08
97 /* UVSCOM_SET_PARAM parameters */
98 #define UVSCOM_DATA_MASK 0x03
99 #define UVSCOM_DATA_BIT_8 0x03
100 #define UVSCOM_DATA_BIT_7 0x02
101 #define UVSCOM_DATA_BIT_6 0x01
102 #define UVSCOM_DATA_BIT_5 0x00
104 #define UVSCOM_STOP_MASK 0x04
105 #define UVSCOM_STOP_BIT_2 0x04
106 #define UVSCOM_STOP_BIT_1 0x00
108 #define UVSCOM_PARITY_MASK 0x18
109 #define UVSCOM_PARITY_EVEN 0x18
110 #define UVSCOM_PARITY_ODD 0x08
111 #define UVSCOM_PARITY_NONE 0x00
114 #define UVSCOM_TXRDY 0x04
115 #define UVSCOM_RXRDY 0x01
117 #define UVSCOM_DCD 0x08
118 #define UVSCOM_NOCARD 0x04
119 #define UVSCOM_DSR 0x02
120 #define UVSCOM_CTS 0x01
121 #define UVSCOM_USTAT_MASK (UVSCOM_NOCARD | UVSCOM_DSR | UVSCOM_CTS)
123 #define UVSCOM_BULK_BUF_SIZE 1024 /* bytes */
132 struct uvscom_softc {
133 struct usb2_com_super_softc sc_super_ucom;
134 struct usb2_com_softc sc_ucom;
136 struct usb2_xfer *sc_xfer[UVSCOM_N_TRANSFER];
137 struct usb2_device *sc_udev;
140 uint16_t sc_line; /* line control register */
142 uint8_t sc_iface_no; /* interface number */
143 uint8_t sc_iface_index; /* interface index */
144 uint8_t sc_lsr; /* local status register */
145 uint8_t sc_msr; /* uvscom status register */
146 uint8_t sc_unit_status; /* unit status */
151 static device_probe_t uvscom_probe;
152 static device_attach_t uvscom_attach;
153 static device_detach_t uvscom_detach;
155 static usb2_callback_t uvscom_write_callback;
156 static usb2_callback_t uvscom_read_callback;
157 static usb2_callback_t uvscom_intr_callback;
159 static void uvscom_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
160 static void uvscom_cfg_set_rts(struct usb2_com_softc *, uint8_t);
161 static void uvscom_cfg_set_break(struct usb2_com_softc *, uint8_t);
162 static int uvscom_pre_param(struct usb2_com_softc *, struct termios *);
163 static void uvscom_cfg_param(struct usb2_com_softc *, struct termios *);
164 static int uvscom_pre_open(struct usb2_com_softc *);
165 static void uvscom_cfg_open(struct usb2_com_softc *);
166 static void uvscom_cfg_close(struct usb2_com_softc *);
167 static void uvscom_start_read(struct usb2_com_softc *);
168 static void uvscom_stop_read(struct usb2_com_softc *);
169 static void uvscom_start_write(struct usb2_com_softc *);
170 static void uvscom_stop_write(struct usb2_com_softc *);
171 static void uvscom_cfg_get_status(struct usb2_com_softc *, uint8_t *,
173 static void uvscom_cfg_write(struct uvscom_softc *, uint8_t, uint16_t);
174 static uint16_t uvscom_cfg_read_status(struct uvscom_softc *);
176 static const struct usb2_config uvscom_config[UVSCOM_N_TRANSFER] = {
178 [UVSCOM_BULK_DT_WR] = {
180 .endpoint = UE_ADDR_ANY,
181 .direction = UE_DIR_OUT,
182 .mh.bufsize = UVSCOM_BULK_BUF_SIZE,
183 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
184 .mh.callback = &uvscom_write_callback,
187 [UVSCOM_BULK_DT_RD] = {
189 .endpoint = UE_ADDR_ANY,
190 .direction = UE_DIR_IN,
191 .mh.bufsize = UVSCOM_BULK_BUF_SIZE,
192 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
193 .mh.callback = &uvscom_read_callback,
196 [UVSCOM_INTR_DT_RD] = {
197 .type = UE_INTERRUPT,
198 .endpoint = UE_ADDR_ANY,
199 .direction = UE_DIR_IN,
200 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
201 .mh.bufsize = 0, /* use wMaxPacketSize */
202 .mh.callback = &uvscom_intr_callback,
206 static const struct usb2_com_callback uvscom_callback = {
207 .usb2_com_cfg_get_status = &uvscom_cfg_get_status,
208 .usb2_com_cfg_set_dtr = &uvscom_cfg_set_dtr,
209 .usb2_com_cfg_set_rts = &uvscom_cfg_set_rts,
210 .usb2_com_cfg_set_break = &uvscom_cfg_set_break,
211 .usb2_com_cfg_param = &uvscom_cfg_param,
212 .usb2_com_cfg_open = &uvscom_cfg_open,
213 .usb2_com_cfg_close = &uvscom_cfg_close,
214 .usb2_com_pre_open = &uvscom_pre_open,
215 .usb2_com_pre_param = &uvscom_pre_param,
216 .usb2_com_start_read = &uvscom_start_read,
217 .usb2_com_stop_read = &uvscom_stop_read,
218 .usb2_com_start_write = &uvscom_start_write,
219 .usb2_com_stop_write = &uvscom_stop_write,
222 static const struct usb2_device_id uvscom_devs[] = {
223 /* SUNTAC U-Cable type A4 */
224 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_AS144L4, 0)},
225 /* SUNTAC U-Cable type D2 */
226 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_DS96L, 0)},
227 /* SUNTAC Ir-Trinity */
228 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_IS96U, 0)},
229 /* SUNTAC U-Cable type P1 */
230 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_PS64P1, 0)},
231 /* SUNTAC Slipper U */
232 {USB_VPI(USB_VENDOR_SUNTAC, USB_PRODUCT_SUNTAC_VS10U, 0)},
235 static device_method_t uvscom_methods[] = {
236 DEVMETHOD(device_probe, uvscom_probe),
237 DEVMETHOD(device_attach, uvscom_attach),
238 DEVMETHOD(device_detach, uvscom_detach),
242 static devclass_t uvscom_devclass;
244 static driver_t uvscom_driver = {
246 .methods = uvscom_methods,
247 .size = sizeof(struct uvscom_softc),
250 DRIVER_MODULE(uvscom, uhub, uvscom_driver, uvscom_devclass, NULL, 0);
251 MODULE_DEPEND(uvscom, ucom, 1, 1, 1);
252 MODULE_DEPEND(uvscom, usb, 1, 1, 1);
253 MODULE_VERSION(uvscom, UVSCOM_MODVER);
256 uvscom_probe(device_t dev)
258 struct usb2_attach_arg *uaa = device_get_ivars(dev);
260 if (uaa->usb2_mode != USB_MODE_HOST) {
263 if (uaa->info.bConfigIndex != UVSCOM_CONFIG_INDEX) {
266 if (uaa->info.bIfaceIndex != UVSCOM_IFACE_INDEX) {
269 return (usb2_lookup_id_by_uaa(uvscom_devs, sizeof(uvscom_devs), uaa));
273 uvscom_attach(device_t dev)
275 struct usb2_attach_arg *uaa = device_get_ivars(dev);
276 struct uvscom_softc *sc = device_get_softc(dev);
279 device_set_usb2_desc(dev);
280 mtx_init(&sc->sc_mtx, "uvscom", NULL, MTX_DEF);
282 sc->sc_udev = uaa->device;
284 DPRINTF("sc=%p\n", sc);
286 sc->sc_iface_no = uaa->info.bIfaceNum;
287 sc->sc_iface_index = UVSCOM_IFACE_INDEX;
289 error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
290 sc->sc_xfer, uvscom_config, UVSCOM_N_TRANSFER, sc, &sc->sc_mtx);
293 DPRINTF("could not allocate all USB transfers!\n");
296 sc->sc_line = UVSCOM_LINE_INIT;
298 /* clear stall at first run */
299 mtx_lock(&sc->sc_mtx);
300 usb2_transfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
301 usb2_transfer_set_stall(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
302 mtx_unlock(&sc->sc_mtx);
304 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
305 &uvscom_callback, &sc->sc_mtx);
309 /* start interrupt pipe */
310 mtx_lock(&sc->sc_mtx);
311 usb2_transfer_start(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
312 mtx_unlock(&sc->sc_mtx);
322 uvscom_detach(device_t dev)
324 struct uvscom_softc *sc = device_get_softc(dev);
326 DPRINTF("sc=%p\n", sc);
328 /* stop interrupt pipe */
330 if (sc->sc_xfer[UVSCOM_INTR_DT_RD])
331 usb2_transfer_stop(sc->sc_xfer[UVSCOM_INTR_DT_RD]);
333 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
334 usb2_transfer_unsetup(sc->sc_xfer, UVSCOM_N_TRANSFER);
335 mtx_destroy(&sc->sc_mtx);
341 uvscom_write_callback(struct usb2_xfer *xfer)
343 struct uvscom_softc *sc = xfer->priv_sc;
346 switch (USB_GET_STATE(xfer)) {
348 case USB_ST_TRANSFERRED:
350 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
351 UVSCOM_BULK_BUF_SIZE, &actlen)) {
353 xfer->frlengths[0] = actlen;
354 usb2_start_hardware(xfer);
359 if (xfer->error != USB_ERR_CANCELLED) {
360 /* try to clear stall first */
361 xfer->flags.stall_pipe = 1;
369 uvscom_read_callback(struct usb2_xfer *xfer)
371 struct uvscom_softc *sc = xfer->priv_sc;
373 switch (USB_GET_STATE(xfer)) {
374 case USB_ST_TRANSFERRED:
375 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
379 xfer->frlengths[0] = xfer->max_data_length;
380 usb2_start_hardware(xfer);
384 if (xfer->error != USB_ERR_CANCELLED) {
385 /* try to clear stall first */
386 xfer->flags.stall_pipe = 1;
394 uvscom_intr_callback(struct usb2_xfer *xfer)
396 struct uvscom_softc *sc = xfer->priv_sc;
399 switch (USB_GET_STATE(xfer)) {
400 case USB_ST_TRANSFERRED:
401 if (xfer->actlen >= 2) {
403 usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
407 sc->sc_unit_status = buf[1];
409 if (buf[0] & UVSCOM_TXRDY) {
410 sc->sc_lsr |= ULSR_TXRDY;
412 if (buf[0] & UVSCOM_RXRDY) {
413 sc->sc_lsr |= ULSR_RXRDY;
415 if (buf[1] & UVSCOM_CTS) {
416 sc->sc_msr |= SER_CTS;
418 if (buf[1] & UVSCOM_DSR) {
419 sc->sc_msr |= SER_DSR;
421 if (buf[1] & UVSCOM_DCD) {
422 sc->sc_msr |= SER_DCD;
425 * the UCOM layer will ignore this call if the TTY
428 usb2_com_status_change(&sc->sc_ucom);
432 xfer->frlengths[0] = xfer->max_data_length;
433 usb2_start_hardware(xfer);
437 if (xfer->error != USB_ERR_CANCELLED) {
438 /* try to clear stall first */
439 xfer->flags.stall_pipe = 1;
447 uvscom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
449 struct uvscom_softc *sc = ucom->sc_parent;
451 DPRINTF("onoff = %d\n", onoff);
454 sc->sc_line |= UVSCOM_DTR;
456 sc->sc_line &= ~UVSCOM_DTR;
458 uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
462 uvscom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
464 struct uvscom_softc *sc = ucom->sc_parent;
466 DPRINTF("onoff = %d\n", onoff);
469 sc->sc_line |= UVSCOM_RTS;
471 sc->sc_line &= ~UVSCOM_RTS;
473 uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
477 uvscom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
479 struct uvscom_softc *sc = ucom->sc_parent;
481 DPRINTF("onoff = %d\n", onoff);
484 sc->sc_line |= UVSCOM_BREAK;
486 sc->sc_line &= ~UVSCOM_BREAK;
488 uvscom_cfg_write(sc, UVSCOM_LINE_CTL, sc->sc_line);
492 uvscom_pre_param(struct usb2_com_softc *ucom, struct termios *t)
494 switch (t->c_ospeed) {
513 uvscom_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
515 struct uvscom_softc *sc = ucom->sc_parent;
520 switch (t->c_ospeed) {
522 value = UVSCOM_SPEED_150BPS;
525 value = UVSCOM_SPEED_300BPS;
528 value = UVSCOM_SPEED_600BPS;
531 value = UVSCOM_SPEED_1200BPS;
534 value = UVSCOM_SPEED_2400BPS;
537 value = UVSCOM_SPEED_4800BPS;
540 value = UVSCOM_SPEED_9600BPS;
543 value = UVSCOM_SPEED_19200BPS;
546 value = UVSCOM_SPEED_38400BPS;
549 value = UVSCOM_SPEED_57600BPS;
552 value = UVSCOM_SPEED_115200BPS;
558 uvscom_cfg_write(sc, UVSCOM_SET_SPEED, value);
562 if (t->c_cflag & CSTOPB) {
563 value |= UVSCOM_STOP_BIT_2;
565 if (t->c_cflag & PARENB) {
566 if (t->c_cflag & PARODD) {
567 value |= UVSCOM_PARITY_ODD;
569 value |= UVSCOM_PARITY_EVEN;
572 value |= UVSCOM_PARITY_NONE;
575 switch (t->c_cflag & CSIZE) {
577 value |= UVSCOM_DATA_BIT_5;
580 value |= UVSCOM_DATA_BIT_6;
583 value |= UVSCOM_DATA_BIT_7;
587 value |= UVSCOM_DATA_BIT_8;
591 uvscom_cfg_write(sc, UVSCOM_SET_PARAM, value);
595 uvscom_pre_open(struct usb2_com_softc *ucom)
597 struct uvscom_softc *sc = ucom->sc_parent;
599 DPRINTF("sc = %p\n", sc);
601 /* check if PC card was inserted */
603 if (sc->sc_unit_status & UVSCOM_NOCARD) {
604 DPRINTF("no PC card!\n");
611 uvscom_cfg_open(struct usb2_com_softc *ucom)
613 struct uvscom_softc *sc = ucom->sc_parent;
615 DPRINTF("sc = %p\n", sc);
617 uvscom_cfg_read_status(sc);
621 uvscom_cfg_close(struct usb2_com_softc *ucom)
623 struct uvscom_softc *sc = ucom->sc_parent;
625 DPRINTF("sc=%p\n", sc);
627 uvscom_cfg_write(sc, UVSCOM_SHUTDOWN, 0);
631 uvscom_start_read(struct usb2_com_softc *ucom)
633 struct uvscom_softc *sc = ucom->sc_parent;
635 usb2_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
639 uvscom_stop_read(struct usb2_com_softc *ucom)
641 struct uvscom_softc *sc = ucom->sc_parent;
643 usb2_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_RD]);
647 uvscom_start_write(struct usb2_com_softc *ucom)
649 struct uvscom_softc *sc = ucom->sc_parent;
651 usb2_transfer_start(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
655 uvscom_stop_write(struct usb2_com_softc *ucom)
657 struct uvscom_softc *sc = ucom->sc_parent;
659 usb2_transfer_stop(sc->sc_xfer[UVSCOM_BULK_DT_WR]);
663 uvscom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
665 struct uvscom_softc *sc = ucom->sc_parent;
672 uvscom_cfg_write(struct uvscom_softc *sc, uint8_t index, uint16_t value)
674 struct usb2_device_request req;
677 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
678 req.bRequest = index;
679 USETW(req.wValue, value);
680 USETW(req.wIndex, 0);
681 USETW(req.wLength, 0);
683 err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
684 &req, NULL, 0, 1000);
686 DPRINTFN(0, "device request failed, err=%s "
687 "(ignored)\n", usb2_errstr(err));
692 uvscom_cfg_read_status(struct uvscom_softc *sc)
694 struct usb2_device_request req;
698 req.bmRequestType = UT_READ_VENDOR_DEVICE;
699 req.bRequest = UVSCOM_READ_STATUS;
700 USETW(req.wValue, 0);
701 USETW(req.wIndex, 0);
702 USETW(req.wLength, 2);
704 err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
705 &req, data, 0, 1000);
707 DPRINTFN(0, "device request failed, err=%s "
708 "(ignored)\n", usb2_errstr(err));
710 return (data[0] | (data[1] << 8));