]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/usb/serial/umoscom.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / usb / serial / umoscom.c
1 /* $FreeBSD$ */
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 <sys/stdint.h>
21 #include <sys/stddef.h>
22 #include <sys/param.h>
23 #include <sys/queue.h>
24 #include <sys/types.h>
25 #include <sys/systm.h>
26 #include <sys/kernel.h>
27 #include <sys/bus.h>
28 #include <sys/module.h>
29 #include <sys/lock.h>
30 #include <sys/mutex.h>
31 #include <sys/condvar.h>
32 #include <sys/sysctl.h>
33 #include <sys/sx.h>
34 #include <sys/unistd.h>
35 #include <sys/callout.h>
36 #include <sys/malloc.h>
37 #include <sys/priv.h>
38
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 #include <dev/usb/usbdi_util.h>
42 #include "usbdevs.h"
43
44 #define USB_DEBUG_VAR umoscom_debug
45 #include <dev/usb/usb_debug.h>
46 #include <dev/usb/usb_process.h>
47
48 #include <dev/usb/serial/usb_serial.h>
49
50 #ifdef USB_DEBUG
51 static int umoscom_debug = 0;
52
53 SYSCTL_NODE(_hw_usb, OID_AUTO, umoscom, CTLFLAG_RW, 0, "USB umoscom");
54 SYSCTL_INT(_hw_usb_umoscom, OID_AUTO, debug, CTLFLAG_RW,
55     &umoscom_debug, 0, "Debug level");
56 #endif
57
58 #define UMOSCOM_BUFSIZE        1024     /* bytes */
59
60 #define UMOSCOM_CONFIG_INDEX    0
61 #define UMOSCOM_IFACE_INDEX     0
62
63 /* interrupt packet */
64 #define UMOSCOM_IIR_RLS         0x06
65 #define UMOSCOM_IIR_RDA         0x04
66 #define UMOSCOM_IIR_CTI         0x0c
67 #define UMOSCOM_IIR_THR         0x02
68 #define UMOSCOM_IIR_MS          0x00
69
70 /* registers */
71 #define UMOSCOM_READ            0x0d
72 #define UMOSCOM_WRITE           0x0e
73 #define UMOSCOM_UART_REG        0x0300
74 #define UMOSCOM_VEND_REG        0x0000
75
76 #define UMOSCOM_TXBUF           0x00    /* Write */
77 #define UMOSCOM_RXBUF           0x00    /* Read */
78 #define UMOSCOM_INT             0x01
79 #define UMOSCOM_FIFO            0x02    /* Write */
80 #define UMOSCOM_ISR             0x02    /* Read */
81 #define UMOSCOM_LCR             0x03
82 #define UMOSCOM_MCR             0x04
83 #define UMOSCOM_LSR             0x05
84 #define UMOSCOM_MSR             0x06
85 #define UMOSCOM_SCRATCH         0x07
86 #define UMOSCOM_DIV_LO          0x08
87 #define UMOSCOM_DIV_HI          0x09
88 #define UMOSCOM_EFR             0x0a
89 #define UMOSCOM_XON1            0x0b
90 #define UMOSCOM_XON2            0x0c
91 #define UMOSCOM_XOFF1           0x0d
92 #define UMOSCOM_XOFF2           0x0e
93
94 #define UMOSCOM_BAUDLO          0x00
95 #define UMOSCOM_BAUDHI          0x01
96
97 #define UMOSCOM_INT_RXEN        0x01
98 #define UMOSCOM_INT_TXEN        0x02
99 #define UMOSCOM_INT_RSEN        0x04
100 #define UMOSCOM_INT_MDMEM       0x08
101 #define UMOSCOM_INT_SLEEP       0x10
102 #define UMOSCOM_INT_XOFF        0x20
103 #define UMOSCOM_INT_RTS         0x40
104
105 #define UMOSCOM_FIFO_EN         0x01
106 #define UMOSCOM_FIFO_RXCLR      0x02
107 #define UMOSCOM_FIFO_TXCLR      0x04
108 #define UMOSCOM_FIFO_DMA_BLK    0x08
109 #define UMOSCOM_FIFO_TXLVL_MASK 0x30
110 #define UMOSCOM_FIFO_TXLVL_8    0x00
111 #define UMOSCOM_FIFO_TXLVL_16   0x10
112 #define UMOSCOM_FIFO_TXLVL_32   0x20
113 #define UMOSCOM_FIFO_TXLVL_56   0x30
114 #define UMOSCOM_FIFO_RXLVL_MASK 0xc0
115 #define UMOSCOM_FIFO_RXLVL_8    0x00
116 #define UMOSCOM_FIFO_RXLVL_16   0x40
117 #define UMOSCOM_FIFO_RXLVL_56   0x80
118 #define UMOSCOM_FIFO_RXLVL_80   0xc0
119
120 #define UMOSCOM_ISR_MDM         0x00
121 #define UMOSCOM_ISR_NONE        0x01
122 #define UMOSCOM_ISR_TX          0x02
123 #define UMOSCOM_ISR_RX          0x04
124 #define UMOSCOM_ISR_LINE        0x06
125 #define UMOSCOM_ISR_RXTIMEOUT   0x0c
126 #define UMOSCOM_ISR_RX_XOFF     0x10
127 #define UMOSCOM_ISR_RTSCTS      0x20
128 #define UMOSCOM_ISR_FIFOEN      0xc0
129
130 #define UMOSCOM_LCR_DBITS(x)    ((x) - 5)
131 #define UMOSCOM_LCR_STOP_BITS_1 0x00
132 #define UMOSCOM_LCR_STOP_BITS_2 0x04    /* 2 if 6-8 bits/char or 1.5 if 5 */
133 #define UMOSCOM_LCR_PARITY_NONE 0x00
134 #define UMOSCOM_LCR_PARITY_ODD  0x08
135 #define UMOSCOM_LCR_PARITY_EVEN 0x18
136 #define UMOSCOM_LCR_BREAK       0x40
137 #define UMOSCOM_LCR_DIVLATCH_EN 0x80
138
139 #define UMOSCOM_MCR_DTR         0x01
140 #define UMOSCOM_MCR_RTS         0x02
141 #define UMOSCOM_MCR_LOOP        0x04
142 #define UMOSCOM_MCR_INTEN       0x08
143 #define UMOSCOM_MCR_LOOPBACK    0x10
144 #define UMOSCOM_MCR_XONANY      0x20
145 #define UMOSCOM_MCR_IRDA_EN     0x40
146 #define UMOSCOM_MCR_BAUD_DIV4   0x80
147
148 #define UMOSCOM_LSR_RXDATA      0x01
149 #define UMOSCOM_LSR_RXOVER      0x02
150 #define UMOSCOM_LSR_RXPAR_ERR   0x04
151 #define UMOSCOM_LSR_RXFRM_ERR   0x08
152 #define UMOSCOM_LSR_RXBREAK     0x10
153 #define UMOSCOM_LSR_TXEMPTY     0x20
154 #define UMOSCOM_LSR_TXALLEMPTY  0x40
155 #define UMOSCOM_LSR_TXFIFO_ERR  0x80
156
157 #define UMOSCOM_MSR_CTS_CHG     0x01
158 #define UMOSCOM_MSR_DSR_CHG     0x02
159 #define UMOSCOM_MSR_RI_CHG      0x04
160 #define UMOSCOM_MSR_CD_CHG      0x08
161 #define UMOSCOM_MSR_CTS         0x10
162 #define UMOSCOM_MSR_RTS         0x20
163 #define UMOSCOM_MSR_RI          0x40
164 #define UMOSCOM_MSR_CD          0x80
165
166 #define UMOSCOM_BAUD_REF        115200
167
168 enum {
169         UMOSCOM_BULK_DT_WR,
170         UMOSCOM_BULK_DT_RD,
171         UMOSCOM_INTR_DT_RD,
172         UMOSCOM_N_TRANSFER,
173 };
174
175 struct umoscom_softc {
176         struct ucom_super_softc sc_super_ucom;
177         struct ucom_softc sc_ucom;
178
179         struct usb_xfer *sc_xfer[UMOSCOM_N_TRANSFER];
180         struct usb_device *sc_udev;
181         struct mtx sc_mtx;
182
183         uint8_t sc_mcr;
184         uint8_t sc_lcr;
185 };
186
187 /* prototypes */
188
189 static device_probe_t umoscom_probe;
190 static device_attach_t umoscom_attach;
191 static device_detach_t umoscom_detach;
192
193 static usb_callback_t umoscom_write_callback;
194 static usb_callback_t umoscom_read_callback;
195 static usb_callback_t umoscom_intr_callback;
196
197 static void     umoscom_cfg_open(struct ucom_softc *);
198 static void     umoscom_cfg_close(struct ucom_softc *);
199 static void     umoscom_cfg_set_break(struct ucom_softc *, uint8_t);
200 static void     umoscom_cfg_set_dtr(struct ucom_softc *, uint8_t);
201 static void     umoscom_cfg_set_rts(struct ucom_softc *, uint8_t);
202 static int      umoscom_pre_param(struct ucom_softc *, struct termios *);
203 static void     umoscom_cfg_param(struct ucom_softc *, struct termios *);
204 static void     umoscom_cfg_get_status(struct ucom_softc *, uint8_t *,
205                     uint8_t *);
206 static void     umoscom_cfg_write(struct umoscom_softc *, uint16_t, uint16_t);
207 static uint8_t  umoscom_cfg_read(struct umoscom_softc *, uint16_t);
208 static void     umoscom_start_read(struct ucom_softc *);
209 static void     umoscom_stop_read(struct ucom_softc *);
210 static void     umoscom_start_write(struct ucom_softc *);
211 static void     umoscom_stop_write(struct ucom_softc *);
212 static void     umoscom_poll(struct ucom_softc *ucom);
213
214 static const struct usb_config umoscom_config_data[UMOSCOM_N_TRANSFER] = {
215
216         [UMOSCOM_BULK_DT_WR] = {
217                 .type = UE_BULK,
218                 .endpoint = UE_ADDR_ANY,
219                 .direction = UE_DIR_OUT,
220                 .bufsize = UMOSCOM_BUFSIZE,
221                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
222                 .callback = &umoscom_write_callback,
223         },
224
225         [UMOSCOM_BULK_DT_RD] = {
226                 .type = UE_BULK,
227                 .endpoint = UE_ADDR_ANY,
228                 .direction = UE_DIR_IN,
229                 .bufsize = UMOSCOM_BUFSIZE,
230                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
231                 .callback = &umoscom_read_callback,
232         },
233
234         [UMOSCOM_INTR_DT_RD] = {
235                 .type = UE_INTERRUPT,
236                 .endpoint = UE_ADDR_ANY,
237                 .direction = UE_DIR_IN,
238                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
239                 .bufsize = 0,   /* use wMaxPacketSize */
240                 .callback = &umoscom_intr_callback,
241         },
242 };
243
244 static const struct ucom_callback umoscom_callback = {
245         /* configuration callbacks */
246         .ucom_cfg_get_status = &umoscom_cfg_get_status,
247         .ucom_cfg_set_dtr = &umoscom_cfg_set_dtr,
248         .ucom_cfg_set_rts = &umoscom_cfg_set_rts,
249         .ucom_cfg_set_break = &umoscom_cfg_set_break,
250         .ucom_cfg_param = &umoscom_cfg_param,
251         .ucom_cfg_open = &umoscom_cfg_open,
252         .ucom_cfg_close = &umoscom_cfg_close,
253
254         /* other callbacks */
255         .ucom_pre_param = &umoscom_pre_param,
256         .ucom_start_read = &umoscom_start_read,
257         .ucom_stop_read = &umoscom_stop_read,
258         .ucom_start_write = &umoscom_start_write,
259         .ucom_stop_write = &umoscom_stop_write,
260         .ucom_poll = &umoscom_poll,
261 };
262
263 static device_method_t umoscom_methods[] = {
264         DEVMETHOD(device_probe, umoscom_probe),
265         DEVMETHOD(device_attach, umoscom_attach),
266         DEVMETHOD(device_detach, umoscom_detach),
267         {0, 0}
268 };
269
270 static devclass_t umoscom_devclass;
271
272 static driver_t umoscom_driver = {
273         .name = "umoscom",
274         .methods = umoscom_methods,
275         .size = sizeof(struct umoscom_softc),
276 };
277
278 DRIVER_MODULE(umoscom, uhub, umoscom_driver, umoscom_devclass, NULL, 0);
279 MODULE_DEPEND(umoscom, ucom, 1, 1, 1);
280 MODULE_DEPEND(umoscom, usb, 1, 1, 1);
281 MODULE_VERSION(umoscom, 1);
282
283 static const STRUCT_USB_HOST_ID umoscom_devs[] = {
284         {USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7703, 0)}
285 };
286
287 static int
288 umoscom_probe(device_t dev)
289 {
290         struct usb_attach_arg *uaa = device_get_ivars(dev);
291
292         if (uaa->usb_mode != USB_MODE_HOST) {
293                 return (ENXIO);
294         }
295         if (uaa->info.bConfigIndex != UMOSCOM_CONFIG_INDEX) {
296                 return (ENXIO);
297         }
298         if (uaa->info.bIfaceIndex != UMOSCOM_IFACE_INDEX) {
299                 return (ENXIO);
300         }
301         return (usbd_lookup_id_by_uaa(umoscom_devs, sizeof(umoscom_devs), uaa));
302 }
303
304 static int
305 umoscom_attach(device_t dev)
306 {
307         struct usb_attach_arg *uaa = device_get_ivars(dev);
308         struct umoscom_softc *sc = device_get_softc(dev);
309         int error;
310         uint8_t iface_index;
311
312         sc->sc_udev = uaa->device;
313         sc->sc_mcr = 0x08;              /* enable interrupts */
314
315         /* XXX the device doesn't provide any ID string, so set a static one */
316         device_set_desc(dev, "MOSCHIP USB Serial Port Adapter");
317         device_printf(dev, "<MOSCHIP USB Serial Port Adapter>\n");
318
319         mtx_init(&sc->sc_mtx, "umoscom", NULL, MTX_DEF);
320
321         iface_index = UMOSCOM_IFACE_INDEX;
322         error = usbd_transfer_setup(uaa->device, &iface_index,
323             sc->sc_xfer, umoscom_config_data,
324             UMOSCOM_N_TRANSFER, sc, &sc->sc_mtx);
325
326         if (error) {
327                 goto detach;
328         }
329         /* clear stall at first run */
330         mtx_lock(&sc->sc_mtx);
331         usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
332         usbd_xfer_set_stall(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
333         mtx_unlock(&sc->sc_mtx);
334
335         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
336             &umoscom_callback, &sc->sc_mtx);
337         if (error) {
338                 goto detach;
339         }
340         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
341
342         return (0);
343
344 detach:
345         device_printf(dev, "attach error: %s\n", usbd_errstr(error));
346         umoscom_detach(dev);
347         return (ENXIO);
348 }
349
350 static int
351 umoscom_detach(device_t dev)
352 {
353         struct umoscom_softc *sc = device_get_softc(dev);
354
355         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
356         usbd_transfer_unsetup(sc->sc_xfer, UMOSCOM_N_TRANSFER);
357         mtx_destroy(&sc->sc_mtx);
358
359         return (0);
360 }
361
362 static void
363 umoscom_cfg_open(struct ucom_softc *ucom)
364 {
365         struct umoscom_softc *sc = ucom->sc_parent;
366
367         DPRINTF("\n");
368
369         /* Purge FIFOs or odd things happen */
370         umoscom_cfg_write(sc, UMOSCOM_FIFO, 0x00 | UMOSCOM_UART_REG);
371
372         /* Enable FIFO */
373         umoscom_cfg_write(sc, UMOSCOM_FIFO, UMOSCOM_FIFO_EN |
374             UMOSCOM_FIFO_RXCLR | UMOSCOM_FIFO_TXCLR |
375             UMOSCOM_FIFO_DMA_BLK | UMOSCOM_FIFO_RXLVL_MASK |
376             UMOSCOM_UART_REG);
377
378         /* Enable Interrupt Registers */
379         umoscom_cfg_write(sc, UMOSCOM_INT, 0x0C | UMOSCOM_UART_REG);
380
381         /* Magic */
382         umoscom_cfg_write(sc, 0x01, 0x08);
383
384         /* Magic */
385         umoscom_cfg_write(sc, 0x00, 0x02);
386 }
387
388 static void
389 umoscom_cfg_close(struct ucom_softc *ucom)
390 {
391         return;
392 }
393
394 static void
395 umoscom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
396 {
397         struct umoscom_softc *sc = ucom->sc_parent;
398         uint16_t val;
399
400         val = sc->sc_lcr;
401         if (onoff)
402                 val |= UMOSCOM_LCR_BREAK;
403
404         umoscom_cfg_write(sc, UMOSCOM_LCR, val | UMOSCOM_UART_REG);
405 }
406
407 static void
408 umoscom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
409 {
410         struct umoscom_softc *sc = ucom->sc_parent;
411
412         if (onoff)
413                 sc->sc_mcr |= UMOSCOM_MCR_DTR;
414         else
415                 sc->sc_mcr &= ~UMOSCOM_MCR_DTR;
416
417         umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
418 }
419
420 static void
421 umoscom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
422 {
423         struct umoscom_softc *sc = ucom->sc_parent;
424
425         if (onoff)
426                 sc->sc_mcr |= UMOSCOM_MCR_RTS;
427         else
428                 sc->sc_mcr &= ~UMOSCOM_MCR_RTS;
429
430         umoscom_cfg_write(sc, UMOSCOM_MCR, sc->sc_mcr | UMOSCOM_UART_REG);
431 }
432
433 static int
434 umoscom_pre_param(struct ucom_softc *ucom, struct termios *t)
435 {
436         if ((t->c_ospeed <= 1) || (t->c_ospeed > 115200))
437                 return (EINVAL);
438
439         return (0);
440 }
441
442 static void
443 umoscom_cfg_param(struct ucom_softc *ucom, struct termios *t)
444 {
445         struct umoscom_softc *sc = ucom->sc_parent;
446         uint16_t data;
447
448         DPRINTF("speed=%d\n", t->c_ospeed);
449
450         data = ((uint32_t)UMOSCOM_BAUD_REF) / ((uint32_t)t->c_ospeed);
451
452         if (data == 0) {
453                 DPRINTF("invalid baud rate!\n");
454                 return;
455         }
456         umoscom_cfg_write(sc, UMOSCOM_LCR,
457             UMOSCOM_LCR_DIVLATCH_EN | UMOSCOM_UART_REG);
458
459         umoscom_cfg_write(sc, UMOSCOM_BAUDLO,
460             (data & 0xFF) | UMOSCOM_UART_REG);
461
462         umoscom_cfg_write(sc, UMOSCOM_BAUDHI,
463             ((data >> 8) & 0xFF) | UMOSCOM_UART_REG);
464
465         if (t->c_cflag & CSTOPB)
466                 data = UMOSCOM_LCR_STOP_BITS_2;
467         else
468                 data = UMOSCOM_LCR_STOP_BITS_1;
469
470         if (t->c_cflag & PARENB) {
471                 if (t->c_cflag & PARODD)
472                         data |= UMOSCOM_LCR_PARITY_ODD;
473                 else
474                         data |= UMOSCOM_LCR_PARITY_EVEN;
475         } else
476                 data |= UMOSCOM_LCR_PARITY_NONE;
477
478         switch (t->c_cflag & CSIZE) {
479         case CS5:
480                 data |= UMOSCOM_LCR_DBITS(5);
481                 break;
482         case CS6:
483                 data |= UMOSCOM_LCR_DBITS(6);
484                 break;
485         case CS7:
486                 data |= UMOSCOM_LCR_DBITS(7);
487                 break;
488         case CS8:
489                 data |= UMOSCOM_LCR_DBITS(8);
490                 break;
491         }
492
493         sc->sc_lcr = data;
494         umoscom_cfg_write(sc, UMOSCOM_LCR, data | UMOSCOM_UART_REG);
495 }
496
497 static void
498 umoscom_cfg_get_status(struct ucom_softc *ucom, uint8_t *p_lsr, uint8_t *p_msr)
499 {
500         struct umoscom_softc *sc = ucom->sc_parent;
501         uint8_t lsr;
502         uint8_t msr;
503
504         DPRINTFN(5, "\n");
505
506         /* read status registers */
507
508         lsr = umoscom_cfg_read(sc, UMOSCOM_LSR);
509         msr = umoscom_cfg_read(sc, UMOSCOM_MSR);
510
511         /* translate bits */
512
513         if (msr & UMOSCOM_MSR_CTS)
514                 *p_msr |= SER_CTS;
515
516         if (msr & UMOSCOM_MSR_CD)
517                 *p_msr |= SER_DCD;
518
519         if (msr & UMOSCOM_MSR_RI)
520                 *p_msr |= SER_RI;
521
522         if (msr & UMOSCOM_MSR_RTS)
523                 *p_msr |= SER_DSR;
524 }
525
526 static void
527 umoscom_cfg_write(struct umoscom_softc *sc, uint16_t reg, uint16_t val)
528 {
529         struct usb_device_request req;
530
531         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
532         req.bRequest = UMOSCOM_WRITE;
533         USETW(req.wValue, val);
534         USETW(req.wIndex, reg);
535         USETW(req.wLength, 0);
536
537         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
538             &req, NULL, 0, 1000);
539 }
540
541 static uint8_t
542 umoscom_cfg_read(struct umoscom_softc *sc, uint16_t reg)
543 {
544         struct usb_device_request req;
545         uint8_t val;
546
547         req.bmRequestType = UT_READ_VENDOR_DEVICE;
548         req.bRequest = UMOSCOM_READ;
549         USETW(req.wValue, 0);
550         USETW(req.wIndex, reg);
551         USETW(req.wLength, 1);
552
553         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
554             &req, &val, 0, 1000);
555
556         DPRINTF("reg=0x%04x, val=0x%02x\n", reg, val);
557
558         return (val);
559 }
560
561 static void
562 umoscom_start_read(struct ucom_softc *ucom)
563 {
564         struct umoscom_softc *sc = ucom->sc_parent;
565
566 #if 0
567         /* start interrupt endpoint */
568         usbd_transfer_start(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
569 #endif
570         /* start read endpoint */
571         usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
572 }
573
574 static void
575 umoscom_stop_read(struct ucom_softc *ucom)
576 {
577         struct umoscom_softc *sc = ucom->sc_parent;
578
579         /* stop interrupt transfer */
580         usbd_transfer_stop(sc->sc_xfer[UMOSCOM_INTR_DT_RD]);
581
582         /* stop read endpoint */
583         usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_RD]);
584 }
585
586 static void
587 umoscom_start_write(struct ucom_softc *ucom)
588 {
589         struct umoscom_softc *sc = ucom->sc_parent;
590
591         usbd_transfer_start(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
592 }
593
594 static void
595 umoscom_stop_write(struct ucom_softc *ucom)
596 {
597         struct umoscom_softc *sc = ucom->sc_parent;
598
599         usbd_transfer_stop(sc->sc_xfer[UMOSCOM_BULK_DT_WR]);
600 }
601
602 static void
603 umoscom_write_callback(struct usb_xfer *xfer, usb_error_t error)
604 {
605         struct umoscom_softc *sc = usbd_xfer_softc(xfer);
606         struct usb_page_cache *pc;
607         uint32_t actlen;
608
609         switch (USB_GET_STATE(xfer)) {
610         case USB_ST_SETUP:
611         case USB_ST_TRANSFERRED:
612 tr_setup:
613                 DPRINTF("\n");
614
615                 pc = usbd_xfer_get_frame(xfer, 0);
616                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
617                     UMOSCOM_BUFSIZE, &actlen)) {
618
619                         usbd_xfer_set_frame_len(xfer, 0, actlen);
620                         usbd_transfer_submit(xfer);
621                 }
622                 return;
623
624         default:                        /* Error */
625                 if (error != USB_ERR_CANCELLED) {
626                         DPRINTFN(0, "transfer failed\n");
627                         /* try to clear stall first */
628                         usbd_xfer_set_stall(xfer);
629                         goto tr_setup;
630                 }
631                 return;
632         }
633 }
634
635 static void
636 umoscom_read_callback(struct usb_xfer *xfer, usb_error_t error)
637 {
638         struct umoscom_softc *sc = usbd_xfer_softc(xfer);
639         struct usb_page_cache *pc;
640         int actlen;
641
642         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
643
644         switch (USB_GET_STATE(xfer)) {
645         case USB_ST_TRANSFERRED:
646                 DPRINTF("got %d bytes\n", actlen);
647                 pc = usbd_xfer_get_frame(xfer, 0);
648                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
649
650         case USB_ST_SETUP:
651 tr_setup:
652                 DPRINTF("\n");
653
654                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
655                 usbd_transfer_submit(xfer);
656                 return;
657
658         default:                        /* Error */
659                 if (error != USB_ERR_CANCELLED) {
660                         DPRINTFN(0, "transfer failed\n");
661                         /* try to clear stall first */
662                         usbd_xfer_set_stall(xfer);
663                         goto tr_setup;
664                 }
665                 return;
666         }
667 }
668
669 static void
670 umoscom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
671 {
672         struct umoscom_softc *sc = usbd_xfer_softc(xfer);
673         int actlen;
674
675         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
676
677         switch (USB_GET_STATE(xfer)) {
678         case USB_ST_TRANSFERRED:
679                 if (actlen < 2) {
680                         DPRINTF("too short message\n");
681                         goto tr_setup;
682                 }
683                 ucom_status_change(&sc->sc_ucom);
684
685         case USB_ST_SETUP:
686 tr_setup:
687                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
688                 usbd_transfer_submit(xfer);
689                 return;
690
691         default:                        /* Error */
692                 if (error != USB_ERR_CANCELLED) {
693                         DPRINTFN(0, "transfer failed\n");
694                         /* try to clear stall first */
695                         usbd_xfer_set_stall(xfer);
696                         goto tr_setup;
697                 }
698                 return;
699         }
700 }
701
702 static void
703 umoscom_poll(struct ucom_softc *ucom)
704 {
705         struct umoscom_softc *sc = ucom->sc_parent;
706         usbd_transfer_poll(sc->sc_xfer, UMOSCOM_N_TRANSFER);
707 }