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