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