]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
The original version of Arnaud Westenberg Linux CAN-bus driver
[lincan.git] / lincan / src / main.c
1 /* main.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * This software is released under the GPL-License.
5  * Version 0.7  6 Aug 2001
6  */
7
8 #define EXPORT_SYMTAB
9
10 #include <linux/autoconf.h>
11 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
12 #define MODVERSIONS
13 #endif
14
15 #include <linux/module.h>
16
17 #if defined (MODVERSIONS)
18 #include <linux/modversions.h>
19 #endif
20
21 #include <linux/kernel.h>
22 #include <linux/fs.h>
23 #include <linux/wrapper.h>
24 #include <linux/sched.h>
25 #include <linux/version.h>
26 #include <linux/autoconf.h>
27
28 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,2,19))
29 #include <asm/spinlock.h>
30 #else
31 #include <linux/spinlock.h>
32 #endif
33
34 #if !defined (__GENKSYMS__) 
35 #if (defined (MODVERSIONS) && !defined(NOVER))
36 #include <linux/modversions.h>
37 #include "../include/main.ver"
38 #endif
39 #endif
40
41 #include "../include/main.h"
42 #include "../include/modparms.h"
43 #include "../include/setup.h"
44 #include "../include/proc.h"
45 #include "../include/open.h"
46 #include "../include/close.h"
47 #include "../include/read.h"
48 #include "../include/irq.h"
49 #include "../include/ioctl.h"
50 #include "../include/write.h"
51
52 #define EXPORT_SYMTAB
53
54 /* Module parameters, some must be supplied at module loading time */
55 int major=CAN_MAJOR;
56 MODULE_PARM(major,"1i");
57 int minor[MAX_TOT_CHIPS]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
58 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");
59 int extended=0;
60 MODULE_PARM(extended,"1i");
61 int pelican=0;
62 MODULE_PARM(pelican,"1i");
63 int baudrate=0;
64 MODULE_PARM(baudrate,"1i");
65 char *hw[MAX_HW_CARDS]={NULL,};
66 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
67 int irq[MAX_IRQ]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
68 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
69 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
70 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
71 int stdmask=0;
72 MODULE_PARM(stdmask, "1i");
73 int extmask=0;
74 MODULE_PARM(extmask, "1i");
75 int mo15mask=0;
76 MODULE_PARM(mo15mask, "1i");
77
78 /* Global structures, used to describe the installed hardware. */
79 struct canhardware_t canhardware;
80 struct canhardware_t *hardware_p=&canhardware;
81 struct candevice_t *candevices_p[MAX_HW_CARDS];
82 struct chip_t *chips_p[MAX_TOT_CHIPS];
83 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
84
85 /* Pointers to dynamically allocated memory are maintained in a linked list
86  * to ease memory deallocation.
87  */
88 struct mem_addr *mem_head=NULL;
89
90 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,2,19))
91 struct file_operations can_fops=
92 {
93         NULL,                           /* llseek */
94         read:           can_read,
95         write:          can_write,
96         NULL,                           /* readdir */
97         NULL,                           /* poll */
98         ioctl:          can_ioctl,
99         NULL,                           /* mmap */
100         open:           can_open,
101         NULL,                           /* flush */
102         release:        can_close,
103         NULL,                           /* fsync */
104 }; 
105 #else 
106 struct file_operations can_fops=
107 {
108         owner:          THIS_MODULE,    
109         read:           can_read,
110         write:          can_write,
111         ioctl:          can_ioctl,
112         open:           can_open,
113         release:        can_close,
114 };
115 #endif
116
117 EXPORT_SYMBOL(can_fops);
118
119 int init_module(void)
120 {
121         int res=0,i=0;
122
123         if (parse_mod_parms())
124                 return -EINVAL;
125
126         if (init_hw_struct())
127                 return -ENODEV;
128
129         #ifdef CAN_DEBUG
130                 list_hw();
131         #endif
132
133         res=register_chrdev(major,DEVICE_NAME, &can_fops);
134         if (res<0) {
135                 CANMSG("Error registering driver.\n");
136                 return -ENODEV;
137         }
138
139         for (i=0; i<hardware_p->nr_boards; i++) {
140                 if (candevices_p[i]->hwspecops->request_io(candevices_p[i]->io_addr)) 
141                 goto memory_error;
142         }
143
144         for (i=0; i<hardware_p->nr_boards; i++) {
145                 if (candevices_p[i]->hwspecops->reset(i)) 
146                         goto reset_error;
147         }
148
149         i=0;
150         while ( (chips_p[i] != NULL) && (i < MAX_TOT_CHIPS) ) {
151                 if (!strcmp(chips_p[i]->chip_type,"i82527")) {
152                         if (request_irq(chips_p[i]->chip_irq,i82527_irq_handler,SA_SHIRQ,DEVICE_NAME,chips_p[i]))  
153                                 goto interrupt_error;
154                         else
155                                 DEBUGMSG("Registered interrupt %d\n",chips_p[i]->chip_irq);
156                 }
157                 if (!strcmp(chips_p[i]->chip_type,"sja1000p") ||  
158                                 !strcmp(chips_p[i]->chip_type,"sja1000")) {
159                         if (request_irq(chips_p[i]->chip_irq,
160         chips_p[i]->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chips_p[i]))
161                                 goto interrupt_error;
162                         else
163                                 DEBUGMSG("Registered interrupt %d\n",chips_p[i]->chip_irq);
164                 }
165                 i++;
166         }
167
168         for (i=0; i<hardware_p->nr_boards; i++) {
169                 if (candevices_p[i]->flags & PROGRAMMABLE_IRQ)
170                         if (candevices_p[i]->hwspecops->program_irq(i))
171                                 goto interrupt_error;
172         }
173
174         spin_lock_init(&hardware_p->rtr_lock);
175         hardware_p->rtr_queue=NULL;
176
177 #ifdef CONFIG_PROC_FS
178         if (can_init_procdir())
179                 goto proc_error;
180 #endif
181
182         return 0;
183
184 #ifdef CONFIG_PROC_FS
185         proc_error: ;
186                 CANMSG("Error registering /proc entry.\n");
187                 goto memory_error; 
188 #endif
189
190         interrupt_error: ;
191                 CANMSG("Error registering interrupt line.\n");
192                 goto memory_error;
193
194         reset_error: ;
195                 goto memory_error;
196
197         memory_error: ;
198                 for (i=0; i<hardware_p->nr_boards; i++)
199                         candevices_p[i]->hwspecops->release_io(candevices_p[i]->io_addr);
200                 goto register_error;
201
202         register_error: ;
203                 res=unregister_chrdev(major,DEVICE_NAME);
204                 if (res<0)
205                         CANMSG("Error unloading CAN driver, error: %d\n",res);
206                 else
207                         CANMSG("Successfully unloaded CAN driver.\n");
208                 return -ENODEV;
209
210 }
211
212 void cleanup_module(void)
213 {
214         int res=0,i=0;
215
216 #ifdef CONFIG_PROC_FS
217         if (can_delete_procdir())
218                 CANMSG("Error unregistering /proc/can entry.\n"); 
219 #endif
220
221         while ( (chips_p[i] != NULL) & (i < MAX_TOT_CHIPS) ) {
222                 free_irq(chips_p[i]->chip_irq, chips_p[i]);
223                 i++;
224         }
225
226         for (i=0; i<hardware_p->nr_boards; i++) 
227                 candevices_p[i]->hwspecops->release_io(candevices_p[i]->io_addr);
228
229         if ( del_mem_list() ) 
230                 CANMSG("Error deallocating memory\n");
231
232         res=unregister_chrdev(major,DEVICE_NAME);
233         if (res<0)
234                 CANMSG("Error unregistering CAN driver, error: %d\n",res);
235 }