umcs.c revision 1.7.8.3 1 1.7.8.2 tls /* $NetBSD: umcs.c,v 1.7.8.3 2017/12/03 11:37:34 jdolecek Exp $ */
2 1.7.8.2 tls /* $FreeBSD: head/sys/dev/usb/serial/umcs.c 260559 2014-01-12 11:44:28Z hselasky $ */
3 1.7.8.2 tls
4 1.7.8.2 tls /*-
5 1.7.8.2 tls * Copyright (c) 2010 Lev Serebryakov <lev (at) FreeBSD.org>.
6 1.7.8.2 tls * All rights reserved.
7 1.7.8.2 tls *
8 1.7.8.2 tls * Redistribution and use in source and binary forms, with or without
9 1.7.8.2 tls * modification, are permitted provided that the following conditions
10 1.7.8.2 tls * are met:
11 1.7.8.2 tls * 1. Redistributions of source code must retain the above copyright
12 1.7.8.2 tls * notice, this list of conditions and the following disclaimer.
13 1.7.8.2 tls * 2. Redistributions in binary form must reproduce the above copyright
14 1.7.8.2 tls * notice, this list of conditions and the following disclaimer in the
15 1.7.8.2 tls * documentation and/or other materials provided with the distribution.
16 1.7.8.2 tls *
17 1.7.8.2 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.7.8.2 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.7.8.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.7.8.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.7.8.2 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.7.8.2 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.7.8.2 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.7.8.2 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.7.8.2 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.7.8.2 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.7.8.2 tls * SUCH DAMAGE.
28 1.7.8.2 tls */
29 1.7.8.2 tls
30 1.7.8.2 tls /*
31 1.7.8.2 tls * This driver supports several multiport USB-to-RS232 serial adapters driven
32 1.7.8.2 tls * by MosChip mos7820 and mos7840, bridge chips.
33 1.7.8.2 tls * The adapters are sold under many different brand names.
34 1.7.8.2 tls *
35 1.7.8.2 tls * Datasheets are available at MosChip www site at
36 1.7.8.2 tls * http://www.moschip.com. The datasheets don't contain full
37 1.7.8.2 tls * programming information for the chip.
38 1.7.8.2 tls *
39 1.7.8.2 tls * It is nornal to have only two enabled ports in devices, based on
40 1.7.8.2 tls * quad-port mos7840.
41 1.7.8.2 tls *
42 1.7.8.2 tls */
43 1.7.8.2 tls #include <sys/cdefs.h>
44 1.7.8.2 tls __KERNEL_RCSID(0, "$NetBSD: umcs.c,v 1.7.8.3 2017/12/03 11:37:34 jdolecek Exp $");
45 1.7.8.2 tls
46 1.7.8.2 tls #include <sys/param.h>
47 1.7.8.2 tls #include <sys/systm.h>
48 1.7.8.2 tls #include <sys/atomic.h>
49 1.7.8.2 tls #include <sys/kernel.h>
50 1.7.8.2 tls #include <sys/conf.h>
51 1.7.8.2 tls #include <sys/tty.h>
52 1.7.8.2 tls #include <sys/device.h>
53 1.7.8.2 tls #include <sys/kmem.h>
54 1.7.8.2 tls
55 1.7.8.2 tls #include <dev/usb/usb.h>
56 1.7.8.2 tls #include <dev/usb/usbdi.h>
57 1.7.8.2 tls #include <dev/usb/usbdi_util.h>
58 1.7.8.2 tls #include <dev/usb/usbdevs.h>
59 1.7.8.2 tls
60 1.7.8.2 tls #include <dev/usb/usbdevs.h>
61 1.7.8.2 tls #include <dev/usb/ucomvar.h>
62 1.7.8.2 tls
63 1.7.8.2 tls #include "umcs.h"
64 1.7.8.2 tls
65 1.7.8.2 tls #if 0
66 1.7.8.2 tls #define DPRINTF(ARG) printf ARG
67 1.7.8.2 tls #else
68 1.7.8.2 tls #define DPRINTF(ARG)
69 1.7.8.2 tls #endif
70 1.7.8.2 tls
71 1.7.8.2 tls /*
72 1.7.8.2 tls * Two-port devices (both with 7820 chip and 7840 chip configured as two-port)
73 1.7.8.2 tls * have ports 0 and 2, with ports 1 and 3 omitted.
74 1.7.8.2 tls * So, PHYSICAL port numbers on two-port device will be 0 and 2.
75 1.7.8.2 tls *
76 1.7.8.2 tls * We use an array of the following struct, indexed by ucom port index,
77 1.7.8.2 tls * and include the physical port number in it.
78 1.7.8.2 tls */
79 1.7.8.2 tls struct umcs7840_softc_oneport {
80 1.7.8.2 tls device_t sc_port_ucom; /* ucom subdevice */
81 1.7.8.2 tls unsigned int sc_port_phys; /* physical port number */
82 1.7.8.2 tls uint8_t sc_port_lcr; /* local line control register */
83 1.7.8.2 tls uint8_t sc_port_mcr; /* local modem control register */
84 1.7.8.2 tls };
85 1.7.8.2 tls
86 1.7.8.2 tls struct umcs7840_softc {
87 1.7.8.2 tls device_t sc_dev; /* ourself */
88 1.7.8.3 jdolecek struct usbd_interface *sc_iface; /* the usb interface */
89 1.7.8.3 jdolecek struct usbd_device *sc_udev; /* the usb device */
90 1.7.8.3 jdolecek struct usbd_pipe *sc_intr_pipe; /* interrupt pipe */
91 1.7.8.2 tls uint8_t *sc_intr_buf; /* buffer for interrupt xfer */
92 1.7.8.2 tls unsigned int sc_intr_buflen; /* size of buffer */
93 1.7.8.2 tls struct usb_task sc_change_task; /* async status changes */
94 1.7.8.2 tls volatile uint32_t sc_change_mask; /* mask of port changes */
95 1.7.8.2 tls struct umcs7840_softc_oneport sc_ports[UMCS7840_MAX_PORTS];
96 1.7.8.2 tls /* data for each port */
97 1.7.8.2 tls uint8_t sc_numports; /* number of ports (subunits) */
98 1.7.8.2 tls bool sc_init_done; /* special one time init in open */
99 1.7.8.2 tls bool sc_dying; /* we have been deactivated */
100 1.7.8.2 tls };
101 1.7.8.2 tls
102 1.7.8.3 jdolecek static int umcs7840_get_reg(struct umcs7840_softc *, uint8_t, uint8_t *);
103 1.7.8.3 jdolecek static int umcs7840_set_reg(struct umcs7840_softc *, uint8_t, uint8_t);
104 1.7.8.3 jdolecek static int umcs7840_get_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t *);
105 1.7.8.3 jdolecek static int umcs7840_set_UART_reg(struct umcs7840_softc *, uint8_t, uint8_t, uint8_t );
106 1.7.8.3 jdolecek static int umcs7840_calc_baudrate(uint32_t, uint16_t *, uint8_t *);
107 1.7.8.3 jdolecek static void umcs7840_dtr(struct umcs7840_softc *, int, bool);
108 1.7.8.3 jdolecek static void umcs7840_rts(struct umcs7840_softc *, int, bool);
109 1.7.8.3 jdolecek static void umcs7840_break(struct umcs7840_softc *, int, bool );
110 1.7.8.2 tls
111 1.7.8.2 tls static int umcs7840_match(device_t, cfdata_t, void *);
112 1.7.8.2 tls static void umcs7840_attach(device_t, device_t, void *);
113 1.7.8.2 tls static int umcs7840_detach(device_t, int);
114 1.7.8.3 jdolecek static void umcs7840_intr(struct usbd_xfer *, void *, usbd_status);
115 1.7.8.2 tls static void umcs7840_change_task(void *arg);
116 1.7.8.3 jdolecek static int umcs7840_activate(device_t, enum devact);
117 1.7.8.2 tls static void umcs7840_childdet(device_t, device_t);
118 1.7.8.2 tls
119 1.7.8.2 tls static void umcs7840_get_status(void *, int, u_char *, u_char *);
120 1.7.8.2 tls static void umcs7840_set(void *, int, int, int);
121 1.7.8.2 tls static int umcs7840_param(void *, int, struct termios *);
122 1.7.8.3 jdolecek static int umcs7840_port_open(void *, int);
123 1.7.8.3 jdolecek static void umcs7840_port_close(void *, int);
124 1.7.8.2 tls
125 1.7.8.2 tls struct ucom_methods umcs7840_methods = {
126 1.7.8.2 tls .ucom_get_status = umcs7840_get_status,
127 1.7.8.2 tls .ucom_set = umcs7840_set,
128 1.7.8.2 tls .ucom_param = umcs7840_param,
129 1.7.8.2 tls .ucom_open = umcs7840_port_open,
130 1.7.8.2 tls .ucom_close = umcs7840_port_close,
131 1.7.8.2 tls };
132 1.7.8.2 tls
133 1.7.8.2 tls static const struct usb_devno umcs7840_devs[] = {
134 1.7.8.2 tls { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703 },
135 1.7.8.2 tls { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7810 },
136 1.7.8.2 tls { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7820 },
137 1.7.8.2 tls { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7840 },
138 1.7.8.2 tls { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC2324 }
139 1.7.8.2 tls };
140 1.7.8.2 tls #define umcs7840_lookup(v, p) usb_lookup(umcs7840_devs, v, p)
141 1.7.8.2 tls
142 1.7.8.2 tls CFATTACH_DECL2_NEW(umcs, sizeof(struct umcs7840_softc), umcs7840_match,
143 1.7.8.2 tls umcs7840_attach, umcs7840_detach, umcs7840_activate, NULL,
144 1.7.8.2 tls umcs7840_childdet);
145 1.7.8.2 tls
146 1.7.8.2 tls static inline int
147 1.7.8.2 tls umcs7840_reg_sp(int phyport)
148 1.7.8.2 tls {
149 1.7.8.2 tls KASSERT(phyport >= 0 && phyport < 4);
150 1.7.8.2 tls switch (phyport) {
151 1.7.8.2 tls default:
152 1.7.8.2 tls case 0: return MCS7840_DEV_REG_SP1;
153 1.7.8.2 tls case 1: return MCS7840_DEV_REG_SP2;
154 1.7.8.2 tls case 2: return MCS7840_DEV_REG_SP3;
155 1.7.8.2 tls case 3: return MCS7840_DEV_REG_SP4;
156 1.7.8.2 tls }
157 1.7.8.2 tls }
158 1.7.8.2 tls
159 1.7.8.2 tls static inline int
160 1.7.8.2 tls umcs7840_reg_ctrl(int phyport)
161 1.7.8.2 tls {
162 1.7.8.2 tls KASSERT(phyport >= 0 && phyport < 4);
163 1.7.8.2 tls switch (phyport) {
164 1.7.8.2 tls default:
165 1.7.8.2 tls case 0: return MCS7840_DEV_REG_CONTROL1;
166 1.7.8.2 tls case 1: return MCS7840_DEV_REG_CONTROL2;
167 1.7.8.2 tls case 2: return MCS7840_DEV_REG_CONTROL3;
168 1.7.8.2 tls case 3: return MCS7840_DEV_REG_CONTROL4;
169 1.7.8.2 tls }
170 1.7.8.2 tls }
171 1.7.8.2 tls
172 1.7.8.2 tls static int
173 1.7.8.2 tls umcs7840_match(device_t dev, cfdata_t match, void *aux)
174 1.7.8.2 tls {
175 1.7.8.2 tls struct usb_attach_arg *uaa = aux;
176 1.7.8.2 tls
177 1.7.8.3 jdolecek return umcs7840_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
178 1.7.8.3 jdolecek UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
179 1.7.8.2 tls }
180 1.7.8.2 tls
181 1.7.8.2 tls static void
182 1.7.8.3 jdolecek umcs7840_attach(device_t parent, device_t self, void *aux)
183 1.7.8.2 tls {
184 1.7.8.2 tls struct umcs7840_softc *sc = device_private(self);
185 1.7.8.2 tls struct usb_attach_arg *uaa = aux;
186 1.7.8.3 jdolecek struct usbd_device *dev = uaa->uaa_device;
187 1.7.8.2 tls usb_interface_descriptor_t *id;
188 1.7.8.2 tls usb_endpoint_descriptor_t *ed;
189 1.7.8.2 tls char *devinfop;
190 1.7.8.3 jdolecek struct ucom_attach_args ucaa;
191 1.7.8.2 tls int error, i, intr_addr;
192 1.7.8.2 tls uint8_t data;
193 1.7.8.2 tls
194 1.7.8.2 tls sc->sc_dev = self;
195 1.7.8.3 jdolecek sc->sc_udev = uaa->uaa_device;
196 1.7.8.2 tls
197 1.7.8.2 tls if (usbd_set_config_index(sc->sc_udev, MCS7840_CONFIG_INDEX, 1) != 0) {
198 1.7.8.2 tls aprint_error(": could not set configuration no\n");
199 1.7.8.2 tls return;
200 1.7.8.2 tls }
201 1.7.8.2 tls
202 1.7.8.2 tls /* get the first interface handle */
203 1.7.8.2 tls error = usbd_device2interface_handle(sc->sc_udev, MCS7840_IFACE_INDEX,
204 1.7.8.2 tls &sc->sc_iface);
205 1.7.8.2 tls if (error != 0) {
206 1.7.8.2 tls aprint_error(": could not get interface handle\n");
207 1.7.8.2 tls return;
208 1.7.8.2 tls }
209 1.7.8.2 tls
210 1.7.8.2 tls /*
211 1.7.8.2 tls * Get number of ports
212 1.7.8.2 tls * Documentation (full datasheet) says, that number of ports is
213 1.7.8.2 tls * set as MCS7840_DEV_MODE_SELECT24S bit in MODE R/Only
214 1.7.8.2 tls * register. But vendor driver uses these undocumented
215 1.7.8.2 tls * register & bit.
216 1.7.8.2 tls *
217 1.7.8.2 tls * Experiments show, that MODE register can have `0'
218 1.7.8.2 tls * (4 ports) bit on 2-port device, so use vendor driver's way.
219 1.7.8.2 tls *
220 1.7.8.2 tls * Also, see notes in header file for these constants.
221 1.7.8.2 tls */
222 1.7.8.2 tls umcs7840_get_reg(sc, MCS7840_DEV_REG_GPIO, &data);
223 1.7.8.2 tls if (data & MCS7840_DEV_GPIO_4PORTS) {
224 1.7.8.2 tls sc->sc_numports = 4;
225 1.7.8.2 tls /* physical port no are : 0, 1, 2, 3 */
226 1.7.8.2 tls } else {
227 1.7.8.3 jdolecek if (uaa->uaa_product == USB_PRODUCT_MOSCHIP_MCS7810)
228 1.7.8.2 tls sc->sc_numports = 1;
229 1.7.8.2 tls else {
230 1.7.8.2 tls sc->sc_numports = 2;
231 1.7.8.2 tls /* physical port no are : 0 and 2 */
232 1.7.8.2 tls }
233 1.7.8.2 tls }
234 1.7.8.2 tls devinfop = usbd_devinfo_alloc(dev, 0);
235 1.7.8.2 tls aprint_normal(": %s\n", devinfop);
236 1.7.8.2 tls usbd_devinfo_free(devinfop);
237 1.7.8.2 tls aprint_verbose_dev(self, "found %d active ports\n", sc->sc_numports);
238 1.7.8.2 tls
239 1.7.8.2 tls if (!umcs7840_get_reg(sc, MCS7840_DEV_REG_MODE, &data)) {
240 1.7.8.2 tls aprint_verbose_dev(self, "On-die confguration: RST: active %s, "
241 1.7.8.2 tls "HRD: %s, PLL: %s, POR: %s, Ports: %s, EEPROM write %s, "
242 1.7.8.2 tls "IrDA is %savailable\n",
243 1.7.8.2 tls (data & MCS7840_DEV_MODE_RESET) ? "low" : "high",
244 1.7.8.2 tls (data & MCS7840_DEV_MODE_SER_PRSNT) ? "yes" : "no",
245 1.7.8.2 tls (data & MCS7840_DEV_MODE_PLLBYPASS) ? "bypassed" : "avail",
246 1.7.8.2 tls (data & MCS7840_DEV_MODE_PORBYPASS) ? "bypassed" : "avail",
247 1.7.8.2 tls (data & MCS7840_DEV_MODE_SELECT24S) ? "2" : "4",
248 1.7.8.2 tls (data & MCS7840_DEV_MODE_EEPROMWR) ? "enabled" : "disabled",
249 1.7.8.2 tls (data & MCS7840_DEV_MODE_IRDA) ? "" : "not ");
250 1.7.8.2 tls }
251 1.7.8.2 tls
252 1.7.8.2 tls /*
253 1.7.8.2 tls * Set up the interrupt pipe
254 1.7.8.2 tls */
255 1.7.8.2 tls id = usbd_get_interface_descriptor(sc->sc_iface);
256 1.7.8.2 tls intr_addr = -1;
257 1.7.8.2 tls for (i = 0 ; i < id->bNumEndpoints ; i++) {
258 1.7.8.2 tls ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
259 1.7.8.2 tls if (ed == NULL) continue;
260 1.7.8.2 tls if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN
261 1.7.8.2 tls || UE_GET_XFERTYPE(ed->bmAttributes) != UE_INTERRUPT)
262 1.7.8.2 tls continue;
263 1.7.8.2 tls sc->sc_intr_buflen = UGETW(ed->wMaxPacketSize);
264 1.7.8.2 tls intr_addr = ed->bEndpointAddress;
265 1.7.8.2 tls break;
266 1.7.8.2 tls }
267 1.7.8.2 tls if (intr_addr < 0) {
268 1.7.8.2 tls aprint_error_dev(self, "interrupt pipe not found\n");
269 1.7.8.2 tls return;
270 1.7.8.2 tls }
271 1.7.8.2 tls sc->sc_intr_buf = kmem_alloc(sc->sc_intr_buflen, KM_SLEEP);
272 1.7.8.2 tls
273 1.7.8.2 tls error = usbd_open_pipe_intr(sc->sc_iface, intr_addr,
274 1.7.8.2 tls USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_intr_buf,
275 1.7.8.2 tls sc->sc_intr_buflen, umcs7840_intr, 100);
276 1.7.8.2 tls if (error) {
277 1.7.8.2 tls aprint_error_dev(self, "cannot open interrupt pipe "
278 1.7.8.2 tls "(addr %d)\n", intr_addr);
279 1.7.8.2 tls return;
280 1.7.8.2 tls }
281 1.7.8.2 tls
282 1.7.8.2 tls usb_init_task(&sc->sc_change_task, umcs7840_change_task, sc,
283 1.7.8.2 tls USB_TASKQ_MPSAFE);
284 1.7.8.2 tls
285 1.7.8.3 jdolecek usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
286 1.7.8.2 tls
287 1.7.8.3 jdolecek memset(&ucaa, 0, sizeof(ucaa));
288 1.7.8.3 jdolecek ucaa.ucaa_ibufsize = 256;
289 1.7.8.3 jdolecek ucaa.ucaa_obufsize = 256;
290 1.7.8.3 jdolecek ucaa.ucaa_ibufsizepad = 256;
291 1.7.8.3 jdolecek ucaa.ucaa_opkthdrlen = 0;
292 1.7.8.3 jdolecek ucaa.ucaa_device = sc->sc_udev;
293 1.7.8.3 jdolecek ucaa.ucaa_iface = sc->sc_iface;
294 1.7.8.3 jdolecek ucaa.ucaa_methods = &umcs7840_methods;
295 1.7.8.3 jdolecek ucaa.ucaa_arg = sc;
296 1.7.8.2 tls
297 1.7.8.2 tls for (i = 0; i < sc->sc_numports; i++) {
298 1.7.8.3 jdolecek ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
299 1.7.8.2 tls
300 1.7.8.2 tls /*
301 1.7.8.2 tls * On four port cards, endpoints are 0/1 for first,
302 1.7.8.2 tls * 2/3 for second, ...
303 1.7.8.2 tls * On two port cards, they are 0/1 for first, 4/5 for second.
304 1.7.8.2 tls * On single port, just 0/1 will be used.
305 1.7.8.2 tls */
306 1.7.8.2 tls int phyport = i * (sc->sc_numports == 2 ? 2 : 1);
307 1.7.8.2 tls
308 1.7.8.2 tls ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
309 1.7.8.2 tls phyport*2);
310 1.7.8.2 tls if (ed == NULL) {
311 1.7.8.2 tls aprint_error_dev(self,
312 1.7.8.2 tls "no bulk in endpoint found for %d\n", i);
313 1.7.8.2 tls return;
314 1.7.8.2 tls }
315 1.7.8.3 jdolecek ucaa.ucaa_bulkin = ed->bEndpointAddress;
316 1.7.8.2 tls
317 1.7.8.2 tls ed = usbd_interface2endpoint_descriptor(sc->sc_iface,
318 1.7.8.2 tls phyport*2 + 1);
319 1.7.8.2 tls if (ed == NULL) {
320 1.7.8.2 tls aprint_error_dev(self,
321 1.7.8.2 tls "no bulk out endpoint found for %d\n", i);
322 1.7.8.2 tls return;
323 1.7.8.2 tls }
324 1.7.8.3 jdolecek ucaa.ucaa_bulkout = ed->bEndpointAddress;
325 1.7.8.3 jdolecek ucaa.ucaa_portno = i;
326 1.7.8.2 tls DPRINTF(("port %d physical port %d bulk-in %d bulk-out %d\n",
327 1.7.8.3 jdolecek i, phyport, ucaa.ucaa_bulkin, ucaa.ucaa_bulkout));
328 1.7.8.2 tls
329 1.7.8.2 tls sc->sc_ports[i].sc_port_phys = phyport;
330 1.7.8.2 tls sc->sc_ports[i].sc_port_ucom =
331 1.7.8.3 jdolecek config_found_sm_loc(self, "ucombus", NULL, &ucaa,
332 1.7.8.2 tls ucomprint, ucomsubmatch);
333 1.7.8.2 tls }
334 1.7.8.2 tls }
335 1.7.8.2 tls
336 1.7.8.2 tls static int
337 1.7.8.2 tls umcs7840_get_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t *data)
338 1.7.8.2 tls {
339 1.7.8.2 tls usb_device_request_t req;
340 1.7.8.2 tls int err;
341 1.7.8.2 tls
342 1.7.8.2 tls req.bmRequestType = UT_READ_VENDOR_DEVICE;
343 1.7.8.2 tls req.bRequest = MCS7840_RDREQ;
344 1.7.8.2 tls USETW(req.wValue, 0);
345 1.7.8.2 tls USETW(req.wIndex, reg);
346 1.7.8.2 tls USETW(req.wLength, UMCS7840_READ_LENGTH);
347 1.7.8.2 tls
348 1.7.8.2 tls err = usbd_do_request(sc->sc_udev, &req, data);
349 1.7.8.2 tls if (err)
350 1.7.8.2 tls aprint_normal_dev(sc->sc_dev,
351 1.7.8.2 tls "Reading register %d failed: %s\n", reg, usbd_errstr(err));
352 1.7.8.2 tls return err;
353 1.7.8.2 tls }
354 1.7.8.2 tls
355 1.7.8.2 tls static int
356 1.7.8.2 tls umcs7840_set_reg(struct umcs7840_softc *sc, uint8_t reg, uint8_t data)
357 1.7.8.2 tls {
358 1.7.8.2 tls usb_device_request_t req;
359 1.7.8.2 tls int err;
360 1.7.8.2 tls
361 1.7.8.2 tls req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
362 1.7.8.2 tls req.bRequest = MCS7840_WRREQ;
363 1.7.8.2 tls USETW(req.wValue, data);
364 1.7.8.2 tls USETW(req.wIndex, reg);
365 1.7.8.2 tls USETW(req.wLength, 0);
366 1.7.8.2 tls
367 1.7.8.2 tls err = usbd_do_request(sc->sc_udev, &req, 0);
368 1.7.8.2 tls if (err)
369 1.7.8.2 tls aprint_normal_dev(sc->sc_dev, "Writing register %d failed: %s\n", reg, usbd_errstr(err));
370 1.7.8.2 tls
371 1.7.8.2 tls return err;
372 1.7.8.2 tls }
373 1.7.8.2 tls
374 1.7.8.2 tls static int
375 1.7.8.2 tls umcs7840_get_UART_reg(struct umcs7840_softc *sc, uint8_t portno,
376 1.7.8.2 tls uint8_t reg, uint8_t *data)
377 1.7.8.2 tls {
378 1.7.8.2 tls usb_device_request_t req;
379 1.7.8.2 tls uint16_t wVal;
380 1.7.8.2 tls int err;
381 1.7.8.2 tls
382 1.7.8.2 tls /* portno is port number */
383 1.7.8.2 tls wVal = ((uint16_t)(portno + 1)) << 8;
384 1.7.8.2 tls
385 1.7.8.2 tls req.bmRequestType = UT_READ_VENDOR_DEVICE;
386 1.7.8.2 tls req.bRequest = MCS7840_RDREQ;
387 1.7.8.2 tls USETW(req.wValue, wVal);
388 1.7.8.2 tls USETW(req.wIndex, reg);
389 1.7.8.2 tls USETW(req.wLength, UMCS7840_READ_LENGTH);
390 1.7.8.2 tls
391 1.7.8.2 tls err = usbd_do_request(sc->sc_udev, &req, data);
392 1.7.8.2 tls if (err)
393 1.7.8.2 tls aprint_normal_dev(sc->sc_dev, "Reading UART %d register %d failed: %s\n", portno, reg, usbd_errstr(err));
394 1.7.8.2 tls return err;
395 1.7.8.2 tls }
396 1.7.8.2 tls
397 1.7.8.2 tls static int
398 1.7.8.2 tls umcs7840_set_UART_reg(struct umcs7840_softc *sc, uint8_t portno, uint8_t reg, uint8_t data)
399 1.7.8.2 tls {
400 1.7.8.2 tls usb_device_request_t req;
401 1.7.8.2 tls int err;
402 1.7.8.2 tls uint16_t wVal;
403 1.7.8.2 tls
404 1.7.8.2 tls /* portno is the physical port number */
405 1.7.8.2 tls wVal = ((uint16_t)(portno + 1)) << 8 | data;
406 1.7.8.2 tls
407 1.7.8.2 tls req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
408 1.7.8.2 tls req.bRequest = MCS7840_WRREQ;
409 1.7.8.2 tls USETW(req.wValue, wVal);
410 1.7.8.2 tls USETW(req.wIndex, reg);
411 1.7.8.2 tls USETW(req.wLength, 0);
412 1.7.8.2 tls
413 1.7.8.2 tls err = usbd_do_request(sc->sc_udev, &req, NULL);
414 1.7.8.2 tls if (err)
415 1.7.8.2 tls aprint_error_dev(sc->sc_dev,
416 1.7.8.2 tls "Writing UART %d register %d failed: %s\n",
417 1.7.8.2 tls portno, reg, usbd_errstr(err));
418 1.7.8.2 tls return err;
419 1.7.8.2 tls }
420 1.7.8.2 tls
421 1.7.8.2 tls static int
422 1.7.8.3 jdolecek umcs7840_set_baudrate(struct umcs7840_softc *sc, uint8_t portno,
423 1.7.8.3 jdolecek uint32_t rate)
424 1.7.8.2 tls {
425 1.7.8.2 tls int err;
426 1.7.8.2 tls uint16_t divisor;
427 1.7.8.2 tls uint8_t clk;
428 1.7.8.2 tls uint8_t data;
429 1.7.8.2 tls uint8_t physport = sc->sc_ports[portno].sc_port_phys;
430 1.7.8.2 tls int spreg = umcs7840_reg_sp(physport);
431 1.7.8.2 tls
432 1.7.8.2 tls if (umcs7840_calc_baudrate(rate, &divisor, &clk)) {
433 1.7.8.2 tls DPRINTF(("Port %d bad speed: %d\n", portno, rate));
434 1.7.8.3 jdolecek return -1;
435 1.7.8.2 tls }
436 1.7.8.2 tls if (divisor == 0 || (clk & MCS7840_DEV_SPx_CLOCK_MASK) != clk) {
437 1.7.8.3 jdolecek DPRINTF(("Port %d bad speed calculation: %d\n", portno,
438 1.7.8.3 jdolecek rate));
439 1.7.8.3 jdolecek return -1;
440 1.7.8.2 tls }
441 1.7.8.2 tls DPRINTF(("Port %d set speed: %d (%02x / %d)\n", portno, rate, clk, divisor));
442 1.7.8.2 tls
443 1.7.8.2 tls /* Set clock source for standard BAUD frequences */
444 1.7.8.2 tls err = umcs7840_get_reg(sc, spreg, &data);
445 1.7.8.2 tls if (err)
446 1.7.8.2 tls return err;
447 1.7.8.2 tls data &= MCS7840_DEV_SPx_CLOCK_MASK;
448 1.7.8.2 tls data |= clk;
449 1.7.8.2 tls err = umcs7840_set_reg(sc, spreg, data);
450 1.7.8.2 tls if (err)
451 1.7.8.2 tls return err;
452 1.7.8.2 tls
453 1.7.8.2 tls /* Set divider */
454 1.7.8.2 tls sc->sc_ports[portno].sc_port_lcr |= MCS7840_UART_LCR_DIVISORS;
455 1.7.8.2 tls err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
456 1.7.8.2 tls if (err)
457 1.7.8.2 tls return err;
458 1.7.8.2 tls
459 1.7.8.2 tls err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLL, (uint8_t)(divisor & 0xff));
460 1.7.8.2 tls if (err)
461 1.7.8.2 tls return err;
462 1.7.8.2 tls err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_DLM, (uint8_t)((divisor >> 8) & 0xff));
463 1.7.8.2 tls if (err)
464 1.7.8.2 tls return err;
465 1.7.8.2 tls
466 1.7.8.2 tls /* Turn off access to DLL/DLM registers of UART */
467 1.7.8.2 tls sc->sc_ports[portno].sc_port_lcr &= ~MCS7840_UART_LCR_DIVISORS;
468 1.7.8.2 tls err = umcs7840_set_UART_reg(sc, physport, MCS7840_UART_REG_LCR, sc->sc_ports[portno].sc_port_lcr);
469 1.7.8.2 tls if (err)
470 1.7.8.2 tls return err;
471 1.7.8.3 jdolecek return 0;
472 1.7.8.2 tls }
473 1.7.8.2 tls
474 1.7.8.2 tls static int
475 1.7.8.2 tls umcs7840_calc_baudrate(uint32_t rate, uint16_t *divisor, uint8_t *clk)
476 1.7.8.2 tls {
477 1.7.8.2 tls /* Maximum speeds for standard frequences, when PLL is not used */
478 1.7.8.2 tls static const uint32_t umcs7840_baudrate_divisors[] =
479 1.7.8.2 tls {0, 115200, 230400, 403200, 460800, 806400, 921600,
480 1.7.8.2 tls 1572864, 3145728,};
481 1.7.8.2 tls static const uint8_t umcs7840_baudrate_divisors_len =
482 1.7.8.2 tls __arraycount(umcs7840_baudrate_divisors);
483 1.7.8.2 tls uint8_t i = 0;
484 1.7.8.2 tls
485 1.7.8.2 tls if (rate > umcs7840_baudrate_divisors[umcs7840_baudrate_divisors_len - 1])
486 1.7.8.3 jdolecek return -1;
487 1.7.8.2 tls
488 1.7.8.2 tls for (i = 0; i < umcs7840_baudrate_divisors_len - 1
489 1.7.8.2 tls && !(rate > umcs7840_baudrate_divisors[i]
490 1.7.8.2 tls && rate <= umcs7840_baudrate_divisors[i + 1]); ++i);
491 1.7.8.2 tls *divisor = umcs7840_baudrate_divisors[i + 1] / rate;
492 1.7.8.2 tls /* 0x00 .. 0x70 */
493 1.7.8.2 tls *clk = i << MCS7840_DEV_SPx_CLOCK_SHIFT;
494 1.7.8.3 jdolecek return 0;
495 1.7.8.2 tls }
496 1.7.8.2 tls
497 1.7.8.3 jdolecek static int
498 1.7.8.2 tls umcs7840_detach(device_t self, int flags)
499 1.7.8.2 tls {
500 1.7.8.2 tls struct umcs7840_softc *sc = device_private(self);
501 1.7.8.2 tls int rv = 0, i;
502 1.7.8.2 tls
503 1.7.8.2 tls sc->sc_dying = true;
504 1.7.8.2 tls
505 1.7.8.2 tls /* close interrupt pipe */
506 1.7.8.2 tls if (sc->sc_intr_pipe != NULL) {
507 1.7.8.2 tls rv = usbd_abort_pipe(sc->sc_intr_pipe);
508 1.7.8.2 tls if (rv)
509 1.7.8.2 tls aprint_error_dev(sc->sc_dev,
510 1.7.8.2 tls "abort interrupt pipe failed: %s\n",
511 1.7.8.2 tls usbd_errstr(rv));
512 1.7.8.2 tls rv = usbd_close_pipe(sc->sc_intr_pipe);
513 1.7.8.2 tls if (rv)
514 1.7.8.2 tls aprint_error_dev(sc->sc_dev,
515 1.7.8.2 tls "failed to close interrupt pipe: %s\n",
516 1.7.8.2 tls usbd_errstr(rv));
517 1.7.8.2 tls kmem_free(sc->sc_intr_buf, sc->sc_intr_buflen);
518 1.7.8.2 tls sc->sc_intr_pipe = NULL;
519 1.7.8.2 tls }
520 1.7.8.2 tls usb_rem_task(sc->sc_udev, &sc->sc_change_task);
521 1.7.8.2 tls
522 1.7.8.2 tls /* detach children */
523 1.7.8.2 tls for (i = 0; i < sc->sc_numports; i++) {
524 1.7.8.2 tls if (sc->sc_ports[i].sc_port_ucom) {
525 1.7.8.2 tls rv = config_detach(sc->sc_ports[i].sc_port_ucom,
526 1.7.8.2 tls flags);
527 1.7.8.2 tls if (rv)
528 1.7.8.2 tls break;
529 1.7.8.2 tls }
530 1.7.8.2 tls }
531 1.7.8.2 tls
532 1.7.8.3 jdolecek usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
533 1.7.8.2 tls
534 1.7.8.2 tls return rv;
535 1.7.8.2 tls }
536 1.7.8.2 tls
537 1.7.8.2 tls int
538 1.7.8.2 tls umcs7840_activate(device_t self, enum devact act)
539 1.7.8.2 tls {
540 1.7.8.2 tls struct umcs7840_softc *sc = device_private(self);
541 1.7.8.2 tls
542 1.7.8.2 tls switch (act) {
543 1.7.8.2 tls case DVACT_DEACTIVATE:
544 1.7.8.2 tls sc->sc_dying = true;
545 1.7.8.2 tls return 0;
546 1.7.8.2 tls default:
547 1.7.8.2 tls return EOPNOTSUPP;
548 1.7.8.2 tls }
549 1.7.8.2 tls }
550 1.7.8.2 tls
551 1.7.8.2 tls static void
552 1.7.8.2 tls umcs7840_childdet(device_t self, device_t child)
553 1.7.8.2 tls {
554 1.7.8.2 tls struct umcs7840_softc *sc = device_private(self);
555 1.7.8.2 tls int i;
556 1.7.8.2 tls
557 1.7.8.2 tls for (i = 0; i < sc->sc_numports; i++) {
558 1.7.8.2 tls if (child == sc->sc_ports[i].sc_port_ucom) {
559 1.7.8.2 tls sc->sc_ports[i].sc_port_ucom = NULL;
560 1.7.8.2 tls return;
561 1.7.8.2 tls }
562 1.7.8.2 tls }
563 1.7.8.2 tls }
564 1.7.8.2 tls
565 1.7.8.2 tls static void
566 1.7.8.2 tls umcs7840_get_status(void *self, int portno, u_char *lsr, u_char *msr)
567 1.7.8.2 tls {
568 1.7.8.2 tls struct umcs7840_softc *sc = self;
569 1.7.8.2 tls uint8_t pn = sc->sc_ports[portno].sc_port_phys;
570 1.7.8.2 tls uint8_t hw_lsr = 0; /* local line status register */
571 1.7.8.2 tls uint8_t hw_msr = 0; /* local modem status register */
572 1.7.8.2 tls
573 1.7.8.2 tls if (sc->sc_dying)
574 1.7.8.2 tls return;
575 1.7.8.2 tls
576 1.7.8.2 tls /* Read LSR & MSR */
577 1.7.8.2 tls umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LSR, &hw_lsr);
578 1.7.8.2 tls umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_MSR, &hw_msr);
579 1.7.8.2 tls
580 1.7.8.2 tls *lsr = hw_lsr;
581 1.7.8.2 tls *msr = hw_msr;
582 1.7.8.2 tls }
583 1.7.8.2 tls
584 1.7.8.2 tls static void
585 1.7.8.2 tls umcs7840_set(void *self, int portno, int reg, int onoff)
586 1.7.8.2 tls {
587 1.7.8.2 tls struct umcs7840_softc *sc = self;
588 1.7.8.2 tls
589 1.7.8.2 tls if (sc->sc_dying)
590 1.7.8.2 tls return;
591 1.7.8.2 tls
592 1.7.8.2 tls switch (reg) {
593 1.7.8.2 tls case UCOM_SET_DTR:
594 1.7.8.3 jdolecek umcs7840_dtr(sc, portno, onoff);
595 1.7.8.2 tls break;
596 1.7.8.2 tls case UCOM_SET_RTS:
597 1.7.8.3 jdolecek umcs7840_rts(sc, portno, onoff);
598 1.7.8.2 tls break;
599 1.7.8.2 tls case UCOM_SET_BREAK:
600 1.7.8.3 jdolecek umcs7840_break(sc, portno, onoff);
601 1.7.8.2 tls break;
602 1.7.8.2 tls default:
603 1.7.8.2 tls break;
604 1.7.8.2 tls }
605 1.7.8.2 tls }
606 1.7.8.2 tls
607 1.7.8.2 tls static int
608 1.7.8.2 tls umcs7840_param(void *self, int portno, struct termios *t)
609 1.7.8.2 tls {
610 1.7.8.2 tls struct umcs7840_softc *sc = self;
611 1.7.8.2 tls int pn = sc->sc_ports[portno].sc_port_phys;
612 1.7.8.2 tls uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
613 1.7.8.2 tls uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
614 1.7.8.2 tls
615 1.7.8.2 tls if (t->c_cflag & CSTOPB) {
616 1.7.8.2 tls lcr |= MCS7840_UART_LCR_STOPB2;
617 1.7.8.2 tls } else {
618 1.7.8.2 tls lcr |= MCS7840_UART_LCR_STOPB1;
619 1.7.8.2 tls }
620 1.7.8.2 tls
621 1.7.8.2 tls lcr &= ~MCS7840_UART_LCR_PARITYMASK;
622 1.7.8.2 tls if (t->c_cflag & PARENB) {
623 1.7.8.2 tls lcr |= MCS7840_UART_LCR_PARITYON;
624 1.7.8.2 tls if (t->c_cflag & PARODD) {
625 1.7.8.2 tls lcr = MCS7840_UART_LCR_PARITYODD;
626 1.7.8.2 tls } else {
627 1.7.8.2 tls lcr = MCS7840_UART_LCR_PARITYEVEN;
628 1.7.8.2 tls }
629 1.7.8.2 tls } else {
630 1.7.8.2 tls lcr &= ~MCS7840_UART_LCR_PARITYON;
631 1.7.8.2 tls }
632 1.7.8.2 tls
633 1.7.8.2 tls lcr &= ~MCS7840_UART_LCR_DATALENMASK;
634 1.7.8.2 tls switch (t->c_cflag & CSIZE) {
635 1.7.8.2 tls case CS5:
636 1.7.8.2 tls lcr |= MCS7840_UART_LCR_DATALEN5;
637 1.7.8.2 tls break;
638 1.7.8.2 tls case CS6:
639 1.7.8.2 tls lcr |= MCS7840_UART_LCR_DATALEN6;
640 1.7.8.2 tls break;
641 1.7.8.2 tls case CS7:
642 1.7.8.2 tls lcr |= MCS7840_UART_LCR_DATALEN7;
643 1.7.8.2 tls break;
644 1.7.8.2 tls case CS8:
645 1.7.8.2 tls lcr |= MCS7840_UART_LCR_DATALEN8;
646 1.7.8.2 tls break;
647 1.7.8.2 tls }
648 1.7.8.2 tls
649 1.7.8.2 tls if (t->c_cflag & CRTSCTS)
650 1.7.8.2 tls mcr |= MCS7840_UART_MCR_CTSRTS;
651 1.7.8.2 tls else
652 1.7.8.2 tls mcr &= ~MCS7840_UART_MCR_CTSRTS;
653 1.7.8.2 tls
654 1.7.8.2 tls if (t->c_cflag & CLOCAL)
655 1.7.8.2 tls mcr &= ~MCS7840_UART_MCR_DTRDSR;
656 1.7.8.2 tls else
657 1.7.8.2 tls mcr |= MCS7840_UART_MCR_DTRDSR;
658 1.7.8.2 tls
659 1.7.8.2 tls sc->sc_ports[portno].sc_port_lcr = lcr;
660 1.7.8.2 tls umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
661 1.7.8.2 tls sc->sc_ports[pn].sc_port_lcr);
662 1.7.8.2 tls
663 1.7.8.2 tls sc->sc_ports[portno].sc_port_mcr = mcr;
664 1.7.8.2 tls umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
665 1.7.8.2 tls sc->sc_ports[pn].sc_port_mcr);
666 1.7.8.2 tls
667 1.7.8.3 jdolecek if (umcs7840_set_baudrate(sc, portno, t->c_ospeed))
668 1.7.8.2 tls return EIO;
669 1.7.8.2 tls
670 1.7.8.2 tls return 0;
671 1.7.8.2 tls }
672 1.7.8.2 tls
673 1.7.8.2 tls static void
674 1.7.8.2 tls umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff)
675 1.7.8.2 tls {
676 1.7.8.2 tls int pn = sc->sc_ports[portno].sc_port_phys;
677 1.7.8.2 tls uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
678 1.7.8.2 tls
679 1.7.8.2 tls if (onoff)
680 1.7.8.2 tls mcr |= MCS7840_UART_MCR_DTR;
681 1.7.8.2 tls else
682 1.7.8.2 tls mcr &= ~MCS7840_UART_MCR_DTR;
683 1.7.8.2 tls
684 1.7.8.2 tls sc->sc_ports[portno].sc_port_mcr = mcr;
685 1.7.8.2 tls umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
686 1.7.8.2 tls sc->sc_ports[pn].sc_port_mcr);
687 1.7.8.2 tls }
688 1.7.8.2 tls
689 1.7.8.2 tls static void
690 1.7.8.2 tls umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff)
691 1.7.8.2 tls {
692 1.7.8.2 tls int pn = sc->sc_ports[portno].sc_port_phys;
693 1.7.8.2 tls uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
694 1.7.8.2 tls
695 1.7.8.2 tls if (onoff)
696 1.7.8.2 tls mcr |= MCS7840_UART_MCR_RTS;
697 1.7.8.2 tls else
698 1.7.8.2 tls mcr &= ~MCS7840_UART_MCR_RTS;
699 1.7.8.2 tls
700 1.7.8.2 tls sc->sc_ports[portno].sc_port_mcr = mcr;
701 1.7.8.2 tls umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
702 1.7.8.2 tls sc->sc_ports[pn].sc_port_mcr);
703 1.7.8.2 tls }
704 1.7.8.2 tls
705 1.7.8.2 tls static void
706 1.7.8.2 tls umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff)
707 1.7.8.2 tls {
708 1.7.8.2 tls int pn = sc->sc_ports[portno].sc_port_phys;
709 1.7.8.2 tls uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
710 1.7.8.2 tls
711 1.7.8.2 tls if (onoff)
712 1.7.8.2 tls lcr |= MCS7840_UART_LCR_BREAK;
713 1.7.8.2 tls else
714 1.7.8.2 tls lcr &= ~MCS7840_UART_LCR_BREAK;
715 1.7.8.2 tls
716 1.7.8.2 tls sc->sc_ports[portno].sc_port_lcr = lcr;
717 1.7.8.2 tls umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
718 1.7.8.2 tls sc->sc_ports[pn].sc_port_lcr);
719 1.7.8.2 tls }
720 1.7.8.2 tls
721 1.7.8.2 tls static int
722 1.7.8.2 tls umcs7840_port_open(void *self, int portno)
723 1.7.8.2 tls {
724 1.7.8.2 tls struct umcs7840_softc *sc = self;
725 1.7.8.2 tls int pn = sc->sc_ports[portno].sc_port_phys;
726 1.7.8.2 tls int spreg = umcs7840_reg_sp(pn);
727 1.7.8.2 tls int ctrlreg = umcs7840_reg_ctrl(pn);
728 1.7.8.2 tls uint8_t data;
729 1.7.8.2 tls
730 1.7.8.2 tls if (sc->sc_dying)
731 1.7.8.2 tls return EIO;
732 1.7.8.2 tls
733 1.7.8.2 tls /* If it very first open, finish global configuration */
734 1.7.8.2 tls if (!sc->sc_init_done) {
735 1.7.8.2 tls if (umcs7840_get_reg(sc, MCS7840_DEV_REG_CONTROL1, &data))
736 1.7.8.2 tls return EIO;
737 1.7.8.2 tls data |= MCS7840_DEV_CONTROL1_DRIVER_DONE;
738 1.7.8.2 tls if (umcs7840_set_reg(sc, MCS7840_DEV_REG_CONTROL1, data))
739 1.7.8.2 tls return EIO;
740 1.7.8.2 tls sc->sc_init_done = 1;
741 1.7.8.2 tls }
742 1.7.8.2 tls
743 1.7.8.2 tls /* Toggle reset bit on-off */
744 1.7.8.2 tls if (umcs7840_get_reg(sc, spreg, &data))
745 1.7.8.2 tls return EIO;
746 1.7.8.2 tls data |= MCS7840_DEV_SPx_UART_RESET;
747 1.7.8.2 tls if (umcs7840_set_reg(sc, spreg, data))
748 1.7.8.2 tls return EIO;
749 1.7.8.2 tls data &= ~MCS7840_DEV_SPx_UART_RESET;
750 1.7.8.2 tls if (umcs7840_set_reg(sc, spreg, data))
751 1.7.8.2 tls return EIO;
752 1.7.8.2 tls
753 1.7.8.2 tls /* Set RS-232 mode */
754 1.7.8.2 tls if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_SCRATCHPAD,
755 1.7.8.2 tls MCS7840_UART_SCRATCHPAD_RS232))
756 1.7.8.2 tls return EIO;
757 1.7.8.2 tls
758 1.7.8.2 tls /* Disable RX on time of initialization */
759 1.7.8.2 tls if (umcs7840_get_reg(sc, ctrlreg, &data))
760 1.7.8.2 tls return EIO;
761 1.7.8.2 tls data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
762 1.7.8.2 tls if (umcs7840_set_reg(sc, ctrlreg, data))
763 1.7.8.2 tls return EIO;
764 1.7.8.2 tls
765 1.7.8.2 tls /* Disable all interrupts */
766 1.7.8.2 tls if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0))
767 1.7.8.2 tls return EIO;
768 1.7.8.2 tls
769 1.7.8.2 tls /* Reset FIFO -- documented */
770 1.7.8.2 tls if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR, 0))
771 1.7.8.2 tls return EIO;
772 1.7.8.2 tls if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR,
773 1.7.8.2 tls MCS7840_UART_FCR_ENABLE | MCS7840_UART_FCR_FLUSHRHR |
774 1.7.8.2 tls MCS7840_UART_FCR_FLUSHTHR | MCS7840_UART_FCR_RTL_1_14))
775 1.7.8.2 tls return EIO;
776 1.7.8.2 tls
777 1.7.8.2 tls /* Set 8 bit, no parity, 1 stop bit -- documented */
778 1.7.8.2 tls sc->sc_ports[pn].sc_port_lcr =
779 1.7.8.2 tls MCS7840_UART_LCR_DATALEN8 | MCS7840_UART_LCR_STOPB1;
780 1.7.8.2 tls if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
781 1.7.8.2 tls sc->sc_ports[pn].sc_port_lcr))
782 1.7.8.2 tls return EIO;
783 1.7.8.2 tls
784 1.7.8.2 tls /*
785 1.7.8.2 tls * Enable DTR/RTS on modem control, enable modem interrupts --
786 1.7.8.2 tls * documented
787 1.7.8.2 tls */
788 1.7.8.2 tls sc->sc_ports[pn].sc_port_mcr = MCS7840_UART_MCR_DTR
789 1.7.8.2 tls | MCS7840_UART_MCR_RTS | MCS7840_UART_MCR_IE;
790 1.7.8.2 tls if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
791 1.7.8.2 tls sc->sc_ports[pn].sc_port_mcr))
792 1.7.8.2 tls return EIO;
793 1.7.8.2 tls
794 1.7.8.2 tls /* Clearing Bulkin and Bulkout FIFO */
795 1.7.8.2 tls if (umcs7840_get_reg(sc, spreg, &data))
796 1.7.8.2 tls return EIO;
797 1.7.8.2 tls data |= MCS7840_DEV_SPx_RESET_OUT_FIFO | MCS7840_DEV_SPx_RESET_IN_FIFO;
798 1.7.8.2 tls if (umcs7840_set_reg(sc, spreg, data))
799 1.7.8.2 tls return EIO;
800 1.7.8.2 tls data &= ~(MCS7840_DEV_SPx_RESET_OUT_FIFO
801 1.7.8.2 tls | MCS7840_DEV_SPx_RESET_IN_FIFO);
802 1.7.8.2 tls if (umcs7840_set_reg(sc, spreg, data))
803 1.7.8.2 tls return EIO;
804 1.7.8.2 tls
805 1.7.8.2 tls /* Set speed 9600 */
806 1.7.8.3 jdolecek if (umcs7840_set_baudrate(sc, portno, 9600))
807 1.7.8.2 tls return EIO;
808 1.7.8.2 tls
809 1.7.8.2 tls
810 1.7.8.2 tls /* Finally enable all interrupts -- documented */
811 1.7.8.2 tls /*
812 1.7.8.2 tls * Copied from vendor driver, I don't know why we should read LCR
813 1.7.8.2 tls * here
814 1.7.8.2 tls */
815 1.7.8.2 tls if (umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
816 1.7.8.2 tls &sc->sc_ports[pn].sc_port_lcr))
817 1.7.8.2 tls return EIO;
818 1.7.8.2 tls if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER,
819 1.7.8.2 tls MCS7840_UART_IER_RXSTAT | MCS7840_UART_IER_MODEM))
820 1.7.8.2 tls return EIO;
821 1.7.8.2 tls
822 1.7.8.2 tls /* Enable RX */
823 1.7.8.2 tls if (umcs7840_get_reg(sc, ctrlreg, &data))
824 1.7.8.2 tls return EIO;
825 1.7.8.2 tls data &= ~MCS7840_DEV_CONTROLx_RX_DISABLE;
826 1.7.8.2 tls if (umcs7840_set_reg(sc, ctrlreg, data))
827 1.7.8.2 tls return EIO;
828 1.7.8.2 tls return 0;
829 1.7.8.2 tls }
830 1.7.8.2 tls
831 1.7.8.2 tls static void
832 1.7.8.2 tls umcs7840_port_close(void *self, int portno)
833 1.7.8.2 tls {
834 1.7.8.2 tls struct umcs7840_softc *sc = self;
835 1.7.8.2 tls int pn = sc->sc_ports[portno].sc_port_phys;
836 1.7.8.2 tls int ctrlreg = umcs7840_reg_ctrl(pn);
837 1.7.8.2 tls uint8_t data;
838 1.7.8.2 tls
839 1.7.8.2 tls if (sc->sc_dying)
840 1.7.8.2 tls return;
841 1.7.8.2 tls
842 1.7.8.2 tls umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR, 0);
843 1.7.8.2 tls umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0);
844 1.7.8.2 tls
845 1.7.8.2 tls /* Disable RX */
846 1.7.8.2 tls if (umcs7840_get_reg(sc, ctrlreg, &data))
847 1.7.8.2 tls return;
848 1.7.8.2 tls data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
849 1.7.8.2 tls if (umcs7840_set_reg(sc, ctrlreg, data))
850 1.7.8.2 tls return;
851 1.7.8.2 tls }
852 1.7.8.2 tls
853 1.7.8.2 tls static void
854 1.7.8.3 jdolecek umcs7840_intr(struct usbd_xfer *xfer, void *priv,
855 1.7.8.2 tls usbd_status status)
856 1.7.8.2 tls {
857 1.7.8.2 tls struct umcs7840_softc *sc = priv;
858 1.7.8.2 tls u_char *buf = sc->sc_intr_buf;
859 1.7.8.2 tls int actlen;
860 1.7.8.2 tls int subunit;
861 1.7.8.2 tls
862 1.7.8.2 tls if (status == USBD_NOT_STARTED || status == USBD_CANCELLED
863 1.7.8.2 tls || status == USBD_IOERROR)
864 1.7.8.2 tls return;
865 1.7.8.2 tls
866 1.7.8.2 tls if (status != USBD_NORMAL_COMPLETION) {
867 1.7.8.2 tls aprint_error_dev(sc->sc_dev,
868 1.7.8.2 tls "umcs7840_intr: abnormal status: %s\n",
869 1.7.8.2 tls usbd_errstr(status));
870 1.7.8.2 tls usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
871 1.7.8.2 tls return;
872 1.7.8.2 tls }
873 1.7.8.2 tls
874 1.7.8.2 tls usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
875 1.7.8.2 tls if (actlen == 5 || actlen == 13) {
876 1.7.8.2 tls uint32_t change_mask = 0;
877 1.7.8.2 tls /* Check status of all ports */
878 1.7.8.2 tls for (subunit = 0; subunit < sc->sc_numports; subunit++) {
879 1.7.8.2 tls uint8_t pn = sc->sc_ports[subunit].sc_port_phys;
880 1.7.8.2 tls if (buf[pn] & MCS7840_UART_ISR_NOPENDING)
881 1.7.8.2 tls continue;
882 1.7.8.2 tls DPRINTF(("Port %d has pending interrupt: %02x "
883 1.7.8.2 tls "(FIFO: %02x)\n", pn,
884 1.7.8.2 tls buf[pn] & MCS7840_UART_ISR_INTMASK,
885 1.7.8.2 tls buf[pn] & (~MCS7840_UART_ISR_INTMASK)));
886 1.7.8.2 tls switch (buf[pn] & MCS7840_UART_ISR_INTMASK) {
887 1.7.8.2 tls case MCS7840_UART_ISR_RXERR:
888 1.7.8.2 tls case MCS7840_UART_ISR_RXHASDATA:
889 1.7.8.2 tls case MCS7840_UART_ISR_RXTIMEOUT:
890 1.7.8.2 tls case MCS7840_UART_ISR_MSCHANGE:
891 1.7.8.2 tls change_mask |= (1U << subunit);
892 1.7.8.2 tls break;
893 1.7.8.2 tls default:
894 1.7.8.2 tls /* Do nothing */
895 1.7.8.2 tls break;
896 1.7.8.2 tls }
897 1.7.8.2 tls }
898 1.7.8.2 tls
899 1.7.8.2 tls if (change_mask != 0) {
900 1.7.8.2 tls atomic_or_32(&sc->sc_change_mask, change_mask);
901 1.7.8.2 tls usb_add_task(sc->sc_udev, &sc->sc_change_task,
902 1.7.8.2 tls USB_TASKQ_DRIVER);
903 1.7.8.2 tls }
904 1.7.8.2 tls } else {
905 1.7.8.2 tls aprint_error_dev(sc->sc_dev,
906 1.7.8.2 tls "Invalid interrupt data length %d", actlen);
907 1.7.8.2 tls }
908 1.7.8.2 tls }
909 1.7.8.2 tls
910 1.7.8.2 tls static void
911 1.7.8.2 tls umcs7840_change_task(void *arg)
912 1.7.8.2 tls {
913 1.7.8.2 tls struct umcs7840_softc *sc = arg;
914 1.7.8.2 tls uint32_t change_mask;
915 1.7.8.2 tls int i;
916 1.7.8.2 tls
917 1.7.8.2 tls change_mask = atomic_swap_32(&sc->sc_change_mask, 0);
918 1.7.8.2 tls for (i = 0; i < sc->sc_numports; i++) {
919 1.7.8.2 tls if (ISSET(change_mask, (1U << i)))
920 1.7.8.2 tls ucom_status_change(device_private(
921 1.7.8.2 tls sc->sc_ports[i].sc_port_ucom));
922 1.7.8.2 tls }
923 1.7.8.2 tls }
924