Example #1
0
static int usbhub_get_hub_descriptor(usbdev_t *dev,usb_hub_descr_t *dscr,int idx,int maxlen)
{
    int res;
    uint8_t *respbuf;
    int len;

    /*
     * Get the hub descriptor.  Get the first 8 bytes first, then get
     * the rest if there is more.
     */

    respbuf = usb_dma_alloc(USB_HUB_DESCR_SIZE);
    res = usb_std_request(dev,0xA0,
			  USB_HUBREQ_GET_DESCRIPTOR,
			  0, 0,
			  respbuf,
			  USB_HUB_DESCR_SIZE);
    len = ((usb_hub_descr_t *)respbuf)->bDescriptorLength;
    if (len > USB_HUB_DESCR_SIZE) {
	usb_dma_free(respbuf);
	respbuf = usb_dma_alloc(USB_HUB_DESCR_SIZE);
	res = usb_std_request(dev,0xA0,
			      USB_HUBREQ_GET_DESCRIPTOR,
			      0, 0,
			      respbuf,
			      len);
	}
    memcpy(dscr, respbuf, (len <= maxlen ? len : maxlen));
    usb_dma_free(respbuf);
    return res;
}
Example #2
0
static int usbhub_detach(usbdev_t *dev)
{
    usbhub_softc_t *hub;
    usbdev_t *deldev;
    int idx;

    if (!IS_HUB(dev)) return 0;		/* should not happen */

    hub = dev->ud_private;
    for (idx = 0; idx < UHUB_MAX_DEVICES; idx++) {
	deldev = hub->uhub_devices[idx];
	if (deldev) {
	    if (usb_noisy > 0)
		console_log("USB: Removing device attached to bus %d hub %d port %d",
			    dev->ud_bus->ub_num,
			    dev->ud_address,idx+1);
	    if (deldev->ud_drv) {
		/* close open pipes, cancel reqs */
		usb_destroy_all_pipes(deldev);
		/* 
		 * Try to process the done queue.  This will complete any
		 * requests that made it out of the pipes while we were
		 * doing the stuff above.
		 */
		usb_poll(deldev->ud_bus);
		/* Call detach method, clean up device softc */
		(*(deldev->ud_drv->udrv_detach))(deldev);
		}
	    else {
		if (usb_noisy > 0) {
		    console_log("USB: Detached device on bus %d hub %d port %d "
			   "has no methods",
			   dev->ud_bus->ub_num,
			   dev->ud_address,idx+1);
		    }
		}
	    if (deldev->ud_cfgdescr) usb_dma_free(deldev->ud_cfgdescr);
	    usb_destroy_device(deldev);
	    }
	}

    if (hub->uhub_imsg != NULL) {
	usb_dma_free(hub->uhub_imsg);
	}
    KFREE(hub);				/* remove softc */

    return 0;
}
Example #3
0
static int rtek_send_eth_frame( void *ctx, hsaddr_t buf, int len )
{
    rtek_softc_t *softc = (rtek_softc_t *) ctx;
    usbreq_t *ur;
    int txlen = len;
    unsigned char *txbuf;

    /* First some Realtek chip workarounds */
    if( txlen < 60 )		/* some strange limitation */
	txlen = 60;
    else if( !(txlen % 64) )	/* to handle module 64 packets */
	++txlen;

    txbuf = usb_dma_alloc(txlen);
    hs_memcpy_from_hs( txbuf, buf, txlen );

#if USBETH_DEBUG
    xprintf( "Outgoing packet :\n" );
    hexdump( txbuf, txlen, 16, txlen / 16 + 1 );
#endif
    ur = usb_make_request(softc->dev, softc->bulk_outpipe,
	                      txbuf, txlen, UR_FLAG_OUT);
    usb_sync_request(ur);
    usb_free_request(ur);
    usb_dma_free(txbuf);

    return( len );
}
Example #4
0
static void usbhub_scan_ports(usbdev_t *dev,void *arg)
{
    uint16_t current;
    uint16_t changed;
    usbhub_softc_t *softc;
    int idx;
    int res;
    int len;
    uint8_t *buf;
    usbdev_t *newdev;
    usb_driver_t *newdrv;
    int addr;
    usb_port_status_t *portstatus;
    usb_config_descr_t cfgdescr;
    unsigned int powerondelay;

    if (!IS_HUB(dev)) return;		/* should not happen.  */

    portstatus = usb_dma_alloc(sizeof(usb_port_status_t));
    if (portstatus == NULL) return;

    /*
     * We know this is a hub.  Get the softc back.
     */

    softc = (usbhub_softc_t *) dev->ud_private;

    powerondelay = ((unsigned int) softc->uhub_descr.bPowerOnToPowerGood)*2 + 20;

    /*
     * Turn on the power to the ports whose power is not yet on.
     */

    for (idx = 0; idx < softc->uhub_nports; idx++) {

	usbhub_get_port_status(dev,idx+1,portstatus);

	current = GETUSBFIELD(portstatus,wPortStatus);
	changed = GETUSBFIELD(portstatus,wPortChange);
	if (usb_noisy > 1) {
	    printf("BeforePowerup: port %d status %04X changed %04X\n",idx+1,current,changed);
	    }

	if (!(current & USB_PORT_STATUS_POWER)) {
	    if (usb_noisy > 1) console_log("USB: Powering up bus %d port %d",
				      dev->ud_bus->ub_num,idx+1);
	    usbhub_set_port_feature(dev,idx+1,USB_PORT_FEATURE_POWER);
	    usb_delay_ms(dev->ud_bus,powerondelay);
	    }
	}

    /*
     * Begin exploration at this level.
     */

    for (idx = 0; idx < softc->uhub_nports; idx++) {

	usbhub_get_port_status(dev,idx+1,portstatus);

	current = GETUSBFIELD(portstatus,wPortStatus);
	changed = GETUSBFIELD(portstatus,wPortChange);

	if (usb_noisy > 0) {
	    printf("USB: Explore: Bus %d Hub %d port %d status %04X changed %04X\n",
		   dev->ud_bus->ub_num,
		   dev->ud_address,idx+1,current,changed);
	    usb_dbg_dumpportstatus(idx+1,portstatus,1);
	    }


//	if (changed & USB_PORT_STATUS_RESET) {
//	    usbhub_clear_port_feature(dev,idx+1,USB_PORT_FEATURE_C_PORT_RESET);
//	    }

	if (changed & USB_PORT_STATUS_ENABLED) {
	    usbhub_clear_port_feature(dev,idx+1,USB_PORT_FEATURE_C_PORT_ENABLE);
	    }

	if (changed & USB_PORT_STATUS_CONNECT) {
	    /*
	     * A device was either connected or disconnected.
	     * Clear the status change first.
	     */

	    usbhub_clear_port_feature(dev,idx+1,USB_PORT_FEATURE_C_PORT_CONNECTION);

	    if (current & USB_PORT_STATUS_CONNECT) {

		/*
		 * The device has been CONNECTED.
		 */

		console_log("USB: New device connected to bus %d hub %d port %d",
			    dev->ud_bus->ub_num,
			    dev->ud_address,idx+1);

		/*
		 * Reset the device.  Reuse our old port status structure
		 * so we get the latest status.  Some devices do not report
		 * lowspeed until they are reset.
		 */

		usbhub_reset_device(dev,idx+1,portstatus);
		current = GETUSBFIELD(portstatus,wPortStatus);
		changed = GETUSBFIELD(portstatus,wPortChange);

		/*
		 * Create a device for this port.
		 */

		newdev = usb_create_device(dev->ud_bus,(current & USB_PORT_STATUS_LOWSPD) ? 1 : 0);

		/*
		 * Get the device descriptor. 
		 */

		res = usb_get_device_descriptor(newdev,&newdev->ud_devdescr,TRUE);

		if (usb_noisy > 1) usb_dbg_dumpdescriptors(newdev,(uint8_t *) &(newdev->ud_devdescr),8);

		/*
		 * Set up the max packet size for the control endpoint,
		 * then get the rest of the descriptor.
		 */
    
		usb_set_ep0mps(newdev,newdev->ud_devdescr.bMaxPacketSize0);
		res = usb_get_device_descriptor(newdev,&newdev->ud_devdescr,FALSE);

		/*
		 * Obtain a new address and set the address of the
		 * root hub to this address.
		 */

		addr = usb_new_address(newdev->ud_bus);
		res = usb_set_address(newdev,addr);

		/*
		 * Get the configuration descriptor and all the
		 * associated interface and endpoint descriptors.
		 */

		res = usb_get_config_descriptor(newdev,&cfgdescr,0,
						sizeof(usb_config_descr_t));
		if (res != sizeof(usb_config_descr_t)) {
		    printf("[a] usb_get_config_descriptor returns %d\n",res);
		    }

		len = GETUSBFIELD(&cfgdescr,wTotalLength);
		buf = usb_dma_alloc(len);

		res = usb_get_config_descriptor(newdev,(usb_config_descr_t *)buf,0,len);
		if (res != len) {
		    printf("[b] usb_get_config_descriptor returns %d\n",res);
		    }

		newdev->ud_cfgdescr = (usb_config_descr_t *) buf;

		if (usb_noisy > 1) usb_dbg_dumpdescriptors(newdev,buf,len);

		/*
		 * Point the hub at the devices it owns
		 */

		softc->uhub_devices[idx] = newdev;

		/*
		 * Find the driver for this.  It had better be the hub
		 * driver.
		 */

		newdrv = usb_find_driver(newdev);

		/*
		 * Call the attach method.
		 */

		if (newdrv) {
		    newdev->ud_drv = newdrv;	/* remember driver dispatch in device */
		    (*(newdrv->udrv_attach))(newdev,newdrv);
		    }
		}

	    else {

		/*
		 * The device has been DISCONNECTED.
		 */

		console_log("USB: Device disconnected from bus %d hub %d port %d",
			    dev->ud_bus->ub_num,
			    dev->ud_address,idx+1);

		/*
		 * Recover pointer to device below hub and clear
		 * this pointer.
		 */

		newdev = softc->uhub_devices[idx];	/* Get device pointer */

		usbhub_markdetached(newdev);		/* mark device and all subordinate 
							   devices as "removing" */

		softc->uhub_devices[idx] = NULL;	/* remove device from hub */

		/*
		 * Deassign the USB device's address and then 
		 * call detach method to free resources.  Devices that
		 * do not have drivers will not have any methods.
		 */

		if (newdev) {
		    if (newdev->ud_drv) {
			/* close open pipes, cancel reqs */
			usb_destroy_all_pipes(newdev);
			/* 
			 * Try to process the done queue.  This will complete any
			 * requests that made it out of the pipes while we were
			 * doing the stuff above.
			 */
			usb_poll(newdev->ud_bus);
			/* Call detach method, clean up device softc */
			(*(newdev->ud_drv->udrv_detach))(newdev);
			}
		    else {
			if (usb_noisy > 0) {
			    console_log("USB: Detached device on bus %d hub %d port %d "
				   "has no methods",
				   dev->ud_bus->ub_num,
				   dev->ud_address,idx+1);
			    }
			}

		    if (newdev->ud_cfgdescr) usb_dma_free(newdev->ud_cfgdescr);

		    usb_destroy_device(newdev);	
		    }

		}
	    }

	}

    usb_dma_free(portstatus);

    /*
     * Queue up a request for the interrupt pipe.  This will catch further
     * changes at this port.
     */

    usbhub_queue_intreq(dev,softc);

}
Example #5
0
static int usbctrl_set_led_callback(usbreq_t *ur) {
    usb_dma_free(ur->ur_buffer); //Don't leak the buffer
    //printf("Got callback for set leds\n");
    usb_free_request(ur);
    return 0;
}