linux usb做复合设备,usb从设备驱动usb_composite_driver复合设备

Linux/arm 3.10.104

drivers/usb/gadget/composite.c

static const struct usb_gadget_driver composite_driver_template = {

.bind        = composite_bind,

.unbind        = composite_unbind,

.setup        = composite_setup,

.disconnect    = composite_disconnect,

.suspend    = composite_suspend,

.resume        = composite_resume,

.driver    = {

.owner        = THIS_MODULE,

},

};

drivers/usb/gadget/composite.c

/*

* The setup() callback implements all the ep0 functionality that's

* not handled lower down, in hardware or the hardware driver(like

* device and endpoint feature flags, and their status).  It's all

* housekeeping for the gadget function we're implementing.  Most of

* the work is in config and function specific setup.

*/

int

composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)

{

struct usb_composite_dev    *cdev = get_gadget_data(gadget);

struct usb_request        *req = cdev->req;

int                value = -EOPNOTSUPP;

int                status = 0;

u16                w_index = le16_to_cpu(ctrl->wIndex);

u8                intf = w_index & 0xFF;

u16                w_value = le16_to_cpu(ctrl->wValue);

u16                w_length = le16_to_cpu(ctrl->wLength);

struct usb_function        *f = NULL;

u8                endp;

/* partial re-init of the response message; the function or the

* gadget might need to intercept e.g. a control-OUT completion

* when we delegate to it.

*/

req->zero = 0;

req->complete = composite_setup_complete;

req->length = 0;

gadget->ep0->driver_data = cdev;

switch (ctrl->bRequest) {

/* we handle all standard USB descriptors */

case USB_REQ_GET_DESCRIPTOR:

if (ctrl->bRequestType != USB_DIR_IN)

goto unknown;

switch (w_value >> 8) {

case USB_DT_DEVICE:

cdev->desc.bNumConfigurations =

count_configs(cdev, USB_DT_DEVICE);

cdev->desc.bMaxPacketSize0 =

cdev->gadget->ep0->maxpacket;

if (gadget_is_superspeed(gadget)) {

if (gadget->speed >= USB_SPEED_SUPER) {

cdev->desc.bcdUSB = cpu_to_le16(0x0300);

cdev->desc.bMaxPacketSize0 = 9;

} else {

cdev->desc.bcdUSB = cpu_to_le16(0x0210);

}

}

value = min(w_length, (u16) sizeof cdev->desc);

memcpy(req->buf, &cdev->desc, value);

break;

case USB_DT_DEVICE_QUALIFIER:

if (!gadget_is_dualspeed(gadget) ||

gadget->speed >= USB_SPEED_SUPER)

break;

device_qual(cdev);

value = min_t(int, w_length,

sizeof(struct usb_qualifier_descriptor));

break;

case USB_DT_OTHER_SPEED_CONFIG:

if (!gadget_is_dualspeed(gadget) ||

gadget->speed >= USB_SPEED_SUPER)

break;

/* FALLTHROUGH */

case USB_DT_CONFIG:

value = config_desc(cdev, w_value);

if (value >= 0)

value = min(w_length, (u16) value);

break;

case USB_DT_STRING:

value = get_string(cdev, req->buf,

w_index, w_value & 0xff);

if (value >= 0)

value = min(w_length, (u16) value);

break;

case USB_DT_BOS:

if (gadget_is_superspeed(gadget)) {

value = bos_desc(cdev);

value = min(w_length, (u16) value);

}

break;

}

break;

/* any number of configs can work */

case USB_REQ_SET_CONFIGURATION:

if (ctrl->bRequestType != 0)

goto unknown;

if (gadget_is_otg(gadget)) {

if (gadget->a_hnp_support)

DBG(cdev, "HNP available\n");

else if (gadget->a_alt_hnp_support)

DBG(cdev, "HNP on another port\n");

else

VDBG(cdev, "HNP inactive\n");

}

spin_lock(&cdev->lock);

value = set_config(cdev, ctrl, w_value);

spin_unlock(&cdev->lock);

break;

case USB_REQ_GET_CONFIGURATION:

if (ctrl->bRequestType != USB_DIR_IN)

goto unknown;

if (cdev->config)

*(u8 *)req->buf = cdev->config->bConfigurationValue;

else

*(u8 *)req->buf = 0;

value = min(w_length, (u16) 1);

break;

/* function drivers must handle get/set altsetting; if there's

* no get() method, we know only altsetting zero works.

*/

case USB_REQ_SET_INTERFACE:

if (ctrl->bRequestType != USB_RECIP_INTERFACE)

goto unknown;

if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)

break;

f = cdev->config->interface[intf];

if (!f)

break;

if (w_value && !f->set_alt)

break;

//此处调用下文 audio->card.func.set_alt = f_audio_set_alt;

value = f->set_alt(f, w_index, w_value);

if (value == USB_GADGET_DELAYED_STATUS) {

DBG(cdev,

"%s: interface %d (%s) requested delayed status\n",

__func__, intf, f->name);

cdev->delayed_status++;

DBG(cdev, "delayed_status count %d\n",

cdev->delayed_status);

}

break;

case USB_REQ_GET_INTERFACE:

if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))

