]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/sysdep_lnx.c
LinCAN sources go through big white-space cleanup.
[lincan.git] / lincan / src / sysdep_lnx.c
1 /**************************************************************************/
2 /* File: sysdep_lnx.c - Linux system dependant support routines           */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #include "../include/can.h"
36 #include "../include/can_sysdep.h"
37 #include "../include/main.h"
38 #include "../include/devcommon.h"
39 #include "../include/setup.h"
40 #include "../include/finish.h"
41
42 #ifdef CAN_ENABLE_VME_SUPPORT
43 #include "ca91c042.h"
44 /* Modified version of ca91c042 driver can be found in
45  * components/comm/contrib directory. */
46 #endif
47
48 #ifndef IRQF_SHARED
49 #define IRQF_SHARED SA_SHIRQ
50 #endif  /*IRQF_SHARED*/
51
52 /**
53  * can_checked_malloc - memory allocation with registering of requested blocks
54  * @size: size of the requested block
55  *
56  * The function is used in the driver initialization phase to catch possible memory
57  * leaks for future driver finalization or case, that driver initialization fail.
58  *
59  * Return Value: pointer to the allocated memory or NULL in the case of fail
60  */
61 void *can_checked_malloc(size_t size)
62 {
63         struct mem_addr *mem_new;
64         void *address_p;
65
66         address_p=kmalloc(size,GFP_KERNEL);
67         if(address_p == NULL) {
68                 CANMSG("can_checked_malloc: out of the memory\n");
69                 return NULL;
70         }
71
72 #ifdef DEBUG_MEM
73         DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
74                         (int)size, address_p, mem_head);
75 #endif
76
77         mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
78         if (mem_new == NULL) {
79                 CANMSG("can_checked_malloc: memory list allocation error.\n");
80                 kfree(address_p);
81                 return NULL;
82         }
83         mem_new->next=mem_head;
84         mem_new->address=address_p;
85         mem_new->size=size;
86         mem_head=mem_new;
87
88         return address_p;
89 }
90
91 /**
92  * can_checked_free - free memory allocated by  can_checked_malloc()
93  * @address_p: pointer to the memory block
94  */
95 int can_checked_free(void *address_p)
96 {
97         struct mem_addr **mem_pptr;
98         struct mem_addr *mem_del=NULL;
99
100 #ifdef DEBUG_MEM
101         DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
102 #endif
103
104         for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
105                 if (mem_del->address != address_p)
106                         continue;
107                 *mem_pptr=mem_del->next;
108                 kfree(mem_del);
109                 kfree(address_p);
110                 return 0;
111         }
112
113         CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
114
115         kfree(address_p);
116         return -1;
117 }
118
119
120 /**
121  * can_del_mem_list - check for stale memory allocations at driver finalization
122  *
123  * Checks, if there are still some memory blocks allocated and releases memory
124  * occupied by such blocks back to the system
125  */
126 int can_del_mem_list(void)
127 {
128         struct mem_addr *mem;
129
130 #ifdef DEBUG_MEM
131         DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
132 #endif
133         if(mem_head == NULL) {
134                 CANMSG("can_del_mem_list: no entries on the list - OK\n");
135                 return 0;
136         }
137
138         while((mem=mem_head) != NULL) {
139                 mem_head=mem->next;
140                 CANMSG("can_del_mem_list: deleting %p with size %d\n",
141                         mem->address, (int)mem->size);
142                 kfree(mem->address);
143                 kfree(mem);
144         }
145
146         return 0;
147 }
148
149 /**
150  * can_request_io_region - request IO space region
151  * @start: the first IO port address
152  * @n: number of the consecutive IO port addresses
153  * @name: name/label for the requested region
154  *
155  * The function hides system specific implementation of the feature.
156  *
157  * Return Value: returns positive value (1) in the case, that region could
158  *      be reserved for the driver. Returns zero (0) if there is collision with
159  *      other driver or region cannot be taken for some other reason.
160  */
161 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
162 {
163     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
164         if(check_region(start,n)) return 0;
165         request_region(start,n,name);
166         return 1;
167     #else
168         return (request_region(start,n,name))?1:0;
169     #endif
170 }
171
172 /**
173  * can_release_io_region - release IO space region
174  * @start: the first IO port address
175  * @n: number of the consecutive IO port addresses
176  */
177 void can_release_io_region(unsigned long start, unsigned long n)
178 {
179         release_region(start,n);
180 }
181
182 /**
183  * can_request_mem_region - request memory space region
184  * @start: the first memory port physical address
185  * @n: number of the consecutive memory port addresses
186  * @name: name/label for the requested region
187  *
188  * The function hides system specific implementation of the feature.
189  *
190  * Return Value: returns positive value (1) in the case, that region could
191  *      be reserved for the driver. Returns zero (0) if there is collision with
192  *      other driver or region cannot be taken for some other reason.
193  */
194 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
195 {
196     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
197         return 1;
198     #else
199         return (request_mem_region(start,n,name))?1:0;
200     #endif
201 }
202
203 /**
204  * can_release_mem_region - release memory space region
205  * @start: the first memory port physical address
206  * @n: number of the consecutive memory port addresses
207  */
208 void can_release_mem_region(unsigned long start, unsigned long n)
209 {
210     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
211         return;
212     #else
213         release_mem_region(start,n);
214     #endif
215 }
216
217 #ifdef CAN_ENABLE_PCI_SUPPORT
218
219 struct pci_dev *can_pci_get_next_untaken_device(unsigned int vendor, unsigned int device)
220 {
221         struct pci_dev *pcidev = NULL;
222
223         do {
224                 pcidev = can_pci_get_device(vendor, device, pcidev);
225                 if(pcidev == NULL)
226                         return NULL;
227         } while(can_check_dev_taken(pcidev));
228
229         return pcidev;
230 }
231
232 struct pci_dev *can_pci_get_next_untaken_subsyst(unsigned int vendor, unsigned int device,
233                         unsigned int ss_vendor, unsigned int ss_device)
234 {
235         struct pci_dev *pcidev = NULL;
236
237         do {
238                 pcidev = can_pci_get_subsys(vendor, device, ss_vendor, ss_device, pcidev);
239                 if(pcidev == NULL)
240                         return NULL;
241         } while(can_check_dev_taken(pcidev));
242
243         return pcidev;
244 }
245
246 #endif /*CAN_ENABLE_PCI_SUPPORT*/
247
248 #ifndef CAN_WITH_RTL
249
250 /**
251  * can_default_irq_dispatch - the first level interrupt dispatch handler
252  * @irq: interrupt vector number, this value is system specific
253  * @dev_id: driver private pointer registered at time of request_irq() call.
254  *      The CAN driver uses this pointer to store relationship of interrupt
255  *      to chip state structure - @struct canchip_t
256  * @regs: system dependent value pointing to registers stored in exception frame
257  *
258  * File: src/setup.c
259  */
260 can_irqreturn_t can_default_irq_dispatch(CAN_IRQ_HANDLER_ARGS(irq_number, dev_id))
261 {
262         int retval;
263         struct canchip_t *chip=(struct canchip_t *)dev_id;
264
265         retval=chip->chipspecops->irq_handler(irq_number, chip);
266         return CAN_IRQ_RETVAL(retval);
267 }
268
269 /**
270  * can_chip_setup_irq - attaches chip to the system interrupt processing
271  * @chip: pointer to CAN chip structure
272  *
273  * Return Value: returns negative number in the case of fail
274  */
275 int can_chip_setup_irq(struct canchip_t *chip)
276 {
277         if(chip==NULL)
278                 return -1;
279         if(!chip->chipspecops->irq_handler)
280                 return 0;
281         if(chip->flags & CHIP_IRQ_CUSTOM)
282                 return 1;
283
284         if ((chip->flags & CHIP_IRQ_VME) == 0) {
285                 if (request_irq(chip->chip_irq,can_default_irq_dispatch,IRQF_SHARED,DEVICE_NAME,chip))
286                         return -1;
287                 else {
288                         DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
289                         chip->flags |= CHIP_IRQ_SETUP;
290                 }
291         } else {
292 #ifdef CAN_ENABLE_VME_SUPPORT
293                 if (chip->chip_irq < 1 || chip->chip_irq > 255) {
294                         CANMSG("Bad irq parameter. (1 <= irq <= 255).\n");
295                         return -EINVAL;
296                 }
297
298                 request_vmeirq(chip->chip_irq, can_default_irq_dispatch, chip);
299                 DEBUGMSG("Registered VME interrupt vector %d\n",chip->chip_irq);
300                 chip->flags |= CHIP_IRQ_SETUP;
301 #endif
302         }
303         return 1;
304 }
305
306
307 /**
308  * can_chip_free_irq - unregisters chip interrupt handler from the system
309  * @chip: pointer to CAN chip structure
310  */
311 void can_chip_free_irq(struct canchip_t *chip)
312 {
313         if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
314                 if(chip->flags & CHIP_IRQ_CUSTOM)
315                         return;
316
317                 if ((chip->flags & CHIP_IRQ_VME) == 0)
318                         free_irq(chip->chip_irq, chip);
319                 else {
320 #ifdef CAN_ENABLE_VME_SUPPORT
321                         free_vmeirq(chip->chip_irq);
322 #endif
323                 }
324                         chip->flags &= ~CHIP_IRQ_SETUP;
325         }
326 }
327
328 #endif /*CAN_WITH_RTL*/