]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/include/main.h
b4a0b32a7eb29cf395e357d822fc1f9bb17a17fe
[lincan.git] / lincan / include / main.h
1 /* main.h
2  * Header file for the Linux CAN-bus driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5  * email:pisa@cmp.felk.cvut.cz
6  * This software is released under the GPL-License.
7  * Version lincan-0.2  9 Jul 2003
8  */
9
10 #include "./can.h"
11 #include "./constants.h"
12 #include "./can_sysdep.h"
13 #include "./can_queue.h"
14
15 #ifdef CAN_DEBUG
16         #define DEBUGMSG(fmt,args...) can_printk(KERN_ERR "can.o (debug): " fmt,\
17         ##args)
18 #else
19         #define DEBUGMSG(fmt,args...)
20 #endif
21
22 #define CANMSG(fmt,args...) can_printk(KERN_ERR "can.o: " fmt,##args)
23
24
25 extern can_spinlock_t canuser_manipulation_lock;
26
27 /**
28  * struct canhardware_t - structure representing pointers to all CAN boards
29  * @nr_boards: number of present boards
30  * @rtr_queue: RTR - remote transmission request queue (expect some changes there)
31  * @rtr_lock: locking for RTR queue
32  * @candevice: array of pointers to CAN devices/boards
33  */
34 struct canhardware_t {
35         int nr_boards;
36         struct rtr_id *rtr_queue;
37         can_spinlock_t rtr_lock;
38         struct candevice_t *candevice[MAX_HW_CARDS];
39 };
40
41 /**
42  * struct candevice_t - CAN device/board structure
43  * @hwname: text string with board type
44  * @candev_idx: board index in canhardware_t.candevice[]
45  * @io_addr: IO/physical MEM address
46  * @res_addr: optional reset register port
47  * @dev_base_addr: CPU translated IO/virtual MEM address
48  * @flags: board flags: %PROGRAMMABLE_IRQ .. interrupt number
49  *      can be programmed into board
50  * @nr_all_chips: number of chips present on the board
51  * @nr_82527_chips: number of Intel 8257 chips 
52  * @nr_sja1000_chips: number of Philips SJA100 chips
53  * @chip: array of pointers to the chip structures
54  * @hwspecops: pointer to board specific operations
55  * @hosthardware_p: pointer to the root hardware structure
56  *
57  * The structure represent configuration and state of associated board.
58  * The driver infrastructure prepares this structure and calls
59  * board type specific board_register() function. The board support provided
60  * register function fills right function pointers in @hwspecops structure.
61  * Then driver setup calls functions init_hw_data(), init_chip_data(),
62  * init_chip_data(), init_obj_data() and program_irq(). Function init_hw_data()
63  * and init_chip_data() have to specify number and types of connected chips
64  * or objects respectively.
65  * The use of @nr_all_chips is preferred over use of fields @nr_82527_chips
66  * and @nr_sja1000_chips in the board non-specific functions.
67  * The @io_addr and @dev_base_addr is filled from module parameters
68  * to the same value. The request_io function can fix-up @dev_base_addr
69  * field if virtual address is different than bus address.
70  */
71 struct candevice_t {
72         char *hwname;                   /* text board type */
73         int candev_idx;                 /* board index in canhardware_t.candevice[] */
74         unsigned long io_addr;          /* IO/physical MEM address */
75         unsigned long res_addr;         /* optional seset register port */
76         unsigned long dev_base_addr;    /* CPU translated IO/virtual MEM address */
77         unsigned int flags;
78         int nr_all_chips;
79         int nr_82527_chips;
80         int nr_sja1000_chips;
81         struct chip_t *chip[MAX_HW_CHIPS];
82
83         struct hwspecops_t *hwspecops;
84
85         struct canhardware_t *hosthardware_p;
86         
87         union {
88             #ifdef CAN_ENABLE_PCI_SUPPORT
89                 struct pci_dev *pcidev;
90             #endif /*CAN_ENABLE_PCI_SUPPORT*/
91         } sysdevptr;
92
93 };
94
95 /**
96  * struct chip_t - CAN chip state and type information
97  * @chip_type: text string describing chip type
98  * @chip_idx: index of the chip in candevice_t.chip[] array
99  * @chip_irq: chip interrupt number if any
100  * @chip_base_addr: chip base address in the CPU IO or virtual memory space
101  * @flags: chip flags: %CHIP_CONFIGURED .. chip is configured,
102  *      %CHIP_SEGMENTED .. access to the chip is segmented (mainly for i82527 chips)
103  * @clock: chip base clock frequency in Hz
104  * @baudrate: selected chip baudrate in Hz
105  * @write_register: write chip register function copy -
106  * @read_register: read chip register function copy
107  * @sja_cdr_reg: SJA specific register -
108  *      holds hardware specific options for the Clock Divider
109  *      register. Options defined in the sja1000.h file:
110  *      %CDR_CLKOUT_MASK, %CDR_CLK_OFF, %CDR_RXINPEN, %CDR_CBP, %CDR_PELICAN
111  * @sja_ocr_reg: SJA specific register -
112  *      hold hardware specific options for the Output Control
113  *      register. Options defined in the sja1000.h file:
114  *      %OCR_MODE_BIPHASE, %OCR_MODE_TEST, %OCR_MODE_NORMAL, %OCR_MODE_CLOCK,
115  *      %OCR_TX0_LH, %OCR_TX1_ZZ.
116  * @int_cpu_reg: Intel specific register -
117  *      holds hardware specific options for the CPU Interface
118  *      register. Options defined in the i82527.h file:
119  *      %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
120  * @int_clk_reg: Intel specific register -
121  *      holds hardware specific options for the Clock Out
122  *      register. Options defined in the i82527.h file:
123  *      %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
124  * @int_bus_reg: Intel specific register -
125  *      holds hardware specific options for the Bus Configuration
126  *      register. Options defined in the i82527.h file:
127  *      %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
128  * @msgobj: array of pointers to individual communication objects
129  * @chipspecops: pointer to the set of chip specific object filled by init_chip_data() function
130  * @hostdevice: pointer to chip hosting board
131  * @max_objects: maximal number of communication objects connected to this chip
132  *
133  * The fields @write_register and @read_register are copied from
134  * corresponding fields from @hwspecops structure
135  * (chip->hostdevice->hwspecops->write_register and 
136  * chip->hostdevice->hwspecops->read_register)
137  * to speedup can_write_reg() and can_read_reg() functions.
138  */
139 struct chip_t {
140         char *chip_type;
141         int chip_idx;   /* chip index in candevice_t.chip[] */
142         int chip_irq;
143         unsigned long chip_base_addr;
144         unsigned int flags;
145         long clock; /* Chip clock in Hz */
146         long baudrate;
147
148         void (*write_register)(unsigned char data,unsigned long address);
149         unsigned (*read_register)(unsigned long address);
150
151         unsigned short sja_cdr_reg; /* sja1000 only! */
152         unsigned short sja_ocr_reg; /* sja1000 only! */
153         unsigned short int_cpu_reg; /* intel 82527 only! */
154         unsigned short int_clk_reg; /* intel 82527 only! */
155         unsigned short int_bus_reg; /* intel 82527 only! */
156
157         struct msgobj_t *msgobj[MAX_MSGOBJS];
158
159         struct chipspecops_t *chipspecops;
160
161         struct candevice_t *hostdevice;
162         
163         int max_objects;        /* 1 for sja1000, 15 for i82527 */
164
165         can_spinlock_t chip_lock;
166
167     #ifdef CAN_WITH_RTL
168         pthread_t worker_thread;
169         unsigned long pend_flags;
170     #endif /*CAN_WITH_RTL*/
171 };
172
173 /**
174  * struct msgobj_t - structure holding communication object state
175  * @obj_base_addr: 
176  * @minor: associated device minor number
177  * @object: object number in chip_t structure +1
178  * @flags: message object flags
179  * @ret: field holding status of the last Tx operation
180  * @qends: pointer to message object corresponding ends structure
181  * @tx_qedge: edge corresponding to transmitted message
182  * @tx_slot: slot holding transmitted message, slot is taken from
183  *      canque_test_outslot() call and is freed by canque_free_outslot()
184  *      or rescheduled canque_again_outslot()
185  * @tx_retry_cnt: transmission attempt counter
186  * @tx_timeout: can be used by chip driver to check for the transmission timeout
187  * @rx_msg: temporary storage to hold received messages before
188  *      calling to canque_filter_msg2edges()
189  * @hostchip: pointer to the &chip_t structure this object belongs to
190  * @obj_used: counter of users (associated file structures for Linux
191  *      userspace clients) of this object
192  * @obj_users: list of user structures of type &canuser_t.
193  */
194 struct msgobj_t {
195         unsigned long obj_base_addr;
196         unsigned int minor;     /* associated device minor number  */
197         unsigned int object;    /* object number in chip_t +1 for debug printk */
198         unsigned long obj_flags; 
199         int ret;
200
201         struct canque_ends_t *qends;
202
203         struct canque_edge_t *tx_qedge;
204         struct canque_slot_t *tx_slot;
205         int tx_retry_cnt;
206         struct timer_list tx_timeout;
207         
208         struct canmsg_t rx_msg;
209
210         struct chip_t *hostchip;
211
212         atomic_t obj_used;
213         struct list_head obj_users;
214 };
215
216 #define CAN_USER_MAGIC 0x05402033
217
218 /**
219  * struct canuser_t - structure holding CAN user/client state
220  * @flags: used to distinguish Linux/RT-Linux type
221  * @peers: for connection into list of object users
222  * @qends: pointer to the ends structure corresponding for this user
223  * @msgobj: communication object the user is connected to
224  * @rx_edge0: default receive queue for filter IOCTL
225  * @userinfo: stores user context specific information.
226  *      The field @fileinfo.file holds pointer to open device file state structure
227  *      for the Linux user-space client applications
228  * @magic: magic number to check consistency when pointer is retrieved
229  *      from file private field
230  */
231 struct canuser_t {
232         unsigned long flags;
233         struct list_head peers;
234         struct canque_ends_t *qends;
235         struct msgobj_t *msgobj;
236         struct canque_edge_t *rx_edge0; /* simplifies IOCTL */
237         union {
238                 struct {
239                         struct file *file;  /* back ptr to file */
240                 } fileinfo;
241             #ifdef CAN_WITH_RTL
242                 struct {
243                         struct rtl_file *file;
244                 } rtlinfo;
245             #endif /*CAN_WITH_RTL*/
246         } userinfo;
247         int magic;
248 };
249
250 /**
251  * struct hwspecops_t - hardware/board specific operations
252  * @request_io: reserve io or memory range for can board
253  * @release_io: free reserved io memory range
254  * @reset: hardware reset routine
255  * @init_hw_data: called to initialize &candevice_t structure, mainly 
256  *      @res_add, @nr_all_chips, @nr_82527_chips, @nr_sja1000_chips
257  *      and @flags fields
258  * @init_chip_data: called initialize each &chip_t structure, mainly
259  *      @chip_type, @chip_base_addr, @clock and chip specific registers.
260  *      It is responsible to setup &chip_t->@chipspecops functions
261  *      for non-standard chip types (type other than "i82527", "sja1000" or "sja1000p")
262  * @init_obj_data: called initialize each &msgobj_t structure,
263  *      mainly @obj_base_addr field.
264  * @program_irq: program interrupt generation hardware of the board
265  *      if flag %PROGRAMMABLE_IRQ is present for specified device/board 
266  * @write_register: low level write register routine
267  * @read_register: low level read register routine
268  */
269 struct hwspecops_t {
270         int (*request_io)(struct candevice_t *candev);
271         int (*release_io)(struct candevice_t *candev);
272         int (*reset)(struct candevice_t *candev);
273         int (*init_hw_data)(struct candevice_t *candev);
274         int (*init_chip_data)(struct candevice_t *candev, int chipnr);
275         int (*init_obj_data)(struct chip_t *chip, int objnr);
276         int (*program_irq)(struct candevice_t *candev);
277         void (*write_register)(unsigned char data,unsigned long address);
278         unsigned (*read_register)(unsigned long address);
279 };
280
281 /**
282  * struct chipspecops_t - can controller chip specific operations
283  * @chip_config: CAN chip configuration
284  * @baud_rate: set communication parameters
285  * @standard_mask: setup of mask for message filtering
286  * @extended_mask: setup of extended mask for message filtering
287  * @message15_mask: set mask of i82527 message object 15
288  * @clear_objects: clears state of all message object residing in chip
289  * @config_irqs: tunes chip hardware interrupt delivery
290  * @pre_read_config: prepares message object for message reception
291  * @pre_write_config: prepares message object for message transmission
292  * @send_msg: initiate message transmission
293  * @remote_request: configures message object and asks for RTR message
294  * @check_tx_stat: checks state of transmission engine
295  * @wakeup_tx: wakeup TX processing
296  * @enable_configuration: enable chip configuration mode
297  * @disable_configuration: disable chip configuration mode
298  * @set_btregs: configures bitrate registers
299  * @start_chip: starts chip message processing
300  * @stop_chip: stops chip message processing
301  * @irq_handler: interrupt service routine
302  */
303 struct chipspecops_t {
304         int (*chip_config)(struct chip_t *chip);
305         int (*baud_rate)(struct chip_t *chip, int rate, int clock, int sjw,
306                                                 int sampl_pt, int flags);
307         int (*standard_mask)(struct chip_t *chip, unsigned short code, 
308                                                         unsigned short mask);
309         int (*extended_mask)(struct chip_t *chip, unsigned long code, 
310                                                         unsigned long mask);
311         int (*message15_mask)(struct chip_t *chip, unsigned long code, 
312                                                         unsigned long mask);
313         int (*clear_objects)(struct chip_t *chip);
314         int (*config_irqs)(struct chip_t *chip, short irqs);
315         int (*pre_read_config)(struct chip_t *chip, struct msgobj_t *obj);
316         int (*pre_write_config)(struct chip_t *chip, struct msgobj_t *obj,
317                                                         struct canmsg_t *msg);
318         int (*send_msg)(struct chip_t *chip, struct msgobj_t *obj,
319                                                         struct canmsg_t *msg);
320         int (*remote_request)(struct chip_t *chip, struct msgobj_t *obj);
321         int (*check_tx_stat)(struct chip_t *chip);
322         int (*wakeup_tx)(struct chip_t *chip, struct msgobj_t *obj);
323         int (*enable_configuration)(struct chip_t *chip);
324         int (*disable_configuration)(struct chip_t *chip);
325         int (*set_btregs)(struct chip_t *chip, unsigned short btr0, 
326                                                         unsigned short btr1);
327         int (*start_chip)(struct chip_t *chip);
328         int (*stop_chip)(struct chip_t *chip);
329         can_irqreturn_t (*irq_handler)(int irq, void *dev_id, struct pt_regs *regs);
330 };
331
332 struct mem_addr {
333         void *address;
334         struct mem_addr *next;
335         size_t size;
336 };
337
338 /* Structure for the RTR queue */
339 struct rtr_id {
340         unsigned long id;
341         struct canmsg_t *rtr_message;
342         wait_queue_head_t rtr_wq;
343         struct rtr_id *next;
344 };
345
346 extern int major;
347 extern int minor[MAX_TOT_CHIPS];
348 extern int extended;
349 extern int baudrate[MAX_TOT_CHIPS];
350 extern char *hw[MAX_HW_CARDS];
351 extern int irq[MAX_IRQ];
352 extern unsigned long io[MAX_HW_CARDS];
353 extern int processlocal;
354
355 extern struct canhardware_t *hardware_p;
356 extern struct chip_t *chips_p[MAX_TOT_CHIPS];
357 extern struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
358
359 extern struct mem_addr *mem_head;
360
361 /* Inline function to write to the hardware registers. The argument address is 
362  * relative to the memory map of the chip and not the absolute memory address.
363  */
364 extern inline void can_write_reg(const struct chip_t *chip, unsigned char data, unsigned address)
365 {
366         unsigned long address_to_write;
367         address_to_write = chip->chip_base_addr+address;
368         chip->write_register(data, address_to_write);
369 }
370
371 extern inline unsigned can_read_reg(const struct chip_t *chip, unsigned address)
372 {
373         unsigned long address_to_read;
374         address_to_read = chip->chip_base_addr+address;
375         return chip->read_register(address_to_read);
376 }
377
378 extern inline void canobj_write_reg(const struct chip_t *chip, const struct msgobj_t *obj,
379                                 unsigned char data, unsigned address)
380 {
381         unsigned long address_to_write;
382         address_to_write = obj->obj_base_addr+address;
383         chip->write_register(data, address_to_write);
384 }
385
386 extern inline unsigned canobj_read_reg(const struct chip_t *chip, const struct msgobj_t *obj,
387                                 unsigned address)
388 {
389         unsigned long address_to_read;
390         address_to_read = obj->obj_base_addr+address;
391         return chip->read_register(address_to_read);
392 }
393
394 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base);
395 int can_request_io_region(unsigned long start, unsigned long n, const char *name);
396 void can_release_io_region(unsigned long start, unsigned long n);
397 int can_request_mem_region(unsigned long start, unsigned long n, const char *name);
398 void can_release_mem_region(unsigned long start, unsigned long n);
399
400 struct boardtype_t {
401         const char *boardtype;
402         int (*board_register)(struct hwspecops_t *hwspecops);
403         int irqnum;
404 };
405
406 const struct boardtype_t* boardtype_find(const char *str);
407
408 #ifdef CAN_WITH_RTL
409 extern int can_rtl_priority;
410 #endif /*CAN_WITH_RTL*/