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