0cf900507beeeb7c8f821924b5eafa2c80346cc5
[linux-2.4.git] / drivers / usb / storage / usb.h
1 /* Driver for USB Mass Storage compliant devices
2  * Main Header File
3  *
4  * $Id: usb.h,v 1.18 2001/07/30 00:27:59 mdharm Exp $
5  *
6  * Current development and maintenance by:
7  *   (c) 1999, 2000 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/blk.h>
49 #include <linux/smp_lock.h>
50 #include <linux/completion.h>
51 #include <linux/spinlock.h>
52 #include <asm/atomic.h>
53 #include "scsi.h"
54 #include "hosts.h"
55
56 /* 
57  * GUID definitions
58  */
59
60 #define GUID(x) __u32 x[3]
61 #define GUID_EQUAL(x, y) (x[0] == y[0] && x[1] == y[1] && x[2] == y[2])
62 #define GUID_CLEAR(x) x[0] = x[1] = x[2] = 0;
63 #define GUID_NONE(x) (!x[0] && !x[1] && !x[2])
64 #define GUID_FORMAT "%08x%08x%08x"
65 #define GUID_ARGS(x) x[0], x[1], x[2]
66
67 static inline void make_guid( __u32 *pg, __u16 vendor, __u16 product, char *serial)
68 {
69         pg[0] = (vendor << 16) | product;
70         pg[1] = pg[2] = 0;
71         while (*serial) {
72                 pg[1] <<= 4;
73                 pg[1] |= pg[2] >> 28;
74                 pg[2] <<= 4;
75                 if (*serial >= 'a')
76                         *serial -= 'a' - 'A';
77                 pg[2] |= (*serial <= '9' && *serial >= '0') ? *serial - '0'
78                         : *serial - 'A' + 10;
79                 serial++;
80         }
81 }
82
83 struct us_data;
84
85 /*
86  * Unusual device list definitions 
87  */
88
89 struct us_unusual_dev {
90         const char* vendorName;
91         const char* productName;
92         __u8  useProtocol;
93         __u8  useTransport;
94         int (*initFunction)(struct us_data *);
95         unsigned int flags;
96 };
97
98 /* Flag definitions */
99 #define US_FL_SINGLE_LUN      0x00000001 /* allow access to only LUN 0      */
100 #define US_FL_MODE_XLATE      0x00000002 /* translate _6 to _10 commands for
101                                                     Win/MacOS compatibility */
102 #define US_FL_IGNORE_SER      0x00000010 /* Ignore the serial number given  */
103 #define US_FL_SCM_MULT_TARG   0x00000020 /* supports multiple targets */
104 #define US_FL_FIX_INQUIRY     0x00000040 /* INQUIRY response needs fixing */
105 #define US_FL_FIX_CAPACITY    0x00000080 /* READ_CAPACITY response too big */
106
107 #define USB_STOR_STRING_LEN 32
108
109 typedef int (*trans_cmnd)(Scsi_Cmnd*, struct us_data*);
110 typedef int (*trans_reset)(struct us_data*);
111 typedef void (*proto_cmnd)(Scsi_Cmnd*, struct us_data*);
112 typedef void (*extra_data_destructor)(void *);   /* extra data destructor   */
113
114 /* we allocate one of these for every device that we remember */
115 struct us_data {
116         struct us_data          *next;           /* next device */
117
118         /* the device we're working with */
119         struct semaphore        dev_semaphore;   /* protect many things */
120         struct usb_device       *pusb_dev;       /* this usb_device */
121
122         unsigned int            flags;           /* from filter initially */
123
124         /* information about the device -- always good */
125         char                    vendor[USB_STOR_STRING_LEN];
126         char                    product[USB_STOR_STRING_LEN];
127         char                    serial[USB_STOR_STRING_LEN];
128         char                    *transport_name;
129         char                    *protocol_name;
130         u8                      subclass;
131         u8                      protocol;
132         u8                      max_lun;
133
134         /* information about the device -- only good if device is attached */
135         u8                      ifnum;           /* interface number   */
136         u8                      ep_in;           /* bulk in endpoint   */
137         u8                      ep_out;          /* bulk out endpoint  */
138         struct usb_endpoint_descriptor *ep_int;  /* interrupt endpoint */ 
139
140         /* function pointers for this device */
141         trans_cmnd              transport;       /* transport function     */
142         trans_reset             transport_reset; /* transport device reset */
143         proto_cmnd              proto_handler;   /* protocol handler       */
144
145         /* SCSI interfaces */
146         GUID(guid);                              /* unique dev id       */
147         struct Scsi_Host        *host;           /* our dummy host data */
148         Scsi_Host_Template      htmplt;          /* own host template   */
149         int                     host_number;     /* to find us          */
150         int                     host_no;         /* allocated by scsi   */
151         Scsi_Cmnd               *srb;            /* current srb         */
152         atomic_t                abortcnt;        /* must complete(&notify) */
153         
154
155         /* thread information */
156         Scsi_Cmnd               *queue_srb;      /* the single queue slot */
157         int                     action;          /* what to do            */
158         pid_t                   pid;             /* control thread        */
159
160         /* interrupt info for CBI devices -- only good if attached */
161         struct semaphore        ip_waitq;        /* for CBI interrupts   */
162         atomic_t                ip_wanted[1];    /* is an IRQ expected?  */
163
164         /* interrupt communications data */
165         struct urb              *irq_urb;        /* for USB int requests */
166         unsigned char           irqbuf[2];       /* buffer for USB IRQ   */
167         unsigned char           irqdata[2];      /* data from USB IRQ    */
168
169         /* control and bulk communications data */
170         struct semaphore        current_urb_sem; /* to protect irq_urb   */
171         struct urb              *current_urb;    /* non-int USB requests */
172         struct completion       current_done;    /* the done flag        */
173         unsigned int            tag;             /* tag for bulk CBW/CSW */
174
175         /* the semaphore for sleeping the control thread */
176         struct semaphore        sema;            /* to sleep thread on   */
177
178         /* mutual exclusion structures */
179         struct completion       notify;          /* thread begin/end        */
180         spinlock_t              queue_exclusion; /* to protect data structs */
181         struct us_unusual_dev   *unusual_dev;    /* If unusual device       */
182         void                    *extra;          /* Any extra data          */
183         extra_data_destructor   extra_destructor;/* extra data destructor   */
184 };
185
186 /* The list of structures and the protective lock for them */
187 extern struct us_data *us_list;
188 extern struct semaphore us_list_semaphore;
189
190 /* The structure which defines our driver */
191 extern struct usb_driver usb_storage_driver;
192
193 /* Function to fill an inquiry response. See usb.c for details */
194 extern void fill_inquiry_response(struct us_data *us,
195         unsigned char *data, unsigned int data_len);
196
197 /* Vendor ID list for devices that require special handling */
198 #define USB_VENDOR_ID_GENESYS           0x05e3  /* Genesys Logic */
199 #endif