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