Newer
Older
/*
* 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
* SPDX-License-Identifier: GPL-2.0+
*/
/*
* 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>
#include <memalign.h>
#include <linux/ctype.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>
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
/***************************************************************************
* Init USB Device
*/
int usb_init(void)
{
void *ctrl;
struct usb_device *dev;
int i, start_index = 0;
Hans de Goede
committed
int controllers_initialized = 0;
dev_index = 0;
asynch_allowed = 1;
/* 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;
}
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");
Hans de Goede
committed
controllers_initialized++;
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
*/
Hans de Goede
committed
controllers_initialized++;
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");
printf("%d USB Device(s) found\n",
dev_index - start_index);
usb_started = 1;
debug("scan end\n");
/* if we were not able to find at least one working bus, bail out */
Hans de Goede
committed
if (controllers_initialized == 0)
puts("USB error: all controllers failed lowlevel init\n");
return usb_started ? 0 : -ENODEV;
}
/******************************************************************************
* 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);
}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/******************************************************************************
* 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;
}
/*
* 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.
#endif /* !CONFIG_DM_USB */
/*-------------------------------------------------------------------
* 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)
Loading
Loading full blame...