30a25cf0d8082a3eb7a71c27f2b20af43d881b6e
[dragonfly.git] / sys / dev / usbmisc / uftdi / uftdi.c
1 /*
2  * $NetBSD: uftdi.c,v 1.13 2002/09/23 05:51:23 simonb Exp $
3  * $FreeBSD: src/sys/dev/usb/uftdi.c,v 1.37 2007/06/22 05:53:05 imp Exp $
4  * $DragonFly: src/sys/dev/usbmisc/uftdi/uftdi.c,v 1.15 2007/08/19 19:45:39 hasso Exp $
5  */
6
7 /*-
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
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>
53 #include <sys/module.h>
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
61 #include <sys/select.h>
62
63 #include <sys/sysctl.h>
64
65 #include <bus/usb/usb.h>
66 #include <bus/usb/usbhid.h>
67
68 #include <bus/usb/usbdi.h>
69 #include <bus/usb/usbdi_util.h>
70 #include <bus/usb/usbdevs.h>
71
72 #include "../ucom/ucomvar.h"
73
74 #include "uftdireg.h"
75
76 #ifdef USB_DEBUG
77 static int uftdidebug = 0;
78 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
79 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
80            &uftdidebug, 0, "uftdi debug level");
81 #define DPRINTF(x)      do { \
82                                 if (uftdidebug) \
83                                         kprintf x; \
84                         } while (0)
85
86 #define DPRINTFN(n, x)  do { \
87                                 if (uftdidebug > (n)) \
88                                         kprintf x; \
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
99
100 /*
101  * These are the maximum number of bytes transferred per frame.
102  * The output buffer size cannot be increased due to the size encoding.
103  */
104 #define UFTDIIBUFSIZE 64
105 #define UFTDIOBUFSIZE 64
106
107 struct uftdi_softc {
108         struct ucom_softc       sc_ucom;
109         usbd_interface_handle   sc_iface;       /* interface */
110         enum uftdi_type         sc_type;
111         u_int                   sc_hdrlen;
112         u_char                  sc_msr;
113         u_char                  sc_lsr;
114         u_int                   last_lcr;
115 };
116
117 static void     uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
118 static void     uftdi_set(void *, int, int, int);
119 static int      uftdi_param(void *, int, struct termios *);
120 static int      uftdi_open(void *sc, int portno);
121 static void     uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
122 static void     uftdi_write(void *sc, int portno, u_char *to, u_char *from,
123                             u_int32_t *count);
124 static void     uftdi_break(void *sc, int portno, int onoff);
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         char *devinfo;
202         int i;
203         usbd_status err;
204         struct ucom_softc *ucom = &sc->sc_ucom;
205         DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
206         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
207
208         ucom->sc_dev = self;
209         ucom->sc_udev = dev;
210
211         usbd_devinfo(dev, 0, devinfo);
212         device_printf(ucom->sc_dev, "%s\n", devinfo);
213         kfree(devinfo, M_USBDEV);
214
215         if (uaa->iface == NULL) {
216                 /* Move the device into the configured state. */
217                 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
218                 if (err) {
219                         device_printf(ucom->sc_dev,
220                             "failed to set configuration, err=%s\n",
221                             usbd_errstr(err));
222                         goto bad;
223                 }
224
225                 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
226                 if (err) {
227                         device_printf(ucom->sc_dev,
228                             "failed to get interface, err=%s\n", usbd_errstr(err));
229                         goto bad;
230                 }
231         } else {
232                 iface = uaa->iface;
233         }
234
235         id = usbd_get_interface_descriptor(iface);
236         ucom->sc_iface = iface;
237         switch( uaa->vendor ){
238         case USB_VENDOR_FTDI:
239                 switch( uaa->product ){
240                 case USB_PRODUCT_FTDI_SERIAL_8U100AX:
241                         sc->sc_type = UFTDI_TYPE_SIO;
242                         sc->sc_hdrlen = 1;
243                         break;
244                 case USB_PRODUCT_FTDI_SEMC_DSS20:
245                 case USB_PRODUCT_FTDI_SERIAL_8U232AM:
246                 case USB_PRODUCT_FTDI_SERIAL_2232C:
247                 case USB_PRODUCT_FTDI_CFA_631:
248                 case USB_PRODUCT_FTDI_CFA_632:
249                 case USB_PRODUCT_FTDI_CFA_633:
250                 case USB_PRODUCT_FTDI_CFA_634:
251                 case USB_PRODUCT_FTDI_CFA_635:
252                 case USB_PRODUCT_FTDI_USBSERIAL:
253                 case USB_PRODUCT_FTDI_MX2_3:
254                 case USB_PRODUCT_FTDI_MX4_5:
255                 case USB_PRODUCT_FTDI_LK202:
256                 case USB_PRODUCT_FTDI_LK204:
257                 case USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13M:
258                 case USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13S:
259                 case USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13U:
260                 case USB_PRODUCT_FTDI_EISCOU:
261                 case USB_PRODUCT_FTDI_UOPTBR:
262                 case USB_PRODUCT_FTDI_EMCU2D:
263                 case USB_PRODUCT_FTDI_PCMSFU:
264                 case USB_PRODUCT_FTDI_EMCU2H:
265                         sc->sc_type = UFTDI_TYPE_8U232AM;
266                         sc->sc_hdrlen = 0;
267                         break;
268
269                 default:                /* Can't happen */
270                         goto bad;
271                 }
272                 break;
273
274         case USB_VENDOR_INTREPIDCS:
275                 switch( uaa->product ){
276                 case USB_PRODUCT_INTREPIDCS_VALUECAN:
277                 case USB_PRODUCT_INTREPIDCS_NEOVI:
278                         sc->sc_type = UFTDI_TYPE_8U232AM;
279                         sc->sc_hdrlen = 0;
280                         break;
281
282                 default:                /* Can't happen */
283                         goto bad;
284                 }
285                 break;
286
287         case USB_VENDOR_SIIG2:
288                 switch( uaa->product ){
289                 case USB_PRODUCT_SIIG2_US2308:
290                         sc->sc_type = UFTDI_TYPE_8U232AM;
291                         sc->sc_hdrlen = 0;
292                         break;
293
294                 default:                /* Can't happen */
295                         goto bad;
296                 }
297                 break;
298
299         case USB_VENDOR_BBELECTRONICS:
300                 switch( uaa->product ){
301                 case USB_PRODUCT_BBELECTRONICS_USOTL4:
302                         sc->sc_type = UFTDI_TYPE_8U232AM;
303                         sc->sc_hdrlen = 0;
304                         break;
305
306                 default:                /* Can't happen */
307                         goto bad;
308                 }
309                 break;
310
311         case USB_VENDOR_MELCO:
312                 switch( uaa->product ){
313                 case USB_PRODUCT_MELCO_PCOPRS1:
314                         sc->sc_type = UFTDI_TYPE_8U232AM;
315                         sc->sc_hdrlen = 0;
316                         break;
317
318                 default:                /* Can't happen */
319                         goto bad;
320                 }
321                 break;
322
323         default:                /* Can't happen */
324                 goto bad;
325         }
326
327         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
328
329         for (i = 0; i < id->bNumEndpoints; i++) {
330                 int addr, dir, attr;
331                 ed = usbd_interface2endpoint_descriptor(iface, i);
332                 if (ed == NULL) {
333                         device_printf(ucom->sc_dev,
334                             "could not read endpoint descriptor\n");
335                         goto bad;
336                 }
337
338                 addr = ed->bEndpointAddress;
339                 dir = UE_GET_DIR(ed->bEndpointAddress);
340                 attr = ed->bmAttributes & UE_XFERTYPE;
341                 if (dir == UE_DIR_IN && attr == UE_BULK)
342                         ucom->sc_bulkin_no = addr;
343                 else if (dir == UE_DIR_OUT && attr == UE_BULK)
344                         ucom->sc_bulkout_no = addr;
345                 else {
346                         device_printf(ucom->sc_dev, "unexpected endpoint\n");
347                         goto bad;
348                 }
349         }
350         if (ucom->sc_bulkin_no == -1) {
351                 device_printf(ucom->sc_dev, "Could not find data bulk in\n");
352                 goto bad;
353         }
354         if (ucom->sc_bulkout_no == -1) {
355                 device_printf(ucom->sc_dev, "Could not find data bulk out\n");
356                 goto bad;
357         }
358         ucom->sc_parent  = sc;
359         if (uaa->iface == NULL)
360                 ucom->sc_portno = FTDI_PIT_SIOA;
361         else
362                 ucom->sc_portno = FTDI_PIT_SIOA + id->bInterfaceNumber;
363         /* bulkin, bulkout set above */
364
365         ucom->sc_ibufsize = UFTDIIBUFSIZE;
366         ucom->sc_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
367         ucom->sc_ibufsizepad = UFTDIIBUFSIZE;
368         ucom->sc_opkthdrlen = sc->sc_hdrlen;
369
370
371         ucom->sc_callback = &uftdi_callback;
372 #if 0
373         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
374           ucom->sc_dev);
375 #endif
376         DPRINTF(("uftdi: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
377         ucom_attach(&sc->sc_ucom);
378         return 0;
379
380 bad:
381         DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
382         ucom->sc_dying = 1;
383         return ENXIO;
384 }
385 #if 0
386 int
387 uftdi_activate(device_t self, enum devact act)
388 {
389         struct uftdi_softc *sc = (struct uftdi_softc *)self;
390         int rv = 0;
391
392         switch (act) {
393         case DVACT_ACTIVATE:
394                 return (EOPNOTSUPP);
395
396         case DVACT_DEACTIVATE:
397                 if (sc->sc_subdev != NULL)
398                         rv = config_deactivate(sc->sc_subdev);
399                 sc->sc_ucom.sc_dying = 1;
400                 break;
401         }
402         return (rv);
403 }
404 #endif
405
406 static int
407 uftdi_detach(device_t self)
408 {
409         struct uftdi_softc *sc = device_get_softc(self);
410
411         int rv = 0;
412
413         DPRINTF(("uftdi_detach: sc=%p\n", sc));
414         sc->sc_ucom.sc_dying = 1;
415         rv = ucom_detach(&sc->sc_ucom);
416
417         return rv;
418 }
419
420 static int
421 uftdi_open(void *vsc, int portno)
422 {
423         struct uftdi_softc *sc = vsc;
424         struct ucom_softc *ucom = &sc->sc_ucom;
425         usb_device_request_t req;
426         usbd_status err;
427         struct termios t;
428
429         DPRINTF(("uftdi_open: sc=%p\n", sc));
430
431         if (ucom->sc_dying)
432                 return (EIO);
433
434         /* Perform a full reset on the device */
435         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
436         req.bRequest = FTDI_SIO_RESET;
437         USETW(req.wValue, FTDI_SIO_RESET_SIO);
438         USETW(req.wIndex, portno);
439         USETW(req.wLength, 0);
440         err = usbd_do_request(ucom->sc_udev, &req, NULL);
441         if (err)
442                 return (EIO);
443
444         /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
445         t.c_ospeed = 9600;
446         t.c_cflag = CSTOPB | CS8;
447         (void)uftdi_param(sc, portno, &t);
448
449         /* Turn on RTS/CTS flow control */
450         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
451         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
452         USETW(req.wValue, 0);
453         USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
454         USETW(req.wLength, 0);
455         err = usbd_do_request(ucom->sc_udev, &req, NULL);
456         if (err)
457                 return (EIO);
458
459         return (0);
460 }
461
462 static void
463 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
464 {
465         struct uftdi_softc *sc = vsc;
466         u_char msr, lsr;
467
468         DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
469                      *count));
470
471         msr = FTDI_GET_MSR(*ptr);
472         lsr = FTDI_GET_LSR(*ptr);
473
474 #ifdef USB_DEBUG
475         if (*count != 2)
476                 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
477                             "0x%02x\n", sc, portno, *count, (*ptr)[2]));
478 #endif
479
480         if (sc->sc_msr != msr ||
481             (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
482                 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
483                          "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
484                          lsr, sc->sc_lsr));
485                 sc->sc_msr = msr;
486                 sc->sc_lsr = lsr;
487                 ucom_status_change(&sc->sc_ucom);
488         }
489
490         /* Pick up status and adjust data part. */
491         *ptr += 2;
492         *count -= 2;
493 }
494
495 static void
496 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
497 {
498         struct uftdi_softc *sc = vsc;
499
500         DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
501                      vsc, portno, *count, from[0]));
502
503         /* Make length tag and copy data */
504         if (sc->sc_hdrlen > 0)
505                 *to = FTDI_OUT_TAG(*count, portno);
506
507         memcpy(to + sc->sc_hdrlen, from, *count);
508         *count += sc->sc_hdrlen;
509 }
510
511 static void
512 uftdi_set(void *vsc, int portno, int reg, int onoff)
513 {
514         struct uftdi_softc *sc = vsc;
515         struct ucom_softc *ucom = vsc;
516         usb_device_request_t req;
517         int ctl;
518
519         DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
520                  reg, onoff));
521
522         switch (reg) {
523         case UCOM_SET_DTR:
524                 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
525                 break;
526         case UCOM_SET_RTS:
527                 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
528                 break;
529         case UCOM_SET_BREAK:
530                 uftdi_break(sc, portno, onoff);
531                 return;
532         default:
533                 return;
534         }
535         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
536         req.bRequest = FTDI_SIO_MODEM_CTRL;
537         USETW(req.wValue, ctl);
538         USETW(req.wIndex, portno);
539         USETW(req.wLength, 0);
540         DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
541                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
542                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
543         (void)usbd_do_request(ucom->sc_udev, &req, NULL);
544 }
545
546 static int
547 uftdi_param(void *vsc, int portno, struct termios *t)
548 {
549         struct uftdi_softc *sc = vsc;
550         struct ucom_softc *ucom = &sc->sc_ucom;
551         usb_device_request_t req;
552         usbd_status err;
553         int rate=0, data, flow;
554
555         DPRINTF(("uftdi_param: sc=%p\n", sc));
556
557         if (ucom->sc_dying)
558                 return (EIO);
559
560         switch (sc->sc_type) {
561         case UFTDI_TYPE_SIO:
562                 switch (t->c_ospeed) {
563                 case 300: rate = ftdi_sio_b300; break;
564                 case 600: rate = ftdi_sio_b600; break;
565                 case 1200: rate = ftdi_sio_b1200; break;
566                 case 2400: rate = ftdi_sio_b2400; break;
567                 case 4800: rate = ftdi_sio_b4800; break;
568                 case 9600: rate = ftdi_sio_b9600; break;
569                 case 19200: rate = ftdi_sio_b19200; break;
570                 case 38400: rate = ftdi_sio_b38400; break;
571                 case 57600: rate = ftdi_sio_b57600; break;
572                 case 115200: rate = ftdi_sio_b115200; break;
573                 default:
574                         return (EINVAL);
575                 }
576                 break;
577
578         case UFTDI_TYPE_8U232AM:
579                 switch(t->c_ospeed) {
580                 case 300: rate = ftdi_8u232am_b300; break;
581                 case 600: rate = ftdi_8u232am_b600; break;
582                 case 1200: rate = ftdi_8u232am_b1200; break;
583                 case 2400: rate = ftdi_8u232am_b2400; break;
584                 case 4800: rate = ftdi_8u232am_b4800; break;
585                 case 9600: rate = ftdi_8u232am_b9600; break;
586                 case 19200: rate = ftdi_8u232am_b19200; break;
587                 case 38400: rate = ftdi_8u232am_b38400; break;
588                 case 57600: rate = ftdi_8u232am_b57600; break;
589                 case 115200: rate = ftdi_8u232am_b115200; break;
590                 case 230400: rate = ftdi_8u232am_b230400; break;
591                 case 460800: rate = ftdi_8u232am_b460800; break;
592                 case 921600: rate = ftdi_8u232am_b921600; break;
593                 case 2000000: rate = ftdi_8u232am_b2000000; break;
594                 case 3000000: rate = ftdi_8u232am_b3000000; break;
595                 default:
596                         return (EINVAL);
597                 }
598                 break;
599         }
600         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
601         req.bRequest = FTDI_SIO_SET_BAUD_RATE;
602         USETW(req.wValue, rate);
603         USETW(req.wIndex, portno);
604         USETW(req.wLength, 0);
605         DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
606                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
607                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
608         err = usbd_do_request(ucom->sc_udev, &req, NULL);
609         if (err)
610                 return (EIO);
611
612         if (ISSET(t->c_cflag, CSTOPB))
613                 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
614         else
615                 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
616         if (ISSET(t->c_cflag, PARENB)) {
617                 if (ISSET(t->c_cflag, PARODD))
618                         data |= FTDI_SIO_SET_DATA_PARITY_ODD;
619                 else
620                         data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
621         } else
622                 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
623         switch (ISSET(t->c_cflag, CSIZE)) {
624         case CS5:
625                 data |= FTDI_SIO_SET_DATA_BITS(5);
626                 break;
627         case CS6:
628                 data |= FTDI_SIO_SET_DATA_BITS(6);
629                 break;
630         case CS7:
631                 data |= FTDI_SIO_SET_DATA_BITS(7);
632                 break;
633         case CS8:
634                 data |= FTDI_SIO_SET_DATA_BITS(8);
635                 break;
636         }
637         sc->last_lcr = data;
638
639         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
640         req.bRequest = FTDI_SIO_SET_DATA;
641         USETW(req.wValue, data);
642         USETW(req.wIndex, portno);
643         USETW(req.wLength, 0);
644         DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
645                     "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
646                     UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
647         err = usbd_do_request(ucom->sc_udev, &req, NULL);
648         if (err)
649                 return (EIO);
650
651         if (ISSET(t->c_cflag, CRTSCTS)) {
652                 flow = FTDI_SIO_RTS_CTS_HS;
653                 USETW(req.wValue, 0);
654         } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
655                 flow = FTDI_SIO_XON_XOFF_HS;
656                 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
657         } else {
658                 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
659                 USETW(req.wValue, 0);
660         }
661         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
662         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
663         USETW2(req.wIndex, flow, portno);
664         USETW(req.wLength, 0);
665         err = usbd_do_request(ucom->sc_udev, &req, NULL);
666         if (err)
667                 return (EIO);
668
669         return (0);
670 }
671
672 void
673 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
674 {
675         struct uftdi_softc *sc = vsc;
676
677         DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
678                  sc->sc_msr, sc->sc_lsr));
679
680         if (msr != NULL)
681                 *msr = sc->sc_msr;
682         if (lsr != NULL)
683                 *lsr = sc->sc_lsr;
684 }
685
686 void
687 uftdi_break(void *vsc, int portno, int onoff)
688 {
689         struct uftdi_softc *sc = vsc;
690         struct ucom_softc *ucom = vsc;
691
692         usb_device_request_t req;
693         int data;
694
695         DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
696                   onoff));
697
698         if (onoff) {
699                 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
700         } else {
701                 data = sc->last_lcr;
702         }
703
704         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
705         req.bRequest = FTDI_SIO_SET_DATA;
706         USETW(req.wValue, data);
707         USETW(req.wIndex, portno);
708         USETW(req.wLength, 0);
709         (void)usbd_do_request(ucom->sc_udev, &req, NULL);
710 }
711
712 static device_method_t uftdi_methods[] = {
713         /* Device interface */
714         DEVMETHOD(device_probe, uftdi_match),
715         DEVMETHOD(device_attach, uftdi_attach),
716         DEVMETHOD(device_detach, uftdi_detach),
717
718         { 0, 0 }
719 };
720
721 static driver_t uftdi_driver = {
722         "ucom",
723         uftdi_methods,
724         sizeof (struct uftdi_softc)
725 };
726
727 DRIVER_MODULE(uftdi, uhub, uftdi_driver, ucom_devclass, usbd_driver_load, 0);
728 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
729 MODULE_DEPEND(uftdi, ucom,UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);