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) {
62 req->irq = acpi_register_irq(ext_irq->interrupts[0],
63 ext_irq->active_high_low, ext_irq->edge_level);
65 req->irq = ext_irq->interrupts[0];
71 static acpi_status acpi_serial_irq(struct serial_struct *req,
72 struct acpi_resource_irq *irq)
74 if (irq->number_of_interrupts > 0) {
76 req->irq = acpi_register_irq(irq->interrupts[0],
77 irq->active_high_low, irq->edge_level);
79 req->irq = irq->interrupts[0];
85 static acpi_status acpi_serial_resource(struct acpi_resource *res, void *data)
87 struct serial_struct *serial_req = (struct serial_struct *) data;
88 struct acpi_resource_address64 addr;
91 status = acpi_resource_to_address64(res, &addr);
92 if (ACPI_SUCCESS(status))
93 return acpi_serial_mmio(serial_req, &addr);
94 else if (res->id == ACPI_RSTYPE_IO)
95 return acpi_serial_port(serial_req, &res->data.io);
96 else if (res->id == ACPI_RSTYPE_EXT_IRQ)
97 return acpi_serial_ext_irq(serial_req, &res->data.extended_irq);
98 else if (res->id == ACPI_RSTYPE_IRQ)
99 return acpi_serial_irq(serial_req, &res->data.irq);
103 static int acpi_serial_add(struct acpi_device *device)
105 struct serial_private *priv;
107 struct serial_struct serial_req;
110 memset(&serial_req, 0, sizeof(serial_req));
112 priv = kmalloc(sizeof(struct serial_private), GFP_KERNEL);
117 memset(priv, 0, sizeof(*priv));
119 status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
120 acpi_serial_resource, &serial_req);
121 if (ACPI_FAILURE(status)) {
126 if (serial_req.iomem_base)
127 priv->iomem_base = serial_req.iomem_base;
128 else if (!serial_req.port) {
129 printk(KERN_ERR "%s: no iomem or port address in %s _CRS\n",
130 __FUNCTION__, device->pnp.bus_id);
135 serial_req.baud_base = BASE_BAUD;
136 serial_req.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_RESOURCES;
138 priv->line = register_serial(&serial_req);
139 if (priv->line < 0) {
140 printk(KERN_WARNING "Couldn't register serial port %s: %d\n",
141 device->pnp.bus_id, priv->line);
146 acpi_driver_data(device) = priv;
150 if (serial_req.iomem_base)
151 iounmap(serial_req.iomem_base);
157 static int acpi_serial_remove(struct acpi_device *device, int type)
159 struct serial_private *priv;
161 if (!device || !acpi_driver_data(device))
164 priv = acpi_driver_data(device);
165 unregister_serial(priv->line);
166 if (priv->iomem_base)
167 iounmap(priv->iomem_base);
173 static struct acpi_driver acpi_serial_driver = {
178 .add = acpi_serial_add,
179 .remove = acpi_serial_remove,
183 static int __init acpi_serial_init(void)
185 return acpi_bus_register_driver(&acpi_serial_driver);
188 static void __exit acpi_serial_exit(void)
190 acpi_bus_unregister_driver(&acpi_serial_driver);
193 module_init(acpi_serial_init);
194 module_exit(acpi_serial_exit);
196 MODULE_LICENSE("GPL");
197 MODULE_DESCRIPTION("Generic 8250/16x50 ACPI serial driver");