jsm_driver.c 6.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/************************************************************************
 * Copyright 2003 Digi International (www.digi.com)
 *
 * Copyright (C) 2004 IBM Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
 * MA  02111-1307, USA.
 *
 * Contact Information:
 * Scott H Kilau <Scott_Kilau@digi.com>
23
 * Wendy Xiong   <wendyx@us.ibm.com>
L
Linus Torvalds 已提交
24
 *
V
V. ANANDA KRISHNAN 已提交
25
 *
L
Linus Torvalds 已提交
26 27 28 29 30 31 32
 ***********************************************************************/
#include <linux/moduleparam.h>
#include <linux/pci.h>

#include "jsm.h"

MODULE_AUTHOR("Digi International, http://www.digi.com");
33 34 35
MODULE_DESCRIPTION("Driver for the Digi International "
		   "Neo PCI based product line");
MODULE_LICENSE("GPL");
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45
MODULE_SUPPORTED_DEVICE("jsm");

#define JSM_DRIVER_NAME "jsm"
#define NR_PORTS	32
#define JSM_MINOR_START	0

struct uart_driver jsm_uart_driver = {
	.owner		= THIS_MODULE,
	.driver_name	= JSM_DRIVER_NAME,
	.dev_name	= "ttyn",
46
	.major		= 0,
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54
	.minor		= JSM_MINOR_START,
	.nr		= NR_PORTS,
};

int jsm_debug;
module_param(jsm_debug, int, 0);
MODULE_PARM_DESC(jsm_debug, "Driver debugging level");

55
static int jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
L
Linus Torvalds 已提交
56 57
{
	int rc = 0;
58 59 60
	struct jsm_board *brd;
	static int adapter_count = 0;
	int retval;
L
Linus Torvalds 已提交
61

62 63 64 65
	rc = pci_enable_device(pdev);
	if (rc) {
		dev_err(&pdev->dev, "Device enable FAILED\n");
		goto out;
L
Linus Torvalds 已提交
66 67
	}

68 69 70 71 72
	rc = pci_request_regions(pdev, "jsm");
	if (rc) {
		dev_err(&pdev->dev, "pci_request_region FAILED\n");
		goto out_disable_device;
	}
L
Linus Torvalds 已提交
73

74
	brd = kzalloc(sizeof(struct jsm_board), GFP_KERNEL);
L
Linus Torvalds 已提交
75
	if (!brd) {
76 77 78 79
		dev_err(&pdev->dev,
			"memory allocation for board structure failed\n");
		rc = -ENOMEM;
		goto out_release_regions;
L
Linus Torvalds 已提交
80 81 82
	}

	/* store the info for the board we've found */
83
	brd->boardnum = adapter_count++;
L
Linus Torvalds 已提交
84
	brd->pci_dev = pdev;
85
	brd->maxports = 2;
L
Linus Torvalds 已提交
86 87 88 89 90

	spin_lock_init(&brd->bd_lock);
	spin_lock_init(&brd->bd_intr_lock);

	/* store which revision we have */
91
	brd->rev = pdev->revision;
L
Linus Torvalds 已提交
92 93 94

	brd->irq = pdev->irq;

95 96
	jsm_printk(INIT, INFO, &brd->pci_dev,
		"jsm_found_board - NEO adapter\n");
L
Linus Torvalds 已提交
97

98 99 100
	/* get the PCI Base Address Registers */
	brd->membase	= pci_resource_start(pdev, 0);
	brd->membase_end = pci_resource_end(pdev, 0);
L
Linus Torvalds 已提交
101

102 103 104 105
	if (brd->membase & 1)
		brd->membase &= ~3;
	else
		brd->membase &= ~15;
L
Linus Torvalds 已提交
106

107 108
	/* Assign the board_ops struct */
	brd->bd_ops = &jsm_neo_ops;
L
Linus Torvalds 已提交
109

110 111
	brd->bd_uart_offset = 0x200;
	brd->bd_dividend = 921600;
L
Linus Torvalds 已提交
112

113 114 115 116 117 118 119
	brd->re_map_membase = ioremap(brd->membase, 0x1000);
	if (!brd->re_map_membase) {
		dev_err(&pdev->dev,
			"card has no PCI Memory resources, "
			"failing board.\n");
		rc = -ENOMEM;
		goto out_kfree_brd;
L
Linus Torvalds 已提交
120 121
	}

122
	rc = request_irq(brd->irq, brd->bd_ops->intr,
123
			IRQF_DISABLED|IRQF_SHARED, "JSM", brd);
124 125 126
	if (rc) {
		printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
		goto out_iounmap;
L
Linus Torvalds 已提交
127 128 129 130 131 132
	}

	rc = jsm_tty_init(brd);
	if (rc < 0) {
		dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
		retval = -ENXIO;
133
		goto out_free_irq;
L
Linus Torvalds 已提交
134 135 136 137
	}

	rc = jsm_uart_port_init(brd);
	if (rc < 0) {
138
		/* XXX: leaking all resources from jsm_tty_init here! */
L
Linus Torvalds 已提交
139 140
		dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
		retval = -ENXIO;
141
		goto out_free_irq;
L
Linus Torvalds 已提交
142 143 144
	}

	/* Log the information about the board */
145 146
	dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n",
			adapter_count, brd->rev, brd->irq);
