]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/dev/usb/serial/uftdi.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.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 MODULE_VERSION(uftdi, 1);
216
217 static struct usb_device_id uftdi_devs[] = {
218 #define UFTDI_DEV(v,p,t) \
219   { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, UFTDI_TYPE_##t) }
220         UFTDI_DEV(ATMEL, STK541, 8U232AM),
221         UFTDI_DEV(DRESDENELEKTRONIK, SENSORTERMINALBOARD, 8U232AM),
222         UFTDI_DEV(DRESDENELEKTRONIK, WIRELESSHANDHELDTERMINAL, 8U232AM),
223         UFTDI_DEV(FTDI, GAMMASCOUT, 8U232AM),
224         UFTDI_DEV(FTDI, SERIAL_8U100AX, SIO),
225         UFTDI_DEV(FTDI, SERIAL_2232C, 8U232AM),
226         UFTDI_DEV(FTDI, SERIAL_2232D, 8U232AM),
227         UFTDI_DEV(FTDI, SERIAL_4232H, 8U232AM),
228         UFTDI_DEV(FTDI, SERIAL_8U232AM, 8U232AM),
229         UFTDI_DEV(FTDI, SERIAL_8U232AM4, 8U232AM),
230         UFTDI_DEV(FTDI, SEMC_DSS20, 8U232AM),
231         UFTDI_DEV(FTDI, CFA_631, 8U232AM),
232         UFTDI_DEV(FTDI, CFA_632, 8U232AM),
233         UFTDI_DEV(FTDI, CFA_633, 8U232AM),
234         UFTDI_DEV(FTDI, CFA_634, 8U232AM),
235         UFTDI_DEV(FTDI, CFA_635, 8U232AM),
236         UFTDI_DEV(FTDI, USB_UIRT, 8U232AM),
237         UFTDI_DEV(FTDI, USBSERIAL, 8U232AM),
238         UFTDI_DEV(FTDI, KBS, 8U232AM),
239         UFTDI_DEV(FTDI, MX2_3, 8U232AM),
240         UFTDI_DEV(FTDI, MX4_5, 8U232AM),
241         UFTDI_DEV(FTDI, LK202, 8U232AM),
242         UFTDI_DEV(FTDI, LK204, 8U232AM),
243         UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13M, 8U232AM),
244         UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13S, 8U232AM),
245         UFTDI_DEV(FTDI, TACTRIX_OPENPORT_13U, 8U232AM),
246         UFTDI_DEV(FTDI, EISCOU, 8U232AM),
247         UFTDI_DEV(FTDI, UOPTBR, 8U232AM),
248         UFTDI_DEV(FTDI, EMCU2D, 8U232AM),
249         UFTDI_DEV(FTDI, PCMSFU, 8U232AM),
250         UFTDI_DEV(FTDI, EMCU2H, 8U232AM),
251         UFTDI_DEV(FTDI, MAXSTREAM, 8U232AM),
252         UFTDI_DEV(FTDI, CTI_USB_NANO_485, 8U232AM),
253         UFTDI_DEV(FTDI, CTI_USB_MINI_485, 8U232AM),
254         UFTDI_DEV(SIIG2, US2308, 8U232AM),
255         UFTDI_DEV(INTREPIDCS, VALUECAN, 8U232AM),
256         UFTDI_DEV(INTREPIDCS, NEOVI, 8U232AM),
257         UFTDI_DEV(BBELECTRONICS, USOTL4, 8U232AM),
258         UFTDI_DEV(MATRIXORBITAL, MOUA, 8U232AM),
259         UFTDI_DEV(MARVELL, SHEEVAPLUG, 8U232AM),
260         UFTDI_DEV(MELCO, PCOPRS1, 8U232AM),
261         UFTDI_DEV(RATOC, REXUSB60F, 8U232AM),
262 #undef UFTDI_DEV
263 };
264
265 static int
266 uftdi_probe(device_t dev)
267 {
268         struct usb_attach_arg *uaa = device_get_ivars(dev);
269
270         if (uaa->usb_mode != USB_MODE_HOST) {
271                 return (ENXIO);
272         }
273         if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
274                 return (ENXIO);
275         }
276         /* attach to all present interfaces */
277
278         return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
279 }
280
281 static int
282 uftdi_attach(device_t dev)
283 {
284         struct usb_attach_arg *uaa = device_get_ivars(dev);
285         struct uftdi_softc *sc = device_get_softc(dev);
286         int error;
287
288         sc->sc_udev = uaa->device;
289         sc->sc_dev = dev;
290         sc->sc_unit = device_get_unit(dev);
291
292         device_set_usb_desc(dev);
293         mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF);
294
295         snprintf(sc->sc_name, sizeof(sc->sc_name),
296             "%s", device_get_nameunit(dev));
297
298         DPRINTF("\n");
299
300         sc->sc_iface_index = uaa->info.bIfaceIndex;
301         sc->sc_type = USB_GET_DRIVER_INFO(uaa);
302
303         switch (sc->sc_type) {
304         case UFTDI_TYPE_SIO:
305                 sc->sc_hdrlen = 1;
306                 break;
307         case UFTDI_TYPE_8U232AM:
308         default:
309                 sc->sc_hdrlen = 0;
310                 break;
311         }
312
313         error = usbd_transfer_setup(uaa->device,
314             &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
315             UFTDI_N_TRANSFER, sc, &sc->sc_mtx);
316
317         if (error) {
318                 device_printf(dev, "allocating USB "
319                     "transfers failed\n");
320                 goto detach;
321         }
322         sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
323
324         /* clear stall at first run */
325         mtx_lock(&sc->sc_mtx);
326         usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
327         usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
328         mtx_unlock(&sc->sc_mtx);
329
330         /* set a valid "lcr" value */
331
332         sc->sc_last_lcr =
333             (FTDI_SIO_SET_DATA_STOP_BITS_2 |
334             FTDI_SIO_SET_DATA_PARITY_NONE |
335             FTDI_SIO_SET_DATA_BITS(8));
336
337         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
338             &uftdi_callback, &sc->sc_mtx);
339         if (error) {
340                 goto detach;
341         }
342         return (0);                     /* success */
343
344 detach:
345         uftdi_detach(dev);
346         return (ENXIO);
347 }
348
349 static int
350 uftdi_detach(device_t dev)
351 {
352         struct uftdi_softc *sc = device_get_softc(dev);
353
354         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
355         usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
356         mtx_destroy(&sc->sc_mtx);
357
358         return (0);
359 }
360
361 static void
362 uftdi_cfg_open(struct ucom_softc *ucom)
363 {
364         struct uftdi_softc *sc = ucom->sc_parent;
365         uint16_t wIndex = ucom->sc_portno;
366         struct usb_device_request req;
367
368         DPRINTF("");
369
370         /* perform a full reset on the device */
371
372         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
373         req.bRequest = FTDI_SIO_RESET;
374         USETW(req.wValue, FTDI_SIO_RESET_SIO);
375         USETW(req.wIndex, wIndex);
376         USETW(req.wLength, 0);
377         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
378             &req, NULL, 0, 1000);
379
380         /* turn on RTS/CTS flow control */
381
382         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
383         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
384         USETW(req.wValue, 0);
385         USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
386         USETW(req.wLength, 0);
387         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
388             &req, NULL, 0, 1000);
389
390         /*
391          * NOTE: with the new UCOM layer there will always be a
392          * "uftdi_cfg_param()" call after "open()", so there is no need for
393          * "open()" to configure anything
394          */
395 }
396
397 static void
398 uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
399 {
400         struct uftdi_softc *sc = usbd_xfer_softc(xfer);
401         struct usb_page_cache *pc;
402         uint32_t actlen;
403         uint8_t buf[1];
404
405         switch (USB_GET_STATE(xfer)) {
406         case USB_ST_SETUP:
407         case USB_ST_TRANSFERRED:
408 tr_setup:
409                 pc = usbd_xfer_get_frame(xfer, 0);
410                 if (ucom_get_data(&sc->sc_ucom, pc,
411                     sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
412                     &actlen)) {
413
414                         if (sc->sc_hdrlen > 0) {
415                                 buf[0] =
416                                     FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
417                                 usbd_copy_in(pc, 0, buf, 1);
418                         }
419                         usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
420                         usbd_transfer_submit(xfer);
421                 }
422                 return;
423
424         default:                        /* Error */
425                 if (error != USB_ERR_CANCELLED) {
426                         /* try to clear stall first */
427                         usbd_xfer_set_stall(xfer);
428                         goto tr_setup;
429                 }
430                 return;
431         }
432 }
433
434 static void
435 uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
436 {
437         struct uftdi_softc *sc = usbd_xfer_softc(xfer);
438         struct usb_page_cache *pc;
439         uint8_t buf[2];
440         uint8_t ftdi_msr;
441         uint8_t msr;
442         uint8_t lsr;
443         int actlen;
444
445         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
446
447         switch (USB_GET_STATE(xfer)) {
448         case USB_ST_TRANSFERRED:
449
450                 if (actlen < 2) {
451                         goto tr_setup;
452                 }
453                 pc = usbd_xfer_get_frame(xfer, 0);
454                 usbd_copy_out(pc, 0, buf, 2);
455
456                 ftdi_msr = FTDI_GET_MSR(buf);
457                 lsr = FTDI_GET_LSR(buf);
458
459                 msr = 0;
460                 if (ftdi_msr & FTDI_SIO_CTS_MASK)
461                         msr |= SER_CTS;
462                 if (ftdi_msr & FTDI_SIO_DSR_MASK)
463                         msr |= SER_DSR;
464                 if (ftdi_msr & FTDI_SIO_RI_MASK)
465                         msr |= SER_RI;
466                 if (ftdi_msr & FTDI_SIO_RLSD_MASK)
467                         msr |= SER_DCD;
468
469                 if ((sc->sc_msr != msr) ||
470                     ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
471                         DPRINTF("status change msr=0x%02x (0x%02x) "
472                             "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
473                             lsr, sc->sc_lsr);
474
475                         sc->sc_msr = msr;
476                         sc->sc_lsr = lsr;
477
478                         ucom_status_change(&sc->sc_ucom);
479                 }
480                 actlen -= 2;
481
482                 if (actlen > 0) {
483                         ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
484                 }
485         case USB_ST_SETUP:
486 tr_setup:
487                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
488                 usbd_transfer_submit(xfer);
489                 return;
490
491         default:                        /* Error */
492                 if (error != USB_ERR_CANCELLED) {
493                         /* try to clear stall first */
494                         usbd_xfer_set_stall(xfer);
495                         goto tr_setup;
496                 }
497                 return;
498         }
499 }
500
501 static void
502 uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
503 {
504         struct uftdi_softc *sc = ucom->sc_parent;
505         uint16_t wIndex = ucom->sc_portno;
506         uint16_t wValue;
507         struct usb_device_request req;
508
509         wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
510
511         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
512         req.bRequest = FTDI_SIO_MODEM_CTRL;
513         USETW(req.wValue, wValue);
514         USETW(req.wIndex, wIndex);
515         USETW(req.wLength, 0);
516         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
517             &req, NULL, 0, 1000);
518 }
519
520 static void
521 uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
522 {
523         struct uftdi_softc *sc = ucom->sc_parent;
524         uint16_t wIndex = ucom->sc_portno;
525         uint16_t wValue;
526         struct usb_device_request req;
527
528         wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
529
530         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
531         req.bRequest = FTDI_SIO_MODEM_CTRL;
532         USETW(req.wValue, wValue);
533         USETW(req.wIndex, wIndex);
534         USETW(req.wLength, 0);
535         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
536             &req, NULL, 0, 1000);
537 }
538
539 static void
540 uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
541 {
542         struct uftdi_softc *sc = ucom->sc_parent;
543         uint16_t wIndex = ucom->sc_portno;
544         uint16_t wValue;
545         struct usb_device_request req;
546
547         if (onoff) {
548                 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
549         } else {
550                 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
551         }
552
553         wValue = sc->sc_last_lcr;
554
555         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
556         req.bRequest = FTDI_SIO_SET_DATA;
557         USETW(req.wValue, wValue);
558         USETW(req.wIndex, wIndex);
559         USETW(req.wLength, 0);
560         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
561             &req, NULL, 0, 1000);
562 }
563
564 static int
565 uftdi_set_parm_soft(struct termios *t,
566     struct uftdi_param_config *cfg, uint8_t type)
567 {
568         bzero(cfg, sizeof(*cfg));
569
570         switch (type) {
571         case UFTDI_TYPE_SIO:
572                 switch (t->c_ospeed) {
573                 case 300:
574                         cfg->rate = ftdi_sio_b300;
575                         break;
576                 case 600:
577                         cfg->rate = ftdi_sio_b600;
578                         break;
579                 case 1200:
580                         cfg->rate = ftdi_sio_b1200;
581                         break;
582                 case 2400:
583                         cfg->rate = ftdi_sio_b2400;
584                         break;
585                 case 4800:
586                         cfg->rate = ftdi_sio_b4800;
587                         break;
588                 case 9600:
589                         cfg->rate = ftdi_sio_b9600;
590                         break;
591                 case 19200:
592                         cfg->rate = ftdi_sio_b19200;
593                         break;
594                 case 38400:
595                         cfg->rate = ftdi_sio_b38400;
596                         break;
597                 case 57600:
598                         cfg->rate = ftdi_sio_b57600;
599                         break;
600                 case 115200:
601                         cfg->rate = ftdi_sio_b115200;
602                         break;
603                 default:
604                         return (EINVAL);
605                 }
606                 break;
607
608         case UFTDI_TYPE_8U232AM:
609                 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
610                         return (EINVAL);
611                 }
612                 break;
613         }
614
615         if (t->c_cflag & CSTOPB)
616                 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
617         else
618                 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
619
620         if (t->c_cflag & PARENB) {
621                 if (t->c_cflag & PARODD) {
622                         cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
623                 } else {
624                         cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
625                 }
626         } else {
627                 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
628         }
629
630         switch (t->c_cflag & CSIZE) {
631         case CS5:
632                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
633                 break;
634
635         case CS6:
636                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
637                 break;
638
639         case CS7:
640                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
641                 break;
642
643         case CS8:
644                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
645                 break;
646         }
647
648         if (t->c_cflag & CRTSCTS) {
649                 cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
650         } else if (t->c_iflag & (IXON | IXOFF)) {
651                 cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
652                 cfg->v_start = t->c_cc[VSTART];
653                 cfg->v_stop = t->c_cc[VSTOP];
654         } else {
655                 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
656         }
657
658         return (0);
659 }
660
661 static int
662 uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
663 {
664         struct uftdi_softc *sc = ucom->sc_parent;
665         struct uftdi_param_config cfg;
666
667         DPRINTF("\n");
668
669         return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
670 }
671
672 static void
673 uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
674 {
675         struct uftdi_softc *sc = ucom->sc_parent;
676         uint16_t wIndex = ucom->sc_portno;
677         struct uftdi_param_config cfg;
678         struct usb_device_request req;
679
680         if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
681                 /* should not happen */
682                 return;
683         }
684         sc->sc_last_lcr = cfg.lcr;
685
686         DPRINTF("\n");
687
688         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
689         req.bRequest = FTDI_SIO_SET_BAUD_RATE;
690         USETW(req.wValue, cfg.rate);
691         USETW(req.wIndex, wIndex);
692         USETW(req.wLength, 0);
693         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
694             &req, NULL, 0, 1000);
695
696         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
697         req.bRequest = FTDI_SIO_SET_DATA;
698         USETW(req.wValue, cfg.lcr);
699         USETW(req.wIndex, wIndex);
700         USETW(req.wLength, 0);
701         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
702             &req, NULL, 0, 1000);
703
704         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
705         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
706         USETW2(req.wValue, cfg.v_stop, cfg.v_start);
707         USETW2(req.wIndex, cfg.v_flow, wIndex);
708         USETW(req.wLength, 0);
709         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
710             &req, NULL, 0, 1000);
711 }
712
713 static void
714 uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
715 {
716         struct uftdi_softc *sc = ucom->sc_parent;
717
718         DPRINTF("msr=0x%02x lsr=0x%02x\n",
719             sc->sc_msr, sc->sc_lsr);
720
721         *msr = sc->sc_msr;
722         *lsr = sc->sc_lsr;
723 }
724
725 static void
726 uftdi_start_read(struct ucom_softc *ucom)
727 {
728         struct uftdi_softc *sc = ucom->sc_parent;
729
730         usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
731 }
732
733 static void
734 uftdi_stop_read(struct ucom_softc *ucom)
735 {
736         struct uftdi_softc *sc = ucom->sc_parent;
737
738         usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
739 }
740
741 static void
742 uftdi_start_write(struct ucom_softc *ucom)
743 {
744         struct uftdi_softc *sc = ucom->sc_parent;
745
746         usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
747 }
748
749 static void
750 uftdi_stop_write(struct ucom_softc *ucom)
751 {
752         struct uftdi_softc *sc = ucom->sc_parent;
753
754         usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
755 }
756
757 /*------------------------------------------------------------------------*
758  *      uftdi_8u232am_getrate
759  *
760  * Return values:
761  *    0: Success
762  * Else: Failure
763  *------------------------------------------------------------------------*/
764 static uint8_t
765 uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
766 {
767         /* Table of the nearest even powers-of-2 for values 0..15. */
768         static const uint8_t roundoff[16] = {
769                 0, 2, 2, 4, 4, 4, 8, 8,
770                 8, 8, 8, 8, 16, 16, 16, 16,
771         };
772         uint32_t d;
773         uint32_t freq;
774         uint16_t result;
775
776         if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
777                 return (1);             /* prevent numerical overflow */
778
779         /* Special cases for 2M and 3M. */
780         if ((speed >= ((3000000 * 100) / 103)) &&
781             (speed <= ((3000000 * 100) / 97))) {
782                 result = 0;
783                 goto done;
784         }
785         if ((speed >= ((2000000 * 100) / 103)) &&
786             (speed <= ((2000000 * 100) / 97))) {
787                 result = 1;
788                 goto done;
789         }
790         d = (FTDI_8U232AM_FREQ << 4) / speed;
791         d = (d & ~15) + roundoff[d & 15];
792
793         if (d < FTDI_8U232AM_MIN_DIV)
794                 d = FTDI_8U232AM_MIN_DIV;
795         else if (d > FTDI_8U232AM_MAX_DIV)
796                 d = FTDI_8U232AM_MAX_DIV;
797
798         /*
799          * Calculate the frequency needed for "d" to exactly divide down to
800          * our target "speed", and check that the actual frequency is within
801          * 3% of this.
802          */
803         freq = (speed * d);
804         if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
805             (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
806                 return (1);
807
808         /*
809          * Pack the divisor into the resultant value.  The lower 14-bits
810          * hold the integral part, while the upper 2 bits encode the
811          * fractional component: either 0, 0.5, 0.25, or 0.125.
812          */
813         result = (d >> 4);
814         if (d & 8)
815                 result |= 0x4000;
816         else if (d & 4)
817                 result |= 0x8000;
818         else if (d & 2)
819                 result |= 0xc000;
820
821 done:
822         *rate = result;
823         return (0);
824 }
825
826 static void
827 uftdi_poll(struct ucom_softc *ucom)
828 {
829         struct uftdi_softc *sc = ucom->sc_parent;
830         usbd_transfer_poll(sc->sc_xfer, UFTDI_N_TRANSFER);
831 }