]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/uhub.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / dev / usb / uhub.c
1 /*      $NetBSD: uhub.c,v 1.34 1999/11/18 23:32:29 augustss Exp $       */
2 /*      $FreeBSD$       */
3
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (augustss@carlstedt.se) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 /*
42  * USB spec: http://www.usb.org/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #if defined(__NetBSD__) || defined(__OpenBSD__)
50 #include <sys/device.h>
51 #include <sys/proc.h>
52 #elif defined(__FreeBSD__)
53 #include <sys/module.h>
54 #include <sys/bus.h>
55 #include "bus_if.h"
56 #endif
57
58 #include <machine/bus.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdivar.h>
64
65 #ifdef UHUB_DEBUG
66 #define DPRINTF(x)      if (uhubdebug) logprintf x
67 #define DPRINTFN(n,x)   if (uhubdebug>(n)) logprintf x
68 int     uhubdebug;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73
74 struct uhub_softc {
75         USBBASEDEVICE           sc_dev;         /* base device */
76         usbd_device_handle      sc_hub;         /* USB device */
77         usbd_pipe_handle        sc_ipipe;       /* interrupt pipe */
78         u_int8_t                sc_status[1];   /* XXX more ports */
79         u_char                  sc_running;
80 };
81
82 static usbd_status uhub_init_port __P((struct usbd_port *));
83 static usbd_status uhub_explore __P((usbd_device_handle hub));
84 static void uhub_intr __P((usbd_xfer_handle, usbd_private_handle,usbd_status));
85
86 #if defined(__FreeBSD__)
87 static bus_child_detached_t uhub_child_detached;
88 #endif
89
90
91 /* 
92  * We need two attachment points:
93  * hub to usb and hub to hub
94  * Every other driver only connects to hubs
95  */
96
97 #if defined(__NetBSD__) || defined(__OpenBSD__)
98 USB_DECLARE_DRIVER(uhub);
99
100 /* Create the driver instance for the hub connected to hub case */
101 struct cfattach uhub_uhub_ca = {
102         sizeof(struct uhub_softc), uhub_match, uhub_attach,
103         uhub_detach, uhub_activate
104 };
105 #elif defined(__FreeBSD__)
106 USB_DECLARE_DRIVER_INIT(uhub,
107                         DEVMETHOD(bus_child_detached, uhub_child_detached),
108                         DEVMETHOD(device_suspend, bus_generic_suspend),
109                         DEVMETHOD(device_resume, bus_generic_resume),
110                         DEVMETHOD(device_shutdown, bus_generic_shutdown)
111                         );
112                         
113 /* Create the driver instance for the hub connected to usb case. */
114 devclass_t uhubroot_devclass;
115
116 static device_method_t uhubroot_methods[] = {
117         DEVMETHOD(device_probe, uhub_match),
118         DEVMETHOD(device_attach, uhub_attach),
119         /* detach is not allowed for a root hub */
120         DEVMETHOD(device_suspend, bus_generic_suspend),
121         DEVMETHOD(device_resume, bus_generic_suspend),
122         DEVMETHOD(device_shutdown, bus_generic_shutdown),
123         {0,0}
124 };
125
126 static  driver_t uhubroot_driver = {
127         "uhub",
128         uhubroot_methods,
129         sizeof(struct uhub_softc)
130 };
131 #endif
132
133 USB_MATCH(uhub)
134 {
135         USB_MATCH_START(uhub, uaa);
136         usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
137         
138         DPRINTFN(5,("uhub_match, dd=%p\n", dd));
139         /* 
140          * The subclass for hubs seems to be 0 for some and 1 for others,
141          * so we just ignore the subclass.
142          */
143         if (uaa->iface == NULL && dd->bDeviceClass == UCLASS_HUB)
144                 return (UMATCH_DEVCLASS_DEVSUBCLASS);
145         return (UMATCH_NONE);
146 }
147
148 USB_ATTACH(uhub)
149 {
150         USB_ATTACH_START(uhub, sc, uaa);
151         usbd_device_handle dev = uaa->device;
152         char devinfo[1024];
153         usbd_status err;
154         struct usbd_hub *hub;
155         usb_device_request_t req;
156         usb_hub_descriptor_t hubdesc;
157         int p, port, nports, nremov;
158         usbd_interface_handle iface;
159         usb_endpoint_descriptor_t *ed;
160         
161         DPRINTFN(1,("uhub_attach\n"));
162         sc->sc_hub = dev;
163         usbd_devinfo(dev, 1, devinfo);
164         USB_ATTACH_SETUP;
165         printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
166
167         err = usbd_set_config_index(dev, 0, 1);
168         if (err) {
169                 DPRINTF(("%s: configuration failed, error=%s\n",
170                          USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
171                 USB_ATTACH_ERROR_RETURN;
172         }
173
174         if (dev->depth > USB_HUB_MAX_DEPTH) {
175                 printf("%s: hub depth (%d) exceeded, hub ignored\n",
176                        USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
177                 USB_ATTACH_ERROR_RETURN;
178         }
179
180         /* Get hub descriptor. */
181         req.bmRequestType = UT_READ_CLASS_DEVICE;
182         req.bRequest = UR_GET_DESCRIPTOR;
183         USETW(req.wValue, 0);
184         USETW(req.wIndex, 0);
185         USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
186         DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
187         err = usbd_do_request(dev, &req, &hubdesc);
188         nports = hubdesc.bNbrPorts;
189         if (!err && nports > 7) {
190                 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
191                 err = usbd_do_request(dev, &req, &hubdesc);
192         }
193         if (err) {
194                 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
195                          USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
196                 USB_ATTACH_ERROR_RETURN;
197         }
198
199         for (nremov = 0, port = 1; port <= nports; port++)
200                 if (!UHD_NOT_REMOV(&hubdesc, port))
201                         nremov++;
202         printf("%s: %d port%s with %d removable, %s powered\n",
203                USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
204                nremov, dev->self_powered ? "self" : "bus");
205
206         hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
207                      M_USBDEV, M_NOWAIT);
208         if (hub == NULL)
209                 USB_ATTACH_ERROR_RETURN;
210         dev->hub = hub;
211         dev->hub->hubsoftc = sc;
212         hub->explore = uhub_explore;
213         hub->hubdesc = hubdesc;
214         
215         DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
216                     "parent->selfpowered=%d\n",
217                  dev->self_powered, dev->powersrc->parent,
218                  dev->powersrc->parent ? 
219                  dev->powersrc->parent->self_powered : 0));
220
221         if (!dev->self_powered && dev->powersrc->parent != NULL &&
222             !dev->powersrc->parent->self_powered) {
223                 printf("%s: bus powered hub connected to bus powered hub, "
224                        "ignored\n", USBDEVNAME(sc->sc_dev));
225                 goto bad;
226         }
227
228         /* Set up interrupt pipe. */
229         err = usbd_device2interface_handle(dev, 0, &iface);
230         if (err) {
231                 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
232                 goto bad;
233         }
234         ed = usbd_interface2endpoint_descriptor(iface, 0);
235         if (ed == NULL) {
236                 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
237                 goto bad;
238         }
239         if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
240                 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
241                 goto bad;
242         }
243
244         err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
245                   USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status, 
246                   sizeof(sc->sc_status), uhub_intr, USBD_DEFAULT_INTERVAL);
247         if (err) {
248                 printf("%s: cannot open interrupt pipe\n", 
249                        USBDEVNAME(sc->sc_dev));
250                 goto bad;
251         }
252
253         /* Wait with power off for a while. */
254         usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
255
256         for (p = 0; p < nports; p++) {
257                 struct usbd_port *up = &hub->ports[p];
258                 up->device = 0;
259                 up->parent = dev;
260                 up->portno = p+1;
261                 err = uhub_init_port(up);
262                 if (err)
263                         printf("%s: init of port %d failed\n", 
264                             USBDEVNAME(sc->sc_dev), up->portno);
265         }
266         sc->sc_running = 1;
267
268         USB_ATTACH_SUCCESS_RETURN;
269
270  bad:
271         free(hub, M_USBDEV);
272         dev->hub = 0;
273         USB_ATTACH_ERROR_RETURN;
274 }
275
276 usbd_status
277 uhub_init_port(up)
278         struct usbd_port *up;
279 {
280         int port = up->portno;
281         usbd_device_handle dev = up->parent;
282         usbd_status err;
283         u_int16_t pstatus;
284
285         err = usbd_get_port_status(dev, port, &up->status);
286         if (err)
287                 return (err);
288         pstatus = UGETW(up->status.wPortStatus);
289         DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
290                  "change=0x%04x\n",
291                  port, pstatus, UGETW(up->status.wPortChange)));
292         if ((pstatus & UPS_PORT_POWER) == 0) {
293                 /* Port lacks power, turn it on */
294
295                 /* First let the device go through a good power cycle, */
296                 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME);
297
298 #if 0
299 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
300 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
301 #endif
302
303                 /* then turn the power on. */
304                 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
305                 if (err)
306                         return (err);
307                 DPRINTF(("usb_init_port: turn on port %d power status=0x%04x "
308                          "change=0x%04x\n",
309                          port, UGETW(up->status.wPortStatus),
310                          UGETW(up->status.wPortChange)));
311                 /* Wait for stable power. */
312                 usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood * 
313                                    UHD_PWRON_FACTOR);
314                 /* Get the port status again. */
315                 err = usbd_get_port_status(dev, port, &up->status);
316                 if (err)
317                         return (err);
318                 DPRINTF(("usb_init_port: after power on status=0x%04x "
319                          "change=0x%04x\n",
320                          UGETW(up->status.wPortStatus),
321                          UGETW(up->status.wPortChange)));
322
323 #if 0
324 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
325 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
326 usbd_get_port_status(dev, port, &up->status);
327 #endif
328
329                 pstatus = UGETW(up->status.wPortStatus);
330                 if ((pstatus & UPS_PORT_POWER) == 0)
331                         printf("%s: port %d did not power up\n",
332  USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port);
333
334         }
335         if (dev->self_powered)
336                 /* Self powered hub, give ports maximum current. */
337                 up->power = USB_MAX_POWER;
338         else
339                 up->power = USB_MIN_POWER;
340         return (USBD_NORMAL_COMPLETION);
341 }
342
343 usbd_status
344 uhub_explore(dev)
345         usbd_device_handle dev;
346 {
347         usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
348         struct uhub_softc *sc = dev->hub->hubsoftc;
349         struct usbd_port *up;
350         usbd_status err;
351         int port;
352         int change, status;
353
354         DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
355
356         if (!sc->sc_running)
357                 return (USBD_NOT_STARTED);
358
359         /* Ignore hubs that are too deep. */
360         if (dev->depth > USB_HUB_MAX_DEPTH)
361                 return (USBD_TOO_DEEP);
362
363         for(port = 1; port <= hd->bNbrPorts; port++) {
364                 up = &dev->hub->ports[port-1];
365                 err = usbd_get_port_status(dev, port, &up->status);
366                 if (err) {
367                         DPRINTF(("uhub_explore: get port status failed, "
368                                  "error=%s\n", usbd_errstr(err)));
369                         continue;
370                 }
371                 status = UGETW(up->status.wPortStatus);
372                 change = UGETW(up->status.wPortChange);
373                 DPRINTFN(3,("uhub_explore: port %d status 0x%04x 0x%04x\n",
374                             port, status, change));
375                 if (change & UPS_C_PORT_ENABLED) {
376                         DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
377                         usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
378                         if (status & UPS_PORT_ENABLED) {
379                                 printf("%s: illegal enable change, port %d\n",
380                                        USBDEVNAME(sc->sc_dev), port);
381                         } else {
382                                 /* Port error condition. */
383                                 if (up->restartcnt++ < USBD_RESTART_MAX) {
384                                         printf("%s: port error, restarting "
385                                                "port %d\n",
386                                                USBDEVNAME(sc->sc_dev), port);
387                                         goto disco;
388                                 } else {
389                                         printf("%s: port error, giving up "
390                                                "port %d\n",
391                                                USBDEVNAME(sc->sc_dev), port);
392                                 }
393                         }
394                 }
395                 if (!(change & UPS_C_CONNECT_STATUS)) {
396                         DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
397                                     "STATUS\n", port));
398                         /* No status change, just do recursive explore. */
399                         if (up->device && up->device->hub)
400                                 up->device->hub->explore(up->device);
401                         continue;
402                 }
403                 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
404                          dev->address, port));
405                 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
406                 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
407                 /*
408                  * If there is already a device on the port the change status
409                  * must mean that is has disconnected.  Looking at the
410                  * current connect status is not enough to figure this out
411                  * since a new unit may have been connected before we handle
412                  * the disconnect.
413                  */
414         disco:
415                 if (up->device != NULL) {
416                         /* Disconnected */
417                         DPRINTF(("uhub_explore: device addr=%d disappeared "
418                                  "on port %d\n", up->device->address, port));
419                         usb_disconnect_port(up, USBDEV(sc->sc_dev));
420                         usbd_clear_port_feature(dev, port, 
421                                                 UHF_C_PORT_CONNECTION);
422                 }
423                 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
424                         DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
425                                     "_STATUS\n", port));
426                         continue;
427                 }
428
429                 /* Connected */
430                 up->restartcnt = 0;
431
432                 /* Wait for maximum device power up time. */
433                 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
434
435                 /* Reset port, which implies enabling it. */
436                 if (usbd_reset_port(dev, port, &up->status)) {
437                         DPRINTF(("uhub_explore: port=%d reset failed\n",
438                                  port));
439                         continue;
440                 }
441
442                 /* Get device info and set its address. */
443                 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus, 
444                           dev->depth + 1, status & UPS_LOW_SPEED, 
445                           port, up);
446                 /* XXX retry a few times? */
447                 if (err) {
448                         DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
449                                      "error=%s\n", usbd_errstr(err)));
450                         /* Avoid addressing problems by disabling. */
451                         /* usbd_reset_port(dev, port, &up->status); */
452
453                         /* 
454                          * The unit refused to accept a new address, or had
455                          * some other serious problem.  Since we cannot leave
456                          * at 0 we have to disable the port instead.
457                          */
458                         printf("%s: device problem, disabling port %d\n",
459                                USBDEVNAME(sc->sc_dev), port);
460                         usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
461                         /* Make sure we don't try to restart it infinitely. */
462                         up->restartcnt = USBD_RESTART_MAX;
463                 } else {
464                         if (up->device->hub)
465                                 up->device->hub->explore(up->device);
466                 }
467         }
468         return (USBD_NORMAL_COMPLETION);
469 }
470
471 #if defined(__NetBSD__) || defined(__OpenBSD__)
472 int
473 uhub_activate(self, act)
474         device_ptr_t self;
475         enum devact act;
476 {
477         struct uhub_softc *sc = (struct uhub_softc *)self;
478         usbd_device_handle devhub = sc->sc_hub;
479         usbd_device_handle dev;
480         int nports, port, i;
481
482         switch (act) {
483         case DVACT_ACTIVATE:
484                 return (EOPNOTSUPP);
485                 break;
486
487         case DVACT_DEACTIVATE:
488                 nports = devhub->hub->hubdesc.bNbrPorts;
489                 for(port = 0; port < nports; port++) {
490                         dev = devhub->hub->ports[port].device;
491                         if (dev != NULL) {
492                                 for (i = 0; dev->subdevs[i]; i++)
493                                         config_deactivate(dev->subdevs[i]);
494                         }
495                 }
496                 break;
497         }
498         return (0);
499 }
500 #endif
501
502 /*
503  * Called from process context when the hub is gone.
504  * Detach all devices on active ports.
505  */
506 USB_DETACH(uhub)
507 {
508         USB_DETACH_START(uhub, sc);
509         usbd_device_handle dev = sc->sc_hub;
510         struct usbd_port *rup;
511         int port, nports;
512
513 #if defined(__NetBSD__) || defined(__OpenBSD__)
514         DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
515 #elif defined(__FreeBSD__)
516         DPRINTF(("uhub_detach: sc=%port\n", sc));
517 #endif
518
519         if (dev->hub == NULL)           /* Must be partially working */
520                 return (0);
521
522         usbd_abort_pipe(sc->sc_ipipe);
523         usbd_close_pipe(sc->sc_ipipe);
524
525         nports = dev->hub->hubdesc.bNbrPorts;
526         for(port = 0; port < nports; port++) {
527                 rup = &dev->hub->ports[port];
528                 if (rup->device)
529                         usb_disconnect_port(rup, self);
530         }
531         
532         free(dev->hub, M_USBDEV);
533         dev->hub = NULL;
534
535         return (0);
536 }
537
538 #if defined(__FreeBSD__)
539 /* Called when a device has been detached from it */
540 static void
541 uhub_child_detached(self, child)
542        device_t self;
543        device_t child;
544 {
545        struct uhub_softc *sc = device_get_softc(self);
546        usbd_device_handle devhub = sc->sc_hub;
547        usbd_device_handle dev;
548        int nports;
549        int port;
550        int i;
551
552        if (!devhub->hub)  
553                /* should never happen; children are only created after init */
554                panic("hub not fully initialised, but child deleted?");
555
556        nports = devhub->hub->hubdesc.bNbrPorts;
557        for (port = 0; port < nports; port++) {
558                dev = devhub->hub->ports[port].device;
559                if (dev && dev->subdevs) {
560                        for (i = 0; dev->subdevs[i]; i++) {
561                                if (dev->subdevs[i] == child) {
562                                        dev->subdevs[i] = NULL;
563                                        return;
564                                }
565                        }
566                }
567        }
568 }
569 #endif
570
571
572 /*
573  * Hub interrupt.
574  * This an indication that some port has changed status.
575  * Notify the bus event handler thread that we need
576  * to be explored again.
577  */
578 void
579 uhub_intr(xfer, addr, status)
580         usbd_xfer_handle xfer;
581         usbd_private_handle addr;
582         usbd_status status;
583 {
584         struct uhub_softc *sc = addr;
585
586         DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
587         if (status != USBD_NORMAL_COMPLETION)
588                 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
589
590         usb_needs_explore(sc->sc_hub->bus);
591 }
592
593 #if defined(__FreeBSD__)
594 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
595 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
596 #endif