umcs.c revision 1.3 1 /* $NetBSD: umcs.c,v 1.3 2014/03/17 19:59:42 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.3 2014/03/17 19:59:42 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 /* detach children */
521 for (i = 0; i < sc->sc_numports; i++) {
522 if (sc->sc_ports[i].sc_port_ucom) {
523 rv = config_detach(sc->sc_ports[i].sc_port_ucom,
524 flags);
525 if (rv)
526 break;
527 }
528 }
529
530 /* close interrupt pipe */
531 if (sc->sc_intr_pipe != NULL) {
532 rv = usbd_abort_pipe(sc->sc_intr_pipe);
533 if (rv)
534 aprint_error_dev(sc->sc_dev,
535 "abort interrupt pipe failed: %s\n",
536 usbd_errstr(rv));
537 rv = usbd_close_pipe(sc->sc_intr_pipe);
538 if (rv)
539 aprint_error_dev(sc->sc_dev,
540 "failed to close interrupt pipe: %s\n",
541 usbd_errstr(rv));
542 kmem_free(sc->sc_intr_buf, sc->sc_intr_buflen);
543 sc->sc_intr_pipe = NULL;
544 }
545 if (sc->sc_change_wq != NULL)
546 workqueue_destroy(sc->sc_change_wq);
547
548 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
549 sc->sc_dev);
550
551 return rv;
552 }
553
554 int
555 umcs7840_activate(device_t self, enum devact act)
556 {
557 struct umcs7840_softc *sc = device_private(self);
558
559 switch (act) {
560 case DVACT_DEACTIVATE:
561 sc->sc_dying = true;
562 return 0;
563 default:
564 return EOPNOTSUPP;
565 }
566 }
567
568 static void
569 umcs7840_childdet(device_t self, device_t child)
570 {
571 struct umcs7840_softc *sc = device_private(self);
572 int i;
573
574 for (i = 0; i < sc->sc_numports; i++) {
575 if (child == sc->sc_ports[i].sc_port_ucom) {
576 sc->sc_ports[i].sc_port_ucom = NULL;
577 return;
578 }
579 }
580 }
581
582 static void
583 umcs7840_get_status(void *self, int portno, u_char *lsr, u_char *msr)
584 {
585 struct umcs7840_softc *sc = self;
586 uint8_t pn = sc->sc_ports[portno].sc_port_phys;
587 uint8_t hw_lsr = 0; /* local line status register */
588 uint8_t hw_msr = 0; /* local modem status register */
589
590 /* Read LSR & MSR */
591 umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LSR, &hw_lsr);
592 umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_MSR, &hw_msr);
593
594 *lsr = hw_lsr;
595 *msr = hw_msr;
596 }
597
598 static void
599 umcs7840_set(void *self, int portno, int reg, int onoff)
600 {
601 struct umcs7840_softc *sc = self;
602 int pn = sc->sc_ports[portno].sc_port_phys;
603
604 if (sc->sc_dying)
605 return;
606
607 switch (reg) {
608 case UCOM_SET_DTR:
609 umcs7840_dtr(sc, pn, onoff);
610 break;
611 case UCOM_SET_RTS:
612 umcs7840_rts(sc, pn, onoff);
613 break;
614 case UCOM_SET_BREAK:
615 umcs7840_break(sc, pn, onoff);
616 break;
617 default:
618 break;
619 }
620 }
621
622 static int
623 umcs7840_param(void *self, int portno, struct termios *t)
624 {
625 struct umcs7840_softc *sc = self;
626 int pn = sc->sc_ports[portno].sc_port_phys;
627 uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
628 uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
629
630 if (t->c_cflag & CSTOPB) {
631 lcr |= MCS7840_UART_LCR_STOPB2;
632 } else {
633 lcr |= MCS7840_UART_LCR_STOPB1;
634 }
635
636 lcr &= ~MCS7840_UART_LCR_PARITYMASK;
637 if (t->c_cflag & PARENB) {
638 lcr |= MCS7840_UART_LCR_PARITYON;
639 if (t->c_cflag & PARODD) {
640 lcr = MCS7840_UART_LCR_PARITYODD;
641 } else {
642 lcr = MCS7840_UART_LCR_PARITYEVEN;
643 }
644 } else {
645 lcr &= ~MCS7840_UART_LCR_PARITYON;
646 }
647
648 lcr &= ~MCS7840_UART_LCR_DATALENMASK;
649 switch (t->c_cflag & CSIZE) {
650 case CS5:
651 lcr |= MCS7840_UART_LCR_DATALEN5;
652 break;
653 case CS6:
654 lcr |= MCS7840_UART_LCR_DATALEN6;
655 break;
656 case CS7:
657 lcr |= MCS7840_UART_LCR_DATALEN7;
658 break;
659 case CS8:
660 lcr |= MCS7840_UART_LCR_DATALEN8;
661 break;
662 }
663
664 if (t->c_cflag & CRTSCTS)
665 mcr |= MCS7840_UART_MCR_CTSRTS;
666 else
667 mcr &= ~MCS7840_UART_MCR_CTSRTS;
668
669 if (t->c_cflag & CLOCAL)
670 mcr &= ~MCS7840_UART_MCR_DTRDSR;
671 else
672 mcr |= MCS7840_UART_MCR_DTRDSR;
673
674 sc->sc_ports[portno].sc_port_lcr = lcr;
675 umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
676 sc->sc_ports[pn].sc_port_lcr);
677
678 sc->sc_ports[portno].sc_port_mcr = mcr;
679 umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
680 sc->sc_ports[pn].sc_port_mcr);
681
682 if (umcs7840_set_baudrate(sc, pn, t->c_ospeed))
683 return EIO;
684
685 return 0;
686 }
687
688 static void
689 umcs7840_dtr(struct umcs7840_softc *sc, int portno, bool onoff)
690 {
691 int pn = sc->sc_ports[portno].sc_port_phys;
692 uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
693
694 if (onoff)
695 mcr |= MCS7840_UART_MCR_DTR;
696 else
697 mcr &= ~MCS7840_UART_MCR_DTR;
698
699 sc->sc_ports[portno].sc_port_mcr = mcr;
700 umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
701 sc->sc_ports[pn].sc_port_mcr);
702 }
703
704 static void
705 umcs7840_rts(struct umcs7840_softc *sc, int portno, bool onoff)
706 {
707 int pn = sc->sc_ports[portno].sc_port_phys;
708 uint8_t mcr = sc->sc_ports[portno].sc_port_mcr;
709
710 if (onoff)
711 mcr |= MCS7840_UART_MCR_RTS;
712 else
713 mcr &= ~MCS7840_UART_MCR_RTS;
714
715 sc->sc_ports[portno].sc_port_mcr = mcr;
716 umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
717 sc->sc_ports[pn].sc_port_mcr);
718 }
719
720 static void
721 umcs7840_break(struct umcs7840_softc *sc, int portno, bool onoff)
722 {
723 int pn = sc->sc_ports[portno].sc_port_phys;
724 uint8_t lcr = sc->sc_ports[portno].sc_port_lcr;
725
726 if (onoff)
727 lcr |= MCS7840_UART_LCR_BREAK;
728 else
729 lcr &= ~MCS7840_UART_LCR_BREAK;
730
731 sc->sc_ports[portno].sc_port_lcr = lcr;
732 umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
733 sc->sc_ports[pn].sc_port_lcr);
734 }
735
736 static int
737 umcs7840_port_open(void *self, int portno)
738 {
739 struct umcs7840_softc *sc = self;
740 int pn = sc->sc_ports[portno].sc_port_phys;
741 int spreg = umcs7840_reg_sp(pn);
742 int ctrlreg = umcs7840_reg_ctrl(pn);
743 uint8_t data;
744
745 if (sc->sc_dying)
746 return EIO;
747
748 /* If it very first open, finish global configuration */
749 if (!sc->sc_init_done) {
750 if (umcs7840_get_reg(sc, MCS7840_DEV_REG_CONTROL1, &data))
751 return EIO;
752 data |= MCS7840_DEV_CONTROL1_DRIVER_DONE;
753 if (umcs7840_set_reg(sc, MCS7840_DEV_REG_CONTROL1, data))
754 return EIO;
755 sc->sc_init_done = 1;
756 }
757
758 /* Toggle reset bit on-off */
759 if (umcs7840_get_reg(sc, spreg, &data))
760 return EIO;
761 data |= MCS7840_DEV_SPx_UART_RESET;
762 if (umcs7840_set_reg(sc, spreg, data))
763 return EIO;
764 data &= ~MCS7840_DEV_SPx_UART_RESET;
765 if (umcs7840_set_reg(sc, spreg, data))
766 return EIO;
767
768 /* Set RS-232 mode */
769 if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_SCRATCHPAD,
770 MCS7840_UART_SCRATCHPAD_RS232))
771 return EIO;
772
773 /* Disable RX on time of initialization */
774 if (umcs7840_get_reg(sc, ctrlreg, &data))
775 return EIO;
776 data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
777 if (umcs7840_set_reg(sc, ctrlreg, data))
778 return EIO;
779
780 /* Disable all interrupts */
781 if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0))
782 return EIO;
783
784 /* Reset FIFO -- documented */
785 if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR, 0))
786 return EIO;
787 if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_FCR,
788 MCS7840_UART_FCR_ENABLE | MCS7840_UART_FCR_FLUSHRHR |
789 MCS7840_UART_FCR_FLUSHTHR | MCS7840_UART_FCR_RTL_1_14))
790 return EIO;
791
792 /* Set 8 bit, no parity, 1 stop bit -- documented */
793 sc->sc_ports[pn].sc_port_lcr =
794 MCS7840_UART_LCR_DATALEN8 | MCS7840_UART_LCR_STOPB1;
795 if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
796 sc->sc_ports[pn].sc_port_lcr))
797 return EIO;
798
799 /*
800 * Enable DTR/RTS on modem control, enable modem interrupts --
801 * documented
802 */
803 sc->sc_ports[pn].sc_port_mcr = MCS7840_UART_MCR_DTR
804 | MCS7840_UART_MCR_RTS | MCS7840_UART_MCR_IE;
805 if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR,
806 sc->sc_ports[pn].sc_port_mcr))
807 return EIO;
808
809 /* Clearing Bulkin and Bulkout FIFO */
810 if (umcs7840_get_reg(sc, spreg, &data))
811 return EIO;
812 data |= MCS7840_DEV_SPx_RESET_OUT_FIFO | MCS7840_DEV_SPx_RESET_IN_FIFO;
813 if (umcs7840_set_reg(sc, spreg, data))
814 return EIO;
815 data &= ~(MCS7840_DEV_SPx_RESET_OUT_FIFO
816 | MCS7840_DEV_SPx_RESET_IN_FIFO);
817 if (umcs7840_set_reg(sc, spreg, data))
818 return EIO;
819
820 /* Set speed 9600 */
821 if (umcs7840_set_baudrate(sc, pn, 9600))
822 return EIO;
823
824
825 /* Finally enable all interrupts -- documented */
826 /*
827 * Copied from vendor driver, I don't know why we should read LCR
828 * here
829 */
830 if (umcs7840_get_UART_reg(sc, pn, MCS7840_UART_REG_LCR,
831 &sc->sc_ports[pn].sc_port_lcr))
832 return EIO;
833 if (umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER,
834 MCS7840_UART_IER_RXSTAT | MCS7840_UART_IER_MODEM))
835 return EIO;
836
837 /* Enable RX */
838 if (umcs7840_get_reg(sc, ctrlreg, &data))
839 return EIO;
840 data &= ~MCS7840_DEV_CONTROLx_RX_DISABLE;
841 if (umcs7840_set_reg(sc, ctrlreg, data))
842 return EIO;
843 return 0;
844 }
845
846 static void
847 umcs7840_port_close(void *self, int portno)
848 {
849 struct umcs7840_softc *sc = self;
850 int pn = sc->sc_ports[portno].sc_port_phys;
851 int ctrlreg = umcs7840_reg_ctrl(pn);
852 uint8_t data;
853
854 atomic_swap_32(&sc->sc_ports[portno].sc_port_changed, 0);
855
856 umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_MCR, 0);
857 umcs7840_set_UART_reg(sc, pn, MCS7840_UART_REG_IER, 0);
858
859 /* Disable RX */
860 if (umcs7840_get_reg(sc, ctrlreg, &data))
861 return;
862 data |= MCS7840_DEV_CONTROLx_RX_DISABLE;
863 if (umcs7840_set_reg(sc, ctrlreg, data))
864 return;
865 }
866
867 static void
868 umcs7840_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
869 usbd_status status)
870 {
871 struct umcs7840_softc *sc = priv;
872 u_char *buf = sc->sc_intr_buf;
873 int actlen;
874 int subunit, found;
875
876 if (sc->sc_dying)
877 return;
878
879 found = 0;
880 if (status != USBD_NORMAL_COMPLETION) {
881 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
882 return;
883
884 aprint_error_dev(sc->sc_dev,
885 "umcs7840_intr: abnormal status: %s\n",
886 usbd_errstr(status));
887 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
888 return;
889 }
890
891 usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
892 if (actlen == 5 || actlen == 13) {
893 /* Check status of all ports */
894 for (subunit = 0; subunit < sc->sc_numports; subunit++) {
895 uint8_t pn = sc->sc_ports[subunit].sc_port_phys;
896 if (buf[pn] & MCS7840_UART_ISR_NOPENDING)
897 continue;
898 DPRINTF(("Port %d has pending interrupt: %02x "
899 "(FIFO: %02x)\n", pn,
900 buf[pn] & MCS7840_UART_ISR_INTMASK,
901 buf[pn] & (~MCS7840_UART_ISR_INTMASK)));
902 switch (buf[pn] & MCS7840_UART_ISR_INTMASK) {
903 case MCS7840_UART_ISR_RXERR:
904 case MCS7840_UART_ISR_RXHASDATA:
905 case MCS7840_UART_ISR_RXTIMEOUT:
906 case MCS7840_UART_ISR_MSCHANGE:
907 atomic_swap_32(
908 &sc->sc_ports[subunit].sc_port_changed,
909 1);
910 found++;
911 break;
912 default:
913 /* Do nothing */
914 break;
915 }
916 }
917 if (found)
918 workqueue_enqueue(sc->sc_change_wq, &sc->sc_work,
919 NULL);
920 } else {
921 aprint_error_dev(sc->sc_dev,
922 "Invalid interrupt data length %d", actlen);
923 }
924 }
925
926 static void
927 umcs7840_change_worker(struct work *w, void *arg)
928 {
929 struct umcs7840_softc *sc = arg;
930 int i;
931
932 for (i = 0; i < sc->sc_numports; i++) {
933 if (sc->sc_ports[i].sc_port_changed) {
934 ucom_status_change(device_private(
935 sc->sc_ports[i].sc_port_ucom));
936 atomic_swap_32(&sc->sc_ports[i].sc_port_changed, 0);
937 }
938 }
939 }
940