USB subsystem gadget analyze

Source: Internet
Author: User
Struct usb_gadget_driver {char * function; Enum usb_device_speed speed; void (* unbind) (struct usb_gadget *); int (* setup) (struct usb_gadget *, const struct usb_ctrlrequest *); void (* disconnect) (struct usb_gadget *); void (* suspend) (struct usb_gadget *); void (* resume) (struct usb_gadget *); /* fixme support safe rmmod */struct device_driver driver;}; struct s3c2410_udc {spinlock_t lock; struct s3c2410_ep [s3c2410_endpoints]; int address; struct usb_gadget gadget; struct drivers * driver; struct s3c2410_request response; u8 response [Response]; b2devstatus; u32 port_status; int ep0state; unsigned got_irq: 1; unsigned req_std: 1; unsigned req_config: 1; unsigned response: 1; u8 vbus; struct dentry * regs_info;}; USB device test analysis. first, we need to know that the USB controller runs in OTG mode when running the USB on the client role, and the usb otg standard is fully compatible with the USB2.0 standard, it allows the device to be either a host or a peripheral, and the driver that controls the USB device controller is called a UDC driver. We remember that what controls the USB device controller in host mode is: USB host controller driver. there are many concepts. it is easy to understand as long as we understand it clearly. on the UDC is the gadget API. the above is the gadeget driver. the device controller of usb otg is abstracted as: "struct s3c2410_udc {spinlock_t lock; struct s3c2410_ep [unlock]; int address; struct usb_gadget gadget; struct drivers * driver; struct s3c2410_request paio_req; u8 queue [queue]; 2010devstatus; u32 port_status; int ep0state; unsigned got_irq: 1; unsigned req_std: 1; unsigned req_config: 1; unsigned req_pending: 1; u8 vbus; struct dentry * regs_info;};/* ------------------------------------------------------------------------- */static struct s3c2410_udc memory = {. gadget = {. ops = & s3c2410_ops ,. ep0 = & memory. EP [0]. EP ,. name = gadget_name ,. dev = {. init_name = "Gadget" ,},},/* control endpoint */. EP [0] = {. num = 0 ,. ep = {. name = ep0name ,. ops = & s3c2410_ep_ops ,. maxpacket = ep0_1_o_size ,},. dev = & memory,},/* First Group of endpoints */. EP [1] = {. num = 1 ,. ep = {. name = "ep1-bulk ",. ops = & s3c2410_ep_ops ,. maxpacket = ep_1_o_size ,},. dev = & memory ,. export o_size = ep_1_o_size ,. bendpointaddress = 1 ,. bmattributes = usb_endpoint_xfer_bulk ,},. EP [2] = {. num = 2 ,. ep = {. name = "ep2-bulk ",. ops = & s3c2410_ep_ops ,. maxpacket = ep_1_o_size ,},. dev = & memory ,. export o_size = ep_1_o_size ,. bendpointaddress = 2 ,. bmattributes = usb_endpoint_xfer_bulk ,},. EP [3] = {. num = 3 ,. ep = {. name = "ep3-bulk ",. ops = & s3c2410_ep_ops ,. maxpacket = ep_1_o_size ,},. dev = & memory ,. export o_size = ep_1_o_size ,. bendpointaddress = 3 ,. bmattributes = usb_endpoint_xfer_bulk ,},. EP [4] = {. num = 4 ,. ep = {. name = "ep4-bulk ",. ops = & s3c2410_ep_ops ,. maxpacket = ep_1_o_size ,},. dev = & memory ,. export o_size = ep_1_o_size ,. bendpointaddress = 4 ,. bmattributes = usb_endpoint_xfer_bulk ,}};

Usb_add_function.

Add one or more functions for configuration and add process entries using the @ bind function.

For example, add the following ADB

static int adb_bind_config(struct usb_configuration *c){struct adb_dev *dev = _adb_dev;printk(KERN_INFO "adb_bind_config\n");dev->cdev = c->cdev;dev->function.name = "adb";dev->function.descriptors = fs_adb_descs;dev->function.hs_descriptors = hs_adb_descs;dev->function.bind = adb_function_bind;dev->function.unbind = adb_function_unbind;dev->function.set_alt = adb_function_set_alt;dev->function.disable = adb_function_disable;return usb_add_function(c, &dev->function);}

Dev-> function. Bind = adb_function_bind; assign values to the BIND function.

The following

 usb_add_function

This bind function will be called when this function is used.

