1 /* 2 * mass_storage.c -- Mass Storage USB Gadget 3 * 4 * Copyright (C) 2003-2008 Alan Stern 5 * Copyright (C) 2009 Samsung Electronics 6 * Author: Michal Nazarewicz <mina86@mina86.com> 7 * All rights reserved. 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 */ 14 15 16 /* 17 * The Mass Storage Gadget acts as a USB Mass Storage device, 18 * appearing to the host as a disk drive or as a CD-ROM drive. In 19 * addition to providing an example of a genuinely useful gadget 20 * driver for a USB device, it also illustrates a technique of 21 * double-buffering for increased throughput. Last but not least, it 22 * gives an easy way to probe the behavior of the Mass Storage drivers 23 * in a USB host. 24 * 25 * Since this file serves only administrative purposes and all the 26 * business logic is implemented in f_mass_storage.* file. Read 27 * comments in this file for more detailed description. 28 */ 29 30 31 #include <linux/kernel.h> 32 #include <linux/usb/ch9.h> 33 #include <linux/module.h> 34 35 /*-------------------------------------------------------------------------*/ 36 37 #define DRIVER_DESC "Mass Storage Gadget" 38 #define DRIVER_VERSION "2009/09/11" 39 40 /* 41 * Thanks to NetChip Technologies for donating this product ID. 42 * 43 * DO NOT REUSE THESE IDs with any other driver!! Ever!! 44 * Instead: allocate your own, using normal USB-IF procedures. 45 */ 46 #define FSG_VENDOR_ID 0x0525 /* NetChip */ 47 #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */ 48 49 #include "f_mass_storage.h" 50 51 /*-------------------------------------------------------------------------*/ 52 USB_GADGET_COMPOSITE_OPTIONS(); 53 54 static struct usb_device_descriptor msg_device_desc = { 55 .bLength = sizeof msg_device_desc, 56 .bDescriptorType = USB_DT_DEVICE, 57 58 /* .bcdUSB = DYNAMIC */ 59 .bDeviceClass = USB_CLASS_PER_INTERFACE, 60 61 /* Vendor and product id can be overridden by module parameters. */ 62 .idVendor = cpu_to_le16(FSG_VENDOR_ID), 63 .idProduct = cpu_to_le16(FSG_PRODUCT_ID), 64 .bNumConfigurations = 1, 65 }; 66 67 static const struct usb_descriptor_header *otg_desc[2]; 68 69 static struct usb_string strings_dev[] = { 70 [USB_GADGET_MANUFACTURER_IDX].s = "", 71 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, 72 [USB_GADGET_SERIAL_IDX].s = "", 73 { } /* end of list */ 74 }; 75 76 static struct usb_gadget_strings stringtab_dev = { 77 .language = 0x0409, /* en-us */ 78 .strings = strings_dev, 79 }; 80 81 static struct usb_gadget_strings *dev_strings[] = { 82 &stringtab_dev, 83 NULL, 84 }; 85 86 static struct usb_function_instance *fi_msg; 87 static struct usb_function *f_msg; 88 89 /****************************** Configurations ******************************/ 90 91 static struct fsg_module_parameters mod_data = { 92 .stall = 1 93 }; 94 #ifdef CONFIG_USB_GADGET_DEBUG_FILES 95 96 static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; 97 98 #else 99 100 /* 101 * Number of buffers we will use. 102 * 2 is usually enough for good buffering pipeline 103 */ 104 #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS 105 106 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 107 108 FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); 109 110 static int msg_do_config(struct usb_configuration *c) 111 { 112 struct fsg_opts *opts; 113 int ret; 114 115 if (gadget_is_otg(c->cdev->gadget)) { 116 c->descriptors = otg_desc; 117 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; 118 } 119 120 opts = fsg_opts_from_func_inst(fi_msg); 121 122 f_msg = usb_get_function(fi_msg); 123 if (IS_ERR(f_msg)) 124 return PTR_ERR(f_msg); 125 126 ret = usb_add_function(c, f_msg); 127 if (ret) 128 goto put_func; 129 130 return 0; 131 132 put_func: 133 usb_put_function(f_msg); 134 return ret; 135 } 136 137 static struct usb_configuration msg_config_driver = { 138 .label = "Linux File-Backed Storage", 139 .bConfigurationValue = 1, 140 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 141 }; 142 143 144 /****************************** Gadget Bind ******************************/ 145 146 static int msg_bind(struct usb_composite_dev *cdev) 147 { 148 struct fsg_opts *opts; 149 struct fsg_config config; 150 int status; 151 152 fi_msg = usb_get_function_instance("mass_storage"); 153 if (IS_ERR(fi_msg)) 154 return PTR_ERR(fi_msg); 155 156 fsg_config_from_params(&config, &mod_data, fsg_num_buffers); 157 opts = fsg_opts_from_func_inst(fi_msg); 158 159 opts->no_configfs = true; 160 status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers); 161 if (status) 162 goto fail; 163 164 status = fsg_common_set_cdev(opts->common, cdev, config.can_stall); 165 if (status) 166 goto fail_set_cdev; 167 168 fsg_common_set_sysfs(opts->common, true); 169 status = fsg_common_create_luns(opts->common, &config); 170 if (status) 171 goto fail_set_cdev; 172 173 fsg_common_set_inquiry_string(opts->common, config.vendor_name, 174 config.product_name); 175 176 status = usb_string_ids_tab(cdev, strings_dev); 177 if (status < 0) 178 goto fail_string_ids; 179 msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; 180 181 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) { 182 struct usb_descriptor_header *usb_desc; 183 184 usb_desc = usb_otg_descriptor_alloc(cdev->gadget); 185 if (!usb_desc) 186 goto fail_string_ids; 187 usb_otg_descriptor_init(cdev->gadget, usb_desc); 188 otg_desc[0] = usb_desc; 189 otg_desc[1] = NULL; 190 } 191 192 status = usb_add_config(cdev, &msg_config_driver, msg_do_config); 193 if (status < 0) 194 goto fail_otg_desc; 195 196 usb_composite_overwrite_options(cdev, &coverwrite); 197 dev_info(&cdev->gadget->dev, 198 DRIVER_DESC ", version: " DRIVER_VERSION "\n"); 199 return 0; 200 201 fail_otg_desc: 202 kfree(otg_desc[0]); 203 otg_desc[0] = NULL; 204 fail_string_ids: 205 fsg_common_remove_luns(opts->common); 206 fail_set_cdev: 207 fsg_common_free_buffers(opts->common); 208 fail: 209 usb_put_function_instance(fi_msg); 210 return status; 211 } 212 213 static int msg_unbind(struct usb_composite_dev *cdev) 214 { 215 if (!IS_ERR(f_msg)) 216 usb_put_function(f_msg); 217 218 if (!IS_ERR(fi_msg)) 219 usb_put_function_instance(fi_msg); 220 221 kfree(otg_desc[0]); 222 otg_desc[0] = NULL; 223 224 return 0; 225 } 226 227 /****************************** Some noise ******************************/ 228 229 static struct usb_composite_driver msg_driver = { 230 .name = "g_mass_storage", 231 .dev = &msg_device_desc, 232 .max_speed = USB_SPEED_SUPER, 233 .needs_serial = 1, 234 .strings = dev_strings, 235 .bind = msg_bind, 236 .unbind = msg_unbind, 237 }; 238 239 MODULE_DESCRIPTION(DRIVER_DESC); 240 MODULE_AUTHOR("Michal Nazarewicz"); 241 MODULE_LICENSE("GPL"); 242 243 static int __init msg_init(void) 244 { 245 return usb_composite_probe(&msg_driver); 246 } 247 module_init(msg_init); 248 249 static void __exit msg_cleanup(void) 250 { 251 usb_composite_unregister(&msg_driver); 252 } 253 module_exit(msg_cleanup); 254