L
Linus Torvalds 已提交
147 148 149 150 151 152 153

	/*
	 * allocate flip buffer for board.
	 *
	 * Okay to malloc with GFP_KERNEL, we are not at interrupt
	 * context, and there are no locks held.
	 */
154
	brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
L
Linus Torvalds 已提交
155
	if (!brd->flipbuf) {
156 157
		/* XXX: leaking all resources from jsm_tty_init and
		 	jsm_uart_port_init here! */
L
Linus Torvalds 已提交
158 159
		dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
		retval = -ENOMEM;
160
		goto out_free_irq;
L
Linus Torvalds 已提交
161 162
	}

163
	pci_set_drvdata(pdev, brd);
L
Linus Torvalds 已提交
164 165

	return 0;
166
 out_free_irq:
L
Linus Torvalds 已提交
167
	free_irq(brd->irq, brd);
168
 out_iounmap:
L
Linus Torvalds 已提交
169
	iounmap(brd->re_map_membase);
170 171 172 173 174 175 176
 out_kfree_brd:
	kfree(brd);
 out_release_regions:
	pci_release_regions(pdev);
 out_disable_device:
	pci_disable_device(pdev);
 out:
L
Linus Torvalds 已提交
177 178 179
	return rc;
}

180
static void jsm_remove_one(struct pci_dev *pdev)
L
Linus Torvalds 已提交
181
{
182
	struct jsm_board *brd = pci_get_drvdata(pdev);
L
Linus Torvalds 已提交
183 184
	int i = 0;

185 186
	jsm_remove_uart_port(brd);

L
Linus Torvalds 已提交
187 188 189 190 191 192
	free_irq(brd->irq, brd);
	iounmap(brd->re_map_membase);

	/* Free all allocated channels structs */
	for (i = 0; i < brd->maxports; i++) {
		if (brd->channels[i]) {
193 194 195
			kfree(brd->channels[i]->ch_rqueue);
			kfree(brd->channels[i]->ch_equeue);
			kfree(brd->channels[i]->ch_wqueue);
L
Linus Torvalds 已提交
196 197 198 199
			kfree(brd->channels[i]);
		}
	}

200 201
	pci_release_regions(pdev);
	pci_disable_device(pdev);
L
Linus Torvalds 已提交
202 203 204 205
	kfree(brd->flipbuf);
	kfree(brd);
}

206 207 208 209 210 211 212 213
static struct pci_device_id jsm_pci_tbl[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
	{ 0, }
};
MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
L
Linus Torvalds 已提交
214

215
static struct pci_driver jsm_driver = {
L
Linus Torvalds 已提交
216 217
	.name		= "jsm",
	.id_table	= jsm_pci_tbl,
218
	.probe		= jsm_probe_one,
L
Linus Torvalds 已提交
219 220 221 222 223
	.remove		= __devexit_p(jsm_remove_one),
};

static int __init jsm_init_module(void)
{
224
	int rc;
L
Linus Torvalds 已提交
225 226

	rc = uart_register_driver(&jsm_uart_driver);
227 228 229 230
	if (!rc) {
		rc = pci_register_driver(&jsm_driver);
		if (rc)
			uart_unregister_driver(&jsm_uart_driver);
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239
	}
	return rc;
}

static void __exit jsm_exit_module(void)
{
	pci_unregister_driver(&jsm_driver);
	uart_unregister_driver(&jsm_uart_driver);
}
240 241

module_init(jsm_init_module);
L
Linus Torvalds 已提交
242
module_exit(jsm_exit_module);