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