uvisor.c revision 1.3 1 /* $NetBSD: uvisor.c,v 1.3 2000/04/05 11:12:48 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 (augustss (at) carlstedt.se) 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 struct uvisor_softc {
119 USBBASEDEVICE sc_dev; /* base device */
120 usbd_device_handle sc_udev; /* device */
121 usbd_interface_handle sc_iface; /* interface */
122
123 device_ptr_t sc_subdev;
124
125 u_char sc_dying;
126 };
127
128 Static usbd_status uvisor_init __P((struct uvisor_softc *));
129
130 Static void uvisor_close __P((void *, int));
131
132
133 struct ucom_methods uvisor_methods = {
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 NULL,
139 uvisor_close,
140 };
141
142 USB_DECLARE_DRIVER(uvisor);
143
144 USB_MATCH(uvisor)
145 {
146 USB_MATCH_START(uvisor, uaa);
147
148 if (uaa->iface != NULL)
149 return (UMATCH_NONE);
150
151 DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
152 uaa->vendor, uaa->product));
153
154 if (uaa->vendor == USB_VENDOR_HANDSPRING &&
155 uaa->product == USB_PRODUCT_HANDSPRING_VISOR)
156 return (UMATCH_VENDOR_PRODUCT);
157
158 return (UMATCH_NONE);
159 }
160
161 USB_ATTACH(uvisor)
162 {
163 USB_ATTACH_START(uvisor, sc, uaa);
164 usbd_device_handle dev = uaa->device;
165 usbd_interface_handle iface;
166 usb_interface_descriptor_t *id;
167 usb_endpoint_descriptor_t *ed;
168 char devinfo[1024];
169 char *devname = USBDEVNAME(sc->sc_dev);
170 int i;
171 usbd_status err;
172 struct ucom_attach_args uca;
173
174 DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
175
176 /* Move the device into the configured state. */
177 err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
178 if (err) {
179 printf("\n%s: failed to set configuration, err=%s\n",
180 devname, usbd_errstr(err));
181 goto bad;
182 }
183
184 err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
185 if (err) {
186 printf("\n%s: failed to get interface, err=%s\n",
187 devname, usbd_errstr(err));
188 goto bad;
189 }
190
191 usbd_devinfo(dev, 0, devinfo);
192 USB_ATTACH_SETUP;
193 printf("%s: %s\n", devname, devinfo);
194
195 id = usbd_get_interface_descriptor(iface);
196
197 sc->sc_udev = dev;
198 sc->sc_iface = iface;
199
200 uca.device = dev;
201 uca.iface = iface;
202 uca.methods = &uvisor_methods;
203 uca.arg = sc;
204 uca.portno = UCOM_UNK_PORTNO;
205
206 uca.bulkin = uca.bulkout = -1;
207 for (i = 0; i < id->bNumEndpoints; i++) {
208 int addr, dir, attr;
209 ed = usbd_interface2endpoint_descriptor(iface, i);
210 if (ed == NULL) {
211 printf("%s: could not read endpoint descriptor"
212 ": %s\n", devname, usbd_errstr(err));
213 goto bad;
214 }
215
216 addr = ed->bEndpointAddress;
217 dir = UE_GET_DIR(ed->bEndpointAddress);
218 attr = ed->bmAttributes & UE_XFERTYPE;
219 if (dir == UE_DIR_IN && attr == UE_BULK)
220 uca.bulkin = addr;
221 else if (dir == UE_DIR_OUT && attr == UE_BULK)
222 uca.bulkout = addr;
223 else {
224 printf("%s: unexpected endpoint\n", devname);
225 goto bad;
226 }
227 }
228 if (uca.bulkin == -1) {
229 printf("%s: Could not find data bulk in\n",
230 USBDEVNAME(sc->sc_dev));
231 goto bad;
232 }
233 if (uca.bulkout == -1) {
234 printf("%s: Could not find data bulk out\n",
235 USBDEVNAME(sc->sc_dev));
236 goto bad;
237 }
238
239 err = uvisor_init(sc);
240 if (err) {
241 printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
242 usbd_errstr(err));
243 goto bad;
244 }
245
246 DPRINTF(("uvisor: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
247 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
248
249 USB_ATTACH_SUCCESS_RETURN;
250
251 bad:
252 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
253 sc->sc_dying = 1;
254 USB_ATTACH_ERROR_RETURN;
255 }
256
257 int
258 uvisor_activate(self, act)
259 device_ptr_t self;
260 enum devact act;
261 {
262 struct uvisor_softc *sc = (struct uvisor_softc *)self;
263 int rv = 0;
264
265 switch (act) {
266 case DVACT_ACTIVATE:
267 return (EOPNOTSUPP);
268 break;
269
270 case DVACT_DEACTIVATE:
271 if (sc->sc_subdev != NULL)
272 rv = config_deactivate(sc->sc_subdev);
273 sc->sc_dying = 1;
274 break;
275 }
276 return (rv);
277 }
278
279 int
280 uvisor_detach(self, flags)
281 device_ptr_t self;
282 int flags;
283 {
284 struct uvisor_softc *sc = (struct uvisor_softc *)self;
285 int rv = 0;
286
287 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
288 sc->sc_dying = 1;
289 if (sc->sc_subdev != NULL) {
290 rv = config_detach(sc->sc_subdev, flags);
291 sc->sc_subdev = NULL;
292 }
293 return (0);
294 }
295
296 usbd_status
297 uvisor_init(sc)
298 struct uvisor_softc *sc;
299 {
300 usbd_status err;
301 usb_device_request_t req;
302 struct uvisor_connection_info coninfo;
303 int actlen;
304 uWord avail;
305
306 DPRINTF(("uvisor_init: getting connection info\n"));
307 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
308 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
309 USETW(req.wValue, 0);
310 USETW(req.wIndex, 0);
311 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
312 err = usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
313 USBD_SHORT_XFER_OK, &actlen);
314 if (err)
315 return (err);
316
317 #ifdef UVISOR_DEBUG
318 {
319 int i, np;
320 char *string;
321
322 np = UGETW(coninfo.num_ports);
323 printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_dev), np);
324 for (i = 0; i < np; ++i) {
325 switch (coninfo.connections[i].port_function_id) {
326 case UVISOR_FUNCTION_GENERIC:
327 string = "Generic";
328 break;
329 case UVISOR_FUNCTION_DEBUGGER:
330 string = "Debugger";
331 break;
332 case UVISOR_FUNCTION_HOTSYNC:
333 string = "HotSync";
334 break;
335 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
336 string = "Remote File System";
337 break;
338 default:
339 string = "unknown";
340 break;
341 }
342 printf("%s: port %d, is for %s\n",
343 USBDEVNAME(sc->sc_dev), coninfo.connections[i].port,
344 string);
345 }
346 }
347 #endif
348
349 DPRINTF(("uvisor_init: getting available bytes\n"));
350 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
351 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
352 USETW(req.wValue, 0);
353 USETW(req.wIndex, 5);
354 USETW(req.wLength, sizeof avail);
355 err = usbd_do_request(sc->sc_udev, &req, &avail);
356 if (err)
357 return (err);
358 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
359
360 DPRINTF(("uvisor_init: done\n"));
361 return (err);
362 }
363
364 void
365 uvisor_close(addr, portno)
366 void *addr;
367 int portno;
368 {
369 struct uvisor_softc *sc = addr;
370 usb_device_request_t req;
371 struct uvisor_connection_info coninfo; /* XXX ? */
372 int actlen;
373
374 if (sc->sc_dying)
375 return;
376
377 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
378 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
379 USETW(req.wValue, 0);
380 USETW(req.wIndex, 0);
381 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
382 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
383 USBD_SHORT_XFER_OK, &actlen);
384 }
385