1 // SPDX-License-Identifier: GPL-2.0+
2 /************************************************************************
3 * Copyright 2003 Digi International (www.digi.com)
5 * Copyright (C) 2004 IBM Corporation. All rights reserved.
8 * Scott H Kilau <Scott_Kilau@digi.com>
9 * Wendy Xiong <wendyx@us.ibm.com>
12 ***********************************************************************/
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/slab.h>
19 MODULE_AUTHOR("Digi International, https://www.digi.com");
20 MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
21 MODULE_LICENSE("GPL");
22 MODULE_SUPPORTED_DEVICE("jsm");
24 #define JSM_DRIVER_NAME "jsm"
26 #define JSM_MINOR_START 0
28 struct uart_driver jsm_uart_driver
= {
30 .driver_name
= JSM_DRIVER_NAME
,
33 .minor
= JSM_MINOR_START
,
37 static pci_ers_result_t
jsm_io_error_detected(struct pci_dev
*pdev
,
38 pci_channel_state_t state
);
39 static pci_ers_result_t
jsm_io_slot_reset(struct pci_dev
*pdev
);
40 static void jsm_io_resume(struct pci_dev
*pdev
);
42 static const struct pci_error_handlers jsm_err_handler
= {
43 .error_detected
= jsm_io_error_detected
,
44 .slot_reset
= jsm_io_slot_reset
,
45 .resume
= jsm_io_resume
,
49 module_param(jsm_debug
, int, 0);
50 MODULE_PARM_DESC(jsm_debug
, "Driver debugging level");
52 static int jsm_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
55 struct jsm_board
*brd
;
56 static int adapter_count
;
58 rc
= pci_enable_device(pdev
);
60 dev_err(&pdev
->dev
, "Device enable FAILED\n");
64 rc
= pci_request_regions(pdev
, JSM_DRIVER_NAME
);
66 dev_err(&pdev
->dev
, "pci_request_region FAILED\n");
67 goto out_disable_device
;
70 brd
= kzalloc(sizeof(*brd
), GFP_KERNEL
);
73 goto out_release_regions
;
76 /* store the info for the board we've found */
77 brd
->boardnum
= adapter_count
++;
80 switch (pdev
->device
) {
81 case PCI_DEVICE_ID_NEO_2DB9
:
82 case PCI_DEVICE_ID_NEO_2DB9PRI
:
83 case PCI_DEVICE_ID_NEO_2RJ45
:
84 case PCI_DEVICE_ID_NEO_2RJ45PRI
:
85 case PCI_DEVICE_ID_NEO_2_422_485
:
89 case PCI_DEVICE_ID_CLASSIC_4
:
90 case PCI_DEVICE_ID_CLASSIC_4_422
:
91 case PCI_DEVICE_ID_NEO_4
:
92 case PCIE_DEVICE_ID_NEO_4
:
93 case PCIE_DEVICE_ID_NEO_4RJ45
:
94 case PCIE_DEVICE_ID_NEO_4_IBM
:
98 case PCI_DEVICE_ID_CLASSIC_8
:
99 case PCI_DEVICE_ID_CLASSIC_8_422
:
100 case PCI_DEVICE_ID_DIGI_NEO_8
:
101 case PCIE_DEVICE_ID_NEO_8
:
102 case PCIE_DEVICE_ID_NEO_8RJ45
:
111 spin_lock_init(&brd
->bd_intr_lock
);
113 /* store which revision we have */
114 brd
->rev
= pdev
->revision
;
116 brd
->irq
= pdev
->irq
;
118 switch (pdev
->device
) {
119 case PCI_DEVICE_ID_CLASSIC_4
:
120 case PCI_DEVICE_ID_CLASSIC_4_422
:
121 case PCI_DEVICE_ID_CLASSIC_8
:
122 case PCI_DEVICE_ID_CLASSIC_8_422
:
124 jsm_dbg(INIT
, &brd
->pci_dev
,
125 "jsm_found_board - Classic adapter\n");
128 * For PCI ClassicBoards
129 * PCI Local Address (.i.e. "resource" number) space
130 * 0 PLX Memory Mapped Config
131 * 1 PLX I/O Mapped Config
132 * 2 I/O Mapped UARTs and Status
133 * 3 Memory Mapped VPD
134 * 4 Memory Mapped UARTs and Status
137 /* Get the PCI Base Address Registers */
138 brd
->membase
= pci_resource_start(pdev
, 4);
139 brd
->membase_end
= pci_resource_end(pdev
, 4);
141 if (brd
->membase
& 0x1)
142 brd
->membase
&= ~0x3;
144 brd
->membase
&= ~0xF;
146 brd
->iobase
= pci_resource_start(pdev
, 1);
147 brd
->iobase_end
= pci_resource_end(pdev
, 1);
148 brd
->iobase
= ((unsigned int)(brd
->iobase
)) & 0xFFFE;
150 /* Assign the board_ops struct */
151 brd
->bd_ops
= &jsm_cls_ops
;
153 brd
->bd_uart_offset
= 0x8;
154 brd
->bd_dividend
= 921600;
156 brd
->re_map_membase
= ioremap(brd
->membase
,
157 pci_resource_len(pdev
, 4));
158 if (!brd
->re_map_membase
) {
160 "Card has no PCI Memory resources, failing board.\n");
166 * Enable Local Interrupt 1 (0x1),
167 * Local Interrupt 1 Polarity Active high (0x2),
168 * Enable PCI interrupt (0x43)
170 outb(0x43, brd
->iobase
+ 0x4c);
174 case PCI_DEVICE_ID_NEO_2DB9
:
175 case PCI_DEVICE_ID_NEO_2DB9PRI
:
176 case PCI_DEVICE_ID_NEO_2RJ45
:
177 case PCI_DEVICE_ID_NEO_2RJ45PRI
:
178 case PCI_DEVICE_ID_NEO_2_422_485
:
179 case PCI_DEVICE_ID_NEO_4
:
180 case PCIE_DEVICE_ID_NEO_4
:
181 case PCIE_DEVICE_ID_NEO_4RJ45
:
182 case PCIE_DEVICE_ID_NEO_4_IBM
:
183 case PCI_DEVICE_ID_DIGI_NEO_8
:
184 case PCIE_DEVICE_ID_NEO_8
:
185 case PCIE_DEVICE_ID_NEO_8RJ45
:
187 jsm_dbg(INIT
, &brd
->pci_dev
, "jsm_found_board - NEO adapter\n");
189 /* get the PCI Base Address Registers */
190 brd
->membase
= pci_resource_start(pdev
, 0);
191 brd
->membase_end
= pci_resource_end(pdev
, 0);
193 if (brd
->membase
& 1)
194 brd
->membase
&= ~0x3;
196 brd
->membase
&= ~0xF;
198 /* Assign the board_ops struct */
199 brd
->bd_ops
= &jsm_neo_ops
;
201 brd
->bd_uart_offset
= 0x200;
202 brd
->bd_dividend
= 921600;
204 brd
->re_map_membase
= ioremap(brd
->membase
,
205 pci_resource_len(pdev
, 0));
206 if (!brd
->re_map_membase
) {
208 "Card has no PCI Memory resources, failing board.\n");
218 rc
= request_irq(brd
->irq
, brd
->bd_ops
->intr
, IRQF_SHARED
, "JSM", brd
);
220 dev_warn(&pdev
->dev
, "Failed to hook IRQ %d\n", brd
->irq
);
224 rc
= jsm_tty_init(brd
);
226 dev_err(&pdev
->dev
, "Can't init tty devices (%d)\n", rc
);
231 rc
= jsm_uart_port_init(brd
);
233 /* XXX: leaking all resources from jsm_tty_init here! */
234 dev_err(&pdev
->dev
, "Can't init uart port (%d)\n", rc
);
239 /* Log the information about the board */
240 dev_info(&pdev
->dev
, "board %d: Digi Classic/Neo (rev %d), irq %d\n",
241 adapter_count
, brd
->rev
, brd
->irq
);
243 pci_set_drvdata(pdev
, brd
);
244 pci_save_state(pdev
);
248 jsm_remove_uart_port(brd
);
249 free_irq(brd
->irq
, brd
);
251 iounmap(brd
->re_map_membase
);
255 pci_release_regions(pdev
);
257 pci_disable_device(pdev
);
262 static void jsm_remove_one(struct pci_dev
*pdev
)
264 struct jsm_board
*brd
= pci_get_drvdata(pdev
);
267 switch (pdev
->device
) {
268 case PCI_DEVICE_ID_CLASSIC_4
:
269 case PCI_DEVICE_ID_CLASSIC_4_422
:
270 case PCI_DEVICE_ID_CLASSIC_8
:
271 case PCI_DEVICE_ID_CLASSIC_8_422
:
272 /* Tell card not to interrupt anymore. */
273 outb(0x0, brd
->iobase
+ 0x4c);
279 jsm_remove_uart_port(brd
);
281 free_irq(brd
->irq
, brd
);
282 iounmap(brd
->re_map_membase
);
284 /* Free all allocated channels structs */
285 for (i
= 0; i
< brd
->maxports
; i
++) {
286 if (brd
->channels
[i
]) {
287 kfree(brd
->channels
[i
]->ch_rqueue
);
288 kfree(brd
->channels
[i
]->ch_equeue
);
289 kfree(brd
->channels
[i
]);
293 pci_release_regions(pdev
);
294 pci_disable_device(pdev
);
298 static const struct pci_device_id jsm_pci_tbl
[] = {
299 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_2DB9
), 0, 0, 0 },
300 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_2DB9PRI
), 0, 0, 1 },
301 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_2RJ45
), 0, 0, 2 },
302 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_2RJ45PRI
), 0, 0, 3 },
303 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCIE_DEVICE_ID_NEO_4_IBM
), 0, 0, 4 },
304 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_DIGI_NEO_8
), 0, 0, 5 },
305 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_4
), 0, 0, 6 },
306 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_1_422
), 0, 0, 7 },
307 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_1_422_485
), 0, 0, 8 },
308 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_NEO_2_422_485
), 0, 0, 9 },
309 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCIE_DEVICE_ID_NEO_8
), 0, 0, 10 },
310 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCIE_DEVICE_ID_NEO_4
), 0, 0, 11 },
311 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCIE_DEVICE_ID_NEO_4RJ45
), 0, 0, 12 },
312 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCIE_DEVICE_ID_NEO_8RJ45
), 0, 0, 13 },
313 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_CLASSIC_4
), 0, 0, 14 },
314 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_CLASSIC_4_422
), 0, 0, 15 },
315 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_CLASSIC_8
), 0, 0, 16 },
316 { PCI_DEVICE(PCI_VENDOR_ID_DIGI
, PCI_DEVICE_ID_CLASSIC_8_422
), 0, 0, 17 },
319 MODULE_DEVICE_TABLE(pci
, jsm_pci_tbl
);
321 static struct pci_driver jsm_driver
= {
322 .name
= JSM_DRIVER_NAME
,
323 .id_table
= jsm_pci_tbl
,
324 .probe
= jsm_probe_one
,
325 .remove
= jsm_remove_one
,
326 .err_handler
= &jsm_err_handler
,
329 static pci_ers_result_t
jsm_io_error_detected(struct pci_dev
*pdev
,
330 pci_channel_state_t state
)
332 struct jsm_board
*brd
= pci_get_drvdata(pdev
);
334 jsm_remove_uart_port(brd
);
336 return PCI_ERS_RESULT_NEED_RESET
;
339 static pci_ers_result_t
jsm_io_slot_reset(struct pci_dev
*pdev
)
343 rc
= pci_enable_device(pdev
);
346 return PCI_ERS_RESULT_DISCONNECT
;
348 pci_set_master(pdev
);
350 return PCI_ERS_RESULT_RECOVERED
;
353 static void jsm_io_resume(struct pci_dev
*pdev
)
355 struct jsm_board
*brd
= pci_get_drvdata(pdev
);
357 pci_restore_state(pdev
);
358 pci_save_state(pdev
);
360 jsm_uart_port_init(brd
);
363 static int __init
jsm_init_module(void)
367 rc
= uart_register_driver(&jsm_uart_driver
);
369 rc
= pci_register_driver(&jsm_driver
);
371 uart_unregister_driver(&jsm_uart_driver
);
376 static void __exit
jsm_exit_module(void)
378 pci_unregister_driver(&jsm_driver
);
379 uart_unregister_driver(&jsm_uart_driver
);
382 module_init(jsm_init_module
);
383 module_exit(jsm_exit_module
);