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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * NOTE: all function names beginning like "uftdi_cfg_" can only
34 * be called from within the config thread function !
38 * FTDI FT8U100AX serial adapter driver
41 #include <sys/stdint.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/module.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
56 #include <sys/serial.h>
58 #include <bus/u4b/usb.h>
59 #include <bus/u4b/usbdi.h>
60 #include <bus/u4b/usbdi_util.h>
61 #include <bus/u4b/usbdevs.h>
63 #define USB_DEBUG_VAR uftdi_debug
64 #include <bus/u4b/usb_debug.h>
65 #include <bus/u4b/usb_process.h>
67 #include <bus/u4b/serial/usb_serial.h>
68 #include <bus/u4b/serial/uftdi_reg.h>
71 static int uftdi_debug = 0;
73 static SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
74 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
75 &uftdi_debug, 0, "Debug level");
78 #define UFTDI_CONFIG_INDEX 0
79 #define UFTDI_IFACE_INDEX 0
81 #define UFTDI_OBUFSIZE 64 /* bytes, cannot be increased due to
91 struct ucom_super_softc sc_super_ucom;
92 struct ucom_softc sc_ucom;
94 struct usb_device *sc_udev;
95 struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER];
100 enum uftdi_type sc_type;
102 uint16_t sc_last_lcr;
104 uint8_t sc_iface_index;
112 struct uftdi_param_config {
122 static device_probe_t uftdi_probe;
123 static device_attach_t uftdi_attach;
124 static device_detach_t uftdi_detach;
126 static usb_callback_t uftdi_write_callback;
127 static usb_callback_t uftdi_read_callback;
129 static void uftdi_cfg_open(struct ucom_softc *);
130 static void uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t);
131 static void uftdi_cfg_set_rts(struct ucom_softc *, uint8_t);
132 static void uftdi_cfg_set_break(struct ucom_softc *, uint8_t);
133 static int uftdi_set_parm_soft(struct termios *,
134 struct uftdi_param_config *, uint8_t);
135 static int uftdi_pre_param(struct ucom_softc *, struct termios *);
136 static void uftdi_cfg_param(struct ucom_softc *, struct termios *);
137 static void uftdi_cfg_get_status(struct ucom_softc *, uint8_t *,
139 static void uftdi_start_read(struct ucom_softc *);
140 static void uftdi_stop_read(struct ucom_softc *);
141 static void uftdi_start_write(struct ucom_softc *);
142 static void uftdi_stop_write(struct ucom_softc *);
143 static uint8_t uftdi_8u232am_getrate(uint32_t, uint16_t *);
144 static void uftdi_poll(struct ucom_softc *ucom);
146 static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = {
148 [UFTDI_BULK_DT_WR] = {
150 .endpoint = UE_ADDR_ANY,
151 .direction = UE_DIR_OUT,
152 .bufsize = UFTDI_OBUFSIZE,
153 .flags = {.pipe_bof = 1,},
154 .callback = &uftdi_write_callback,
157 [UFTDI_BULK_DT_RD] = {
159 .endpoint = UE_ADDR_ANY,
160 .direction = UE_DIR_IN,
161 .bufsize = 0, /* use wMaxPacketSize */
162 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
163 .callback = &uftdi_read_callback,
167 static const struct ucom_callback uftdi_callback = {
168 .ucom_cfg_get_status = &uftdi_cfg_get_status,
169 .ucom_cfg_set_dtr = &uftdi_cfg_set_dtr,
170 .ucom_cfg_set_rts = &uftdi_cfg_set_rts,
171 .ucom_cfg_set_break = &uftdi_cfg_set_break,
172 .ucom_cfg_param = &uftdi_cfg_param,
173 .ucom_cfg_open = &uftdi_cfg_open,
174 .ucom_pre_param = &uftdi_pre_param,
175 .ucom_start_read = &uftdi_start_read,
176 .ucom_stop_read = &uftdi_stop_read,
177 .ucom_start_write = &uftdi_start_write,
178 .ucom_stop_write = &uftdi_stop_write,
179 .ucom_poll = &uftdi_poll,
182 static device_method_t uftdi_methods[] = {
183 /* Device interface */
184 DEVMETHOD(device_probe, uftdi_probe),
185 DEVMETHOD(device_attach, uftdi_attach),
186 DEVMETHOD(device_detach, uftdi_detach),
191 static devclass_t uftdi_devclass;
193 static driver_t uftdi_driver = {
195 .methods = uftdi_methods,
196 .size = sizeof(struct uftdi_softc),
199 DRIVER_MODULE(uftdi, uhub, uftdi_driver, uftdi_devclass, NULL, 0);
200 MODULE_DEPEND(uftdi, ucom, 1, 1, 1);
201 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
202 MODULE_VERSION(uftdi, 1);
204 static STRUCT_USB_HOST_ID uftdi_devs[] = {
205 #define UFTDI_DEV(v,p,t) \
206 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, UFTDI_TYPE_##t) }
207 UFTDI_DEV(ATMEL, STK541, 8U232AM),
208 UFTDI_DEV(DRESDENELEKTRONIK, SENSORTERMINALBOARD, 8U232AM),
209 UFTDI_DEV(DRESDENELEKTRONIK, WIRELESSHANDHELDTERMINAL, 8U232AM),
210 UFTDI_DEV(FALCOM, TWIST, 8U232AM),
211 UFTDI_DEV(FTDI, GAMMASCOUT, 8U232AM),
212 UFTDI_DEV(FTDI, SERIAL_8U100AX, SIO),
213 UFTDI_DEV(FTDI, SERIAL_2232C, 8U232AM),
214 UFTDI_DEV(FTDI, SERIAL_2232D, 8U232AM),
215 UFTDI_DEV(FTDI, BEAGLEBONE, 8U232AM),
216 UFTDI_DEV(FTDI, SERIAL_4232H, 8U232AM),
217 UFTDI_DEV(FTDI, SERIAL_8U232AM, 8U232AM),
218 UFTDI_DEV(FTDI, SERIAL_8U232AM4, 8U232AM),
219 UFTDI_DEV(FTDI, SEMC_DSS20, 8U232AM),
220 UFTDI_DEV(FTDI, CFA_631, 8U232AM),
221 UFTDI_DEV(FTDI, CFA_632, 8U232AM),
222 UFTDI_DEV(FTDI, CFA_633, 8U232AM),
223 UFTDI_DEV(FTDI, CFA_634, 8U232AM),
224 UFTDI_DEV(FTDI, CFA_635, 8U232AM),
225 UFTDI_DEV(FTDI, USB_UIRT, 8U232AM),
226 UFTDI_DEV(FTDI, USBSERIAL, 8U232AM),
227 UFTDI_DEV(FTDI, KBS, 8U232AM),
228 UFTDI_DEV(FTDI, MX2_3, 8U232AM),
229 UFTDI_DEV(FTDI, MX4_5, 8U232AM),
230 UFTDI_DEV(FTDI, LK202, 8U232AM),
231 UFTDI_DEV(FTDI, LK204, 8U232AM),
232 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13M, 8U232AM),
233 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13S, 8U232AM),
234 UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13U, 8U232AM),
235 UFTDI_DEV(FTDI, EISCOU, 8U232AM),
236 UFTDI_DEV(FTDI, UOPTBR, 8U232AM),
237 UFTDI_DEV(FTDI, EMCU2D, 8U232AM),
238 UFTDI_DEV(FTDI, PCMSFU, 8U232AM),
239 UFTDI_DEV(FTDI, EMCU2H, 8U232AM),
240 UFTDI_DEV(FTDI, MAXSTREAM, 8U232AM),
241 UFTDI_DEV(FTDI, CTI_USB_NANO_485, 8U232AM),
242 UFTDI_DEV(FTDI, CTI_USB_MINI_485, 8U232AM),
243 UFTDI_DEV(SIIG2, US2308, 8U232AM),
244 UFTDI_DEV(INTREPIDCS, VALUECAN, 8U232AM),
245 UFTDI_DEV(INTREPIDCS, NEOVI, 8U232AM),
246 UFTDI_DEV(BBELECTRONICS, USOTL4, 8U232AM),
247 UFTDI_DEV(MATRIXORBITAL, MOUA, 8U232AM),
248 UFTDI_DEV(MARVELL, SHEEVAPLUG, 8U232AM),
249 UFTDI_DEV(MELCO, PCOPRS1, 8U232AM),
250 UFTDI_DEV(RATOC, REXUSB60F, 8U232AM),
255 uftdi_probe(device_t dev)
257 struct usb_attach_arg *uaa = device_get_ivars(dev);
259 if (uaa->usb_mode != USB_MODE_HOST) {
262 if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
265 /* attach to all present interfaces */
267 return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
271 uftdi_attach(device_t dev)
273 struct usb_attach_arg *uaa = device_get_ivars(dev);
274 struct uftdi_softc *sc = device_get_softc(dev);
277 sc->sc_udev = uaa->device;
279 sc->sc_unit = device_get_unit(dev);
281 device_set_usb_desc(dev);
282 lockinit(&sc->sc_lock, "uftdi", 0, LK_CANRECURSE);
284 ksnprintf(sc->sc_name, sizeof(sc->sc_name),
285 "%s", device_get_nameunit(dev));
289 sc->sc_iface_index = uaa->info.bIfaceIndex;
290 sc->sc_type = USB_GET_DRIVER_INFO(uaa);
292 switch (sc->sc_type) {
296 case UFTDI_TYPE_8U232AM:
302 error = usbd_transfer_setup(uaa->device,
303 &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
304 UFTDI_N_TRANSFER, sc, &sc->sc_lock);
307 device_printf(dev, "allocating USB "
308 "transfers failed\n");
311 sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
313 /* clear stall at first run */
314 lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
315 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
316 usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
317 lockmgr(&sc->sc_lock, LK_RELEASE);
319 /* set a valid "lcr" value */
322 (FTDI_SIO_SET_DATA_STOP_BITS_2 |
323 FTDI_SIO_SET_DATA_PARITY_NONE |
324 FTDI_SIO_SET_DATA_BITS(8));
326 error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
327 &uftdi_callback, &sc->sc_lock);
331 ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
333 return (0); /* success */
341 uftdi_detach(device_t dev)
343 struct uftdi_softc *sc = device_get_softc(dev);
345 ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
346 usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
347 lockuninit(&sc->sc_lock);
353 uftdi_cfg_open(struct ucom_softc *ucom)
355 struct uftdi_softc *sc = ucom->sc_parent;
356 uint16_t wIndex = ucom->sc_portno;
357 struct usb_device_request req;
361 /* perform a full reset on the device */
363 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
364 req.bRequest = FTDI_SIO_RESET;
365 USETW(req.wValue, FTDI_SIO_RESET_SIO);
366 USETW(req.wIndex, wIndex);
367 USETW(req.wLength, 0);
368 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
369 &req, NULL, 0, 1000);
371 /* turn on RTS/CTS flow control */
373 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
374 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
375 USETW(req.wValue, 0);
376 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
377 USETW(req.wLength, 0);
378 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
379 &req, NULL, 0, 1000);
382 * NOTE: with the new UCOM layer there will always be a
383 * "uftdi_cfg_param()" call after "open()", so there is no need for
384 * "open()" to configure anything
389 uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
391 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
392 struct usb_page_cache *pc;
396 switch (USB_GET_STATE(xfer)) {
398 case USB_ST_TRANSFERRED:
400 pc = usbd_xfer_get_frame(xfer, 0);
401 if (ucom_get_data(&sc->sc_ucom, pc,
402 sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
405 if (sc->sc_hdrlen > 0) {
407 FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
408 usbd_copy_in(pc, 0, buf, 1);
410 usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
411 usbd_transfer_submit(xfer);
416 if (error != USB_ERR_CANCELLED) {
417 /* try to clear stall first */
418 usbd_xfer_set_stall(xfer);
426 uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
428 struct uftdi_softc *sc = usbd_xfer_softc(xfer);
429 struct usb_page_cache *pc;
436 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
438 switch (USB_GET_STATE(xfer)) {
439 case USB_ST_TRANSFERRED:
444 pc = usbd_xfer_get_frame(xfer, 0);
445 usbd_copy_out(pc, 0, buf, 2);
447 ftdi_msr = FTDI_GET_MSR(buf);
448 lsr = FTDI_GET_LSR(buf);
451 if (ftdi_msr & FTDI_SIO_CTS_MASK)
453 if (ftdi_msr & FTDI_SIO_DSR_MASK)
455 if (ftdi_msr & FTDI_SIO_RI_MASK)
457 if (ftdi_msr & FTDI_SIO_RLSD_MASK)
460 if ((sc->sc_msr != msr) ||
461 ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
462 DPRINTF("status change msr=0x%02x (0x%02x) "
463 "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
469 ucom_status_change(&sc->sc_ucom);
474 ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
478 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
479 usbd_transfer_submit(xfer);
483 if (error != USB_ERR_CANCELLED) {
484 /* try to clear stall first */
485 usbd_xfer_set_stall(xfer);
493 uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
495 struct uftdi_softc *sc = ucom->sc_parent;
496 uint16_t wIndex = ucom->sc_portno;
498 struct usb_device_request req;
500 wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
502 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
503 req.bRequest = FTDI_SIO_MODEM_CTRL;
504 USETW(req.wValue, wValue);
505 USETW(req.wIndex, wIndex);
506 USETW(req.wLength, 0);
507 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
508 &req, NULL, 0, 1000);
512 uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
514 struct uftdi_softc *sc = ucom->sc_parent;
515 uint16_t wIndex = ucom->sc_portno;
517 struct usb_device_request req;
519 wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
521 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
522 req.bRequest = FTDI_SIO_MODEM_CTRL;
523 USETW(req.wValue, wValue);
524 USETW(req.wIndex, wIndex);
525 USETW(req.wLength, 0);
526 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
527 &req, NULL, 0, 1000);
531 uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
533 struct uftdi_softc *sc = ucom->sc_parent;
534 uint16_t wIndex = ucom->sc_portno;
536 struct usb_device_request req;
539 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
541 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
544 wValue = sc->sc_last_lcr;
546 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
547 req.bRequest = FTDI_SIO_SET_DATA;
548 USETW(req.wValue, wValue);
549 USETW(req.wIndex, wIndex);
550 USETW(req.wLength, 0);
551 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
552 &req, NULL, 0, 1000);
556 uftdi_set_parm_soft(struct termios *t,
557 struct uftdi_param_config *cfg, uint8_t type)
559 memset(cfg, 0, sizeof(*cfg));
563 switch (t->c_ospeed) {
565 cfg->rate = ftdi_sio_b300;
568 cfg->rate = ftdi_sio_b600;
571 cfg->rate = ftdi_sio_b1200;
574 cfg->rate = ftdi_sio_b2400;
577 cfg->rate = ftdi_sio_b4800;
580 cfg->rate = ftdi_sio_b9600;
583 cfg->rate = ftdi_sio_b19200;
586 cfg->rate = ftdi_sio_b38400;
589 cfg->rate = ftdi_sio_b57600;
592 cfg->rate = ftdi_sio_b115200;
599 case UFTDI_TYPE_8U232AM:
600 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
606 if (t->c_cflag & CSTOPB)
607 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
609 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
611 if (t->c_cflag & PARENB) {
612 if (t->c_cflag & PARODD) {
613 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
615 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
618 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
621 switch (t->c_cflag & CSIZE) {
623 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
627 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
631 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
635 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
639 if (t->c_cflag & CRTSCTS) {
640 cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
641 } else if (t->c_iflag & (IXON | IXOFF)) {
642 cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
643 cfg->v_start = t->c_cc[VSTART];
644 cfg->v_stop = t->c_cc[VSTOP];
646 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
653 uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
655 struct uftdi_softc *sc = ucom->sc_parent;
656 struct uftdi_param_config cfg;
660 return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
664 uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
666 struct uftdi_softc *sc = ucom->sc_parent;
667 uint16_t wIndex = ucom->sc_portno;
668 struct uftdi_param_config cfg;
669 struct usb_device_request req;
671 if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
672 /* should not happen */
675 sc->sc_last_lcr = cfg.lcr;
679 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
680 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
681 USETW(req.wValue, cfg.rate);
682 USETW(req.wIndex, wIndex);
683 USETW(req.wLength, 0);
684 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
685 &req, NULL, 0, 1000);
687 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
688 req.bRequest = FTDI_SIO_SET_DATA;
689 USETW(req.wValue, cfg.lcr);
690 USETW(req.wIndex, wIndex);
691 USETW(req.wLength, 0);
692 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
693 &req, NULL, 0, 1000);
695 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
696 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
697 USETW2(req.wValue, cfg.v_stop, cfg.v_start);
698 USETW2(req.wIndex, cfg.v_flow, wIndex);
699 USETW(req.wLength, 0);
700 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
701 &req, NULL, 0, 1000);
705 uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
707 struct uftdi_softc *sc = ucom->sc_parent;
709 DPRINTF("msr=0x%02x lsr=0x%02x\n",
710 sc->sc_msr, sc->sc_lsr);
717 uftdi_start_read(struct ucom_softc *ucom)
719 struct uftdi_softc *sc = ucom->sc_parent;
721 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
725 uftdi_stop_read(struct ucom_softc *ucom)
727 struct uftdi_softc *sc = ucom->sc_parent;
729 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
733 uftdi_start_write(struct ucom_softc *ucom)
735 struct uftdi_softc *sc = ucom->sc_parent;
737 usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
741 uftdi_stop_write(struct ucom_softc *ucom)
743 struct uftdi_softc *sc = ucom->sc_parent;
745 usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
748 /*------------------------------------------------------------------------*
749 * uftdi_8u232am_getrate
754 *------------------------------------------------------------------------*/
756 uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
758 /* Table of the nearest even powers-of-2 for values 0..15. */
759 static const uint8_t roundoff[16] = {
760 0, 2, 2, 4, 4, 4, 8, 8,
761 8, 8, 8, 8, 16, 16, 16, 16,
767 if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
768 return (1); /* prevent numerical overflow */
770 /* Special cases for 2M and 3M. */
771 if ((speed >= ((3000000 * 100) / 103)) &&
772 (speed <= ((3000000 * 100) / 97))) {
776 if ((speed >= ((2000000 * 100) / 103)) &&
777 (speed <= ((2000000 * 100) / 97))) {
781 d = (FTDI_8U232AM_FREQ << 4) / speed;
782 d = (d & ~15) + roundoff[d & 15];
784 if (d < FTDI_8U232AM_MIN_DIV)
785 d = FTDI_8U232AM_MIN_DIV;
786 else if (d > FTDI_8U232AM_MAX_DIV)
787 d = FTDI_8U232AM_MAX_DIV;
790 * Calculate the frequency needed for "d" to exactly divide down to
791 * our target "speed", and check that the actual frequency is within
795 if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
796 (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
800 * Pack the divisor into the resultant value. The lower 14-bits
801 * hold the integral part, while the upper 2 bits encode the
802 * fractional component: either 0, 0.5, 0.25, or 0.125.
818 uftdi_poll(struct ucom_softc *ucom)
820 struct uftdi_softc *sc = ucom->sc_parent;
821 usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER);