]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/serial/umodem.c
MFC r197570
[FreeBSD/stable/8.git] / sys / dev / usb / serial / umodem.c
1 /*      $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $       */
2
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5
6 /*-
7  * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*-
33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Lennart Augustsson (lennart@augustsson.net) at
38  * Carlstedt Research & Technology.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *        This product includes software developed by the NetBSD
51  *        Foundation, Inc. and its contributors.
52  * 4. Neither the name of The NetBSD Foundation nor the names of its
53  *    contributors may be used to endorse or promote products derived
54  *    from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66  * POSSIBILITY OF SUCH DAMAGE.
67  */
68
69 /*
70  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
71  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
72  *                   http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
73  */
74
75 /*
76  * TODO:
77  * - Add error recovery in various places; the big problem is what
78  *   to do in a callback if there is an error.
79  * - Implement a Call Device for modems without multiplexed commands.
80  *
81  */
82
83 #include <sys/stdint.h>
84 #include <sys/stddef.h>
85 #include <sys/param.h>
86 #include <sys/queue.h>
87 #include <sys/types.h>
88 #include <sys/systm.h>
89 #include <sys/kernel.h>
90 #include <sys/bus.h>
91 #include <sys/linker_set.h>
92 #include <sys/module.h>
93 #include <sys/lock.h>
94 #include <sys/mutex.h>
95 #include <sys/condvar.h>
96 #include <sys/sysctl.h>
97 #include <sys/sx.h>
98 #include <sys/unistd.h>
99 #include <sys/callout.h>
100 #include <sys/malloc.h>
101 #include <sys/priv.h>
102
103 #include <dev/usb/usb.h>
104 #include <dev/usb/usbdi.h>
105 #include <dev/usb/usbdi_util.h>
106 #include <dev/usb/usbhid.h>
107 #include <dev/usb/usb_cdc.h>
108 #include "usbdevs.h"
109
110 #include <dev/usb/usb_ioctl.h>
111
112 #define USB_DEBUG_VAR umodem_debug
113 #include <dev/usb/usb_debug.h>
114 #include <dev/usb/usb_process.h>
115
116 #include <dev/usb/serial/usb_serial.h>
117
118 #if USB_DEBUG
119 static int umodem_debug = 0;
120
121 SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
122 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
123     &umodem_debug, 0, "Debug level");
124 #endif
125
126 static const struct usb_device_id umodem_devs[] = {
127         /* Generic Modem class match */
128         {USB_IFACE_CLASS(UICLASS_CDC),
129                 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
130         USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
131         /* Kyocera AH-K3001V */
132         {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
133         {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
134         {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
135 };
136
137 /*
138  * As speeds for umodem deivces increase, these numbers will need to
139  * be increased. They should be good for G3 speeds and below.
140  *
141  * TODO: The TTY buffers should be increased!
142  */
143 #define UMODEM_BUF_SIZE 1024
144
145 enum {
146         UMODEM_BULK_WR,
147         UMODEM_BULK_RD,
148         UMODEM_INTR_RD,
149         UMODEM_N_TRANSFER,
150 };
151
152 #define UMODEM_MODVER                   1       /* module version */
153
154 struct umodem_softc {
155         struct ucom_super_softc sc_super_ucom;
156         struct ucom_softc sc_ucom;
157
158         struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
159         struct usb_device *sc_udev;
160         struct mtx sc_mtx;
161
162         uint16_t sc_line;
163
164         uint8_t sc_lsr;                 /* local status register */
165         uint8_t sc_msr;                 /* modem status register */
166         uint8_t sc_ctrl_iface_no;
167         uint8_t sc_data_iface_no;
168         uint8_t sc_iface_index[2];
169         uint8_t sc_cm_over_data;
170         uint8_t sc_cm_cap;              /* CM capabilities */
171         uint8_t sc_acm_cap;             /* ACM capabilities */
172 };
173
174 static device_probe_t umodem_probe;
175 static device_attach_t umodem_attach;
176 static device_detach_t umodem_detach;
177
178 static usb_callback_t umodem_intr_callback;
179 static usb_callback_t umodem_write_callback;
180 static usb_callback_t umodem_read_callback;
181
182 static void     umodem_start_read(struct ucom_softc *);
183 static void     umodem_stop_read(struct ucom_softc *);
184 static void     umodem_start_write(struct ucom_softc *);
185 static void     umodem_stop_write(struct ucom_softc *);
186 static void     umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
187 static void     umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
188                     uint8_t *);
189 static int      umodem_pre_param(struct ucom_softc *, struct termios *);
190 static void     umodem_cfg_param(struct ucom_softc *, struct termios *);
191 static int      umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
192                     struct thread *);
193 static void     umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
194 static void     umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
195 static void     umodem_cfg_set_break(struct ucom_softc *, uint8_t);
196 static void     *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
197 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
198                     uint16_t, uint16_t);
199 static void     umodem_poll(struct ucom_softc *ucom);
200
201 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
202
203         [UMODEM_BULK_WR] = {
204                 .type = UE_BULK,
205                 .endpoint = UE_ADDR_ANY,
206                 .direction = UE_DIR_OUT,
207                 .if_index = 0,
208                 .bufsize = UMODEM_BUF_SIZE,
209                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
210                 .callback = &umodem_write_callback,
211         },
212
213         [UMODEM_BULK_RD] = {
214                 .type = UE_BULK,
215                 .endpoint = UE_ADDR_ANY,
216                 .direction = UE_DIR_IN,
217                 .if_index = 0,
218                 .bufsize = UMODEM_BUF_SIZE,
219                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
220                 .callback = &umodem_read_callback,
221         },
222
223         [UMODEM_INTR_RD] = {
224                 .type = UE_INTERRUPT,
225                 .endpoint = UE_ADDR_ANY,
226                 .direction = UE_DIR_IN,
227                 .if_index = 1,
228                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
229                 .bufsize = 0,   /* use wMaxPacketSize */
230                 .callback = &umodem_intr_callback,
231         },
232 };
233
234 static const struct ucom_callback umodem_callback = {
235         .ucom_cfg_get_status = &umodem_cfg_get_status,
236         .ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
237         .ucom_cfg_set_rts = &umodem_cfg_set_rts,
238         .ucom_cfg_set_break = &umodem_cfg_set_break,
239         .ucom_cfg_param = &umodem_cfg_param,
240         .ucom_pre_param = &umodem_pre_param,
241         .ucom_ioctl = &umodem_ioctl,
242         .ucom_start_read = &umodem_start_read,
243         .ucom_stop_read = &umodem_stop_read,
244         .ucom_start_write = &umodem_start_write,
245         .ucom_stop_write = &umodem_stop_write,
246         .ucom_poll = &umodem_poll,
247 };
248
249 static device_method_t umodem_methods[] = {
250         DEVMETHOD(device_probe, umodem_probe),
251         DEVMETHOD(device_attach, umodem_attach),
252         DEVMETHOD(device_detach, umodem_detach),
253         {0, 0}
254 };
255
256 static devclass_t umodem_devclass;
257
258 static driver_t umodem_driver = {
259         .name = "umodem",
260         .methods = umodem_methods,
261         .size = sizeof(struct umodem_softc),
262 };
263
264 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
265 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
266 MODULE_DEPEND(umodem, usb, 1, 1, 1);
267 MODULE_VERSION(umodem, UMODEM_MODVER);
268
269 static int
270 umodem_probe(device_t dev)
271 {
272         struct usb_attach_arg *uaa = device_get_ivars(dev);
273         int error;
274
275         DPRINTFN(11, "\n");
276
277         if (uaa->usb_mode != USB_MODE_HOST) {
278                 return (ENXIO);
279         }
280         error = usbd_lookup_id_by_uaa(umodem_devs, sizeof(umodem_devs), uaa);
281         return (error);
282 }
283
284 static int
285 umodem_attach(device_t dev)
286 {
287         struct usb_attach_arg *uaa = device_get_ivars(dev);
288         struct umodem_softc *sc = device_get_softc(dev);
289         struct usb_cdc_cm_descriptor *cmd;
290         struct usb_cdc_union_descriptor *cud;
291         uint8_t i;
292         int error;
293
294         device_set_usb_desc(dev);
295         mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
296
297         sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
298         sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
299         sc->sc_udev = uaa->device;
300
301         umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
302
303         /* get the data interface number */
304
305         cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
306
307         if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
308
309                 cud = usbd_find_descriptor(uaa->device, NULL,
310                     uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
311                     0 - 1, UDESCSUB_CDC_UNION, 0 - 1);
312
313                 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
314                         device_printf(dev, "no CM or union descriptor!\n");
315                         goto detach;
316                 }
317
318                 sc->sc_data_iface_no = cud->bSlaveInterface[0];
319         } else {
320                 sc->sc_data_iface_no = cmd->bDataInterface;
321         }
322
323         device_printf(dev, "data interface %d, has %sCM over "
324             "data, has %sbreak\n",
325             sc->sc_data_iface_no,
326             sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
327             sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
328
329         /* get the data interface too */
330
331         for (i = 0;; i++) {
332                 struct usb_interface *iface;
333                 struct usb_interface_descriptor *id;
334
335                 iface = usbd_get_iface(uaa->device, i);
336
337                 if (iface) {
338
339                         id = usbd_get_interface_descriptor(iface);
340
341                         if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
342                                 sc->sc_iface_index[0] = i;
343                                 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
344                                 break;
345                         }
346                 } else {
347                         device_printf(dev, "no data interface!\n");
348                         goto detach;
349                 }
350         }
351
352         if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
353                 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
354
355                         error = umodem_set_comm_feature
356                             (uaa->device, sc->sc_ctrl_iface_no,
357                             UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
358
359                         /* ignore any errors */
360                 }
361                 sc->sc_cm_over_data = 1;
362         }
363         error = usbd_transfer_setup(uaa->device,
364             sc->sc_iface_index, sc->sc_xfer,
365             umodem_config, UMODEM_N_TRANSFER,
366             sc, &sc->sc_mtx);
367         if (error) {
368                 goto detach;
369         }
370
371         /* clear stall at first run */
372         mtx_lock(&sc->sc_mtx);
373         usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
374         usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
375         mtx_unlock(&sc->sc_mtx);
376
377         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
378             &umodem_callback, &sc->sc_mtx);
379         if (error) {
380                 goto detach;
381         }
382         return (0);
383
384 detach:
385         umodem_detach(dev);
386         return (ENXIO);
387 }
388
389 static void
390 umodem_start_read(struct ucom_softc *ucom)
391 {
392         struct umodem_softc *sc = ucom->sc_parent;
393
394         /* start interrupt endpoint, if any */
395         usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
396
397         /* start read endpoint */
398         usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
399 }
400
401 static void
402 umodem_stop_read(struct ucom_softc *ucom)
403 {
404         struct umodem_softc *sc = ucom->sc_parent;
405
406         /* stop interrupt endpoint, if any */
407         usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
408
409         /* stop read endpoint */
410         usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
411 }
412
413 static void
414 umodem_start_write(struct ucom_softc *ucom)
415 {
416         struct umodem_softc *sc = ucom->sc_parent;
417
418         usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
419 }
420
421 static void
422 umodem_stop_write(struct ucom_softc *ucom)
423 {
424         struct umodem_softc *sc = ucom->sc_parent;
425
426         usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
427 }
428
429 static void
430 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
431 {
432         struct usb_cdc_cm_descriptor *cmd;
433         struct usb_cdc_acm_descriptor *cad;
434
435         cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
436         if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
437                 DPRINTF("no CM desc (faking one)\n");
438                 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
439         } else
440                 *cm = cmd->bmCapabilities;
441
442         cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
443         if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
444                 DPRINTF("no ACM desc\n");
445                 *acm = 0;
446         } else
447                 *acm = cad->bmCapabilities;
448 }
449
450 static void
451 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
452 {
453         struct umodem_softc *sc = ucom->sc_parent;
454
455         DPRINTF("\n");
456
457         *lsr = sc->sc_lsr;
458         *msr = sc->sc_msr;
459 }
460
461 static int
462 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
463 {
464         return (0);                     /* we accept anything */
465 }
466
467 static void
468 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
469 {
470         struct umodem_softc *sc = ucom->sc_parent;
471         struct usb_cdc_line_state ls;
472         struct usb_device_request req;
473
474         DPRINTF("sc=%p\n", sc);
475
476         bzero(&ls, sizeof(ls));
477
478         USETDW(ls.dwDTERate, t->c_ospeed);
479
480         ls.bCharFormat = (t->c_cflag & CSTOPB) ?
481             UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
482
483         ls.bParityType = (t->c_cflag & PARENB) ?
484             ((t->c_cflag & PARODD) ?
485             UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
486
487         switch (t->c_cflag & CSIZE) {
488         case CS5:
489                 ls.bDataBits = 5;
490                 break;
491         case CS6:
492                 ls.bDataBits = 6;
493                 break;
494         case CS7:
495                 ls.bDataBits = 7;
496                 break;
497         case CS8:
498                 ls.bDataBits = 8;
499                 break;
500         }
501
502         DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
503             UGETDW(ls.dwDTERate), ls.bCharFormat,
504             ls.bParityType, ls.bDataBits);
505
506         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
507         req.bRequest = UCDC_SET_LINE_CODING;
508         USETW(req.wValue, 0);
509         req.wIndex[0] = sc->sc_ctrl_iface_no;
510         req.wIndex[1] = 0;
511         USETW(req.wLength, sizeof(ls));
512
513         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
514             &req, &ls, 0, 1000);
515 }
516
517 static int
518 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
519     int flag, struct thread *td)
520 {
521         struct umodem_softc *sc = ucom->sc_parent;
522         int error = 0;
523
524         DPRINTF("cmd=0x%08x\n", cmd);
525
526         switch (cmd) {
527         case USB_GET_CM_OVER_DATA:
528                 *(int *)data = sc->sc_cm_over_data;
529                 break;
530
531         case USB_SET_CM_OVER_DATA:
532                 if (*(int *)data != sc->sc_cm_over_data) {
533                         /* XXX change it */
534                 }
535                 break;
536
537         default:
538                 DPRINTF("unknown\n");
539                 error = ENOIOCTL;
540                 break;
541         }
542
543         return (error);
544 }
545
546 static void
547 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
548 {
549         struct umodem_softc *sc = ucom->sc_parent;
550         struct usb_device_request req;
551
552         DPRINTF("onoff=%d\n", onoff);
553
554         if (onoff)
555                 sc->sc_line |= UCDC_LINE_DTR;
556         else
557                 sc->sc_line &= ~UCDC_LINE_DTR;
558
559         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
560         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
561         USETW(req.wValue, sc->sc_line);
562         req.wIndex[0] = sc->sc_ctrl_iface_no;
563         req.wIndex[1] = 0;
564         USETW(req.wLength, 0);
565
566         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
567             &req, NULL, 0, 1000);
568 }
569
570 static void
571 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
572 {
573         struct umodem_softc *sc = ucom->sc_parent;
574         struct usb_device_request req;
575
576         DPRINTF("onoff=%d\n", onoff);
577
578         if (onoff)
579                 sc->sc_line |= UCDC_LINE_RTS;
580         else
581                 sc->sc_line &= ~UCDC_LINE_RTS;
582
583         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
584         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
585         USETW(req.wValue, sc->sc_line);
586         req.wIndex[0] = sc->sc_ctrl_iface_no;
587         req.wIndex[1] = 0;
588         USETW(req.wLength, 0);
589
590         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
591             &req, NULL, 0, 1000);
592 }
593
594 static void
595 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
596 {
597         struct umodem_softc *sc = ucom->sc_parent;
598         struct usb_device_request req;
599         uint16_t temp;
600
601         DPRINTF("onoff=%d\n", onoff);
602
603         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
604
605                 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
606
607                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
608                 req.bRequest = UCDC_SEND_BREAK;
609                 USETW(req.wValue, temp);
610                 req.wIndex[0] = sc->sc_ctrl_iface_no;
611                 req.wIndex[1] = 0;
612                 USETW(req.wLength, 0);
613
614                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
615                     &req, NULL, 0, 1000);
616         }
617 }
618
619 static void
620 umodem_intr_callback(struct usb_xfer *xfer, usb_error_t error)
621 {
622         struct usb_cdc_notification pkt;
623         struct umodem_softc *sc = usbd_xfer_softc(xfer);
624         struct usb_page_cache *pc;
625         uint16_t wLen;
626         int actlen;
627
628         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
629
630         switch (USB_GET_STATE(xfer)) {
631         case USB_ST_TRANSFERRED:
632
633                 if (actlen < 8) {
634                         DPRINTF("received short packet, "
635                             "%d bytes\n", actlen);
636                         goto tr_setup;
637                 }
638                 if (actlen > sizeof(pkt)) {
639                         DPRINTF("truncating message\n");
640                         actlen = sizeof(pkt);
641                 }
642                 pc = usbd_xfer_get_frame(xfer, 0);
643                 usbd_copy_out(pc, 0, &pkt, actlen);
644
645                 actlen -= 8;
646
647                 wLen = UGETW(pkt.wLength);
648                 if (actlen > wLen) {
649                         actlen = wLen;
650                 }
651                 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
652                         DPRINTF("unknown message type, "
653                             "0x%02x, on notify pipe!\n",
654                             pkt.bmRequestType);
655                         goto tr_setup;
656                 }
657                 switch (pkt.bNotification) {
658                 case UCDC_N_SERIAL_STATE:
659                         /*
660                          * Set the serial state in ucom driver based on
661                          * the bits from the notify message
662                          */
663                         if (actlen < 2) {
664                                 DPRINTF("invalid notification "
665                                     "length, %d bytes!\n", actlen);
666                                 break;
667                         }
668                         DPRINTF("notify bytes = %02x%02x\n",
669                             pkt.data[0],
670                             pkt.data[1]);
671
672                         /* Currently, lsr is always zero. */
673                         sc->sc_lsr = 0;
674                         sc->sc_msr = 0;
675
676                         if (pkt.data[0] & UCDC_N_SERIAL_RI) {
677                                 sc->sc_msr |= SER_RI;
678                         }
679                         if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
680                                 sc->sc_msr |= SER_DSR;
681                         }
682                         if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
683                                 sc->sc_msr |= SER_DCD;
684                         }
685                         ucom_status_change(&sc->sc_ucom);
686                         break;
687
688                 default:
689                         DPRINTF("unknown notify message: 0x%02x\n",
690                             pkt.bNotification);
691                         break;
692                 }
693
694         case USB_ST_SETUP:
695 tr_setup:
696                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
697                 usbd_transfer_submit(xfer);
698                 return;
699
700         default:                        /* Error */
701                 if (error != USB_ERR_CANCELLED) {
702                         /* try to clear stall first */
703                         usbd_xfer_set_stall(xfer);
704                         goto tr_setup;
705                 }
706                 return;
707
708         }
709 }
710
711 static void
712 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
713 {
714         struct umodem_softc *sc = usbd_xfer_softc(xfer);
715         struct usb_page_cache *pc;
716         uint32_t actlen;
717
718         switch (USB_GET_STATE(xfer)) {
719         case USB_ST_SETUP:
720         case USB_ST_TRANSFERRED:
721 tr_setup:
722                 pc = usbd_xfer_get_frame(xfer, 0);
723                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
724                     UMODEM_BUF_SIZE, &actlen)) {
725
726                         usbd_xfer_set_frame_len(xfer, 0, actlen);
727                         usbd_transfer_submit(xfer);
728                 }
729                 return;
730
731         default:                        /* Error */
732                 if (error != USB_ERR_CANCELLED) {
733                         /* try to clear stall first */
734                         usbd_xfer_set_stall(xfer);
735                         goto tr_setup;
736                 }
737                 return;
738         }
739 }
740
741 static void
742 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
743 {
744         struct umodem_softc *sc = usbd_xfer_softc(xfer);
745         struct usb_page_cache *pc;
746         int actlen;
747
748         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
749
750         switch (USB_GET_STATE(xfer)) {
751         case USB_ST_TRANSFERRED:
752
753                 DPRINTF("actlen=%d\n", actlen);
754
755                 pc = usbd_xfer_get_frame(xfer, 0);
756                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
757
758         case USB_ST_SETUP:
759 tr_setup:
760                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
761                 usbd_transfer_submit(xfer);
762                 return;
763
764         default:                        /* Error */
765                 if (error != USB_ERR_CANCELLED) {
766                         /* try to clear stall first */
767                         usbd_xfer_set_stall(xfer);
768                         goto tr_setup;
769                 }
770                 return;
771         }
772 }
773
774 static void *
775 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
776 {
777         return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
778             type, 0 - 1, subtype, 0 - 1));
779 }
780
781 static usb_error_t
782 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
783     uint16_t feature, uint16_t state)
784 {
785         struct usb_device_request req;
786         struct usb_cdc_abstract_state ast;
787
788         DPRINTF("feature=%d state=%d\n",
789             feature, state);
790
791         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
792         req.bRequest = UCDC_SET_COMM_FEATURE;
793         USETW(req.wValue, feature);
794         req.wIndex[0] = iface_no;
795         req.wIndex[1] = 0;
796         USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
797         USETW(ast.wState, state);
798
799         return (usbd_do_request(udev, NULL, &req, &ast));
800 }
801
802 static int
803 umodem_detach(device_t dev)
804 {
805         struct umodem_softc *sc = device_get_softc(dev);
806
807         DPRINTF("sc=%p\n", sc);
808
809         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
810         usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
811         mtx_destroy(&sc->sc_mtx);
812
813         return (0);
814 }
815
816 static void
817 umodem_poll(struct ucom_softc *ucom)
818 {
819         struct umodem_softc *sc = ucom->sc_parent;
820         usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
821 }