]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/serial/jsm/jsm_driver.c
Merge commit 'v2.6.34-rc1' into perf/urgent
[mv-sheeva.git] / drivers / serial / jsm / jsm_driver.c
1 /************************************************************************
2  * Copyright 2003 Digi International (www.digi.com)
3  *
4  * Copyright (C) 2004 IBM Corporation. All rights reserved.
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, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  * PURPOSE.  See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
19  * MA  02111-1307, USA.
20  *
21  * Contact Information:
22  * Scott H Kilau <Scott_Kilau@digi.com>
23  * Wendy Xiong   <wendyx@us.ibm.com>
24  *
25  *
26  ***********************************************************************/
27 #include <linux/moduleparam.h>
28 #include <linux/pci.h>
29
30 #include "jsm.h"
31
32 MODULE_AUTHOR("Digi International, http://www.digi.com");
33 MODULE_DESCRIPTION("Driver for the Digi International "
34                    "Neo PCI based product line");
35 MODULE_LICENSE("GPL");
36 MODULE_SUPPORTED_DEVICE("jsm");
37
38 #define JSM_DRIVER_NAME "jsm"
39 #define NR_PORTS        32
40 #define JSM_MINOR_START 0
41
42 struct uart_driver jsm_uart_driver = {
43         .owner          = THIS_MODULE,
44         .driver_name    = JSM_DRIVER_NAME,
45         .dev_name       = "ttyn",
46         .major          = 0,
47         .minor          = JSM_MINOR_START,
48         .nr             = NR_PORTS,
49 };
50
51 static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
52                                        pci_channel_state_t state);
53 static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev);
54 static void jsm_io_resume(struct pci_dev *pdev);
55
56 static struct pci_error_handlers jsm_err_handler = {
57         .error_detected = jsm_io_error_detected,
58         .slot_reset = jsm_io_slot_reset,
59         .resume = jsm_io_resume,
60 };
61
62 int jsm_debug;
63 module_param(jsm_debug, int, 0);
64 MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
65
66 static int __devinit jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
67 {
68         int rc = 0;
69         struct jsm_board *brd;
70         static int adapter_count = 0;
71
72         rc = pci_enable_device(pdev);
73         if (rc) {
74                 dev_err(&pdev->dev, "Device enable FAILED\n");
75                 goto out;
76         }
77
78         rc = pci_request_regions(pdev, "jsm");
79         if (rc) {
80                 dev_err(&pdev->dev, "pci_request_region FAILED\n");
81                 goto out_disable_device;
82         }
83
84         brd = kzalloc(sizeof(struct jsm_board), GFP_KERNEL);
85         if (!brd) {
86                 dev_err(&pdev->dev,
87                         "memory allocation for board structure failed\n");
88                 rc = -ENOMEM;
89                 goto out_release_regions;
90         }
91
92         /* store the info for the board we've found */
93         brd->boardnum = adapter_count++;
94         brd->pci_dev = pdev;
95         if (pdev->device == PCIE_DEVICE_ID_NEO_4_IBM)
96                 brd->maxports = 4;
97         else if (pdev->device == PCI_DEVICE_ID_DIGI_NEO_8)
98                 brd->maxports = 8;
99         else
100                 brd->maxports = 2;
101
102         spin_lock_init(&brd->bd_intr_lock);
103
104         /* store which revision we have */
105         brd->rev = pdev->revision;
106
107         brd->irq = pdev->irq;
108
109         jsm_printk(INIT, INFO, &brd->pci_dev,
110                 "jsm_found_board - NEO adapter\n");
111
112         /* get the PCI Base Address Registers */
113         brd->membase    = pci_resource_start(pdev, 0);
114         brd->membase_end = pci_resource_end(pdev, 0);
115
116         if (brd->membase & 1)
117                 brd->membase &= ~3;
118         else
119                 brd->membase &= ~15;
120
121         /* Assign the board_ops struct */
122         brd->bd_ops = &jsm_neo_ops;
123
124         brd->bd_uart_offset = 0x200;
125         brd->bd_dividend = 921600;
126
127         brd->re_map_membase = ioremap(brd->membase, 0x1000);
128         if (!brd->re_map_membase) {
129                 dev_err(&pdev->dev,
130                         "card has no PCI Memory resources, "
131                         "failing board.\n");
132                 rc = -ENOMEM;
133                 goto out_kfree_brd;
134         }
135
136         rc = request_irq(brd->irq, brd->bd_ops->intr,
137                         IRQF_SHARED, "JSM", brd);
138         if (rc) {
139                 printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
140                 goto out_iounmap;
141         }
142
143         rc = jsm_tty_init(brd);
144         if (rc < 0) {
145                 dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
146                 rc = -ENXIO;
147                 goto out_free_irq;
148         }
149
150         rc = jsm_uart_port_init(brd);
151         if (rc < 0) {
152                 /* XXX: leaking all resources from jsm_tty_init here! */
153                 dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
154                 rc = -ENXIO;
155                 goto out_free_irq;
156         }
157
158         /* Log the information about the board */
159         dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n",
160                         adapter_count, brd->rev, brd->irq);
161
162         /*
163          * allocate flip buffer for board.
164          *
165          * Okay to malloc with GFP_KERNEL, we are not at interrupt
166          * context, and there are no locks held.
167          */
168         brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
169         if (!brd->flipbuf) {
170                 /* XXX: leaking all resources from jsm_tty_init and
171                         jsm_uart_port_init here! */
172                 dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
173                 rc = -ENOMEM;
174                 goto out_free_irq;
175         }
176
177         pci_set_drvdata(pdev, brd);
178         pci_save_state(pdev);
179
180         return 0;
181  out_free_irq:
182         jsm_remove_uart_port(brd);
183         free_irq(brd->irq, brd);
184  out_iounmap:
185         iounmap(brd->re_map_membase);
186  out_kfree_brd:
187         kfree(brd);
188  out_release_regions:
189         pci_release_regions(pdev);
190  out_disable_device:
191         pci_disable_device(pdev);
192  out:
193         return rc;
194 }
195
196 static void __devexit jsm_remove_one(struct pci_dev *pdev)
197 {
198         struct jsm_board *brd = pci_get_drvdata(pdev);
199         int i = 0;
200
201         jsm_remove_uart_port(brd);
202
203         free_irq(brd->irq, brd);
204         iounmap(brd->re_map_membase);
205
206         /* Free all allocated channels structs */
207         for (i = 0; i < brd->maxports; i++) {
208                 if (brd->channels[i]) {
209                         kfree(brd->channels[i]->ch_rqueue);
210                         kfree(brd->channels[i]->ch_equeue);
211                         kfree(brd->channels[i]->ch_wqueue);
212                         kfree(brd->channels[i]);
213                 }
214         }
215
216         pci_release_regions(pdev);
217         pci_disable_device(pdev);
218         kfree(brd->flipbuf);
219         kfree(brd);
220 }
221
222 static struct pci_device_id jsm_pci_tbl[] = {
223         { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
224         { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
225         { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
226         { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
227         { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
228         { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
229         { 0, }
230 };
231 MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
232
233 static struct pci_driver jsm_driver = {
234         .name           = "jsm",
235         .id_table       = jsm_pci_tbl,
236         .probe          = jsm_probe_one,
237         .remove         = __devexit_p(jsm_remove_one),
238         .err_handler    = &jsm_err_handler,
239 };
240
241 static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
242                                         pci_channel_state_t state)
243 {
244         struct jsm_board *brd = pci_get_drvdata(pdev);
245
246         jsm_remove_uart_port(brd);
247
248         return PCI_ERS_RESULT_NEED_RESET;
249 }
250
251 static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
252 {
253         int rc;
254
255         rc = pci_enable_device(pdev);
256
257         if (rc)
258                 return PCI_ERS_RESULT_DISCONNECT;
259
260         pci_set_master(pdev);
261
262         return PCI_ERS_RESULT_RECOVERED;
263 }
264
265 static void jsm_io_resume(struct pci_dev *pdev)
266 {
267         struct jsm_board *brd = pci_get_drvdata(pdev);
268
269         pci_restore_state(pdev);
270
271         jsm_uart_port_init(brd);
272 }
273
274 static int __init jsm_init_module(void)
275 {
276         int rc;
277
278         rc = uart_register_driver(&jsm_uart_driver);
279         if (!rc) {
280                 rc = pci_register_driver(&jsm_driver);
281                 if (rc)
282                         uart_unregister_driver(&jsm_uart_driver);
283         }
284         return rc;
285 }
286
287 static void __exit jsm_exit_module(void)
288 {
289         pci_unregister_driver(&jsm_driver);
290         uart_unregister_driver(&jsm_uart_driver);
291 }
292
293 module_init(jsm_init_module);
294 module_exit(jsm_exit_module);