1 /**************************************************************************/
2 /* File: sysdep_lnx.c - Linux system dependant support routines */
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> */
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. */
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: */
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 /**************************************************************************/
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"
42 #ifdef CAN_ENABLE_VME_SUPPORT
44 /* Modified version of ca91c042 driver can be found in
45 * components/comm/contrib directory. */
49 #define IRQF_SHARED SA_SHIRQ
50 #endif /*IRQF_SHARED*/
53 * can_checked_malloc - memory allocation with registering of requested blocks
54 * @size: size of the requested block
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.
59 * Return Value: pointer to the allocated memory or NULL in the case of fail
61 void *can_checked_malloc(size_t size)
63 struct mem_addr *mem_new;
66 address_p=kmalloc(size,GFP_KERNEL);
67 if(address_p == NULL) {
68 CANMSG("can_checked_malloc: out of the memory\n");
73 DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
74 (int)size, address_p, mem_head);
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");
83 mem_new->next=mem_head;
84 mem_new->address=address_p;
92 * can_checked_free - free memory allocated by can_checked_malloc()
93 * @address_p: pointer to the memory block
95 int can_checked_free(void *address_p)
97 struct mem_addr **mem_pptr;
98 struct mem_addr *mem_del=NULL;
101 DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
104 for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
105 if (mem_del->address != address_p)
107 *mem_pptr=mem_del->next;
113 CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
121 * can_del_mem_list - check for stale memory allocations at driver finalization
123 * Checks, if there are still some memory blocks allocated and releases memory
124 * occupied by such blocks back to the system
126 int can_del_mem_list(void)
128 struct mem_addr *mem;
131 DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
133 if(mem_head == NULL) {
134 CANMSG("can_del_mem_list: no entries on the list - OK\n");
138 while((mem=mem_head) != NULL) {
140 CANMSG("can_del_mem_list: deleting %p with size %d\n",
141 mem->address, (int)mem->size);
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
155 * The function hides system specific implementation of the feature.
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.
161 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
163 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
164 if(check_region(start,n)) return 0;
165 request_region(start,n,name);
168 return (request_region(start,n,name))?1:0;
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
177 void can_release_io_region(unsigned long start, unsigned long n)
179 release_region(start,n);
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
188 * The function hides system specific implementation of the feature.
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.
194 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
196 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
199 return (request_mem_region(start,n,name))?1:0;
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
208 void can_release_mem_region(unsigned long start, unsigned long n)
210 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
213 release_mem_region(start,n);
220 * can_default_irq_dispatch - the first level interrupt dispatch handler
221 * @irq: interrupt vector number, this value is system specific
222 * @dev_id: driver private pointer registered at time of request_irq() call.
223 * The CAN driver uses this pointer to store relationship of interrupt
224 * to chip state structure - @struct canchip_t
225 * @regs: system dependent value pointing to registers stored in exception frame
229 can_irqreturn_t can_default_irq_dispatch(CAN_IRQ_HANDLER_ARGS(irq_number, dev_id))
232 struct canchip_t *chip=(struct canchip_t *)dev_id;
234 retval=chip->chipspecops->irq_handler(irq_number, chip);
235 return CAN_IRQ_RETVAL(retval);
239 * can_chip_setup_irq - attaches chip to the system interrupt processing
240 * @chip: pointer to CAN chip structure
242 * Return Value: returns negative number in the case of fail
244 int can_chip_setup_irq(struct canchip_t *chip)
248 if(!chip->chipspecops->irq_handler)
250 if(chip->flags & CHIP_IRQ_CUSTOM)
253 if ((chip->flags & CHIP_IRQ_VME) == 0) {
254 if (request_irq(chip->chip_irq,can_default_irq_dispatch,IRQF_SHARED,DEVICE_NAME,chip))
257 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
258 chip->flags |= CHIP_IRQ_SETUP;
261 #ifdef CAN_ENABLE_VME_SUPPORT
262 if (chip->chip_irq < 1 || chip->chip_irq > 255) {
263 CANMSG("Bad irq parameter. (1 <= irq <= 255).\n");
267 request_vmeirq(chip->chip_irq, can_default_irq_dispatch, chip);
268 DEBUGMSG("Registered VME interrupt vector %d\n",chip->chip_irq);
269 chip->flags |= CHIP_IRQ_SETUP;
277 * can_chip_free_irq - unregisters chip interrupt handler from the system
278 * @chip: pointer to CAN chip structure
280 void can_chip_free_irq(struct canchip_t *chip)
282 if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
283 if(chip->flags & CHIP_IRQ_CUSTOM)
286 if ((chip->flags & CHIP_IRQ_VME) == 0)
287 free_irq(chip->chip_irq, chip);
289 #ifdef CAN_ENABLE_VME_SUPPORT
290 free_vmeirq(chip->chip_irq);
293 chip->flags &= ~CHIP_IRQ_SETUP;
297 #endif /*CAN_WITH_RTL*/