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