]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
CAN driver infrastructure redesign to LinCAN-0.2 version
[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  * 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.2  9 Jul 2003
8  */
9
10 #define EXPORT_SYMTAB
11
12 #include <linux/autoconf.h>
13
14 #include <linux/module.h>
15
16 #include <linux/kernel.h>
17 #include <linux/fs.h>
18 #include <linux/sched.h>
19 #include <linux/poll.h>
20 #include <linux/version.h>
21 #include <linux/autoconf.h>
22 #include <linux/interrupt.h>
23
24 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
25 #include <linux/wrapper.h>
26 #endif
27
28 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
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 /*#undef CONFIG_DEVFS_FS*/
42
43 #ifdef CONFIG_DEVFS_FS
44 #include <linux/miscdevice.h>
45 #endif
46
47 #include "../include/main.h"
48 #include "../include/modparms.h"
49 #include "../include/devcommon.h"
50 #include "../include/setup.h"
51 #include "../include/proc.h"
52 #include "../include/open.h"
53 #include "../include/close.h"
54 #include "../include/read.h"
55 #include "../include/select.h"
56 #include "../include/irq.h"
57 #include "../include/ioctl.h"
58 #include "../include/write.h"
59
60 #define EXPORT_SYMTAB
61
62 /* Module parameters, some must be supplied at module loading time */
63 int major=CAN_MAJOR;
64 MODULE_PARM(major,"1i");
65 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};
66 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
67 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
68 int extended=0;
69 MODULE_PARM(extended,"1i");
70 int pelican=0;
71 MODULE_PARM(pelican,"1i");
72 int baudrate=0;
73 MODULE_PARM(baudrate,"1i");
74 char *hw[MAX_HW_CARDS]={NULL,};
75 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
76 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};
77 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
78 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
79 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
80 int stdmask=0;
81 MODULE_PARM(stdmask, "1i");
82 int extmask=0;
83 MODULE_PARM(extmask, "1i");
84 int mo15mask=0;
85 MODULE_PARM(mo15mask, "1i");
86
87 /* Other module attributes */
88 #ifdef MODULE_SUPPORTED_DEVICE
89 MODULE_SUPPORTED_DEVICE("can");
90 #endif
91 #ifdef MODULE_LICENSE
92 MODULE_LICENSE("GPL");
93 #endif
94 #ifdef MODULE_DESCRIPTION
95 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
96 #endif
97
98 /* Global structures, used to describe the installed hardware. */
99 struct canhardware_t canhardware;
100 struct canhardware_t *hardware_p=&canhardware;
101 struct chip_t *chips_p[MAX_TOT_CHIPS];
102 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
103 #ifdef CONFIG_DEVFS_FS
104 devfs_handle_t  devfs_handles[MAX_TOT_MSGOBJS];
105 #endif
106
107 /* Pointers to dynamically allocated memory are maintained in a linked list
108  * to ease memory deallocation.
109  */
110 struct mem_addr *mem_head=NULL;
111
112 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
113 struct file_operations can_fops=
114 {
115         NULL,                           /* llseek */
116         read:           can_read,
117         write:          can_write,
118         NULL,                           /* readdir */
119         poll:           can_poll,
120         ioctl:          can_ioctl,
121         NULL,                           /* mmap */
122         open:           can_open,
123         NULL,                           /* flush */
124         release:        can_close,
125         NULL,                           /* fsync */
126 }; 
127 #else 
128 struct file_operations can_fops=
129 {
130         owner:          THIS_MODULE,    
131         read:           can_read,
132         write:          can_write,
133         poll:           can_poll,
134         ioctl:          can_ioctl,
135         open:           can_open,
136         release:        can_close,
137 };
138 #endif
139
140 EXPORT_SYMBOL(can_fops);
141
142 int init_module(void)
143 {
144         int res=0,i=0;
145         struct candevice_t *candev;
146
147         if (parse_mod_parms())
148                 return -EINVAL;
149
150         if (init_hw_struct())
151                 return -ENODEV;
152
153         #ifdef CAN_DEBUG
154                 list_hw();
155         #endif
156
157         res=register_chrdev(major,DEVICE_NAME, &can_fops);
158         if (res<0) {
159                 CANMSG("Error registering driver.\n");
160                 return -ENODEV;
161         }
162
163         for (i=0; i<hardware_p->nr_boards; i++) {
164                 candev=hardware_p->candevice[i];
165                 if (candev->hwspecops->request_io(candev)) 
166                         goto memory_error;
167         }
168
169         for (i=0; i<hardware_p->nr_boards; i++) {
170                 candev=hardware_p->candevice[i];
171                 if (candev->hwspecops->reset(candev)) 
172                         goto reset_error;
173         }
174
175         spin_lock_init(&hardware_p->rtr_lock);
176         hardware_p->rtr_queue=NULL;
177
178         i=0;
179         while ( (chips_p[i] != NULL) && (i < MAX_TOT_CHIPS) ) {
180                 if (chips_p[i]->chipspecops->irq_handler) {
181                         if (request_irq(chips_p[i]->chip_irq,chips_p[i]->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chips_p[i]))
182                                 goto interrupt_error;
183                         else
184                                 DEBUGMSG("Registered interrupt %d\n",chips_p[i]->chip_irq);
185                 }
186                 i++;
187         }
188
189         for (i=0; i<hardware_p->nr_boards; i++) {
190                 candev=hardware_p->candevice[i];
191                 if (candev->flags & PROGRAMMABLE_IRQ)
192                         if (candev->hwspecops->program_irq(candev))
193                                 goto interrupt_error;
194         }
195
196 #ifdef CONFIG_PROC_FS
197         if (can_init_procdir())
198                 goto proc_error;
199 #endif
200
201 #ifdef CONFIG_DEVFS_FS
202         {
203                 char dev_name[32];
204                 int dev_minor;
205                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
206                         if(!objects_p[i]) continue;
207                         dev_minor=objects_p[i]->minor;
208                         sprintf (dev_name, "can%d", dev_minor);
209                         devfs_handles[i]=devfs_register(NULL, dev_name,
210                                 DEVFS_FL_DEFAULT, major, dev_minor,
211                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
212                                 &can_fops, (void*)objects_p[i]);
213                 }
214         }
215 #endif
216         return 0;
217
218 #ifdef CONFIG_PROC_FS
219         proc_error: ;
220                 CANMSG("Error registering /proc entry.\n");
221                 goto memory_error; 
222 #endif
223
224         interrupt_error: ;
225                 CANMSG("Error registering interrupt line.\n");
226                 goto memory_error;
227
228         reset_error: ;
229                 goto memory_error;
230
231         memory_error: ;
232                 for (i=0; i<hardware_p->nr_boards; i++) {
233                         candev=hardware_p->candevice[i];
234                         candev->hwspecops->release_io(candev);
235                 }
236                 goto register_error;
237
238         register_error: ;
239                 res=unregister_chrdev(major,DEVICE_NAME);
240                 if (res<0)
241                         CANMSG("Error unloading CAN driver, error: %d\n",res);
242                 else
243                         CANMSG("Successfully unloaded CAN driver.\n");
244                 return -ENODEV;
245
246 }
247
248 void cleanup_module(void)
249 {
250         int res=0,i=0;
251         struct candevice_t *candev;
252
253 #ifdef CONFIG_PROC_FS
254         if (can_delete_procdir())
255                 CANMSG("Error unregistering /proc/can entry.\n"); 
256 #endif
257
258 #ifdef CONFIG_DEVFS_FS
259         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
260                 if(devfs_handles[i])
261                         devfs_unregister(devfs_handles[i]);
262         }
263 #endif
264         i=0;
265         while ( (chips_p[i] != NULL) & (i < MAX_TOT_CHIPS) ) {
266                 free_irq(chips_p[i]->chip_irq, chips_p[i]);
267                 i++;
268         }
269
270         for (i=0; i<hardware_p->nr_boards; i++){ 
271                 candev=hardware_p->candevice[i];
272                 candev->hwspecops->release_io(candev);
273         }
274
275         if ( del_mem_list() ) 
276                 CANMSG("Error deallocating memory\n");
277
278         res=unregister_chrdev(major,DEVICE_NAME);
279         if (res<0)
280                 CANMSG("Error unregistering CAN driver, error: %d\n",res);
281 }