18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * mass_storage.c -- Mass Storage USB Gadget 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2003-2008 Alan Stern 68c2ecf20Sopenharmony_ci * Copyright (C) 2009 Samsung Electronics 78c2ecf20Sopenharmony_ci * Author: Michal Nazarewicz <mina86@mina86.com> 88c2ecf20Sopenharmony_ci * All rights reserved. 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/* 138c2ecf20Sopenharmony_ci * The Mass Storage Gadget acts as a USB Mass Storage device, 148c2ecf20Sopenharmony_ci * appearing to the host as a disk drive or as a CD-ROM drive. In 158c2ecf20Sopenharmony_ci * addition to providing an example of a genuinely useful gadget 168c2ecf20Sopenharmony_ci * driver for a USB device, it also illustrates a technique of 178c2ecf20Sopenharmony_ci * double-buffering for increased throughput. Last but not least, it 188c2ecf20Sopenharmony_ci * gives an easy way to probe the behavior of the Mass Storage drivers 198c2ecf20Sopenharmony_ci * in a USB host. 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * Since this file serves only administrative purposes and all the 228c2ecf20Sopenharmony_ci * business logic is implemented in f_mass_storage.* file. Read 238c2ecf20Sopenharmony_ci * comments in this file for more detailed description. 248c2ecf20Sopenharmony_ci */ 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include <linux/kernel.h> 288c2ecf20Sopenharmony_ci#include <linux/usb/ch9.h> 298c2ecf20Sopenharmony_ci#include <linux/module.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define DRIVER_DESC "Mass Storage Gadget" 348c2ecf20Sopenharmony_ci#define DRIVER_VERSION "2009/09/11" 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* 378c2ecf20Sopenharmony_ci * Thanks to NetChip Technologies for donating this product ID. 388c2ecf20Sopenharmony_ci * 398c2ecf20Sopenharmony_ci * DO NOT REUSE THESE IDs with any other driver!! Ever!! 408c2ecf20Sopenharmony_ci * Instead: allocate your own, using normal USB-IF procedures. 418c2ecf20Sopenharmony_ci */ 428c2ecf20Sopenharmony_ci#define FSG_VENDOR_ID 0x0525 /* NetChip */ 438c2ecf20Sopenharmony_ci#define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#include "f_mass_storage.h" 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 488c2ecf20Sopenharmony_ciUSB_GADGET_COMPOSITE_OPTIONS(); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic struct usb_device_descriptor msg_device_desc = { 518c2ecf20Sopenharmony_ci .bLength = sizeof msg_device_desc, 528c2ecf20Sopenharmony_ci .bDescriptorType = USB_DT_DEVICE, 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* .bcdUSB = DYNAMIC */ 558c2ecf20Sopenharmony_ci .bDeviceClass = USB_CLASS_PER_INTERFACE, 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci /* Vendor and product id can be overridden by module parameters. */ 588c2ecf20Sopenharmony_ci .idVendor = cpu_to_le16(FSG_VENDOR_ID), 598c2ecf20Sopenharmony_ci .idProduct = cpu_to_le16(FSG_PRODUCT_ID), 608c2ecf20Sopenharmony_ci .bNumConfigurations = 1, 618c2ecf20Sopenharmony_ci}; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic const struct usb_descriptor_header *otg_desc[2]; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic struct usb_string strings_dev[] = { 668c2ecf20Sopenharmony_ci [USB_GADGET_MANUFACTURER_IDX].s = "", 678c2ecf20Sopenharmony_ci [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, 688c2ecf20Sopenharmony_ci [USB_GADGET_SERIAL_IDX].s = "", 698c2ecf20Sopenharmony_ci { } /* end of list */ 708c2ecf20Sopenharmony_ci}; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic struct usb_gadget_strings stringtab_dev = { 738c2ecf20Sopenharmony_ci .language = 0x0409, /* en-us */ 748c2ecf20Sopenharmony_ci .strings = strings_dev, 758c2ecf20Sopenharmony_ci}; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic struct usb_gadget_strings *dev_strings[] = { 788c2ecf20Sopenharmony_ci &stringtab_dev, 798c2ecf20Sopenharmony_ci NULL, 808c2ecf20Sopenharmony_ci}; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic struct usb_function_instance *fi_msg; 838c2ecf20Sopenharmony_cistatic struct usb_function *f_msg; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/****************************** Configurations ******************************/ 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic struct fsg_module_parameters mod_data = { 888c2ecf20Sopenharmony_ci .stall = 1 898c2ecf20Sopenharmony_ci}; 908c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_GADGET_DEBUG_FILES 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#else 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/* 978c2ecf20Sopenharmony_ci * Number of buffers we will use. 988c2ecf20Sopenharmony_ci * 2 is usually enough for good buffering pipeline 998c2ecf20Sopenharmony_ci */ 1008c2ecf20Sopenharmony_ci#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci#endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciFSG_MODULE_PARAMETERS(/* no prefix */, mod_data); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic int msg_do_config(struct usb_configuration *c) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci int ret; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci if (gadget_is_otg(c->cdev->gadget)) { 1118c2ecf20Sopenharmony_ci c->descriptors = otg_desc; 1128c2ecf20Sopenharmony_ci c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci f_msg = usb_get_function(fi_msg); 1168c2ecf20Sopenharmony_ci if (IS_ERR(f_msg)) 1178c2ecf20Sopenharmony_ci return PTR_ERR(f_msg); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci ret = usb_add_function(c, f_msg); 1208c2ecf20Sopenharmony_ci if (ret) 1218c2ecf20Sopenharmony_ci goto put_func; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci return 0; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ciput_func: 1268c2ecf20Sopenharmony_ci usb_put_function(f_msg); 1278c2ecf20Sopenharmony_ci return ret; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic struct usb_configuration msg_config_driver = { 1318c2ecf20Sopenharmony_ci .label = "Linux File-Backed Storage", 1328c2ecf20Sopenharmony_ci .bConfigurationValue = 1, 1338c2ecf20Sopenharmony_ci .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 1348c2ecf20Sopenharmony_ci}; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci/****************************** Gadget Bind ******************************/ 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic int msg_bind(struct usb_composite_dev *cdev) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct fsg_opts *opts; 1428c2ecf20Sopenharmony_ci struct fsg_config config; 1438c2ecf20Sopenharmony_ci int status; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci fi_msg = usb_get_function_instance("mass_storage"); 1468c2ecf20Sopenharmony_ci if (IS_ERR(fi_msg)) 1478c2ecf20Sopenharmony_ci return PTR_ERR(fi_msg); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci fsg_config_from_params(&config, &mod_data, fsg_num_buffers); 1508c2ecf20Sopenharmony_ci opts = fsg_opts_from_func_inst(fi_msg); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci opts->no_configfs = true; 1538c2ecf20Sopenharmony_ci status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers); 1548c2ecf20Sopenharmony_ci if (status) 1558c2ecf20Sopenharmony_ci goto fail; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci status = fsg_common_set_cdev(opts->common, cdev, config.can_stall); 1588c2ecf20Sopenharmony_ci if (status) 1598c2ecf20Sopenharmony_ci goto fail_set_cdev; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci fsg_common_set_sysfs(opts->common, true); 1628c2ecf20Sopenharmony_ci status = fsg_common_create_luns(opts->common, &config); 1638c2ecf20Sopenharmony_ci if (status) 1648c2ecf20Sopenharmony_ci goto fail_set_cdev; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci fsg_common_set_inquiry_string(opts->common, config.vendor_name, 1678c2ecf20Sopenharmony_ci config.product_name); 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci status = usb_string_ids_tab(cdev, strings_dev); 1708c2ecf20Sopenharmony_ci if (status < 0) 1718c2ecf20Sopenharmony_ci goto fail_string_ids; 1728c2ecf20Sopenharmony_ci msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) { 1758c2ecf20Sopenharmony_ci struct usb_descriptor_header *usb_desc; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci usb_desc = usb_otg_descriptor_alloc(cdev->gadget); 1788c2ecf20Sopenharmony_ci if (!usb_desc) 1798c2ecf20Sopenharmony_ci goto fail_string_ids; 1808c2ecf20Sopenharmony_ci usb_otg_descriptor_init(cdev->gadget, usb_desc); 1818c2ecf20Sopenharmony_ci otg_desc[0] = usb_desc; 1828c2ecf20Sopenharmony_ci otg_desc[1] = NULL; 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci status = usb_add_config(cdev, &msg_config_driver, msg_do_config); 1868c2ecf20Sopenharmony_ci if (status < 0) 1878c2ecf20Sopenharmony_ci goto fail_otg_desc; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci usb_composite_overwrite_options(cdev, &coverwrite); 1908c2ecf20Sopenharmony_ci dev_info(&cdev->gadget->dev, 1918c2ecf20Sopenharmony_ci DRIVER_DESC ", version: " DRIVER_VERSION "\n"); 1928c2ecf20Sopenharmony_ci return 0; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cifail_otg_desc: 1958c2ecf20Sopenharmony_ci kfree(otg_desc[0]); 1968c2ecf20Sopenharmony_ci otg_desc[0] = NULL; 1978c2ecf20Sopenharmony_cifail_string_ids: 1988c2ecf20Sopenharmony_ci fsg_common_remove_luns(opts->common); 1998c2ecf20Sopenharmony_cifail_set_cdev: 2008c2ecf20Sopenharmony_ci fsg_common_free_buffers(opts->common); 2018c2ecf20Sopenharmony_cifail: 2028c2ecf20Sopenharmony_ci usb_put_function_instance(fi_msg); 2038c2ecf20Sopenharmony_ci return status; 2048c2ecf20Sopenharmony_ci} 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_cistatic int msg_unbind(struct usb_composite_dev *cdev) 2078c2ecf20Sopenharmony_ci{ 2088c2ecf20Sopenharmony_ci if (!IS_ERR(f_msg)) 2098c2ecf20Sopenharmony_ci usb_put_function(f_msg); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (!IS_ERR(fi_msg)) 2128c2ecf20Sopenharmony_ci usb_put_function_instance(fi_msg); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci kfree(otg_desc[0]); 2158c2ecf20Sopenharmony_ci otg_desc[0] = NULL; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci return 0; 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci/****************************** Some noise ******************************/ 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic struct usb_composite_driver msg_driver = { 2238c2ecf20Sopenharmony_ci .name = "g_mass_storage", 2248c2ecf20Sopenharmony_ci .dev = &msg_device_desc, 2258c2ecf20Sopenharmony_ci .max_speed = USB_SPEED_SUPER_PLUS, 2268c2ecf20Sopenharmony_ci .needs_serial = 1, 2278c2ecf20Sopenharmony_ci .strings = dev_strings, 2288c2ecf20Sopenharmony_ci .bind = msg_bind, 2298c2ecf20Sopenharmony_ci .unbind = msg_unbind, 2308c2ecf20Sopenharmony_ci}; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cimodule_usb_composite_driver(msg_driver); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC); 2358c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michal Nazarewicz"); 2368c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 237