embox

Форк
0
/
uart_dev_dynamic_repo.c 
97 строк · 2.2 Кб
1
/**
2
 * @file
3
 * @brief
4
 *
5
 * @author  Anton Kozlov
6
 * @date    09.08.2013
7
 */
8

9
#include <errno.h>
10
#include <stdio.h>
11
#include <string.h>
12

13
#include <drivers/device.h>
14
#include <drivers/serial/uart_dev.h>
15
#include <drivers/ttys.h>
16
#include <kernel/irq.h>
17
#include <lib/libds/array.h>
18
#include <lib/libds/dlist.h>
19
#include <lib/libds/indexator.h>
20
#include <lib/libds/ring_buff.h>
21
#include <mem/misc/pool.h>
22
#include <util/err.h>
23
#include <util/log.h>
24

25
ARRAY_SPREAD_DECLARE(struct uart *const, __uart_device_registry);
26

27
#define UART_MAX_N OPTION_GET(NUMBER, uart_max_n)
28

29
INDEX_DEF(serial_indexator, 0, UART_MAX_N);
30
static struct uart *__uart_device_dynamic_registry[UART_MAX_N];
31

32
int uart_register(struct uart *uart, const struct uart_params *uart_defparams) {
33
	int res;
34
	size_t allocated_idx;
35
	struct uart *existed_uart_dev;
36

37
	array_spread_foreach(existed_uart_dev, __uart_device_registry) {
38
		if (uart == existed_uart_dev) {
39
			/* uart has been inited statically */
40
			return 0;
41
		}
42
	}
43

44
	allocated_idx = index_alloc(&serial_indexator, INDEX_MIN);
45
	if (allocated_idx == INDEX_NONE) {
46
		return -EBUSY;
47
	}
48
	uart->idx = (unsigned char)allocated_idx;
49

50
	snprintf(uart->dev_name, UART_NAME_MAXLEN, "ttyS%d", uart->idx);
51

52
	if (uart_defparams) {
53
		memcpy(&uart->params, uart_defparams, sizeof(struct uart_params));
54
	}
55
	else {
56
		memset(&uart->params, 0, sizeof(struct uart_params));
57
	}
58

59
	res = ttys_register(uart->dev_name, uart);
60
	if (0 != res) {
61
		log_error("Failed to register tty %s", uart->dev_name);
62
		index_free(&serial_indexator, uart->idx);
63
		return res;
64
	}
65

66
	__uart_device_dynamic_registry[uart->idx] = uart;
67

68
	return 0;
69
}
70

71
void uart_deregister(struct uart *uart) {
72
	dlist_del(&uart->uart_lnk);
73

74
	index_free(&serial_indexator, uart->idx);
75
}
76

77
struct uart *uart_dev_lookup(const char *name) {
78
	int i;
79
	struct uart *uart = NULL;
80

81
	array_spread_foreach(uart, __uart_device_registry) {
82
		if (0 == strcmp(uart->dev_name, name)) {
83
			return uart;
84
		}
85
	}
86
	for (i = 0; i < ARRAY_SIZE(__uart_device_dynamic_registry); i++) {
87
		if (NULL == __uart_device_dynamic_registry[i]) {
88
			continue;
89
		}
90
		uart = __uart_device_dynamic_registry[i];
91
		if (0 == strcmp(uart->dev_name, name)) {
92
			return uart;
93
		}
94
	}
95

96
	return NULL;
97
}
98

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.