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