xref: /netbsd-src/sys/dev/usb/ugensa.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ugensa.c,v 1.35 2016/11/25 12:56:29 skrll 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.35 2016/11/25 12:56:29 skrll 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 	struct usbd_device *	sc_udev;	/* device */
66 	struct usbd_interface *	sc_iface;	/* interface */
67 
68 	device_t		sc_subdev;
69 	int			sc_numcon;
70 
71 	u_char			sc_dying;
72 };
73 
74 struct ucom_methods ugensa_methods = {
75 	.ucom_get_status = NULL,
76 	.ucom_set = NULL,
77 	.ucom_param = NULL,
78 	.ucom_ioctl = NULL,
79 	.ucom_open = NULL,
80 	.ucom_close = NULL,
81 	.ucom_read = NULL,
82 	.ucom_write = NULL,
83 };
84 
85 #define UGENSA_CONFIG_INDEX	0
86 #define UGENSA_IFACE_INDEX	0
87 #define UGENSA_BUFSIZE		1024
88 
89 struct ugensa_type {
90 	struct usb_devno	ugensa_dev;
91 	uint16_t		ugensa_flags;
92 #define UNTESTED		0x0001
93 };
94 
95 static const struct ugensa_type ugensa_devs[] = {
96 	{{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 }, 0 },
97 	{{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }, 0 },
98 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS }, 0 },
99 	{{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K }, 0 },
100 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_USB305 }, 0 },
101 	{{ USB_VENDOR_ZTE, USB_PRODUCT_ZTE_AC8700 }, 0 },
102 
103 	/*
104 	 * The following devices are untested, but they are purported to
105 	 * to work in similar device drivers on other OSes:
106 	 */
107 
108 	{{ USB_VENDOR_ANYDATA, USB_PRODUCT_ANYDATA_ADU_500A }, UNTESTED },
109 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EXPRESSCARD }, UNTESTED },
110 	{{ USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_MSM_HSDPA }, UNTESTED },
111 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, UNTESTED },
112 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, UNTESTED },
113 };
114 #define ugensa_lookup(v, p) \
115 	((const struct ugensa_type *)usb_lookup(ugensa_devs, v, p))
116 
117 int ugensa_match(device_t, cfdata_t, void *);
118 void ugensa_attach(device_t, device_t, void *);
119 void ugensa_childdet(device_t, device_t);
120 int ugensa_detach(device_t, int);
121 int ugensa_activate(device_t, enum devact);
122 extern struct cfdriver ugensa_cd;
123 CFATTACH_DECL2_NEW(ugensa, sizeof(struct ugensa_softc), ugensa_match,
124     ugensa_attach, ugensa_detach, ugensa_activate, NULL, ugensa_childdet);
125 
126 int
127 ugensa_match(device_t parent, cfdata_t match, void *aux)
128 {
129 	struct usb_attach_arg *uaa = aux;
130 
131 	DPRINTFN(20,("ugensa: vendor=0x%x, product=0x%x\n",
132 		     uaa->uaa_vendor, uaa->uaa_product));
133 
134 	return ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
135 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
136 }
137 
138 void
139 ugensa_attach(device_t parent, device_t self, void *aux)
140 {
141 	struct ugensa_softc *sc = device_private(self);
142 	struct usb_attach_arg *uaa = aux;
143 	struct usbd_device *dev = uaa->uaa_device;
144 	struct usbd_interface *iface;
145 	usb_interface_descriptor_t *id;
146 	usb_endpoint_descriptor_t *ed;
147 	char *devinfop;
148 	const char *devname = device_xname(self);
149 	usbd_status err;
150 	struct ucom_attach_args ucaa;
151 	int i;
152 
153 	DPRINTFN(10,("\nugensa_attach: sc=%p\n", sc));
154 
155 	sc->sc_dev = self;
156 
157 	aprint_naive("\n");
158 	aprint_normal("\n");
159 
160 	devinfop = usbd_devinfo_alloc(dev, 0);
161 	aprint_normal_dev(self, "%s\n", devinfop);
162 	usbd_devinfo_free(devinfop);
163 
164 	/* Move the device into the configured state. */
165 	err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1);
166 	if (err) {
167 		aprint_error("\n%s: failed to set configuration, err=%s\n",
168 		       devname, usbd_errstr(err));
169 		goto bad;
170 	}
171 
172 	err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface);
173 	if (err) {
174 		aprint_error("\n%s: failed to get interface, err=%s\n",
175 		       devname, usbd_errstr(err));
176 		goto bad;
177 	}
178 
179 	if (ugensa_lookup(uaa->uaa_vendor, uaa->uaa_product)->ugensa_flags & UNTESTED)
180 		aprint_normal_dev(self, "WARNING: This device is marked as "
181 		    "untested. Please submit a report via send-pr(1).\n");
182 
183 	id = usbd_get_interface_descriptor(iface);
184 
185 	sc->sc_udev = dev;
186 	sc->sc_iface = iface;
187 
188 	ucaa.ucaa_info = "Generic Serial Device";
189 	ucaa.ucaa_ibufsize = UGENSA_BUFSIZE;
190 	ucaa.ucaa_obufsize = UGENSA_BUFSIZE;
191 	ucaa.ucaa_ibufsizepad = UGENSA_BUFSIZE;
192 	ucaa.ucaa_portno = UCOM_UNK_PORTNO;
193 	ucaa.ucaa_opkthdrlen = 0;
194 	ucaa.ucaa_device = dev;
195 	ucaa.ucaa_iface = iface;
196 	ucaa.ucaa_methods = &ugensa_methods;
197 	ucaa.ucaa_arg = sc;
198 
199 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
200 
201 	ucaa.ucaa_bulkin = ucaa.ucaa_bulkout = -1;
202 	for (i = 0; i < id->bNumEndpoints; i++) {
203 		int addr, dir, attr;
204 
205 		ed = usbd_interface2endpoint_descriptor(iface, i);
206 		if (ed == NULL) {
207 			aprint_error_dev(self,
208 			    "could not read endpoint descriptor: %s\n",
209 			    usbd_errstr(err));
210 			goto bad;
211 		}
212 
213 		addr = ed->bEndpointAddress;
214 		dir = UE_GET_DIR(ed->bEndpointAddress);
215 		attr = ed->bmAttributes & UE_XFERTYPE;
216 		if (attr == UE_BULK) {
217 			if (ucaa.ucaa_bulkin == -1 && dir == UE_DIR_IN) {
218 				DPRINTF(("%s: Bulk in %d\n", devname, i));
219 				ucaa.ucaa_bulkin = addr;
220 				continue;
221 			}
222 			if (ucaa.ucaa_bulkout == -1 && dir == UE_DIR_OUT) {
223 				DPRINTF(("%s: Bulk out %d\n", devname, i));
224 				ucaa.ucaa_bulkout = addr;
225 				continue;
226 			}
227 		}
228 		aprint_error_dev(self, "unexpected endpoint\n");
229 	}
230 	if (ucaa.ucaa_bulkin == -1) {
231 		aprint_error_dev(self, "Could not find data bulk in\n");
232 		goto bad;
233 	}
234 	if (ucaa.ucaa_bulkout == -1) {
235 		aprint_error_dev(self, "Could not find data bulk out\n");
236 		goto bad;
237 	}
238 
239 	DPRINTF(("ugensa: in=0x%x out=0x%x\n", ucaa.ucaa_bulkin,
240 	    ucaa.ucaa_bulkout));
241 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &ucaa,
242 					    ucomprint, ucomsubmatch);
243 
244 	if (!pmf_device_register(self, NULL, NULL))
245 		aprint_error_dev(self, "couldn't establish power handler\n");
246 	return;
247 
248 bad:
249 	DPRINTF(("ugensa_attach: ATTACH ERROR\n"));
250 	sc->sc_dying = 1;
251 	return;
252 }
253 
254 void
255 ugensa_childdet(device_t self, device_t child)
256 {
257 	struct ugensa_softc *sc = device_private(self);
258 
259 	KASSERT(sc->sc_subdev == child);
260 	sc->sc_subdev = NULL;
261 }
262 
263 int
264 ugensa_activate(device_t self, enum devact act)
265 {
266 	struct ugensa_softc *sc = device_private(self);
267 
268 	DPRINTF(("ugensa_activate: sc=%p\n", sc));
269 
270 	switch (act) {
271 	case DVACT_DEACTIVATE:
272 		sc->sc_dying = 1;
273 		return 0;
274 	default:
275 		return EOPNOTSUPP;
276 	}
277 }
278 
279 int
280 ugensa_detach(device_t self, int flags)
281 {
282 	struct ugensa_softc *sc = device_private(self);
283 	int rv = 0;
284 
285 	DPRINTF(("ugensa_detach: sc=%p flags=%d\n", sc, flags));
286 
287 	sc->sc_dying = 1;
288 	pmf_device_deregister(self);
289 
290 	if (sc->sc_subdev != NULL)
291 		rv = config_detach(sc->sc_subdev, flags);
292 
293 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
294 
295 	return rv;
296 }
297