blob: 1f376eba31f6f8a4b4f6abf02577dac5336e0d79 [file] [log] [blame]
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01001/*
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01002 * mass_storage.c -- Mass Storage USB Gadget
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01003 *
4 * Copyright (C) 2003-2008 Alan Stern
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01005 * Copyright (C) 2009 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01006 * Author: Michal Nazarewicz <mina86@mina86.com>
Michal Nazarewiczd26a6aa2009-11-09 14:15:23 +01007 * All rights reserved.
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +01008 *
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 Nazarewiczd23b0f02009-11-09 14:15:20 +010013 */
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 Nazarewiczd26a6aa2009-11-09 14:15:23 +010039#define DRIVER_VERSION "2009/09/11"
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010040
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"
52#include "usbstring.c"
53#include "config.c"
54#include "epautoconf.c"
55#include "f_mass_storage.c"
56
57/*-------------------------------------------------------------------------*/
58
59static struct usb_device_descriptor msg_device_desc = {
60 .bLength = sizeof msg_device_desc,
61 .bDescriptorType = USB_DT_DEVICE,
62
63 .bcdUSB = cpu_to_le16(0x0200),
64 .bDeviceClass = USB_CLASS_PER_INTERFACE,
65
66 /* Vendor and product id can be overridden by module parameters. */
67 .idVendor = cpu_to_le16(FSG_VENDOR_ID),
68 .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010069 .bNumConfigurations = 1,
70};
71
72static struct usb_otg_descriptor otg_descriptor = {
73 .bLength = sizeof otg_descriptor,
74 .bDescriptorType = USB_DT_OTG,
75
Michal Nazarewicz7c2b61d2010-08-12 17:43:47 +020076 /*
77 * REVISIT SRP-only hardware is possible, although
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010078 * it would not be called "OTG" ...
79 */
80 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
81};
82
83static const struct usb_descriptor_header *otg_desc[] = {
84 (struct usb_descriptor_header *) &otg_descriptor,
85 NULL,
86};
87
88
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +010089/****************************** Configurations ******************************/
90
Michal Nazarewicz481e4922009-11-09 14:15:21 +010091static struct fsg_module_parameters mod_data = {
92 .stall = 1
93};
94FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
95
Michal Nazarewiczb73af612010-10-28 17:31:23 +020096static unsigned long msg_registered;
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +010097static void msg_cleanup(void);
98
Michal Nazarewicz7f1ee822010-01-28 13:05:26 +010099static int msg_thread_exits(struct fsg_common *common)
100{
101 msg_cleanup();
102 return 0;
103}
104
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200105static int __init msg_do_config(struct usb_configuration *c)
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100106{
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +0200107 static const struct fsg_operations ops = {
108 .thread_exits = msg_thread_exits,
109 };
Michal Nazarewicz26eca10e2010-06-16 12:07:56 +0200110 static struct fsg_common common;
111
112 struct fsg_common *retp;
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100113 struct fsg_config config;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100114 int ret;
115
116 if (gadget_is_otg(c->cdev->gadget)) {
117 c->descriptors = otg_desc;
118 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
119 }
120
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100121 fsg_config_from_params(&config, &mod_data);
Michal Nazarewicz8876f5e2010-06-21 13:57:09 +0200122 config.ops = &ops;
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100123
Michal Nazarewicz26eca10e2010-06-16 12:07:56 +0200124 retp = fsg_common_init(&common, c->cdev, &config);
125 if (IS_ERR(retp))
126 return PTR_ERR(retp);
127
Michal Nazarewicz1dc90982010-06-16 12:07:57 +0200128 ret = fsg_bind_config(c->cdev, c, &common);
Michal Nazarewicz26eca10e2010-06-16 12:07:56 +0200129 fsg_common_put(&common);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100130 return ret;
131}
132
133static struct usb_configuration msg_config_driver = {
134 .label = "Linux File-Backed Storage",
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100135 .bConfigurationValue = 1,
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100136 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
137};
138
139
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100140/****************************** Gadget Bind ******************************/
141
Michal Nazarewicze12995e2010-08-12 17:43:52 +0200142static int __init msg_bind(struct usb_composite_dev *cdev)
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100143{
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100144 int status;
145
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200146 status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100147 if (status < 0)
148 return status;
149
Michal Nazarewicz7c2b61d2010-08-12 17:43:47 +0200150 dev_info(&cdev->gadget->dev,
151 DRIVER_DESC ", version: " DRIVER_VERSION "\n");
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100152 set_bit(0, &msg_registered);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100153 return 0;
154}
155
156
157/****************************** Some noise ******************************/
158
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100159static struct usb_composite_driver msg_driver = {
160 .name = "g_mass_storage",
161 .dev = &msg_device_desc,
Michal Nazarewicz7c2b61d2010-08-12 17:43:47 +0200162 .iProduct = DRIVER_DESC,
Felipe Balbi4bb99b72011-08-03 14:33:27 +0300163 .max_speed = USB_SPEED_SUPER,
Michal Nazarewicz7c2b61d2010-08-12 17:43:47 +0200164 .needs_serial = 1,
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100165};
166
167MODULE_DESCRIPTION(DRIVER_DESC);
168MODULE_AUTHOR("Michal Nazarewicz");
169MODULE_LICENSE("GPL");
170
171static int __init msg_init(void)
172{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200173 return usb_composite_probe(&msg_driver, msg_bind);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100174}
175module_init(msg_init);
176
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100177static void msg_cleanup(void)
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100178{
Michal Nazarewiczc85efcb2009-11-09 14:15:26 +0100179 if (test_and_clear_bit(0, &msg_registered))
180 usb_composite_unregister(&msg_driver);
Michal Nazarewiczd23b0f02009-11-09 14:15:20 +0100181}
182module_exit(msg_cleanup);