goto unknown;

if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)

break;

f = cdev->config->interface[intf];

if (!f)

break;

/* lots of interfaces only need altsetting zero... */

value = f->get_alt ? f->get_alt(f, w_index) : 0;

if (value < 0)

break;

*((u8 *)req->buf) = value;

value = min(w_length, (u16) 1);

break;

/*

* USB 3.0 additions:

* Function driver should handle get_status request. If such cb

* wasn't supplied we respond with default value = 0

* Note: function driver should supply such cb only for the first

* interface of the function

*/

case USB_REQ_GET_STATUS:

if (!gadget_is_superspeed(gadget))

goto unknown;

if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))

goto unknown;

value = 2;    /* This is the length of the get_status reply */

put_unaligned_le16(0, req->buf);

if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)

break;

f = cdev->config->interface[intf];

if (!f)

break;

status = f->get_status ? f->get_status(f) : 0;

if (status < 0)

break;

put_unaligned_le16(status & 0x0000ffff, req->buf);

break;

/*

* Function drivers should handle SetFeature/ClearFeature

* (FUNCTION_SUSPEND) request. function_suspend cb should be supplied

* only for the first interface of the function

*/

case USB_REQ_CLEAR_FEATURE:

case USB_REQ_SET_FEATURE:

if (!gadget_is_superspeed(gadget))

goto unknown;

if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))

goto unknown;

switch (w_value) {

case USB_INTRF_FUNC_SUSPEND:

if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)

break;

f = cdev->config->interface[intf];

if (!f)

break;

value = 0;

if (f->func_suspend)

value = f->func_suspend(f, w_index >> 8);

if (value < 0) {

ERROR(cdev,

"func_suspend() returned error %d\n",

value);

value = 0;

}

break;

}

break;

default:

unknown:

VDBG(cdev,

"non-core control req%02x.%02x v%04x i%04x l%d\n",

ctrl->bRequestType, ctrl->bRequest,

w_value, w_index, w_length);

/* functions always handle their interfaces and endpoints...

* punt other recipients (other, WUSB, ...) to the current

* configuration code.

*

* REVISIT it could make sense to let the composite device

* take such requests too, if that's ever needed:  to work

* in config 0, etc.

*/

switch (ctrl->bRequestType & USB_RECIP_MASK) {

case USB_RECIP_INTERFACE:

if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)

break;

f = cdev->config->interface[intf];

break;

case USB_RECIP_ENDPOINT:

endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);

list_for_each_entry(f, &cdev->config->functions, list) {

if (test_bit(endp, f->endpoints))

break;

}

if (&f->list == &cdev->config->functions)

f = NULL;

break;

}

if (f && f->setup)

value = f->setup(f, ctrl); //如果usb_add_function有setup优先调用

//此处调用下文 audio->card.func.setup = f_audio_setup;

else {

struct usb_configuration    *c;

c = cdev->config;

if (c && c->setup)

value = c->setup(c, ctrl);//否则调用usb_configuration中的setup函数

}

goto done;

}

/* respond with data transfer before status phase? */

if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {

req->length = value;

req->zero = value < w_length;

value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);

if (value < 0) {

DBG(cdev, "ep_queue --> %d\n", value);

req->status = 0;

composite_setup_complete(gadget->ep0, req);

}

} else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {

WARN(cdev,

"%s: Delayed status not supported for w_length != 0",

__func__);

}

done:

/* device either stalls (value < 0) or reports success */

return value;

}

//  driver/usb/gadget/f_uac1.c

static int

f_audio_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)

{

struct usb_composite_dev *cdev = f->config->cdev;

struct usb_request    *req = cdev->req;

int            value = -EOPNOTSUPP;

u16            w_index = le16_to_cpu(ctrl->wIndex);

u16            w_value = le16_to_cpu(ctrl->wValue);

u16            w_length = le16_to_cpu(ctrl->wLength);

/* composite driver infrastructure handles everything; interface

* activation uses set_alt().

*/

switch (ctrl->bRequestType) {

case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:

value = audio_set_intf_req(f, ctrl);

break;

case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:

value = audio_get_intf_req(f, ctrl);

break;

case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:

value = audio_set_endpoint_req(f, ctrl);

break;

case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:

value = audio_get_endpoint_req(f, ctrl);

break;

default:

ERROR(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",

ctrl->bRequestType, ctrl->bRequest,

w_value, w_index, w_length);

}

/* respond with data transfer or status phase? */

if (value >= 0) {

DBG(cdev, "audio req%02x.%02x v%04x i%04x l%d\n",

ctrl->bRequestType, ctrl->bRequest,

w_value, w_index, w_length);

req->zero = 0;

req->length = value;

value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);

if (value < 0)

ERROR(cdev, "audio response on err %d\n", value);

}

/* device either stalls (value < 0) or reports success */

return value;

}

//  driver/usb/gadget/f_uac1.c

static int f_audio_set_alt(struct usb_function *f, unsigned intf, unsigned alt)

