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 /* This function shifts all base address structures acording to address
161 translation between physical and virtual address mappings */
162 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
167 offs=new_base-candev->dev_base_addr;
168 candev->dev_base_addr=new_base;
169 for(i=0;i<candev->nr_all_chips;i++){
170 candev->chip[i]->chip_base_addr += offs;
171 for(j=0;j<candev->chip[i]->max_objects;j++)
172 candev->chip[i]->msgobj[j]->obj_base_addr += offs;
177 /* The function init_hw_struct is used to initialize the hardware structure. */
178 int init_hw_struct(void)
182 hardware_p->nr_boards=0;
183 while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
184 hardware_p->nr_boards++;
186 if (init_device_struct(i)) {
187 CANMSG("Error initializing candevice_t structures.\n");
196 /* The function init_device_struct is used to initialize a single device
199 int init_device_struct(int card)
201 struct candevice_t *candev;
203 candev=(struct candevice_t *)kmalloc(sizeof(struct candevice_t),GFP_KERNEL);
207 if ( add_mem_to_list(candev) )
210 memset(candev, 0, sizeof(struct candevice_t));
212 hardware_p->candevice[card]=candev;
213 candev->candev_idx=card;
217 candev->hwname=hw[card];
218 candev->io_addr=io[card];
219 candev->dev_base_addr=io[card];
221 candev->hwspecops=(struct hwspecops_t *)kmalloc(sizeof(struct hwspecops_t),GFP_KERNEL);
222 if (candev->hwspecops==NULL)
225 if ( add_mem_to_list(candev->hwspecops) )
228 if (init_hwspecops(candev))
231 if (candev->hwspecops->init_hw_data(candev))
234 if (init_chip_struct(candev))
240 /* The function init_chip_struct is used to initialize all chip_t structures
241 * on one hardware board.
243 int init_chip_struct(struct candevice_t *candev)
245 static int irq_count=0;
248 /* Alocate and initialize the chip structures */
249 for (i=0; i < candev->nr_all_chips; i++) {
250 candev->chip[i]=(struct chip_t *)kmalloc(sizeof(struct chip_t),GFP_KERNEL);
251 if (candev->chip[i]==NULL)
254 if ( add_mem_to_list(candev->chip[i]) )
257 memset(candev->chip[i], 0, sizeof(struct chip_t));
259 candev->chip[i]->write_register=candev->hwspecops->write_register;
260 candev->chip[i]->read_register=candev->hwspecops->read_register;
262 candev->chip[i]->chipspecops=(struct chipspecops_t *)kmalloc(sizeof(struct chipspecops_t),GFP_KERNEL);
263 if (candev->chip[i]->chipspecops==NULL)
266 if ( add_mem_to_list(candev->chip[i]->chipspecops) )
269 chips_p[irq_count]=candev->chip[i];
270 candev->chip[i]->chip_idx=i;
271 candev->chip[i]->hostdevice=candev;
272 candev->chip[i]->chip_irq=irq[irq_count];
273 candev->chip[i]->flags=0x0;
275 candev->hwspecops->init_chip_data(candev,i);
277 if (init_chipspecops(candev,i))
280 init_obj_struct(candev, candev->chip[i], minor[irq_count]);
288 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int minorbase)
290 struct canque_ends_t *qends;
291 static int obj_count=0;
293 struct msgobj_t *obj;
295 max_objects=hostchip->max_objects;
296 for (i=0; i<max_objects; i++) {
297 obj=(struct msgobj_t *)kmalloc(sizeof(struct msgobj_t),GFP_KERNEL);
298 hostchip->msgobj[i]=obj;
302 if ( add_mem_to_list(obj) )
305 memset(obj, 0, sizeof(struct msgobj_t));
307 atomic_set(&obj->obj_used,0);
308 INIT_LIST_HEAD(&obj->obj_users);
309 qends = (struct canque_ends_t *)kmalloc(sizeof(struct canque_ends_t), GFP_KERNEL);
310 if(qends == NULL) return -ENOMEM;
311 if(add_mem_to_list(qends)) return -ENOMEM;
312 memset(qends, 0, sizeof(struct canque_ends_t));
313 obj->hostchip=hostchip;
320 canqueue_ends_init_chip(qends, hostchip, obj);
322 if (minorbase == -1) minorbase=obj_count;
323 if ((minorbase >= 0) && (minorbase+i<MAX_TOT_MSGOBJS)){
324 objects_p[minorbase+i]=obj;
325 obj->minor=minorbase+i;
326 } else obj->minor=-1;
328 candev->hwspecops->init_obj_data(hostchip,i);
336 int init_hwspecops(struct candevice_t *candev)
338 #ifdef ENABLE_CARD_template
339 if (!strcmp(candev->hwname,"template")) {
340 template_register(candev->hwspecops);
343 #ifdef ENABLE_CARD_pip
344 if (!strcmp(candev->hwname,"pip5")) {
345 pip5_register(candev->hwspecops);
347 else if (!strcmp(candev->hwname,"pip6")) {
348 pip6_register(candev->hwspecops);
351 #ifdef ENABLE_CARD_smartcan
352 if (!strcmp(candev->hwname,"smartcan")) {
353 smartcan_register(candev->hwspecops);
356 #ifdef ENABLE_CARD_nsi
357 if (!strcmp(candev->hwname,"nsican")) {
358 nsi_register(candev->hwspecops);
361 #ifdef ENABLE_CARD_cc_can104
362 if (!strcmp(candev->hwname,"cc104")) {
363 cc104_register(candev->hwspecops);
366 #ifdef ENABLE_CARD_aim104
367 if (!strcmp(candev->hwname,"aim104")) {
368 aim104_register(candev->hwspecops);
371 #ifdef ENABLE_CARD_pc_i03
372 if (!strcmp(candev->hwname,"pc-i03")) {
373 pci03_register(candev->hwspecops);
376 #ifdef ENABLE_CARD_pcm3680
377 if (!strcmp(candev->hwname,"pcm3680")) {
378 pcm3680_register(candev->hwspecops);
381 #ifdef ENABLE_CARD_pccan
382 if (!strcmp(candev->hwname,"pccan-f") |
383 !strcmp(candev->hwname,"pccan-s") ) {
384 pccanf_register(candev->hwspecops);
386 if (!strcmp(candev->hwname,"pccan-d")) {
387 pccand_register(candev->hwspecops);
389 if (!strcmp(candev->hwname,"pccan-q")) {
390 pccanq_register(candev->hwspecops);
393 #ifdef ENABLE_CARD_m437
394 if (!strcmp(candev->hwname,"m437")) {
395 m437_register(candev->hwspecops);
398 #ifdef ENABLE_CARD_pcccan
399 if (!strcmp(candev->hwname,"pcccan")) {
400 pcccan_register(candev->hwspecops);
403 #ifdef ENABLE_CARD_ssv
404 if (!strcmp(candev->hwname,"ssv")) {
405 ssv_register(candev->hwspecops);
408 #ifdef ENABLE_CARD_bfadcan
409 if (!strcmp(candev->hwname,"bfadcan")) {
410 bfadcan_register(candev->hwspecops);
413 #ifdef ENABLE_CARD_pikronisa
414 if (!strcmp(candev->hwname,"pikronisa")) {
415 pikronisa_register(candev->hwspecops);
421 int init_chipspecops(struct candevice_t *candev, int chipnr)
423 candev->chip[chipnr]->max_objects=0;
425 if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
426 candev->chip[chipnr]->max_objects=15;
427 i82527_register(candev->chip[chipnr]->chipspecops);
429 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
430 candev->chip[chipnr]->max_objects=1;
431 sja1000_register(candev->chip[chipnr]->chipspecops);
433 if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
434 candev->chip[chipnr]->max_objects=1;
435 sja1000p_register(candev->chip[chipnr]->chipspecops);