mass_storage.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * mass_storage.c -- Mass Storage USB Gadget
  3. *
  4. * Copyright (C) 2003-2008 Alan Stern
  5. * Copyright (C) 2009 Samsung Electronics
  6. * Author: Michal Nazarewicz <mina86@mina86.com>
  7. * All rights reserved.
  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. /*
  15. * The Mass Storage Gadget acts as a USB Mass Storage device,
  16. * appearing to the host as a disk drive or as a CD-ROM drive. In
  17. * addition to providing an example of a genuinely useful gadget
  18. * driver for a USB device, it also illustrates a technique of
  19. * double-buffering for increased throughput. Last but not least, it
  20. * gives an easy way to probe the behavior of the Mass Storage drivers
  21. * in a USB host.
  22. *
  23. * Since this file serves only administrative purposes and all the
  24. * business logic is implemented in f_mass_storage.* file. Read
  25. * comments in this file for more detailed description.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/usb/ch9.h>
  29. #include <linux/module.h>
  30. /*-------------------------------------------------------------------------*/
  31. #define DRIVER_DESC "Mass Storage Gadget"
  32. #define DRIVER_VERSION "2009/09/11"
  33. /*
  34. * Thanks to NetChip Technologies for donating this product ID.
  35. *
  36. * DO NOT REUSE THESE IDs with any other driver!! Ever!!
  37. * Instead: allocate your own, using normal USB-IF procedures.
  38. */
  39. #define FSG_VENDOR_ID 0x0525 /* NetChip */
  40. #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  41. #include "f_mass_storage.h"
  42. /*-------------------------------------------------------------------------*/
  43. USB_GADGET_COMPOSITE_OPTIONS();
  44. static struct usb_device_descriptor msg_device_desc = {
  45. .bLength = sizeof msg_device_desc,
  46. .bDescriptorType = USB_DT_DEVICE,
  47. /* .bcdUSB = DYNAMIC */
  48. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  49. /* Vendor and product id can be overridden by module parameters. */
  50. .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  51. .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  52. .bNumConfigurations = 1,
  53. };
  54. static const struct usb_descriptor_header *otg_desc[2];
  55. static struct usb_string strings_dev[] = {
  56. [USB_GADGET_MANUFACTURER_IDX].s = "",
  57. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  58. [USB_GADGET_SERIAL_IDX].s = "",
  59. { } /* end of list */
  60. };
  61. static struct usb_gadget_strings stringtab_dev = {
  62. .language = 0x0409, /* en-us */
  63. .strings = strings_dev,
  64. };
  65. static struct usb_gadget_strings *dev_strings[] = {
  66. &stringtab_dev,
  67. NULL,
  68. };
  69. static struct usb_function_instance *fi_msg;
  70. static struct usb_function *f_msg;
  71. /****************************** Configurations ******************************/
  72. static struct fsg_module_parameters mod_data = {
  73. .stall = 1
  74. };
  75. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  76. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  77. #else
  78. /*
  79. * Number of buffers we will use.
  80. * 2 is usually enough for good buffering pipeline
  81. */
  82. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  83. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  84. FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);
  85. static unsigned long msg_registered;
  86. static void msg_cleanup(void);
  87. static int msg_thread_exits(struct fsg_common *common)
  88. {
  89. msg_cleanup();
  90. return 0;
  91. }
  92. static int msg_do_config(struct usb_configuration *c)
  93. {
  94. struct fsg_opts *opts;
  95. int ret;
  96. if (gadget_is_otg(c->cdev->gadget)) {
  97. c->descriptors = otg_desc;
  98. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  99. }
  100. opts = fsg_opts_from_func_inst(fi_msg);
  101. f_msg = usb_get_function(fi_msg);
  102. if (IS_ERR(f_msg))
  103. return PTR_ERR(f_msg);
  104. ret = usb_add_function(c, f_msg);
  105. if (ret)
  106. goto put_func;
  107. return 0;
  108. put_func:
  109. usb_put_function(f_msg);
  110. return ret;
  111. }
  112. static struct usb_configuration msg_config_driver = {
  113. .label = "Linux File-Backed Storage",
  114. .bConfigurationValue = 1,
  115. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  116. };
  117. /****************************** Gadget Bind ******************************/
  118. static int msg_bind(struct usb_composite_dev *cdev)
  119. {
  120. static const struct fsg_operations ops = {
  121. .thread_exits = msg_thread_exits,
  122. };
  123. struct fsg_opts *opts;
  124. struct fsg_config config;
  125. int status;
  126. fi_msg = usb_get_function_instance("mass_storage");
  127. if (IS_ERR(fi_msg))
  128. return PTR_ERR(fi_msg);
  129. fsg_config_from_params(&config, &mod_data, fsg_num_buffers);
  130. opts = fsg_opts_from_func_inst(fi_msg);
  131. opts->no_configfs = true;
  132. status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
  133. if (status)
  134. goto fail;
  135. fsg_common_set_ops(opts->common, &ops);
  136. status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
  137. if (status)
  138. goto fail_set_cdev;
  139. fsg_common_set_sysfs(opts->common, true);
  140. status = fsg_common_create_luns(opts->common, &config);
  141. if (status)
  142. goto fail_set_cdev;
  143. fsg_common_set_inquiry_string(opts->common, config.vendor_name,
  144. config.product_name);
  145. status = usb_string_ids_tab(cdev, strings_dev);
  146. if (status < 0)
  147. goto fail_string_ids;
  148. msg_device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  149. if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
  150. struct usb_descriptor_header *usb_desc;
  151. usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
  152. if (!usb_desc)
  153. goto fail_string_ids;
  154. usb_otg_descriptor_init(cdev->gadget, usb_desc);
  155. otg_desc[0] = usb_desc;
  156. otg_desc[1] = NULL;
  157. }
  158. status = usb_add_config(cdev, &msg_config_driver, msg_do_config);
  159. if (status < 0)
  160. goto fail_otg_desc;
  161. usb_composite_overwrite_options(cdev, &coverwrite);
  162. dev_info(&cdev->gadget->dev,
  163. DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  164. set_bit(0, &msg_registered);
  165. return 0;
  166. fail_otg_desc:
  167. kfree(otg_desc[0]);
  168. otg_desc[0] = NULL;
  169. fail_string_ids:
  170. fsg_common_remove_luns(opts->common);
  171. fail_set_cdev:
  172. fsg_common_free_buffers(opts->common);
  173. fail:
  174. usb_put_function_instance(fi_msg);
  175. return status;
  176. }
  177. static int msg_unbind(struct usb_composite_dev *cdev)
  178. {
  179. if (!IS_ERR(f_msg))
  180. usb_put_function(f_msg);
  181. if (!IS_ERR(fi_msg))
  182. usb_put_function_instance(fi_msg);
  183. kfree(otg_desc[0]);
  184. otg_desc[0] = NULL;
  185. return 0;
  186. }
  187. /****************************** Some noise ******************************/
  188. static struct usb_composite_driver msg_driver = {
  189. .name = "g_mass_storage",
  190. .dev = &msg_device_desc,
  191. .max_speed = USB_SPEED_SUPER,
  192. .needs_serial = 1,
  193. .strings = dev_strings,
  194. .bind = msg_bind,
  195. .unbind = msg_unbind,
  196. };
  197. MODULE_DESCRIPTION(DRIVER_DESC);
  198. MODULE_AUTHOR("Michal Nazarewicz");
  199. MODULE_LICENSE("GPL");
  200. static int __init msg_init(void)
  201. {
  202. return usb_composite_probe(&msg_driver);
  203. }
  204. module_init(msg_init);
  205. static void msg_cleanup(void)
  206. {
  207. if (test_and_clear_bit(0, &msg_registered))
  208. usb_composite_unregister(&msg_driver);
  209. }
  210. module_exit(msg_cleanup);