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