]> rtime.felk.cvut.cz Git - can-utils.git/blob - can-calc-bit-timing.c
can-calc-bit-timing: use "struct can_bittiming_const" from uapi headers
[can-utils.git] / can-calc-bit-timing.c
1 /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
2  *
3  * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
4  *
5  * Derived from:
6  *   can_baud.c - CAN baudrate calculation
7  *   Code based on LinCAN sources and H8S2638 project
8  *   Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
9  *   Copyright 2005      Stanislav Marek
10  *   email:pisa@cmp.felk.cvut.cz
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * any later version.
16  */
17
18 #include <errno.h>
19 #include <getopt.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <linux/types.h>
27 #include <linux/can/netlink.h>
28
29 /* seems not to be defined in errno.h */
30 #ifndef ENOTSUPP
31 #define ENOTSUPP        524     /* Operation is not supported */
32 #endif
33
34 /* useful defines */
35 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
36
37 #define do_div(a,b) a = (a) / (b)
38
39 #define abs(x) ({                               \
40                 long __x = (x);                 \
41                 (__x < 0) ? -__x : __x;         \
42         })
43
44 /**
45  * clamp - return a value clamped to a given range with strict typechecking
46  * @val: current value
47  * @min: minimum allowable value
48  * @max: maximum allowable value
49  *
50  * This macro does strict typechecking of min/max to make sure they are of the
51  * same type as val.  See the unnecessary pointer comparisons.
52  */
53 #define clamp(val, min, max) ({                 \
54         typeof(val) __val = (val);              \
55         typeof(min) __min = (min);              \
56         typeof(max) __max = (max);              \
57         (void) (&__val == &__min);              \
58         (void) (&__val == &__max);              \
59         __val = __val < __min ? __min: __val;   \
60         __val > __max ? __max: __val; })
61
62 /* we don't want to see these prints */
63 #define dev_err(dev, format, arg...)    do { } while (0)
64 #define dev_warn(dev, format, arg...)   do { } while (0)
65
66 /* define in-kernel-types */
67 typedef __u64 u64;
68 typedef __u32 u32;
69
70 struct calc_bittiming_const {
71         struct can_bittiming_const bittiming_const;
72
73         __u32 ref_clk;          /* CAN system clock frequency in Hz */
74         void (*printf_btr)(struct can_bittiming *bt, bool hdr);
75 };
76
77 /*
78  * minimal structs, just enough to be source level compatible
79  */
80 struct can_priv {
81         const struct can_bittiming_const *bittiming_const;
82         struct can_clock clock;
83 };
84
85 struct net_device {
86         struct can_priv priv;
87 };
88
89 static inline void *netdev_priv(const struct net_device *dev)
90 {
91         return (void *)&dev->priv;
92 }
93
94 static void print_usage(char *cmd)
95 {
96         printf("Usage: %s [options] [<CAN-contoller-name>]\n"
97                "\tOptions:\n"
98                "\t-q           : don't print header line\n"
99                "\t-l           : list all support CAN controller names\n"
100                "\t-b <bitrate> : bit-rate in bits/sec\n"
101                "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
102                "\t               or 0 for CIA recommended sample points\n"
103                "\t-c <clock>   : real CAN system clock in Hz\n",
104                cmd);
105
106         exit(EXIT_FAILURE);
107 }
108
109 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
110 {
111         uint8_t btr0, btr1;
112
113         if (hdr) {
114                 printf("BTR0 BTR1");
115         } else {
116                 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
117                 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
118                         (((bt->phase_seg2 - 1) & 0x7) << 4);
119                 printf("0x%02x 0x%02x", btr0, btr1);
120         }
121 }
122
123 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
124 {
125         if (hdr) {
126                 printf("%10s", "CAN_BR");
127         } else {
128                 uint32_t br = ((bt->phase_seg2 - 1) |
129                                ((bt->phase_seg1 - 1) << 4) |
130                                ((bt->prop_seg - 1) << 8) |
131                                ((bt->sjw - 1) << 12) |
132                                ((bt->brp - 1) << 16));
133                 printf("0x%08x", br);
134         }
135 }
136
137 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
138 {
139         if (hdr) {
140                 printf("%10s", "CAN_CTRL");
141         } else {
142                 uint32_t ctrl = (((bt->brp        - 1) << 24) |
143                                  ((bt->sjw        - 1) << 22) |
144                                  ((bt->phase_seg1 - 1) << 19) |
145                                  ((bt->phase_seg2 - 1) << 16) |
146                                  ((bt->prop_seg   - 1) <<  0));
147
148                 printf("0x%08x", ctrl);
149         }
150 }
151
152 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
153 {
154         uint8_t cnf1, cnf2, cnf3;
155
156         if (hdr) {
157                 printf("CNF1 CNF2 CNF3");
158         } else {
159                 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
160                 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
161                 cnf3 = bt->phase_seg2 - 1;
162                 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
163         }
164 }
165
166 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
167 {
168         if (hdr) {
169                 printf("%10s", "CANBTC");
170         } else {
171                 uint32_t can_btc;
172
173                 can_btc = (bt->phase_seg2 - 1) & 0x7;
174                 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
175                             & 0xF) << 3;
176                 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
177                 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
178
179                 printf("0x%08x", can_btc);
180         }
181 }
182
183 #define RCAR_CAN_BCR_TSEG1(x)   (((x) & 0x0f) << 20)
184 #define RCAR_CAN_BCR_BPR(x)     (((x) & 0x3ff) << 8)
185 #define RCAR_CAN_BCR_SJW(x)     (((x) & 0x3) << 4)
186 #define RCAR_CAN_BCR_TSEG2(x)   ((x) & 0x07)
187
188 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
189 {
190         if (hdr) {
191                 printf("%10s", "CiBCR");
192         } else {
193                 uint32_t bcr;
194
195                 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
196                         RCAR_CAN_BCR_BPR(bt->brp - 1) |
197                         RCAR_CAN_BCR_SJW(bt->sjw - 1) |
198                         RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
199
200                 printf("0x%08x", bcr << 8);
201         }
202 }
203
204 static struct calc_bittiming_const can_calc_consts[] = {
205         {
206                 .bittiming_const = {
207                         .name = "sja1000",
208                         .tseg1_min = 1,
209                         .tseg1_max = 16,
210                         .tseg2_min = 1,
211                         .tseg2_max = 8,
212                         .sjw_max = 4,
213                         .brp_min = 1,
214                         .brp_max = 64,
215                         .brp_inc = 1,
216                 },
217                 .ref_clk = 8000000,
218                 .printf_btr = printf_btr_sja1000,
219         }, {
220                 .bittiming_const = {
221                         .name = "mscan",
222                         .tseg1_min = 4,
223                         .tseg1_max = 16,
224                         .tseg2_min = 2,
225                         .tseg2_max = 8,
226                         .sjw_max = 4,
227                         .brp_min = 1,
228                         .brp_max = 64,
229                         .brp_inc = 1,
230                 },
231                 .ref_clk = 32000000,
232                 .printf_btr = printf_btr_sja1000,
233         }, {
234                 .bittiming_const = {
235                         .name = "mscan",
236                         .tseg1_min = 4,
237                         .tseg1_max = 16,
238                         .tseg2_min = 2,
239                         .tseg2_max = 8,
240                         .sjw_max = 4,
241                         .brp_min = 1,
242                         .brp_max = 64,
243                         .brp_inc = 1,
244                 },
245                 .ref_clk = 33000000,
246                 .printf_btr = printf_btr_sja1000,
247         }, {
248                 .bittiming_const = {
249                         .name = "mscan",
250                         .tseg1_min = 4,
251                         .tseg1_max = 16,
252                         .tseg2_min = 2,
253                         .tseg2_max = 8,
254                         .sjw_max = 4,
255                         .brp_min = 1,
256                         .brp_max = 64,
257                         .brp_inc = 1,
258                 },
259                 .ref_clk = 33300000,
260                 .printf_btr = printf_btr_sja1000,
261         }, {
262                 .bittiming_const = {
263                         .name = "mscan",
264                         .tseg1_min = 4,
265                         .tseg1_max = 16,
266                         .tseg2_min = 2,
267                         .tseg2_max = 8,
268                         .sjw_max = 4,
269                         .brp_min = 1,
270                         .brp_max = 64,
271                         .brp_inc = 1,
272                 },
273                 .ref_clk = 33333333,
274                 .printf_btr = printf_btr_sja1000,
275         }, {
276                 .bittiming_const = {
277                         .name = "mscan",
278                         .tseg1_min = 4,
279                         .tseg1_max = 16,
280                         .tseg2_min = 2,
281                         .tseg2_max = 8,
282                         .sjw_max = 4,
283                         .brp_min = 1,
284                         .brp_max = 64,
285                         .brp_inc = 1,
286                 },
287                 .ref_clk = 66660000,    /* mpc5121 */
288                 .printf_btr = printf_btr_sja1000,
289         }, {
290                 .bittiming_const = {
291                         .name = "mscan",
292                         .tseg1_min = 4,
293                         .tseg1_max = 16,
294                         .tseg2_min = 2,
295                         .tseg2_max = 8,
296                         .sjw_max = 4,
297                         .brp_min = 1,
298                         .brp_max = 64,
299                         .brp_inc = 1,
300                 },
301                 .ref_clk = 66666666,    /* mpc5121 */
302                 .printf_btr = printf_btr_sja1000,
303         }, {
304                 .bittiming_const = {
305                         .name = "at91",
306                         .tseg1_min = 4,
307                         .tseg1_max = 16,
308                         .tseg2_min = 2,
309                         .tseg2_max = 8,
310                         .sjw_max = 4,
311                         .brp_min = 2,
312                         .brp_max = 128,
313                         .brp_inc = 1,
314                 },
315                 .ref_clk = 100000000,
316                 .printf_btr = printf_btr_at91,
317         }, {
318                 .bittiming_const = {
319                         .name = "at91",
320                         .tseg1_min = 4,
321                         .tseg1_max = 16,
322                         .tseg2_min = 2,
323                         .tseg2_max = 8,
324                         .sjw_max = 4,
325                         .brp_min = 2,
326                         .brp_max = 128,
327                         .brp_inc = 1,
328                 },
329                 /* real world clock as found on the ronetix PM9263 */
330                 .ref_clk = 99532800,
331                 .printf_btr = printf_btr_at91,
332         }, {
333                 .bittiming_const = {
334                         .name = "flexcan",
335                         .tseg1_min = 4,
336                         .tseg1_max = 16,
337                         .tseg2_min = 2,
338                         .tseg2_max = 8,
339                         .sjw_max = 4,
340                         .brp_min = 1,
341                         .brp_max = 256,
342                         .brp_inc = 1,
343                 },
344                 .ref_clk = 24000000,    /* mx28 */
345                 .printf_btr = printf_btr_flexcan,
346         }, {
347                 .bittiming_const = {
348                         .name = "flexcan",
349                         .tseg1_min = 4,
350                         .tseg1_max = 16,
351                         .tseg2_min = 2,
352                         .tseg2_max = 8,
353                         .sjw_max = 4,
354                         .brp_min = 1,
355                         .brp_max = 256,
356                         .brp_inc = 1,
357                 },
358                 .ref_clk = 30000000,    /* mx6 */
359                 .printf_btr = printf_btr_flexcan,
360         }, {
361                 .bittiming_const = {
362                         .name = "flexcan",
363                         .tseg1_min = 4,
364                         .tseg1_max = 16,
365                         .tseg2_min = 2,
366                         .tseg2_max = 8,
367                         .sjw_max = 4,
368                         .brp_min = 1,
369                         .brp_max = 256,
370                         .brp_inc = 1,
371                 },
372                 .ref_clk = 49875000,
373                 .printf_btr = printf_btr_flexcan,
374         }, {
375                 .bittiming_const = {
376                         .name = "flexcan",
377                         .tseg1_min = 4,
378                         .tseg1_max = 16,
379                         .tseg2_min = 2,
380                         .tseg2_max = 8,
381                         .sjw_max = 4,
382                         .brp_min = 1,
383                         .brp_max = 256,
384                         .brp_inc = 1,
385                 },
386                 .ref_clk = 66000000,
387                 .printf_btr = printf_btr_flexcan,
388         }, {
389                 .bittiming_const = {
390                         .name = "flexcan",
391                         .tseg1_min = 4,
392                         .tseg1_max = 16,
393                         .tseg2_min = 2,
394                         .tseg2_max = 8,
395                         .sjw_max = 4,
396                         .brp_min = 1,
397                         .brp_max = 256,
398                         .brp_inc = 1,
399                 },
400                 .ref_clk = 66500000,
401                 .printf_btr = printf_btr_flexcan,
402         }, {
403                 .bittiming_const = {
404                         .name = "flexcan",
405                         .tseg1_min = 4,
406                         .tseg1_max = 16,
407                         .tseg2_min = 2,
408                         .tseg2_max = 8,
409                         .sjw_max = 4,
410                         .brp_min = 1,
411                         .brp_max = 256,
412                         .brp_inc = 1,
413                 },
414                 .ref_clk = 66666666,
415                 .printf_btr = printf_btr_flexcan,
416         }, {
417                 .bittiming_const = {
418                         .name = "flexcan",
419                         .tseg1_min = 4,
420                         .tseg1_max = 16,
421                         .tseg2_min = 2,
422                         .tseg2_max = 8,
423                         .sjw_max = 4,
424                         .brp_min = 1,
425                         .brp_max = 256,
426                         .brp_inc = 1,
427                 },
428                 .ref_clk = 83368421,
429                 .printf_btr = printf_btr_flexcan, /* vybrid */
430         }, {
431                 .bittiming_const = {
432                         .name = "mcp251x",
433                         .tseg1_min = 3,
434                         .tseg1_max = 16,
435                         .tseg2_min = 2,
436                         .tseg2_max = 8,
437                         .sjw_max = 4,
438                         .brp_min = 1,
439                         .brp_max = 64,
440                         .brp_inc = 1,
441                 },
442                 .ref_clk = 8000000,
443                 .printf_btr = printf_btr_mcp251x,
444         }, {
445                 .bittiming_const = {
446                         .name = "mcp251x",
447                         .tseg1_min = 3,
448                         .tseg1_max = 16,
449                         .tseg2_min = 2,
450                         .tseg2_max = 8,
451                         .sjw_max = 4,
452                         .brp_min = 1,
453                         .brp_max = 64,
454                         .brp_inc = 1,
455                 },
456                 .ref_clk = 16000000,
457                 .printf_btr = printf_btr_mcp251x,
458         }, {
459                 .bittiming_const = {
460                         .name = "ti_hecc",
461                         .tseg1_min = 1,
462                         .tseg1_max = 16,
463                         .tseg2_min = 1,
464                         .tseg2_max = 8,
465                         .sjw_max = 4,
466                         .brp_min = 1,
467                         .brp_max = 256,
468                         .brp_inc = 1,
469                 },
470                 .ref_clk = 13000000,
471                 .printf_btr = printf_btr_ti_hecc,
472         }, {
473                 .bittiming_const = {
474                         .name = "rcar_can",
475                         .tseg1_min = 4,
476                         .tseg1_max = 16,
477                         .tseg2_min = 2,
478                         .tseg2_max = 8,
479                         .sjw_max = 4,
480                         .brp_min = 1,
481                         .brp_max = 1024,
482                         .brp_inc = 1,
483                 },
484                 .ref_clk = 65000000,
485                 .printf_btr = printf_btr_rcar_can,
486         },
487 };
488
489 static long common_bitrates[] = {
490         1000000,
491         800000,
492         500000,
493         250000,
494         125000,
495         100000,
496         50000,
497         20000,
498         10000,
499 };
500
501 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
502
503 static int can_update_spt(const struct can_bittiming_const *btc,
504                           int sampl_pt, int tseg, int *tseg1, int *tseg2)
505 {
506         *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
507         if (*tseg2 < btc->tseg2_min)
508                 *tseg2 = btc->tseg2_min;
509         if (*tseg2 > btc->tseg2_max)
510                 *tseg2 = btc->tseg2_max;
511         *tseg1 = tseg - *tseg2;
512         if (*tseg1 > btc->tseg1_max) {
513                 *tseg1 = btc->tseg1_max;
514                 *tseg2 = tseg - *tseg1;
515         }
516         return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
517 }
518
519 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
520 {
521         struct can_priv *priv = netdev_priv(dev);
522         const struct can_bittiming_const *btc = priv->bittiming_const;
523         long rate = 0;
524         long best_error = 1000000000, error = 0;
525         int best_tseg = 0, best_brp = 0, brp = 0;
526         int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
527         int spt_error = 1000, spt = 0, sampl_pt;
528         u64 v64;
529
530         if (!priv->bittiming_const)
531                 return -ENOTSUPP;
532
533         /* Use CIA recommended sample points */
534         if (bt->sample_point) {
535                 sampl_pt = bt->sample_point;
536         } else {
537                 if (bt->bitrate > 800000)
538                         sampl_pt = 750;
539                 else if (bt->bitrate > 500000)
540                         sampl_pt = 800;
541                 else
542                         sampl_pt = 875;
543         }
544
545         /* tseg even = round down, odd = round up */
546         for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
547              tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
548                 tsegall = 1 + tseg / 2;
549                 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
550                 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
551                 /* chose brp step which is possible in system */
552                 brp = (brp / btc->brp_inc) * btc->brp_inc;
553                 if ((brp < btc->brp_min) || (brp > btc->brp_max))
554                         continue;
555                 rate = priv->clock.freq / (brp * tsegall);
556                 error = bt->bitrate - rate;
557                 /* tseg brp biterror */
558                 if (error < 0)
559                         error = -error;
560                 if (error > best_error)
561                         continue;
562                 best_error = error;
563                 if (error == 0) {
564                         spt = can_update_spt(btc, sampl_pt, tseg / 2,
565                                              &tseg1, &tseg2);
566                         error = sampl_pt - spt;
567                         if (error < 0)
568                                 error = -error;
569                         if (error > spt_error)
570                                 continue;
571                         spt_error = error;
572                 }
573                 best_tseg = tseg / 2;
574                 best_brp = brp;
575                 if (error == 0)
576                         break;
577         }
578
579         if (best_error) {
580                 /* Error in one-tenth of a percent */
581                 error = (best_error * 1000) / bt->bitrate;
582                 if (error > CAN_CALC_MAX_ERROR) {
583                         dev_err(dev->dev.parent,
584                                 "bitrate error %ld.%ld%% too high\n",
585                                 error / 10, error % 10);
586                         return -EDOM;
587                 } else {
588                         dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
589                                  error / 10, error % 10);
590                 }
591         }
592
593         /* real sample point */
594         bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
595                                           &tseg1, &tseg2);
596
597         v64 = (u64)best_brp * 1000000000UL;
598         do_div(v64, priv->clock.freq);
599         bt->tq = (u32)v64;
600         bt->prop_seg = tseg1 / 2;
601         bt->phase_seg1 = tseg1 - bt->prop_seg;
602         bt->phase_seg2 = tseg2;
603         bt->sjw = 1;
604         bt->brp = best_brp;
605
606         /* real bit-rate */
607         bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
608
609         return 0;
610 }
611
612 static __u32 get_cia_sample_point(__u32 bitrate)
613 {
614         __u32 sampl_pt;
615
616         if (bitrate > 800000)
617                 sampl_pt = 750;
618         else if (bitrate > 500000)
619                 sampl_pt = 800;
620         else
621                 sampl_pt = 875;
622
623         return sampl_pt;
624 }
625
626 static void print_bit_timing(const struct calc_bittiming_const *btc,
627                              __u32 bitrate, __u32 sample_point, __u32 ref_clk,
628                              bool quiet)
629 {
630         struct net_device dev = {
631                 .priv.bittiming_const = &btc->bittiming_const,
632                 .priv.clock.freq = ref_clk,
633         };
634         struct can_bittiming bt = {
635                 .bitrate = bitrate,
636                 .sample_point = sample_point,
637         };
638         long rate_error, spt_error;
639
640         if (!quiet) {
641                 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
642                        "nominal                                 real Bitrt   nom  real SampP\n"
643                        "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
644                        btc->bittiming_const.name,
645                        ref_clk / 1000000.0);
646
647                 btc->printf_btr(&bt, true);
648                 printf("\n");
649         }
650
651         if (can_calc_bittiming(&dev, &bt)) {
652                 printf("%7d ***bitrate not possible***\n", bitrate);
653                 return;
654         }
655
656         /* get nominal sample point */
657         if (!sample_point)
658                 sample_point = get_cia_sample_point(bitrate);
659
660         rate_error = abs((__s32)(bitrate - bt.bitrate));
661         spt_error = abs((__s32)(sample_point - bt.sample_point));
662
663         printf("%7d "
664                "%6d %3d %4d %4d "
665                "%3d %3d "
666                "%7d %4.1f%% "
667                "%4.1f%% %4.1f%% %4.1f%% ",
668                bitrate,
669                bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
670                bt.sjw, bt.brp,
671
672                bt.bitrate,
673                100.0 * rate_error / bitrate,
674
675                sample_point / 10.0,
676                bt.sample_point / 10.0,
677                100.0 * spt_error / sample_point);
678
679         btc->printf_btr(&bt, false);
680         printf("\n");
681 }
682
683 static void do_list(void)
684 {
685         unsigned int i;
686
687         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
688                 printf("%s\n", can_calc_consts[i].bittiming_const.name);
689 }
690
691 int main(int argc, char *argv[])
692 {
693         __u32 bitrate = 0;
694         __u32 opt_ref_clk = 0, ref_clk;
695         int sampl_pt = 0;
696         bool quiet = false, list = false, found = false;
697         char *name = NULL;
698         unsigned int i, j;
699         int opt;
700
701         const struct calc_bittiming_const *btc = NULL;
702
703         while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
704                 switch (opt) {
705                 case 'b':
706                         bitrate = atoi(optarg);
707                         break;
708
709                 case 'c':
710                         opt_ref_clk = atoi(optarg);
711                         break;
712
713                 case 'l':
714                         list = true;
715                         break;
716
717                 case 'q':
718                         quiet = true;
719                         break;
720
721                 case 's':
722                         sampl_pt = atoi(optarg);
723                         break;
724
725                 default:
726                         print_usage(argv[0]);
727                         break;
728                 }
729         }
730
731         if (argc > optind + 1)
732                 print_usage(argv[0]);
733
734         if (argc == optind + 1)
735                 name = argv[optind];
736
737         if (list) {
738                 do_list();
739                 exit(EXIT_SUCCESS);
740         }
741
742         if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
743                 print_usage(argv[0]);
744
745         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
746                 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
747                         continue;
748
749                 found = true;
750                 btc = &can_calc_consts[i];
751
752                 if (opt_ref_clk)
753                         ref_clk = opt_ref_clk;
754                 else
755                         ref_clk = btc->ref_clk;
756
757                 if (bitrate) {
758                         print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
759                 } else {
760                         for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
761                                 print_bit_timing(btc, common_bitrates[j],
762                                                  sampl_pt, ref_clk, j);
763                 }
764                 printf("\n");
765         }
766
767         if (!found) {
768                 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);
769                 do_list();
770                 exit(EXIT_FAILURE);
771         }
772
773         exit(EXIT_SUCCESS);
774 }