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