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