2 * Copyright (c) 2002-2003 Matthew Wilcox for Hewlett-Packard
3 * Copyright (C) 2004 Hewlett-Packard Co
4 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/acpi.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/serial.h>
17 #include <linux/serial_core.h>
19 #include <acpi/acpi_bus.h>
22 #include <asm/serial.h>
24 struct serial_private {
29 static acpi_status acpi_serial_mmio(struct serial_struct *req,
30 struct acpi_resource_address64 *addr)
34 size = addr->max_address_range - addr->min_address_range + 1;
35 req->iomap_base = addr->min_address_range;
36 req->iomem_base = ioremap(req->iomap_base, size);
37 if (!req->iomem_base) {
38 printk(KERN_ERR "%s: couldn't ioremap 0x%lx-0x%lx\n",
39 __FUNCTION__, req->iomap_base, req->iomap_base + size);
42 req->io_type = SERIAL_IO_MEM;
46 static acpi_status acpi_serial_port(struct serial_struct *req,
47 struct acpi_resource_io *io)
49 if (io->range_length) {
50 req->port = io->min_base_address;
51 req->io_type = SERIAL_IO_PORT;
53 printk(KERN_ERR "%s: zero-length IO port range?\n", __FUNCTION__);
57 static acpi_status acpi_serial_ext_irq(struct serial_struct *req,
58 struct acpi_resource_ext_irq *ext_irq)
60 if (ext_irq->number_of_interrupts > 0)
61 req->irq = acpi_register_gsi(ext_irq->interrupts[0],
62 ext_irq->edge_level, ext_irq->active_high_low);
66 static acpi_status acpi_serial_irq(struct serial_struct *req,
67 struct acpi_resource_irq *irq)
69 if (irq->number_of_interrupts > 0)
70 req->irq = acpi_register_gsi(irq->interrupts[0],
71 irq->edge_level, irq->active_high_low);
75 static acpi_status acpi_serial_resource(struct acpi_resource *res, void *data)
77 struct serial_struct *serial_req = (struct serial_struct *) data;
78 struct acpi_resource_address64 addr;
81 status = acpi_resource_to_address64(res, &addr);
82 if (ACPI_SUCCESS(status))
83 return acpi_serial_mmio(serial_req, &addr);
84 else if (res->id == ACPI_RSTYPE_IO)
85 return acpi_serial_port(serial_req, &res->data.io);
86 else if (res->id == ACPI_RSTYPE_EXT_IRQ)
87 return acpi_serial_ext_irq(serial_req, &res->data.extended_irq);
88 else if (res->id == ACPI_RSTYPE_IRQ)
89 return acpi_serial_irq(serial_req, &res->data.irq);
93 static int acpi_serial_add(struct acpi_device *device)
95 struct serial_private *priv;
97 struct serial_struct serial_req;
100 memset(&serial_req, 0, sizeof(serial_req));
102 priv = kmalloc(sizeof(struct serial_private), GFP_KERNEL);
107 memset(priv, 0, sizeof(*priv));
109 status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
110 acpi_serial_resource, &serial_req);
111 if (ACPI_FAILURE(status)) {
116 if (serial_req.iomem_base)
117 priv->iomem_base = serial_req.iomem_base;
118 else if (!serial_req.port) {
119 printk(KERN_ERR "%s: no iomem or port address in %s _CRS\n",
120 __FUNCTION__, device->pnp.bus_id);
125 serial_req.baud_base = BASE_BAUD;
126 serial_req.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
128 priv->line = register_serial(&serial_req);
129 if (priv->line < 0) {
130 printk(KERN_WARNING "Couldn't register serial port %s: %d\n",
131 device->pnp.bus_id, priv->line);
136 acpi_driver_data(device) = priv;
140 if (serial_req.iomem_base)
141 iounmap(serial_req.iomem_base);
147 static int acpi_serial_remove(struct acpi_device *device, int type)
149 struct serial_private *priv;
151 if (!device || !acpi_driver_data(device))
154 priv = acpi_driver_data(device);
155 unregister_serial(priv->line);
156 if (priv->iomem_base)
157 iounmap(priv->iomem_base);
163 static struct acpi_driver acpi_serial_driver = {
168 .add = acpi_serial_add,
169 .remove = acpi_serial_remove,
173 static int __init acpi_serial_init(void)
175 return acpi_bus_register_driver(&acpi_serial_driver);
178 static void __exit acpi_serial_exit(void)
180 acpi_bus_unregister_driver(&acpi_serial_driver);
183 module_init(acpi_serial_init);
184 module_exit(acpi_serial_exit);
186 MODULE_LICENSE("GPL");
187 MODULE_DESCRIPTION("Generic 8250/16x50 ACPI serial driver");