summaryrefslogtreecommitdiffstats
path: root/lib_generic/misc.c
blob: 31db4621832a7dabf630bf75a5766077128f0541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

#include <common.h>
#include <command.h>
#include <driver.h>
#include <init.h>
#include <malloc.h>
#include <linux/ctype.h>
#include <errno.h>
#include <fs.h>

int cmd_get_data_size(char* arg, int default_size)
{
	/* Check for a size specification .b, .w or .l.
	 */
	int len = strlen(arg);
	if (len > 2 && arg[len-2] == '.') {
		switch(arg[len-1]) {
		case 'b':
			return 1;
		case 'w':
			return 2;
		case 'l':
			return 4;
		case 's':
			return -2;
		default:
			return -1;
		}
	}
	return default_size;
}

static struct device_d *first_device = NULL;
static struct driver_d *first_driver = NULL;

struct device_d *get_first_device(void)
{
	return first_device;
}

struct device_d *get_device_by_id(const char *_id)
{
	struct device_d *d;
	char *id, *colon;

	/* FIXME: is this still needed? */
	id = strdup(_id);
	if ((colon = strchr(id, ':')))
		*colon = 0;

	d = first_device;

	while(d) {
		if(!strcmp(id, d->id))
			break;
		d = d->next;
	}

	free(id);
	return d;
}

int get_free_deviceid(char *id, char *id_template)
{
	int i = 0;

	while (1) {
		sprintf(id, "%s%d", id_template, i);
		if (!get_device_by_id(id))
			return 0;
		i++;
	};

	return -1;
}

static int match(struct driver_d *drv, struct device_d *dev)
{
        if (strcmp(dev->name, drv->name))
                return -1;
        if (dev->type != drv->type)
                return -1;
        if(drv->probe(dev))
                return -1;

        dev->driver = drv;

        return 0;
}

int register_device(struct device_d *new_device)
{
	struct driver_d *drv;
        struct device_d *dev;

	dev = first_device;

	if(*new_device->id && get_device_by_id(new_device->id)) {
		printf("device %s already exists\n", new_device->id);
		return -EINVAL;
	}
//        printf("register_device: %s\n",new_device->name);

	if(!dev) {
		first_device = new_device;
		dev = first_device;
	} else {
		while(dev->next)
			dev = dev->next;
	}

	dev->next = new_device;
	new_device->next = 0;

        drv = first_driver;

        while(drv) {
                if (!match(drv, new_device))
                        break;
                drv = drv->next;
        }

	return 0;
}

void unregister_device(struct device_d *old_dev)
{
        struct device_d *dev;
//        printf("unregister_device: %s\n",old_dev->name);

	dev = first_device;

        while (dev) {
                if (!strcmp(dev->next->name, old_dev->name)) {
			if (old_dev->driver)
				old_dev->driver->remove(old_dev);
                        dev->next = old_dev->next;
                        return;
                }
                dev = dev->next;
        }
}

struct driver_d *get_driver_by_name(const char *name)
{
	struct driver_d *d;

	d = first_driver;

	while(d) {
		if(!strcmp(name, d->name))
			break;
		d = d->next;
	}

	return d;
}

static void noinfo(struct device_d *dev)
{
        printf("no info available for %s\n", dev->id);
}

static void noshortinfo(struct device_d *dev)
{
}

int register_driver(struct driver_d *new_driver)
{
	struct driver_d *drv;
        struct device_d *dev = NULL;

	drv = first_driver;

//        printf("register_driver: %s\n",new_driver->name);

	if(!drv) {
		first_driver = new_driver;
		drv = first_driver;
	} else {
		while(drv->next)
			drv = drv->next;
	}

	drv->next = new_driver;
	new_driver->next = 0;

        if (!new_driver->info)
                new_driver->info = noinfo;
        if (!new_driver->shortinfo)
                new_driver->shortinfo = noshortinfo;

        dev = first_device;
        while (dev) {
                match(new_driver, dev);
                dev = dev->next;
        }

	return 0;
}

