blob: 8fae28b40bb4ac7e806b899ae33c3d90867f6963 [file] [log] [blame]
Felipe Balbif0183a32016-04-18 13:09:11 +03001/*
2 * Driver for USB Mass Storage compliant devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Main Header File
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Current development and maintenance by:
6 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
7 *
8 * Initial work by:
9 * (c) 1999 Michael Gee (michael@linuxspecific.com)
10 *
11 * This driver is based on the 'USB Mass Storage Class' document. This
12 * describes in detail the protocol used to communicate with such
13 * devices. Clearly, the designers had SCSI and ATAPI commands in
14 * mind when they created this document. The commands are all very
15 * similar to commands in the SCSI-II and ATAPI specifications.
16 *
17 * It is important to note that in a number of cases this class
18 * exhibits class-specific exemptions from the USB specification.
19 * Notably the usage of NAK, STALL and ACK differs from the norm, in
20 * that they are used to communicate wait, failed and OK on commands.
21 *
22 * Also, for certain devices, the interrupt endpoint is used to convey
23 * status of a command.
24 *
25 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
26 * information about this driver.
27 *
28 * This program is free software; you can redistribute it and/or modify it
29 * under the terms of the GNU General Public License as published by the
30 * Free Software Foundation; either version 2, or (at your option) any
31 * later version.
32 *
33 * This program is distributed in the hope that it will be useful, but
34 * WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License along
39 * with this program; if not, write to the Free Software Foundation, Inc.,
40 * 675 Mass Ave, Cambridge, MA 02139, USA.
41 */
42
43#ifndef _USB_H_
44#define _USB_H_
45
46#include <linux/usb.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070047#include <linux/usb_usual.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/completion.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010050#include <linux/mutex.h>
Alan Sternbb94a402012-02-21 13:16:32 -050051#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <scsi/scsi_host.h>
53
54struct us_data;
55struct scsi_cmnd;
56
57/*
58 * Unusual device list definitions
59 */
60
61struct us_unusual_dev {
62 const char* vendorName;
63 const char* productName;
64 __u8 useProtocol;
65 __u8 useTransport;
66 int (*initFunction)(struct us_data *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Alan Stern7e4d6c32008-05-01 15:35:18 -040070/* Dynamic bitflag definitions (us->dflags): used in set_bit() etc. */
71#define US_FLIDX_URB_ACTIVE 0 /* current_urb is in use */
72#define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */
73#define US_FLIDX_ABORTING 2 /* abort is in progress */
74#define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */
Alan Stern7e4d6c32008-05-01 15:35:18 -040075#define US_FLIDX_RESETTING 4 /* device reset in progress */
76#define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
Alan Sternbb94a402012-02-21 13:16:32 -050077#define US_FLIDX_SCAN_PENDING 6 /* scanning not yet done */
Alan Stern21c13a42011-06-07 11:35:52 -040078#define US_FLIDX_REDO_READ10 7 /* redo READ(10) command */
79#define US_FLIDX_READ10_WORKED 8 /* previous READ(10) succeeded */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define USB_STOR_STRING_LEN 32
82
83/*
84 * We provide a DMA-mapped I/O buffer for use with small USB transfers.
85 * It turns out that CB[I] needs a 12-byte buffer and Bulk-only needs a
86 * 31-byte buffer. But Freecom needs a 64-byte buffer, so that's the
87 * size we'll allocate.
88 */
89
90#define US_IOBUF_SIZE 64 /* Size of the DMA-mapped I/O buffer */
Alan Sternbbafa462005-10-23 19:40:22 -070091#define US_SENSE_SIZE 18 /* Size of the autosense data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93typedef int (*trans_cmnd)(struct scsi_cmnd *, struct us_data*);
94typedef int (*trans_reset)(struct us_data*);
95typedef void (*proto_cmnd)(struct scsi_cmnd*, struct us_data*);
Matthew Dharm7931e1c2005-12-04 21:56:51 -080096typedef void (*extra_data_destructor)(void *); /* extra data destructor */
97typedef void (*pm_hook)(struct us_data *, int); /* power management hook */
98
99#define US_SUSPEND 0
100#define US_RESUME 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* we allocate one of these for every device that we remember */
103struct us_data {
Felipe Balbif0183a32016-04-18 13:09:11 +0300104 /*
105 * The device we're working with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * It's important to note:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100107 * (o) you must hold dev_mutex to change pusb_dev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100109 struct mutex dev_mutex; /* protect pusb_dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct usb_device *pusb_dev; /* this usb_device */
111 struct usb_interface *pusb_intf; /* this interface */
112 struct us_unusual_dev *unusual_dev; /* device-filter entry */
Alan Stern7e4d6c32008-05-01 15:35:18 -0400113 unsigned long fflags; /* fixed flags from filter */
114 unsigned long dflags; /* dynamic atomic bitflags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned int send_bulk_pipe; /* cached pipe values */
116 unsigned int recv_bulk_pipe;
117 unsigned int send_ctrl_pipe;
118 unsigned int recv_ctrl_pipe;
119 unsigned int recv_intr_pipe;
120
121 /* information about the device */
122 char *transport_name;
123 char *protocol_name;
124 __le32 bcs_signature;
125 u8 subclass;
126 u8 protocol;
127 u8 max_lun;
128
129 u8 ifnum; /* interface number */
Felipe Balbif0183a32016-04-18 13:09:11 +0300130 u8 ep_bInterval; /* interrupt interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* function pointers for this device */
133 trans_cmnd transport; /* transport function */
134 trans_reset transport_reset; /* transport device reset */
135 proto_cmnd proto_handler; /* protocol handler */
136
137 /* SCSI interfaces */
138 struct scsi_cmnd *srb; /* current srb */
Matthew Dharm0f64e072005-07-28 14:43:08 -0700139 unsigned int tag; /* current dCBWTag */
Matthew Wilcox00fa43e2009-09-24 16:19:11 -0600140 char scsi_name[32]; /* scsi_host name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* control and bulk communications data */
143 struct urb *current_urb; /* USB requests */
144 struct usb_ctrlrequest *cr; /* control requests */
145 struct usb_sg_request current_sg; /* scatter-gather req. */
146 unsigned char *iobuf; /* I/O buffer */
Alan Stern0ede76f2010-03-05 15:10:17 -0500147 dma_addr_t iobuf_dma; /* buffer DMA addresses */
Alan Sterned76cac2007-06-07 17:12:25 -0400148 struct task_struct *ctl_thread; /* the control thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* mutual exclusion and synchronization structures */
Alan Stern7119e3c2008-05-01 15:36:13 -0400151 struct completion cmnd_ready; /* to sleep thread on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct completion notify; /* thread begin/end */
Alan Sternbb94a402012-02-21 13:16:32 -0500153 wait_queue_head_t delay_wait; /* wait during reset */
154 struct delayed_work scan_dwork; /* for async scanning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* subdriver information */
157 void *extra; /* Any extra data */
158 extra_data_destructor extra_destructor;/* extra data destructor */
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800159#ifdef CONFIG_PM
160 pm_hook suspend_resume_hook;
161#endif
Alan Stern25ff1c32008-12-15 12:43:41 -0500162
163 /* hacks for READ CAPACITY bug handling */
164 int use_last_sector_hacks;
165 int last_sector_retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168/* Convert between us_data and the corresponding Scsi_Host */
Jesper Juhlf274afc2006-06-26 19:01:01 +0200169static inline struct Scsi_Host *us_to_host(struct us_data *us) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return container_of((void *) us, struct Scsi_Host, hostdata);
171}
Jesper Juhlf274afc2006-06-26 19:01:01 +0200172static inline struct us_data *host_to_us(struct Scsi_Host *host) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return (struct us_data *) host->hostdata;
174}
175
176/* Function to fill an inquiry response. See usb.c for details */
177extern void fill_inquiry_response(struct us_data *us,
178 unsigned char *data, unsigned int data_len);
179
Felipe Balbif0183a32016-04-18 13:09:11 +0300180/*
181 * The scsi_lock() and scsi_unlock() macros protect the sm_state and the
182 * single queue element srb for write access
183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define scsi_unlock(host) spin_unlock_irq(host->host_lock)
185#define scsi_lock(host) spin_lock_irq(host->host_lock)
186
Alan Sterne6e244b2009-02-12 14:47:44 -0500187/* General routines provided by the usb-storage standard core */
188#ifdef CONFIG_PM
189extern int usb_stor_suspend(struct usb_interface *iface, pm_message_t message);
190extern int usb_stor_resume(struct usb_interface *iface);
191extern int usb_stor_reset_resume(struct usb_interface *iface);
192#else
193#define usb_stor_suspend NULL
194#define usb_stor_resume NULL
195#define usb_stor_reset_resume NULL
196#endif
197
198extern int usb_stor_pre_reset(struct usb_interface *iface);
199extern int usb_stor_post_reset(struct usb_interface *iface);
200
201extern int usb_stor_probe1(struct us_data **pus,
202 struct usb_interface *intf,
203 const struct usb_device_id *id,
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900204 struct us_unusual_dev *unusual_dev,
205 struct scsi_host_template *sht);
Alan Sterne6e244b2009-02-12 14:47:44 -0500206extern int usb_stor_probe2(struct us_data *us);
207extern void usb_stor_disconnect(struct usb_interface *intf);
208
Hans de Goeded24d4812013-11-16 12:09:39 +0100209extern void usb_stor_adjust_quirks(struct usb_device *dev,
210 unsigned long *fflags);
211
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900212#define module_usb_stor_driver(__driver, __sht, __name) \
213static int __init __driver##_init(void) \
214{ \
215 usb_stor_host_template_init(&(__sht), __name, THIS_MODULE); \
216 return usb_register(&(__driver)); \
217} \
218module_init(__driver##_init); \
219static void __exit __driver##_exit(void) \
220{ \
221 usb_deregister(&(__driver)); \
222} \
223module_exit(__driver##_exit)
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#endif