1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for USB Mass Storage compliant devices
6 * Current development and maintenance by:
12 * This driver is based on the 'USB Mass Storage Class' document. This
13 * describes in detail the protocol used to communicate with such
14 * devices. Clearly, the designers had SCSI and ATAPI commands in
15 * mind when they created this document. The commands are all very
16 * similar to commands in the SCSI-II and ATAPI specifications.
18 * It is important to note that in a number of cases this class
19 * exhibits class-specific exemptions from the USB specification.
20 * Notably the usage of NAK, STALL and ACK differs from the norm, in
21 * that they are used to communicate wait, failed and OK on commands.
23 * Also, for certain devices, the interrupt endpoint is used to convey
24 * status of a command.
26 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
27 * information about this driver.
33 #include <linux/usb.h>
34 #include <linux/usb_usual.h>
35 #include <linux/blkdev.h>
36 #include <linux/completion.h>
37 #include <linux/mutex.h>
38 #include <linux/workqueue.h>
39 #include <scsi/scsi_host.h>
45 * Unusual device list definitions
48 struct us_unusual_dev {
49 const char* vendorName;
50 const char* productName;
53 int (*initFunction)(struct us_data *);
57 /* Dynamic bitflag definitions (us->dflags): used in set_bit() etc. */
58 #define US_FLIDX_URB_ACTIVE 0 /* current_urb is in use */
59 #define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */
60 #define US_FLIDX_ABORTING 2 /* abort is in progress */
61 #define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */
62 #define US_FLIDX_RESETTING 4 /* device reset in progress */
63 #define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
64 #define US_FLIDX_SCAN_PENDING 6 /* scanning not yet done */
65 #define US_FLIDX_REDO_READ10 7 /* redo READ(10) command */
66 #define US_FLIDX_READ10_WORKED 8 /* previous READ(10) succeeded */
68 #define USB_STOR_STRING_LEN 32
71 * We provide a DMA-mapped I/O buffer for use with small USB transfers.
72 * It turns out that CB[I] needs a 12-byte buffer and Bulk-only needs a
73 * 31-byte buffer. But Freecom needs a 64-byte buffer, so that's the
74 * size we'll allocate.
77 #define US_IOBUF_SIZE 64 /* Size of the DMA-mapped I/O buffer */
78 #define US_SENSE_SIZE 18 /* Size of the autosense data buffer */
80 typedef int (*trans_cmnd)(struct scsi_cmnd *, struct us_data*);
81 typedef int (*trans_reset)(struct us_data*);
82 typedef void (*proto_cmnd)(struct scsi_cmnd*, struct us_data*);
83 typedef void (*extra_data_destructor)(void *); /* extra data destructor */
84 typedef void (*pm_hook)(struct us_data *, int); /* power management hook */
89 /* we allocate one of these for every device that we remember */
92 * The device we're working with
93 * It's important to note:
94 * (o) you must hold dev_mutex to change pusb_dev
96 struct mutex dev_mutex; /* protect pusb_dev */
97 struct usb_device *pusb_dev; /* this usb_device */
98 struct usb_interface *pusb_intf; /* this interface */
99 struct us_unusual_dev *unusual_dev; /* device-filter entry */
100 unsigned long fflags; /* fixed flags from filter */
101 unsigned long dflags; /* dynamic atomic bitflags */
102 unsigned int send_bulk_pipe; /* cached pipe values */
103 unsigned int recv_bulk_pipe;
104 unsigned int send_ctrl_pipe;
105 unsigned int recv_ctrl_pipe;
106 unsigned int recv_intr_pipe;
108 /* information about the device */
109 char *transport_name;
111 __le32 bcs_signature;
116 u8 ifnum; /* interface number */
117 u8 ep_bInterval; /* interrupt interval */
119 /* function pointers for this device */
120 trans_cmnd transport; /* transport function */
121 trans_reset transport_reset; /* transport device reset */
122 proto_cmnd proto_handler; /* protocol handler */
124 /* SCSI interfaces */
125 struct scsi_cmnd *srb; /* current srb */
126 unsigned int tag; /* current dCBWTag */
127 char scsi_name[32]; /* scsi_host name */
129 /* control and bulk communications data */
130 struct urb *current_urb; /* USB requests */
131 struct usb_ctrlrequest *cr; /* control requests */
132 struct usb_sg_request current_sg; /* scatter-gather req. */
133 unsigned char *iobuf; /* I/O buffer */
134 dma_addr_t iobuf_dma; /* buffer DMA addresses */
135 struct task_struct *ctl_thread; /* the control thread */
137 /* mutual exclusion and synchronization structures */
138 struct completion cmnd_ready; /* to sleep thread on */
139 struct completion notify; /* thread begin/end */
140 wait_queue_head_t delay_wait; /* wait during reset */
141 struct delayed_work scan_dwork; /* for async scanning */
143 /* subdriver information */
144 void *extra; /* Any extra data */
145 extra_data_destructor extra_destructor;/* extra data destructor */
147 pm_hook suspend_resume_hook;
150 /* hacks for READ CAPACITY bug handling */
151 int use_last_sector_hacks;
152 int last_sector_retries;
155 /* Convert between us_data and the corresponding Scsi_Host */
156 static inline struct Scsi_Host *us_to_host(struct us_data *us) {
157 return container_of((void *) us, struct Scsi_Host, hostdata);
159 static inline struct us_data *host_to_us(struct Scsi_Host *host) {
160 return (struct us_data *) host->hostdata;
163 /* Function to fill an inquiry response. See usb.c for details */
164 extern void fill_inquiry_response(struct us_data *us,
165 unsigned char *data, unsigned int data_len);
168 * The scsi_lock() and scsi_unlock() macros protect the sm_state and the
169 * single queue element srb for write access
171 #define scsi_unlock(host) spin_unlock_irq(host->host_lock)
172 #define scsi_lock(host) spin_lock_irq(host->host_lock)
174 /* General routines provided by the usb-storage standard core */
176 extern int usb_stor_suspend(struct usb_interface *iface, pm_message_t message);
177 extern int usb_stor_resume(struct usb_interface *iface);
178 extern int usb_stor_reset_resume(struct usb_interface *iface);
180 #define usb_stor_suspend NULL
181 #define usb_stor_resume NULL
182 #define usb_stor_reset_resume NULL
185 extern int usb_stor_pre_reset(struct usb_interface *iface);
186 extern int usb_stor_post_reset(struct usb_interface *iface);
188 extern int usb_stor_probe1(struct us_data **pus,
189 struct usb_interface *intf,
190 const struct usb_device_id *id,
191 struct us_unusual_dev *unusual_dev,
192 struct scsi_host_template *sht);
193 extern int usb_stor_probe2(struct us_data *us);
194 extern void usb_stor_disconnect(struct usb_interface *intf);
196 extern void usb_stor_adjust_quirks(struct usb_device *dev,
197 unsigned long *fflags);
199 #define module_usb_stor_driver(__driver, __sht, __name) \
200 static int __init __driver##_init(void) \
202 usb_stor_host_template_init(&(__sht), __name, THIS_MODULE); \
203 return usb_register(&(__driver)); \
205 module_init(__driver##_init); \
206 static void __exit __driver##_exit(void) \
208 usb_deregister(&(__driver)); \
210 module_exit(__driver##_exit)