1 /* $NetBSD: umodem.c,v 1.5 1999/01/08 11:58:25 augustss Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * Comm Class spec: http://www.usb.org/developers/data/devclass/usbcdc10.pdf
43 * http://www.usb.org/developers/data/devclass/usbcdc11.pdf
48 * - Add error recovery in various places; the big problem is what
49 * to do in a callback if there is an error.
50 * - Implement a Call Device for modems without multiplexed commands.
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #if defined(__NetBSD__) || defined(__OpenBSD__)
59 #include <sys/device.h>
60 #include <sys/ioctl.h>
61 #elif defined(__FreeBSD__)
63 #include <sys/ioccom.h>
64 #include <sys/fcntl.h>
68 #include <sys/clist.h>
70 #if __FreeBSD_version >= 500014
71 #include <sys/selinfo.h>
73 #include <sys/select.h>
76 #include <sys/vnode.h>
78 #include <sys/sysctl.h>
80 #include <dev/usb/usb.h>
81 #include <dev/usb/usbcdc.h>
83 #include <dev/usb/usbdi.h>
84 #include <dev/usb/usbdi_util.h>
85 #include <dev/usb/usbdevs.h>
86 #include <dev/usb/usb_quirks.h>
88 #include <dev/usb/usbdevs.h>
91 #define DPRINTF(x) if(umodemdebug) logprintf x
92 #define DPRINTFN(n, x) if(umodemdebug > (n)) logprintf x
94 SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
95 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
96 &umodemdebug, 0, "umodem debug level");
99 #define DPRINTFN(n, x)
102 /* Macros to clear/set/test flags. */
103 #define SET(t, f) (t) |= (f)
104 #define CLR(t, f) (t) &= ~((unsigned)(f))
105 #define ISSET(t, f) ((t) & (f))
107 #define UMODEMUNIT_MASK 0x3ffff
108 #define UMODEMDIALOUT_MASK 0x80000
109 #define UMODEMCALLUNIT_MASK 0x40000
111 #define UMODEMUNIT(x) (minor(x) & UMODEMUNIT_MASK)
112 #define UMODEMDIALOUT(x) (minor(x) & UMODEMDIALOUT_MASK)
113 #define UMODEMCALLUNIT(x) (minor(x) & UMODEMCALLUNIT_MASK)
115 #define UMODEMIBUFSIZE 64
117 struct umodem_softc {
118 USBBASEDEVICE sc_dev; /* base device */
120 usbd_device_handle sc_udev; /* USB device */
123 usbd_interface_handle sc_ctl_iface; /* control interface */
124 int sc_data_iface_no;
125 usbd_interface_handle sc_data_iface; /* data interface */
127 int sc_bulkin_no; /* bulk in endpoint address */
128 usbd_pipe_handle sc_bulkin_pipe; /* bulk in pipe */
129 usbd_xfer_handle sc_ixfer; /* read request */
130 u_char *sc_ibuf; /* read buffer */
132 int sc_bulkout_no; /* bulk out endpoint address */
133 usbd_pipe_handle sc_bulkout_pipe;/* bulk out pipe */
134 usbd_xfer_handle sc_oxfer; /* read request */
136 int sc_cm_cap; /* CM capabilities */
137 int sc_acm_cap; /* ACM capabilities */
141 struct tty *sc_tty; /* our tty */
143 usb_cdc_line_state_t sc_line_state; /* current line state */
144 u_char sc_dtr; /* current DTR state */
146 u_char sc_opening; /* lock during open */
147 u_char sc_dying; /* disconnecting */
149 #if defined(__FreeBSD__)
150 dev_t dev; /* special device node */
154 #if defined(__NetBSD__) || defined(__OpenBSD__)
157 #elif defined(__FreeBSD__)
159 d_close_t umodemclose;
161 d_write_t umodemwrite;
162 d_ioctl_t umodemioctl;
164 #define UMODEM_CDEV_MAJOR 124
166 static struct cdevsw umodem_cdevsw = {
167 /* open */ umodemopen,
168 /* close */ umodemclose,
169 /* read */ umodemread,
170 /* write */ umodemwrite,
171 /* ioctl */ umodemioctl,
174 /* strategy */ nostrategy,
176 /* maj */ UMODEM_CDEV_MAJOR,
179 /* flags */ D_TTY | D_KQFILTER,
180 #if __FreeBSD_version < 500014
183 /* kqfilter */ ttykqfilter,
187 void *umodem_get_desc
188 (usbd_device_handle dev, int type, int subtype);
189 usbd_status umodem_set_comm_feature
190 (struct umodem_softc *sc, int feature, int state);
191 usbd_status umodem_set_line_coding
192 (struct umodem_softc *sc, usb_cdc_line_state_t *state);
194 void umodem_get_caps (usbd_device_handle, int *, int *);
195 void umodem_cleanup (struct umodem_softc *);
196 int umodemparam (struct tty *, struct termios *);
197 void umodemstart (struct tty *);
198 void umodemstop (struct tty *, int);
199 struct tty * umodemtty (dev_t dev);
200 void umodem_shutdown (struct umodem_softc *);
201 void umodem_modem (struct umodem_softc *, int);
202 void umodem_break (struct umodem_softc *, int);
203 usbd_status umodemstartread (struct umodem_softc *);
204 void umodemreadcb (usbd_xfer_handle, usbd_private_handle,
206 void umodemwritecb (usbd_xfer_handle, usbd_private_handle,
209 USB_DECLARE_DRIVER(umodem);
213 USB_MATCH_START(umodem, uaa);
214 usb_interface_descriptor_t *id;
218 return (UMATCH_NONE);
220 id = usbd_get_interface_descriptor(uaa->iface);
222 id->bInterfaceClass != UICLASS_CDC ||
223 id->bInterfaceSubClass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
224 id->bInterfaceProtocol != UIPROTO_CDC_AT)
225 return (UMATCH_NONE);
227 umodem_get_caps(uaa->device, &cm, &acm);
228 if (!(cm & USB_CDC_CM_DOES_CM) ||
229 !(cm & USB_CDC_CM_OVER_DATA) ||
230 !(acm & USB_CDC_ACM_HAS_LINE))
231 return (UMATCH_NONE);
233 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
238 USB_ATTACH_START(umodem, sc, uaa);
239 usbd_device_handle dev = uaa->device;
240 usb_interface_descriptor_t *id;
241 usb_endpoint_descriptor_t *ed;
242 usb_cdc_cm_descriptor_t *cmd;
249 usbd_devinfo(uaa->device, 0, devinfo);
254 sc->sc_ctl_iface = uaa->iface;
255 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
256 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
257 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
258 sc->sc_ctl_iface_no = id->bInterfaceNumber;
260 umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
262 /* Get the data interface no. */
263 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
265 DPRINTF(("%s: no CM desc\n", USBDEVNAME(sc->sc_dev)));
268 sc->sc_data_iface_no = data_ifaceno = cmd->bDataInterface;
270 printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
271 USBDEVNAME(sc->sc_dev), data_ifaceno,
272 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
273 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
276 /* Get the data interface too. */
277 for (i = 0; i < uaa->nifaces; i++) {
278 if (uaa->ifaces[i]) {
279 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
280 if (id->bInterfaceNumber == data_ifaceno) {
281 sc->sc_data_iface = uaa->ifaces[i];
286 if (!sc->sc_data_iface) {
287 printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
292 * Find the bulk endpoints.
293 * Iterate over all endpoints in the data interface and take note.
295 sc->sc_bulkin_no = sc->sc_bulkout_no = -1;
297 id = usbd_get_interface_descriptor(sc->sc_data_iface);
298 for (i = 0; i < id->bNumEndpoints; i++) {
299 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
301 printf("%s: no endpoint descriptor for %d\n",
302 USBDEVNAME(sc->sc_dev), i);
305 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
306 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
307 sc->sc_bulkin_no = ed->bEndpointAddress;
308 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
309 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
310 sc->sc_bulkout_no = ed->bEndpointAddress;
314 if (sc->sc_bulkin_no == -1) {
315 DPRINTF(("%s: Could not find data bulk in\n",
316 USBDEVNAME(sc->sc_dev)));
319 if (sc->sc_bulkout_no == -1) {
320 DPRINTF(("%s: Could not find data bulk out\n",
321 USBDEVNAME(sc->sc_dev)));
325 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
326 sc->sc_cm_over_data = 1;
328 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
329 err = umodem_set_comm_feature(sc, UCDC_ABSTRACT_STATE,
330 UCDC_DATA_MULTIPLEXED);
333 sc->sc_cm_over_data = 1;
337 #if defined(__NetBSD__) || defined(__OpenBSD__)
338 sc->sc_tty = tp = ttymalloc();
339 #elif defined(__FreeBSD__)
340 sc->sc_tty = tp = ttymalloc(sc->sc_tty);
342 tp->t_oproc = umodemstart;
343 tp->t_param = umodemparam;
344 tp->t_stop = umodemstop;
345 DPRINTF(("umodem_attach: tty_attach %p\n", tp));
346 #if defined(__NetBSD__) || defined(__OpenBSD__)
350 #if defined(__FreeBSD__)
351 DPRINTF(("umodem_attach: make_dev: umodem%d\n", device_get_unit(self)));
352 sc->dev = make_dev(&umodem_cdevsw, device_get_unit(self),
353 UID_UUCP, GID_DIALER, 0660,
354 "umodem%d", device_get_unit(self));
355 sc->dev->si_tty = tp;
360 USB_ATTACH_SUCCESS_RETURN;
363 DPRINTF(("umodem_attach: BAD -> DYING\n"));
365 USB_ATTACH_ERROR_RETURN;
369 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm)
371 usb_cdc_cm_descriptor_t *cmd;
372 usb_cdc_acm_descriptor_t *cad;
376 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
378 DPRINTF(("umodem_get_desc: no CM desc\n"));
381 *cm = cmd->bmCapabilities;
383 cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
385 DPRINTF(("umodem_get_desc: no ACM desc\n"));
388 *acm = cad->bmCapabilities;
392 umodemstart(struct tty *tp)
394 struct umodem_softc *sc;
400 USB_GET_SC(umodem, UMODEMUNIT(tp->t_dev), sc);
406 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
408 DPRINTFN(4,("umodemstart: stopped\n"));
412 #if defined(__NetBSD__) || defined(__OpenBSD__)
413 if (tp->t_outq.c_cc <= tp->t_lowat) {
414 if (ISSET(tp->t_state, TS_ASLEEP)) {
415 CLR(tp->t_state, TS_ASLEEP);
418 selwakeup(&tp->t_wsel);
419 if (tp->t_outq.c_cc == 0)
422 #elif defined(__FreeBSD__)
423 if (tp->t_outq.c_cc <= tp->t_olowat) {
424 if (ISSET(tp->t_state, TS_SO_OLOWAT)) {
425 CLR(tp->t_state, TS_SO_OLOWAT);
426 wakeup(TSA_OLOWAT(tp));
428 selwakeup(&tp->t_wsel);
429 if (tp->t_outq.c_cc == 0) {
430 if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
431 TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
432 CLR(tp->t_state, TS_SO_OCOMPLETE);
433 wakeup(TSA_OCOMPLETE(tp));
440 /* Grab the first contiguous region of buffer space. */
441 data = tp->t_outq.c_cf;
442 #if defined(__NetBSD__) || defined(__OpenBSD__)
443 cnt = ndqb(&tp->t_outq, 0);
444 #elif defined(__FreeBSD__)
445 cbp = (struct cblock *) ((intptr_t) tp->t_outq.c_cf & ~CROUND);
446 cnt = min((char *) (cbp+1) - tp->t_outq.c_cf, tp->t_outq.c_cc);
450 DPRINTF(("umodemstart: cnt==0\n"));
455 SET(tp->t_state, TS_BUSY);
457 DPRINTFN(4,("umodemstart: %d chars\n", cnt));
458 /* XXX what can we do on error? */
459 usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
460 (usbd_private_handle)sc, data, cnt,
461 0, USBD_NO_TIMEOUT, umodemwritecb);
462 (void)usbd_transfer(sc->sc_oxfer);
470 umodemwritecb(usbd_xfer_handle xfer, usbd_private_handle priv,
473 struct umodem_softc *sc = (struct umodem_softc *)priv;
474 struct tty *tp = sc->sc_tty;
478 DPRINTFN(5,("umodemwritecb: status=%d\n", status));
480 if (status == USBD_CANCELLED)
483 if (status != USBD_NORMAL_COMPLETION) {
484 DPRINTF(("umodemwritecb: status=%d\n", status));
485 if (status == USBD_STALLED)
486 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
487 /* XXX we should restart after some delay. */
491 usbd_get_xfer_status(xfer, 0, 0, &cc, 0);
492 DPRINTFN(5,("umodemwritecb: cc=%d\n", cc));
495 CLR(tp->t_state, TS_BUSY);
496 if (ISSET(tp->t_state, TS_FLUSH))
497 CLR(tp->t_state, TS_FLUSH);
499 ndflush(&tp->t_outq, cc);
500 (*linesw[tp->t_line].l_start)(tp);
505 umodemparam(struct tty *tp, struct termios *t)
507 struct umodem_softc *sc;
508 usb_cdc_line_state_t ls;
510 USB_GET_SC(umodem, UMODEMUNIT(tp->t_dev), sc);
515 /* Check requested parameters. */
518 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
522 * If there were no changes, don't do anything. This avoids dropping
523 * input and improves performance when all we did was frob things like
526 if (tp->t_ospeed == t->c_ospeed &&
527 tp->t_cflag == t->c_cflag)
530 /* And copy to tty. */
532 tp->t_ospeed = t->c_ospeed;
533 tp->t_cflag = t->c_cflag;
535 USETDW(ls.dwDTERate, t->c_ospeed);
536 if (ISSET(t->c_cflag, CSTOPB))
537 ls.bCharFormat = UCDC_STOP_BIT_2;
539 ls.bCharFormat = UCDC_STOP_BIT_1;
540 if (ISSET(t->c_cflag, PARENB)) {
541 if (ISSET(t->c_cflag, PARODD))
542 ls.bParityType = UCDC_PARITY_ODD;
544 ls.bParityType = UCDC_PARITY_EVEN;
546 ls.bParityType = UCDC_PARITY_NONE;
547 switch (ISSET(t->c_cflag, CSIZE)) {
561 /* XXX what can we if it fails? */
562 (void)umodem_set_line_coding(sc, &ls);
565 * Update the tty layer's idea of the carrier bit, in case we changed
566 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
569 (void) (*linesw[tp->t_line].l_modem)(tp, 1 /* XXX carrier */ );
575 umodemopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
577 int unit = UMODEMUNIT(dev);
578 struct umodem_softc *sc;
584 USB_GET_SC_OPEN(umodem, unit, sc);
589 #if defined(__NetBSD__) || defined(__OpenBBSD__)
590 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
596 DPRINTF(("%s: umodemopen: tp=%p\n", USBDEVNAME(sc->sc_dev), tp));
598 if (ISSET(tp->t_state, TS_ISOPEN) &&
599 ISSET(tp->t_state, TS_XCLUDE) &&
604 * Do the following iff this is a first open.
607 while (sc->sc_opening)
608 tsleep(&sc->sc_opening, PRIBIO, "umdmop", 0);
611 #if defined(__NetBSD__) || defined(__OpenBSD__)
612 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
613 #elif defined(__FreeBSD__)
614 if (!ISSET(tp->t_state, TS_ISOPEN)) {
621 * Initialize the termios status to the defaults. Add in the
622 * sticky bits from TIOCSFLAGS.
625 t.c_ospeed = TTYDEF_SPEED;
626 t.c_cflag = TTYDEF_CFLAG;
627 /* Make sure umodemparam() will do something. */
629 (void) umodemparam(tp, &t);
630 tp->t_iflag = TTYDEF_IFLAG;
631 tp->t_oflag = TTYDEF_OFLAG;
632 tp->t_lflag = TTYDEF_LFLAG;
637 * Turn on DTR. We must always do this, even if carrier is not
638 * present, because otherwise we'd have to use TIOCSDTR
639 * immediately after setting CLOCAL, which applications do not
640 * expect. We always assert DTR while the device is open
641 * unless explicitly requested to deassert it.
645 DPRINTF(("umodemopen: open pipes\n"));
647 /* Open the bulk pipes */
648 err = usbd_open_pipe(sc->sc_data_iface, sc->sc_bulkin_no, 0,
649 &sc->sc_bulkin_pipe);
651 DPRINTF(("%s: cannot open bulk out pipe (addr %d)\n",
652 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no));
655 err = usbd_open_pipe(sc->sc_data_iface, sc->sc_bulkout_no,
656 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
658 DPRINTF(("%s: cannot open bulk in pipe (addr %d)\n",
659 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no));
660 usbd_close_pipe(sc->sc_bulkin_pipe);
664 /* Allocate a request and an input buffer and start reading. */
665 sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
666 if (sc->sc_ixfer == 0) {
667 usbd_close_pipe(sc->sc_bulkin_pipe);
668 usbd_close_pipe(sc->sc_bulkout_pipe);
671 sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
672 if (sc->sc_oxfer == 0) {
673 usbd_close_pipe(sc->sc_bulkin_pipe);
674 usbd_close_pipe(sc->sc_bulkout_pipe);
675 usbd_free_xfer(sc->sc_ixfer);
678 sc->sc_ibuf = malloc(UMODEMIBUFSIZE, M_USBDEV, M_WAITOK);
682 wakeup(&sc->sc_opening);
685 #if defined(__NetBSD__) || defined(__OpenBSD__)
686 error = ttyopen(tp, UMODEMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
689 #elif defined(__FreeBSD__)
690 error = ttyopen(dev, tp);
695 error = (*linesw[tp->t_line].l_open)(dev, tp);
702 #if defined(__NetBSD__) || defined(__OpenBSD__)
703 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
704 #elif defined(__FreeBSD__)
705 if (!ISSET(tp->t_state, TS_ISOPEN)) {
708 * We failed to open the device, and nobody else had it opened.
709 * Clean up the state as appropriate.
718 umodemstartread(struct umodem_softc *sc)
722 DPRINTFN(5,("umodemstartread: start\n"));
723 usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
724 (usbd_private_handle)sc,
725 sc->sc_ibuf, UMODEMIBUFSIZE, USBD_SHORT_XFER_OK,
726 USBD_NO_TIMEOUT, umodemreadcb);
728 err = usbd_transfer(sc->sc_ixfer);
729 if (err != USBD_IN_PROGRESS)
732 return (USBD_NORMAL_COMPLETION);
736 umodemreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
738 struct umodem_softc *sc = (struct umodem_softc *)p;
739 struct tty *tp = sc->sc_tty;
740 int (*rint) (int c, struct tty *tp) = linesw[tp->t_line].l_rint;
746 if (status == USBD_CANCELLED)
749 if (status != USBD_NORMAL_COMPLETION) {
750 DPRINTF(("umodemreadcb: status=%d\n", status));
751 if (status == USBD_STALLED)
752 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
753 /* XXX we should restart after some delay. */
757 usbd_get_xfer_status(xfer, 0, (void **)&cp, &cc, 0);
758 DPRINTFN(5,("umodemreadcb: got %d chars, tp=%p\n", cc, tp));
760 /* Give characters to tty layer. */
762 DPRINTFN(7,("umodemreadcb: char=0x%02x\n", *cp));
763 if ((*rint)(*cp++, tp) == -1) {
764 /* XXX what should we do? */
770 err = umodemstartread(sc);
772 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
773 /* XXX what should we dow now? */
778 umodemclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
780 struct umodem_softc *sc;
784 USB_GET_SC(umodem, UMODEMUNIT(dev), sc);
788 DPRINTF(("%s: umodemclose sc_tty=%p\n", USBDEVNAME(sc->sc_dev), tp));
790 if (!ISSET(tp->t_state, TS_ISOPEN))
793 DPRINTF(("%s: umodemclose lclose(%p,%d)\n", USBDEVNAME(sc->sc_dev), tp,flag));
796 DPRINTF(("%s: umodemclose lclose=%p\n", USBDEVNAME(sc->sc_dev), linesw[tp->t_line].l_close));
797 (*linesw[tp->t_line].l_close)(tp, flag);
799 DPRINTF(("%s: umodemclose ttyclose(%p)\n", USBDEVNAME(sc->sc_dev), tp));
803 DPRINTF(("%s: umodemclose sc->sc_dying=%d\n", USBDEVNAME(sc->sc_dev), sc->sc_dying));
807 DPRINTF(("%s: umodemclose tp->t_state=%d\n", USBDEVNAME(sc->sc_dev), tp->t_state));
808 #if defined(__NetBSD__) || defined(__OpenBSD__)
809 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
810 #elif defined(__FreeBSD__)
811 if (!ISSET(tp->t_state, TS_ISOPEN)) {
814 * Although we got a last close, the device may still be in
815 * use; e.g. if this was the dialout node, and there are still
816 * processes waiting for carrier on the non-dialout node.
818 DPRINTF(("%s: umodemclose umodem_cleanup(%p)\n", USBDEVNAME(sc->sc_dev), sc));
821 DPRINTF(("%s: umodemclose return\n", USBDEVNAME(sc->sc_dev)));
827 umodem_cleanup(struct umodem_softc *sc)
831 DPRINTFN(5, ("%s: umodem_cleanup: closing pipes\n",
832 USBDEVNAME(sc->sc_dev)));
834 usbd_abort_pipe(sc->sc_bulkin_pipe);
835 usbd_close_pipe(sc->sc_bulkin_pipe);
836 usbd_abort_pipe(sc->sc_bulkout_pipe);
837 usbd_close_pipe(sc->sc_bulkout_pipe);
838 usbd_free_xfer(sc->sc_ixfer);
839 usbd_free_xfer(sc->sc_oxfer);
840 free(sc->sc_ibuf, M_USBDEV);
844 umodemread(dev_t dev, struct uio *uio, int flag)
846 struct umodem_softc *sc;
849 USB_GET_SC(umodem, UMODEMUNIT(dev), sc);
856 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
860 umodemwrite(dev_t dev, struct uio *uio, int flag)
862 struct umodem_softc *sc;
865 USB_GET_SC(umodem, UMODEMUNIT(dev), sc);
872 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
876 umodemstop(struct tty *tp, int flag)
878 struct umodem_softc *sc;
881 USB_GET_SC(umodem, UMODEMUNIT(tp->t_dev), sc);
883 DPRINTF(("umodemstop: %d\n", flag));
885 if (ISSET(tp->t_state, TS_BUSY)) {
886 DPRINTF(("umodemstop: XXX\n"));
888 if (!ISSET(tp->t_state, TS_TTSTOP))
889 SET(tp->t_state, TS_FLUSH);
897 struct umodem_softc *sc;
900 USB_GET_SC(umodem, UMODEMUNIT(dev), sc);
908 umodemioctl(dev_t dev, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
910 struct umodem_softc *sc;
916 USB_GET_SC(umodem, UMODEMUNIT(dev), sc);
923 DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
925 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
929 #if defined(__NetBSD__) || defined(__OpenBSD__)
930 error = ttioctl(tp, cmd, data, flag, p);
931 #elif defined(__FreeBSD__)
932 error = ttioctl(tp, cmd, data, flag);
937 DPRINTF(("umodemioctl: our cmd=0x%08lx\n", cmd));
967 case USB_GET_CM_OVER_DATA:
968 *(int *)data = sc->sc_cm_over_data;
971 case USB_SET_CM_OVER_DATA:
972 if (*(int *)data != sc->sc_cm_over_data) {
978 DPRINTF(("umodemioctl: unknown\n"));
989 umodem_shutdown(struct umodem_softc *sc)
991 struct tty *tp = sc->sc_tty;
993 DPRINTF(("%s: umodem_shutdown\n", USBDEVNAME(sc->sc_dev)));
995 * Hang up if necessary. Wait a bit, so the other side has time to
996 * notice even if we immediately open the port again.
998 if (ISSET(tp->t_cflag, HUPCL)) {
1000 #if defined(__NetBSD__) || defined(__OpenBSD__)
1001 (void) tsleep(sc, TTIPRI, ttclos, hz);
1002 #elif defined(__FreeBSD__)
1003 (void) tsleep(sc, TTIPRI, "umdmsd", hz);
1009 umodem_modem(struct umodem_softc *sc, int onoff)
1011 usb_device_request_t req;
1013 DPRINTF(("%s: umodem_modem: onoff=%d\n", USBDEVNAME(sc->sc_dev),onoff));
1015 if (sc->sc_dtr == onoff)
1018 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1019 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1020 USETW(req.wValue, onoff ? UCDC_LINE_DTR : 0);
1021 USETW(req.wIndex, sc->sc_ctl_iface_no);
1022 USETW(req.wLength, 0);
1024 (void)usbd_do_request(sc->sc_udev, &req, 0);
1030 umodem_break(struct umodem_softc *sc, int onoff)
1032 usb_device_request_t req;
1034 DPRINTF(("%s: umodem_break: onoff=%d\n", USBDEVNAME(sc->sc_dev),onoff));
1036 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
1039 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1040 req.bRequest = UCDC_SEND_BREAK;
1041 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
1042 USETW(req.wIndex, sc->sc_ctl_iface_no);
1043 USETW(req.wLength, 0);
1045 (void)usbd_do_request(sc->sc_udev, &req, 0);
1049 umodem_get_desc(usbd_device_handle dev, int type, int subtype)
1051 usb_descriptor_t *desc;
1052 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
1053 uByte *p = (uByte *)cd;
1054 uByte *end = p + UGETW(cd->wTotalLength);
1057 desc = (usb_descriptor_t *)p;
1058 if (desc->bDescriptorType == type &&
1059 desc->bDescriptorSubtype == subtype)
1068 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
1070 usb_device_request_t req;
1072 usb_cdc_abstract_state_t ast;
1074 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1075 req.bRequest = UCDC_SET_COMM_FEATURE;
1076 USETW(req.wValue, feature);
1077 USETW(req.wIndex, sc->sc_ctl_iface_no);
1078 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
1079 USETW(ast.wState, state);
1081 err = usbd_do_request(sc->sc_udev, &req, &ast);
1083 DPRINTF(("%s: umodem_set_comm_feat: feature=%d failed, err=%d\n",
1084 USBDEVNAME(sc->sc_dev), feature, err));
1088 return (USBD_NORMAL_COMPLETION);
1092 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
1094 usb_device_request_t req;
1097 DPRINTF(("%s: umodem_set_line_cod: rate=%d fmt=%d parity=%d bits=%d\n",
1098 USBDEVNAME(sc->sc_dev), UGETDW(state->dwDTERate),
1099 state->bCharFormat, state->bParityType, state->bDataBits));
1101 if (bcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
1102 DPRINTF(("%s: umodem_set_line_coding: already set\n",
1103 USBDEVNAME(sc->sc_dev)));
1104 return (USBD_NORMAL_COMPLETION);
1107 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1108 req.bRequest = UCDC_SET_LINE_CODING;
1109 USETW(req.wValue, 0);
1110 USETW(req.wIndex, sc->sc_ctl_iface_no);
1111 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1113 err = usbd_do_request(sc->sc_udev, &req, state);
1115 DPRINTF(("%s: umodem_set_line_coding: failed, err=%d\n",
1116 USBDEVNAME(sc->sc_dev), err));
1120 sc->sc_line_state = *state;
1122 return (USBD_NORMAL_COMPLETION);
1125 #if defined(__NetBSD__) || defined(__OpenBSD__)
1127 umodem_activate(device_ptr_t self, enum devact act)
1129 struct umodem_softc *sc = (struct umodem_softc *)self;
1132 case DVACT_ACTIVATE:
1133 return (EOPNOTSUPP);
1136 case DVACT_DEACTIVATE:
1146 USB_DETACH_START(umodem, sc);
1147 #if defined(__NetBSD__) || defined(__OpenBSD__)
1150 DPRINTF(("umodem_detach: sc=%p flags=%d tp=%p\n",
1151 sc, flags, sc->sc_tty));
1152 #elif defined(__FreeBSD__)
1153 DPRINTF(("umodem_detach: sc=%p flags=%d, tp=%p\n",
1154 sc, 0, sc->sc_tty));
1160 if (sc->sc_tty == 0) {
1161 DPRINTF(("umodem_detach: no tty\n"));
1166 /* use refernce count? XXX */
1168 #if defined(__NetBSD__) || defined(__OpenBSD__)
1169 /* locate the major number */
1170 for (maj = 0; maj < nchrdev; maj++)
1171 if (cdevsw[maj].d_open == umodemopen)
1174 /* Nuke the vnodes for any open instances. */
1176 vdevgone(maj, mn, mn, VCHR);
1177 vdevgone(maj, mn, mn | UMODEMDIALOUT_MASK, VCHR);
1178 vdevgone(maj, mn, mn | UMODEMCALLUNIT_MASK, VCHR);
1179 #elif defined(__FreeBSD__)
1180 /* XXX not yet implemented */
1183 #if defined(__FreeBSD__)
1184 destroy_dev(sc->dev);
1187 /* Detach and free the tty. */
1188 #if defined(__NetBSD__) || defined(__OpenBSD__)
1189 tty_detach(sc->sc_tty);
1190 ttyfree(sc->sc_tty);
1197 #if defined(__FreeBSD__)
1198 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, usbd_driver_load,0);