881202576899cb64bb9ebc3b16386cc88fd04c0e
[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.22 2007/11/06 07:37:01 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/bus.h>
94 #include <sys/poll.h>
95
96 #include <bus/usb/usb.h>
97 #include <bus/usb/usbcdc.h>
98
99 #include <bus/usb/usbdi.h>
100 #include <bus/usb/usbdi_util.h>
101 #include <dev/usbmisc/ucom/ucomvar.h>
102 #include <bus/usb/usb_quirks.h>
103
104 #ifdef USB_DEBUG
105 int     umodemdebug = 0;
106 SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
107 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
108            &umodemdebug, 0, "umodem debug level");
109 #define DPRINTFN(n, x) do { if (umodemdebug > (n)) kprintf x; } while (0)
110 #else
111 #define DPRINTFN(n, x)
112 #endif
113 #define DPRINTF(x) DPRINTFN(0, x)
114
115 /*
116  * These are the maximum number of bytes transferred per frame. These
117  * values were increased from 64/256 used in older versions of the driver
118  * to better support EVDO wireless PPP. Old values were good enough for
119  * normal modems, but not for really high speed devices.
120  *
121  * The sizes should not be increased further, or there will be problems
122  * with contiguous storage allocation.
123  */
124 #define UMODEMIBUFSIZE 4096
125 #define UMODEMOBUFSIZE 4096
126
127 #define UMODEM_MODVER                   1       /* module version */
128
129 struct umodem_softc {
130         struct ucom_softc       sc_ucom;
131
132         device_t                sc_dev;         /* base device */
133
134         usbd_device_handle      sc_udev;        /* USB device */
135
136         int                     sc_ctl_iface_no;
137         usbd_interface_handle   sc_ctl_iface;   /* control interface */
138         int                     sc_data_iface_no;
139         usbd_interface_handle   sc_data_iface;  /* data interface */
140
141         int                     sc_cm_cap;      /* CM capabilities */
142         int                     sc_acm_cap;     /* ACM capabilities */
143
144         int                     sc_cm_over_data;
145
146         usb_cdc_line_state_t    sc_line_state;  /* current line state */
147         u_char                  sc_dtr;         /* current DTR state */
148         u_char                  sc_rts;         /* current RTS state */
149
150         u_char                  sc_opening;     /* lock during open */
151
152         int                     sc_ctl_notify;  /* Notification endpoint */
153         usbd_pipe_handle        sc_notify_pipe; /* Notification pipe */
154         usb_cdc_notification_t  sc_notify_buf;  /* Notification structure */
155         u_char                  sc_lsr;         /* Local status register */
156         u_char                  sc_msr;         /* Modem status register */
157 };
158
159 static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
160                                            int feature, int state);
161 static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
162                                           usb_cdc_line_state_t *state);
163
164 static int      umodem_get_caps(usbd_device_handle, int *, int *,
165                                 usb_interface_descriptor_t *);
166
167 static void     umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
168 static void     umodem_set(void *, int, int, int);
169 static void     umodem_dtr(struct umodem_softc *, int);
170 static void     umodem_rts(struct umodem_softc *, int);
171 static void     umodem_break(struct umodem_softc *, int);
172 static void     umodem_set_line_state(struct umodem_softc *);
173 static int      umodem_param(void *, int, struct termios *);
174 static int      umodem_ioctl(void *, int, u_long, caddr_t, int, struct thread * );
175 static int      umodem_open(void *, int portno);
176 static void     umodem_close(void *, int portno);
177 static void     umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
178
179 static struct ucom_callback umodem_callback = {
180         umodem_get_status,
181         umodem_set,
182         umodem_param,
183         umodem_ioctl,
184         umodem_open,
185         umodem_close,
186         NULL,
187         NULL,
188 };
189
190 static device_probe_t umodem_match;
191 static device_attach_t umodem_attach;
192 static device_detach_t umodem_detach;
193
194 static device_method_t umodem_methods[] = {
195         /* Device interface */
196         DEVMETHOD(device_probe, umodem_match),
197         DEVMETHOD(device_attach, umodem_attach),
198         DEVMETHOD(device_detach, umodem_detach),
199         { 0, 0 }
200 };
201
202 static driver_t umodem_driver = {
203         "ucom",
204         umodem_methods,
205         sizeof (struct umodem_softc)
206 };
207
208 DRIVER_MODULE(umodem, uhub, umodem_driver, ucom_devclass, usbd_driver_load, 0);
209 MODULE_DEPEND(umodem, usb, 1, 1, 1);
210 MODULE_DEPEND(umodem, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
211 MODULE_VERSION(umodem, UMODEM_MODVER);
212
213 static int
214 umodem_match(device_t self)
215 {
216         struct usb_attach_arg *uaa = device_get_ivars(self);
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         if (umodem_get_caps(uaa->device, &cm, &acm, id) == -1)
231                 return (UMATCH_NONE);
232
233         return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
234 }
235
236 static int
237 umodem_attach(device_t self)
238 {
239         struct umodem_softc *sc = device_get_softc(self);
240         struct usb_attach_arg *uaa = device_get_ivars(self);
241         usbd_device_handle dev = uaa->device;
242         usb_interface_descriptor_t *id;
243         usb_endpoint_descriptor_t *ed;
244         const char *devname;
245         usbd_status err;
246         int data_ifcno;
247         int i;
248         struct ucom_softc *ucom;
249
250         ucom = &sc->sc_ucom;
251         ucom->sc_dev = self;
252         sc->sc_dev = self;
253         ucom->sc_udev = dev;
254         ucom->sc_iface = uaa->iface;
255
256         sc->sc_udev = dev;
257         sc->sc_ctl_iface = uaa->iface;
258
259         devname = device_get_nameunit(sc->sc_dev);
260         /* XXX ? use something else ? XXX */
261         id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
262         sc->sc_ctl_iface_no = id->bInterfaceNumber;
263
264         sc->sc_data_iface_no = data_ifcno =
265                 umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
266
267         if (data_ifcno == -1) {
268                 kprintf("%s: no pointer to data interface\n", devname);
269                 goto bad;
270         }
271
272         kprintf("%s: data interface %d, has %sCM over data, has %sbreak\n",
273                devname, data_ifcno,
274                sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
275                sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
276
277         /* Get the data interface too. */
278         for (i = 0; i < uaa->nifaces; i++) {
279                 if (uaa->ifaces[i] != NULL) {
280                         id = usbd_get_interface_descriptor(uaa->ifaces[i]);
281                         if (id != NULL && id->bInterfaceNumber == data_ifcno) {
282                                 sc->sc_data_iface = uaa->ifaces[i];
283                                 uaa->ifaces[i] = NULL;
284                         }
285                 }
286         }
287         if (sc->sc_data_iface == NULL) {
288                 kprintf("%s: no data interface\n", devname);
289                 goto bad;
290         }
291         ucom->sc_iface = sc->sc_data_iface;
292
293         /*
294          * Find the bulk endpoints.
295          * Iterate over all endpoints in the data interface and take note.
296          */
297         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
298
299         id = usbd_get_interface_descriptor(sc->sc_data_iface);
300         for (i = 0; i < id->bNumEndpoints; i++) {
301                 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
302                 if (ed == NULL) {
303                         kprintf("%s: no endpoint descriptor for %d\n", devname,
304                             i);
305                         goto bad;
306                 }
307                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
308                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
309                         ucom->sc_bulkin_no = ed->bEndpointAddress;
310                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
311                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
312                         ucom->sc_bulkout_no = ed->bEndpointAddress;
313                 }
314         }
315
316         if (ucom->sc_bulkin_no == -1) {
317                 kprintf("%s: Could not find data bulk in\n", devname);
318                 goto bad;
319         }
320         if (ucom->sc_bulkout_no == -1) {
321                 kprintf("%s: Could not find data bulk out\n", devname);
322                 goto bad;
323         }
324
325         if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
326                 DPRINTF(("Quirk says to assume CM over data\n"));
327                 sc->sc_cm_over_data = 1;
328         } else {
329                 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
330                         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
331                                 err = umodem_set_comm_feature(sc,
332                                     UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
333                         else
334                                 err = 0;
335                         if (err) {
336                                 kprintf("%s: could not set data multiplex mode\n",
337                                     devname);
338                                 goto bad;
339                         }
340                         sc->sc_cm_over_data = 1;
341                 }
342         }
343
344         /*
345          * The standard allows for notification messages (to indicate things
346          * like a modem hangup) to come in via an interrupt endpoint
347          * off of the control interface.  Iterate over the endpoints on
348          * the control interface and see if there are any interrupt
349          * endpoints; if there are, then register it.
350          */
351
352         sc->sc_ctl_notify = -1;
353         sc->sc_notify_pipe = NULL;
354
355         for (i = 0; i < id->bNumEndpoints; i++) {
356                 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
357                 if (ed == NULL)
358                         continue;
359
360                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
361                     (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
362                         kprintf("%s: status change notification available\n",
363                             devname);
364                         sc->sc_ctl_notify = ed->bEndpointAddress;
365                 }
366         }
367
368         sc->sc_dtr = -1;
369
370         ucom->sc_parent = sc;
371         ucom->sc_portno = UCOM_UNK_PORTNO;
372         /* bulkin, bulkout set above */
373         ucom->sc_ibufsize = UMODEMIBUFSIZE;
374         ucom->sc_obufsize = UMODEMOBUFSIZE;
375         ucom->sc_ibufsizepad = UMODEMIBUFSIZE;
376         ucom->sc_opkthdrlen = 0;
377         ucom->sc_callback = &umodem_callback;
378
379         ucom_attach(&sc->sc_ucom);
380
381         return 0;
382
383  bad:
384         ucom->sc_dying = 1;
385         return ENXIO;
386 }
387
388 static int
389 umodem_open(void *addr, int portno)
390 {
391         struct umodem_softc *sc = addr;
392         int err;
393
394         DPRINTF(("umodem_open: sc=%p\n", sc));
395
396         if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
397                 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
398                     USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
399                     &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
400                     umodem_intr, USBD_DEFAULT_INTERVAL);
401
402                 if (err) {
403                         DPRINTF(("Failed to establish notify pipe: %s\n",
404                                 usbd_errstr(err)));
405                         return EIO;
406                 }
407         }
408
409         return 0;
410 }
411
412 static void
413 umodem_close(void *addr, int portno)
414 {
415         struct umodem_softc *sc = addr;
416         int err;
417
418         DPRINTF(("umodem_close: sc=%p\n", sc));
419
420         if (sc->sc_notify_pipe != NULL) {
421                 err = usbd_abort_pipe(sc->sc_notify_pipe);
422                 if (err)
423                         kprintf("%s: abort notify pipe failed: %s\n",
424                             device_get_nameunit(sc->sc_dev), usbd_errstr(err));
425                 err = usbd_close_pipe(sc->sc_notify_pipe);
426                 if (err)
427                         kprintf("%s: close notify pipe failed: %s\n",
428                             device_get_nameunit(sc->sc_dev), usbd_errstr(err));
429                 sc->sc_notify_pipe = NULL;
430         }
431 }
432
433 static void
434 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
435 {
436         struct umodem_softc *sc = priv;
437         u_char mstatus;
438
439         if (sc->sc_ucom.sc_dying)
440                 return;
441
442         if (status != USBD_NORMAL_COMPLETION) {
443                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
444                         return;
445                 kprintf("%s: abnormal status: %s\n", device_get_nameunit(sc->sc_dev),
446                        usbd_errstr(status));
447                 return;
448         }
449
450         if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
451                 DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
452                          device_get_nameunit(sc->sc_dev),
453                          sc->sc_notify_buf.bmRequestType));
454                 return;
455         }
456
457         switch (sc->sc_notify_buf.bNotification) {
458         case UCDC_N_SERIAL_STATE:
459                 /*
460                  * Set the serial state in ucom driver based on
461                  * the bits from the notify message
462                  */
463                 if (UGETW(sc->sc_notify_buf.wLength) != 2) {
464                         kprintf("%s: Invalid notification length! (%d)\n",
465                                device_get_nameunit(sc->sc_dev),
466                                UGETW(sc->sc_notify_buf.wLength));
467                         break;
468                 }
469                 DPRINTF(("%s: notify bytes = %02x%02x\n",
470                          device_get_nameunit(sc->sc_dev),
471                          sc->sc_notify_buf.data[0],
472                          sc->sc_notify_buf.data[1]));
473                 /* Currently, lsr is always zero. */
474                 sc->sc_lsr = sc->sc_msr = 0;
475                 mstatus = sc->sc_notify_buf.data[0];
476
477                 if (ISSET(mstatus, UCDC_N_SERIAL_RI))
478                         sc->sc_msr |= UMSR_RI;
479                 if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
480                         sc->sc_msr |= UMSR_DSR;
481                 if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
482                         sc->sc_msr |= UMSR_DCD;
483                 ucom_status_change(&sc->sc_ucom);
484                 break;
485         default:
486                 DPRINTF(("%s: unknown notify message: %02x\n",
487                          device_get_nameunit(sc->sc_dev),
488                          sc->sc_notify_buf.bNotification));
489                 break;
490         }
491 }
492
493 int
494 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm,
495                 usb_interface_descriptor_t *id)
496 {
497         const usb_cdc_cm_descriptor_t *cmd;
498         const usb_cdc_acm_descriptor_t *cad;
499         const usb_cdc_union_descriptor_t *cud;
500
501         *cm = *acm = 0;
502
503         cmd = (const usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
504                                                         UDESC_CS_INTERFACE,
505                                                         UDESCSUB_CDC_CM, id);
506         if (cmd == NULL)
507                 DPRINTF(("umodem_get_caps: no CM desc\n"));
508         else
509                 *cm = cmd->bmCapabilities;
510
511         cad = (const usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
512                                                         UDESC_CS_INTERFACE,
513                                                         UDESCSUB_CDC_ACM, id);
514         if (cad == NULL)
515                 DPRINTF(("umodem_get_caps: no ACM desc\n"));
516         else
517                 *acm = cad->bmCapabilities;
518
519         cud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
520                                                         UDESC_CS_INTERFACE,
521                                                         UDESCSUB_CDC_UNION, id);
522         if (cud == NULL)
523                 DPRINTF(("umodem_get_caps: no UNION desc\n"));
524
525         return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
526 }
527
528 void
529 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
530 {
531         struct umodem_softc *sc = addr;
532
533         DPRINTF(("umodem_get_status:\n"));
534
535         if (lsr != NULL)
536                 *lsr = sc->sc_lsr;
537         if (msr != NULL)
538                 *msr = sc->sc_msr;
539 }
540
541 int
542 umodem_param(void *addr, int portno, struct termios *t)
543 {
544         struct umodem_softc *sc = addr;
545         usbd_status err;
546         usb_cdc_line_state_t ls;
547
548         DPRINTF(("umodem_param: sc=%p\n", sc));
549
550         USETDW(ls.dwDTERate, t->c_ospeed);
551         if (ISSET(t->c_cflag, CSTOPB))
552                 ls.bCharFormat = UCDC_STOP_BIT_2;
553         else
554                 ls.bCharFormat = UCDC_STOP_BIT_1;
555         if (ISSET(t->c_cflag, PARENB)) {
556                 if (ISSET(t->c_cflag, PARODD))
557                         ls.bParityType = UCDC_PARITY_ODD;
558                 else
559                         ls.bParityType = UCDC_PARITY_EVEN;
560         } else
561                 ls.bParityType = UCDC_PARITY_NONE;
562         switch (ISSET(t->c_cflag, CSIZE)) {
563         case CS5:
564                 ls.bDataBits = 5;
565                 break;
566         case CS6:
567                 ls.bDataBits = 6;
568                 break;
569         case CS7:
570                 ls.bDataBits = 7;
571                 break;
572         case CS8:
573                 ls.bDataBits = 8;
574                 break;
575         }
576
577         err = umodem_set_line_coding(sc, &ls);
578         if (err) {
579                 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
580                 return (ENOTTY);
581         }
582         return (0);
583 }
584
585 int
586 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
587              struct thread * p)
588 {
589         struct umodem_softc *sc = addr;
590         int error = 0;
591
592         if (sc->sc_ucom.sc_dying)
593                 return (EIO);
594
595         DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
596
597         switch (cmd) {
598         case USB_GET_CM_OVER_DATA:
599                 *(int *)data = sc->sc_cm_over_data;
600                 break;
601
602         case USB_SET_CM_OVER_DATA:
603                 if (*(int *)data != sc->sc_cm_over_data) {
604                         /* XXX change it */
605                 }
606                 break;
607
608         default:
609                 DPRINTF(("umodem_ioctl: unknown\n"));
610                 error = ENOTTY;
611                 break;
612         }
613
614         return (error);
615 }
616
617 void
618 umodem_dtr(struct umodem_softc *sc, int onoff)
619 {
620         DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
621
622         if (sc->sc_dtr == onoff)
623                 return;
624         sc->sc_dtr = onoff;
625
626         umodem_set_line_state(sc);
627 }
628
629 void
630 umodem_rts(struct umodem_softc *sc, int onoff)
631 {
632         DPRINTF(("umodem_rts: onoff=%d\n", onoff));
633
634         if (sc->sc_rts == onoff)
635                 return;
636         sc->sc_rts = onoff;
637
638         umodem_set_line_state(sc);
639 }
640
641 void
642 umodem_set_line_state(struct umodem_softc *sc)
643 {
644         usb_device_request_t req;
645         int ls;
646
647         ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
648              (sc->sc_rts ? UCDC_LINE_RTS : 0);
649         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
650         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
651         USETW(req.wValue, ls);
652         USETW(req.wIndex, sc->sc_ctl_iface_no);
653         USETW(req.wLength, 0);
654
655         (void)usbd_do_request(sc->sc_udev, &req, 0);
656
657 }
658
659 void
660 umodem_break(struct umodem_softc *sc, int onoff)
661 {
662         usb_device_request_t req;
663
664         DPRINTF(("umodem_break: onoff=%d\n", onoff));
665
666         if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
667                 return;
668
669         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
670         req.bRequest = UCDC_SEND_BREAK;
671         USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
672         USETW(req.wIndex, sc->sc_ctl_iface_no);
673         USETW(req.wLength, 0);
674
675         (void)usbd_do_request(sc->sc_udev, &req, 0);
676 }
677
678 void
679 umodem_set(void *addr, int portno, int reg, int onoff)
680 {
681         struct umodem_softc *sc = addr;
682
683         switch (reg) {
684         case UCOM_SET_DTR:
685                 umodem_dtr(sc, onoff);
686                 break;
687         case UCOM_SET_RTS:
688                 umodem_rts(sc, onoff);
689                 break;
690         case UCOM_SET_BREAK:
691                 umodem_break(sc, onoff);
692                 break;
693         default:
694                 break;
695         }
696 }
697
698 usbd_status
699 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
700 {
701         usb_device_request_t req;
702         usbd_status err;
703
704         DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
705                  UGETDW(state->dwDTERate), state->bCharFormat,
706                  state->bParityType, state->bDataBits));
707
708         if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
709                 DPRINTF(("umodem_set_line_coding: already set\n"));
710                 return (USBD_NORMAL_COMPLETION);
711         }
712
713         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
714         req.bRequest = UCDC_SET_LINE_CODING;
715         USETW(req.wValue, 0);
716         USETW(req.wIndex, sc->sc_ctl_iface_no);
717         USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
718
719         err = usbd_do_request(sc->sc_udev, &req, state);
720         if (err) {
721                 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
722                          usbd_errstr(err)));
723                 return (err);
724         }
725
726         sc->sc_line_state = *state;
727
728         return (USBD_NORMAL_COMPLETION);
729 }
730
731 usbd_status
732 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
733 {
734         usb_device_request_t req;
735         usbd_status err;
736         usb_cdc_abstract_state_t ast;
737
738         DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
739                  state));
740
741         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
742         req.bRequest = UCDC_SET_COMM_FEATURE;
743         USETW(req.wValue, feature);
744         USETW(req.wIndex, sc->sc_ctl_iface_no);
745         USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
746         USETW(ast.wState, state);
747
748         err = usbd_do_request(sc->sc_udev, &req, &ast);
749         if (err) {
750                 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
751                          feature, usbd_errstr(err)));
752                 return (err);
753         }
754
755         return (USBD_NORMAL_COMPLETION);
756 }
757
758 static int
759 umodem_detach(device_t self)
760 {
761         struct umodem_softc *sc = device_get_softc(self);
762         int rv = 0;
763
764         DPRINTF(("umodem_detach: sc=%p\n", sc));
765
766         if (sc->sc_notify_pipe != NULL) {
767                 usbd_abort_pipe(sc->sc_notify_pipe);
768                 usbd_close_pipe(sc->sc_notify_pipe);
769                 sc->sc_notify_pipe = NULL;
770         }
771
772         sc->sc_ucom.sc_dying = 1;
773         rv = ucom_detach(&sc->sc_ucom);
774
775         return (rv);
776 }