uvisor.c revision 1.9 1 /* $NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 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 (at) 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.opkthdrlen = 0;
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 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
257 USBDEV(sc->sc_dev));
258
259 DPRINTF(("uvisor: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
260 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
261
262 USB_ATTACH_SUCCESS_RETURN;
263
264 bad:
265 DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
266 sc->sc_dying = 1;
267 USB_ATTACH_ERROR_RETURN;
268 }
269
270 int
271 uvisor_activate(device_ptr_t self, enum devact act)
272 {
273 struct uvisor_softc *sc = (struct uvisor_softc *)self;
274 int rv = 0;
275
276 switch (act) {
277 case DVACT_ACTIVATE:
278 return (EOPNOTSUPP);
279 break;
280
281 case DVACT_DEACTIVATE:
282 if (sc->sc_subdev != NULL)
283 rv = config_deactivate(sc->sc_subdev);
284 sc->sc_dying = 1;
285 break;
286 }
287 return (rv);
288 }
289
290 int
291 uvisor_detach(device_ptr_t self, int flags)
292 {
293 struct uvisor_softc *sc = (struct uvisor_softc *)self;
294 int rv = 0;
295
296 DPRINTF(("uvisor_detach: sc=%p flags=%d\n", sc, flags));
297 sc->sc_dying = 1;
298 if (sc->sc_subdev != NULL) {
299 rv = config_detach(sc->sc_subdev, flags);
300 sc->sc_subdev = NULL;
301 }
302
303 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
304 USBDEV(sc->sc_dev));
305
306
307 return (0);
308 }
309
310 usbd_status
311 uvisor_init(struct uvisor_softc *sc)
312 {
313 usbd_status err;
314 usb_device_request_t req;
315 struct uvisor_connection_info coninfo;
316 int actlen;
317 uWord avail;
318
319 DPRINTF(("uvisor_init: getting connection info\n"));
320 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
321 req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
322 USETW(req.wValue, 0);
323 USETW(req.wIndex, 0);
324 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
325 err = usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
326 USBD_SHORT_XFER_OK, &actlen);
327 if (err)
328 return (err);
329
330 #ifdef UVISOR_DEBUG
331 {
332 int i, np;
333 char *string;
334
335 np = UGETW(coninfo.num_ports);
336 printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_dev), np);
337 for (i = 0; i < np; ++i) {
338 switch (coninfo.connections[i].port_function_id) {
339 case UVISOR_FUNCTION_GENERIC:
340 string = "Generic";
341 break;
342 case UVISOR_FUNCTION_DEBUGGER:
343 string = "Debugger";
344 break;
345 case UVISOR_FUNCTION_HOTSYNC:
346 string = "HotSync";
347 break;
348 case UVISOR_FUNCTION_REMOTE_FILE_SYS:
349 string = "Remote File System";
350 break;
351 default:
352 string = "unknown";
353 break;
354 }
355 printf("%s: port %d, is for %s\n",
356 USBDEVNAME(sc->sc_dev), coninfo.connections[i].port,
357 string);
358 }
359 }
360 #endif
361
362 DPRINTF(("uvisor_init: getting available bytes\n"));
363 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
364 req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
365 USETW(req.wValue, 0);
366 USETW(req.wIndex, 5);
367 USETW(req.wLength, sizeof avail);
368 err = usbd_do_request(sc->sc_udev, &req, &avail);
369 if (err)
370 return (err);
371 DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
372
373 DPRINTF(("uvisor_init: done\n"));
374 return (err);
375 }
376
377 void
378 uvisor_close(void *addr, int portno)
379 {
380 struct uvisor_softc *sc = addr;
381 usb_device_request_t req;
382 struct uvisor_connection_info coninfo; /* XXX ? */
383 int actlen;
384
385 if (sc->sc_dying)
386 return;
387
388 req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
389 req.bRequest = UVISOR_CLOSE_NOTIFICATION;
390 USETW(req.wValue, 0);
391 USETW(req.wIndex, 0);
392 USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
393 (void)usbd_do_request_flags(sc->sc_udev, &req, &coninfo,
394 USBD_SHORT_XFER_OK, &actlen);
395 }
396