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