42d1874ab8b379adbcdba8d01313730edd218ba4
[dragonfly.git] / sys / dev / usbmisc / uvisor / uvisor.c
1 /*      $NetBSD: uvisor.c,v 1.9 2001/01/23 14:04:14 augustss Exp $      */
2 /*      $FreeBSD: src/sys/dev/usb/uvisor.c,v 1.7.2.3 2002/08/27 13:46:28 joe Exp $      */
3 /*      $DragonFly: src/sys/dev/usbmisc/uvisor/uvisor.c,v 1.3 2003/08/07 21:17:15 dillon Exp $  */
4
5 /* This version of uvisor is heavily based upon the version in NetBSD
6  * but is missing the following patches:
7  *
8  * 1.10 needed?         connect a ucom to each of the uvisor ports
9  * 1.11 needed          ucom has an "info" attach message - use it
10  * 1.12 not needed      rcsids
11  * 1.13 already merged  extra arg to usbd_do_request_flags
12  * 1.14 already merged  sony and palm support
13  * 1.15 already merged  sony clie
14  * 1.16 already merged  trailing whites
15  */
16
17 /*
18  * Copyright (c) 2000 The NetBSD Foundation, Inc.
19  * All rights reserved.
20  *
21  * This code is derived from software contributed to The NetBSD Foundation
22  * by Lennart Augustsson (lennart@augustsson.net) at
23  * Carlstedt Research & Technology.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. All advertising materials mentioning features or use of this software
34  *    must display the following acknowledgement:
35  *        This product includes software developed by the NetBSD
36  *        Foundation, Inc. and its contributors.
37  * 4. Neither the name of The NetBSD Foundation nor the names of its
38  *    contributors may be used to endorse or promote products derived
39  *    from this software without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
42  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
43  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
45  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51  * POSSIBILITY OF SUCH DAMAGE.
52  */
53
54 /*
55  * Handspring Visor (Palmpilot compatible PDA) driver
56  */
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #if defined(__NetBSD__) || defined(__OpenBSD__)
62 #include <sys/device.h>
63 #elif defined(__FreeBSD__)
64 #include <sys/bus.h>
65 #endif
66 #include <sys/conf.h>
67 #include <sys/tty.h>
68 #include <sys/sysctl.h>
69
70 #include <bus/usb/usb.h>
71 #include <bus/usb/usbhid.h>
72
73 #include <bus/usb/usbdi.h>
74 #include <bus/usb/usbdi_util.h>
75 #include <bus/usb/usbdevs.h>
76
77 #include "../ucom/ucomvar.h"
78
79 #ifdef USB_DEBUG
80 #define DPRINTF(x)      if (uvisordebug) printf x
81 #define DPRINTFN(n,x)   if (uvisordebug>(n)) printf x
82 int uvisordebug = 0;
83 SYSCTL_NODE(_hw_usb, OID_AUTO, uvisor, CTLFLAG_RW, 0, "USB uvisor");
84 SYSCTL_INT(_hw_usb_uvisor, OID_AUTO, debug, CTLFLAG_RW,
85            &uvisordebug, 0, "uvisor debug level");
86 #else
87 #define DPRINTF(x)
88 #define DPRINTFN(n,x)
89 #endif
90
91 #define UVISOR_CONFIG_INDEX     0
92 #define UVISOR_IFACE_INDEX      0
93 #define UVISOR_MODVER           1
94
95 /* From the Linux driver */
96 /*
97  * UVISOR_REQUEST_BYTES_AVAILABLE asks the visor for the number of bytes that
98  * are available to be transfered to the host for the specified endpoint.
99  * Currently this is not used, and always returns 0x0001
100  */
101 #define UVISOR_REQUEST_BYTES_AVAILABLE          0x01
102
103 /*
104  * UVISOR_CLOSE_NOTIFICATION is set to the device to notify it that the host
105  * is now closing the pipe. An empty packet is sent in response.
106  */
107 #define UVISOR_CLOSE_NOTIFICATION               0x02
108
109 /*
110  * UVISOR_GET_CONNECTION_INFORMATION is sent by the host during enumeration to
111  * get the endpoints used by the connection.
112  */
113 #define UVISOR_GET_CONNECTION_INFORMATION       0x03
114
115
116 /*
117  * UVISOR_GET_CONNECTION_INFORMATION returns data in the following format
118  */
119 #define UVISOR_MAX_CONN 8
120 struct uvisor_connection_info {
121         uWord   num_ports;
122         struct {
123                 uByte   port_function_id;
124                 uByte   port;
125         } connections[UVISOR_MAX_CONN];
126 };
127 #define UVISOR_CONNECTION_INFO_SIZE 18
128
129 /* struct uvisor_connection_info.connection[x].port defines: */
130 #define UVISOR_ENDPOINT_1               0x01
131 #define UVISOR_ENDPOINT_2               0x02
132
133 /* struct uvisor_connection_info.connection[x].port_function_id defines: */
134 #define UVISOR_FUNCTION_GENERIC         0x00
135 #define UVISOR_FUNCTION_DEBUGGER        0x01
136 #define UVISOR_FUNCTION_HOTSYNC         0x02
137 #define UVISOR_FUNCTION_CONSOLE         0x03
138 #define UVISOR_FUNCTION_REMOTE_FILE_SYS 0x04
139
140 /*
141  * Unknown PalmOS stuff.
142  */
143 #define UVISOR_GET_PALM_INFORMATION             0x04
144 #define UVISOR_GET_PALM_INFORMATION_LEN         0x14
145
146
147 #define UVISORIBUFSIZE 1024
148 #define UVISOROBUFSIZE 1024
149
150 struct uvisor_softc {
151         struct ucom_softc       sc_ucom;
152         u_int16_t               sc_flags;
153 };
154
155 Static usbd_status uvisor_init(struct uvisor_softc *);
156
157 Static void uvisor_close(void *, int);
158
159 struct ucom_callback uvisor_callback = {
160         NULL,
161         NULL,
162         NULL,
163         NULL,
164         NULL,
165         uvisor_close,
166         NULL,
167         NULL,
168 };
169
170 Static device_probe_t uvisor_match;
171 Static device_attach_t uvisor_attach;
172 Static device_detach_t uvisor_detach;
173 Static device_method_t uvisor_methods[] = {
174        /* Device interface */
175        DEVMETHOD(device_probe, uvisor_match),
176        DEVMETHOD(device_attach, uvisor_attach),
177        DEVMETHOD(device_detach, uvisor_detach),
178        { 0, 0 }
179  };
180
181
182 Static driver_t uvisor_driver = {
183        "ucom",
184        uvisor_methods,
185        sizeof (struct uvisor_softc)
186 };
187
188 DRIVER_MODULE(uvisor, uhub, uvisor_driver, ucom_devclass, usbd_driver_load, 0);
189 MODULE_DEPEND(uvisor, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
190 MODULE_VERSION(uvisor, UVISOR_MODVER);
191
192 struct uvisor_type {
193         struct usb_devno        uv_dev;
194         u_int16_t               uv_flags;
195 #define PALM4   0x0001
196 };
197 static const struct uvisor_type uvisor_devs[] = {
198         {{ USB_VENDOR_HANDSPRING, USB_PRODUCT_HANDSPRING_VISOR }, 0 },
199         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M500 }, PALM4 },
200         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M505 }, PALM4 },
201         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M515 }, PALM4 },
202         {{ USB_VENDOR_PALM, USB_PRODUCT_PALM_M125 }, PALM4 },
203         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_40 }, PALM4 },
204         {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_41 }, 0 },
205 /*      {{ USB_VENDOR_SONY, USB_PRODUCT_SONY_CLIE_25 }, PALM4 },*/
206 };
207 #define uvisor_lookup(v, p) ((const struct uvisor_type *)usb_lookup(uvisor_devs, v, p))
208
209
210 USB_MATCH(uvisor)
211 {
212         USB_MATCH_START(uvisor, uaa);
213         
214         if (uaa->iface != NULL)
215                 return (UMATCH_NONE);
216
217         DPRINTFN(20,("uvisor: vendor=0x%x, product=0x%x\n",
218                      uaa->vendor, uaa->product));
219
220         return (uvisor_lookup(uaa->vendor, uaa->product) != NULL ?
221                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
222 }
223
224 USB_ATTACH(uvisor)
225 {
226         USB_ATTACH_START(uvisor, sc, uaa);
227         usbd_device_handle dev = uaa->device;
228         usbd_interface_handle iface;
229         usb_interface_descriptor_t *id;
230         usb_endpoint_descriptor_t *ed;
231         char *devinfo;
232         const char *devname;
233         int i;
234         usbd_status err;
235         struct ucom_softc *ucom;
236
237         devinfo = malloc(1024, M_USBDEV, M_WAITOK);
238         ucom = &sc->sc_ucom;
239
240         bzero(sc, sizeof (struct uvisor_softc));
241         usbd_devinfo(dev, 0, devinfo);
242
243         ucom->sc_dev = self;
244         device_set_desc_copy(self, devinfo);
245
246         ucom->sc_udev = dev;
247         ucom->sc_iface = uaa->iface;
248
249         devname = USBDEVNAME(ucom->sc_dev);
250         printf("%s: %s\n", devname, devinfo);
251
252         DPRINTFN(10,("\nuvisor_attach: sc=%p\n", sc));
253
254         /* Move the device into the configured state. */
255         err = usbd_set_config_index(dev, UVISOR_CONFIG_INDEX, 1);
256         if (err) {
257                 printf("\n%s: failed to set configuration, err=%s\n",
258                        devname, usbd_errstr(err));
259                 goto bad;
260         }
261
262         err = usbd_device2interface_handle(dev, UVISOR_IFACE_INDEX, &iface);
263         if (err) {
264                 printf("\n%s: failed to get interface, err=%s\n",
265                        devname, usbd_errstr(err));
266                 goto bad;
267         }
268
269         printf("%s: %s\n", devname, devinfo);
270
271         sc->sc_flags = uvisor_lookup(uaa->vendor, uaa->product)->uv_flags;
272
273         id = usbd_get_interface_descriptor(iface);
274
275         ucom->sc_udev = dev;
276         ucom->sc_iface = iface;
277
278         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
279         for (i = 0; i < id->bNumEndpoints; i++) {
280                 int addr, dir, attr;
281                 ed = usbd_interface2endpoint_descriptor(iface, i);
282                 if (ed == NULL) {
283                         printf("%s: could not read endpoint descriptor"
284                                ": %s\n", devname, usbd_errstr(err));
285                         goto bad;
286                 }
287                 
288                 addr = ed->bEndpointAddress;
289                 dir = UE_GET_DIR(ed->bEndpointAddress);
290                 attr = ed->bmAttributes & UE_XFERTYPE;
291                 if (dir == UE_DIR_IN && attr == UE_BULK)
292                         ucom->sc_bulkin_no = addr;
293                 else if (dir == UE_DIR_OUT && attr == UE_BULK)
294                         ucom->sc_bulkout_no = addr;
295                 else {
296                         printf("%s: unexpected endpoint\n", devname);
297                         goto bad;
298                 }
299         }
300         if (ucom->sc_bulkin_no == -1) {
301                 printf("%s: Could not find data bulk in\n",
302                        USBDEVNAME(ucom->sc_dev));
303                 goto bad;
304         }
305         if (ucom->sc_bulkout_no == -1) {
306                 printf("%s: Could not find data bulk out\n",
307                        USBDEVNAME(ucom->sc_dev));
308                 goto bad;
309         }
310         
311         ucom->sc_parent = sc;
312         ucom->sc_portno = UCOM_UNK_PORTNO;
313         /* bulkin, bulkout set above */
314         ucom->sc_ibufsize = UVISORIBUFSIZE;
315         ucom->sc_obufsize = UVISOROBUFSIZE;
316         ucom->sc_ibufsizepad = UVISORIBUFSIZE;
317         ucom->sc_opkthdrlen = 0;
318         ucom->sc_callback = &uvisor_callback;
319
320         err = uvisor_init(sc);
321         if (err) {
322                 printf("%s: init failed, %s\n", USBDEVNAME(ucom->sc_dev),
323                        usbd_errstr(err));
324                 goto bad;
325         }
326
327         DPRINTF(("uvisor: in=0x%x out=0x%x\n", ucom->sc_bulkin_no, ucom->sc_bulkout_no));
328         ucom_attach(&sc->sc_ucom);
329
330         USB_ATTACH_SUCCESS_RETURN;
331
332 bad:
333         DPRINTF(("uvisor_attach: ATTACH ERROR\n"));
334         ucom->sc_dying = 1;
335         USB_ATTACH_ERROR_RETURN;
336 }
337
338 #if 0
339
340 int
341 uvisor_activate(device_ptr_t self, enum devact act)
342 {
343         struct uvisor_softc *sc = (struct uvisor_softc *)self;
344         int rv = 0;
345
346         switch (act) {
347         case DVACT_ACTIVATE:
348                 return (EOPNOTSUPP);
349                 break;
350
351         case DVACT_DEACTIVATE:
352                 if (sc->sc_subdev != NULL)
353                         rv = config_deactivate(sc->sc_subdev);
354                 sc->sc_dying = 1;
355                 break;
356         }
357         return (rv);
358 }
359
360 #endif
361
362 USB_DETACH(uvisor)
363 {
364         USB_DETACH_START(uvisor, sc);
365         int rv = 0;
366
367         DPRINTF(("uvisor_detach: sc=%p\n", sc));
368         sc->sc_ucom.sc_dying = 1;
369         rv = ucom_detach(&sc->sc_ucom);
370
371         return (rv);
372 }
373
374 usbd_status
375 uvisor_init(struct uvisor_softc *sc)
376 {
377         usbd_status err;
378         usb_device_request_t req;
379         struct uvisor_connection_info coninfo;
380         int actlen;
381         uWord avail;
382         char buffer[256];
383
384         DPRINTF(("uvisor_init: getting connection info\n"));
385         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
386         req.bRequest = UVISOR_GET_CONNECTION_INFORMATION;
387         USETW(req.wValue, 0);
388         USETW(req.wIndex, 0);
389         USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
390         err = usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
391                                     USBD_SHORT_XFER_OK, &actlen);
392         if (err)
393                 return (err);
394
395 #ifdef USB_DEBUG
396         {
397                 int i, np;
398                 char *string;
399
400                 np = UGETW(coninfo.num_ports);
401                 printf("%s: Number of ports: %d\n", USBDEVNAME(sc->sc_ucom.sc_dev), np);
402                 for (i = 0; i < np; ++i) {
403                         switch (coninfo.connections[i].port_function_id) {
404                         case UVISOR_FUNCTION_GENERIC:
405                                 string = "Generic";
406                                 break;
407                         case UVISOR_FUNCTION_DEBUGGER:
408                                 string = "Debugger";
409                                 break;
410                         case UVISOR_FUNCTION_HOTSYNC:
411                                 string = "HotSync";
412                                 break;
413                         case UVISOR_FUNCTION_REMOTE_FILE_SYS:
414                                 string = "Remote File System";
415                                 break;
416                         default:
417                                 string = "unknown";
418                                 break;  
419                         }
420                         printf("%s: port %d, is for %s\n",
421                             USBDEVNAME(sc->sc_ucom.sc_dev), coninfo.connections[i].port,
422                             string);
423                 }
424         }
425 #endif
426
427         if (sc->sc_flags & PALM4) {
428                 /* Palm OS 4.0 Hack */
429                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
430                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
431                 USETW(req.wValue, 0);
432                 USETW(req.wIndex, 0);
433                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
434                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
435                 if (err)
436                         return (err);
437                 req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
438                 req.bRequest = UVISOR_GET_PALM_INFORMATION;
439                 USETW(req.wValue, 0);
440                 USETW(req.wIndex, 0);
441                 USETW(req.wLength, UVISOR_GET_PALM_INFORMATION_LEN);
442                 err = usbd_do_request(sc->sc_ucom.sc_udev, &req, buffer);
443                 if (err)
444                         return (err);
445         }
446
447         DPRINTF(("uvisor_init: getting available bytes\n"));
448         req.bmRequestType = UT_READ_VENDOR_ENDPOINT;
449         req.bRequest = UVISOR_REQUEST_BYTES_AVAILABLE;
450         USETW(req.wValue, 0);
451         USETW(req.wIndex, 5);
452         USETW(req.wLength, sizeof avail);
453         err = usbd_do_request(sc->sc_ucom.sc_udev, &req, &avail);
454         if (err)
455                 return (err);
456         DPRINTF(("uvisor_init: avail=%d\n", UGETW(avail)));
457
458         DPRINTF(("uvisor_init: done\n"));
459         return (err);
460 }
461
462 void
463 uvisor_close(void *addr, int portno)
464 {
465         struct uvisor_softc *sc = addr;
466         usb_device_request_t req;
467         struct uvisor_connection_info coninfo; /* XXX ? */
468         int actlen;
469
470         if (sc->sc_ucom.sc_dying)
471                 return;
472
473         req.bmRequestType = UT_READ_VENDOR_ENDPOINT; /* XXX read? */
474         req.bRequest = UVISOR_CLOSE_NOTIFICATION;
475         USETW(req.wValue, 0);
476         USETW(req.wIndex, 0);
477         USETW(req.wLength, UVISOR_CONNECTION_INFO_SIZE);
478         (void)usbd_do_request_flags(sc->sc_ucom.sc_udev, &req, &coninfo,
479                                     USBD_SHORT_XFER_OK, &actlen);
480 }