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. */
24 #define IRQF_SHARED SA_SHIRQ
25 #endif /*IRQF_SHARED*/
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);
195 * can_default_irq_dispatch - the first level interrupt dispatch handler
196 * @irq: interrupt vector number, this value is system specific
197 * @dev_id: driver private pointer registered at time of request_irq() call.
198 * The CAN driver uses this pointer to store relationship of interrupt
199 * to chip state structure - @struct canchip_t
200 * @regs: system dependent value pointing to registers stored in exception frame
204 can_irqreturn_t can_default_irq_dispatch(CAN_IRQ_HANDLER_ARGS(irq_number, dev_id))
207 struct canchip_t *chip=(struct canchip_t *)dev_id;
209 retval=chip->chipspecops->irq_handler(irq_number, chip);
210 return CAN_IRQ_RETVAL(retval);
214 * can_chip_setup_irq - attaches chip to the system interrupt processing
215 * @chip: pointer to CAN chip structure
217 * Return Value: returns negative number in the case of fail
219 int can_chip_setup_irq(struct canchip_t *chip)
223 if(!chip->chipspecops->irq_handler)
225 if(chip->flags & CHIP_IRQ_CUSTOM)
228 if ((chip->flags & CHIP_IRQ_VME) == 0) {
229 if (request_irq(chip->chip_irq,can_default_irq_dispatch,IRQF_SHARED,DEVICE_NAME,chip))
232 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
233 chip->flags |= CHIP_IRQ_SETUP;
236 #ifdef CAN_ENABLE_VME_SUPPORT
237 if (chip->chip_irq < 1 || chip->chip_irq > 255) {
238 CANMSG("Bad irq parameter. (1 <= irq <= 255).\n");
242 request_vmeirq(chip->chip_irq, can_default_irq_dispatch, chip);
243 DEBUGMSG("Registered VME interrupt vector %d\n",chip->chip_irq);
244 chip->flags |= CHIP_IRQ_SETUP;
252 * can_chip_free_irq - unregisters chip interrupt handler from the system
253 * @chip: pointer to CAN chip structure
255 void can_chip_free_irq(struct canchip_t *chip)
257 if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
258 if(chip->flags & CHIP_IRQ_CUSTOM)
261 if ((chip->flags & CHIP_IRQ_VME) == 0)
262 free_irq(chip->chip_irq, chip);
264 #ifdef CAN_ENABLE_VME_SUPPORT
265 free_vmeirq(chip->chip_irq);
268 chip->flags &= ~CHIP_IRQ_SETUP;
272 #endif /*CAN_WITH_RTL*/