xref: /netbsd-src/sys/dev/usb/ugensa.c (revision cd22f25e6f6d1cc1f197fe8c5468a80f51d1c4e1)
1 /*	$NetBSD: ugensa.c,v 1.19 2008/04/28 20:23:59 martin 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.19 2008/04/28 20:23:59 martin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/tty.h>
41 
42 #include <dev/usb/usb.h>
43 
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdi_util.h>
46 #include <dev/usb/usbdevs.h>
47 
48 #include <dev/usb/ucomvar.h>
49 
50 /* XXXrcd: heh */
51 #define UGENSA_DEBUG 1
52 
53 #ifdef UGENSA_DEBUG
54 #define DPRINTF(x)	if (ugensadebug) printf x
55 #define DPRINTFN(n,x)	if (ugensadebug>(n)) printf x
56 int ugensadebug = 0;
57 #else
58 #define DPRINTF(x)
59 #define DPRINTFN(n,x)
60 #endif
61 
62 struct ugensa_softc {
63 	USBBASEDEVICE		sc_dev;		/* base device */
64 	usbd_device_handle	sc_udev;	/* device */
65 	usbd_interface_handle	sc_iface;	/* interface */
66 
67 	device_t		sc_subdev;
68 	int			sc_numcon;
69 
70 	u_char			sc_dying;
71 };
72 
73 struct ucom_methods ugensa_methods = {
74 	NULL,
75 	NULL,
76 	NULL,
77 	NULL,
78 	NULL,
79 	NULL,
80 	NULL,
81 	NULL,
82 };
83 
84 #define UGENSA_CONFIG_INDEX	0
85 #define UGENSA_IFACE_INDEX	0
86 #define UGENSA_BUFSIZE		1024
87 
88 struct ugensa_type {
89 	struct usb_devno	ugensa_dev;
90 	u_int16_t		ugensa_flags;
91 #define UNTESTED		0x0001
92 };
93 
94 static const struct ugensa_type ugensa_devs[] = {
95 	{{ USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220 }, 0 },
96 	{{ USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_FLEXPACKGPS }, 0 },
97 	{{ USB_VENDOR_QUALCOMM_K, USB_PRODUCT_QUALCOMM_K_CDMA_MSM_K }, 0 },
98 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580 }, 0 },
99 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595 }, 0 },
100 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725}, 0 },
101 	{{ USB_VENDOR_DELL, USB_PRODUCT_DELL_HSDPA }, 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_DELL, USB_PRODUCT_DELL_W5500 }, UNTESTED },
110         {{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_EXPRESSCARD }, UNTESTED },
111 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_MERLINV620 }, UNTESTED },
112 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_S720 }, UNTESTED },
113 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_U720 }, UNTESTED },
114 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_XU870 }, UNTESTED },
115 	{{ USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_ES620 }, UNTESTED },
116 	{{ USB_VENDOR_QUALCOMM, USB_PRODUCT_QUALCOMM_MSM_HSDPA }, UNTESTED },
117 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625 }, UNTESTED },
118 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875 }, UNTESTED },
119 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720 }, UNTESTED },
120 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725 }, UNTESTED },
121 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755 }, UNTESTED },
122 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2 }, UNTESTED },
123 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3 }, UNTESTED },
124 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765 }, UNTESTED },
125 	{{ USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775 }, UNTESTED }
126 };
127 #define ugensa_lookup(v, p) \
128 	((const struct ugensa_type *)usb_lookup(ugensa_devs, v, p))
129 
130 int ugensa_match(device_t, struct cfdata *, void *);
131 void ugensa_attach(device_t, device_t, void *);
132 void ugensa_childdet(device_t, device_t);
133 int ugensa_detach(device_t, int);
134 int ugensa_activate(device_t, enum devact);
135 extern struct cfdriver ugensa_cd;
136 CFATTACH_DECL2(ugensa, sizeof(struct ugensa_softc), ugensa_match,
137     ugensa_attach, ugensa_detach, ugensa_activate, NULL, ugensa_childdet);
138 
139 USB_MATCH(ugensa)
140 {
141 	USB_MATCH_START(ugensa, uaa);
142 
143 	DPRINTFN(20,("ugensa: vendor=0x%x, product=0x%x\n",
144 		     uaa->vendor, uaa->product));
145 
146 	return (ugensa_lookup(uaa->vendor, uaa->product) != NULL ?
147 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
148 }
149 
150 USB_ATTACH(ugensa)
151 {
152 	USB_ATTACH_START(ugensa, sc, uaa);
153 	usbd_device_handle dev = uaa->device;
154 	usbd_interface_handle iface;
155 	usb_interface_descriptor_t *id;
156 	usb_endpoint_descriptor_t *ed;
157 	char *devinfop;
158 	const char *devname = USBDEVNAME(sc->sc_dev);
159 	usbd_status err;
160 	struct ucom_attach_args uca;
161 	int i;
162 
163 	DPRINTFN(10,("\nugensa_attach: sc=%p\n", sc));
164 
165 	/* Move the device into the configured state. */
166 	err = usbd_set_config_index(dev, UGENSA_CONFIG_INDEX, 1);
167 	if (err) {
168 		printf("\n%s: failed to set configuration, err=%s\n",
169 		       devname, usbd_errstr(err));
170 		goto bad;
171 	}
172 
173 	err = usbd_device2interface_handle(dev, UGENSA_IFACE_INDEX, &iface);
174 	if (err) {
175 		printf("\n%s: failed to get interface, err=%s\n",
176 		       devname, usbd_errstr(err));
177 		goto bad;
178 	}
179 
180 	devinfop = usbd_devinfo_alloc(dev, 0);
181 	USB_ATTACH_SETUP;
182 	printf("%s: %s\n", devname, devinfop);
183 	usbd_devinfo_free(devinfop);
184 
185 	if (ugensa_lookup(uaa->vendor, uaa->product)->ugensa_flags & UNTESTED)
186 		printf("%s: WARNING: This device is marked as untested. "
187 		    "Please submit a report via send-pr(1).\n", devname);
188 
189 	id = usbd_get_interface_descriptor(iface);
190 
191 	sc->sc_udev = dev;
192 	sc->sc_iface = iface;
193 
194 	uca.info = "Generic Serial Device";
195 	uca.ibufsize = UGENSA_BUFSIZE;
196 	uca.obufsize = UGENSA_BUFSIZE;
197 	uca.ibufsizepad = UGENSA_BUFSIZE;
198 	uca.portno = UCOM_UNK_PORTNO;
199 	uca.opkthdrlen = 0;
200 	uca.device = dev;
201 	uca.iface = iface;
202 	uca.methods = &ugensa_methods;
203 	uca.arg = sc;
204 
205 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
206 			   USBDEV(sc->sc_dev));
207 
208 	uca.bulkin = uca.bulkout = -1;
209 	for (i = 0; i < id->bNumEndpoints; i++) {
210 		int addr, dir, attr;
211 
212 		ed = usbd_interface2endpoint_descriptor(iface, i);
213 		if (ed == NULL) {
214 			printf("%s: could not read endpoint descriptor"
215 			       ": %s\n", devname, usbd_errstr(err));
216 			goto bad;
217 		}
218 
219 		addr = ed->bEndpointAddress;
220 		dir = UE_GET_DIR(ed->bEndpointAddress);
221 		attr = ed->bmAttributes & UE_XFERTYPE;
222 		if (attr == UE_BULK) {
223 			if (uca.bulkin == -1 && dir == UE_DIR_IN) {
224 				DPRINTF(("%s: Bulk in %d\n", devname, i));
225 				uca.bulkin = addr;
226 				continue;
227 			}
228 			if (uca.bulkout == -1 && dir == UE_DIR_OUT) {
229 				DPRINTF(("%s: Bulk out %d\n", devname, i));
230 				uca.bulkout = addr;
231 				continue;
232 			}
233 		}
234 		printf("%s: unexpected endpoint\n", devname);
235 	}
236 	if (uca.bulkin == -1) {
237 		printf("%s: Could not find data bulk in\n",
238 		       USBDEVNAME(sc->sc_dev));
239 		goto bad;
240 	}
241 	if (uca.bulkout == -1) {
242 		printf("%s: Could not find data bulk out\n",
243 		       USBDEVNAME(sc->sc_dev));
244 		goto bad;
245 	}
246 
247 	DPRINTF(("ugensa: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
248 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, &uca,
249 					    ucomprint, ucomsubmatch);
250 
251 	if (!pmf_device_register(self, NULL, NULL))
252 		aprint_error_dev(self, "couldn't establish power handler\n");
253 	USB_ATTACH_SUCCESS_RETURN;
254 
255 bad:
256 	DPRINTF(("ugensa_attach: ATTACH ERROR\n"));
257 	sc->sc_dying = 1;
258 	USB_ATTACH_ERROR_RETURN;
259 }
260 
261 void
262 ugensa_childdet(device_t self, device_t child)
263 {
264 	struct ugensa_softc *sc = device_private(self);
265 
266 	KASSERT(sc->sc_subdev == child);
267 	sc->sc_subdev = NULL;
268 }
269 
270 int
271 ugensa_activate(device_t self, enum devact act)
272 {
273 	struct ugensa_softc *sc = device_private(self);
274 	int rv = 0;
275 
276 	DPRINTF(("ugensa_activate: sc=%p\n", sc));
277 
278 	switch (act) {
279 	case DVACT_ACTIVATE:
280 		return (EOPNOTSUPP);
281 		break;
282 
283 	case DVACT_DEACTIVATE:
284 		sc->sc_dying = 1;
285 		if (sc->sc_subdev)
286 			rv = config_deactivate(sc->sc_subdev);
287 		break;
288 	}
289 	return (rv);
290 }
291 
292 USB_DETACH(ugensa)
293 {
294 	USB_DETACH_START(ugensa, sc);
295 	int rv = 0;
296 
297 	DPRINTF(("ugensa_detach: sc=%p flags=%d\n", sc, flags));
298 
299 	sc->sc_dying = 1;
300 	pmf_device_deregister(self);
301 
302 	if (sc->sc_subdev != NULL)
303 		rv = config_detach(sc->sc_subdev, flags);
304 
305 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
306 			   USBDEV(sc->sc_dev));
307 
308 	return (rv);
309 }
310