]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/setup.c
bd6d59cbbd61992ed99ce57d6c8948c2cf5ea1ee
[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.3  17 Jun 2004
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 int init_hwspecops(struct candevice_t *candev, int *irqnum_p);
18 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p);
19 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate, long clock);
20 int init_obj_struct(struct candevice_t *candev, struct canchip_t *hostchip, int objnr);
21
22 /**
23  * can_base_addr_fixup - relocates board physical memory addresses to the CPU accessible ones
24  * @candev: pointer to the previously filled device/board, chips and message objects structures
25  * @new_base: @candev new base address
26  *
27  * This function adapts base addresses of all structures of one board
28  * to the new board base address.
29  * It is required for translation between physical and virtual address mappings.
30  * This function is prepared to simplify board specific xxx_request_io() function
31  * for memory mapped devices.
32  */
33 int can_base_addr_fixup(struct candevice_t *candev, can_ioptr_t new_base)
34 {
35         long offs;
36         int i, j;
37
38         offs=new_base-candev->dev_base_addr;
39         candev->dev_base_addr=new_base;
40         for(i=0;i<candev->nr_all_chips;i++){
41                 candev->chip[i]->chip_base_addr += offs;
42                 for(j=0;j<candev->chip[i]->max_objects;j++)
43                         candev->chip[i]->msgobj[j]->obj_base_addr += offs;
44         }
45         return 0;
46 }
47
48 /**
49  * can_check_dev_taken - checks if bus device description is already taken by driver
50  * @anydev:     pointer to bus specific Linux device description
51  *
52  * Returns: Returns 1 if device is already used by LinCAN driver, 0 otherwise.
53  */
54 int can_check_dev_taken(void *anydev)
55 {
56         int board_nr;
57         struct candevice_t *candev;
58         void *boarddev;
59
60         for (board_nr=hardware_p->nr_boards; board_nr--; ) {
61                 if((candev=hardware_p->candevice[board_nr])==NULL)
62                         continue;
63                 boarddev=candev->sysdevptr.anydev;
64                 if(boarddev == anydev)
65                         return 1;
66         }
67
68         return 0;
69 }
70
71
72 /**
73  * register_obj_struct - registers message object into global array
74  * @obj: the initialized message object being registered
75  * @minorbase: wanted minor number, if (-1) automatically selected
76  *
77  * Return Value: returns negative number in the case of fail
78  */
79 int register_obj_struct(struct msgobj_t *obj, int minorbase)
80 {
81         static int next_minor=0;
82         int i;
83
84         if(minorbase>=0)
85                 next_minor=minorbase;
86         if(next_minor>=MAX_TOT_MSGOBJS)
87                 next_minor=0;
88         i=next_minor;
89         do{
90                 if(objects_p[i]==NULL){
91                         objects_p[i]=obj;
92                         obj->minor=i;
93                         next_minor=i+1;
94                         return 0;
95                 }
96                 if(++i >= MAX_TOT_MSGOBJS) i=0;
97         }while(i!=next_minor);
98         obj->minor=-1;
99         return -1;
100 }
101
102
103 /**
104  * register_chip_struct - registers chip into global array
105  * @chip: the initialized chip structure being registered
106  * @minorbase: wanted minor number base, if (-1) automatically selected
107  *
108  * Return Value: returns negative number in the case of fail
109  */
110 int register_chip_struct(struct canchip_t *chip, int minorbase)
111 {
112         static int next_chip_slot=0;
113         int i;
114
115         if(next_chip_slot>=MAX_TOT_CHIPS)
116                 next_chip_slot=0;
117         i=next_chip_slot;
118         do{
119                 if(chips_p[i]==NULL){
120                         chips_p[i]=chip;
121
122                         next_chip_slot=i+1;
123                         return 0;
124                 }
125                 if(++i >= MAX_TOT_CHIPS) i=0;
126         }while(i!=next_chip_slot);
127         return -1;
128 }
129
130
131
132 /**
133  * init_hw_struct - initializes driver hardware description structures
134  *
135  * The function init_hw_struct() is used to initialize the hardware structure.
136  *
137  * Return Value: returns negative number in the case of fail
138  */
139 int init_hw_struct(void)
140 {
141         int i=0;
142         int irq_param_idx=0;
143         int chan_param_idx=0;
144
145         hardware_p->nr_boards=0;
146         while ( (hw[i] != NULL) & (i < MAX_HW_CARDS) ) {
147                 hardware_p->nr_boards++;
148
149                 if (init_device_struct(i, &chan_param_idx, &irq_param_idx)) {
150                         CANMSG("Error initializing candevice_t structures.\n");
151                         return -ENODEV;
152                 }
153                 i++;
154         }
155
156         return 0;
157 }
158
159 /**
160  * init_new_hw_struct - initializes driver description structures for new hardware
161  *
162  * The function init_new_hw_struct() is used to initialize the hardware structure.
163  *
164  * Return Value: returns negative number in the case of fail
165  */
166 int init_new_hw_struct(int devnr)
167 {
168         int irq_param_idx=0;
169         int chan_param_idx=0;
170
171         if ( (hw[devnr] != NULL) & (devnr < MAX_HW_CARDS) ) {
172                 hardware_p->nr_boards++;
173
174                 if (init_device_struct(devnr, &chan_param_idx, &irq_param_idx)) {
175                         CANMSG("Error initializing candevice_t structures.\n");
176                         return -ENODEV;
177                 }
178         }
179
180         return 0;
181 }
182
183 /**
184  * init_device_struct - initializes single CAN device/board
185  * @card: index into @hardware_p HW description
186  * @chan_param_idx_p: pointer to the index into arrays of the CAN channel parameters
187  * @irq_param_idx_p: pointer to the index into arrays of the per CAN channel IRQ parameters
188  *
189  * The function builds representation of the one board from parameters provided
190  * in the module parameters arrays:
191  *      @hw[card] .. hardware type,
192  *      @io[card] .. base IO address,
193  *      @baudrate[chan_param_idx] .. per channel baudrate,
194  *      @minor[chan_param_idx] .. optional specification of requested channel minor base,
195  *      @irq[irq_param_idx] .. one or more board/chips IRQ parameters.
196  * The indexes are advanced after consumed parameters if the registration is successful.
197  *
198  * The hardware specific operations of the device/board are initialized by call to
199  * init_hwspecops() function. Then board data are initialized by board specific
200  * init_hw_data() function. Then chips and objects representation is build by
201  * init_chip_struct() function. If all above steps are successful, chips and
202  * message objects are registered into global arrays.
203  *
204  * Return Value: returns negative number in the case of fail
205  */
206 int init_device_struct(int card, int *chan_param_idx_p, int *irq_param_idx_p)
207 {
208         struct candevice_t *candev;
209         int ret;
210         int irqnum;
211         int chipnr;
212         long bd;
213         int irqsig=-1;
214         long clock;
215
216         candev=(struct candevice_t *)can_checked_malloc(sizeof(struct candevice_t));
217         if (candev==NULL)
218                 return -ENOMEM;
219
220         memset(candev, 0, sizeof(struct candevice_t));
221
222         hardware_p->candevice[card]=candev;
223         candev->candev_idx=card;
224
225         candev=candev;
226
227         candev->hwname=hw[card];
228         candev->io_addr=io[card];
229         candev->dev_base_addr=io[card];
230         clock=clockfreq[card];
231
232         candev->hwspecops=(struct hwspecops_t *)can_checked_malloc(sizeof(struct hwspecops_t));
233         if (candev->hwspecops==NULL)
234                 goto error_nomem;
235
236         memset(candev->hwspecops, 0, sizeof(struct hwspecops_t));
237
238         if (init_hwspecops(candev, &irqnum))
239                 goto error_nodev;
240
241         if (candev->hwspecops->init_hw_data(candev))
242                 goto error_nodev;
243
244         /* Alocate and initialize the chip structures */
245         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
246
247                 if(chipnr<irqnum)
248                         irqsig=irq[*irq_param_idx_p+chipnr];
249
250                 bd=baudrate[*chan_param_idx_p+chipnr];
251                 if(!bd) bd=baudrate[0];
252
253                 if ((ret=init_chip_struct(candev, chipnr, irqsig, bd*1000, clock*1000)))
254                         goto error_chip;
255         }
256
257
258
259         for (chipnr=0; chipnr < candev->nr_all_chips; chipnr++) {
260                 int m=minor[*chan_param_idx_p+chipnr];
261                 struct canchip_t *chip=candev->chip[chipnr];
262                 int objnr;
263
264                 register_chip_struct(chip, m);
265
266                 for (objnr=0; objnr<chip->max_objects; objnr++) {
267                         register_obj_struct(chip->msgobj[objnr], m);
268                         if(m>=0) m++;
269                 }
270         }
271
272         *irq_param_idx_p += irqnum;
273         *chan_param_idx_p += candev->nr_all_chips;
274
275         return 0;
276
277     error_nodev:
278         ret=-ENODEV;
279     error_chip:
280         candevice_done(candev);
281         goto error_both;
282
283     error_nomem:
284         ret=-ENOMEM;
285
286     error_both:
287         hardware_p->candevice[card]=NULL;
288         can_checked_free(candev);
289         return ret;
290
291 }
292
293 /**
294  * init_chip_struct - initializes one CAN chip structure
295  * @candev: pointer to the corresponding CAN device/board
296  * @chipnr: index of the chip in the corresponding device/board structure
297  * @irq: chip IRQ number or (-1) if not appropriate
298  * @baudrate: baudrate in the units of 1Bd
299  * @clock: optional chip base clock frequency in 1Hz step
300  *
301  * Chip structure is allocated and chip specific operations are filled by
302  * call to board specific init_chip_data() which calls chip specific
303  * fill_chipspecops(). The message objects are generated by
304  * calls to init_obj_struct() function.
305  *
306  * Return Value: returns negative number in the case of fail
307  */
308 int init_chip_struct(struct candevice_t *candev, int chipnr, int irq, long baudrate, long clock)
309 {
310         struct canchip_t *chip;
311         int objnr;
312         int ret;
313
314         candev->chip[chipnr]=(struct canchip_t *)can_checked_malloc(sizeof(struct canchip_t));
315         if ((chip=candev->chip[chipnr])==NULL)
316                 return -ENOMEM;
317
318         memset(chip, 0, sizeof(struct canchip_t));
319
320         chip->write_register=candev->hwspecops->write_register;
321         chip->read_register=candev->hwspecops->read_register;
322
323         chip->chipspecops=can_checked_malloc(sizeof(struct chipspecops_t));
324         if (chip->chipspecops==NULL)
325                 return -ENOMEM;
326         memset(chip->chipspecops,0,sizeof(struct chipspecops_t));
327
328         chip->chip_idx=chipnr;
329         chip->hostdevice=candev;
330         chip->chip_irq=irq;
331         chip->baudrate=baudrate;
332         chip->clock=clock;
333         chip->flags=0x0;
334
335         if(candev->hwspecops->init_chip_data(candev,chipnr)<0)
336                 return -ENODEV;
337
338         for (objnr=0; objnr<chip->max_objects; objnr++) {
339                 ret=init_obj_struct(candev, chip, objnr);
340                 if(ret<0) return ret;
341         }
342
343         return 0;
344 }
345
346
347 /**
348  * init_obj_struct - initializes one CAN message object structure
349  * @candev: pointer to the corresponding CAN device/board
350  * @hostchip: pointer to the chip containing this object
351  * @objnr: index of the builded object in the chip structure
352  *
353  * The function initializes message object structure and allocates and initializes
354  * CAN queue chip ends structure.
355  *
356  * Return Value: returns negative number in the case of fail
357  */
358 int init_obj_struct(struct candevice_t *candev, struct canchip_t *hostchip, int objnr)
359 {
360         struct canque_ends_t *qends;
361         struct msgobj_t *obj;
362         int ret;
363
364         obj=(struct msgobj_t *)can_checked_malloc(sizeof(struct msgobj_t));
365         hostchip->msgobj[objnr]=obj;
366         if (obj == NULL)
367                 return -ENOMEM;
368
369         memset(obj, 0, sizeof(struct msgobj_t));
370         obj->minor=-1;
371
372         atomic_set(&obj->obj_used,0);
373         INIT_LIST_HEAD(&obj->obj_users);
374         init_timer(&obj->tx_timeout);
375
376         qends = (struct canque_ends_t *)can_checked_malloc(sizeof(struct canque_ends_t));
377         if(qends == NULL) return -ENOMEM;
378         memset(qends, 0, sizeof(struct canque_ends_t));
379         obj->hostchip=hostchip;
380         obj->object=objnr+1;
381         obj->qends=qends;
382         obj->tx_qedge=NULL;
383         obj->tx_slot=NULL;
384         obj->obj_flags = 0x0;
385
386         ret=canqueue_ends_init_chip(qends, hostchip, obj);
387         if(ret<0) return ret;
388
389         ret=candev->hwspecops->init_obj_data(hostchip,objnr);
390         if(ret<0) return ret;
391
392         return 0;
393 }
394
395
396 /**
397  * init_hwspecops - finds and initializes board/device specific operations
398  * @candev: pointer to the corresponding CAN device/board
399  * @irqnum_p: optional pointer to the number of interrupts required by board
400  *
401  * The function searches board @hwname in the list of supported boards types.
402  * The board type specific board_register() function is used to initialize
403  * @hwspecops operations.
404  *
405  * Return Value: returns negative number in the case of fail
406  */
407 int init_hwspecops(struct candevice_t *candev, int *irqnum_p)
408 {
409         const struct boardtype_t *brp;
410
411         brp = boardtype_find(candev->hwname);
412
413         if(!brp) {
414                 CANMSG("Sorry, hardware \"%s\" is currently not supported.\n",candev->hwname);
415                 return -EINVAL;
416         }
417
418         if(irqnum_p)
419                 *irqnum_p=brp->irqnum;
420         brp->board_register(candev->hwspecops);
421
422         return 0;
423 }