struct ring_buffer { int head; int tail; struct msg *data; int size; unsigned int capacity; }; struct msg { u16 module_id; u16 cmd_id; u16 cmd_subid; u16 complete; u8 data[128]; };struct pokemon_uart_port { struct uart_port port; struct clk *clk; const struct vendor_data vendor; unsigned int im; / interrupt mask / unsigned int old_status; unsigned int fifosize; unsigned int old_cr; / state during shutdown */ unsigned int fixed_baud; struct ring_buffer tx_buf; struct ring_buffer rx_buf; char type[12]; };struct ring_buffer ring_buffer_init(unsigned int capacity) { struct ring_buffer rbuf=kmalloc(sizeof(struct ring_buffer),GFP_KERNEL); rbuf->capacity=capacity; rbuf->head = rbuf->size=0; rbuf->tail = capacity - 1; rbuf->data = kmalloc(rbuf->capacity * sizeof(struct msg), GFP_KERNEL); printk(KERN_DEBUG "ring_buffer create successfully!/n"); return rbuf; }static int pokemon_uart_probe(struct amba_device *dev, const struct amba_id *id) { struct pokemon_uart_port *pup; struct vendor_data *vendor = id->data; int portnr, ret; portnr = pokemon_find_free_port(); if (portnr < 0) return portnr; pup = devm_kzalloc(&dev->dev, sizeof(struct pokemon_uart_port), GFP_KERNEL); if(!pup) return -ENOMEM; pup->clk = devm_clk_get(&dev->dev, NULL); if(IS_ERR(pup->clk)) return PTR_ERR(pup->clk); pup->port.irq = dev->irq[0]; pup->port.line = portnr; pup->vendor = vendor; pup->fifosize = 32; pup->port.iotype = pup->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM; pup->port.ops = &pokemon_uart_ops; snprintf(pup->type, sizeof(pup->type), "PL011 rev%u", amba_rev(dev)); pup->tx_buf = ring_buffer_init(10); pup->rx_buf = ring_buffer_init(10); ret = pokemon_setup_port(&dev->dev, pup, &dev->res, portnr); if (ret) return ret; amba_set_drvdata(dev, pup); return pokemon_register_port(pup); }检查一下这段linux内核驱动代码中,有无代码逻辑和格式错误,如果有,请给出修改之后的代码
时间: 2024-03-12 22:45:10 浏览: 161
Linux环境下一些主要实用数据结构简介
以下是修改后的代码:
```
struct ring_buffer {
int head;
int tail;
struct msg *data;
int size;
unsigned int capacity;
};
struct msg {
u16 module_id;
u16 cmd_id;
u16 cmd_subid;
u16 complete;
u8 data[128];
};
struct pokemon_uart_port {
struct uart_port port;
struct clk *clk;
const struct vendor_data vendor;
unsigned int im; /* interrupt mask */
unsigned int old_status;
unsigned int fifosize;
unsigned int old_cr; /* state during shutdown */
unsigned int fixed_baud;
struct ring_buffer tx_buf;
struct ring_buffer rx_buf;
char type[12];
};
struct ring_buffer *ring_buffer_init(unsigned int capacity) {
struct ring_buffer *rbuf = kmalloc(sizeof(struct ring_buffer), GFP_KERNEL);
if (!rbuf) {
return NULL;
}
rbuf->capacity = capacity;
rbuf->head = rbuf->size = 0;
rbuf->tail = capacity - 1;
rbuf->data = kmalloc(rbuf->capacity * sizeof(struct msg), GFP_KERNEL);
if (!rbuf->data) {
kfree(rbuf);
return NULL;
}
printk(KERN_DEBUG "ring_buffer create successfully!\n");
return rbuf;
}
static int pokemon_uart_probe(struct amba_device *dev, const struct amba_id *id) {
struct pokemon_uart_port *pup;
struct vendor_data *vendor = id->data;
int portnr, ret;
portnr = pokemon_find_free_port();
if (portnr < 0) {
return portnr;
}
pup = devm_kzalloc(&dev->dev, sizeof(struct pokemon_uart_port), GFP_KERNEL);
if (!pup) {
return -ENOMEM;
}
pup->clk = devm_clk_get(&dev->dev, NULL);
if (IS_ERR(pup->clk)) {
return PTR_ERR(pup->clk);
}
pup->port.irq = dev->irq[0];
pup->port.line = portnr;
pup->vendor = vendor;
pup->fifosize = 32;
pup->port.iotype = pup->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
pup->port.ops = &pokemon_uart_ops;
snprintf(pup->type, sizeof(pup->type), "PL011 rev%u", amba_rev(dev));
pup->tx_buf.data = NULL;
pup->rx_buf.data = NULL;
pup->tx_buf.capacity = 10;
pup->rx_buf.capacity = 10;
pup->tx_buf.size = 0;
pup->rx_buf.size = 0;
pup->tx_buf.head = 0;
pup->tx_buf.tail = pup->tx_buf.capacity - 1;
pup->rx_buf.head = 0;
pup->rx_buf.tail = pup->rx_buf.capacity - 1;
pup->tx_buf.data = kmalloc(pup->tx_buf.capacity * sizeof(struct msg), GFP_KERNEL);
if (!pup->tx_buf.data) {
devm_kfree(&dev->dev, pup);
return -ENOMEM;
}
pup->rx_buf.data = kmalloc(pup->rx_buf.capacity * sizeof(struct msg), GFP_KERNEL);
if (!pup->rx_buf.data) {
kfree(pup->tx_buf.data);
devm_kfree(&dev->dev, pup);
return -ENOMEM;
}
ret = pokemon_setup_port(&dev->dev, pup, &dev->res, portnr);
if (ret) {
kfree(pup->tx_buf.data);
kfree(pup->rx_buf.data);
devm_kfree(&dev->dev, pup);
return ret;
}
amba_set_drvdata(dev, pup);
ret = pokemon_register_port(pup);
if (ret) {
kfree(pup->tx_buf.data);
kfree(pup->rx_buf.data);
devm_kfree(&dev->dev, pup);
return ret;
}
return 0;
}
```
修改说明:
1. `ring_buffer_init` 函数返回类型应该是 `struct ring_buffer *` 而不是 `struct ring_buffer`。
2. `ring_buffer_init` 函数内存分配失败时需要释放已经分配的内存。
3. `pup->tx_buf` 和 `pup->rx_buf` 的 `data` 成员在初始化之前需要置为 `NULL`。
4. `pup->tx_buf` 和 `pup->rx_buf` 的 `size` 成员在初始化之前需要置为 `0`。
5. `pup->tx_buf` 和 `pup->rx_buf` 的 `head` 和 `tail` 成员在初始化时需要分别设置为 `0` 和 `capacity - 1`。
6. 在 `pokemon_uart_probe` 函数中,`pup->tx_buf` 和 `pup->rx_buf` 的 `data` 成员需要进行内存分配,并在分配失败时要释放已经分配的内存。
7. 在 `pokemon_uart_probe` 函数中,需要在成功分配内存后初始化 `pup->tx_buf` 和 `pup->rx_buf` 的 `capacity` 成员。
阅读全文