1 // SPDX-License-Identifier: GPL-2.0+
3 * mass_storage.c -- Mass Storage USB Gadget
5 * Copyright (C) 2003-2008 Alan Stern
6 * Copyright (C) 2009 Samsung Electronics
7 * Author: Michal Nazarewicz <mina86@mina86.com>
13 * The Mass Storage Gadget acts as a USB Mass Storage device,
14 * appearing to the host as a disk drive or as a CD-ROM drive. In
15 * addition to providing an example of a genuinely useful gadget
16 * driver for a USB device, it also illustrates a technique of
17 * double-buffering for increased throughput. Last but not least, it
18 * gives an easy way to probe the behavior of the Mass Storage drivers
21 * Since this file serves only administrative purposes and all the
22 * business logic is implemented in f_mass_storage.* file. Read
23 * comments in this file for more detailed description.
27 #include <linux/kernel.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/module.h>
31 /*-------------------------------------------------------------------------*/
33 #define DRIVER_DESC "Mass Storage Gadget"
34 #define DRIVER_VERSION "2009/09/11"
37 * Thanks to NetChip Technologies for donating this product ID.
39 * DO NOT REUSE THESE IDs with any other driver!! Ever!!
40 * Instead: allocate your own, using normal USB-IF procedures.
42 #define FSG_VENDOR_ID 0x0525 /* NetChip */
43 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
45 #include "f_mass_storage.h"
47 /*-------------------------------------------------------------------------*/
48 USB_GADGET_COMPOSITE_OPTIONS();
50 static struct usb_device_descriptor msg_device_desc
= {
51 .bLength
= sizeof msg_device_desc
,
52 .bDescriptorType
= USB_DT_DEVICE
,
54 /* .bcdUSB = DYNAMIC */
55 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
57 /* Vendor and product id can be overridden by module parameters. */
58 .idVendor
= cpu_to_le16(FSG_VENDOR_ID
),
59 .idProduct
= cpu_to_le16(FSG_PRODUCT_ID
),
60 .bNumConfigurations
= 1,
63 static const struct usb_descriptor_header
*otg_desc
[2];
65 static struct usb_string strings_dev
[] = {
66 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
67 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
68 [USB_GADGET_SERIAL_IDX
].s
= "",
72 static struct usb_gadget_strings stringtab_dev
= {
73 .language
= 0x0409, /* en-us */
74 .strings
= strings_dev
,
77 static struct usb_gadget_strings
*dev_strings
[] = {
82 static struct usb_function_instance
*fi_msg
;
83 static struct usb_function
*f_msg
;
85 /****************************** Configurations ******************************/
87 static struct fsg_module_parameters mod_data
= {
90 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
92 static unsigned int fsg_num_buffers
= CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
;
97 * Number of buffers we will use.
98 * 2 is usually enough for good buffering pipeline
100 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
102 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
104 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data
);
106 static int msg_do_config(struct usb_configuration
*c
)
110 if (gadget_is_otg(c
->cdev
->gadget
)) {
111 c
->descriptors
= otg_desc
;
112 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
115 f_msg
= usb_get_function(fi_msg
);
117 return PTR_ERR(f_msg
);
119 ret
= usb_add_function(c
, f_msg
);
126 usb_put_function(f_msg
);
130 static struct usb_configuration msg_config_driver
= {
131 .label
= "Linux File-Backed Storage",
132 .bConfigurationValue
= 1,
133 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
137 /****************************** Gadget Bind ******************************/
139 static int msg_bind(struct usb_composite_dev
*cdev
)
141 struct fsg_opts
*opts
;
142 struct fsg_config config
;
145 fi_msg
= usb_get_function_instance("mass_storage");
147 return PTR_ERR(fi_msg
);
149 fsg_config_from_params(&config
, &mod_data
, fsg_num_buffers
);
150 opts
= fsg_opts_from_func_inst(fi_msg
);
152 opts
->no_configfs
= true;
153 status
= fsg_common_set_num_buffers(opts
->common
, fsg_num_buffers
);
157 status
= fsg_common_set_cdev(opts
->common
, cdev
, config
.can_stall
);
161 fsg_common_set_sysfs(opts
->common
, true);
162 status
= fsg_common_create_luns(opts
->common
, &config
);
166 fsg_common_set_inquiry_string(opts
->common
, config
.vendor_name
,
167 config
.product_name
);
169 status
= usb_string_ids_tab(cdev
, strings_dev
);
171 goto fail_string_ids
;
172 msg_device_desc
.iProduct
= strings_dev
[USB_GADGET_PRODUCT_IDX
].id
;
174 if (gadget_is_otg(cdev
->gadget
) && !otg_desc
[0]) {
175 struct usb_descriptor_header
*usb_desc
;
177 usb_desc
= usb_otg_descriptor_alloc(cdev
->gadget
);
179 goto fail_string_ids
;
180 usb_otg_descriptor_init(cdev
->gadget
, usb_desc
);
181 otg_desc
[0] = usb_desc
;
185 status
= usb_add_config(cdev
, &msg_config_driver
, msg_do_config
);
189 usb_composite_overwrite_options(cdev
, &coverwrite
);
190 dev_info(&cdev
->gadget
->dev
,
191 DRIVER_DESC
", version: " DRIVER_VERSION
"\n");
198 fsg_common_remove_luns(opts
->common
);
200 fsg_common_free_buffers(opts
->common
);
202 usb_put_function_instance(fi_msg
);
206 static int msg_unbind(struct usb_composite_dev
*cdev
)
209 usb_put_function(f_msg
);
212 usb_put_function_instance(fi_msg
);
220 /****************************** Some noise ******************************/
222 static struct usb_composite_driver msg_driver
= {
223 .name
= "g_mass_storage",
224 .dev
= &msg_device_desc
,
225 .max_speed
= USB_SPEED_SUPER_PLUS
,
227 .strings
= dev_strings
,
229 .unbind
= msg_unbind
,
232 module_usb_composite_driver(msg_driver
);
234 MODULE_DESCRIPTION(DRIVER_DESC
);
235 MODULE_AUTHOR("Michal Nazarewicz");
236 MODULE_LICENSE("GPL");