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