Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[firefly-linux-kernel-4.4.55.git] / drivers / staging / ced1401 / usb1401.h
1 /* usb1401.h
2  Header file for the CED 1401 USB device driver for Linux
3  Copyright (C) 2010 Cambridge Electronic Design Ltd
4  Author Greg P Smith (greg@ced.co.uk)
5
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20 #ifndef __USB1401_H__
21 #define __USB1401_H__
22 #include "use1401.h"
23 #include "ced_ioctl.h"
24
25 #ifndef UINT
26 #define UINT unsigned int
27 #endif
28
29 /// Device type codes, but these don't need to be extended - a succession is assumed
30 /// These are set for usb from the bcdDevice field (suitably mangled). Future devices
31 /// will be added in order of device creation to the list, so the names here are just
32 /// to help use remember which device is which. The U14ERR_... values follow the same
33 /// pattern for modern devices.
34 #define TYPEUNKNOWN        -1             // dont know
35 #define TYPE1401           0              // standard 1401
36 #define TYPEPLUS           1              // 1401 plus
37 #define TYPEU1401          2              // u1401
38 #define TYPEPOWER          3              // Power1401
39 #define TYPEU14012         4              // u1401 mkII
40 #define TYPEPOWER2         5              // Power1401 mk II
41 #define TYPEMICRO3         6              // Micro1401-3
42 #define TYPEPOWER3         7              // Power1401-3
43
44 /// Some useful defines of constants. DONT FORGET to change the version in the
45 /// resources whenever you change it here!.
46 #define DRIVERMAJREV      2             // driver revision level major (match windows)
47 #define DRIVERMINREV      0             // driver revision level minor
48
49 /// Definitions of the various block transfer command codes
50 #define TM_EXTTOHOST    8               // extended tohost
51 #define TM_EXTTO1401    9               // extended to1401
52
53 /// Definitions of values in usbReqtype. Used in sorting out setup actions
54 #define H_TO_D 0x00
55 #define D_TO_H 0x80
56 #define VENDOR 0x40
57 #define DEVREQ 0x00
58 #define INTREQ 0x01
59 #define ENDREQ 0x02
60
61 /// Definition of values in usbRequest, again used to sort out setup
62 #define GET_STATUS      0x00
63 #define CLEAR_FEATURE   0x01
64 #define SET_FEATURE     0x03
65 #define SET_ADDRESS     0x05
66 #define GET_DESC        0x06
67 #define SET_DESC        0x07
68 #define GET_CONF        0x08
69 #define SET_CONF        0x09
70 #define GET_INTERFACE   0x0a
71 #define SET_INTERFACE   0x0b
72 #define SYNCH_FRAME     0x0c
73
74 /// Definitions of the various debug command codes understood by the 1401. These
75 /// are used in various vendor-specific commands to achieve the desired effect
76 #define DB_GRAB         0x50            /* Grab is a NOP for USB */
77 #define DB_FREE         0x51            /* Free is a NOP for the USB */
78 #define DB_SETADD       0x52            /* Set debug address (double) */
79 #define DB_SELFTEST     0x53            /* Start self test */
80 #define DB_SETMASK      0x54            /* Set enable mask (double) */
81 #define DB_SETDEF       0x55            /* Set default mask (double) */
82 #define DB_PEEK         0x56            /* Peek address, save result */
83 #define DB_POKE         0x57            /* Poke address with data (double) */
84 #define DB_RAMPD        0x58            /* Ramp data at debug address */
85 #define DB_RAMPA        0x59            /* Ramp address bus */
86 #define DB_REPEATS      0x5A            /* Set repeats for operations (double) */
87 #define DB_WIDTH        0x5B            /* Set width for operations (byte) */
88 #define DB_DATA         0x5C            /* Get 4-byte data read by PEEK */
89 #define DB_CHARS        0x5D            /* Send chars via EP0 control write */
90
91 #define CR_CHAR          0x0D           /* The carriage return character */
92 #define CR_CHAR_80       0x8d           /*  and with bit 7 set */
93
94 /// A structure holding information about a block of memory for use in circular transfers
95 typedef struct circBlk
96 {
97     volatile UINT dwOffset;             /* Offset within area of block start */
98     volatile UINT dwSize;               /* Size of the block, in bytes (0 = unused) */
99 } CIRCBLK;
100
101 /// A structure holding all of the information about a transfer area - an area of
102 ///  memory set up for use either as a source or destination in DMA transfers.
103 typedef struct transarea
104 {
105     void*       lpvBuff;                // User address of xfer area saved for completeness
106     UINT        dwBaseOffset;           // offset to start of xfer area in first page
107     UINT        dwLength;               // Length of xfer area, in bytes
108     struct page **pPages;               // Points at array of locked down pages
109     int         nPages;                 // number of pages that are locked down
110     bool        bUsed;                  // Is this structure in use?
111     bool        bCircular;              // Is this area for circular transfers?
112     bool        bCircToHost;            // Flag for direction of circular transfer
113     bool        bEventToHost;           // Set event on transfer to host?
114     int         iWakeUp;                // Set 1 on event, cleared by TestEvent()
115     UINT        dwEventSt;              // Defines section within xfer area for...
116     UINT        dwEventSz;              // ...notification by the event SZ is 0 if unset
117     CIRCBLK     aBlocks[2];             // Info on a pair of circular blocks
118     wait_queue_head_t wqEvent;          // The wait queue for events in this area MUST BE LAST
119 } TRANSAREA;
120
121 /// The DMADESC structure is used to hold information on the transfer in progress. It
122 /// is set up by ReadDMAInfo, using information sent by the 1401 in an escape sequence.
123 typedef struct dmadesc
124 {
125     unsigned short wTransType;          /* transfer type as TM_xxx above        */
126     unsigned short wIdent;              /* identifier word                      */
127     unsigned int   dwSize;              /* bytes to transfer                    */
128     unsigned int   dwOffset;            /* offset into transfer area for trans  */
129     bool           bOutWard;            /* true when data is going TO 1401      */
130 } DMADESC;
131
132 #define INBUF_SZ         256            /* input buffer size */
133 #define OUTBUF_SZ        256            /* output buffer size */
134 #define STAGED_SZ 0x10000               // size of coherent buffer for staged transfers
135
136 /// Structure to hold all of our device specific stuff. We are making this as similar as we
137 /// can to the Windows driver to help in our understanding of what is going on.
138 typedef struct _DEVICE_EXTENSION
139 {
140     char inputBuffer[INBUF_SZ];         /* The two buffers */
141     char outputBuffer[OUTBUF_SZ];       /* accessed by the host functions */
142     volatile unsigned int dwNumInput;   /* num of chars in input buffer   */
143     volatile unsigned int dwInBuffGet;  /* where to get from input buffer */
144     volatile unsigned int dwInBuffPut;  /* where to put into input buffer */
145     volatile unsigned int dwNumOutput;  /* num of chars in output buffer  */
146     volatile unsigned int dwOutBuffGet; /* where to get from output buffer*/
147     volatile unsigned int dwOutBuffPut; /* where to put into output buffer*/
148
149     volatile bool bSendCharsPending;    /* Flag to indicate sendchar active */
150     volatile bool bReadCharsPending;    /* Flag to indicate a read is primed */
151     char* pCoherCharOut;                /* special aligned buffer for chars to 1401 */
152     struct urb* pUrbCharOut;            /* urb used for chars to 1401 */
153     char* pCoherCharIn;                 /* special aligned buffer for chars to host */
154     struct urb* pUrbCharIn;             /* urb used for chars to host */
155
156     spinlock_t charOutLock;             /* to protect the outputBuffer and outputting */
157     spinlock_t charInLock;              /* to protect the inputBuffer and char reads */
158     __u8 bInterval;                     /* Interrupt end point interval */
159
160     volatile unsigned int dwDMAFlag;    /* state of DMA */
161     TRANSAREA rTransDef[MAX_TRANSAREAS];/* transfer area info */
162     volatile DMADESC rDMAInfo;          // info on current DMA transfer
163     volatile bool bXFerWaiting;         // Flag set if DMA transfer stalled
164     volatile bool bInDrawDown;          // Flag that we want to halt transfers
165
166     // Parameters relating to a block read\write that is in progress. Some of these values
167     //  are equivalent to values in rDMAInfo. The values here are those in use, while those
168     //  in rDMAInfo are those received from the 1401 via an escape sequence. If another
169     //  escape sequence arrives before the previous xfer ends, rDMAInfo values are updated while these
170     //  are used to finish off the current transfer.
171     volatile short StagedId;            // The transfer area id for this transfer
172     volatile bool StagedRead;           // Flag TRUE for read from 1401, FALSE for write
173     volatile unsigned int StagedLength; // Total length of this transfer
174     volatile unsigned int StagedOffset; // Offset within memory area for transfer start
175     volatile unsigned int StagedDone;   // Bytes transferred so far
176     volatile bool bStagedUrbPending;    // Flag to indicate active
177     char* pCoherStagedIO;               // buffer used for block transfers
178     struct urb* pStagedUrb;             // The URB to use
179     spinlock_t stagedLock;              // protects ReadWriteMem() and circular buffer stuff
180
181     short s1401Type;                    // type of 1401 attached
182     short sCurrentState;                // current error state
183     bool bIsUSB2;                       // type of the interface we connect to
184     bool bForceReset;                   // Flag to make sure we get a real reset
185     __u32 statBuf[2];                   // buffer for 1401 state info
186
187     unsigned long ulSelfTestTime;       // used to timeout self test
188
189     int nPipes;                         // Should be 3 or 4 depending on 1401 usb chip
190     int bPipeError[4];                  // set non-zero if an error on one of the pipe
191     __u8 epAddr[4];                     // addresses of the 3/4 end points
192
193     struct usb_device *udev;            // the usb device for this device
194     struct usb_interface *interface;    // the interface for this device, NULL if removed
195     struct usb_anchor submitted;        // in case we need to retract our submissions
196     struct mutex io_mutex;              // synchronize I/O with disconnect, one user-mode caller at a time
197
198     int    errors;                      // the last request tanked
199     int    open_count;                  // count the number of openers
200     spinlock_t err_lock;                // lock for errors
201     struct kref kref;
202 }DEVICE_EXTENSION, *PDEVICE_EXTENSION;
203 #define to_DEVICE_EXTENSION(d) container_of(d, DEVICE_EXTENSION, kref)
204
205 /// Definitions of routimes used between compilation object files
206 // in usb1401.c
207 extern int Allowi(DEVICE_EXTENSION* pdx, bool bInCallback);
208 extern int SendChars(DEVICE_EXTENSION* pdx);
209 extern void ced_draw_down(DEVICE_EXTENSION *pdx);
210 extern int ReadWriteMem(DEVICE_EXTENSION *pdx, bool Read, unsigned short wIdent,
211                       unsigned int dwOffs, unsigned int dwLen);
212
213 // in ced_ioc.c
214 extern int ClearArea(DEVICE_EXTENSION *pdx, int nArea);
215 extern int SendString(DEVICE_EXTENSION* pdx, const char __user* pData, unsigned int n);
216 extern int SendChar(DEVICE_EXTENSION *pdx, char c);
217 extern int Get1401State(DEVICE_EXTENSION* pdx, __u32* state, __u32* error);
218 extern int ReadWrite_Cancel(DEVICE_EXTENSION *pdx);
219 extern bool Is1401(DEVICE_EXTENSION* pdx);
220 extern bool QuickCheck(DEVICE_EXTENSION* pdx, bool bTestBuff, bool bCanReset);
221 extern int Reset1401(DEVICE_EXTENSION *pdx);
222 extern int GetChar(DEVICE_EXTENSION *pdx);
223 extern int GetString(DEVICE_EXTENSION *pdx, char __user* pUser, int n);
224 extern int SetTransfer(DEVICE_EXTENSION *pdx, TRANSFERDESC __user *pTD);
225 extern int UnsetTransfer(DEVICE_EXTENSION *pdx, int nArea);
226 extern int SetEvent(DEVICE_EXTENSION *pdx, TRANSFEREVENT __user*pTE);
227 extern int Stat1401(DEVICE_EXTENSION *pdx);
228 extern int LineCount(DEVICE_EXTENSION *pdx);
229 extern int GetOutBufSpace(DEVICE_EXTENSION *pdx);
230 extern int GetTransfer(DEVICE_EXTENSION *pdx, TGET_TX_BLOCK __user *pGTB);
231 extern int KillIO1401(DEVICE_EXTENSION *pdx);
232 extern int BlkTransState(DEVICE_EXTENSION *pdx);
233 extern int StateOf1401(DEVICE_EXTENSION *pdx);
234 extern int StartSelfTest(DEVICE_EXTENSION *pdx);
235 extern int CheckSelfTest(DEVICE_EXTENSION *pdx, TGET_SELFTEST __user *pGST);
236 extern int TypeOf1401(DEVICE_EXTENSION *pdx);
237 extern int TransferFlags(DEVICE_EXTENSION *pdx);
238 extern int DbgPeek(DEVICE_EXTENSION *pdx, TDBGBLOCK __user* pDB);
239 extern int DbgPoke(DEVICE_EXTENSION *pdx, TDBGBLOCK __user *pDB);
240 extern int DbgRampData(DEVICE_EXTENSION *pdx, TDBGBLOCK __user *pDB);
241 extern int DbgRampAddr(DEVICE_EXTENSION *pdx, TDBGBLOCK __user *pDB);
242 extern int DbgGetData(DEVICE_EXTENSION *pdx, TDBGBLOCK __user *pDB);
243 extern int DbgStopLoop(DEVICE_EXTENSION *pdx);
244 extern int SetCircular(DEVICE_EXTENSION *pdx, TRANSFERDESC __user *pTD);
245 extern int GetCircBlock(DEVICE_EXTENSION *pdx, TCIRCBLOCK __user* pCB);
246 extern int FreeCircBlock(DEVICE_EXTENSION *pdx, TCIRCBLOCK __user* pCB);
247 extern int WaitEvent(DEVICE_EXTENSION *pdx, int nArea, int msTimeOut);
248 extern int TestEvent(DEVICE_EXTENSION *pdx, int nArea);
249 #endif