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
9 #include "../include/can.h"
10 #include "../include/can_sysdep.h"
11 #include "../include/main.h"
14 long virtual_bus_latency(struct msgobj_t *obj)
17 latency=obj->hostchip->baudrate;
19 latency=(long)HZ*1000/latency;
25 /* * * Virtual Chip Functionality * * */
27 int virtual_enable_configuration(struct chip_t *chip)
32 int virtual_disable_configuration(struct chip_t *chip)
38 * virtual_chip_config: - can chip configuration
39 * @chip: pointer to chip state structure
41 * Return Value: negative value reports error.
44 int virtual_chip_config(struct chip_t *chip)
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
55 * Return Value: negative value reports error.
58 int virtual_extended_mask(struct chip_t *chip, unsigned long code, unsigned long mask)
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
72 * Return Value: negative value reports error.
75 int virtual_baud_rate(struct chip_t *chip, int rate, int clock, int sjw,
76 int sampl_pt, int flags)
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
88 void virtual_read(struct chip_t *chip, struct msgobj_t *obj) {
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
97 * Return Value: negative value reports error.
98 * Positive value indicates immediate reception of message.
101 int virtual_pre_read_config(struct chip_t *chip, struct msgobj_t *obj)
106 #define MAX_TRANSMIT_WAIT_LOOPS 10
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
113 * Return Value: negative value reports error.
114 * File: src/virtual.c
116 int virtual_pre_write_config(struct chip_t *chip, struct msgobj_t *obj,
117 struct canmsg_t *msg)
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
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
133 int virtual_send_msg(struct chip_t *chip, struct msgobj_t *obj,
134 struct canmsg_t *msg)
140 * virtual_check_tx_stat: - checks state of transmission engine
141 * @chip: pointer to chip state structure
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
148 int virtual_check_tx_stat(struct chip_t *chip)
154 * virtual_set_btregs: - configures bitrate registers
155 * @chip: pointer to chip state structure
156 * @btr0: bitrate register 0
157 * @btr1: bitrate register 1
159 * Return Value: negative value reports error.
160 * File: src/virtual.c
162 int virtual_set_btregs(struct chip_t *chip, unsigned short btr0,
169 * virtual_stop_chip: - starts chip message processing
170 * @chip: pointer to chip state structure
172 * Return Value: negative value reports error.
173 * File: src/virtual.c
175 int virtual_start_chip(struct chip_t *chip)
181 * virtual_stop_chip: - stops chip message processing
182 * @chip: pointer to chip state structure
184 * Return Value: negative value reports error.
185 * File: src/virtual.c
187 int virtual_stop_chip(struct chip_t *chip)
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
198 * Return Value: negative value reports error.
199 * File: src/virtual.c
201 int virtual_remote_request(struct chip_t *chip, struct msgobj_t *obj)
203 CANMSG("virtual_remote_request not implemented\n");
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
213 * Return Value: negative value reports error.
214 * File: src/virtual.c
216 int virtual_standard_mask(struct chip_t *chip, unsigned short code,
219 CANMSG("virtual_standard_mask not implemented\n");
224 * virtual_clear_objects: - clears state of all message object residing in chip
225 * @chip: pointer to chip state structure
227 * Return Value: negative value reports error.
228 * File: src/virtual.c
230 int virtual_clear_objects(struct chip_t *chip)
232 CANMSG("virtual_clear_objects not implemented\n");
237 * virtual_config_irqs: - tunes chip hardware interrupt delivery
238 * @chip: pointer to chip state structure
239 * @irqs: requested chip IRQ configuration
241 * Return Value: negative value reports error.
242 * File: src/virtual.c
244 int virtual_config_irqs(struct chip_t *chip, short irqs)
246 CANMSG("virtual_config_irqs not implemented\n");
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
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
261 void virtual_irq_write_handler(struct chip_t *chip, struct msgobj_t *obj)
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
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
281 * File: src/virtual.c
283 irqreturn_t virtual_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
289 void virtual_schedule_next(struct msgobj_t *obj)
292 /* dummy lock to prevent preemption fully portable way */
293 can_spinlock_t dummy_lock;
295 /* preempt_disable() */
296 can_spin_lock_init(&dummy_lock);
297 can_spin_lock(&dummy_lock);
299 set_bit(OBJ_TX_REQUEST,&obj->flags);
301 while(!test_and_set_bit(OBJ_TX_LOCK,&obj->flags)){
303 clear_bit(OBJ_TX_REQUEST,&obj->flags);
305 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
307 mod_timer(&obj->tx_timeout,
308 jiffies+virtual_bus_latency(obj));
309 CANMSG("virtual: scheduled delivery\n");
312 clear_bit(OBJ_TX_LOCK,&obj->flags);
314 if(!test_bit(OBJ_TX_REQUEST,&obj->flags)) break;
315 DEBUGMSG("TX looping in virtual_schedule_next\n");
318 /* preempt_enable(); */
319 can_spin_unlock(&dummy_lock);
323 void virtual_do_tx_timeout(unsigned long data)
325 struct msgobj_t *obj=(struct msgobj_t *)data;
328 /* Deliver message to edges */
329 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
330 /* Free transmitted slot */
331 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
333 CANMSG("virtual: delayed delivery\n");
335 clear_bit(OBJ_TX_LOCK,&obj->flags);
337 virtual_schedule_next(obj);
341 * virtual_wakeup_tx: - wakeups TX processing
342 * @chip: pointer to chip state structure
343 * @obj: pointer to message object structure
345 * Return Value: negative value reports error.
346 * File: src/virtual.c
348 int virtual_wakeup_tx(struct chip_t *chip, struct msgobj_t *obj)
350 /* set_bit(OBJ_TX_REQUEST,&obj->flags); */
352 struct canque_edge_t *qedge;
353 struct canque_slot_t *slot;
356 if(!virtual_bus_latency(obj)) {
357 /* Ensure delivery of all ready slots */
358 while((cmd=canque_test_outslot(obj->qends, &qedge, &slot)) >= 0){
360 canque_filter_msg2edges(obj->qends, &slot->msg);
361 CANMSG("virtual: direct delivery\n");
363 canque_free_outslot(obj->qends, qedge, slot);
366 virtual_schedule_next(obj);
373 /* * * Virtual Board Functionality * * */
376 * virtual_request_io: - reserve io or memory range for can board
377 * @candev: pointer to candevice/board which asks for io. Field @io_addr
378 * of @candev is used in most cases to define start of the range
380 * Return Value: The function returns zero on success or %-ENODEV on failure
381 * File: src/virtual.c
383 int virtual_request_io(struct candevice_t *candev)
389 * virtual_elease_io - free reserved io memory range
390 * @candev: pointer to candevice/board which releases io
392 * Return Value: The function always returns zero
393 * File: src/virtual.c
395 int virtual_release_io(struct candevice_t *candev)
401 * virtual_reset - hardware reset routine
402 * @candev: Pointer to candevice/board structure
404 * Return Value: The function returns zero on success or %-ENODEV on failure
405 * File: src/virtual.c
407 int virtual_reset(struct candevice_t *candev)
413 * virtual_init_hw_data - Initialize hardware cards
414 * @candev: Pointer to candevice/board structure
416 * Return Value: The function always returns zero
417 * File: src/virtual.c
419 int virtual_init_hw_data(struct candevice_t *candev)
422 candev->nr_82527_chips=0;
423 candev->nr_sja1000_chips=0;
424 candev->nr_all_chips=1;
425 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
430 #define CHIP_TYPE "virtual"
433 * virtual_init_chip_data - Initialize chips
434 * @candev: Pointer to candevice/board structure
435 * @chipnr: Number of the CAN chip on the hardware card
437 * Return Value: The function always returns zero
438 * File: src/virtual.c
440 int virtual_init_chip_data(struct candevice_t *candev, int chipnr)
442 struct chip_t *chip = candev->chip[chipnr];
443 chip->chip_type = CHIP_TYPE;
444 chip->chip_base_addr = 0;
445 chip->clock = 10000000;
446 chip->int_clk_reg = 0x0;
447 chip->int_bus_reg = 0x0;
448 chip->max_objects = 1;
450 CANMSG("initializing virtual chip operations\n");
451 chip->chipspecops->chip_config=virtual_chip_config;
452 chip->chipspecops->baud_rate=virtual_baud_rate;
453 chip->chipspecops->standard_mask=virtual_standard_mask;
454 chip->chipspecops->extended_mask=virtual_extended_mask;
455 chip->chipspecops->message15_mask=virtual_extended_mask;
456 chip->chipspecops->clear_objects=virtual_clear_objects;
457 chip->chipspecops->config_irqs=virtual_config_irqs;
458 chip->chipspecops->pre_read_config=virtual_pre_read_config;
459 chip->chipspecops->pre_write_config=virtual_pre_write_config;
460 chip->chipspecops->send_msg=virtual_send_msg;
461 chip->chipspecops->check_tx_stat=virtual_check_tx_stat;
462 chip->chipspecops->wakeup_tx=virtual_wakeup_tx;
463 chip->chipspecops->remote_request=virtual_remote_request;
464 chip->chipspecops->enable_configuration=virtual_enable_configuration;
465 chip->chipspecops->disable_configuration=virtual_disable_configuration;
466 chip->chipspecops->set_btregs=virtual_set_btregs;
467 chip->chipspecops->start_chip=virtual_start_chip;
468 chip->chipspecops->stop_chip=virtual_stop_chip;
469 chip->chipspecops->irq_handler=NULL;
475 * virtual_init_obj_data - Initialize message buffers
476 * @chip: Pointer to chip specific structure
477 * @objnr: Number of the message buffer
479 * Return Value: The function always returns zero
480 * File: src/virtual.c
482 int virtual_init_obj_data(struct chip_t *chip, int objnr)
484 struct msgobj_t *obj=chip->msgobj[objnr];
485 obj->obj_base_addr=chip->chip_base_addr;
487 obj->tx_timeout.function=virtual_do_tx_timeout;
488 obj->tx_timeout.data=(unsigned long)obj;
493 * virtual_program_irq - program interrupts
494 * @candev: Pointer to candevice/board structure
496 * Return value: The function returns zero on success or %-ENODEV on failure
497 * File: src/virtual.c
499 int virtual_program_irq(struct candevice_t *candev)
504 int virtual_register(struct hwspecops_t *hwspecops)
506 hwspecops->request_io = virtual_request_io;
507 hwspecops->release_io = virtual_release_io;
508 hwspecops->reset = virtual_reset;
509 hwspecops->init_hw_data = virtual_init_hw_data;
510 hwspecops->init_chip_data = virtual_init_chip_data;
511 hwspecops->init_obj_data = virtual_init_obj_data;
512 hwspecops->write_register = NULL;
513 hwspecops->read_register = NULL;
514 hwspecops->program_irq = virtual_program_irq;