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