storage_common.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef USB_STORAGE_COMMON_H
  3. #define USB_STORAGE_COMMON_H
  4. #include <linux/device.h>
  5. #include <linux/usb/storage.h>
  6. #include <scsi/scsi.h>
  7. #include <asm/unaligned.h>
  8. #ifndef DEBUG
  9. #undef VERBOSE_DEBUG
  10. #undef DUMP_MSGS
  11. #endif /* !DEBUG */
  12. #ifdef VERBOSE_DEBUG
  13. #define VLDBG LDBG
  14. #else
  15. #define VLDBG(lun, fmt, args...) do { } while (0)
  16. #endif /* VERBOSE_DEBUG */
  17. #define _LMSG(func, lun, fmt, args...) \
  18. do { \
  19. if ((lun)->name_pfx && *(lun)->name_pfx) \
  20. func("%s/%s: " fmt, *(lun)->name_pfx, \
  21. (lun)->name, ## args); \
  22. else \
  23. func("%s: " fmt, (lun)->name, ## args); \
  24. } while (0)
  25. #define LDBG(lun, fmt, args...) _LMSG(pr_debug, lun, fmt, ## args)
  26. #define LERROR(lun, fmt, args...) _LMSG(pr_err, lun, fmt, ## args)
  27. #define LWARN(lun, fmt, args...) _LMSG(pr_warn, lun, fmt, ## args)
  28. #define LINFO(lun, fmt, args...) _LMSG(pr_info, lun, fmt, ## args)
  29. #ifdef DUMP_MSGS
  30. # define dump_msg(fsg, /* const char * */ label, \
  31. /* const u8 * */ buf, /* unsigned */ length) \
  32. do { \
  33. if (length < 512) { \
  34. DBG(fsg, "%s, length %u:\n", label, length); \
  35. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
  36. 16, 1, buf, length, 0); \
  37. } \
  38. } while (0)
  39. # define dump_cdb(fsg) do { } while (0)
  40. #else
  41. # define dump_msg(fsg, /* const char * */ label, \
  42. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  43. # ifdef VERBOSE_DEBUG
  44. # define dump_cdb(fsg) \
  45. print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
  46. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  47. # else
  48. # define dump_cdb(fsg) do { } while (0)
  49. # endif /* VERBOSE_DEBUG */
  50. #endif /* DUMP_MSGS */
  51. /* Length of a SCSI Command Data Block */
  52. #define MAX_COMMAND_SIZE 16
  53. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  54. #define SS_NO_SENSE 0
  55. #define SS_COMMUNICATION_FAILURE 0x040800
  56. #define SS_INVALID_COMMAND 0x052000
  57. #define SS_INVALID_FIELD_IN_CDB 0x052400
  58. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  59. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  60. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  61. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  62. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  63. #define SS_RESET_OCCURRED 0x062900
  64. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  65. #define SS_UNRECOVERED_READ_ERROR 0x031100
  66. #define SS_WRITE_ERROR 0x030c02
  67. #define SS_WRITE_PROTECTED 0x072700
  68. #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
  69. #define ASC(x) ((u8) ((x) >> 8))
  70. #define ASCQ(x) ((u8) (x))
  71. /*
  72. * Vendor (8 chars), product (16 chars), release (4 hexadecimal digits) and NUL
  73. * byte
  74. */
  75. #define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
  76. struct fsg_lun {
  77. struct file *filp;
  78. loff_t file_length;
  79. loff_t num_sectors;
  80. unsigned int initially_ro:1;
  81. unsigned int ro:1;
  82. unsigned int removable:1;
  83. unsigned int cdrom:1;
  84. unsigned int prevent_medium_removal:1;
  85. unsigned int registered:1;
  86. unsigned int info_valid:1;
  87. unsigned int nofua:1;
  88. u32 sense_data;
  89. u32 sense_data_info;
  90. u32 unit_attention_data;
  91. unsigned int blkbits; /* Bits of logical block size
  92. of bound block device */
  93. unsigned int blksize; /* logical block size of bound block device */
  94. struct device dev;
  95. const char *name; /* "lun.name" */
  96. const char **name_pfx; /* "function.name" */
  97. char inquiry_string[INQUIRY_STRING_LEN];
  98. };
  99. static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
  100. {
  101. return curlun->filp != NULL;
  102. }
  103. /* Default size of buffer length. */
  104. #define FSG_BUFLEN ((u32)16384)
  105. /* Maximal number of LUNs supported in mass storage function */
  106. #define FSG_MAX_LUNS 16
  107. enum fsg_buffer_state {
  108. BUF_STATE_SENDING = -2,
  109. BUF_STATE_RECEIVING,
  110. BUF_STATE_EMPTY = 0,
  111. BUF_STATE_FULL
  112. };
  113. struct fsg_buffhd {
  114. void *buf;
  115. enum fsg_buffer_state state;
  116. struct fsg_buffhd *next;
  117. /*
  118. * The NetChip 2280 is faster, and handles some protocol faults
  119. * better, if we don't submit any short bulk-out read requests.
  120. * So we will record the intended request length here.
  121. */
  122. unsigned int bulk_out_intended_length;
  123. struct usb_request *inreq;
  124. struct usb_request *outreq;
  125. };
  126. enum fsg_state {
  127. FSG_STATE_NORMAL,
  128. FSG_STATE_ABORT_BULK_OUT,
  129. FSG_STATE_PROTOCOL_RESET,
  130. FSG_STATE_CONFIG_CHANGE,
  131. FSG_STATE_EXIT,
  132. FSG_STATE_TERMINATED
  133. };
  134. enum data_direction {
  135. DATA_DIR_UNKNOWN = 0,
  136. DATA_DIR_FROM_HOST,
  137. DATA_DIR_TO_HOST,
  138. DATA_DIR_NONE
  139. };
  140. static inline u32 get_unaligned_be24(u8 *buf)
  141. {
  142. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  143. }
  144. static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  145. {
  146. return container_of(dev, struct fsg_lun, dev);
  147. }
  148. enum {
  149. FSG_STRING_INTERFACE
  150. };
  151. extern struct usb_interface_descriptor fsg_intf_desc;
  152. extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
  153. extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
  154. extern struct usb_descriptor_header *fsg_fs_function[];
  155. extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
  156. extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
  157. extern struct usb_descriptor_header *fsg_hs_function[];
  158. extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
  159. extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
  160. extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
  161. extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
  162. extern struct usb_descriptor_header *fsg_ss_function[];
  163. void fsg_lun_close(struct fsg_lun *curlun);
  164. int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
  165. int fsg_lun_fsync_sub(struct fsg_lun *curlun);
  166. void store_cdrom_address(u8 *dest, int msf, u32 addr);
  167. ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
  168. ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
  169. ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  170. char *buf);
  171. ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf);
  172. ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
  173. ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
  174. ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  175. const char *buf, size_t count);
  176. ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
  177. ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  178. const char *buf, size_t count);
  179. ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  180. const char *buf, size_t count);
  181. ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
  182. size_t count);
  183. ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
  184. size_t count);
  185. #endif /* USB_STORAGE_COMMON_H */