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