uhci.c

来自「这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统」· C语言 代码 · 共 2,164 行 · 第 1/5 页

C
2,164
字号
/**
 * uhci.c - USB driver stack project for Windows NT 4.0
 *
 * Copyright (c) 2002-2004 Zhiming  mypublic99@yahoo.com
 *
 * This program/include file is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program/include file is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program (in the main directory of the distribution, the file
 * COPYING); if not, write to the Free Software Foundation,Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "usbdriver.h"

//----------------------------------------------------------
// uhci routines
//#define DEMO

#ifdef INCLUDE_EHCI

#define rh_port1_status rh_port_status[ 1 ]
#define rh_port2_status rh_port_status[ 2 ]

extern PDEVICE_OBJECT ehci_probe(PDRIVER_OBJECT drvr_obj, PUNICODE_STRING reg_path, PUSB_DEV_MANAGER dev_mgr);

#endif

#define DEFAULT_ENDP( enDP ) \
( enDP->flags & USB_ENDP_FLAG_DEFAULT_ENDP )

#define dev_from_endp( enDP ) \
( DEFAULT_ENDP( enDP )\
  ? ( ( PUSB_DEV )( enDP )->pusb_if )\
  : ( ( enDP )->pusb_if->pusb_config->pusb_dev ) )

#define endp_state( enDP ) ( ( enDP )->flags & USB_ENDP_FLAG_STAT_MASK )

#define endp_num( enDP ) \
( DEFAULT_ENDP( enDP )\
  ? 0 \
  : ( ( enDP )->pusb_endp_desc->bEndpointAddress & 0x0f ) )

#define endp_dir( enDP ) \
( DEFAULT_ENDP( enDP )\
  ? 0L\
  : ( ( enDP )->pusb_endp_desc->bEndpointAddress & USB_DIR_IN ) )

#define dev_set_state( pdEV, staTE ) \
( pdEV->flags = ( ( pdEV )->flags & ( ~USB_DEV_STATE_MASK ) ) | ( staTE ) )

#define endp_max_packet_size( enDP ) \
( DEFAULT_ENDP( enDP )\
  ? ( ( ( PUSB_DEV )enDP->pusb_if )->pusb_dev_desc ? \
	  ( ( PUSB_DEV )enDP->pusb_if )->pusb_dev_desc->bMaxPacketSize0\
	  : 8 )\
  : enDP->pusb_endp_desc->wMaxPacketSize )


#if 0
/* WTF?! */
#define release_adapter( padapTER ) \
{\
    ( ( padapTER ) ); \
}
#else
#define release_adapter( padapTER ) (void)(padapTER)
#endif

#define get_int_idx( _urb, _idx ) \
{\
	UCHAR interVAL;\
	interVAL = ( UCHAR )( ( _urb )->pipe >> 24 );\
	for( _idx = 1; _idx < 9; _idx++ )\
	{\
		interVAL >>= 1;\
		if( !interVAL )\
			break;\
	}\
	_idx --;\
}

#define uhci_insert_urb_to_schedule( uHCI, pURB, rET ) \
{\
	SYNC_PARAM sync_param;\
	sync_param.uhci = uHCI;\
	sync_param.context = pURB;\
\
	rET = KeSynchronizeExecution( uHCI->pdev_ext->uhci_int, uhci_sync_insert_urb_schedule, &sync_param );\
}

//declarations
typedef struct
{
    PUHCI_DEV uhci;
    PVOID context;
    ULONG ret;
} SYNC_PARAM, *PSYNC_PARAM;

PDEVICE_OBJECT
uhci_alloc(PDRIVER_OBJECT drvr_obj, PUNICODE_STRING reg_path, ULONG bus_addr, PUSB_DEV_MANAGER dev_mgr);

BOOLEAN uhci_init_schedule(PUHCI_DEV uhci, PADAPTER_OBJECT padapter);

BOOLEAN uhci_release(PDEVICE_OBJECT pdev);

static VOID uhci_stop(PUHCI_DEV uhci);

BOOLEAN uhci_destroy_schedule(PUHCI_DEV uhci);

BOOLEAN NTAPI uhci_sync_insert_urb_schedule(PVOID context);

VOID uhci_init_hcd_interface(PUHCI_DEV uhci);

NTSTATUS uhci_rh_submit_urb(PUSB_DEV rh, PURB purb);

NTSTATUS uhci_dispatch_irp(IN PDEVICE_OBJECT DeviceObject, IN PIRP irp);

extern VOID rh_timer_svc_reset_port_completion(PUSB_DEV dev, PVOID context);

extern VOID rh_timer_svc_int_completion(PUSB_DEV dev, PVOID context);

ULONG debug_level = DBGLVL_MINIMUM;//DBGLVL_MAXIMUM;
PDRIVER_OBJECT usb_driver_obj = NULL;
extern USB_DEV_MANAGER g_dev_mgr;

