]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/usb/serial/uftdi.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.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 #if 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_IBUFSIZE 64               /* bytes, maximum number of bytes per
95                                          * frame */
96 #define UFTDI_OBUFSIZE 64               /* bytes, cannot be increased due to
97                                          * do size encoding */
98
99 enum {
100         UFTDI_BULK_DT_WR,
101         UFTDI_BULK_DT_RD,
102         UFTDI_N_TRANSFER,
103 };
104
105 struct uftdi_softc {
106         struct ucom_super_softc sc_super_ucom;
107         struct ucom_softc sc_ucom;
108
109         struct usb_device *sc_udev;
110         struct usb_xfer *sc_xfer[UFTDI_N_TRANSFER];
111         device_t sc_dev;
112         struct mtx sc_mtx;
113
114         uint32_t sc_unit;
115         enum uftdi_type sc_type;
116
117         uint16_t sc_last_lcr;
118
119         uint8_t sc_iface_index;
120         uint8_t sc_hdrlen;
121         uint8_t sc_msr;
122         uint8_t sc_lsr;
123
124         uint8_t sc_name[16];
125 };
126
127 struct uftdi_param_config {
128         uint16_t rate;
129         uint16_t lcr;
130         uint8_t v_start;
131         uint8_t v_stop;
132         uint8_t v_flow;
133 };
134
135 /* prototypes */
136
137 static device_probe_t uftdi_probe;
138 static device_attach_t uftdi_attach;
139 static device_detach_t uftdi_detach;
140
141 static usb_callback_t uftdi_write_callback;
142 static usb_callback_t uftdi_read_callback;
143
144 static void     uftdi_cfg_open(struct ucom_softc *);
145 static void     uftdi_cfg_set_dtr(struct ucom_softc *, uint8_t);
146 static void     uftdi_cfg_set_rts(struct ucom_softc *, uint8_t);
147 static void     uftdi_cfg_set_break(struct ucom_softc *, uint8_t);
148 static int      uftdi_set_parm_soft(struct termios *,
149                     struct uftdi_param_config *, uint8_t);
150 static int      uftdi_pre_param(struct ucom_softc *, struct termios *);
151 static void     uftdi_cfg_param(struct ucom_softc *, struct termios *);
152 static void     uftdi_cfg_get_status(struct ucom_softc *, uint8_t *,
153                     uint8_t *);
154 static void     uftdi_start_read(struct ucom_softc *);
155 static void     uftdi_stop_read(struct ucom_softc *);
156 static void     uftdi_start_write(struct ucom_softc *);
157 static void     uftdi_stop_write(struct ucom_softc *);
158 static uint8_t  uftdi_8u232am_getrate(uint32_t, uint16_t *);
159
160 static const struct usb_config uftdi_config[UFTDI_N_TRANSFER] = {
161
162         [UFTDI_BULK_DT_WR] = {
163                 .type = UE_BULK,
164                 .endpoint = UE_ADDR_ANY,
165                 .direction = UE_DIR_OUT,
166                 .bufsize = UFTDI_OBUFSIZE,
167                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
168                 .callback = &uftdi_write_callback,
169         },
170
171         [UFTDI_BULK_DT_RD] = {
172                 .type = UE_BULK,
173                 .endpoint = UE_ADDR_ANY,
174                 .direction = UE_DIR_IN,
175                 .bufsize = UFTDI_IBUFSIZE,
176                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
177                 .callback = &uftdi_read_callback,
178         },
179 };
180
181 static const struct ucom_callback uftdi_callback = {
182         .ucom_cfg_get_status = &uftdi_cfg_get_status,
183         .ucom_cfg_set_dtr = &uftdi_cfg_set_dtr,
184         .ucom_cfg_set_rts = &uftdi_cfg_set_rts,
185         .ucom_cfg_set_break = &uftdi_cfg_set_break,
186         .ucom_cfg_param = &uftdi_cfg_param,
187         .ucom_cfg_open = &uftdi_cfg_open,
188         .ucom_pre_param = &uftdi_pre_param,
189         .ucom_start_read = &uftdi_start_read,
190         .ucom_stop_read = &uftdi_stop_read,
191         .ucom_start_write = &uftdi_start_write,
192         .ucom_stop_write = &uftdi_stop_write,
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         {USB_VPI(USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_STK541, UFTDI_TYPE_8U232AM)},
218         {USB_VPI(USB_VENDOR_DRESDENELEKTRONIK, USB_PRODUCT_DRESDENELEKTRONIK_SENSORTERMINALBOARD, UFTDI_TYPE_8U232AM)},
219         {USB_VPI(USB_VENDOR_DRESDENELEKTRONIK, USB_PRODUCT_DRESDENELEKTRONIK_WIRELESSHANDHELDTERMINAL, UFTDI_TYPE_8U232AM)},
220         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX, UFTDI_TYPE_SIO)},
221         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C, UFTDI_TYPE_8U232AM)},
222         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM, UFTDI_TYPE_8U232AM)},
223         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM4, UFTDI_TYPE_8U232AM)},
224         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20, UFTDI_TYPE_8U232AM)},
225         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_631, UFTDI_TYPE_8U232AM)},
226         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_632, UFTDI_TYPE_8U232AM)},
227         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_633, UFTDI_TYPE_8U232AM)},
228         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_634, UFTDI_TYPE_8U232AM)},
229         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CFA_635, UFTDI_TYPE_8U232AM)},
230         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_USBSERIAL, UFTDI_TYPE_8U232AM)},
231         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MX2_3, UFTDI_TYPE_8U232AM)},
232         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MX4_5, UFTDI_TYPE_8U232AM)},
233         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LK202, UFTDI_TYPE_8U232AM)},
234         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LK204, UFTDI_TYPE_8U232AM)},
235         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13M, UFTDI_TYPE_8U232AM)},
236         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13S, UFTDI_TYPE_8U232AM)},
237         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_TACTRIX_OPENPORT_13U, UFTDI_TYPE_8U232AM)},
238         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EISCOU, UFTDI_TYPE_8U232AM)},
239         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_UOPTBR, UFTDI_TYPE_8U232AM)},
240         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2D, UFTDI_TYPE_8U232AM)},
241         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_PCMSFU, UFTDI_TYPE_8U232AM)},
242         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_EMCU2H, UFTDI_TYPE_8U232AM)},
243         {USB_VPI(USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM, UFTDI_TYPE_8U232AM)},
244         {USB_VPI(USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308, UFTDI_TYPE_8U232AM)},
245         {USB_VPI(USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN, UFTDI_TYPE_8U232AM)},
246         {USB_VPI(USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI, UFTDI_TYPE_8U232AM)},
247         {USB_VPI(USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4, UFTDI_TYPE_8U232AM)},
248         {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1, UFTDI_TYPE_8U232AM)},
249 };
250
251 static int
252 uftdi_probe(device_t dev)
253 {
254         struct usb_attach_arg *uaa = device_get_ivars(dev);
255
256         if (uaa->usb_mode != USB_MODE_HOST) {
257                 return (ENXIO);
258         }
259         if (uaa->info.bConfigIndex != UFTDI_CONFIG_INDEX) {
260                 return (ENXIO);
261         }
262         /* attach to all present interfaces */
263
264         return (usbd_lookup_id_by_uaa(uftdi_devs, sizeof(uftdi_devs), uaa));
265 }
266
267 static int
268 uftdi_attach(device_t dev)
269 {
270         struct usb_attach_arg *uaa = device_get_ivars(dev);
271         struct uftdi_softc *sc = device_get_softc(dev);
272         int error;
273
274         sc->sc_udev = uaa->device;
275         sc->sc_dev = dev;
276         sc->sc_unit = device_get_unit(dev);
277
278         device_set_usb_desc(dev);
279         mtx_init(&sc->sc_mtx, "uftdi", NULL, MTX_DEF);
280
281         snprintf(sc->sc_name, sizeof(sc->sc_name),
282             "%s", device_get_nameunit(dev));
283
284         DPRINTF("\n");
285
286         sc->sc_iface_index = uaa->info.bIfaceIndex;
287         sc->sc_type = USB_GET_DRIVER_INFO(uaa);
288
289         switch (sc->sc_type) {
290         case UFTDI_TYPE_SIO:
291                 sc->sc_hdrlen = 1;
292                 break;
293         case UFTDI_TYPE_8U232AM:
294         default:
295                 sc->sc_hdrlen = 0;
296                 break;
297         }
298
299         error = usbd_transfer_setup(uaa->device,
300             &sc->sc_iface_index, sc->sc_xfer, uftdi_config,
301             UFTDI_N_TRANSFER, sc, &sc->sc_mtx);
302
303         if (error) {
304                 device_printf(dev, "allocating USB "
305                     "transfers failed!\n");
306                 goto detach;
307         }
308         sc->sc_ucom.sc_portno = FTDI_PIT_SIOA + uaa->info.bIfaceNum;
309
310         /* clear stall at first run */
311         mtx_lock(&sc->sc_mtx);
312         usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_WR]);
313         usbd_xfer_set_stall(sc->sc_xfer[UFTDI_BULK_DT_RD]);
314         mtx_unlock(&sc->sc_mtx);
315
316         /* set a valid "lcr" value */
317
318         sc->sc_last_lcr =
319             (FTDI_SIO_SET_DATA_STOP_BITS_2 |
320             FTDI_SIO_SET_DATA_PARITY_NONE |
321             FTDI_SIO_SET_DATA_BITS(8));
322
323         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
324             &uftdi_callback, &sc->sc_mtx);
325         if (error) {
326                 goto detach;
327         }
328         return (0);                     /* success */
329
330 detach:
331         uftdi_detach(dev);
332         return (ENXIO);
333 }
334
335 static int
336 uftdi_detach(device_t dev)
337 {
338         struct uftdi_softc *sc = device_get_softc(dev);
339
340         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
341         usbd_transfer_unsetup(sc->sc_xfer, UFTDI_N_TRANSFER);
342         mtx_destroy(&sc->sc_mtx);
343
344         return (0);
345 }
346
347 static void
348 uftdi_cfg_open(struct ucom_softc *ucom)
349 {
350         struct uftdi_softc *sc = ucom->sc_parent;
351         uint16_t wIndex = ucom->sc_portno;
352         struct usb_device_request req;
353
354         DPRINTF("");
355
356         /* perform a full reset on the device */
357
358         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
359         req.bRequest = FTDI_SIO_RESET;
360         USETW(req.wValue, FTDI_SIO_RESET_SIO);
361         USETW(req.wIndex, wIndex);
362         USETW(req.wLength, 0);
363         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
364             &req, NULL, 0, 1000);
365
366         /* turn on RTS/CTS flow control */
367
368         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
369         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
370         USETW(req.wValue, 0);
371         USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, wIndex);
372         USETW(req.wLength, 0);
373         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
374             &req, NULL, 0, 1000);
375
376         /*
377          * NOTE: with the new UCOM layer there will always be a
378          * "uftdi_cfg_param()" call after "open()", so there is no need for
379          * "open()" to configure anything
380          */
381 }
382
383 static void
384 uftdi_write_callback(struct usb_xfer *xfer, usb_error_t error)
385 {
386         struct uftdi_softc *sc = usbd_xfer_softc(xfer);
387         struct usb_page_cache *pc;
388         uint32_t actlen;
389         uint8_t buf[1];
390
391         switch (USB_GET_STATE(xfer)) {
392         case USB_ST_SETUP:
393         case USB_ST_TRANSFERRED:
394 tr_setup:
395                 pc = usbd_xfer_get_frame(xfer, 0);
396                 if (ucom_get_data(&sc->sc_ucom, pc,
397                     sc->sc_hdrlen, UFTDI_OBUFSIZE - sc->sc_hdrlen,
398                     &actlen)) {
399
400                         if (sc->sc_hdrlen > 0) {
401                                 buf[0] =
402                                     FTDI_OUT_TAG(actlen, sc->sc_ucom.sc_portno);
403                                 usbd_copy_in(pc, 0, buf, 1);
404                         }
405                         usbd_xfer_set_frame_len(xfer, 0, actlen + sc->sc_hdrlen);
406                         usbd_transfer_submit(xfer);
407                 }
408                 return;
409
410         default:                        /* Error */
411                 if (error != USB_ERR_CANCELLED) {
412                         /* try to clear stall first */
413                         usbd_xfer_set_stall(xfer);
414                         goto tr_setup;
415                 }
416                 return;
417         }
418 }
419
420 static void
421 uftdi_read_callback(struct usb_xfer *xfer, usb_error_t error)
422 {
423         struct uftdi_softc *sc = usbd_xfer_softc(xfer);
424         struct usb_page_cache *pc;
425         uint8_t buf[2];
426         uint8_t ftdi_msr;
427         uint8_t msr;
428         uint8_t lsr;
429         int actlen;
430
431         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
432
433         switch (USB_GET_STATE(xfer)) {
434         case USB_ST_TRANSFERRED:
435
436                 if (actlen < 2) {
437                         goto tr_setup;
438                 }
439                 pc = usbd_xfer_get_frame(xfer, 0);
440                 usbd_copy_out(pc, 0, buf, 2);
441
442                 ftdi_msr = FTDI_GET_MSR(buf);
443                 lsr = FTDI_GET_LSR(buf);
444
445                 msr = 0;
446                 if (ftdi_msr & FTDI_SIO_CTS_MASK)
447                         msr |= SER_CTS;
448                 if (ftdi_msr & FTDI_SIO_DSR_MASK)
449                         msr |= SER_DSR;
450                 if (ftdi_msr & FTDI_SIO_RI_MASK)
451                         msr |= SER_RI;
452                 if (ftdi_msr & FTDI_SIO_RLSD_MASK)
453                         msr |= SER_DCD;
454
455                 if ((sc->sc_msr != msr) ||
456                     ((sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK))) {
457                         DPRINTF("status change msr=0x%02x (0x%02x) "
458                             "lsr=0x%02x (0x%02x)\n", msr, sc->sc_msr,
459                             lsr, sc->sc_lsr);
460
461                         sc->sc_msr = msr;
462                         sc->sc_lsr = lsr;
463
464                         ucom_status_change(&sc->sc_ucom);
465                 }
466                 actlen -= 2;
467
468                 if (actlen > 0) {
469                         ucom_put_data(&sc->sc_ucom, pc, 2, actlen);
470                 }
471         case USB_ST_SETUP:
472 tr_setup:
473                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
474                 usbd_transfer_submit(xfer);
475                 return;
476
477         default:                        /* Error */
478                 if (error != USB_ERR_CANCELLED) {
479                         /* try to clear stall first */
480                         usbd_xfer_set_stall(xfer);
481                         goto tr_setup;
482                 }
483                 return;
484         }
485 }
486
487 static void
488 uftdi_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
489 {
490         struct uftdi_softc *sc = ucom->sc_parent;
491         uint16_t wIndex = ucom->sc_portno;
492         uint16_t wValue;
493         struct usb_device_request req;
494
495         wValue = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
496
497         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
498         req.bRequest = FTDI_SIO_MODEM_CTRL;
499         USETW(req.wValue, wValue);
500         USETW(req.wIndex, wIndex);
501         USETW(req.wLength, 0);
502         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
503             &req, NULL, 0, 1000);
504 }
505
506 static void
507 uftdi_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
508 {
509         struct uftdi_softc *sc = ucom->sc_parent;
510         uint16_t wIndex = ucom->sc_portno;
511         uint16_t wValue;
512         struct usb_device_request req;
513
514         wValue = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
515
516         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
517         req.bRequest = FTDI_SIO_MODEM_CTRL;
518         USETW(req.wValue, wValue);
519         USETW(req.wIndex, wIndex);
520         USETW(req.wLength, 0);
521         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
522             &req, NULL, 0, 1000);
523 }
524
525 static void
526 uftdi_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
527 {
528         struct uftdi_softc *sc = ucom->sc_parent;
529         uint16_t wIndex = ucom->sc_portno;
530         uint16_t wValue;
531         struct usb_device_request req;
532
533         if (onoff) {
534                 sc->sc_last_lcr |= FTDI_SIO_SET_BREAK;
535         } else {
536                 sc->sc_last_lcr &= ~FTDI_SIO_SET_BREAK;
537         }
538
539         wValue = sc->sc_last_lcr;
540
541         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
542         req.bRequest = FTDI_SIO_SET_DATA;
543         USETW(req.wValue, wValue);
544         USETW(req.wIndex, wIndex);
545         USETW(req.wLength, 0);
546         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
547             &req, NULL, 0, 1000);
548 }
549
550 static int
551 uftdi_set_parm_soft(struct termios *t,
552     struct uftdi_param_config *cfg, uint8_t type)
553 {
554         bzero(cfg, sizeof(*cfg));
555
556         switch (type) {
557         case UFTDI_TYPE_SIO:
558                 switch (t->c_ospeed) {
559                 case 300:
560                         cfg->rate = ftdi_sio_b300;
561                         break;
562                 case 600:
563                         cfg->rate = ftdi_sio_b600;
564                         break;
565                 case 1200:
566                         cfg->rate = ftdi_sio_b1200;
567                         break;
568                 case 2400:
569                         cfg->rate = ftdi_sio_b2400;
570                         break;
571                 case 4800:
572                         cfg->rate = ftdi_sio_b4800;
573                         break;
574                 case 9600:
575                         cfg->rate = ftdi_sio_b9600;
576                         break;
577                 case 19200:
578                         cfg->rate = ftdi_sio_b19200;
579                         break;
580                 case 38400:
581                         cfg->rate = ftdi_sio_b38400;
582                         break;
583                 case 57600:
584                         cfg->rate = ftdi_sio_b57600;
585                         break;
586                 case 115200:
587                         cfg->rate = ftdi_sio_b115200;
588                         break;
589                 default:
590                         return (EINVAL);
591                 }
592                 break;
593
594         case UFTDI_TYPE_8U232AM:
595                 if (uftdi_8u232am_getrate(t->c_ospeed, &cfg->rate)) {
596                         return (EINVAL);
597                 }
598                 break;
599         }
600
601         if (t->c_cflag & CSTOPB)
602                 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_2;
603         else
604                 cfg->lcr = FTDI_SIO_SET_DATA_STOP_BITS_1;
605
606         if (t->c_cflag & PARENB) {
607                 if (t->c_cflag & PARODD) {
608                         cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_ODD;
609                 } else {
610                         cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_EVEN;
611                 }
612         } else {
613                 cfg->lcr |= FTDI_SIO_SET_DATA_PARITY_NONE;
614         }
615
616         switch (t->c_cflag & CSIZE) {
617         case CS5:
618                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(5);
619                 break;
620
621         case CS6:
622                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(6);
623                 break;
624
625         case CS7:
626                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(7);
627                 break;
628
629         case CS8:
630                 cfg->lcr |= FTDI_SIO_SET_DATA_BITS(8);
631                 break;
632         }
633
634         if (t->c_cflag & CRTSCTS) {
635                 cfg->v_flow = FTDI_SIO_RTS_CTS_HS;
636         } else if (t->c_iflag & (IXON | IXOFF)) {
637                 cfg->v_flow = FTDI_SIO_XON_XOFF_HS;
638                 cfg->v_start = t->c_cc[VSTART];
639                 cfg->v_stop = t->c_cc[VSTOP];
640         } else {
641                 cfg->v_flow = FTDI_SIO_DISABLE_FLOW_CTRL;
642         }
643
644         return (0);
645 }
646
647 static int
648 uftdi_pre_param(struct ucom_softc *ucom, struct termios *t)
649 {
650         struct uftdi_softc *sc = ucom->sc_parent;
651         struct uftdi_param_config cfg;
652
653         DPRINTF("\n");
654
655         return (uftdi_set_parm_soft(t, &cfg, sc->sc_type));
656 }
657
658 static void
659 uftdi_cfg_param(struct ucom_softc *ucom, struct termios *t)
660 {
661         struct uftdi_softc *sc = ucom->sc_parent;
662         uint16_t wIndex = ucom->sc_portno;
663         struct uftdi_param_config cfg;
664         struct usb_device_request req;
665
666         if (uftdi_set_parm_soft(t, &cfg, sc->sc_type)) {
667                 /* should not happen */
668                 return;
669         }
670         sc->sc_last_lcr = cfg.lcr;
671
672         DPRINTF("\n");
673
674         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
675         req.bRequest = FTDI_SIO_SET_BAUD_RATE;
676         USETW(req.wValue, cfg.rate);
677         USETW(req.wIndex, wIndex);
678         USETW(req.wLength, 0);
679         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
680             &req, NULL, 0, 1000);
681
682         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
683         req.bRequest = FTDI_SIO_SET_DATA;
684         USETW(req.wValue, cfg.lcr);
685         USETW(req.wIndex, wIndex);
686         USETW(req.wLength, 0);
687         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
688             &req, NULL, 0, 1000);
689
690         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
691         req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
692         USETW2(req.wValue, cfg.v_stop, cfg.v_start);
693         USETW2(req.wIndex, cfg.v_flow, wIndex);
694         USETW(req.wLength, 0);
695         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
696             &req, NULL, 0, 1000);
697 }
698
699 static void
700 uftdi_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
701 {
702         struct uftdi_softc *sc = ucom->sc_parent;
703
704         DPRINTF("msr=0x%02x lsr=0x%02x\n",
705             sc->sc_msr, sc->sc_lsr);
706
707         *msr = sc->sc_msr;
708         *lsr = sc->sc_lsr;
709 }
710
711 static void
712 uftdi_start_read(struct ucom_softc *ucom)
713 {
714         struct uftdi_softc *sc = ucom->sc_parent;
715
716         usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_RD]);
717 }
718
719 static void
720 uftdi_stop_read(struct ucom_softc *ucom)
721 {
722         struct uftdi_softc *sc = ucom->sc_parent;
723
724         usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_RD]);
725 }
726
727 static void
728 uftdi_start_write(struct ucom_softc *ucom)
729 {
730         struct uftdi_softc *sc = ucom->sc_parent;
731
732         usbd_transfer_start(sc->sc_xfer[UFTDI_BULK_DT_WR]);
733 }
734
735 static void
736 uftdi_stop_write(struct ucom_softc *ucom)
737 {
738         struct uftdi_softc *sc = ucom->sc_parent;
739
740         usbd_transfer_stop(sc->sc_xfer[UFTDI_BULK_DT_WR]);
741 }
742
743 /*------------------------------------------------------------------------*
744  *      uftdi_8u232am_getrate
745  *
746  * Return values:
747  *    0: Success
748  * Else: Failure
749  *------------------------------------------------------------------------*/
750 static uint8_t
751 uftdi_8u232am_getrate(uint32_t speed, uint16_t *rate)
752 {
753         /* Table of the nearest even powers-of-2 for values 0..15. */
754         static const uint8_t roundoff[16] = {
755                 0, 2, 2, 4, 4, 4, 8, 8,
756                 8, 8, 8, 8, 16, 16, 16, 16,
757         };
758         uint32_t d;
759         uint32_t freq;
760         uint16_t result;
761
762         if ((speed < 178) || (speed > ((3000000 * 100) / 97)))
763                 return (1);             /* prevent numerical overflow */
764
765         /* Special cases for 2M and 3M. */
766         if ((speed >= ((3000000 * 100) / 103)) &&
767             (speed <= ((3000000 * 100) / 97))) {
768                 result = 0;
769                 goto done;
770         }
771         if ((speed >= ((2000000 * 100) / 103)) &&
772             (speed <= ((2000000 * 100) / 97))) {
773                 result = 1;
774                 goto done;
775         }
776         d = (FTDI_8U232AM_FREQ << 4) / speed;
777         d = (d & ~15) + roundoff[d & 15];
778
779         if (d < FTDI_8U232AM_MIN_DIV)
780                 d = FTDI_8U232AM_MIN_DIV;
781         else if (d > FTDI_8U232AM_MAX_DIV)
782                 d = FTDI_8U232AM_MAX_DIV;
783
784         /*
785          * Calculate the frequency needed for "d" to exactly divide down to
786          * our target "speed", and check that the actual frequency is within
787          * 3% of this.
788          */
789         freq = (speed * d);
790         if ((freq < ((FTDI_8U232AM_FREQ * 1600ULL) / 103)) ||
791             (freq > ((FTDI_8U232AM_FREQ * 1600ULL) / 97)))
792                 return (1);
793
794         /*
795          * Pack the divisor into the resultant value.  The lower 14-bits
796          * hold the integral part, while the upper 2 bits encode the
797          * fractional component: either 0, 0.5, 0.25, or 0.125.
798          */
799         result = (d >> 4);
800         if (d & 8)
801                 result |= 0x4000;
802         else if (d & 4)
803                 result |= 0x8000;
804         else if (d & 2)
805                 result |= 0xc000;
806
807 done:
808         *rate = result;
809         return (0);
810 }