]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/sysdep_lnx.c
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[lincan.git] / lincan / src / sysdep_lnx.c
1 /* sysdep_lnx.c
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
8  */ 
9
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"
16
17 #ifdef CAN_ENABLE_VME_SUPPORT
18 #include "ca91c042.h"
19 /* Modified version of ca91c042 driver can be found in
20  * components/comm/contrib directory. */
21 #endif
22
23 #ifndef IRQF_SHARED
24 #define IRQF_SHARED SA_SHIRQ
25 #endif  /*IRQF_SHARED*/
26
27 /**
28  * can_checked_malloc - memory allocation with registering of requested blocks
29  * @size: size of the requested block
30  *
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.
33  * 
34  * Return Value: pointer to the allocated memory or NULL in the case of fail
35  */
36 void *can_checked_malloc(size_t size)
37 {
38         struct mem_addr *mem_new;
39         void *address_p;
40         
41         address_p=kmalloc(size,GFP_KERNEL);
42         if(address_p == NULL) {
43                 CANMSG("can_checked_malloc: out of the memory\n");
44                 return NULL;
45         }
46
47 #ifdef DEBUG_MEM
48         DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
49                         (int)size, address_p, mem_head);
50 #endif
51
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");
55                 kfree(address_p);
56                 return NULL;
57         }
58         mem_new->next=mem_head;
59         mem_new->address=address_p;
60         mem_new->size=size;
61         mem_head=mem_new;
62
63         return address_p;
64 }
65
66 /**
67  * can_checked_free - free memory allocated by  can_checked_malloc()
68  * @address_p: pointer to the memory block
69  */
70 int can_checked_free(void *address_p)
71 {
72         struct mem_addr **mem_pptr;
73         struct mem_addr *mem_del=NULL;
74
75 #ifdef DEBUG_MEM
76         DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
77 #endif
78
79         for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
80                 if (mem_del->address != address_p)
81                         continue;
82                 *mem_pptr=mem_del->next;
83                 kfree(mem_del);
84                 kfree(address_p);
85                 return 0;
86         }
87         
88         CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
89         
90         kfree(address_p);
91         return -1;
92 }
93
94
95 /**
96  * can_del_mem_list - check for stale memory allocations at driver finalization
97  *
98  * Checks, if there are still some memory blocks allocated and releases memory
99  * occupied by such blocks back to the system
100  */
101 int can_del_mem_list(void)
102 {
103         struct mem_addr *mem;
104
105 #ifdef DEBUG_MEM
106         DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
107 #endif
108         if(mem_head == NULL) {
109                 CANMSG("can_del_mem_list: no entries on the list - OK\n");
110                 return 0;
111         }
112
113         while((mem=mem_head) != NULL) {
114                 mem_head=mem->next;
115                 CANMSG("can_del_mem_list: deleting %p with size %d\n",
116                         mem->address, (int)mem->size);
117                 kfree(mem->address);
118                 kfree(mem);
119         }
120         
121         return 0;
122 }
123
124 /**
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
129  *
130  * The function hides system specific implementation of the feature.
131  *
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.
135  */
136 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
137 {
138     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
139         if(check_region(start,n)) return 0;
140         request_region(start,n,name);
141         return 1;
142     #else
143         return (request_region(start,n,name))?1:0;
144     #endif
145 }
146
147 /**
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
151  */
152 void can_release_io_region(unsigned long start, unsigned long n)
153 {
154         release_region(start,n);
155 }
156
157 /**
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
162  *
163  * The function hides system specific implementation of the feature.
164  *
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.
168  */
169 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
170 {
171     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
172         return 1;
173     #else
174         return (request_mem_region(start,n,name))?1:0;
175     #endif
176 }
177
178 /**
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
182  */
183 void can_release_mem_region(unsigned long start, unsigned long n)
184 {
185     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
186         return;
187     #else
188         release_mem_region(start,n);
189     #endif
190 }
191
192 #ifndef CAN_WITH_RTL
193
194 /**
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
201  * 
202  * File: src/setup.c
203  */
204 can_irqreturn_t can_default_irq_dispatch(CAN_IRQ_HANDLER_ARGS(irq_number, dev_id))
205 {
206         int retval;
207         struct canchip_t *chip=(struct canchip_t *)dev_id;
208
209         retval=chip->chipspecops->irq_handler(irq_number, chip);
210         return CAN_IRQ_RETVAL(retval);
211 }
212
213 /**
214  * can_chip_setup_irq - attaches chip to the system interrupt processing
215  * @chip: pointer to CAN chip structure
216  *
217  * Return Value: returns negative number in the case of fail
218  */
219 int can_chip_setup_irq(struct canchip_t *chip)
220 {
221         if(chip==NULL)
222                 return -1;
223         if(!chip->chipspecops->irq_handler)
224                 return 0;
225         if(chip->flags & CHIP_IRQ_CUSTOM)
226                 return 1;
227                         
228         if ((chip->flags & CHIP_IRQ_VME) == 0) {
229                 if (request_irq(chip->chip_irq,can_default_irq_dispatch,IRQF_SHARED,DEVICE_NAME,chip))
230                         return -1;
231                 else {
232                         DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
233                         chip->flags |= CHIP_IRQ_SETUP;
234                 }
235         } else {
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");
239                         return -EINVAL;
240                 }
241                 
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;
245 #endif
246         }
247         return 1;
248 }
249
250
251 /**
252  * can_chip_free_irq - unregisters chip interrupt handler from the system
253  * @chip: pointer to CAN chip structure
254  */
255 void can_chip_free_irq(struct canchip_t *chip)
256 {
257         if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
258                 if(chip->flags & CHIP_IRQ_CUSTOM)
259                         return;
260
261                 if ((chip->flags & CHIP_IRQ_VME) == 0)
262                         free_irq(chip->chip_irq, chip);
263                 else { 
264 #ifdef CAN_ENABLE_VME_SUPPORT
265                         free_vmeirq(chip->chip_irq);
266 #endif
267                 }
268                         chip->flags &= ~CHIP_IRQ_SETUP;
269         }
270 }
271
272 #endif /*CAN_WITH_RTL*/