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