2017-11-03 18:28:30 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2012-05-04 10:51:36 +08:00
|
|
|
/* Target based USB-Gadget
|
|
|
|
*
|
|
|
|
* UAS protocol handling, target callbacks, configfs handling,
|
|
|
|
* BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling.
|
|
|
|
*
|
|
|
|
* Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de>
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/configfs.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <linux/usb/ch9.h>
|
|
|
|
#include <linux/usb/composite.h>
|
|
|
|
#include <linux/usb/gadget.h>
|
|
|
|
#include <linux/usb/storage.h>
|
|
|
|
#include <scsi/scsi_tcq.h>
|
|
|
|
#include <target/target_core_base.h>
|
|
|
|
#include <target/target_core_fabric.h>
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
#include "u_tcm.h"
|
2012-05-04 10:51:36 +08:00
|
|
|
|
2012-09-10 21:01:53 +08:00
|
|
|
USB_GADGET_COMPOSITE_OPTIONS();
|
|
|
|
|
2012-05-04 10:51:36 +08:00
|
|
|
#define UAS_VENDOR_ID 0x0525 /* NetChip */
|
|
|
|
#define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
|
|
|
|
|
|
|
|
static struct usb_device_descriptor usbg_device_desc = {
|
|
|
|
.bLength = sizeof(usbg_device_desc),
|
|
|
|
.bDescriptorType = USB_DT_DEVICE,
|
2015-10-21 00:33:13 +08:00
|
|
|
/* .bcdUSB = DYNAMIC */
|
2012-05-04 10:51:36 +08:00
|
|
|
.bDeviceClass = USB_CLASS_PER_INTERFACE,
|
|
|
|
.idVendor = cpu_to_le16(UAS_VENDOR_ID),
|
|
|
|
.idProduct = cpu_to_le16(UAS_PRODUCT_ID),
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
};
|
|
|
|
|
2015-12-11 23:06:16 +08:00
|
|
|
#define USB_G_STR_CONFIG USB_GADGET_FIRST_AVAIL_IDX
|
|
|
|
|
2012-05-04 10:51:36 +08:00
|
|
|
static struct usb_string usbg_us_strings[] = {
|
2018-05-22 17:05:05 +08:00
|
|
|
[USB_GADGET_MANUFACTURER_IDX].s = "Target Manufacturer",
|
2012-09-07 02:11:21 +08:00
|
|
|
[USB_GADGET_PRODUCT_IDX].s = "Target Product",
|
|
|
|
[USB_GADGET_SERIAL_IDX].s = "000000000001",
|
2012-09-07 02:11:18 +08:00
|
|
|
[USB_G_STR_CONFIG].s = "default config",
|
2012-05-04 10:51:36 +08:00
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings usbg_stringtab = {
|
|
|
|
.language = 0x0409,
|
|
|
|
.strings = usbg_us_strings,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_gadget_strings *usbg_strings[] = {
|
|
|
|
&usbg_stringtab,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
static struct usb_function_instance *fi_tcm;
|
|
|
|
static struct usb_function *f_tcm;
|
2012-05-04 10:51:36 +08:00
|
|
|
|
|
|
|
static int guas_unbind(struct usb_composite_dev *cdev)
|
|
|
|
{
|
2015-12-11 23:06:22 +08:00
|
|
|
if (!IS_ERR_OR_NULL(f_tcm))
|
|
|
|
usb_put_function(f_tcm);
|
2012-10-23 04:15:06 +08:00
|
|
|
|
2012-05-04 10:51:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
static int tcm_do_config(struct usb_configuration *c)
|
2012-05-04 10:51:36 +08:00
|
|
|
{
|
2015-12-11 23:06:22 +08:00
|
|
|
int status;
|
2012-05-04 10:51:36 +08:00
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
f_tcm = usb_get_function(fi_tcm);
|
|
|
|
if (IS_ERR(f_tcm))
|
|
|
|
return PTR_ERR(f_tcm);
|
2012-05-04 10:51:36 +08:00
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
status = usb_add_function(c, f_tcm);
|
|
|
|
if (status < 0) {
|
|
|
|
usb_put_function(f_tcm);
|
|
|
|
return status;
|
2012-05-04 10:51:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct usb_configuration usbg_config_driver = {
|
|
|
|
.label = "Linux Target",
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.bmAttributes = USB_CONFIG_ATT_SELFPOWER,
|
|
|
|
};
|
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
static int usbg_attach(struct usb_function_instance *f);
|
|
|
|
static void usbg_detach(struct usb_function_instance *f);
|
2012-05-04 10:51:36 +08:00
|
|
|
|
|
|
|
static int usb_target_bind(struct usb_composite_dev *cdev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-09-07 02:11:18 +08:00
|
|
|
ret = usb_string_ids_tab(cdev, usbg_us_strings);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
usbg_device_desc.iManufacturer =
|
2012-09-07 02:11:21 +08:00
|
|
|
usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id;
|
|
|
|
usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id;
|
|
|
|
usbg_device_desc.iSerialNumber =
|
|
|
|
usbg_us_strings[USB_GADGET_SERIAL_IDX].id;
|
2012-09-07 02:11:18 +08:00
|
|
|
usbg_config_driver.iConfiguration =
|
|
|
|
usbg_us_strings[USB_G_STR_CONFIG].id;
|
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
ret = usb_add_config(cdev, &usbg_config_driver, tcm_do_config);
|
2012-09-10 21:01:53 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
usb_composite_overwrite_options(cdev, &coverwrite);
|
2012-05-04 10:51:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:14:21 +08:00
|
|
|
static struct usb_composite_driver usbg_driver = {
|
2012-05-04 10:51:36 +08:00
|
|
|
.name = "g_target",
|
|
|
|
.dev = &usbg_device_desc,
|
|
|
|
.strings = usbg_strings,
|
|
|
|
.max_speed = USB_SPEED_SUPER,
|
2012-09-07 02:11:04 +08:00
|
|
|
.bind = usb_target_bind,
|
2012-05-04 10:51:36 +08:00
|
|
|
.unbind = guas_unbind,
|
|
|
|
};
|
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
static int usbg_attach(struct usb_function_instance *f)
|
2012-05-04 10:51:36 +08:00
|
|
|
{
|
2012-09-07 02:11:04 +08:00
|
|
|
return usb_composite_probe(&usbg_driver);
|
2012-05-04 10:51:36 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 23:06:22 +08:00
|
|
|
static void usbg_detach(struct usb_function_instance *f)
|
2012-05-04 10:51:36 +08:00
|
|
|
{
|
|
|
|
usb_composite_unregister(&usbg_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init usb_target_gadget_init(void)
|
|
|
|
{
|
2015-12-11 23:06:22 +08:00
|
|
|
struct f_tcm_opts *tcm_opts;
|
|
|
|
|
|
|
|
fi_tcm = usb_get_function_instance("tcm");
|
|
|
|
if (IS_ERR(fi_tcm))
|
|
|
|
return PTR_ERR(fi_tcm);
|
|
|
|
|
|
|
|
tcm_opts = container_of(fi_tcm, struct f_tcm_opts, func_inst);
|
|
|
|
mutex_lock(&tcm_opts->dep_lock);
|
|
|
|
tcm_opts->tcm_register_callback = usbg_attach;
|
|
|
|
tcm_opts->tcm_unregister_callback = usbg_detach;
|
|
|
|
tcm_opts->dependent = THIS_MODULE;
|
|
|
|
tcm_opts->can_attach = true;
|
|
|
|
tcm_opts->has_dep = true;
|
|
|
|
mutex_unlock(&tcm_opts->dep_lock);
|
|
|
|
|
|
|
|
fi_tcm->set_inst_name(fi_tcm, "tcm-legacy");
|
|
|
|
|
|
|
|
return 0;
|
2012-05-04 10:51:36 +08:00
|
|
|
}
|
|
|
|
module_init(usb_target_gadget_init);
|
|
|
|
|
|
|
|
static void __exit usb_target_gadget_exit(void)
|
|
|
|
{
|
2015-12-11 23:06:22 +08:00
|
|
|
if (!IS_ERR_OR_NULL(fi_tcm))
|
|
|
|
usb_put_function_instance(fi_tcm);
|
|
|
|
|
2012-05-04 10:51:36 +08:00
|
|
|
}
|
|
|
|
module_exit(usb_target_gadget_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");
|
|
|
|
MODULE_DESCRIPTION("usb-gadget fabric");
|
|
|
|
MODULE_LICENSE("GPL v2");
|