//pending endpoint pool funcs
VOID
uhci_wait_ms(PUHCI_DEV uhci, LONG ms)
{
    LARGE_INTEGER lms;
    if (ms <= 0)
        return;

    lms.QuadPart = -10 * ms;
    KeSetTimer(&uhci->reset_timer, lms, NULL);

    KeWaitForSingleObject(&uhci->reset_timer, Executive, KernelMode, FALSE, NULL);

    return;
}

BOOLEAN
init_pending_endp_pool(PUHCI_PENDING_ENDP_POOL pool)
{
    int i;
    if (pool == NULL)
        return FALSE;

    pool->pending_endp_array =
        usb_alloc_mem(NonPagedPool, sizeof(UHCI_PENDING_ENDP) * UHCI_MAX_PENDING_ENDPS);
    InitializeListHead(&pool->free_que);
    pool->free_count = 0;
    pool->total_count = UHCI_MAX_PENDING_ENDPS;
    KeInitializeSpinLock(&pool->pool_lock);

    for(i = 0; i < MAX_TIMER_SVCS; i++)
    {
        free_pending_endp(pool, &pool->pending_endp_array[i]);
    }

    return TRUE;

}

BOOLEAN
free_pending_endp(PUHCI_PENDING_ENDP_POOL pool, PUHCI_PENDING_ENDP pending_endp)
{
    if (pool == NULL || pending_endp == NULL)
    {
        return FALSE;
    }

    RtlZeroMemory(pending_endp, sizeof(UHCI_PENDING_ENDP));
    InsertTailList(&pool->free_que, (PLIST_ENTRY) & pending_endp->endp_link);
    pool->free_count++;

    return TRUE;
}

PUHCI_PENDING_ENDP
alloc_pending_endp(PUHCI_PENDING_ENDP_POOL pool, LONG count)
{
    PUHCI_PENDING_ENDP new;
    if (pool == NULL || count != 1)
        return NULL;

    if (pool->free_count <= 0)
        return NULL;

    new = (PUHCI_PENDING_ENDP) RemoveHeadList(&pool->free_que);
    pool->free_count--;
    return new;
}

BOOLEAN
destroy_pending_endp_pool(PUHCI_PENDING_ENDP_POOL pool)
{
    if (pool == NULL)
        return FALSE;

    InitializeListHead(&pool->free_que);
    pool->free_count = pool->total_count = 0;
    usb_free_mem(pool->pending_endp_array);
    pool->pending_endp_array = NULL;

    return TRUE;

}


//end of pending endpoint pool funcs

static void
uhci_fill_td(PUHCI_TD td, ULONG status, ULONG info, ULONG buffer)
{
    td->status = status;
    td->info = info;
    td->buffer = buffer;
}

BOOLEAN
uhci_insert_td_fl(PUHCI_TD prev_td, PUHCI_TD ptd)
{
    PLIST_ENTRY temp_entry;

    if (prev_td == NULL || ptd == NULL)
        return FALSE;

    temp_entry = &prev_td->ptde->hori_link;

    ptd->link = (struct_ptr(temp_entry, TD_EXTENSION, hori_link))->ptd->phy_addr;
    prev_td->link = ptd->phy_addr;

    InsertHeadList(&prev_td->ptde->hori_link, &ptd->ptde->hori_link);
    return TRUE;
}

BOOLEAN
uhci_remove_td_fl(PUHCI_TD ptd)
{
    PUHCI_TD prev_td;

    if (ptd == NULL)
        return FALSE;

    prev_td = (struct_ptr(ptd->ptde->hori_link.Blink, TD_EXTENSION, hori_link))->ptd;
    prev_td->link = ptd->link;
    ptd->link = UHCI_PTR_TERM;

    RemoveEntryList(&ptd->ptde->hori_link);

    return FALSE;
}

BOOLEAN
uhci_insert_qh_fl(PVOID prev_item, PUHCI_QH pqh)
{
    //only horizontal link allowed
    PUHCI_QH pprev_qh;
    PUHCI_TD pprev_td;
    PLIST_ENTRY temp_entry;

    if (prev_item == NULL || pqh == NULL)
        return FALSE;

    if ((((PUHCI_TD) prev_item)->ptde->flags & UHCI_ITEM_FLAG_TYPE) == UHCI_ITEM_FLAG_QH)
    {
        pprev_qh = (PUHCI_QH) prev_item;
        temp_entry = pprev_qh->pqhe->hori_link.Flink;
        pqh->link = (struct_ptr(temp_entry, TD_EXTENSION, hori_link))->ptd->phy_addr;
        pprev_qh->link = pqh->phy_addr;

        InsertHeadList(&pprev_qh->pqhe->hori_link, &pqh->pqhe->hori_link);
    }
    else
    {
        pprev_td = ((PUHCI_TD) prev_item);

        temp_entry = pprev_td->ptde->hori_link.Flink;
        pprev_td->link = pqh->phy_addr;
        pqh->link = (struct_ptr(temp_entry, TD_EXTENSION, hori_link))->ptd->phy_addr;

        InsertHeadList(&pprev_td->ptde->hori_link, &pqh->pqhe->hori_link);
    }

    return FALSE;
}

