1 /* $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * NOTE: all function names beginning like "uftdi_cfg_" can only
44 * be called from within the config thread function !
48 * FTDI FT8U100AX serial adapter driver
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/linker_set.h>
60 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
76 #define USB_DEBUG_VAR uftdi_debug
77 #include <dev/usb/usb_debug.h>
78 #include <dev/usb/usb_process.h>
80 #include <dev/usb/serial/usb_serial.h>
81 #include <dev/usb/serial/uftdi_reg.h>
84 static int uftdi_debug = 0;
86 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
87 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
88 &uftdi_debug, 0, "Debug level");
91 #define UFTDI_CONFIG_INDEX 0
92 #define UFTDI_IFACE_INDEX 0
94 #define UFTDI_OBUFSIZE 64 /* bytes, cannot be increased due to
104 struct ucom_super_softc sc_super_ucom;
105 struct ucom_softc sc_ucom;
107 struct usb_device *sc_udev;
108 struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER];
113 enum uftdi_type sc_type;
115 uint16_t sc_last_lcr;
117 uint8_t sc_iface_index;
125 struct uftdi_param_config {
135 static device_probe_t uftdi_probe;
136 static device_attach_t uftdi_attach;
137 static device_detach_t uftdi_detach;
139 static usb_callback_t uftdi_write_callback;
140 static usb_callback_t uftdi_read_callback;
142 static void uftdi_cfg_open(struct ucom_softc *);
143 static void uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t);
144 static void uftdi_cfg_set_rts(struct ucom_softc *, uint8_t);
145 static void uftdi_cfg_set_break(struct ucom_softc *, uint8_t);
146 static int uftdi_set_parm_soft(struct termios *,
147 struct uftdi_param_config *, uint8_t);
148 static int uftdi_pre_param(struct ucom_softc *, struct termios *);
149 static void uftdi_cfg_param(struct ucom_softc *, struct termios *);
150 static void uftdi_cfg_get_status(struct ucom_softc *, uint8_t *,
152 static void uftdi_start_read(struct ucom_softc *);
153 static void uftdi_stop_read(struct ucom_softc *);
154 static void uftdi_start_write(struct ucom_softc *);
155 static void uftdi_stop_write(struct ucom_softc *);
156 static uint8_t uftdi_8u232am_getrate(uint32_t, uint16_t *);
157 static void uftdi_poll(struct ucom_softc *ucom);
159 static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = {
161 [UFTDI_BULK_DT_WR] = {
163 .endpoint = UE_ADDR_ANY,
164 .direction = UE_DIR_OUT,
165 .bufsize = UFTDI_OBUFSIZE,
166 .flags = {.pipe_bof = 1,},
167 .callback = &uftdi_write_callback,
170 [UFTDI_BULK_DT_RD] = {
172 .endpoint = UE_ADDR_ANY,
173 .direction = UE_DIR_IN,
174 .bufsize = 0, /* use wMaxPacketSize */
175 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
176 .callback = &uftdi_read_callback,
180 static const struct ucom_callback uftdi_callback = {
181 .ucom_cfg_get_status = &uftdi_cfg_get_status,
182 .ucom_cfg_set_dtr = &uftdi_cfg_set_dtr,
183 .ucom_cfg_set_rts = &uftdi_cfg_set_rts,
184 .ucom_cfg_set_break = &uftdi_cfg_set_break,
185 .ucom_cfg_param = &uftdi_cfg_param,
186 .ucom_cfg_open = &uftdi_cfg_open,
187 .ucom_pre_param = &uftdi_pre_param,
188 .ucom_start_read = &uftdi_start_read,
189 .ucom_stop_read = &uftdi_stop_read,
190 .ucom_start_write = &uftdi_start_write,
191 .ucom_stop_write = &uftdi_stop_write,
192 .ucom_poll = &uftdi_poll,
195 static device_method_t uftdi_methods[] = {
196 /* Device interface */
197 DEVMETHOD(device_probe, uftdi_probe),
198 DEVMETHOD(device_attach, uftdi_attach),
199 DEVMETHOD(device_detach, uftdi_detach),
204 static devclass_t uftdi_devclass;
206 static driver_t uftdi_driver = {
208 .methods = uftdi_methods,
209 .size = sizeof(struct uftdi_softc),
212 DRIVER_MODULE(uftdi, uhub, uftdi_driver, uftdi_devclass, NULL, 0);
213 MODULE_DEPEND(uftdi, ucom, 1, 1, 1);
214 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
215 MODULE_VERSION(uftdi, 1);
217 static struct usb_device_id uftdi_devs[] = {
218 #define UFTDI_DEV(v,p,t) \
219 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, UFTDI_TYPE_##t) }
220 UFTDI_DEV(ATMEL, STK541, 8U232AM),
221 UFTDI_DEV(DRESDENELEKTRONIK, SENSORTERMINALBOARD, 8U232AM),
222 UFTDI_DEV(DRESDENELEKTRONIK, WIRELESSHANDHELDTERMINAL, 8U232AM),
223 UFTDI_DEV(FTDI, GAMMASCOUT, 8U232AM),
224 UFTDI_DEV(FTDI, SERIAL_8U100AX, SIO),
225 UFTDI_DEV(FTDI, SERIAL_2232C, 8U232AM),
226 UFTDI_DEV(FTDI, SERIAL_2232D, 8U232AM),
227 UFTDI_DEV(FTDI, SERIAL_4232H, 8U232AM),
228 UFTDI_DEV(FTDI, SERIAL_8U232AM, 8U232AM),
229 UFTDI_DEV(FTDI, SERIAL_8U232AM4, 8U232AM),
230 UFTDI_DEV(FTDI, SEMC_DSS20, 8U232AM),
231 UFTDI_DEV(FTDI, CFA_631, 8U232AM),
232 UFTDI_DEV(FTDI, CFA_632, 8U232AM),
233 UFTDI_DEV(FTDI, CFA_633, 8U232AM),
234 UFTDI_DEV(FTDI, CFA_634, 8U232AM),
235 UFTDI_DEV(FTDI, CFA_635, 8U232AM),
236 UFTDI_DEV(FTDI, USBSERIAL, 8U232AM),
237 UFTDI_DEV(FTDI, KBS, 8U232AM),
238 UFTDI_DEV(FTDI, MX2_3, 8U232AM),
239 UFTDI_DEV(FTDI, MX4_5, 8U232AM),
240 UFTDI_DEV(FTDI, LK202, 8U232AM),
241 UFTDI_DEV(FTDI, LK204, 8U232AM),
242 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13M, 8U232AM),
243 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13S, 8U232AM),
244 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13U, 8U232AM),
245 UFTDI_DEV(FTDI, EISCOU, 8U232AM),
246 UFTDI_DEV(FTDI, UOPTBR, 8U232AM),
247 UFTDI_DEV(FTDI, EMCU2D, 8U232AM),
248 UFTDI_DEV(FTDI, PCMSFU, 8U232AM),
249 UFTDI_DEV(FTDI, EMCU2H, 8U232AM),
250 UFTDI_DEV(FTDI, MAXSTREAM, 8U232AM),
251 UFTDI_DEV(FTDI, CTI_USB_NANO_485, 8U232AM),
252 UFTDI_DEV(FTDI, CTI_USB_MINI_485, 8U232AM),
253 UFTDI_DEV(SIIG2, US2308, 8U232AM),
254 UFTDI_DEV(INTREPIDCS, VALUECAN, 8U232AM),
255 UFTDI_DEV(INTREPIDCS, NEOVI, 8U232AM),
256 UFTDI_DEV(BBELECTRONICS, USOTL4, 8U232AM),
257 UFTDI_DEV(MARVELL, SHEEVAPLUG, 8U232AM),
258 UFTDI_DEV(MELCO, PCOPRS1, 8U232AM),
259 UFTDI_DEV(RATOC, REXUSB60F, 8U232AM),
264 uftdi_probe(device_t dev)
266 struct usb_attach_arg *uaa = device_get_ivars(dev);
268 if (uaa->usb_mode != USB_MODE_HOST) {
271 if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
274 /* attach to all present interfaces */
276 return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
280 uftdi_attach(device_t dev)
282 struct usb_attach_arg *uaa = device_get_ivars(dev);
283 struct uftdi_softc *sc = device_get_softc(dev);
286 sc->sc_udev = uaa->device;
288 sc->sc_unit = device_get_unit(dev);
290 device_set_usb_desc(dev);
291 mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF);
293 snprintf(sc->sc_name, sizeof(sc->sc_name),
294 "%s", device_get_nameunit(dev));
298 sc->sc_iface_index = uaa->info.bIfaceIndex;
299 sc->sc_type = USB_GET_DRIVER_INFO(uaa);
301 switch (sc->sc_type) {
305 case UFTDI_TYPE_8U232AM:
311 error = usbd_transfer_setup(uaa->device,
312 &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
313 UFTDI_N_TRANSFER, sc, &sc->sc_mtx);
316 device_printf(dev, "allocating USB "
317 "transfers failed\n");
320 sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
322 /* clear stall at first run */
323 mtx_lock(&sc->sc_mtx);
324 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
325 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
326 mtx_unlock(&sc->sc_mtx);
328 /* set a valid "lcr" value */
331 (FTDI_SIO_SET_DATA_STOP_BITS_2 |
332 FTDI_SIO_SET_DATA_PARITY_NONE |
333 FTDI_SIO_SET_DATA_BITS(8));
335 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
336 &uftdi_callback, &sc->sc_mtx);
340 return (0); /* success */
348 uftdi_detach(device_t dev)
350 struct uftdi_softc *sc = device_get_softc(dev);
352 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
353 usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
354 mtx_destroy(&sc->sc_mtx);
360 uftdi_cfg_open(struct ucom_softc *ucom)
362 struct uftdi_softc *sc = ucom->sc_parent;
363 uint16_t wIndex = ucom->sc_portno;
364 struct usb_device_request req;
368 /* perform a full reset on the device */
370 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
371 req.bRequest = FTDI_SIO_RESET;
372 USETW(req.wValue, FTDI_SIO_RESET_SIO);
373 USETW(req.wIndex, wIndex);
374 USETW(req.wLength, 0);
375 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
376 &req, NULL, 0, 1000);
378 /* turn on RTS/CTS flow control */
380 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
381 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
382 USETW(req.wValue, 0);
383 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
384 USETW(req.wLength, 0);
385 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
386 &req, NULL, 0, 1000);
389 * NOTE: with the new UCOM layer there will always be a
390 * "uftdi_cfg_param()" call after "open()", so there is no need for
391 * "open()" to configure anything
396 uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
398 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
399 struct usb_page_cache *pc;
403 switch (USB_GET_STATE(xfer)) {
405 case USB_ST_TRANSFERRED:
407 pc = usbd_xfer_get_frame(xfer, 0);
408 if (ucom_get_data(&sc->sc_ucom, pc,
409 sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
412 if (sc->sc_hdrlen > 0) {
414 FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
415 usbd_copy_in(pc, 0, buf, 1);
417 usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
418 usbd_transfer_submit(xfer);
423 if (error != USB_ERR_CANCELLED) {
424 /* try to clear stall first */
425 usbd_xfer_set_stall(xfer);
433 uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
435 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
436 struct usb_page_cache *pc;
443 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
445 switch (USB_GET_STATE(xfer)) {
446 case USB_ST_TRANSFERRED:
451 pc = usbd_xfer_get_frame(xfer, 0);
452 usbd_copy_out(pc, 0, buf, 2);
454 ftdi_msr = FTDI_GET_MSR(buf);
455 lsr = FTDI_GET_LSR(buf);
458 if (ftdi_msr & FTDI_SIO_CTS_MASK)
460 if (ftdi_msr & FTDI_SIO_DSR_MASK)
462 if (ftdi_msr & FTDI_SIO_RI_MASK)
464 if (ftdi_msr & FTDI_SIO_RLSD_MASK)
467 if ((sc->sc_msr != msr) ||
468 ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
469 DPRINTF("status change msr=0x%02x (0x%02x) "
470 "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
476 ucom_status_change(&sc->sc_ucom);
481 ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
485 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
486 usbd_transfer_submit(xfer);
490 if (error != USB_ERR_CANCELLED) {
491 /* try to clear stall first */
492 usbd_xfer_set_stall(xfer);
500 uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
502 struct uftdi_softc *sc = ucom->sc_parent;
503 uint16_t wIndex = ucom->sc_portno;
505 struct usb_device_request req;
507 wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
509 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
510 req.bRequest = FTDI_SIO_MODEM_CTRL;
511 USETW(req.wValue, wValue);
512 USETW(req.wIndex, wIndex);
513 USETW(req.wLength, 0);
514 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
515 &req, NULL, 0, 1000);
519 uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
521 struct uftdi_softc *sc = ucom->sc_parent;
522 uint16_t wIndex = ucom->sc_portno;
524 struct usb_device_request req;
526 wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
528 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
529 req.bRequest = FTDI_SIO_MODEM_CTRL;
530 USETW(req.wValue, wValue);
531 USETW(req.wIndex, wIndex);
532 USETW(req.wLength, 0);
533 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
534 &req, NULL, 0, 1000);
538 uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
540 struct uftdi_softc *sc = ucom->sc_parent;
541 uint16_t wIndex = ucom->sc_portno;
543 struct usb_device_request req;
546 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
548 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
551 wValue = sc->sc_last_lcr;
553 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
554 req.bRequest = FTDI_SIO_SET_DATA;
555 USETW(req.wValue, wValue);
556 USETW(req.wIndex, wIndex);
557 USETW(req.wLength, 0);
558 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
559 &req, NULL, 0, 1000);
563 uftdi_set_parm_soft(struct termios *t,
564 struct uftdi_param_config *cfg, uint8_t type)
566 bzero(cfg, sizeof(*cfg));
570 switch (t->c_ospeed) {
572 cfg->rate = ftdi_sio_b300;
575 cfg->rate = ftdi_sio_b600;
578 cfg->rate = ftdi_sio_b1200;
581 cfg->rate = ftdi_sio_b2400;
584 cfg->rate = ftdi_sio_b4800;
587 cfg->rate = ftdi_sio_b9600;
590 cfg->rate = ftdi_sio_b19200;
593 cfg->rate = ftdi_sio_b38400;
596 cfg->rate = ftdi_sio_b57600;
599 cfg->rate = ftdi_sio_b115200;
606 case UFTDI_TYPE_8U232AM:
607 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
613 if (t->c_cflag & CSTOPB)
614 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
616 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
618 if (t->c_cflag & PARENB) {
619 if (t->c_cflag & PARODD) {
620 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
622 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
625 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
628 switch (t->c_cflag & CSIZE) {
630 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
634 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
638 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
642 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
646 if (t->c_cflag & CRTSCTS) {
647 cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
648 } else if (t->c_iflag & (IXON | IXOFF)) {
649 cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
650 cfg->v_start = t->c_cc[VSTART];
651 cfg->v_stop = t->c_cc[VSTOP];
653 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
660 uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
662 struct uftdi_softc *sc = ucom->sc_parent;
663 struct uftdi_param_config cfg;
667 return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
671 uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
673 struct uftdi_softc *sc = ucom->sc_parent;
674 uint16_t wIndex = ucom->sc_portno;
675 struct uftdi_param_config cfg;
676 struct usb_device_request req;
678 if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
679 /* should not happen */
682 sc->sc_last_lcr = cfg.lcr;
686 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
687 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
688 USETW(req.wValue, cfg.rate);
689 USETW(req.wIndex, wIndex);
690 USETW(req.wLength, 0);
691 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
692 &req, NULL, 0, 1000);
694 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
695 req.bRequest = FTDI_SIO_SET_DATA;
696 USETW(req.wValue, cfg.lcr);
697 USETW(req.wIndex, wIndex);
698 USETW(req.wLength, 0);
699 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
700 &req, NULL, 0, 1000);
702 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
703 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
704 USETW2(req.wValue, cfg.v_stop, cfg.v_start);
705 USETW2(req.wIndex, cfg.v_flow, wIndex);
706 USETW(req.wLength, 0);
707 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
708 &req, NULL, 0, 1000);
712 uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
714 struct uftdi_softc *sc = ucom->sc_parent;
716 DPRINTF("msr=0x%02x lsr=0x%02x\n",
717 sc->sc_msr, sc->sc_lsr);
724 uftdi_start_read(struct ucom_softc *ucom)
726 struct uftdi_softc *sc = ucom->sc_parent;
728 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
732 uftdi_stop_read(struct ucom_softc *ucom)
734 struct uftdi_softc *sc = ucom->sc_parent;
736 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
740 uftdi_start_write(struct ucom_softc *ucom)
742 struct uftdi_softc *sc = ucom->sc_parent;
744 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
748 uftdi_stop_write(struct ucom_softc *ucom)
750 struct uftdi_softc *sc = ucom->sc_parent;
752 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
755 /*------------------------------------------------------------------------*
756 * uftdi_8u232am_getrate
761 *------------------------------------------------------------------------*/
763 uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
765 /* Table of the nearest even powers-of-2 for values 0..15. */
766 static const uint8_t roundoff[16] = {
767 0, 2, 2, 4, 4, 4, 8, 8,
768 8, 8, 8, 8, 16, 16, 16, 16,
774 if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
775 return (1); /* prevent numerical overflow */
777 /* Special cases for 2M and 3M. */
778 if ((speed >= ((3000000 * 100) / 103)) &&
779 (speed <= ((3000000 * 100) / 97))) {
783 if ((speed >= ((2000000 * 100) / 103)) &&
784 (speed <= ((2000000 * 100) / 97))) {
788 d = (FTDI_8U232AM_FREQ << 4) / speed;
789 d = (d & ~15) + roundoff[d & 15];
791 if (d < FTDI_8U232AM_MIN_DIV)
792 d = FTDI_8U232AM_MIN_DIV;
793 else if (d > FTDI_8U232AM_MAX_DIV)
794 d = FTDI_8U232AM_MAX_DIV;
797 * Calculate the frequency needed for "d" to exactly divide down to
798 * our target "speed", and check that the actual frequency is within
802 if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
803 (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
807 * Pack the divisor into the resultant value. The lower 14-bits
808 * hold the integral part, while the upper 2 bits encode the
809 * fractional component: either 0, 0.5, 0.25, or 0.125.
825 uftdi_poll(struct ucom_softc *ucom)
827 struct uftdi_softc *sc = ucom->sc_parent;
828 usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER);