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