Linux 2.6.31-rc6
[linux-2.6] / drivers / staging / cpc-usb / cpc.h
1 /*
2  * CPC CAN Interface Definitions
3  *
4  * Copyright (C) 2000-2008 EMS Dr. Thomas Wuensche
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  */
10 #ifndef CPC_HEADER
11 #define CPC_HEADER
12
13 // the maximum length of the union members within a CPC_MSG
14 // this value can be defined by the customer, but has to be
15 // >= 64 bytes
16 // however, if not defined before, we set a length of 64 byte
17 #if !defined(CPC_MSG_LEN) || (CPC_MSG_LEN < 64)
18 #undef CPC_MSG_LEN
19 #define CPC_MSG_LEN 64
20 #endif
21
22 // check the operating system used
23 #ifdef _WIN32 // running a Windows OS
24
25 // define basic types on Windows platforms
26 #ifdef _MSC_VER // Visual Studio
27         typedef unsigned __int8 u8;
28         typedef unsigned __int16 u16;
29         typedef unsigned __int32 u32;
30 #else // Borland Compiler
31         typedef unsigned char u8;
32         typedef unsigned short u16;
33         typedef unsigned int u32;
34 #endif
35         // on Windows OS we use a byte alignment of 1
36         #pragma pack(push, 1)
37
38         // set the calling conventions for the library function calls
39         #define CALL_CONV __stdcall
40 #else
41         // Kernel headers already define this types
42         #ifndef __KERNEL__
43                 // define basic types
44                 typedef unsigned char u8;
45                 typedef unsigned short u16;
46                 typedef unsigned int u32;
47         #endif
48
49         // Linux does not use this calling convention
50         #define CALL_CONV
51 #endif
52
53 // Transmission of events from CPC interfaces to PC can be individually
54 // controlled per event type. Default state is: don't transmit
55 // Control values are constructed by bit-or of Subject and Action
56 // and passed to CPC_Control()
57
58 // Control-Values for CPC_Control() Command Subject Selection
59 #define CONTR_CAN_Message 0x04
60 #define CONTR_Busload     0x08
61 #define CONTR_CAN_State   0x0C
62 #define CONTR_SendAck     0x10
63 #define CONTR_Filter      0x14
64 #define CONTR_CmdQueue    0x18  // reserved, do not use
65 #define CONTR_BusError    0x1C
66
67 // Control Command Actions
68 #define CONTR_CONT_OFF    0
69 #define CONTR_CONT_ON     1
70 #define CONTR_SING_ON     2
71 // CONTR_SING_ON doesn't change CONTR_CONT_ON state, so it should be
72 // read as: transmit at least once
73
74 // defines for confirmed request
75 #define DO_NOT_CONFIRM 0
76 #define DO_CONFIRM     1
77
78 // event flags
79 #define EVENT_READ 0x01
80 #define EVENT_WRITE 0x02
81
82 // Messages from CPC to PC contain a message object type field.
83 // The following message types are sent by CPC and can be used in
84 // handlers, others should be ignored.
85 #define CPC_MSG_T_RESYNC        0 // Normally to be ignored
86 #define CPC_MSG_T_CAN           1 // CAN data frame
87 #define CPC_MSG_T_BUSLOAD       2 // Busload message
88 #define CPC_MSG_T_STRING        3 // Normally to be ignored
89 #define CPC_MSG_T_CONTI         4 // Normally to be ignored
90 #define CPC_MSG_T_MEM           7 // Normally not to be handled
91 #define CPC_MSG_T_RTR           8 // CAN remote frame
92 #define CPC_MSG_T_TXACK         9 // Send acknowledge
93 #define CPC_MSG_T_POWERUP      10 // Power-up message
94 #define CPC_MSG_T_CMD_NO       11 // Normally to be ignored
95 #define CPC_MSG_T_CAN_PRMS     12 // Actual CAN parameters
96 #define CPC_MSG_T_ABORTED      13 // Command aborted message
97 #define CPC_MSG_T_CANSTATE     14 // CAN state message
98 #define CPC_MSG_T_RESET        15 // used to reset CAN-Controller
99 #define CPC_MSG_T_XCAN         16 // XCAN data frame
100 #define CPC_MSG_T_XRTR         17 // XCAN remote frame
101 #define CPC_MSG_T_INFO         18 // information strings
102 #define CPC_MSG_T_CONTROL      19 // used for control of interface/driver behaviour
103 #define CPC_MSG_T_CONFIRM      20 // response type for confirmed requests
104 #define CPC_MSG_T_OVERRUN      21 // response type for overrun conditions
105 #define CPC_MSG_T_KEEPALIVE    22 // response type for keep alive conditions
106 #define CPC_MSG_T_CANERROR     23 // response type for bus error conditions
107 #define CPC_MSG_T_DISCONNECTED 24 // response type for a disconnected interface
108 #define CPC_MSG_T_ERR_COUNTER  25 // RX/TX error counter of CAN controller
109
110 #define CPC_MSG_T_FIRMWARE    100 // response type for USB firmware download
111
112 // Messages from the PC to the CPC interface contain a command field
113 // Most of the command types are wrapped by the library functions and have therefore
114 // normally not to be used.
115 // However, programmers who wish to circumvent the library and talk directly
116 // to the drivers (mainly Linux programmers) can use the following
117 // command types:
118
119 #define CPC_CMD_T_CAN                 1 // CAN data frame
120 #define CPC_CMD_T_CONTROL             3 // used for control of interface/driver behaviour
121 #define CPC_CMD_T_CAN_PRMS            6 // set CAN parameters
122 #define CPC_CMD_T_CLEARBUF            8 // clears input queue; this is depricated, use CPC_CMD_T_CLEAR_MSG_QUEUE instead
123 #define CPC_CMD_T_INQ_CAN_PARMS      11 // inquire actual CAN parameters
124 #define CPC_CMD_T_FILTER_PRMS        12 // set filter parameter
125 #define CPC_CMD_T_RTR                13 // CAN remote frame
126 #define CPC_CMD_T_CANSTATE           14 // CAN state message
127 #define CPC_CMD_T_XCAN               15 // XCAN data frame
128 #define CPC_CMD_T_XRTR               16 // XCAN remote frame
129 #define CPC_CMD_T_RESET              17 // used to reset CAN-Controller
130 #define CPC_CMD_T_INQ_INFO           18 // miscellanous information strings
131 #define CPC_CMD_T_OPEN_CHAN          19 // open a channel
132 #define CPC_CMD_T_CLOSE_CHAN         20 // close a channel
133 #define CPC_CMD_T_CNTBUF             21 // this is depricated, use CPC_CMD_T_INQ_MSG_QUEUE_CNT instead
134 #define CPC_CMD_T_CAN_EXIT          200 // exit the CAN (disable interrupts; reset bootrate; reset output_cntr; mode = 1)
135
136 #define CPC_CMD_T_INQ_MSG_QUEUE_CNT  CPC_CMD_T_CNTBUF   // inquires the count of elements in the message queue
137 #define CPC_CMD_T_INQ_ERR_COUNTER    25                 // request the CAN controllers error counter
138 #define CPC_CMD_T_CLEAR_MSG_QUEUE    CPC_CMD_T_CLEARBUF // clear CPC_MSG queue
139 #define CPC_CMD_T_CLEAR_CMD_QUEUE    28                 // clear CPC_CMD queue
140 #define CPC_CMD_T_FIRMWARE          100                 // reserved, must not be used
141 #define CPC_CMD_T_USB_RESET         101                 // reserved, must not be used
142 #define CPC_CMD_T_WAIT_NOTIFY       102                 // reserved, must not be used
143 #define CPC_CMD_T_WAIT_SETUP        103                 // reserved, must not be used
144 #define CPC_CMD_T_ABORT             255                 // Normally not to be used
145
146 // definitions for CPC_MSG_T_INFO
147 // information sources
148 #define CPC_INFOMSG_T_UNKNOWN_SOURCE 0
149 #define CPC_INFOMSG_T_INTERFACE      1
150 #define CPC_INFOMSG_T_DRIVER         2
151 #define CPC_INFOMSG_T_LIBRARY        3
152
153 // information types
154 #define CPC_INFOMSG_T_UNKNOWN_TYPE   0
155 #define CPC_INFOMSG_T_VERSION        1
156 #define CPC_INFOMSG_T_SERIAL         2
157
158 // definitions for controller types
159 #define PCA82C200   1 // Philips basic CAN controller, replaced by SJA1000
160 #define SJA1000     2 // Philips basic CAN controller
161 #define AN82527     3 // Intel full CAN controller
162 #define M16C_BASIC  4 // M16C controller running in basic CAN (not full CAN) mode
163
164 // channel open error codes
165 #define CPC_ERR_NO_FREE_CHANNEL            -1   // no more free space within the channel array
166 #define CPC_ERR_CHANNEL_ALREADY_OPEN       -2   // the channel is already open
167 #define CPC_ERR_CHANNEL_NOT_ACTIVE         -3   // access to a channel not active failed
168 #define CPC_ERR_NO_DRIVER_PRESENT          -4   // no driver at the location searched by the library
169 #define CPC_ERR_NO_INIFILE_PRESENT         -5   // the library could not find the inifile
170 #define CPC_ERR_WRONG_PARAMETERS           -6   // wrong parameters in the inifile
171 #define CPC_ERR_NO_INTERFACE_PRESENT       -7   // 1. The specified interface is not connected
172                                                 // 2. The interface (mostly CPC-USB) was disconnected upon operation
173 #define CPC_ERR_NO_MATCHING_CHANNEL        -8   // the driver couldn't find a matching channel
174 #define CPC_ERR_NO_BUFFER_AVAILABLE        -9   // the driver couldn't allocate buffer for messages
175 #define CPC_ERR_NO_INTERRUPT               -10  // the requested interrupt couldn't be claimed
176 #define CPC_ERR_NO_MATCHING_INTERFACE      -11  // no interface type related to this channel was found
177 #define CPC_ERR_NO_RESOURCES               -12  // the requested resources could not be claimed
178 #define CPC_ERR_SOCKET                     -13  // error concerning TCP sockets
179
180 // init error codes
181 #define CPC_ERR_WRONG_CONTROLLER_TYPE      -14  // wrong CAN controller type within initialization
182 #define CPC_ERR_NO_RESET_MODE              -15  // the controller could not be set into reset mode
183 #define CPC_ERR_NO_CAN_ACCESS              -16  // the CAN controller could not be accessed
184
185 // transmit error codes
186 #define CPC_ERR_CAN_WRONG_ID               -20  // the provided CAN id is too big
187 #define CPC_ERR_CAN_WRONG_LENGTH           -21  // the provided CAN length is too long
188 #define CPC_ERR_CAN_NO_TRANSMIT_BUF        -22  // the transmit buffer was occupied
189 #define CPC_ERR_CAN_TRANSMIT_TIMEOUT       -23  // The message could not be sent within a
190                                                 // specified time
191
192 // other error codes
193 #define CPC_ERR_SERVICE_NOT_SUPPORTED      -30  // the requested service is not supported by the interface
194 #define CPC_ERR_IO_TRANSFER                -31  // a transmission error down to the driver occurred
195 #define CPC_ERR_TRANSMISSION_FAILED        -32  // a transmission error down to the interface occurred
196 #define CPC_ERR_TRANSMISSION_TIMEOUT       -33  // a timeout occurred within transmission to the interface
197 #define CPC_ERR_OP_SYS_NOT_SUPPORTED       -35  // the operating system is not supported
198 #define CPC_ERR_UNKNOWN                    -40  // an unknown error ocurred (mostly IOCTL errors)
199
200 #define CPC_ERR_LOADING_DLL                -50  // the library 'cpcwin.dll' could not be loaded
201 #define CPC_ERR_ASSIGNING_FUNCTION         -51  // the specified function could not be assigned
202 #define CPC_ERR_DLL_INITIALIZATION         -52  // the DLL was not initialized correctly
203 #define CPC_ERR_MISSING_LICFILE            -55  // the file containing the licenses does not exist
204 #define CPC_ERR_MISSING_LICENSE            -56  // a required license was not found
205
206 // CAN state bit values. Ignore any bits not listed
207 #define CPC_CAN_STATE_BUSOFF     0x80
208 #define CPC_CAN_STATE_ERROR      0x40
209
210 // Mask to help ignore undefined bits
211 #define CPC_CAN_STATE_MASK       0xc0
212
213 // CAN-Message representation in a CPC_MSG
214 // Message object type is CPC_MSG_T_CAN or CPC_MSG_T_RTR
215 // or CPC_MSG_T_XCAN or CPC_MSG_T_XRTR
216 typedef struct CPC_CAN_MSG {
217         u32 id;
218         u8 length;
219         u8 msg[8];
220 } CPC_CAN_MSG_T;
221
222
223 // representation of the CAN parameters for the PCA82C200 controller
224 typedef struct CPC_PCA82C200_PARAMS {
225         u8 acc_code;    // Acceptance-code for receive, Standard: 0
226         u8 acc_mask;    // Acceptance-mask for receive, Standard: 0xff (everything)
227         u8 btr0;        // Bus-timing register 0
228         u8 btr1;        // Bus-timing register 1
229         u8 outp_contr;  // Output-control register
230 } CPC_PCA82C200_PARAMS_T;
231
232 // representation of the CAN parameters for the SJA1000 controller
233 typedef struct CPC_SJA1000_PARAMS {
234         u8 mode;        // enables single or dual acceptance filtering
235         u8 acc_code0;   // Acceptance-code for receive, Standard: 0
236         u8 acc_code1;
237         u8 acc_code2;
238         u8 acc_code3;
239         u8 acc_mask0;   // Acceptance-mask for receive, Standard: 0xff (everything)
240         u8 acc_mask1;
241         u8 acc_mask2;
242         u8 acc_mask3;
243         u8 btr0;        // Bus-timing register 0
244         u8 btr1;        // Bus-timing register 1
245         u8 outp_contr;  // Output-control register
246 } CPC_SJA1000_PARAMS_T;
247
248 // representation of the CAN parameters for the M16C controller
249 // in basic CAN mode (means no full CAN)
250 typedef struct CPC_M16C_BASIC_PARAMS {
251         u8 con0;
252         u8 con1;
253         u8 ctlr0;
254         u8 ctlr1;
255         u8 clk;
256         u8 acc_std_code0;
257         u8 acc_std_code1;
258         u8 acc_ext_code0;
259         u8 acc_ext_code1;
260         u8 acc_ext_code2;
261         u8 acc_ext_code3;
262         u8 acc_std_mask0;
263         u8 acc_std_mask1;
264         u8 acc_ext_mask0;
265         u8 acc_ext_mask1;
266         u8 acc_ext_mask2;
267         u8 acc_ext_mask3;
268 } CPC_M16C_BASIC_PARAMS_T;
269
270 // CAN params message representation
271 typedef struct CPC_CAN_PARAMS {
272         u8 cc_type;     // represents the controller type
273         union {
274                 CPC_M16C_BASIC_PARAMS_T m16c_basic;
275                 CPC_SJA1000_PARAMS_T sja1000;
276                 CPC_PCA82C200_PARAMS_T pca82c200;
277         } cc_params;
278 } CPC_CAN_PARAMS_T;
279
280 // the following structures are slightly different for Windows and Linux
281 // To be able to use the 'Select' mechanism with Linux the application
282 // needs to know the devices file desciptor.
283 // This mechanism is not implemented within Windows and the file descriptor
284 // is therefore not needed
285 #ifdef _WIN32
286
287 // CAN init params message representation
288 typedef struct CPC_INIT_PARAMS {
289         CPC_CAN_PARAMS_T canparams;
290 } CPC_INIT_PARAMS_T;
291
292 #else// Linux
293
294 // CHAN init params representation
295 typedef struct CPC_CHAN_PARAMS {
296         int fd;
297 } CPC_CHAN_PARAMS_T;
298
299 // CAN init params message representation
300 typedef struct CPC_INIT_PARAMS {
301         CPC_CHAN_PARAMS_T chanparams;
302         CPC_CAN_PARAMS_T canparams;
303 } CPC_INIT_PARAMS_T;
304
305 #endif
306
307 // structure for confirmed message handling
308 typedef struct CPC_CONFIRM {
309         u8 result; // error code
310 } CPC_CONFIRM_T;
311
312 // structure for information requests
313 typedef struct CPC_INFO {
314         u8 source;                 // interface, driver or library
315         u8 type;                   // version or serial number
316         char msg[CPC_MSG_LEN - 2]; // string holding the requested information
317 } CPC_INFO_T;
318
319 // OVERRUN ///////////////////////////////////////
320 // In general two types of overrun may occur.
321 // A hardware overrun, where the CAN controller
322 // lost a message, because the interrupt was
323 // not handled before the next messgae comes in.
324 // Or a software overrun, where i.e. a received
325 // message could not be stored in the CPC_MSG
326 // buffer.
327
328 // After a software overrun has occurred
329 // we wait until we have CPC_OVR_GAP slots
330 // free in the CPC_MSG buffer.
331 #define CPC_OVR_GAP               10
332
333 // Two types of software overrun may occur.
334 // A received CAN message or a CAN state event
335 // can cause an overrun.
336 // Note: A CPC_CMD which would normally store
337 // its result immediately in the CPC_MSG
338 // queue may fail, because the message queue is full.
339 // This will not generate an overrun message, but
340 // will halt command execution, until this command
341 // is able to store its message in the message queue.
342 #define CPC_OVR_EVENT_CAN       0x01
343 #define CPC_OVR_EVENT_CANSTATE  0x02
344 #define CPC_OVR_EVENT_BUSERROR  0x04
345
346 // If the CAN controller lost a message
347 // we indicate it with the highest bit
348 // set in the count field.
349 #define CPC_OVR_HW              0x80
350
351 // structure for overrun conditions
352 typedef struct {
353         u8 event;
354         u8 count;
355 } CPC_OVERRUN_T;
356
357 // CAN errors ////////////////////////////////////
358 // Each CAN controller type has different
359 // registers to record errors.
360 // Therefor a structure containing the specific
361 // errors is set up for each controller here
362
363 // SJA1000 error structure
364 // see the SJA1000 datasheet for detailed
365 // explanation of the registers
366 typedef struct CPC_SJA1000_CAN_ERROR {
367         u8 ecc;   // error capture code register
368         u8 rxerr; // RX error counter register
369         u8 txerr; // TX error counter register
370 } CPC_SJA1000_CAN_ERROR_T;
371
372 // M16C error structure
373 // see the M16C datasheet for detailed
374 // explanation of the registers
375 typedef struct CPC_M16C_CAN_ERROR {
376         u8 tbd; // to be defined
377 } CPC_M16C_CAN_ERROR_T;
378
379 // structure for CAN error conditions
380 #define  CPC_CAN_ECODE_ERRFRAME   0x01
381 typedef struct CPC_CAN_ERROR {
382         u8 ecode;
383         struct {
384                 u8 cc_type; // CAN controller type
385                 union {
386                         CPC_SJA1000_CAN_ERROR_T sja1000;
387                         CPC_M16C_CAN_ERROR_T m16c;
388                 } regs;
389         } cc;
390 } CPC_CAN_ERROR_T;
391
392 // Structure containing RX/TX error counter.
393 // This structure is used to request the
394 // values of the CAN controllers TX and RX
395 // error counter.
396 typedef struct CPC_CAN_ERR_COUNTER {
397         u8 rx;
398         u8 tx;
399 } CPC_CAN_ERR_COUNTER_T;
400
401 // If this flag is set, transmissions from PC to CPC are protected against loss
402 #define CPC_SECURE_TO_CPC       0x01
403
404 // If this flag is set, transmissions from CPC to PC are protected against loss
405 #define CPC_SECURE_TO_PC        0x02
406
407 // If this flag is set, the CAN-transmit buffer is checked to be free before sending a message
408 #define CPC_SECURE_SEND         0x04
409
410 // If this flag is set, the transmission complete flag is checked
411 // after sending a message
412 // THIS IS CURRENTLY ONLY IMPLEMENTED IN THE PASSIVE INTERFACE DRIVERS
413 #define CPC_SECURE_TRANSMIT     0x08
414
415 // main message type used between library and application
416 typedef struct CPC_MSG {
417         u8 type;        // type of message
418         u8 length;      // length of data within union 'msg'
419         u8 msgid;       // confirmation handle
420         u32 ts_sec;     // timestamp in seconds
421         u32 ts_nsec;    // timestamp in nano seconds
422         union {
423                 u8 generic[CPC_MSG_LEN];
424                 CPC_CAN_MSG_T canmsg;
425                 CPC_CAN_PARAMS_T canparams;
426                 CPC_CONFIRM_T confirmation;
427                 CPC_INFO_T info;
428                 CPC_OVERRUN_T overrun;
429                 CPC_CAN_ERROR_T error;
430                 CPC_CAN_ERR_COUNTER_T err_counter;
431                 u8 busload;
432                 u8 canstate;
433         } msg;
434 } CPC_MSG_T;
435
436 #ifdef _WIN32
437 #pragma pack(pop)               // reset the byte alignment
438 #endif
439
440 #endif                          // CPC_HEADER