Skip to content
Snippets Groups Projects
usb.c 33.7 KiB
Newer Older
Wolfgang Denk's avatar
Wolfgang Denk committed
/*
 * Most of this source has been derived from the Linux USB
 * project:
 * (C) Copyright Linus Torvalds 1999
 * (C) Copyright Johannes Erdfelt 1999-2001
 * (C) Copyright Andreas Gal 1999
 * (C) Copyright Gregory P. Smith 1999
 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
 * (C) Copyright Randy Dunlap 2000
 * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
 * (C) Copyright Yggdrasil Computing, Inc. 2000
 *     (usb_device_id matching changes by Adam J. Richter)
 *
 * Adapted for U-Boot:
 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
Wolfgang Denk's avatar
Wolfgang Denk committed
 *
 * SPDX-License-Identifier:	GPL-2.0+
Wolfgang Denk's avatar
Wolfgang Denk committed
 */

/*
 * How it works:
 *
 * Since this is a bootloader, the devices will not be automatic
 * (re)configured on hotplug, but after a restart of the USB the
 * device should work.
 *
 * For each transfer (except "Interrupt") we wait for completion.
 */
#include <common.h>
#include <command.h>
Wolfgang Denk's avatar
Wolfgang Denk committed
#include <asm/processor.h>
Mike Frysinger's avatar
Mike Frysinger committed
#include <linux/compiler.h>
#include <linux/ctype.h>
#include <asm/unaligned.h>
#include <errno.h>
Wolfgang Denk's avatar
Wolfgang Denk committed
#include <usb.h>

#define USB_BUFSIZ	512

Wolfgang Denk's avatar
Wolfgang Denk committed
static int asynch_allowed;
char usb_started; /* flag for the started/stopped USB status */

#ifndef CONFIG_DM_USB
static struct usb_device usb_dev[USB_MAX_DEVICE];
static int dev_index;

#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
#endif
Wolfgang Denk's avatar
Wolfgang Denk committed

/***************************************************************************
 * Init USB Device
 */
int usb_init(void)
{
	void *ctrl;
	struct usb_device *dev;
	int i, start_index = 0;
Wolfgang Denk's avatar
Wolfgang Denk committed

	dev_index = 0;
	asynch_allowed = 1;
Wolfgang Denk's avatar
Wolfgang Denk committed
	usb_hub_reset();

	/* first make all devices unknown */
	for (i = 0; i < USB_MAX_DEVICE; i++) {
		memset(&usb_dev[i], 0, sizeof(struct usb_device));
		usb_dev[i].devnum = -1;
	}

Wolfgang Denk's avatar
Wolfgang Denk committed
	/* init low_level USB */
	for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
		/* init low_level USB */
		printf("USB%d:   ", i);
		ret = usb_lowlevel_init(i, USB_INIT_HOST, &ctrl);
		if (ret == -ENODEV) {	/* No such device. */
			puts("Port not available.\n");
			continue;
		}

		if (ret) {		/* Other error. */
			puts("lowlevel init failed\n");
			continue;
		}
		/*
		 * lowlevel init is OK, now scan the bus for devices
		 * i.e. search HUBs and configure them
		 */
		start_index = dev_index;
		printf("scanning bus %d for devices... ", i);
		ret = usb_alloc_new_device(ctrl, &dev);
		if (ret)
		/*
		 * device 0 is always present
		 * (root hub, so let it analyze)
		 */
		ret = usb_new_device(dev);
		if (ret)
			usb_free_device(dev->controller);
		if (start_index == dev_index) {
			puts("No USB Device found\n");
			continue;
		} else {
			printf("%d USB Device(s) found\n",
				dev_index - start_index);
	/* if we were not able to find at least one working bus, bail out */
		puts("USB error: all controllers failed lowlevel init\n");

	return usb_started ? 0 : -ENODEV;
Wolfgang Denk's avatar
Wolfgang Denk committed
}

/******************************************************************************
 * Stop USB this stops the LowLevel Part and deregisters USB devices.
 */
int usb_stop(void)
{

	if (usb_started) {
		asynch_allowed = 1;
		usb_started = 0;
		usb_hub_reset();

		for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
			if (usb_lowlevel_stop(i))
				printf("failed to stop USB controller %d\n", i);
		}
Wolfgang Denk's avatar
Wolfgang Denk committed
}

/******************************************************************************
 * Detect if a USB device has been plugged or unplugged.
 */
int usb_detect_change(void)
{
	int i, j;
	int change = 0;

	for (j = 0; j < USB_MAX_DEVICE; j++) {
		for (i = 0; i < usb_dev[j].maxchild; i++) {
			struct usb_port_status status;

			if (usb_get_port_status(&usb_dev[j], i + 1,
						&status) < 0)
				/* USB request failed */
				continue;

			if (le16_to_cpu(status.wPortChange) &
			    USB_PORT_STAT_C_CONNECTION)
				change++;
		}
	}

	return change;
}

Wolfgang Denk's avatar
Wolfgang Denk committed
/*
 * disables the asynch behaviour of the control message. This is used for data
 * transfers that uses the exclusiv access to the control and bulk messages.
 * Returns the old value so it can be restored later.
Wolfgang Denk's avatar
Wolfgang Denk committed
 */
int usb_disable_asynch(int disable)
Wolfgang Denk's avatar
Wolfgang Denk committed
{
	int old_value = asynch_allowed;

	asynch_allowed = !disable;
	return old_value;
Wolfgang Denk's avatar
Wolfgang Denk committed
}
#endif /* !CONFIG_DM_USB */
Wolfgang Denk's avatar
Wolfgang Denk committed


/*-------------------------------------------------------------------
 * Message wrappers.
 *
 */

/*
 * submits an Interrupt Message
 */
int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
			void *buffer, int transfer_len, int interval)
Wolfgang Denk's avatar
Wolfgang Denk committed
{
Loading
Loading full blame...