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