]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/usb/uftdi.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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
125 struct ucom_callback uftdi_callback = {
126         uftdi_get_status,
127         uftdi_set,
128         uftdi_param,
129         NULL,
130         uftdi_open,
131         NULL,
132         uftdi_read,
133         uftdi_write,
134 };
135
136 static int
137 uftdi_match(device_t self)
138 {
139         struct usb_attach_arg *uaa = device_get_ivars(self);
140
141         if (uaa->iface != NULL) {
142                 if (uaa->vendor == USB_VENDOR_FTDI &&
143                     (uaa->product == USB_PRODUCT_FTDI_SERIAL_2232C))
144                         return (UMATCH_VENDOR_IFACESUBCLASS);
145                 return (UMATCH_NONE);
146         }
147
148         DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
149                      uaa->vendor, uaa->product));
150
151         if (uaa->vendor == USB_VENDOR_FTDI &&
152             (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX ||
153              uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM ||
154              uaa->product == USB_PRODUCT_FTDI_SEMC_DSS20 ||
155              uaa->product == USB_PRODUCT_FTDI_CFA_631 ||
156              uaa->product == USB_PRODUCT_FTDI_CFA_632 ||
157              uaa->product == USB_PRODUCT_FTDI_CFA_633 ||
158              uaa->product == USB_PRODUCT_FTDI_CFA_634 ||
159              uaa->product == USB_PRODUCT_FTDI_CFA_635 ||
160              uaa->product == USB_PRODUCT_FTDI_USBSERIAL ||
161              uaa->product == USB_PRODUCT_FTDI_MX2_3 ||
162              uaa->product == USB_PRODUCT_FTDI_MX4_5 ||
163              uaa->product == USB_PRODUCT_FTDI_LK202 ||
164              uaa->product == USB_PRODUCT_FTDI_LK204 ||
165              uaa->product == USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13M ||
166              uaa->product == USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13S ||
167              uaa->product == USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13U ||
168              uaa->product == USB_PRODUCT_FTDI_EISCOU ||
169              uaa->product == USB_PRODUCT_FTDI_UOPTBR ||
170              uaa->product == USB_PRODUCT_FTDI_EMCU2D ||
171              uaa->product == USB_PRODUCT_FTDI_PCMSFU ||
172              uaa->product == USB_PRODUCT_FTDI_EMCU2H ))
173                 return (UMATCH_VENDOR_PRODUCT);
174         if (uaa->vendor == USB_VENDOR_SIIG2 &&
175             (uaa->product == USB_PRODUCT_SIIG2_US2308))
176                 return (UMATCH_VENDOR_PRODUCT);
177         if (uaa->vendor == USB_VENDOR_INTREPIDCS &&
178             (uaa->product == USB_PRODUCT_INTREPIDCS_VALUECAN ||
179             uaa->product == USB_PRODUCT_INTREPIDCS_NEOVI))
180                 return (UMATCH_VENDOR_PRODUCT);
181         if (uaa->vendor == USB_VENDOR_BBELECTRONICS &&
182             (uaa->product == USB_PRODUCT_BBELECTRONICS_USOTL4))
183                 return (UMATCH_VENDOR_PRODUCT);
184         if (uaa->vendor == USB_VENDOR_MELCO &&
185             (uaa->product == USB_PRODUCT_MELCO_PCOPRS1))
186                 return (UMATCH_VENDOR_PRODUCT);
187
188         return (UMATCH_NONE);
189 }
190
191 static int
192 uftdi_attach(device_t self)
193 {
194         struct uftdi_softc *sc = device_get_softc(self);
195         struct usb_attach_arg *uaa = device_get_ivars(self);
196         usbd_device_handle dev = uaa->device;
197         usbd_interface_handle iface;
198         usb_interface_descriptor_t *id;
199         usb_endpoint_descriptor_t *ed;
200         int i;
201         usbd_status err;
202         struct ucom_softc *ucom = &sc->sc_ucom;
203         DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
204
205         ucom->sc_dev = self;
206         ucom->sc_udev = dev;
207
208         if (uaa->iface == NULL) {
209                 /* Move the device into the configured state. */
210                 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
211                 if (err) {
212                         device_printf(ucom->sc_dev,
213                             "failed to set configuration, err=%s\n",
214                             usbd_errstr(err));
215                         goto bad;
216                 }
217
218                 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
219                 if (err) {
220                         device_printf(ucom->sc_dev,
221                             "failed to get interface, err=%s\n", usbd_errstr(err));
222                         goto bad;
223                 }
224         } else {
225                 iface = uaa->iface;
226         }
227
228         id = usbd_get_interface_descriptor(iface);
229         ucom->sc_iface = iface;
230         switch( uaa->vendor ){
231         case USB_VENDOR_FTDI:
232                 switch( uaa->product ){
233                 case USB_PRODUCT_FTDI_SERIAL_8U100AX:
234                         sc->sc_type = UFTDI_TYPE_SIO;
235                         sc->sc_hdrlen = 1;
236                         break;
237                 case USB_PRODUCT_FTDI_SEMC_DSS20:
238                 case USB_PRODUCT_FTDI_SERIAL_8U232AM:
239                 case USB_PRODUCT_FTDI_SERIAL_2232C:
240                 case USB_PRODUCT_FTDI_CFA_631:
241                 case USB_PRODUCT_FTDI_CFA_632:
242                 case USB_PRODUCT_FTDI_CFA_633:
243                 case USB_PRODUCT_FTDI_CFA_634:
244                 case USB_PRODUCT_FTDI_CFA_635:
245                 case USB_PRODUCT_FTDI_USBSERIAL:
246                 case USB_PRODUCT_FTDI_MX2_3:
247                 case USB_PRODUCT_FTDI_MX4_5:
248                 case USB_PRODUCT_FTDI_LK202:
249                 case USB_PRODUCT_FTDI_LK204:
250                 case USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13M:
251                 case USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13S:
252                 case USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13U:
253                 case USB_PRODUCT_FTDI_EISCOU:
254                 case USB_PRODUCT_FTDI_UOPTBR:
255                 case USB_PRODUCT_FTDI_EMCU2D:
256                 case USB_PRODUCT_FTDI_PCMSFU:
257                 case USB_PRODUCT_FTDI_EMCU2H:
258                         sc->sc_type = UFTDI_TYPE_8U232AM;
259                         sc->sc_hdrlen = 0;
260                         break;
261
262                 default:                /* Can't happen */
263                         goto bad;
264                 }
265                 break;
266
267         case USB_VENDOR_INTREPIDCS:
268                 switch( uaa->product ){
269                 case USB_PRODUCT_INTREPIDCS_VALUECAN:
270                 case USB_PRODUCT_INTREPIDCS_NEOVI:
271                         sc->sc_type = UFTDI_TYPE_8U232AM;
272                         sc->sc_hdrlen = 0;
273                         break;
274
275                 default:                /* Can't happen */
276                         goto bad;
277                 }
278                 break;
279
280         case USB_VENDOR_SIIG2:
281                 switch( uaa->product ){
282                 case USB_PRODUCT_SIIG2_US2308:
283                         sc->sc_type = UFTDI_TYPE_8U232AM;
284                         sc->sc_hdrlen = 0;
285                         break;
286
287                 default:                /* Can't happen */
288                         goto bad;
289                 }
290                 break;
291
292         case USB_VENDOR_BBELECTRONICS:
293                 switch( uaa->product ){
294                 case USB_PRODUCT_BBELECTRONICS_USOTL4:
295                         sc->sc_type = UFTDI_TYPE_8U232AM;
296                         sc->sc_hdrlen = 0;
297                         break;
298
299                 default:                /* Can't happen */
300                         goto bad;
301                 }
302                 break;
303
304         case USB_VENDOR_MELCO:
305                 switch( uaa->product ){
306                 case USB_PRODUCT_MELCO_PCOPRS1:
307                         sc->sc_type = UFTDI_TYPE_8U232AM;
308                         sc->sc_hdrlen = 0;
309                         break;
310
311                 default:                /* Can't happen */
312                         goto bad;
313                 }
314                 break;
315
316         default:                /* Can't happen */
317                 goto bad;
318         }
319
320         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
321
322         for (i = 0; i < id->bNumEndpoints; i++) {
323                 int addr, dir, attr;
324                 ed = usbd_interface2endpoint_descriptor(iface, i);
325                 if (ed == NULL) {
326                         device_printf(ucom->sc_dev,
327                             "could not read endpoint descriptor\n");
328                         goto bad;
329                 }
330
331                 addr = ed->bEndpointAddress;
332                 dir = UE_GET_DIR(ed->bEndpointAddress);
333                 attr = ed->bmAttributes & UE_XFERTYPE;
334                 if (dir == UE_DIR_IN && attr == UE_BULK)
335                         ucom->sc_bulkin_no = addr;
336                 else if (dir == UE_DIR_OUT && attr == UE_BULK)
337                         ucom->sc_bulkout_no = addr;
338                 else {
339                         device_printf(ucom->sc_dev, "unexpected endpoint\n");
340                         goto bad;
341                 }
342         }
343         if (ucom->sc_bulkin_no == -1) {
344                 device_printf(ucom->sc_dev, "Could not find data bulk in\n");
345                 goto bad;
346         }
347         if (ucom->sc_bulkout_no == -1) {
348                 device_printf(ucom->sc_dev, "Could not find data bulk out\n");
349                 goto bad;
350         }
351         ucom->sc_parent  = sc;
352         if (uaa->iface == NULL)
353                 ucom->sc_portno = FTDI_PIT_SIOA;
354         else
355                 ucom->sc_portno = FTDI_PIT_SIOA + id->bInterfaceNumber;
356         /* bulkin, bulkout set above */
357
358         ucom->sc_ibufsize = UFTDIIBUFSIZE;
359         ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
360         ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
361         ucom->sc_opkthdrlen = sc->sc_hdrlen;
362
363
364         ucom->sc_callback = &uftdi_callback;
365 #if 0
366         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
367           ucom->sc_dev);
368 #endif
369         DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
370         ucom_attach(&sc->sc_ucom);
371         return 0;
372
373 bad:
374         DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
375         ucom->sc_dying = 1;
376         return ENXIO;
377 }
378 #if 0
379 int
380 uftdi_activate(device_t self, enum devact act)
381 {
382         struct uftdi_softc *sc = (struct uftdi_softc *)self;
383         int rv = 0;
384
385         switch (act) {
386         case DVACT_ACTIVATE:
387                 return (EOPNOTSUPP);
388
389         case DVACT_DEACTIVATE:
390                 if (sc->sc_subdev != NULL)
391                         rv = config_deactivate(sc->sc_subdev);
392                 sc->sc_ucom.sc_dying = 1;
393                 break;
394         }
395         return (rv);
396 }
397 #endif
398
399 static int
400 uftdi_detach(device_t self)
401 {
402         struct uftdi_softc *sc = device_get_softc(self);
403
404         int rv = 0;
405
406         DPRINTF(("uftdi_detach: sc=%p\n", sc));
407         sc->sc_ucom.sc_dying = 1;
408         rv = ucom_detach(&sc->sc_ucom);
409
410         return rv;
411 }
412
413 static int
414 uftdi_open(void *vsc, int portno)
415 {
416         struct uftdi_softc *sc = vsc;
417         struct ucom_softc *ucom = &sc->sc_ucom;
418         usb_device_request_t req;
419         usbd_status err;
420         struct termios t;
421
422         DPRINTF(("uftdi_open: sc=%p\n", sc));
423
424         if (ucom->sc_dying)
425                 return (EIO);
426
427         /* Perform a full reset on the device */
428         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
429         req.bRequest = FTDI_SIO_RESET;
430         USETW(req.wValue, FTDI_SIO_RESET_SIO);
431         USETW(req.wIndex, portno);
432         USETW(req.wLength, 0);
433         err = usbd_do_request(ucom->sc_udev, &req, NULL);
434         if (err)
435                 return (EIO);
436
437         /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
438         t.c_ospeed = 9600;
439         t.c_cflag = CSTOPB | CS8;
440         (void)uftdi_param(sc, portno, &t);
441
442         /* Turn on RTS/CTS flow control */
443         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
444         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
445         USETW(req.wValue, 0);
446         USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
447         USETW(req.wLength, 0);
448         err = usbd_do_request(ucom->sc_udev, &req, NULL);
449         if (err)
450                 return (EIO);
451
452         return (0);
453 }
454
455 static void
456 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
457 {
458         struct uftdi_softc *sc = vsc;
459         u_char msr, lsr;
460
461         DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
462                      *count));
463
464         msr = FTDI_GET_MSR(*ptr);
465         lsr = FTDI_GET_LSR(*ptr);
466
467 #ifdef USB_DEBUG
468         if (*count != 2)
469                 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
470                             "0x%02x\n", sc, portno, *count, (*ptr)[2]));
471 #endif
472
473         if (sc->sc_msr != msr ||
474             (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
475                 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
476                          "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
477                          lsr, sc->sc_lsr));
478                 sc->sc_msr = msr;
479                 sc->sc_lsr = lsr;
480                 ucom_status_change(&sc->sc_ucom);
481         }
482
483         /* Pick up status and adjust data part. */
484         *ptr += 2;
485         *count -= 2;
486 }
487
488 static void
489 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
490 {
491         struct uftdi_softc *sc = vsc;
492
493         DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
494                      vsc, portno, *count, from[0]));
495
496         /* Make length tag and copy data */
497         if (sc->sc_hdrlen > 0)
498                 *to = FTDI_OUT_TAG(*count, portno);
499
500         memcpy(to + sc->sc_hdrlen, from, *count);
501         *count += sc->sc_hdrlen;
502 }
503
504 static void
505 uftdi_set(void *vsc, int portno, int reg, int onoff)
506 {
507         struct uftdi_softc *sc = vsc;
508         struct ucom_softc *ucom = vsc;
509         usb_device_request_t req;
510         int ctl;
511
512         DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
513                  reg, onoff));
514
515         switch (reg) {
516         case UCOM_SET_DTR:
517                 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
518                 break;
519         case UCOM_SET_RTS:
520                 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
521                 break;
522         case UCOM_SET_BREAK:
523                 uftdi_break(sc, portno, onoff);
524                 return;
525         default:
526                 return;
527         }
528         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
529         req.bRequest = FTDI_SIO_MODEM_CTRL;
530         USETW(req.wValue, ctl);
531         USETW(req.wIndex, portno);
532         USETW(req.wLength, 0);
533         DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
534                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
535                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
536         (void)usbd_do_request(ucom->sc_udev, &req, NULL);
537 }
538
539 static int
540 uftdi_param(void *vsc, int portno, struct termios *t)
541 {
542         struct uftdi_softc *sc = vsc;
543         struct ucom_softc *ucom = &sc->sc_ucom;
544         usb_device_request_t req;
545         usbd_status err;
546         int rate=0, data, flow;
547
548         DPRINTF(("uftdi_param: sc=%p\n", sc));
549
550         if (ucom->sc_dying)
551                 return (EIO);
552
553         switch (sc->sc_type) {
554         case UFTDI_TYPE_SIO:
555                 switch (t->c_ospeed) {
556                 case 300: rate = ftdi_sio_b300; break;
557                 case 600: rate = ftdi_sio_b600; break;
558                 case 1200: rate = ftdi_sio_b1200; break;
559                 case 2400: rate = ftdi_sio_b2400; break;
560                 case 4800: rate = ftdi_sio_b4800; break;
561                 case 9600: rate = ftdi_sio_b9600; break;
562                 case 19200: rate = ftdi_sio_b19200; break;
563                 case 38400: rate = ftdi_sio_b38400; break;
564                 case 57600: rate = ftdi_sio_b57600; break;
565                 case 115200: rate = ftdi_sio_b115200; break;
566                 default:
567                         return (EINVAL);
568                 }
569                 break;
570
571         case UFTDI_TYPE_8U232AM:
572                 switch(t->c_ospeed) {
573                 case 300: rate = ftdi_8u232am_b300; break;
574                 case 600: rate = ftdi_8u232am_b600; break;
575                 case 1200: rate = ftdi_8u232am_b1200; break;
576                 case 2400: rate = ftdi_8u232am_b2400; break;
577                 case 4800: rate = ftdi_8u232am_b4800; break;
578                 case 9600: rate = ftdi_8u232am_b9600; break;
579                 case 19200: rate = ftdi_8u232am_b19200; break;
580                 case 38400: rate = ftdi_8u232am_b38400; break;
581                 case 57600: rate = ftdi_8u232am_b57600; break;
582                 case 115200: rate = ftdi_8u232am_b115200; break;
583                 case 230400: rate = ftdi_8u232am_b230400; break;
584                 case 460800: rate = ftdi_8u232am_b460800; break;
585                 case 921600: rate = ftdi_8u232am_b921600; break;
586                 case 2000000: rate = ftdi_8u232am_b2000000; break;
587                 case 3000000: rate = ftdi_8u232am_b3000000; break;
588                 default:
589                         return (EINVAL);
590                 }
591                 break;
592         }
593         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
594         req.bRequest = FTDI_SIO_SET_BAUD_RATE;
595         USETW(req.wValue, rate);
596         USETW(req.wIndex, portno);
597         USETW(req.wLength, 0);
598         DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
599                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
600                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
601         err = usbd_do_request(ucom->sc_udev, &req, NULL);
602         if (err)
603                 return (EIO);
604
605         if (ISSET(t->c_cflag, CSTOPB))
606                 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
607         else
608                 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
609         if (ISSET(t->c_cflag, PARENB)) {
610                 if (ISSET(t->c_cflag, PARODD))
611                         data |= FTDI_SIO_SET_DATA_PARITY_ODD;
612                 else
613                         data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
614         } else
615                 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
616         switch (ISSET(t->c_cflag, CSIZE)) {
617         case CS5:
618                 data |= FTDI_SIO_SET_DATA_BITS(5);
619                 break;
620         case CS6:
621                 data |= FTDI_SIO_SET_DATA_BITS(6);
622                 break;
623         case CS7:
624                 data |= FTDI_SIO_SET_DATA_BITS(7);
625                 break;
626         case CS8:
627                 data |= FTDI_SIO_SET_DATA_BITS(8);
628                 break;
629         }
630         sc->last_lcr = data;
631
632         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
633         req.bRequest = FTDI_SIO_SET_DATA;
634         USETW(req.wValue, data);
635         USETW(req.wIndex, portno);
636         USETW(req.wLength, 0);
637         DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
638                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
639                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
640         err = usbd_do_request(ucom->sc_udev, &req, NULL);
641         if (err)
642                 return (EIO);
643
644         if (ISSET(t->c_cflag, CRTSCTS)) {
645                 flow = FTDI_SIO_RTS_CTS_HS;
646                 USETW(req.wValue, 0);
647         } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
648                 flow = FTDI_SIO_XON_XOFF_HS;
649                 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
650         } else {
651                 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
652                 USETW(req.wValue, 0);
653         }
654         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
655         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
656         USETW2(req.wIndex, flow, portno);
657         USETW(req.wLength, 0);
658         err = usbd_do_request(ucom->sc_udev, &req, NULL);
659         if (err)
660                 return (EIO);
661
662         return (0);
663 }
664
665 void
666 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
667 {
668         struct uftdi_softc *sc = vsc;
669
670         DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
671                  sc->sc_msr, sc->sc_lsr));
672
673         if (msr != NULL)
674                 *msr = sc->sc_msr;
675         if (lsr != NULL)
676                 *lsr = sc->sc_lsr;
677 }
678
679 void
680 uftdi_break(void *vsc, int portno, int onoff)
681 {
682         struct uftdi_softc *sc = vsc;
683         struct ucom_softc *ucom = vsc;
684
685         usb_device_request_t req;
686         int data;
687
688         DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
689                   onoff));
690
691         if (onoff) {
692                 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
693         } else {
694                 data = sc->last_lcr;
695         }
696
697         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
698         req.bRequest = FTDI_SIO_SET_DATA;
699         USETW(req.wValue, data);
700         USETW(req.wIndex, portno);
701         USETW(req.wLength, 0);
702         (void)usbd_do_request(ucom->sc_udev, &req, NULL);
703 }
704
705 static device_method_t uftdi_methods[] = {
706         /* Device interface */
707         DEVMETHOD(device_probe, uftdi_match),
708         DEVMETHOD(device_attach, uftdi_attach),
709         DEVMETHOD(device_detach, uftdi_detach),
710
711         { 0, 0 }
712 };
713
714 static driver_t uftdi_driver = {
715         "ucom",
716         uftdi_methods,
717         sizeof (struct uftdi_softc)
718 };
719
720 DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
721 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
722 MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);