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 int init_hwspecops(struct candevice_t *candev, int *irqnum_p);
18 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p);
19 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate);
20 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr);
23 * can_checked_malloc - memory allocation with registering of requested blocks
24 * @size: size of the requested block
26 * The function is used in the driver initialization phase to catch possible memory
27 * leaks for future driver finalization or case, that driver initialization fail.
29 * Return Value: pointer to the allocated memory or NULL in the case of fail
31 void *can_checked_malloc(size_t size)
33 struct mem_addr *mem_new;
36 address_p=kmalloc(size,GFP_KERNEL);
37 if(address_p == NULL) {
38 CANMSG("can_checked_malloc: out of the memory\n");
43 DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
44 (int)size, address_p, mem_head);
47 mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
48 if (mem_new == NULL) {
49 CANMSG("can_checked_malloc: memory list allocation error.\n");
53 mem_new->next=mem_head;
54 mem_new->address=address_p;
62 * can_checked_free - free memory allocated by can_checked_malloc()
63 * @address_p: pointer to the memory block
65 int can_checked_free(void *address_p)
67 struct mem_addr **mem_pptr;
68 struct mem_addr *mem_del=NULL;
71 DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
74 for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
75 if (mem_del->address != address_p)
77 *mem_pptr=mem_del->next;
83 CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
91 * can_del_mem_list - check for stale memory allocations at driver finalization
93 * Checks, if there are still some memory blocks allocated and releases memory
94 * occupied by such blocks back to the system
96 int can_del_mem_list(void)
101 DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
103 if(mem_head == NULL) {
104 CANMSG("can_del_mem_list: no entries on the list - OK\n");
108 while((mem=mem_head) != NULL) {
110 CANMSG("can_del_mem_list: deleting %p with size %d\n",
111 mem->address, (int)mem->size);
120 * can_request_io_region - request IO space region
121 * @start: the first IO port address
122 * @n: number of the consecutive IO port addresses
123 * @name: name/label for the requested region
125 * The function hides system specific implementation of the feature.
127 * Return Value: returns positive value (1) in the case, that region could
128 * be reserved for the driver. Returns zero (0) if there is collision with
129 * other driver or region cannot be taken for some other reason.
131 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
133 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
134 if(check_region(start,n)) return 0;
135 request_region(start,n,name);
138 return (request_region(start,n,name))?1:0;
143 * can_release_io_region - release IO space region
144 * @start: the first IO port address
145 * @n: number of the consecutive IO port addresses
147 void can_release_io_region(unsigned long start, unsigned long n)
149 release_region(start,n);
153 * can_request_mem_region - request memory space region
154 * @start: the first memory port physical address
155 * @n: number of the consecutive memory port addresses
156 * @name: name/label for the requested region
158 * The function hides system specific implementation of the feature.
160 * Return Value: returns positive value (1) in the case, that region could
161 * be reserved for the driver. Returns zero (0) if there is collision with
162 * other driver or region cannot be taken for some other reason.
164 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
166 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
169 return (request_mem_region(start,n,name))?1:0;
174 * can_release_mem_region - release memory space region
175 * @start: the first memory port physical address
176 * @n: number of the consecutive memory port addresses
178 void can_release_mem_region(unsigned long start, unsigned long n)
180 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
183 release_mem_region(start,n);
188 * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
189 * @candev: pointer to the previously filled device/board, chips and message objects structures
190 * @new_base: @candev new base address
192 * This function adapts base addresses of all structures of one board
193 * to the new board base address.
194 * It is required for translation between physical and virtual address mappings.
195 * This function is prepared to simplify board specific xxx_request_io() function
196 * for memory mapped devices.
198 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
203 offs=new_base-candev->dev_base_addr;
204 candev->dev_base_addr=new_base;
205 for(i=0;i<candev->nr_all_chips;i++){
206 candev->chip[i]->chip_base_addr += offs;
207 for(j=0;j<candev->chip[i]->max_objects;j++)
208 candev->chip[i]->msgobj[j]->obj_base_addr += offs;
214 * can_check_dev_taken - checks if bus device description is already taken by driver
215 * @anydev: pointer to bus specific Linux device description
217 * Returns: Returns 1 if device is already used by LinCAN driver, 0 otherwise.
219 int can_check_dev_taken(void *anydev)
222 struct candevice_t *candev;
225 for (board_nr=hardware_p->nr_boards; board_nr--; ) {
226 if((candev=hardware_p->candevice[board_nr])==NULL)
228 boarddev=candev->sysdevptr.anydev;
229 if(boarddev == anydev)
238 * register_obj_struct - registers message object into global array
239 * @obj: the initialized message object being registered
240 * @minorbase: wanted minor number, if (-1) automatically selected
242 * Return Value: returns negative number in the case of fail
244 int register_obj_struct(struct msgobj_t *obj, int minorbase)
246 static int next_minor=0;
250 next_minor=minorbase;
251 if(next_minor>=MAX_TOT_MSGOBJS)
255 if(objects_p[i]==NULL){
261 if(++i >= MAX_TOT_MSGOBJS) i=0;
262 }while(i!=next_minor);
269 * register_chip_struct - registers chip into global array
270 * @chip: the initialized chip structure being registered
271 * @minorbase: wanted minor number base, if (-1) automatically selected
273 * Return Value: returns negative number in the case of fail
275 int register_chip_struct(struct chip_t *chip, int minorbase)
277 static int next_chip_slot=0;
280 if(next_chip_slot>=MAX_TOT_CHIPS)
284 if(chips_p[i]==NULL){
290 if(++i >= MAX_TOT_CHIPS) i=0;
291 }while(i!=next_chip_slot);
298 * init_hw_struct - initializes driver hardware description structures
300 * The function init_hw_struct() is used to initialize the hardware structure.
302 * Return Value: returns negative number in the case of fail
304 int init_hw_struct(void)
308 int chan_param_idx=0;
310 hardware_p->nr_boards=0;
311 while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
312 hardware_p->nr_boards++;
314 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
315 CANMSG("Error initializing candevice_t structures.\n");
325 * init_device_struct - initializes single CAN device/board
326 * @card: index into @hardware_p HW description
327 * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
328 * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
330 * The function builds representation of the one board from parameters provided
331 * in the module parameters arrays:
332 * @hw[card] .. hardware type,
333 * @io[card] .. base IO address,
334 * @baudrate[chan_param_idx] .. per channel baudrate,
335 * @minor[chan_param_idx] .. optional specification of requested channel minor base,
336 * @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
337 * The indexes are advanced after consumed parameters if the registration is successful.
339 * The hardware specific operations of the device/board are initialized by call to
340 * init_hwspecops() function. Then board data are initialized by board specific
341 * init_hw_data() function. Then chips and objects representation is build by
342 * init_chip_struct() function. If all above steps are successful, chips and
343 * message objects are registered into global arrays.
345 * Return Value: returns negative number in the case of fail
347 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
349 struct candevice_t *candev;
356 candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
360 memset(candev, 0, sizeof(struct candevice_t));
362 hardware_p->candevice[card]=candev;
363 candev->candev_idx=card;
367 candev->hwname=hw[card];
368 candev->io_addr=io[card];
369 candev->dev_base_addr=io[card];
371 candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
372 if (candev->hwspecops==NULL)
375 memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
377 if (init_hwspecops(candev, &irqnum))
380 if (candev->hwspecops->init_hw_data(candev))
383 /* Alocate and initialize the chip structures */
384 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
387 irqsig=irq[*irq_param_idx_p+chipnr];
389 bd=baudrate[*chan_param_idx_p+chipnr];
390 if(!bd) bd=baudrate[0];
392 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000)))
398 for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
399 int m=minor[*chan_param_idx_p+chipnr];
400 struct chip_t *chip=candev->chip[chipnr];
403 register_chip_struct(chip, m);
405 for (objnr=0; objnr<chip->max_objects; objnr++) {
406 register_obj_struct(chip->msgobj[objnr], m);
411 *irq_param_idx_p += irqnum;
412 *chan_param_idx_p += candev->nr_all_chips;
419 candevice_done(candev);
426 hardware_p->candevice[card]=NULL;
427 can_checked_free(candev);
433 * init_chip_struct - initializes one CAN chip structure
434 * @candev: pointer to the corresponding CAN device/board
435 * @chipnr: index of the chip in the corresponding device/board structure
436 * @irq: chip IRQ number or (-1) if not appropriate
437 * @baudrate: baudrate in the units of 1Bd
439 * Chip structure is allocated and chip specific operations are filled by
440 * call to board specific init_chip_data() which calls chip specific
441 * fill_chipspecops(). The message objects are generated by
442 * calls to init_obj_struct() function.
444 * Return Value: returns negative number in the case of fail
446 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate)
452 candev->chip[chipnr]=(struct chip_t *)can_checked_malloc(sizeof(struct chip_t));
453 if ((chip=candev->chip[chipnr])==NULL)
456 memset(chip, 0, sizeof(struct chip_t));
458 chip->write_register=candev->hwspecops->write_register;
459 chip->read_register=candev->hwspecops->read_register;
461 chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
462 if (chip->chipspecops==NULL)
464 memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
466 chip->chip_idx=chipnr;
467 chip->hostdevice=candev;
469 chip->baudrate=baudrate;
472 if(candev->hwspecops->init_chip_data(candev,chipnr)<0)
475 for (objnr=0; objnr<chip->max_objects; objnr++) {
476 ret=init_obj_struct(candev, chip, objnr);
477 if(ret<0) return ret;
485 * init_obj_struct - initializes one CAN message object structure
486 * @candev: pointer to the corresponding CAN device/board
487 * @hostchip: pointer to the chip containing this object
488 * @objnr: index of the builded object in the chip structure
490 * The function initializes message object structure and allocates and initializes
491 * CAN queue chip ends structure.
493 * Return Value: returns negative number in the case of fail
495 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr)
497 struct canque_ends_t *qends;
498 struct msgobj_t *obj;
501 obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
502 hostchip->msgobj[objnr]=obj;
506 memset(obj, 0, sizeof(struct msgobj_t));
509 atomic_set(&obj->obj_used,0);
510 INIT_LIST_HEAD(&obj->obj_users);
511 init_timer(&obj->tx_timeout);
513 qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
514 if(qends == NULL) return -ENOMEM;
515 memset(qends, 0, sizeof(struct canque_ends_t));
516 obj->hostchip=hostchip;
521 obj->obj_flags = 0x0;
523 ret=canqueue_ends_init_chip(qends, hostchip, obj);
524 if(ret<0) return ret;
526 ret=candev->hwspecops->init_obj_data(hostchip,objnr);
527 if(ret<0) return ret;
534 * init_hwspecops - finds and initializes board/device specific operations
535 * @candev: pointer to the corresponding CAN device/board
536 * @irqnum_p: optional pointer to the number of interrupts required by board
538 * The function searches board @hwname in the list of supported boards types.
539 * The board type specific board_register() function is used to initialize
540 * @hwspecops operations.
542 * Return Value: returns negative number in the case of fail
544 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
546 const struct boardtype_t *brp;
548 brp = boardtype_find(candev->hwname);
551 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
556 *irqnum_p=brp->irqnum;
557 brp->board_register(candev->hwspecops);
566 * can_chip_setup_irq - attaches chip to the system interrupt processing
567 * @chip: pointer to CAN chip structure
569 * Return Value: returns negative number in the case of fail
571 int can_chip_setup_irq(struct chip_t *chip)
575 if(!chip->chipspecops->irq_handler)
578 if ((chip->flags & CHIP_IRQ_VME) == 0) {
579 if (request_irq(chip->chip_irq,chip->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chip))
582 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
583 chip->flags |= CHIP_IRQ_SETUP;
586 #ifdef CAN_ENABLE_VME_SUPPORT
587 /* TODO: Move here the irq setup from
588 * unican_vme_request_io(). To do this, the VME bridge
589 * driver should be modified. */
597 * can_chip_free_irq - unregisters chip interrupt handler from the system
598 * @chip: pointer to CAN chip structure
600 void can_chip_free_irq(struct chip_t *chip)
602 if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
603 if ((chip->flags & CHIP_IRQ_VME) == 0)
604 free_irq(chip->chip_irq, chip);
606 #ifdef CAN_ENABLE_VME_SUPPORT
607 /* TODO: Move here the irq cleanup from
608 * unican_vme_release_io(). To do this, the VME bridge
609 * driver should be modified. */
612 chip->flags &= ~CHIP_IRQ_SETUP;
616 #endif /*CAN_WITH_RTL*/