]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/virtual.c
d8818abb60b5a3589ad92d635c730f2c25283172
[lincan.git] / lincan / src / virtual.c
1 /* virtual.c
2  * Linux CAN-bus device driver.
3  * Written for new CAN driver version by Pavel Pisa - OCERA team member
4  * email:pisa@cmp.felk.cvut.cz
5  * This software is released under the GPL-License.
6  * Version lincan-0.2  9 Jul 2003
7  */ 
8
9 #include "../include/can.h"
10 #include "../include/can_sysdep.h"
11 #include "../include/main.h"
12
13
14 long virtual_bus_latency(struct msgobj_t *obj)
15 {
16         long latency;
17         latency=obj->hostchip->baudrate;
18         if(latency){
19                 latency=(long)HZ*1000/latency;
20         }
21         return latency;
22 }
23
24
25 /* * * Virtual Chip Functionality * * */
26
27 int virtual_enable_configuration(struct chip_t *chip)
28 {
29         return 0;
30 }
31
32 int virtual_disable_configuration(struct chip_t *chip)
33 {
34         return 0;
35 }
36
37 /**
38  * virtual_chip_config: - can chip configuration
39  * @chip: pointer to chip state structure
40  *
41  * Return Value: negative value reports error.
42  * File: src/virtual.c
43  */
44 int virtual_chip_config(struct chip_t *chip)
45 {
46         return 0;
47 }
48
49 /**
50  * virtual_extended_mask: - setup of extended mask for message filtering
51  * @chip: pointer to chip state structure
52  * @code: can message acceptance code
53  * @mask: can message acceptance mask
54  *
55  * Return Value: negative value reports error.
56  * File: src/virtual.c
57  */
58 int virtual_extended_mask(struct chip_t *chip, unsigned long code, unsigned  long mask)
59 {
60         return 0;
61 }
62
63 /**
64  * virtual_baud_rate: - set communication parameters.
65  * @chip: pointer to chip state structure
66  * @rate: baud rate in Hz
67  * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
68  * @sjw: synchronization jump width (0-3) prescaled clock cycles
69  * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
70  * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
71  *
72  * Return Value: negative value reports error.
73  * File: src/virtual.c
74  */
75 int virtual_baud_rate(struct chip_t *chip, int rate, int clock, int sjw,
76                                                         int sampl_pt, int flags)
77 {
78         return 0;
79 }
80
81 /**
82  * virtual_read: - reads and distributes one or more received messages
83  * @chip: pointer to chip state structure
84  * @obj: pinter to CAN message queue information
85  *
86  * File: src/virtual.c
87  */
88 void virtual_read(struct chip_t *chip, struct msgobj_t *obj) {
89
90 }
91
92 /**
93  * virtual_pre_read_config: - prepares message object for message reception
94  * @chip: pointer to chip state structure
95  * @obj: pointer to message object state structure
96  *
97  * Return Value: negative value reports error.
98  *      Positive value indicates immediate reception of message.
99  * File: src/virtual.c
100  */
101 int virtual_pre_read_config(struct chip_t *chip, struct msgobj_t *obj)
102 {
103         return 0;
104 }
105
106 #define MAX_TRANSMIT_WAIT_LOOPS 10
107 /**
108  * virtual_pre_write_config: - prepares message object for message transmission
109  * @chip: pointer to chip state structure
110  * @obj: pointer to message object state structure
111  * @msg: pointer to CAN message
112  *
113  * Return Value: negative value reports error.
114  * File: src/virtual.c
115  */
116 int virtual_pre_write_config(struct chip_t *chip, struct msgobj_t *obj, 
117                                                         struct canmsg_t *msg)
118 {
119         return 0;
120 }
121
122 /**
123  * virtual_send_msg: - initiate message transmission
124  * @chip: pointer to chip state structure
125  * @obj: pointer to message object state structure
126  * @msg: pointer to CAN message
127  *
128  * This function is called after virtual_pre_write_config() function,
129  * which prepares data in chip buffer.
130  * Return Value: negative value reports error.
131  * File: src/virtual.c
132  */
133 int virtual_send_msg(struct chip_t *chip, struct msgobj_t *obj, 
134                                                         struct canmsg_t *msg)
135 {
136         return 0;
137 }
138
139 /**
140  * virtual_check_tx_stat: - checks state of transmission engine
141  * @chip: pointer to chip state structure
142  *
143  * Return Value: negative value reports error.
144  *      Positive return value indicates transmission under way status.
145  *      Zero value indicates finishing of all issued transmission requests.
146  * File: src/virtual.c
147  */
148 int virtual_check_tx_stat(struct chip_t *chip)
149 {
150         return 0;
151 }
152
153 /**
154  * virtual_set_btregs: -  configures bitrate registers
155  * @chip: pointer to chip state structure
156  * @btr0: bitrate register 0
157  * @btr1: bitrate register 1
158  *
159  * Return Value: negative value reports error.
160  * File: src/virtual.c
161  */
162 int virtual_set_btregs(struct chip_t *chip, unsigned short btr0, 
163                                                         unsigned short btr1)
164 {
165         return 0;
166 }
167
168 /**
169  * virtual_stop_chip: -  starts chip message processing
170  * @chip: pointer to chip state structure
171  *
172  * Return Value: negative value reports error.
173  * File: src/virtual.c
174  */
175 int virtual_start_chip(struct chip_t *chip)
176 {
177         return 0;
178 }
179
180 /**
181  * virtual_stop_chip: -  stops chip message processing
182  * @chip: pointer to chip state structure
183  *
184  * Return Value: negative value reports error.
185  * File: src/virtual.c
186  */
187 int virtual_stop_chip(struct chip_t *chip)
188 {
189         return 0;
190 }
191
192
193 /**
194  * virtual_remote_request: - configures message object and asks for RTR message
195  * @chip: pointer to chip state structure
196  * @obj: pointer to message object structure
197  *
198  * Return Value: negative value reports error.
199  * File: src/virtual.c
200  */
201 int virtual_remote_request(struct chip_t *chip, struct msgobj_t *obj)
202 {
203         CANMSG("virtual_remote_request not implemented\n");
204         return -ENOSYS;
205 }
206
207 /**
208  * virtual_standard_mask: - setup of mask for message filtering
209  * @chip: pointer to chip state structure
210  * @code: can message acceptance code
211  * @mask: can message acceptance mask
212  *
213  * Return Value: negative value reports error.
214  * File: src/virtual.c
215  */
216 int virtual_standard_mask(struct chip_t *chip, unsigned short code,
217                 unsigned short mask)
218 {
219         CANMSG("virtual_standard_mask not implemented\n");
220         return -ENOSYS;
221 }
222
223 /**
224  * virtual_clear_objects: - clears state of all message object residing in chip
225  * @chip: pointer to chip state structure
226  *
227  * Return Value: negative value reports error.
228  * File: src/virtual.c
229  */
230 int virtual_clear_objects(struct chip_t *chip)
231 {
232         CANMSG("virtual_clear_objects not implemented\n");
233         return -ENOSYS;
234 }
235
236 /**
237  * virtual_config_irqs: - tunes chip hardware interrupt delivery
238  * @chip: pointer to chip state structure
239  * @irqs: requested chip IRQ configuration
240  *
241  * Return Value: negative value reports error.
242  * File: src/virtual.c
243  */
244 int virtual_config_irqs(struct chip_t *chip, short irqs)
245 {
246         CANMSG("virtual_config_irqs not implemented\n");
247         return -ENOSYS;
248 }
249
250 /**
251  * virtual_irq_write_handler: - part of ISR code responsible for transmit events
252  * @chip: pointer to chip state structure
253  * @obj: pointer to attached queue description
254  *
255  * The main purpose of this function is to read message from attached queues
256  * and transfer message contents into CAN controller chip.
257  * This subroutine is called by
258  * virtual_irq_write_handler() for transmit events.
259  * File: src/virtual.c
260  */
261 void virtual_irq_write_handler(struct chip_t *chip, struct msgobj_t *obj)
262 {
263
264 }
265
266 #define MAX_RETR 10
267
268 /**
269  * virtual_irq_handler: - interrupt service routine
270  * @irq: interrupt vector number, this value is system specific
271  * @dev_id: driver private pointer registered at time of request_irq() call.
272  *      The CAN driver uses this pointer to store relationship of interrupt
273  *      to chip state structure - @struct chip_t
274  * @regs: system dependent value pointing to registers stored in exception frame
275  * 
276  * Interrupt handler is activated when state of CAN controller chip changes,
277  * there is message to be read or there is more space for new messages or
278  * error occurs. The receive events results in reading of the message from
279  * CAN controller chip and distribution of message through attached
280  * message queues.
281  * File: src/virtual.c
282  */
283 can_irqreturn_t virtual_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
284 {
285         return CAN_IRQ_HANDLED;
286 }
287
288
289 void virtual_schedule_next(struct msgobj_t *obj)
290 {
291         int cmd;
292
293         can_preempt_disable();
294
295         can_msgobj_set_fl(obj,TX_REQUEST);
296         
297         while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
298
299                 can_msgobj_clear_fl(obj,TX_REQUEST);
300                 
301                 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
302                 if(cmd>=0) {
303                         mod_timer(&obj->tx_timeout,
304                                 jiffies+virtual_bus_latency(obj));
305                         DEBUGMSG("virtual: scheduled delivery\n");
306
307                 } else          
308                         can_msgobj_clear_fl(obj,TX_LOCK);
309                 
310                 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
311                 DEBUGMSG("TX looping in virtual_schedule_next\n");
312         }
313
314         can_preempt_enable();
315 }
316
317
318 void virtual_do_tx_timeout(unsigned long data)
319 {
320         struct msgobj_t *obj=(struct msgobj_t *)data;
321         
322         if(obj->tx_slot) {
323                 /* Deliver message to edges */
324                 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
325                 /* Free transmitted slot */
326                 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
327                 obj->tx_slot=NULL;
328                 DEBUGMSG("virtual: delayed delivery\n");
329         }
330         can_msgobj_clear_fl(obj,TX_LOCK);
331
332         virtual_schedule_next(obj);
333 }
334
335 /**
336  * virtual_wakeup_tx: - wakeups TX processing
337  * @chip: pointer to chip state structure
338  * @obj: pointer to message object structure
339  *
340  * Return Value: negative value reports error.
341  * File: src/virtual.c
342  */
343 int virtual_wakeup_tx(struct chip_t *chip, struct msgobj_t *obj)
344 {
345         /* can_msgobj_set_fl(obj,TX_REQUEST); */
346         
347         struct canque_edge_t *qedge;
348         struct canque_slot_t *slot;
349         int cmd;
350
351     #ifndef CAN_WITH_RTL
352         if(!virtual_bus_latency(obj)) {
353     #endif /*CAN_WITH_RTL*/
354                 /* Ensure delivery of all ready slots */
355                 while((cmd=canque_test_outslot(obj->qends, &qedge, &slot)) >= 0){
356                         if(cmd==0) {
357                                 canque_filter_msg2edges(obj->qends, &slot->msg);
358                                 DEBUGMSG("virtual: direct delivery\n");
359                         }
360                         canque_free_outslot(obj->qends, qedge, slot);
361                 }
362     #ifndef CAN_WITH_RTL
363         } else {
364                 virtual_schedule_next(obj);
365         }
366     #endif /*CAN_WITH_RTL*/
367
368         return 0;
369 }
370
371
372 /* * * Virtual Board Functionality * * */
373
374 /**
375  * virtual_request_io: - reserve io or memory range for can board
376  * @candev: pointer to candevice/board which asks for io. Field @io_addr
377  *      of @candev is used in most cases to define start of the range
378  *
379  * Return Value: The function returns zero on success or %-ENODEV on failure
380  * File: src/virtual.c
381  */
382 int virtual_request_io(struct candevice_t *candev)
383 {
384         return 0;
385 }
386
387 /**
388  * virtual_elease_io - free reserved io memory range
389  * @candev: pointer to candevice/board which releases io
390  *
391  * Return Value: The function always returns zero
392  * File: src/virtual.c
393  */
394 int virtual_release_io(struct candevice_t *candev)
395 {
396         return 0;
397 }
398
399 /**
400  * virtual_reset - hardware reset routine
401  * @candev: Pointer to candevice/board structure
402  *
403  * Return Value: The function returns zero on success or %-ENODEV on failure
404  * File: src/virtual.c
405  */
406 int virtual_reset(struct candevice_t *candev)
407 {
408         return 0;
409 }
410
411 /**
412  * virtual_init_hw_data - Initialize hardware cards
413  * @candev: Pointer to candevice/board structure
414  *
415  * Return Value: The function always returns zero
416  * File: src/virtual.c
417  */
418 int virtual_init_hw_data(struct candevice_t *candev) 
419 {
420         candev->res_addr=0;
421         candev->nr_82527_chips=0;
422         candev->nr_sja1000_chips=0;
423         candev->nr_all_chips=1;
424         candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
425
426         return 0;
427 }
428
429 #define CHIP_TYPE "virtual"
430
431 /**
432  * virtual_init_chip_data - Initialize chips
433  * @candev: Pointer to candevice/board structure
434  * @chipnr: Number of the CAN chip on the hardware card
435  *
436  * Return Value: The function always returns zero
437  * File: src/virtual.c
438  */
439 int virtual_init_chip_data(struct candevice_t *candev, int chipnr)
440 {
441         struct chip_t *chip = candev->chip[chipnr];
442         chip->chip_type = CHIP_TYPE;
443         chip->chip_base_addr = 0;
444         chip->clock = 10000000;
445         chip->int_clk_reg = 0x0;
446         chip->int_bus_reg = 0x0;
447         chip->max_objects = 1;
448
449         CANMSG("initializing virtual chip operations\n");
450         chip->chipspecops->chip_config=virtual_chip_config;
451         chip->chipspecops->baud_rate=virtual_baud_rate;
452         chip->chipspecops->standard_mask=virtual_standard_mask;
453         chip->chipspecops->extended_mask=virtual_extended_mask;
454         chip->chipspecops->message15_mask=virtual_extended_mask;
455         chip->chipspecops->clear_objects=virtual_clear_objects;
456         chip->chipspecops->config_irqs=virtual_config_irqs;
457         chip->chipspecops->pre_read_config=virtual_pre_read_config;
458         chip->chipspecops->pre_write_config=virtual_pre_write_config;
459         chip->chipspecops->send_msg=virtual_send_msg;
460         chip->chipspecops->check_tx_stat=virtual_check_tx_stat;
461         chip->chipspecops->wakeup_tx=virtual_wakeup_tx;
462         chip->chipspecops->remote_request=virtual_remote_request;
463         chip->chipspecops->enable_configuration=virtual_enable_configuration;
464         chip->chipspecops->disable_configuration=virtual_disable_configuration;
465         chip->chipspecops->set_btregs=virtual_set_btregs;
466         chip->chipspecops->start_chip=virtual_start_chip;
467         chip->chipspecops->stop_chip=virtual_stop_chip;
468         chip->chipspecops->irq_handler=NULL;
469
470         return 0;
471 }
472
473 /**
474  * virtual_init_obj_data - Initialize message buffers
475  * @chip: Pointer to chip specific structure
476  * @objnr: Number of the message buffer
477  *
478  * Return Value: The function always returns zero
479  * File: src/virtual.c
480  */
481 int virtual_init_obj_data(struct chip_t *chip, int objnr)
482 {
483         struct msgobj_t *obj=chip->msgobj[objnr];
484         obj->obj_base_addr=chip->chip_base_addr;
485         obj->tx_timeout.function=virtual_do_tx_timeout;
486         obj->tx_timeout.data=(unsigned long)obj;
487         return 0;
488 }
489
490 /**
491  * virtual_program_irq - program interrupts
492  * @candev: Pointer to candevice/board structure
493  *
494  * Return value: The function returns zero on success or %-ENODEV on failure
495  * File: src/virtual.c
496  */
497 int virtual_program_irq(struct candevice_t *candev)
498 {
499         return 0;
500 }
501
502 int virtual_register(struct hwspecops_t *hwspecops)
503 {
504         hwspecops->request_io = virtual_request_io;
505         hwspecops->release_io = virtual_release_io;
506         hwspecops->reset = virtual_reset;
507         hwspecops->init_hw_data = virtual_init_hw_data;
508         hwspecops->init_chip_data = virtual_init_chip_data;
509         hwspecops->init_obj_data = virtual_init_obj_data;
510         hwspecops->write_register = NULL;
511         hwspecops->read_register = NULL;
512         hwspecops->program_irq = virtual_program_irq;
513         return 0;
514 }