xref: /netbsd-src/sys/dev/usb/ugensa.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: ugensa.c,v 1.45 2021/08/07 16:19:17 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roland C. Dowdeswell <elric@netbsd.org>.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ugensa.c,v 1.45 2021/08/07 16:19:17 thorpej Exp $");
34 
35 #ifdef _KERNEL_OPT
36 #include "opt_usb.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/conf.h>
44 #include <sys/tty.h>
45 
46 #include <dev/usb/usb.h>
47 
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
50 #include <dev/usb/usbdevs.h>
51 
52 #include <dev/usb/ucomvar.h>
53 
54 #ifdef UGENSA_DEBUG
55 #define DPRINTF(x)	if (ugensadebug) printf x
56 #define DPRINTFN(n,x)	if (ugensadebug>(n)) printf x
57 int ugensadebug = 0;
58 #else
59 #define DPRINTF(x)
60 #define DPRINTFN(n,x)
61 #endif
62 
63 struct ugensa_softc {
64 	device_t		sc_dev;		/* base device */
65 
66 	enum {
67 		UGENSA_INIT_NONE,
68 		UGENSA_INIT_INITED
69 	} sc_init_state;
70 
71 	struct usbd_device *	sc_udev;	/* device */
72 	struct usbd_interface *	sc_iface;	/* interface */
73 
74 	device_t		sc_subdev;
75 	int			sc_numcon;
76 
77 	bool			sc_dying;
78 };
79 
80 struct ucom_methods ugensa_methods = { 0 };
81 
82 #define UGENSA_CONFIG_INDEX	0
83 #define UGENSA_IFACE_INDEX	0
84 #define UGENSA_BUFSIZE		1024
85 
86 struct ugensa_type {
87 	struct usb_devno	ugensa_dev;
88 	uint16_t		ugensa_flags;
89 #define UNTESTED		0x0001
90 };
91 
92 static const struct ugensa_type ugensa_devs[] = {
93 	{{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 }, 0 },
94 	{{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }, 0 },
95 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS }, 0 },
96 	{{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K }, 0 },
97 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_AC8700 }, 0 },
98 	{{ USB_VENDOR_LINUXFOUNDATION, USB_PRODUCT_LINUXFOUNDATION_USB3DEBUG}, 0 },
99 
100 	/*
101 	 * The following devices are untested, but they are purported to
102 	 * to work in similar device drivers on other OSes:
103 	 */
104 
105 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A }, UNTESTED },
106 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EXPRESSCARD }, UNTESTED },
107 	{{ USB_VENDOR_LG, USB_PRODUCT_LG_MSM_HSDPA }, UNTESTED },
108 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, UNTESTED },
109 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, UNTESTED },
110 };
111 #define ugensa_lookup(v, p) \
112 	((const struct ugensa_type *)usb_lookup(ugensa_devs, v, p))
113 
114 static int	ugensa_match(device_t, cfdata_t, void *);
115 static void	ugensa_attach(device_t, device_t, void *);
116 static void	ugensa_childdet(device_t, device_t);
117 static int	ugensa_detach(device_t, int);
118 
119 CFATTACH_DECL2_NEW(ugensa, sizeof(struct ugensa_softc), ugensa_match,
120     ugensa_attach, ugensa_detach, NULL, NULL, ugensa_childdet);
121 
122 static int
ugensa_match(device_t parent,cfdata_t match,void * aux)123 ugensa_match(device_t parent, cfdata_t match, void *aux)
124 {
125 	struct usb_attach_arg *uaa = aux;
126 
127 	DPRINTFN(20,("ugensa: vendor=%#x, product=%#x\n",
128 		     uaa->uaa_vendor, uaa->uaa_product));
129 
130 	return ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
131 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
132 }
133 
134 static void
ugensa_attach(device_t parent,device_t self,void * aux)135 ugensa_attach(device_t parent, device_t self, void *aux)
136 {
137 	struct ugensa_softc *sc = device_private(self);
138 	struct usb_attach_arg *uaa = aux;
139 	struct usbd_device *dev = uaa->uaa_device;
140 	struct usbd_interface *iface;
141 	usb_interface_descriptor_t *id;
142 	usb_endpoint_descriptor_t *ed;
143 	char *devinfop;
144 	const char *devname = device_xname(self);
145 	usbd_status err;
146 	struct ucom_attach_args ucaa;
147 	int i;
148 
149 	DPRINTFN(10,("\nugensa_attach: sc=%p\n", sc));
150 
151 	sc->sc_dev = self;
152 	sc->sc_dying = false;
153 	sc->sc_init_state = UGENSA_INIT_NONE;
154 
155 	aprint_naive("\n");
156 	aprint_normal("\n");
157 
158 	devinfop = usbd_devinfo_alloc(dev, 0);
159 	aprint_normal_dev(self, "%s\n", devinfop);
160 	usbd_devinfo_free(devinfop);
161 
162 	/* Move the device into the configured state. */
163 	err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1);
164 	if (err) {
165 		aprint_error("\n%s: failed to set configuration, err=%s\n",
166 		       devname, usbd_errstr(err));
167 		goto bad;
168 	}
169 
170 	err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface);
171 	if (err) {
172 		aprint_error("\n%s: failed to get interface, err=%s\n",
173 		       devname, usbd_errstr(err));
174 		goto bad;
175 	}
176 
177 	if (ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product)->ugensa_flags & UNTESTED)
178 		aprint_normal_dev(self, "WARNING: This device is marked as "
179 		    "untested. Please submit a report via send-pr(1).\n");
180 
181 	id = usbd_get_interface_descriptor(iface);
182 
183 	sc->sc_udev = dev;
184 	sc->sc_iface = iface;
185 
186 	ucaa.ucaa_info = "Generic Serial Device";
187 	ucaa.ucaa_ibufsize = UGENSA_BUFSIZE;
188 	ucaa.ucaa_obufsize = UGENSA_BUFSIZE;
189 	ucaa.ucaa_ibufsizepad = UGENSA_BUFSIZE;
190 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
191 	ucaa.ucaa_opkthdrlen = 0;
192 	ucaa.ucaa_device = dev;
193 	ucaa.ucaa_iface = iface;
194 	ucaa.ucaa_methods = &ugensa_methods;
195 	ucaa.ucaa_arg = sc;
196 
197 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
198 	for (i = 0; i < id->bNumEndpoints; i++) {
199 		int addr, dir, attr;
200 
201 		ed = usbd_interface2endpoint_descriptor(iface, i);
202 		if (ed == NULL) {
203 			aprint_error_dev(self,
204 			    "could not read endpoint descriptor: %s\n",
205 			    usbd_errstr(err));
206 			goto bad;
207 		}
208 
209 		addr = ed->bEndpointAddress;
210 		dir = UE_GET_DIR(ed->bEndpointAddress);
211 		attr = ed->bmAttributes & UE_XFERTYPE;
212 		if (attr == UE_BULK) {
213 			if (ucaa.ucaa_bulkin == -1 && dir == UE_DIR_IN) {
214 				DPRINTF(("%s: Bulk in %d\n", devname, i));
215 				ucaa.ucaa_bulkin = addr;
216 				continue;
217 			}
218 			if (ucaa.ucaa_bulkout == -1 && dir == UE_DIR_OUT) {
219 				DPRINTF(("%s: Bulk out %d\n", devname, i));
220 				ucaa.ucaa_bulkout = addr;
221 				continue;
222 			}
223 		}
224 		aprint_error_dev(self, "unexpected endpoint\n");
225 	}
226 	if (ucaa.ucaa_bulkin == -1) {
227 		aprint_error_dev(self, "Could not find data bulk in\n");
228 		goto bad;
229 	}
230 	if (ucaa.ucaa_bulkout == -1) {
231 		aprint_error_dev(self, "Could not find data bulk out\n");
232 		goto bad;
233 	}
234 
235 	sc->sc_init_state = UGENSA_INIT_INITED;
236 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
237 
238 	DPRINTF(("ugensa: in=%#x out=%#x\n", ucaa.ucaa_bulkin,
239 	    ucaa.ucaa_bulkout));
240 	sc->sc_subdev = config_found(self, &ucaa, ucomprint,
241 	    CFARGS(.submatch = ucomsubmatch));
242 
243 	if (!pmf_device_register(self, NULL, NULL))
244 		aprint_error_dev(self, "couldn't establish power handler\n");
245 	return;
246 
247 bad:
248 	DPRINTF(("ugensa_attach: ATTACH ERROR\n"));
249 	sc->sc_dying = true;
250 	return;
251 }
252 
253 static void
ugensa_childdet(device_t self,device_t child)254 ugensa_childdet(device_t self, device_t child)
255 {
256 	struct ugensa_softc *sc = device_private(self);
257 
258 	KASSERT(sc->sc_subdev == child);
259 	sc->sc_subdev = NULL;
260 }
261 
262 static int
ugensa_detach(device_t self,int flags)263 ugensa_detach(device_t self, int flags)
264 {
265 	struct ugensa_softc *sc = device_private(self);
266 	int rv = 0;
267 
268 	DPRINTF(("ugensa_detach: sc=%p flags=%d\n", sc, flags));
269 
270 	sc->sc_dying = true;
271 
272 	if (sc->sc_init_state < UGENSA_INIT_INITED)
273 		return 0;
274 
275 	if (sc->sc_subdev != NULL) {
276 		rv = config_detach(sc->sc_subdev, flags);
277 		sc->sc_subdev = NULL;
278 	}
279 
280 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
281 
282 	pmf_device_deregister(self);
283 
284 	return rv;
285 }
286