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