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
10 #define __NO_VERSION__
11 #include <linux/module.h>
13 #include <linux/autoconf.h>
15 #include <linux/version.h>
16 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
17 #include <linux/malloc.h>
19 #include <linux/slab.h>
22 #include <linux/ioport.h>
24 #include ".supported_cards.h"
26 #include "../include/main.h"
27 #include "../include/devcommon.h"
28 #include "../include/setup.h"
29 #include "../include/pip.h"
30 #include "../include/pccan.h"
31 #include "../include/smartcan.h"
32 #include "../include/pc-i03.h"
33 #include "../include/pcm3680.h"
34 #include "../include/m437.h"
35 #include "../include/template.h"
36 #include "../include/i82527.h"
37 #include "../include/aim104.h"
38 #include "../include/pcccan.h"
40 extern int sja1000_register(struct chipspecops_t *chipspecops);
41 extern int sja1000p_register(struct chipspecops_t *chipspecops);
42 extern int i82527_register(struct chipspecops_t *chipspecops);
43 extern int template_register(struct hwspecops_t *hwspecops);
44 extern int pip5_register(struct hwspecops_t *hwspecops);
45 extern int pip6_register(struct hwspecops_t *hwspecops);
46 extern int smartcan_register(struct hwspecops_t *hwspecops);
47 extern int pccanf_register(struct hwspecops_t *hwspecops);
48 extern int pccand_register(struct hwspecops_t *hwspecops);
49 extern int pccanq_register(struct hwspecops_t *hwspecops);
50 extern int nsi_register(struct hwspecops_t *hwspecops);
51 extern int cc104_register(struct hwspecops_t *hwspecops);
52 extern int pci03_register(struct hwspecops_t *hwspecops);
53 extern int pcm3680_register(struct hwspecops_t *hwspecops);
54 extern int aim104_register(struct hwspecops_t *hwspecops);
55 extern int pcccan_register(struct hwspecops_t *hwspecops);
56 extern int ssv_register(struct hwspecops_t *hwspecops);
57 extern int bfadcan_register(struct hwspecops_t *hwspecops);
58 extern int pikronisa_register(struct hwspecops_t *hwspecops);
60 int init_device_struct(int card);
61 int init_hwspecops(struct candevice_t *candev);
62 int init_chip_struct(struct candevice_t *candev);
63 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int minorbase);
64 int init_chipspecops(struct candevice_t *candev, int chipnr);
66 int add_mem_to_list(void *address_p)
68 struct mem_addr *mem_new;
71 DEBUGMSG("add_mem_to_list %p, mem_head=%p\n",address_p, mem_head);
75 mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
76 if (mem_new == NULL) {
77 CANMSG("Memory list error.\n");
80 mem_new->next=mem_head;
81 mem_new->address=address_p;
87 int del_mem_from_list(void *address_p)
89 struct mem_addr *mem_search=NULL;
90 struct mem_addr *mem_delete=NULL;
93 DEBUGMSG("del_mem_from_list %p, mem_head=%p\n", address_p, mem_head);
96 if(mem_head == NULL) {
97 CANMSG("del_mem_from_list: mem_head == NULL address_p=%p!\n",
102 mem_search = mem_head;
104 if (mem_head->address == address_p) {
105 kfree(mem_head->address);
106 mem_head=mem_head->next;
110 while (mem_search->next->address != address_p)
111 mem_search=mem_search->next;
112 kfree(mem_search->next->address);
113 mem_delete=mem_search->next;
114 mem_search->next=mem_search->next->next;
121 int del_mem_list(void)
123 struct mem_addr *mem_old;
126 DEBUGMSG("del_mem_list, mem_head=%p\n", mem_head);
129 if(mem_head == NULL) {
130 CANMSG("del_mem_list: mem_head == NULL!\n");
134 while (mem_head->next != NULL) {
136 kfree(mem_old->address);
137 mem_head=mem_old->next;
144 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
146 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
147 if(check_region(start,n)) return 0;
148 request_region(start,n,name);
151 return (request_region(start,n,name))?1:0;
155 void can_release_io_region(unsigned long start, unsigned long n)
157 release_region(start,n);
160 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
162 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
165 return (request_mem_region(start,n,name))?1:0;
169 void can_release_mem_region(unsigned long start, unsigned long n)
171 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
174 release_mem_region(start,n);
178 /* This function shifts all base address structures acording to address
179 translation between physical and virtual address mappings */
180 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
185 offs=new_base-candev->dev_base_addr;
186 candev->dev_base_addr=new_base;
187 for(i=0;i<candev->nr_all_chips;i++){
188 candev->chip[i]->chip_base_addr += offs;
189 for(j=0;j<candev->chip[i]->max_objects;j++)
190 candev->chip[i]->msgobj[j]->obj_base_addr += offs;
195 /* The function init_hw_struct is used to initialize the hardware structure. */
196 int init_hw_struct(void)
200 hardware_p->nr_boards=0;
201 while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
202 hardware_p->nr_boards++;
204 if (init_device_struct(i)) {
205 CANMSG("Error initializing candevice_t structures.\n");
214 /* The function init_device_struct is used to initialize a single device
217 int init_device_struct(int card)
219 struct candevice_t *candev;
221 candev=(struct candevice_t *)kmalloc(sizeof(struct candevice_t),GFP_KERNEL);
225 if ( add_mem_to_list(candev) )
228 memset(candev, 0, sizeof(struct candevice_t));
230 hardware_p->candevice[card]=candev;
231 candev->candev_idx=card;
235 candev->hwname=hw[card];
236 candev->io_addr=io[card];
237 candev->dev_base_addr=io[card];
239 candev->hwspecops=(struct hwspecops_t *)kmalloc(sizeof(struct hwspecops_t),GFP_KERNEL);
240 if (candev->hwspecops==NULL)
243 if ( add_mem_to_list(candev->hwspecops) )
246 if (init_hwspecops(candev))
249 if (candev->hwspecops->init_hw_data(candev))
252 if (init_chip_struct(candev))
258 /* The function init_chip_struct is used to initialize all chip_t structures
259 * on one hardware board.
261 int init_chip_struct(struct candevice_t *candev)
263 static int irq_count=0;
266 /* Alocate and initialize the chip structures */
267 for (i=0; i < candev->nr_all_chips; i++) {
268 candev->chip[i]=(struct chip_t *)kmalloc(sizeof(struct chip_t),GFP_KERNEL);
269 if (candev->chip[i]==NULL)
272 if ( add_mem_to_list(candev->chip[i]) )
275 memset(candev->chip[i], 0, sizeof(struct chip_t));
277 candev->chip[i]->write_register=candev->hwspecops->write_register;
278 candev->chip[i]->read_register=candev->hwspecops->read_register;
280 candev->chip[i]->chipspecops=(struct chipspecops_t *)kmalloc(sizeof(struct chipspecops_t),GFP_KERNEL);
281 if (candev->chip[i]->chipspecops==NULL)
284 if ( add_mem_to_list(candev->chip[i]->chipspecops) )
287 chips_p[irq_count]=candev->chip[i];
288 candev->chip[i]->chip_idx=i;
289 candev->chip[i]->hostdevice=candev;
290 candev->chip[i]->chip_irq=irq[irq_count];
291 candev->chip[i]->flags=0x0;
293 candev->hwspecops->init_chip_data(candev,i);
295 if (init_chipspecops(candev,i))
298 init_obj_struct(candev, candev->chip[i], minor[irq_count]);
306 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int minorbase)
308 struct canque_ends_t *qends;
309 static int obj_count=0;
311 struct msgobj_t *obj;
313 max_objects=hostchip->max_objects;
314 for (i=0; i<max_objects; i++) {
315 obj=(struct msgobj_t *)kmalloc(sizeof(struct msgobj_t),GFP_KERNEL);
316 hostchip->msgobj[i]=obj;
320 if ( add_mem_to_list(obj) )
323 memset(obj, 0, sizeof(struct msgobj_t));
325 atomic_set(&obj->obj_used,0);
326 INIT_LIST_HEAD(&obj->obj_users);
327 qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
328 if(qends == NULL) return -ENOMEM;
329 if(add_mem_to_list(qends)) return -ENOMEM;
330 memset(qends, 0, sizeof(struct canque_ends_t));
331 obj->hostchip=hostchip;
338 canqueue_ends_init_chip(qends, hostchip, obj);
340 if (minorbase == -1) minorbase=obj_count;
341 if ((minorbase >= 0) && (minorbase+i<MAX_TOT_MSGOBJS)){
342 objects_p[minorbase+i]=obj;
343 obj->minor=minorbase+i;
344 } else obj->minor=-1;
346 candev->hwspecops->init_obj_data(hostchip,i);
354 int init_hwspecops(struct candevice_t *candev)
356 #ifdef ENABLE_CARD_template
357 if (!strcmp(candev->hwname,"template")) {
358 template_register(candev->hwspecops);
361 #ifdef ENABLE_CARD_pip
362 if (!strcmp(candev->hwname,"pip5")) {
363 pip5_register(candev->hwspecops);
365 else if (!strcmp(candev->hwname,"pip6")) {
366 pip6_register(candev->hwspecops);
369 #ifdef ENABLE_CARD_smartcan
370 if (!strcmp(candev->hwname,"smartcan")) {
371 smartcan_register(candev->hwspecops);
374 #ifdef ENABLE_CARD_nsi
375 if (!strcmp(candev->hwname,"nsican")) {
376 nsi_register(candev->hwspecops);
379 #ifdef ENABLE_CARD_cc_can104
380 if (!strcmp(candev->hwname,"cc104")) {
381 cc104_register(candev->hwspecops);
384 #ifdef ENABLE_CARD_aim104
385 if (!strcmp(candev->hwname,"aim104")) {
386 aim104_register(candev->hwspecops);
389 #ifdef ENABLE_CARD_pc_i03
390 if (!strcmp(candev->hwname,"pc-i03")) {
391 pci03_register(candev->hwspecops);
394 #ifdef ENABLE_CARD_pcm3680
395 if (!strcmp(candev->hwname,"pcm3680")) {
396 pcm3680_register(candev->hwspecops);
399 #ifdef ENABLE_CARD_pccan
400 if (!strcmp(candev->hwname,"pccan-f") |
401 !strcmp(candev->hwname,"pccan-s") ) {
402 pccanf_register(candev->hwspecops);
404 if (!strcmp(candev->hwname,"pccan-d")) {
405 pccand_register(candev->hwspecops);
407 if (!strcmp(candev->hwname,"pccan-q")) {
408 pccanq_register(candev->hwspecops);
411 #ifdef ENABLE_CARD_m437
412 if (!strcmp(candev->hwname,"m437")) {
413 m437_register(candev->hwspecops);
416 #ifdef ENABLE_CARD_pcccan
417 if (!strcmp(candev->hwname,"pcccan")) {
418 pcccan_register(candev->hwspecops);
421 #ifdef ENABLE_CARD_ssv
422 if (!strcmp(candev->hwname,"ssv")) {
423 ssv_register(candev->hwspecops);
426 #ifdef ENABLE_CARD_bfadcan
427 if (!strcmp(candev->hwname,"bfadcan")) {
428 bfadcan_register(candev->hwspecops);
431 #ifdef ENABLE_CARD_pikronisa
432 if (!strcmp(candev->hwname,"pikronisa")) {
433 pikronisa_register(candev->hwspecops);
439 int init_chipspecops(struct candevice_t *candev, int chipnr)
441 candev->chip[chipnr]->max_objects=0;
443 if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
444 candev->chip[chipnr]->max_objects=15;
445 i82527_register(candev->chip[chipnr]->chipspecops);
447 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
448 candev->chip[chipnr]->max_objects=1;
449 sja1000_register(candev->chip[chipnr]->chipspecops);
451 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
452 candev->chip[chipnr]->max_objects=1;
453 sja1000p_register(candev->chip[chipnr]->chipspecops);