VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / serial / 8250_acpi.c
1 /*
2  * Copyright (c) 2002-2003 Matthew Wilcox for Hewlett-Packard
3  * Copyright (C) 2004 Hewlett-Packard Co
4  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
5  *
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.
10  */
11
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>
18
19 #include <acpi/acpi_bus.h>
20
21 #include <asm/io.h>
22 #include <asm/serial.h>
23
24 struct serial_private {
25         int     line;
26         void    *iomem_base;
27 };
28
29 static acpi_status acpi_serial_mmio(struct serial_struct *req,
30                                     struct acpi_resource_address64 *addr)
31 {
32         unsigned long size;
33
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);
40                 return AE_ERROR;
41         }
42         req->io_type = SERIAL_IO_MEM;
43         return AE_OK;
44 }
45
46 static acpi_status acpi_serial_port(struct serial_struct *req,
47                                     struct acpi_resource_io *io)
48 {
49         if (io->range_length) {
50                 req->port = io->min_base_address;
51                 req->io_type = SERIAL_IO_PORT;
52         } else
53                 printk(KERN_ERR "%s: zero-length IO port range?\n", __FUNCTION__);
54         return AE_OK;
55 }
56
57 static acpi_status acpi_serial_ext_irq(struct serial_struct *req,
58                                        struct acpi_resource_ext_irq *ext_irq)
59 {
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);
63         return AE_OK;
64 }
65
66 static acpi_status acpi_serial_irq(struct serial_struct *req,
67                                    struct acpi_resource_irq *irq)
68 {
69         if (irq->number_of_interrupts > 0)
70                 req->irq = acpi_register_gsi(irq->interrupts[0],
71                           irq->edge_level, irq->active_high_low);
72         return AE_OK;
73 }
74
75 static acpi_status acpi_serial_resource(struct acpi_resource *res, void *data)
76 {
77         struct serial_struct *serial_req = (struct serial_struct *) data;
78         struct acpi_resource_address64 addr;
79         acpi_status status;
80
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);
90         return AE_OK;
91 }
92
93 static int acpi_serial_add(struct acpi_device *device)
94 {
95         struct serial_private *priv;
96         acpi_status status;
97         struct serial_struct serial_req;
98         int result;
99
100         memset(&serial_req, 0, sizeof(serial_req));
101
102         priv = kmalloc(sizeof(struct serial_private), GFP_KERNEL);
103         if (!priv) {
104                 result = -ENOMEM;
105                 goto fail;
106         }
107         memset(priv, 0, sizeof(*priv));
108
109         status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
110                                      acpi_serial_resource, &serial_req);
111         if (ACPI_FAILURE(status)) {
112                 result = -ENODEV;
113                 goto fail;
114         }
115
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);
121                 result = -ENODEV;
122                 goto fail;
123         }
124
125         serial_req.baud_base = BASE_BAUD;
126         serial_req.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
127
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);
132                 result = -ENODEV;
133                 goto fail;
134         }
135
136         acpi_driver_data(device) = priv;
137         return 0;
138
139 fail:
140         if (serial_req.iomem_base)
141                 iounmap(serial_req.iomem_base);
142         kfree(priv);
143
144         return result;
145 }
146
147 static int acpi_serial_remove(struct acpi_device *device, int type)
148 {
149         struct serial_private *priv;
150
151         if (!device || !acpi_driver_data(device))
152                 return -EINVAL;
153
154         priv = acpi_driver_data(device);
155         unregister_serial(priv->line);
156         if (priv->iomem_base)
157                 iounmap(priv->iomem_base);
158         kfree(priv);
159
160         return 0;
161 }
162
163 static struct acpi_driver acpi_serial_driver = {
164         .name =         "serial",
165         .class =        "",
166         .ids =          "PNP0501",
167         .ops =  {
168                 .add =          acpi_serial_add,
169                 .remove =       acpi_serial_remove,
170         },
171 };
172
173 static int __init acpi_serial_init(void)
174 {
175         return acpi_bus_register_driver(&acpi_serial_driver);
176 }
177
178 static void __exit acpi_serial_exit(void)
179 {
180         acpi_bus_unregister_driver(&acpi_serial_driver);
181 }
182
183 module_init(acpi_serial_init);
184 module_exit(acpi_serial_exit);
185
186 MODULE_LICENSE("GPL");
187 MODULE_DESCRIPTION("Generic 8250/16x50 ACPI serial driver");