?? ohci-s3c2410.c
字號:
?
/*
* OHCI HCD (Host Controller Driver) for USB
該程序主要完成s3c2410 USB 主機控制器的初始化工作,包括電源,時鐘等
*/
//需要用到的頭文件
#include <asm/hardware.h>
#include <asm/mach-types.h>
#include <asm/hardware/clock.h>
#include <asm/arch/usb-control.h>
//定義主機控制器的有效端口數
#define valid_port(idx) ((idx) == 1 || (idx) == 2)
/* 主機控制器的時鐘,在clock.h中有它的定義 */
static struct clk *clk;
/* 預定義,s3c2410_hcd_oc 主機端口的過流保護函數*/
static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);
/* 該函數返回總線上的當前的平臺信息
該函數的返回結構: (具體的定義在usb_control.h中)
struct s3c2410_hcd_info{
struct usb_hcd *hcd;
struct s3c2410_hcd_port port[2];
void (*power_control)(int port,int to);
void (*enable_oc)(struct s3c2410_hcd_info *,int on);
void (*report_oc)(struct s3c2410_hcd_info *,int ports);
};
*/
struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
{
return hcd->self.controller->platform_data;
}
/* s3c2410_start_hc 該函數啟動主機控制器,主要是啟動時鐘,對打開對端口的過流保護
*/
static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
{
struct s3c2410_hcd_info *info = dev->dev.platform_data;
dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
clk_enable(clk);
if (info != NULL) {
info->hcd = hcd;
info->report_oc = s3c2410_hcd_oc;
if (info->enable_oc != NULL) {
(info->enable_oc)(info, 1);
}
}
}
/* s3c2410_stop_hc 該函數停止主機控制器,關閉時鐘
*/
static void s3c2410_stop_hc(struct platform_device *dev)
{
struct s3c2410_hcd_info *info = dev->dev.platform_data;
dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");
if (info != NULL) {
info->report_oc = NULL;
info->hcd = NULL;
if (info->enable_oc != NULL) {
(info->enable_oc)(info, 0);
}
}
clk_disable(clk);
}
/* ohci_s3c2410_hub_status_data 該函數是集線器的狀態函數,
掃描集線器的兩個下行端口,檢測是否連接USB設備,若有返回1,無,返回0
*/
static int
ohci_s3c2410_hub_status_data (struct usb_hcd *hcd, char *buf)
{
struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
struct s3c2410_hcd_port *port;
int orig;
int portno;
orig = ohci_hub_status_data (hcd, buf);
if (info == NULL)
return orig;
port = &info->port[0];
/*掃描兩個端口*/
for (portno = 0; portno < 2; port++, portno++) {
if (port->oc_changed == 1 &&
port->flags & S3C_HCDFLG_USED) {
dev_dbg(hcd->self.controller,
"oc change on port %d\n", portno);
if (orig < 1)
orig = 1;
buf[0] |= 1<<(portno+1);
}
}
return orig;
}
/* s3c2410_usb_set_power 該函數實現對端口的電源配置
*/
static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
int port, int to)
{
if (info == NULL)
return;
if (info->power_control != NULL) {
info->port[port-1].power = to;
(info->power_control)(port, to);
}
}
/* ohci_s3c2410_hub_control 該函數是集線器的控制函數
處理一些對 集線器的控制請求
具體參數:
typeReq 控制請求類別
wValue 請求值
*/
static int ohci_s3c2410_hub_control (
struct usb_hcd *hcd,
u16 typeReq,
u16 wValue,
u16 wIndex,
char *buf,
u16 wLength)
{
struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
struct usb_hub_descriptor *desc;
int ret = -EINVAL;
u32 *data = (u32 *)buf;
dev_dbg(hcd->self.controller,
"s3c2410_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
hcd, typeReq, wValue, wIndex, buf, wLength);
if (info == NULL) {
ret = ohci_hub_control(hcd, typeReq, wValue,
wIndex, buf, wLength);
goto out;
}
/* 檢測HUB的請求類別,并且處理相關請求
*/
switch (typeReq) {
/*集線器啟用端口的請求 */
case SetPortFeature:
if (wValue == USB_PORT_FEAT_POWER) {
dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
s3c2410_usb_set_power(info, wIndex, 1);
goto out;
}
break;
/* 消除端口的請求*/
case ClearPortFeature:
/*具體的請求值*/
switch (wValue) {
case USB_PORT_FEAT_C_OVER_CURRENT:
dev_dbg(hcd->self.controller,
"ClearPortFeature: C_OVER_CURRENT\n");
if (valid_port(wIndex)) {
info->port[wIndex-1].oc_changed = 0;
info->port[wIndex-1].oc_status = 0;
}
goto out;
case USB_PORT_FEAT_OVER_CURRENT:
dev_dbg(hcd->self.controller,
"ClearPortFeature: OVER_CURRENT\n");
if (valid_port(wIndex)) {
info->port[wIndex-1].oc_status = 0;
}
goto out;
case USB_PORT_FEAT_POWER:
dev_dbg(hcd->self.controller,
"ClearPortFeature: POWER\n");
if (valid_port(wIndex)) {
s3c2410_usb_set_power(info, wIndex, 0);
return 0;
}
}
break;
}
ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
if (ret)
goto out;
switch (typeReq) {
/*獲取集線器特定的描述符*/
case GetHubDescriptor:
/* 更新集線器的描述符*/
desc = (struct usb_hub_descriptor *)buf;
if (info->power_control == NULL)
return ret;
dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
desc->wHubCharacteristics);
/* 更新集線器的配置,包括HUB電源和過流保護的配置
*/
desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
desc->wHubCharacteristics |= cpu_to_le16(0x0001);
if (info->enable_oc) {
desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
desc->wHubCharacteristics |= cpu_to_le16(0x0008|0x0001);
}
dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
desc->wHubCharacteristics);
return ret;
/* 獲取端口的狀態 */
case GetPortStatus:
dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
if (valid_port(wIndex)) {
if (info->port[wIndex-1].oc_changed) {
*data |= cpu_to_le32(RH_PS_OCIC);
}
if (info->port[wIndex-1].oc_status) {
*data |= cpu_to_le32(RH_PS_POCI);
}
}
}
out:
return ret;
}
/* s3c2410_hcd_oc 該函數是主機控制器對端口的過流保護
*/
static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
{
struct s3c2410_hcd_port *port;
struct usb_hcd *hcd;
unsigned long flags;
int portno;
if (info == NULL)
return;
port = &info->port[0];
hcd = info->hcd;
local_irq_save(flags);
for (portno = 0; portno < 2; port++, portno++) {
if (port_oc & (1<<portno) &&
port->flags & S3C_HCDFLG_USED) {
port->oc_status = 1;
port->oc_changed = 1;
/* 停止該端口的供電 */
s3c2410_usb_set_power(info, portno+1, 0);
}
}
local_irq_restore(flags);
}
/* usb_hcd_s3c2410_remove 主機控制器的卸載
停止主機控制器,釋放內存資源
*
*/
void usb_hcd_s3c2410_remove (struct usb_hcd *hcd, struct platform_device *dev)
{
usb_remove_hcd(hcd);
s3c2410_stop_hc(dev);
iounmap(hcd->regs); //解除與內存映射的關系函數
release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
usb_put_hcd(hcd);
}
/** usb_hcd_s3c2410_probe USB 主機控制器的探測函數.
主要實現對主機控制器進行分配資源,并啟動主機控制器等
*/
int usb_hcd_s3c2410_probe (const struct hc_driver *driver,
struct platform_device *dev)
{
struct usb_hcd *hcd = NULL;
int retval;
/** 給端口進行供電*/
s3c2410_usb_set_power(dev->dev.platform_data, 0, 1);
s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
/** usb_create_hcd() 函數初始化主機控制器*/
hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
if (hcd == NULL)
return -ENOMEM; //系統錯誤,沒有相應的內存空間
/*主機控制器的資源節點*/
hcd->rsrc_start = dev->resource[0].start;
hcd->rsrc_len = dev->resource[0].end - dev->resource[0].start + 1;
/*請求分配內存資源*/
if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
dev_err(&dev->dev, "request_mem_region failed");
retval = -EBUSY; //系統錯誤,表示要分配的內存資源已經被占用
goto err0;
}
clk = clk_get(NULL, "usb-host");
if (IS_ERR(clk)) {
dev_err(&dev->dev, "cannot get usb-host clock\n");
retval = -ENOENT;
goto err1;
}
/*啟動始終,并啟動主機控制器*/
clk_use(clk);
s3c2410_start_hc(dev, hcd);
/*ioremap()作用是將物理地址映射成相應的虛擬地址*/
hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
if (!hcd->regs) {
dev_err(&dev->dev, "ioremap failed\n");
retval = -ENOMEM;
goto err2;
}
ohci_hcd_init(hcd_to_ohci(hcd));
retval = usb_add_hcd(hcd, dev->resource[1].start, SA_INTERRUPT);
if (retval != 0)
goto err2;
return 0;
err2:
s3c2410_stop_hc(dev);
iounmap(hcd->regs);
clk_unuse(clk);
clk_put(clk);
err1:
release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
err0:
usb_put_hcd(hcd);
return retval;
}
/* ohci_s3c2410_start 開放式主機控制器接口的啟動函數
調用上層的 ohci_run()函數來實現該功能
*/
static int
ohci_s3c2410_start (struct usb_hcd *hcd)
{
struct ohci_hcd *ohci = hcd_to_ohci (hcd);
int ret;
if ((ret = ohci_init(ohci)) < 0)
return ret;
if ((ret = ohci_run (ohci)) < 0) {
err ("can't start %s", hcd->self.bus_name);
ohci_stop (hcd);
return ret;
}
return 0;
}
/*節點操作函數的實例
定義了2410 USB主機控制器的具體操作函數實例
*/
static const struct hc_driver ohci_s3c2410_hc_driver = {
.description = hcd_name,
.product_desc = "S3C24XX OHCI",
.hcd_priv_size = sizeof(struct ohci_hcd),
.irq = ohci_irq,
.flags = HCD_USB11 | HCD_MEMORY,
.start = ohci_s3c2410_start,
.stop = ohci_stop,
/*
* 管理I/O請求&相關的設備資源
*/
.urb_enqueue = ohci_urb_enqueue, //提交URB的具體處理函數
.urb_dequeue = ohci_urb_dequeue,
.endpoint_disable = ohci_endpoint_disable,
/*
*調度支持,獲取當前總線幀號
*/
.get_frame_number = ohci_get_frame,
/*
* 根集線器的控制實例
*/
.hub_status_data = ohci_s3c2410_hub_status_data,
.hub_control = ohci_s3c2410_hub_control,
#if defined(CONFIG_USB_SUSPEND) && 0
.hub_suspend = ohci_hub_suspend,
.hub_resume = ohci_hub_resume,
#endif
};
/*ohci_hcd_s3c2410_drv_probe 主機控制器驅動的探測函數 */
static int ohci_hcd_s3c2410_drv_probe(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
}
/*ohci_hcd_s3c2410_drv_remove 主機控制器驅動的卸載函數 */
static int ohci_hcd_s3c2410_drv_remove(struct device *dev)
{
struct platform_device *pdev = to_platform_device(dev);
struct usb_hcd *hcd = dev_get_drvdata(dev);
usb_hcd_s3c2410_remove(hcd, pdev);
return 0;
}
/*
*s3c2410 主機控制驅動的結構實例
***/
static struct device_driver ohci_hcd_s3c2410_driver = {
.name = "s3c2410-ohci", //驅動名稱
.bus = &platform_bus_type, //總線類型
.probe = ohci_hcd_s3c2410_drv_probe, //驅動的探測
.remove = ohci_hcd_s3c2410_drv_remove, //驅動的卸載
};
/*s3c2410主機控制器的初始,系統調用,注冊主機設備 */
static int __init ohci_hcd_s3c2410_init (void)
{
return driver_register(&ohci_hcd_s3c2410_driver);
}
/* s3c2410主機控制器的注銷,從Linux內核中注銷驅動程序 */
static void __exit ohci_hcd_s3c2410_cleanup (void)
{
driver_unregister(&ohci_hcd_s3c2410_driver);
}
/* 初始&卸載 s3c2410主機控制模塊 */
module_init (ohci_hcd_s3c2410_init);
module_exit (ohci_hcd_s3c2410_cleanup);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -