]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/dev/usb/ubser.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / sys / dev / usb / ubser.c
1 /*-
2  * Copyright (c) 2004 Bernd Walter <ticso@freebsd.org>
3  *
4  * $URL: https://devel.bwct.de/svn/projects/ubser/ubser.c $
5  * $Date: 2004-02-29 01:53:10 +0100 (Sun, 29 Feb 2004) $
6  * $Author: ticso $
7  * $Rev: 1127 $
8  */
9
10 /*-
11  * Copyright (c) 2001-2002, Shunsuke Akiyama <akiyama@jp.FreeBSD.org>.
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 /*-
37  * Copyright (c) 2000 The NetBSD Foundation, Inc.
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to The NetBSD Foundation
41  * by Lennart Augustsson (lennart@augustsson.net).
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *        This product includes software developed by the NetBSD
54  *        Foundation, Inc. and its contributors.
55  * 4. Neither the name of The NetBSD Foundation nor the names of its
56  *    contributors may be used to endorse or promote products derived
57  *    from this software without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69  * POSSIBILITY OF SUCH DAMAGE.
70  */
71
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
74
75 /*
76  * BWCT serial adapter driver
77  */
78
79 #include <sys/cdefs.h>
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/module.h>
85 #include <sys/malloc.h>
86 #include <sys/bus.h>
87 #include <sys/ioccom.h>
88 #include <sys/fcntl.h>
89 #include <sys/conf.h>
90 #include <sys/serial.h>
91 #include <sys/tty.h>
92 #include <sys/clist.h>
93 #include <sys/file.h>
94
95 #include <sys/selinfo.h>
96
97 #include <sys/sysctl.h>
98
99 #include <dev/usb/usb.h>
100 #include <dev/usb/usbhid.h>
101
102 #include <dev/usb/usbdi.h>
103 #include <dev/usb/usbdi_util.h>
104 #include "usbdevs.h"
105
106 #include <dev/usb/ubser.h>
107
108 #ifdef USB_DEBUG
109 static int ubserdebug = 0;
110 SYSCTL_NODE(_hw_usb, OID_AUTO, ubser, CTLFLAG_RW, 0, "USB ubser");
111 SYSCTL_INT(_hw_usb_ubser, OID_AUTO, debug, CTLFLAG_RW,
112            &ubserdebug, 0, "ubser debug level");
113 #define DPRINTF(x)      do { \
114                                 if (ubserdebug) \
115                                         logprintf x; \
116                         } while (0)
117
118 #define DPRINTFN(n, x)  do { \
119                                 if (ubserdebug > (n)) \
120                                         logprintf x; \
121                         } while (0)
122 #else
123 #define DPRINTF(x)
124 #define DPRINTFN(n,x)
125 #endif
126
127 #define ISSET(t, f)     ((t) & (f))
128 #define SET(t, f)       (t) |= (f)
129 #define CLR(t, f)       (t) &= ~((unsigned)(f))
130
131 struct ubser_port {
132         int                      p_port;
133         struct ubser_softc      *p_sc;
134         usbd_xfer_handle         p_oxfer;       /* write request */
135         u_char                  *p_obuf;        /* write buffer */
136         struct tty              *p_tty;
137 };
138
139 struct ubser_softc {
140         USBBASEDEVICE           sc_dev;
141         usbd_device_handle      sc_udev;
142         usbd_interface_handle   sc_iface;       /* data interface */
143         int                     sc_ifaceno;
144
145         int                     sc_refcnt;
146         u_char                  sc_dying;
147         u_char                  sc_opening;
148         int                     sc_state;
149         uint8_t                 sc_numser;
150
151         int                     sc_bulkin_no;   /* bulk in endpoint address */
152         usbd_pipe_handle        sc_bulkin_pipe; /* bulk in pipe */
153         usbd_xfer_handle        sc_ixfer;       /* read request */
154         u_char                  *sc_ibuf;       /* read buffer */
155         u_int                   sc_ibufsize;    /* read buffer size */
156         u_int                   sc_ibufsizepad; /* read buffer size padded */
157
158         int                     sc_bulkout_no;  /* bulk out endpoint address */
159         usbd_pipe_handle        sc_bulkout_pipe;/* bulk out pipe */
160         u_int                   sc_obufsize;    /* write buffer size */
161         u_int                   sc_opkthdrlen;  /* header length of
162                                                    output packet */
163
164         struct ubser_port       *sc_port;
165 };
166
167 Static int ubserparam(struct tty *, struct termios *);
168 Static void ubserstart(struct tty *);
169 Static void ubserstop(struct tty *, int);
170 Static usbd_status ubserstartread(struct ubser_softc *);
171 Static void ubserreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
172 Static void ubserwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
173 Static void ubser_cleanup(struct ubser_softc *sc);
174
175 Static t_break_t        ubserbreak;
176 Static t_open_t         ubseropen;
177 Static t_close_t        ubserclose;
178 Static t_modem_t        ubsermodem;
179
180 USB_DECLARE_DRIVER(ubser);
181
182 USB_MATCH(ubser)
183 {
184         USB_MATCH_START(ubser, uaa);
185         usb_string_descriptor_t us;
186         usb_interface_descriptor_t *id;
187         usb_device_descriptor_t *dd;
188         int err, size;
189
190         if (uaa->iface == NULL)
191                 return (UMATCH_NONE);
192
193         DPRINTFN(20,("ubser: vendor=0x%x, product=0x%x\n",
194                      uaa->vendor, uaa->product));
195
196         dd = usbd_get_device_descriptor(uaa->device);
197         if (dd == NULL) {
198                 printf("ubser: failed to get device descriptor\n");
199                 return (UMATCH_NONE);
200         }
201
202         id = usbd_get_interface_descriptor(uaa->iface);
203         if (id == NULL) {
204                 printf("ubser: failed to get interface descriptor\n");
205                 return (UMATCH_NONE);
206         }
207
208         err = usbd_get_string_desc(uaa->device, dd->iManufacturer, 0, &us,
209             &size);
210         if (err != 0)
211                 return (UMATCH_NONE);
212
213         /* check if this is a BWCT vendor specific ubser interface */
214         if (strcmp((char*)us.bString, "B\0W\0C\0T\0") == 0 &&
215             id->bInterfaceClass == 0xff && id->bInterfaceSubClass == 0x00)
216                 return (UMATCH_VENDOR_IFACESUBCLASS);
217
218         return (UMATCH_NONE);
219 }
220
221 USB_ATTACH(ubser)
222 {
223         USB_ATTACH_START(ubser, sc, uaa);
224         usbd_device_handle udev = uaa->device;
225         usb_endpoint_descriptor_t *ed;
226         usb_interface_descriptor_t *id;
227         usb_device_request_t req;
228         char *devinfo;
229         struct tty *tp;
230         usbd_status err;
231         int i;
232         int alen;
233         uint8_t epcount;
234         struct ubser_port *pp;
235
236         devinfo = malloc(1024, M_USBDEV, M_WAITOK);
237         usbd_devinfo(udev, 0, devinfo);
238         USB_ATTACH_SETUP;
239
240         DPRINTFN(10,("\nubser_attach: sc=%p\n", sc));
241
242         sc->sc_udev = udev = uaa->device;
243         sc->sc_iface = uaa->iface;
244         sc->sc_numser = 0;
245         sc->sc_port = NULL;
246
247         /* get interface index */
248         id = usbd_get_interface_descriptor(uaa->iface);
249         if (id == NULL) {
250                 printf("ubser: failed to get interface descriptor\n");
251                 return (UMATCH_NONE);
252         }
253         sc->sc_ifaceno = id->bInterfaceNumber;
254
255         /* get number of serials */
256         req.bmRequestType = UT_READ_VENDOR_INTERFACE;
257         req.bRequest = VENDOR_GET_NUMSER;
258         USETW(req.wValue, 0);
259         USETW(req.wIndex, sc->sc_ifaceno);
260         USETW(req.wLength, 1);
261         err = usbd_do_request_flags(udev, &req, &sc->sc_numser,
262             USBD_SHORT_XFER_OK, &alen, USBD_DEFAULT_TIMEOUT);
263         if (err) {
264                 printf("%s: failed to get number of serials\n",
265                     USBDEVNAME(sc->sc_dev));
266                 goto bad;
267         } else if (alen != 1) {
268                 printf("%s: bogus answer on get_numser\n",
269                     USBDEVNAME(sc->sc_dev));
270                 goto bad;
271         }
272         if (sc->sc_numser > MAX_SER)
273                 sc->sc_numser = MAX_SER;
274         printf("%s: found %i serials\n", USBDEVNAME(sc->sc_dev), sc->sc_numser);
275
276         sc->sc_port = malloc(sizeof(*sc->sc_port) * sc->sc_numser,
277             M_USBDEV, M_WAITOK);
278
279         /* find our bulk endpoints */
280         epcount = 0;
281         usbd_endpoint_count(sc->sc_iface, &epcount);
282         sc->sc_bulkin_no = -1;
283         sc->sc_bulkout_no = -1;
284         for (i = 0; i < epcount; i++) {
285                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
286                 if (ed == NULL) {
287                         printf("%s: couldn't get ep %d\n",
288                         USBDEVNAME(sc->sc_dev), i);
289                         USB_ATTACH_ERROR_RETURN;
290                 }
291                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
292                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
293                         sc->sc_bulkin_no = ed->bEndpointAddress;
294                         sc->sc_ibufsizepad = UGETW(ed->wMaxPacketSize);
295                         sc->sc_ibufsizepad = UGETW(ed->wMaxPacketSize) - 1;
296                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
297                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
298                         sc->sc_bulkout_no = ed->bEndpointAddress;
299                         sc->sc_obufsize = UGETW(ed->wMaxPacketSize) - 1;
300                         sc->sc_opkthdrlen = 1;
301                 }
302         }
303         if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1) {
304                 printf("%s: could not find bulk in/out endpoint\n",
305                     USBDEVNAME(sc->sc_dev));
306                 sc->sc_dying = 1;
307                 goto bad;
308         }
309
310         /* Open the bulk pipes */
311         /* Bulk-in pipe */
312         err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
313                              &sc->sc_bulkin_pipe);
314         if (err) {
315                 printf("%s: open bulk in error (addr %d): %s\n",
316                        USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
317                        usbd_errstr(err));
318                 goto fail_0;
319         }
320         /* Bulk-out pipe */
321         err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
322                              USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
323         if (err) {
324                 printf("%s: open bulk out error (addr %d): %s\n",
325                        USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
326                        usbd_errstr(err));
327                 goto fail_1;
328         }
329
330         /* Allocate a request and an input buffer */
331         sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
332         if (sc->sc_ixfer == NULL) {
333                 goto fail_2;
334         }
335
336         sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
337                                         sc->sc_ibufsizepad);
338         if (sc->sc_ibuf == NULL) {
339                 goto fail_3;
340         }
341
342         for (i = 0; i < sc->sc_numser; i++) {
343                 pp = &sc->sc_port[i];
344                 pp->p_port = i;
345                 pp->p_sc = sc;
346                 tp = pp->p_tty = ttyalloc();
347                 tp->t_sc = pp;
348                 DPRINTF(("ubser_attach: tty_attach tp = %p\n", tp));
349                 tp->t_oproc = ubserstart;
350                 tp->t_param = ubserparam;
351                 tp->t_stop = ubserstop;
352                 tp->t_break = ubserbreak;
353                 tp->t_open = ubseropen;
354                 tp->t_close = ubserclose;
355                 tp->t_modem = ubsermodem;
356                 ttycreate(tp, NULL, 0, 0, "y%r%r", USBDEVUNIT(sc->sc_dev), i);
357         }
358
359
360         for (i = 0; i < sc->sc_numser; i++) {
361                 sc->sc_port[i].p_oxfer = NULL;
362                 sc->sc_port[i].p_obuf = NULL;
363         }
364         for (i = 0; i < sc->sc_numser; i++) {
365                 sc->sc_port[i].p_oxfer = usbd_alloc_xfer(sc->sc_udev);
366                 if (sc->sc_port[i].p_oxfer == NULL) {
367                         goto fail_4;
368                 }
369
370                 sc->sc_port[i].p_obuf = usbd_alloc_buffer(sc->sc_port[i].p_oxfer,
371                                                 sc->sc_obufsize +
372                                                 sc->sc_opkthdrlen);
373                 if (sc->sc_port[i].p_obuf == NULL) {
374                         goto fail_4;
375                 }
376         }
377
378         ubserstartread(sc);
379
380         free(devinfo, M_USBDEV);
381         USB_ATTACH_SUCCESS_RETURN;
382
383 fail_4:
384         for (i = 0; i < sc->sc_numser; i++) {
385                 if (sc->sc_port[i].p_oxfer != NULL) {
386                         usbd_free_xfer(sc->sc_port[i].p_oxfer);
387                         sc->sc_port[i].p_oxfer = NULL;
388                 }
389         }
390 fail_3:
391         usbd_free_xfer(sc->sc_ixfer);
392         sc->sc_ixfer = NULL;
393 fail_2:
394         usbd_close_pipe(sc->sc_bulkout_pipe);
395         sc->sc_bulkout_pipe = NULL;
396 fail_1:
397         usbd_close_pipe(sc->sc_bulkin_pipe);
398         sc->sc_bulkin_pipe = NULL;
399 fail_0:
400         sc->sc_opening = 0;
401         wakeup(&sc->sc_opening);
402
403 bad:
404         ubser_cleanup(sc);
405         if (sc->sc_port != NULL) {
406                 for (i = 0; i < sc->sc_numser; i++) {
407                         pp = &sc->sc_port[i];
408                         if (pp->p_tty != NULL)
409                                 ttyfree(pp->p_tty);
410                 }
411                 free(sc->sc_port, M_USBDEV);
412                 sc->sc_port = NULL;
413         }
414
415         DPRINTF(("ubser_attach: ATTACH ERROR\n"));
416         free(devinfo, M_USBDEV);
417
418         USB_ATTACH_ERROR_RETURN;
419 }
420
421 USB_DETACH(ubser)
422 {
423         USB_DETACH_START(ubser, sc);
424         int i;
425         struct ubser_port *pp;
426
427         DPRINTF(("ubser_detach: sc=%p\n", sc));
428
429         sc->sc_dying = 1;
430         for (i = 0; i < sc->sc_numser; i++) {
431                 pp = &sc->sc_port[i];
432                 if (pp->p_tty != NULL)
433                         ttygone(pp->p_tty);
434         }
435
436         if (sc->sc_bulkin_pipe != NULL)
437                 usbd_abort_pipe(sc->sc_bulkin_pipe);
438         if (sc->sc_bulkout_pipe != NULL)
439                 usbd_abort_pipe(sc->sc_bulkout_pipe);
440
441         if (sc->sc_port != NULL) {
442                 for (i = 0; i < sc->sc_numser; i++) {
443                         pp = &sc->sc_port[i];
444                         if (pp->p_tty != NULL)
445                                 ttyfree(pp->p_tty);
446                 }
447                 free(sc->sc_port, M_USBDEV);
448                 sc->sc_port = NULL;
449         }
450
451         if (--sc->sc_refcnt >= 0) {
452                 /* Wait for processes to go away. */
453                 usb_detach_wait(USBDEV(sc->sc_dev));
454         }
455
456         return (0);
457 }
458
459 Static int
460 ubserparam(struct tty *tp, struct termios *t)
461 {
462         struct ubser_softc *sc;
463         struct ubser_port *pp;
464
465         pp = tp->t_sc;
466         sc = pp->p_sc;
467
468         if (sc->sc_dying)
469                 return (EIO);
470
471         DPRINTF(("ubserparam: sc = %p\n", sc));
472
473         /*
474          * The firmware on our devices can only do 8n1@9600bps
475          * without handshake.
476          * We refuse to accept other configurations.
477          */
478
479         /* enshure 9600bps */
480         switch (t->c_ospeed) {
481         case 9600:
482                 break;
483         default:
484                 return (EINVAL);
485         }
486
487         /* 2 stop bits not possible */
488         if (ISSET(t->c_cflag, CSTOPB))
489                 return (EINVAL);
490
491         /* XXX parity handling not possible with current firmware */
492         if (ISSET(t->c_cflag, PARENB))
493                 return (EINVAL);
494
495         /* we can only do 8 data bits */
496         switch (ISSET(t->c_cflag, CSIZE)) {
497         case CS8:
498                 break;
499         default:
500                 return (EINVAL);
501         }
502
503         /* we can't do any kind of hardware handshaking */
504         if ((t->c_cflag &
505             (CRTS_IFLOW | CDTR_IFLOW |CDSR_OFLOW |CCAR_OFLOW)) != 0)
506                 return (EINVAL);
507
508         /*
509          * XXX xon/xoff not supported by the firmware!
510          * This is handled within FreeBSD only and may overflow buffers
511          * because of delayed reaction due to device buffering.
512          */
513
514         ttsetwater(tp);
515
516         return (0);
517 }
518
519 Static void
520 ubserstart(struct tty *tp)
521 {
522         struct ubser_softc *sc;
523         struct ubser_port *pp;
524         struct cblock *cbp;
525         usbd_status err;
526         u_char *data;
527         int cnt;
528         uint8_t serial;
529
530         pp = tp->t_sc;
531         sc = pp->p_sc;
532         serial = pp->p_port;
533         DPRINTF(("ubserstart: sc = %p, tp = %p\n", sc, tp));
534
535         if (sc->sc_dying)
536                 return;
537
538         if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
539                 ttwwakeup(tp);
540                 DPRINTF(("ubserstart: stopped\n"));
541                 return;
542         }
543
544         if (tp->t_outq.c_cc <= tp->t_olowat) {
545                 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
546                         CLR(tp->t_state, TS_SO_OLOWAT);
547                         wakeup(TSA_OLOWAT(tp));
548                 }
549                 selwakeuppri(&tp->t_wsel, TTIPRI);
550                 if (tp->t_outq.c_cc == 0) {
551                         if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
552                             TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
553                                 CLR(tp->t_state, TS_SO_OCOMPLETE);
554                                 wakeup(TSA_OCOMPLETE(tp));
555                         }
556                         return;
557                 }
558         }
559
560         /* Grab the first contiguous region of buffer space. */
561         data = tp->t_outq.c_cf;
562         cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
563         cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
564
565         if (cnt == 0) {
566                 DPRINTF(("ubserstart: cnt == 0\n"));
567                 return;
568         }
569
570         SET(tp->t_state, TS_BUSY);
571
572         if (cnt + sc->sc_opkthdrlen > sc->sc_obufsize) {
573                 DPRINTF(("ubserstart: big buffer %d chars\n", cnt));
574                 cnt = sc->sc_obufsize;
575         }
576         sc->sc_port[serial].p_obuf[0] = serial;
577         memcpy(sc->sc_port[serial].p_obuf + sc->sc_opkthdrlen, data, cnt);
578
579
580         DPRINTF(("ubserstart: %d chars\n", cnt));
581         usbd_setup_xfer(sc->sc_port[serial].p_oxfer, sc->sc_bulkout_pipe,
582                         (usbd_private_handle)tp, sc->sc_port[serial].p_obuf,
583                         cnt + sc->sc_opkthdrlen,
584                         USBD_NO_COPY, USBD_NO_TIMEOUT, ubserwritecb);
585         /* What can we do on error? */
586         err = usbd_transfer(sc->sc_port[serial].p_oxfer);
587         if (err != USBD_IN_PROGRESS)
588                 printf("ubserstart: err=%s\n", usbd_errstr(err));
589
590         ttwwakeup(tp);
591 }
592
593 Static void
594 ubserstop(struct tty *tp, int flag)
595 {
596         struct ubser_softc *sc;
597
598         sc = tp->t_sc;
599
600         DPRINTF(("ubserstop: %d\n", flag));
601
602         if (flag & FWRITE) {
603                 DPRINTF(("ubserstop: write\n"));
604                 if (ISSET(tp->t_state, TS_BUSY)) {
605                         /* XXX do what? */
606                         if (!ISSET(tp->t_state, TS_TTSTOP))
607                                 SET(tp->t_state, TS_FLUSH);
608                 }
609         }
610
611         DPRINTF(("ubserstop: done\n"));
612 }
613
614 Static void
615 ubserwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
616 {
617         struct tty *tp;
618         struct ubser_softc *sc;
619         struct ubser_port *pp;
620         u_int32_t cc;
621
622         tp = (struct tty *)p;
623         pp = tp->t_sc;
624         sc = pp->p_sc;
625
626         DPRINTF(("ubserwritecb: status = %d\n", status));
627
628         if (status == USBD_CANCELLED || sc->sc_dying)
629                 goto error;
630
631         if (status != USBD_NORMAL_COMPLETION) {
632                 printf("%s: ubserwritecb: %s\n",
633                        USBDEVNAME(sc->sc_dev), usbd_errstr(status));
634                 if (status == USBD_STALLED)
635                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
636                 /* XXX we should restart after some delay. */
637                 goto error;
638         }
639
640         usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
641         DPRINTF(("ubserwritecb: cc = %d\n", cc));
642         if (cc <= sc->sc_opkthdrlen) {
643                 printf("%s: sent size too small, cc = %d\n",
644                        USBDEVNAME(sc->sc_dev), cc);
645                 goto error;
646         }
647
648         /* convert from USB bytes to tty bytes */
649         cc -= sc->sc_opkthdrlen;
650
651         CLR(tp->t_state, TS_BUSY);
652         if (ISSET(tp->t_state, TS_FLUSH))
653                 CLR(tp->t_state, TS_FLUSH);
654         else
655                 ndflush(&tp->t_outq, cc);
656         ttyld_start(tp);
657
658         return;
659
660 error:
661         CLR(tp->t_state, TS_BUSY);
662         return;
663 }
664
665 Static usbd_status
666 ubserstartread(struct ubser_softc *sc)
667 {
668         usbd_status err;
669
670         DPRINTF(("ubserstartread: start\n"));
671
672         if (sc->sc_bulkin_pipe == NULL)
673                 return (USBD_NORMAL_COMPLETION);
674
675         usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
676                         (usbd_private_handle)sc,
677                         sc->sc_ibuf, sc->sc_ibufsizepad,
678                         USBD_SHORT_XFER_OK | USBD_NO_COPY,
679                         USBD_NO_TIMEOUT, ubserreadcb);
680
681         err = usbd_transfer(sc->sc_ixfer);
682         if (err != USBD_IN_PROGRESS) {
683                 DPRINTF(("ubserstartread: err = %s\n", usbd_errstr(err)));
684                 return (err);
685         }
686
687         return (USBD_NORMAL_COMPLETION);
688 }
689
690 Static void
691 ubserreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
692 {
693         struct ubser_softc *sc = (struct ubser_softc *)p;
694         struct tty *tp;
695         usbd_status err;
696         u_int32_t cc;
697         u_char *cp;
698         int lostcc;
699
700         if (status == USBD_IOERROR) {
701                 printf("%s: ubserreadcb: %s - restarting\n",
702                     USBDEVNAME(sc->sc_dev), usbd_errstr(status));
703                 goto resubmit;
704         }
705
706         DPRINTF(("ubserreadcb: status = %d\n", status));
707
708         if (status != USBD_NORMAL_COMPLETION) {
709                 if (status != USBD_CANCELLED) {
710                         printf("%s: ubserreadcb: %s\n",
711                             USBDEVNAME(sc->sc_dev), usbd_errstr(status));
712                 }
713                 if (status == USBD_STALLED)
714                         usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
715                 return;
716         }
717
718         usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);
719
720         DPRINTF(("ubserreadcb: got %d bytes from device\n", cc));
721         if (cc == 0)
722                 goto resubmit;
723
724         if (cc > sc->sc_ibufsizepad) {
725                 printf("%s: invalid receive data size, %d chars\n",
726                        USBDEVNAME(sc->sc_dev), cc);
727                 goto resubmit;
728         }
729
730         /* parse header */
731         if (cc < 1)
732                 goto resubmit;
733         DPRINTF(("ubserreadcb: got %d chars for serial %d\n", cc - 1, *cp));
734         tp = sc->sc_port[*cp].p_tty;
735         cp++;
736         cc--;
737
738         if (cc < 1)
739                 goto resubmit;
740
741         if (!(tp->t_state & TS_ISOPEN)) /* drop data for unused serials */
742                 goto resubmit;
743
744         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
745                 if (tp->t_rawq.c_cc + cc > tp->t_ihiwat
746                     && (tp->t_iflag & IXOFF)
747                     && !(tp->t_state & TS_TBLOCK))
748                         ttyblock(tp);
749                 lostcc = b_to_q((char *)cp, cc, &tp->t_rawq);
750                 tp->t_rawcc += cc;
751                 ttwakeup(tp);
752                 if (tp->t_state & TS_TTSTOP
753                     && (tp->t_iflag & IXANY
754                         || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
755                         tp->t_state &= ~TS_TTSTOP;
756                         tp->t_lflag &= ~FLUSHO;
757                         ubserstart(tp);
758                 }
759                 if (lostcc > 0)
760                         printf("%s: lost %d chars\n", USBDEVNAME(sc->sc_dev),
761                                lostcc);
762         } else {
763                 /* Give characters to tty layer. */
764                 while (cc > 0) {
765                         DPRINTFN(7, ("ubserreadcb: char = 0x%02x\n", *cp));
766                         if (ttyld_rint(tp, *cp) == -1) {
767                                 /* XXX what should we do? */
768                                 printf("%s: lost %d chars\n",
769                                        USBDEVNAME(sc->sc_dev), cc);
770                                 break;
771                         }
772                         cc--;
773                         cp++;
774                 }
775         }
776
777 resubmit:
778         err = ubserstartread(sc);
779         if (err) {
780                 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
781                 /* XXX what should we do now? */
782         }
783
784 }
785
786 Static void
787 ubser_cleanup(struct ubser_softc *sc)
788 {
789         int i;
790         struct ubser_port *pp;
791
792         DPRINTF(("ubser_cleanup: closing pipes\n"));
793
794         if (sc->sc_bulkin_pipe != NULL) {
795                 usbd_abort_pipe(sc->sc_bulkin_pipe);
796                 usbd_close_pipe(sc->sc_bulkin_pipe);
797                 sc->sc_bulkin_pipe = NULL;
798         }
799         if (sc->sc_bulkout_pipe != NULL) {
800                 usbd_abort_pipe(sc->sc_bulkout_pipe);
801                 usbd_close_pipe(sc->sc_bulkout_pipe);
802                 sc->sc_bulkout_pipe = NULL;
803         }
804         if (sc->sc_ixfer != NULL) {
805                 usbd_free_xfer(sc->sc_ixfer);
806                 sc->sc_ixfer = NULL;
807         }
808         for (i = 0; i < sc->sc_numser; i++) {
809                 pp = &sc->sc_port[i];
810                 if (pp->p_oxfer != NULL) {
811                         usbd_free_xfer(pp->p_oxfer);
812                         pp->p_oxfer = NULL;
813                 }
814         }
815 }
816
817 static int
818 ubseropen(struct tty *tp, struct cdev *dev)
819 {
820         struct ubser_softc *sc;
821         struct ubser_port *pp;
822
823         pp = tp->t_sc;
824         sc = pp->p_sc;
825
826         sc->sc_refcnt++;        /* XXX: wrong refcnt on error later on */
827         return (0);
828 }
829
830 static void
831 ubserclose(struct tty *tp)
832 {
833         struct ubser_softc *sc;
834         struct ubser_port *pp;
835
836         pp = tp->t_sc;
837         sc = pp->p_sc;
838         if (--sc->sc_refcnt < 0)
839                 usb_detach_wakeup(USBDEV(sc->sc_dev));
840 }
841
842 static void
843 ubserbreak(struct tty *tp, int sig)
844 {
845         usb_device_request_t req;
846         struct ubser_softc *sc;
847         struct ubser_port *pp;
848         int error;
849         int alen;
850
851         pp = tp->t_sc;
852         sc = pp->p_sc;
853         if (sig) {
854                 DPRINTF(("ubser_break: TIOCSBRK\n"));
855                 req.bmRequestType = UT_READ_VENDOR_INTERFACE;
856                 req.bRequest = VENDOR_SET_BREAK;
857                 USETW(req.wValue, pp->p_port);
858                 USETW(req.wIndex, sc->sc_ifaceno);
859                 USETW(req.wLength, 0);
860                 error = usbd_do_request_flags(sc->sc_udev, &req, &sc->sc_numser,
861                     USBD_SHORT_XFER_OK, &alen, USBD_DEFAULT_TIMEOUT);
862         }
863 }
864
865 static int
866 ubsermodem(struct tty *tp, int sigon, int sigoff)
867 {
868
869         return (SER_DTR | SER_RTS | SER_DCD);
870 }
871
872 DRIVER_MODULE(ubser, uhub, ubser_driver, ubser_devclass, usbd_driver_load, 0);
873