refactored USB driver code, enabled debug printouts everywhere

This commit is contained in:
Markus Fröschle
2014-09-01 19:22:26 +00:00
parent 3b0e69127f
commit d25e6cfd98
12 changed files with 896 additions and 852 deletions

View File

@@ -267,3 +267,5 @@ tos/jtagwait/sources/printf_helper.S
tos/bascook/Makefile
tos/bascook/sources/bascook.c
tos/Makefile
usb/usb_hub.c
include/usb_hub.h

View File

@@ -95,6 +95,7 @@ CSRCS= \
usb.c \
ohci-hcd.c \
ehci-hcd.c \
usb_hub.c \
usb_mouse.c \
ikbd.c \
\

View File

@@ -51,6 +51,7 @@ SECTIONS
OBJDIR/pci_wrappers.o(.text)
OBJDIR/usb.o(.text)
OBJDIR/driver_mem.o(.text)
OBJDIR/usb_hub.o(.text)
OBJDIR/usb_mouse.o(.text)
OBJDIR/ohci-hcd.o(.text)
OBJDIR/ehci-hcd.o(.text)

View File

@@ -104,7 +104,8 @@ struct ehci_hcor {
#define USBMODE_CM_IDLE (0 << 0) /* idle state */
/* Interface descriptor */
struct usb_linux_interface_descriptor {
struct usb_linux_interface_descriptor
{
unsigned char bLength;
unsigned char bDescriptorType;
unsigned char bInterfaceNumber;
@@ -117,7 +118,8 @@ struct usb_linux_interface_descriptor {
} __attribute__ ((packed));
/* Configuration descriptor information.. */
struct usb_linux_config_descriptor {
struct usb_linux_config_descriptor
{
unsigned char bLength;
unsigned char bDescriptorType;
unsigned short wTotalLength;
@@ -129,11 +131,11 @@ struct usb_linux_config_descriptor {
} __attribute__ ((packed));
#if defined CONFIG_EHCI_DESC_BIG_ENDIAN
#define ehci_readl(x) (*((volatile u32 *)(x)))
#define ehci_writel(a, b) (*((volatile u32 *)(a)) = ((volatile u32)b))
#define ehci_readl(x) (*((volatile uint32_t *)(x)))
#define ehci_writel(a, b) (*((volatile uint32_t *)(a)) = ((volatile uint32_t) b))
#else
#define ehci_readl(x) swpl((*((volatile u32 *)(x))))
#define ehci_writel(a, b) (*((volatile u32 *)(a)) = swpl(((volatile u32)b)))
#define ehci_readl(x) swpl((*((volatile uint32_t *)(x))))
#define ehci_writel(a, b) (*((volatile uint32_t *)(a)) = swpl(((volatile uint32_t) b)))
#endif
#if defined CONFIG_EHCI_MMIO_BIG_ENDIAN
@@ -174,7 +176,8 @@ struct usb_linux_config_descriptor {
*/
/* Queue Element Transfer Descriptor (qTD). */
struct qTD {
struct qTD
{
uint32_t qt_next;
#define QT_NEXT_TERMINATE 1
uint32_t qt_altnext;
@@ -183,7 +186,8 @@ struct qTD {
};
/* Queue Head (QH). */
struct QH {
struct QH
{
uint32_t qh_link;
#define QH_LINK_TERMINATE 1
#define QH_LINK_TYPE_ITD 0

View File

@@ -26,7 +26,6 @@
#ifndef _USB_H_
#define _USB_H_
//#include <stdlib.h>
#include <bas_string.h>
#include "driver_mem.h"
#include "pci.h"
@@ -37,14 +36,6 @@
extern long *tab_funcs_pci;
#define in8(addr) Fast_read_mem_byte(usb_handle,addr)
#define in16r(addr) Fast_read_mem_word(usb_handle,addr)
#define in32r(addr) Fast_read_mem_longword(usb_handle,addr)
#define out8(addr,val) Write_mem_byte(usb_handle,addr,val)
#define out16r(addr,val) Write_mem_word(usb_handle,addr,val)
#define out32r(addr,val) Write_mem_longword(usb_handle,addr,val)
#define __u8 uint8_t
#define __u16 uint16_t
#define __u32 uint32_t
@@ -74,6 +65,8 @@ extern int sprintD(char *s, const char *fmt, ...);
#define USB_CNTL_TIMEOUT 100 /* 100ms timeout */
#define USB_BUFSIZ 512
/* String descriptor */
struct usb_string_descriptor
{
@@ -238,65 +231,62 @@ typedef struct
* this is how the lowlevel part communicate with the outer world
*/
int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void **priv);
int ohci_usb_lowlevel_stop(void *priv);
int ohci_submit_bulk_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len);
int ohci_submit_control_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, struct devrequest *setup);
int ohci_submit_int_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, int interval);
void ohci_usb_enable_interrupt(int enable);
extern int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void **priv);
extern int ohci_usb_lowlevel_stop(void *priv);
extern int ohci_submit_bulk_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len);
extern int ohci_submit_control_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, struct devrequest *setup);
extern int ohci_submit_int_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, int interval);
extern void ohci_usb_enable_interrupt(int enable);
int ehci_usb_lowlevel_init(long handle, const struct pci_device_id *ent, void **priv);
int ehci_usb_lowlevel_stop(void *priv);
int ehci_submit_bulk_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len);
int ehci_submit_control_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, struct devrequest *setup);
int ehci_submit_int_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, int interval);
void ehci_usb_enable_interrupt(int enable);
extern int ehci_usb_lowlevel_init(long handle, const struct pci_device_id *ent, void **priv);
extern int ehci_usb_lowlevel_stop(void *priv);
extern int ehci_submit_bulk_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len);
extern int ehci_submit_control_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, struct devrequest *setup);
extern int ehci_submit_int_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, int interval);
extern void ehci_usb_enable_interrupt(int enable);
void usb_enable_interrupt(int enable);
extern void usb_enable_interrupt(int enable);
extern int usb_new_device(struct usb_device *dev);
extern struct usb_device *usb_alloc_new_device(int bus_index, void *priv);
extern void usb_disconnect(struct usb_device **pdev);
#define USB_MAX_STOR_DEV 5
block_dev_desc_t *usb_stor_get_dev(int index);
int usb_stor_scan(void);
int usb_stor_info(void);
int usb_stor_register(struct usb_device *dev);
int usb_stor_deregister(struct usb_device *dev);
int drv_usb_kbd_init(void);
int usb_kbd_register(struct usb_device *dev);
int usb_kbd_deregister(struct usb_device *dev);
extern block_dev_desc_t *usb_stor_get_dev(int index);
extern int usb_stor_scan(void);
extern int usb_stor_info(void);
extern int usb_stor_register(struct usb_device *dev);
extern int usb_stor_deregister(struct usb_device *dev);
int drv_usb_mouse_init(void);
int usb_mouse_register(struct usb_device *dev);
int usb_mouse_deregister(struct usb_device *dev);
extern int drv_usb_kbd_init(void);
extern int usb_kbd_register(struct usb_device *dev);
extern int usb_kbd_deregister(struct usb_device *dev);
/* memory */
void *usb_malloc(long amount);
int usb_free(void *addr);
int usb_mem_init(void);
void usb_mem_stop(void);
extern int drv_usb_mouse_init(void);
extern int usb_mouse_register(struct usb_device *dev);
extern int usb_mouse_deregister(struct usb_device *dev);
/* routines */
USB_COOKIE *usb_get_cookie(long id);
void usb_error_msg(const char *const fmt, ... );
int usb_init(int32_t handle, const struct pci_device_id *ent); /* initialize the USB Controller */
int usb_stop(void); /* stop the USB Controller */
extern int usb_init(int32_t handle, const struct pci_device_id *ent); /* initialize the USB Controller */
extern int usb_stop(void); /* stop the USB Controller */
int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id);
struct usb_device *usb_get_dev_index(int index, int bus);
int usb_control_msg(struct usb_device *dev, unsigned int pipe, uint8_t request, uint8_t requesttype, uint16_t value,
uint16_t index, void *data, uint16_t size, int timeout);
int usb_bulk_msg(struct usb_device *dev, unsigned int pipe, void *data, int len, int *actual_length, int timeout);
int usb_submit_int_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, int interval);
void usb_disable_asynch(int disable);
int usb_maxpacket(struct usb_device *dev, uint32_t pipe);
void wait_ms(uint32_t ms);
int usb_get_configuration_no(struct usb_device *dev, uint8_t *buffer, int cfgno);
int usb_get_report(struct usb_device *dev, int ifnum, uint8_t type, uint8_t id, void *buf, int size);
int usb_get_class_descriptor(struct usb_device *dev, int ifnum, uint8_t type, uint8_t id, void *buf, int size);
int usb_clear_halt(struct usb_device *dev, int pipe);
int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
int usb_set_interface(struct usb_device *dev, int interface, int alternate);
extern int usb_set_protocol(struct usb_device *dev, int ifnum, int protocol);
extern int usb_set_idle(struct usb_device *dev, int ifnum, int duration, int report_id);
extern struct usb_device *usb_get_dev_index(int index, int bus);
extern int usb_control_msg(struct usb_device *dev, unsigned int pipe, uint8_t request, uint8_t requesttype,
uint16_t value, uint16_t index, void *data, uint16_t size, int timeout);
extern int usb_bulk_msg(struct usb_device *dev, unsigned int pipe, void *data, int len, int *actual_length, int timeout);
extern int usb_submit_int_msg(struct usb_device *dev, uint32_t pipe, void *buffer, int transfer_len, int interval);
extern void usb_disable_asynch(int disable);
extern int usb_maxpacket(struct usb_device *dev, uint32_t pipe);
extern int usb_get_configuration_no(struct usb_device *dev, uint8_t *buffer, int cfgno);
extern int usb_get_report(struct usb_device *dev, int ifnum, uint8_t type, uint8_t id, void *buf, int size);
extern int usb_get_class_descriptor(struct usb_device *dev, int ifnum, uint8_t type, uint8_t id, void *buf, int size);
extern int usb_clear_halt(struct usb_device *dev, int pipe);
extern int usb_string(struct usb_device *dev, int index, char *buf, size_t size);
extern int usb_set_interface(struct usb_device *dev, int interface, int alternate);
/*
* Calling this entity a "pipe" is glorifying it. A USB pipe

10
BaS_gcc/include/usb_hub.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef USB_HUB_H
#define USB_HUB_H
extern int bus_index;
extern void usb_hub_reset(int bus_index);
extern int usb_hub_probe(struct usb_device *dev, int ifnum);
extern int hub_port_reset(struct usb_device *dev, int port, unsigned short *portstat);
#endif // USB_HUB_H

View File

@@ -31,8 +31,8 @@
//extern xQueueHandle queue_poll_hub;
#undef DEBUG
#undef SHOW_INFO
//#undef DEBUG
//#undef SHOW_INFO
static char ehci_inited;
static int rootdev;

View File

@@ -53,22 +53,19 @@
//extern xQueueHandle queue_poll_hub;
#undef DEBUG_PCIE
#undef OHCI_USE_NPS /* force NoPowerSwitching mode */
#undef OHCI_VERBOSE_DEBUG /* not always helpful */
#undef DEBUG
#undef DEBUG_OHCI
#undef SHOW_INFO
#undef OHCI_FILL_TRACE
//#define DEBUG
#ifdef DEBUG
#define debug_printf(format, arg...) do { xprintf("DEBUG: " format "\r\n", ##arg); } while (0)
#define DEBUG_OHCI
#ifdef DEBUG_OHCI
#define dbg(format, arg...) do { xprintf("DEBUG: %s(): " format, __FUNCTION__, ##arg); } while (0)
#else
#define debug_printf(format, arg...) do { ; } while (0)
#endif /* DEBUG */
#define dbg(format, arg...) do { ; } while (0)
#endif /* DEBUG_OHCI */
/* For initializing controller (mask in an HCFS mode too) */
#define OHCI_CONTROL_INIT (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
@@ -86,19 +83,18 @@ inline uint32_t readl(volatile uint32_t *addr)
{
uint32_t res;
//debug_printf("reading from 0x%08x in %s, %d", addr, __FILE__, __LINE__);
res = swpl(*addr);
chip_errata_135();
//debug_printf(" result=0x%08x\r\n", res);
//chip_errata_135();
//dbg("reading from 0x%08x = 0x%08x\r\n", addr, res);
return res;
}
/*
#define writel(a, b) {debug_printf("writing %08x to %08x\r\n", (a), (b)); *((volatile uint32_t *)(b)) = swpl((volatile uint32_t)(a)); }
#define writel(a, b) {dbg("writing %08x to %08x\r\n", (a), (b)); *((volatile uint32_t *)(b)) = swpl((volatile uint32_t)(a)); }
*/
inline void writel(uint32_t value, uint32_t *address)
{
//debug_printf("writing %08x to %08x in %s, %d\r\n", value, address, __FILE__, __LINE__);
// dbg("writing %08x to %08x\r\n", value, address);
* (volatile uint32_t *) address = swpl(value);
}
#else
@@ -152,16 +148,8 @@ struct pci_device_id ohci_usb_pci_table[] =
}
};
#define DEBUG
#ifdef DEBUG
#define dbg(format, arg...) do {debug_printf("DEBUG: " format "\r\n", ## arg);} while(0)
#else
#define dbg(format, arg...) do {} while (0)
#endif /* DEBUG */
#define err(format, arg...) do {debug_printf("ERROR: " format "\r\n", ## arg); }while(0)
#define info(format, arg...) debug_printf("INFO: " format "\r\n", ## arg)
extern void udelay(long usec);
#define err(format, arg...) do {dbg("ERROR: " format "\r\n", ## arg); }while(0)
#define info(format, arg...) dbg("INFO: " format "\r\n", ## arg)
/* global ohci_t */
static ohci_t gohci[2];
@@ -231,7 +219,7 @@ static void urb_free_priv(urb_priv_t *urb)
/*-------------------------------------------------------------------------*/
#ifdef DEBUG
#ifdef DEBUG_OHCI
static int sohci_get_current_frame_number(ohci_t *ohci, struct usb_device *dev);
/* debug| print the main components of an URB
@@ -241,7 +229,7 @@ static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
uint32_t pipe, void *buffer, int transfer_len,
struct devrequest *setup, char *str, int small)
{
dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx\r\n",
str,
sohci_get_current_frame_number(ohci, dev),
usb_pipedevice(pipe),
@@ -260,27 +248,29 @@ static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
if (usb_pipecontrol(pipe))
{
debug_printf(__FILE__ ": cmd(8):");
dbg(__FILE__ ": cmd(8):");
for (i = 0; i < 8 ; i++)
debug_printf(" %02x", ((uint8_t *)setup)[i]);
debug_printf("\r\n");
dbg(" %02x", ((uint8_t *)setup)[i]);
dbg("\r\n");
}
if (transfer_len > 0 && buffer)
{
debug_printf(__FILE__ ": data(%d/%d):", (purb ? purb->actual_length : 0), transfer_len);
dbg(__FILE__ ": data(%d/%d):", (purb ? purb->actual_length : 0), transfer_len);
len = usb_pipeout(pipe)? transfer_len : (purb ? purb->actual_length : 0);
for (i = 0; i < 16 && i < len; i++)
debug_printf(" %02x", ((uint8_t *)buffer)[i]);
debug_printf("%s\r\n", i < len? "...": "");
dbg(" %02x", ((uint8_t *)buffer)[i]);
dbg("%s\r\n", i < len? "...": "");
}
}
#endif
}
/* just for debugging; prints non-empty branches of the int ed tree
* inclusive iso eds */
/*
* just for debugging; prints non-empty branches of the int ed tree
* inclusive iso eds
*/
static void ep_print_int_eds(ohci_t *ohci, char *str)
{
int i, j;
@@ -293,20 +283,20 @@ static void ep_print_int_eds(ohci_t *ohci, char *str)
if (*ed_p == 0)
continue;
debug_printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
dbg(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
while (*ed_p != 0 && j--)
{
ed_t *ed = (ed_t *)swpl((uint32_t)ed_p);
debug_printf(" ed: %4x;", ed->hwINFO);
dbg(" ed: %4x;", ed->hwINFO);
ed_p = &ed->hwNextED;
}
debug_printf("\r\n");
dbg("\r\n");
}
}
static void ohci_dump_intr_mask(char *label, uint32_t mask)
{
dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s\r\n",
label,
mask,
(mask & OHCI_INTR_MIE) ? " MIE" : "",
@@ -331,15 +321,15 @@ static void maybe_print_eds(ohci_t *controller, char *label, uint32_t value)
if (value && (value < 0xDFFFF0)) /* STRAM */
{
dbg("%s %08x", label, value);
dbg("%08x", edp->hwINFO);
dbg("%08x", edp->hwTailP);
dbg("%08x", edp->hwHeadP);
dbg("%08x", edp->hwNextED);
dbg("%s %08x\r\n", label, value);
dbg("%08x\r\n", edp->hwINFO);
dbg("%08x\r\n", edp->hwTailP);
dbg("%08x\r\n", edp->hwHeadP);
dbg("%08x\r\n", edp->hwNextED);
}
}
#ifdef DEBUG
#ifdef DEBUG_OHCI
static char *hcfs2string(int state)
{
switch (state)
@@ -358,10 +348,12 @@ static void ohci_dump_status(ohci_t *controller)
{
struct ohci_regs *regs = controller->regs;
uint32_t temp = readl(&regs->revision) & 0xff;
if (temp != 0x10)
dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
dbg("spec %d.%d\r\n", (temp >> 4), (temp & 0x0f));
temp = readl(&regs->control);
dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d\r\n", temp,
(temp & OHCI_CTRL_RWE) ? " RWE" : "",
(temp & OHCI_CTRL_RWC) ? " RWC" : "",
(temp & OHCI_CTRL_IR) ? " IR" : "",
@@ -372,8 +364,9 @@ static void ohci_dump_status(ohci_t *controller)
(temp & OHCI_CTRL_PLE) ? " PLE" : "",
temp & OHCI_CTRL_CBSR
);
temp = readl(&regs->cmdstatus);
dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s\r\n", temp,
(temp & OHCI_SOC) >> 16,
(temp & OHCI_OCR) ? " OCR" : "",
(temp & OHCI_BLF) ? " BLF" : "",
@@ -403,7 +396,7 @@ static void ohci_dump_roothub(ohci_t *controller, int verbose)
ndp = controller->ndp;
if (verbose)
{
dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d\r\n", temp,
((temp & RH_A_POTPGT) >> 24) & 0xff,
(temp & RH_A_NOCP) ? " NOCP" : "",
(temp & RH_A_OCPM) ? " OCPM" : "",
@@ -413,13 +406,13 @@ static void ohci_dump_roothub(ohci_t *controller, int verbose)
ndp
);
temp = roothub_b(controller);
dbg("roothub.b: %08x PPCM=%04x DR=%04x",
dbg("roothub.b: %08x PPCM=%04x DR=%04x\r\n",
temp,
(temp & RH_B_PPCM) >> 16,
(temp & RH_B_DR)
);
temp = roothub_status(controller);
dbg("roothub.status: %08x%s%s%s%s%s%s",
dbg("roothub.status: %08x%s%s%s%s%s%s\r\n",
temp,
(temp & RH_HS_CRWE) ? " CRWE" : "",
(temp & RH_HS_OCIC) ? " OCIC" : "",
@@ -433,7 +426,7 @@ static void ohci_dump_roothub(ohci_t *controller, int verbose)
for (i = 0; i < ndp; i++)
{
temp = roothub_portstatus(controller, i);
dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\r\n",
i,
temp,
(temp & RH_PS_PRSC) ? " PRSC" : "",
@@ -456,15 +449,15 @@ static void ohci_dump_roothub(ohci_t *controller, int verbose)
static void ohci_dump(ohci_t *ohci, int verbose)
{
dbg("OHCI controller usb-%s-%c state", ohci->slot_name, (char)ohci->controller + '0');
dbg("OHCI controller usb-%s-%c state\r\n", ohci->slot_name, (char)ohci->controller + '0');
/* dumps some of the state we know about */
ohci_dump_status(ohci);
if (verbose)
ep_print_int_eds(ohci, "hcca");
dbg("hcca frame #%04x", ohci->hcca->frame_no);
dbg("hcca frame #%04x\r\n", ohci->hcca->frame_no);
ohci_dump_roothub(ohci, 1);
}
#endif /* DEBUG */
#endif /* DEBUG_OHCI */
/*-------------------------------------------------------------------------*
* Interface functions (URB)
@@ -596,7 +589,7 @@ static inline int sohci_return_job(ohci_t *ohci, urb_priv_t *urb)
/*-------------------------------------------------------------------------*/
#ifdef DEBUG
#ifdef DEBUG_OHCI
/* tell us the current USB frame number */
static int sohci_get_current_frame_number(ohci_t *ohci, struct usb_device *usb_dev)
@@ -896,8 +889,8 @@ static void td_fill(ohci_t *ohci, unsigned int info, void *data, int len,
if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe))
{
for (i = 0; i < len; i++)
debug_printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
debug_printf("\r\n");
dbg("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
dbg("\r\n");
}
#endif
if (!len)
@@ -921,14 +914,14 @@ static void td_fill(ohci_t *ohci, unsigned int info, void *data, int len,
if (data)
{
int i;
debug_printf("td_fill: %08x %08x %08X %08X at 0x%08X\r\n",
dbg("td_fill: %08x %08x %08X %08X at 0x%08X\r\n",
swpl(td->hwINFO), swpl(td->hwCBP), swpl(td->hwNextTD), swpl(td->hwBE), td);
for (i = 0; i < len; i++)
debug_printf("%02X ", *(unsigned char *)(data + i) & 0xff);
debug_printf("\r\n");
dbg("%02X ", *(unsigned char *)(data + i) & 0xff);
dbg("\r\n");
}
else
debug_printf("td_fill: %08x %08x %08X %08X at 0x%08X\r\n",
dbg("td_fill: %08x %08x %08X %08X at 0x%08X\r\n",
swpl(td->hwINFO), swpl(td->hwCBP), swpl(td->hwNextTD), swpl(td->hwBE), td);
#endif
}
@@ -1049,7 +1042,7 @@ static void check_status(ohci_t *ohci, td_t *td_list)
if (cc)
{
err("OHCI usb-%s-%c error: %s (%x)", ohci->slot_name, (char)ohci->controller + '0', cc_to_string[cc], cc);
//err("OHCI usb-%s-%c error: %s (%x)", ohci->slot_name, (char)ohci->controller + '0', cc_to_string[cc], cc);
if (*phwHeadP & swpl(0x1))
{
if (lurb_priv && ((td_list->index + 1) < urb_len))
@@ -1130,7 +1123,7 @@ static int takeback_td(ohci_t *ohci, td_t *td_list)
cc = TD_CC_GET(tdINFO);
if (cc)
{
err("OHCI usb-%s-%c error: %s (%x)", ohci->slot_name, (char)ohci->controller + '0', cc_to_string[cc], cc);
//err("OHCI usb-%s-%c error: %s (%x)", ohci->slot_name, (char)ohci->controller + '0', cc_to_string[cc], cc);
stat = cc_to_error[cc];
}
@@ -1273,7 +1266,7 @@ static unsigned char root_hub_str_index1[] =
/*-------------------------------------------------------------------------*/
#define OK(x) len = (x); break
#ifdef DEBUG
#ifdef DEBUG_OHCI
#define WR_RH_STAT(x) { info("WR:status %#8x", (x)); writel((x), &ohci->regs->roothub.status); }
#define WR_RH_PORTSTAT(x) { info("WR:portstatus[%d] %#8x", wIndex-1, (x)); writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
#else
@@ -1325,7 +1318,7 @@ static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev, uint32_t pip
uint16_t wIndex;
uint16_t wLength;
#ifdef DEBUG
#ifdef DEBUG_OHCI
pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
#else
if (ohci->irq)
@@ -1479,7 +1472,7 @@ static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev, uint32_t pip
dbg("unsupported root hub command");
stat = USB_ST_STALLED;
}
#ifdef DEBUG
#ifdef DEBUG_OHCI
ohci_dump_roothub(ohci, 1);
#else
if (ohci->irq)
@@ -1490,7 +1483,7 @@ static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev, uint32_t pip
memcpy(data, data_buf, len);
dev->act_len = len;
dev->status = stat;
#ifdef DEBUG
#ifdef DEBUG_OHCI
pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
#else
if (ohci->irq)
@@ -1529,7 +1522,7 @@ static int submit_common_msg(ohci_t *ohci, struct usb_device *dev, uint32_t pipe
dev->status = USB_ST_CRC_ERR;
return 0;
}
#ifdef DEBUG
#ifdef DEBUG_OHCI
urb->actual_length = 0;
pkt_print(ohci, urb, dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
#else
@@ -1609,7 +1602,7 @@ static int submit_common_msg(ohci_t *ohci, struct usb_device *dev, uint32_t pipe
}
dev->status = stat;
dev->act_len = transfer_len;
#ifdef DEBUG
#ifdef DEBUG_OHCI
pkt_print(ohci, urb, dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
#else
if (ohci->irq)
@@ -1633,7 +1626,7 @@ int ohci_submit_control_msg(struct usb_device *dev, uint32_t pipe, void *buffer,
ohci_t *ohci = (ohci_t *)dev->priv_hcd;
int maxsize = usb_maxpacket(dev, pipe);
info("submit_control_msg dev 0x%p ohci 0x%p", dev, ohci);
#ifdef DEBUG
#ifdef DEBUG_OHCI
pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
#else
if (ohci->irq)
@@ -1672,21 +1665,23 @@ static int hc_reset(ohci_t *ohci)
int timeout = 30;
int smm_timeout = 50; /* 0,5 sec */
dbg("%s\r\n", __FUNCTION__);
if ((ohci->ent->vendor == PCI_VENDOR_ID_PHILIPS)
&& (ohci->ent->device == PCI_DEVICE_ID_PHILIPS_ISP1561))
{
#define EHCI_USBCMD_OFF 0x20
#define EHCI_USBCMD_HCRESET (1 << 1)
/* Some multi-function controllers (e.g. ISP1562) allow root hub
resetting via EHCI registers only. */
/*
* Some multi-function controllers (e.g. ISP1562) allow root hub
* resetting via EHCI registers only.
*/
short index = 0;
long handle;
do
{
handle = pci_find_device(0x0, 0xffff, index++);
if (handle >= 0)
{
uint32_t id = 0;
@@ -1732,20 +1727,23 @@ static int hc_reset(ohci_t *ohci)
if ((ohci->controller == 0) && (ohci->ent->vendor == PCI_VENDOR_ID_NEC)
&& (ohci->ent->device == PCI_DEVICE_ID_NEC_USB))
{
if (ohci->handle == 1) /* NEC on motherboard has FPGA clock */
//if (ohci->handle == 1) /* NEC on motherboard has FPGA clock */
#if defined(MACHINE_FIREBEE)
{
dbg("USB OHCI set 48MHz clock\r\n");
pci_write_config_longword(ohci->handle, 0xE4, 0x21); // oscillator & disable ehci
wait(10);
}
else
//else
#else
{
pci_write_config_longword(ohci->handle, 0xE4, pci_read_config_longword(ohci->handle, 0xE4) | 0x01); // disable ehci
wait(10);
}
#endif
}
debug_printf("control: %x\r\n", readl(&ohci->regs->control));
dbg("control: %x\r\n", readl(&ohci->regs->control));
if (readl(&ohci->regs->control) & OHCI_CTRL_IR)
{
/* SMM owns the HC */
@@ -1765,11 +1763,14 @@ static int hc_reset(ohci_t *ohci)
/* Disable HC interrupts */
writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
ohci_dump_status(ohci);
dbg("USB OHCI HC reset_hc usb-%s-%c: ctrl = 0x%X", ohci->slot_name, (char)ohci->controller + '0', readl(&ohci->regs->control));
dbg("USB OHCI HC reset_hc usb-%s-%c: ctrl = 0x%X\r\n", ohci->slot_name,
(char) ohci->controller + '0', readl(&ohci->regs->control));
/* Reset USB (needed by some controllers) */
ohci->hc_control = 0;
writel(ohci->hc_control, &ohci->regs->control);
wait(50);
/* HC Reset requires max 10 us delay */
writel(OHCI_HCR, &ohci->regs->cmdstatus);
while ((readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0)
@@ -1927,7 +1928,7 @@ static int hc_interrupt(ohci_t *ohci)
status & 0x8000 ? ", Parity error" : "", status & 0x4000 ? ", Signaled system error" : "", status & 0x2000 ? ", Received master abort" : "",
status & 0x1000 ? ", Received target abort" : "", status & 0x800 ? ", Signaled target abort" : "", status & 0x100 ? ", Data parity error" : "");
ohci->disabled++;
#ifdef DEBUG
#ifdef DEBUG_OHCI
ohci_dump(ohci, 1);
#else
if (ohci->irq)
@@ -2057,7 +2058,8 @@ int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void
info("ohci %p", ohci);
ohci->controller = (ohci->handle >> 16) & 3; /* PCI function */
ohci->controller = PCI_FUNCTION_FROM_HANDLE(ohci->handle);
// ohci->controller = (ohci->handle >> 16) & 3; /* PCI function */
/* this must be aligned to a 256 byte boundary */
ohci->hcca_unaligned = (struct ohci_hcca *) driver_mem_alloc(sizeof(struct ohci_hcca) + 256);
@@ -2092,7 +2094,7 @@ int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void
ptd = (struct td *) (((uint32_t) ohci->td_unaligned + 7) & ~7);
debug_printf("memset from %p to %p\r\n", ptd, ptd + sizeof(td_t) * NUM_TD);
dbg("memset from %p to %p\r\n", ptd, ptd + sizeof(td_t) * NUM_TD);
memset(ptd, 0, sizeof(td_t) * NUM_TD);
info("aligned TDs %p", ptd);
@@ -2105,7 +2107,7 @@ int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void
unsigned short flags;
do
{
debug_printf("\r\nPCI USB descriptors (at %p): flags 0x%04x start 0x%08lx \r\n offset 0x%08lx dmaoffset 0x%08lx length 0x%08lx\r\n", pci_rsc_desc,
dbg("\r\nPCI USB descriptors (at %p): flags 0x%04x start 0x%08lx \r\n offset 0x%08lx dmaoffset 0x%08lx length 0x%08lx\r\n", pci_rsc_desc,
pci_rsc_desc->flags, pci_rsc_desc->start, pci_rsc_desc->offset, pci_rsc_desc->dmaoffset, pci_rsc_desc->length);
if (!(pci_rsc_desc->flags & FLG_IO))
{
@@ -2131,7 +2133,7 @@ int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void
else
{
hc_free_buffers(ohci);
debug_printf("pci_get_resource() failed in %s %s\r\n", __FILE__, __LINE__);
dbg("pci_get_resource() failed in %s %s\r\n", __FILE__, __LINE__);
return(-1); /* get_resource error */
}
@@ -2153,7 +2155,7 @@ int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void
}
}
debug_printf("OHCI usb-%s-%c, regs address 0x%08X, PCI handle 0x%X\r\n", ohci->slot_name, (char)ohci->controller + '0', ohci->regs, handle);
dbg("OHCI usb-%s-%c, regs address 0x%08X, PCI handle 0x%X\r\n", ohci->slot_name, (char)ohci->controller + '0', ohci->regs, handle);
if (hc_reset(ohci) < 0)
{
err("Can't reset OHCI usb-%s-%c", ohci->slot_name, (char)ohci->controller + '0');
@@ -2171,7 +2173,7 @@ int ohci_usb_lowlevel_init(int32_t handle, const struct pci_device_id *ent, void
return(-1);
}
#ifdef DEBUG
#ifdef DEBUG_OHCI
ohci_dump(ohci, 1);
#endif
pci_hook_interrupt(handle, handle_usb_interrupt, ohci);

View File

@@ -28,7 +28,7 @@
#error "unknown machine!"
#endif
//#define DBG_DM
#define DBG_DM
#ifdef DBG_DM
#define dbg(fmt, args...) xprintf(fmt, ##args)

View File

@@ -52,6 +52,7 @@
#include "wait.h"
#include <stdarg.h>
#include "usb.h"
#include "usb_hub.h"
extern int usb_stor_curr_dev;
extern uint32_t usb_1st_disk_drive;
@@ -63,9 +64,6 @@ extern uint32_t usb_1st_disk_drive;
#define dbg(format, arg...) do { ; } while (0)
#endif /* DEBUG_USB */
#define USB_BUFSIZ 512
struct hci
{
/* ------- common part -------- */
@@ -76,7 +74,7 @@ struct hci
};
static struct usb_device *usb_dev;
static int bus_index;
int bus_index;
static int dev_index[USB_MAX_BUS];
static struct hci *controller_priv[USB_MAX_BUS];
static int asynch_allowed;
@@ -84,33 +82,20 @@ static struct devrequest *setup_packet;
char usb_started; /* flag for the started/stopped USB status */
typedef struct
{
int dest;
void (*func)(char);
char *loc;
} PRINTK_INFO;
#define DEST_CONSOLE (1)
#define DEST_STRING (2)
/**********************************************************************
/*
* some forward declerations...
*/
void usb_scan_devices(void *priv);
int usb_hub_probe(struct usb_device *dev, int ifnum);
void usb_hub_reset(int index_bus);
static int hub_port_reset(struct usb_device *dev, int port, unsigned short *portstat);
extern int printk(PRINTK_INFO *info, const char *fmt, va_list ap);
/***************************************************************************
* Init USB Device
/*
* Init USB device controller
*/
int usb_init(int32_t handle, const struct pci_device_id *ent)
{
void *priv;
struct hci *priv;
int res = 0;
if (bus_index >= USB_MAX_BUS)
return -1;
@@ -154,7 +139,6 @@ int usb_init(int32_t handle, const struct pci_device_id *ent)
usb_hub_reset(bus_index);
/* init low_level USB */
dbg("USB: ");
switch(ent->class)
{
@@ -162,33 +146,41 @@ int usb_init(int32_t handle, const struct pci_device_id *ent)
//res = uhci_usb_lowlevel_init(handle, ent, &priv);
dbg("sorry, no uhci driver available\r\n");
break;
case PCI_CLASS_SERIAL_USB_OHCI:
dbg("initialize ohci interface\r\n");
res = ohci_usb_lowlevel_init(handle, ent, &priv);
dbg("initialize ohci host controller interface\r\n");
res = ohci_usb_lowlevel_init(handle, ent, (void *) &priv);
break;
case PCI_CLASS_SERIAL_USB_EHCI:
dbg("initialize ehci interface\r\n");
res = ehci_usb_lowlevel_init(handle, ent, &priv);
dbg("initialize ehci host controller interface\r\n");
res = ehci_usb_lowlevel_init(handle, ent, (void *) &priv);
break;
default:
res = -1;
break;
default: res = -1; break;
}
if (!res)
{
/* if lowlevel init is OK, scan the bus for devices
* i.e. search HUBs and configure them */
/*
* if lowlevel init is OK, scan the bus for devices
* i.e. search HUBs and configure them
*/
if (setup_packet == NULL)
setup_packet = (void *) driver_mem_alloc(sizeof(struct devrequest));
{
setup_packet = driver_mem_alloc(sizeof(struct devrequest));
if (setup_packet == NULL)
{
usb_started = 0;
return -1; /* out of memoy */
return -1; /* no memory, no USB */
}
}
xprintf("Scanning bus for devices... ");
controller_priv[bus_index] = (struct hci *) priv;
controller_priv[bus_index] = priv;
controller_priv[bus_index]->usbnum = bus_index;
usb_scan_devices(priv);
@@ -323,6 +315,7 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
dev->status = USB_ST_NOT_PROC; /* not yet processed */
ohci_submit_control_msg(dev, pipe, data, size, setup_packet);
break;
case PCI_CLASS_SERIAL_USB_EHCI:
dev->status = USB_ST_NOT_PROC; /* not yet processed */
ehci_submit_control_msg(dev, pipe, data, size, setup_packet);
@@ -340,7 +333,6 @@ int usb_control_msg(struct usb_device *dev, unsigned int pipe,
* Let's wait a while for the timeout to elapse.
* It has no real use, but it keeps the interface happy.
*/
wait(timeout);
return -1;
}
return dev->act_len;
@@ -972,7 +964,7 @@ struct usb_device *usb_alloc_new_device(int bus_index, void *priv)
return dev;
}
#define CONFIG_LEGACY_USB_INIT_SEQ
// #define CONFIG_LEGACY_USB_INIT_SEQ
/*
* By the time we get here, the device has gotten a new device ID
* and is in the default state. We need to identify the thing and
@@ -1007,11 +999,13 @@ int usb_new_device(struct usb_device *dev)
}
#ifdef CONFIG_LEGACY_USB_INIT_SEQ
/* this is the old and known way of initializing devices, it is
/*
* this is the old and known way of initializing devices, it is
* different than what Windows and Linux are doing. Windows and Linux
* both retrieve 64 bytes while reading the device descriptor
* Several USB stick devices report ERR: CTL_TIMEOUT, caused by an
* invalid header while reading 8 bytes as device descriptor. */
* invalid header while reading 8 bytes as device descriptor.
*/
dev->descriptor.bMaxPacketSize0 = 8; /* Start off at 8 bytes */
dev->maxpacketsize = PACKET_SIZE_8;
dev->epmaxpacketin[0] = 8;
@@ -1024,17 +1018,21 @@ int usb_new_device(struct usb_device *dev)
return 1;
}
#else
/* This is a Windows scheme of initialization sequence, with double
/*
* This is a Windows scheme of initialization sequence, with double
* reset of the device (Linux uses the same sequence)
* Some equipment is said to work only with such init sequence; this
* patch is based on the work by Alan Stern:
* http://sourceforge.net/mailarchive/forum.php?
* thread_id=5729457&forum_id=5398
*/
/* send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
/*
* send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
* only 18 bytes long, this will terminate with a short packet. But if
* the maxpacket size is 8 or 16 the device may be waiting to transmit
* some more, or keeps on retransmitting the 8 byte header. */
* some more, or keeps on retransmitting the 8 byte header.
*/
desc = (struct usb_device_descriptor *) tmpbuf;
dev->descriptor.bMaxPacketSize0 = 64; /* Start off at 64 bytes */
/* Default to 64 byte max packet size */
@@ -1062,12 +1060,14 @@ int usb_new_device(struct usb_device *dev)
break;
}
}
if (port < 0)
{
dbg("usb_new_device: cannot locate device's port.\r\n");
driver_mem_free(tmpbuf);
return 1;
}
/* reset the port for the second time */
err = hub_port_reset(dev->parent, port, &portstatus);
if (err < 0)
@@ -1107,7 +1107,8 @@ int usb_new_device(struct usb_device *dev)
driver_mem_free(tmpbuf);
return 1;
}
/* correct le values */
/* correct values */
dev->descriptor.bcdUSB = swpw(dev->descriptor.bcdUSB);
dev->descriptor.idVendor = swpw(dev->descriptor.idVendor);
dev->descriptor.idProduct = swpw(dev->descriptor.idProduct);
@@ -1138,9 +1139,11 @@ int usb_new_device(struct usb_device *dev)
dbg("Manufacturer %s\r\n", dev->mf);
dbg("Product %s\r\n", dev->prod);
dbg("SerialNumber %s\r\n", dev->serial);
/* now prode if the device is a hub */
/* now probe if the device is a hub */
usb_hub_probe(dev, 0);
driver_mem_free(tmpbuf);
return 0;
}
@@ -1186,514 +1189,3 @@ void usb_scan_devices(void *priv)
xprintf("Scan end\r\n");
}
/****************************************************************************
* HUB "Driver"
* Probes device for being a hub and configurate it
*/
//#define USB_HUB_DEBUG
#ifdef USB_HUB_DEBUG
#define dbg_hub(fmt, args...) xprintf(fmt , ##args)
#else
#define dbg_hub(fmt, args...)
#endif
static struct usb_hub_device hub_dev[USB_MAX_BUS][USB_MAX_HUB];
static int usb_hub_index[USB_MAX_BUS];
int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
}
int usb_clear_hub_feature(struct usb_device *dev, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, USB_CNTL_TIMEOUT);
}
int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, USB_CNTL_TIMEOUT);
}
int usb_set_port_feature(struct usb_device *dev, int port, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, USB_CNTL_TIMEOUT);
}
int usb_get_hub_status(struct usb_device *dev, void *data)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
}
int usb_get_port_status(struct usb_device *dev, int port, void *data)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port, data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
}
static void usb_hub_power_on(struct usb_hub_device *hub)
{
int i;
struct usb_device *dev;
dev = hub->pusb_dev;
/* Enable power to the ports */
dbg_hub("enabling power on all ports\r\n");
for (i = 0; i < dev->maxchild; i++)
{
usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
dbg_hub("port %d returns %lX\r\n", i + 1, dev->status);
wait(hub->desc.bPwrOn2PwrGood * 2 * 1000);
}
}
void usb_hub_reset(int bus_index)
{
usb_hub_index[bus_index] = 0;
}
struct usb_hub_device *usb_hub_allocate(void)
{
if (usb_hub_index[bus_index] < USB_MAX_HUB)
return &hub_dev[bus_index][usb_hub_index[bus_index]++];
dbg_hub("ERROR: USB_MAX_HUB (%d) reached\r\n", USB_MAX_HUB);
return NULL;
}
#define MAX_TRIES 5
static inline char *portspeed(int portstatus)
{
if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
return "480 Mb/s";
else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
return "1.5 Mb/s";
else
return "12 Mb/s";
}
static int hub_port_reset(struct usb_device *dev, int port, unsigned short *portstat)
{
int tries;
struct usb_port_status portsts;
unsigned short portstatus, portchange;
dbg_hub("hub_port_reset: resetting port %d...\r\n", port + 1);
for (tries = 0; tries < MAX_TRIES; tries++)
{
usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(10000);
if (usb_get_port_status(dev, port + 1, &portsts) < 0)
{
dbg_hub("get_port_status failed status %lX\r\n", dev->status);
return -1;
}
portstatus = swpw(portsts.wPortStatus);
portchange = swpw(portsts.wPortChange);
dbg_hub("USB %d portstatus 0x%x, change 0x%x, %s\r\n", dev->usbnum, portstatus, portchange, portspeed(portstatus));
dbg_hub("STAT_C_CONNECTION = %d STAT_CONNECTION = %d USB_PORT_STAT_ENABLE = %d\r\n",
(portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
(portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
(portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
if ((portchange & USB_PORT_STAT_C_CONNECTION) || !(portstatus & USB_PORT_STAT_CONNECTION))
return -1;
if (portstatus & USB_PORT_STAT_ENABLE)
break;
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(20000);
}
if (tries == MAX_TRIES)
{
dbg_hub("USB %d, cannot enable port %d after %d retries, disabling port.\r\n", dev->usbnum, port + 1, MAX_TRIES);
dbg_hub("Maybe the USB cable is bad?\r\n");
return -1;
}
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
*portstat = portstatus;
return 0;
}
void usb_hub_port_connect_change(struct usb_device *dev, int port)
{
struct usb_device *usb;
struct usb_port_status portsts;
unsigned short portstatus;
/* Check status */
if (usb_get_port_status(dev, port + 1, &portsts) < 0)
{
dbg_hub("USB %d get_port_status failed\r\n", dev->usbnum);
return;
}
portstatus = swpw(portsts.wPortStatus);
#ifdef USB_DEBUG
{
unsigned short portchange;
portchange = swpw(portsts.wPortChange);
dbg_hub("USB %d, portstatus %x, change %x, %s\r\n", dev->usbnum, portstatus, portchange, portspeed(portstatus));
}
#endif /* USB_DEBUG */
/* Clear the connection change status */
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
/* Disconnect any existing devices under this port */
if (((!(portstatus & USB_PORT_STAT_CONNECTION))
&& (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port]))
{
dbg_hub("USB %d port %i disconnected\r\n", dev->usbnum, port + 1);
usb_disconnect(&dev->children[port]);
/* Return now if nothing is connected */
if (!(portstatus & USB_PORT_STAT_CONNECTION))
return;
}
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(2000);
/* Reset the port */
if (hub_port_reset(dev, port, &portstatus) < 0)
{
dbg_hub("USB %d cannot reset port %i!?\r\n", dev->usbnum, port + 1);
return;
}
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(2000);
/* Allocate a new device struct for it */
usb = usb_alloc_new_device(dev->usbnum, dev->priv_hcd);
if (portstatus & USB_PORT_STAT_HIGH_SPEED)
usb->speed = USB_SPEED_HIGH;
else if (portstatus & USB_PORT_STAT_LOW_SPEED)
usb->speed = USB_SPEED_LOW;
else
usb->speed = USB_SPEED_FULL;
dbg_hub("%s: usb=%p\r\n", __FUNCTION__, usb);
dev->children[port] = usb;
usb->parent = dev;
/* Run it through the hoops (find a driver, etc) */
if (usb_new_device(usb))
{
/* Woops, disable the port */
dbg_hub("USB %d hub: disabling port %d\r\n", dev->usbnum, port + 1);
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
}
#ifdef USB_POLL_HUB
else if (pxCurrentTCB != NULL)
{
#ifdef CONFIG_USB_KEYBOARD
usb_kbd_register(usb);
#endif /* CONFIG_USB_KEYBOARD */
#ifdef CONFIG_USB_MOUSE
usb_mouse_register(usb);
#endif /* CONFIG_USB_MOUSE */
#ifdef CONFIG_USB_STORAGE
usb_stor_register(usb);
#endif /* CONFIG_USB_STORAGE */
}
#endif
}
static void usb_hub_events(struct usb_device *dev)
{
int i;
struct usb_hub_device *hub = dev->hub;
if (hub == NULL)
return;
for (i = 0; i < dev->maxchild; i++)
{
struct usb_port_status portsts;
unsigned short portstatus, portchange;
if (usb_get_port_status(dev, i + 1, &portsts) < 0)
{
dbg_hub("get_port_status failed\r\n");
continue;
}
portstatus = swpw(portsts.wPortStatus);
portchange = swpw(portsts.wPortChange);
dbg_hub("USB %d Port %d Status %X Change %X\r\n", dev->usbnum, i + 1, portstatus, portchange);
if (portchange & USB_PORT_STAT_C_CONNECTION)
{
dbg_hub("USB %d port %d connection change\r\n", dev->usbnum, i + 1);
usb_hub_port_connect_change(dev, i);
}
if (portchange & USB_PORT_STAT_C_ENABLE)
{
dbg_hub("USB %d port %d enable change, status %x\r\n", dev->usbnum, i + 1, portstatus);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
/* EM interference sometimes causes bad shielded USB
* devices to be shutdown by the hub, this hack enables
* them again. Works at least with mouse driver */
if (!(portstatus & USB_PORT_STAT_ENABLE) && (portstatus & USB_PORT_STAT_CONNECTION) && ((dev->children[i])))
{
dbg_hub("USB %d already running port %i disabled by hub (EMI?), re-enabling...\r\n", dev->usbnum, i + 1);
usb_hub_port_connect_change(dev, i);
}
}
if (portstatus & USB_PORT_STAT_SUSPEND)
{
dbg_hub("USB %d port %d suspend change\r\n", dev->usbnum, i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
}
if (portchange & USB_PORT_STAT_C_OVERCURRENT)
{
dbg_hub("USB %d port %d over-current change\r\n", dev->usbnum, i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
usb_hub_power_on(hub);
}
if (portchange & USB_PORT_STAT_C_RESET)
{
dbg_hub("USB %d port %d reset change\r\n", dev->usbnum, i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
}
} /* end for i all ports */
}
#ifdef USB_POLL_HUB
void usb_poll_hub_task(void *pvParameters)
{
int index_bus = 0;
portTickType timeout = configTICK_RATE_HZ/10;
if (pvParameters);
while(1)
{
if (xQueueAltReceive(queue_poll_hub, &index_bus, timeout) == pdPASS)
{
if ((index_bus >= 0) && (index_bus < USB_MAX_BUS) && (controller_priv[index_bus] != NULL))
{
dbg_hub("USB %d event change\r\n", index_bus);
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 0;
#endif
usb_hub_events(&usb_dev[index_bus * USB_MAX_DEVICE]);
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 1;
#endif
}
}
else /* timeout */
{
int i;
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 0;
#endif
for (i = 0; i < USB_MAX_BUS ; i++)
{
if (controller_priv[i] != NULL)
usb_hub_events(&usb_dev[i * USB_MAX_DEVICE]);
}
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 1;
#endif
}
timeout = portMAX_DELAY;
}
}
#endif /* USB_POLL_HUB */
int usb_hub_configure(struct usb_device *dev)
{
unsigned char *buffer, *bitmap;
struct usb_hub_descriptor *descriptor;
int i;
struct usb_hub_device *hub;
/* "allocate" Hub device */
hub = usb_hub_allocate();
dev->hub = hub;
if (hub == NULL)
return -1;
hub->pusb_dev = dev;
buffer = (unsigned char *)driver_mem_alloc(USB_BUFSIZ);
if (buffer == NULL)
{
dbg_hub("usb_hub_configure: malloc failure\r\n");
return -1;
}
/* Get the the hub descriptor */
if (usb_get_hub_descriptor(dev, buffer, 4) < 0)
{
dbg_hub("usb_hub_configure: failed to get hub descriptor, giving up %lX\r\n", dev->status);
driver_mem_free(buffer);
return -1;
}
dbg_hub("bLength:%02X bDescriptorType:%02X bNbrPorts:%02X\r\n", buffer[0], buffer[1], buffer[2]);
descriptor = (struct usb_hub_descriptor *)buffer;
/* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
i = descriptor->bLength;
if (i > USB_BUFSIZ)
{
dbg_hub("usb_hub_configure: failed to get hub descriptor - too long: %d\r\n", descriptor->bLength);
driver_mem_free(buffer);
return -1;
}
if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0)
{
dbg_hub("usb_hub_configure: failed to get hub descriptor 2nd giving up %lX\r\n", dev->status);
driver_mem_free(buffer);
return -1;
}
memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
/* adjust 16bit values */
hub->desc.wHubCharacteristics = swpw(descriptor->wHubCharacteristics);
/* set the bitmap */
bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
/* devices not removable by default */
memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
hub->desc.DeviceRemovable[i] = descriptor->PortPowerCtrlMask[i];
dev->maxchild = descriptor->bNbrPorts;
dbg_hub("USB %d, %d ports detected\r\n", dev->usbnum, dev->maxchild);
if (dev->maxchild >= 10)
dev->maxchild = 10;
switch(hub->desc.wHubCharacteristics & HUB_CHAR_LPSM)
{
case 0x00: dbg_hub("ganged power switching\r\n"); break;
case 0x01: dbg_hub("individual port power switching\r\n"); break;
case 0x02:
case 0x03: dbg_hub("unknown reserved power switching mode\r\n"); break;
}
if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
{
dbg_hub("part of a compound device\r\n");
}
else
{
dbg_hub("standalone hub\r\n");
}
switch(hub->desc.wHubCharacteristics & HUB_CHAR_OCPM)
{
case 0x00: dbg_hub("global over-current protection\r\n"); break;
case 0x08: dbg_hub("individual port over-current protection\r\n"); break;
case 0x10:
case 0x18: dbg_hub("no over-current protection\r\n"); break;
}
dbg_hub("power on to power good time: %dms\r\n", descriptor->bPwrOn2PwrGood * 2);
dbg_hub("hub controller current requirement: %dmA\r\n", descriptor->bHubContrCurrent);
for (i = 0; i < dev->maxchild; i++)
{
dbg_hub("USB %d port %d is%s removable\r\n", dev->usbnum, i + 1, hub->desc.DeviceRemovable[(i + 1) / 8] & (1 << ((i + 1) % 8)) ? " not" : "");
}
if (sizeof(struct usb_hub_status) > USB_BUFSIZ)
{
dbg_hub("usb_hub_configure: failed to get Status - too long: %d\r\n", descriptor->bLength);
driver_mem_free(buffer);
return -1;
}
if (usb_get_hub_status(dev, buffer) < 0)
{
dbg_hub("usb_hub_configure: failed to get Status %lX\r\n", dev->status);
driver_mem_free(buffer);
return -1;
}
#ifdef USB_DEBUG
{
struct usb_hub_status *hubsts;
hubsts = (struct usb_hub_status *)buffer;
dbg_hub("get_hub_status returned status %X, change %X\r\n",
swpw(hubsts->wHubStatus), swpw(hubsts->wHubChange));
dbg_hub("local power source is %s\r\n",
(swpw(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? "lost (inactive)" : "good");
dbg_hub("%sover-current condition exists\r\n",
(swpw(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? "" : "no ");
}
#endif /* USB_DEBUG */
usb_hub_power_on(hub);
#ifdef USB_POLL_HUB
if ((queue_poll_hub == NULL) && (pxCurrentTCB != NULL))
{
queue_poll_hub = xQueueCreate(64, sizeof(int));
if (queue_poll_hub != NULL)
{
/* Create poll/event task */
if (xTaskCreate(usb_poll_hub_task, (void *)"USBHub", configMINIMAL_STACK_SIZE, NULL, 16, NULL) != pdPASS)
{
vQueueDelete(queue_poll_hub);
queue_poll_hub = NULL;
}
}
vTaskDelay(configTICK_RATE_HZ);
}
if (queue_poll_hub == NULL)
#endif
usb_hub_events(dev);
driver_mem_free(buffer);
return 0;
}
int usb_hub_probe(struct usb_device *dev, int ifnum)
{
struct usb_interface_descriptor *iface;
struct usb_endpoint_descriptor *ep;
int ret;
iface = &dev->config.if_desc[ifnum];
/* Is it a hub? */
if (iface->bInterfaceClass != USB_CLASS_HUB)
{
dbg("iface->bInterfaceClass != USB_CLASS_HUB (%d), %d instead\r\n", USB_CLASS_HUB, iface->bInterfaceClass);
return 0;
}
/* Some hubs have a subclass of 1, which AFAICT according to the */
/* specs is not defined, but it works */
if ((iface->bInterfaceSubClass != 0) && (iface->bInterfaceSubClass != 1))
return 0;
/* Multiple endpoints? What kind of mutant ninja-hub is this? */
if (iface->bNumEndpoints != 1)
return 0;
ep = &iface->ep_desc[0];
/* Output endpoint? Curiousier and curiousier.. */
if (!(ep->bEndpointAddress & USB_DIR_IN))
return 0;
/* If it's not an interrupt endpoint, we'd better punt! */
if ((ep->bmAttributes & 3) != 3)
return 0;
/* We found a hub */
dbg_hub("USB %d hub found\r\n", dev->usbnum);
ret = usb_hub_configure(dev);
return ret;
}

542
BaS_gcc/usb/usb_hub.c Normal file
View File

@@ -0,0 +1,542 @@
/*
* HUB "Driver"
* Probes device for being a hub and configure it
*/
#include <stdint.h>
#include "bas_string.h"
#include "bas_printf.h"
#include "util.h" /* for byte swap funcs */
#include "wait.h"
#include <stdarg.h>
#include "usb.h"
#include "usb_hub.h"
#define DEBUG_HUB
#ifdef DEBUG_HUB
#define dbg(format, arg...) do { xprintf("DEBUG: %s(): " format, __FUNCTION__, ##arg); } while (0)
#else
#define dbg(format, arg...) do { ; } while (0)
#endif /* DEBUG_HUB */
static struct usb_hub_device hub_dev[USB_MAX_BUS][USB_MAX_HUB];
static int usb_hub_index[USB_MAX_BUS];
int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
}
int usb_clear_hub_feature(struct usb_device *dev, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, USB_CNTL_TIMEOUT);
}
int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, USB_CNTL_TIMEOUT);
}
int usb_set_port_feature(struct usb_device *dev, int port, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, USB_CNTL_TIMEOUT);
}
int usb_get_hub_status(struct usb_device *dev, void *data)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
}
int usb_get_port_status(struct usb_device *dev, int port, void *data)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port, data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
}
static void usb_hub_power_on(struct usb_hub_device *hub)
{
int i;
struct usb_device *dev;
dev = hub->pusb_dev;
/* Enable power to the ports */
dbg("enabling power on all ports\r\n");
for (i = 0; i < dev->maxchild; i++)
{
usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
dbg("port %d returns %lX\r\n", i + 1, dev->status);
wait(hub->desc.bPwrOn2PwrGood * 2 * 1000);
}
}
void usb_hub_reset(int bus_index)
{
usb_hub_index[bus_index] = 0;
}
struct usb_hub_device *usb_hub_allocate(void)
{
if (usb_hub_index[bus_index] < USB_MAX_HUB)
return &hub_dev[bus_index][usb_hub_index[bus_index]++];
dbg("ERROR: USB_MAX_HUB (%d) reached\r\n", USB_MAX_HUB);
return NULL;
}
#define MAX_TRIES 5
static inline char *portspeed(int portstatus)
{
if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
return "480 Mb/s";
else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
return "1.5 Mb/s";
else
return "12 Mb/s";
}
int hub_port_reset(struct usb_device *dev, int port, unsigned short *portstat)
{
int tries;
struct usb_port_status portsts;
unsigned short portstatus, portchange;
dbg("hub_port_reset: resetting port %d...\r\n", port + 1);
for (tries = 0; tries < MAX_TRIES; tries++)
{
usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(10000);
if (usb_get_port_status(dev, port + 1, &portsts) < 0)
{
dbg("get_port_status failed status %lX\r\n", dev->status);
return -1;
}
portstatus = swpw(portsts.wPortStatus);
portchange = swpw(portsts.wPortChange);
dbg("USB %d portstatus 0x%x, change 0x%x, %s\r\n", dev->usbnum, portstatus, portchange, portspeed(portstatus));
dbg("STAT_C_CONNECTION = %d STAT_CONNECTION = %d USB_PORT_STAT_ENABLE = %d\r\n",
(portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
(portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
(portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
if ((portchange & USB_PORT_STAT_C_CONNECTION) || !(portstatus & USB_PORT_STAT_CONNECTION))
return -1;
if (portstatus & USB_PORT_STAT_ENABLE)
break;
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(20000);
}
if (tries == MAX_TRIES)
{
dbg("USB %d, cannot enable port %d after %d retries, disabling port.\r\n", dev->usbnum, port + 1, MAX_TRIES);
dbg("Maybe the USB cable is bad?\r\n");
return -1;
}
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
*portstat = portstatus;
return 0;
}
void usb_hub_port_connect_change(struct usb_device *dev, int port)
{
struct usb_device *usb;
struct usb_port_status portsts;
unsigned short portstatus;
/* Check status */
if (usb_get_port_status(dev, port + 1, &portsts) < 0)
{
dbg("USB %d get_port_status failed\r\n", dev->usbnum);
return;
}
portstatus = swpw(portsts.wPortStatus);
#ifdef USB_DEBUG
{
unsigned short portchange;
portchange = swpw(portsts.wPortChange);
dbg("USB %d, portstatus %x, change %x, %s\r\n", dev->usbnum, portstatus, portchange, portspeed(portstatus));
}
#endif /* USB_DEBUG */
/* Clear the connection change status */
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
/* Disconnect any existing devices under this port */
if (((!(portstatus & USB_PORT_STAT_CONNECTION))
&& (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port]))
{
dbg("USB %d port %i disconnected\r\n", dev->usbnum, port + 1);
usb_disconnect(&dev->children[port]);
/* Return now if nothing is connected */
if (!(portstatus & USB_PORT_STAT_CONNECTION))
return;
}
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(2000);
/* Reset the port */
if (hub_port_reset(dev, port, &portstatus) < 0)
{
dbg("USB %d cannot reset port %i!?\r\n", dev->usbnum, port + 1);
return;
}
#ifdef USB_POLL_HUB
if (pxCurrentTCB != NULL)
vTaskDelay((200*configTICK_RATE_HZ)/1000);
else
#endif
wait(2000);
/* Allocate a new device struct for it */
usb = usb_alloc_new_device(dev->usbnum, dev->priv_hcd);
if (portstatus & USB_PORT_STAT_HIGH_SPEED)
usb->speed = USB_SPEED_HIGH;
else if (portstatus & USB_PORT_STAT_LOW_SPEED)
usb->speed = USB_SPEED_LOW;
else
usb->speed = USB_SPEED_FULL;
dbg("usb=%p\r\n", usb);
dev->children[port] = usb;
usb->parent = dev;
/* Run it through the hoops (find a driver, etc) */
if (usb_new_device(usb))
{
/* Woops, disable the port */
dbg("USB %d hub: disabling port %d\r\n", dev->usbnum, port + 1);
usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
}
#ifdef USB_POLL_HUB
else if (pxCurrentTCB != NULL)
{
#ifdef CONFIG_USB_KEYBOARD
usb_kbd_register(usb);
#endif /* CONFIG_USB_KEYBOARD */
#ifdef CONFIG_USB_MOUSE
usb_mouse_register(usb);
#endif /* CONFIG_USB_MOUSE */
#ifdef CONFIG_USB_STORAGE
usb_stor_register(usb);
#endif /* CONFIG_USB_STORAGE */
}
#endif
}
static void usb_hub_events(struct usb_device *dev)
{
int i;
struct usb_hub_device *hub = dev->hub;
if (hub == NULL)
return;
for (i = 0; i < dev->maxchild; i++)
{
struct usb_port_status portsts;
unsigned short portstatus, portchange;
if (usb_get_port_status(dev, i + 1, &portsts) < 0)
{
dbg("get_port_status failed\r\n");
continue;
}
portstatus = swpw(portsts.wPortStatus);
portchange = swpw(portsts.wPortChange);
dbg("USB %d Port %d Status %X Change %X\r\n", dev->usbnum, i + 1, portstatus, portchange);
if (portchange & USB_PORT_STAT_C_CONNECTION)
{
dbg("USB %d port %d connection change\r\n", dev->usbnum, i + 1);
usb_hub_port_connect_change(dev, i);
}
if (portchange & USB_PORT_STAT_C_ENABLE)
{
dbg("USB %d port %d enable change, status %x\r\n", dev->usbnum, i + 1, portstatus);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
/* EM interference sometimes causes bad shielded USB
* devices to be shutdown by the hub, this hack enables
* them again. Works at least with mouse driver */
if (!(portstatus & USB_PORT_STAT_ENABLE) && (portstatus & USB_PORT_STAT_CONNECTION) && ((dev->children[i])))
{
dbg("USB %d already running port %i disabled by hub (EMI?), re-enabling...\r\n", dev->usbnum, i + 1);
usb_hub_port_connect_change(dev, i);
}
}
if (portstatus & USB_PORT_STAT_SUSPEND)
{
dbg("USB %d port %d suspend change\r\n", dev->usbnum, i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
}
if (portchange & USB_PORT_STAT_C_OVERCURRENT)
{
dbg("USB %d port %d over-current change\r\n", dev->usbnum, i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
usb_hub_power_on(hub);
}
if (portchange & USB_PORT_STAT_C_RESET)
{
dbg("USB %d port %d reset change\r\n", dev->usbnum, i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
}
} /* end for i all ports */
}
#ifdef USB_POLL_HUB
void usb_poll_hub_task(void *pvParameters)
{
int index_bus = 0;
portTickType timeout = configTICK_RATE_HZ/10;
if (pvParameters);
while(1)
{
if (xQueueAltReceive(queue_poll_hub, &index_bus, timeout) == pdPASS)
{
if ((index_bus >= 0) && (index_bus < USB_MAX_BUS) && (controller_priv[index_bus] != NULL))
{
dbg("USB %d event change\r\n", index_bus);
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 0;
#endif
usb_hub_events(&usb_dev[index_bus * USB_MAX_DEVICE]);
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 1;
#endif
}
}
else /* timeout */
{
int i;
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 0;
#endif
for (i = 0; i < USB_MAX_BUS ; i++)
{
if (controller_priv[i] != NULL)
usb_hub_events(&usb_dev[i * USB_MAX_DEVICE]);
}
#ifdef CONFIG_USB_INTERRUPT_POLLING
*vblsem = 1;
#endif
}
timeout = portMAX_DELAY;
}
}
#endif /* USB_POLL_HUB */
int usb_hub_configure(struct usb_device *dev)
{
unsigned char *buffer, *bitmap;
struct usb_hub_descriptor *descriptor;
int i;
struct usb_hub_device *hub;
/* "allocate" Hub device */
hub = usb_hub_allocate();
dev->hub = hub;
if (hub == NULL)
return -1;
hub->pusb_dev = dev;
buffer = (unsigned char *) driver_mem_alloc(USB_BUFSIZ);
if (buffer == NULL)
{
dbg("driver_mem_alloc() failure\r\n");
return -1;
}
/* Get the the hub descriptor */
if (usb_get_hub_descriptor(dev, buffer, 4) < 0)
{
dbg("failed to get hub descriptor, giving up %lX\r\n", dev->status);
driver_mem_free(buffer);
return -1;
}
dbg("bLength:%02X bDescriptorType:%02X bNbrPorts:%02X\r\n", buffer[0], buffer[1], buffer[2]);
descriptor = (struct usb_hub_descriptor *)buffer;
/* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
i = descriptor->bLength;
if (i > USB_BUFSIZ)
{
dbg("failed to get hub descriptor - too long: %d\r\n", descriptor->bLength);
driver_mem_free(buffer);
return -1;
}
if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0)
{
dbg("failed to get hub descriptor 2nd giving up %lX\r\n", dev->status);
driver_mem_free(buffer);
return -1;
}
memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
/* adjust 16bit values */
hub->desc.wHubCharacteristics = swpw(descriptor->wHubCharacteristics);
/* set the bitmap */
bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
/* devices not removable by default */
memset(bitmap, 0xff, (USB_MAXCHILDREN + 1 + 7) / 8);
bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
memset(bitmap, 0xff, (USB_MAXCHILDREN + 1 + 7) / 8); /* PowerMask = 1B */
for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7) / 8); i++)
hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
hub->desc.DeviceRemovable[i] = descriptor->PortPowerCtrlMask[i];
dev->maxchild = descriptor->bNbrPorts;
dbg("USB %d, %d ports detected\r\n", dev->usbnum, dev->maxchild);
if (dev->maxchild >= 10)
dev->maxchild = 10;
switch(hub->desc.wHubCharacteristics & HUB_CHAR_LPSM)
{
case 0x00: dbg("ganged power switching\r\n"); break;
case 0x01: dbg("individual port power switching\r\n"); break;
case 0x02:
case 0x03: dbg("unknown reserved power switching mode\r\n"); break;
}
if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
{
dbg("part of a compound device\r\n");
}
else
{
dbg("standalone hub\r\n");
}
switch(hub->desc.wHubCharacteristics & HUB_CHAR_OCPM)
{
case 0x00: dbg("global over-current protection\r\n"); break;
case 0x08: dbg("individual port over-current protection\r\n"); break;
case 0x10:
case 0x18: dbg("no over-current protection\r\n"); break;
}
dbg("power on to power good time: %dms\r\n", descriptor->bPwrOn2PwrGood * 2);
dbg("hub controller current requirement: %dmA\r\n", descriptor->bHubContrCurrent);
for (i = 0; i < dev->maxchild; i++)
{
dbg("USB %d port %d is%s removable\r\n", dev->usbnum, i + 1, hub->desc.DeviceRemovable[(i + 1) / 8] & (1 << ((i + 1) % 8)) ? " not" : "");
}
if (sizeof(struct usb_hub_status) > USB_BUFSIZ)
{
dbg("usb_hub_configure: failed to get Status - too long: %d\r\n", descriptor->bLength);
driver_mem_free(buffer);
return -1;
}
if (usb_get_hub_status(dev, buffer) < 0)
{
dbg("usb_hub_configure: failed to get Status %lX\r\n", dev->status);
driver_mem_free(buffer);
return -1;
}
#ifdef USB_DEBUG
{
struct usb_hub_status *hubsts;
hubsts = (struct usb_hub_status *)buffer;
dbg("get_hub_status returned status %X, change %X\r\n",
swpw(hubsts->wHubStatus), swpw(hubsts->wHubChange));
dbg("local power source is %s\r\n",
(swpw(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? "lost (inactive)" : "good");
dbg("%sover-current condition exists\r\n",
(swpw(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? "" : "no ");
}
#endif /* USB_DEBUG */
usb_hub_power_on(hub);
#ifdef USB_POLL_HUB
if ((queue_poll_hub == NULL) && (pxCurrentTCB != NULL))
{
queue_poll_hub = xQueueCreate(64, sizeof(int));
if (queue_poll_hub != NULL)
{
/* Create poll/event task */
if (xTaskCreate(usb_poll_hub_task, (void *)"USBHub", configMINIMAL_STACK_SIZE, NULL, 16, NULL) != pdPASS)
{
vQueueDelete(queue_poll_hub);
queue_poll_hub = NULL;
}
}
vTaskDelay(configTICK_RATE_HZ);
}
if (queue_poll_hub == NULL)
#endif
usb_hub_events(dev);
driver_mem_free(buffer);
return 0;
}
int usb_hub_probe(struct usb_device *dev, int ifnum)
{
struct usb_interface_descriptor *iface;
struct usb_endpoint_descriptor *ep;
int ret;
iface = &dev->config.if_desc[ifnum];
/* Is it a hub? */
if (iface->bInterfaceClass != USB_CLASS_HUB)
{
dbg("iface->bInterfaceClass != USB_CLASS_HUB (%d), %d instead\r\n", USB_CLASS_HUB, iface->bInterfaceClass);
return 0;
}
/* Some hubs have a subclass of 1, which AFAICT according to the */
/* specs is not defined, but it works */
if ((iface->bInterfaceSubClass != 0) && (iface->bInterfaceSubClass != 1))
return 0;
/* Multiple endpoints? What kind of mutant ninja-hub is this? */
if (iface->bNumEndpoints != 1)
return 0;
ep = &iface->ep_desc[0];
/* Output endpoint? Curiousier and curiousier.. */
if (!(ep->bEndpointAddress & USB_DIR_IN))
return 0;
/* If it's not an interrupt endpoint, we'd better punt! */
if ((ep->bmAttributes & 3) != 3)
return 0;
/* We found a hub */
dbg("USB %d hub found\r\n", dev->usbnum);
ret = usb_hub_configure(dev);
return ret;
}

View File

@@ -99,7 +99,7 @@ int drv_usb_mouse_init(void)
if (dev == NULL)
break;
xprintf("USB mouse detected. Trying to register it\r\n");
xprintf("Try to register usb device %d,%d as mouse\r\n", i, j);
if (usb_mouse_register(dev) > 0)
return 1;
}