1 /*	$OpenBSD: uftdi.c,v 1.24 2005/05/24 03:26:05 pascoe Exp $ 	*/
2 /*	$NetBSD: uftdi.c,v 1.14 2003/02/23 04:20:07 simonb Exp $	*/
3 
4 /*
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * FTDI FT8U100AX serial adapter driver
42  */
43 
44 /*
45  * XXX This driver will not support multiple serial ports.
46  * XXX The ucom layer needs to be extended first.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/device.h>
53 #include <sys/conf.h>
54 #include <sys/tty.h>
55 
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbhid.h>
58 
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 
63 #include <dev/usb/ucomvar.h>
64 
65 #include <dev/usb/uftdireg.h>
66 
67 #ifdef UFTDI_DEBUG
68 #define DPRINTF(x)	do { if (uftdidebug) printf x; } while (0)
69 #define DPRINTFN(n,x)	do { if (uftdidebug>(n)) printf x; } while (0)
70 int uftdidebug = 0;
71 #else
72 #define DPRINTF(x)
73 #define DPRINTFN(n,x)
74 #endif
75 
76 #define UFTDI_CONFIG_INDEX	0
77 #define UFTDI_IFACE_INDEX	0
78 
79 
80 /*
81  * These are the maximum number of bytes transferred per frame.
82  * The output buffer size cannot be increased due to the size encoding.
83  */
84 #define UFTDIIBUFSIZE 64
85 #define UFTDIOBUFSIZE 64
86 
87 struct uftdi_softc {
88 	USBBASEDEVICE		sc_dev;		/* base device */
89 	usbd_device_handle	sc_udev;	/* device */
90 	usbd_interface_handle	sc_iface;	/* interface */
91 
92 	enum uftdi_type		sc_type;
93 	u_int			sc_hdrlen;
94 
95 	u_char			sc_msr;
96 	u_char			sc_lsr;
97 
98 	device_ptr_t		sc_subdev;
99 
100 	u_char			sc_dying;
101 
102 	u_int			last_lcr;
103 };
104 
105 Static void	uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
106 Static void	uftdi_set(void *, int, int, int);
107 Static int	uftdi_param(void *, int, struct termios *);
108 Static int	uftdi_open(void *sc, int portno);
109 Static void	uftdi_read(void *sc, int portno, u_char **ptr,
110 			   u_int32_t *count);
111 Static void	uftdi_write(void *sc, int portno, u_char *to, u_char *from,
112 			    u_int32_t *count);
113 Static void	uftdi_break(void *sc, int portno, int onoff);
114 
115 struct ucom_methods uftdi_methods = {
116 	uftdi_get_status,
117 	uftdi_set,
118 	uftdi_param,
119 	NULL,
120 	uftdi_open,
121 	NULL,
122 	uftdi_read,
123 	uftdi_write,
124 };
125 
126 USB_DECLARE_DRIVER(uftdi);
127 
USB_MATCH(uftdi)128 USB_MATCH(uftdi)
129 {
130 	USB_MATCH_START(uftdi, uaa);
131 
132 	if (uaa->iface != NULL) {
133 		if (uaa->vendor == USB_VENDOR_FTDI &&
134 		    (uaa->product == USB_PRODUCT_FTDI_SERIAL_2232C))
135 			return (UMATCH_VENDOR_IFACESUBCLASS);
136 		return (UMATCH_NONE);
137 	}
138 
139 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
140 		     uaa->vendor, uaa->product));
141 
142 	if (uaa->vendor == USB_VENDOR_FTDI &&
143 	    (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX ||
144 	     uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM ||
145 	     uaa->product == USB_PRODUCT_FTDI_SEMC_DSS20 ||
146 	     uaa->product == USB_PRODUCT_FTDI_LCD_LK202_24 ||
147 	     uaa->product == USB_PRODUCT_FTDI_LCD_LK204_24 ||
148 	     uaa->product == USB_PRODUCT_FTDI_LCD_MX200 ||
149 	     uaa->product == USB_PRODUCT_FTDI_LCD_CFA_631 ||
150 	     uaa->product == USB_PRODUCT_FTDI_LCD_CFA_632 ||
151 	     uaa->product == USB_PRODUCT_FTDI_LCD_CFA_633 ||
152 	     uaa->product == USB_PRODUCT_FTDI_LCD_CFA_634))
153 	    return (UMATCH_VENDOR_PRODUCT);
154 	if (uaa->vendor == USB_VENDOR_OCT &&
155 	    (uaa->product == USB_PRODUCT_OCT_US2308))
156 		return (UMATCH_VENDOR_PRODUCT);
157 	if (uaa->vendor == USB_VENDOR_INTREPIDCS &&
158 	    (uaa->product == USB_PRODUCT_INTREPIDCS_VALUECAN ||
159 	     uaa->product == USB_PRODUCT_INTREPIDCS_NEOVI))
160 		return (UMATCH_VENDOR_PRODUCT);
161 	if (uaa->vendor == USB_VENDOR_BBELECTR &&
162 	    (uaa->product == USB_PRODUCT_BBELECTR_USOTL4))
163 		return (UMATCH_VENDOR_PRODUCT);
164 	if (uaa->vendor == USB_VENDOR_FALCOM &&
165 	    (uaa->product == USB_PRODUCT_FALCOM_TWIST))
166 		 return (UMATCH_VENDOR_PRODUCT);
167 	if (uaa->vendor == USB_VENDOR_SEALEVEL &&
168 	    uaa->product == USB_PRODUCT_SEALEVEL_2101)
169 		return (UMATCH_VENDOR_PRODUCT);
170 
171 	return (UMATCH_NONE);
172 }
173 
USB_ATTACH(uftdi)174 USB_ATTACH(uftdi)
175 {
176 	USB_ATTACH_START(uftdi, sc, uaa);
177 	usbd_device_handle dev = uaa->device;
178 	usbd_interface_handle iface;
179 	usb_interface_descriptor_t *id;
180 	usb_endpoint_descriptor_t *ed;
181 	char devinfo[1024];
182 	char *devname = USBDEVNAME(sc->sc_dev);
183 	int i;
184 	usbd_status err;
185 	struct ucom_attach_args uca;
186 
187 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
188 
189 	if (uaa->iface == NULL) {
190 		/* Move the device into the configured state. */
191 		err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
192 		if (err) {
193 			printf("\n%s: failed to set configuration, err=%s\n",
194 			       devname, usbd_errstr(err));
195 			goto bad;
196 		}
197 
198 		err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
199 		if (err) {
200 			printf("\n%s: failed to get interface, err=%s\n",
201 			       devname, usbd_errstr(err));
202 			goto bad;
203 		}
204 	} else
205 		iface = uaa->iface;
206 
207 	usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
208 	USB_ATTACH_SETUP;
209 	printf("%s: %s\n", devname, devinfo);
210 
211 	id = usbd_get_interface_descriptor(iface);
212 
213 	sc->sc_udev = dev;
214 	sc->sc_iface = iface;
215 
216 	switch (uaa->vendor) {
217 	case USB_VENDOR_FTDI:
218 		switch (uaa->product) {
219 		case USB_PRODUCT_FTDI_SERIAL_8U100AX:
220 			sc->sc_type = UFTDI_TYPE_SIO;
221 			sc->sc_hdrlen = 1;
222 			break;
223 
224 		case USB_PRODUCT_FTDI_SEMC_DSS20:
225 		case USB_PRODUCT_FTDI_SERIAL_8U232AM:
226 		case USB_PRODUCT_FTDI_SERIAL_2232C:
227 		case USB_PRODUCT_FTDI_LCD_LK202_24:
228 		case USB_PRODUCT_FTDI_LCD_LK204_24:
229 		case USB_PRODUCT_FTDI_LCD_MX200:
230 		case USB_PRODUCT_FTDI_LCD_CFA_631:
231 		case USB_PRODUCT_FTDI_LCD_CFA_632:
232 		case USB_PRODUCT_FTDI_LCD_CFA_633:
233 		case USB_PRODUCT_FTDI_LCD_CFA_634:
234 		case USB_PRODUCT_SEALEVEL_2101:
235 			sc->sc_type = UFTDI_TYPE_8U232AM;
236 			sc->sc_hdrlen = 0;
237 			break;
238 
239 		default:		/* Can't happen */
240 			goto bad;
241 		}
242 		break;
243 
244 	case USB_VENDOR_INTREPIDCS:
245 		switch (uaa->product) {
246 		case USB_PRODUCT_INTREPIDCS_VALUECAN:
247 		case USB_PRODUCT_INTREPIDCS_NEOVI:
248 		sc->sc_type = UFTDI_TYPE_8U232AM;
249 			sc->sc_hdrlen = 0;
250 			break;
251 
252 		default:                /* Can't happen */
253 			goto bad;
254 		}
255 		break;
256 
257 	case USB_VENDOR_OCT:
258 		switch (uaa->product) {
259 		case USB_PRODUCT_OCT_US2308:
260 			sc->sc_type = UFTDI_TYPE_8U232AM;
261 			sc->sc_hdrlen = 0;
262 			break;
263 
264 		default:		/* Can't happen */
265 			goto bad;
266 		}
267 		break;
268 
269 	case USB_VENDOR_BBELECTR:
270 		switch( uaa->product ){
271 		case USB_PRODUCT_BBELECTR_USOTL4:
272 			sc->sc_type = UFTDI_TYPE_8U232AM;
273 			sc->sc_hdrlen = 0;
274 			break;
275 		default:		/* Can't happen */
276 			goto bad;
277 		}
278 		break;
279 
280 	case USB_VENDOR_FALCOM:
281 		switch( uaa->product ){
282 		case USB_PRODUCT_FALCOM_TWIST:
283 			sc->sc_type = UFTDI_TYPE_8U232AM;
284 			sc->sc_hdrlen = 0;
285 			break;
286 		default:		/* Can't happen */
287 			goto bad;
288 		}
289 		break;
290 	}
291 
292 
293 	uca.bulkin = uca.bulkout = -1;
294 	for (i = 0; i < id->bNumEndpoints; i++) {
295 		int addr, dir, attr;
296 		ed = usbd_interface2endpoint_descriptor(iface, i);
297 		if (ed == NULL) {
298 			printf("%s: could not read endpoint descriptor\n",
299 			    devname);
300 			goto bad;
301 		}
302 
303 		addr = ed->bEndpointAddress;
304 		dir = UE_GET_DIR(ed->bEndpointAddress);
305 		attr = ed->bmAttributes & UE_XFERTYPE;
306 		if (dir == UE_DIR_IN && attr == UE_BULK)
307 			uca.bulkin = addr;
308 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
309 			uca.bulkout = addr;
310 		else {
311 			printf("%s: unexpected endpoint\n", devname);
312 			goto bad;
313 		}
314 	}
315 	if (uca.bulkin == -1) {
316 		printf("%s: Could not find data bulk in\n",
317 		       USBDEVNAME(sc->sc_dev));
318 		goto bad;
319 	}
320 	if (uca.bulkout == -1) {
321 		printf("%s: Could not find data bulk out\n",
322 		       USBDEVNAME(sc->sc_dev));
323 		goto bad;
324 	}
325 
326 	if (uaa->iface == NULL)
327 		uca.portno = FTDI_PIT_SIOA;
328 	else
329 		uca.portno = FTDI_PIT_SIOA + id->bInterfaceNumber;
330 	/* bulkin, bulkout set above */
331 	uca.ibufsize = UFTDIIBUFSIZE;
332 	uca.obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
333 	uca.ibufsizepad = UFTDIIBUFSIZE;
334 	uca.opkthdrlen = sc->sc_hdrlen;
335 	uca.device = dev;
336 	uca.iface = iface;
337 	uca.methods = &uftdi_methods;
338 	uca.arg = sc;
339 	uca.info = NULL;
340 
341 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
342 			   USBDEV(sc->sc_dev));
343 
344 	DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
345 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
346 
347 	USB_ATTACH_SUCCESS_RETURN;
348 
349 bad:
350 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
351 	sc->sc_dying = 1;
352 	USB_ATTACH_ERROR_RETURN;
353 }
354 
355 int
uftdi_activate(device_ptr_t self,enum devact act)356 uftdi_activate(device_ptr_t self, enum devact act)
357 {
358 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
359 	int rv = 0;
360 
361 	switch (act) {
362 	case DVACT_ACTIVATE:
363 		return (EOPNOTSUPP);
364 
365 	case DVACT_DEACTIVATE:
366 		if (sc->sc_subdev != NULL)
367 			rv = config_deactivate(sc->sc_subdev);
368 		sc->sc_dying = 1;
369 		break;
370 	}
371 	return (rv);
372 }
373 
374 int
uftdi_detach(device_ptr_t self,int flags)375 uftdi_detach(device_ptr_t self, int flags)
376 {
377 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
378 
379 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
380 	sc->sc_dying = 1;
381 	if (sc->sc_subdev != NULL) {
382 		config_detach(sc->sc_subdev, flags);
383 		sc->sc_subdev = NULL;
384 	}
385 
386 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
387 			   USBDEV(sc->sc_dev));
388 
389 	return (0);
390 }
391 
392 Static int
uftdi_open(void * vsc,int portno)393 uftdi_open(void *vsc, int portno)
394 {
395 	struct uftdi_softc *sc = vsc;
396 	usb_device_request_t req;
397 	usbd_status err;
398 	struct termios t;
399 
400 	DPRINTF(("uftdi_open: sc=%p\n", sc));
401 
402 	if (sc->sc_dying)
403 		return (EIO);
404 
405 	/* Perform a full reset on the device */
406 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
407 	req.bRequest = FTDI_SIO_RESET;
408 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
409 	USETW(req.wIndex, portno);
410 	USETW(req.wLength, 0);
411 	err = usbd_do_request(sc->sc_udev, &req, NULL);
412 	if (err)
413 		return (EIO);
414 
415 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
416 	t.c_ospeed = 9600;
417 	t.c_cflag = CSTOPB | CS8;
418 	(void)uftdi_param(sc, portno, &t);
419 
420 	/* Turn on RTS/CTS flow control */
421 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
422 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
423 	USETW(req.wValue, 0);
424 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
425 	USETW(req.wLength, 0);
426 	err = usbd_do_request(sc->sc_udev, &req, NULL);
427 	if (err)
428 		return (EIO);
429 
430 	return (0);
431 }
432 
433 Static void
uftdi_read(void * vsc,int portno,u_char ** ptr,u_int32_t * count)434 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
435 {
436 	struct uftdi_softc *sc = vsc;
437 	u_char msr, lsr;
438 
439 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
440 		     *count));
441 
442 	msr = FTDI_GET_MSR(*ptr);
443 	lsr = FTDI_GET_LSR(*ptr);
444 
445 #ifdef UFTDI_DEBUG
446 	if (*count != 2)
447 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
448 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
449 #endif
450 
451 	if (sc->sc_msr != msr ||
452 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
453 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
454 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
455 			 lsr, sc->sc_lsr));
456 		sc->sc_msr = msr;
457 		sc->sc_lsr = lsr;
458 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
459 	}
460 
461 	/* Pick up status and adjust data part. */
462 	*ptr += 2;
463 	*count -= 2;
464 }
465 
466 Static void
uftdi_write(void * vsc,int portno,u_char * to,u_char * from,u_int32_t * count)467 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
468 {
469 	struct uftdi_softc *sc = vsc;
470 
471 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
472 		     vsc, portno, *count, from[0]));
473 
474 	/* Make length tag and copy data */
475 	if (sc->sc_hdrlen > 0)
476 		*to = FTDI_OUT_TAG(*count, portno);
477 
478 	memcpy(to + sc->sc_hdrlen, from, *count);
479 	*count += sc->sc_hdrlen;
480 }
481 
482 Static void
uftdi_set(void * vsc,int portno,int reg,int onoff)483 uftdi_set(void *vsc, int portno, int reg, int onoff)
484 {
485 	struct uftdi_softc *sc = vsc;
486 	usb_device_request_t req;
487 	int ctl;
488 
489 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
490 		 reg, onoff));
491 
492 	switch (reg) {
493 	case UCOM_SET_DTR:
494 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
495 		break;
496 	case UCOM_SET_RTS:
497 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
498 		break;
499 	case UCOM_SET_BREAK:
500 		uftdi_break(sc, portno, onoff);
501 		return;
502 	default:
503 		return;
504 	}
505 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
506 	req.bRequest = FTDI_SIO_MODEM_CTRL;
507 	USETW(req.wValue, ctl);
508 	USETW(req.wIndex, portno);
509 	USETW(req.wLength, 0);
510 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
511 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
512 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
513 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
514 }
515 
516 Static int
uftdi_param(void * vsc,int portno,struct termios * t)517 uftdi_param(void *vsc, int portno, struct termios *t)
518 {
519 	struct uftdi_softc *sc = vsc;
520 	usb_device_request_t req;
521 	usbd_status err;
522 	int rate, data, flow;
523 
524 	DPRINTF(("uftdi_param: sc=%p\n", sc));
525 
526 	if (sc->sc_dying)
527 		return (EIO);
528 
529 	switch (sc->sc_type) {
530 	case UFTDI_TYPE_SIO:
531 		switch (t->c_ospeed) {
532 		case 300: rate = ftdi_sio_b300; break;
533 		case 600: rate = ftdi_sio_b600; break;
534 		case 1200: rate = ftdi_sio_b1200; break;
535 		case 2400: rate = ftdi_sio_b2400; break;
536 		case 4800: rate = ftdi_sio_b4800; break;
537 		case 9600: rate = ftdi_sio_b9600; break;
538 		case 19200: rate = ftdi_sio_b19200; break;
539 		case 38400: rate = ftdi_sio_b38400; break;
540 		case 57600: rate = ftdi_sio_b57600; break;
541 		case 115200: rate = ftdi_sio_b115200; break;
542 		default:
543 			return (EINVAL);
544 		}
545 		break;
546 
547 	case UFTDI_TYPE_8U232AM:
548 		switch(t->c_ospeed) {
549 		case 300: rate = ftdi_8u232am_b300; break;
550 		case 600: rate = ftdi_8u232am_b600; break;
551 		case 1200: rate = ftdi_8u232am_b1200; break;
552 		case 2400: rate = ftdi_8u232am_b2400; break;
553 		case 4800: rate = ftdi_8u232am_b4800; break;
554 		case 9600: rate = ftdi_8u232am_b9600; break;
555 		case 19200: rate = ftdi_8u232am_b19200; break;
556 		case 38400: rate = ftdi_8u232am_b38400; break;
557 		case 57600: rate = ftdi_8u232am_b57600; break;
558 		case 115200: rate = ftdi_8u232am_b115200; break;
559 		case 230400: rate = ftdi_8u232am_b230400; break;
560 		case 460800: rate = ftdi_8u232am_b460800; break;
561 		case 921600: rate = ftdi_8u232am_b921600; break;
562 		case 2000000: rate = ftdi_8u232am_b2000000; break;
563 		case 3000000: rate = ftdi_8u232am_b3000000; break;
564 		default:
565 			return (EINVAL);
566 		}
567 		break;
568 	}
569 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
570 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
571 	USETW(req.wValue, rate);
572 	USETW(req.wIndex, portno);
573 	USETW(req.wLength, 0);
574 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
575 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
576 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
577 	err = usbd_do_request(sc->sc_udev, &req, NULL);
578 	if (err)
579 		return (EIO);
580 
581 	if (ISSET(t->c_cflag, CSTOPB))
582 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
583 	else
584 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
585 	if (ISSET(t->c_cflag, PARENB)) {
586 		if (ISSET(t->c_cflag, PARODD))
587 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
588 		else
589 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
590 	} else
591 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
592 	switch (ISSET(t->c_cflag, CSIZE)) {
593 	case CS5:
594 		data |= FTDI_SIO_SET_DATA_BITS(5);
595 		break;
596 	case CS6:
597 		data |= FTDI_SIO_SET_DATA_BITS(6);
598 		break;
599 	case CS7:
600 		data |= FTDI_SIO_SET_DATA_BITS(7);
601 		break;
602 	case CS8:
603 		data |= FTDI_SIO_SET_DATA_BITS(8);
604 		break;
605 	}
606 	sc->last_lcr = data;
607 
608 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
609 	req.bRequest = FTDI_SIO_SET_DATA;
610 	USETW(req.wValue, data);
611 	USETW(req.wIndex, portno);
612 	USETW(req.wLength, 0);
613 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
614 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
615 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
616 	err = usbd_do_request(sc->sc_udev, &req, NULL);
617 	if (err)
618 		return (EIO);
619 
620 	if (ISSET(t->c_cflag, CRTSCTS)) {
621 		flow = FTDI_SIO_RTS_CTS_HS;
622 		USETW(req.wValue, 0);
623 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
624 		flow = FTDI_SIO_XON_XOFF_HS;
625 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
626 	} else {
627 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
628 		USETW(req.wValue, 0);
629 	}
630 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
631 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
632 	USETW2(req.wIndex, flow, portno);
633 	USETW(req.wLength, 0);
634 	err = usbd_do_request(sc->sc_udev, &req, NULL);
635 	if (err)
636 		return (EIO);
637 
638 	return (0);
639 }
640 
641 void
uftdi_get_status(void * vsc,int portno,u_char * lsr,u_char * msr)642 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
643 {
644 	struct uftdi_softc *sc = vsc;
645 
646 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
647 		 sc->sc_msr, sc->sc_lsr));
648 
649 	if (msr != NULL)
650 		*msr = sc->sc_msr;
651 	if (lsr != NULL)
652 		*lsr = sc->sc_lsr;
653 }
654 
655 void
uftdi_break(void * vsc,int portno,int onoff)656 uftdi_break(void *vsc, int portno, int onoff)
657 {
658 	struct uftdi_softc *sc = vsc;
659 	usb_device_request_t req;
660 	int data;
661 
662 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
663 		  onoff));
664 
665 	if (onoff) {
666 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
667 	} else {
668 		data = sc->last_lcr;
669 	}
670 
671 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
672 	req.bRequest = FTDI_SIO_SET_DATA;
673 	USETW(req.wValue, data);
674 	USETW(req.wIndex, portno);
675 	USETW(req.wLength, 0);
676 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
677 }
678