xref: /netbsd-src/sys/dev/usb/uftdi.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: uftdi.c,v 1.67 2018/02/20 15:48:37 ws 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  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.67 2018/02/20 15:48:37 ws Exp $");
34 
35 #ifdef _KERNEL_OPT
36 #include "opt_usb.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/conf.h>
44 #include <sys/tty.h>
45 
46 #include <dev/usb/usb.h>
47 
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
50 #include <dev/usb/usbdevs.h>
51 
52 #include <dev/usb/ucomvar.h>
53 
54 #include <dev/usb/uftdireg.h>
55 
56 #ifdef UFTDI_DEBUG
57 #define DPRINTF(x)	if (uftdidebug) printf x
58 #define DPRINTFN(n,x)	if (uftdidebug>(n)) printf x
59 int uftdidebug = 0;
60 #else
61 #define DPRINTF(x)
62 #define DPRINTFN(n,x)
63 #endif
64 
65 #define UFTDI_CONFIG_NO		1
66 
67 /*
68  * These are the default number of bytes transferred per frame if the
69  * endpoint doesn't tell us.  The output buffer size is a hard limit
70  * for devices that use a 6-bit size encoding.
71  */
72 #define UFTDIIBUFSIZE 64
73 #define UFTDIOBUFSIZE 64
74 
75 /*
76  * Magic constants!  Where do these come from?  They're what Linux uses...
77  */
78 #define	UFTDI_MAX_IBUFSIZE	512
79 #define	UFTDI_MAX_OBUFSIZE	256
80 
81 struct uftdi_softc {
82 	device_t		sc_dev;		/* base device */
83 	struct usbd_device *	sc_udev;	/* device */
84 	struct usbd_interface *	sc_iface;	/* interface */
85 	int			sc_iface_no;
86 
87 	enum uftdi_type		sc_type;
88 	u_int			sc_hdrlen;
89 	u_int			sc_chiptype;
90 
91 	u_char			sc_msr;
92 	u_char			sc_lsr;
93 
94 	device_t		sc_subdev;
95 
96 	u_char			sc_dying;
97 
98 	u_int			last_lcr;
99 
100 };
101 
102 Static void	uftdi_get_status(void *, int, u_char *, u_char *);
103 Static void	uftdi_set(void *, int, int, int);
104 Static int	uftdi_param(void *, int, struct termios *);
105 Static int	uftdi_open(void *, int);
106 Static void	uftdi_read(void *, int, u_char **, uint32_t *);
107 Static void	uftdi_write(void *, int, u_char *, u_char *,
108 			    uint32_t *);
109 Static void	uftdi_break(void *, int, int);
110 
111 struct ucom_methods uftdi_methods = {
112 	.ucom_get_status = uftdi_get_status,
113 	.ucom_set = uftdi_set,
114 	.ucom_param = uftdi_param,
115 	.ucom_ioctl = NULL,
116 	.ucom_open = uftdi_open,
117 	.ucom_close = NULL,
118 	.ucom_read = uftdi_read,
119 	.ucom_write = uftdi_write,
120 };
121 
122 /*
123  * The devices default to UFTDI_TYPE_8U232AM.
124  * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead
125  */
126 static const struct usb_devno uftdi_devs[] = {
127 	{ USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 },
128 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
129 	{ USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
130 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X },
131 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H },
132 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL },
133 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C },
134 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H },
135 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
136 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
137 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
138 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
139 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
140 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
141 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
142 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
143 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
144 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
145 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
146 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI },
147 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 },
148 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
149 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
150 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
151 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
152 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
153 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
154 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
155 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
156 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
157 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
158 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY },
159 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE },
160 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U },
161 	{ USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG },
162 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
163 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
164 	{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 },
165 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F },
166 	{ USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_CT57A },
167 	{ USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_RTS03 },
168 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL },
169 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 },
170 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 },
171 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 },
172 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 },
173 	{ USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 },
174 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK },
175 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO },
176 };
177 #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p)
178 
179 int uftdi_match(device_t, cfdata_t, void *);
180 void uftdi_attach(device_t, device_t, void *);
181 void uftdi_childdet(device_t, device_t);
182 int uftdi_detach(device_t, int);
183 int uftdi_activate(device_t, enum devact);
184 extern struct cfdriver uftdi_cd;
185 CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match,
186     uftdi_attach, uftdi_detach, uftdi_activate, NULL, uftdi_childdet);
187 
188 int
189 uftdi_match(device_t parent, cfdata_t match, void *aux)
190 {
191 	struct usbif_attach_arg *uiaa = aux;
192 
193 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
194 		     uiaa->uiaa_vendor, uiaa->uiaa_product));
195 
196 	if (uiaa->uiaa_configno != UFTDI_CONFIG_NO)
197 		return UMATCH_NONE;
198 
199 	return uftdi_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL ?
200 		UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
201 }
202 
203 void
204 uftdi_attach(device_t parent, device_t self, void *aux)
205 {
206 	struct uftdi_softc *sc = device_private(self);
207 	struct usbif_attach_arg *uiaa = aux;
208 	struct usbd_device *dev = uiaa->uiaa_device;
209 	struct usbd_interface *iface = uiaa->uiaa_iface;
210 	usb_device_descriptor_t *ddesc;
211 	usb_interface_descriptor_t *id;
212 	usb_endpoint_descriptor_t *ed;
213 	char *devinfop;
214 	int i;
215 	struct ucom_attach_args ucaa;
216 
217 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
218 
219 	aprint_naive("\n");
220 	aprint_normal("\n");
221 
222 	devinfop = usbd_devinfo_alloc(dev, 0);
223 	aprint_normal_dev(self, "%s\n", devinfop);
224 	usbd_devinfo_free(devinfop);
225 
226 	sc->sc_dev = self;
227 	sc->sc_udev = dev;
228 	sc->sc_iface_no = uiaa->uiaa_ifaceno;
229 	sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */
230 	sc->sc_hdrlen = 0;
231 	if (uiaa->uiaa_vendor == USB_VENDOR_FTDI
232 	    && uiaa->uiaa_product == USB_PRODUCT_FTDI_SERIAL_8U100AX) {
233 		sc->sc_type = UFTDI_TYPE_SIO;
234 		sc->sc_hdrlen = 1;
235 	}
236 
237 	ddesc = usbd_get_device_descriptor(dev);
238 	sc->sc_chiptype = UGETW(ddesc->bcdDevice);
239 
240 	id = usbd_get_interface_descriptor(iface);
241 
242 	sc->sc_iface = iface;
243 
244 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
245 	ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0;
246 	for (i = 0; i < id->bNumEndpoints; i++) {
247 		int addr, dir, attr;
248 		ed = usbd_interface2endpoint_descriptor(iface, i);
249 		if (ed == NULL) {
250 			aprint_error_dev(self,
251 			    "could not read endpoint descriptor\n");
252 			goto bad;
253 		}
254 
255 		addr = ed->bEndpointAddress;
256 		dir = UE_GET_DIR(ed->bEndpointAddress);
257 		attr = ed->bmAttributes & UE_XFERTYPE;
258 		if (dir == UE_DIR_IN && attr == UE_BULK) {
259 			ucaa.ucaa_bulkin = addr;
260 			ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
261 			if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE)
262 				ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE;
263 		} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
264 			ucaa.ucaa_bulkout = addr;
265 			ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize)
266 			    - sc->sc_hdrlen;
267 			if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE)
268 				ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE;
269 			/* Limit length if we have a 6-bit header.  */
270 			if ((sc->sc_hdrlen > 0) &&
271 			    (ucaa.ucaa_obufsize > UFTDIOBUFSIZE))
272 				ucaa.ucaa_obufsize = UFTDIOBUFSIZE;
273 		} else {
274 			aprint_error_dev(self, "unexpected endpoint\n");
275 			goto bad;
276 		}
277 	}
278 	if (ucaa.ucaa_bulkin == -1) {
279 		aprint_error_dev(self, "Could not find data bulk in\n");
280 		goto bad;
281 	}
282 	if (ucaa.ucaa_bulkout == -1) {
283 		aprint_error_dev(self, "Could not find data bulk out\n");
284 		goto bad;
285 	}
286 
287 	ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no;
288 	/* ucaa_bulkin, ucaa_bulkout set above */
289 	if (ucaa.ucaa_ibufsize == 0)
290 		ucaa.ucaa_ibufsize = UFTDIIBUFSIZE;
291 	ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
292 	if (ucaa.ucaa_obufsize == 0)
293 		ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
294 	ucaa.ucaa_opkthdrlen = sc->sc_hdrlen;
295 	ucaa.ucaa_device = dev;
296 	ucaa.ucaa_iface = iface;
297 	ucaa.ucaa_methods = &uftdi_methods;
298 	ucaa.ucaa_arg = sc;
299 	ucaa.ucaa_info = NULL;
300 
301 	DPRINTF(("uftdi: in=0x%x out=0x%x isize=0x%x osize=0x%x\n",
302 		ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
303 		ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize));
304 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL,
305 	    &ucaa, ucomprint, ucomsubmatch);
306 
307 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
308 
309 	if (!pmf_device_register(self, NULL, NULL))
310 		aprint_error_dev(self, "couldn't establish power handler\n");
311 
312 	return;
313 
314 bad:
315 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
316 	sc->sc_dying = 1;
317 	return;
318 }
319 
320 int
321 uftdi_activate(device_t self, enum devact act)
322 {
323 	struct uftdi_softc *sc = device_private(self);
324 
325 	switch (act) {
326 	case DVACT_DEACTIVATE:
327 		sc->sc_dying = 1;
328 		return 0;
329 	default:
330 		return EOPNOTSUPP;
331 	}
332 }
333 
334 void
335 uftdi_childdet(device_t self, device_t child)
336 {
337 	struct uftdi_softc *sc = device_private(self);
338 
339 	KASSERT(child == sc->sc_subdev);
340 	sc->sc_subdev = NULL;
341 }
342 
343 int
344 uftdi_detach(device_t self, int flags)
345 {
346 	struct uftdi_softc *sc = device_private(self);
347 
348 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
349 	sc->sc_dying = 1;
350 	if (sc->sc_subdev != NULL)
351 		config_detach(sc->sc_subdev, flags);
352 
353 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
354 
355 	return 0;
356 }
357 
358 Static int
359 uftdi_open(void *vsc, int portno)
360 {
361 	struct uftdi_softc *sc = vsc;
362 	usb_device_request_t req;
363 	usbd_status err;
364 	struct termios t;
365 
366 	DPRINTF(("uftdi_open: sc=%p\n", sc));
367 
368 	if (sc->sc_dying)
369 		return EIO;
370 
371 	/* Perform a full reset on the device */
372 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
373 	req.bRequest = FTDI_SIO_RESET;
374 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
375 	USETW(req.wIndex, portno);
376 	USETW(req.wLength, 0);
377 	err = usbd_do_request(sc->sc_udev, &req, NULL);
378 	if (err)
379 		return EIO;
380 
381 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
382 	t.c_ospeed = 9600;
383 	t.c_cflag = CSTOPB | CS8;
384 	(void)uftdi_param(sc, portno, &t);
385 
386 	/* Turn on RTS/CTS flow control */
387 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
388 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
389 	USETW(req.wValue, 0);
390 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
391 	USETW(req.wLength, 0);
392 	err = usbd_do_request(sc->sc_udev, &req, NULL);
393 	if (err)
394 		return EIO;
395 
396 	return 0;
397 }
398 
399 Static void
400 uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count)
401 {
402 	struct uftdi_softc *sc = vsc;
403 	u_char msr, lsr;
404 
405 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
406 		     *count));
407 
408 	msr = FTDI_GET_MSR(*ptr);
409 	lsr = FTDI_GET_LSR(*ptr);
410 
411 #ifdef UFTDI_DEBUG
412 	if (*count != 2)
413 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
414 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
415 #endif
416 
417 	if (sc->sc_msr != msr ||
418 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
419 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
420 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
421 			 lsr, sc->sc_lsr));
422 		sc->sc_msr = msr;
423 		sc->sc_lsr = lsr;
424 		ucom_status_change(device_private(sc->sc_subdev));
425 	}
426 
427 	/* Adjust buffer pointer to skip status prefix */
428 	*ptr += 2;
429 }
430 
431 Static void
432 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count)
433 {
434 	struct uftdi_softc *sc = vsc;
435 
436 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
437 		     vsc, portno, *count, from[0]));
438 
439 	/* Make length tag and copy data */
440 	if (sc->sc_hdrlen > 0)
441 		*to = FTDI_OUT_TAG(*count, portno);
442 
443 	memcpy(to + sc->sc_hdrlen, from, *count);
444 	*count += sc->sc_hdrlen;
445 }
446 
447 Static void
448 uftdi_set(void *vsc, int portno, int reg, int onoff)
449 {
450 	struct uftdi_softc *sc = vsc;
451 	usb_device_request_t req;
452 	int ctl;
453 
454 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
455 		 reg, onoff));
456 
457 	switch (reg) {
458 	case UCOM_SET_DTR:
459 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
460 		break;
461 	case UCOM_SET_RTS:
462 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
463 		break;
464 	case UCOM_SET_BREAK:
465 		uftdi_break(sc, portno, onoff);
466 		return;
467 	default:
468 		return;
469 	}
470 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
471 	req.bRequest = FTDI_SIO_MODEM_CTRL;
472 	USETW(req.wValue, ctl);
473 	USETW(req.wIndex, portno);
474 	USETW(req.wLength, 0);
475 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
476 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
477 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
478 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
479 }
480 
481 Static int
482 uftdi_param(void *vsc, int portno, struct termios *t)
483 {
484 	struct uftdi_softc *sc = vsc;
485 	usb_device_request_t req;
486 	usbd_status err;
487 	int rate, data, flow;
488 
489 	DPRINTF(("uftdi_param: sc=%p\n", sc));
490 
491 	if (sc->sc_dying)
492 		return EIO;
493 
494 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
495 	req.bRequest = FTDI_SIO_SET_BITMODE;
496 	USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00);
497 	USETW(req.wIndex, portno);
498 	USETW(req.wLength, 0);
499 	err = usbd_do_request(sc->sc_udev, &req, NULL);
500 	if (err)
501 		return EIO;
502 
503 	switch (sc->sc_type) {
504 	case UFTDI_TYPE_SIO:
505 		switch (t->c_ospeed) {
506 		case 300: rate = ftdi_sio_b300; break;
507 		case 600: rate = ftdi_sio_b600; break;
508 		case 1200: rate = ftdi_sio_b1200; break;
509 		case 2400: rate = ftdi_sio_b2400; break;
510 		case 4800: rate = ftdi_sio_b4800; break;
511 		case 9600: rate = ftdi_sio_b9600; break;
512 		case 19200: rate = ftdi_sio_b19200; break;
513 		case 38400: rate = ftdi_sio_b38400; break;
514 		case 57600: rate = ftdi_sio_b57600; break;
515 		case 115200: rate = ftdi_sio_b115200; break;
516 		default:
517 			return EINVAL;
518 		}
519 		break;
520 
521 	case UFTDI_TYPE_8U232AM:
522 		switch(t->c_ospeed) {
523 		case 300: rate = ftdi_8u232am_b300; break;
524 		case 600: rate = ftdi_8u232am_b600; break;
525 		case 1200: rate = ftdi_8u232am_b1200; break;
526 		case 2400: rate = ftdi_8u232am_b2400; break;
527 		case 4800: rate = ftdi_8u232am_b4800; break;
528 		case 9600: rate = ftdi_8u232am_b9600; break;
529 		case 19200: rate = ftdi_8u232am_b19200; break;
530 		case 38400: rate = ftdi_8u232am_b38400; break;
531 		case 57600: rate = ftdi_8u232am_b57600; break;
532 		case 115200: rate = ftdi_8u232am_b115200; break;
533 		case 230400: rate = ftdi_8u232am_b230400; break;
534 		case 460800: rate = ftdi_8u232am_b460800; break;
535 		case 921600: rate = ftdi_8u232am_b921600; break;
536 		default:
537 			return EINVAL;
538 		}
539 		break;
540 
541 	default:
542 		return EINVAL;
543 	}
544 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
545 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
546 	USETW(req.wValue, rate);
547 	USETW(req.wIndex, portno);
548 	USETW(req.wLength, 0);
549 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
550 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
551 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
552 	err = usbd_do_request(sc->sc_udev, &req, NULL);
553 	if (err)
554 		return EIO;
555 
556 	if (ISSET(t->c_cflag, CSTOPB))
557 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
558 	else
559 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
560 	if (ISSET(t->c_cflag, PARENB)) {
561 		if (ISSET(t->c_cflag, PARODD))
562 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
563 		else
564 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
565 	} else
566 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
567 	switch (ISSET(t->c_cflag, CSIZE)) {
568 	case CS5:
569 		data |= FTDI_SIO_SET_DATA_BITS(5);
570 		break;
571 	case CS6:
572 		data |= FTDI_SIO_SET_DATA_BITS(6);
573 		break;
574 	case CS7:
575 		data |= FTDI_SIO_SET_DATA_BITS(7);
576 		break;
577 	case CS8:
578 		data |= FTDI_SIO_SET_DATA_BITS(8);
579 		break;
580 	}
581 	sc->last_lcr = data;
582 
583 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
584 	req.bRequest = FTDI_SIO_SET_DATA;
585 	USETW(req.wValue, data);
586 	USETW(req.wIndex, portno);
587 	USETW(req.wLength, 0);
588 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
589 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
590 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
591 	err = usbd_do_request(sc->sc_udev, &req, NULL);
592 	if (err)
593 		return EIO;
594 
595 	if (ISSET(t->c_cflag, CRTSCTS)) {
596 		flow = FTDI_SIO_RTS_CTS_HS;
597 		USETW(req.wValue, 0);
598 	} else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) {
599 		flow = FTDI_SIO_XON_XOFF_HS;
600 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
601 	} else {
602 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
603 		USETW(req.wValue, 0);
604 	}
605 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
606 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
607 	USETW2(req.wIndex, flow, portno);
608 	USETW(req.wLength, 0);
609 	err = usbd_do_request(sc->sc_udev, &req, NULL);
610 	if (err)
611 		return EIO;
612 
613 	return 0;
614 }
615 
616 void
617 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
618 {
619 	struct uftdi_softc *sc = vsc;
620 
621 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
622 		 sc->sc_msr, sc->sc_lsr));
623 
624 	if (msr != NULL)
625 		*msr = sc->sc_msr;
626 	if (lsr != NULL)
627 		*lsr = sc->sc_lsr;
628 }
629 
630 void
631 uftdi_break(void *vsc, int portno, int onoff)
632 {
633 	struct uftdi_softc *sc = vsc;
634 	usb_device_request_t req;
635 	int data;
636 
637 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
638 		  onoff));
639 
640 	if (onoff) {
641 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
642 	} else {
643 		data = sc->last_lcr;
644 	}
645 
646 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
647 	req.bRequest = FTDI_SIO_SET_DATA;
648 	USETW(req.wValue, data);
649 	USETW(req.wIndex, portno);
650 	USETW(req.wLength, 0);
651 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
652 }
653