Merge from vendor branch GCC:
[dragonfly.git] / sys / dev / usbmisc / umodem / umodem.c
1 /*
2  * $NetBSD: umodem.c,v 1.5 1999/01/08 11:58:25 augustss Exp $
3  * $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $
4  * $FreeBSD: src/sys/dev/usb/umodem.c,v 1.48 2003/08/24 17:55:55 obrien Exp $
5  * $DragonFly: src/sys/dev/usbmisc/umodem/umodem.c,v 1.12 2006/12/22 23:26:26 swildner Exp $
6  */
7
8 /*-
9  * Copyright (c) 2003, M. Warner Losh <imp@freebsd.org>.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1998 The NetBSD Foundation, Inc.
36  * All rights reserved.
37  *
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Lennart Augustsson (lennart@augustsson.net) at
40  * Carlstedt Research & Technology.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *        This product includes software developed by the NetBSD
53  *        Foundation, Inc. and its contributors.
54  * 4. Neither the name of The NetBSD Foundation nor the names of its
55  *    contributors may be used to endorse or promote products derived
56  *    from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68  * POSSIBILITY OF SUCH DAMAGE.
69  */
70
71 /*
72  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
73  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
74  */
75
76 /*
77  * TODO:
78  * - Add error recovery in various places; the big problem is what
79  *   to do in a callback if there is an error.
80  * - Implement a Call Device for modems without multiplexed commands.
81  *
82  */
83
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/kernel.h>
87 #include <sys/ioccom.h>
88 #include <sys/conf.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/vnode.h>
95 #include <sys/bus.h>
96 #include <sys/poll.h>
97
98 #include <bus/usb/usb.h>
99 #include <bus/usb/usbcdc.h>
100
101 #include <bus/usb/usbdi.h>
102 #include <bus/usb/usbdi_util.h>
103 #include <bus/usb/usbdevs.h>
104 #include <dev/usbmisc/ucom/ucomvar.h>
105 #include <bus/usb/usb_quirks.h>
106
107 #include <bus/usb/usbdevs.h>
108
109 #ifdef USB_DEBUG
110 int     umodemdebug = 0;
111 SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
112 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
113            &umodemdebug, 0, "umodem debug level");
114 #define DPRINTFN(n, x)  if (umodemdebug > (n)) logprintf x
115 #else
116 #define DPRINTFN(n, x)
117 #endif
118 #define DPRINTF(x) DPRINTFN(0, x)
119
120 /*
121  * These are the maximum number of bytes transferred per frame.
122  * If some really high speed devices should use this driver they
123  * may need to be increased, but this is good enough for normal modems.
124  */
125 #define UMODEMIBUFSIZE 64
126 #define UMODEMOBUFSIZE 256
127
128 #define UMODEM_MODVER                   1       /* module version */
129
130 struct umodem_softc {
131         struct ucom_softc       sc_ucom;
132
133         USBBASEDEVICE           sc_dev;         /* base device */
134
135         usbd_device_handle      sc_udev;        /* USB device */
136
137         int                     sc_ctl_iface_no;
138         usbd_interface_handle   sc_ctl_iface;   /* control interface */
139         int                     sc_data_iface_no;
140         usbd_interface_handle   sc_data_iface;  /* data interface */
141
142         int                     sc_cm_cap;      /* CM capabilities */
143         int                     sc_acm_cap;     /* ACM capabilities */
144
145         int                     sc_cm_over_data;
146
147         usb_cdc_line_state_t    sc_line_state;  /* current line state */
148         u_char                  sc_dtr;         /* current DTR state */
149         u_char                  sc_rts;         /* current RTS state */
150
151         u_char                  sc_opening;     /* lock during open */
152
153         int                     sc_ctl_notify;  /* Notification endpoint */
154         usbd_pipe_handle        sc_notify_pipe; /* Notification pipe */
155         usb_cdc_notification_t  sc_notify_buf;  /* Notification structure */
156         u_char                  sc_lsr;         /* Local status register */
157         u_char                  sc_msr;         /* Modem status register */
158 };
159
160 Static void     *umodem_get_desc(usbd_device_handle dev, int type, int subtype);
161 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
162                                            int feature, int state);
163 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
164                                           usb_cdc_line_state_t *state);
165
166 Static void     umodem_get_caps(usbd_device_handle, int *, int *);
167
168 Static void     umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
169 Static void     umodem_set(void *, int, int, int);
170 Static void     umodem_dtr(struct umodem_softc *, int);
171 Static void     umodem_rts(struct umodem_softc *, int);
172 Static void     umodem_break(struct umodem_softc *, int);
173 Static void     umodem_set_line_state(struct umodem_softc *);
174 Static int      umodem_param(void *, int, struct termios *);
175 Static int      umodem_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
176 Static int      umodem_open(void *, int portno);
177 Static void     umodem_close(void *, int portno);
178 Static void     umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
179
180 Static struct ucom_callback umodem_callback = {
181         umodem_get_status,
182         umodem_set,
183         umodem_param,
184         umodem_ioctl,
185         umodem_open,
186         umodem_close,
187         NULL,
188         NULL,
189 };
190
191 Static device_probe_t umodem_match;
192 Static device_attach_t umodem_attach;
193 Static device_detach_t umodem_detach;
194
195 Static device_method_t umodem_methods[] = {
196         /* Device interface */
197         DEVMETHOD(device_probe, umodem_match),
198         DEVMETHOD(device_attach, umodem_attach),
199         DEVMETHOD(device_detach, umodem_detach),
200         { 0, 0 }
201 };
202
203 Static driver_t umodem_driver = {
204         "ucom",
205         umodem_methods,
206         sizeof (struct umodem_softc)
207 };
208
209 DRIVER_MODULE(umodem, uhub, umodem_driver, ucom_devclass, usbd_driver_load, 0);
210 MODULE_DEPEND(umodem, usb, 1, 1, 1);
211 MODULE_DEPEND(umodem, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
212 MODULE_VERSION(umodem, UMODEM_MODVER);
213
214 USB_MATCH(umodem)
215 {
216         USB_MATCH_START(umodem, uaa);
217         usb_interface_descriptor_t *id;
218         int cm, acm;
219
220         if (uaa->iface == NULL)
221                 return (UMATCH_NONE);
222
223         id = usbd_get_interface_descriptor(uaa->iface);
224         if (id == NULL ||
225             id->bInterfaceClass != UICLASS_CDC ||
226             id->bInterfaceSubClass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
227             id->bInterfaceProtocol != UIPROTO_CDC_AT)
228                 return (UMATCH_NONE);
229
230         umodem_get_caps(uaa->device, &cm, &acm);
231         if (!(cm & USB_CDC_CM_DOES_CM) ||
232             !(cm & USB_CDC_CM_OVER_DATA) ||
233             !(acm & USB_CDC_ACM_HAS_LINE))
234                 return (UMATCH_NONE);
235
236         return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
237 }
238
239 USB_ATTACH(umodem)
240 {
241         USB_ATTACH_START(umodem, sc, uaa);
242         usbd_device_handle dev = uaa->device;
243         usb_interface_descriptor_t *id;
244         usb_endpoint_descriptor_t *ed;
245         usb_cdc_cm_descriptor_t *cmd;
246         char *devinfo = NULL;
247         const char *devname;
248         usbd_status err;
249         int data_ifcno;
250         int i;
251         struct ucom_softc *ucom;
252
253         devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
254         usbd_devinfo(dev, 0, devinfo);
255         ucom = &sc->sc_ucom;
256         ucom->sc_dev = self;
257         sc->sc_dev = self;
258         device_set_desc_copy(self, devinfo);
259         ucom->sc_udev = dev;
260         ucom->sc_iface = uaa->iface;
261         /*USB_ATTACH_SETUP; */
262
263         sc->sc_udev = dev;
264         sc->sc_ctl_iface = uaa->iface;
265
266         devname = USBDEVNAME(sc->sc_dev);
267         /* XXX ? use something else ? XXX */
268         id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
269         kprintf("%s: %s, iclass %d/%d\n", devname, devinfo,
270           id->bInterfaceClass, id->bInterfaceSubClass);
271         sc->sc_ctl_iface_no = id->bInterfaceNumber;
272
273         umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
274
275         /* Get the data interface no. */
276         cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
277         if (cmd == NULL) {
278                 kprintf("%s: no CM descriptor\n", devname);
279                 goto bad;
280         }
281         sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
282
283         kprintf("%s: data interface %d, has %sCM over data, has %sbreak\n",
284                devname, data_ifcno,
285                sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
286                sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
287
288         /* Get the data interface too. */
289         for (i = 0; i < uaa->nifaces; i++) {
290                 if (uaa->ifaces[i] != NULL) {
291                         id = usbd_get_interface_descriptor(uaa->ifaces[i]);
292                         if (id != NULL && id->bInterfaceNumber == data_ifcno) {
293                                 sc->sc_data_iface = uaa->ifaces[i];
294                                 uaa->ifaces[i] = NULL;
295                         }
296                 }
297         }
298         if (sc->sc_data_iface == NULL) {
299                 kprintf("%s: no data interface\n", devname);
300                 goto bad;
301         }
302         ucom->sc_iface = sc->sc_data_iface;
303
304         /*
305          * Find the bulk endpoints.
306          * Iterate over all endpoints in the data interface and take note.
307          */
308         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
309
310         id = usbd_get_interface_descriptor(sc->sc_data_iface);
311         for (i = 0; i < id->bNumEndpoints; i++) {
312                 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
313                 if (ed == NULL) {
314                         kprintf("%s: no endpoint descriptor for %d\n", devname,
315                             i);
316                         goto bad;
317                 }
318                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
319                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
320                         ucom->sc_bulkin_no = ed->bEndpointAddress;
321                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
322                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
323                         ucom->sc_bulkout_no = ed->bEndpointAddress;
324                 }
325         }
326
327         if (ucom->sc_bulkin_no == -1) {
328                 kprintf("%s: Could not find data bulk in\n", devname);
329                 goto bad;
330         }
331         if (ucom->sc_bulkout_no == -1) {
332                 kprintf("%s: Could not find data bulk out\n", devname);
333                 goto bad;
334         }
335
336         if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
337                 DPRINTF(("Quirk says to assume CM over data\n"));
338                 sc->sc_cm_over_data = 1;
339         } else {
340                 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
341                         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
342                                 err = umodem_set_comm_feature(sc,
343                                     UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
344                         else
345                                 err = 0;
346                         if (err) {
347                                 kprintf("%s: could not set data multiplex mode\n",
348                                     devname);
349                                 goto bad;
350                         }
351                         sc->sc_cm_over_data = 1;
352                 }
353         }
354
355         /*
356          * The standard allows for notification messages (to indicate things
357          * like a modem hangup) to come in via an interrupt endpoint
358          * off of the control interface.  Iterate over the endpoints on
359          * the control interface and see if there are any interrupt
360          * endpoints; if there are, then register it.
361          */
362
363         sc->sc_ctl_notify = -1;
364         sc->sc_notify_pipe = NULL;
365
366         for (i = 0; i < id->bNumEndpoints; i++) {
367                 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
368                 if (ed == NULL)
369                         continue;
370
371                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
372                     (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
373                         kprintf("%s: status change notification available\n",
374                             devname);
375                         sc->sc_ctl_notify = ed->bEndpointAddress;
376                 }
377         }
378
379         sc->sc_dtr = -1;
380
381         ucom->sc_parent = sc;
382         ucom->sc_portno = UCOM_UNK_PORTNO;
383         /* bulkin, bulkout set above */
384         ucom->sc_ibufsize = UMODEMIBUFSIZE;
385         ucom->sc_obufsize = UMODEMOBUFSIZE;
386         ucom->sc_ibufsizepad = UMODEMIBUFSIZE;
387         ucom->sc_opkthdrlen = 0;
388         ucom->sc_callback = &umodem_callback;
389
390         ucom_attach(&sc->sc_ucom);
391
392         kfree(devinfo, M_USBDEV);
393         USB_ATTACH_SUCCESS_RETURN;
394
395  bad:
396         ucom->sc_dying = 1;
397         kfree(devinfo, M_USBDEV);
398         USB_ATTACH_ERROR_RETURN;
399 }
400
401 Static int
402 umodem_open(void *addr, int portno)
403 {
404         struct umodem_softc *sc = addr;
405         int err;
406
407         DPRINTF(("umodem_open: sc=%p\n", sc));
408
409         if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
410                 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
411                     USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
412                     &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
413                     umodem_intr, USBD_DEFAULT_INTERVAL);
414
415                 if (err) {
416                         DPRINTF(("Failed to establish notify pipe: %s\n",
417                                 usbd_errstr(err)));
418                         return EIO;
419                 }
420         }
421
422         return 0;
423 }
424
425 Static void
426 umodem_close(void *addr, int portno)
427 {
428         struct umodem_softc *sc = addr;
429         int err;
430
431         DPRINTF(("umodem_close: sc=%p\n", sc));
432
433         if (sc->sc_notify_pipe != NULL) {
434                 err = usbd_abort_pipe(sc->sc_notify_pipe);
435                 if (err)
436                         kprintf("%s: abort notify pipe failed: %s\n",
437                             USBDEVNAME(sc->sc_dev), usbd_errstr(err));
438                 err = usbd_close_pipe(sc->sc_notify_pipe);
439                 if (err)
440                         kprintf("%s: close notify pipe failed: %s\n",
441                             USBDEVNAME(sc->sc_dev), usbd_errstr(err));
442                 sc->sc_notify_pipe = NULL;
443         }
444 }
445
446 Static void
447 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
448 {
449         struct umodem_softc *sc = priv;
450         u_char mstatus;
451
452         if (sc->sc_ucom.sc_dying)
453                 return;
454
455         if (status != USBD_NORMAL_COMPLETION) {
456                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
457                         return;
458                 kprintf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
459                        usbd_errstr(status));
460                 return;
461         }
462
463         if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
464                 DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
465                          USBDEVNAME(sc->sc_dev),
466                          sc->sc_notify_buf.bmRequestType));
467                 return;
468         }
469
470         switch (sc->sc_notify_buf.bNotification) {
471         case UCDC_N_SERIAL_STATE:
472                 /*
473                  * Set the serial state in ucom driver based on
474                  * the bits from the notify message
475                  */
476                 if (UGETW(sc->sc_notify_buf.wLength) != 2) {
477                         kprintf("%s: Invalid notification length! (%d)\n",
478                                USBDEVNAME(sc->sc_dev),
479                                UGETW(sc->sc_notify_buf.wLength));
480                         break;
481                 }
482                 DPRINTF(("%s: notify bytes = %02x%02x\n",
483                          USBDEVNAME(sc->sc_dev),
484                          sc->sc_notify_buf.data[0],
485                          sc->sc_notify_buf.data[1]));
486                 /* Currently, lsr is always zero. */
487                 sc->sc_lsr = sc->sc_msr = 0;
488                 mstatus = sc->sc_notify_buf.data[0];
489
490                 if (ISSET(mstatus, UCDC_N_SERIAL_RI))
491                         sc->sc_msr |= UMSR_RI;
492                 if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
493                         sc->sc_msr |= UMSR_DSR;
494                 if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
495                         sc->sc_msr |= UMSR_DCD;
496                 ucom_status_change(&sc->sc_ucom);
497                 break;
498         default:
499                 DPRINTF(("%s: unknown notify message: %02x\n",
500                          USBDEVNAME(sc->sc_dev),
501                          sc->sc_notify_buf.bNotification));
502                 break;
503         }
504 }
505
506 void
507 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm)
508 {
509         usb_cdc_cm_descriptor_t *cmd;
510         usb_cdc_acm_descriptor_t *cad;
511
512         *cm = *acm = 0;
513
514         cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
515         if (cmd == NULL) {
516                 DPRINTF(("umodem_get_desc: no CM desc\n"));
517                 return;
518         }
519         *cm = cmd->bmCapabilities;
520
521         cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
522         if (cad == NULL) {
523                 DPRINTF(("umodem_get_desc: no ACM desc\n"));
524                 return;
525         }
526         *acm = cad->bmCapabilities;
527 }
528
529 void
530 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
531 {
532         struct umodem_softc *sc = addr;
533
534         DPRINTF(("umodem_get_status:\n"));
535
536         if (lsr != NULL)
537                 *lsr = sc->sc_lsr;
538         if (msr != NULL)
539                 *msr = sc->sc_msr;
540 }
541
542 int
543 umodem_param(void *addr, int portno, struct termios *t)
544 {
545         struct umodem_softc *sc = addr;
546         usbd_status err;
547         usb_cdc_line_state_t ls;
548
549         DPRINTF(("umodem_param: sc=%p\n", sc));
550
551         USETDW(ls.dwDTERate, t->c_ospeed);
552         if (ISSET(t->c_cflag, CSTOPB))
553                 ls.bCharFormat = UCDC_STOP_BIT_2;
554         else
555                 ls.bCharFormat = UCDC_STOP_BIT_1;
556         if (ISSET(t->c_cflag, PARENB)) {
557                 if (ISSET(t->c_cflag, PARODD))
558                         ls.bParityType = UCDC_PARITY_ODD;
559                 else
560                         ls.bParityType = UCDC_PARITY_EVEN;
561         } else
562                 ls.bParityType = UCDC_PARITY_NONE;
563         switch (ISSET(t->c_cflag, CSIZE)) {
564         case CS5:
565                 ls.bDataBits = 5;
566                 break;
567         case CS6:
568                 ls.bDataBits = 6;
569                 break;
570         case CS7:
571                 ls.bDataBits = 7;
572                 break;
573         case CS8:
574                 ls.bDataBits = 8;
575                 break;
576         }
577
578         err = umodem_set_line_coding(sc, &ls);
579         if (err) {
580                 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
581                 return (ENOTTY);
582         }
583         return (0);
584 }
585
586 int
587 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
588              usb_proc_ptr p)
589 {
590         struct umodem_softc *sc = addr;
591         int error = 0;
592
593         if (sc->sc_ucom.sc_dying)
594                 return (EIO);
595
596         DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
597
598         switch (cmd) {
599         case USB_GET_CM_OVER_DATA:
600                 *(int *)data = sc->sc_cm_over_data;
601                 break;
602
603         case USB_SET_CM_OVER_DATA:
604                 if (*(int *)data != sc->sc_cm_over_data) {
605                         /* XXX change it */
606                 }
607                 break;
608
609         default:
610                 DPRINTF(("umodemioctl: unknown\n"));
611                 error = ENOTTY;
612                 break;
613         }
614
615         return (error);
616 }
617
618 void
619 umodem_dtr(struct umodem_softc *sc, int onoff)
620 {
621         DPRINTF(("umodem_modem: onoff=%d\n", onoff));
622
623         if (sc->sc_dtr == onoff)
624                 return;
625         sc->sc_dtr = onoff;
626
627         umodem_set_line_state(sc);
628 }
629
630 void
631 umodem_rts(struct umodem_softc *sc, int onoff)
632 {
633         DPRINTF(("umodem_modem: onoff=%d\n", onoff));
634
635         if (sc->sc_rts == onoff)
636                 return;
637         sc->sc_rts = onoff;
638
639         umodem_set_line_state(sc);
640 }
641
642 void
643 umodem_set_line_state(struct umodem_softc *sc)
644 {
645         usb_device_request_t req;
646         int ls;
647
648         ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
649              (sc->sc_rts ? UCDC_LINE_RTS : 0);
650         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
651         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
652         USETW(req.wValue, ls);
653         USETW(req.wIndex, sc->sc_ctl_iface_no);
654         USETW(req.wLength, 0);
655
656         (void)usbd_do_request(sc->sc_udev, &req, 0);
657
658 }
659
660 void
661 umodem_break(struct umodem_softc *sc, int onoff)
662 {
663         usb_device_request_t req;
664
665         DPRINTF(("umodem_break: onoff=%d\n", onoff));
666
667         if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
668                 return;
669
670         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
671         req.bRequest = UCDC_SEND_BREAK;
672         USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
673         USETW(req.wIndex, sc->sc_ctl_iface_no);
674         USETW(req.wLength, 0);
675
676         (void)usbd_do_request(sc->sc_udev, &req, 0);
677 }
678
679 void
680 umodem_set(void *addr, int portno, int reg, int onoff)
681 {
682         struct umodem_softc *sc = addr;
683
684         switch (reg) {
685         case UCOM_SET_DTR:
686                 umodem_dtr(sc, onoff);
687                 break;
688         case UCOM_SET_RTS:
689                 umodem_rts(sc, onoff);
690                 break;
691         case UCOM_SET_BREAK:
692                 umodem_break(sc, onoff);
693                 break;
694         default:
695                 break;
696         }
697 }
698
699 usbd_status
700 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
701 {
702         usb_device_request_t req;
703         usbd_status err;
704
705         DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
706                  UGETDW(state->dwDTERate), state->bCharFormat,
707                  state->bParityType, state->bDataBits));
708
709         if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
710                 DPRINTF(("umodem_set_line_coding: already set\n"));
711                 return (USBD_NORMAL_COMPLETION);
712         }
713
714         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
715         req.bRequest = UCDC_SET_LINE_CODING;
716         USETW(req.wValue, 0);
717         USETW(req.wIndex, sc->sc_ctl_iface_no);
718         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
719
720         err = usbd_do_request(sc->sc_udev, &req, state);
721         if (err) {
722                 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
723                          usbd_errstr(err)));
724                 return (err);
725         }
726
727         sc->sc_line_state = *state;
728
729         return (USBD_NORMAL_COMPLETION);
730 }
731
732 void *
733 umodem_get_desc(usbd_device_handle dev, int type, int subtype)
734 {
735         usb_descriptor_t *desc;
736         usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
737         uByte *p = (uByte *)cd;
738         uByte *end = p + UGETW(cd->wTotalLength);
739
740         while (p < end) {
741                 desc = (usb_descriptor_t *)p;
742                 if (desc->bDescriptorType == type &&
743                     desc->bDescriptorSubtype == subtype)
744                         return (desc);
745                 p += desc->bLength;
746         }
747
748         return (0);
749 }
750
751 usbd_status
752 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
753 {
754         usb_device_request_t req;
755         usbd_status err;
756         usb_cdc_abstract_state_t ast;
757
758         DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
759                  state));
760
761         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
762         req.bRequest = UCDC_SET_COMM_FEATURE;
763         USETW(req.wValue, feature);
764         USETW(req.wIndex, sc->sc_ctl_iface_no);
765         USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
766         USETW(ast.wState, state);
767
768         err = usbd_do_request(sc->sc_udev, &req, &ast);
769         if (err) {
770                 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
771                          feature, usbd_errstr(err)));
772                 return (err);
773         }
774
775         return (USBD_NORMAL_COMPLETION);
776 }
777
778 USB_DETACH(umodem)
779 {
780         USB_DETACH_START(umodem, sc);
781         int rv = 0;
782
783         DPRINTF(("umodem_detach: sc=%p\n", sc));
784
785         if (sc->sc_notify_pipe != NULL) {
786                 usbd_abort_pipe(sc->sc_notify_pipe);
787                 usbd_close_pipe(sc->sc_notify_pipe);
788                 sc->sc_notify_pipe = NULL;
789         }
790
791         sc->sc_ucom.sc_dying = 1;
792         rv = ucom_detach(&sc->sc_ucom);
793
794         return (rv);
795 }