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;
219 * can_check_dev_taken - checks if bus device description is already taken by driver
220 * @anydev: pointer to bus specific Linux device description
222 * Returns: Returns 1 if device is already used by LinCAN driver, 0 otherwise.
224 int can_check_dev_taken(void *anydev)
227 struct candevice_t *candev;
230 for (board_nr=hardware_p->nr_boards; board_nr--; ) {
231 if((candev=hardware_p->candevice[board_nr])==NULL)
233 boarddev=candev->sysdevptr.anydev;
234 if(boarddev == anydev)
243 * register_obj_struct - registers message object into global array
244 * @obj: the initialized message object being registered
245 * @minorbase: wanted minor number, if (-1) automatically selected
247 * Return Value: returns negative number in the case of fail
249 int register_obj_struct(struct msgobj_t *obj, int minorbase)
251 static int next_minor=0;
255 next_minor=minorbase;
256 if(next_minor>=MAX_TOT_MSGOBJS)
260 if(objects_p[i]==NULL){
266 if(++i >= MAX_TOT_MSGOBJS) i=0;
267 }while(i!=next_minor);
274 * register_chip_struct - registers chip into global array
275 * @chip: the initialized chip structure being registered
276 * @minorbase: wanted minor number base, if (-1) automatically selected
278 * Return Value: returns negative number in the case of fail
280 int register_chip_struct(struct chip_t *chip, int minorbase)
282 static int next_chip_slot=0;
285 if(next_chip_slot>=MAX_TOT_CHIPS)
289 if(chips_p[i]==NULL){
295 if(++i >= MAX_TOT_CHIPS) i=0;
296 }while(i!=next_chip_slot);
303 * init_hw_struct - initializes driver hardware description structures
305 * The function init_hw_struct() is used to initialize the hardware structure.
307 * Return Value: returns negative number in the case of fail
309 int init_hw_struct(void)
313 int chan_param_idx=0;
315 hardware_p->nr_boards=0;
316 while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
317 hardware_p->nr_boards++;
319 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
320 CANMSG("Error initializing candevice_t structures.\n");
330 * init_device_struct - initializes single CAN device/board
331 * @card: index into @hardware_p HW description
332 * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
333 * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
335 * The function builds representation of the one board from parameters provided
336 * in the module parameters arrays:
337 * @hw[card] .. hardware type,
338 * @io[card] .. base IO address,
339 * @baudrate[chan_param_idx] .. per channel baudrate,
340 * @minor[chan_param_idx] .. optional specification of requested channel minor base,
341 * @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
342 * The indexes are advanced after consumed parameters if the registration is successful.
344 * The hardware specific operations of the device/board are initialized by call to
345 * init_hwspecops() function. Then board data are initialized by board specific
346 * init_hw_data() function. Then chips and objects representation is build by
347 * init_chip_struct() function. If all above steps are successful, chips and
348 * message objects are registered into global arrays.
350 * Return Value: returns negative number in the case of fail
352 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
354 struct candevice_t *candev;
361 candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
365 memset(candev, 0, sizeof(struct candevice_t));
367 hardware_p->candevice[card]=candev;
368 candev->candev_idx=card;
372 candev->hwname=hw[card];
373 candev->io_addr=io[card];
374 candev->dev_base_addr=io[card];
376 candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
377 if (candev->hwspecops==NULL)
380 memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
382 if (init_hwspecops(candev, &irqnum))
385 if (candev->hwspecops->init_hw_data(candev))
388 /* Alocate and initialize the chip structures */
389 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
392 irqsig=irq[*irq_param_idx_p+chipnr];
394 bd=baudrate[*chan_param_idx_p+chipnr];
395 if(!bd) bd=baudrate[0];
397 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000)))
403 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
404 int m=minor[*chan_param_idx_p+chipnr];
405 struct chip_t *chip=candev->chip[chipnr];
408 register_chip_struct(chip, m);
410 for (objnr=0; objnr<chip->max_objects; objnr++) {
411 register_obj_struct(chip->msgobj[objnr], m);
416 *irq_param_idx_p += irqnum;
417 *chan_param_idx_p += candev->nr_all_chips;
424 candevice_done(candev);
431 hardware_p->candevice[card]=NULL;
432 can_checked_free(candev);
438 * init_chip_struct - initializes one CAN chip structure
439 * @candev: pointer to the corresponding CAN device/board
440 * @chipnr: index of the chip in the corresponding device/board structure
441 * @irq: chip IRQ number or (-1) if not appropriate
442 * @baudrate: baudrate in the units of 1Bd
444 * Chip structure is allocated and chip specific operations are filled by
445 * call to board specific init_chip_data() function and generic
446 * init_chipspecops() function. The message objects are generated by
447 * calls to init_obj_struct() function.
449 * Return Value: returns negative number in the case of fail
451 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate)
457 candev->chip[chipnr]=(struct chip_t *)can_checked_malloc(sizeof(struct chip_t));
458 if ((chip=candev->chip[chipnr])==NULL)
461 memset(chip, 0, sizeof(struct chip_t));
463 chip->write_register=candev->hwspecops->write_register;
464 chip->read_register=candev->hwspecops->read_register;
466 chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
467 if (chip->chipspecops==NULL)
469 memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
471 chip->chip_idx=chipnr;
472 chip->hostdevice=candev;
474 chip->baudrate=baudrate;
477 candev->hwspecops->init_chip_data(candev,chipnr);
479 if (init_chipspecops(candev,chipnr))
482 for (objnr=0; objnr<chip->max_objects; objnr++) {
483 ret=init_obj_struct(candev, chip, objnr);
484 if(ret<0) return ret;
492 * init_obj_struct - initializes one CAN message object structure
493 * @candev: pointer to the corresponding CAN device/board
494 * @hostchip: pointer to the chip containing this object
495 * @objnr: index of the builded object in the chip structure
497 * The function initializes message object structure and allocates and initializes
498 * CAN queue chip ends structure.
500 * Return Value: returns negative number in the case of fail
502 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr)
504 struct canque_ends_t *qends;
505 struct msgobj_t *obj;
508 obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
509 hostchip->msgobj[objnr]=obj;
513 memset(obj, 0, sizeof(struct msgobj_t));
516 atomic_set(&obj->obj_used,0);
517 INIT_LIST_HEAD(&obj->obj_users);
518 init_timer(&obj->tx_timeout);
520 qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
521 if(qends == NULL) return -ENOMEM;
522 memset(qends, 0, sizeof(struct canque_ends_t));
523 obj->hostchip=hostchip;
528 obj->obj_flags = 0x0;
530 ret=canqueue_ends_init_chip(qends, hostchip, obj);
531 if(ret<0) return ret;
533 ret=candev->hwspecops->init_obj_data(hostchip,objnr);
534 if(ret<0) return ret;
541 * init_hwspecops - finds and initializes board/device specific operations
542 * @candev: pointer to the corresponding CAN device/board
543 * @irqnum_p: optional pointer to the number of interrupts required by board
545 * The function searches board @hwname in the list of supported boards types.
546 * The board type specific board_register() function is used to initialize
547 * @hwspecops operations.
549 * Return Value: returns negative number in the case of fail
551 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
553 const struct boardtype_t *brp;
555 brp = boardtype_find(candev->hwname);
558 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
563 *irqnum_p=brp->irqnum;
564 brp->board_register(candev->hwspecops);
571 * init_chipspecops - fills chip specific operations for board for known chip types
572 * @candev: pointer to the corresponding CAN device/board
573 * @chipnr: index of the chip in the device/board structure
575 * The function fills chip specific operations for next known generic chip
576 * types "i82527", "sja1000", "sja1000p" (PeliCAN). Other non generic chip types
577 * operations has to be initialized in the board specific init_chip_data() function.
579 * Return Value: returns negative number in the case of fail
581 int init_chipspecops(struct candevice_t *candev, int chipnr)
583 if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
584 candev->chip[chipnr]->max_objects=15;
585 i82527_register(candev->chip[chipnr]->chipspecops);
587 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
588 candev->chip[chipnr]->max_objects=1;
589 sja1000_register(candev->chip[chipnr]->chipspecops);
591 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
592 candev->chip[chipnr]->max_objects=1;
593 sja1000p_register(candev->chip[chipnr]->chipspecops);
602 * can_chip_setup_irq - attaches chip to the system interrupt processing
603 * @chip: pointer to CAN chip structure
605 * Return Value: returns negative number in the case of fail
607 int can_chip_setup_irq(struct chip_t *chip)
611 if(!chip->chipspecops->irq_handler)
614 if (request_irq(chip->chip_irq,chip->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chip))
617 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
618 chip->flags |= CHIP_IRQ_SETUP;
625 * can_chip_free_irq - unregisters chip interrupt handler from the system
626 * @chip: pointer to CAN chip structure
628 void can_chip_free_irq(struct chip_t *chip)
630 if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
631 free_irq(chip->chip_irq, chip);
632 chip->flags &= ~CHIP_IRQ_SETUP;
636 #endif /*CAN_WITH_RTL*/