]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/dev/usb/umodem.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / dev / usb / umodem.c
1 /*      $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $       */
2
3
4 #include <sys/cdefs.h>
5 __FBSDID("$FreeBSD$");
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  */
73
74 /*
75  * TODO:
76  * - Add error recovery in various places; the big problem is what
77  *   to do in a callback if there is an error.
78  * - Implement a Call Device for modems without multiplexed commands.
79  *
80  */
81
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/module.h>
86 #include <sys/ioccom.h>
87 #include <sys/conf.h>
88 #include <sys/serial.h>
89 #include <sys/tty.h>
90 #include <sys/file.h>
91 #include <sys/select.h>
92 #include <sys/sysctl.h>
93 #include <sys/proc.h>
94 #include <sys/bus.h>
95 #include <sys/poll.h>
96 #include <sys/uio.h>
97 #include <sys/taskqueue.h>
98
99 #include <dev/usb/usb.h>
100 #include <dev/usb/usbcdc.h>
101
102 #include <dev/usb/usbdi.h>
103 #include <dev/usb/usbdi_util.h>
104 #include <dev/usb/usb_quirks.h>
105
106 #include <dev/usb/ucomvar.h>
107
108 #include "usbdevs.h"
109
110 #ifdef USB_DEBUG
111 int     umodemdebug = 0;
112 SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
113 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
114            &umodemdebug, 0, "umodem debug level");
115 #define DPRINTFN(n, x)  if (umodemdebug > (n)) logprintf x
116 #else
117 #define DPRINTFN(n, x)
118 #endif
119 #define DPRINTF(x) DPRINTFN(0, x)
120
121 static const struct umodem_product {
122         u_int16_t       vendor;
123         u_int16_t       product;
124         u_int8_t        interface;
125 } umodem_products[] = {
126         /* Kyocera AH-K3001V*/
127         { USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 0 },
128         { USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 0 },
129         { USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 0 },
130         { 0, 0, 0 },
131 };
132
133 /*
134  * These are the maximum number of bytes transferred per frame.
135  * If some really high speed devices should use this driver they
136  * may need to be increased, but this is good enough for normal modems.
137  */
138 #define UMODEMIBUFSIZE 64
139 #define UMODEMOBUFSIZE 256
140
141 #define UMODEM_MODVER                   1       /* module version */
142
143 struct umodem_softc {
144         struct ucom_softc       sc_ucom;
145
146         USBBASEDEVICE           sc_dev;         /* base device */
147
148         usbd_device_handle      sc_udev;        /* USB device */
149
150         int                     sc_ctl_iface_no;
151         usbd_interface_handle   sc_ctl_iface;   /* control interface */
152         int                     sc_data_iface_no;
153         usbd_interface_handle   sc_data_iface;  /* data interface */
154
155         int                     sc_cm_cap;      /* CM capabilities */
156         int                     sc_acm_cap;     /* ACM capabilities */
157
158         int                     sc_cm_over_data;
159
160         usb_cdc_line_state_t    sc_line_state;  /* current line state */
161         u_char                  sc_dtr;         /* current DTR state */
162         u_char                  sc_rts;         /* current RTS state */
163
164         u_char                  sc_opening;     /* lock during open */
165
166         int                     sc_ctl_notify;  /* Notification endpoint */
167         usbd_pipe_handle        sc_notify_pipe; /* Notification pipe */
168         usb_cdc_notification_t  sc_notify_buf;  /* Notification structure */
169         u_char                  sc_lsr;         /* Local status register */
170         u_char                  sc_msr;         /* Modem status register */
171
172         struct task             sc_task;
173 };
174
175 Static void     *umodem_get_desc(usbd_device_handle dev, int type, int subtype);
176 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
177                                            int feature, int state);
178 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
179                                           usb_cdc_line_state_t *state);
180
181 Static void     umodem_get_caps(usbd_device_handle, int *, int *);
182
183 Static void     umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
184 Static void     umodem_set(void *, int, int, int);
185 Static void     umodem_dtr(struct umodem_softc *, int);
186 Static void     umodem_rts(struct umodem_softc *, int);
187 Static void     umodem_break(struct umodem_softc *, int);
188 Static void     umodem_set_line_state(struct umodem_softc *);
189 Static int      umodem_param(void *, int, struct termios *);
190 Static int      umodem_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
191 Static int      umodem_open(void *, int portno);
192 Static void     umodem_close(void *, int portno);
193 Static void     umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
194 Static void     umodem_notify(void *, int);
195
196 Static struct ucom_callback umodem_callback = {
197         umodem_get_status,
198         umodem_set,
199         umodem_param,
200         umodem_ioctl,
201         umodem_open,
202         umodem_close,
203         NULL,
204         NULL,
205 };
206
207 Static device_probe_t umodem_match;
208 Static device_attach_t umodem_attach;
209 Static device_detach_t umodem_detach;
210
211 Static device_method_t umodem_methods[] = {
212         /* Device interface */
213         DEVMETHOD(device_probe, umodem_match),
214         DEVMETHOD(device_attach, umodem_attach),
215         DEVMETHOD(device_detach, umodem_detach),
216         { 0, 0 }
217 };
218
219 Static driver_t umodem_driver = {
220         "ucom",
221         umodem_methods,
222         sizeof (struct umodem_softc)
223 };
224
225 DRIVER_MODULE(umodem, uhub, umodem_driver, ucom_devclass, usbd_driver_load, 0);
226 MODULE_DEPEND(umodem, usb, 1, 1, 1);
227 MODULE_DEPEND(umodem, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
228 MODULE_VERSION(umodem, UMODEM_MODVER);
229
230 USB_MATCH(umodem)
231 {
232         USB_MATCH_START(umodem, uaa);
233         usb_interface_descriptor_t *id;
234         usb_device_descriptor_t *dd;
235         int cm, acm, i, ret;
236
237         if (uaa->iface == NULL)
238                 return (UMATCH_NONE);
239
240         id = usbd_get_interface_descriptor(uaa->iface);
241         dd = usbd_get_device_descriptor(uaa->device);
242         if (id == NULL || dd == NULL)
243                 return (UMATCH_NONE);
244
245         ret = UMATCH_NONE;
246         for (i = 0; umodem_products[i].vendor != 0; i++) {
247                 if (umodem_products[i].vendor == UGETW(dd->idVendor) &&
248                     umodem_products[i].product == UGETW(dd->idProduct) &&
249                     umodem_products[i].interface == id->bInterfaceNumber) {
250                         ret = UMATCH_VENDOR_PRODUCT;
251                         break;
252                 }
253         }
254
255         if (ret == UMATCH_NONE &&
256             id->bInterfaceClass == UICLASS_CDC &&
257             id->bInterfaceSubClass == UISUBCLASS_ABSTRACT_CONTROL_MODEL &&
258             id->bInterfaceProtocol == UIPROTO_CDC_AT)
259                 ret = UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
260
261         if (ret == UMATCH_NONE)
262                 return (ret);
263
264         umodem_get_caps(uaa->device, &cm, &acm);
265         if (!(cm & USB_CDC_CM_DOES_CM) ||
266             !(cm & USB_CDC_CM_OVER_DATA) ||
267             !(acm & USB_CDC_ACM_HAS_LINE))
268                 return (UMATCH_NONE);
269
270         return ret;
271 }
272
273 USB_ATTACH(umodem)
274 {
275         USB_ATTACH_START(umodem, sc, uaa);
276         usbd_device_handle dev = uaa->device;
277         usb_interface_descriptor_t *id;
278         usb_endpoint_descriptor_t *ed;
279         usb_cdc_cm_descriptor_t *cmd;
280         char *devinfo = NULL;
281         const char *devname;
282         usbd_status err;
283         int data_ifcno;
284         int i;
285         struct ucom_softc *ucom;
286
287         devinfo = malloc(1024, M_USBDEV, M_WAITOK);
288         usbd_devinfo(dev, 0, devinfo);
289         ucom = &sc->sc_ucom;
290         ucom->sc_dev = self;
291         sc->sc_dev = self;
292         device_set_desc_copy(self, devinfo);
293         ucom->sc_udev = dev;
294         ucom->sc_iface = uaa->iface;
295         /*USB_ATTACH_SETUP; */
296
297         sc->sc_udev = dev;
298         sc->sc_ctl_iface = uaa->iface;
299
300         devname = USBDEVNAME(sc->sc_dev);
301         /* XXX ? use something else ? XXX */
302         id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
303         printf("%s: %s, iclass %d/%d\n", devname, devinfo,
304           id->bInterfaceClass, id->bInterfaceSubClass);
305         sc->sc_ctl_iface_no = id->bInterfaceNumber;
306
307         umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
308
309         /* Get the data interface no. */
310         cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
311         if (cmd == NULL) {
312                 printf("%s: no CM descriptor\n", devname);
313                 goto bad;
314         }
315         sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
316
317         printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
318                devname, data_ifcno,
319                sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
320                sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
321
322         /* Get the data interface too. */
323         for (i = 0; i < uaa->nifaces; i++) {
324                 if (uaa->ifaces[i] != NULL) {
325                         id = usbd_get_interface_descriptor(uaa->ifaces[i]);
326                         if (id != NULL && id->bInterfaceNumber == data_ifcno) {
327                                 sc->sc_data_iface = uaa->ifaces[i];
328                                 uaa->ifaces[i] = NULL;
329                         }
330                 }
331         }
332         if (sc->sc_data_iface == NULL) {
333                 printf("%s: no data interface\n", devname);
334                 goto bad;
335         }
336         ucom->sc_iface = sc->sc_data_iface;
337
338         /*
339          * Find the bulk endpoints.
340          * Iterate over all endpoints in the data interface and take note.
341          */
342         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
343
344         id = usbd_get_interface_descriptor(sc->sc_data_iface);
345         for (i = 0; i < id->bNumEndpoints; i++) {
346                 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
347                 if (ed == NULL) {
348                         printf("%s: no endpoint descriptor for %d\n", devname,
349                             i);
350                         goto bad;
351                 }
352                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
353                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
354                         ucom->sc_bulkin_no = ed->bEndpointAddress;
355                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
356                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
357                         ucom->sc_bulkout_no = ed->bEndpointAddress;
358                 }
359         }
360
361         if (ucom->sc_bulkin_no == -1) {
362                 printf("%s: Could not find data bulk in\n", devname);
363                 goto bad;
364         }
365         if (ucom->sc_bulkout_no == -1) {
366                 printf("%s: Could not find data bulk out\n", devname);
367                 goto bad;
368         }
369
370         if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
371                 DPRINTF(("Quirk says to assume CM over data\n"));
372                 sc->sc_cm_over_data = 1;
373         } else {
374                 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
375                         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
376                                 err = umodem_set_comm_feature(sc,
377                                     UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
378                         else
379                                 err = 0;
380                         if (err) {
381                                 printf("%s: could not set data multiplex mode\n",
382                                     devname);
383                                 goto bad;
384                         }
385                         sc->sc_cm_over_data = 1;
386                 }
387         }
388
389         /*
390          * The standard allows for notification messages (to indicate things
391          * like a modem hangup) to come in via an interrupt endpoint
392          * off of the control interface.  Iterate over the endpoints on
393          * the control interface and see if there are any interrupt
394          * endpoints; if there are, then register it.
395          */
396
397         sc->sc_ctl_notify = -1;
398         sc->sc_notify_pipe = NULL;
399
400         for (i = 0; i < id->bNumEndpoints; i++) {
401                 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
402                 if (ed == NULL)
403                         continue;
404
405                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
406                     (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
407                         printf("%s: status change notification available\n",
408                             devname);
409                         sc->sc_ctl_notify = ed->bEndpointAddress;
410                 }
411         }
412
413         sc->sc_dtr = -1;
414
415         ucom->sc_parent = sc;
416         ucom->sc_portno = UCOM_UNK_PORTNO;
417         /* bulkin, bulkout set above */
418         ucom->sc_ibufsize = UMODEMIBUFSIZE;
419         ucom->sc_obufsize = UMODEMOBUFSIZE;
420         ucom->sc_ibufsizepad = UMODEMIBUFSIZE;
421         ucom->sc_opkthdrlen = 0;
422         ucom->sc_callback = &umodem_callback;
423
424         TASK_INIT(&sc->sc_task, 0, umodem_notify, sc);
425         ucom_attach(&sc->sc_ucom);
426
427         free(devinfo, M_USBDEV);
428         USB_ATTACH_SUCCESS_RETURN;
429
430  bad:
431         ucom->sc_dying = 1;
432         free(devinfo, M_USBDEV);
433         USB_ATTACH_ERROR_RETURN;
434 }
435
436 Static int
437 umodem_open(void *addr, int portno)
438 {
439         struct umodem_softc *sc = addr;
440         int err;
441
442         DPRINTF(("umodem_open: sc=%p\n", sc));
443
444         if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
445                 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
446                     USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
447                     &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
448                     umodem_intr, USBD_DEFAULT_INTERVAL);
449
450                 if (err) {
451                         DPRINTF(("Failed to establish notify pipe: %s\n",
452                                 usbd_errstr(err)));
453                         return EIO;
454                 }
455         }
456
457         return 0;
458 }
459
460 Static void
461 umodem_close(void *addr, int portno)
462 {
463         struct umodem_softc *sc = addr;
464         int err;
465
466         DPRINTF(("umodem_close: sc=%p\n", sc));
467
468         if (sc->sc_notify_pipe != NULL) {
469                 err = usbd_abort_pipe(sc->sc_notify_pipe);
470                 if (err)
471                         printf("%s: abort notify pipe failed: %s\n",
472                             USBDEVNAME(sc->sc_dev), usbd_errstr(err));
473                 err = usbd_close_pipe(sc->sc_notify_pipe);
474                 if (err)
475                         printf("%s: close notify pipe failed: %s\n",
476                             USBDEVNAME(sc->sc_dev), usbd_errstr(err));
477                 sc->sc_notify_pipe = NULL;
478         }
479 }
480
481 Static void
482 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
483 {
484         struct umodem_softc *sc = priv;
485         u_char mstatus;
486
487         if (sc->sc_ucom.sc_dying)
488                 return;
489
490         if (status != USBD_NORMAL_COMPLETION) {
491                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
492                         return;
493                 printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
494                        usbd_errstr(status));
495                 return;
496         }
497
498         if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
499                 DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
500                          USBDEVNAME(sc->sc_dev),
501                          sc->sc_notify_buf.bmRequestType));
502                 return;
503         }
504
505         switch (sc->sc_notify_buf.bNotification) {
506         case UCDC_N_SERIAL_STATE:
507                 /*
508                  * Set the serial state in ucom driver based on
509                  * the bits from the notify message
510                  */
511                 if (UGETW(sc->sc_notify_buf.wLength) != 2) {
512                         printf("%s: Invalid notification length! (%d)\n",
513                                USBDEVNAME(sc->sc_dev),
514                                UGETW(sc->sc_notify_buf.wLength));
515                         break;
516                 }
517                 DPRINTF(("%s: notify bytes = %02x%02x\n",
518                          USBDEVNAME(sc->sc_dev),
519                          sc->sc_notify_buf.data[0],
520                          sc->sc_notify_buf.data[1]));
521                 /* Currently, lsr is always zero. */
522                 sc->sc_lsr = sc->sc_msr = 0;
523                 mstatus = sc->sc_notify_buf.data[0];
524
525                 if (ISSET(mstatus, UCDC_N_SERIAL_RI))
526                         sc->sc_msr |= SER_RI;
527                 if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
528                         sc->sc_msr |= SER_DSR;
529                 if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
530                         sc->sc_msr |= SER_DCD;
531                 /* Deferred notifying to the ucom layer */
532                 taskqueue_enqueue(taskqueue_swi_giant, &sc->sc_task);
533                 break;
534         default:
535                 DPRINTF(("%s: unknown notify message: %02x\n",
536                          USBDEVNAME(sc->sc_dev),
537                          sc->sc_notify_buf.bNotification));
538                 break;
539         }
540 }
541
542 Static void
543 umodem_notify(void *arg, int count)
544 {
545         struct umodem_softc *sc;
546
547         sc = (struct umodem_softc *)arg;
548         if (sc->sc_ucom.sc_dying)
549                 return;
550         ucom_status_change(&sc->sc_ucom);
551 }
552
553 void
554 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm)
555 {
556         usb_cdc_cm_descriptor_t *cmd;
557         usb_cdc_acm_descriptor_t *cad;
558
559         *cm = *acm = 0;
560
561         cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
562         if (cmd == NULL) {
563                 DPRINTF(("umodem_get_desc: no CM desc\n"));
564                 return;
565         }
566         *cm = cmd->bmCapabilities;
567
568         cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
569         if (cad == NULL) {
570                 DPRINTF(("umodem_get_desc: no ACM desc\n"));
571                 return;
572         }
573         *acm = cad->bmCapabilities;
574 }
575
576 void
577 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
578 {
579         struct umodem_softc *sc = addr;
580
581         DPRINTF(("umodem_get_status:\n"));
582
583         if (lsr != NULL)
584                 *lsr = sc->sc_lsr;
585         if (msr != NULL)
586                 *msr = sc->sc_msr;
587 }
588
589 int
590 umodem_param(void *addr, int portno, struct termios *t)
591 {
592         struct umodem_softc *sc = addr;
593         usbd_status err;
594         usb_cdc_line_state_t ls;
595
596         DPRINTF(("umodem_param: sc=%p\n", sc));
597
598         USETDW(ls.dwDTERate, t->c_ospeed);
599         if (ISSET(t->c_cflag, CSTOPB))
600                 ls.bCharFormat = UCDC_STOP_BIT_2;
601         else
602                 ls.bCharFormat = UCDC_STOP_BIT_1;
603         if (ISSET(t->c_cflag, PARENB)) {
604                 if (ISSET(t->c_cflag, PARODD))
605                         ls.bParityType = UCDC_PARITY_ODD;
606                 else
607                         ls.bParityType = UCDC_PARITY_EVEN;
608         } else
609                 ls.bParityType = UCDC_PARITY_NONE;
610         switch (ISSET(t->c_cflag, CSIZE)) {
611         case CS5:
612                 ls.bDataBits = 5;
613                 break;
614         case CS6:
615                 ls.bDataBits = 6;
616                 break;
617         case CS7:
618                 ls.bDataBits = 7;
619                 break;
620         case CS8:
621                 ls.bDataBits = 8;
622                 break;
623         }
624
625         err = umodem_set_line_coding(sc, &ls);
626         if (err) {
627                 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
628                 return (ENOTTY);
629         }
630         return (0);
631 }
632
633 int
634 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
635              usb_proc_ptr p)
636 {
637         struct umodem_softc *sc = addr;
638         int error = 0;
639
640         if (sc->sc_ucom.sc_dying)
641                 return (EIO);
642
643         DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
644
645         switch (cmd) {
646         case USB_GET_CM_OVER_DATA:
647                 *(int *)data = sc->sc_cm_over_data;
648                 break;
649
650         case USB_SET_CM_OVER_DATA:
651                 if (*(int *)data != sc->sc_cm_over_data) {
652                         /* XXX change it */
653                 }
654                 break;
655
656         default:
657                 DPRINTF(("umodemioctl: unknown\n"));
658                 error = ENOTTY;
659                 break;
660         }
661
662         return (error);
663 }
664
665 void
666 umodem_dtr(struct umodem_softc *sc, int onoff)
667 {
668         DPRINTF(("umodem_modem: onoff=%d\n", onoff));
669
670         if (sc->sc_dtr == onoff)
671                 return;
672         sc->sc_dtr = onoff;
673
674         umodem_set_line_state(sc);
675 }
676
677 void
678 umodem_rts(struct umodem_softc *sc, int onoff)
679 {
680         DPRINTF(("umodem_modem: onoff=%d\n", onoff));
681
682         if (sc->sc_rts == onoff)
683                 return;
684         sc->sc_rts = onoff;
685
686         umodem_set_line_state(sc);
687 }
688
689 void
690 umodem_set_line_state(struct umodem_softc *sc)
691 {
692         usb_device_request_t req;
693         int ls;
694
695         ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
696              (sc->sc_rts ? UCDC_LINE_RTS : 0);
697         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
698         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
699         USETW(req.wValue, ls);
700         USETW(req.wIndex, sc->sc_ctl_iface_no);
701         USETW(req.wLength, 0);
702
703         (void)usbd_do_request(sc->sc_udev, &req, 0);
704
705 }
706
707 void
708 umodem_break(struct umodem_softc *sc, int onoff)
709 {
710         usb_device_request_t req;
711
712         DPRINTF(("umodem_break: onoff=%d\n", onoff));
713
714         if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
715                 return;
716
717         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
718         req.bRequest = UCDC_SEND_BREAK;
719         USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
720         USETW(req.wIndex, sc->sc_ctl_iface_no);
721         USETW(req.wLength, 0);
722
723         (void)usbd_do_request(sc->sc_udev, &req, 0);
724 }
725
726 void
727 umodem_set(void *addr, int portno, int reg, int onoff)
728 {
729         struct umodem_softc *sc = addr;
730
731         switch (reg) {
732         case UCOM_SET_DTR:
733                 umodem_dtr(sc, onoff);
734                 break;
735         case UCOM_SET_RTS:
736                 umodem_rts(sc, onoff);
737                 break;
738         case UCOM_SET_BREAK:
739                 umodem_break(sc, onoff);
740                 break;
741         default:
742                 break;
743         }
744 }
745
746 usbd_status
747 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
748 {
749         usb_device_request_t req;
750         usbd_status err;
751
752         DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
753                  UGETDW(state->dwDTERate), state->bCharFormat,
754                  state->bParityType, state->bDataBits));
755
756         if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
757                 DPRINTF(("umodem_set_line_coding: already set\n"));
758                 return (USBD_NORMAL_COMPLETION);
759         }
760
761         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
762         req.bRequest = UCDC_SET_LINE_CODING;
763         USETW(req.wValue, 0);
764         USETW(req.wIndex, sc->sc_ctl_iface_no);
765         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
766
767         err = usbd_do_request(sc->sc_udev, &req, state);
768         if (err) {
769                 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
770                          usbd_errstr(err)));
771                 return (err);
772         }
773
774         sc->sc_line_state = *state;
775
776         return (USBD_NORMAL_COMPLETION);
777 }
778
779 void *
780 umodem_get_desc(usbd_device_handle dev, int type, int subtype)
781 {
782         usb_descriptor_t *desc;
783         usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
784         uByte *p = (uByte *)cd;
785         uByte *end = p + UGETW(cd->wTotalLength);
786
787         while (p < end) {
788                 desc = (usb_descriptor_t *)p;
789                 if (desc->bDescriptorType == type &&
790                     desc->bDescriptorSubtype == subtype)
791                         return (desc);
792                 p += desc->bLength;
793         }
794
795         return (0);
796 }
797
798 usbd_status
799 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
800 {
801         usb_device_request_t req;
802         usbd_status err;
803         usb_cdc_abstract_state_t ast;
804
805         DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
806                  state));
807
808         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
809         req.bRequest = UCDC_SET_COMM_FEATURE;
810         USETW(req.wValue, feature);
811         USETW(req.wIndex, sc->sc_ctl_iface_no);
812         USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
813         USETW(ast.wState, state);
814
815         err = usbd_do_request(sc->sc_udev, &req, &ast);
816         if (err) {
817                 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
818                          feature, usbd_errstr(err)));
819                 return (err);
820         }
821
822         return (USBD_NORMAL_COMPLETION);
823 }
824
825 USB_DETACH(umodem)
826 {
827         USB_DETACH_START(umodem, sc);
828         int rv = 0;
829
830         DPRINTF(("umodem_detach: sc=%p\n", sc));
831
832         if (sc->sc_notify_pipe != NULL) {
833                 usbd_abort_pipe(sc->sc_notify_pipe);
834                 usbd_close_pipe(sc->sc_notify_pipe);
835                 sc->sc_notify_pipe = NULL;
836         }
837
838         sc->sc_ucom.sc_dying = 1;
839         rv = ucom_detach(&sc->sc_ucom);
840
841         return (rv);
842 }