Deleted Added
full compact
umoscom.c (185950) umoscom.c (187176)
1/* $FreeBSD: head/sys/dev/usb2/serial/umoscom2.c 185950 2008-12-11 23:17:48Z thompsa $ */
1/* $FreeBSD: head/sys/dev/usb2/serial/umoscom2.c 187176 2009-01-13 19:03:47Z thompsa $ */
2/* $OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $ */
3
4/*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <dev/usb2/include/usb2_devid.h>
21#include <dev/usb2/include/usb2_standard.h>
22#include <dev/usb2/include/usb2_mfunc.h>
23#include <dev/usb2/include/usb2_error.h>
24#include <dev/usb2/include/usb2_cdc.h>
25
26#define USB_DEBUG_VAR umoscom_debug
27
28#include <dev/usb2/core/usb2_core.h>
29#include <dev/usb2/core/usb2_debug.h>
30#include <dev/usb2/core/usb2_process.h>
2/* $OpenBSD: umoscom.c,v 1.2 2006/10/26 06:02:43 jsg Exp $ */
3
4/*
5 * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <dev/usb2/include/usb2_devid.h>
21#include <dev/usb2/include/usb2_standard.h>
22#include <dev/usb2/include/usb2_mfunc.h>
23#include <dev/usb2/include/usb2_error.h>
24#include <dev/usb2/include/usb2_cdc.h>
25
26#define USB_DEBUG_VAR umoscom_debug
27
28#include <dev/usb2/core/usb2_core.h>
29#include <dev/usb2/core/usb2_debug.h>
30#include <dev/usb2/core/usb2_process.h>
31#include <dev/usb2/core/usb2_config_td.h>
32#include <dev/usb2/core/usb2_request.h>
33#include <dev/usb2/core/usb2_lookup.h>
34#include <dev/usb2/core/usb2_util.h>
35#include <dev/usb2/core/usb2_busdma.h>
36
37#include <dev/usb2/serial/usb2_serial.h>
38
39#if USB_DEBUG
40static int umoscom_debug = 0;
41
42SYSCTL_NODE(_hw_usb2, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
43SYSCTL_INT(_hw_usb2_umoscom, OID_AUTO, debug, CTLFLAG_RW,
44 &umoscom_debug, 0, "Debug level");
45#endif
46
47#define UMOSCOM_BUFSIZE 1024 /* bytes */
48#define UMOSCOM_N_DATA_TRANSFER 6 /* units */
49
50#define UMOSCOM_CONFIG_INDEX 0
51#define UMOSCOM_IFACE_INDEX 0
52
53/* interrupt packet */
54#define UMOSCOM_IIR_RLS 0x06
55#define UMOSCOM_IIR_RDA 0x04
56#define UMOSCOM_IIR_CTI 0x0c
57#define UMOSCOM_IIR_THR 0x02
58#define UMOSCOM_IIR_MS 0x00
59
60/* registers */
61#define UMOSCOM_READ 0x0d
62#define UMOSCOM_WRITE 0x0e
63#define UMOSCOM_UART_REG 0x0300
64#define UMOSCOM_VEND_REG 0x0000
65
66#define UMOSCOM_TXBUF 0x00 /* Write */
67#define UMOSCOM_RXBUF 0x00 /* Read */
68#define UMOSCOM_INT 0x01
69#define UMOSCOM_FIFO 0x02 /* Write */
70#define UMOSCOM_ISR 0x02 /* Read */
71#define UMOSCOM_LCR 0x03
72#define UMOSCOM_MCR 0x04
73#define UMOSCOM_LSR 0x05
74#define UMOSCOM_MSR 0x06
75#define UMOSCOM_SCRATCH 0x07
76#define UMOSCOM_DIV_LO 0x08
77#define UMOSCOM_DIV_HI 0x09
78#define UMOSCOM_EFR 0x0a
79#define UMOSCOM_XON1 0x0b
80#define UMOSCOM_XON2 0x0c
81#define UMOSCOM_XOFF1 0x0d
82#define UMOSCOM_XOFF2 0x0e
83
84#define UMOSCOM_BAUDLO 0x00
85#define UMOSCOM_BAUDHI 0x01
86
87#define UMOSCOM_INT_RXEN 0x01
88#define UMOSCOM_INT_TXEN 0x02
89#define UMOSCOM_INT_RSEN 0x04
90#define UMOSCOM_INT_MDMEM 0x08
91#define UMOSCOM_INT_SLEEP 0x10
92#define UMOSCOM_INT_XOFF 0x20
93#define UMOSCOM_INT_RTS 0x40
94
95#define UMOSCOM_FIFO_EN 0x01
96#define UMOSCOM_FIFO_RXCLR 0x02
97#define UMOSCOM_FIFO_TXCLR 0x04
98#define UMOSCOM_FIFO_DMA_BLK 0x08
99#define UMOSCOM_FIFO_TXLVL_MASK 0x30
100#define UMOSCOM_FIFO_TXLVL_8 0x00
101#define UMOSCOM_FIFO_TXLVL_16 0x10
102#define UMOSCOM_FIFO_TXLVL_32 0x20
103#define UMOSCOM_FIFO_TXLVL_56 0x30
104#define UMOSCOM_FIFO_RXLVL_MASK 0xc0
105#define UMOSCOM_FIFO_RXLVL_8 0x00
106#define UMOSCOM_FIFO_RXLVL_16 0x40
107#define UMOSCOM_FIFO_RXLVL_56 0x80
108#define UMOSCOM_FIFO_RXLVL_80 0xc0
109
110#define UMOSCOM_ISR_MDM 0x00
111#define UMOSCOM_ISR_NONE 0x01
112#define UMOSCOM_ISR_TX 0x02
113#define UMOSCOM_ISR_RX 0x04
114#define UMOSCOM_ISR_LINE 0x06
115#define UMOSCOM_ISR_RXTIMEOUT 0x0c
116#define UMOSCOM_ISR_RX_XOFF 0x10
117#define UMOSCOM_ISR_RTSCTS 0x20
118#define UMOSCOM_ISR_FIFOEN 0xc0
119
120#define UMOSCOM_LCR_DBITS(x) ((x) - 5)
121#define UMOSCOM_LCR_STOP_BITS_1 0x00
122#define UMOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
123#define UMOSCOM_LCR_PARITY_NONE 0x00
124#define UMOSCOM_LCR_PARITY_ODD 0x08
125#define UMOSCOM_LCR_PARITY_EVEN 0x18
126#define UMOSCOM_LCR_BREAK 0x40
127#define UMOSCOM_LCR_DIVLATCH_EN 0x80
128
129#define UMOSCOM_MCR_DTR 0x01
130#define UMOSCOM_MCR_RTS 0x02
131#define UMOSCOM_MCR_LOOP 0x04
132#define UMOSCOM_MCR_INTEN 0x08
133#define UMOSCOM_MCR_LOOPBACK 0x10
134#define UMOSCOM_MCR_XONANY 0x20
135#define UMOSCOM_MCR_IRDA_EN 0x40
136#define UMOSCOM_MCR_BAUD_DIV4 0x80
137
138#define UMOSCOM_LSR_RXDATA 0x01
139#define UMOSCOM_LSR_RXOVER 0x02
140#define UMOSCOM_LSR_RXPAR_ERR 0x04
141#define UMOSCOM_LSR_RXFRM_ERR 0x08
142#define UMOSCOM_LSR_RXBREAK 0x10
143#define UMOSCOM_LSR_TXEMPTY 0x20
144#define UMOSCOM_LSR_TXALLEMPTY 0x40
145#define UMOSCOM_LSR_TXFIFO_ERR 0x80
146
147#define UMOSCOM_MSR_CTS_CHG 0x01
148#define UMOSCOM_MSR_DSR_CHG 0x02
149#define UMOSCOM_MSR_RI_CHG 0x04
150#define UMOSCOM_MSR_CD_CHG 0x08
151#define UMOSCOM_MSR_CTS 0x10
152#define UMOSCOM_MSR_RTS 0x20
153#define UMOSCOM_MSR_RI 0x40
154#define UMOSCOM_MSR_CD 0x80
155
156#define UMOSCOM_BAUD_REF 115200
157
158struct umoscom_softc {
159 struct usb2_com_super_softc sc_super_ucom;
160 struct usb2_com_softc sc_ucom;
161
162 struct usb2_xfer *sc_xfer_data[UMOSCOM_N_DATA_TRANSFER];
163 struct usb2_device *sc_udev;
164
165 uint8_t sc_mcr;
166 uint8_t sc_lcr;
167 uint8_t sc_flags;
168#define UMOSCOM_FLAG_READ_STALL 0x01
169#define UMOSCOM_FLAG_WRITE_STALL 0x02
170#define UMOSCOM_FLAG_INTR_STALL 0x04
171};
172
173/* prototypes */
174
175static device_probe_t umoscom_probe;
176static device_attach_t umoscom_attach;
177static device_detach_t umoscom_detach;
178
179static usb2_callback_t umoscom_write_callback;
180static usb2_callback_t umoscom_write_clear_stall_callback;
181static usb2_callback_t umoscom_read_callback;
182static usb2_callback_t umoscom_read_clear_stall_callback;
183static usb2_callback_t umoscom_intr_callback;
184static usb2_callback_t umoscom_intr_clear_stall_callback;
185
186static void umoscom_cfg_open(struct usb2_com_softc *);
187static void umoscom_cfg_close(struct usb2_com_softc *);
188static void umoscom_cfg_set_break(struct usb2_com_softc *, uint8_t);
189static void umoscom_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
190static void umoscom_cfg_set_rts(struct usb2_com_softc *, uint8_t);
191static int umoscom_pre_param(struct usb2_com_softc *, struct termios *);
192static void umoscom_cfg_param(struct usb2_com_softc *, struct termios *);
193static void umoscom_cfg_get_status(struct usb2_com_softc *, uint8_t *,
194 uint8_t *);
195static void umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
196static uint8_t umoscom_cfg_read(struct umoscom_softc *, uint16_t);
197static void umoscom_cfg_do_request(struct umoscom_softc *,
198 struct usb2_device_request *, void *);
199static void umoscom_start_read(struct usb2_com_softc *);
200static void umoscom_stop_read(struct usb2_com_softc *);
201static void umoscom_start_write(struct usb2_com_softc *);
202static void umoscom_stop_write(struct usb2_com_softc *);
203
204static const struct usb2_config umoscom_config_data[UMOSCOM_N_DATA_TRANSFER] = {
205
206 [0] = {
207 .type = UE_BULK,
208 .endpoint = UE_ADDR_ANY,
209 .direction = UE_DIR_OUT,
210 .mh.bufsize = UMOSCOM_BUFSIZE,
211 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
212 .mh.callback = &umoscom_write_callback,
213 },
214
215 [1] = {
216 .type = UE_BULK,
217 .endpoint = UE_ADDR_ANY,
218 .direction = UE_DIR_IN,
219 .mh.bufsize = UMOSCOM_BUFSIZE,
220 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
221 .mh.callback = &umoscom_read_callback,
222 },
223
224 [2] = {
225 .type = UE_CONTROL,
226 .endpoint = 0x00, /* Control pipe */
227 .direction = UE_DIR_ANY,
228 .mh.bufsize = sizeof(struct usb2_device_request),
229 .mh.callback = &umoscom_write_clear_stall_callback,
230 .mh.timeout = 1000, /* 1 second */
231 .mh.interval = 50, /* 50ms */
232 },
233
234 [3] = {
235 .type = UE_CONTROL,
236 .endpoint = 0x00, /* Control pipe */
237 .direction = UE_DIR_ANY,
238 .mh.bufsize = sizeof(struct usb2_device_request),
239 .mh.callback = &umoscom_read_clear_stall_callback,
240 .mh.timeout = 1000, /* 1 second */
241 .mh.interval = 50, /* 50ms */
242 },
243
244 [4] = {
245 .type = UE_INTERRUPT,
246 .endpoint = UE_ADDR_ANY,
247 .direction = UE_DIR_IN,
248 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
249 .mh.bufsize = 0, /* use wMaxPacketSize */
250 .mh.callback = &umoscom_intr_callback,
251 },
252
253 [5] = {
254 .type = UE_CONTROL,
255 .endpoint = 0x00, /* Control pipe */
256 .direction = UE_DIR_ANY,
257 .mh.bufsize = sizeof(struct usb2_device_request),
258 .mh.callback = &umoscom_intr_clear_stall_callback,
259 .mh.timeout = 1000, /* 1 second */
260 .mh.interval = 50, /* 50ms */
261 },
262};
263
264static const struct usb2_com_callback umoscom_callback = {
265 /* configuration callbacks */
266 .usb2_com_cfg_get_status = &umoscom_cfg_get_status,
267 .usb2_com_cfg_set_dtr = &umoscom_cfg_set_dtr,
268 .usb2_com_cfg_set_rts = &umoscom_cfg_set_rts,
269 .usb2_com_cfg_set_break = &umoscom_cfg_set_break,
270 .usb2_com_cfg_param = &umoscom_cfg_param,
271 .usb2_com_cfg_open = &umoscom_cfg_open,
272 .usb2_com_cfg_close = &umoscom_cfg_close,
273
274 /* other callbacks */
275 .usb2_com_pre_param = &umoscom_pre_param,
276 .usb2_com_start_read = &umoscom_start_read,
277 .usb2_com_stop_read = &umoscom_stop_read,
278 .usb2_com_start_write = &umoscom_start_write,
279 .usb2_com_stop_write = &umoscom_stop_write,
280};
281
282static device_method_t umoscom_methods[] = {
283 DEVMETHOD(device_probe, umoscom_probe),
284 DEVMETHOD(device_attach, umoscom_attach),
285 DEVMETHOD(device_detach, umoscom_detach),
286 {0, 0}
287};
288
289static devclass_t umoscom_devclass;
290
291static driver_t umoscom_driver = {
292 .name = "umoscom",
293 .methods = umoscom_methods,
294 .size = sizeof(struct umoscom_softc),
295};
296
297DRIVER_MODULE(umoscom, ushub, umoscom_driver, umoscom_devclass, NULL, 0);
298MODULE_DEPEND(umoscom, usb2_serial, 1, 1, 1);
299MODULE_DEPEND(umoscom, usb2_core, 1, 1, 1);
300
301static const struct usb2_device_id umoscom_devs[] = {
302 {USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
303};
304
305static int
306umoscom_probe(device_t dev)
307{
308 struct usb2_attach_arg *uaa = device_get_ivars(dev);
309
310 if (uaa->usb2_mode != USB_MODE_HOST) {
311 return (ENXIO);
312 }
313 if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
314 return (ENXIO);
315 }
316 if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
317 return (ENXIO);
318 }
319 return (usb2_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
320}
321
322static int
323umoscom_attach(device_t dev)
324{
325 struct usb2_attach_arg *uaa = device_get_ivars(dev);
326 struct umoscom_softc *sc = device_get_softc(dev);
327 int error;
328 uint8_t iface_index;
329
330 if (sc == NULL) {
331 return (ENOMEM);
332 }
333 sc->sc_udev = uaa->device;
334 sc->sc_mcr = 0x08; /* enable interrupts */
335
336 /* XXX the device doesn't provide any ID string, so set a static one */
337 device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
338 device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
339
340 iface_index = UMOSCOM_IFACE_INDEX;
341 error = usb2_transfer_setup(uaa->device, &iface_index,
342 sc->sc_xfer_data, umoscom_config_data,
343 UMOSCOM_N_DATA_TRANSFER, sc, &Giant);
344
345 if (error) {
346 goto detach;
347 }
348 /* clear stall at first run */
349 sc->sc_flags |= (UMOSCOM_FLAG_READ_STALL |
350 UMOSCOM_FLAG_WRITE_STALL);
351
352 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
353 &umoscom_callback, &Giant);
354 if (error) {
355 goto detach;
356 }
357 return (0);
358
359detach:
360 device_printf(dev, "attach error: %s\n", usb2_errstr(error));
361 umoscom_detach(dev);
362 return (ENXIO);
363}
364
365static int
366umoscom_detach(device_t dev)
367{
368 struct umoscom_softc *sc = device_get_softc(dev);
369
370 mtx_lock(&Giant);
371
372 mtx_unlock(&Giant);
373
374 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
375
376 usb2_transfer_unsetup(sc->sc_xfer_data, UMOSCOM_N_DATA_TRANSFER);
377
378 return (0);
379}
380
381static void
382umoscom_cfg_open(struct usb2_com_softc *ucom)
383{
384 struct umoscom_softc *sc = ucom->sc_parent;
385
386 DPRINTF("\n");
387
388 /* Purge FIFOs or odd things happen */
389 umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
390
391 /* Enable FIFO */
392 umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
393 UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
394 UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
395 UMOSCOM_UART_REG);
396
397 /* Enable Interrupt Registers */
398 umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
399
400 /* Magic */
401 umoscom_cfg_write(sc, 0x01, 0x08);
402
403 /* Magic */
404 umoscom_cfg_write(sc, 0x00, 0x02);
405}
406
407static void
408umoscom_cfg_close(struct usb2_com_softc *ucom)
409{
410 return;
411}
412
413static void
414umoscom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
415{
416 struct umoscom_softc *sc = ucom->sc_parent;
417 uint16_t val;
418
419 val = sc->sc_lcr;
420 if (onoff)
421 val |= UMOSCOM_LCR_BREAK;
422
423 umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
424}
425
426static void
427umoscom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
428{
429 struct umoscom_softc *sc = ucom->sc_parent;
430
431 if (onoff)
432 sc->sc_mcr |= UMOSCOM_MCR_DTR;
433 else
434 sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
435
436 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
437}
438
439static void
440umoscom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
441{
442 struct umoscom_softc *sc = ucom->sc_parent;
443
444 if (onoff)
445 sc->sc_mcr |= UMOSCOM_MCR_RTS;
446 else
447 sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
448
449 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
450}
451
452static int
453umoscom_pre_param(struct usb2_com_softc *ucom, struct termios *t)
454{
455 if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
456 return (EINVAL);
457
458 return (0);
459}
460
461static void
462umoscom_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
463{
464 struct umoscom_softc *sc = ucom->sc_parent;
465 uint16_t data;
466
467 DPRINTF("speed=%d\n", t->c_ospeed);
468
469 data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
470
471 if (data == 0) {
472 DPRINTF("invalid baud rate!\n");
473 return;
474 }
475 umoscom_cfg_write(sc, UMOSCOM_LCR,
476 UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
477
478 umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
479 (data & 0xFF) | UMOSCOM_UART_REG);
480
481 umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
482 ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
483
484 if (t->c_cflag & CSTOPB)
485 data = UMOSCOM_LCR_STOP_BITS_2;
486 else
487 data = UMOSCOM_LCR_STOP_BITS_1;
488
489 if (t->c_cflag & PARENB) {
490 if (t->c_cflag & PARODD)
491 data |= UMOSCOM_LCR_PARITY_ODD;
492 else
493 data |= UMOSCOM_LCR_PARITY_EVEN;
494 } else
495 data |= UMOSCOM_LCR_PARITY_NONE;
496
497 switch (t->c_cflag & CSIZE) {
498 case CS5:
499 data |= UMOSCOM_LCR_DBITS(5);
500 break;
501 case CS6:
502 data |= UMOSCOM_LCR_DBITS(6);
503 break;
504 case CS7:
505 data |= UMOSCOM_LCR_DBITS(7);
506 break;
507 case CS8:
508 data |= UMOSCOM_LCR_DBITS(8);
509 break;
510 }
511
512 sc->sc_lcr = data;
513 umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
514}
515
516static void
517umoscom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
518{
519 struct umoscom_softc *sc = ucom->sc_parent;
520 uint8_t lsr;
521 uint8_t msr;
522
523 DPRINTFN(5, "\n");
524
525 /* read status registers */
526
527 lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
528 msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
529
530 /* translate bits */
531
532 if (msr & UMOSCOM_MSR_CTS)
533 *p_msr |= SER_CTS;
534
535 if (msr & UMOSCOM_MSR_CD)
536 *p_msr |= SER_DCD;
537
538 if (msr & UMOSCOM_MSR_RI)
539 *p_msr |= SER_RI;
540
541 if (msr & UMOSCOM_MSR_RTS)
542 *p_msr |= SER_DSR;
543}
544
545static void
546umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
547{
548 struct usb2_device_request req;
549
550 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
551 req.bRequest = UMOSCOM_WRITE;
552 USETW(req.wValue, val);
553 USETW(req.wIndex, reg);
554 USETW(req.wLength, 0);
555
556 umoscom_cfg_do_request(sc, &req, NULL);
557}
558
559static uint8_t
560umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
561{
562 struct usb2_device_request req;
563 uint8_t val;
564
565 req.bmRequestType = UT_READ_VENDOR_DEVICE;
566 req.bRequest = UMOSCOM_READ;
567 USETW(req.wValue, 0);
568 USETW(req.wIndex, reg);
569 USETW(req.wLength, 1);
570
571 umoscom_cfg_do_request(sc, &req, &val);
572
573 DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
574
575 return (val);
576}
577
578static void
579umoscom_cfg_do_request(struct umoscom_softc *sc, struct usb2_device_request *req,
580 void *data)
581{
582 uint16_t length;
583 usb2_error_t err;
584
585 if (usb2_com_cfg_is_gone(&sc->sc_ucom))
586 goto error;
587
588 err = usb2_do_request_flags
589 (sc->sc_udev, &Giant, req, data, 0, NULL, 1000);
590
591 if (err) {
592 DPRINTFN(0, "control request failed: %s\n",
593 usb2_errstr(err));
594error:
595 length = UGETW(req->wLength);
596
597 if ((req->bmRequestType & UT_READ) && length) {
598 bzero(data, length);
599 }
600 }
601}
602
603static void
604umoscom_start_read(struct usb2_com_softc *ucom)
605{
606 struct umoscom_softc *sc = ucom->sc_parent;
607
608#if 0
609 /* start interrupt endpoint */
610 usb2_transfer_start(sc->sc_xfer_data[4]);
611#endif
612 /* start read endpoint */
613 usb2_transfer_start(sc->sc_xfer_data[1]);
614}
615
616static void
617umoscom_stop_read(struct usb2_com_softc *ucom)
618{
619 struct umoscom_softc *sc = ucom->sc_parent;
620
621 /* stop interrupt transfer */
622 usb2_transfer_stop(sc->sc_xfer_data[5]);
623 usb2_transfer_stop(sc->sc_xfer_data[4]);
624
625 /* stop read endpoint */
626 usb2_transfer_stop(sc->sc_xfer_data[3]);
627 usb2_transfer_stop(sc->sc_xfer_data[1]);
628}
629
630static void
631umoscom_start_write(struct usb2_com_softc *ucom)
632{
633 struct umoscom_softc *sc = ucom->sc_parent;
634
635 usb2_transfer_start(sc->sc_xfer_data[0]);
636}
637
638static void
639umoscom_stop_write(struct usb2_com_softc *ucom)
640{
641 struct umoscom_softc *sc = ucom->sc_parent;
642
643 usb2_transfer_stop(sc->sc_xfer_data[2]);
644 usb2_transfer_stop(sc->sc_xfer_data[0]);
645}
646
647static void
648umoscom_write_callback(struct usb2_xfer *xfer)
649{
650 struct umoscom_softc *sc = xfer->priv_sc;
651 uint32_t actlen;
652
653 switch (USB_GET_STATE(xfer)) {
654 case USB_ST_SETUP:
655 case USB_ST_TRANSFERRED:
656 DPRINTF("\n");
657
658 if (sc->sc_flags & UMOSCOM_FLAG_WRITE_STALL) {
659 usb2_transfer_start(sc->sc_xfer_data[2]);
660 return;
661 }
662 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
663 UMOSCOM_BUFSIZE, &actlen)) {
664
665 xfer->frlengths[0] = actlen;
666 usb2_start_hardware(xfer);
667 }
668 return;
669
670 default: /* Error */
671 if (xfer->error != USB_ERR_CANCELLED) {
672 DPRINTFN(0, "transfer failed\n");
673 sc->sc_flags |= UMOSCOM_FLAG_WRITE_STALL;
674 usb2_transfer_start(sc->sc_xfer_data[2]);
675 }
676 return;
677 }
678}
679
680static void
681umoscom_write_clear_stall_callback(struct usb2_xfer *xfer)
682{
683 struct umoscom_softc *sc = xfer->priv_sc;
684 struct usb2_xfer *xfer_other = sc->sc_xfer_data[0];
685
686 if (usb2_clear_stall_callback(xfer, xfer_other)) {
687 DPRINTF("stall cleared\n");
688 sc->sc_flags &= ~UMOSCOM_FLAG_WRITE_STALL;
689 usb2_transfer_start(xfer_other);
690 }
691}
692
693static void
694umoscom_read_callback(struct usb2_xfer *xfer)
695{
696 struct umoscom_softc *sc = xfer->priv_sc;
697
698 switch (USB_GET_STATE(xfer)) {
699 case USB_ST_TRANSFERRED:
700 DPRINTF("got %d bytes\n", xfer->actlen);
701 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
702
703 case USB_ST_SETUP:
704 DPRINTF("\n");
705
706 if (sc->sc_flags & UMOSCOM_FLAG_READ_STALL) {
707 usb2_transfer_start(sc->sc_xfer_data[3]);
708 } else {
709 xfer->frlengths[0] = xfer->max_data_length;
710 usb2_start_hardware(xfer);
711 }
712 return;
713
714 default: /* Error */
715 if (xfer->error != USB_ERR_CANCELLED) {
716 DPRINTFN(0, "transfer failed\n");
717 sc->sc_flags |= UMOSCOM_FLAG_READ_STALL;
718 usb2_transfer_start(sc->sc_xfer_data[3]);
719 }
720 return;
721
722 }
723}
724
725static void
726umoscom_read_clear_stall_callback(struct usb2_xfer *xfer)
727{
728 struct umoscom_softc *sc = xfer->priv_sc;
729 struct usb2_xfer *xfer_other = sc->sc_xfer_data[1];
730
731 if (usb2_clear_stall_callback(xfer, xfer_other)) {
732 DPRINTF("stall cleared\n");
733 sc->sc_flags &= ~UMOSCOM_FLAG_READ_STALL;
734 usb2_transfer_start(xfer_other);
735 }
736}
737
738static void
739umoscom_intr_callback(struct usb2_xfer *xfer)
740{
741 struct umoscom_softc *sc = xfer->priv_sc;
742
743 switch (USB_GET_STATE(xfer)) {
744 case USB_ST_TRANSFERRED:
745 if (xfer->actlen < 2) {
746 DPRINTF("too short message\n");
747 goto tr_setup;
748 }
749 usb2_com_status_change(&sc->sc_ucom);
750
751 case USB_ST_SETUP:
752tr_setup:
753 if (sc->sc_flags & UMOSCOM_FLAG_INTR_STALL) {
754 usb2_transfer_start(sc->sc_xfer_data[5]);
755 } else {
756 xfer->frlengths[0] = xfer->max_data_length;
757 usb2_start_hardware(xfer);
758 }
759 return;
760
761 default: /* Error */
762 if (xfer->error != USB_ERR_CANCELLED) {
763 DPRINTFN(0, "transfer failed\n");
764 sc->sc_flags |= UMOSCOM_FLAG_INTR_STALL;
765 usb2_transfer_start(sc->sc_xfer_data[5]);
766 }
767 return;
768 }
769}
770
771static void
772umoscom_intr_clear_stall_callback(struct usb2_xfer *xfer)
773{
774 struct umoscom_softc *sc = xfer->priv_sc;
775 struct usb2_xfer *xfer_other = sc->sc_xfer_data[4];
776
777 if (usb2_clear_stall_callback(xfer, xfer_other)) {
778 DPRINTF("stall cleared\n");
779 sc->sc_flags &= ~UMOSCOM_FLAG_INTR_STALL;
780 usb2_transfer_start(xfer_other);
781 }
782}
31#include <dev/usb2/core/usb2_request.h>
32#include <dev/usb2/core/usb2_lookup.h>
33#include <dev/usb2/core/usb2_util.h>
34#include <dev/usb2/core/usb2_busdma.h>
35
36#include <dev/usb2/serial/usb2_serial.h>
37
38#if USB_DEBUG
39static int umoscom_debug = 0;
40
41SYSCTL_NODE(_hw_usb2, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
42SYSCTL_INT(_hw_usb2_umoscom, OID_AUTO, debug, CTLFLAG_RW,
43 &umoscom_debug, 0, "Debug level");
44#endif
45
46#define UMOSCOM_BUFSIZE 1024 /* bytes */
47#define UMOSCOM_N_DATA_TRANSFER 6 /* units */
48
49#define UMOSCOM_CONFIG_INDEX 0
50#define UMOSCOM_IFACE_INDEX 0
51
52/* interrupt packet */
53#define UMOSCOM_IIR_RLS 0x06
54#define UMOSCOM_IIR_RDA 0x04
55#define UMOSCOM_IIR_CTI 0x0c
56#define UMOSCOM_IIR_THR 0x02
57#define UMOSCOM_IIR_MS 0x00
58
59/* registers */
60#define UMOSCOM_READ 0x0d
61#define UMOSCOM_WRITE 0x0e
62#define UMOSCOM_UART_REG 0x0300
63#define UMOSCOM_VEND_REG 0x0000
64
65#define UMOSCOM_TXBUF 0x00 /* Write */
66#define UMOSCOM_RXBUF 0x00 /* Read */
67#define UMOSCOM_INT 0x01
68#define UMOSCOM_FIFO 0x02 /* Write */
69#define UMOSCOM_ISR 0x02 /* Read */
70#define UMOSCOM_LCR 0x03
71#define UMOSCOM_MCR 0x04
72#define UMOSCOM_LSR 0x05
73#define UMOSCOM_MSR 0x06
74#define UMOSCOM_SCRATCH 0x07
75#define UMOSCOM_DIV_LO 0x08
76#define UMOSCOM_DIV_HI 0x09
77#define UMOSCOM_EFR 0x0a
78#define UMOSCOM_XON1 0x0b
79#define UMOSCOM_XON2 0x0c
80#define UMOSCOM_XOFF1 0x0d
81#define UMOSCOM_XOFF2 0x0e
82
83#define UMOSCOM_BAUDLO 0x00
84#define UMOSCOM_BAUDHI 0x01
85
86#define UMOSCOM_INT_RXEN 0x01
87#define UMOSCOM_INT_TXEN 0x02
88#define UMOSCOM_INT_RSEN 0x04
89#define UMOSCOM_INT_MDMEM 0x08
90#define UMOSCOM_INT_SLEEP 0x10
91#define UMOSCOM_INT_XOFF 0x20
92#define UMOSCOM_INT_RTS 0x40
93
94#define UMOSCOM_FIFO_EN 0x01
95#define UMOSCOM_FIFO_RXCLR 0x02
96#define UMOSCOM_FIFO_TXCLR 0x04
97#define UMOSCOM_FIFO_DMA_BLK 0x08
98#define UMOSCOM_FIFO_TXLVL_MASK 0x30
99#define UMOSCOM_FIFO_TXLVL_8 0x00
100#define UMOSCOM_FIFO_TXLVL_16 0x10
101#define UMOSCOM_FIFO_TXLVL_32 0x20
102#define UMOSCOM_FIFO_TXLVL_56 0x30
103#define UMOSCOM_FIFO_RXLVL_MASK 0xc0
104#define UMOSCOM_FIFO_RXLVL_8 0x00
105#define UMOSCOM_FIFO_RXLVL_16 0x40
106#define UMOSCOM_FIFO_RXLVL_56 0x80
107#define UMOSCOM_FIFO_RXLVL_80 0xc0
108
109#define UMOSCOM_ISR_MDM 0x00
110#define UMOSCOM_ISR_NONE 0x01
111#define UMOSCOM_ISR_TX 0x02
112#define UMOSCOM_ISR_RX 0x04
113#define UMOSCOM_ISR_LINE 0x06
114#define UMOSCOM_ISR_RXTIMEOUT 0x0c
115#define UMOSCOM_ISR_RX_XOFF 0x10
116#define UMOSCOM_ISR_RTSCTS 0x20
117#define UMOSCOM_ISR_FIFOEN 0xc0
118
119#define UMOSCOM_LCR_DBITS(x) ((x) - 5)
120#define UMOSCOM_LCR_STOP_BITS_1 0x00
121#define UMOSCOM_LCR_STOP_BITS_2 0x04 /* 2 if 6-8 bits/char or 1.5 if 5 */
122#define UMOSCOM_LCR_PARITY_NONE 0x00
123#define UMOSCOM_LCR_PARITY_ODD 0x08
124#define UMOSCOM_LCR_PARITY_EVEN 0x18
125#define UMOSCOM_LCR_BREAK 0x40
126#define UMOSCOM_LCR_DIVLATCH_EN 0x80
127
128#define UMOSCOM_MCR_DTR 0x01
129#define UMOSCOM_MCR_RTS 0x02
130#define UMOSCOM_MCR_LOOP 0x04
131#define UMOSCOM_MCR_INTEN 0x08
132#define UMOSCOM_MCR_LOOPBACK 0x10
133#define UMOSCOM_MCR_XONANY 0x20
134#define UMOSCOM_MCR_IRDA_EN 0x40
135#define UMOSCOM_MCR_BAUD_DIV4 0x80
136
137#define UMOSCOM_LSR_RXDATA 0x01
138#define UMOSCOM_LSR_RXOVER 0x02
139#define UMOSCOM_LSR_RXPAR_ERR 0x04
140#define UMOSCOM_LSR_RXFRM_ERR 0x08
141#define UMOSCOM_LSR_RXBREAK 0x10
142#define UMOSCOM_LSR_TXEMPTY 0x20
143#define UMOSCOM_LSR_TXALLEMPTY 0x40
144#define UMOSCOM_LSR_TXFIFO_ERR 0x80
145
146#define UMOSCOM_MSR_CTS_CHG 0x01
147#define UMOSCOM_MSR_DSR_CHG 0x02
148#define UMOSCOM_MSR_RI_CHG 0x04
149#define UMOSCOM_MSR_CD_CHG 0x08
150#define UMOSCOM_MSR_CTS 0x10
151#define UMOSCOM_MSR_RTS 0x20
152#define UMOSCOM_MSR_RI 0x40
153#define UMOSCOM_MSR_CD 0x80
154
155#define UMOSCOM_BAUD_REF 115200
156
157struct umoscom_softc {
158 struct usb2_com_super_softc sc_super_ucom;
159 struct usb2_com_softc sc_ucom;
160
161 struct usb2_xfer *sc_xfer_data[UMOSCOM_N_DATA_TRANSFER];
162 struct usb2_device *sc_udev;
163
164 uint8_t sc_mcr;
165 uint8_t sc_lcr;
166 uint8_t sc_flags;
167#define UMOSCOM_FLAG_READ_STALL 0x01
168#define UMOSCOM_FLAG_WRITE_STALL 0x02
169#define UMOSCOM_FLAG_INTR_STALL 0x04
170};
171
172/* prototypes */
173
174static device_probe_t umoscom_probe;
175static device_attach_t umoscom_attach;
176static device_detach_t umoscom_detach;
177
178static usb2_callback_t umoscom_write_callback;
179static usb2_callback_t umoscom_write_clear_stall_callback;
180static usb2_callback_t umoscom_read_callback;
181static usb2_callback_t umoscom_read_clear_stall_callback;
182static usb2_callback_t umoscom_intr_callback;
183static usb2_callback_t umoscom_intr_clear_stall_callback;
184
185static void umoscom_cfg_open(struct usb2_com_softc *);
186static void umoscom_cfg_close(struct usb2_com_softc *);
187static void umoscom_cfg_set_break(struct usb2_com_softc *, uint8_t);
188static void umoscom_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
189static void umoscom_cfg_set_rts(struct usb2_com_softc *, uint8_t);
190static int umoscom_pre_param(struct usb2_com_softc *, struct termios *);
191static void umoscom_cfg_param(struct usb2_com_softc *, struct termios *);
192static void umoscom_cfg_get_status(struct usb2_com_softc *, uint8_t *,
193 uint8_t *);
194static void umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
195static uint8_t umoscom_cfg_read(struct umoscom_softc *, uint16_t);
196static void umoscom_cfg_do_request(struct umoscom_softc *,
197 struct usb2_device_request *, void *);
198static void umoscom_start_read(struct usb2_com_softc *);
199static void umoscom_stop_read(struct usb2_com_softc *);
200static void umoscom_start_write(struct usb2_com_softc *);
201static void umoscom_stop_write(struct usb2_com_softc *);
202
203static const struct usb2_config umoscom_config_data[UMOSCOM_N_DATA_TRANSFER] = {
204
205 [0] = {
206 .type = UE_BULK,
207 .endpoint = UE_ADDR_ANY,
208 .direction = UE_DIR_OUT,
209 .mh.bufsize = UMOSCOM_BUFSIZE,
210 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
211 .mh.callback = &umoscom_write_callback,
212 },
213
214 [1] = {
215 .type = UE_BULK,
216 .endpoint = UE_ADDR_ANY,
217 .direction = UE_DIR_IN,
218 .mh.bufsize = UMOSCOM_BUFSIZE,
219 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
220 .mh.callback = &umoscom_read_callback,
221 },
222
223 [2] = {
224 .type = UE_CONTROL,
225 .endpoint = 0x00, /* Control pipe */
226 .direction = UE_DIR_ANY,
227 .mh.bufsize = sizeof(struct usb2_device_request),
228 .mh.callback = &umoscom_write_clear_stall_callback,
229 .mh.timeout = 1000, /* 1 second */
230 .mh.interval = 50, /* 50ms */
231 },
232
233 [3] = {
234 .type = UE_CONTROL,
235 .endpoint = 0x00, /* Control pipe */
236 .direction = UE_DIR_ANY,
237 .mh.bufsize = sizeof(struct usb2_device_request),
238 .mh.callback = &umoscom_read_clear_stall_callback,
239 .mh.timeout = 1000, /* 1 second */
240 .mh.interval = 50, /* 50ms */
241 },
242
243 [4] = {
244 .type = UE_INTERRUPT,
245 .endpoint = UE_ADDR_ANY,
246 .direction = UE_DIR_IN,
247 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
248 .mh.bufsize = 0, /* use wMaxPacketSize */
249 .mh.callback = &umoscom_intr_callback,
250 },
251
252 [5] = {
253 .type = UE_CONTROL,
254 .endpoint = 0x00, /* Control pipe */
255 .direction = UE_DIR_ANY,
256 .mh.bufsize = sizeof(struct usb2_device_request),
257 .mh.callback = &umoscom_intr_clear_stall_callback,
258 .mh.timeout = 1000, /* 1 second */
259 .mh.interval = 50, /* 50ms */
260 },
261};
262
263static const struct usb2_com_callback umoscom_callback = {
264 /* configuration callbacks */
265 .usb2_com_cfg_get_status = &umoscom_cfg_get_status,
266 .usb2_com_cfg_set_dtr = &umoscom_cfg_set_dtr,
267 .usb2_com_cfg_set_rts = &umoscom_cfg_set_rts,
268 .usb2_com_cfg_set_break = &umoscom_cfg_set_break,
269 .usb2_com_cfg_param = &umoscom_cfg_param,
270 .usb2_com_cfg_open = &umoscom_cfg_open,
271 .usb2_com_cfg_close = &umoscom_cfg_close,
272
273 /* other callbacks */
274 .usb2_com_pre_param = &umoscom_pre_param,
275 .usb2_com_start_read = &umoscom_start_read,
276 .usb2_com_stop_read = &umoscom_stop_read,
277 .usb2_com_start_write = &umoscom_start_write,
278 .usb2_com_stop_write = &umoscom_stop_write,
279};
280
281static device_method_t umoscom_methods[] = {
282 DEVMETHOD(device_probe, umoscom_probe),
283 DEVMETHOD(device_attach, umoscom_attach),
284 DEVMETHOD(device_detach, umoscom_detach),
285 {0, 0}
286};
287
288static devclass_t umoscom_devclass;
289
290static driver_t umoscom_driver = {
291 .name = "umoscom",
292 .methods = umoscom_methods,
293 .size = sizeof(struct umoscom_softc),
294};
295
296DRIVER_MODULE(umoscom, ushub, umoscom_driver, umoscom_devclass, NULL, 0);
297MODULE_DEPEND(umoscom, usb2_serial, 1, 1, 1);
298MODULE_DEPEND(umoscom, usb2_core, 1, 1, 1);
299
300static const struct usb2_device_id umoscom_devs[] = {
301 {USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
302};
303
304static int
305umoscom_probe(device_t dev)
306{
307 struct usb2_attach_arg *uaa = device_get_ivars(dev);
308
309 if (uaa->usb2_mode != USB_MODE_HOST) {
310 return (ENXIO);
311 }
312 if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
313 return (ENXIO);
314 }
315 if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
316 return (ENXIO);
317 }
318 return (usb2_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
319}
320
321static int
322umoscom_attach(device_t dev)
323{
324 struct usb2_attach_arg *uaa = device_get_ivars(dev);
325 struct umoscom_softc *sc = device_get_softc(dev);
326 int error;
327 uint8_t iface_index;
328
329 if (sc == NULL) {
330 return (ENOMEM);
331 }
332 sc->sc_udev = uaa->device;
333 sc->sc_mcr = 0x08; /* enable interrupts */
334
335 /* XXX the device doesn't provide any ID string, so set a static one */
336 device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
337 device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
338
339 iface_index = UMOSCOM_IFACE_INDEX;
340 error = usb2_transfer_setup(uaa->device, &iface_index,
341 sc->sc_xfer_data, umoscom_config_data,
342 UMOSCOM_N_DATA_TRANSFER, sc, &Giant);
343
344 if (error) {
345 goto detach;
346 }
347 /* clear stall at first run */
348 sc->sc_flags |= (UMOSCOM_FLAG_READ_STALL |
349 UMOSCOM_FLAG_WRITE_STALL);
350
351 error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
352 &umoscom_callback, &Giant);
353 if (error) {
354 goto detach;
355 }
356 return (0);
357
358detach:
359 device_printf(dev, "attach error: %s\n", usb2_errstr(error));
360 umoscom_detach(dev);
361 return (ENXIO);
362}
363
364static int
365umoscom_detach(device_t dev)
366{
367 struct umoscom_softc *sc = device_get_softc(dev);
368
369 mtx_lock(&Giant);
370
371 mtx_unlock(&Giant);
372
373 usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
374
375 usb2_transfer_unsetup(sc->sc_xfer_data, UMOSCOM_N_DATA_TRANSFER);
376
377 return (0);
378}
379
380static void
381umoscom_cfg_open(struct usb2_com_softc *ucom)
382{
383 struct umoscom_softc *sc = ucom->sc_parent;
384
385 DPRINTF("\n");
386
387 /* Purge FIFOs or odd things happen */
388 umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
389
390 /* Enable FIFO */
391 umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
392 UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
393 UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
394 UMOSCOM_UART_REG);
395
396 /* Enable Interrupt Registers */
397 umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
398
399 /* Magic */
400 umoscom_cfg_write(sc, 0x01, 0x08);
401
402 /* Magic */
403 umoscom_cfg_write(sc, 0x00, 0x02);
404}
405
406static void
407umoscom_cfg_close(struct usb2_com_softc *ucom)
408{
409 return;
410}
411
412static void
413umoscom_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
414{
415 struct umoscom_softc *sc = ucom->sc_parent;
416 uint16_t val;
417
418 val = sc->sc_lcr;
419 if (onoff)
420 val |= UMOSCOM_LCR_BREAK;
421
422 umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
423}
424
425static void
426umoscom_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
427{
428 struct umoscom_softc *sc = ucom->sc_parent;
429
430 if (onoff)
431 sc->sc_mcr |= UMOSCOM_MCR_DTR;
432 else
433 sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
434
435 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
436}
437
438static void
439umoscom_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
440{
441 struct umoscom_softc *sc = ucom->sc_parent;
442
443 if (onoff)
444 sc->sc_mcr |= UMOSCOM_MCR_RTS;
445 else
446 sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
447
448 umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
449}
450
451static int
452umoscom_pre_param(struct usb2_com_softc *ucom, struct termios *t)
453{
454 if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
455 return (EINVAL);
456
457 return (0);
458}
459
460static void
461umoscom_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
462{
463 struct umoscom_softc *sc = ucom->sc_parent;
464 uint16_t data;
465
466 DPRINTF("speed=%d\n", t->c_ospeed);
467
468 data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
469
470 if (data == 0) {
471 DPRINTF("invalid baud rate!\n");
472 return;
473 }
474 umoscom_cfg_write(sc, UMOSCOM_LCR,
475 UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
476
477 umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
478 (data & 0xFF) | UMOSCOM_UART_REG);
479
480 umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
481 ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
482
483 if (t->c_cflag & CSTOPB)
484 data = UMOSCOM_LCR_STOP_BITS_2;
485 else
486 data = UMOSCOM_LCR_STOP_BITS_1;
487
488 if (t->c_cflag & PARENB) {
489 if (t->c_cflag & PARODD)
490 data |= UMOSCOM_LCR_PARITY_ODD;
491 else
492 data |= UMOSCOM_LCR_PARITY_EVEN;
493 } else
494 data |= UMOSCOM_LCR_PARITY_NONE;
495
496 switch (t->c_cflag & CSIZE) {
497 case CS5:
498 data |= UMOSCOM_LCR_DBITS(5);
499 break;
500 case CS6:
501 data |= UMOSCOM_LCR_DBITS(6);
502 break;
503 case CS7:
504 data |= UMOSCOM_LCR_DBITS(7);
505 break;
506 case CS8:
507 data |= UMOSCOM_LCR_DBITS(8);
508 break;
509 }
510
511 sc->sc_lcr = data;
512 umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
513}
514
515static void
516umoscom_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
517{
518 struct umoscom_softc *sc = ucom->sc_parent;
519 uint8_t lsr;
520 uint8_t msr;
521
522 DPRINTFN(5, "\n");
523
524 /* read status registers */
525
526 lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
527 msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
528
529 /* translate bits */
530
531 if (msr & UMOSCOM_MSR_CTS)
532 *p_msr |= SER_CTS;
533
534 if (msr & UMOSCOM_MSR_CD)
535 *p_msr |= SER_DCD;
536
537 if (msr & UMOSCOM_MSR_RI)
538 *p_msr |= SER_RI;
539
540 if (msr & UMOSCOM_MSR_RTS)
541 *p_msr |= SER_DSR;
542}
543
544static void
545umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
546{
547 struct usb2_device_request req;
548
549 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
550 req.bRequest = UMOSCOM_WRITE;
551 USETW(req.wValue, val);
552 USETW(req.wIndex, reg);
553 USETW(req.wLength, 0);
554
555 umoscom_cfg_do_request(sc, &req, NULL);
556}
557
558static uint8_t
559umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
560{
561 struct usb2_device_request req;
562 uint8_t val;
563
564 req.bmRequestType = UT_READ_VENDOR_DEVICE;
565 req.bRequest = UMOSCOM_READ;
566 USETW(req.wValue, 0);
567 USETW(req.wIndex, reg);
568 USETW(req.wLength, 1);
569
570 umoscom_cfg_do_request(sc, &req, &val);
571
572 DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
573
574 return (val);
575}
576
577static void
578umoscom_cfg_do_request(struct umoscom_softc *sc, struct usb2_device_request *req,
579 void *data)
580{
581 uint16_t length;
582 usb2_error_t err;
583
584 if (usb2_com_cfg_is_gone(&sc->sc_ucom))
585 goto error;
586
587 err = usb2_do_request_flags
588 (sc->sc_udev, &Giant, req, data, 0, NULL, 1000);
589
590 if (err) {
591 DPRINTFN(0, "control request failed: %s\n",
592 usb2_errstr(err));
593error:
594 length = UGETW(req->wLength);
595
596 if ((req->bmRequestType & UT_READ) && length) {
597 bzero(data, length);
598 }
599 }
600}
601
602static void
603umoscom_start_read(struct usb2_com_softc *ucom)
604{
605 struct umoscom_softc *sc = ucom->sc_parent;
606
607#if 0
608 /* start interrupt endpoint */
609 usb2_transfer_start(sc->sc_xfer_data[4]);
610#endif
611 /* start read endpoint */
612 usb2_transfer_start(sc->sc_xfer_data[1]);
613}
614
615static void
616umoscom_stop_read(struct usb2_com_softc *ucom)
617{
618 struct umoscom_softc *sc = ucom->sc_parent;
619
620 /* stop interrupt transfer */
621 usb2_transfer_stop(sc->sc_xfer_data[5]);
622 usb2_transfer_stop(sc->sc_xfer_data[4]);
623
624 /* stop read endpoint */
625 usb2_transfer_stop(sc->sc_xfer_data[3]);
626 usb2_transfer_stop(sc->sc_xfer_data[1]);
627}
628
629static void
630umoscom_start_write(struct usb2_com_softc *ucom)
631{
632 struct umoscom_softc *sc = ucom->sc_parent;
633
634 usb2_transfer_start(sc->sc_xfer_data[0]);
635}
636
637static void
638umoscom_stop_write(struct usb2_com_softc *ucom)
639{
640 struct umoscom_softc *sc = ucom->sc_parent;
641
642 usb2_transfer_stop(sc->sc_xfer_data[2]);
643 usb2_transfer_stop(sc->sc_xfer_data[0]);
644}
645
646static void
647umoscom_write_callback(struct usb2_xfer *xfer)
648{
649 struct umoscom_softc *sc = xfer->priv_sc;
650 uint32_t actlen;
651
652 switch (USB_GET_STATE(xfer)) {
653 case USB_ST_SETUP:
654 case USB_ST_TRANSFERRED:
655 DPRINTF("\n");
656
657 if (sc->sc_flags & UMOSCOM_FLAG_WRITE_STALL) {
658 usb2_transfer_start(sc->sc_xfer_data[2]);
659 return;
660 }
661 if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
662 UMOSCOM_BUFSIZE, &actlen)) {
663
664 xfer->frlengths[0] = actlen;
665 usb2_start_hardware(xfer);
666 }
667 return;
668
669 default: /* Error */
670 if (xfer->error != USB_ERR_CANCELLED) {
671 DPRINTFN(0, "transfer failed\n");
672 sc->sc_flags |= UMOSCOM_FLAG_WRITE_STALL;
673 usb2_transfer_start(sc->sc_xfer_data[2]);
674 }
675 return;
676 }
677}
678
679static void
680umoscom_write_clear_stall_callback(struct usb2_xfer *xfer)
681{
682 struct umoscom_softc *sc = xfer->priv_sc;
683 struct usb2_xfer *xfer_other = sc->sc_xfer_data[0];
684
685 if (usb2_clear_stall_callback(xfer, xfer_other)) {
686 DPRINTF("stall cleared\n");
687 sc->sc_flags &= ~UMOSCOM_FLAG_WRITE_STALL;
688 usb2_transfer_start(xfer_other);
689 }
690}
691
692static void
693umoscom_read_callback(struct usb2_xfer *xfer)
694{
695 struct umoscom_softc *sc = xfer->priv_sc;
696
697 switch (USB_GET_STATE(xfer)) {
698 case USB_ST_TRANSFERRED:
699 DPRINTF("got %d bytes\n", xfer->actlen);
700 usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
701
702 case USB_ST_SETUP:
703 DPRINTF("\n");
704
705 if (sc->sc_flags & UMOSCOM_FLAG_READ_STALL) {
706 usb2_transfer_start(sc->sc_xfer_data[3]);
707 } else {
708 xfer->frlengths[0] = xfer->max_data_length;
709 usb2_start_hardware(xfer);
710 }
711 return;
712
713 default: /* Error */
714 if (xfer->error != USB_ERR_CANCELLED) {
715 DPRINTFN(0, "transfer failed\n");
716 sc->sc_flags |= UMOSCOM_FLAG_READ_STALL;
717 usb2_transfer_start(sc->sc_xfer_data[3]);
718 }
719 return;
720
721 }
722}
723
724static void
725umoscom_read_clear_stall_callback(struct usb2_xfer *xfer)
726{
727 struct umoscom_softc *sc = xfer->priv_sc;
728 struct usb2_xfer *xfer_other = sc->sc_xfer_data[1];
729
730 if (usb2_clear_stall_callback(xfer, xfer_other)) {
731 DPRINTF("stall cleared\n");
732 sc->sc_flags &= ~UMOSCOM_FLAG_READ_STALL;
733 usb2_transfer_start(xfer_other);
734 }
735}
736
737static void
738umoscom_intr_callback(struct usb2_xfer *xfer)
739{
740 struct umoscom_softc *sc = xfer->priv_sc;
741
742 switch (USB_GET_STATE(xfer)) {
743 case USB_ST_TRANSFERRED:
744 if (xfer->actlen < 2) {
745 DPRINTF("too short message\n");
746 goto tr_setup;
747 }
748 usb2_com_status_change(&sc->sc_ucom);
749
750 case USB_ST_SETUP:
751tr_setup:
752 if (sc->sc_flags & UMOSCOM_FLAG_INTR_STALL) {
753 usb2_transfer_start(sc->sc_xfer_data[5]);
754 } else {
755 xfer->frlengths[0] = xfer->max_data_length;
756 usb2_start_hardware(xfer);
757 }
758 return;
759
760 default: /* Error */
761 if (xfer->error != USB_ERR_CANCELLED) {
762 DPRINTFN(0, "transfer failed\n");
763 sc->sc_flags |= UMOSCOM_FLAG_INTR_STALL;
764 usb2_transfer_start(sc->sc_xfer_data[5]);
765 }
766 return;
767 }
768}
769
770static void
771umoscom_intr_clear_stall_callback(struct usb2_xfer *xfer)
772{
773 struct umoscom_softc *sc = xfer->priv_sc;
774 struct usb2_xfer *xfer_other = sc->sc_xfer_data[4];
775
776 if (usb2_clear_stall_callback(xfer, xfer_other)) {
777 DPRINTF("stall cleared\n");
778 sc->sc_flags &= ~UMOSCOM_FLAG_INTR_STALL;
779 usb2_transfer_start(xfer_other);
780 }
781}