]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
b57c79c87b53e3b00795bd2e28c53d11095be842
[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 #ifdef MODULE_AUTHOR
106 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
107 #endif
108
109 /* Global structures, used to describe the installed hardware. */
110 struct canhardware_t canhardware;
111 struct canhardware_t *hardware_p=&canhardware;
112 struct chip_t *chips_p[MAX_TOT_CHIPS];
113 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
114 #ifdef CONFIG_DEVFS_FS
115 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,60))
116 devfs_handle_t  devfs_handles[MAX_TOT_MSGOBJS];
117 #endif
118 #endif
119
120 /* Pointers to dynamically allocated memory are maintained in a linked list
121  * to ease memory deallocation.
122  */
123 struct mem_addr *mem_head=NULL;
124
125 struct file_operations can_fops=
126 {
127  #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
128         owner:          THIS_MODULE,    
129  #endif
130         read:           can_read,
131         write:          can_write,
132         poll:           can_poll,
133         ioctl:          can_ioctl,
134         open:           can_open,
135         release:        can_close,
136   #ifdef CAN_ENABLE_KERN_FASYNC
137         fasync:         can_fasync
138   #endif /*CAN_ENABLE_KERN_FASYNC*/
139 };
140
141 EXPORT_SYMBOL(can_fops);
142
143
144 #ifdef CAN_WITH_RTL
145 struct rtl_file_operations can_fops_rtl = {
146         llseek:         NULL,
147         read:           can_read_rtl_posix,
148         write:          can_write_rtl_posix,
149         ioctl:          can_ioctl_rtl_posix,
150         mmap:           NULL,
151         open:           can_open_rtl_posix,
152         release:        can_release_rtl_posix
153 };
154 #endif /*CAN_WITH_RTL*/
155
156
157 int init_module(void)
158 {
159         int res=0,i=0, j;
160         struct candevice_t *candev;
161         struct chip_t *chip;
162
163         if (parse_mod_parms())
164                 return -EINVAL;
165
166         canqueue_kern_initialize();
167
168         if (init_hw_struct())
169                 return -ENODEV;
170
171         #ifdef CAN_DEBUG
172                 list_hw();
173         #endif
174
175         res=register_chrdev(major,DEVICE_NAME, &can_fops);
176         if (res<0) {
177                 CANMSG("Error registering driver.\n");
178                 return -ENODEV;
179         }
180
181         #ifdef CAN_WITH_RTL
182         canqueue_rtl_initialize();
183         res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
184         if (res<0) {
185                 CANMSG("Error registering RT-Linux major number.\n");
186                 goto rtldev_error;
187         }
188         #endif /*CAN_WITH_RTL*/
189
190         for (i=0; i<hardware_p->nr_boards; i++) {
191                 candev=hardware_p->candevice[i];
192                 if (candev->hwspecops->request_io(candev)) 
193                         goto memory_error;
194                 candev->flags|=CANDEV_IO_RESERVED;
195         }
196
197         for (i=0; i<hardware_p->nr_boards; i++) {
198                 candev=hardware_p->candevice[i];
199                 if (candev->hwspecops->reset(candev)) 
200                         goto reset_error;
201         }
202
203         can_spin_lock_init(&hardware_p->rtr_lock);
204         hardware_p->rtr_queue=NULL;
205
206         for (i=0; i<hardware_p->nr_boards; i++) {
207                 candev=hardware_p->candevice[i];
208                 for(j=0; j<candev->nr_all_chips; j++) {
209                         if((chip=candev->chip[j])==NULL)
210                                 continue;
211                         if(can_chip_setup_irq(chip)<0) {
212                                 CANMSG("IRQ setup failed\n");
213                                 goto interrupt_error;
214                         }
215                 }
216                 
217                 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
218                         if (candev->hwspecops->program_irq(candev))
219                                 goto interrupt_error;
220         }
221
222 #ifdef CONFIG_PROC_FS
223         if (can_init_procdir())
224                 goto proc_error;
225 #endif
226
227 #ifdef CONFIG_DEVFS_FS
228         {
229                 char dev_name[32];
230                 int dev_minor;
231                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
232                         if(!objects_p[i]) continue;
233                         dev_minor=objects_p[i]->minor;
234                     #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
235                         sprintf (dev_name, "can%d", dev_minor);
236                         devfs_handles[i]=devfs_register(NULL, dev_name,
237                                 DEVFS_FL_DEFAULT, major, dev_minor,
238                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
239                                 &can_fops, (void*)objects_p[i]);
240                     #else
241                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
242                     #endif
243                 }
244         }
245 #endif
246         return 0;
247
248 #ifdef CONFIG_PROC_FS
249         proc_error: ;
250                 CANMSG("Error registering /proc entry.\n");
251                 goto memory_error; 
252 #endif
253
254         interrupt_error: ;
255                 CANMSG("Error registering interrupt line.\n");
256                 goto memory_error;
257
258         reset_error: ;
259                 goto memory_error;
260
261         memory_error: ;
262                 canhardware_done(hardware_p);
263
264                 #ifdef CAN_WITH_RTL
265                 rtl_unregister_rtldev(major,DEVICE_NAME);
266         rtldev_error:
267                 canqueue_rtl_done();
268                 #endif /*CAN_WITH_RTL*/
269
270                 res=unregister_chrdev(major,DEVICE_NAME);
271                 if (res<0)
272                         CANMSG("Error unloading CAN driver, error: %d\n",res);
273                 else
274                         CANMSG("No CAN devices or driver setup error.\n");
275                 return -ENODEV;
276
277 }
278
279 void cleanup_module(void)
280 {
281         int res=0,i=0;
282
283 #ifdef CONFIG_PROC_FS
284         if (can_delete_procdir())
285                 CANMSG("Error unregistering /proc/can entry.\n"); 
286 #endif
287
288 #ifdef CONFIG_DEVFS_FS
289         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
290             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
291                 if(devfs_handles[i])
292                         devfs_unregister(devfs_handles[i]);
293             #else
294                 int dev_minor;
295                 if(!objects_p[i]) continue;
296                 dev_minor=objects_p[i]->minor;
297                 if(minor>=0)
298                         devfs_remove("can%d", dev_minor);
299             #endif
300         }
301 #endif
302
303         canhardware_done(hardware_p);
304
305         #ifdef CAN_WITH_RTL
306         rtl_unregister_rtldev(major,DEVICE_NAME);
307         canqueue_rtl_done();
308         #endif /*CAN_WITH_RTL*/
309
310         if ( can_del_mem_list() ) 
311                 CANMSG("Error deallocating memory\n");
312
313         res=unregister_chrdev(major,DEVICE_NAME);
314         if (res<0)
315                 CANMSG("Error unregistering CAN driver, error: %d\n",res);
316 }