storage_common.h 5.8 KB

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