static char devicename_from_spec_str_buf[PATH_MAX];

char *deviceid_from_spec_str(const char *str, char **endp)
{
        char *buf = devicename_from_spec_str_buf;
        const char *end;
	int i = 0;

        if (isdigit(*str)) {
                /* No device name given, use default driver mem */
                sprintf(buf, "mem");
                end = str;
        } else {
                /* OK, we have a device name, parse it */
	        while (*str) {
		        if (*str == ':') {
                                str++;
			        buf[i] = 0;
			        break;
        		}

	        	buf[i++] = *str++;
		        buf[i] = 0;
		        if (i == MAX_DRIVER_NAME)
			        return NULL;
        	}
                end = str;
        }

        if (endp)
                *endp = (char *)end;

        return buf;
}

/* Get a device struct from the beginning of the string. Default to mem if no
 * device is given, return NULL if a unknown device is given.
 * If endp is not NULL, this function stores a pointer to the first character
 * after the device name in *endp.
 */
struct device_d *device_from_spec_str(const char *str, char **endp)
{
        char *name;
        name = deviceid_from_spec_str(str, endp);
        return get_device_by_id(name);
}

/* Get devices from their type.
 * If last is NULL the first device of this type is given.
 * If last is not NULL, the next device of this type starting
 * from last is given.
 */
struct device_d *get_device_by_type(ulong type, struct device_d *last)
{
	struct device_d *dev;

	if (!last)
		dev = first_device;
	else
		dev = last->next;

	while (dev) {
		if (dev->type == type)
			return dev;
		dev = dev->next;
	}

	return NULL;
}

unsigned long strtoul_suffix(const char *str, char **endp, int base)
{
        unsigned long val;
        char *end;

        val = simple_strtoul(str, &end, base);

        switch (*end) {
        case 'G':
                val *= 1024;
        case 'M':
		val *= 1024;
        case 'k':
        case 'K':
                val *= 1024;
                end++;
        }

        if (endp)
                *endp = (char *)end;

        return val;
}

int parse_area_spec(const char *str, ulong *start, ulong *size)
{
	char *endp;

	if (*str == '+') {
		/* no beginning given but size so start is 0 */
		*start = 0;
		*size = strtoul_suffix(str + 1, &endp, 0);
		return 0;
	}

	if (*str == '-') {
		/* no beginning given but end, so start is 0 */
		*start = 0;
		*size = strtoul_suffix(str + 1, &endp, 0) + 1;
		return 0;
	}

	*start = strtoul_suffix(str, &endp, 0);

	str = endp;

	if (!*str) {
		/* beginning given, but no size, assume maximum size */
		*size = ~0;
		return 0;
	}

	if (*str == '-') {
                /* beginning and end given */
		*size = strtoul_suffix(str + 1, NULL, 0) + 1;
		return 0;
	}

	if (*str == '+') {
                /* beginning and size given */
		*size = strtoul_suffix(str + 1, NULL, 0);
		return 0;
	}

	return -1;
}

int spec_str_to_info(const char *str, struct memarea_info *info)
{
	char *endp;
	int ret;

	info->device = device_from_spec_str(str, &endp);
	if (!info->device) {
		printf("unknown device: %s\n", deviceid_from_spec_str(str, NULL));
		return -ENODEV;
	}

	ret = parse_area_spec(endp, &info->start, &info->size);
	if (ret)
		return ret;

	if (info->size == ~0)
		info->size = info->device->size;

	return 0;
}

ssize_t dev_read(struct device_d *dev, void *buf, size_t count, unsigned long offset, ulong flags)
{
        if (dev->driver->read)
                return dev->driver->read(dev, buf, count, offset, flags);
	errno = -ENOSYS;
        return -ENOSYS;
}

ssize_t dev_write(struct device_d *dev, const void *buf, size_t count, unsigned long offset, ulong flags)
{
        if (dev->driver->write)
                return dev->driver->write(dev, buf, count, offset, flags);
	errno = -ENOSYS;
        return -ENOSYS;
}

