uftdi.c revision 1.78 1 1.78 maya /* $NetBSD: uftdi.c,v 1.78 2024/04/17 02:34:45 maya Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss *
19 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
30 1.1 augustss */
31 1.1 augustss
32 1.7 lukem #include <sys/cdefs.h>
33 1.78 maya __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.78 2024/04/17 02:34:45 maya Exp $");
34 1.63 skrll
35 1.63 skrll #ifdef _KERNEL_OPT
36 1.63 skrll #include "opt_usb.h"
37 1.63 skrll #endif
38 1.1 augustss
39 1.1 augustss #include <sys/param.h>
40 1.1 augustss #include <sys/systm.h>
41 1.1 augustss #include <sys/kernel.h>
42 1.1 augustss #include <sys/device.h>
43 1.1 augustss #include <sys/conf.h>
44 1.1 augustss #include <sys/tty.h>
45 1.1 augustss
46 1.1 augustss #include <dev/usb/usb.h>
47 1.1 augustss
48 1.1 augustss #include <dev/usb/usbdi.h>
49 1.1 augustss #include <dev/usb/usbdi_util.h>
50 1.77 thorpej #include <dev/usb/usbdivar.h>
51 1.1 augustss #include <dev/usb/usbdevs.h>
52 1.1 augustss
53 1.1 augustss #include <dev/usb/ucomvar.h>
54 1.1 augustss
55 1.1 augustss #include <dev/usb/uftdireg.h>
56 1.1 augustss
57 1.1 augustss #ifdef UFTDI_DEBUG
58 1.1 augustss #define DPRINTF(x) if (uftdidebug) printf x
59 1.1 augustss #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x
60 1.4 augustss int uftdidebug = 0;
61 1.1 augustss #else
62 1.1 augustss #define DPRINTF(x)
63 1.1 augustss #define DPRINTFN(n,x)
64 1.1 augustss #endif
65 1.1 augustss
66 1.67 ws #define UFTDI_CONFIG_NO 1
67 1.1 augustss
68 1.1 augustss /*
69 1.53 riastrad * These are the default number of bytes transferred per frame if the
70 1.53 riastrad * endpoint doesn't tell us. The output buffer size is a hard limit
71 1.53 riastrad * for devices that use a 6-bit size encoding.
72 1.1 augustss */
73 1.1 augustss #define UFTDIIBUFSIZE 64
74 1.1 augustss #define UFTDIOBUFSIZE 64
75 1.1 augustss
76 1.53 riastrad /*
77 1.53 riastrad * Magic constants! Where do these come from? They're what Linux uses...
78 1.53 riastrad */
79 1.53 riastrad #define UFTDI_MAX_IBUFSIZE 512
80 1.53 riastrad #define UFTDI_MAX_OBUFSIZE 256
81 1.53 riastrad
82 1.1 augustss struct uftdi_softc {
83 1.47 dyoung device_t sc_dev; /* base device */
84 1.61 skrll struct usbd_device * sc_udev; /* device */
85 1.67 ws struct usbd_interface * sc_iface; /* interface */
86 1.67 ws int sc_iface_no;
87 1.1 augustss
88 1.10 scw enum uftdi_type sc_type;
89 1.71 ryo u_int sc_flags;
90 1.71 ryo #define FLAGS_BAUDCLK_12M 0x00000001
91 1.71 ryo #define FLAGS_ROUNDOFF_232A 0x00000002
92 1.71 ryo #define FLAGS_BAUDBITS_HINDEX 0x00000004
93 1.10 scw u_int sc_hdrlen;
94 1.41 nisimura u_int sc_chiptype;
95 1.10 scw
96 1.1 augustss u_char sc_msr;
97 1.1 augustss u_char sc_lsr;
98 1.1 augustss
99 1.67 ws device_t sc_subdev;
100 1.1 augustss
101 1.70 mrg bool sc_dying;
102 1.8 ichiro
103 1.8 ichiro u_int last_lcr;
104 1.1 augustss };
105 1.1 augustss
106 1.70 mrg static void uftdi_get_status(void *, int, u_char *, u_char *);
107 1.70 mrg static void uftdi_set(void *, int, int, int);
108 1.70 mrg static int uftdi_param(void *, int, struct termios *);
109 1.70 mrg static int uftdi_open(void *, int);
110 1.70 mrg static void uftdi_read(void *, int, u_char **, uint32_t *);
111 1.70 mrg static void uftdi_write(void *, int, u_char *, u_char *, uint32_t *);
112 1.70 mrg static void uftdi_break(void *, int, int);
113 1.1 augustss
114 1.72 maxv static const struct ucom_methods uftdi_methods = {
115 1.61 skrll .ucom_get_status = uftdi_get_status,
116 1.61 skrll .ucom_set = uftdi_set,
117 1.61 skrll .ucom_param = uftdi_param,
118 1.61 skrll .ucom_open = uftdi_open,
119 1.61 skrll .ucom_read = uftdi_read,
120 1.61 skrll .ucom_write = uftdi_write,
121 1.1 augustss };
122 1.1 augustss
123 1.61 skrll /*
124 1.26 christos * The devices default to UFTDI_TYPE_8U232AM.
125 1.47 dyoung * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead
126 1.26 christos */
127 1.26 christos static const struct usb_devno uftdi_devs[] = {
128 1.26 christos { USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 },
129 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US101 },
130 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US159 },
131 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US235 },
132 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US257 },
133 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_12 },
134 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_34 },
135 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_56 },
136 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US279_78 },
137 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US313 },
138 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US320 },
139 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US324 },
140 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US346_12 },
141 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US346_34 },
142 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US701_12 },
143 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US701_34 },
144 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_12 },
145 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_34 },
146 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_56 },
147 1.78 maya { USB_VENDOR_BRAINBOXES, USB_PRODUCT_BRAINBOXES_US842_78 },
148 1.26 christos { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
149 1.26 christos { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
150 1.59 msaitoh { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_230X },
151 1.55 reinoud { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H },
152 1.56 mlelstv { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232RL },
153 1.30 riz { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C },
154 1.42 nisimura { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H },
155 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
156 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
157 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
158 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
159 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
160 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
161 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
162 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
163 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
164 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
165 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
166 1.45 martin { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI },
167 1.45 martin { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 },
168 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
169 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
170 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
171 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
172 1.27 xtraeme { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
173 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
174 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
175 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
176 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
177 1.27 xtraeme { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
178 1.52 matt { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY },
179 1.57 cheusov { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_BEAGLEBONE },
180 1.54 jakllsch { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U },
181 1.48 matt { USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG },
182 1.26 christos { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
183 1.26 christos { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
184 1.60 nonaka { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_PCOPRS1 },
185 1.40 taca { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F },
186 1.65 jnemeth { USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_CT57A },
187 1.66 jakllsch { USB_VENDOR_RTSYS, USB_PRODUCT_RTSYS_RTS03 },
188 1.26 christos { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL },
189 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 },
190 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 },
191 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 },
192 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 },
193 1.26 christos { USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 },
194 1.51 plunky { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK },
195 1.51 plunky { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO },
196 1.26 christos };
197 1.26 christos #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p)
198 1.26 christos
199 1.70 mrg static int uftdi_match(device_t, cfdata_t, void *);
200 1.70 mrg static void uftdi_attach(device_t, device_t, void *);
201 1.70 mrg static void uftdi_childdet(device_t, device_t);
202 1.70 mrg static int uftdi_detach(device_t, int);
203 1.69 mrg
204 1.38 cube CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match,
205 1.70 mrg uftdi_attach, uftdi_detach, NULL, NULL, uftdi_childdet);
206 1.1 augustss
207 1.77 thorpej struct uftdi_match_quirk_entry {
208 1.77 thorpej uint16_t vendor_id;
209 1.77 thorpej uint16_t product_id;
210 1.77 thorpej int iface_no;
211 1.77 thorpej const char * vendor_str;
212 1.77 thorpej const char * product_str;
213 1.77 thorpej int match_ret;
214 1.77 thorpej };
215 1.77 thorpej
216 1.77 thorpej static const struct uftdi_match_quirk_entry uftdi_match_quirks[] = {
217 1.77 thorpej /*
218 1.77 thorpej * The Tigard board (https://github.com/tigard-tools/tigard)
219 1.77 thorpej * has two interfaces, one of which is meant to act as a
220 1.77 thorpej * regular USB serial port (interface 0), the other of which
221 1.77 thorpej * is meant for other protocols (SWD, JTAG, etc.). We must
222 1.77 thorpej * reject interface 1 so that ugenif matches, thus allowing
223 1.77 thorpej * full user-space control of that port.
224 1.77 thorpej */
225 1.77 thorpej {
226 1.77 thorpej .vendor_id = USB_VENDOR_FTDI,
227 1.77 thorpej .product_id = USB_PRODUCT_FTDI_SERIAL_2232C,
228 1.77 thorpej .iface_no = 1,
229 1.77 thorpej .vendor_str = "SecuringHardware.com",
230 1.77 thorpej .product_str = "Tigard V1.1",
231 1.77 thorpej .match_ret = UMATCH_NONE,
232 1.77 thorpej }
233 1.77 thorpej };
234 1.77 thorpej
235 1.77 thorpej static int
236 1.77 thorpej uftdi_quirk_match(struct usbif_attach_arg *uiaa, int rv)
237 1.77 thorpej {
238 1.77 thorpej struct usbd_device *dev = uiaa->uiaa_device;
239 1.77 thorpej const struct uftdi_match_quirk_entry *q;
240 1.77 thorpej int i;
241 1.77 thorpej
242 1.77 thorpej for (i = 0; i < __arraycount(uftdi_match_quirks); i++) {
243 1.77 thorpej q = &uftdi_match_quirks[i];
244 1.77 thorpej if (uiaa->uiaa_vendor != q->vendor_id ||
245 1.77 thorpej uiaa->uiaa_product != q->product_id ||
246 1.77 thorpej uiaa->uiaa_ifaceno != q->iface_no) {
247 1.77 thorpej continue;
248 1.77 thorpej }
249 1.77 thorpej if (q->vendor_str != NULL &&
250 1.77 thorpej (dev->ud_vendor == NULL ||
251 1.77 thorpej strcmp(dev->ud_vendor, q->vendor_str) != 0)) {
252 1.77 thorpej continue;
253 1.77 thorpej }
254 1.77 thorpej if (q->product_str != NULL &&
255 1.77 thorpej (dev->ud_product == NULL ||
256 1.77 thorpej strcmp(dev->ud_product, q->product_str) != 0)) {
257 1.77 thorpej continue;
258 1.77 thorpej }
259 1.77 thorpej /*
260 1.77 thorpej * Got a match!
261 1.77 thorpej */
262 1.77 thorpej rv = q->match_ret;
263 1.77 thorpej break;
264 1.77 thorpej }
265 1.77 thorpej return rv;
266 1.77 thorpej }
267 1.77 thorpej
268 1.70 mrg static int
269 1.47 dyoung uftdi_match(device_t parent, cfdata_t match, void *aux)
270 1.1 augustss {
271 1.67 ws struct usbif_attach_arg *uiaa = aux;
272 1.77 thorpej int rv;
273 1.11 augustss
274 1.73 christos DPRINTFN(20,("uftdi: vendor=%#x, product=%#x\n",
275 1.67 ws uiaa->uiaa_vendor, uiaa->uiaa_product));
276 1.1 augustss
277 1.67 ws if (uiaa->uiaa_configno != UFTDI_CONFIG_NO)
278 1.67 ws return UMATCH_NONE;
279 1.67 ws
280 1.77 thorpej rv = uftdi_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product) != NULL ?
281 1.67 ws UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
282 1.77 thorpej if (rv != UMATCH_NONE) {
283 1.77 thorpej rv = uftdi_quirk_match(uiaa, rv);
284 1.77 thorpej }
285 1.77 thorpej return rv;
286 1.1 augustss }
287 1.1 augustss
288 1.70 mrg static void
289 1.47 dyoung uftdi_attach(device_t parent, device_t self, void *aux)
290 1.1 augustss {
291 1.47 dyoung struct uftdi_softc *sc = device_private(self);
292 1.67 ws struct usbif_attach_arg *uiaa = aux;
293 1.67 ws struct usbd_device *dev = uiaa->uiaa_device;
294 1.67 ws struct usbd_interface *iface = uiaa->uiaa_iface;
295 1.41 nisimura usb_device_descriptor_t *ddesc;
296 1.1 augustss usb_interface_descriptor_t *id;
297 1.1 augustss usb_endpoint_descriptor_t *ed;
298 1.24 augustss char *devinfop;
299 1.67 ws int i;
300 1.61 skrll struct ucom_attach_args ucaa;
301 1.1 augustss
302 1.1 augustss DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
303 1.1 augustss
304 1.43 plunky aprint_naive("\n");
305 1.43 plunky aprint_normal("\n");
306 1.43 plunky
307 1.43 plunky devinfop = usbd_devinfo_alloc(dev, 0);
308 1.43 plunky aprint_normal_dev(self, "%s\n", devinfop);
309 1.43 plunky usbd_devinfo_free(devinfop);
310 1.43 plunky
311 1.38 cube sc->sc_dev = self;
312 1.1 augustss sc->sc_udev = dev;
313 1.70 mrg sc->sc_dying = false;
314 1.67 ws sc->sc_iface_no = uiaa->uiaa_ifaceno;
315 1.41 nisimura sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */
316 1.41 nisimura sc->sc_hdrlen = 0;
317 1.41 nisimura
318 1.41 nisimura ddesc = usbd_get_device_descriptor(dev);
319 1.41 nisimura sc->sc_chiptype = UGETW(ddesc->bcdDevice);
320 1.11 augustss
321 1.71 ryo switch (sc->sc_chiptype) {
322 1.71 ryo case 0x0200:
323 1.71 ryo if (ddesc->iSerialNumber != 0)
324 1.71 ryo sc->sc_flags |= FLAGS_ROUNDOFF_232A;
325 1.71 ryo ucaa.ucaa_portno = 0;
326 1.71 ryo break;
327 1.71 ryo case 0x0400:
328 1.71 ryo ucaa.ucaa_portno = 0;
329 1.71 ryo break;
330 1.71 ryo case 0x0500:
331 1.71 ryo sc->sc_flags |= FLAGS_BAUDBITS_HINDEX;
332 1.71 ryo ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no;
333 1.71 ryo break;
334 1.71 ryo case 0x0600:
335 1.71 ryo ucaa.ucaa_portno = 0;
336 1.71 ryo break;
337 1.71 ryo case 0x0700:
338 1.71 ryo case 0x0800:
339 1.71 ryo case 0x0900:
340 1.71 ryo sc->sc_flags |= FLAGS_BAUDCLK_12M;
341 1.71 ryo sc->sc_flags |= FLAGS_BAUDBITS_HINDEX;
342 1.71 ryo ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no;
343 1.71 ryo break;
344 1.71 ryo case 0x1000:
345 1.71 ryo sc->sc_flags |= FLAGS_BAUDBITS_HINDEX;
346 1.71 ryo ucaa.ucaa_portno = FTDI_PIT_SIOA + sc->sc_iface_no;
347 1.71 ryo break;
348 1.71 ryo default:
349 1.71 ryo if (sc->sc_chiptype < 0x0200) {
350 1.71 ryo sc->sc_type = UFTDI_TYPE_SIO;
351 1.71 ryo sc->sc_hdrlen = 1;
352 1.71 ryo }
353 1.71 ryo ucaa.ucaa_portno = 0;
354 1.71 ryo break;
355 1.71 ryo }
356 1.71 ryo
357 1.67 ws id = usbd_get_interface_descriptor(iface);
358 1.32 riz
359 1.67 ws sc->sc_iface = iface;
360 1.32 riz
361 1.67 ws ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
362 1.67 ws ucaa.ucaa_ibufsize = ucaa.ucaa_obufsize = 0;
363 1.67 ws for (i = 0; i < id->bNumEndpoints; i++) {
364 1.67 ws int addr, dir, attr;
365 1.67 ws ed = usbd_interface2endpoint_descriptor(iface, i);
366 1.67 ws if (ed == NULL) {
367 1.38 cube aprint_error_dev(self,
368 1.67 ws "could not read endpoint descriptor\n");
369 1.1 augustss goto bad;
370 1.1 augustss }
371 1.67 ws
372 1.67 ws addr = ed->bEndpointAddress;
373 1.67 ws dir = UE_GET_DIR(ed->bEndpointAddress);
374 1.67 ws attr = ed->bmAttributes & UE_XFERTYPE;
375 1.67 ws if (dir == UE_DIR_IN && attr == UE_BULK) {
376 1.67 ws ucaa.ucaa_bulkin = addr;
377 1.67 ws ucaa.ucaa_ibufsize = UGETW(ed->wMaxPacketSize);
378 1.67 ws if (ucaa.ucaa_ibufsize >= UFTDI_MAX_IBUFSIZE)
379 1.67 ws ucaa.ucaa_ibufsize = UFTDI_MAX_IBUFSIZE;
380 1.67 ws } else if (dir == UE_DIR_OUT && attr == UE_BULK) {
381 1.67 ws ucaa.ucaa_bulkout = addr;
382 1.67 ws ucaa.ucaa_obufsize = UGETW(ed->wMaxPacketSize)
383 1.67 ws - sc->sc_hdrlen;
384 1.67 ws if (ucaa.ucaa_obufsize >= UFTDI_MAX_OBUFSIZE)
385 1.67 ws ucaa.ucaa_obufsize = UFTDI_MAX_OBUFSIZE;
386 1.67 ws /* Limit length if we have a 6-bit header. */
387 1.67 ws if ((sc->sc_hdrlen > 0) &&
388 1.67 ws (ucaa.ucaa_obufsize > UFTDIOBUFSIZE))
389 1.67 ws ucaa.ucaa_obufsize = UFTDIOBUFSIZE;
390 1.67 ws } else {
391 1.67 ws aprint_error_dev(self, "unexpected endpoint\n");
392 1.32 riz goto bad;
393 1.32 riz }
394 1.67 ws }
395 1.67 ws if (ucaa.ucaa_bulkin == -1) {
396 1.67 ws aprint_error_dev(self, "Could not find data bulk in\n");
397 1.67 ws goto bad;
398 1.67 ws }
399 1.67 ws if (ucaa.ucaa_bulkout == -1) {
400 1.67 ws aprint_error_dev(self, "Could not find data bulk out\n");
401 1.67 ws goto bad;
402 1.67 ws }
403 1.32 riz
404 1.67 ws /* ucaa_bulkin, ucaa_bulkout set above */
405 1.67 ws if (ucaa.ucaa_ibufsize == 0)
406 1.67 ws ucaa.ucaa_ibufsize = UFTDIIBUFSIZE;
407 1.67 ws ucaa.ucaa_ibufsizepad = ucaa.ucaa_ibufsize;
408 1.67 ws if (ucaa.ucaa_obufsize == 0)
409 1.67 ws ucaa.ucaa_obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
410 1.67 ws ucaa.ucaa_opkthdrlen = sc->sc_hdrlen;
411 1.67 ws ucaa.ucaa_device = dev;
412 1.67 ws ucaa.ucaa_iface = iface;
413 1.67 ws ucaa.ucaa_methods = &uftdi_methods;
414 1.67 ws ucaa.ucaa_arg = sc;
415 1.67 ws ucaa.ucaa_info = NULL;
416 1.67 ws
417 1.73 christos DPRINTF(("uftdi: in=%#x out=%#x isize=%#x osize=%#x\n",
418 1.67 ws ucaa.ucaa_bulkin, ucaa.ucaa_bulkout,
419 1.67 ws ucaa.ucaa_ibufsize, ucaa.ucaa_obufsize));
420 1.75 thorpej sc->sc_subdev = config_found(self, &ucaa, ucomprint,
421 1.76 thorpej CFARGS(.submatch = ucomsubmatch));
422 1.1 augustss
423 1.62 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
424 1.5 augustss
425 1.64 maya if (!pmf_device_register(self, NULL, NULL))
426 1.64 maya aprint_error_dev(self, "couldn't establish power handler\n");
427 1.64 maya
428 1.47 dyoung return;
429 1.1 augustss
430 1.1 augustss bad:
431 1.1 augustss DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
432 1.70 mrg sc->sc_dying = true;
433 1.47 dyoung return;
434 1.1 augustss }
435 1.1 augustss
436 1.70 mrg static void
437 1.35 dyoung uftdi_childdet(device_t self, device_t child)
438 1.35 dyoung {
439 1.35 dyoung struct uftdi_softc *sc = device_private(self);
440 1.35 dyoung
441 1.67 ws KASSERT(child == sc->sc_subdev);
442 1.67 ws sc->sc_subdev = NULL;
443 1.35 dyoung }
444 1.35 dyoung
445 1.70 mrg static int
446 1.35 dyoung uftdi_detach(device_t self, int flags)
447 1.1 augustss {
448 1.35 dyoung struct uftdi_softc *sc = device_private(self);
449 1.70 mrg int rv = 0;
450 1.1 augustss
451 1.1 augustss DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
452 1.70 mrg
453 1.70 mrg sc->sc_dying = true;
454 1.70 mrg
455 1.70 mrg if (sc->sc_subdev != NULL) {
456 1.70 mrg rv = config_detach(sc->sc_subdev, flags);
457 1.70 mrg sc->sc_subdev = NULL;
458 1.70 mrg }
459 1.5 augustss
460 1.62 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
461 1.5 augustss
462 1.70 mrg return rv;
463 1.1 augustss }
464 1.1 augustss
465 1.70 mrg static int
466 1.2 augustss uftdi_open(void *vsc, int portno)
467 1.1 augustss {
468 1.1 augustss struct uftdi_softc *sc = vsc;
469 1.1 augustss usb_device_request_t req;
470 1.1 augustss usbd_status err;
471 1.1 augustss struct termios t;
472 1.1 augustss
473 1.1 augustss DPRINTF(("uftdi_open: sc=%p\n", sc));
474 1.1 augustss
475 1.1 augustss if (sc->sc_dying)
476 1.61 skrll return EIO;
477 1.1 augustss
478 1.1 augustss /* Perform a full reset on the device */
479 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
480 1.1 augustss req.bRequest = FTDI_SIO_RESET;
481 1.1 augustss USETW(req.wValue, FTDI_SIO_RESET_SIO);
482 1.1 augustss USETW(req.wIndex, portno);
483 1.1 augustss USETW(req.wLength, 0);
484 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
485 1.1 augustss if (err)
486 1.61 skrll return EIO;
487 1.1 augustss
488 1.1 augustss /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
489 1.1 augustss t.c_ospeed = 9600;
490 1.1 augustss t.c_cflag = CSTOPB | CS8;
491 1.1 augustss (void)uftdi_param(sc, portno, &t);
492 1.1 augustss
493 1.1 augustss /* Turn on RTS/CTS flow control */
494 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
495 1.1 augustss req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
496 1.1 augustss USETW(req.wValue, 0);
497 1.1 augustss USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
498 1.1 augustss USETW(req.wLength, 0);
499 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
500 1.1 augustss if (err)
501 1.61 skrll return EIO;
502 1.1 augustss
503 1.61 skrll return 0;
504 1.1 augustss }
505 1.1 augustss
506 1.70 mrg static void
507 1.61 skrll uftdi_read(void *vsc, int portno, u_char **ptr, uint32_t *count)
508 1.1 augustss {
509 1.1 augustss struct uftdi_softc *sc = vsc;
510 1.1 augustss u_char msr, lsr;
511 1.1 augustss
512 1.1 augustss DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
513 1.1 augustss *count));
514 1.1 augustss
515 1.1 augustss msr = FTDI_GET_MSR(*ptr);
516 1.1 augustss lsr = FTDI_GET_LSR(*ptr);
517 1.1 augustss
518 1.1 augustss #ifdef UFTDI_DEBUG
519 1.1 augustss if (*count != 2)
520 1.1 augustss DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
521 1.74 christos "0x%02x\n", sc, portno, *count, (*ptr)[2]));
522 1.1 augustss #endif
523 1.1 augustss
524 1.1 augustss if (sc->sc_msr != msr ||
525 1.1 augustss (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
526 1.74 christos DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
527 1.74 christos "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
528 1.1 augustss lsr, sc->sc_lsr));
529 1.1 augustss sc->sc_msr = msr;
530 1.1 augustss sc->sc_lsr = lsr;
531 1.67 ws ucom_status_change(device_private(sc->sc_subdev));
532 1.1 augustss }
533 1.1 augustss
534 1.46 scw /* Adjust buffer pointer to skip status prefix */
535 1.1 augustss *ptr += 2;
536 1.1 augustss }
537 1.1 augustss
538 1.70 mrg static void
539 1.61 skrll uftdi_write(void *vsc, int portno, u_char *to, u_char *from, uint32_t *count)
540 1.1 augustss {
541 1.10 scw struct uftdi_softc *sc = vsc;
542 1.10 scw
543 1.74 christos DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
544 1.1 augustss vsc, portno, *count, from[0]));
545 1.1 augustss
546 1.1 augustss /* Make length tag and copy data */
547 1.10 scw if (sc->sc_hdrlen > 0)
548 1.10 scw *to = FTDI_OUT_TAG(*count, portno);
549 1.10 scw
550 1.10 scw memcpy(to + sc->sc_hdrlen, from, *count);
551 1.10 scw *count += sc->sc_hdrlen;
552 1.1 augustss }
553 1.1 augustss
554 1.70 mrg static void
555 1.2 augustss uftdi_set(void *vsc, int portno, int reg, int onoff)
556 1.1 augustss {
557 1.1 augustss struct uftdi_softc *sc = vsc;
558 1.1 augustss usb_device_request_t req;
559 1.1 augustss int ctl;
560 1.1 augustss
561 1.1 augustss DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
562 1.1 augustss reg, onoff));
563 1.1 augustss
564 1.70 mrg if (sc->sc_dying)
565 1.70 mrg return;
566 1.70 mrg
567 1.1 augustss switch (reg) {
568 1.1 augustss case UCOM_SET_DTR:
569 1.1 augustss ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
570 1.1 augustss break;
571 1.1 augustss case UCOM_SET_RTS:
572 1.1 augustss ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
573 1.1 augustss break;
574 1.1 augustss case UCOM_SET_BREAK:
575 1.8 ichiro uftdi_break(sc, portno, onoff);
576 1.1 augustss return;
577 1.1 augustss default:
578 1.1 augustss return;
579 1.1 augustss }
580 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
581 1.1 augustss req.bRequest = FTDI_SIO_MODEM_CTRL;
582 1.1 augustss USETW(req.wValue, ctl);
583 1.1 augustss USETW(req.wIndex, portno);
584 1.1 augustss USETW(req.wLength, 0);
585 1.74 christos DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
586 1.74 christos "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
587 1.1 augustss UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
588 1.1 augustss (void)usbd_do_request(sc->sc_udev, &req, NULL);
589 1.1 augustss }
590 1.1 augustss
591 1.71 ryo /*
592 1.71 ryo * Return true if the given speed is within operational tolerance of the target
593 1.71 ryo * speed. FTDI recommends that the hardware speed be within 3% of nominal.
594 1.71 ryo */
595 1.71 ryo static inline bool
596 1.71 ryo uftdi_baud_within_tolerance(uint64_t speed, uint64_t target)
597 1.71 ryo {
598 1.71 ryo return ((speed >= (target * 100) / 103) &&
599 1.71 ryo (speed <= (target * 100) / 97));
600 1.71 ryo }
601 1.71 ryo
602 1.71 ryo static int
603 1.71 ryo uftdi_encode_baudrate(struct uftdi_softc *sc, int speed, int *rate, int *ratehi)
604 1.71 ryo {
605 1.71 ryo static const uint8_t encoded_fraction[8] = {
606 1.71 ryo 0, 3, 2, 4, 1, 5, 6, 7
607 1.71 ryo };
608 1.71 ryo static const uint8_t roundoff_232a[16] = {
609 1.71 ryo 0, 1, 0, 1, 0, -1, 2, 1,
610 1.71 ryo 0, -1, -2, -3, 4, 3, 2, 1,
611 1.71 ryo };
612 1.71 ryo uint32_t clk, divisor, fastclk_flag, frac, hwspeed;
613 1.71 ryo
614 1.71 ryo /*
615 1.71 ryo * If this chip has the fast clock capability and the speed is within
616 1.71 ryo * range, use the 12MHz clock, otherwise the standard clock is 3MHz.
617 1.71 ryo */
618 1.71 ryo if ((sc->sc_flags & FLAGS_BAUDCLK_12M) && speed >= 1200) {
619 1.71 ryo clk = 12000000;
620 1.71 ryo fastclk_flag = (1 << 17);
621 1.71 ryo } else {
622 1.71 ryo clk = 3000000;
623 1.71 ryo fastclk_flag = 0;
624 1.71 ryo }
625 1.71 ryo
626 1.71 ryo /*
627 1.71 ryo * Make sure the requested speed is reachable with the available clock
628 1.71 ryo * and a 14-bit divisor.
629 1.71 ryo */
630 1.71 ryo if (speed < (clk >> 14) || speed > clk)
631 1.71 ryo return -1;
632 1.71 ryo
633 1.71 ryo /*
634 1.71 ryo * Calculate the divisor, initially yielding a fixed point number with a
635 1.71 ryo * 4-bit (1/16ths) fraction, then round it to the nearest fraction the
636 1.71 ryo * hardware can handle. When the integral part of the divisor is
637 1.71 ryo * greater than one, the fractional part is in 1/8ths of the base clock.
638 1.71 ryo * The FT8U232AM chips can handle only 0.125, 0.250, and 0.5 fractions.
639 1.71 ryo * Later chips can handle all 1/8th fractions.
640 1.71 ryo *
641 1.71 ryo * If the integral part of the divisor is 1, a special rule applies: the
642 1.71 ryo * fractional part can only be .0 or .5 (this is a limitation of the
643 1.71 ryo * hardware). We handle this by truncating the fraction rather than
644 1.71 ryo * rounding, because this only applies to the two fastest speeds the
645 1.71 ryo * chip can achieve and rounding doesn't matter, either you've asked for
646 1.71 ryo * that exact speed or you've asked for something the chip can't do.
647 1.71 ryo *
648 1.71 ryo * For the FT8U232AM chips, use a roundoff table to adjust the result
649 1.71 ryo * to the nearest 1/8th fraction that is supported by the hardware,
650 1.71 ryo * leaving a fixed-point number with a 3-bit fraction which exactly
651 1.71 ryo * represents the math the hardware divider will do. For later-series
652 1.71 ryo * chips that support all 8 fractional divisors, just round 16ths to
653 1.71 ryo * 8ths by adding 1 and dividing by 2.
654 1.71 ryo */
655 1.71 ryo divisor = (clk << 4) / speed;
656 1.71 ryo if ((divisor & 0xf) == 1)
657 1.71 ryo divisor &= 0xfffffff8;
658 1.71 ryo else if (sc->sc_flags & FLAGS_ROUNDOFF_232A)
659 1.71 ryo divisor += roundoff_232a[divisor & 0x0f];
660 1.71 ryo else
661 1.71 ryo divisor += 1; /* Rounds odd 16ths up to next 8th. */
662 1.71 ryo divisor >>= 1;
663 1.71 ryo
664 1.71 ryo /*
665 1.71 ryo * Ensure the resulting hardware speed will be within operational
666 1.71 ryo * tolerance (within 3% of nominal).
667 1.71 ryo */
668 1.71 ryo hwspeed = (clk << 3) / divisor;
669 1.71 ryo if (!uftdi_baud_within_tolerance(hwspeed, speed))
670 1.71 ryo return -1;
671 1.71 ryo
672 1.71 ryo /*
673 1.71 ryo * Re-pack the divisor into hardware format. The lower 14-bits hold the
674 1.71 ryo * integral part, while the upper bits specify the fraction by indexing
675 1.71 ryo * a table of fractions within the hardware which is laid out as:
676 1.71 ryo * {0.0, 0.5, 0.25, 0.125, 0.325, 0.625, 0.725, 0.875}
677 1.71 ryo * The A-series chips only have the first four table entries; the
678 1.71 ryo * roundoff table logic above ensures that the fractional part for those
679 1.71 ryo * chips will be one of the first four values.
680 1.71 ryo *
681 1.71 ryo * When the divisor is 1 a special encoding applies: 1.0 is encoded as
682 1.71 ryo * 0.0, and 1.5 is encoded as 1.0. The rounding logic above has already
683 1.71 ryo * ensured that the fraction is either .0 or .5 if the integral is 1.
684 1.71 ryo */
685 1.71 ryo frac = divisor & 0x07;
686 1.71 ryo divisor >>= 3;
687 1.71 ryo if (divisor == 1) {
688 1.71 ryo if (frac == 0)
689 1.71 ryo divisor = 0; /* 1.0 becomes 0.0 */
690 1.71 ryo else
691 1.71 ryo frac = 0; /* 1.5 becomes 1.0 */
692 1.71 ryo }
693 1.71 ryo divisor |= (encoded_fraction[frac] << 14) | fastclk_flag;
694 1.71 ryo
695 1.71 ryo *rate = (uint16_t)divisor;
696 1.71 ryo *ratehi = (uint16_t)(divisor >> 16);
697 1.71 ryo
698 1.71 ryo /*
699 1.71 ryo * If this chip requires the baud bits to be in the high byte of the
700 1.71 ryo * index word, move the bits up to that location.
701 1.71 ryo */
702 1.71 ryo if (sc->sc_flags & FLAGS_BAUDBITS_HINDEX)
703 1.71 ryo *ratehi <<= 8;
704 1.71 ryo
705 1.71 ryo return 0;
706 1.71 ryo }
707 1.71 ryo
708 1.70 mrg static int
709 1.2 augustss uftdi_param(void *vsc, int portno, struct termios *t)
710 1.1 augustss {
711 1.1 augustss struct uftdi_softc *sc = vsc;
712 1.1 augustss usb_device_request_t req;
713 1.1 augustss usbd_status err;
714 1.71 ryo int rate, ratehi, rerr, data, flow;
715 1.1 augustss
716 1.1 augustss DPRINTF(("uftdi_param: sc=%p\n", sc));
717 1.1 augustss
718 1.1 augustss if (sc->sc_dying)
719 1.61 skrll return EIO;
720 1.1 augustss
721 1.56 mlelstv req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
722 1.56 mlelstv req.bRequest = FTDI_SIO_SET_BITMODE;
723 1.56 mlelstv USETW(req.wValue, FTDI_BITMODE_RESET << 8 | 0x00);
724 1.56 mlelstv USETW(req.wIndex, portno);
725 1.56 mlelstv USETW(req.wLength, 0);
726 1.56 mlelstv err = usbd_do_request(sc->sc_udev, &req, NULL);
727 1.56 mlelstv if (err)
728 1.61 skrll return EIO;
729 1.56 mlelstv
730 1.10 scw switch (sc->sc_type) {
731 1.10 scw case UFTDI_TYPE_SIO:
732 1.10 scw switch (t->c_ospeed) {
733 1.10 scw case 300: rate = ftdi_sio_b300; break;
734 1.10 scw case 600: rate = ftdi_sio_b600; break;
735 1.10 scw case 1200: rate = ftdi_sio_b1200; break;
736 1.10 scw case 2400: rate = ftdi_sio_b2400; break;
737 1.10 scw case 4800: rate = ftdi_sio_b4800; break;
738 1.10 scw case 9600: rate = ftdi_sio_b9600; break;
739 1.10 scw case 19200: rate = ftdi_sio_b19200; break;
740 1.10 scw case 38400: rate = ftdi_sio_b38400; break;
741 1.10 scw case 57600: rate = ftdi_sio_b57600; break;
742 1.10 scw case 115200: rate = ftdi_sio_b115200; break;
743 1.10 scw default:
744 1.61 skrll return EINVAL;
745 1.10 scw }
746 1.71 ryo ratehi = 0;
747 1.10 scw break;
748 1.10 scw case UFTDI_TYPE_8U232AM:
749 1.71 ryo rerr = uftdi_encode_baudrate(sc, t->c_ospeed, &rate, &ratehi);
750 1.71 ryo if (rerr != 0)
751 1.61 skrll return EINVAL;
752 1.10 scw break;
753 1.15 christos default:
754 1.61 skrll return EINVAL;
755 1.1 augustss }
756 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
757 1.1 augustss req.bRequest = FTDI_SIO_SET_BAUD_RATE;
758 1.1 augustss USETW(req.wValue, rate);
759 1.71 ryo USETW(req.wIndex, portno | ratehi);
760 1.1 augustss USETW(req.wLength, 0);
761 1.74 christos DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
762 1.74 christos "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
763 1.1 augustss UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
764 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
765 1.1 augustss if (err)
766 1.61 skrll return EIO;
767 1.1 augustss
768 1.1 augustss if (ISSET(t->c_cflag, CSTOPB))
769 1.1 augustss data = FTDI_SIO_SET_DATA_STOP_BITS_2;
770 1.1 augustss else
771 1.1 augustss data = FTDI_SIO_SET_DATA_STOP_BITS_1;
772 1.1 augustss if (ISSET(t->c_cflag, PARENB)) {
773 1.1 augustss if (ISSET(t->c_cflag, PARODD))
774 1.1 augustss data |= FTDI_SIO_SET_DATA_PARITY_ODD;
775 1.1 augustss else
776 1.1 augustss data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
777 1.1 augustss } else
778 1.1 augustss data |= FTDI_SIO_SET_DATA_PARITY_NONE;
779 1.1 augustss switch (ISSET(t->c_cflag, CSIZE)) {
780 1.1 augustss case CS5:
781 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(5);
782 1.1 augustss break;
783 1.1 augustss case CS6:
784 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(6);
785 1.1 augustss break;
786 1.1 augustss case CS7:
787 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(7);
788 1.1 augustss break;
789 1.1 augustss case CS8:
790 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(8);
791 1.1 augustss break;
792 1.1 augustss }
793 1.8 ichiro sc->last_lcr = data;
794 1.8 ichiro
795 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
796 1.1 augustss req.bRequest = FTDI_SIO_SET_DATA;
797 1.1 augustss USETW(req.wValue, data);
798 1.1 augustss USETW(req.wIndex, portno);
799 1.1 augustss USETW(req.wLength, 0);
800 1.74 christos DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
801 1.74 christos "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
802 1.1 augustss UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
803 1.12 scw err = usbd_do_request(sc->sc_udev, &req, NULL);
804 1.12 scw if (err)
805 1.61 skrll return EIO;
806 1.12 scw
807 1.12 scw if (ISSET(t->c_cflag, CRTSCTS)) {
808 1.12 scw flow = FTDI_SIO_RTS_CTS_HS;
809 1.12 scw USETW(req.wValue, 0);
810 1.58 mlelstv } else if (ISSET(t->c_iflag, IXON) && ISSET(t->c_iflag, IXOFF)) {
811 1.12 scw flow = FTDI_SIO_XON_XOFF_HS;
812 1.12 scw USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
813 1.12 scw } else {
814 1.12 scw flow = FTDI_SIO_DISABLE_FLOW_CTRL;
815 1.12 scw USETW(req.wValue, 0);
816 1.12 scw }
817 1.12 scw req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
818 1.12 scw req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
819 1.12 scw USETW2(req.wIndex, flow, portno);
820 1.12 scw USETW(req.wLength, 0);
821 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
822 1.1 augustss if (err)
823 1.61 skrll return EIO;
824 1.1 augustss
825 1.61 skrll return 0;
826 1.1 augustss }
827 1.1 augustss
828 1.70 mrg static void
829 1.29 christos uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
830 1.1 augustss {
831 1.1 augustss struct uftdi_softc *sc = vsc;
832 1.1 augustss
833 1.74 christos DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
834 1.1 augustss sc->sc_msr, sc->sc_lsr));
835 1.1 augustss
836 1.70 mrg if (sc->sc_dying)
837 1.70 mrg return;
838 1.70 mrg
839 1.68 mrg *msr = sc->sc_msr;
840 1.68 mrg *lsr = sc->sc_lsr;
841 1.8 ichiro }
842 1.8 ichiro
843 1.70 mrg static void
844 1.8 ichiro uftdi_break(void *vsc, int portno, int onoff)
845 1.8 ichiro {
846 1.8 ichiro struct uftdi_softc *sc = vsc;
847 1.9 ichiro usb_device_request_t req;
848 1.8 ichiro int data;
849 1.8 ichiro
850 1.8 ichiro DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
851 1.11 augustss onoff));
852 1.8 ichiro
853 1.8 ichiro if (onoff) {
854 1.8 ichiro data = sc->last_lcr | FTDI_SIO_SET_BREAK;
855 1.8 ichiro } else {
856 1.8 ichiro data = sc->last_lcr;
857 1.8 ichiro }
858 1.8 ichiro
859 1.8 ichiro req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
860 1.8 ichiro req.bRequest = FTDI_SIO_SET_DATA;
861 1.8 ichiro USETW(req.wValue, data);
862 1.8 ichiro USETW(req.wIndex, portno);
863 1.8 ichiro USETW(req.wLength, 0);
864 1.8 ichiro (void)usbd_do_request(sc->sc_udev, &req, NULL);
865 1.1 augustss }
866