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