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