uftdi.c revision 197570
1/*	$NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $	*/
2
3/*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
25 *
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.
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/dev/usb/serial/uftdi.c 197570 2009-09-28 08:13:50Z thompsa $");
41
42/*
43 * NOTE: all function names beginning like "uftdi_cfg_" can only
44 * be called from within the config thread function !
45 */
46
47/*
48 * FTDI FT8U100AX serial adapter driver
49 */
50
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>
58#include <sys/bus.h>
59#include <sys/linker_set.h>
60#include <sys/module.h>
61#include <sys/lock.h>
62#include <sys/mutex.h>
63#include <sys/condvar.h>
64#include <sys/sysctl.h>
65#include <sys/sx.h>
66#include <sys/unistd.h>
67#include <sys/callout.h>
68#include <sys/malloc.h>
69#include <sys/priv.h>
70
71#include <dev/usb/usb.h>
72#include <dev/usb/usbdi.h>
73#include <dev/usb/usbdi_util.h>
74#include "usbdevs.h"
75
76#define	USB_DEBUG_VAR uftdi_debug
77#include <dev/usb/usb_debug.h>
78#include <dev/usb/usb_process.h>
79
80#include <dev/usb/serial/usb_serial.h>
81#include <dev/usb/serial/uftdi_reg.h>
82
83#if USB_DEBUG
84static int uftdi_debug = 0;
85
86SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
87SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
88    &uftdi_debug, 0, "Debug level");
89#endif
90
91#define	UFTDI_CONFIG_INDEX	0
92#define	UFTDI_IFACE_INDEX	0
93
94#define	UFTDI_IBUFSIZE 64		/* bytes, maximum number of bytes per
95					 * frame */
96#define	UFTDI_OBUFSIZE 64		/* bytes, cannot be increased due to
97					 * do size encoding */
98
99enum {
100	UFTDI_BULK_DT_WR,
101	UFTDI_BULK_DT_RD,
102	UFTDI_N_TRANSFER,
103};
104
105struct uftdi_softc {
106	struct ucom_super_softc sc_super_ucom;
107	struct ucom_softc sc_ucom;
108
109	struct usb_device *sc_udev;
110	struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER];
111	device_t sc_dev;
112	struct mtx sc_mtx;
113
114	uint32_t sc_unit;
115	enum uftdi_type sc_type;
116
117	uint16_t sc_last_lcr;
118
119	uint8_t	sc_iface_index;
120	uint8_t	sc_hdrlen;
121	uint8_t	sc_msr;
122	uint8_t	sc_lsr;
123
124	uint8_t	sc_name[16];
125};
126
127struct uftdi_param_config {
128	uint16_t rate;
129	uint16_t lcr;
130	uint8_t	v_start;
131	uint8_t	v_stop;
132	uint8_t	v_flow;
133};
134
135/* prototypes */
136
137static device_probe_t uftdi_probe;
138static device_attach_t uftdi_attach;
139static device_detach_t uftdi_detach;
140
141static usb_callback_t uftdi_write_callback;
142static usb_callback_t uftdi_read_callback;
143
144static void	uftdi_cfg_open(struct ucom_softc *);
145static void	uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t);
146static void	uftdi_cfg_set_rts(struct ucom_softc *, uint8_t);
147static void	uftdi_cfg_set_break(struct ucom_softc *, uint8_t);
148static int	uftdi_set_parm_soft(struct termios *,
149		    struct uftdi_param_config *, uint8_t);
150static int	uftdi_pre_param(struct ucom_softc *, struct termios *);
151static void	uftdi_cfg_param(struct ucom_softc *, struct termios *);
152static void	uftdi_cfg_get_status(struct ucom_softc *, uint8_t *,
153		    uint8_t *);
154static void	uftdi_start_read(struct ucom_softc *);
155static void	uftdi_stop_read(struct ucom_softc *);
156static void	uftdi_start_write(struct ucom_softc *);
157static void	uftdi_stop_write(struct ucom_softc *);
158static uint8_t	uftdi_8u232am_getrate(uint32_t, uint16_t *);
159static void	uftdi_poll(struct ucom_softc *ucom);
160
161static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = {
162
163	[UFTDI_BULK_DT_WR] = {
164		.type = UE_BULK,
165		.endpoint = UE_ADDR_ANY,
166		.direction = UE_DIR_OUT,
167		.bufsize = UFTDI_OBUFSIZE,
168		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
169		.callback = &uftdi_write_callback,
170	},
171
172	[UFTDI_BULK_DT_RD] = {
173		.type = UE_BULK,
174		.endpoint = UE_ADDR_ANY,
175		.direction = UE_DIR_IN,
176		.bufsize = UFTDI_IBUFSIZE,
177		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
178		.callback = &uftdi_read_callback,
179	},
180};
181
182static const struct ucom_callback uftdi_callback = {
183	.ucom_cfg_get_status = &uftdi_cfg_get_status,
184	.ucom_cfg_set_dtr = &uftdi_cfg_set_dtr,
185	.ucom_cfg_set_rts = &uftdi_cfg_set_rts,
186	.ucom_cfg_set_break = &uftdi_cfg_set_break,
187	.ucom_cfg_param = &uftdi_cfg_param,
188	.ucom_cfg_open = &uftdi_cfg_open,
189	.ucom_pre_param = &uftdi_pre_param,
190	.ucom_start_read = &uftdi_start_read,
191	.ucom_stop_read = &uftdi_stop_read,
192	.ucom_start_write = &uftdi_start_write,
193	.ucom_stop_write = &uftdi_stop_write,
194	.ucom_poll = &uftdi_poll,
195};
196
197static device_method_t uftdi_methods[] = {
198	/* Device interface */
199	DEVMETHOD(device_probe, uftdi_probe),
200	DEVMETHOD(device_attach, uftdi_attach),
201	DEVMETHOD(device_detach, uftdi_detach),
202
203	{0, 0}
204};
205
206static devclass_t uftdi_devclass;
207
208static driver_t uftdi_driver = {
209	.name = "uftdi",
210	.methods = uftdi_methods,
211	.size = sizeof(struct uftdi_softc),
212};
213
214DRIVER_MODULE(uftdi, uhub, uftdi_driver, uftdi_devclass, NULL, 0);
215MODULE_DEPEND(uftdi, ucom, 1, 1, 1);
216MODULE_DEPEND(uftdi, usb, 1, 1, 1);
217
218static struct usb_device_id uftdi_devs[] = {
219	{USB_VPI(USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_STK541, UFTDI_TYPE_8U232AM)},
220	{USB_VPI(USB_VENDOR_DRESDENELEKTRONIK, USB_PRODUCT_DRESDENELEKTRONIK_SENSORTERMINALBOARD, UFTDI_TYPE_8U232AM)},
221	{USB_VPI(USB_VENDOR_DRESDENELEKTRONIK, USB_PRODUCT_DRESDENELEKTRONIK_WIRELESSHANDHELDTERMINAL, UFTDI_TYPE_8U232AM)},
222	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX, UFTDI_TYPE_SIO)},
223	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C, UFTDI_TYPE_8U232AM)},
224	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM, UFTDI_TYPE_8U232AM)},
225	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM4, UFTDI_TYPE_8U232AM)},
226	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20, UFTDI_TYPE_8U232AM)},
227	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_631, UFTDI_TYPE_8U232AM)},
228	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_632, UFTDI_TYPE_8U232AM)},
229	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_633, UFTDI_TYPE_8U232AM)},
230	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_634, UFTDI_TYPE_8U232AM)},
231	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_635, UFTDI_TYPE_8U232AM)},
232	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_USBSERIAL, UFTDI_TYPE_8U232AM)},
233	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MX2_3, UFTDI_TYPE_8U232AM)},
234	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MX4_5, UFTDI_TYPE_8U232AM)},
235	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LK202, UFTDI_TYPE_8U232AM)},
236	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LK204, UFTDI_TYPE_8U232AM)},
237	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13M, UFTDI_TYPE_8U232AM)},
238	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13S, UFTDI_TYPE_8U232AM)},
239	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13U, UFTDI_TYPE_8U232AM)},
240	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EISCOU, UFTDI_TYPE_8U232AM)},
241	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_UOPTBR, UFTDI_TYPE_8U232AM)},
242	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2D, UFTDI_TYPE_8U232AM)},
243	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_PCMSFU, UFTDI_TYPE_8U232AM)},
244	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2H, UFTDI_TYPE_8U232AM)},
245	{USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM, UFTDI_TYPE_8U232AM)},
246	{USB_VPI(USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308, UFTDI_TYPE_8U232AM)},
247	{USB_VPI(USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN, UFTDI_TYPE_8U232AM)},
248	{USB_VPI(USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI, UFTDI_TYPE_8U232AM)},
249	{USB_VPI(USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4, UFTDI_TYPE_8U232AM)},
250	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1, UFTDI_TYPE_8U232AM)},
251};
252
253static int
254uftdi_probe(device_t dev)
255{
256	struct usb_attach_arg *uaa = device_get_ivars(dev);
257
258	if (uaa->usb_mode != USB_MODE_HOST) {
259		return (ENXIO);
260	}
261	if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
262		return (ENXIO);
263	}
264	/* attach to all present interfaces */
265
266	return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
267}
268
269static int
270uftdi_attach(device_t dev)
271{
272	struct usb_attach_arg *uaa = device_get_ivars(dev);
273	struct uftdi_softc *sc = device_get_softc(dev);
274	int error;
275
276	sc->sc_udev = uaa->device;
277	sc->sc_dev = dev;
278	sc->sc_unit = device_get_unit(dev);
279
280	device_set_usb_desc(dev);
281	mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF);
282
283	snprintf(sc->sc_name, sizeof(sc->sc_name),
284	    "%s", device_get_nameunit(dev));
285
286	DPRINTF("\n");
287
288	sc->sc_iface_index = uaa->info.bIfaceIndex;
289	sc->sc_type = USB_GET_DRIVER_INFO(uaa);
290
291	switch (sc->sc_type) {
292	case UFTDI_TYPE_SIO:
293		sc->sc_hdrlen = 1;
294		break;
295	case UFTDI_TYPE_8U232AM:
296	default:
297		sc->sc_hdrlen = 0;
298		break;
299	}
300
301	error = usbd_transfer_setup(uaa->device,
302	    &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
303	    UFTDI_N_TRANSFER, sc, &sc->sc_mtx);
304
305	if (error) {
306		device_printf(dev, "allocating USB "
307		    "transfers failed!\n");
308		goto detach;
309	}
310	sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
311
312	/* clear stall at first run */
313	mtx_lock(&sc->sc_mtx);
314	usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
315	usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
316	mtx_unlock(&sc->sc_mtx);
317
318	/* set a valid "lcr" value */
319
320	sc->sc_last_lcr =
321	    (FTDI_SIO_SET_DATA_STOP_BITS_2 |
322	    FTDI_SIO_SET_DATA_PARITY_NONE |
323	    FTDI_SIO_SET_DATA_BITS(8));
324
325	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
326	    &uftdi_callback, &sc->sc_mtx);
327	if (error) {
328		goto detach;
329	}
330	return (0);			/* success */
331
332detach:
333	uftdi_detach(dev);
334	return (ENXIO);
335}
336
337static int
338uftdi_detach(device_t dev)
339{
340	struct uftdi_softc *sc = device_get_softc(dev);
341
342	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
343	usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
344	mtx_destroy(&sc->sc_mtx);
345
346	return (0);
347}
348
349static void
350uftdi_cfg_open(struct ucom_softc *ucom)
351{
352	struct uftdi_softc *sc = ucom->sc_parent;
353	uint16_t wIndex = ucom->sc_portno;
354	struct usb_device_request req;
355
356	DPRINTF("");
357
358	/* perform a full reset on the device */
359
360	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
361	req.bRequest = FTDI_SIO_RESET;
362	USETW(req.wValue, FTDI_SIO_RESET_SIO);
363	USETW(req.wIndex, wIndex);
364	USETW(req.wLength, 0);
365	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
366	    &req, NULL, 0, 1000);
367
368	/* turn on RTS/CTS flow control */
369
370	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
371	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
372	USETW(req.wValue, 0);
373	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
374	USETW(req.wLength, 0);
375	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
376	    &req, NULL, 0, 1000);
377
378	/*
379	 * NOTE: with the new UCOM layer there will always be a
380	 * "uftdi_cfg_param()" call after "open()", so there is no need for
381	 * "open()" to configure anything
382	 */
383}
384
385static void
386uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
387{
388	struct uftdi_softc *sc = usbd_xfer_softc(xfer);
389	struct usb_page_cache *pc;
390	uint32_t actlen;
391	uint8_t buf[1];
392
393	switch (USB_GET_STATE(xfer)) {
394	case USB_ST_SETUP:
395	case USB_ST_TRANSFERRED:
396tr_setup:
397		pc = usbd_xfer_get_frame(xfer, 0);
398		if (ucom_get_data(&sc->sc_ucom, pc,
399		    sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
400		    &actlen)) {
401
402			if (sc->sc_hdrlen > 0) {
403				buf[0] =
404				    FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
405				usbd_copy_in(pc, 0, buf, 1);
406			}
407			usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
408			usbd_transfer_submit(xfer);
409		}
410		return;
411
412	default:			/* Error */
413		if (error != USB_ERR_CANCELLED) {
414			/* try to clear stall first */
415			usbd_xfer_set_stall(xfer);
416			goto tr_setup;
417		}
418		return;
419	}
420}
421
422static void
423uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
424{
425	struct uftdi_softc *sc = usbd_xfer_softc(xfer);
426	struct usb_page_cache *pc;
427	uint8_t buf[2];
428	uint8_t ftdi_msr;
429	uint8_t msr;
430	uint8_t lsr;
431	int actlen;
432
433	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
434
435	switch (USB_GET_STATE(xfer)) {
436	case USB_ST_TRANSFERRED:
437
438		if (actlen < 2) {
439			goto tr_setup;
440		}
441		pc = usbd_xfer_get_frame(xfer, 0);
442		usbd_copy_out(pc, 0, buf, 2);
443
444		ftdi_msr = FTDI_GET_MSR(buf);
445		lsr = FTDI_GET_LSR(buf);
446
447		msr = 0;
448		if (ftdi_msr & FTDI_SIO_CTS_MASK)
449			msr |= SER_CTS;
450		if (ftdi_msr & FTDI_SIO_DSR_MASK)
451			msr |= SER_DSR;
452		if (ftdi_msr & FTDI_SIO_RI_MASK)
453			msr |= SER_RI;
454		if (ftdi_msr & FTDI_SIO_RLSD_MASK)
455			msr |= SER_DCD;
456
457		if ((sc->sc_msr != msr) ||
458		    ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
459			DPRINTF("status change msr=0x%02x (0x%02x) "
460			    "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
461			    lsr, sc->sc_lsr);
462
463			sc->sc_msr = msr;
464			sc->sc_lsr = lsr;
465
466			ucom_status_change(&sc->sc_ucom);
467		}
468		actlen -= 2;
469
470		if (actlen > 0) {
471			ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
472		}
473	case USB_ST_SETUP:
474tr_setup:
475		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
476		usbd_transfer_submit(xfer);
477		return;
478
479	default:			/* Error */
480		if (error != USB_ERR_CANCELLED) {
481			/* try to clear stall first */
482			usbd_xfer_set_stall(xfer);
483			goto tr_setup;
484		}
485		return;
486	}
487}
488
489static void
490uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
491{
492	struct uftdi_softc *sc = ucom->sc_parent;
493	uint16_t wIndex = ucom->sc_portno;
494	uint16_t wValue;
495	struct usb_device_request req;
496
497	wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
498
499	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
500	req.bRequest = FTDI_SIO_MODEM_CTRL;
501	USETW(req.wValue, wValue);
502	USETW(req.wIndex, wIndex);
503	USETW(req.wLength, 0);
504	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
505	    &req, NULL, 0, 1000);
506}
507
508static void
509uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
510{
511	struct uftdi_softc *sc = ucom->sc_parent;
512	uint16_t wIndex = ucom->sc_portno;
513	uint16_t wValue;
514	struct usb_device_request req;
515
516	wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
517
518	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
519	req.bRequest = FTDI_SIO_MODEM_CTRL;
520	USETW(req.wValue, wValue);
521	USETW(req.wIndex, wIndex);
522	USETW(req.wLength, 0);
523	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
524	    &req, NULL, 0, 1000);
525}
526
527static void
528uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
529{
530	struct uftdi_softc *sc = ucom->sc_parent;
531	uint16_t wIndex = ucom->sc_portno;
532	uint16_t wValue;
533	struct usb_device_request req;
534
535	if (onoff) {
536		sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
537	} else {
538		sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
539	}
540
541	wValue = sc->sc_last_lcr;
542
543	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
544	req.bRequest = FTDI_SIO_SET_DATA;
545	USETW(req.wValue, wValue);
546	USETW(req.wIndex, wIndex);
547	USETW(req.wLength, 0);
548	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
549	    &req, NULL, 0, 1000);
550}
551
552static int
553uftdi_set_parm_soft(struct termios *t,
554    struct uftdi_param_config *cfg, uint8_t type)
555{
556	bzero(cfg, sizeof(*cfg));
557
558	switch (type) {
559	case UFTDI_TYPE_SIO:
560		switch (t->c_ospeed) {
561		case 300:
562			cfg->rate = ftdi_sio_b300;
563			break;
564		case 600:
565			cfg->rate = ftdi_sio_b600;
566			break;
567		case 1200:
568			cfg->rate = ftdi_sio_b1200;
569			break;
570		case 2400:
571			cfg->rate = ftdi_sio_b2400;
572			break;
573		case 4800:
574			cfg->rate = ftdi_sio_b4800;
575			break;
576		case 9600:
577			cfg->rate = ftdi_sio_b9600;
578			break;
579		case 19200:
580			cfg->rate = ftdi_sio_b19200;
581			break;
582		case 38400:
583			cfg->rate = ftdi_sio_b38400;
584			break;
585		case 57600:
586			cfg->rate = ftdi_sio_b57600;
587			break;
588		case 115200:
589			cfg->rate = ftdi_sio_b115200;
590			break;
591		default:
592			return (EINVAL);
593		}
594		break;
595
596	case UFTDI_TYPE_8U232AM:
597		if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
598			return (EINVAL);
599		}
600		break;
601	}
602
603	if (t->c_cflag & CSTOPB)
604		cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
605	else
606		cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
607
608	if (t->c_cflag & PARENB) {
609		if (t->c_cflag & PARODD) {
610			cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
611		} else {
612			cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
613		}
614	} else {
615		cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
616	}
617
618	switch (t->c_cflag & CSIZE) {
619	case CS5:
620		cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
621		break;
622
623	case CS6:
624		cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
625		break;
626
627	case CS7:
628		cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
629		break;
630
631	case CS8:
632		cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
633		break;
634	}
635
636	if (t->c_cflag & CRTSCTS) {
637		cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
638	} else if (t->c_iflag & (IXON | IXOFF)) {
639		cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
640		cfg->v_start = t->c_cc[VSTART];
641		cfg->v_stop = t->c_cc[VSTOP];
642	} else {
643		cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
644	}
645
646	return (0);
647}
648
649static int
650uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
651{
652	struct uftdi_softc *sc = ucom->sc_parent;
653	struct uftdi_param_config cfg;
654
655	DPRINTF("\n");
656
657	return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
658}
659
660static void
661uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
662{
663	struct uftdi_softc *sc = ucom->sc_parent;
664	uint16_t wIndex = ucom->sc_portno;
665	struct uftdi_param_config cfg;
666	struct usb_device_request req;
667
668	if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
669		/* should not happen */
670		return;
671	}
672	sc->sc_last_lcr = cfg.lcr;
673
674	DPRINTF("\n");
675
676	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
677	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
678	USETW(req.wValue, cfg.rate);
679	USETW(req.wIndex, wIndex);
680	USETW(req.wLength, 0);
681	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
682	    &req, NULL, 0, 1000);
683
684	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
685	req.bRequest = FTDI_SIO_SET_DATA;
686	USETW(req.wValue, cfg.lcr);
687	USETW(req.wIndex, wIndex);
688	USETW(req.wLength, 0);
689	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
690	    &req, NULL, 0, 1000);
691
692	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
693	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
694	USETW2(req.wValue, cfg.v_stop, cfg.v_start);
695	USETW2(req.wIndex, cfg.v_flow, wIndex);
696	USETW(req.wLength, 0);
697	ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
698	    &req, NULL, 0, 1000);
699}
700
701static void
702uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
703{
704	struct uftdi_softc *sc = ucom->sc_parent;
705
706	DPRINTF("msr=0x%02x lsr=0x%02x\n",
707	    sc->sc_msr, sc->sc_lsr);
708
709	*msr = sc->sc_msr;
710	*lsr = sc->sc_lsr;
711}
712
713static void
714uftdi_start_read(struct ucom_softc *ucom)
715{
716	struct uftdi_softc *sc = ucom->sc_parent;
717
718	usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
719}
720
721static void
722uftdi_stop_read(struct ucom_softc *ucom)
723{
724	struct uftdi_softc *sc = ucom->sc_parent;
725
726	usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
727}
728
729static void
730uftdi_start_write(struct ucom_softc *ucom)
731{
732	struct uftdi_softc *sc = ucom->sc_parent;
733
734	usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
735}
736
737static void
738uftdi_stop_write(struct ucom_softc *ucom)
739{
740	struct uftdi_softc *sc = ucom->sc_parent;
741
742	usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
743}
744
745/*------------------------------------------------------------------------*
746 *	uftdi_8u232am_getrate
747 *
748 * Return values:
749 *    0: Success
750 * Else: Failure
751 *------------------------------------------------------------------------*/
752static uint8_t
753uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
754{
755	/* Table of the nearest even powers-of-2 for values 0..15. */
756	static const uint8_t roundoff[16] = {
757		0, 2, 2, 4, 4, 4, 8, 8,
758		8, 8, 8, 8, 16, 16, 16, 16,
759	};
760	uint32_t d;
761	uint32_t freq;
762	uint16_t result;
763
764	if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
765		return (1);		/* prevent numerical overflow */
766
767	/* Special cases for 2M and 3M. */
768	if ((speed >= ((3000000 * 100) / 103)) &&
769	    (speed <= ((3000000 * 100) / 97))) {
770		result = 0;
771		goto done;
772	}
773	if ((speed >= ((2000000 * 100) / 103)) &&
774	    (speed <= ((2000000 * 100) / 97))) {
775		result = 1;
776		goto done;
777	}
778	d = (FTDI_8U232AM_FREQ << 4) / speed;
779	d = (d & ~15) + roundoff[d & 15];
780
781	if (d < FTDI_8U232AM_MIN_DIV)
782		d = FTDI_8U232AM_MIN_DIV;
783	else if (d > FTDI_8U232AM_MAX_DIV)
784		d = FTDI_8U232AM_MAX_DIV;
785
786	/*
787	 * Calculate the frequency needed for "d" to exactly divide down to
788	 * our target "speed", and check that the actual frequency is within
789	 * 3% of this.
790	 */
791	freq = (speed * d);
792	if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
793	    (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
794		return (1);
795
796	/*
797	 * Pack the divisor into the resultant value.  The lower 14-bits
798	 * hold the integral part, while the upper 2 bits encode the
799	 * fractional component: either 0, 0.5, 0.25, or 0.125.
800	 */
801	result = (d >> 4);
802	if (d & 8)
803		result |= 0x4000;
804	else if (d & 4)
805		result |= 0x8000;
806	else if (d & 2)
807		result |= 0xc000;
808
809done:
810	*rate = result;
811	return (0);
812}
813
814static void
815uftdi_poll(struct ucom_softc *ucom)
816{
817	struct uftdi_softc *sc = ucom->sc_parent;
818	usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER);
819}
820