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