2 * Linux CAN-bus device 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
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/devcommon.h"
14 #include "../include/setup.h"
15 #include "../include/finish.h"
17 extern int sja1000_register(struct chipspecops_t *chipspecops);
18 extern int sja1000p_register(struct chipspecops_t *chipspecops);
19 extern int i82527_register(struct chipspecops_t *chipspecops);
21 int init_hwspecops(struct candevice_t *candev, int *irqnum_p);
22 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p);
23 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate);
24 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr);
25 int init_chipspecops(struct candevice_t *candev, int chipnr);
28 * can_checked_malloc - memory allocation with registering of requested blocks
29 * @size: size of the requested block
31 * The function is used in the driver initialization phase to catch possible memory
32 * leaks for future driver finalization or case, that driver initialization fail.
34 * Return Value: pointer to the allocated memory or NULL in the case of fail
36 void *can_checked_malloc(size_t size)
38 struct mem_addr *mem_new;
41 address_p=kmalloc(size,GFP_KERNEL);
42 if(address_p == NULL) {
43 CANMSG("can_checked_malloc: out of the memory\n");
48 DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
49 (int)size, address_p, mem_head);
52 mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
53 if (mem_new == NULL) {
54 CANMSG("can_checked_malloc: memory list allocation error.\n");
58 mem_new->next=mem_head;
59 mem_new->address=address_p;
67 * can_checked_free - free memory allocated by can_checked_malloc()
68 * @address_p: pointer to the memory block
70 int can_checked_free(void *address_p)
72 struct mem_addr **mem_pptr;
73 struct mem_addr *mem_del=NULL;
76 DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
79 for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
80 if (mem_del->address != address_p)
82 *mem_pptr=mem_del->next;
88 CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
96 * can_del_mem_list - check for stale memory allocations at driver finalization
98 * Checks, if there are still some memory blocks allocated and releases memory
99 * occupied by such blocks back to the system
101 int can_del_mem_list(void)
103 struct mem_addr *mem;
106 DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
108 if(mem_head == NULL) {
109 CANMSG("can_del_mem_list: no entries on the list - OK\n");
113 while((mem=mem_head) != NULL) {
115 CANMSG("can_del_mem_list: deleting %p with size %d\n",
116 mem->address, (int)mem->size);
125 * can_request_io_region - request IO space region
126 * @start: the first IO port address
127 * @n: number of the consecutive IO port addresses
128 * @name: name/label for the requested region
130 * The function hides system specific implementation of the feature.
132 * Return Value: returns positive value (1) in the case, that region could
133 * be reserved for the driver. Returns zero (0) if there is collision with
134 * other driver or region cannot be taken for some other reason.
136 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
138 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
139 if(check_region(start,n)) return 0;
140 request_region(start,n,name);
143 return (request_region(start,n,name))?1:0;
148 * can_release_io_region - release IO space region
149 * @start: the first IO port address
150 * @n: number of the consecutive IO port addresses
152 void can_release_io_region(unsigned long start, unsigned long n)
154 release_region(start,n);
158 * can_request_mem_region - request memory space region
159 * @start: the first memory port physical address
160 * @n: number of the consecutive memory port addresses
161 * @name: name/label for the requested region
163 * The function hides system specific implementation of the feature.
165 * Return Value: returns positive value (1) in the case, that region could
166 * be reserved for the driver. Returns zero (0) if there is collision with
167 * other driver or region cannot be taken for some other reason.
169 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
171 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
174 return (request_mem_region(start,n,name))?1:0;
179 * can_release_mem_region - release memory space region
180 * @start: the first memory port physical address
181 * @n: number of the consecutive memory port addresses
183 void can_release_mem_region(unsigned long start, unsigned long n)
185 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
188 release_mem_region(start,n);
193 * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
194 * @candev: pointer to the previously filled device/board, chips and message objects structures
195 * @new_base: @candev new base address
197 * This function adapts base addresses of all structures of one board
198 * to the new board base address.
199 * It is required for translation between physical and virtual address mappings.
200 * This function is prepared to simplify board specific xxx_request_io() function
201 * for memory mapped devices.
203 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
208 offs=new_base-candev->dev_base_addr;
209 candev->dev_base_addr=new_base;
210 for(i=0;i<candev->nr_all_chips;i++){
211 candev->chip[i]->chip_base_addr += offs;
212 for(j=0;j<candev->chip[i]->max_objects;j++)
213 candev->chip[i]->msgobj[j]->obj_base_addr += offs;
220 * register_obj_struct - registers message object into global array
221 * @obj: the initialized message object being registered
222 * @minorbase: wanted minor number, if (-1) automatically selected
224 * Return Value: returns negative number in the case of fail
226 int register_obj_struct(struct msgobj_t *obj, int minorbase)
228 static int next_minor=0;
232 next_minor=minorbase;
233 if(next_minor>=MAX_TOT_MSGOBJS)
237 if(objects_p[i]==NULL){
243 if(++i >= MAX_TOT_MSGOBJS) i=0;
244 }while(i!=next_minor);
251 * register_chip_struct - registers chip into global array
252 * @chip: the initialized chip structure being registered
253 * @minorbase: wanted minor number base, if (-1) automatically selected
255 * Return Value: returns negative number in the case of fail
257 int register_chip_struct(struct chip_t *chip, int minorbase)
259 static int next_chip_slot=0;
262 if(next_chip_slot>=MAX_TOT_CHIPS)
266 if(chips_p[i]==NULL){
272 if(++i >= MAX_TOT_CHIPS) i=0;
273 }while(i!=next_chip_slot);
280 * init_hw_struct - initializes driver hardware description structures
282 * The function init_hw_struct() is used to initialize the hardware structure.
284 * Return Value: returns negative number in the case of fail
286 int init_hw_struct(void)
290 int chan_param_idx=0;
292 hardware_p->nr_boards=0;
293 while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
294 hardware_p->nr_boards++;
296 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
297 CANMSG("Error initializing candevice_t structures.\n");
307 * init_device_struct - initializes single CAN device/board
308 * @card: index into @hardware_p HW description
309 * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
310 * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
312 * The function builds representation of the one board from parameters provided
313 * in the module parameters arrays:
314 * @hw[card] .. hardware type,
315 * @io[card] .. base IO address,
316 * @baudrate[chan_param_idx] .. per channel baudrate,
317 * @minor[chan_param_idx] .. optional specification of requested channel minor base,
318 * @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
319 * The indexes are advanced after consumed parameters if the registration is successful.
321 * The hardware specific operations of the device/board are initialized by call to
322 * init_hwspecops() function. Then board data are initialized by board specific
323 * init_hw_data() function. Then chips and objects representation is build by
324 * init_chip_struct() function. If all above steps are successful, chips and
325 * message objects are registered into global arrays.
327 * Return Value: returns negative number in the case of fail
329 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
331 struct candevice_t *candev;
338 candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
342 memset(candev, 0, sizeof(struct candevice_t));
344 hardware_p->candevice[card]=candev;
345 candev->candev_idx=card;
349 candev->hwname=hw[card];
350 candev->io_addr=io[card];
351 candev->dev_base_addr=io[card];
353 candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
354 if (candev->hwspecops==NULL)
357 memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
359 if (init_hwspecops(candev, &irqnum))
362 if (candev->hwspecops->init_hw_data(candev))
365 /* Alocate and initialize the chip structures */
366 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
369 irqsig=irq[*irq_param_idx_p+chipnr];
371 bd=baudrate[*chan_param_idx_p+chipnr];
372 if(!bd) bd=baudrate[0];
374 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000)))
380 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
381 int m=minor[*chan_param_idx_p+chipnr];
382 struct chip_t *chip=candev->chip[chipnr];
385 register_chip_struct(chip, m);
387 for (objnr=0; objnr<chip->max_objects; objnr++) {
388 register_obj_struct(chip->msgobj[objnr], m);
393 *irq_param_idx_p += irqnum;
394 *chan_param_idx_p += candev->nr_all_chips;
401 candevice_done(candev);
408 hardware_p->candevice[card]=NULL;
409 can_checked_free(candev);
415 * init_chip_struct - initializes one CAN chip structure
416 * @candev: pointer to the corresponding CAN device/board
417 * @chipnr: index of the chip in the corresponding device/board structure
418 * @irq: chip IRQ number or (-1) if not appropriate
419 * @baudrate: baudrate in the units of 1Bd
421 * Chip structure is allocated and chip specific operations are filled by
422 * call to board specific init_chip_data() function and generic
423 * init_chipspecops() function. The message objects are generated by
424 * calls to init_obj_struct() function.
426 * Return Value: returns negative number in the case of fail
428 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate)
434 candev->chip[chipnr]=(struct chip_t *)can_checked_malloc(sizeof(struct chip_t));
435 if ((chip=candev->chip[chipnr])==NULL)
438 memset(chip, 0, sizeof(struct chip_t));
440 chip->write_register=candev->hwspecops->write_register;
441 chip->read_register=candev->hwspecops->read_register;
443 chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
444 if (chip->chipspecops==NULL)
446 memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
448 chip->chip_idx=chipnr;
449 chip->hostdevice=candev;
451 chip->baudrate=baudrate;
454 candev->hwspecops->init_chip_data(candev,chipnr);
456 if (init_chipspecops(candev,chipnr))
459 for (objnr=0; objnr<chip->max_objects; objnr++) {
460 ret=init_obj_struct(candev, chip, objnr);
461 if(ret<0) return ret;
469 * init_obj_struct - initializes one CAN message object structure
470 * @candev: pointer to the corresponding CAN device/board
471 * @hostchip: pointer to the chip containing this object
472 * @objnr: index of the builded object in the chip structure
474 * The function initializes message object structure and allocates and initializes
475 * CAN queue chip ends structure.
477 * Return Value: returns negative number in the case of fail
479 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr)
481 struct canque_ends_t *qends;
482 struct msgobj_t *obj;
485 obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
486 hostchip->msgobj[objnr]=obj;
490 memset(obj, 0, sizeof(struct msgobj_t));
493 atomic_set(&obj->obj_used,0);
494 INIT_LIST_HEAD(&obj->obj_users);
495 init_timer(&obj->tx_timeout);
497 qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
498 if(qends == NULL) return -ENOMEM;
499 memset(qends, 0, sizeof(struct canque_ends_t));
500 obj->hostchip=hostchip;
505 obj->obj_flags = 0x0;
507 ret=canqueue_ends_init_chip(qends, hostchip, obj);
508 if(ret<0) return ret;
510 ret=candev->hwspecops->init_obj_data(hostchip,objnr);
511 if(ret<0) return ret;
518 * init_hwspecops - finds and initializes board/device specific operations
519 * @candev: pointer to the corresponding CAN device/board
520 * @irqnum_p: optional pointer to the number of interrupts required by board
522 * The function searches board @hwname in the list of supported boards types.
523 * The board type specific board_register() function is used to initialize
524 * @hwspecops operations.
526 * Return Value: returns negative number in the case of fail
528 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
530 const struct boardtype_t *brp;
532 brp = boardtype_find(candev->hwname);
535 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
540 *irqnum_p=brp->irqnum;
541 brp->board_register(candev->hwspecops);
548 * init_chipspecops - fills chip specific operations for board for known chip types
549 * @candev: pointer to the corresponding CAN device/board
550 * @chipnr: index of the chip in the device/board structure
552 * The function fills chip specific operations for next known generic chip
553 * types "i82527", "sja1000", "sja1000p" (PeliCAN). Other non generic chip types
554 * operations has to be initialized in the board specific init_chip_data() function.
556 * Return Value: returns negative number in the case of fail
558 int init_chipspecops(struct candevice_t *candev, int chipnr)
560 if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
561 candev->chip[chipnr]->max_objects=15;
562 i82527_register(candev->chip[chipnr]->chipspecops);
564 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
565 candev->chip[chipnr]->max_objects=1;
566 sja1000_register(candev->chip[chipnr]->chipspecops);
568 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
569 candev->chip[chipnr]->max_objects=1;
570 sja1000p_register(candev->chip[chipnr]->chipspecops);
579 * can_chip_setup_irq - attaches chip to the system interrupt processing
580 * @chip: pointer to CAN chip structure
582 * Return Value: returns negative number in the case of fail
584 int can_chip_setup_irq(struct chip_t *chip)
588 if(!chip->chipspecops->irq_handler)
591 if (request_irq(chip->chip_irq,chip->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chip))
594 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
595 chip->flags |= CHIP_IRQ_SETUP;
602 * can_chip_free_irq - unregisters chip interrupt handler from the system
603 * @chip: pointer to CAN chip structure
605 void can_chip_free_irq(struct chip_t *chip)
607 if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
608 free_irq(chip->chip_irq, chip);
609 chip->flags &= ~CHIP_IRQ_SETUP;
613 #endif /*CAN_WITH_RTL*/