xref: /netbsd-src/sys/dev/usb/uvisor.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: uvisor.c,v 1.7 2000/06/01 14:29:03 augustss 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/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #include <sys/conf.h>
49 #include <sys/tty.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbhid.h>
53 
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbdevs.h>
57 
58 #include <dev/usb/ucomvar.h>
59 
60 #ifdef UVISOR_DEBUG
61 #define DPRINTF(x)	if (uvisordebug) printf x
62 #define DPRINTFN(n,x)	if (uvisordebug>(n)) printf x
63 int uvisordebug = 0;
64 #else
65 #define DPRINTF(x)
66 #define DPRINTFN(n,x)
67 #endif
68 
69 #define UVISOR_CONFIG_INDEX	0
70 #define UVISOR_IFACE_INDEX	0
71 
72 /* From the Linux driver */
73 /*
74  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
75  * are available to be transfered to the host for the specified endpoint.
76  * Currently this is not used, and always returns 0x0001
77  */
78 #define UVISOR_REQUEST_BYTES_AVAILABLE		0x01
79 
80 /*
81  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
82  * is now closing the pipe. An empty packet is sent in response.
83  */
84 #define UVISOR_CLOSE_NOTIFICATION		0x02
85 
86 /*
87  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
88  * get the endpoints used by the connection.
89  */
90 #define UVISOR_GET_CONNECTION_INFORMATION	0x03
91 
92 
93 /*
94  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
95  */
96 struct uvisor_connection_info {
97 	uWord	num_ports;
98 	struct {
99 		uByte	port_function_id;
100 		uByte	port;
101 	} connections[8];
102 };
103 #define UVISOR_CONNECTION_INFO_SIZE 18
104 
105 
106 /* struct uvisor_connection_info.connection[x].port defines: */
107 #define UVISOR_ENDPOINT_1		0x01
108 #define UVISOR_ENDPOINT_2		0x02
109 
110 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
111 #define UVISOR_FUNCTION_GENERIC		0x00
112 #define UVISOR_FUNCTION_DEBUGGER	0x01
113 #define UVISOR_FUNCTION_HOTSYNC		0x02
114 #define UVISOR_FUNCTION_CONSOLE		0x03
115 #define UVISOR_FUNCTION_REMOTE_FILE_SYS	0x04
116 
117 
118 #define UVISORIBUFSIZE 1024
119 #define UVISOROBUFSIZE 1024
120 
121 struct uvisor_softc {
122 	USBBASEDEVICE		sc_dev;		/* base device */
123 	usbd_device_handle	sc_udev;	/* device */
124 	usbd_interface_handle	sc_iface;	/* interface */
125 
126 	device_ptr_t		sc_subdev;
127 
128 	u_char			sc_dying;
129 };
130 
131 Static usbd_status uvisor_init(struct uvisor_softc *);
132 
133 Static void uvisor_close(void *, int);
134 
135 
136 struct ucom_methods uvisor_methods = {
137 	NULL,
138 	NULL,
139 	NULL,
140 	NULL,
141 	NULL,
142 	uvisor_close,
143 	NULL,
144 	NULL,
145 };
146 
147 USB_DECLARE_DRIVER(uvisor);
148 
149 USB_MATCH(uvisor)
150 {
151 	USB_MATCH_START(uvisor, uaa);
152 
153 	if (uaa->iface != NULL)
154 		return (UMATCH_NONE);
155 
156 	DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
157 		     uaa->vendor, uaa->product));
158 
159 	if (uaa->vendor == USB_VENDOR_HANDSPRING &&
160 	    uaa->product == USB_PRODUCT_HANDSPRING_VISOR)
161 		return (UMATCH_VENDOR_PRODUCT);
162 
163 	return (UMATCH_NONE);
164 }
165 
166 USB_ATTACH(uvisor)
167 {
168 	USB_ATTACH_START(uvisor, sc, uaa);
169 	usbd_device_handle dev = uaa->device;
170 	usbd_interface_handle iface;
171 	usb_interface_descriptor_t *id;
172 	usb_endpoint_descriptor_t *ed;
173 	char devinfo[1024];
174 	char *devname = USBDEVNAME(sc->sc_dev);
175 	int i;
176 	usbd_status err;
177 	struct ucom_attach_args uca;
178 
179 	DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
180 
181 	/* Move the device into the configured state. */
182 	err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
183 	if (err) {
184 		printf("\n%s: failed to set configuration, err=%s\n",
185 		       devname, usbd_errstr(err));
186 		goto bad;
187 	}
188 
189 	err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
190 	if (err) {
191 		printf("\n%s: failed to get interface, err=%s\n",
192 		       devname, usbd_errstr(err));
193 		goto bad;
194 	}
195 
196 	usbd_devinfo(dev, 0, devinfo);
197 	USB_ATTACH_SETUP;
198 	printf("%s: %s\n", devname, devinfo);
199 
200 	id = usbd_get_interface_descriptor(iface);
201 
202 	sc->sc_udev = dev;
203 	sc->sc_iface = iface;
204 
205 	uca.bulkin = uca.bulkout = -1;
206 	for (i = 0; i < id->bNumEndpoints; i++) {
207 		int addr, dir, attr;
208 		ed = usbd_interface2endpoint_descriptor(iface, i);
209 		if (ed == NULL) {
210 			printf("%s: could not read endpoint descriptor"
211 			       ": %s\n", devname, usbd_errstr(err));
212 			goto bad;
213 		}
214 
215 		addr = ed->bEndpointAddress;
216 		dir = UE_GET_DIR(ed->bEndpointAddress);
217 		attr = ed->bmAttributes & UE_XFERTYPE;
218 		if (dir == UE_DIR_IN && attr == UE_BULK)
219 			uca.bulkin = addr;
220 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
221 			uca.bulkout = addr;
222 		else {
223 			printf("%s: unexpected endpoint\n", devname);
224 			goto bad;
225 		}
226 	}
227 	if (uca.bulkin == -1) {
228 		printf("%s: Could not find data bulk in\n",
229 		       USBDEVNAME(sc->sc_dev));
230 		goto bad;
231 	}
232 	if (uca.bulkout == -1) {
233 		printf("%s: Could not find data bulk out\n",
234 		       USBDEVNAME(sc->sc_dev));
235 		goto bad;
236 	}
237 
238 	uca.portno = UCOM_UNK_PORTNO;
239 	/* bulkin, bulkout set above */
240 	uca.ibufsize = UVISORIBUFSIZE;
241 	uca.obufsize = UVISOROBUFSIZE;
242 	uca.ibufsizepad = UVISORIBUFSIZE;
243 	uca.obufsizepad = UVISOROBUFSIZE;
244 	uca.device = dev;
245 	uca.iface = iface;
246 	uca.methods = &uvisor_methods;
247 	uca.arg = sc;
248 
249 	err = uvisor_init(sc);
250 	if (err) {
251 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
252 		       usbd_errstr(err));
253 		goto bad;
254 	}
255 
256 	DPRINTF(("uvisor: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
257 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
258 
259 	USB_ATTACH_SUCCESS_RETURN;
260 
261 bad:
262 	DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
263 	sc->sc_dying = 1;
264 	USB_ATTACH_ERROR_RETURN;
265 }
266 
267 int
268 uvisor_activate(device_ptr_t self, enum devact act)
269 {
270 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
271 	int rv = 0;
272 
273 	switch (act) {
274 	case DVACT_ACTIVATE:
275 		return (EOPNOTSUPP);
276 		break;
277 
278 	case DVACT_DEACTIVATE:
279 		if (sc->sc_subdev != NULL)
280 			rv = config_deactivate(sc->sc_subdev);
281 		sc->sc_dying = 1;
282 		break;
283 	}
284 	return (rv);
285 }
286 
287 int
288 uvisor_detach(device_ptr_t self, int flags)
289 {
290 	struct uvisor_softc *sc = (struct uvisor_softc *)self;
291 	int rv = 0;
292 
293 	DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
294 	sc->sc_dying = 1;
295 	if (sc->sc_subdev != NULL) {
296 		rv = config_detach(sc->sc_subdev, flags);
297 		sc->sc_subdev = NULL;
298 	}
299 	return (0);
300 }
301 
302 usbd_status
303 uvisor_init(struct uvisor_softc *sc)
304 {
305 	usbd_status err;
306 	usb_device_request_t req;
307 	struct uvisor_connection_info coninfo;
308 	int actlen;
309 	uWord avail;
310 
311 	DPRINTF(("uvisor_init: getting connection info\n"));
312 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
313 	req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
314 	USETW(req.wValue, 0);
315 	USETW(req.wIndex, 0);
316 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
317 	err = usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
318 				    USBD_SHORT_XFER_OK, &actlen);
319 	if (err)
320 		return (err);
321 
322 #ifdef UVISOR_DEBUG
323 	{
324 		int i, np;
325 		char *string;
326 
327 		np = UGETW(coninfo.num_ports);
328 		printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_dev), np);
329 		for (i = 0; i < np; ++i) {
330 			switch (coninfo.connections[i].port_function_id) {
331 			case UVISOR_FUNCTION_GENERIC:
332 				string = "Generic";
333 				break;
334 			case UVISOR_FUNCTION_DEBUGGER:
335 				string = "Debugger";
336 				break;
337 			case UVISOR_FUNCTION_HOTSYNC:
338 				string = "HotSync";
339 				break;
340 			case UVISOR_FUNCTION_REMOTE_FILE_SYS:
341 				string = "Remote File System";
342 				break;
343 			default:
344 				string = "unknown";
345 				break;
346 			}
347 			printf("%s: port %d, is for %s\n",
348 			    USBDEVNAME(sc->sc_dev), coninfo.connections[i].port,
349 			    string);
350 		}
351 	}
352 #endif
353 
354 	DPRINTF(("uvisor_init: getting available bytes\n"));
355 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
356 	req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
357 	USETW(req.wValue, 0);
358 	USETW(req.wIndex, 5);
359 	USETW(req.wLength, sizeof avail);
360 	err = usbd_do_request(sc->sc_udev, &req, &avail);
361 	if (err)
362 		return (err);
363 	DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
364 
365 	DPRINTF(("uvisor_init: done\n"));
366 	return (err);
367 }
368 
369 void
370 uvisor_close(void *addr, int portno)
371 {
372 	struct uvisor_softc *sc = addr;
373 	usb_device_request_t req;
374 	struct uvisor_connection_info coninfo; /* XXX ? */
375 	int actlen;
376 
377 	if (sc->sc_dying)
378 		return;
379 
380 	req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
381 	req.bRequest = UVISOR_CLOSE_NOTIFICATION;
382 	USETW(req.wValue, 0);
383 	USETW(req.wIndex, 0);
384 	USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
385 	(void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
386 				    USBD_SHORT_XFER_OK, &actlen);
387 }
388