1 /*        $NetBSD: umodem.c,v 1.74 2020/04/12 01:10:54 simonb Exp $   */
2 
3 /*
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Comm Class spec:  http://www.usb.org/developers/devclass_docs/usbccs10.pdf
35  *                   http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
36  */
37 
38 /*
39  * TODO:
40  * - Add error recovery in various places; the big problem is what
41  *   to do in a callback if there is an error.
42  * - Implement a Call Device for modems without multiplexed commands.
43  *
44  */
45 
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: umodem.c,v 1.74 2020/04/12 01:10:54 simonb Exp $");
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/ioctl.h>
53 #include <sys/conf.h>
54 #include <sys/tty.h>
55 #include <sys/file.h>
56 #include <sys/select.h>
57 #include <sys/proc.h>
58 #include <sys/vnode.h>
59 #include <sys/device.h>
60 #include <sys/poll.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbcdc.h>
64 
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/usb_quirks.h>
69 
70 #include <dev/usb/ucomvar.h>
71 #include <dev/usb/umodemvar.h>
72 
73 Static const struct ucom_methods umodem_methods = {
74           .ucom_get_status = umodem_get_status,
75           .ucom_set = umodem_set,
76           .ucom_param = umodem_param,
77           .ucom_ioctl = umodem_ioctl,
78           .ucom_open = umodem_open,
79           .ucom_close = umodem_close,
80 };
81 
82 static int          umodem_match(device_t, cfdata_t, void *);
83 static void         umodem_attach(device_t, device_t, void *);
84 static int          umodem_detach(device_t, int);
85 
86 
87 
88 CFATTACH_DECL_NEW(umodem, sizeof(struct umodem_softc), umodem_match,
89     umodem_attach, umodem_detach, NULL);
90 
91 static int
umodem_match(device_t parent,cfdata_t match,void * aux)92 umodem_match(device_t parent, cfdata_t match, void *aux)
93 {
94           struct usbif_attach_arg *uiaa = aux;
95           usb_interface_descriptor_t *id;
96           int cm, acm;
97 
98           if (uiaa->uiaa_class != UICLASS_CDC ||
99               uiaa->uiaa_subclass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
100               !(uiaa->uiaa_proto == UIPROTO_CDC_NOCLASS || uiaa->uiaa_proto == UIPROTO_CDC_AT))
101                     return UMATCH_NONE;
102 
103           id = usbd_get_interface_descriptor(uiaa->uiaa_iface);
104           if (umodem_get_caps(uiaa->uiaa_device, &cm, &acm, id) == -1)
105                     return UMATCH_NONE;
106 
107           return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
108 }
109 
110 static void
umodem_attach(device_t parent,device_t self,void * aux)111 umodem_attach(device_t parent, device_t self, void *aux)
112 {
113           struct umodem_softc *sc = device_private(self);
114           struct usbif_attach_arg *uiaa = aux;
115           struct ucom_attach_args ucaa;
116 
117           memset(&ucaa, 0, sizeof(ucaa));
118 
119           ucaa.ucaa_portno = UCOM_UNK_PORTNO;
120           ucaa.ucaa_methods = &umodem_methods;
121           ucaa.ucaa_info = NULL;
122 
123           if (!pmf_device_register(self, NULL, NULL))
124                     aprint_error_dev(self, "couldn't establish power handler");
125 
126           if (umodem_common_attach(self, sc, uiaa, &ucaa))
127                     return;
128           return;
129 }
130 
131 static int
umodem_detach(device_t self,int flags)132 umodem_detach(device_t self, int flags)
133 {
134           struct umodem_softc *sc = device_private(self);
135 
136           pmf_device_deregister(self);
137 
138           return umodem_common_detach(sc, flags);
139 }
140