Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 1 | /* |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 2 | * mass_storage.c -- Mass Storage USB Gadget |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003-2008 Alan Stern |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 5 | * Copyright (C) 2009 Samsung Electronics |
| 6 | * Author: Michal Nazarewicz <m.nazarewicz@samsung.com> |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 7 | * All rights reserved. |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 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 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | /* |
| 26 | * The Mass Storage Gadget acts as a USB Mass Storage device, |
| 27 | * appearing to the host as a disk drive or as a CD-ROM drive. In |
| 28 | * addition to providing an example of a genuinely useful gadget |
| 29 | * driver for a USB device, it also illustrates a technique of |
| 30 | * double-buffering for increased throughput. Last but not least, it |
| 31 | * gives an easy way to probe the behavior of the Mass Storage drivers |
| 32 | * in a USB host. |
| 33 | * |
| 34 | * Since this file serves only administrative purposes and all the |
| 35 | * business logic is implemented in f_mass_storage.* file. Read |
| 36 | * comments in this file for more detailed description. |
| 37 | */ |
| 38 | |
| 39 | |
| 40 | #include <linux/kernel.h> |
| 41 | #include <linux/utsname.h> |
| 42 | #include <linux/usb/ch9.h> |
| 43 | |
| 44 | |
| 45 | /*-------------------------------------------------------------------------*/ |
| 46 | |
| 47 | #define DRIVER_DESC "Mass Storage Gadget" |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 48 | #define DRIVER_VERSION "2009/09/11" |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 49 | |
| 50 | /*-------------------------------------------------------------------------*/ |
| 51 | |
| 52 | /* |
| 53 | * kbuild is not very cooperative with respect to linking separately |
| 54 | * compiled library objects into one module. So for now we won't use |
| 55 | * separate compilation ... ensuring init/exit sections work to shrink |
| 56 | * the runtime footprint, and giving us at least some parts of what |
| 57 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 58 | */ |
| 59 | |
| 60 | #include "composite.c" |
| 61 | #include "usbstring.c" |
| 62 | #include "config.c" |
| 63 | #include "epautoconf.c" |
| 64 | #include "f_mass_storage.c" |
| 65 | |
| 66 | /*-------------------------------------------------------------------------*/ |
| 67 | |
| 68 | static struct usb_device_descriptor msg_device_desc = { |
| 69 | .bLength = sizeof msg_device_desc, |
| 70 | .bDescriptorType = USB_DT_DEVICE, |
| 71 | |
| 72 | .bcdUSB = cpu_to_le16(0x0200), |
| 73 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 74 | |
| 75 | /* Vendor and product id can be overridden by module parameters. */ |
| 76 | .idVendor = cpu_to_le16(FSG_VENDOR_ID), |
| 77 | .idProduct = cpu_to_le16(FSG_PRODUCT_ID), |
| 78 | /* .bcdDevice = f(hardware) */ |
| 79 | /* .iManufacturer = DYNAMIC */ |
| 80 | /* .iProduct = DYNAMIC */ |
| 81 | /* NO SERIAL NUMBER */ |
| 82 | .bNumConfigurations = 1, |
| 83 | }; |
| 84 | |
| 85 | static struct usb_otg_descriptor otg_descriptor = { |
| 86 | .bLength = sizeof otg_descriptor, |
| 87 | .bDescriptorType = USB_DT_OTG, |
| 88 | |
| 89 | /* REVISIT SRP-only hardware is possible, although |
| 90 | * it would not be called "OTG" ... |
| 91 | */ |
| 92 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 93 | }; |
| 94 | |
| 95 | static const struct usb_descriptor_header *otg_desc[] = { |
| 96 | (struct usb_descriptor_header *) &otg_descriptor, |
| 97 | NULL, |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | /* string IDs are assigned dynamically */ |
| 102 | |
| 103 | #define STRING_MANUFACTURER_IDX 0 |
| 104 | #define STRING_PRODUCT_IDX 1 |
| 105 | #define STRING_CONFIGURATION_IDX 2 |
| 106 | |
| 107 | static char manufacturer[50]; |
| 108 | |
| 109 | static struct usb_string strings_dev[] = { |
| 110 | [STRING_MANUFACTURER_IDX].s = manufacturer, |
| 111 | [STRING_PRODUCT_IDX].s = DRIVER_DESC, |
| 112 | [STRING_CONFIGURATION_IDX].s = "Self Powered", |
| 113 | { } /* end of list */ |
| 114 | }; |
| 115 | |
| 116 | static struct usb_gadget_strings stringtab_dev = { |
| 117 | .language = 0x0409, /* en-us */ |
| 118 | .strings = strings_dev, |
| 119 | }; |
| 120 | |
| 121 | static struct usb_gadget_strings *dev_strings[] = { |
| 122 | &stringtab_dev, |
| 123 | NULL, |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | |
| 128 | /****************************** Configurations ******************************/ |
| 129 | |
Michal Nazarewicz | 481e492 | 2009-11-09 14:15:21 +0100 | [diff] [blame] | 130 | static struct fsg_module_parameters mod_data = { |
| 131 | .stall = 1 |
| 132 | }; |
| 133 | FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); |
| 134 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame^] | 135 | static unsigned long msg_registered = 0; |
| 136 | static void msg_cleanup(void); |
| 137 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 138 | static int __init msg_do_config(struct usb_configuration *c) |
| 139 | { |
| 140 | struct fsg_common *common; |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame^] | 141 | struct fsg_config config; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 142 | int ret; |
| 143 | |
| 144 | if (gadget_is_otg(c->cdev->gadget)) { |
| 145 | c->descriptors = otg_desc; |
| 146 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 147 | } |
| 148 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame^] | 149 | fsg_config_from_params(&config, &mod_data); |
| 150 | config.thread_exits = (void(*)(struct fsg_common*))&msg_cleanup; |
| 151 | common = fsg_common_init(0, c->cdev, &config); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 152 | if (IS_ERR(common)) |
| 153 | return PTR_ERR(common); |
| 154 | |
| 155 | ret = fsg_add(c->cdev, c, common); |
| 156 | fsg_common_put(common); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | static struct usb_configuration msg_config_driver = { |
| 161 | .label = "Linux File-Backed Storage", |
| 162 | .bind = msg_do_config, |
| 163 | .bConfigurationValue = 1, |
| 164 | /* .iConfiguration = DYNAMIC */ |
| 165 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | |
| 170 | /****************************** Gadget Bind ******************************/ |
| 171 | |
| 172 | |
| 173 | static int __init msg_bind(struct usb_composite_dev *cdev) |
| 174 | { |
| 175 | struct usb_gadget *gadget = cdev->gadget; |
| 176 | int status; |
| 177 | |
| 178 | /* Allocate string descriptor numbers ... note that string |
| 179 | * contents can be overridden by the composite_dev glue. |
| 180 | */ |
| 181 | |
| 182 | /* device descriptor strings: manufacturer, product */ |
| 183 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", |
| 184 | init_utsname()->sysname, init_utsname()->release, |
| 185 | gadget->name); |
| 186 | status = usb_string_id(cdev); |
| 187 | if (status < 0) |
| 188 | return status; |
| 189 | strings_dev[STRING_MANUFACTURER_IDX].id = status; |
| 190 | msg_device_desc.iManufacturer = status; |
| 191 | |
| 192 | status = usb_string_id(cdev); |
| 193 | if (status < 0) |
| 194 | return status; |
| 195 | strings_dev[STRING_PRODUCT_IDX].id = status; |
| 196 | msg_device_desc.iProduct = status; |
| 197 | |
| 198 | status = usb_string_id(cdev); |
| 199 | if (status < 0) |
| 200 | return status; |
| 201 | strings_dev[STRING_CONFIGURATION_IDX].id = status; |
| 202 | msg_config_driver.iConfiguration = status; |
| 203 | |
| 204 | /* register our second configuration */ |
| 205 | status = usb_add_config(cdev, &msg_config_driver); |
| 206 | if (status < 0) |
| 207 | return status; |
| 208 | |
| 209 | dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame^] | 210 | set_bit(0, &msg_registered); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /****************************** Some noise ******************************/ |
| 216 | |
| 217 | |
| 218 | static struct usb_composite_driver msg_driver = { |
| 219 | .name = "g_mass_storage", |
| 220 | .dev = &msg_device_desc, |
| 221 | .strings = dev_strings, |
| 222 | .bind = msg_bind, |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 226 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 227 | MODULE_LICENSE("GPL"); |
| 228 | |
| 229 | static int __init msg_init(void) |
| 230 | { |
| 231 | return usb_composite_register(&msg_driver); |
| 232 | } |
| 233 | module_init(msg_init); |
| 234 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame^] | 235 | static void msg_cleanup(void) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 236 | { |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame^] | 237 | if (test_and_clear_bit(0, &msg_registered)) |
| 238 | usb_composite_unregister(&msg_driver); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 239 | } |
| 240 | module_exit(msg_cleanup); |