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