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