ssize_t dev_erase(struct device_d *dev, size_t count, unsigned long offset)
{
        if (dev->driver->erase)
                return dev->driver->erase(dev, count, offset);
	errno = -ENOSYS;
        return -ENOSYS;
}

int dummy_probe(struct device_d *dev)
{
        return 0;
}

struct param_d *get_param_by_name(struct device_d *dev, char *name)
{
        struct param_d *param = dev->param;

        while (param) {
                if (!strcmp(param->name, name))
                        return param;
                param = param->next;
        }

        return NULL;
}

void print_param(struct param_d *param) {
	switch (param->type) {
	case PARAM_TYPE_STRING:
		printf("%s", param->value.val_str);
		break;
	case PARAM_TYPE_ULONG:
		printf("%ld", param->value.val_ulong);
		break;
	case PARAM_TYPE_IPADDR:
		print_IPaddr(param->value.val_ip);
		break;
	}
}

struct param_d* dev_get_param(struct device_d *dev, char *name)
{
        struct param_d *param = get_param_by_name(dev, name);

	if (!param) {
		errno = -EINVAL;
		return NULL;
	}

        if (param->get)
                return param->get(dev, param);

        return param;
}

IPaddr_t dev_get_param_ip(struct device_d *dev, char *name)
{
	struct param_d *param = dev_get_param(dev, name);

	if (!param || param->type != PARAM_TYPE_IPADDR) {
		errno = -EINVAL;
		return 0;
	}

	return param->value.val_ip;
}

int dev_set_param_ip(struct device_d *dev, char *name, IPaddr_t ip)
{
	value_t val;

	val.val_ip = ip;

	return dev_set_param(dev, name, val);
}

int dev_set_param(struct device_d *dev, char *name, value_t val)
{
        struct param_d *param;

	if (!dev) {
		errno = -ENODEV;
		return -ENODEV;
	}

	param = get_param_by_name(dev, name);

	if (!param) {
		errno = -EINVAL;
		return -EINVAL;
	}

	if (param->flags & PARAM_FLAG_RO) {
		errno = -EACCES;
		return -EACCES;
	}

        if (param->set)
                return param->set(dev, param, val);

	if (param->type == PARAM_TYPE_STRING) {
		if (param->value.val_str)
			free(param->value.val_str);
		param->value.val_str = strdup(val.val_str);
		return 0;
	}

        param->value = val;
	return 0;
}

int dev_add_parameter(struct device_d *dev, struct param_d *newparam)
{
        struct param_d *param = dev->param;

        newparam->next = 0;

        if (param) {
                while (param->next)
                        param = param->next;
                        param->next = newparam;
        } else {
                dev->param = newparam;
        }

        return 0;
}

int do_devinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
        struct device_d *dev = first_device;
        struct driver_d *drv = first_driver;
        struct param_d *param;

        if (argc == 1) {
                printf("devices:\n");

                while(dev) {
                        printf("%10s: base=0x%08x size=0x%08x (driver %s)\n",
                                        dev->id, dev->map_base, dev->size, dev->driver ? dev->driver->name : "none");
                        dev = dev->next;
                }

                printf("drivers:\n");
                while(drv) {
                        printf("%10s\n",drv->name);
                        drv = drv->next;
                }
        } else {
                struct device_d *dev = get_device_by_id(argv[1]);

                if (!dev) {
                        printf("no such device: %s\n",argv[1]);
                        return -1;
                }

                if (dev->driver)
                        dev->driver->info(dev);

                param = dev->param;

                printf("%s\n", param ? "Parameters:" : "no parameters available");

                while (param) {
			printf("%16s = ", param->name);
			print_param(param);
			printf("\n");
                        param = param->next;
                }

        }

        return 0;
}

U_BOOT_CMD(
	devinfo,     2,     0,      do_devinfo,
	"devinfo     - display info about devices and drivers\n",
	""
);