xref: /netbsd-src/sys/dev/usb/uftdi.c (revision e61202360d5611414dd6f6115934a96aa1f50b1a)
1 /*	$NetBSD: uftdi.c,v 1.53 2012/08/11 07:09:09 riastradh 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.53 2012/08/11 07:09:09 riastradh 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_2232C },
129 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H },
130 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
131 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
132 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
133 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
134 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
135 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
136 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
137 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
138 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
139 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
140 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
141 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI },
142 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 },
143 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
144 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
145 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
146 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
147 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
148 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
149 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
150 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
151 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
152 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
153 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY },
154 	{ USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG },
155 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
156 	{ USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
157 	{ USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F },
158 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL },
159 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 },
160 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 },
161 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 },
162 	{ USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 },
163 	{ USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 },
164 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK },
165 	{ USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO },
166 };
167 #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p)
168 
169 int uftdi_match(device_t, cfdata_t, void *);
170 void uftdi_attach(device_t, device_t, void *);
171 void uftdi_childdet(device_t, device_t);
172 int uftdi_detach(device_t, int);
173 int uftdi_activate(device_t, enum devact);
174 extern struct cfdriver uftdi_cd;
175 CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match,
176     uftdi_attach, uftdi_detach, uftdi_activate, NULL, uftdi_childdet);
177 
178 int
179 uftdi_match(device_t parent, cfdata_t match, void *aux)
180 {
181 	struct usb_attach_arg *uaa = aux;
182 
183 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
184 		     uaa->vendor, uaa->product));
185 
186         return (uftdi_lookup(uaa->vendor, uaa->product) != NULL ?
187                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
188 }
189 
190 void
191 uftdi_attach(device_t parent, device_t self, void *aux)
192 {
193 	struct uftdi_softc *sc = device_private(self);
194 	struct usb_attach_arg *uaa = aux;
195 	usbd_device_handle dev = uaa->device;
196 	usbd_interface_handle iface;
197 	usb_device_descriptor_t *ddesc;
198 	usb_interface_descriptor_t *id;
199 	usb_endpoint_descriptor_t *ed;
200 	char *devinfop;
201 	const char *devname = device_xname(self);
202 	int i,idx;
203 	usbd_status err;
204 	struct ucom_attach_args uca;
205 
206 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
207 
208 	aprint_naive("\n");
209 	aprint_normal("\n");
210 
211 	devinfop = usbd_devinfo_alloc(dev, 0);
212 	aprint_normal_dev(self, "%s\n", devinfop);
213 	usbd_devinfo_free(devinfop);
214 
215 	/* Move the device into the configured state. */
216 	err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
217 	if (err) {
218 		aprint_error("\n%s: failed to set configuration, err=%s\n",
219 		       devname, usbd_errstr(err));
220 		goto bad;
221 	}
222 
223 	sc->sc_dev = self;
224 	sc->sc_udev = dev;
225 	sc->sc_numports = 1;
226 	sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */
227 	sc->sc_hdrlen = 0;
228 	if (uaa->vendor == USB_VENDOR_FTDI
229 	    && uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX) {
230 		sc->sc_type = UFTDI_TYPE_SIO;
231 		sc->sc_hdrlen = 1;
232 	}
233 
234 	ddesc = usbd_get_device_descriptor(dev);
235 	sc->sc_chiptype = UGETW(ddesc->bcdDevice);
236 	switch (sc->sc_chiptype) {
237 	case 0x500: /* 2232D */
238 	case 0x700: /* 2232H */
239 		sc->sc_numports = 2;
240 		break;
241 	case 0x800: /* 4232H */
242 		sc->sc_numports = 4;
243 		break;
244 	case 0x200: /* 232/245AM */
245 	case 0x400: /* 232/245BL */
246 	case 0x600: /* 232/245R */
247 	default:
248 		break;
249 	}
250 
251 	for (idx = UFTDI_IFACE_INDEX; idx < sc->sc_numports; idx++) {
252 		err = usbd_device2interface_handle(dev, idx, &iface);
253 		if (err) {
254 			aprint_error(
255 			    "\n%s: failed to get interface idx=%d, err=%s\n",
256 			    devname, idx, usbd_errstr(err));
257 			goto bad;
258 		}
259 
260 		id = usbd_get_interface_descriptor(iface);
261 
262 		sc->sc_iface[idx] = iface;
263 
264 		uca.bulkin = uca.bulkout = -1;
265 		uca.ibufsize = uca.obufsize = 0;
266 		for (i = 0; i < id->bNumEndpoints; i++) {
267 			int addr, dir, attr;
268 			ed = usbd_interface2endpoint_descriptor(iface, i);
269 			if (ed == NULL) {
270 				aprint_error_dev(self,
271 				    "could not read endpoint descriptor: %s\n",
272 				    usbd_errstr(err));
273 				goto bad;
274 			}
275 
276 			addr = ed->bEndpointAddress;
277 			dir = UE_GET_DIR(ed->bEndpointAddress);
278 			attr = ed->bmAttributes & UE_XFERTYPE;
279 			if (dir == UE_DIR_IN && attr == UE_BULK) {
280 				uca.bulkin = addr;
281 				uca.ibufsize = UGETW(ed->wMaxPacketSize);
282 				if (uca.ibufsize >= UFTDI_MAX_IBUFSIZE)
283 					uca.ibufsize = UFTDI_MAX_IBUFSIZE;
284 			} else if (dir == UE_DIR_OUT && attr == UE_BULK) {
285 				uca.bulkout = addr;
286 				uca.obufsize = UGETW(ed->wMaxPacketSize)
287 				    - sc->sc_hdrlen;
288 				if (uca.obufsize >= UFTDI_MAX_OBUFSIZE)
289 					uca.obufsize = UFTDI_MAX_OBUFSIZE;
290 				/* Limit length if we have a 6-bit header.  */
291 				if ((sc->sc_hdrlen > 0) &&
292 				    (uca.obufsize > UFTDIOBUFSIZE))
293 					uca.obufsize = UFTDIOBUFSIZE;
294 			} else {
295 				aprint_error_dev(self,
296 				    "unexpected endpoint\n");
297 				goto bad;
298 			}
299 		}
300 		if (uca.bulkin == -1) {
301 			aprint_error_dev(self,
302 			    "Could not find data bulk in\n");
303 			goto bad;
304 		}
305 		if (uca.bulkout == -1) {
306 			aprint_error_dev(self,
307 			    "Could not find data bulk out\n");
308 			goto bad;
309 		}
310 
311 		uca.portno = FTDI_PIT_SIOA + idx;
312 		/* bulkin, bulkout set above */
313 		if (uca.ibufsize == 0)
314 			uca.ibufsize = UFTDIIBUFSIZE;
315 		uca.ibufsizepad = uca.ibufsize;
316 		if (uca.obufsize == 0)
317 			uca.obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
318 		uca.opkthdrlen = sc->sc_hdrlen;
319 		uca.device = dev;
320 		uca.iface = iface;
321 		uca.methods = &uftdi_methods;
322 		uca.arg = sc;
323 		uca.info = NULL;
324 
325 		DPRINTF(("uftdi: in=0x%x out=0x%x isize=0x%x osize=0x%x\n",
326 			uca.bulkin, uca.bulkout,
327 			uca.ibufsize, uca.obufsize));
328 		sc->sc_subdev[idx] = config_found_sm_loc(self, "ucombus", NULL,
329 		    &uca, ucomprint, ucomsubmatch);
330 	}
331 
332 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
333 			   sc->sc_dev);
334 
335 	return;
336 
337 bad:
338 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
339 	sc->sc_dying = 1;
340 	return;
341 }
342 
343 int
344 uftdi_activate(device_t self, enum devact act)
345 {
346 	struct uftdi_softc *sc = device_private(self);
347 
348 	switch (act) {
349 	case DVACT_DEACTIVATE:
350 		sc->sc_dying = 1;
351 		return 0;
352 	default:
353 		return EOPNOTSUPP;
354 	}
355 }
356 
357 void
358 uftdi_childdet(device_t self, device_t child)
359 {
360 	int i;
361 	struct uftdi_softc *sc = device_private(self);
362 
363 	for (i = 0; i < sc->sc_numports; i++) {
364 		if (sc->sc_subdev[i] == child)
365 			break;
366 	}
367 	KASSERT(i < sc->sc_numports);
368 	sc->sc_subdev[i] = NULL;
369 }
370 
371 int
372 uftdi_detach(device_t self, int flags)
373 {
374 	struct uftdi_softc *sc = device_private(self);
375 	int i;
376 
377 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
378 	sc->sc_dying = 1;
379 	for (i=0; i < sc->sc_numports; i++) {
380 		if (sc->sc_subdev[i] != NULL)
381 			config_detach(sc->sc_subdev[i], flags);
382 	}
383 
384 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
385 			   sc->sc_dev);
386 
387 	return (0);
388 }
389 
390 Static int
391 uftdi_open(void *vsc, int portno)
392 {
393 	struct uftdi_softc *sc = vsc;
394 	usb_device_request_t req;
395 	usbd_status err;
396 	struct termios t;
397 
398 	DPRINTF(("uftdi_open: sc=%p\n", sc));
399 
400 	if (sc->sc_dying)
401 		return (EIO);
402 
403 	/* Perform a full reset on the device */
404 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
405 	req.bRequest = FTDI_SIO_RESET;
406 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
407 	USETW(req.wIndex, portno);
408 	USETW(req.wLength, 0);
409 	err = usbd_do_request(sc->sc_udev, &req, NULL);
410 	if (err)
411 		return (EIO);
412 
413 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
414 	t.c_ospeed = 9600;
415 	t.c_cflag = CSTOPB | CS8;
416 	(void)uftdi_param(sc, portno, &t);
417 
418 	/* Turn on RTS/CTS flow control */
419 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
420 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
421 	USETW(req.wValue, 0);
422 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
423 	USETW(req.wLength, 0);
424 	err = usbd_do_request(sc->sc_udev, &req, NULL);
425 	if (err)
426 		return (EIO);
427 
428 	return (0);
429 }
430 
431 Static void
432 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
433 {
434 	struct uftdi_softc *sc = vsc;
435 	u_char msr, lsr;
436 
437 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
438 		     *count));
439 
440 	msr = FTDI_GET_MSR(*ptr);
441 	lsr = FTDI_GET_LSR(*ptr);
442 
443 #ifdef UFTDI_DEBUG
444 	if (*count != 2)
445 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
446 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
447 #endif
448 
449 	if (sc->sc_msr != msr ||
450 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
451 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
452 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
453 			 lsr, sc->sc_lsr));
454 		sc->sc_msr = msr;
455 		sc->sc_lsr = lsr;
456 		ucom_status_change(device_private(sc->sc_subdev[portno-1]));
457 	}
458 
459 	/* Adjust buffer pointer to skip status prefix */
460 	*ptr += 2;
461 }
462 
463 Static void
464 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
465 {
466 	struct uftdi_softc *sc = vsc;
467 
468 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
469 		     vsc, portno, *count, from[0]));
470 
471 	/* Make length tag and copy data */
472 	if (sc->sc_hdrlen > 0)
473 		*to = FTDI_OUT_TAG(*count, portno);
474 
475 	memcpy(to + sc->sc_hdrlen, from, *count);
476 	*count += sc->sc_hdrlen;
477 }
478 
479 Static void
480 uftdi_set(void *vsc, int portno, int reg, int onoff)
481 {
482 	struct uftdi_softc *sc = vsc;
483 	usb_device_request_t req;
484 	int ctl;
485 
486 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
487 		 reg, onoff));
488 
489 	switch (reg) {
490 	case UCOM_SET_DTR:
491 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
492 		break;
493 	case UCOM_SET_RTS:
494 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
495 		break;
496 	case UCOM_SET_BREAK:
497 		uftdi_break(sc, portno, onoff);
498 		return;
499 	default:
500 		return;
501 	}
502 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
503 	req.bRequest = FTDI_SIO_MODEM_CTRL;
504 	USETW(req.wValue, ctl);
505 	USETW(req.wIndex, portno);
506 	USETW(req.wLength, 0);
507 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
508 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
509 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
510 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
511 }
512 
513 Static int
514 uftdi_param(void *vsc, int portno, struct termios *t)
515 {
516 	struct uftdi_softc *sc = vsc;
517 	usb_device_request_t req;
518 	usbd_status err;
519 	int rate, data, flow;
520 
521 	DPRINTF(("uftdi_param: sc=%p\n", sc));
522 
523 	if (sc->sc_dying)
524 		return (EIO);
525 
526 	switch (sc->sc_type) {
527 	case UFTDI_TYPE_SIO:
528 		switch (t->c_ospeed) {
529 		case 300: rate = ftdi_sio_b300; break;
530 		case 600: rate = ftdi_sio_b600; break;
531 		case 1200: rate = ftdi_sio_b1200; break;
532 		case 2400: rate = ftdi_sio_b2400; break;
533 		case 4800: rate = ftdi_sio_b4800; break;
534 		case 9600: rate = ftdi_sio_b9600; break;
535 		case 19200: rate = ftdi_sio_b19200; break;
536 		case 38400: rate = ftdi_sio_b38400; break;
537 		case 57600: rate = ftdi_sio_b57600; break;
538 		case 115200: rate = ftdi_sio_b115200; break;
539 		default:
540 			return (EINVAL);
541 		}
542 		break;
543 
544 	case UFTDI_TYPE_8U232AM:
545 		switch(t->c_ospeed) {
546 		case 300: rate = ftdi_8u232am_b300; break;
547 		case 600: rate = ftdi_8u232am_b600; break;
548 		case 1200: rate = ftdi_8u232am_b1200; break;
549 		case 2400: rate = ftdi_8u232am_b2400; break;
550 		case 4800: rate = ftdi_8u232am_b4800; break;
551 		case 9600: rate = ftdi_8u232am_b9600; break;
552 		case 19200: rate = ftdi_8u232am_b19200; break;
553 		case 38400: rate = ftdi_8u232am_b38400; break;
554 		case 57600: rate = ftdi_8u232am_b57600; break;
555 		case 115200: rate = ftdi_8u232am_b115200; break;
556 		case 230400: rate = ftdi_8u232am_b230400; break;
557 		case 460800: rate = ftdi_8u232am_b460800; break;
558 		case 921600: rate = ftdi_8u232am_b921600; break;
559 		default:
560 			return (EINVAL);
561 		}
562 		break;
563 
564 	default:
565 		return (EINVAL);
566 	}
567 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
568 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
569 	USETW(req.wValue, rate);
570 	USETW(req.wIndex, portno);
571 	USETW(req.wLength, 0);
572 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
573 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
574 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
575 	err = usbd_do_request(sc->sc_udev, &req, NULL);
576 	if (err)
577 		return (EIO);
578 
579 	if (ISSET(t->c_cflag, CSTOPB))
580 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
581 	else
582 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
583 	if (ISSET(t->c_cflag, PARENB)) {
584 		if (ISSET(t->c_cflag, PARODD))
585 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
586 		else
587 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
588 	} else
589 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
590 	switch (ISSET(t->c_cflag, CSIZE)) {
591 	case CS5:
592 		data |= FTDI_SIO_SET_DATA_BITS(5);
593 		break;
594 	case CS6:
595 		data |= FTDI_SIO_SET_DATA_BITS(6);
596 		break;
597 	case CS7:
598 		data |= FTDI_SIO_SET_DATA_BITS(7);
599 		break;
600 	case CS8:
601 		data |= FTDI_SIO_SET_DATA_BITS(8);
602 		break;
603 	}
604 	sc->last_lcr = data;
605 
606 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
607 	req.bRequest = FTDI_SIO_SET_DATA;
608 	USETW(req.wValue, data);
609 	USETW(req.wIndex, portno);
610 	USETW(req.wLength, 0);
611 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
612 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
613 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
614 	err = usbd_do_request(sc->sc_udev, &req, NULL);
615 	if (err)
616 		return (EIO);
617 
618 	if (ISSET(t->c_cflag, CRTSCTS)) {
619 		flow = FTDI_SIO_RTS_CTS_HS;
620 		USETW(req.wValue, 0);
621 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
622 		flow = FTDI_SIO_XON_XOFF_HS;
623 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
624 	} else {
625 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
626 		USETW(req.wValue, 0);
627 	}
628 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
629 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
630 	USETW2(req.wIndex, flow, portno);
631 	USETW(req.wLength, 0);
632 	err = usbd_do_request(sc->sc_udev, &req, NULL);
633 	if (err)
634 		return (EIO);
635 
636 	return (0);
637 }
638 
639 void
640 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
641 {
642 	struct uftdi_softc *sc = vsc;
643 
644 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
645 		 sc->sc_msr, sc->sc_lsr));
646 
647 	if (msr != NULL)
648 		*msr = sc->sc_msr;
649 	if (lsr != NULL)
650 		*lsr = sc->sc_lsr;
651 }
652 
653 void
654 uftdi_break(void *vsc, int portno, int onoff)
655 {
656 	struct uftdi_softc *sc = vsc;
657 	usb_device_request_t req;
658 	int data;
659 
660 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
661 		  onoff));
662 
663 	if (onoff) {
664 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
665 	} else {
666 		data = sc->last_lcr;
667 	}
668 
669 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
670 	req.bRequest = FTDI_SIO_SET_DATA;
671 	USETW(req.wValue, data);
672 	USETW(req.wIndex, portno);
673 	USETW(req.wLength, 0);
674 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
675 }
676