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