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