/** * usb_add_function() - add a function to a configuration * @config: the configuration * @function: the function being added * Context: single threaded during gadget setup * * After initialization, each configuration must have one or more * functions added to it.  Adding a function involves calling its @bind() * method to allocate resources such as interface and string identifiers * and endpoints. * * This function returns the value of the function's bind(), which is * zero for success else a negative errno value. */int usb_add_function(struct usb_configuration *config,struct usb_function *function){intvalue = -EINVAL;DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",function->name, function,config->label, config);if (!function->set_alt || !function->disable)goto done;function->config = config;list_add_tail(&function->list, &config->functions);/* REVISIT *require* function->bind? */
/* Call the BIND function to BIND configuration and usb_function */
If (function-> bind) {value = function-> BIND (config, function); // call start if (value <0) {list_del (& function-> list ); function-> Config = NULL;} elsevalue = 0;/* We allow mappings that don't work at both speeds. * If we run into a lowspeed Linux system, treat it the same * as full speed... it's the function drivers that will need * to avoid bulk and ISO transfers. */If (! Config-> fullspeed & function-> descriptors) config-> fullspeed = true; If (! Config-> higheat eed & function-> hs_descriptors) config-> higheat eed = true; If (! Config-> superspeed & function-> ss_descriptors) config-> superspeed = true; done: If (value) dbg (config-> cdev, "adding '% s'/% P --> % d \ n", function-> name, function, value); return value ;}

Look at the code above. Dev-> function. Bind = adb_function_bind is called. This function binds functions and configuration.

 

 

Let's look at usb_function again.

 

So what is the function of this usb_function?

See the following introduction.

/**
* Struct usb_function-describes one function of a configuration
* @ Name: for diagnostics, identifies the function.
* @ Strings: tables of strings, keyed by identifiers assigned during BIND ()
* And by language IDs provided in control requests
* @ Descriptors: Table of full (or low) Speed descriptors, using interface and
* String identifiers assigned during @ BIND (). If this pointer is null,
* The function will not be available at full speed (or at low speed ).
* @ Hs_descriptors: Table of High Speed descriptors, using interface and
* String identifiers assigned during @ BIND (). If this pointer is null,
* The function will not be available at high speed.
* @ Ss_descriptors: Table of super speed descriptors, using interface and
* String identifiers assigned during @ BIND (). If this
* Pointer is null after initiation, the function will not
* Be available at super speed.
* @ Config: assigned when @ usb_add_function () is called; this is
* Configuration with which this function is associated.
* @ BIND: Before the Gadget can register, all of its functions BIND () to
* Available resources including string and interface identifiers used
* In interface or class descriptors; endpoints; I/O buffers; and so on.
* @ Unbind: reverses @ bind; called as a side effect of unregistering
* Driver which added this function.
* @ Set_alt: (required) reconfigures altsettings; function drivers may
* Initialize usb_ep.driver data at this time (when it is used ).
* Note that setting an interface to its current altsetting resets
* Interface state, and that all interfaces have a disabled state.
* @ Get_alt: returns the active altsetting. If this is not provided,
* Then only altsetting zero is supported.
* @ Disable: (required) indicates the function shocould be disabled. Reasons
* Include host resetting or reconfiguring the gadget, and disconnection.
* @ Setup: used for interface-specific control requests.
* @ Suspend: notifies functions when the host stops sending USB traffic.
* @ Resume: notifies functions when the host restarts USB traffic.
* @ Get_status: Returns function status as a reply
* Getstatus () request when the specified ient is interface.
* @ Func_suspend: callback to be called when
* Setfeature (function_suspend) is reseived
*
* A single USB function uses one or more interfaces, and shoshould in most
* Cases support operation at both full and high speeds. Each function is
* Associated by @ usb_add_function () with a one configuration; that function
* Causes @ BIND () to be called so resources can be allocated as part
* Setting up a gadget driver. Those resources include endpoints, which
* Shoshould be allocated using @ usb_ep_autoconfig ().
*
* To support dual speed operation, a function driver provides Descriptors
* For both high and full speed operation. Wait t in rare cases that don't
* Involve bulk endpoints, each speed needs different endpoint descriptors.
*
* Function drivers choose their own strategies for managing instance data.
* The simplest strategy just declares it "static", which means the function
* Can only be activated once. If the function needs to be exposed in more
* Than one configuration at a given speed, it needs to support multiple
* Usb_function structures (one for each configuration ).
*
* A more complex strategy might encapsulate a @ usb_function structure inside
* A driver-specific instance structure to allows multiple activations.
* Example of multiple activations might be a cdc acm function that supports
* Two or more distinct instances within the same configuration, providing
* Several independent logical data links to a USB host.
*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.