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