f_mass_storage.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef USB_F_MASS_STORAGE_H
  2. #define USB_F_MASS_STORAGE_H
  3. #include <linux/usb/composite.h>
  4. #include "storage_common.h"
  5. struct fsg_module_parameters {
  6. char *file[FSG_MAX_LUNS];
  7. bool ro[FSG_MAX_LUNS];
  8. bool removable[FSG_MAX_LUNS];
  9. bool cdrom[FSG_MAX_LUNS];
  10. bool nofua[FSG_MAX_LUNS];
  11. unsigned int file_count, ro_count, removable_count, cdrom_count;
  12. unsigned int nofua_count;
  13. unsigned int luns; /* nluns */
  14. bool stall; /* can_stall */
  15. };
  16. #define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
  17. module_param_array_named(prefix ## name, params.name, type, \
  18. &prefix ## params.name ## _count, \
  19. S_IRUGO); \
  20. MODULE_PARM_DESC(prefix ## name, desc)
  21. #define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
  22. module_param_named(prefix ## name, params.name, type, \
  23. S_IRUGO); \
  24. MODULE_PARM_DESC(prefix ## name, desc)
  25. #define __FSG_MODULE_PARAMETERS(prefix, params) \
  26. _FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
  27. "names of backing files or devices"); \
  28. _FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
  29. "true to force read-only"); \
  30. _FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
  31. "true to simulate removable media"); \
  32. _FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
  33. "true to simulate CD-ROM instead of disk"); \
  34. _FSG_MODULE_PARAM_ARRAY(prefix, params, nofua, bool, \
  35. "true to ignore SCSI WRITE(10,12) FUA bit"); \
  36. _FSG_MODULE_PARAM(prefix, params, luns, uint, \
  37. "number of LUNs"); \
  38. _FSG_MODULE_PARAM(prefix, params, stall, bool, \
  39. "false to prevent bulk stalls")
  40. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  41. #define FSG_MODULE_PARAMETERS(prefix, params) \
  42. __FSG_MODULE_PARAMETERS(prefix, params); \
  43. module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);\
  44. MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers")
  45. #else
  46. #define FSG_MODULE_PARAMETERS(prefix, params) \
  47. __FSG_MODULE_PARAMETERS(prefix, params)
  48. #endif
  49. struct fsg_common;
  50. /* FSF callback functions */
  51. struct fsg_operations {
  52. /*
  53. * Callback function to call when thread exits. If no
  54. * callback is set or it returns value lower then zero MSF
  55. * will force eject all LUNs it operates on (including those
  56. * marked as non-removable or with prevent_medium_removal flag
  57. * set).
  58. */
  59. int (*thread_exits)(struct fsg_common *common);
  60. };
  61. struct fsg_lun_opts {
  62. struct config_group group;
  63. struct fsg_lun *lun;
  64. int lun_id;
  65. };
  66. struct fsg_opts {
  67. struct fsg_common *common;
  68. struct usb_function_instance func_inst;
  69. struct fsg_lun_opts lun0;
  70. struct config_group *default_groups[2];
  71. bool no_configfs; /* for legacy gadgets */
  72. /*
  73. * Read/write access to configfs attributes is handled by configfs.
  74. *
  75. * This is to protect the data from concurrent access by read/write
  76. * and create symlink/remove symlink.
  77. */
  78. struct mutex lock;
  79. int refcnt;
  80. };
  81. struct fsg_lun_config {
  82. const char *filename;
  83. char ro;
  84. char removable;
  85. char cdrom;
  86. char nofua;
  87. };
  88. struct fsg_config {
  89. unsigned nluns;
  90. struct fsg_lun_config luns[FSG_MAX_LUNS];
  91. /* Callback functions. */
  92. const struct fsg_operations *ops;
  93. /* Gadget's private data. */
  94. void *private_data;
  95. const char *vendor_name; /* 8 characters or less */
  96. const char *product_name; /* 16 characters or less */
  97. char can_stall;
  98. unsigned int fsg_num_buffers;
  99. };
  100. static inline struct fsg_opts *
  101. fsg_opts_from_func_inst(const struct usb_function_instance *fi)
  102. {
  103. return container_of(fi, struct fsg_opts, func_inst);
  104. }
  105. void fsg_common_get(struct fsg_common *common);
  106. void fsg_common_put(struct fsg_common *common);
  107. struct fsg_common *fsg_common_init(struct fsg_common *common,
  108. struct usb_composite_dev *cdev,
  109. struct fsg_config *cfg);
  110. void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs);
  111. int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n);
  112. void fsg_common_free_buffers(struct fsg_common *common);
  113. int fsg_common_set_cdev(struct fsg_common *common,
  114. struct usb_composite_dev *cdev, bool can_stall);
  115. void fsg_common_remove_lun(struct fsg_lun *lun, bool sysfs);
  116. void fsg_common_remove_luns(struct fsg_common *common);
  117. void fsg_common_free_luns(struct fsg_common *common);
  118. int fsg_common_set_nluns(struct fsg_common *common, int nluns);
  119. void fsg_common_set_ops(struct fsg_common *common,
  120. const struct fsg_operations *ops);
  121. int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
  122. unsigned int id, const char *name,
  123. const char **name_pfx);
  124. int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg);
  125. void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
  126. const char *pn);
  127. int fsg_common_run_thread(struct fsg_common *common);
  128. void fsg_config_from_params(struct fsg_config *cfg,
  129. const struct fsg_module_parameters *params,
  130. unsigned int fsg_num_buffers);
  131. static inline struct fsg_common *
  132. fsg_common_from_params(struct fsg_common *common,
  133. struct usb_composite_dev *cdev,
  134. const struct fsg_module_parameters *params,
  135. unsigned int fsg_num_buffers)
  136. __attribute__((unused));
  137. static inline struct fsg_common *
  138. fsg_common_from_params(struct fsg_common *common,
  139. struct usb_composite_dev *cdev,
  140. const struct fsg_module_parameters *params,
  141. unsigned int fsg_num_buffers)
  142. {
  143. struct fsg_config cfg;
  144. fsg_config_from_params(&cfg, params, fsg_num_buffers);
  145. return fsg_common_init(common, cdev, &cfg);
  146. }
  147. #endif /* USB_F_MASS_STORAGE_H */