]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/serial/umodem.c
Some devices take undesired actions when RTS and DTR are
[FreeBSD/FreeBSD.git] / sys / dev / usb / serial / umodem.c
1 /*      $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $       */
2
3 #include <sys/cdefs.h>
4 __FBSDID("$FreeBSD$");
5
6 /*-
7  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
8  *
9  * Copyright (c) 2003, M. Warner Losh <imp@FreeBSD.org>.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*-
35  * Copyright (c) 1998 The NetBSD Foundation, Inc.
36  * All rights reserved.
37  *
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Lennart Augustsson (lennart@augustsson.net) at
40  * Carlstedt Research & Technology.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
52  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
55  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61  * POSSIBILITY OF SUCH DAMAGE.
62  */
63
64 /*
65  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
66  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
67  *                   http://www.usb.org/developers/devclass_docs/cdc_wmc10.zip
68  */
69
70 /*
71  * TODO:
72  * - Add error recovery in various places; the big problem is what
73  *   to do in a callback if there is an error.
74  * - Implement a Call Device for modems without multiplexed commands.
75  *
76  */
77
78 #include <sys/stdint.h>
79 #include <sys/stddef.h>
80 #include <sys/param.h>
81 #include <sys/queue.h>
82 #include <sys/types.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/bus.h>
86 #include <sys/module.h>
87 #include <sys/lock.h>
88 #include <sys/mutex.h>
89 #include <sys/condvar.h>
90 #include <sys/sysctl.h>
91 #include <sys/sx.h>
92 #include <sys/unistd.h>
93 #include <sys/callout.h>
94 #include <sys/malloc.h>
95 #include <sys/priv.h>
96
97 #include <dev/usb/usb.h>
98 #include <dev/usb/usbdi.h>
99 #include <dev/usb/usbdi_util.h>
100 #include <dev/usb/usbhid.h>
101 #include <dev/usb/usb_cdc.h>
102 #include "usbdevs.h"
103 #include "usb_if.h"
104
105 #include <dev/usb/usb_ioctl.h>
106
107 #define USB_DEBUG_VAR umodem_debug
108 #include <dev/usb/usb_debug.h>
109 #include <dev/usb/usb_process.h>
110 #include <dev/usb/quirk/usb_quirk.h>
111
112 #include <dev/usb/serial/usb_serial.h>
113
114 #ifdef USB_DEBUG
115 static int umodem_debug = 0;
116
117 static SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
118 SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RWTUN,
119     &umodem_debug, 0, "Debug level");
120 #endif
121
122 static const STRUCT_USB_DUAL_ID umodem_dual_devs[] = {
123         /* Generic Modem class match */
124         {USB_IFACE_CLASS(UICLASS_CDC),
125                 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
126                 USB_IFACE_PROTOCOL(UIPROTO_CDC_AT)},
127         {USB_IFACE_CLASS(UICLASS_CDC),
128                 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
129                 USB_IFACE_PROTOCOL(UIPROTO_CDC_NONE)},
130 };
131
132 static const STRUCT_USB_HOST_ID umodem_host_devs[] = {
133         /* Huawei Modem class match */
134         {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
135                 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x01)},
136         {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
137                 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x02)},
138         {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
139                 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x10)},
140         {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
141                 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x12)},
142         {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
143                 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x61)},
144         {USB_VENDOR(USB_VENDOR_HUAWEI), USB_IFACE_CLASS(UICLASS_VENDOR),
145                 USB_IFACE_SUBCLASS(0x02), USB_IFACE_PROTOCOL(0x62)},
146         {USB_VENDOR(USB_VENDOR_HUAWEI),USB_IFACE_CLASS(UICLASS_CDC),
147                 USB_IFACE_SUBCLASS(UISUBCLASS_ABSTRACT_CONTROL_MODEL),
148                 USB_IFACE_PROTOCOL(0xFF)},
149         /* Kyocera AH-K3001V */
150         {USB_VPI(USB_VENDOR_KYOCERA, USB_PRODUCT_KYOCERA_AHK3001V, 1)},
151         {USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, 1)},
152         {USB_VPI(USB_VENDOR_CURITEL, USB_PRODUCT_CURITEL_PC5740, 1)},
153 };
154
155 /*
156  * As speeds for umodem devices increase, these numbers will need to
157  * be increased. They should be good for G3 speeds and below.
158  *
159  * TODO: The TTY buffers should be increased!
160  */
161 #define UMODEM_BUF_SIZE 1024
162
163 enum {
164         UMODEM_BULK_WR,
165         UMODEM_BULK_RD,
166         UMODEM_INTR_WR,
167         UMODEM_INTR_RD,
168         UMODEM_N_TRANSFER,
169 };
170
171 #define UMODEM_MODVER                   1       /* module version */
172
173 struct umodem_softc {
174         struct ucom_super_softc sc_super_ucom;
175         struct ucom_softc sc_ucom;
176
177         struct usb_xfer *sc_xfer[UMODEM_N_TRANSFER];
178         struct usb_device *sc_udev;
179         struct mtx sc_mtx;
180
181         uint16_t sc_line;
182
183         uint8_t sc_lsr;                 /* local status register */
184         uint8_t sc_msr;                 /* modem status register */
185         uint8_t sc_ctrl_iface_no;
186         uint8_t sc_data_iface_no;
187         uint8_t sc_iface_index[2];
188         uint8_t sc_cm_over_data;
189         uint8_t sc_cm_cap;              /* CM capabilities */
190         uint8_t sc_acm_cap;             /* ACM capabilities */
191         uint8_t sc_line_coding[32];     /* used in USB device mode */
192         uint8_t sc_abstract_state[32];  /* used in USB device mode */
193 };
194
195 static device_probe_t umodem_probe;
196 static device_attach_t umodem_attach;
197 static device_detach_t umodem_detach;
198 static usb_handle_request_t umodem_handle_request;
199
200 static void umodem_free_softc(struct umodem_softc *);
201
202 static usb_callback_t umodem_intr_read_callback;
203 static usb_callback_t umodem_intr_write_callback;
204 static usb_callback_t umodem_write_callback;
205 static usb_callback_t umodem_read_callback;
206
207 static void     umodem_free(struct ucom_softc *);
208 static void     umodem_start_read(struct ucom_softc *);
209 static void     umodem_stop_read(struct ucom_softc *);
210 static void     umodem_start_write(struct ucom_softc *);
211 static void     umodem_stop_write(struct ucom_softc *);
212 static void     umodem_get_caps(struct usb_attach_arg *, uint8_t *, uint8_t *);
213 static void     umodem_cfg_get_status(struct ucom_softc *, uint8_t *,
214                     uint8_t *);
215 static int      umodem_pre_param(struct ucom_softc *, struct termios *);
216 static void     umodem_cfg_param(struct ucom_softc *, struct termios *);
217 static int      umodem_ioctl(struct ucom_softc *, uint32_t, caddr_t, int,
218                     struct thread *);
219 static void     umodem_cfg_set_dtr(struct ucom_softc *, uint8_t);
220 static void     umodem_cfg_set_rts(struct ucom_softc *, uint8_t);
221 static void     umodem_cfg_set_break(struct ucom_softc *, uint8_t);
222 static void     *umodem_get_desc(struct usb_attach_arg *, uint8_t, uint8_t);
223 static usb_error_t umodem_set_comm_feature(struct usb_device *, uint8_t,
224                     uint16_t, uint16_t);
225 static void     umodem_poll(struct ucom_softc *ucom);
226 static void     umodem_find_data_iface(struct usb_attach_arg *uaa,
227                     uint8_t, uint8_t *, uint8_t *);
228
229 static const struct usb_config umodem_config[UMODEM_N_TRANSFER] = {
230
231         [UMODEM_BULK_WR] = {
232                 .type = UE_BULK,
233                 .endpoint = UE_ADDR_ANY,
234                 .direction = UE_DIR_TX,
235                 .if_index = 0,
236                 .bufsize = UMODEM_BUF_SIZE,
237                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
238                 .callback = &umodem_write_callback,
239                 .usb_mode = USB_MODE_DUAL,
240         },
241
242         [UMODEM_BULK_RD] = {
243                 .type = UE_BULK,
244                 .endpoint = UE_ADDR_ANY,
245                 .direction = UE_DIR_RX,
246                 .if_index = 0,
247                 .bufsize = UMODEM_BUF_SIZE,
248                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
249                 .callback = &umodem_read_callback,
250                 .usb_mode = USB_MODE_DUAL,
251         },
252
253         [UMODEM_INTR_WR] = {
254                 .type = UE_INTERRUPT,
255                 .endpoint = UE_ADDR_ANY,
256                 .direction = UE_DIR_TX,
257                 .if_index = 1,
258                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
259                 .bufsize = 0,   /* use wMaxPacketSize */
260                 .callback = &umodem_intr_write_callback,
261                 .usb_mode = USB_MODE_DEVICE,
262         },
263
264         [UMODEM_INTR_RD] = {
265                 .type = UE_INTERRUPT,
266                 .endpoint = UE_ADDR_ANY,
267                 .direction = UE_DIR_RX,
268                 .if_index = 1,
269                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
270                 .bufsize = 0,   /* use wMaxPacketSize */
271                 .callback = &umodem_intr_read_callback,
272                 .usb_mode = USB_MODE_HOST,
273         },
274 };
275
276 static const struct ucom_callback umodem_callback = {
277         .ucom_cfg_get_status = &umodem_cfg_get_status,
278         .ucom_cfg_set_dtr = &umodem_cfg_set_dtr,
279         .ucom_cfg_set_rts = &umodem_cfg_set_rts,
280         .ucom_cfg_set_break = &umodem_cfg_set_break,
281         .ucom_cfg_param = &umodem_cfg_param,
282         .ucom_pre_param = &umodem_pre_param,
283         .ucom_ioctl = &umodem_ioctl,
284         .ucom_start_read = &umodem_start_read,
285         .ucom_stop_read = &umodem_stop_read,
286         .ucom_start_write = &umodem_start_write,
287         .ucom_stop_write = &umodem_stop_write,
288         .ucom_poll = &umodem_poll,
289         .ucom_free = &umodem_free,
290 };
291
292 static device_method_t umodem_methods[] = {
293         /* USB interface */
294         DEVMETHOD(usb_handle_request, umodem_handle_request),
295
296         /* Device interface */
297         DEVMETHOD(device_probe, umodem_probe),
298         DEVMETHOD(device_attach, umodem_attach),
299         DEVMETHOD(device_detach, umodem_detach),
300         DEVMETHOD_END
301 };
302
303 static devclass_t umodem_devclass;
304
305 static driver_t umodem_driver = {
306         .name = "umodem",
307         .methods = umodem_methods,
308         .size = sizeof(struct umodem_softc),
309 };
310
311 DRIVER_MODULE(umodem, uhub, umodem_driver, umodem_devclass, NULL, 0);
312 MODULE_DEPEND(umodem, ucom, 1, 1, 1);
313 MODULE_DEPEND(umodem, usb, 1, 1, 1);
314 MODULE_VERSION(umodem, UMODEM_MODVER);
315 USB_PNP_DUAL_INFO(umodem_dual_devs);
316 USB_PNP_HOST_INFO(umodem_host_devs);
317
318 static int
319 umodem_probe(device_t dev)
320 {
321         struct usb_attach_arg *uaa = device_get_ivars(dev);
322         int error;
323
324         DPRINTFN(11, "\n");
325
326         error = usbd_lookup_id_by_uaa(umodem_host_devs,
327             sizeof(umodem_host_devs), uaa);
328         if (error) {
329                 error = usbd_lookup_id_by_uaa(umodem_dual_devs,
330                     sizeof(umodem_dual_devs), uaa);
331                 if (error)
332                         return (error);
333         }
334         return (BUS_PROBE_GENERIC);
335 }
336
337 static int
338 umodem_attach(device_t dev)
339 {
340         struct usb_attach_arg *uaa = device_get_ivars(dev);
341         struct umodem_softc *sc = device_get_softc(dev);
342         struct usb_cdc_cm_descriptor *cmd;
343         struct usb_cdc_union_descriptor *cud;
344         uint8_t i;
345         int error;
346
347         device_set_usb_desc(dev);
348         mtx_init(&sc->sc_mtx, "umodem", NULL, MTX_DEF);
349         ucom_ref(&sc->sc_super_ucom);
350
351         sc->sc_ctrl_iface_no = uaa->info.bIfaceNum;
352         sc->sc_iface_index[1] = uaa->info.bIfaceIndex;
353         sc->sc_udev = uaa->device;
354
355         umodem_get_caps(uaa, &sc->sc_cm_cap, &sc->sc_acm_cap);
356
357         /* get the data interface number */
358
359         cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
360
361         if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
362
363                 cud = usbd_find_descriptor(uaa->device, NULL,
364                     uaa->info.bIfaceIndex, UDESC_CS_INTERFACE,
365                     0xFF, UDESCSUB_CDC_UNION, 0xFF);
366
367                 if ((cud == NULL) || (cud->bLength < sizeof(*cud))) {
368                         DPRINTF("Missing descriptor. "
369                             "Assuming data interface is next.\n");
370                         if (sc->sc_ctrl_iface_no == 0xFF) {
371                                 goto detach;
372                         } else {
373                                 uint8_t class_match = 0;
374
375                                 /* set default interface number */
376                                 sc->sc_data_iface_no = 0xFF;
377
378                                 /* try to find the data interface backwards */
379                                 umodem_find_data_iface(uaa,
380                                     uaa->info.bIfaceIndex - 1,
381                                     &sc->sc_data_iface_no, &class_match);
382
383                                 /* try to find the data interface forwards */
384                                 umodem_find_data_iface(uaa,
385                                     uaa->info.bIfaceIndex + 1,
386                                     &sc->sc_data_iface_no, &class_match);
387
388                                 /* check if nothing was found */
389                                 if (sc->sc_data_iface_no == 0xFF)
390                                         goto detach;
391                         }
392                 } else {
393                         sc->sc_data_iface_no = cud->bSlaveInterface[0];
394                 }
395         } else {
396                 sc->sc_data_iface_no = cmd->bDataInterface;
397         }
398
399         device_printf(dev, "data interface %d, has %sCM over "
400             "data, has %sbreak\n",
401             sc->sc_data_iface_no,
402             sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
403             sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
404
405         /* get the data interface too */
406
407         for (i = 0;; i++) {
408                 struct usb_interface *iface;
409                 struct usb_interface_descriptor *id;
410
411                 iface = usbd_get_iface(uaa->device, i);
412
413                 if (iface) {
414
415                         id = usbd_get_interface_descriptor(iface);
416
417                         if (id && (id->bInterfaceNumber == sc->sc_data_iface_no)) {
418                                 sc->sc_iface_index[0] = i;
419                                 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
420                                 break;
421                         }
422                 } else {
423                         device_printf(dev, "no data interface\n");
424                         goto detach;
425                 }
426         }
427
428         if (usb_test_quirk(uaa, UQ_ASSUME_CM_OVER_DATA)) {
429                 sc->sc_cm_over_data = 1;
430         } else {
431                 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
432                         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE) {
433
434                                 error = umodem_set_comm_feature
435                                 (uaa->device, sc->sc_ctrl_iface_no,
436                                  UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
437
438                                 /* ignore any errors */
439                         }
440                         sc->sc_cm_over_data = 1;
441                 }
442         }
443         error = usbd_transfer_setup(uaa->device,
444             sc->sc_iface_index, sc->sc_xfer,
445             umodem_config, UMODEM_N_TRANSFER,
446             sc, &sc->sc_mtx);
447         if (error) {
448                 device_printf(dev, "Can't setup transfer\n");
449                 goto detach;
450         }
451
452         /* clear stall at first run, if USB host mode */
453         if (uaa->usb_mode == USB_MODE_HOST) {
454                 mtx_lock(&sc->sc_mtx);
455                 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_WR]);
456                 usbd_xfer_set_stall(sc->sc_xfer[UMODEM_BULK_RD]);
457                 mtx_unlock(&sc->sc_mtx);
458         }
459
460         ucom_set_usb_mode(&sc->sc_super_ucom, uaa->usb_mode);
461
462         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
463             &umodem_callback, &sc->sc_mtx);
464         if (error) {
465                 device_printf(dev, "Can't attach com\n");
466                 goto detach;
467         }
468         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
469
470         return (0);
471
472 detach:
473         umodem_detach(dev);
474         return (ENXIO);
475 }
476
477 static void
478 umodem_find_data_iface(struct usb_attach_arg *uaa,
479     uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
480 {
481         struct usb_interface_descriptor *id;
482         struct usb_interface *iface;
483         
484         iface = usbd_get_iface(uaa->device, iface_index);
485
486         /* check for end of interfaces */
487         if (iface == NULL)
488                 return;
489
490         id = usbd_get_interface_descriptor(iface);
491
492         /* check for non-matching interface class */
493         if (id->bInterfaceClass != UICLASS_CDC_DATA ||
494             id->bInterfaceSubClass != UISUBCLASS_DATA) {
495                 /* if we got a class match then return */
496                 if (*p_match_class)
497                         return;
498         } else {
499                 *p_match_class = 1;
500         }
501
502         DPRINTFN(11, "Match at index %u\n", iface_index);
503
504         *p_data_no = id->bInterfaceNumber;
505 }
506
507 static void
508 umodem_start_read(struct ucom_softc *ucom)
509 {
510         struct umodem_softc *sc = ucom->sc_parent;
511
512         /* start interrupt endpoint, if any */
513         usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
514
515         /* start read endpoint */
516         usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
517 }
518
519 static void
520 umodem_stop_read(struct ucom_softc *ucom)
521 {
522         struct umodem_softc *sc = ucom->sc_parent;
523
524         /* stop interrupt endpoint, if any */
525         usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
526
527         /* stop read endpoint */
528         usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
529 }
530
531 static void
532 umodem_start_write(struct ucom_softc *ucom)
533 {
534         struct umodem_softc *sc = ucom->sc_parent;
535
536         usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
537         usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
538 }
539
540 static void
541 umodem_stop_write(struct ucom_softc *ucom)
542 {
543         struct umodem_softc *sc = ucom->sc_parent;
544
545         usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
546         usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
547 }
548
549 static void
550 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
551 {
552         struct usb_cdc_cm_descriptor *cmd;
553         struct usb_cdc_acm_descriptor *cad;
554
555         cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
556         if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
557                 DPRINTF("no CM desc (faking one)\n");
558                 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
559         } else
560                 *cm = cmd->bmCapabilities;
561
562         cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
563         if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
564                 DPRINTF("no ACM desc\n");
565                 *acm = 0;
566         } else
567                 *acm = cad->bmCapabilities;
568 }
569
570 static void
571 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
572 {
573         struct umodem_softc *sc = ucom->sc_parent;
574
575         DPRINTF("\n");
576
577         /* XXX Note: sc_lsr is always zero */
578         *lsr = sc->sc_lsr;
579         *msr = sc->sc_msr;
580 }
581
582 static int
583 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
584 {
585         return (0);                     /* we accept anything */
586 }
587
588 static void
589 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
590 {
591         struct umodem_softc *sc = ucom->sc_parent;
592         struct usb_cdc_line_state ls;
593         struct usb_device_request req;
594
595         DPRINTF("sc=%p\n", sc);
596
597         memset(&ls, 0, sizeof(ls));
598
599         USETDW(ls.dwDTERate, t->c_ospeed);
600
601         ls.bCharFormat = (t->c_cflag & CSTOPB) ?
602             UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
603
604         ls.bParityType = (t->c_cflag & PARENB) ?
605             ((t->c_cflag & PARODD) ?
606             UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
607
608         switch (t->c_cflag & CSIZE) {
609         case CS5:
610                 ls.bDataBits = 5;
611                 break;
612         case CS6:
613                 ls.bDataBits = 6;
614                 break;
615         case CS7:
616                 ls.bDataBits = 7;
617                 break;
618         case CS8:
619                 ls.bDataBits = 8;
620                 break;
621         }
622
623         DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
624             UGETDW(ls.dwDTERate), ls.bCharFormat,
625             ls.bParityType, ls.bDataBits);
626
627         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
628         req.bRequest = UCDC_SET_LINE_CODING;
629         USETW(req.wValue, 0);
630         req.wIndex[0] = sc->sc_ctrl_iface_no;
631         req.wIndex[1] = 0;
632         USETW(req.wLength, sizeof(ls));
633
634         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
635             &req, &ls, 0, 1000);
636 }
637
638 static int
639 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
640     int flag, struct thread *td)
641 {
642         struct umodem_softc *sc = ucom->sc_parent;
643         int error = 0;
644
645         DPRINTF("cmd=0x%08x\n", cmd);
646
647         switch (cmd) {
648         case USB_GET_CM_OVER_DATA:
649                 *(int *)data = sc->sc_cm_over_data;
650                 break;
651
652         case USB_SET_CM_OVER_DATA:
653                 if (*(int *)data != sc->sc_cm_over_data) {
654                         /* XXX change it */
655                 }
656                 break;
657
658         default:
659                 DPRINTF("unknown\n");
660                 error = ENOIOCTL;
661                 break;
662         }
663
664         return (error);
665 }
666
667 static void
668 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
669 {
670         struct umodem_softc *sc = ucom->sc_parent;
671         struct usb_device_request req;
672
673         DPRINTF("onoff=%d\n", onoff);
674
675         if (onoff)
676                 sc->sc_line |= UCDC_LINE_DTR;
677         else
678                 sc->sc_line &= ~UCDC_LINE_DTR;
679
680         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
681         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
682         USETW(req.wValue, sc->sc_line);
683         req.wIndex[0] = sc->sc_ctrl_iface_no;
684         req.wIndex[1] = 0;
685         USETW(req.wLength, 0);
686
687         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
688             &req, NULL, 0, 1000);
689 }
690
691 static void
692 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
693 {
694         struct umodem_softc *sc = ucom->sc_parent;
695         struct usb_device_request req;
696
697         DPRINTF("onoff=%d\n", onoff);
698
699         if (onoff)
700                 sc->sc_line |= UCDC_LINE_RTS;
701         else
702                 sc->sc_line &= ~UCDC_LINE_RTS;
703
704         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
705         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
706         USETW(req.wValue, sc->sc_line);
707         req.wIndex[0] = sc->sc_ctrl_iface_no;
708         req.wIndex[1] = 0;
709         USETW(req.wLength, 0);
710
711         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
712             &req, NULL, 0, 1000);
713 }
714
715 static void
716 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
717 {
718         struct umodem_softc *sc = ucom->sc_parent;
719         struct usb_device_request req;
720         uint16_t temp;
721
722         DPRINTF("onoff=%d\n", onoff);
723
724         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
725
726                 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
727
728                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
729                 req.bRequest = UCDC_SEND_BREAK;
730                 USETW(req.wValue, temp);
731                 req.wIndex[0] = sc->sc_ctrl_iface_no;
732                 req.wIndex[1] = 0;
733                 USETW(req.wLength, 0);
734
735                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
736                     &req, NULL, 0, 1000);
737         }
738 }
739
740 static void
741 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
742 {
743         int actlen;
744
745         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
746
747         switch (USB_GET_STATE(xfer)) {
748         case USB_ST_TRANSFERRED:
749
750                 DPRINTF("Transferred %d bytes\n", actlen);
751
752                 /* FALLTHROUGH */
753         case USB_ST_SETUP:
754 tr_setup:
755                 break;
756
757         default:                        /* Error */
758                 if (error != USB_ERR_CANCELLED) {
759                         /* start clear stall */
760                         usbd_xfer_set_stall(xfer);
761                         goto tr_setup;
762                 }
763                 break;
764         }
765 }
766
767 static void
768 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
769 {
770         struct usb_cdc_notification pkt;
771         struct umodem_softc *sc = usbd_xfer_softc(xfer);
772         struct usb_page_cache *pc;
773         uint16_t wLen;
774         int actlen;
775
776         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
777
778         switch (USB_GET_STATE(xfer)) {
779         case USB_ST_TRANSFERRED:
780
781                 if (actlen < 8) {
782                         DPRINTF("received short packet, "
783                             "%d bytes\n", actlen);
784                         goto tr_setup;
785                 }
786                 if (actlen > (int)sizeof(pkt)) {
787                         DPRINTF("truncating message\n");
788                         actlen = sizeof(pkt);
789                 }
790                 pc = usbd_xfer_get_frame(xfer, 0);
791                 usbd_copy_out(pc, 0, &pkt, actlen);
792
793                 actlen -= 8;
794
795                 wLen = UGETW(pkt.wLength);
796                 if (actlen > wLen) {
797                         actlen = wLen;
798                 }
799                 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
800                         DPRINTF("unknown message type, "
801                             "0x%02x, on notify pipe!\n",
802                             pkt.bmRequestType);
803                         goto tr_setup;
804                 }
805                 switch (pkt.bNotification) {
806                 case UCDC_N_SERIAL_STATE:
807                         /*
808                          * Set the serial state in ucom driver based on
809                          * the bits from the notify message
810                          */
811                         if (actlen < 2) {
812                                 DPRINTF("invalid notification "
813                                     "length, %d bytes!\n", actlen);
814                                 break;
815                         }
816                         DPRINTF("notify bytes = %02x%02x\n",
817                             pkt.data[0],
818                             pkt.data[1]);
819
820                         /* Currently, lsr is always zero. */
821                         sc->sc_lsr = 0;
822                         sc->sc_msr = 0;
823
824                         if (pkt.data[0] & UCDC_N_SERIAL_RI) {
825                                 sc->sc_msr |= SER_RI;
826                         }
827                         if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
828                                 sc->sc_msr |= SER_DSR;
829                         }
830                         if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
831                                 sc->sc_msr |= SER_DCD;
832                         }
833                         ucom_status_change(&sc->sc_ucom);
834                         break;
835
836                 default:
837                         DPRINTF("unknown notify message: 0x%02x\n",
838                             pkt.bNotification);
839                         break;
840                 }
841
842         case USB_ST_SETUP:
843 tr_setup:
844                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
845                 usbd_transfer_submit(xfer);
846                 return;
847
848         default:                        /* Error */
849                 if (error != USB_ERR_CANCELLED) {
850                         /* try to clear stall first */
851                         usbd_xfer_set_stall(xfer);
852                         goto tr_setup;
853                 }
854                 return;
855
856         }
857 }
858
859 static void
860 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
861 {
862         struct umodem_softc *sc = usbd_xfer_softc(xfer);
863         struct usb_page_cache *pc;
864         uint32_t actlen;
865
866         switch (USB_GET_STATE(xfer)) {
867         case USB_ST_SETUP:
868         case USB_ST_TRANSFERRED:
869 tr_setup:
870                 pc = usbd_xfer_get_frame(xfer, 0);
871                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
872                     UMODEM_BUF_SIZE, &actlen)) {
873
874                         usbd_xfer_set_frame_len(xfer, 0, actlen);
875                         usbd_transfer_submit(xfer);
876                 }
877                 return;
878
879         default:                        /* Error */
880                 if (error != USB_ERR_CANCELLED) {
881                         /* try to clear stall first */
882                         usbd_xfer_set_stall(xfer);
883                         goto tr_setup;
884                 }
885                 return;
886         }
887 }
888
889 static void
890 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
891 {
892         struct umodem_softc *sc = usbd_xfer_softc(xfer);
893         struct usb_page_cache *pc;
894         int actlen;
895
896         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
897
898         switch (USB_GET_STATE(xfer)) {
899         case USB_ST_TRANSFERRED:
900
901                 DPRINTF("actlen=%d\n", actlen);
902
903                 pc = usbd_xfer_get_frame(xfer, 0);
904                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
905
906         case USB_ST_SETUP:
907 tr_setup:
908                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
909                 usbd_transfer_submit(xfer);
910                 return;
911
912         default:                        /* Error */
913                 if (error != USB_ERR_CANCELLED) {
914                         /* try to clear stall first */
915                         usbd_xfer_set_stall(xfer);
916                         goto tr_setup;
917                 }
918                 return;
919         }
920 }
921
922 static void *
923 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
924 {
925         return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
926             type, 0xFF, subtype, 0xFF));
927 }
928
929 static usb_error_t
930 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
931     uint16_t feature, uint16_t state)
932 {
933         struct usb_device_request req;
934         struct usb_cdc_abstract_state ast;
935
936         DPRINTF("feature=%d state=%d\n",
937             feature, state);
938
939         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
940         req.bRequest = UCDC_SET_COMM_FEATURE;
941         USETW(req.wValue, feature);
942         req.wIndex[0] = iface_no;
943         req.wIndex[1] = 0;
944         USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
945         USETW(ast.wState, state);
946
947         return (usbd_do_request(udev, NULL, &req, &ast));
948 }
949
950 static int
951 umodem_detach(device_t dev)
952 {
953         struct umodem_softc *sc = device_get_softc(dev);
954
955         DPRINTF("sc=%p\n", sc);
956
957         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
958         usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
959
960         device_claim_softc(dev);
961
962         umodem_free_softc(sc);
963
964         return (0);
965 }
966
967 UCOM_UNLOAD_DRAIN(umodem);
968
969 static void
970 umodem_free_softc(struct umodem_softc *sc)
971 {
972         if (ucom_unref(&sc->sc_super_ucom)) {
973                 mtx_destroy(&sc->sc_mtx);
974                 device_free_softc(sc);
975         }
976 }
977
978 static void
979 umodem_free(struct ucom_softc *ucom)
980 {
981         umodem_free_softc(ucom->sc_parent);
982 }
983
984 static void
985 umodem_poll(struct ucom_softc *ucom)
986 {
987         struct umodem_softc *sc = ucom->sc_parent;
988         usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
989 }
990
991 static int
992 umodem_handle_request(device_t dev,
993     const void *preq, void **pptr, uint16_t *plen,
994     uint16_t offset, uint8_t *pstate)
995 {
996         struct umodem_softc *sc = device_get_softc(dev);
997         const struct usb_device_request *req = preq;
998         uint8_t is_complete = *pstate;
999
1000         DPRINTF("sc=%p\n", sc);
1001
1002         if (!is_complete) {
1003                 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1004                     (req->bRequest == UCDC_SET_LINE_CODING) &&
1005                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1006                     (req->wIndex[1] == 0x00) &&
1007                     (req->wValue[0] == 0x00) &&
1008                     (req->wValue[1] == 0x00)) {
1009                         if (offset == 0) {
1010                                 *plen = sizeof(sc->sc_line_coding);
1011                                 *pptr = &sc->sc_line_coding;
1012                         } else {
1013                                 *plen = 0;
1014                         }
1015                         return (0);
1016                 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1017                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1018                     (req->wIndex[1] == 0x00) &&
1019                     (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1020                         if (offset == 0) {
1021                                 *plen = sizeof(sc->sc_abstract_state);
1022                                 *pptr = &sc->sc_abstract_state;
1023                         } else {
1024                                 *plen = 0;
1025                         }
1026                         return (0);
1027                 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1028                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1029                     (req->wIndex[1] == 0x00) &&
1030                     (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1031                         *plen = 0;
1032                         return (0);
1033                 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1034                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1035                     (req->wIndex[1] == 0x00) &&
1036                     (req->bRequest == UCDC_SEND_BREAK)) {
1037                         *plen = 0;
1038                         return (0);
1039                 }
1040         }
1041         return (ENXIO);                 /* use builtin handler */
1042 }