{

struct f_audio        *audio = func_to_audio(f);

struct usb_composite_dev *cdev = f->config->cdev;

struct usb_ep *out_ep = audio->out_ep;

struct usb_request *req;

int i = 0, err = 0;

DBG(cdev, "intf %d, alt %d\n", intf, alt);

if (intf == 1) {

if (alt == 1) {

usb_ep_enable(out_ep);

out_ep->driver_data = audio;

audio->copy_buf = f_audio_buffer_alloc(audio_buf_size);

if (IS_ERR(audio->copy_buf))

return -ENOMEM;

/*

* allocate a bunch of read buffers

* and queue them all at once.

*/

for (i = 0; i < req_count && err == 0; i++) {

req = usb_ep_alloc_request(out_ep, GFP_ATOMIC);

if (req) {

req->buf = kzalloc(req_buf_size,

GFP_ATOMIC);

if (req->buf) {

req->length = req_buf_size;

req->context = audio;

req->complete =

f_audio_complete;

err = usb_ep_queue(out_ep,

req, GFP_ATOMIC);

if (err)

ERROR(cdev,

"%s queue req: %d\n",

out_ep->name, err);

} else

err = -ENOMEM;

} else

err = -ENOMEM;

}

} else {

struct f_audio_buf *copy_buf = audio->copy_buf;

if (copy_buf) {

list_add_tail(&copy_buf->list,

&audio->play_queue);

schedule_work(&audio->playback_work);

}

}

}

return err;

}

//  driver/usb/gadget/f_uac1.c

/* audio function driver setup/binding */

static int __init

f_audio_bind(struct usb_configuration *c, struct usb_function *f)

{

struct usb_composite_dev *cdev = c->cdev;

struct f_audio        *audio = func_to_audio(f);

int            status;

struct usb_ep        *ep = NULL;

f_audio_build_desc(audio);

/* allocate instance-specific interface IDs, and patch descriptors */

status = usb_interface_id(c, f);

if (status < 0)

goto fail;

ac_interface_desc.bInterfaceNumber = status;

status = usb_interface_id(c, f);

if (status < 0)

goto fail;

as_interface_alt_0_desc.bInterfaceNumber = status;

as_interface_alt_1_desc.bInterfaceNumber = status;

status = -ENODEV;

/* allocate instance-specific endpoints */

ep = usb_ep_autoconfig(cdev->gadget, &as_out_ep_desc);

if (!ep)

goto fail;

audio->out_ep = ep;

audio->out_ep->desc = &as_out_ep_desc;

ep->driver_data = cdev;    /* claim */

status = -ENOMEM;

/* copy descriptors, and track endpoint copies */

status = usb_assign_descriptors(f, f_audio_desc, f_audio_desc, NULL);

if (status)

goto fail;

return 0;

fail:

if (ep)

ep->driver_data = NULL;

return status;

}

//  driver/usb/gadget/f_uac1.c

/**

* audio_bind_config - add USB audio function to a configuration

* @c: the configuration to supcard the USB audio function

* Context: single threaded during gadget setup

*

* Returns zero on success, else negative errno.

*/

int __init audio_bind_config(struct usb_configuration *c)

{

struct f_audio *audio;

int status;

/* allocate and initialize one new instance */

audio = kzalloc(sizeof *audio, GFP_KERNEL);

if (!audio)

return -ENOMEM;

audio->card.func.name = "g_audio";

audio->card.gadget = c->cdev->gadget;

INIT_LIST_HEAD(&audio->play_queue);

spin_lock_init(&audio->lock);

/* set up ASLA audio devices */

status = gaudio_setup(&audio->card);

if (status < 0)

goto setup_fail;

audio->card.func.strings = audio_strings;

audio->card.func.bind = f_audio_bind;

audio->card.func.unbind = f_audio_unbind;

audio->card.func.set_alt = f_audio_set_alt;

audio->card.func.setup = f_audio_setup;

audio->card.func.disable = f_audio_disable;

control_selector_init(audio);

INIT_WORK(&audio->playback_work, f_audio_playback_work);

status = usb_add_function(c, &audio->card.func);

if (status)

goto add_fail;

INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n",

audio_buf_size, req_buf_size, req_count);

return status;

add_fail:

gaudio_cleanup();

setup_fail:

kfree(audio);

return status;

}

// driver/usb/gadget/audio.c

static int __init init(void)

{

return usb_composite_probe(&audio_driver);

}

module_init(init);

// driver/usb/gadget/audio.c

static struct usb_composite_driver audio_driver = {

.name        = "g_audio",

.dev        = &device_desc,

.strings    = audio_strings,

.max_speed    = USB_SPEED_HIGH,

.bind        = audio_bind,

.unbind        = audio_unbind,

};

// driver/usb/gadget/audio.c

static int audio_bind(struct usb_composite_dev *cdev)

{

status = usb_add_config(cdev, &audio_config_driver, audio_do_config);

}

// driver/usb/gadget/audio.c

static int audio_do_config(struct usb_configuration *c)

{

audio_bind_config(c);

//调用driver/usb/gadget/f_uac1.c 中的audio_bind_config

}

阅读(762) | 评论(0) | 转发(0) |

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值