18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * acm_ms.c -- Composite driver, with ACM and mass storage support 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2008 David Brownell 68c2ecf20Sopenharmony_ci * Copyright (C) 2008 Nokia Corporation 78c2ecf20Sopenharmony_ci * Author: David Brownell 88c2ecf20Sopenharmony_ci * Modified: Klaus Schwarzkopf <schwarzkopf@sensortherm.de> 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Heavily based on multi.c and cdc2.c 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/kernel.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include "u_serial.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define DRIVER_DESC "Composite Gadget (ACM + MS)" 198c2ecf20Sopenharmony_ci#define DRIVER_VERSION "2011/10/10" 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* 248c2ecf20Sopenharmony_ci * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! 258c2ecf20Sopenharmony_ci * Instead: allocate your own, using normal USB-IF procedures. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci#define ACM_MS_VENDOR_NUM 0x1d6b /* Linux Foundation */ 288c2ecf20Sopenharmony_ci#define ACM_MS_PRODUCT_NUM 0x0106 /* Composite Gadget: ACM + MS*/ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#include "f_mass_storage.h" 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 338c2ecf20Sopenharmony_ciUSB_GADGET_COMPOSITE_OPTIONS(); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic struct usb_device_descriptor device_desc = { 368c2ecf20Sopenharmony_ci .bLength = sizeof device_desc, 378c2ecf20Sopenharmony_ci .bDescriptorType = USB_DT_DEVICE, 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci /* .bcdUSB = DYNAMIC */ 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci .bDeviceClass = USB_CLASS_MISC /* 0xEF */, 428c2ecf20Sopenharmony_ci .bDeviceSubClass = 2, 438c2ecf20Sopenharmony_ci .bDeviceProtocol = 1, 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci /* .bMaxPacketSize0 = f(hardware) */ 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci /* Vendor and product id can be overridden by module parameters. */ 488c2ecf20Sopenharmony_ci .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM), 498c2ecf20Sopenharmony_ci .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM), 508c2ecf20Sopenharmony_ci /* .bcdDevice = f(hardware) */ 518c2ecf20Sopenharmony_ci /* .iManufacturer = DYNAMIC */ 528c2ecf20Sopenharmony_ci /* .iProduct = DYNAMIC */ 538c2ecf20Sopenharmony_ci /* NO SERIAL NUMBER */ 548c2ecf20Sopenharmony_ci /*.bNumConfigurations = DYNAMIC*/ 558c2ecf20Sopenharmony_ci}; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic const struct usb_descriptor_header *otg_desc[2]; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci/* string IDs are assigned dynamically */ 608c2ecf20Sopenharmony_cistatic struct usb_string strings_dev[] = { 618c2ecf20Sopenharmony_ci [USB_GADGET_MANUFACTURER_IDX].s = "", 628c2ecf20Sopenharmony_ci [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, 638c2ecf20Sopenharmony_ci [USB_GADGET_SERIAL_IDX].s = "", 648c2ecf20Sopenharmony_ci { } /* end of list */ 658c2ecf20Sopenharmony_ci}; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic struct usb_gadget_strings stringtab_dev = { 688c2ecf20Sopenharmony_ci .language = 0x0409, /* en-us */ 698c2ecf20Sopenharmony_ci .strings = strings_dev, 708c2ecf20Sopenharmony_ci}; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic struct usb_gadget_strings *dev_strings[] = { 738c2ecf20Sopenharmony_ci &stringtab_dev, 748c2ecf20Sopenharmony_ci NULL, 758c2ecf20Sopenharmony_ci}; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/****************************** Configurations ******************************/ 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic struct fsg_module_parameters fsg_mod_data = { .stall = 1 }; 808c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_GADGET_DEBUG_FILES 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci#else 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci/* 878c2ecf20Sopenharmony_ci * Number of buffers we will use. 888c2ecf20Sopenharmony_ci * 2 is usually enough for good buffering pipeline 898c2ecf20Sopenharmony_ci */ 908c2ecf20Sopenharmony_ci#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci#endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ciFSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 978c2ecf20Sopenharmony_cistatic struct usb_function *f_acm; 988c2ecf20Sopenharmony_cistatic struct usb_function_instance *f_acm_inst; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic struct usb_function_instance *fi_msg; 1018c2ecf20Sopenharmony_cistatic struct usb_function *f_msg; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci/* 1048c2ecf20Sopenharmony_ci * We _always_ have both ACM and mass storage functions. 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_cistatic int acm_ms_do_config(struct usb_configuration *c) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci int status; 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_acm = usb_get_function(f_acm_inst); 1168c2ecf20Sopenharmony_ci if (IS_ERR(f_acm)) 1178c2ecf20Sopenharmony_ci return PTR_ERR(f_acm); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci f_msg = usb_get_function(fi_msg); 1208c2ecf20Sopenharmony_ci if (IS_ERR(f_msg)) { 1218c2ecf20Sopenharmony_ci status = PTR_ERR(f_msg); 1228c2ecf20Sopenharmony_ci goto put_acm; 1238c2ecf20Sopenharmony_ci } 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci status = usb_add_function(c, f_acm); 1268c2ecf20Sopenharmony_ci if (status < 0) 1278c2ecf20Sopenharmony_ci goto put_msg; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci status = usb_add_function(c, f_msg); 1308c2ecf20Sopenharmony_ci if (status) 1318c2ecf20Sopenharmony_ci goto remove_acm; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci return 0; 1348c2ecf20Sopenharmony_ciremove_acm: 1358c2ecf20Sopenharmony_ci usb_remove_function(c, f_acm); 1368c2ecf20Sopenharmony_ciput_msg: 1378c2ecf20Sopenharmony_ci usb_put_function(f_msg); 1388c2ecf20Sopenharmony_ciput_acm: 1398c2ecf20Sopenharmony_ci usb_put_function(f_acm); 1408c2ecf20Sopenharmony_ci return status; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic struct usb_configuration acm_ms_config_driver = { 1448c2ecf20Sopenharmony_ci .label = DRIVER_DESC, 1458c2ecf20Sopenharmony_ci .bConfigurationValue = 1, 1468c2ecf20Sopenharmony_ci /* .iConfiguration = DYNAMIC */ 1478c2ecf20Sopenharmony_ci .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 1488c2ecf20Sopenharmony_ci}; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic int acm_ms_bind(struct usb_composite_dev *cdev) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci struct usb_gadget *gadget = cdev->gadget; 1558c2ecf20Sopenharmony_ci struct fsg_opts *opts; 1568c2ecf20Sopenharmony_ci struct fsg_config config; 1578c2ecf20Sopenharmony_ci int status; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci f_acm_inst = usb_get_function_instance("acm"); 1608c2ecf20Sopenharmony_ci if (IS_ERR(f_acm_inst)) 1618c2ecf20Sopenharmony_ci return PTR_ERR(f_acm_inst); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci fi_msg = usb_get_function_instance("mass_storage"); 1648c2ecf20Sopenharmony_ci if (IS_ERR(fi_msg)) { 1658c2ecf20Sopenharmony_ci status = PTR_ERR(fi_msg); 1668c2ecf20Sopenharmony_ci goto fail_get_msg; 1678c2ecf20Sopenharmony_ci } 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci /* set up mass storage function */ 1708c2ecf20Sopenharmony_ci fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers); 1718c2ecf20Sopenharmony_ci opts = fsg_opts_from_func_inst(fi_msg); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci opts->no_configfs = true; 1748c2ecf20Sopenharmony_ci status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers); 1758c2ecf20Sopenharmony_ci if (status) 1768c2ecf20Sopenharmony_ci goto fail; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci status = fsg_common_set_cdev(opts->common, cdev, config.can_stall); 1798c2ecf20Sopenharmony_ci if (status) 1808c2ecf20Sopenharmony_ci goto fail_set_cdev; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci fsg_common_set_sysfs(opts->common, true); 1838c2ecf20Sopenharmony_ci status = fsg_common_create_luns(opts->common, &config); 1848c2ecf20Sopenharmony_ci if (status) 1858c2ecf20Sopenharmony_ci goto fail_set_cdev; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci fsg_common_set_inquiry_string(opts->common, config.vendor_name, 1888c2ecf20Sopenharmony_ci config.product_name); 1898c2ecf20Sopenharmony_ci /* 1908c2ecf20Sopenharmony_ci * Allocate string descriptor numbers ... note that string 1918c2ecf20Sopenharmony_ci * contents can be overridden by the composite_dev glue. 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_ci status = usb_string_ids_tab(cdev, strings_dev); 1948c2ecf20Sopenharmony_ci if (status < 0) 1958c2ecf20Sopenharmony_ci goto fail_string_ids; 1968c2ecf20Sopenharmony_ci device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; 1978c2ecf20Sopenharmony_ci device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci if (gadget_is_otg(gadget) && !otg_desc[0]) { 2008c2ecf20Sopenharmony_ci struct usb_descriptor_header *usb_desc; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci usb_desc = usb_otg_descriptor_alloc(gadget); 2038c2ecf20Sopenharmony_ci if (!usb_desc) { 2048c2ecf20Sopenharmony_ci status = -ENOMEM; 2058c2ecf20Sopenharmony_ci goto fail_string_ids; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci usb_otg_descriptor_init(gadget, usb_desc); 2088c2ecf20Sopenharmony_ci otg_desc[0] = usb_desc; 2098c2ecf20Sopenharmony_ci otg_desc[1] = NULL; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* register our configuration */ 2138c2ecf20Sopenharmony_ci status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config); 2148c2ecf20Sopenharmony_ci if (status < 0) 2158c2ecf20Sopenharmony_ci goto fail_otg_desc; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci usb_composite_overwrite_options(cdev, &coverwrite); 2188c2ecf20Sopenharmony_ci dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", 2198c2ecf20Sopenharmony_ci DRIVER_DESC); 2208c2ecf20Sopenharmony_ci return 0; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* error recovery */ 2238c2ecf20Sopenharmony_cifail_otg_desc: 2248c2ecf20Sopenharmony_ci kfree(otg_desc[0]); 2258c2ecf20Sopenharmony_ci otg_desc[0] = NULL; 2268c2ecf20Sopenharmony_cifail_string_ids: 2278c2ecf20Sopenharmony_ci fsg_common_remove_luns(opts->common); 2288c2ecf20Sopenharmony_cifail_set_cdev: 2298c2ecf20Sopenharmony_ci fsg_common_free_buffers(opts->common); 2308c2ecf20Sopenharmony_cifail: 2318c2ecf20Sopenharmony_ci usb_put_function_instance(fi_msg); 2328c2ecf20Sopenharmony_cifail_get_msg: 2338c2ecf20Sopenharmony_ci usb_put_function_instance(f_acm_inst); 2348c2ecf20Sopenharmony_ci return status; 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistatic int acm_ms_unbind(struct usb_composite_dev *cdev) 2388c2ecf20Sopenharmony_ci{ 2398c2ecf20Sopenharmony_ci usb_put_function(f_msg); 2408c2ecf20Sopenharmony_ci usb_put_function_instance(fi_msg); 2418c2ecf20Sopenharmony_ci usb_put_function(f_acm); 2428c2ecf20Sopenharmony_ci usb_put_function_instance(f_acm_inst); 2438c2ecf20Sopenharmony_ci kfree(otg_desc[0]); 2448c2ecf20Sopenharmony_ci otg_desc[0] = NULL; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci return 0; 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_cistatic struct usb_composite_driver acm_ms_driver = { 2508c2ecf20Sopenharmony_ci .name = "g_acm_ms", 2518c2ecf20Sopenharmony_ci .dev = &device_desc, 2528c2ecf20Sopenharmony_ci .max_speed = USB_SPEED_SUPER, 2538c2ecf20Sopenharmony_ci .strings = dev_strings, 2548c2ecf20Sopenharmony_ci .bind = acm_ms_bind, 2558c2ecf20Sopenharmony_ci .unbind = acm_ms_unbind, 2568c2ecf20Sopenharmony_ci}; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_cimodule_usb_composite_driver(acm_ms_driver); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC); 2618c2ecf20Sopenharmony_ciMODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>"); 2628c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 263