storage_common.h 6.6 KB

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