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