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.3 17 Jun 2004
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 #ifdef CAN_ENABLE_VME_SUPPORT
19 /* Modified version of ca91c042 driver can be found in
20 * components/comm/contrib directory. */
23 int init_hwspecops(struct candevice_t *candev, int *irqnum_p);
24 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p);
25 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate);
26 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr);
29 * can_checked_malloc - memory allocation with registering of requested blocks
30 * @size: size of the requested block
32 * The function is used in the driver initialization phase to catch possible memory
33 * leaks for future driver finalization or case, that driver initialization fail.
35 * Return Value: pointer to the allocated memory or NULL in the case of fail
37 void *can_checked_malloc(size_t size)
39 struct mem_addr *mem_new;
42 address_p=kmalloc(size,GFP_KERNEL);
43 if(address_p == NULL) {
44 CANMSG("can_checked_malloc: out of the memory\n");
49 DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
50 (int)size, address_p, mem_head);
53 mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
54 if (mem_new == NULL) {
55 CANMSG("can_checked_malloc: memory list allocation error.\n");
59 mem_new->next=mem_head;
60 mem_new->address=address_p;
68 * can_checked_free - free memory allocated by can_checked_malloc()
69 * @address_p: pointer to the memory block
71 int can_checked_free(void *address_p)
73 struct mem_addr **mem_pptr;
74 struct mem_addr *mem_del=NULL;
77 DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
80 for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
81 if (mem_del->address != address_p)
83 *mem_pptr=mem_del->next;
89 CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
97 * can_del_mem_list - check for stale memory allocations at driver finalization
99 * Checks, if there are still some memory blocks allocated and releases memory
100 * occupied by such blocks back to the system
102 int can_del_mem_list(void)
104 struct mem_addr *mem;
107 DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
109 if(mem_head == NULL) {
110 CANMSG("can_del_mem_list: no entries on the list - OK\n");
114 while((mem=mem_head) != NULL) {
116 CANMSG("can_del_mem_list: deleting %p with size %d\n",
117 mem->address, (int)mem->size);
126 * can_request_io_region - request IO space region
127 * @start: the first IO port address
128 * @n: number of the consecutive IO port addresses
129 * @name: name/label for the requested region
131 * The function hides system specific implementation of the feature.
133 * Return Value: returns positive value (1) in the case, that region could
134 * be reserved for the driver. Returns zero (0) if there is collision with
135 * other driver or region cannot be taken for some other reason.
137 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
139 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
140 if(check_region(start,n)) return 0;
141 request_region(start,n,name);
144 return (request_region(start,n,name))?1:0;
149 * can_release_io_region - release IO space region
150 * @start: the first IO port address
151 * @n: number of the consecutive IO port addresses
153 void can_release_io_region(unsigned long start, unsigned long n)
155 release_region(start,n);
159 * can_request_mem_region - request memory space region
160 * @start: the first memory port physical address
161 * @n: number of the consecutive memory port addresses
162 * @name: name/label for the requested region
164 * The function hides system specific implementation of the feature.
166 * Return Value: returns positive value (1) in the case, that region could
167 * be reserved for the driver. Returns zero (0) if there is collision with
168 * other driver or region cannot be taken for some other reason.
170 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
172 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
175 return (request_mem_region(start,n,name))?1:0;
180 * can_release_mem_region - release memory space region
181 * @start: the first memory port physical address
182 * @n: number of the consecutive memory port addresses
184 void can_release_mem_region(unsigned long start, unsigned long n)
186 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
189 release_mem_region(start,n);
194 * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
195 * @candev: pointer to the previously filled device/board, chips and message objects structures
196 * @new_base: @candev new base address
198 * This function adapts base addresses of all structures of one board
199 * to the new board base address.
200 * It is required for translation between physical and virtual address mappings.
201 * This function is prepared to simplify board specific xxx_request_io() function
202 * for memory mapped devices.
204 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
209 offs=new_base-candev->dev_base_addr;
210 candev->dev_base_addr=new_base;
211 for(i=0;i<candev->nr_all_chips;i++){
212 candev->chip[i]->chip_base_addr += offs;
213 for(j=0;j<candev->chip[i]->max_objects;j++)
214 candev->chip[i]->msgobj[j]->obj_base_addr += offs;
220 * can_check_dev_taken - checks if bus device description is already taken by driver
221 * @anydev: pointer to bus specific Linux device description
223 * Returns: Returns 1 if device is already used by LinCAN driver, 0 otherwise.
225 int can_check_dev_taken(void *anydev)
228 struct candevice_t *candev;
231 for (board_nr=hardware_p->nr_boards; board_nr--; ) {
232 if((candev=hardware_p->candevice[board_nr])==NULL)
234 boarddev=candev->sysdevptr.anydev;
235 if(boarddev == anydev)
244 * register_obj_struct - registers message object into global array
245 * @obj: the initialized message object being registered
246 * @minorbase: wanted minor number, if (-1) automatically selected
248 * Return Value: returns negative number in the case of fail
250 int register_obj_struct(struct msgobj_t *obj, int minorbase)
252 static int next_minor=0;
256 next_minor=minorbase;
257 if(next_minor>=MAX_TOT_MSGOBJS)
261 if(objects_p[i]==NULL){
267 if(++i >= MAX_TOT_MSGOBJS) i=0;
268 }while(i!=next_minor);
275 * register_chip_struct - registers chip into global array
276 * @chip: the initialized chip structure being registered
277 * @minorbase: wanted minor number base, if (-1) automatically selected
279 * Return Value: returns negative number in the case of fail
281 int register_chip_struct(struct chip_t *chip, int minorbase)
283 static int next_chip_slot=0;
286 if(next_chip_slot>=MAX_TOT_CHIPS)
290 if(chips_p[i]==NULL){
296 if(++i >= MAX_TOT_CHIPS) i=0;
297 }while(i!=next_chip_slot);
304 * init_hw_struct - initializes driver hardware description structures
306 * The function init_hw_struct() is used to initialize the hardware structure.
308 * Return Value: returns negative number in the case of fail
310 int init_hw_struct(void)
314 int chan_param_idx=0;
316 hardware_p->nr_boards=0;
317 while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
318 hardware_p->nr_boards++;
320 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
321 CANMSG("Error initializing candevice_t structures.\n");
331 * init_device_struct - initializes single CAN device/board
332 * @card: index into @hardware_p HW description
333 * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
334 * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
336 * The function builds representation of the one board from parameters provided
337 * in the module parameters arrays:
338 * @hw[card] .. hardware type,
339 * @io[card] .. base IO address,
340 * @baudrate[chan_param_idx] .. per channel baudrate,
341 * @minor[chan_param_idx] .. optional specification of requested channel minor base,
342 * @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
343 * The indexes are advanced after consumed parameters if the registration is successful.
345 * The hardware specific operations of the device/board are initialized by call to
346 * init_hwspecops() function. Then board data are initialized by board specific
347 * init_hw_data() function. Then chips and objects representation is build by
348 * init_chip_struct() function. If all above steps are successful, chips and
349 * message objects are registered into global arrays.
351 * Return Value: returns negative number in the case of fail
353 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
355 struct candevice_t *candev;
362 candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
366 memset(candev, 0, sizeof(struct candevice_t));
368 hardware_p->candevice[card]=candev;
369 candev->candev_idx=card;
373 candev->hwname=hw[card];
374 candev->io_addr=io[card];
375 candev->dev_base_addr=io[card];
377 candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
378 if (candev->hwspecops==NULL)
381 memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
383 if (init_hwspecops(candev, &irqnum))
386 if (candev->hwspecops->init_hw_data(candev))
389 /* Alocate and initialize the chip structures */
390 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
393 irqsig=irq[*irq_param_idx_p+chipnr];
395 bd=baudrate[*chan_param_idx_p+chipnr];
396 if(!bd) bd=baudrate[0];
398 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000)))
404 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
405 int m=minor[*chan_param_idx_p+chipnr];
406 struct chip_t *chip=candev->chip[chipnr];
409 register_chip_struct(chip, m);
411 for (objnr=0; objnr<chip->max_objects; objnr++) {
412 register_obj_struct(chip->msgobj[objnr], m);
417 *irq_param_idx_p += irqnum;
418 *chan_param_idx_p += candev->nr_all_chips;
425 candevice_done(candev);
432 hardware_p->candevice[card]=NULL;
433 can_checked_free(candev);
439 * init_chip_struct - initializes one CAN chip structure
440 * @candev: pointer to the corresponding CAN device/board
441 * @chipnr: index of the chip in the corresponding device/board structure
442 * @irq: chip IRQ number or (-1) if not appropriate
443 * @baudrate: baudrate in the units of 1Bd
445 * Chip structure is allocated and chip specific operations are filled by
446 * call to board specific init_chip_data() which calls chip specific
447 * fill_chipspecops(). The message objects are generated by
448 * calls to init_obj_struct() function.
450 * Return Value: returns negative number in the case of fail
452 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate)
458 candev->chip[chipnr]=(struct chip_t *)can_checked_malloc(sizeof(struct chip_t));
459 if ((chip=candev->chip[chipnr])==NULL)
462 memset(chip, 0, sizeof(struct chip_t));
464 chip->write_register=candev->hwspecops->write_register;
465 chip->read_register=candev->hwspecops->read_register;
467 chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
468 if (chip->chipspecops==NULL)
470 memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
472 chip->chip_idx=chipnr;
473 chip->hostdevice=candev;
475 chip->baudrate=baudrate;
478 if(candev->hwspecops->init_chip_data(candev,chipnr)<0)
481 for (objnr=0; objnr<chip->max_objects; objnr++) {
482 ret=init_obj_struct(candev, chip, objnr);
483 if(ret<0) return ret;
491 * init_obj_struct - initializes one CAN message object structure
492 * @candev: pointer to the corresponding CAN device/board
493 * @hostchip: pointer to the chip containing this object
494 * @objnr: index of the builded object in the chip structure
496 * The function initializes message object structure and allocates and initializes
497 * CAN queue chip ends structure.
499 * Return Value: returns negative number in the case of fail
501 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr)
503 struct canque_ends_t *qends;
504 struct msgobj_t *obj;
507 obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
508 hostchip->msgobj[objnr]=obj;
512 memset(obj, 0, sizeof(struct msgobj_t));
515 atomic_set(&obj->obj_used,0);
516 INIT_LIST_HEAD(&obj->obj_users);
517 init_timer(&obj->tx_timeout);
519 qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
520 if(qends == NULL) return -ENOMEM;
521 memset(qends, 0, sizeof(struct canque_ends_t));
522 obj->hostchip=hostchip;
527 obj->obj_flags = 0x0;
529 ret=canqueue_ends_init_chip(qends, hostchip, obj);
530 if(ret<0) return ret;
532 ret=candev->hwspecops->init_obj_data(hostchip,objnr);
533 if(ret<0) return ret;
540 * init_hwspecops - finds and initializes board/device specific operations
541 * @candev: pointer to the corresponding CAN device/board
542 * @irqnum_p: optional pointer to the number of interrupts required by board
544 * The function searches board @hwname in the list of supported boards types.
545 * The board type specific board_register() function is used to initialize
546 * @hwspecops operations.
548 * Return Value: returns negative number in the case of fail
550 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
552 const struct boardtype_t *brp;
554 brp = boardtype_find(candev->hwname);
557 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
562 *irqnum_p=brp->irqnum;
563 brp->board_register(candev->hwspecops);
571 can_irqreturn_t can_default_irq_dispatch(int irq, void *dev_id, struct pt_regs *regs)
574 struct chip_t *chip=(struct chip_t *)dev_id;
576 retval=chip->chipspecops->irq_handler(irq, chip);
577 return CAN_IRQ_RETVAL(retval);
581 * can_chip_setup_irq - attaches chip to the system interrupt processing
582 * @chip: pointer to CAN chip structure
584 * Return Value: returns negative number in the case of fail
586 int can_chip_setup_irq(struct chip_t *chip)
590 if(!chip->chipspecops->irq_handler)
593 if ((chip->flags & CHIP_IRQ_VME) == 0) {
594 if (request_irq(chip->chip_irq,can_default_irq_dispatch,SA_SHIRQ,DEVICE_NAME,chip))
597 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
598 chip->flags |= CHIP_IRQ_SETUP;
601 #ifdef CAN_ENABLE_VME_SUPPORT
602 if (chip->chip_irq < 1 || chip->chip_irq > 255) {
603 CANMSG("Bad irq parameter. (1 <= irq <= 255).\n");
607 request_vmeirq(chip->chip_irq, can_default_irq_dispatch, chip);
608 DEBUGMSG("Registered VME interrupt vector %d\n",chip->chip_irq);
609 chip->flags |= CHIP_IRQ_SETUP;
617 * can_chip_free_irq - unregisters chip interrupt handler from the system
618 * @chip: pointer to CAN chip structure
620 void can_chip_free_irq(struct chip_t *chip)
622 if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
623 if ((chip->flags & CHIP_IRQ_VME) == 0)
624 free_irq(chip->chip_irq, chip);
626 #ifdef CAN_ENABLE_VME_SUPPORT
627 free_vmeirq(chip->chip_irq);
630 chip->flags &= ~CHIP_IRQ_SETUP;
634 #endif /*CAN_WITH_RTL*/