18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* Target based USB-Gadget 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * UAS protocol handling, target callbacks, configfs handling, 58c2ecf20Sopenharmony_ci * BBB (USB Mass Storage Class Bulk-Only (BBB) and Transport protocol handling. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author: Sebastian Andrzej Siewior <bigeasy at linutronix dot de> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/types.h> 128c2ecf20Sopenharmony_ci#include <linux/string.h> 138c2ecf20Sopenharmony_ci#include <linux/configfs.h> 148c2ecf20Sopenharmony_ci#include <linux/ctype.h> 158c2ecf20Sopenharmony_ci#include <linux/usb/ch9.h> 168c2ecf20Sopenharmony_ci#include <linux/usb/composite.h> 178c2ecf20Sopenharmony_ci#include <linux/usb/gadget.h> 188c2ecf20Sopenharmony_ci#include <linux/usb/storage.h> 198c2ecf20Sopenharmony_ci#include <scsi/scsi_tcq.h> 208c2ecf20Sopenharmony_ci#include <target/target_core_base.h> 218c2ecf20Sopenharmony_ci#include <target/target_core_fabric.h> 228c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#include "u_tcm.h" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciUSB_GADGET_COMPOSITE_OPTIONS(); 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define UAS_VENDOR_ID 0x0525 /* NetChip */ 298c2ecf20Sopenharmony_ci#define UAS_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic struct usb_device_descriptor usbg_device_desc = { 328c2ecf20Sopenharmony_ci .bLength = sizeof(usbg_device_desc), 338c2ecf20Sopenharmony_ci .bDescriptorType = USB_DT_DEVICE, 348c2ecf20Sopenharmony_ci /* .bcdUSB = DYNAMIC */ 358c2ecf20Sopenharmony_ci .bDeviceClass = USB_CLASS_PER_INTERFACE, 368c2ecf20Sopenharmony_ci .idVendor = cpu_to_le16(UAS_VENDOR_ID), 378c2ecf20Sopenharmony_ci .idProduct = cpu_to_le16(UAS_PRODUCT_ID), 388c2ecf20Sopenharmony_ci .bNumConfigurations = 1, 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define USB_G_STR_CONFIG USB_GADGET_FIRST_AVAIL_IDX 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic struct usb_string usbg_us_strings[] = { 448c2ecf20Sopenharmony_ci [USB_GADGET_MANUFACTURER_IDX].s = "Target Manufacturer", 458c2ecf20Sopenharmony_ci [USB_GADGET_PRODUCT_IDX].s = "Target Product", 468c2ecf20Sopenharmony_ci [USB_GADGET_SERIAL_IDX].s = "000000000001", 478c2ecf20Sopenharmony_ci [USB_G_STR_CONFIG].s = "default config", 488c2ecf20Sopenharmony_ci { }, 498c2ecf20Sopenharmony_ci}; 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic struct usb_gadget_strings usbg_stringtab = { 528c2ecf20Sopenharmony_ci .language = 0x0409, 538c2ecf20Sopenharmony_ci .strings = usbg_us_strings, 548c2ecf20Sopenharmony_ci}; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic struct usb_gadget_strings *usbg_strings[] = { 578c2ecf20Sopenharmony_ci &usbg_stringtab, 588c2ecf20Sopenharmony_ci NULL, 598c2ecf20Sopenharmony_ci}; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic struct usb_function_instance *fi_tcm; 628c2ecf20Sopenharmony_cistatic struct usb_function *f_tcm; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic int guas_unbind(struct usb_composite_dev *cdev) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(f_tcm)) 678c2ecf20Sopenharmony_ci usb_put_function(f_tcm); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci return 0; 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int tcm_do_config(struct usb_configuration *c) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci int status; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci f_tcm = usb_get_function(fi_tcm); 778c2ecf20Sopenharmony_ci if (IS_ERR(f_tcm)) 788c2ecf20Sopenharmony_ci return PTR_ERR(f_tcm); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci status = usb_add_function(c, f_tcm); 818c2ecf20Sopenharmony_ci if (status < 0) { 828c2ecf20Sopenharmony_ci usb_put_function(f_tcm); 838c2ecf20Sopenharmony_ci return status; 848c2ecf20Sopenharmony_ci } 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci return 0; 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic struct usb_configuration usbg_config_driver = { 908c2ecf20Sopenharmony_ci .label = "Linux Target", 918c2ecf20Sopenharmony_ci .bConfigurationValue = 1, 928c2ecf20Sopenharmony_ci .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 938c2ecf20Sopenharmony_ci}; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic int usbg_attach(struct usb_function_instance *f); 968c2ecf20Sopenharmony_cistatic void usbg_detach(struct usb_function_instance *f); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic int usb_target_bind(struct usb_composite_dev *cdev) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci int ret; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci ret = usb_string_ids_tab(cdev, usbg_us_strings); 1038c2ecf20Sopenharmony_ci if (ret) 1048c2ecf20Sopenharmony_ci return ret; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci usbg_device_desc.iManufacturer = 1078c2ecf20Sopenharmony_ci usbg_us_strings[USB_GADGET_MANUFACTURER_IDX].id; 1088c2ecf20Sopenharmony_ci usbg_device_desc.iProduct = usbg_us_strings[USB_GADGET_PRODUCT_IDX].id; 1098c2ecf20Sopenharmony_ci usbg_device_desc.iSerialNumber = 1108c2ecf20Sopenharmony_ci usbg_us_strings[USB_GADGET_SERIAL_IDX].id; 1118c2ecf20Sopenharmony_ci usbg_config_driver.iConfiguration = 1128c2ecf20Sopenharmony_ci usbg_us_strings[USB_G_STR_CONFIG].id; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci ret = usb_add_config(cdev, &usbg_config_driver, tcm_do_config); 1158c2ecf20Sopenharmony_ci if (ret) 1168c2ecf20Sopenharmony_ci return ret; 1178c2ecf20Sopenharmony_ci usb_composite_overwrite_options(cdev, &coverwrite); 1188c2ecf20Sopenharmony_ci return 0; 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistatic struct usb_composite_driver usbg_driver = { 1228c2ecf20Sopenharmony_ci .name = "g_target", 1238c2ecf20Sopenharmony_ci .dev = &usbg_device_desc, 1248c2ecf20Sopenharmony_ci .strings = usbg_strings, 1258c2ecf20Sopenharmony_ci .max_speed = USB_SPEED_SUPER, 1268c2ecf20Sopenharmony_ci .bind = usb_target_bind, 1278c2ecf20Sopenharmony_ci .unbind = guas_unbind, 1288c2ecf20Sopenharmony_ci}; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int usbg_attach(struct usb_function_instance *f) 1318c2ecf20Sopenharmony_ci{ 1328c2ecf20Sopenharmony_ci return usb_composite_probe(&usbg_driver); 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic void usbg_detach(struct usb_function_instance *f) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci usb_composite_unregister(&usbg_driver); 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic int __init usb_target_gadget_init(void) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci struct f_tcm_opts *tcm_opts; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci fi_tcm = usb_get_function_instance("tcm"); 1458c2ecf20Sopenharmony_ci if (IS_ERR(fi_tcm)) 1468c2ecf20Sopenharmony_ci return PTR_ERR(fi_tcm); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci tcm_opts = container_of(fi_tcm, struct f_tcm_opts, func_inst); 1498c2ecf20Sopenharmony_ci mutex_lock(&tcm_opts->dep_lock); 1508c2ecf20Sopenharmony_ci tcm_opts->tcm_register_callback = usbg_attach; 1518c2ecf20Sopenharmony_ci tcm_opts->tcm_unregister_callback = usbg_detach; 1528c2ecf20Sopenharmony_ci tcm_opts->dependent = THIS_MODULE; 1538c2ecf20Sopenharmony_ci tcm_opts->can_attach = true; 1548c2ecf20Sopenharmony_ci tcm_opts->has_dep = true; 1558c2ecf20Sopenharmony_ci mutex_unlock(&tcm_opts->dep_lock); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci fi_tcm->set_inst_name(fi_tcm, "tcm-legacy"); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci return 0; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_cimodule_init(usb_target_gadget_init); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic void __exit usb_target_gadget_exit(void) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci if (!IS_ERR_OR_NULL(fi_tcm)) 1668c2ecf20Sopenharmony_ci usb_put_function_instance(fi_tcm); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_cimodule_exit(usb_target_gadget_exit); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>"); 1728c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("usb-gadget fabric"); 1738c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 174