xref: /linux/drivers/usb/storage/usb.h (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for USB Mass Storage compliant devices
4  * Main Header File
5  *
6  * Current development and maintenance by:
7  *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
8  *
9  * Initial work by:
10  *   (c) 1999 Michael Gee (michael@linuxspecific.com)
11  *
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.
17  *
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.
22  *
23  * Also, for certain devices, the interrupt endpoint is used to convey
24  * status of a command.
25  *
26  * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
27  * information about this driver.
28  *
29  * This program is free software; you can redistribute it and/or modify it
30  * under the terms of the GNU General Public License as published by the
31  * Free Software Foundation; either version 2, or (at your option) any
32  * later version.
33  *
34  * This program is distributed in the hope that it will be useful, but
35  * WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License along
40  * with this program; if not, write to the Free Software Foundation, Inc.,
41  * 675 Mass Ave, Cambridge, MA 02139, USA.
42  */
43 
44 #ifndef _USB_H_
45 #define _USB_H_
46 
47 #include <linux/usb.h>
48 #include <linux/usb_usual.h>
49 #include <linux/blkdev.h>
50 #include <linux/completion.h>
51 #include <linux/mutex.h>
52 #include <linux/workqueue.h>
53 #include <scsi/scsi_host.h>
54 
55 struct us_data;
56 struct scsi_cmnd;
57 
58 /*
59  * Unusual device list definitions
60  */
61 
62 struct us_unusual_dev {
63 	const char* vendorName;
64 	const char* productName;
65 	__u8  useProtocol;
66 	__u8  useTransport;
67 	int (*initFunction)(struct us_data *);
68 };
69 
70 
71 /* Dynamic bitflag definitions (us->dflags): used in set_bit() etc. */
72 #define US_FLIDX_URB_ACTIVE	0	/* current_urb is in use    */
73 #define US_FLIDX_SG_ACTIVE	1	/* current_sg is in use     */
74 #define US_FLIDX_ABORTING	2	/* abort is in progress     */
75 #define US_FLIDX_DISCONNECTING	3	/* disconnect in progress   */
76 #define US_FLIDX_RESETTING	4	/* device reset in progress */
77 #define US_FLIDX_TIMED_OUT	5	/* SCSI midlayer timed out  */
78 #define US_FLIDX_SCAN_PENDING	6	/* scanning not yet done    */
79 #define US_FLIDX_REDO_READ10	7	/* redo READ(10) command    */
80 #define US_FLIDX_READ10_WORKED	8	/* previous READ(10) succeeded */
81 
82 #define USB_STOR_STRING_LEN 32
83 
84 /*
85  * We provide a DMA-mapped I/O buffer for use with small USB transfers.
86  * It turns out that CB[I] needs a 12-byte buffer and Bulk-only needs a
87  * 31-byte buffer.  But Freecom needs a 64-byte buffer, so that's the
88  * size we'll allocate.
89  */
90 
91 #define US_IOBUF_SIZE		64	/* Size of the DMA-mapped I/O buffer */
92 #define US_SENSE_SIZE		18	/* Size of the autosense data buffer */
93 
94 typedef int (*trans_cmnd)(struct scsi_cmnd *, struct us_data*);
95 typedef int (*trans_reset)(struct us_data*);
96 typedef void (*proto_cmnd)(struct scsi_cmnd*, struct us_data*);
97 typedef void (*extra_data_destructor)(void *);	/* extra data destructor */
98 typedef void (*pm_hook)(struct us_data *, int);	/* power management hook */
99 
100 #define US_SUSPEND	0
101 #define US_RESUME	1
102 
103 /* we allocate one of these for every device that we remember */
104 struct us_data {
105 	/*
106 	 * The device we're working with
107 	 * It's important to note:
108 	 *    (o) you must hold dev_mutex to change pusb_dev
109 	 */
110 	struct mutex		dev_mutex;	 /* protect pusb_dev */
111 	struct usb_device	*pusb_dev;	 /* this usb_device */
112 	struct usb_interface	*pusb_intf;	 /* this interface */
113 	struct us_unusual_dev   *unusual_dev;	 /* device-filter entry     */
114 	unsigned long		fflags;		 /* fixed flags from filter */
115 	unsigned long		dflags;		 /* dynamic atomic bitflags */
116 	unsigned int		send_bulk_pipe;	 /* cached pipe values */
117 	unsigned int		recv_bulk_pipe;
118 	unsigned int		send_ctrl_pipe;
119 	unsigned int		recv_ctrl_pipe;
120 	unsigned int		recv_intr_pipe;
121 
122 	/* information about the device */
123 	char			*transport_name;
124 	char			*protocol_name;
125 	__le32			bcs_signature;
126 	u8			subclass;
127 	u8			protocol;
128 	u8			max_lun;
129 
130 	u8			ifnum;		 /* interface number   */
131 	u8			ep_bInterval;	 /* interrupt interval */
132 
133 	/* function pointers for this device */
134 	trans_cmnd		transport;	 /* transport function	   */
135 	trans_reset		transport_reset; /* transport device reset */
136 	proto_cmnd		proto_handler;	 /* protocol handler	   */
137 
138 	/* SCSI interfaces */
139 	struct scsi_cmnd	*srb;		 /* current srb		*/
140 	unsigned int		tag;		 /* current dCBWTag	*/
141 	char			scsi_name[32];	 /* scsi_host name	*/
142 
143 	/* control and bulk communications data */
144 	struct urb		*current_urb;	 /* USB requests	 */
145 	struct usb_ctrlrequest	*cr;		 /* control requests	 */
146 	struct usb_sg_request	current_sg;	 /* scatter-gather req.  */
147 	unsigned char		*iobuf;		 /* I/O buffer		 */
148 	dma_addr_t		iobuf_dma;	 /* buffer DMA addresses */
149 	struct task_struct	*ctl_thread;	 /* the control thread   */
150 
151 	/* mutual exclusion and synchronization structures */
152 	struct completion	cmnd_ready;	 /* to sleep thread on	    */
153 	struct completion	notify;		 /* thread begin/end	    */
154 	wait_queue_head_t	delay_wait;	 /* wait during reset	    */
155 	struct delayed_work	scan_dwork;	 /* for async scanning      */
156 
157 	/* subdriver information */
158 	void			*extra;		 /* Any extra data          */
159 	extra_data_destructor	extra_destructor;/* extra data destructor   */
160 #ifdef CONFIG_PM
161 	pm_hook			suspend_resume_hook;
162 #endif
163 
164 	/* hacks for READ CAPACITY bug handling */
165 	int			use_last_sector_hacks;
166 	int			last_sector_retries;
167 };
168 
169 /* Convert between us_data and the corresponding Scsi_Host */
170 static inline struct Scsi_Host *us_to_host(struct us_data *us) {
171 	return container_of((void *) us, struct Scsi_Host, hostdata);
172 }
173 static inline struct us_data *host_to_us(struct Scsi_Host *host) {
174 	return (struct us_data *) host->hostdata;
175 }
176 
177 /* Function to fill an inquiry response. See usb.c for details */
178 extern void fill_inquiry_response(struct us_data *us,
179 	unsigned char *data, unsigned int data_len);
180 
181 /*
182  * The scsi_lock() and scsi_unlock() macros protect the sm_state and the
183  * single queue element srb for write access
184  */
185 #define scsi_unlock(host)	spin_unlock_irq(host->host_lock)
186 #define scsi_lock(host)		spin_lock_irq(host->host_lock)
187 
188 /* General routines provided by the usb-storage standard core */
189 #ifdef CONFIG_PM
190 extern int usb_stor_suspend(struct usb_interface *iface, pm_message_t message);
191 extern int usb_stor_resume(struct usb_interface *iface);
192 extern int usb_stor_reset_resume(struct usb_interface *iface);
193 #else
194 #define usb_stor_suspend	NULL
195 #define usb_stor_resume		NULL
196 #define usb_stor_reset_resume	NULL
197 #endif
198 
199 extern int usb_stor_pre_reset(struct usb_interface *iface);
200 extern int usb_stor_post_reset(struct usb_interface *iface);
201 
202 extern int usb_stor_probe1(struct us_data **pus,
203 		struct usb_interface *intf,
204 		const struct usb_device_id *id,
205 		struct us_unusual_dev *unusual_dev,
206 		struct scsi_host_template *sht);
207 extern int usb_stor_probe2(struct us_data *us);
208 extern void usb_stor_disconnect(struct usb_interface *intf);
209 
210 extern void usb_stor_adjust_quirks(struct usb_device *dev,
211 		unsigned long *fflags);
212 
213 #define module_usb_stor_driver(__driver, __sht, __name) \
214 static int __init __driver##_init(void) \
215 { \
216 	usb_stor_host_template_init(&(__sht), __name, THIS_MODULE); \
217 	return usb_register(&(__driver)); \
218 } \
219 module_init(__driver##_init); \
220 static void __exit __driver##_exit(void) \
221 { \
222 	usb_deregister(&(__driver)); \
223 } \
224 module_exit(__driver##_exit)
225 
226 #endif
227