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