]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
OMK makefiles updated to support OCERA Qconf tool.
[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 #ifndef EXPORT_SYMTAB
11 #define EXPORT_SYMTAB
12 #endif
13
14 #include <linux/autoconf.h>
15
16 #include <linux/module.h>
17
18 #include <linux/kernel.h>
19 #include <linux/fs.h>
20 #include <linux/sched.h>
21 #include <linux/poll.h>
22 #include <linux/version.h>
23 #include <linux/autoconf.h>
24 #include <linux/interrupt.h>
25
26 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
27 #include <linux/wrapper.h>
28 #endif
29
30 #if !defined (__GENKSYMS__) 
31 #if (defined (MODVERSIONS) && !defined(NOVER))
32 #include <linux/modversions.h>
33 /*#include "../include/main.ver"*/
34 #endif
35 #endif
36
37 /*#undef CONFIG_DEVFS_FS*/
38
39 #ifdef CONFIG_DEVFS_FS
40 #include <linux/devfs_fs_kernel.h>
41 #include <linux/miscdevice.h>
42 #endif
43
44 #include "../include/can.h"
45 #include "../include/can_sysdep.h"
46 #include "../include/main.h"
47 #include "../include/modparms.h"
48 #include "../include/devcommon.h"
49 #include "../include/setup.h"
50 #include "../include/proc.h"
51 #include "../include/open.h"
52 #include "../include/close.h"
53 #include "../include/read.h"
54 #include "../include/select.h"
55 #include "../include/irq.h"
56 #include "../include/ioctl.h"
57 #include "../include/write.h"
58 #include "../include/finish.h"
59 #include "../include/fasync.h"
60
61 #ifdef CAN_WITH_RTL
62 #include <rtl_posixio.h>
63 #include "../include/can_iortl.h"
64 #endif /*CAN_WITH_RTL*/
65
66 #define EXPORT_SYMTAB
67
68 can_spinlock_t canuser_manipulation_lock;
69
70 /* Module parameters, some must be supplied at module loading time */
71 int major=CAN_MAJOR;
72 MODULE_PARM(major,"1i");
73 MODULE_PARM_DESC(major,"can be used to change default major [" __MODULE_STRING(CAN_MAJOR) "]");
74 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};
75 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
76 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
77 MODULE_PARM_DESC(minor,"can be used to change default starting minor for each channel");
78 int extended=0;
79 MODULE_PARM(extended,"1i");
80 MODULE_PARM_DESC(extended,"enables automatic switching to extended format if ID>2047,"
81                         " selects extended frames reception for i82527");
82 int pelican=0;
83 MODULE_PARM(pelican,"1i");
84 MODULE_PARM_DESC(pelican,"unused parameter, PeliCAN used by default for sja1000p chips");
85 int baudrate[MAX_TOT_CHIPS];
86 MODULE_PARM(baudrate, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
87 MODULE_PARM_DESC(baudrate,"baudrate for each channel in step of 1kHz");
88 char *hw[MAX_HW_CARDS]={NULL,};
89 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
90 MODULE_PARM_DESC(hw,"list of boards types to initialize - virtual,pip5,...");
91 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};
92 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
93 MODULE_PARM_DESC(irq,"list of iterrupt signal numbers, most ISA has one per chip, no value for PCI or virtual");
94 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
95 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
96 MODULE_PARM_DESC(io,"IO address for each board, use 0 for PCI or virtual");
97 int stdmask=0;
98 MODULE_PARM(stdmask, "1i");
99 MODULE_PARM_DESC(stdmask,"default standard mask for i82527 chips");
100 int extmask=0;
101 MODULE_PARM(extmask, "1i");
102 MODULE_PARM_DESC(extmask,"default extended mask for i82527 chips");
103 int mo15mask=0;
104 MODULE_PARM(mo15mask, "1i");
105 MODULE_PARM_DESC(mo15mask,"mask for communication object 15 of i82527 chips");
106 int processlocal=0;
107 MODULE_PARM(processlocal, "1i");
108 MODULE_PARM_DESC(processlocal,"select postprocessing/loopback of transmitted messages - "
109                 "0 .. disabled, 1 .. can be enabled by FIFO filter, 2 .. enabled by default");
110 #ifdef CAN_WITH_RTL
111 int can_rtl_priority=-1;
112 MODULE_PARM(can_rtl_priority, "1i");
113 MODULE_PARM_DESC(can_rtl_priority,"select priority of chip worker thread");
114 #endif /*CAN_WITH_RTL*/
115
116 /* Other module attributes */
117 #ifdef MODULE_SUPPORTED_DEVICE
118 MODULE_SUPPORTED_DEVICE("can");
119 #endif
120 #ifdef MODULE_LICENSE
121 MODULE_LICENSE("GPL");
122 #endif
123 #ifdef MODULE_DESCRIPTION
124 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
125 #endif
126 #ifdef MODULE_AUTHOR
127 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
128 #endif
129
130 /* Global structures, used to describe the installed hardware. */
131 struct canhardware_t canhardware;
132 struct canhardware_t *hardware_p=&canhardware;
133 struct chip_t *chips_p[MAX_TOT_CHIPS];
134 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
135 #ifdef CONFIG_DEVFS_FS
136 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,60))
137 devfs_handle_t  devfs_handles[MAX_TOT_MSGOBJS];
138 #endif
139 #endif
140
141 /* Pointers to dynamically allocated memory are maintained in a linked list
142  * to ease memory deallocation.
143  */
144 struct mem_addr *mem_head=NULL;
145
146 struct file_operations can_fops=
147 {
148  #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
149         owner:          THIS_MODULE,    
150  #endif
151         read:           can_read,
152         write:          can_write,
153         poll:           can_poll,
154         ioctl:          can_ioctl,
155         open:           can_open,
156         release:        can_close,
157   #ifdef CAN_ENABLE_KERN_FASYNC
158         fasync:         can_fasync
159   #endif /*CAN_ENABLE_KERN_FASYNC*/
160 };
161
162 EXPORT_SYMBOL(can_fops);
163
164
165 #ifdef CAN_WITH_RTL
166 struct rtl_file_operations can_fops_rtl = {
167         llseek:         NULL,
168         read:           can_read_rtl_posix,
169         write:          can_write_rtl_posix,
170         ioctl:          can_ioctl_rtl_posix,
171         mmap:           NULL,
172         open:           can_open_rtl_posix,
173         release:        can_release_rtl_posix
174 };
175 #endif /*CAN_WITH_RTL*/
176
177
178 int init_module(void)
179 {
180         int res=0,i=0, j;
181         struct candevice_t *candev;
182         struct chip_t *chip;
183
184         if (parse_mod_parms())
185                 return -EINVAL;
186
187         can_spin_lock_init(&canuser_manipulation_lock);
188         canqueue_kern_initialize();
189
190         if (init_hw_struct())
191                 return -ENODEV;
192
193         #ifdef CAN_DEBUG
194                 list_hw();
195         #endif
196
197         res=register_chrdev(major,DEVICE_NAME, &can_fops);
198         if (res<0) {
199                 CANMSG("Error registering driver.\n");
200                 return -ENODEV;
201         }
202
203         #ifdef CAN_WITH_RTL
204         can_spin_lock_init(&can_irq_manipulation_lock);
205         canqueue_rtl_initialize();
206         res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
207         if (res<0) {
208                 CANMSG("Error registering RT-Linux major number.\n");
209                 goto rtldev_error;
210         }
211         #endif /*CAN_WITH_RTL*/
212
213         for (i=0; i<hardware_p->nr_boards; i++) {
214                 candev=hardware_p->candevice[i];
215                 if (candev->hwspecops->request_io(candev)) 
216                         goto memory_error;
217                 candev->flags|=CANDEV_IO_RESERVED;
218         }
219
220         for (i=0; i<hardware_p->nr_boards; i++) {
221                 candev=hardware_p->candevice[i];
222                 if (candev->hwspecops->reset(candev)) 
223                         goto reset_error;
224         }
225
226         can_spin_lock_init(&hardware_p->rtr_lock);
227         hardware_p->rtr_queue=NULL;
228
229         for (i=0; i<hardware_p->nr_boards; i++) {
230                 candev=hardware_p->candevice[i];
231                 for(j=0; j<candev->nr_all_chips; j++) {
232                         if((chip=candev->chip[j])==NULL)
233                                 continue;
234                         if(can_chip_setup_irq(chip)<0) {
235                                 CANMSG("IRQ setup failed\n");
236                                 goto interrupt_error;
237                         }
238                 }
239                 
240                 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
241                         if (candev->hwspecops->program_irq(candev))
242                                 goto interrupt_error;
243         }
244
245 #ifdef CONFIG_PROC_FS
246         if (can_init_procdir())
247                 goto proc_error;
248 #endif
249
250 #ifdef CONFIG_DEVFS_FS
251         {
252                 char dev_name[32];
253                 int dev_minor;
254                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
255                         if(!objects_p[i]) continue;
256                         dev_minor=objects_p[i]->minor;
257                     #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
258                         sprintf (dev_name, "can%d", dev_minor);
259                         devfs_handles[i]=devfs_register(NULL, dev_name,
260                                 DEVFS_FL_DEFAULT, major, dev_minor,
261                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
262                                 &can_fops, (void*)objects_p[i]);
263                     #else
264                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
265                     #endif
266                 }
267         }
268 #endif
269         return 0;
270
271 #ifdef CONFIG_PROC_FS
272         proc_error: ;
273                 CANMSG("Error registering /proc entry.\n");
274                 goto memory_error; 
275 #endif
276
277         interrupt_error: ;
278                 CANMSG("Error registering interrupt line.\n");
279                 goto memory_error;
280
281         reset_error: ;
282                 goto memory_error;
283
284         memory_error: ;
285                 canhardware_done(hardware_p);
286
287                 #ifdef CAN_WITH_RTL
288                 rtl_unregister_rtldev(major,DEVICE_NAME);
289         rtldev_error:
290                 canqueue_rtl_done();
291                 #endif /*CAN_WITH_RTL*/
292
293                 res=unregister_chrdev(major,DEVICE_NAME);
294                 if (res<0)
295                         CANMSG("Error unloading CAN driver, error: %d\n",res);
296                 else
297                         CANMSG("No CAN devices or driver setup error.\n");
298                 return -ENODEV;
299
300 }
301
302 void cleanup_module(void)
303 {
304         int res=0,i=0;
305
306 #ifdef CONFIG_PROC_FS
307         if (can_delete_procdir())
308                 CANMSG("Error unregistering /proc/can entry.\n"); 
309 #endif
310
311 #ifdef CONFIG_DEVFS_FS
312         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
313             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,50))
314                 if(devfs_handles[i])
315                         devfs_unregister(devfs_handles[i]);
316             #else
317                 int dev_minor;
318                 if(!objects_p[i]) continue;
319                 dev_minor=objects_p[i]->minor;
320                 if(minor>=0)
321                         devfs_remove("can%d", dev_minor);
322             #endif
323         }
324 #endif
325
326         canhardware_done(hardware_p);
327
328         #ifdef CAN_WITH_RTL
329         rtl_unregister_rtldev(major,DEVICE_NAME);
330         canqueue_rtl_done();
331         #endif /*CAN_WITH_RTL*/
332
333         if ( can_del_mem_list() ) 
334                 CANMSG("Error deallocating memory\n");
335
336         res=unregister_chrdev(major,DEVICE_NAME);
337         if (res<0)
338                 CANMSG("Error unregistering CAN driver, error: %d\n",res);
339 }