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 |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 6 | * Author: Michal Nazarewicz <mina86@mina86.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. |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 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> |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 32 | #include <linux/usb/ch9.h> |
Sebastian Andrzej Siewior | 721e2e9 | 2012-09-06 20:11:27 +0200 | [diff] [blame] | 33 | #include <linux/module.h> |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 34 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 35 | /*-------------------------------------------------------------------------*/ |
| 36 | |
| 37 | #define DRIVER_DESC "Mass Storage Gadget" |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 38 | #define DRIVER_VERSION "2009/09/11" |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 39 | |
| 40 | /*-------------------------------------------------------------------------*/ |
| 41 | |
| 42 | /* |
| 43 | * kbuild is not very cooperative with respect to linking separately |
| 44 | * compiled library objects into one module. So for now we won't use |
| 45 | * separate compilation ... ensuring init/exit sections work to shrink |
| 46 | * the runtime footprint, and giving us at least some parts of what |
| 47 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. |
| 48 | */ |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 49 | #include "f_mass_storage.c" |
| 50 | |
| 51 | /*-------------------------------------------------------------------------*/ |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 52 | USB_GADGET_COMPOSITE_OPTIONS(); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 53 | |
| 54 | static struct usb_device_descriptor msg_device_desc = { |
| 55 | .bLength = sizeof msg_device_desc, |
| 56 | .bDescriptorType = USB_DT_DEVICE, |
| 57 | |
| 58 | .bcdUSB = cpu_to_le16(0x0200), |
| 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), |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 64 | .bNumConfigurations = 1, |
| 65 | }; |
| 66 | |
| 67 | static struct usb_otg_descriptor otg_descriptor = { |
| 68 | .bLength = sizeof otg_descriptor, |
| 69 | .bDescriptorType = USB_DT_OTG, |
| 70 | |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 71 | /* |
| 72 | * REVISIT SRP-only hardware is possible, although |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 73 | * it would not be called "OTG" ... |
| 74 | */ |
| 75 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 76 | }; |
| 77 | |
| 78 | static const struct usb_descriptor_header *otg_desc[] = { |
| 79 | (struct usb_descriptor_header *) &otg_descriptor, |
| 80 | NULL, |
| 81 | }; |
| 82 | |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 83 | static struct usb_string strings_dev[] = { |
| 84 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 85 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 86 | [USB_GADGET_SERIAL_IDX].s = "", |
| 87 | { } /* end of list */ |
| 88 | }; |
| 89 | |
| 90 | static struct usb_gadget_strings stringtab_dev = { |
| 91 | .language = 0x0409, /* en-us */ |
| 92 | .strings = strings_dev, |
| 93 | }; |
| 94 | |
| 95 | static struct usb_gadget_strings *dev_strings[] = { |
| 96 | &stringtab_dev, |
| 97 | NULL, |
| 98 | }; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 99 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 100 | /****************************** Configurations ******************************/ |
| 101 | |
Michal Nazarewicz | 481e492 | 2009-11-09 14:15:21 +0100 | [diff] [blame] | 102 | static struct fsg_module_parameters mod_data = { |
| 103 | .stall = 1 |
| 104 | }; |
| 105 | FSG_MODULE_PARAMETERS(/* no prefix */, mod_data); |
| 106 | |
Michal Nazarewicz | b73af61 | 2010-10-28 17:31:23 +0200 | [diff] [blame] | 107 | static unsigned long msg_registered; |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 108 | static void msg_cleanup(void); |
| 109 | |
Michal Nazarewicz | 7f1ee82 | 2010-01-28 13:05:26 +0100 | [diff] [blame] | 110 | static int msg_thread_exits(struct fsg_common *common) |
| 111 | { |
| 112 | msg_cleanup(); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 116 | static int __init msg_do_config(struct usb_configuration *c) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 117 | { |
Michal Nazarewicz | 8876f5e | 2010-06-21 13:57:09 +0200 | [diff] [blame] | 118 | static const struct fsg_operations ops = { |
| 119 | .thread_exits = msg_thread_exits, |
| 120 | }; |
Michal Nazarewicz | 26eca10 | 2010-06-16 12:07:56 +0200 | [diff] [blame] | 121 | static struct fsg_common common; |
| 122 | |
| 123 | struct fsg_common *retp; |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 124 | struct fsg_config config; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 125 | int ret; |
| 126 | |
| 127 | if (gadget_is_otg(c->cdev->gadget)) { |
| 128 | c->descriptors = otg_desc; |
| 129 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 130 | } |
| 131 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 132 | fsg_config_from_params(&config, &mod_data); |
Michal Nazarewicz | 8876f5e | 2010-06-21 13:57:09 +0200 | [diff] [blame] | 133 | config.ops = &ops; |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 134 | |
Michal Nazarewicz | 26eca10 | 2010-06-16 12:07:56 +0200 | [diff] [blame] | 135 | retp = fsg_common_init(&common, c->cdev, &config); |
| 136 | if (IS_ERR(retp)) |
| 137 | return PTR_ERR(retp); |
| 138 | |
Michal Nazarewicz | 1dc9098 | 2010-06-16 12:07:57 +0200 | [diff] [blame] | 139 | ret = fsg_bind_config(c->cdev, c, &common); |
Michal Nazarewicz | 26eca10 | 2010-06-16 12:07:56 +0200 | [diff] [blame] | 140 | fsg_common_put(&common); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | static struct usb_configuration msg_config_driver = { |
| 145 | .label = "Linux File-Backed Storage", |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 146 | .bConfigurationValue = 1, |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 147 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, |
| 148 | }; |
| 149 | |
| 150 | |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 151 | /****************************** Gadget Bind ******************************/ |
| 152 | |
Michal Nazarewicz | e12995e | 2010-08-12 17:43:52 +0200 | [diff] [blame] | 153 | static int __init msg_bind(struct usb_composite_dev *cdev) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 154 | { |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 155 | int status; |
| 156 | |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 157 | status = usb_string_ids_tab(cdev, strings_dev); |
| 158 | if (status < 0) |
| 159 | return status; |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 160 | msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 161 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 162 | status = usb_add_config(cdev, &msg_config_driver, msg_do_config); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 163 | if (status < 0) |
| 164 | return status; |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 165 | usb_composite_overwrite_options(cdev, &coverwrite); |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 166 | dev_info(&cdev->gadget->dev, |
| 167 | DRIVER_DESC ", version: " DRIVER_VERSION "\n"); |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 168 | set_bit(0, &msg_registered); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /****************************** Some noise ******************************/ |
| 174 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 175 | static __refdata struct usb_composite_driver msg_driver = { |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 176 | .name = "g_mass_storage", |
| 177 | .dev = &msg_device_desc, |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 178 | .max_speed = USB_SPEED_SUPER, |
Michal Nazarewicz | 7c2b61d | 2010-08-12 17:43:47 +0200 | [diff] [blame] | 179 | .needs_serial = 1, |
Sebastian Andrzej Siewior | 1cf0d26 | 2012-09-10 15:01:54 +0200 | [diff] [blame] | 180 | .strings = dev_strings, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 181 | .bind = msg_bind, |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 185 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 186 | MODULE_LICENSE("GPL"); |
| 187 | |
| 188 | static int __init msg_init(void) |
| 189 | { |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 190 | return usb_composite_probe(&msg_driver); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 191 | } |
| 192 | module_init(msg_init); |
| 193 | |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 194 | static void msg_cleanup(void) |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 195 | { |
Michal Nazarewicz | c85efcb | 2009-11-09 14:15:26 +0100 | [diff] [blame] | 196 | if (test_and_clear_bit(0, &msg_registered)) |
| 197 | usb_composite_unregister(&msg_driver); |
Michal Nazarewicz | d23b0f0 | 2009-11-09 14:15:20 +0100 | [diff] [blame] | 198 | } |
| 199 | module_exit(msg_cleanup); |