blob: 85052cd66839180f97b40700d8def7952d1de63a [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Felipe Balbif0183a32016-04-18 13:09:11 +03002/*
3 * Driver for USB Mass Storage compliant devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Main Header File
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27#ifndef _USB_H_
28#define _USB_H_
29
30#include <linux/usb.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070031#include <linux/usb_usual.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/completion.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010034#include <linux/mutex.h>
Alan Sternbb94a402012-02-21 13:16:32 -050035#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <scsi/scsi_host.h>
37
38struct us_data;
39struct scsi_cmnd;
40
41/*
42 * Unusual device list definitions
43 */
44
45struct us_unusual_dev {
46 const char* vendorName;
47 const char* productName;
48 __u8 useProtocol;
49 __u8 useTransport;
50 int (*initFunction)(struct us_data *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Alan Stern7e4d6c32008-05-01 15:35:18 -040054/* Dynamic bitflag definitions (us->dflags): used in set_bit() etc. */
55#define US_FLIDX_URB_ACTIVE 0 /* current_urb is in use */
56#define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */
57#define US_FLIDX_ABORTING 2 /* abort is in progress */
58#define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */
Alan Stern7e4d6c32008-05-01 15:35:18 -040059#define US_FLIDX_RESETTING 4 /* device reset in progress */
60#define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */
Alan Sternbb94a402012-02-21 13:16:32 -050061#define US_FLIDX_SCAN_PENDING 6 /* scanning not yet done */
Alan Stern21c13a42011-06-07 11:35:52 -040062#define US_FLIDX_REDO_READ10 7 /* redo READ(10) command */
63#define US_FLIDX_READ10_WORKED 8 /* previous READ(10) succeeded */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#define USB_STOR_STRING_LEN 32
66
67/*
68 * We provide a DMA-mapped I/O buffer for use with small USB transfers.
69 * It turns out that CB[I] needs a 12-byte buffer and Bulk-only needs a
70 * 31-byte buffer. But Freecom needs a 64-byte buffer, so that's the
71 * size we'll allocate.
72 */
73
74#define US_IOBUF_SIZE 64 /* Size of the DMA-mapped I/O buffer */
Alan Sternbbafa462005-10-23 19:40:22 -070075#define US_SENSE_SIZE 18 /* Size of the autosense data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77typedef int (*trans_cmnd)(struct scsi_cmnd *, struct us_data*);
78typedef int (*trans_reset)(struct us_data*);
79typedef void (*proto_cmnd)(struct scsi_cmnd*, struct us_data*);
Matthew Dharm7931e1c2005-12-04 21:56:51 -080080typedef void (*extra_data_destructor)(void *); /* extra data destructor */
81typedef void (*pm_hook)(struct us_data *, int); /* power management hook */
82
83#define US_SUSPEND 0
84#define US_RESUME 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* we allocate one of these for every device that we remember */
87struct us_data {
Felipe Balbif0183a32016-04-18 13:09:11 +030088 /*
89 * The device we're working with
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * It's important to note:
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010091 * (o) you must hold dev_mutex to change pusb_dev
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010093 struct mutex dev_mutex; /* protect pusb_dev */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct usb_device *pusb_dev; /* this usb_device */
95 struct usb_interface *pusb_intf; /* this interface */
96 struct us_unusual_dev *unusual_dev; /* device-filter entry */
Alan Stern7e4d6c32008-05-01 15:35:18 -040097 unsigned long fflags; /* fixed flags from filter */
98 unsigned long dflags; /* dynamic atomic bitflags */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned int send_bulk_pipe; /* cached pipe values */
100 unsigned int recv_bulk_pipe;
101 unsigned int send_ctrl_pipe;
102 unsigned int recv_ctrl_pipe;
103 unsigned int recv_intr_pipe;
104
105 /* information about the device */
106 char *transport_name;
107 char *protocol_name;
108 __le32 bcs_signature;
109 u8 subclass;
110 u8 protocol;
111 u8 max_lun;
112
113 u8 ifnum; /* interface number */
Felipe Balbif0183a32016-04-18 13:09:11 +0300114 u8 ep_bInterval; /* interrupt interval */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /* function pointers for this device */
117 trans_cmnd transport; /* transport function */
118 trans_reset transport_reset; /* transport device reset */
119 proto_cmnd proto_handler; /* protocol handler */
120
121 /* SCSI interfaces */
122 struct scsi_cmnd *srb; /* current srb */
Matthew Dharm0f64e072005-07-28 14:43:08 -0700123 unsigned int tag; /* current dCBWTag */
Matthew Wilcox00fa43e2009-09-24 16:19:11 -0600124 char scsi_name[32]; /* scsi_host name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /* control and bulk communications data */
127 struct urb *current_urb; /* USB requests */
128 struct usb_ctrlrequest *cr; /* control requests */
129 struct usb_sg_request current_sg; /* scatter-gather req. */
130 unsigned char *iobuf; /* I/O buffer */
Alan Stern0ede76f2010-03-05 15:10:17 -0500131 dma_addr_t iobuf_dma; /* buffer DMA addresses */
Alan Sterned76cac2007-06-07 17:12:25 -0400132 struct task_struct *ctl_thread; /* the control thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /* mutual exclusion and synchronization structures */
Alan Stern7119e3c2008-05-01 15:36:13 -0400135 struct completion cmnd_ready; /* to sleep thread on */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct completion notify; /* thread begin/end */
Alan Sternbb94a402012-02-21 13:16:32 -0500137 wait_queue_head_t delay_wait; /* wait during reset */
138 struct delayed_work scan_dwork; /* for async scanning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* subdriver information */
141 void *extra; /* Any extra data */
142 extra_data_destructor extra_destructor;/* extra data destructor */
Matthew Dharm7931e1c2005-12-04 21:56:51 -0800143#ifdef CONFIG_PM
144 pm_hook suspend_resume_hook;
145#endif
Alan Stern25ff1c32008-12-15 12:43:41 -0500146
147 /* hacks for READ CAPACITY bug handling */
148 int use_last_sector_hacks;
149 int last_sector_retries;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152/* Convert between us_data and the corresponding Scsi_Host */
Jesper Juhlf274afc2006-06-26 19:01:01 +0200153static inline struct Scsi_Host *us_to_host(struct us_data *us) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return container_of((void *) us, struct Scsi_Host, hostdata);
155}
Jesper Juhlf274afc2006-06-26 19:01:01 +0200156static inline struct us_data *host_to_us(struct Scsi_Host *host) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return (struct us_data *) host->hostdata;
158}
159
160/* Function to fill an inquiry response. See usb.c for details */
161extern void fill_inquiry_response(struct us_data *us,
162 unsigned char *data, unsigned int data_len);
163
Felipe Balbif0183a32016-04-18 13:09:11 +0300164/*
165 * The scsi_lock() and scsi_unlock() macros protect the sm_state and the
166 * single queue element srb for write access
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#define scsi_unlock(host) spin_unlock_irq(host->host_lock)
169#define scsi_lock(host) spin_lock_irq(host->host_lock)
170
Alan Sterne6e244b2009-02-12 14:47:44 -0500171/* General routines provided by the usb-storage standard core */
172#ifdef CONFIG_PM
173extern int usb_stor_suspend(struct usb_interface *iface, pm_message_t message);
174extern int usb_stor_resume(struct usb_interface *iface);
175extern int usb_stor_reset_resume(struct usb_interface *iface);
176#else
177#define usb_stor_suspend NULL
178#define usb_stor_resume NULL
179#define usb_stor_reset_resume NULL
180#endif
181
182extern int usb_stor_pre_reset(struct usb_interface *iface);
183extern int usb_stor_post_reset(struct usb_interface *iface);
184
185extern int usb_stor_probe1(struct us_data **pus,
186 struct usb_interface *intf,
187 const struct usb_device_id *id,
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900188 struct us_unusual_dev *unusual_dev,
189 struct scsi_host_template *sht);
Alan Sterne6e244b2009-02-12 14:47:44 -0500190extern int usb_stor_probe2(struct us_data *us);
191extern void usb_stor_disconnect(struct usb_interface *intf);
192
Hans de Goeded24d4812013-11-16 12:09:39 +0100193extern void usb_stor_adjust_quirks(struct usb_device *dev,
194 unsigned long *fflags);
195
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900196#define module_usb_stor_driver(__driver, __sht, __name) \
197static int __init __driver##_init(void) \
198{ \
199 usb_stor_host_template_init(&(__sht), __name, THIS_MODULE); \
200 return usb_register(&(__driver)); \
201} \
202module_init(__driver##_init); \
203static void __exit __driver##_exit(void) \
204{ \
205 usb_deregister(&(__driver)); \
206} \
207module_exit(__driver##_exit)
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#endif