]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/usb/serial/uftdi.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / usb / serial / 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  * NOTE: all function names beginning like "uftdi_cfg_" can only
44  * be called from within the config thread function !
45  */
46
47 /*
48  * FTDI FT8U100AX serial adapter driver
49  */
50
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/bus.h>
59 #include <sys/linker_set.h>
60 #include <sys/module.h>
61 #include <sys/lock.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
65 #include <sys/sx.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
69 #include <sys/priv.h>
70
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdi_util.h>
74 #include "usbdevs.h"
75
76 #define USB_DEBUG_VAR uftdi_debug
77 #include <dev/usb/usb_debug.h>
78 #include <dev/usb/usb_process.h>
79
80 #include <dev/usb/serial/usb_serial.h>
81 #include <dev/usb/serial/uftdi_reg.h>
82
83 #ifdef USB_DEBUG
84 static int uftdi_debug = 0;
85
86 SYSCTL_NODE(_hw_usb, OID_AUTO, uftdi, CTLFLAG_RW, 0, "USB uftdi");
87 SYSCTL_INT(_hw_usb_uftdi, OID_AUTO, debug, CTLFLAG_RW,
88     &uftdi_debug, 0, "Debug level");
89 #endif
90
91 #define UFTDI_CONFIG_INDEX      0
92 #define UFTDI_IFACE_INDEX       0
93
94 #define UFTDI_OBUFSIZE 64               /* bytes, cannot be increased due to
95                                          * do size encoding */
96
97 enum {
98         UFTDI_BULK_DT_WR,
99         UFTDI_BULK_DT_RD,
100         UFTDI_N_TRANSFER,
101 };
102
103 struct uftdi_softc {
104         struct ucom_super_softc sc_super_ucom;
105         struct ucom_softc sc_ucom;
106
107         struct usb_device *sc_udev;
108         struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER];
109         device_t sc_dev;
110         struct mtx sc_mtx;
111
112         uint32_t sc_unit;
113         enum uftdi_type sc_type;
114
115         uint16_t sc_last_lcr;
116
117         uint8_t sc_iface_index;
118         uint8_t sc_hdrlen;
119         uint8_t sc_msr;
120         uint8_t sc_lsr;
121
122         uint8_t sc_name[16];
123 };
124
125 struct uftdi_param_config {
126         uint16_t rate;
127         uint16_t lcr;
128         uint8_t v_start;
129         uint8_t v_stop;
130         uint8_t v_flow;
131 };
132
133 /* prototypes */
134
135 static device_probe_t uftdi_probe;
136 static device_attach_t uftdi_attach;
137 static device_detach_t uftdi_detach;
138
139 static usb_callback_t uftdi_write_callback;
140 static usb_callback_t uftdi_read_callback;
141
142 static void     uftdi_cfg_open(struct ucom_softc *);
143 static void     uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t);
144 static void     uftdi_cfg_set_rts(struct ucom_softc *, uint8_t);
145 static void     uftdi_cfg_set_break(struct ucom_softc *, uint8_t);
146 static int      uftdi_set_parm_soft(struct termios *,
147                     struct uftdi_param_config *, uint8_t);
148 static int      uftdi_pre_param(struct ucom_softc *, struct termios *);
149 static void     uftdi_cfg_param(struct ucom_softc *, struct termios *);
150 static void     uftdi_cfg_get_status(struct ucom_softc *, uint8_t *,
151                     uint8_t *);
152 static void     uftdi_start_read(struct ucom_softc *);
153 static void     uftdi_stop_read(struct ucom_softc *);
154 static void     uftdi_start_write(struct ucom_softc *);
155 static void     uftdi_stop_write(struct ucom_softc *);
156 static uint8_t  uftdi_8u232am_getrate(uint32_t, uint16_t *);
157 static void     uftdi_poll(struct ucom_softc *ucom);
158
159 static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = {
160
161         [UFTDI_BULK_DT_WR] = {
162                 .type = UE_BULK,
163                 .endpoint = UE_ADDR_ANY,
164                 .direction = UE_DIR_OUT,
165                 .bufsize = UFTDI_OBUFSIZE,
166                 .flags = {.pipe_bof = 1,},
167                 .callback = &uftdi_write_callback,
168         },
169
170         [UFTDI_BULK_DT_RD] = {
171                 .type = UE_BULK,
172                 .endpoint = UE_ADDR_ANY,
173                 .direction = UE_DIR_IN,
174                 .bufsize = 0,           /* use wMaxPacketSize */
175                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
176                 .callback = &uftdi_read_callback,
177         },
178 };
179
180 static const struct ucom_callback uftdi_callback = {
181         .ucom_cfg_get_status = &uftdi_cfg_get_status,
182         .ucom_cfg_set_dtr = &uftdi_cfg_set_dtr,
183         .ucom_cfg_set_rts = &uftdi_cfg_set_rts,
184         .ucom_cfg_set_break = &uftdi_cfg_set_break,
185         .ucom_cfg_param = &uftdi_cfg_param,
186         .ucom_cfg_open = &uftdi_cfg_open,
187         .ucom_pre_param = &uftdi_pre_param,
188         .ucom_start_read = &uftdi_start_read,
189         .ucom_stop_read = &uftdi_stop_read,
190         .ucom_start_write = &uftdi_start_write,
191         .ucom_stop_write = &uftdi_stop_write,
192         .ucom_poll = &uftdi_poll,
193 };
194
195 static device_method_t uftdi_methods[] = {
196         /* Device interface */
197         DEVMETHOD(device_probe, uftdi_probe),
198         DEVMETHOD(device_attach, uftdi_attach),
199         DEVMETHOD(device_detach, uftdi_detach),
200
201         {0, 0}
202 };
203
204 static devclass_t uftdi_devclass;
205
206 static driver_t uftdi_driver = {
207         .name = "uftdi",
208         .methods = uftdi_methods,
209         .size = sizeof(struct uftdi_softc),
210 };
211
212 DRIVER_MODULE(uftdi, uhub, uftdi_driver, uftdi_devclass, NULL, 0);
213 MODULE_DEPEND(uftdi, ucom, 1, 1, 1);
214 MODULE_DEPEND(uftdi, usb, 1, 1, 1);
215
216 static struct usb_device_id uftdi_devs[] = {
217 #define UFTDI_DEV(v,p,t) \
218   { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, UFTDI_TYPE_##t) }
219         UFTDI_DEV(ATMEL, STK541, 8U232AM),
220         UFTDI_DEV(DRESDENELEKTRONIK, SENSORTERMINALBOARD, 8U232AM),
221         UFTDI_DEV(DRESDENELEKTRONIK, WIRELESSHANDHELDTERMINAL, 8U232AM),
222         UFTDI_DEV(FTDI, GAMMASCOUT, 8U232AM),
223         UFTDI_DEV(FTDI, SERIAL_8U100AX, SIO),
224         UFTDI_DEV(FTDI, SERIAL_2232C, 8U232AM),
225         UFTDI_DEV(FTDI, SERIAL_2232D, 8U232AM),
226         UFTDI_DEV(FTDI, SERIAL_4232H, 8U232AM),
227         UFTDI_DEV(FTDI, SERIAL_8U232AM, 8U232AM),
228         UFTDI_DEV(FTDI, SERIAL_8U232AM4, 8U232AM),
229         UFTDI_DEV(FTDI, SEMC_DSS20, 8U232AM),
230         UFTDI_DEV(FTDI, CFA_631, 8U232AM),
231         UFTDI_DEV(FTDI, CFA_632, 8U232AM),
232         UFTDI_DEV(FTDI, CFA_633, 8U232AM),
233         UFTDI_DEV(FTDI, CFA_634, 8U232AM),
234         UFTDI_DEV(FTDI, CFA_635, 8U232AM),
235         UFTDI_DEV(FTDI, USBSERIAL, 8U232AM),
236         UFTDI_DEV(FTDI, MX2_3, 8U232AM),
237         UFTDI_DEV(FTDI, MX4_5, 8U232AM),
238         UFTDI_DEV(FTDI, LK202, 8U232AM),
239         UFTDI_DEV(FTDI, LK204, 8U232AM),
240         UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13M, 8U232AM),
241         UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13S, 8U232AM),
242         UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13U, 8U232AM),
243         UFTDI_DEV(FTDI, EISCOU, 8U232AM),
244         UFTDI_DEV(FTDI, UOPTBR, 8U232AM),
245         UFTDI_DEV(FTDI, EMCU2D, 8U232AM),
246         UFTDI_DEV(FTDI, PCMSFU, 8U232AM),
247         UFTDI_DEV(FTDI, EMCU2H, 8U232AM),
248         UFTDI_DEV(FTDI, MAXSTREAM, 8U232AM),
249         UFTDI_DEV(FTDI, CTI_USB_NANO_485, 8U232AM),
250         UFTDI_DEV(FTDI, CTI_USB_MINI_485, 8U232AM),
251         UFTDI_DEV(SIIG2, US2308, 8U232AM),
252         UFTDI_DEV(INTREPIDCS, VALUECAN, 8U232AM),
253         UFTDI_DEV(INTREPIDCS, NEOVI, 8U232AM),
254         UFTDI_DEV(BBELECTRONICS, USOTL4, 8U232AM),
255         UFTDI_DEV(MARVELL, SHEEVAPLUG, 8U232AM),
256         UFTDI_DEV(MELCO, PCOPRS1, 8U232AM),
257         UFTDI_DEV(RATOC, REXUSB60F, 8U232AM),
258 #undef UFTDI_DEV
259 };
260
261 static int
262 uftdi_probe(device_t dev)
263 {
264         struct usb_attach_arg *uaa = device_get_ivars(dev);
265
266         if (uaa->usb_mode != USB_MODE_HOST) {
267                 return (ENXIO);
268         }
269         if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
270                 return (ENXIO);
271         }
272         /* attach to all present interfaces */
273
274         return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
275 }
276
277 static int
278 uftdi_attach(device_t dev)
279 {
280         struct usb_attach_arg *uaa = device_get_ivars(dev);
281         struct uftdi_softc *sc = device_get_softc(dev);
282         int error;
283
284         sc->sc_udev = uaa->device;
285         sc->sc_dev = dev;
286         sc->sc_unit = device_get_unit(dev);
287
288         device_set_usb_desc(dev);
289         mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF);
290
291         snprintf(sc->sc_name, sizeof(sc->sc_name),
292             "%s", device_get_nameunit(dev));
293
294         DPRINTF("\n");
295
296         sc->sc_iface_index = uaa->info.bIfaceIndex;
297         sc->sc_type = USB_GET_DRIVER_INFO(uaa);
298
299         switch (sc->sc_type) {
300         case UFTDI_TYPE_SIO:
301                 sc->sc_hdrlen = 1;
302                 break;
303         case UFTDI_TYPE_8U232AM:
304         default:
305                 sc->sc_hdrlen = 0;
306                 break;
307         }
308
309         error = usbd_transfer_setup(uaa->device,
310             &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
311             UFTDI_N_TRANSFER, sc, &sc->sc_mtx);
312
313         if (error) {
314                 device_printf(dev, "allocating USB "
315                     "transfers failed\n");
316                 goto detach;
317         }
318         sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
319
320         /* clear stall at first run */
321         mtx_lock(&sc->sc_mtx);
322         usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
323         usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
324         mtx_unlock(&sc->sc_mtx);
325
326         /* set a valid "lcr" value */
327
328         sc->sc_last_lcr =
329             (FTDI_SIO_SET_DATA_STOP_BITS_2 |
330             FTDI_SIO_SET_DATA_PARITY_NONE |
331             FTDI_SIO_SET_DATA_BITS(8));
332
333         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
334             &uftdi_callback, &sc->sc_mtx);
335         if (error) {
336                 goto detach;
337         }
338         return (0);                     /* success */
339
340 detach:
341         uftdi_detach(dev);
342         return (ENXIO);
343 }
344
345 static int
346 uftdi_detach(device_t dev)
347 {
348         struct uftdi_softc *sc = device_get_softc(dev);
349
350         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
351         usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
352         mtx_destroy(&sc->sc_mtx);
353
354         return (0);
355 }
356
357 static void
358 uftdi_cfg_open(struct ucom_softc *ucom)
359 {
360         struct uftdi_softc *sc = ucom->sc_parent;
361         uint16_t wIndex = ucom->sc_portno;
362         struct usb_device_request req;
363
364         DPRINTF("");
365
366         /* perform a full reset on the device */
367
368         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
369         req.bRequest = FTDI_SIO_RESET;
370         USETW(req.wValue, FTDI_SIO_RESET_SIO);
371         USETW(req.wIndex, wIndex);
372         USETW(req.wLength, 0);
373         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
374             &req, NULL, 0, 1000);
375
376         /* turn on RTS/CTS flow control */
377
378         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
379         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
380         USETW(req.wValue, 0);
381         USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
382         USETW(req.wLength, 0);
383         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
384             &req, NULL, 0, 1000);
385
386         /*
387          * NOTE: with the new UCOM layer there will always be a
388          * "uftdi_cfg_param()" call after "open()", so there is no need for
389          * "open()" to configure anything
390          */
391 }
392
393 static void
394 uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
395 {
396         struct uftdi_softc *sc = usbd_xfer_softc(xfer);
397         struct usb_page_cache *pc;
398         uint32_t actlen;
399         uint8_t buf[1];
400
401         switch (USB_GET_STATE(xfer)) {
402         case USB_ST_SETUP:
403         case USB_ST_TRANSFERRED:
404 tr_setup:
405                 pc = usbd_xfer_get_frame(xfer, 0);
406                 if (ucom_get_data(&sc->sc_ucom, pc,
407                     sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
408                     &actlen)) {
409
410                         if (sc->sc_hdrlen > 0) {
411                                 buf[0] =
412                                     FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
413                                 usbd_copy_in(pc, 0, buf, 1);
414                         }
415                         usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
416                         usbd_transfer_submit(xfer);
417                 }
418                 return;
419
420         default:                        /* Error */
421                 if (error != USB_ERR_CANCELLED) {
422                         /* try to clear stall first */
423                         usbd_xfer_set_stall(xfer);
424                         goto tr_setup;
425                 }
426                 return;
427         }
428 }
429
430 static void
431 uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
432 {
433         struct uftdi_softc *sc = usbd_xfer_softc(xfer);
434         struct usb_page_cache *pc;
435         uint8_t buf[2];
436         uint8_t ftdi_msr;
437         uint8_t msr;
438         uint8_t lsr;
439         int actlen;
440
441         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
442
443         switch (USB_GET_STATE(xfer)) {
444         case USB_ST_TRANSFERRED:
445
446                 if (actlen < 2) {
447                         goto tr_setup;
448                 }
449                 pc = usbd_xfer_get_frame(xfer, 0);
450                 usbd_copy_out(pc, 0, buf, 2);
451
452                 ftdi_msr = FTDI_GET_MSR(buf);
453                 lsr = FTDI_GET_LSR(buf);
454
455                 msr = 0;
456                 if (ftdi_msr & FTDI_SIO_CTS_MASK)
457                         msr |= SER_CTS;
458                 if (ftdi_msr & FTDI_SIO_DSR_MASK)
459                         msr |= SER_DSR;
460                 if (ftdi_msr & FTDI_SIO_RI_MASK)
461                         msr |= SER_RI;
462                 if (ftdi_msr & FTDI_SIO_RLSD_MASK)
463                         msr |= SER_DCD;
464
465                 if ((sc->sc_msr != msr) ||
466                     ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
467                         DPRINTF("status change msr=0x%02x (0x%02x) "
468                             "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
469                             lsr, sc->sc_lsr);
470
471                         sc->sc_msr = msr;
472                         sc->sc_lsr = lsr;
473
474                         ucom_status_change(&sc->sc_ucom);
475                 }
476                 actlen -= 2;
477
478                 if (actlen > 0) {
479                         ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
480                 }
481         case USB_ST_SETUP:
482 tr_setup:
483                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
484                 usbd_transfer_submit(xfer);
485                 return;
486
487         default:                        /* Error */
488                 if (error != USB_ERR_CANCELLED) {
489                         /* try to clear stall first */
490                         usbd_xfer_set_stall(xfer);
491                         goto tr_setup;
492                 }
493                 return;
494         }
495 }
496
497 static void
498 uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
499 {
500         struct uftdi_softc *sc = ucom->sc_parent;
501         uint16_t wIndex = ucom->sc_portno;
502         uint16_t wValue;
503         struct usb_device_request req;
504
505         wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
506
507         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
508         req.bRequest = FTDI_SIO_MODEM_CTRL;
509         USETW(req.wValue, wValue);
510         USETW(req.wIndex, wIndex);
511         USETW(req.wLength, 0);
512         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
513             &req, NULL, 0, 1000);
514 }
515
516 static void
517 uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
518 {
519         struct uftdi_softc *sc = ucom->sc_parent;
520         uint16_t wIndex = ucom->sc_portno;
521         uint16_t wValue;
522         struct usb_device_request req;
523
524         wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
525
526         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
527         req.bRequest = FTDI_SIO_MODEM_CTRL;
528         USETW(req.wValue, wValue);
529         USETW(req.wIndex, wIndex);
530         USETW(req.wLength, 0);
531         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
532             &req, NULL, 0, 1000);
533 }
534
535 static void
536 uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
537 {
538         struct uftdi_softc *sc = ucom->sc_parent;
539         uint16_t wIndex = ucom->sc_portno;
540         uint16_t wValue;
541         struct usb_device_request req;
542
543         if (onoff) {
544                 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
545         } else {
546                 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
547         }
548
549         wValue = sc->sc_last_lcr;
550
551         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
552         req.bRequest = FTDI_SIO_SET_DATA;
553         USETW(req.wValue, wValue);
554         USETW(req.wIndex, wIndex);
555         USETW(req.wLength, 0);
556         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
557             &req, NULL, 0, 1000);
558 }
559
560 static int
561 uftdi_set_parm_soft(struct termios *t,
562     struct uftdi_param_config *cfg, uint8_t type)
563 {
564         bzero(cfg, sizeof(*cfg));
565
566         switch (type) {
567         case UFTDI_TYPE_SIO:
568                 switch (t->c_ospeed) {
569                 case 300:
570                         cfg->rate = ftdi_sio_b300;
571                         break;
572                 case 600:
573                         cfg->rate = ftdi_sio_b600;
574                         break;
575                 case 1200:
576                         cfg->rate = ftdi_sio_b1200;
577                         break;
578                 case 2400:
579                         cfg->rate = ftdi_sio_b2400;
580                         break;
581                 case 4800:
582                         cfg->rate = ftdi_sio_b4800;
583                         break;
584                 case 9600:
585                         cfg->rate = ftdi_sio_b9600;
586                         break;
587                 case 19200:
588                         cfg->rate = ftdi_sio_b19200;
589                         break;
590                 case 38400:
591                         cfg->rate = ftdi_sio_b38400;
592                         break;
593                 case 57600:
594                         cfg->rate = ftdi_sio_b57600;
595                         break;
596                 case 115200:
597                         cfg->rate = ftdi_sio_b115200;
598                         break;
599                 default:
600                         return (EINVAL);
601                 }
602                 break;
603
604         case UFTDI_TYPE_8U232AM:
605                 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
606                         return (EINVAL);
607                 }
608                 break;
609         }
610
611         if (t->c_cflag & CSTOPB)
612                 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
613         else
614                 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
615
616         if (t->c_cflag & PARENB) {
617                 if (t->c_cflag & PARODD) {
618                         cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
619                 } else {
620                         cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
621                 }
622         } else {
623                 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
624         }
625
626         switch (t->c_cflag & CSIZE) {
627         case CS5:
628                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
629                 break;
630
631         case CS6:
632                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
633                 break;
634
635         case CS7:
636                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
637                 break;
638
639         case CS8:
640                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
641                 break;
642         }
643
644         if (t->c_cflag & CRTSCTS) {
645                 cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
646         } else if (t->c_iflag & (IXON | IXOFF)) {
647                 cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
648                 cfg->v_start = t->c_cc[VSTART];
649                 cfg->v_stop = t->c_cc[VSTOP];
650         } else {
651                 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
652         }
653
654         return (0);
655 }
656
657 static int
658 uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
659 {
660         struct uftdi_softc *sc = ucom->sc_parent;
661         struct uftdi_param_config cfg;
662
663         DPRINTF("\n");
664
665         return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
666 }
667
668 static void
669 uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
670 {
671         struct uftdi_softc *sc = ucom->sc_parent;
672         uint16_t wIndex = ucom->sc_portno;
673         struct uftdi_param_config cfg;
674         struct usb_device_request req;
675
676         if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
677                 /* should not happen */
678                 return;
679         }
680         sc->sc_last_lcr = cfg.lcr;
681
682         DPRINTF("\n");
683
684         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
685         req.bRequest = FTDI_SIO_SET_BAUD_RATE;
686         USETW(req.wValue, cfg.rate);
687         USETW(req.wIndex, wIndex);
688         USETW(req.wLength, 0);
689         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
690             &req, NULL, 0, 1000);
691
692         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
693         req.bRequest = FTDI_SIO_SET_DATA;
694         USETW(req.wValue, cfg.lcr);
695         USETW(req.wIndex, wIndex);
696         USETW(req.wLength, 0);
697         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
698             &req, NULL, 0, 1000);
699
700         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
701         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
702         USETW2(req.wValue, cfg.v_stop, cfg.v_start);
703         USETW2(req.wIndex, cfg.v_flow, wIndex);
704         USETW(req.wLength, 0);
705         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
706             &req, NULL, 0, 1000);
707 }
708
709 static void
710 uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
711 {
712         struct uftdi_softc *sc = ucom->sc_parent;
713
714         DPRINTF("msr=0x%02x lsr=0x%02x\n",
715             sc->sc_msr, sc->sc_lsr);
716
717         *msr = sc->sc_msr;
718         *lsr = sc->sc_lsr;
719 }
720
721 static void
722 uftdi_start_read(struct ucom_softc *ucom)
723 {
724         struct uftdi_softc *sc = ucom->sc_parent;
725
726         usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
727 }
728
729 static void
730 uftdi_stop_read(struct ucom_softc *ucom)
731 {
732         struct uftdi_softc *sc = ucom->sc_parent;
733
734         usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
735 }
736
737 static void
738 uftdi_start_write(struct ucom_softc *ucom)
739 {
740         struct uftdi_softc *sc = ucom->sc_parent;
741
742         usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
743 }
744
745 static void
746 uftdi_stop_write(struct ucom_softc *ucom)
747 {
748         struct uftdi_softc *sc = ucom->sc_parent;
749
750         usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
751 }
752
753 /*------------------------------------------------------------------------*
754  *      uftdi_8u232am_getrate
755  *
756  * Return values:
757  *    0: Success
758  * Else: Failure
759  *------------------------------------------------------------------------*/
760 static uint8_t
761 uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
762 {
763         /* Table of the nearest even powers-of-2 for values 0..15. */
764         static const uint8_t roundoff[16] = {
765                 0, 2, 2, 4, 4, 4, 8, 8,
766                 8, 8, 8, 8, 16, 16, 16, 16,
767         };
768         uint32_t d;
769         uint32_t freq;
770         uint16_t result;
771
772         if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
773                 return (1);             /* prevent numerical overflow */
774
775         /* Special cases for 2M and 3M. */
776         if ((speed >= ((3000000 * 100) / 103)) &&
777             (speed <= ((3000000 * 100) / 97))) {
778                 result = 0;
779                 goto done;
780         }
781         if ((speed >= ((2000000 * 100) / 103)) &&
782             (speed <= ((2000000 * 100) / 97))) {
783                 result = 1;
784                 goto done;
785         }
786         d = (FTDI_8U232AM_FREQ << 4) / speed;
787         d = (d & ~15) + roundoff[d & 15];
788
789         if (d < FTDI_8U232AM_MIN_DIV)
790                 d = FTDI_8U232AM_MIN_DIV;
791         else if (d > FTDI_8U232AM_MAX_DIV)
792                 d = FTDI_8U232AM_MAX_DIV;
793
794         /*
795          * Calculate the frequency needed for "d" to exactly divide down to
796          * our target "speed", and check that the actual frequency is within
797          * 3% of this.
798          */
799         freq = (speed * d);
800         if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
801             (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
802                 return (1);
803
804         /*
805          * Pack the divisor into the resultant value.  The lower 14-bits
806          * hold the integral part, while the upper 2 bits encode the
807          * fractional component: either 0, 0.5, 0.25, or 0.125.
808          */
809         result = (d >> 4);
810         if (d & 8)
811                 result |= 0x4000;
812         else if (d & 4)
813                 result |= 0x8000;
814         else if (d & 2)
815                 result |= 0xc000;
816
817 done:
818         *rate = result;
819         return (0);
820 }
821
822 static void
823 uftdi_poll(struct ucom_softc *ucom)
824 {
825         struct uftdi_softc *sc = ucom->sc_parent;
826         usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER);
827 }