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), |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 78 | .bNumConfigurations = 1, |
| 79 | }; |
| 80 | |
| 81 | static struct usb_otg_descriptor otg_descriptor = { |
| 82 | .bLength = sizeof otg_descriptor, |
| 83 | .bDescriptorType = USB_DT_OTG, |
| 84 | |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 85 | /* |
| 86 | * REVISIT SRP-only hardware is possible, although |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 87 | * it would not be called "OTG" ... |
| 88 | */ |
| 89 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 90 | }; |
| 91 | |
| 92 | static const struct usb_descriptor_header *otg_desc[] = { |
| 93 | (struct usb_descriptor_header *) &otg_descriptor, |
| 94 | NULL, |
| 95 | }; |
| 96 | |
| 97 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 98 | /****************************** Configurations ******************************/ |
| 99 | |
Michal Nazarewicz | 481e492 | 2009-11-09 14:15:21 +0100 | [diff] [blame] | 100 | static struct fsg_module_parameters mod_data = { |
| 101 | .stall = 1 |
| 102 | }; |
| 103 | FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); |
| 104 | |
Michal Nazarewicz | b73af61 | 2010-10-28 17:31:23 +0200 | [diff] [blame^] | 105 | static unsigned long msg_registered; |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 106 | static void msg_cleanup(void); |
| 107 | |
Michal Nazarewicz | 7f1ee82 | 2010-01-28 13:05:26 +0100 | [diff] [blame] | 108 | static int msg_thread_exits(struct fsg_common *common) |
| 109 | { |
| 110 | msg_cleanup(); |
| 111 | return 0; |
| 112 | } |
| 113 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 114 | static int __init msg_do_config(struct usb_configuration *c) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 115 | { |
Michal Nazarewicz | 8876f5e | 2010-06-21 13:57:09 +0200 | [diff] [blame] | 116 | static const struct fsg_operations ops = { |
| 117 | .thread_exits = msg_thread_exits, |
| 118 | }; |
Michal Nazarewicz | 26eca10 | 2010-06-16 12:07:56 +0200 | [diff] [blame] | 119 | static struct fsg_common common; |
| 120 | |
| 121 | struct fsg_common *retp; |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 122 | struct fsg_config config; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 123 | int ret; |
| 124 | |
| 125 | if (gadget_is_otg(c->cdev->gadget)) { |
| 126 | c->descriptors = otg_desc; |
| 127 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 128 | } |
| 129 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 130 | fsg_config_from_params(&config, &mod_data); |
Michal Nazarewicz | 8876f5e | 2010-06-21 13:57:09 +0200 | [diff] [blame] | 131 | config.ops = &ops; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 132 | |
Michal Nazarewicz | 26eca10 | 2010-06-16 12:07:56 +0200 | [diff] [blame] | 133 | retp = fsg_common_init(&common, c->cdev, &config); |
| 134 | if (IS_ERR(retp)) |
| 135 | return PTR_ERR(retp); |
| 136 | |
Michal Nazarewicz | 1dc9098 | 2010-06-16 12:07:57 +0200 | [diff] [blame] | 137 | ret = fsg_bind_config(c->cdev, c, &common); |
Michal Nazarewicz | 26eca10 | 2010-06-16 12:07:56 +0200 | [diff] [blame] | 138 | fsg_common_put(&common); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static struct usb_configuration msg_config_driver = { |
| 143 | .label = "Linux File-Backed Storage", |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 144 | .bConfigurationValue = 1, |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 145 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 146 | }; |
| 147 | |
| 148 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 149 | /****************************** Gadget Bind ******************************/ |
| 150 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 151 | static int __init msg_bind(struct usb_composite_dev *cdev) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 152 | { |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 153 | int status; |
| 154 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 155 | status = usb_add_config(cdev, &msg_config_driver, msg_do_config); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 156 | if (status < 0) |
| 157 | return status; |
| 158 | |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 159 | dev_info(&cdev->gadget->dev, |
| 160 | DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 161 | set_bit(0, &msg_registered); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /****************************** Some noise ******************************/ |
| 167 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 168 | static struct usb_composite_driver msg_driver = { |
| 169 | .name = "g_mass_storage", |
| 170 | .dev = &msg_device_desc, |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 171 | .iProduct = DRIVER_DESC, |
| 172 | .needs_serial = 1, |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 176 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 177 | MODULE_LICENSE("GPL"); |
| 178 | |
| 179 | static int __init msg_init(void) |
| 180 | { |
Michal Nazarewicz | 07a18bd | 2010-08-12 17:43:54 +0200 | [diff] [blame] | 181 | return usb_composite_probe(&msg_driver, msg_bind); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 182 | } |
| 183 | module_init(msg_init); |
| 184 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 185 | static void msg_cleanup(void) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 186 | { |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 187 | if (test_and_clear_bit(0, &msg_registered)) |
| 188 | usb_composite_unregister(&msg_driver); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 189 | } |
| 190 | module_exit(msg_cleanup); |