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