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