Deleted Added
sdiff udiff text old ( 187176 ) new ( 187259 )
full compact
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>
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}