]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/include/main.h
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[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.3  17 Jun 2004
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 "lincan (debug): " fmt,\
17         ##args)
18 #else
19         #define DEBUGMSG(fmt,args...)
20 #endif
21
22 #define CANMSG(fmt,args...) can_printk(KERN_ERR "lincan: " 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  * @sysdevptr: union reserved for pointer to bus specific
57  *      device structure (case @pcidev is used for PCI devices)
58  *
59  * The structure represent configuration and state of associated board.
60  * The driver infrastructure prepares this structure and calls
61  * board type specific board_register() function. The board support provided
62  * register function fills right function pointers in @hwspecops structure.
63  * Then driver setup calls functions init_hw_data(), init_chip_data(),
64  * init_chip_data(), init_obj_data() and program_irq(). Function init_hw_data()
65  * and init_chip_data() have to specify number and types of connected chips
66  * or objects respectively.
67  * The use of @nr_all_chips is preferred over use of fields @nr_82527_chips
68  * and @nr_sja1000_chips in the board non-specific functions.
69  * The @io_addr and @dev_base_addr is filled from module parameters
70  * to the same value. The request_io function can fix-up @dev_base_addr
71  * field if virtual address is different than bus address.
72  */
73 struct candevice_t {
74         char *hwname;                   /* text board type */
75         int candev_idx;                 /* board index in canhardware_t.candevice[] */
76         unsigned long io_addr;          /* IO/physical MEM address */
77         unsigned long res_addr;         /* optional reset register port */
78         can_ioptr_t dev_base_addr;      /* CPU translated IO/virtual MEM address */
79         can_ioptr_t aux_base_addr;      /* CPU translated IO/virtual MEM address */
80         unsigned int flags;
81         int nr_all_chips;
82         int nr_82527_chips;
83         int nr_sja1000_chips;
84         can_spinlock_t device_lock;
85         struct canchip_t *chip[MAX_HW_CHIPS];
86
87         struct hwspecops_t *hwspecops;
88
89         struct canhardware_t *hosthardware_p;
90         
91         union {
92                 void *anydev;
93             #ifdef CAN_ENABLE_PCI_SUPPORT
94                 struct pci_dev *pcidev;
95             #endif /*CAN_ENABLE_PCI_SUPPORT*/
96         } sysdevptr;
97
98 };
99
100 /**
101  * struct canchip_t - CAN chip state and type information
102  * @chip_type: text string describing chip type
103  * @chip_idx: index of the chip in candevice_t.chip[] array
104  * @chip_irq: chip interrupt number if any
105  * @chip_base_addr: chip base address in the CPU IO or virtual memory space
106  * @flags: chip flags: %CHIP_CONFIGURED .. chip is configured,
107  *      %CHIP_SEGMENTED .. access to the chip is segmented (mainly for i82527 chips)
108  * @clock: chip base clock frequency in Hz
109  * @baudrate: selected chip baudrate in Hz
110  * @write_register: write chip register function copy
111  * @read_register: read chip register function copy
112  * @chip_data: pointer for optional chip specific data extension
113  * @sja_cdr_reg: SJA specific register -
114  *      holds hardware specific options for the Clock Divider
115  *      register. Options defined in the sja1000.h file:
116  *      %CDR_CLKOUT_MASK, %CDR_CLK_OFF, %CDR_RXINPEN, %CDR_CBP, %CDR_PELICAN
117  * @sja_ocr_reg: SJA specific register -
118  *      hold hardware specific options for the Output Control
119  *      register. Options defined in the sja1000.h file:
120  *      %OCR_MODE_BIPHASE, %OCR_MODE_TEST, %OCR_MODE_NORMAL, %OCR_MODE_CLOCK,
121  *      %OCR_TX0_LH, %OCR_TX1_ZZ.
122  * @int_cpu_reg: Intel specific register -
123  *      holds hardware specific options for the CPU Interface
124  *      register. Options defined in the i82527.h file:
125  *      %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
126  * @int_clk_reg: Intel specific register -
127  *      holds hardware specific options for the Clock Out
128  *      register. Options defined in the i82527.h file:
129  *      %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
130  * @int_bus_reg: Intel specific register -
131  *      holds hardware specific options for the Bus Configuration
132  *      register. Options defined in the i82527.h file:
133  *      %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
134  * @msgobj: array of pointers to individual communication objects
135  * @chipspecops: pointer to the set of chip specific object filled by init_chip_data() function
136  * @hostdevice: pointer to chip hosting board
137  * @max_objects: maximal number of communication objects connected to this chip
138  * @chip_lock: reserved for synchronization of the chip supporting routines
139  *      (not used in the current driver version)
140  * @worker_thread: chip worker thread ID (RT-Linux specific field)
141  * @pend_flags: holds information about pending interrupt and tx_wake() operations
142  *      (RT-Linux specific field). Masks values:
143  *      %MSGOBJ_TX_REQUEST .. some of the message objects requires tx_wake() call, 
144  *      %MSGOBJ_IRQ_REQUEST .. chip interrupt processing required
145  *      %MSGOBJ_WORKER_WAKE .. marks, that worker thread should be waked
146  *              for some of above reasons
147  *
148  * The fields @write_register and @read_register are copied from
149  * corresponding fields from @hwspecops structure
150  * (chip->hostdevice->hwspecops->write_register and 
151  * chip->hostdevice->hwspecops->read_register)
152  * to speedup can_write_reg() and can_read_reg() functions.
153  */
154 struct canchip_t {
155         char *chip_type;
156         int chip_idx;   /* chip index in candevice_t.chip[] */
157         int chip_irq;
158         can_ioptr_t chip_base_addr;
159         unsigned int flags;
160         long clock; /* Chip clock in Hz */
161         long baudrate;
162
163         void (*write_register)(unsigned data, can_ioptr_t address);
164         unsigned (*read_register)(can_ioptr_t address);
165         
166         void *chip_data;
167         
168         unsigned short sja_cdr_reg; /* sja1000 only! */
169         unsigned short sja_ocr_reg; /* sja1000 only! */
170         unsigned short int_cpu_reg; /* intel 82527 only! */
171         unsigned short int_clk_reg; /* intel 82527 only! */
172         unsigned short int_bus_reg; /* intel 82527 only! */
173
174         struct msgobj_t *msgobj[MAX_MSGOBJS];
175
176         struct chipspecops_t *chipspecops;
177
178         struct candevice_t *hostdevice;
179         
180         int max_objects;        /* 1 for sja1000, 15 for i82527 */
181
182         can_spinlock_t chip_lock;
183
184     #ifdef CAN_WITH_RTL
185         pthread_t worker_thread;
186         unsigned long pend_flags;
187     #endif /*CAN_WITH_RTL*/
188 };
189
190 /**
191  * struct msgobj_t - structure holding communication object state
192  * @obj_base_addr: 
193  * @minor: associated device minor number
194  * @object: object number in canchip_t structure +1
195  * @flags: message object flags
196  * @ret: field holding status of the last Tx operation
197  * @qends: pointer to message object corresponding ends structure
198  * @tx_qedge: edge corresponding to transmitted message
199  * @tx_slot: slot holding transmitted message, slot is taken from
200  *      canque_test_outslot() call and is freed by canque_free_outslot()
201  *      or rescheduled canque_again_outslot()
202  * @tx_retry_cnt: transmission attempt counter
203  * @tx_timeout: can be used by chip driver to check for the transmission timeout
204  * @rx_msg: temporary storage to hold received messages before
205  *      calling to canque_filter_msg2edges()
206  * @hostchip: pointer to the &canchip_t structure this object belongs to
207  * @obj_used: counter of users (associated file structures for Linux
208  *      userspace clients) of this object
209  * @obj_users: list of user structures of type &canuser_t.
210  * @obj_flags: message object specific flags. Masks values:
211  *      %MSGOBJ_TX_REQUEST .. the message object requests TX activation
212  *      %MSGOBJ_TX_LOCK .. some IRQ routine or callback on some CPU 
213  *              is running inside TX activation processing code
214  * @rx_preconfig_id: place to store RX message identifier for some chip types
215  *               that reuse same object for TX
216  */
217 struct msgobj_t {
218         can_ioptr_t obj_base_addr;
219         unsigned int minor;     /* associated device minor number  */
220         unsigned int object;    /* object number in canchip_t +1 for debug printk */
221         unsigned long obj_flags; 
222         int ret;
223
224         struct canque_ends_t *qends;
225
226         struct canque_edge_t *tx_qedge;
227         struct canque_slot_t *tx_slot;
228         int tx_retry_cnt;
229         struct timer_list tx_timeout;
230         
231         struct canmsg_t rx_msg;
232
233         struct canchip_t *hostchip;
234  
235         unsigned long rx_preconfig_id;
236
237         atomic_t obj_used;
238         struct list_head obj_users;
239 };
240
241 #define CAN_USER_MAGIC 0x05402033
242
243 /**
244  * struct canuser_t - structure holding CAN user/client state
245  * @flags: used to distinguish Linux/RT-Linux type
246  * @peers: for connection into list of object users
247  * @qends: pointer to the ends structure corresponding for this user
248  * @msgobj: communication object the user is connected to
249  * @rx_edge0: default receive queue for filter IOCTL
250  * @userinfo: stores user context specific information.
251  *      The field @fileinfo.file holds pointer to open device file state structure
252  *      for the Linux user-space client applications
253  * @magic: magic number to check consistency when pointer is retrieved
254  *      from file private field
255  */
256 struct canuser_t {
257         unsigned long flags;
258         struct list_head peers;
259         struct canque_ends_t *qends;
260         struct msgobj_t *msgobj;
261         struct canque_edge_t *rx_edge0; /* simplifies IOCTL */
262         union {
263                 struct {
264                         struct file *file;  /* back ptr to file */
265                 } fileinfo;
266             #ifdef CAN_WITH_RTL
267                 struct {
268                         struct rtl_file *file;
269                 } rtlinfo;
270             #endif /*CAN_WITH_RTL*/
271         } userinfo;
272         int magic;
273 };
274
275 /**
276  * struct hwspecops_t - hardware/board specific operations
277  * @request_io: reserve io or memory range for can board
278  * @release_io: free reserved io memory range
279  * @reset: hardware reset routine
280  * @init_hw_data: called to initialize &candevice_t structure, mainly 
281  *      @res_add, @nr_all_chips, @nr_82527_chips, @nr_sja1000_chips
282  *      and @flags fields
283  * @init_chip_data: called initialize each &canchip_t structure, mainly
284  *      @chip_type, @chip_base_addr, @clock and chip specific registers.
285  *      It is responsible to setup &canchip_t->@chipspecops functions
286  *      for non-standard chip types (type other than "i82527", "sja1000" or "sja1000p")
287  * @init_obj_data: called initialize each &msgobj_t structure,
288  *      mainly @obj_base_addr field.
289  * @program_irq: program interrupt generation hardware of the board
290  *      if flag %PROGRAMMABLE_IRQ is present for specified device/board 
291  * @write_register: low level write register routine
292  * @read_register: low level read register routine
293  */
294 struct hwspecops_t {
295         int (*request_io)(struct candevice_t *candev);
296         int (*release_io)(struct candevice_t *candev);
297         int (*reset)(struct candevice_t *candev);
298         int (*init_hw_data)(struct candevice_t *candev);
299         int (*init_chip_data)(struct candevice_t *candev, int chipnr);
300         int (*init_obj_data)(struct canchip_t *chip, int objnr);
301         int (*program_irq)(struct candevice_t *candev);
302         void (*write_register)(unsigned data, can_ioptr_t address);
303         unsigned (*read_register)(can_ioptr_t address);
304 };
305
306 /**
307  * struct chipspecops_t - can controller chip specific operations
308  * @chip_config: CAN chip configuration
309  * @baud_rate: set communication parameters
310  * @standard_mask: setup of mask for message filtering
311  * @extended_mask: setup of extended mask for message filtering
312  * @message15_mask: set mask of i82527 message object 15
313  * @clear_objects: clears state of all message object residing in chip
314  * @config_irqs: tunes chip hardware interrupt delivery
315  * @pre_read_config: prepares message object for message reception
316  * @pre_write_config: prepares message object for message transmission
317  * @send_msg: initiate message transmission
318  * @remote_request: configures message object and asks for RTR message
319  * @check_tx_stat: checks state of transmission engine
320  * @wakeup_tx: wakeup TX processing
321  * @filtch_rq: optional routine for propagation of outgoing edges filters to HW
322  * @enable_configuration: enable chip configuration mode
323  * @disable_configuration: disable chip configuration mode
324  * @set_btregs: configures bitrate registers
325  * @attach_to_chip: attaches to the chip, setups registers and possibly state informations
326  * @release_chip: called before chip structure removal if %CHIP_ATTACHED is set
327  * @start_chip: starts chip message processing
328  * @stop_chip: stops chip message processing
329  * @irq_handler: interrupt service routine
330  * @irq_accept: optional fast irq accept routine responsible for blocking further interrupts
331  */
332 struct chipspecops_t {
333         int (*chip_config)(struct canchip_t *chip);
334         int (*baud_rate)(struct canchip_t *chip, int rate, int clock, int sjw,
335                                                 int sampl_pt, int flags);
336         int (*standard_mask)(struct canchip_t *chip, unsigned short code, 
337                                                         unsigned short mask);
338         int (*extended_mask)(struct canchip_t *chip, unsigned long code, 
339                                                         unsigned long mask);
340         int (*message15_mask)(struct canchip_t *chip, unsigned long code, 
341                                                         unsigned long mask);
342         int (*clear_objects)(struct canchip_t *chip);
343         int (*config_irqs)(struct canchip_t *chip, short irqs);
344         int (*pre_read_config)(struct canchip_t *chip, struct msgobj_t *obj);
345         int (*pre_write_config)(struct canchip_t *chip, struct msgobj_t *obj,
346                                                         struct canmsg_t *msg);
347         int (*send_msg)(struct canchip_t *chip, struct msgobj_t *obj,
348                                                         struct canmsg_t *msg);
349         int (*remote_request)(struct canchip_t *chip, struct msgobj_t *obj);
350         int (*check_tx_stat)(struct canchip_t *chip);
351         int (*wakeup_tx)(struct canchip_t *chip, struct msgobj_t *obj);
352         int (*filtch_rq)(struct canchip_t *chip, struct msgobj_t *obj);
353         int (*enable_configuration)(struct canchip_t *chip);
354         int (*disable_configuration)(struct canchip_t *chip);
355         int (*set_btregs)(struct canchip_t *chip, unsigned short btr0, 
356                                                         unsigned short btr1);
357         int (*attach_to_chip)(struct canchip_t *chip);
358         int (*release_chip)(struct canchip_t *chip);
359         int (*start_chip)(struct canchip_t *chip);
360         int (*stop_chip)(struct canchip_t *chip);
361         int (*irq_handler)(int irq, struct canchip_t *chip);
362         int (*irq_accept)(int irq, struct canchip_t *chip);
363 };
364
365 struct mem_addr {
366         void *address;
367         struct mem_addr *next;
368         size_t size;
369 };
370
371 /* Structure for the RTR queue */
372 struct rtr_id {
373         unsigned long id;
374         struct canmsg_t *rtr_message;
375         wait_queue_head_t rtr_wq;
376         struct rtr_id *next;
377 };
378
379 extern int major;
380 extern int minor[MAX_TOT_CHIPS];
381 extern int extended;
382 extern int baudrate[MAX_TOT_CHIPS];
383 extern int irq[MAX_IRQ];
384 extern char *hw[MAX_HW_CARDS];
385 extern unsigned long io[MAX_HW_CARDS];
386 extern long clockfreq[MAX_HW_CARDS];
387 extern int processlocal;
388
389 extern struct canhardware_t *hardware_p;
390 extern struct canchip_t *chips_p[MAX_TOT_CHIPS];
391 extern struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
392
393 extern struct mem_addr *mem_head;
394
395
396 #if defined(CONFIG_OC_LINCAN_PORTIO_ONLY)
397 extern inline void can_write_reg(const struct canchip_t *chip, unsigned char data, unsigned reg_offs)
398 {
399         can_outb(data, chip->chip_base_addr+reg_offs);
400 }
401 extern inline unsigned can_read_reg(const struct canchip_t *chip, unsigned reg_offs)
402 {
403         return can_inb(chip->chip_base_addr+reg_offs);
404 }
405 extern inline void canobj_write_reg(const struct canchip_t *chip, const struct msgobj_t *obj,
406                                 unsigned char data, unsigned reg_offs)
407 {
408         can_outb(data, obj->obj_base_addr+reg_offs);
409 }
410 extern inline unsigned canobj_read_reg(const struct canchip_t *chip, const struct msgobj_t *obj,
411                                 unsigned reg_offs)
412 {
413         return can_inb(obj->obj_base_addr+reg_offs);
414 }
415
416 #elif defined(CONFIG_OC_LINCAN_MEMIO_ONLY)
417 extern inline void can_write_reg(const struct canchip_t *chip, unsigned char data, unsigned reg_offs)
418 {
419         can_writeb(data, chip->chip_base_addr+reg_offs);
420 }
421 extern inline unsigned can_read_reg(const struct canchip_t *chip, unsigned reg_offs)
422 {
423         return can_readb(chip->chip_base_addr+reg_offs);
424 }
425 extern inline void canobj_write_reg(const struct canchip_t *chip, const struct msgobj_t *obj,
426                                 unsigned char data, unsigned reg_offs)
427 {
428         can_writeb(data, obj->obj_base_addr+reg_offs);
429 }
430 extern inline unsigned canobj_read_reg(const struct canchip_t *chip, const struct msgobj_t *obj,
431                                 unsigned reg_offs)
432 {
433         return can_readb(obj->obj_base_addr+reg_offs);
434 }
435
436 #else /*CONFIG_OC_LINCAN_DYNAMICIO*/
437 #ifndef CONFIG_OC_LINCAN_DYNAMICIO
438 #define CONFIG_OC_LINCAN_DYNAMICIO
439 #endif
440
441 /* Inline function to write to the hardware registers. The argument reg_offs is 
442  * relative to the memory map of the chip and not the absolute memory reg_offs.
443  */
444 extern inline void can_write_reg(const struct canchip_t *chip, unsigned char data, unsigned reg_offs)
445 {
446         can_ioptr_t address_to_write;
447         address_to_write = chip->chip_base_addr+reg_offs;
448         chip->write_register(data, address_to_write);
449 }
450
451 extern inline unsigned can_read_reg(const struct canchip_t *chip, unsigned reg_offs)
452 {
453         can_ioptr_t address_to_read;
454         address_to_read = chip->chip_base_addr+reg_offs;
455         return chip->read_register(address_to_read);
456 }
457
458 extern inline void canobj_write_reg(const struct canchip_t *chip, const struct msgobj_t *obj,
459                                 unsigned char data, unsigned reg_offs)
460 {
461         can_ioptr_t address_to_write;
462         address_to_write = obj->obj_base_addr+reg_offs;
463         chip->write_register(data, address_to_write);
464 }
465
466 extern inline unsigned canobj_read_reg(const struct canchip_t *chip, const struct msgobj_t *obj,
467                                 unsigned reg_offs)
468 {
469         can_ioptr_t address_to_read;
470         address_to_read = obj->obj_base_addr+reg_offs;
471         return chip->read_register(address_to_read);
472 }
473
474 #endif /*CONFIG_OC_LINCAN_DYNAMICIO*/
475
476 int can_base_addr_fixup(struct candevice_t *candev, can_ioptr_t new_base);
477 int can_request_io_region(unsigned long start, unsigned long n, const char *name);
478 void can_release_io_region(unsigned long start, unsigned long n);
479 int can_request_mem_region(unsigned long start, unsigned long n, const char *name);
480 void can_release_mem_region(unsigned long start, unsigned long n);
481
482 struct boardtype_t {
483         const char *boardtype;
484         int (*board_register)(struct hwspecops_t *hwspecops);
485         int irqnum;
486 };
487
488 const struct boardtype_t* boardtype_find(const char *str);
489
490 int can_check_dev_taken(void *anydev);
491
492 #if defined(can_gettimeofday) && defined(CAN_MSG_VERSION_2) && 1
493 static inline
494 void can_filltimestamp(canmsg_tstamp_t *ptimestamp)
495 {
496         can_gettimeofday(ptimestamp);
497 }
498 #else /* No timestamp support, set field to zero */
499 static inline
500 void can_filltimestamp(canmsg_tstamp_t *ptimestamp)
501 {
502     #ifdef CAN_MSG_VERSION_2
503         ptimestamp->tv_sec = 0;
504         ptimestamp->tv_usec = 0;
505     #else /* CAN_MSG_VERSION_2 */
506         *ptimestamp = 0;
507     #endif /* CAN_MSG_VERSION_2 */
508
509 }
510 #endif /* End of timestamp source selection */
511
512 #ifdef CAN_WITH_RTL
513 extern int can_rtl_priority;
514 #endif /*CAN_WITH_RTL*/