xref: /openbsd/sys/dev/usb/uvisor.c (revision 404b540a)
1 /*	$OpenBSD: uvisor.c,v 1.39 2009/10/13 19:33:19 pirofti 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 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
293 			   &sc->sc_dev);
294 
295 	if (sc->sc_flags & VISOR) {
296 		sc->sc_numcon = UGETW(coninfo.num_ports);
297 		if (sc->sc_numcon > UVISOR_MAX_CONN)
298 			sc->sc_numcon = UVISOR_MAX_CONN;
299 
300 		/* Attach a ucom for each connection. */
301 		for (i = 0; i < sc->sc_numcon; ++i) {
302 			switch (coninfo.connections[i].port_function_id) {
303 			case UVISOR_FUNCTION_GENERIC:
304 				uca.info = "Generic";
305 				break;
306 			case UVISOR_FUNCTION_DEBUGGER:
307 				uca.info = "Debugger";
308 				break;
309 			case UVISOR_FUNCTION_HOTSYNC:
310 				uca.info = "HotSync";
311 				break;
312 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
313 				uca.info = "Remote File System";
314 				break;
315 			default:
316 				uca.info = "unknown";
317 				break;
318 			}
319 			port = coninfo.connections[i].port;
320 			uca.portno = port;
321 			uca.bulkin = port | UE_DIR_IN;
322 			uca.bulkout = port | UE_DIR_OUT;
323 			/* Verify that endpoints exist. */
324 			hasin = 0;
325 			hasout = 0;
326 			for (j = 0; j < id->bNumEndpoints; j++) {
327 				ed = usbd_interface2endpoint_descriptor(iface, j);
328 				if (ed == NULL)
329 					break;
330 				if (UE_GET_ADDR(ed->bEndpointAddress) == port &&
331 				    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
332 					if (UE_GET_DIR(ed->bEndpointAddress)
333 					    == UE_DIR_IN)
334 						hasin++;
335 					else
336 						hasout++;
337 				}
338 			}
339 			if (hasin == 1 && hasout == 1)
340 				sc->sc_subdevs[i] = config_found_sm(self, &uca,
341 				    ucomprint, ucomsubmatch);
342 			else
343 				printf("%s: no proper endpoints for port %d (%d,%d)\n",
344 				    sc->sc_dev.dv_xname, port, hasin, hasout);
345 		}
346 	} else {
347 		sc->sc_numcon = palmconinfo.num_ports;
348 		if (sc->sc_numcon > UVISOR_MAX_CONN)
349 			sc->sc_numcon = UVISOR_MAX_CONN;
350 
351 		/* Attach a ucom for each connection. */
352 		for (i = 0; i < sc->sc_numcon; ++i) {
353 			/*
354 			 * XXX this should copy out 4-char string from the
355 			 * XXX port_function_id, but where would the string go?
356 			 * XXX uca.info is a const char *, not an array.
357 			 */
358 			uca.info = "sync";
359 			uca.portno = i;
360 			if (palmconinfo.endpoint_numbers_different) {
361 				port = palmconinfo.connections[i].end_point_info;
362 				uca.bulkin = (port >> 4) | UE_DIR_IN;
363 				uca.bulkout = (port & 0xf) | UE_DIR_OUT;
364 			} else {
365 				port = palmconinfo.connections[i].port;
366 				uca.bulkin = port | UE_DIR_IN;
367 				uca.bulkout = port | UE_DIR_OUT;
368 			}
369 			sc->sc_subdevs[i] = config_found_sm(self, &uca,
370 			    ucomprint, ucomsubmatch);
371 		}
372 	}
373 
374 	return;
375 
376 bad:
377 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
378 	sc->sc_dying = 1;
379 }
380 
381 int
382 uvisor_activate(struct device *self, int act)
383 {
384 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
385 	int rv = 0;
386 	int i;
387 
388 	switch (act) {
389 	case DVACT_ACTIVATE:
390 		break;
391 
392 	case DVACT_DEACTIVATE:
393 		for (i = 0; i < sc->sc_numcon; i++)
394 			if (sc->sc_subdevs[i] != NULL)
395 				rv = config_deactivate(sc->sc_subdevs[i]);
396 		sc->sc_dying = 1;
397 		break;
398 	}
399 	return (rv);
400 }
401 
402 int
403 uvisor_detach(struct device *self, int flags)
404 {
405 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
406 	int rv = 0;
407 	int i;
408 
409 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
410 	sc->sc_dying = 1;
411 	for (i = 0; i < sc->sc_numcon; i++) {
412 		if (sc->sc_subdevs[i] != NULL) {
413 			rv |= config_detach(sc->sc_subdevs[i], flags);
414 			sc->sc_subdevs[i] = NULL;
415 		}
416 	}
417 
418 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
419 			   &sc->sc_dev);
420 
421 	return (rv);
422 }
423 
424 usbd_status
425 uvisor_init(struct uvisor_softc *sc, struct uvisor_connection_info *ci,
426     struct uvisor_palm_connection_info *cpi)
427 {
428 	usbd_status err;
429 	usb_device_request_t req;
430 	int actlen;
431 	uWord avail;
432 
433 	if (sc->sc_flags & PALM4) {
434 		DPRINTF(("uvisor_init: getting Palm connection info\n"));
435 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
436 		req.bRequest = UVISOR_GET_PALM_INFORMATION;
437 		USETW(req.wValue, 0);
438 		USETW(req.wIndex, 0);
439 		USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
440 		err = usbd_do_request_flags(sc->sc_udev, &req, cpi,
441 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
442 		if (err == USBD_STALLED && sc->sc_vendor == USB_VENDOR_SONY) {
443 			/* some sony clie devices stall on palm4 requests,
444 			 * switch them over to using visor. dont do free space
445 			 * checks on them since they dont like them either.
446 			 */
447 			DPRINTF(("switching role for CLIE probe\n"));
448 			sc->sc_flags = CLIE4;
449 			err = 0;
450 		}
451 		if (err)
452 			return (err);
453 	}
454 
455 	if (sc->sc_flags & VISOR) {
456 		DPRINTF(("uvisor_init: getting Visor connection info\n"));
457 		req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
458 		req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
459 		USETW(req.wValue, 0);
460 		USETW(req.wIndex, 0);
461 		USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
462 		err = usbd_do_request_flags(sc->sc_udev, &req, ci,
463 		    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
464 		if (err)
465 			return (err);
466 	}
467 
468 	if (sc->sc_flags & NOFRE)
469 		return (err);
470 
471 	DPRINTF(("uvisor_init: getting available bytes\n"));
472 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
473 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
474 	USETW(req.wValue, 0);
475 	USETW(req.wIndex, 5);
476 	USETW(req.wLength, sizeof avail);
477 	err = usbd_do_request(sc->sc_udev, &req, &avail);
478 	if (err)
479 		return (err);
480 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
481 	DPRINTF(("uvisor_init: done\n"));
482 	return (err);
483 }
484 
485 void
486 uvisor_close(void *addr, int portno)
487 {
488 	struct uvisor_softc *sc = addr;
489 	usb_device_request_t req;
490 	struct uvisor_connection_info coninfo; /* XXX ? */
491 	int actlen;
492 
493 	if (sc->sc_dying)
494 		return;
495 
496 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
497 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
498 	USETW(req.wValue, 0);
499 	USETW(req.wIndex, 0);
500 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
501 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
502 		  USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
503 }
504