]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/serial/umodem.c
Import OpenCSD -- an ARM CoreSight(tm) Trace Decode Library.
[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         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
461             &umodem_callback, &sc->sc_mtx);
462         if (error) {
463                 device_printf(dev, "Can't attach com\n");
464                 goto detach;
465         }
466         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
467
468         return (0);
469
470 detach:
471         umodem_detach(dev);
472         return (ENXIO);
473 }
474
475 static void
476 umodem_find_data_iface(struct usb_attach_arg *uaa,
477     uint8_t iface_index, uint8_t *p_data_no, uint8_t *p_match_class)
478 {
479         struct usb_interface_descriptor *id;
480         struct usb_interface *iface;
481         
482         iface = usbd_get_iface(uaa->device, iface_index);
483
484         /* check for end of interfaces */
485         if (iface == NULL)
486                 return;
487
488         id = usbd_get_interface_descriptor(iface);
489
490         /* check for non-matching interface class */
491         if (id->bInterfaceClass != UICLASS_CDC_DATA ||
492             id->bInterfaceSubClass != UISUBCLASS_DATA) {
493                 /* if we got a class match then return */
494                 if (*p_match_class)
495                         return;
496         } else {
497                 *p_match_class = 1;
498         }
499
500         DPRINTFN(11, "Match at index %u\n", iface_index);
501
502         *p_data_no = id->bInterfaceNumber;
503 }
504
505 static void
506 umodem_start_read(struct ucom_softc *ucom)
507 {
508         struct umodem_softc *sc = ucom->sc_parent;
509
510         /* start interrupt endpoint, if any */
511         usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_RD]);
512
513         /* start read endpoint */
514         usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_RD]);
515 }
516
517 static void
518 umodem_stop_read(struct ucom_softc *ucom)
519 {
520         struct umodem_softc *sc = ucom->sc_parent;
521
522         /* stop interrupt endpoint, if any */
523         usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_RD]);
524
525         /* stop read endpoint */
526         usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_RD]);
527 }
528
529 static void
530 umodem_start_write(struct ucom_softc *ucom)
531 {
532         struct umodem_softc *sc = ucom->sc_parent;
533
534         usbd_transfer_start(sc->sc_xfer[UMODEM_INTR_WR]);
535         usbd_transfer_start(sc->sc_xfer[UMODEM_BULK_WR]);
536 }
537
538 static void
539 umodem_stop_write(struct ucom_softc *ucom)
540 {
541         struct umodem_softc *sc = ucom->sc_parent;
542
543         usbd_transfer_stop(sc->sc_xfer[UMODEM_INTR_WR]);
544         usbd_transfer_stop(sc->sc_xfer[UMODEM_BULK_WR]);
545 }
546
547 static void
548 umodem_get_caps(struct usb_attach_arg *uaa, uint8_t *cm, uint8_t *acm)
549 {
550         struct usb_cdc_cm_descriptor *cmd;
551         struct usb_cdc_acm_descriptor *cad;
552
553         cmd = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
554         if ((cmd == NULL) || (cmd->bLength < sizeof(*cmd))) {
555                 DPRINTF("no CM desc (faking one)\n");
556                 *cm = USB_CDC_CM_DOES_CM | USB_CDC_CM_OVER_DATA;
557         } else
558                 *cm = cmd->bmCapabilities;
559
560         cad = umodem_get_desc(uaa, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
561         if ((cad == NULL) || (cad->bLength < sizeof(*cad))) {
562                 DPRINTF("no ACM desc\n");
563                 *acm = 0;
564         } else
565                 *acm = cad->bmCapabilities;
566 }
567
568 static void
569 umodem_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
570 {
571         struct umodem_softc *sc = ucom->sc_parent;
572
573         DPRINTF("\n");
574
575         /* XXX Note: sc_lsr is always zero */
576         *lsr = sc->sc_lsr;
577         *msr = sc->sc_msr;
578 }
579
580 static int
581 umodem_pre_param(struct ucom_softc *ucom, struct termios *t)
582 {
583         return (0);                     /* we accept anything */
584 }
585
586 static void
587 umodem_cfg_param(struct ucom_softc *ucom, struct termios *t)
588 {
589         struct umodem_softc *sc = ucom->sc_parent;
590         struct usb_cdc_line_state ls;
591         struct usb_device_request req;
592
593         DPRINTF("sc=%p\n", sc);
594
595         memset(&ls, 0, sizeof(ls));
596
597         USETDW(ls.dwDTERate, t->c_ospeed);
598
599         ls.bCharFormat = (t->c_cflag & CSTOPB) ?
600             UCDC_STOP_BIT_2 : UCDC_STOP_BIT_1;
601
602         ls.bParityType = (t->c_cflag & PARENB) ?
603             ((t->c_cflag & PARODD) ?
604             UCDC_PARITY_ODD : UCDC_PARITY_EVEN) : UCDC_PARITY_NONE;
605
606         switch (t->c_cflag & CSIZE) {
607         case CS5:
608                 ls.bDataBits = 5;
609                 break;
610         case CS6:
611                 ls.bDataBits = 6;
612                 break;
613         case CS7:
614                 ls.bDataBits = 7;
615                 break;
616         case CS8:
617                 ls.bDataBits = 8;
618                 break;
619         }
620
621         DPRINTF("rate=%d fmt=%d parity=%d bits=%d\n",
622             UGETDW(ls.dwDTERate), ls.bCharFormat,
623             ls.bParityType, ls.bDataBits);
624
625         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
626         req.bRequest = UCDC_SET_LINE_CODING;
627         USETW(req.wValue, 0);
628         req.wIndex[0] = sc->sc_ctrl_iface_no;
629         req.wIndex[1] = 0;
630         USETW(req.wLength, sizeof(ls));
631
632         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
633             &req, &ls, 0, 1000);
634 }
635
636 static int
637 umodem_ioctl(struct ucom_softc *ucom, uint32_t cmd, caddr_t data,
638     int flag, struct thread *td)
639 {
640         struct umodem_softc *sc = ucom->sc_parent;
641         int error = 0;
642
643         DPRINTF("cmd=0x%08x\n", cmd);
644
645         switch (cmd) {
646         case USB_GET_CM_OVER_DATA:
647                 *(int *)data = sc->sc_cm_over_data;
648                 break;
649
650         case USB_SET_CM_OVER_DATA:
651                 if (*(int *)data != sc->sc_cm_over_data) {
652                         /* XXX change it */
653                 }
654                 break;
655
656         default:
657                 DPRINTF("unknown\n");
658                 error = ENOIOCTL;
659                 break;
660         }
661
662         return (error);
663 }
664
665 static void
666 umodem_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
667 {
668         struct umodem_softc *sc = ucom->sc_parent;
669         struct usb_device_request req;
670
671         DPRINTF("onoff=%d\n", onoff);
672
673         if (onoff)
674                 sc->sc_line |= UCDC_LINE_DTR;
675         else
676                 sc->sc_line &= ~UCDC_LINE_DTR;
677
678         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
679         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
680         USETW(req.wValue, sc->sc_line);
681         req.wIndex[0] = sc->sc_ctrl_iface_no;
682         req.wIndex[1] = 0;
683         USETW(req.wLength, 0);
684
685         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
686             &req, NULL, 0, 1000);
687 }
688
689 static void
690 umodem_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
691 {
692         struct umodem_softc *sc = ucom->sc_parent;
693         struct usb_device_request req;
694
695         DPRINTF("onoff=%d\n", onoff);
696
697         if (onoff)
698                 sc->sc_line |= UCDC_LINE_RTS;
699         else
700                 sc->sc_line &= ~UCDC_LINE_RTS;
701
702         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
703         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
704         USETW(req.wValue, sc->sc_line);
705         req.wIndex[0] = sc->sc_ctrl_iface_no;
706         req.wIndex[1] = 0;
707         USETW(req.wLength, 0);
708
709         ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
710             &req, NULL, 0, 1000);
711 }
712
713 static void
714 umodem_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
715 {
716         struct umodem_softc *sc = ucom->sc_parent;
717         struct usb_device_request req;
718         uint16_t temp;
719
720         DPRINTF("onoff=%d\n", onoff);
721
722         if (sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK) {
723
724                 temp = onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF;
725
726                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
727                 req.bRequest = UCDC_SEND_BREAK;
728                 USETW(req.wValue, temp);
729                 req.wIndex[0] = sc->sc_ctrl_iface_no;
730                 req.wIndex[1] = 0;
731                 USETW(req.wLength, 0);
732
733                 ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom, 
734                     &req, NULL, 0, 1000);
735         }
736 }
737
738 static void
739 umodem_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
740 {
741         int actlen;
742
743         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
744
745         switch (USB_GET_STATE(xfer)) {
746         case USB_ST_TRANSFERRED:
747
748                 DPRINTF("Transferred %d bytes\n", actlen);
749
750                 /* FALLTHROUGH */
751         case USB_ST_SETUP:
752 tr_setup:
753                 break;
754
755         default:                        /* Error */
756                 if (error != USB_ERR_CANCELLED) {
757                         /* start clear stall */
758                         usbd_xfer_set_stall(xfer);
759                         goto tr_setup;
760                 }
761                 break;
762         }
763 }
764
765 static void
766 umodem_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
767 {
768         struct usb_cdc_notification pkt;
769         struct umodem_softc *sc = usbd_xfer_softc(xfer);
770         struct usb_page_cache *pc;
771         uint16_t wLen;
772         int actlen;
773
774         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
775
776         switch (USB_GET_STATE(xfer)) {
777         case USB_ST_TRANSFERRED:
778
779                 if (actlen < 8) {
780                         DPRINTF("received short packet, "
781                             "%d bytes\n", actlen);
782                         goto tr_setup;
783                 }
784                 if (actlen > (int)sizeof(pkt)) {
785                         DPRINTF("truncating message\n");
786                         actlen = sizeof(pkt);
787                 }
788                 pc = usbd_xfer_get_frame(xfer, 0);
789                 usbd_copy_out(pc, 0, &pkt, actlen);
790
791                 actlen -= 8;
792
793                 wLen = UGETW(pkt.wLength);
794                 if (actlen > wLen) {
795                         actlen = wLen;
796                 }
797                 if (pkt.bmRequestType != UCDC_NOTIFICATION) {
798                         DPRINTF("unknown message type, "
799                             "0x%02x, on notify pipe!\n",
800                             pkt.bmRequestType);
801                         goto tr_setup;
802                 }
803                 switch (pkt.bNotification) {
804                 case UCDC_N_SERIAL_STATE:
805                         /*
806                          * Set the serial state in ucom driver based on
807                          * the bits from the notify message
808                          */
809                         if (actlen < 2) {
810                                 DPRINTF("invalid notification "
811                                     "length, %d bytes!\n", actlen);
812                                 break;
813                         }
814                         DPRINTF("notify bytes = %02x%02x\n",
815                             pkt.data[0],
816                             pkt.data[1]);
817
818                         /* Currently, lsr is always zero. */
819                         sc->sc_lsr = 0;
820                         sc->sc_msr = 0;
821
822                         if (pkt.data[0] & UCDC_N_SERIAL_RI) {
823                                 sc->sc_msr |= SER_RI;
824                         }
825                         if (pkt.data[0] & UCDC_N_SERIAL_DSR) {
826                                 sc->sc_msr |= SER_DSR;
827                         }
828                         if (pkt.data[0] & UCDC_N_SERIAL_DCD) {
829                                 sc->sc_msr |= SER_DCD;
830                         }
831                         ucom_status_change(&sc->sc_ucom);
832                         break;
833
834                 default:
835                         DPRINTF("unknown notify message: 0x%02x\n",
836                             pkt.bNotification);
837                         break;
838                 }
839
840         case USB_ST_SETUP:
841 tr_setup:
842                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
843                 usbd_transfer_submit(xfer);
844                 return;
845
846         default:                        /* Error */
847                 if (error != USB_ERR_CANCELLED) {
848                         /* try to clear stall first */
849                         usbd_xfer_set_stall(xfer);
850                         goto tr_setup;
851                 }
852                 return;
853
854         }
855 }
856
857 static void
858 umodem_write_callback(struct usb_xfer *xfer, usb_error_t error)
859 {
860         struct umodem_softc *sc = usbd_xfer_softc(xfer);
861         struct usb_page_cache *pc;
862         uint32_t actlen;
863
864         switch (USB_GET_STATE(xfer)) {
865         case USB_ST_SETUP:
866         case USB_ST_TRANSFERRED:
867 tr_setup:
868                 pc = usbd_xfer_get_frame(xfer, 0);
869                 if (ucom_get_data(&sc->sc_ucom, pc, 0,
870                     UMODEM_BUF_SIZE, &actlen)) {
871
872                         usbd_xfer_set_frame_len(xfer, 0, actlen);
873                         usbd_transfer_submit(xfer);
874                 }
875                 return;
876
877         default:                        /* Error */
878                 if (error != USB_ERR_CANCELLED) {
879                         /* try to clear stall first */
880                         usbd_xfer_set_stall(xfer);
881                         goto tr_setup;
882                 }
883                 return;
884         }
885 }
886
887 static void
888 umodem_read_callback(struct usb_xfer *xfer, usb_error_t error)
889 {
890         struct umodem_softc *sc = usbd_xfer_softc(xfer);
891         struct usb_page_cache *pc;
892         int actlen;
893
894         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
895
896         switch (USB_GET_STATE(xfer)) {
897         case USB_ST_TRANSFERRED:
898
899                 DPRINTF("actlen=%d\n", actlen);
900
901                 pc = usbd_xfer_get_frame(xfer, 0);
902                 ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
903
904         case USB_ST_SETUP:
905 tr_setup:
906                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
907                 usbd_transfer_submit(xfer);
908                 return;
909
910         default:                        /* Error */
911                 if (error != USB_ERR_CANCELLED) {
912                         /* try to clear stall first */
913                         usbd_xfer_set_stall(xfer);
914                         goto tr_setup;
915                 }
916                 return;
917         }
918 }
919
920 static void *
921 umodem_get_desc(struct usb_attach_arg *uaa, uint8_t type, uint8_t subtype)
922 {
923         return (usbd_find_descriptor(uaa->device, NULL, uaa->info.bIfaceIndex,
924             type, 0xFF, subtype, 0xFF));
925 }
926
927 static usb_error_t
928 umodem_set_comm_feature(struct usb_device *udev, uint8_t iface_no,
929     uint16_t feature, uint16_t state)
930 {
931         struct usb_device_request req;
932         struct usb_cdc_abstract_state ast;
933
934         DPRINTF("feature=%d state=%d\n",
935             feature, state);
936
937         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
938         req.bRequest = UCDC_SET_COMM_FEATURE;
939         USETW(req.wValue, feature);
940         req.wIndex[0] = iface_no;
941         req.wIndex[1] = 0;
942         USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
943         USETW(ast.wState, state);
944
945         return (usbd_do_request(udev, NULL, &req, &ast));
946 }
947
948 static int
949 umodem_detach(device_t dev)
950 {
951         struct umodem_softc *sc = device_get_softc(dev);
952
953         DPRINTF("sc=%p\n", sc);
954
955         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
956         usbd_transfer_unsetup(sc->sc_xfer, UMODEM_N_TRANSFER);
957
958         device_claim_softc(dev);
959
960         umodem_free_softc(sc);
961
962         return (0);
963 }
964
965 UCOM_UNLOAD_DRAIN(umodem);
966
967 static void
968 umodem_free_softc(struct umodem_softc *sc)
969 {
970         if (ucom_unref(&sc->sc_super_ucom)) {
971                 mtx_destroy(&sc->sc_mtx);
972                 device_free_softc(sc);
973         }
974 }
975
976 static void
977 umodem_free(struct ucom_softc *ucom)
978 {
979         umodem_free_softc(ucom->sc_parent);
980 }
981
982 static void
983 umodem_poll(struct ucom_softc *ucom)
984 {
985         struct umodem_softc *sc = ucom->sc_parent;
986         usbd_transfer_poll(sc->sc_xfer, UMODEM_N_TRANSFER);
987 }
988
989 static int
990 umodem_handle_request(device_t dev,
991     const void *preq, void **pptr, uint16_t *plen,
992     uint16_t offset, uint8_t *pstate)
993 {
994         struct umodem_softc *sc = device_get_softc(dev);
995         const struct usb_device_request *req = preq;
996         uint8_t is_complete = *pstate;
997
998         DPRINTF("sc=%p\n", sc);
999
1000         if (!is_complete) {
1001                 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1002                     (req->bRequest == UCDC_SET_LINE_CODING) &&
1003                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1004                     (req->wIndex[1] == 0x00) &&
1005                     (req->wValue[0] == 0x00) &&
1006                     (req->wValue[1] == 0x00)) {
1007                         if (offset == 0) {
1008                                 *plen = sizeof(sc->sc_line_coding);
1009                                 *pptr = &sc->sc_line_coding;
1010                         } else {
1011                                 *plen = 0;
1012                         }
1013                         return (0);
1014                 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1015                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1016                     (req->wIndex[1] == 0x00) &&
1017                     (req->bRequest == UCDC_SET_COMM_FEATURE)) {
1018                         if (offset == 0) {
1019                                 *plen = sizeof(sc->sc_abstract_state);
1020                                 *pptr = &sc->sc_abstract_state;
1021                         } else {
1022                                 *plen = 0;
1023                         }
1024                         return (0);
1025                 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1026                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1027                     (req->wIndex[1] == 0x00) &&
1028                     (req->bRequest == UCDC_SET_CONTROL_LINE_STATE)) {
1029                         *plen = 0;
1030                         return (0);
1031                 } else if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
1032                     (req->wIndex[0] == sc->sc_ctrl_iface_no) &&
1033                     (req->wIndex[1] == 0x00) &&
1034                     (req->bRequest == UCDC_SEND_BREAK)) {
1035                         *plen = 0;
1036                         return (0);
1037                 }
1038         }
1039         return (ENXIO);                 /* use builtin handler */
1040 }