Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Adrian Bunk | 2a08b4e | 2006-04-01 01:04:20 +0200 | [diff] [blame] | 23 | * Wendy Xiong <wendyx@us.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * |
V. ANANDA KRISHNAN | c223695 | 2005-07-27 11:43:49 -0700 | [diff] [blame] | 25 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 33 | MODULE_DESCRIPTION("Driver for the Digi International " |
| 34 | "Neo PCI based product line"); |
| 35 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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", |
V. ANANDA KRISHNAN | 9539c1d | 2005-07-27 11:43:48 -0700 | [diff] [blame] | 46 | .major = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | .minor = JSM_MINOR_START, |
| 48 | .nr = NR_PORTS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | int jsm_debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | module_param(jsm_debug, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | MODULE_PARM_DESC(jsm_debug, "Driver debugging level"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 55 | static int jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | struct jsm_board *brd; |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 59 | static int adapter_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int retval; |
| 61 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 62 | rc = pci_enable_device(pdev); |
| 63 | if (rc) { |
| 64 | dev_err(&pdev->dev, "Device enable FAILED\n"); |
| 65 | goto out; |
| 66 | } |
| 67 | |
| 68 | rc = pci_request_regions(pdev, "jsm"); |
| 69 | if (rc) { |
| 70 | dev_err(&pdev->dev, "pci_request_region FAILED\n"); |
| 71 | goto out_disable_device; |
| 72 | } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | brd = kmalloc(sizeof(struct jsm_board), GFP_KERNEL); |
| 75 | if (!brd) { |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 76 | dev_err(&pdev->dev, |
| 77 | "memory allocation for board structure failed\n"); |
| 78 | rc = -ENOMEM; |
| 79 | goto out_release_regions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | memset(brd, 0, sizeof(struct jsm_board)); |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* store the info for the board we've found */ |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 84 | brd->boardnum = adapter_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | brd->pci_dev = pdev; |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 86 | brd->maxports = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | spin_lock_init(&brd->bd_lock); |
| 89 | spin_lock_init(&brd->bd_intr_lock); |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* store which revision we have */ |
| 92 | pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev); |
| 93 | |
| 94 | brd->irq = pdev->irq; |
| 95 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 96 | jsm_printk(INIT, INFO, &brd->pci_dev, |
| 97 | "jsm_found_board - NEO adapter\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 99 | /* get the PCI Base Address Registers */ |
| 100 | brd->membase = pci_resource_start(pdev, 0); |
| 101 | brd->membase_end = pci_resource_end(pdev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 103 | if (brd->membase & 1) |
| 104 | brd->membase &= ~3; |
| 105 | else |
| 106 | brd->membase &= ~15; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 108 | /* Assign the board_ops struct */ |
| 109 | brd->bd_ops = &jsm_neo_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 111 | brd->bd_uart_offset = 0x200; |
| 112 | brd->bd_dividend = 921600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 114 | brd->re_map_membase = ioremap(brd->membase, 0x1000); |
| 115 | if (!brd->re_map_membase) { |
| 116 | dev_err(&pdev->dev, |
| 117 | "card has no PCI Memory resources, " |
| 118 | "failing board.\n"); |
| 119 | rc = -ENOMEM; |
| 120 | goto out_kfree_brd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 123 | rc = request_irq(brd->irq, brd->bd_ops->intr, |
| 124 | SA_INTERRUPT|SA_SHIRQ, "JSM", brd); |
| 125 | if (rc) { |
| 126 | printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq); |
| 127 | goto out_iounmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | rc = jsm_tty_init(brd); |
| 131 | if (rc < 0) { |
| 132 | dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | retval = -ENXIO; |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 134 | goto out_free_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | rc = jsm_uart_port_init(brd); |
| 138 | if (rc < 0) { |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 139 | /* XXX: leaking all resources from jsm_tty_init here! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | retval = -ENXIO; |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 142 | goto out_free_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* Log the information about the board */ |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 146 | dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n", |
| 147 | adapter_count, brd->rev, brd->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * allocate flip buffer for board. |
| 151 | * |
| 152 | * Okay to malloc with GFP_KERNEL, we are not at interrupt |
| 153 | * context, and there are no locks held. |
| 154 | */ |
| 155 | brd->flipbuf = kmalloc(MYFLIPLEN, GFP_KERNEL); |
| 156 | if (!brd->flipbuf) { |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 157 | /* XXX: leaking all resources from jsm_tty_init and |
| 158 | jsm_uart_port_init here! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | dev_err(&pdev->dev, "memory allocation for flipbuf failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | retval = -ENOMEM; |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 161 | goto out_free_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | memset(brd->flipbuf, 0, MYFLIPLEN); |
| 164 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 165 | pci_set_drvdata(pdev, brd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return 0; |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 168 | out_free_irq: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | free_irq(brd->irq, brd); |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 170 | out_iounmap: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | iounmap(brd->re_map_membase); |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 172 | out_kfree_brd: |
| 173 | kfree(brd); |
| 174 | out_release_regions: |
| 175 | pci_release_regions(pdev); |
| 176 | out_disable_device: |
| 177 | pci_disable_device(pdev); |
| 178 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return rc; |
| 180 | } |
| 181 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 182 | static void jsm_remove_one(struct pci_dev *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 184 | struct jsm_board *brd = pci_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | int i = 0; |
| 186 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 187 | jsm_remove_uart_port(brd); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | free_irq(brd->irq, brd); |
| 190 | iounmap(brd->re_map_membase); |
| 191 | |
| 192 | /* Free all allocated channels structs */ |
| 193 | for (i = 0; i < brd->maxports; i++) { |
| 194 | if (brd->channels[i]) { |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 195 | kfree(brd->channels[i]->ch_rqueue); |
| 196 | kfree(brd->channels[i]->ch_equeue); |
| 197 | kfree(brd->channels[i]->ch_wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | kfree(brd->channels[i]); |
| 199 | } |
| 200 | } |
| 201 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 202 | pci_release_regions(pdev); |
| 203 | pci_disable_device(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | kfree(brd->flipbuf); |
| 205 | kfree(brd); |
| 206 | } |
| 207 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 208 | static struct pci_device_id jsm_pci_tbl[] = { |
| 209 | { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 }, |
| 210 | { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 }, |
| 211 | { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 }, |
| 212 | { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 }, |
| 213 | { 0, } |
| 214 | }; |
| 215 | MODULE_DEVICE_TABLE(pci, jsm_pci_tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 217 | static struct pci_driver jsm_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | .name = "jsm", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | .id_table = jsm_pci_tbl, |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 220 | .probe = jsm_probe_one, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | .remove = __devexit_p(jsm_remove_one), |
| 222 | }; |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | static int __init jsm_init_module(void) |
| 225 | { |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 226 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | rc = uart_register_driver(&jsm_uart_driver); |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 229 | if (!rc) { |
| 230 | rc = pci_register_driver(&jsm_driver); |
| 231 | if (rc) |
| 232 | uart_unregister_driver(&jsm_uart_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return rc; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | static void __exit jsm_exit_module(void) |
| 238 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | pci_unregister_driver(&jsm_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | uart_unregister_driver(&jsm_uart_driver); |
| 241 | } |
Christoph Hellwig | 614a7d6 | 2005-04-16 15:25:44 -0700 | [diff] [blame] | 242 | |
| 243 | module_init(jsm_init_module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | module_exit(jsm_exit_module); |