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