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