BOOLEAN
uhci_remove_qh_fl(PUHCI_QH pqh)
{
    PVOID prev_item;
    PUHCI_QH pprevqh;
    PUHCI_TD pprevtd;

    if (pqh == NULL)
        return FALSE;

    prev_item = (struct_ptr(pqh->pqhe->hori_link.Blink, TD_EXTENSION, hori_link))->ptd;

    if ((((PUHCI_TD) prev_item)->ptde->flags & UHCI_ITEM_FLAG_TYPE) == UHCI_ITEM_FLAG_QH)
    {
        pprevqh = (PUHCI_QH) prev_item;
        pprevqh->link = pqh->link;
    }
    else
    {
        pprevtd = ((PUHCI_TD) prev_item);
        pprevtd->link = pqh->link;
    }

    RemoveEntryList(&pqh->pqhe->hori_link);

    pqh->link = UHCI_PTR_TERM;
    pqh->pqhe->hori_link.Flink = pqh->pqhe->hori_link.Blink = NULL;

    return TRUE;
}

BOOLEAN
uhci_init_frame_list(PUHCI_DEV uhci, PADAPTER_OBJECT padapter)
{
    int i;
    if (uhci == NULL || padapter == NULL)
        return FALSE;

    //note: frame_list_lock will be connected to interrupt
    KeInitializeSpinLock(&uhci->frame_list_lock);

    uhci->io_buf = HalAllocateCommonBuffer(padapter, 4096, &uhci->io_buf_logic_addr, FALSE);

    if (uhci->io_buf == NULL)
        return FALSE;

    uhci->frame_list =
        HalAllocateCommonBuffer(padapter,
                                sizeof(ULONG) * UHCI_MAX_FRAMES, &uhci->frame_list_logic_addr, FALSE);

    if (uhci->frame_list == NULL)
        return FALSE;

    RtlZeroMemory(uhci->frame_list, sizeof(ULONG) * UHCI_MAX_FRAMES);

    uhci->frame_list_cpu = usb_alloc_mem(NonPagedPool, sizeof(FRAME_LIST_CPU_ENTRY) * UHCI_MAX_FRAMES);

    if (uhci->frame_list_cpu == NULL)
        return FALSE;

    for(i = 0; i < UHCI_MAX_FRAMES; i++)
        InitializeListHead(&uhci->frame_list_cpu[i].td_link);

    uhci->frame_bw = usb_alloc_mem(NonPagedPool, sizeof(LONG) * UHCI_MAX_FRAMES);

    if (uhci->frame_bw == NULL)
        return FALSE;

    for(i = 0; i < UHCI_MAX_FRAMES; i++)
    {
        uhci->frame_bw[i] = FRAME_TIME_MAX_USECS_ALLOC;
    }
    uhci->fsbr_cnt = 0;

    return TRUE;

}

BOOLEAN
uhci_destroy_frame_list(PUHCI_DEV uhci)
{
    if (uhci == NULL)
        return FALSE;

    if (uhci->frame_list)
        HalFreeCommonBuffer(uhci->pdev_ext->padapter,
                            sizeof(ULONG) * UHCI_MAX_FRAMES,
                            uhci->frame_list_logic_addr, uhci->frame_list, FALSE);

    uhci->frame_list = NULL;
    uhci->frame_list_logic_addr.LowPart = 0;
    uhci->frame_list_logic_addr.HighPart = 0;

    if (uhci->frame_list_cpu)
        usb_free_mem(uhci->frame_list_cpu);

    uhci->frame_list_cpu = NULL;

    if (uhci->frame_bw)
        usb_free_mem(uhci->frame_bw);

    uhci->frame_bw = NULL;

    return TRUE;
}

PDEVICE_OBJECT
uhci_create_device(PDRIVER_OBJECT drvr_obj, PUSB_DEV_MANAGER dev_mgr)
{
    NTSTATUS status;
    PDEVICE_OBJECT pdev;
    PDEVICE_EXTENSION pdev_ext;

    UNICODE_STRING dev_name;
    UNICODE_STRING symb_name;

    STRING string, another_string;
    CHAR str_dev_name[64], str_symb_name[64];
    UCHAR hcd_id;

    if (drvr_obj == NULL)
        return NULL;

    ASSERT(dev_mgr != NULL);

    //note: hcd count wont increment till the hcd is registered in dev_mgr  
    sprintf(str_dev_name, "%s%d", UHCI_DEVICE_NAME, dev_mgr->hcd_count);
    sprintf(str_symb_name, "%s%d", DOS_DEVICE_NAME, dev_mgr->hcd_count);

    RtlInitString(&string, str_dev_name);
    RtlAnsiStringToUnicodeString(&dev_name, &string, TRUE);

    pdev = NULL;
    status = IoCreateDevice(drvr_obj,
                            sizeof(DEVICE_EXTENSION) + sizeof(UHCI_DEV),
                            &dev_name, FILE_UHCI_DEV_TYPE, 0, FALSE, &pdev);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?