]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/setup.c
c071b002b2fec95c70ef4921f1b4d50df9785ba5
[lincan.git] / lincan / src / setup.c
1 /* setup.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.2  9 Jul 2003
8  */ 
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/devcommon.h"
14 #include "../include/setup.h"
15 #include "../include/finish.h"
16
17 extern int sja1000_register(struct chipspecops_t *chipspecops);
18 extern int sja1000p_register(struct chipspecops_t *chipspecops);
19 extern int i82527_register(struct chipspecops_t *chipspecops);
20
21 int init_hwspecops(struct candevice_t *candev, int *irqnum_p);
22 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p);
23 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate);
24 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr);
25 int init_chipspecops(struct candevice_t *candev, int chipnr);
26
27 /**
28  * can_checked_malloc - memory allocation with registering of requested blocks
29  * @size: size of the requested block
30  *
31  * The function is used in the driver initialization phase to catch possible memory
32  * leaks for future driver finalization or case, that driver initialization fail.
33  * 
34  * Return Value: pointer to the allocated memory or NULL in the case of fail
35  */
36 void *can_checked_malloc(size_t size)
37 {
38         struct mem_addr *mem_new;
39         void *address_p;
40         
41         address_p=kmalloc(size,GFP_KERNEL);
42         if(address_p == NULL) {
43                 CANMSG("can_checked_malloc: out of the memory\n");
44                 return NULL;
45         }
46
47 #ifdef DEBUG_MEM
48         DEBUGMSG("can_checked_malloc: allocated %d bytes at %p, mem_head=%p\n",
49                         (int)size, address_p, mem_head);
50 #endif
51
52         mem_new=(struct mem_addr *)kmalloc(sizeof(struct mem_addr),GFP_KERNEL);
53         if (mem_new == NULL) {
54                 CANMSG("can_checked_malloc: memory list allocation error.\n");
55                 kfree(address_p);
56                 return NULL;
57         }
58         mem_new->next=mem_head;
59         mem_new->address=address_p;
60         mem_new->size=size;
61         mem_head=mem_new;
62
63         return address_p;
64 }
65
66 /**
67  * can_checked_free - free memory allocated by  can_checked_malloc()
68  * @address_p: pointer to the memory block
69  */
70 int can_checked_free(void *address_p)
71 {
72         struct mem_addr **mem_pptr;
73         struct mem_addr *mem_del=NULL;
74
75 #ifdef DEBUG_MEM
76         DEBUGMSG("can_checked_free %p, mem_head=%p\n", address_p, mem_head);
77 #endif
78
79         for(mem_pptr = &mem_head; (mem_del = *mem_pptr); mem_pptr = &mem_del->next) {
80                 if (mem_del->address != address_p)
81                         continue;
82                 *mem_pptr=mem_del->next;
83                 kfree(mem_del);
84                 kfree(address_p);
85                 return 0;
86         }
87         
88         CANMSG("can_checked_free: address %p not found on the mem list\n", address_p);
89         
90         kfree(address_p);
91         return -1;
92 }
93
94
95 /**
96  * can_del_mem_list - check for stale memory allocations at driver finalization
97  *
98  * Checks, if there are still some memory blocks allocated and releases memory
99  * occupied by such blocks back to the system
100  */
101 int can_del_mem_list(void)
102 {
103         struct mem_addr *mem;
104
105 #ifdef DEBUG_MEM
106         DEBUGMSG("can_del_mem_list, mem_head=%p\n", mem_head);
107 #endif
108         if(mem_head == NULL) {
109                 CANMSG("can_del_mem_list: no entries on the list - OK\n");
110                 return 0;
111         }
112
113         while((mem=mem_head) != NULL) {
114                 mem_head=mem->next;
115                 CANMSG("can_del_mem_list: deleting %p with size %d\n",
116                         mem->address, (int)mem->size);
117                 kfree(mem->address);
118                 kfree(mem);
119         }
120         
121         return 0;
122 }
123
124 /**
125  * can_request_io_region - request IO space region
126  * @start: the first IO port address
127  * @n: number of the consecutive IO port addresses
128  * @name: name/label for the requested region
129  *
130  * The function hides system specific implementation of the feature.
131  *
132  * Return Value: returns positive value (1) in the case, that region could
133  *      be reserved for the driver. Returns zero (0) if there is collision with
134  *      other driver or region cannot be taken for some other reason.
135  */
136 int can_request_io_region(unsigned long start, unsigned long n, const char *name)
137 {
138     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
139         if(check_region(start,n)) return 0;
140         request_region(start,n,name);
141         return 1;
142     #else
143         return (request_region(start,n,name))?1:0;
144     #endif
145 }
146
147 /**
148  * can_release_io_region - release IO space region
149  * @start: the first IO port address
150  * @n: number of the consecutive IO port addresses
151  */
152 void can_release_io_region(unsigned long start, unsigned long n)
153 {
154         release_region(start,n);
155 }
156
157 /**
158  * can_request_mem_region - request memory space region
159  * @start: the first memory port physical address
160  * @n: number of the consecutive memory port addresses
161  * @name: name/label for the requested region
162  *
163  * The function hides system specific implementation of the feature.
164  *
165  * Return Value: returns positive value (1) in the case, that region could
166  *      be reserved for the driver. Returns zero (0) if there is collision with
167  *      other driver or region cannot be taken for some other reason.
168  */
169 int can_request_mem_region(unsigned long start, unsigned long n, const char *name)
170 {
171     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
172         return 1;
173     #else
174         return (request_mem_region(start,n,name))?1:0;
175     #endif
176 }
177
178 /**
179  * can_release_mem_region - release memory space region
180  * @start: the first memory port physical address
181  * @n: number of the consecutive memory port addresses
182  */
183 void can_release_mem_region(unsigned long start, unsigned long n)
184 {
185     #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
186         return;
187     #else
188         release_mem_region(start,n);
189     #endif
190 }
191
192 /**
193  * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
194  * @candev: pointer to the previously filled device/board, chips and message objects structures
195  * @new_base: @candev new base address
196  *
197  * This function adapts base addresses of all structures of one board
198  * to the new board base address.
199  * It is required for translation between physical and virtual address mappings.
200  * This function is prepared to simplify board specific xxx_request_io() function
201  * for memory mapped devices.
202  */
203 int can_base_addr_fixup(struct candevice_t *candev, unsigned long new_base)
204 {
205         unsigned long offs;
206         int i, j;
207         
208         offs=new_base-candev->dev_base_addr;
209         candev->dev_base_addr=new_base;
210         for(i=0;i<candev->nr_all_chips;i++){
211                 candev->chip[i]->chip_base_addr += offs;
212                 for(j=0;j<candev->chip[i]->max_objects;j++)
213                         candev->chip[i]->msgobj[j]->obj_base_addr += offs;
214         }
215         return 0;
216 }
217
218 /**
219  * can_check_dev_taken - checks if bus device description is already taken by driver
220  * @anydev:     pointer to bus specific Linux device description 
221  *
222  * Returns: Returns 1 if device is already used by LinCAN driver, 0 otherwise.
223  */
224 int can_check_dev_taken(void *anydev)
225 {
226         int board_nr;
227         struct candevice_t *candev;
228         void *boarddev;
229
230         for (board_nr=hardware_p->nr_boards; board_nr--; ) {
231                 if((candev=hardware_p->candevice[board_nr])==NULL)
232                         continue;
233                 boarddev=candev->sysdevptr.anydev;
234                 if(boarddev == anydev)
235                         return 1;
236         }
237         
238         return 0;
239 }
240
241
242 /**
243  * register_obj_struct - registers message object into global array
244  * @obj: the initialized message object being registered
245  * @minorbase: wanted minor number, if (-1) automatically selected
246  *
247  * Return Value: returns negative number in the case of fail
248  */
249 int register_obj_struct(struct msgobj_t *obj, int minorbase)
250 {
251         static int next_minor=0;
252         int i;
253         
254         if(minorbase>=0)
255                 next_minor=minorbase;
256         if(next_minor>=MAX_TOT_MSGOBJS)
257                 next_minor=0;
258         i=next_minor;
259         do{
260                 if(objects_p[i]==NULL){
261                         objects_p[i]=obj;
262                         obj->minor=i;
263                         next_minor=i+1;
264                         return 0;
265                 }
266                 if(++i >= MAX_TOT_MSGOBJS) i=0;
267         }while(i!=next_minor);
268         obj->minor=-1;
269         return -1;
270 }
271
272
273 /**
274  * register_chip_struct - registers chip into global array
275  * @chip: the initialized chip structure being registered
276  * @minorbase: wanted minor number base, if (-1) automatically selected
277  *
278  * Return Value: returns negative number in the case of fail
279  */
280 int register_chip_struct(struct chip_t *chip, int minorbase)
281 {
282         static int next_chip_slot=0;
283         int i;
284         
285         if(next_chip_slot>=MAX_TOT_CHIPS)
286                 next_chip_slot=0;
287         i=next_chip_slot;
288         do{
289                 if(chips_p[i]==NULL){
290                         chips_p[i]=chip;
291
292                         next_chip_slot=i+1;
293                         return 0;
294                 }
295                 if(++i >= MAX_TOT_CHIPS) i=0;
296         }while(i!=next_chip_slot);
297         return -1;
298 }
299
300
301
302 /**
303  * init_hw_struct - initializes driver hardware description structures
304  *
305  * The function init_hw_struct() is used to initialize the hardware structure.
306  *
307  * Return Value: returns negative number in the case of fail
308  */
309 int init_hw_struct(void)
310 {
311         int i=0;
312         int irq_param_idx=0;
313         int chan_param_idx=0;
314
315         hardware_p->nr_boards=0;
316         while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
317                 hardware_p->nr_boards++;
318
319                 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
320                         CANMSG("Error initializing candevice_t structures.\n");
321                         return -ENODEV;
322                 }
323                 i++;
324         }
325
326         return 0;
327 }
328
329 /**
330  * init_device_struct - initializes single CAN device/board
331  * @card: index into @hardware_p HW description
332  * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
333  * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
334  *
335  * The function builds representation of the one board from parameters provided
336  * in the module parameters arrays: 
337  *      @hw[card] .. hardware type,
338  *      @io[card] .. base IO address,
339  *      @baudrate[chan_param_idx] .. per channel baudrate,
340  *      @minor[chan_param_idx] .. optional specification of requested channel minor base,
341  *      @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
342  * The indexes are advanced after consumed parameters if the registration is successful.
343  *
344  * The hardware specific operations of the device/board are initialized by call to
345  * init_hwspecops() function. Then board data are initialized by board specific 
346  * init_hw_data() function. Then chips and objects representation is build by
347  * init_chip_struct() function. If all above steps are successful, chips and
348  * message objects are registered into global arrays. 
349  *
350  * Return Value: returns negative number in the case of fail
351  */
352 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
353 {
354         struct candevice_t *candev;
355         int ret;
356         int irqnum;
357         int chipnr;
358         long bd;
359         int irqsig=-1;
360         
361         candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
362         if (candev==NULL)
363                 return -ENOMEM;
364
365         memset(candev, 0, sizeof(struct candevice_t));
366
367         hardware_p->candevice[card]=candev;
368         candev->candev_idx=card;
369
370         candev=candev;
371
372         candev->hwname=hw[card];
373         candev->io_addr=io[card];
374         candev->dev_base_addr=io[card];
375
376         candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
377         if (candev->hwspecops==NULL)
378                 goto error_nomem;
379
380         memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
381
382         if (init_hwspecops(candev, &irqnum))
383                 goto error_nodev;
384
385         if (candev->hwspecops->init_hw_data(candev))
386                 goto error_nodev;
387
388         /* Alocate and initialize the chip structures */
389         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
390
391                 if(chipnr<irqnum)
392                         irqsig=irq[*irq_param_idx_p+chipnr];
393                 
394                 bd=baudrate[*chan_param_idx_p+chipnr];
395                 if(!bd) bd=baudrate[0];
396         
397                 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000)))
398                         goto error_chip;
399         }
400         
401
402
403         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
404                 int m=minor[*chan_param_idx_p+chipnr];
405                 struct chip_t *chip=candev->chip[chipnr];
406                 int objnr;
407
408                 register_chip_struct(chip, m);
409                 
410                 for (objnr=0; objnr<chip->max_objects; objnr++) {
411                         register_obj_struct(chip->msgobj[objnr], m);
412                         if(m>=0) m++;
413                 }
414         }
415
416         *irq_param_idx_p += irqnum;
417         *chan_param_idx_p += candev->nr_all_chips;
418
419         return 0;
420
421     error_nodev:
422         ret=-ENODEV;
423     error_chip:
424         candevice_done(candev);
425         goto error_both;
426
427     error_nomem:
428         ret=-ENOMEM;
429
430     error_both:
431         hardware_p->candevice[card]=NULL;
432         can_checked_free(candev);
433         return ret;
434         
435 }
436
437 /**
438  * init_chip_struct - initializes one CAN chip structure
439  * @candev: pointer to the corresponding CAN device/board
440  * @chipnr: index of the chip in the corresponding device/board structure
441  * @irq: chip IRQ number or (-1) if not appropriate
442  * @baudrate: baudrate in the units of 1Bd
443  *
444  * Chip structure is allocated and chip specific operations are filled by 
445  * call to board specific init_chip_data() function and generic
446  * init_chipspecops() function. The message objects are generated by 
447  * calls to init_obj_struct() function.
448  *
449  * Return Value: returns negative number in the case of fail
450  */
451 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate)
452 {
453         struct chip_t *chip;
454         int objnr;
455         int ret;
456
457         candev->chip[chipnr]=(struct chip_t *)can_checked_malloc(sizeof(struct chip_t));
458         if ((chip=candev->chip[chipnr])==NULL)
459                 return -ENOMEM;
460
461         memset(chip, 0, sizeof(struct chip_t));
462
463         chip->write_register=candev->hwspecops->write_register;
464         chip->read_register=candev->hwspecops->read_register;
465
466         chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
467         if (chip->chipspecops==NULL)
468                 return -ENOMEM;
469         memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
470
471         chip->chip_idx=chipnr;
472         chip->hostdevice=candev;
473         chip->chip_irq=irq;
474         chip->baudrate=baudrate;
475         chip->flags=0x0;
476
477         candev->hwspecops->init_chip_data(candev,chipnr);
478
479         if (init_chipspecops(candev,chipnr))
480                 return -ENODEV;
481
482         for (objnr=0; objnr<chip->max_objects; objnr++) {
483                 ret=init_obj_struct(candev, chip, objnr);
484                 if(ret<0) return ret;
485         }
486
487         return 0;
488 }
489
490
491 /**
492  * init_obj_struct - initializes one CAN message object structure
493  * @candev: pointer to the corresponding CAN device/board
494  * @hostchip: pointer to the chip containing this object
495  * @objnr: index of the builded object in the chip structure
496  *
497  * The function initializes message object structure and allocates and initializes
498  * CAN queue chip ends structure.
499  *
500  * Return Value: returns negative number in the case of fail
501  */
502 int init_obj_struct(struct candevice_t *candev, struct chip_t *hostchip, int objnr)
503 {
504         struct canque_ends_t *qends;
505         struct msgobj_t *obj;
506         int ret;
507
508         obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
509         hostchip->msgobj[objnr]=obj;
510         if (obj == NULL) 
511                 return -ENOMEM;
512
513         memset(obj, 0, sizeof(struct msgobj_t));
514         obj->minor=-1;
515
516         atomic_set(&obj->obj_used,0);
517         INIT_LIST_HEAD(&obj->obj_users);
518         init_timer(&obj->tx_timeout);
519
520         qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
521         if(qends == NULL) return -ENOMEM;
522         memset(qends, 0, sizeof(struct canque_ends_t));
523         obj->hostchip=hostchip;
524         obj->object=objnr+1;
525         obj->qends=qends;
526         obj->tx_qedge=NULL;
527         obj->tx_slot=NULL;
528         obj->obj_flags = 0x0;
529
530         ret=canqueue_ends_init_chip(qends, hostchip, obj);
531         if(ret<0) return ret;
532
533         ret=candev->hwspecops->init_obj_data(hostchip,objnr);
534         if(ret<0) return ret;
535         
536         return 0;
537 }
538
539
540 /**
541  * init_hwspecops - finds and initializes board/device specific operations
542  * @candev: pointer to the corresponding CAN device/board
543  * @irqnum_p: optional pointer to the number of interrupts required by board
544  *
545  * The function searches board @hwname in the list of supported boards types.
546  * The board type specific board_register() function is used to initialize
547  * @hwspecops operations.
548  *
549  * Return Value: returns negative number in the case of fail
550  */
551 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
552 {
553         const struct boardtype_t *brp;
554         
555         brp = boardtype_find(candev->hwname);
556         
557         if(!brp) {
558                 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
559                 return -EINVAL;
560         }
561         
562         if(irqnum_p)
563                 *irqnum_p=brp->irqnum;
564         brp->board_register(candev->hwspecops);
565
566         return 0;
567 }
568
569
570 /**
571  * init_chipspecops - fills chip specific operations for board for known chip types
572  * @candev: pointer to the corresponding CAN device/board
573  * @chipnr: index of the chip in the device/board structure
574  *
575  * The function fills chip specific operations for next known generic chip
576  * types "i82527", "sja1000", "sja1000p" (PeliCAN). Other non generic chip types
577  * operations has to be initialized in the board specific init_chip_data() function.
578  *
579  * Return Value: returns negative number in the case of fail
580  */
581 int init_chipspecops(struct candevice_t *candev, int chipnr)
582 {
583         if (!strcmp(candev->chip[chipnr]->chip_type,"i82527")) {
584                 candev->chip[chipnr]->max_objects=15;
585                 i82527_register(candev->chip[chipnr]->chipspecops);
586         } 
587         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000")) {
588                 candev->chip[chipnr]->max_objects=1;
589                 sja1000_register(candev->chip[chipnr]->chipspecops);
590         }
591         if (!strcmp(candev->chip[chipnr]->chip_type,"sja1000p")) {
592                 candev->chip[chipnr]->max_objects=1;
593                 sja1000p_register(candev->chip[chipnr]->chipspecops);
594         }
595
596         return 0;
597 }
598
599 #ifndef CAN_WITH_RTL
600
601 /**
602  * can_chip_setup_irq - attaches chip to the system interrupt processing
603  * @chip: pointer to CAN chip structure
604  *
605  * Return Value: returns negative number in the case of fail
606  */
607 int can_chip_setup_irq(struct chip_t *chip)
608 {
609         if(chip==NULL)
610                 return -1;
611         if(!chip->chipspecops->irq_handler)
612                 return 0;
613                         
614         if ((chip->flags & CHIP_IRQ_VME) == 0) {
615                 if (request_irq(chip->chip_irq,chip->chipspecops->irq_handler,SA_SHIRQ,DEVICE_NAME,chip))
616                         return -1;
617                 else {
618                         DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
619                         chip->flags |= CHIP_IRQ_SETUP;
620                 }
621         } else {
622 #ifdef CAN_ENABLE_VME_SUPPORT           
623                 /* TODO: Move here the irq setup from
624                  * unican_vme_request_io(). To do this, the VME bridge
625                  * driver should be modified. */
626 #endif
627         }
628         return 1;
629 }
630
631
632 /**
633  * can_chip_free_irq - unregisters chip interrupt handler from the system
634  * @chip: pointer to CAN chip structure
635  */
636 void can_chip_free_irq(struct chip_t *chip)
637 {
638         if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
639                 if ((chip->flags & CHIP_IRQ_VME) == 0)
640                         free_irq(chip->chip_irq, chip);
641                 else { 
642 #ifdef CAN_ENABLE_VME_SUPPORT
643                 /* TODO: Move here the irq cleanup from
644                  * unican_vme_release_io(). To do this, the VME bridge
645                  * driver should be modified. */
646 #endif
647                 }
648                         chip->flags &= ~CHIP_IRQ_SETUP;
649         }
650 }
651
652 #endif /*CAN_WITH_RTL*/