linux下,如何使用shell命令把素有字符串#if (PCIEID == 0x1106) qdf_export_symbol(*****); #endif替换成qdf_export_symbol(*****);其中*****为可变字符串
时间: 2024-03-01 15:48:11 浏览: 139
在Linux下,你可以使用sed命令来替换字符串。sed是一个流编辑器,它可以对文本进行各种操作,包括替换。
要将字符串#if (PCIEID == 0x1106)
qdf_export_symbol(*****);
#endif替换为qdf_export_symbol(*****),你可以使用以下的sed命令:
```shell
sed -i 's/#if (PCIEID == 0x1106)\nqdf_export_symbol(.*);\n#endif/qdf_export_symbol(*****);/g' 文件名
```
这个命令中的`-i`选项表示直接修改文件内容,`s/old/new/g`表示将old替换为new,`.*`表示匹配任意字符任意次数。
请将上述命令中的"文件名"替换为你要操作的文件名,然后执行该命令即可完成替换。
相关问题
static int sbsa_uart_probe(struct platform_device *pdev) { struct uart_amba_port *uap; struct resource *r; int portnr, ret; int baudrate; /* * Check the mandatory baud rate parameter in the DT node early * so that we can easily exit with the error. */ if (pdev->dev.of_node) { struct device_node *np = pdev->dev.of_node; ret = of_property_read_u32(np, "current-speed", &baudrate); if (ret) return ret; } else { baudrate = 115200; } portnr = pl011_find_free_port(); if (portnr < 0) return portnr; uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port), GFP_KERNEL); if (!uap) return -ENOMEM; ret = platform_get_irq(pdev, 0); if (ret < 0) { if (ret != -EPROBE_DEFER) dev_err(&pdev->dev, "cannot obtain irq\n"); return ret; } uap->port.irq = ret; #ifdef CONFIG_ACPI_SPCR_TABLE if (qdf2400_e44_present) { dev_info(&pdev->dev, "working around QDF2400 SoC erratum 44\n"); uap->vendor = &vendor_qdt_qdf2400_e44; } else #endif uap->vendor = &vendor_sbsa; uap->reg_offset = uap->vendor->reg_offset; uap->fifosize = 32; uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM; uap->port.ops = &sbsa_uart_pops; uap->fixed_baud = baudrate; snprintf(uap->type, sizeof(uap->type), "SBSA"); r = platform_get_resource(pdev, IORESOURCE_MEM, 0); ret = pl011_setup_port(&pdev->dev, uap, r, portnr); if (ret) return ret; platform_set_drvdata(pdev, uap); return pl011_register_port(uap); }linux内核uart驱动在设备注册时,使用acpi表定义的波特率来初始化串口,请根据我的要求和上述代码,在代码中添加这一功能
好的,根据你的要求,我可以在该函数中添加获取ACPI表中定义的波特率的功能,并使用该波特率来初始化串口。代码如下:
```
static int sbsa_uart_probe(struct platform_device *pdev) {
struct uart_amba_port *uap;
struct resource *r;
int portnr, ret;
int baudrate;
struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
/* Check if ACPI companion device exists, and if so, read the current-speed property */
if (adev) {
struct acpi_device_node *adev_node = acpi_node_get_device_node(adev->handle);
if (adev_node) {
struct fwnode_handle *fwnode = &adev_node->fwnode;
ret = fwnode_property_read_u32(fwnode, "current-speed", &baudrate);
if (ret)
return ret;
}
} else if (pdev->dev.of_node) {
/* If there is no ACPI companion device, fall back to reading the current-speed property from DT node */
struct device_node *np = pdev->dev.of_node;
ret = of_property_read_u32(np, "current-speed", &baudrate);
if (ret)
return ret;
} else {
baudrate = 115200;
}
portnr = pl011_find_free_port();
if (portnr < 0)
return portnr;
uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port), GFP_KERNEL);
if (!uap)
return -ENOMEM;
ret = platform_get_irq(pdev, 0);
if (ret < 0) {
if (ret != -EPROBE_DEFER)
dev_err(&pdev->dev, "cannot obtain irq\n");
return ret;
}
uap->port.irq = ret;
#ifdef CONFIG_ACPI_SPCR_TABLE
if (qdf2400_e44_present) {
dev_info(&pdev->dev, "working around QDF2400 SoC erratum 44\n");
uap->vendor = &vendor_qdt_qdf2400_e44;
} else
#endif
uap->vendor = &vendor_sbsa;
uap->reg_offset = uap->vendor->reg_offset;
uap->fifosize = 32;
uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
uap->port.ops = &sbsa_uart_pops;
uap->fixed_baud = baudrate;
snprintf(uap->type, sizeof(uap->type), "SBSA");
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
if (ret)
return ret;
platform_set_drvdata(pdev, uap);
return pl011_register_port(uap);
}
```
我添加的代码在函数开头处,首先判断是否存在ACPI companion device,如果存在则从其属性中读取波特率,否则从DT node中读取。读取到的波特率将被用于初始化串口。
static int sbsa_uart_probe(struct platform_device *pdev) { struct uart_amba_port *uap; struct resource r; int portnr, ret; int baudrate; / * Check the mandatory baud rate parameter in the DT node early * so that we can easily exit with the error. */ if (pdev->dev.of_node) { struct device_node *np = pdev->dev.of_node; ret = of_property_read_u32(np, "current-speed", &baudrate); if (ret) return ret; } else { baudrate = 115200; } portnr = pl011_find_free_port(); if (portnr < 0) return portnr; uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port), GFP_KERNEL); if (!uap) return -ENOMEM; ret = platform_get_irq(pdev, 0); if (ret < 0) { if (ret != -EPROBE_DEFER) dev_err(&pdev->dev, "cannot obtain irq\n"); return ret; } uap->port.irq = ret; #ifdef CONFIG_ACPI_SPCR_TABLE if (qdf2400_e44_present) { dev_info(&pdev->dev, "working around QDF2400 SoC erratum 44\n"); uap->vendor = &vendor_qdt_qdf2400_e44; } else #endif uap->vendor = &vendor_sbsa; uap->reg_offset = uap->vendor->reg_offset; uap->fifosize = 32; uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM; uap->port.ops = &sbsa_uart_pops; uap->fixed_baud = baudrate; snprintf(uap->type, sizeof(uap->type), "SBSA"); r = platform_get_resource(pdev, IORESOURCE_MEM, 0); ret = pl011_setup_port(&pdev->dev, uap, r, portnr); if (ret) return ret; platform_set_drvdata(pdev, uap); return pl011_register_port(uap); }在上述代码中,我需要添加一个功能:在以uefi方式启动系统时,uart驱动会读取acpi表内有关波特率的设置值,并以这个值进行串口波特率设置,请根据我的要求,在原代码中添加这一功能
可以按照以下步骤添加这个功能:
1. 在函数开头添加变量 `int baudrate_from_acpi`,表示从ACPI表中读取的波特率值。
2. 在if语句中添加对于UEFI启动的处理。可以通过检查 `efi_enabled` 变量是否为真来确定是否以UEFI方式启动系统。如果是,则需要读取ACPI表中的波特率值,可以使用 `acpi_table_parse()` 函数来读取ACPI表,具体使用方法可以参考内核中的 `drivers/acpi/tables.c` 文件中的相关代码。
3. 如果读取ACPI表成功,则将 `baudrate` 变量的值设置为从ACPI表中读取的值。否则,仍然使用默认值115200。
4. 在最后调用 `pl011_register_port()` 函数之前,将 `uap->fixed_baud` 变量的值设置为读取到的波特率值。
下面是修改后的代码:
```
static int sbsa_uart_probe(struct platform_device *pdev) {
struct uart_amba_port *uap;
struct resource r;
int portnr, ret;
int baudrate;
int baudrate_from_acpi = 0;
/* Check the mandatory baud rate parameter in the DT node early
* so that we can easily exit with the error.
*/
if (pdev->dev.of_node) {
struct device_node *np = pdev->dev.of_node;
ret = of_property_read_u32(np, "current-speed", &baudrate);
if (ret)
return ret;
} else {
baudrate = 115200;
}
/* Read baudrate from ACPI table if booting with UEFI */
if (efi_enabled(EFI_BOOT) && acpi_table_parse(ACPI_SIG_SPCR, acpi_parse_spcr)) {
baudrate_from_acpi = spcr_baudrate;
}
/* Use baudrate from ACPI table if available, otherwise use default value */
baudrate = baudrate_from_acpi > 0 ? baudrate_from_acpi : baudrate;
portnr = pl011_find_free_port();
if (portnr < 0)
return portnr;
uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port), GFP_KERNEL);
if (!uap)
return -ENOMEM;
ret = platform_get_irq(pdev, 0);
if (ret < 0) {
if (ret != -EPROBE_DEFER)
dev_err(&pdev->dev, "cannot obtain irq\n");
return ret;
}
#ifdef CONFIG_ACPI_SPCR_TABLE
if (qdf2400_e44_present) {
dev_info(&pdev->dev, "working around QDF2400 SoC erratum 44\n");
uap->vendor = &vendor_qdt_qdf2400_e44;
} else
#endif
uap->vendor = &vendor_sbsa;
uap->reg_offset = uap->vendor->reg_offset;
uap->fifosize = 32;
uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM;
uap->port.ops = &sbsa_uart_pops;
uap->fixed_baud = baudrate;
snprintf(uap->type, sizeof(uap->type), "SBSA");
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
ret = pl011_setup_port(&pdev->dev, uap, r, portnr);
if (ret)
return ret;
platform_set_drvdata(pdev, uap);
/* Set fixed_baud value before registering the port */
uap->fixed_baud = baudrate;
return pl011_register_port(uap);
}
```
阅读全文