USB驱动之U盘驱动
Posted 四季帆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了USB驱动之U盘驱动相关的知识,希望对你有一定的参考价值。
U盘使用的是drivers/usb/storage/usb.c驱动
#define DRV_NAME "usb-storage"
//插入U盘时一般会打印usb-storage 1-1:1.0: USB Mass Storage device detected
static struct usb_driver usb_storage_driver =
.name = DRV_NAME,
.probe = storage_probe,
.disconnect = usb_stor_disconnect,
.suspend = usb_stor_suspend,
.resume = usb_stor_resume,
.reset_resume = usb_stor_reset_resume,
.pre_reset = usb_stor_pre_reset,
.post_reset = usb_stor_post_reset,
.id_table = usb_storage_usb_ids,
.supports_autosuspend = 1,
.soft_unbind = 1,
;
module_usb_stor_driver(usb_storage_driver, usb_stor_host_template, DRV_NAME);
/* The main probe routine for standard devices */
static int storage_probe(struct usb_interface *intf,
const struct usb_device_id *id)
struct us_unusual_dev *unusual_dev;
struct us_data *us;
int result;
int size;
/* If uas is enabled and this device can do uas then ignore it. */
#if IS_ENABLED(CONFIG_USB_UAS)
if (uas_use_uas_driver(intf, id, NULL))
return -ENXIO;
#endif
/*
* If the device isn't standard (is handled by a subdriver
* module) then don't accept it.
*/
if (usb_usual_ignore_device(intf)) //检测匹配
return -ENXIO;
/*
* Call the general probe procedures.
*
* The unusual_dev_list array is parallel to the usb_storage_usb_ids
* table, so we use the index of the id entry to find the
* corresponding unusual_devs entry.
*/
size = ARRAY_SIZE(us_unusual_dev_list);
if (id >= usb_storage_usb_ids && id < usb_storage_usb_ids + size)
unusual_dev = (id - usb_storage_usb_ids) + us_unusual_dev_list;
else
unusual_dev = &for_dynamic_ids;
dev_dbg(&intf->dev, "Use Bulk-Only transport with the Transparent SCSI protocol for dynamic id: 0x%04x 0x%04x\\n",
id->idVendor, id->idProduct);
result = usb_stor_probe1(&us, intf, id, unusual_dev,
&usb_stor_host_template);//probe的第一部分
if (result)
return result;
/* No special transport or protocol settings in the main module */
result = usb_stor_probe2(us);//probe的第二部分
return result;
/* First part of general USB mass-storage probing */
int usb_stor_probe1(struct us_data **pus,
struct usb_interface *intf,
const struct usb_device_id *id,
struct us_unusual_dev *unusual_dev,
struct scsi_host_template *sht)
struct Scsi_Host *host;
struct us_data *us;
int result;
dev_info(&intf->dev, "USB Mass Storage device detected\\n");
/*
* Ask the SCSI layer to allocate a host structure, with extra
* space at the end for our private us_data structure.
*/
host = scsi_host_alloc(sht, sizeof(*us));//分配Scsi_Host结构体
if (!host)
dev_warn(&intf->dev, "Unable to allocate the scsi host\\n");
return -ENOMEM;
/*
* Allow 16-byte CDBs and thus > 2TB
*/
host->max_cmd_len = 16;
host->sg_tablesize = usb_stor_sg_tablesize(intf);
*pus = us = host_to_us(host);//从host结构体中提取出us_data结构体
mutex_init(&(us->dev_mutex));
us_set_lock_class(&us->dev_mutex, intf);
init_completion(&us->cmnd_ready);//初始化完成量
init_completion(&(us->notify));
init_waitqueue_head(&us->delay_wait);//初始化等待队列头
INIT_DELAYED_WORK(&us->scan_dwork, usb_stor_scan_dwork);
/* Associate the us_data structure with the USB device */
result = associate_dev(us, intf);//将us_data与USB设备相关联
if (result)
goto BadDevice;
/* Get the unusual_devs entries and the descriptors */
result = get_device_info(us, id, unusual_dev);//获取设备信息
if (result)
goto BadDevice;
/* Get standard transport and protocol settings */
get_transport(us);//获取传输方式
get_protocol(us);//获取传输协议
/*
* Give the caller a chance to fill in specialized transport
* or protocol settings.
*/
return 0;
BadDevice:
usb_stor_dbg(us, "storage_probe() failed\\n");
release_everything(us);
return result;
/* Second part of general USB mass-storage probing */
int usb_stor_probe2(struct us_data *us)
int result;
struct device *dev = &us->pusb_intf->dev;
/* Make sure the transport and protocol have both been set */
if (!us->transport || !us->proto_handler)
result = -ENXIO;
goto BadDevice;
usb_stor_dbg(us, "Transport: %s\\n", us->transport_name);
usb_stor_dbg(us, "Protocol: %s\\n", us->protocol_name);
if (us->fflags & US_FL_SCM_MULT_TARG)
/*
* SCM eUSCSI bridge devices can have different numbers
* of LUNs on different targets; allow all to be probed.
*/
us->max_lun = 7;
/* The eUSCSI itself has ID 7, so avoid scanning that */
us_to_host(us)->this_id = 7;
/* max_id is 8 initially, so no need to set it here */
else
/* In the normal case there is only a single target */
us_to_host(us)->max_id = 1;
/*
* Like Windows, we won't store the LUN bits in CDB[1] for
* SCSI-2 devices using the Bulk-Only transport (even though
* this violates the SCSI spec).
*/
if (us->transport == usb_stor_Bulk_transport)
us_to_host(us)->no_scsi2_lun_in_cdb = 1;
/* fix for single-lun devices */
if (us->fflags & US_FL_SINGLE_LUN)
us->max_lun = 0;
/* Find the endpoints and calculate pipe values */
result = get_pipes(us);//获得管道
if (result)
goto BadDevice;
/*
* If the device returns invalid data for the first READ(10)
* command, indicate the command should be retried.
*/
if (us->fflags & US_FL_INITIAL_READ10)
set_bit(US_FLIDX_REDO_READ10, &us->dflags);
/* Acquire all the other resources and add the host */
result = usb_stor_acquire_resources(us);//获取资源
if (result)
goto BadDevice;
usb_autopm_get_interface_no_resume(us->pusb_intf);
snprintf(us->scsi_name, sizeof(us->scsi_name), "usb-storage %s",
dev_name(&us->pusb_intf->dev));
result = scsi_add_host(us_to_host(us), dev);//添加scsi
if (result)
dev_warn(dev,
"Unable to add the scsi host\\n");
goto HostAddErr;
/* Submit the delayed_work for SCSI-device scanning */
set_bit(US_FLIDX_SCAN_PENDING, &us->dflags);
if (delay_use > 0) //delay_use秒后如果U盘没拔出则继续执行,否则执行disconnect
dev_dbg(dev, "waiting for device to settle before scanning\\n");
queue_delayed_work(system_freezable_wq, &us->scan_dwork,
delay_use * HZ);
return 0;
/* We come here if there are any problems */
HostAddErr:
usb_autopm_put_interface_no_suspend(us->pusb_intf);
BadDevice:
usb_stor_dbg(us, "storage_probe() failed\\n");
release_everything(us);
return result;
U盘驱动程序大概就是如此,其中probe1和probe2中还设计很多细节的内容,比如端点设置、urb控制等内容,这些内容和USB协议有很大关系,过于晦涩,所以暂时不做进一步研究。
以上是关于USB驱动之U盘驱动的主要内容,如果未能解决你的问题,请参考以下文章
U盘 盘符 不显示,电脑识别是USB Mass Storage Device
RK3399驱动开发 | 18 - 使用 usb3.0 作为device模拟u盘(基于linux5.4.32内核)