xref: /openbsd-src/sys/dev/usb/uvisor.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: uvisor.c,v 1.44 2011/07/03 15:47:18 matthew Exp $	*/
2 /*	$NetBSD: uvisor.c,v 1.21 2003/08/03 21:59:26 nathanw Exp $	*/
3 
4 /*
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Handspring Visor (Palmpilot compatible PDA) driver
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/conf.h>
43 #include <sys/tty.h>
44 
45 #include <dev/usb/usb.h>
46 #include <dev/usb/usbhid.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 UVISOR_DEBUG
55 #define DPRINTF(x)	if (uvisordebug) printf x
56 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
57 int uvisordebug = 0;
58 #else
59 #define DPRINTF(x)
60 #define DPRINTFN(n,x)
61 #endif
62 
63 #define UVISOR_CONFIG_INDEX	0
64 #define UVISOR_IFACE_INDEX	0
65 
66 /* From the Linux driver */
67 /*
68  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
69  * are available to be transfered to the host for the specified endpoint.
70  * Currently this is not used, and always returns 0x0001
71  */
72 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
73 
74 /*
75  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
76  * is now closing the pipe. An empty packet is sent in response.
77  */
78 #define UVISOR_CLOSE_NOTIFICATION		0x02
79 
80 /*
81  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
82  * get the endpoints used by the connection.
83  */
84 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
85 
86 
87 /*
88  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
89  */
90 #define UVISOR_MAX_CONN 8
91 struct uvisor_connection_info {
92 	uWord	num_ports;
93 	struct {
94 		uByte	port_function_id;
95 		uByte	port;
96 	} connections[UVISOR_MAX_CONN];
97 };
98 #define UVISOR_CONNECTION_INFO_SIZE 18
99 
100 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
101 #define UVISOR_FUNCTION_GENERIC		0x00
102 #define UVISOR_FUNCTION_DEBUGGER	0x01
103 #define UVISOR_FUNCTION_HOTSYNC		0x02
104 #define UVISOR_FUNCTION_CONSOLE		0x03
105 #define UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
106 
107 /*
108  * Unknown PalmOS stuff.
109  */
110 #define UVISOR_GET_PALM_INFORMATION		0x04
111 #define UVISOR_GET_PALM_INFORMATION_LEN		0x44
112 
113 struct uvisor_palm_connection_info {
114 	uByte	num_ports;
115 	uByte	endpoint_numbers_different;
116 	uWord	reserved1;
117 	struct {
118 		uDWord	port_function_id;
119 		uByte	port;
120 		uByte	end_point_info;
121 		uWord	reserved;
122 	} connections[UVISOR_MAX_CONN];
123 };
124 
125 #define UVISORIBUFSIZE 64
126 #define UVISOROBUFSIZE 1024
127 
128 struct uvisor_softc {
129 	struct device		sc_dev;		/* base device */
130 	usbd_device_handle	sc_udev;	/* device */
131 	usbd_interface_handle	sc_iface;	/* interface */
132 /*
133  * added sc_vendor for later interrogation in failed initialisations
134  */
135 	int			sc_vendor;	/* USB device vendor */
136 
137 	struct device		*sc_subdevs[UVISOR_MAX_CONN];
138 	int			sc_numcon;
139 
140 	u_int16_t		sc_flags;
141 
142 	u_char			sc_dying;
143 };
144 
145 usbd_status uvisor_init(struct uvisor_softc *,
146 			       struct uvisor_connection_info *,
147 			       struct uvisor_palm_connection_info *);
148 
149 void uvisor_close(void *, int);
150 
151 
152 struct ucom_methods uvisor_methods = {
153 	NULL,
154 	NULL,
155 	NULL,
156 	NULL,
157 	NULL,
158 	uvisor_close,
159 	NULL,
160 	NULL,
161 };
162 
163 struct uvisor_type {
164 	struct usb_devno	uv_dev;
165 	u_int16_t		uv_flags;
166 #define PALM4	0x0001
167 #define VISOR	0x0002
168 #define NOFRE	0x0004
169 #define CLIE4	(VISOR|NOFRE)
170 };
171 static const struct uvisor_type uvisor_devs[] = {
172 	{{ USB_VENDOR_ACEECA, USB_PRODUCT_ACEECA_MEZ1000 }, PALM4 },
173 	{{ USB_VENDOR_FOSSIL, USB_PRODUCT_FOSSIL_WRISTPDA }, PALM4 },
174 	{{ USB_VENDOR_GARMIN, USB_PRODUCT_GARMIN_IQUE3600 }, PALM4 },
175 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, VISOR },
176 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO }, PALM4 },
177 	{{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_TREO600 }, VISOR },
178 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
179 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
180 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
181 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_I705 }, PALM4 },
182 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
183 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M130 }, PALM4 },
184 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_Z }, PALM4 },
185 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_TUNGSTEN_T }, PALM4 },
186 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE }, PALM4 },
187 	{{ USB_VENDOR_PALM, USB_PRODUCT_PALM_ZIRE_31 }, PALM4 },
188 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
189 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, PALM4 },
190 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_S360 }, PALM4 },
191 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_NX60 }, PALM4 },
192 	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_TJ25 }, PALM4 },
193 /*	{{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
194 	{{ USB_VENDOR_TAPWAVE, USB_PRODUCT_TAPWAVE_ZODIAC }, PALM4 },
195 };
196 #define uvisor_lookup(v, p) ((struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
197 
198 int uvisor_match(struct device *, void *, void *);
199 void uvisor_attach(struct device *, struct device *, void *);
200 int uvisor_detach(struct device *, int);
201 int uvisor_activate(struct device *, int);
202 
203 struct cfdriver uvisor_cd = {
204 	NULL, "uvisor", DV_DULL
205 };
206 
207 const struct cfattach uvisor_ca = {
208 	sizeof(struct uvisor_softc),
209 	uvisor_match,
210 	uvisor_attach,
211 	uvisor_detach,
212 	uvisor_activate,
213 };
214 
215 int
216 uvisor_match(struct device *parent, void *match, void *aux)
217 {
218 	struct usb_attach_arg *uaa = aux;
219 
220 	if (uaa->iface != NULL)
221 		return (UMATCH_NONE);
222 
223 	DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
224 		     uaa->vendor, uaa->product));
225 
226 	return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
227 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
228 }
229 
230 void
231 uvisor_attach(struct device *parent, struct device *self, void *aux)
232 {
233 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
234 	struct usb_attach_arg *uaa = aux;
235 	usbd_device_handle dev = uaa->device;
236 	usbd_interface_handle iface;
237 	usb_interface_descriptor_t *id;
238 	struct uvisor_connection_info coninfo;
239 	struct uvisor_palm_connection_info palmconinfo;
240 	usb_endpoint_descriptor_t *ed;
241 	int i, j, hasin, hasout, port;
242 	usbd_status err;
243 	struct ucom_attach_args uca;
244 
245 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
246 
247 	/* Move the device into the configured state. */
248 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
249 	if (err) {
250 		printf(": failed to set configuration, err=%s\n",
251 		    usbd_errstr(err));
252 		goto bad;
253 	}
254 
255 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
256 	if (err) {
257 		printf(": failed to get interface, err=%s\n",
258 		    usbd_errstr(err));
259 		goto bad;
260 	}
261 
262 	sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
263 	sc->sc_vendor = uaa->vendor;
264 
265 	if ((sc->sc_flags & (VISOR | PALM4)) == 0) {
266 		printf("%s: device is neither visor nor palm\n",
267 		    sc->sc_dev.dv_xname);
268 		goto bad;
269 	}
270 
271 	id = usbd_get_interface_descriptor(iface);
272 
273 	sc->sc_udev = dev;
274 	sc->sc_iface = iface;
275 
276 	uca.ibufsize = UVISORIBUFSIZE;
277 	uca.obufsize = UVISOROBUFSIZE;
278 	uca.ibufsizepad = UVISORIBUFSIZE;
279 	uca.opkthdrlen = 0;
280 	uca.device = dev;
281 	uca.iface = iface;
282 	uca.methods = &uvisor_methods;
283 	uca.arg = sc;
284 
285 	err = uvisor_init(sc, &coninfo, &palmconinfo);
286 	if (err) {
287 		printf("%s: init failed, %s\n", sc->sc_dev.dv_xname,
288 		       usbd_errstr(err));
289 		goto bad;
290 	}
291 
292 	if (sc->sc_flags & VISOR) {
293 		sc->sc_numcon = UGETW(coninfo.num_ports);
294 		if (sc->sc_numcon > UVISOR_MAX_CONN)
295 			sc->sc_numcon = UVISOR_MAX_CONN;
296 
297 		/* Attach a ucom for each connection. */
298 		for (i = 0; i < sc->sc_numcon; ++i) {
299 			switch (coninfo.connections[i].port_function_id) {
300 			case UVISOR_FUNCTION_GENERIC:
301 				uca.info = "Generic";
302 				break;
303 			case UVISOR_FUNCTION_DEBUGGER:
304 				uca.info = "Debugger";
305 				break;
306 			case UVISOR_FUNCTION_HOTSYNC:
307 				uca.info = "HotSync";
308 				break;
309 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
310 				uca.info = "Remote File System";
311 				break;
312 			default:
313 				uca.info = "unknown";
314 				break;
315 			}
316 			port = coninfo.connections[i].port;
317 			uca.portno = port;
318 			uca.bulkin = port | UE_DIR_IN;
319 			uca.bulkout = port | UE_DIR_OUT;
320 			/* Verify that endpoints exist. */
321 			hasin = 0;
322 			hasout = 0;
323 			for (j = 0; j < id->bNumEndpoints; j++) {
324 				ed = usbd_interface2endpoint_descriptor(iface, j);
325 				if (ed == NULL)
326 					break;
327 				if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
328 				    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
329 					if (UE_GET_DIR(ed->bEndpointAddress)
330 					    == UE_DIR_IN)
331 						hasin++;
332 					else
333 						hasout++;
334 				}
335 			}
336 			if (hasin == 1 && hasout == 1)
337 				sc->sc_subdevs[i] = config_found_sm(self, &uca,
338 				    ucomprint, ucomsubmatch);
339 			else
340 				printf("%s: no proper endpoints for port %d (%d,%d)\n",
341 				    sc->sc_dev.dv_xname, port, hasin, hasout);
342 		}
343 	} else {
344 		sc->sc_numcon = palmconinfo.num_ports;
345 		if (sc->sc_numcon > UVISOR_MAX_CONN)
346 			sc->sc_numcon = UVISOR_MAX_CONN;
347 
348 		/* Attach a ucom for each connection. */
349 		for (i = 0; i < sc->sc_numcon; ++i) {
350 			/*
351 			 * XXX this should copy out 4-char string from the
352 			 * XXX port_function_id, but where would the string go?
353 			 * XXX uca.info is a const char *, not an array.
354 			 */
355 			uca.info = "sync";
356 			uca.portno = i;
357 			if (palmconinfo.endpoint_numbers_different) {
358 				port = palmconinfo.connections[i].end_point_info;
359 				uca.bulkin = (port >> 4) | UE_DIR_IN;
360 				uca.bulkout = (port & 0xf) | UE_DIR_OUT;
361 			} else {
362 				port = palmconinfo.connections[i].port;
363 				uca.bulkin = port | UE_DIR_IN;
364 				uca.bulkout = port | UE_DIR_OUT;
365 			}
366 			sc->sc_subdevs[i] = config_found_sm(self, &uca,
367 			    ucomprint, ucomsubmatch);
368 		}
369 	}
370 
371 	return;
372 
373 bad:
374 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
375 	sc->sc_dying = 1;
376 }
377 
378 int
379 uvisor_activate(struct device *self, int act)
380 {
381 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
382 	int i, rv = 0, r;
383 
384 	switch (act) {
385 	case DVACT_DEACTIVATE:
386 		for (i = 0; i < sc->sc_numcon; i++)
387 			if (sc->sc_subdevs[i] != NULL) {
388 				r = config_deactivate(sc->sc_subdevs[i]);
389 				if (r)
390 					rv = r;
391 			}
392 		sc->sc_dying = 1;
393 		break;
394 	}
395 	return (rv);
396 }
397 
398 int
399 uvisor_detach(struct device *self, int flags)
400 {
401 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
402 	int rv = 0;
403 	int i;
404 
405 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
406 	for (i = 0; i < sc->sc_numcon; i++) {
407 		if (sc->sc_subdevs[i] != NULL) {
408 			rv |= config_detach(sc->sc_subdevs[i], flags);
409 			sc->sc_subdevs[i] = NULL;
410 		}
411 	}
412 
413 	return (rv);
414 }
415 
416 usbd_status
417 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
418     struct uvisor_palm_connection_info *cpi)
419 {
420 	usbd_status err = 0;
421 	usb_device_request_t req;
422 	int actlen;
423 	uWord avail;
424 
425 	if (sc->sc_flags & PALM4) {
426 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
427 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
428 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
429 		USETW(req.wValue, 0);
430 		USETW(req.wIndex, 0);
431 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
432 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
433 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
434 		if (err == USBD_STALLED && sc->sc_vendor == USB_VENDOR_SONY) {
435 			/* some sony clie devices stall on palm4 requests,
436 			 * switch them over to using visor. dont do free space
437 			 * checks on them since they dont like them either.
438 			 */
439 			DPRINTF(("switching role for CLIE probe\n"));
440 			sc->sc_flags = CLIE4;
441 			err = 0;
442 		}
443 		if (err)
444 			return (err);
445 	}
446 
447 	if (sc->sc_flags & VISOR) {
448 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
449 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
450 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
451 		USETW(req.wValue, 0);
452 		USETW(req.wIndex, 0);
453 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
454 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
455 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
456 		if (err)
457 			return (err);
458 	}
459 
460 	if (sc->sc_flags & NOFRE)
461 		return (err);
462 
463 	DPRINTF(("uvisor_init: getting available bytes\n"));
464 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
465 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
466 	USETW(req.wValue, 0);
467 	USETW(req.wIndex, 5);
468 	USETW(req.wLength, sizeof avail);
469 	err = usbd_do_request(sc->sc_udev, &req, &avail);
470 	if (err)
471 		return (err);
472 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
473 	DPRINTF(("uvisor_init: done\n"));
474 	return (err);
475 }
476 
477 void
478 uvisor_close(void *addr, int portno)
479 {
480 	struct uvisor_softc *sc = addr;
481 	usb_device_request_t req;
482 	struct uvisor_connection_info coninfo; /* XXX ? */
483 	int actlen;
484 
485 	if (sc->sc_dying)
486 		return;
487 
488 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
489 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
490 	USETW(req.wValue, 0);
491 	USETW(req.wIndex, 0);
492 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
493 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
494 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
495 }
496