]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/main.c
df2e84cb6be56450d8ea517f0ac6f053491c87bc
[lincan.git] / lincan / src / main.c
1 /**************************************************************************/
2 /* File: main.c - the CAN driver top level glue code (needs rewrite)      */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #ifndef EXPORT_SYMTAB
36 #define EXPORT_SYMTAB
37 #endif
38
39 #include <linux/autoconf.h>
40
41 #include <linux/module.h>
42
43 #include <linux/kernel.h>
44 #include <linux/fs.h>
45 #include <linux/sched.h>
46 #include <linux/poll.h>
47 #include <linux/version.h>
48 #include <linux/autoconf.h>
49 #include <linux/interrupt.h>
50
51 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
52  #include <linux/wrapper.h>
53 #else
54  #include <linux/device.h>
55  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
56   static struct class *can_class;
57  #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
58   static struct class_simple *can_class;
59   #define class_create class_simple_create
60   #define class_device_create class_simple_device_add
61   #define class_device_destroy(a,b) class_simple_device_remove(b)
62   #define class_destroy class_simple_destroy
63  #endif
64 #endif
65
66 #if !defined (__GENKSYMS__)
67 #if (defined (MODVERSIONS) && !defined(NOVER))
68 #include <linux/modversions.h>
69 /*#include "../include/main.ver"*/
70 #endif
71 #endif
72
73 /*#undef CONFIG_DEVFS_FS*/
74
75 #ifdef CONFIG_DEVFS_FS
76 #include <linux/devfs_fs_kernel.h>
77 #include <linux/miscdevice.h>
78 #endif
79
80 #include "../include/can.h"
81 #include "../include/can_sysdep.h"
82 #include "../include/main.h"
83 #include "../include/modparms.h"
84 #include "../include/devcommon.h"
85 #include "../include/setup.h"
86 #include "../include/proc.h"
87 #include "../include/open.h"
88 #include "../include/close.h"
89 #include "../include/read.h"
90 #include "../include/select.h"
91 #include "../include/irq.h"
92 #include "../include/ioctl.h"
93 #include "../include/write.h"
94 #include "../include/finish.h"
95 #include "../include/fasync.h"
96
97 #ifdef CAN_WITH_RTL
98 #include <rtl_posixio.h>
99 #include "../include/can_iortl.h"
100 #endif /*CAN_WITH_RTL*/
101
102 can_spinlock_t canuser_manipulation_lock;
103
104 int major=CAN_MAJOR;
105 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};
106 int extended=0;
107 int pelican=0;
108 int baudrate[MAX_TOT_CHIPS];
109 char *hw[MAX_HW_CARDS]={NULL,};
110 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};
111 unsigned long io[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
112 long clockfreq[MAX_HW_CARDS];
113 int stdmask=0;
114 int extmask=0;
115 int mo15mask=0;
116 int processlocal=0;
117
118 unsigned int minor_specified;
119 unsigned int baudrate_specified;
120 unsigned int hw_specified;
121 unsigned int irq_specified;
122 unsigned int io_specified;
123 unsigned int clockfreq_specified;
124
125 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12))
126 /* Module parameters, some must be supplied at module loading time */
127 MODULE_PARM(major,"1i");
128 /*MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS)"i");*/
129 MODULE_PARM(minor, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
130 MODULE_PARM(extended,"1i");
131 MODULE_PARM(pelican,"1i");
132 MODULE_PARM(baudrate, "1-" __MODULE_STRING(MAX_TOT_CHIPS_STR)"i");
133 MODULE_PARM(hw, "1-" __MODULE_STRING(MAX_HW_CARDS)"s");
134 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_IRQ)"i");
135 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
136 MODULE_PARM(clockfreq, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
137 MODULE_PARM(stdmask, "1i");
138 MODULE_PARM(extmask, "1i");
139 MODULE_PARM(mo15mask, "1i");
140 MODULE_PARM(processlocal, "1i");
141
142 #else /* LINUX_VERSION_CODE >= 2,6,12 */
143 module_param(major, int, 0);
144 module_param_array(minor, int, &minor_specified, 0);
145 module_param(extended, int, 0);
146 module_param(pelican, int, 0);
147 module_param_array(baudrate, int, &baudrate_specified, 0);
148 module_param_array(hw, charp, &hw_specified, 0);
149 module_param_array(irq, int, &irq_specified, 0);
150 module_param_array(io, ulong, &io_specified, 0);
151 module_param_array(clockfreq, long, &clockfreq_specified, 0);
152 module_param(stdmask, int, 0);
153 module_param(extmask, int, 0);
154 module_param(mo15mask, int, 0);
155 module_param(processlocal, int, 0);
156 #endif /* LINUX_VERSION_CODE >= 2,6,12 */
157
158 MODULE_PARM_DESC(major,"can be used to change default major [" __MODULE_STRING(CAN_MAJOR) "]");
159 MODULE_PARM_DESC(minor,"can be used to change default starting minor for each channel");
160 MODULE_PARM_DESC(extended,"enables automatic switching to extended format if ID>2047,"
161                         " selects extended frames reception for i82527");
162 MODULE_PARM_DESC(pelican,"unused parameter, PeliCAN used by default for sja1000p chips");
163 MODULE_PARM_DESC(baudrate,"baudrate for each channel in step of 1kHz");
164 MODULE_PARM_DESC(hw,"list of boards types to initialize - virtual,pip5,...");
165 MODULE_PARM_DESC(irq,"list of iterrupt signal numbers, most ISA has one per chip, no value for PCI or virtual");
166 MODULE_PARM_DESC(io,"IO address for each board, use 0 for PCI or virtual");
167 MODULE_PARM_DESC(clockfreq,"base board clock source frequency in step of 1kHz");
168 MODULE_PARM_DESC(stdmask,"default standard mask for i82527 chips");
169 MODULE_PARM_DESC(extmask,"default extended mask for i82527 chips");
170 MODULE_PARM_DESC(mo15mask,"mask for communication object 15 of i82527 chips");
171 MODULE_PARM_DESC(processlocal,"select postprocessing/loopback of transmitted messages - "
172                 "0 .. disabled, 1 .. can be enabled by FIFO filter, 2 .. enabled by default");
173
174 #ifdef CAN_WITH_RTL
175 int can_rtl_priority=-1;
176 MODULE_PARM(can_rtl_priority, "1i");
177 MODULE_PARM_DESC(can_rtl_priority,"select priority of chip worker thread");
178 #endif /*CAN_WITH_RTL*/
179
180 /* Other module attributes */
181 #ifdef MODULE_SUPPORTED_DEVICE
182 MODULE_SUPPORTED_DEVICE("can");
183 #endif
184 #ifdef MODULE_LICENSE
185 MODULE_LICENSE("GPL");
186 #endif
187 #ifdef MODULE_DESCRIPTION
188 MODULE_DESCRIPTION("Universal Linux CAN-bus device driver");
189 #endif
190 #ifdef MODULE_AUTHOR
191 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>, Arnaud Westenberg");
192 #endif
193
194 /* Global structures, used to describe the installed hardware. */
195 struct canhardware_t canhardware;
196 struct canhardware_t *hardware_p=&canhardware;
197 struct canchip_t *chips_p[MAX_TOT_CHIPS];
198 struct msgobj_t *objects_p[MAX_TOT_MSGOBJS];
199 #ifdef CONFIG_DEVFS_FS
200 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
201 devfs_handle_t  devfs_handles[MAX_TOT_MSGOBJS];
202 #endif
203 #endif
204
205 /* Pointers to dynamically allocated memory are maintained in a linked list
206  * to ease memory deallocation.
207  */
208 struct mem_addr *mem_head=NULL;
209
210 struct file_operations can_fops=
211 {
212  #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
213         owner:          THIS_MODULE,
214  #endif
215         read:           can_read,
216         write:          can_write,
217         poll:           can_poll,
218         ioctl:          can_ioctl,
219         open:           can_open,
220         release:        can_close,
221   #ifdef CAN_ENABLE_KERN_FASYNC
222         fasync:         can_fasync
223   #endif /*CAN_ENABLE_KERN_FASYNC*/
224 };
225
226 EXPORT_SYMBOL(can_fops);
227
228
229 #ifdef CAN_WITH_RTL
230 struct rtl_file_operations can_fops_rtl = {
231         llseek:         NULL,
232         read:           can_read_rtl_posix,
233         write:          can_write_rtl_posix,
234         ioctl:          can_ioctl_rtl_posix,
235         mmap:           NULL,
236         open:           can_open_rtl_posix,
237         release:        can_release_rtl_posix
238 };
239 #endif /*CAN_WITH_RTL*/
240
241
242 /*
243  2.6 kernel attributes for sysfs
244
245 static ssize_t show_xxx(struct class_device *cdev, char *buf)
246 {
247         return sprintf(buf, "xxxx\n");
248 }
249
250 static ssize_t store_xxx(struct class_device *cdev, const char * buf, size_t count)
251 {
252 }
253
254 static CLASS_DEVICE_ATTR(xxx, S_IRUGO, show_xxx, store_xxx/NULL);
255
256 ret = class_device_create_file(class_dev, class_device_attr_xxx);
257 if (ret)
258         goto err_unregister;
259
260 */
261
262 int init_module(void)
263 {
264         int res=0,i=0, j;
265         struct candevice_t *candev;
266         struct canchip_t *chip;
267
268         if (parse_mod_parms())
269                 return -EINVAL;
270
271         can_spin_lock_init(&canuser_manipulation_lock);
272         canqueue_kern_initialize();
273
274         if (init_hw_struct())
275                 return -ENODEV;
276
277         #ifdef CAN_DEBUG
278                 list_hw();
279         #endif
280
281         res=register_chrdev(major,DEVICE_NAME, &can_fops);
282         if (res<0) {
283                 CANMSG("Error registering driver.\n");
284                 goto register_error;
285         }
286
287         #ifdef CAN_WITH_RTL
288         can_spin_lock_init(&can_irq_manipulation_lock);
289         canqueue_rtl_initialize();
290         res=rtl_register_rtldev(major,DEVICE_NAME,&can_fops_rtl);
291         if (res<0) {
292                 CANMSG("Error registering RT-Linux major number.\n");
293                 goto rtldev_error;
294         }
295         #endif /*CAN_WITH_RTL*/
296
297         for (i=0; i<hardware_p->nr_boards; i++) {
298                 candev=hardware_p->candevice[i];
299                 if (candev->hwspecops->request_io(candev))
300                         goto request_io_error;
301                 candev->flags|=CANDEV_IO_RESERVED;
302         }
303
304         for (i=0; i<hardware_p->nr_boards; i++) {
305                 candev=hardware_p->candevice[i];
306                 if (candev->hwspecops->reset(candev))
307                         goto reset_error;
308         }
309
310         can_spin_lock_init(&hardware_p->rtr_lock);
311         hardware_p->rtr_queue=NULL;
312
313         for (i=0; i<hardware_p->nr_boards; i++) {
314                 candev=hardware_p->candevice[i];
315                 for(j=0; j<candev->nr_all_chips; j++) {
316                         if((chip=candev->chip[j])==NULL)
317                                 continue;
318
319                         if(chip->chipspecops->attach_to_chip(chip)<0) {
320                                 CANMSG("Initial attach to the chip HW failed\n");
321                                 goto interrupt_error;
322                         }
323
324                         chip->flags |= CHIP_ATTACHED;
325
326                         if(can_chip_setup_irq(chip)<0) {
327                                 CANMSG("Error to setup chip IRQ\n");
328                                 goto interrupt_error;
329                         }
330                 }
331
332                 if (candev->flags & CANDEV_PROGRAMMABLE_IRQ)
333                         if (candev->hwspecops->program_irq(candev)){
334                                 CANMSG("Error to program board interrupt\n");
335                                 goto interrupt_error;
336                         }
337         }
338
339 #ifdef CONFIG_PROC_FS
340         if (can_init_procdir())
341                 goto proc_error;
342 #endif
343
344 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
345         can_class=class_create(THIS_MODULE, "can");
346 #endif
347
348 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
349         {
350             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
351                 char dev_name[32];
352             #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
353                 struct class_device *this_dev;
354             #else
355                 struct device *this_dev;
356             #endif
357                 int dev_minor;
358                 for(i=0;i<MAX_TOT_MSGOBJS;i++) {
359                         if(!objects_p[i]) continue;
360                         dev_minor=objects_p[i]->minor;
361                     #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
362                         sprintf (dev_name, "can%d", dev_minor);
363                         devfs_handles[i]=devfs_register(NULL, dev_name,
364                                 DEVFS_FL_DEFAULT, major, dev_minor,
365                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
366                                 &can_fops, (void*)objects_p[i]);
367                     #else
368                       #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
369                         this_dev=class_device_create(can_class, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
370                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25) /* >= 2.6.15 */
371                         this_dev=class_device_create(can_class, NULL, MKDEV(major, dev_minor), NULL,  "can%d", dev_minor);
372                       #elif  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
373                         this_dev=device_create_drvdata(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
374                       #else /* >= 2.6.28 */
375                         this_dev=device_create(can_class, NULL, MKDEV(major, dev_minor), objects_p[i],  "can%d", dev_minor);
376                       #endif /* >= 2.6.28 */
377                         if(IS_ERR(this_dev)){
378                                 CANMSG("problem to create device \"can%d\" in the class \"can\"\n", dev_minor);
379                       #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
380                         }else{
381                                 /*this_dev->class_data=objects_p[i];*/
382                                 class_set_devdata(this_dev,objects_p[i]);
383                       #endif /* <= 2.6.25 */
384                         }
385                       #ifdef CONFIG_DEVFS_FS
386                         devfs_mk_cdev(MKDEV(major, dev_minor), S_IFCHR | S_IRUGO | S_IWUGO, "can%d", dev_minor);
387                       #endif
388                     #endif
389                 }
390         }
391 #endif
392         return 0;
393
394 #ifdef CONFIG_PROC_FS
395         proc_error: ;
396                 CANMSG("Error registering /proc entry.\n");
397                 goto memory_error;
398 #endif
399
400         interrupt_error: ;
401                 goto memory_error;
402
403         reset_error: ;
404                 CANMSG("Error resetting device.\n");
405                 goto memory_error;
406
407         request_io_error: ;
408                 CANMSG("Error to request IO resources for device.\n");
409                 goto memory_error;
410
411         memory_error: ;
412                 canhardware_done(hardware_p);
413
414                 #ifdef CAN_WITH_RTL
415                 rtl_unregister_rtldev(major,DEVICE_NAME);
416         rtldev_error:
417                 canqueue_rtl_done();
418                 #endif /*CAN_WITH_RTL*/
419
420                 unregister_chrdev(major,DEVICE_NAME);
421                 CANMSG("No CAN devices or driver setup error.\n");
422
423         register_error:
424                 if ( can_del_mem_list() )
425                         CANMSG("Error deallocating memory\n");
426
427                 return -ENODEV;
428 }
429
430 void cleanup_module(void)
431 {
432 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
433         int i=0;
434 #endif
435
436 #ifdef CONFIG_PROC_FS
437         if (can_delete_procdir())
438                 CANMSG("Error unregistering /proc/can entry.\n");
439 #endif
440
441 #if defined(CONFIG_DEVFS_FS) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
442         for(i=0;i<MAX_TOT_MSGOBJS;i++) {
443             #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0))
444                 if(devfs_handles[i])
445                         devfs_unregister(devfs_handles[i]);
446             #else
447                 int dev_minor;
448                 if(!objects_p[i]) continue;
449                 dev_minor=objects_p[i]->minor;
450                 if(minor>=0){
451                     #ifdef CONFIG_DEVFS_FS
452                         devfs_remove("can%d", dev_minor);
453                     #endif
454                     #if  LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,25)
455                         class_device_destroy(can_class, MKDEV(major, dev_minor));
456                     #else /* >= 2.6.26 */
457                         device_destroy(can_class, MKDEV(major, dev_minor));
458                     #endif /* >= 2.6.26 */
459                 }
460             #endif
461         }
462 #endif
463
464 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
465         class_destroy(can_class);
466 #endif
467
468         canhardware_done(hardware_p);
469
470         #ifdef CAN_WITH_RTL
471         rtl_unregister_rtldev(major,DEVICE_NAME);
472         canqueue_rtl_done();
473         #endif /*CAN_WITH_RTL*/
474
475         if ( can_del_mem_list() )
476                 CANMSG("Error deallocating memory\n");
477
478         unregister_chrdev(major,DEVICE_NAME);
479 }