blob: cc5d21300ed31f080d4771db11fa52ac9b2decc7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/************************************************************************
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.ltcfwd.linux.ibm.com>
24 *
25 ***********************************************************************/
26#include <linux/moduleparam.h>
27#include <linux/pci.h>
28
29#include "jsm.h"
30
31MODULE_AUTHOR("Digi International, http://www.digi.com");
Christoph Hellwig614a7d62005-04-16 15:25:44 -070032MODULE_DESCRIPTION("Driver for the Digi International "
33 "Neo PCI based product line");
34MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035MODULE_SUPPORTED_DEVICE("jsm");
36
37#define JSM_DRIVER_NAME "jsm"
38#define NR_PORTS 32
39#define JSM_MINOR_START 0
40
41struct uart_driver jsm_uart_driver = {
42 .owner = THIS_MODULE,
43 .driver_name = JSM_DRIVER_NAME,
44 .dev_name = "ttyn",
45 .major = 253,
46 .minor = JSM_MINOR_START,
47 .nr = NR_PORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
50int jsm_debug;
51int jsm_rawreadok;
52module_param(jsm_debug, int, 0);
53module_param(jsm_rawreadok, int, 0);
54MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
55MODULE_PARM_DESC(jsm_rawreadok, "Bypass flip buffers on input");
56
Christoph Hellwig614a7d62005-04-16 15:25:44 -070057static int jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct jsm_board *brd;
Christoph Hellwig614a7d62005-04-16 15:25:44 -070061 static int adapter_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int retval;
63
Christoph Hellwig614a7d62005-04-16 15:25:44 -070064 rc = pci_enable_device(pdev);
65 if (rc) {
66 dev_err(&pdev->dev, "Device enable FAILED\n");
67 goto out;
68 }
69
70 rc = pci_request_regions(pdev, "jsm");
71 if (rc) {
72 dev_err(&pdev->dev, "pci_request_region FAILED\n");
73 goto out_disable_device;
74 }
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 brd = kmalloc(sizeof(struct jsm_board), GFP_KERNEL);
77 if (!brd) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -070078 dev_err(&pdev->dev,
79 "memory allocation for board structure failed\n");
80 rc = -ENOMEM;
81 goto out_release_regions;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83 memset(brd, 0, sizeof(struct jsm_board));
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* store the info for the board we've found */
Christoph Hellwig614a7d62005-04-16 15:25:44 -070086 brd->boardnum = adapter_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 brd->pci_dev = pdev;
Christoph Hellwig614a7d62005-04-16 15:25:44 -070088 brd->maxports = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 spin_lock_init(&brd->bd_lock);
91 spin_lock_init(&brd->bd_intr_lock);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* store which revision we have */
94 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
95
96 brd->irq = pdev->irq;
97
Christoph Hellwig614a7d62005-04-16 15:25:44 -070098 jsm_printk(INIT, INFO, &brd->pci_dev,
99 "jsm_found_board - NEO adapter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700101 /* get the PCI Base Address Registers */
102 brd->membase = pci_resource_start(pdev, 0);
103 brd->membase_end = pci_resource_end(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700105 if (brd->membase & 1)
106 brd->membase &= ~3;
107 else
108 brd->membase &= ~15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700110 /* Assign the board_ops struct */
111 brd->bd_ops = &jsm_neo_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700113 brd->bd_uart_offset = 0x200;
114 brd->bd_dividend = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700116 brd->re_map_membase = ioremap(brd->membase, 0x1000);
117 if (!brd->re_map_membase) {
118 dev_err(&pdev->dev,
119 "card has no PCI Memory resources, "
120 "failing board.\n");
121 rc = -ENOMEM;
122 goto out_kfree_brd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700125 rc = request_irq(brd->irq, brd->bd_ops->intr,
126 SA_INTERRUPT|SA_SHIRQ, "JSM", brd);
127 if (rc) {
128 printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
129 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131
132 rc = jsm_tty_init(brd);
133 if (rc < 0) {
134 dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 retval = -ENXIO;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700136 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
139 rc = jsm_uart_port_init(brd);
140 if (rc < 0) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700141 /* XXX: leaking all resources from jsm_tty_init here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 retval = -ENXIO;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700144 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* Log the information about the board */
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700148 dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n",
149 adapter_count, brd->rev, brd->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /*
152 * allocate flip buffer for board.
153 *
154 * Okay to malloc with GFP_KERNEL, we are not at interrupt
155 * context, and there are no locks held.
156 */
157 brd->flipbuf = kmalloc(MYFLIPLEN, GFP_KERNEL);
158 if (!brd->flipbuf) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700159 /* XXX: leaking all resources from jsm_tty_init and
160 jsm_uart_port_init here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 retval = -ENOMEM;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700163 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 memset(brd->flipbuf, 0, MYFLIPLEN);
166
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700167 pci_set_drvdata(pdev, brd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700170 out_free_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 free_irq(brd->irq, brd);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700172 out_iounmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 iounmap(brd->re_map_membase);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700174 out_kfree_brd:
175 kfree(brd);
176 out_release_regions:
177 pci_release_regions(pdev);
178 out_disable_device:
179 pci_disable_device(pdev);
180 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return rc;
182}
183
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700184static void jsm_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700186 struct jsm_board *brd = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 int i = 0;
188
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700189 jsm_remove_uart_port(brd);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 free_irq(brd->irq, brd);
192 iounmap(brd->re_map_membase);
193
194 /* Free all allocated channels structs */
195 for (i = 0; i < brd->maxports; i++) {
196 if (brd->channels[i]) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700197 kfree(brd->channels[i]->ch_rqueue);
198 kfree(brd->channels[i]->ch_equeue);
199 kfree(brd->channels[i]->ch_wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 kfree(brd->channels[i]);
201 }
202 }
203
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700204 pci_release_regions(pdev);
205 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 kfree(brd->flipbuf);
207 kfree(brd);
208}
209
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700210static struct pci_device_id jsm_pci_tbl[] = {
211 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
212 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
213 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
214 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
215 { 0, }
216};
217MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700219static struct pci_driver jsm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .name = "jsm",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .id_table = jsm_pci_tbl,
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700222 .probe = jsm_probe_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .remove = __devexit_p(jsm_remove_one),
224};
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int __init jsm_init_module(void)
227{
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700228 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 rc = uart_register_driver(&jsm_uart_driver);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700231 if (!rc) {
232 rc = pci_register_driver(&jsm_driver);
233 if (rc)
234 uart_unregister_driver(&jsm_uart_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return rc;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static void __exit jsm_exit_module(void)
240{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 pci_unregister_driver(&jsm_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 uart_unregister_driver(&jsm_uart_driver);
243}
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700244
245module_init(jsm_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246module_exit(jsm_exit_module);