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