umodem.c revision 1.17 1 /* $NetBSD: umodem.c,v 1.17 1999/11/16 10:21:11 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Comm Class spec: http://www.usb.org/developers/data/usbcdc11.pdf
42 */
43
44 /*
45 * TODO:
46 * - Add error recovery in various places; the big problem is what
47 * to do in a callback if there is an error.
48 * - Implement a Call Device for modems without multiplexed commands.
49 *
50 */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/ioctl.h>
57 #include <sys/conf.h>
58 #include <sys/tty.h>
59 #include <sys/file.h>
60 #include <sys/select.h>
61 #include <sys/proc.h>
62 #include <sys/vnode.h>
63 #include <sys/device.h>
64 #include <sys/poll.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbcdc.h>
68
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
72 #include <dev/usb/usb_quirks.h>
73
74 #include <dev/usb/usbdevs.h>
75
76 #ifdef USB_DEBUG
77 #define DPRINTFN(n, x) if (umodemdebug > (n)) logprintf x
78 int umodemdebug = 0;
79 #else
80 #define DPRINTFN(n, x)
81 #endif
82 #define DPRINTF(x) DPRINTFN(0, x)
83
84 /* Macros to clear/set/test flags. */
85 #define SET(t, f) (t) |= (f)
86 #define CLR(t, f) (t) &= ~((unsigned)(f))
87 #define ISSET(t, f) ((t) & (f))
88
89 #define UMODEMUNIT_MASK 0x3ffff
90 #define UMODEMDIALOUT_MASK 0x80000
91 #define UMODEMCALLUNIT_MASK 0x40000
92
93 #define UMODEMUNIT(x) (minor(x) & UMODEMUNIT_MASK)
94 #define UMODEMDIALOUT(x) (minor(x) & UMODEMDIALOUT_MASK)
95 #define UMODEMCALLUNIT(x) (minor(x) & UMODEMCALLUNIT_MASK)
96
97 /*
98 * These are the maximum number of bytes transferred per frame.
99 * If some really high speed devices should use this driver they
100 * may need to be increased, but this is good enough for modems.
101 */
102 #define UMODEMIBUFSIZE 64
103 #define UMODEMOBUFSIZE 256
104
105 struct umodem_softc {
106 USBBASEDEVICE sc_dev; /* base device */
107
108 usbd_device_handle sc_udev; /* USB device */
109
110 int sc_ctl_iface_no;
111 usbd_interface_handle sc_ctl_iface; /* control interface */
112 int sc_data_iface_no;
113 usbd_interface_handle sc_data_iface; /* data interface */
114
115 int sc_bulkin_no; /* bulk in endpoint address */
116 usbd_pipe_handle sc_bulkin_pipe; /* bulk in pipe */
117 usbd_xfer_handle sc_ixfer; /* read request */
118 u_char *sc_ibuf; /* read buffer */
119
120 int sc_bulkout_no; /* bulk out endpoint address */
121 usbd_pipe_handle sc_bulkout_pipe;/* bulk out pipe */
122 usbd_xfer_handle sc_oxfer; /* write request */
123 u_char *sc_obuf; /* write buffer */
124
125 int sc_cm_cap; /* CM capabilities */
126 int sc_acm_cap; /* ACM capabilities */
127
128 int sc_cm_over_data;
129
130 struct tty *sc_tty; /* our tty */
131
132 usb_cdc_line_state_t sc_line_state; /* current line state */
133 u_char sc_dtr; /* current DTR state */
134
135 u_char sc_opening; /* lock during open */
136 u_char sc_dying; /* disconnecting */
137 };
138
139 cdev_decl(umodem);
140
141 void *umodem_get_desc
142 __P((usbd_device_handle dev, int type, int subtype));
143 usbd_status umodem_set_comm_feature
144 __P((struct umodem_softc *sc, int feature, int state));
145 usbd_status umodem_set_line_coding
146 __P((struct umodem_softc *sc, usb_cdc_line_state_t *state));
147
148 void umodem_get_caps __P((usbd_device_handle, int *, int *));
149 void umodem_cleanup __P((struct umodem_softc *));
150 int umodemparam __P((struct tty *, struct termios *));
151 void umodemstart __P((struct tty *));
152 void umodem_shutdown __P((struct umodem_softc *));
153 void umodem_modem __P((struct umodem_softc *, int));
154 void umodem_break __P((struct umodem_softc *, int));
155 usbd_status umodemstartread __P((struct umodem_softc *));
156 void umodemreadcb __P((usbd_xfer_handle, usbd_private_handle,
157 usbd_status status));
158 void umodemwritecb __P((usbd_xfer_handle, usbd_private_handle,
159 usbd_status status));
160
161 USB_DECLARE_DRIVER(umodem);
162
163 USB_MATCH(umodem)
164 {
165 USB_MATCH_START(umodem, uaa);
166 usb_interface_descriptor_t *id;
167 int cm, acm;
168
169 if (!uaa->iface)
170 return (UMATCH_NONE);
171
172 id = usbd_get_interface_descriptor(uaa->iface);
173 if (id == 0 ||
174 id->bInterfaceClass != UCLASS_CDC ||
175 id->bInterfaceSubClass != USUBCLASS_ABSTRACT_CONTROL_MODEL ||
176 id->bInterfaceProtocol != UPROTO_CDC_AT)
177 return (UMATCH_NONE);
178
179 umodem_get_caps(uaa->device, &cm, &acm);
180 if (!(cm & USB_CDC_CM_DOES_CM) ||
181 !(cm & USB_CDC_CM_OVER_DATA) ||
182 !(acm & USB_CDC_ACM_HAS_LINE))
183 return (UMATCH_NONE);
184
185 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
186 }
187
188 USB_ATTACH(umodem)
189 {
190 USB_ATTACH_START(umodem, sc, uaa);
191 usbd_device_handle dev = uaa->device;
192 usb_interface_descriptor_t *id;
193 usb_endpoint_descriptor_t *ed;
194 usb_cdc_cm_descriptor_t *cmd;
195 char devinfo[1024];
196 usbd_status r;
197 int data_ifaceno;
198 int i;
199 struct tty *tp;
200
201 usbd_devinfo(uaa->device, 0, devinfo);
202 USB_ATTACH_SETUP;
203
204 sc->sc_udev = dev;
205
206 sc->sc_ctl_iface = uaa->iface;
207 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
208 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
209 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
210 sc->sc_ctl_iface_no = id->bInterfaceNumber;
211
212 umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
213
214 /* Get the data interface no. */
215 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
216 if (!cmd) {
217 DPRINTF(("%s: no CM desc\n", USBDEVNAME(sc->sc_dev)));
218 goto bad;
219 }
220 sc->sc_data_iface_no = data_ifaceno = cmd->bDataInterface;
221
222 printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
223 USBDEVNAME(sc->sc_dev), data_ifaceno,
224 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
225 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
226
227
228 /* Get the data interface too. */
229 for (i = 0; i < uaa->nifaces; i++) {
230 if (uaa->ifaces[i]) {
231 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
232 if (id->bInterfaceNumber == data_ifaceno) {
233 sc->sc_data_iface = uaa->ifaces[i];
234 uaa->ifaces[i] = 0;
235 }
236 }
237 }
238 if (!sc->sc_data_iface) {
239 printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
240 goto bad;
241 }
242
243 /*
244 * Find the bulk endpoints.
245 * Iterate over all endpoints in the data interface and take note.
246 */
247 sc->sc_bulkin_no = sc->sc_bulkout_no = -1;
248
249 id = usbd_get_interface_descriptor(sc->sc_data_iface);
250 for (i = 0; i < id->bNumEndpoints; i++) {
251 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
252 if (!ed) {
253 printf("%s: no endpoint descriptor for %d\n",
254 USBDEVNAME(sc->sc_dev), i);
255 goto bad;
256 }
257 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
258 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
259 sc->sc_bulkin_no = ed->bEndpointAddress;
260 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
261 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
262 sc->sc_bulkout_no = ed->bEndpointAddress;
263 }
264 }
265
266 if (sc->sc_bulkin_no == -1) {
267 DPRINTF(("%s: Could not find data bulk in\n",
268 USBDEVNAME(sc->sc_dev)));
269 goto bad;
270 }
271 if (sc->sc_bulkout_no == -1) {
272 DPRINTF(("%s: Could not find data bulk out\n",
273 USBDEVNAME(sc->sc_dev)));
274 goto bad;
275 }
276
277 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
278 r = umodem_set_comm_feature(sc, UCDC_ABSTRACT_STATE,
279 UCDC_DATA_MULTIPLEXED);
280 if (r != USBD_NORMAL_COMPLETION)
281 goto bad;
282 sc->sc_cm_over_data = 1;
283 }
284
285 tp = ttymalloc();
286 tp->t_oproc = umodemstart;
287 tp->t_param = umodemparam;
288 sc->sc_tty = tp;
289 DPRINTF(("umodem_attach: tty_attach %p\n", tp));
290 tty_attach(tp);
291
292 sc->sc_dtr = -1;
293
294 USB_ATTACH_SUCCESS_RETURN;
295
296 bad:
297 sc->sc_dying = 1;
298 USB_ATTACH_ERROR_RETURN;
299 }
300
301 void
302 umodem_get_caps(dev, cm, acm)
303 usbd_device_handle dev;
304 int *cm, *acm;
305 {
306 usb_cdc_cm_descriptor_t *cmd;
307 usb_cdc_acm_descriptor_t *cad;
308
309 *cm = *acm = 0;
310
311 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
312 if (!cmd) {
313 DPRINTF(("umodem_get_desc: no CM desc\n"));
314 return;
315 }
316 *cm = cmd->bmCapabilities;
317
318 cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
319 if (!cad) {
320 DPRINTF(("umodem_get_desc: no ACM desc\n"));
321 return;
322 }
323 *acm = cad->bmCapabilities;
324 }
325
326 void
327 umodemstart(tp)
328 struct tty *tp;
329 {
330 struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(tp->t_dev)];
331 int s;
332 u_char *data;
333 int cnt;
334
335 if (sc->sc_dying)
336 return;
337
338 s = spltty();
339 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
340 DPRINTFN(4,("umodemstart: stopped\n"));
341 goto out;
342 }
343
344 if (tp->t_outq.c_cc <= tp->t_lowat) {
345 if (ISSET(tp->t_state, TS_ASLEEP)) {
346 CLR(tp->t_state, TS_ASLEEP);
347 wakeup(&tp->t_outq);
348 }
349 selwakeup(&tp->t_wsel);
350 if (tp->t_outq.c_cc == 0)
351 goto out;
352 }
353
354 /* Grab the first contiguous region of buffer space. */
355 data = tp->t_outq.c_cf;
356 cnt = ndqb(&tp->t_outq, 0);
357
358 if (cnt == 0) {
359 DPRINTF(("umodemstart: cnt==0\n"));
360 splx(s);
361 return;
362 }
363
364 SET(tp->t_state, TS_BUSY);
365
366 /* XXX Is it really correct not to transfer all of the buffer? */
367 if (cnt > UMODEMOBUFSIZE) {
368 DPRINTF(("umodemstart: big buffer %d chars\n", cnt));
369 cnt = UMODEMOBUFSIZE;
370 }
371
372 memcpy(sc->sc_obuf, data, cnt);
373
374 DPRINTFN(4,("umodemstart: %d chars\n", cnt));
375 /* XXX what can we do on error? */
376 usbd_setup_request(sc->sc_oxfer, sc->sc_bulkout_pipe,
377 (usbd_private_handle)sc, sc->sc_obuf, cnt,
378 USBD_NO_COPY, USBD_NO_TIMEOUT, umodemwritecb);
379 (void)usbd_transfer(sc->sc_oxfer);
380
381 out:
382 splx(s);
383 }
384
385 void
386 umodemwritecb(xfer, p, status)
387 usbd_xfer_handle xfer;
388 usbd_private_handle p;
389 usbd_status status;
390 {
391 struct umodem_softc *sc = (struct umodem_softc *)p;
392 struct tty *tp = sc->sc_tty;
393 u_int32_t cc;
394 int s;
395
396 DPRINTFN(5,("umodemwritecb: status=%d\n", status));
397
398 if (status == USBD_CANCELLED)
399 return;
400
401 if (status != USBD_NORMAL_COMPLETION) {
402 DPRINTF(("umodemwritecb: status=%d\n", status));
403 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
404 /* XXX we should restart after some delay. */
405 return;
406 }
407
408 usbd_get_request_status(xfer, 0, 0, &cc, 0);
409 DPRINTFN(5,("umodemwritecb: cc=%d\n", cc));
410
411 s = spltty();
412 CLR(tp->t_state, TS_BUSY);
413 if (ISSET(tp->t_state, TS_FLUSH))
414 CLR(tp->t_state, TS_FLUSH);
415 else
416 ndflush(&tp->t_outq, cc);
417 (*linesw[tp->t_line].l_start)(tp);
418 splx(s);
419 }
420
421 int
422 umodemparam(tp, t)
423 struct tty *tp;
424 struct termios *t;
425 {
426 struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(tp->t_dev)];
427 usb_cdc_line_state_t ls;
428
429 if (sc->sc_dying)
430 return (EIO);
431
432 /* Check requested parameters. */
433 if (t->c_ospeed < 0)
434 return (EINVAL);
435 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
436 return (EINVAL);
437
438 /*
439 * If there were no changes, don't do anything. This avoids dropping
440 * input and improves performance when all we did was frob things like
441 * VMIN and VTIME.
442 */
443 if (tp->t_ospeed == t->c_ospeed &&
444 tp->t_cflag == t->c_cflag)
445 return (0);
446
447 /* And copy to tty. */
448 tp->t_ispeed = 0;
449 tp->t_ospeed = t->c_ospeed;
450 tp->t_cflag = t->c_cflag;
451
452 USETDW(ls.dwDTERate, t->c_ospeed);
453 if (ISSET(t->c_cflag, CSTOPB))
454 ls.bCharFormat = UCDC_STOP_BIT_2;
455 else
456 ls.bCharFormat = UCDC_STOP_BIT_1;
457 if (ISSET(t->c_cflag, PARENB)) {
458 if (ISSET(t->c_cflag, PARODD))
459 ls.bParityType = UCDC_PARITY_ODD;
460 else
461 ls.bParityType = UCDC_PARITY_EVEN;
462 } else
463 ls.bParityType = UCDC_PARITY_NONE;
464 switch (ISSET(t->c_cflag, CSIZE)) {
465 case CS5:
466 ls.bDataBits = 5;
467 break;
468 case CS6:
469 ls.bDataBits = 6;
470 break;
471 case CS7:
472 ls.bDataBits = 7;
473 break;
474 case CS8:
475 ls.bDataBits = 8;
476 break;
477 }
478 /* XXX what can we if it fails? */
479 (void)umodem_set_line_coding(sc, &ls);
480
481 /*
482 * Update the tty layer's idea of the carrier bit, in case we changed
483 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
484 * explicit request.
485 */
486 (void) (*linesw[tp->t_line].l_modem)(tp, 1 /* XXX carrier */ );
487
488 return (0);
489 }
490
491 int
492 umodemopen(dev, flag, mode, p)
493 dev_t dev;
494 int flag, mode;
495 struct proc *p;
496 {
497 int unit = UMODEMUNIT(dev);
498 usbd_status r;
499 struct umodem_softc *sc;
500 struct tty *tp;
501 int s;
502 int error;
503
504 if (unit >= umodem_cd.cd_ndevs)
505 return (ENXIO);
506 sc = umodem_cd.cd_devs[unit];
507 if (sc == 0)
508 return (ENXIO);
509
510 if (sc->sc_dying)
511 return (EIO);
512
513 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
514 return (ENXIO);
515
516 tp = sc->sc_tty;
517
518 DPRINTF(("umodemopen: unit=%d, tp=%p\n", unit, tp));
519
520 if (ISSET(tp->t_state, TS_ISOPEN) &&
521 ISSET(tp->t_state, TS_XCLUDE) &&
522 p->p_ucred->cr_uid != 0)
523 return (EBUSY);
524
525 /*
526 * Do the following iff this is a first open.
527 */
528 s = spltty();
529 while (sc->sc_opening)
530 tsleep(&sc->sc_opening, PRIBIO, "umdmop", 0);
531 sc->sc_opening = 1;
532
533 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
534 struct termios t;
535
536 tp->t_dev = dev;
537
538 /*
539 * Initialize the termios status to the defaults. Add in the
540 * sticky bits from TIOCSFLAGS.
541 */
542 t.c_ispeed = 0;
543 t.c_ospeed = TTYDEF_SPEED;
544 t.c_cflag = TTYDEF_CFLAG;
545 /* Make sure umodemparam() will do something. */
546 tp->t_ospeed = 0;
547 (void) umodemparam(tp, &t);
548 tp->t_iflag = TTYDEF_IFLAG;
549 tp->t_oflag = TTYDEF_OFLAG;
550 tp->t_lflag = TTYDEF_LFLAG;
551 ttychars(tp);
552 ttsetwater(tp);
553
554 /*
555 * Turn on DTR. We must always do this, even if carrier is not
556 * present, because otherwise we'd have to use TIOCSDTR
557 * immediately after setting CLOCAL, which applications do not
558 * expect. We always assert DTR while the device is open
559 * unless explicitly requested to deassert it.
560 */
561 umodem_modem(sc, 1);
562
563 DPRINTF(("umodemopen: open pipes\n"));
564
565 /* Open the bulk pipes */
566 r = usbd_open_pipe(sc->sc_data_iface, sc->sc_bulkin_no, 0,
567 &sc->sc_bulkin_pipe);
568 if (r != USBD_NORMAL_COMPLETION) {
569 DPRINTF(("%s: cannot open bulk out pipe (addr %d)\n",
570 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no));
571 return (EIO);
572 }
573 r = usbd_open_pipe(sc->sc_data_iface, sc->sc_bulkout_no,
574 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
575 if (r != USBD_NORMAL_COMPLETION) {
576 DPRINTF(("%s: cannot open bulk in pipe (addr %d)\n",
577 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no));
578 usbd_close_pipe(sc->sc_bulkin_pipe);
579 return (EIO);
580 }
581
582 /* Allocate a request and an input buffer and start reading. */
583 sc->sc_ixfer = usbd_alloc_request(sc->sc_udev);
584 if (sc->sc_ixfer == NULL) {
585 usbd_close_pipe(sc->sc_bulkin_pipe);
586 usbd_close_pipe(sc->sc_bulkout_pipe);
587 return (ENOMEM);
588 }
589 sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer, UMODEMIBUFSIZE);
590 if (sc->sc_ibuf == NULL) {
591 usbd_free_request(sc->sc_ixfer);
592 usbd_close_pipe(sc->sc_bulkin_pipe);
593 usbd_close_pipe(sc->sc_bulkout_pipe);
594 return (ENOMEM);
595 }
596
597 sc->sc_oxfer = usbd_alloc_request(sc->sc_udev);
598 if (sc->sc_oxfer == NULL) {
599 usbd_free_request(sc->sc_ixfer);
600 usbd_close_pipe(sc->sc_bulkin_pipe);
601 usbd_close_pipe(sc->sc_bulkout_pipe);
602 return (ENOMEM);
603 }
604 sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer, UMODEMOBUFSIZE);
605 if (sc->sc_obuf == NULL) {
606 usbd_free_request(sc->sc_oxfer);
607 usbd_free_request(sc->sc_ixfer);
608 usbd_close_pipe(sc->sc_bulkin_pipe);
609 usbd_close_pipe(sc->sc_bulkout_pipe);
610 return (ENOMEM);
611 }
612
613 umodemstartread(sc);
614 }
615 sc->sc_opening = 0;
616 wakeup(&sc->sc_opening);
617 splx(s);
618
619 error = ttyopen(tp, UMODEMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
620 if (error)
621 goto bad;
622
623 error = (*linesw[tp->t_line].l_open)(dev, tp);
624 if (error)
625 goto bad;
626
627 return (0);
628
629 bad:
630 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
631 /*
632 * We failed to open the device, and nobody else had it opened.
633 * Clean up the state as appropriate.
634 */
635 umodem_cleanup(sc);
636 }
637
638 return (error);
639 }
640
641 usbd_status
642 umodemstartread(sc)
643 struct umodem_softc *sc;
644 {
645 usbd_status r;
646
647 DPRINTFN(5,("umodemstartread: start\n"));
648 usbd_setup_request(sc->sc_ixfer, sc->sc_bulkin_pipe,
649 (usbd_private_handle)sc,
650 sc->sc_ibuf, UMODEMIBUFSIZE,
651 USBD_SHORT_XFER_OK | USBD_NO_COPY,
652 USBD_NO_TIMEOUT, umodemreadcb);
653 r = usbd_transfer(sc->sc_ixfer);
654 if (r != USBD_IN_PROGRESS)
655 return (r);
656 return (USBD_NORMAL_COMPLETION);
657 }
658
659 void
660 umodemreadcb(xfer, p, status)
661 usbd_xfer_handle xfer;
662 usbd_private_handle p;
663 usbd_status status;
664 {
665 struct umodem_softc *sc = (struct umodem_softc *)p;
666 struct tty *tp = sc->sc_tty;
667 int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
668 usbd_status r;
669 u_int32_t cc;
670 u_char *cp;
671 int s;
672
673 if (status == USBD_CANCELLED)
674 return;
675
676 if (status != USBD_NORMAL_COMPLETION) {
677 DPRINTF(("umodemreadcb: status=%d\n", status));
678 usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
679 /* XXX we should restart after some delay. */
680 return;
681 }
682
683 usbd_get_request_status(xfer, 0, (void **)&cp, &cc, 0);
684 DPRINTFN(5,("umodemreadcb: got %d chars, tp=%p\n", cc, tp));
685 s = spltty();
686 /* Give characters to tty layer. */
687 while (cc-- > 0) {
688 DPRINTFN(7,("umodemreadcb: char=0x%02x\n", *cp));
689 if ((*rint)(*cp++, tp) == -1) {
690 /* XXX what should we do? */
691 break;
692 }
693 }
694 splx(s);
695
696 r = umodemstartread(sc);
697 if (r != USBD_NORMAL_COMPLETION) {
698 printf("%s: read start failed\n", USBDEVNAME(sc->sc_dev));
699 /* XXX what should we dow now? */
700 }
701 }
702
703 int
704 umodemclose(dev, flag, mode, p)
705 dev_t dev;
706 int flag, mode;
707 struct proc *p;
708 {
709 struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(dev)];
710 struct tty *tp = sc->sc_tty;
711
712 DPRINTF(("umodemclose: unit=%d\n", UMODEMUNIT(dev)));
713 if (!ISSET(tp->t_state, TS_ISOPEN))
714 return (0);
715
716 (*linesw[tp->t_line].l_close)(tp, flag);
717 ttyclose(tp);
718
719 if (sc->sc_dying)
720 return (0);
721
722 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
723 /*
724 * Although we got a last close, the device may still be in
725 * use; e.g. if this was the dialout node, and there are still
726 * processes waiting for carrier on the non-dialout node.
727 */
728 umodem_cleanup(sc);
729 }
730
731 return (0);
732 }
733
734 void
735 umodem_cleanup(sc)
736 struct umodem_softc *sc;
737 {
738 umodem_shutdown(sc);
739 DPRINTF(("umodem_cleanup: closing pipes\n"));
740 usbd_abort_pipe(sc->sc_bulkin_pipe);
741 usbd_close_pipe(sc->sc_bulkin_pipe);
742 usbd_abort_pipe(sc->sc_bulkout_pipe);
743 usbd_close_pipe(sc->sc_bulkout_pipe);
744 usbd_free_request(sc->sc_ixfer);
745 usbd_free_request(sc->sc_oxfer);
746 }
747
748 int
749 umodemread(dev, uio, flag)
750 dev_t dev;
751 struct uio *uio;
752 int flag;
753 {
754 struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(dev)];
755 struct tty *tp = sc->sc_tty;
756
757 if (sc->sc_dying)
758 return (EIO);
759
760 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
761 }
762
763 int
764 umodemwrite(dev, uio, flag)
765 dev_t dev;
766 struct uio *uio;
767 int flag;
768 {
769 struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(dev)];
770 struct tty *tp = sc->sc_tty;
771
772 if (sc->sc_dying)
773 return (EIO);
774
775 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
776 }
777
778 void
779 umodemstop(tp, flag)
780 struct tty *tp;
781 int flag;
782 {
783 /*struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(tp->t_dev)];*/
784 int s;
785
786 DPRINTF(("umodemstop: %d\n", flag));
787 s = spltty();
788 if (ISSET(tp->t_state, TS_BUSY)) {
789 DPRINTF(("umodemstop: XXX\n"));
790 /* XXX do what? */
791 if (!ISSET(tp->t_state, TS_TTSTOP))
792 SET(tp->t_state, TS_FLUSH);
793 }
794 splx(s);
795 }
796
797 struct tty *
798 umodemtty(dev)
799 dev_t dev;
800 {
801 struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(dev)];
802 struct tty *tp = sc->sc_tty;
803
804 return (tp);
805 }
806
807 int
808 umodemioctl(dev, cmd, data, flag, p)
809 dev_t dev;
810 u_long cmd;
811 caddr_t data;
812 int flag;
813 struct proc *p;
814 {
815 struct umodem_softc *sc = umodem_cd.cd_devs[UMODEMUNIT(dev)];
816 struct tty *tp = sc->sc_tty;
817 int error;
818 int s;
819
820 if (sc->sc_dying)
821 return (EIO);
822
823 DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
824
825 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
826 if (error >= 0)
827 return (error);
828
829 error = ttioctl(tp, cmd, data, flag, p);
830 if (error >= 0)
831 return (error);
832
833 error = 0;
834
835 DPRINTF(("umodemioctl: our cmd=0x%08lx\n", cmd));
836 s = spltty();
837
838 switch (cmd) {
839 case TIOCSBRK:
840 umodem_break(sc, 1);
841 break;
842
843 case TIOCCBRK:
844 umodem_break(sc, 0);
845 break;
846
847 case TIOCSDTR:
848 umodem_modem(sc, 1);
849 break;
850
851 case TIOCCDTR:
852 umodem_modem(sc, 0);
853 break;
854
855 case USB_GET_CM_OVER_DATA:
856 *(int *)data = sc->sc_cm_over_data;
857 break;
858
859 case USB_SET_CM_OVER_DATA:
860 if (*(int *)data != sc->sc_cm_over_data) {
861 /* XXX change it */
862 }
863 break;
864
865 default:
866 DPRINTF(("umodemioctl: unknown\n"));
867 error = ENOTTY;
868 break;
869 }
870
871 splx(s);
872
873 return (error);
874 }
875
876 void
877 umodem_shutdown(sc)
878 struct umodem_softc *sc;
879 {
880 struct tty *tp = sc->sc_tty;
881
882 DPRINTF(("umodem_shutdown\n"));
883 /*
884 * Hang up if necessary. Wait a bit, so the other side has time to
885 * notice even if we immediately open the port again.
886 */
887 if (ISSET(tp->t_cflag, HUPCL)) {
888 umodem_modem(sc, 0);
889 (void) tsleep(sc, TTIPRI, ttclos, hz);
890 }
891 }
892
893 void
894 umodem_modem(sc, onoff)
895 struct umodem_softc *sc;
896 int onoff;
897 {
898 usb_device_request_t req;
899
900 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
901
902 if (sc->sc_dtr == onoff)
903 return;
904
905 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
906 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
907 USETW(req.wValue, onoff ? UCDC_LINE_DTR : 0);
908 USETW(req.wIndex, sc->sc_ctl_iface_no);
909 USETW(req.wLength, 0);
910
911 (void)usbd_do_request(sc->sc_udev, &req, 0);
912
913 sc->sc_dtr = onoff;
914 }
915
916 void
917 umodem_break(sc, onoff)
918 struct umodem_softc *sc;
919 int onoff;
920 {
921 usb_device_request_t req;
922
923 DPRINTF(("umodem_break: onoff=%d\n", onoff));
924
925 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
926 return;
927
928 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
929 req.bRequest = UCDC_SEND_BREAK;
930 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
931 USETW(req.wIndex, sc->sc_ctl_iface_no);
932 USETW(req.wLength, 0);
933
934 (void)usbd_do_request(sc->sc_udev, &req, 0);
935 }
936
937 void *
938 umodem_get_desc(dev, type, subtype)
939 usbd_device_handle dev;
940 int type;
941 int subtype;
942 {
943 usb_descriptor_t *desc;
944 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
945 uByte *p = (uByte *)cd;
946 uByte *end = p + UGETW(cd->wTotalLength);
947
948 while (p < end) {
949 desc = (usb_descriptor_t *)p;
950 if (desc->bDescriptorType == type &&
951 desc->bDescriptorSubtype == subtype)
952 return (desc);
953 p += desc->bLength;
954 }
955
956 return (0);
957 }
958
959 usbd_status
960 umodem_set_comm_feature(sc, feature, state)
961 struct umodem_softc *sc;
962 int feature;
963 int state;
964 {
965 usb_device_request_t req;
966 usbd_status r;
967 usb_cdc_abstract_state_t ast;
968
969 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
970 req.bRequest = UCDC_SET_COMM_FEATURE;
971 USETW(req.wValue, feature);
972 USETW(req.wIndex, sc->sc_ctl_iface_no);
973 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
974 USETW(ast.wState, state);
975
976 r = usbd_do_request(sc->sc_udev, &req, &ast);
977 if (r != USBD_NORMAL_COMPLETION) {
978 DPRINTF(("umodem_set_comm_feature: feature=%d failed, r=%d\n",
979 feature, r));
980 return (r);
981 }
982
983 return (USBD_NORMAL_COMPLETION);
984 }
985
986 usbd_status
987 umodem_set_line_coding(sc, state)
988 struct umodem_softc *sc;
989 usb_cdc_line_state_t *state;
990 {
991 usb_device_request_t req;
992 usbd_status r;
993
994 DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
995 UGETDW(state->dwDTERate), state->bCharFormat,
996 state->bParityType, state->bDataBits));
997
998 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
999 DPRINTF(("umodem_set_line_coding: already set\n"));
1000 return (USBD_NORMAL_COMPLETION);
1001 }
1002
1003 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1004 req.bRequest = UCDC_SET_LINE_CODING;
1005 USETW(req.wValue, 0);
1006 USETW(req.wIndex, sc->sc_ctl_iface_no);
1007 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
1008
1009 r = usbd_do_request(sc->sc_udev, &req, state);
1010 if (r != USBD_NORMAL_COMPLETION) {
1011 DPRINTF(("umodem_set_line_coding: failed, r=%d\n", r));
1012 return (r);
1013 }
1014
1015 sc->sc_line_state = *state;
1016
1017 return (USBD_NORMAL_COMPLETION);
1018 }
1019
1020 int
1021 umodem_activate(self, act)
1022 device_ptr_t self;
1023 enum devact act;
1024 {
1025 struct umodem_softc *sc = (struct umodem_softc *)self;
1026
1027 switch (act) {
1028 case DVACT_ACTIVATE:
1029 return (EOPNOTSUPP);
1030 break;
1031
1032 case DVACT_DEACTIVATE:
1033 sc->sc_dying = 1;
1034 break;
1035 }
1036 return (0);
1037 }
1038
1039 int
1040 umodem_detach(self, flags)
1041 device_ptr_t self;
1042 int flags;
1043 {
1044 struct umodem_softc *sc = (struct umodem_softc *)self;
1045 int maj, mn;
1046
1047 DPRINTF(("umodem_detach: sc=%p flags=%d tp=%p\n",
1048 sc, flags, sc->sc_tty));
1049
1050 sc->sc_dying = 1;
1051
1052 #ifdef DIAGNOSTIC
1053 if (sc->sc_tty == 0) {
1054 DPRINTF(("umodem_detach: no tty\n"));
1055 return (0);
1056 }
1057 #endif
1058
1059 /* use refernce count? XXX */
1060
1061 /* locate the major number */
1062 for (maj = 0; maj < nchrdev; maj++)
1063 if (cdevsw[maj].d_open == umodemopen)
1064 break;
1065
1066 /* Nuke the vnodes for any open instances. */
1067 mn = self->dv_unit;
1068 vdevgone(maj, mn, mn, VCHR);
1069 vdevgone(maj, mn, mn | UMODEMDIALOUT_MASK, VCHR);
1070 vdevgone(maj, mn, mn | UMODEMCALLUNIT_MASK, VCHR);
1071
1072 /* Detach and free the tty. */
1073 tty_detach(sc->sc_tty);
1074 ttyfree(sc->sc_tty);
1075 sc->sc_tty = 0;
1076
1077 return (0);
1078 }
1079