storage_common.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /* Big enough to hold our biggest descriptor */
  97. #define EP0_BUFSIZE 256
  98. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  99. /* Default size of buffer length. */
  100. #define FSG_BUFLEN ((u32)16384)
  101. /* Maximal number of LUNs supported in mass storage function */
  102. #define FSG_MAX_LUNS 8
  103. enum fsg_buffer_state {
  104. BUF_STATE_EMPTY = 0,
  105. BUF_STATE_FULL,
  106. BUF_STATE_BUSY
  107. };
  108. struct fsg_buffhd {
  109. void *buf;
  110. enum fsg_buffer_state state;
  111. struct fsg_buffhd *next;
  112. /*
  113. * The NetChip 2280 is faster, and handles some protocol faults
  114. * better, if we don't submit any short bulk-out read requests.
  115. * So we will record the intended request length here.
  116. */
  117. unsigned int bulk_out_intended_length;
  118. struct usb_request *inreq;
  119. int inreq_busy;
  120. struct usb_request *outreq;
  121. int outreq_busy;
  122. };
  123. enum fsg_state {
  124. /* This one isn't used anywhere */
  125. FSG_STATE_COMMAND_PHASE = -10,
  126. FSG_STATE_DATA_PHASE,
  127. FSG_STATE_STATUS_PHASE,
  128. FSG_STATE_IDLE = 0,
  129. FSG_STATE_ABORT_BULK_OUT,
  130. FSG_STATE_RESET,
  131. FSG_STATE_INTERFACE_CHANGE,
  132. FSG_STATE_CONFIG_CHANGE,
  133. FSG_STATE_DISCONNECT,
  134. FSG_STATE_EXIT,
  135. FSG_STATE_TERMINATED
  136. };
  137. enum data_direction {
  138. DATA_DIR_UNKNOWN = 0,
  139. DATA_DIR_FROM_HOST,
  140. DATA_DIR_TO_HOST,
  141. DATA_DIR_NONE
  142. };
  143. static inline u32 get_unaligned_be24(u8 *buf)
  144. {
  145. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  146. }
  147. static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  148. {
  149. return container_of(dev, struct fsg_lun, dev);
  150. }
  151. enum {
  152. FSG_STRING_INTERFACE
  153. };
  154. extern struct usb_interface_descriptor fsg_intf_desc;
  155. extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
  156. extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
  157. extern struct usb_descriptor_header *fsg_fs_function[];
  158. extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
  159. extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
  160. extern struct usb_descriptor_header *fsg_hs_function[];
  161. extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
  162. extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
  163. extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
  164. extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
  165. extern struct usb_descriptor_header *fsg_ss_function[];
  166. void fsg_lun_close(struct fsg_lun *curlun);
  167. int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
  168. int fsg_lun_fsync_sub(struct fsg_lun *curlun);
  169. void store_cdrom_address(u8 *dest, int msf, u32 addr);
  170. ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
  171. ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
  172. ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  173. char *buf);
  174. ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
  175. ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
  176. ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  177. const char *buf, size_t count);
  178. ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
  179. ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  180. const char *buf, size_t count);
  181. ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
  182. const char *buf, size_t count);
  183. ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
  184. size_t count);
  185. #endif /* USB_STORAGE_COMMON_H */