storage_common.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. struct fsg_lun {
  71. struct file *filp;
  72. loff_t file_length;
  73. loff_t num_sectors;
  74. unsigned int initially_ro:1;
  75. unsigned int ro:1;
  76. unsigned int removable:1;
  77. unsigned int cdrom:1;
  78. unsigned int prevent_medium_removal:1;
  79. unsigned int registered:1;
  80. unsigned int info_valid:1;
  81. unsigned int nofua:1;
  82. u32 sense_data;
  83. u32 sense_data_info;
  84. u32 unit_attention_data;
  85. unsigned int blkbits; /* Bits of logical block size
  86. of bound block device */
  87. unsigned int blksize; /* logical block size of bound block device */
  88. struct device dev;
  89. const char *name; /* "lun.name" */
  90. const char **name_pfx; /* "function.name" */
  91. };
  92. static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
  93. {
  94. return curlun->filp != NULL;
  95. }
  96. /* Default size of buffer length. */
  97. #define FSG_BUFLEN ((u32)16384)
  98. /* Maximal number of LUNs supported in mass storage function */
  99. #define FSG_MAX_LUNS 16
  100. enum fsg_buffer_state {
  101. BUF_STATE_EMPTY = 0,
  102. BUF_STATE_FULL,
  103. BUF_STATE_BUSY
  104. };
  105. struct fsg_buffhd {
  106. void *buf;
  107. enum fsg_buffer_state state;
  108. struct fsg_buffhd *next;
  109. /*
  110. * The NetChip 2280 is faster, and handles some protocol faults
  111. * better, if we don't submit any short bulk-out read requests.
  112. * So we will record the intended request length here.
  113. */
  114. unsigned int bulk_out_intended_length;
  115. struct usb_request *inreq;
  116. int inreq_busy;
  117. struct usb_request *outreq;
  118. int outreq_busy;
  119. };
  120. enum fsg_state {
  121. /* This one isn't used anywhere */
  122. FSG_STATE_COMMAND_PHASE = -10,
  123. FSG_STATE_DATA_PHASE,
  124. FSG_STATE_STATUS_PHASE,
  125. FSG_STATE_IDLE = 0,
  126. FSG_STATE_ABORT_BULK_OUT,
  127. FSG_STATE_RESET,
  128. FSG_STATE_INTERFACE_CHANGE,
  129. FSG_STATE_CONFIG_CHANGE,
  130. FSG_STATE_DISCONNECT,
  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_cdrom(struct fsg_lun *curlun, char *buf);
  172. ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
  173. ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  174. const char *buf, size_t count);
  175. ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
  176. ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  177. const char *buf, size_t count);
  178. ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  179. const char *buf, size_t count);
  180. ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
  181. size_t count);
  182. #endif /* USB_STORAGE_COMMON_H */