jsm_driver.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. * Contact Information:
  17. * Scott H Kilau <Scott_Kilau@digi.com>
  18. * Wendy Xiong <wendyx@us.ibm.com>
  19. *
  20. *
  21. ***********************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/slab.h>
  25. #include "jsm.h"
  26. MODULE_AUTHOR("Digi International, http://www.digi.com");
  27. MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
  28. MODULE_LICENSE("GPL");
  29. MODULE_SUPPORTED_DEVICE("jsm");
  30. #define JSM_DRIVER_NAME "jsm"
  31. #define NR_PORTS 32
  32. #define JSM_MINOR_START 0
  33. struct uart_driver jsm_uart_driver = {
  34. .owner = THIS_MODULE,
  35. .driver_name = JSM_DRIVER_NAME,
  36. .dev_name = "ttyn",
  37. .major = 0,
  38. .minor = JSM_MINOR_START,
  39. .nr = NR_PORTS,
  40. };
  41. static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
  42. pci_channel_state_t state);
  43. static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev);
  44. static void jsm_io_resume(struct pci_dev *pdev);
  45. static const struct pci_error_handlers jsm_err_handler = {
  46. .error_detected = jsm_io_error_detected,
  47. .slot_reset = jsm_io_slot_reset,
  48. .resume = jsm_io_resume,
  49. };
  50. int jsm_debug;
  51. module_param(jsm_debug, int, 0);
  52. MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
  53. static int jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  54. {
  55. int rc = 0;
  56. struct jsm_board *brd;
  57. static int adapter_count;
  58. rc = pci_enable_device(pdev);
  59. if (rc) {
  60. dev_err(&pdev->dev, "Device enable FAILED\n");
  61. goto out;
  62. }
  63. rc = pci_request_regions(pdev, "jsm");
  64. if (rc) {
  65. dev_err(&pdev->dev, "pci_request_region FAILED\n");
  66. goto out_disable_device;
  67. }
  68. brd = kzalloc(sizeof(*brd), GFP_KERNEL);
  69. if (!brd) {
  70. rc = -ENOMEM;
  71. goto out_release_regions;
  72. }
  73. /* store the info for the board we've found */
  74. brd->boardnum = adapter_count++;
  75. brd->pci_dev = pdev;
  76. switch (pdev->device) {
  77. case PCI_DEVICE_ID_NEO_2DB9:
  78. case PCI_DEVICE_ID_NEO_2DB9PRI:
  79. case PCI_DEVICE_ID_NEO_2RJ45:
  80. case PCI_DEVICE_ID_NEO_2RJ45PRI:
  81. case PCI_DEVICE_ID_NEO_2_422_485:
  82. brd->maxports = 2;
  83. break;
  84. case PCI_DEVICE_ID_CLASSIC_4:
  85. case PCI_DEVICE_ID_CLASSIC_4_422:
  86. case PCI_DEVICE_ID_NEO_4:
  87. case PCIE_DEVICE_ID_NEO_4:
  88. case PCIE_DEVICE_ID_NEO_4RJ45:
  89. case PCIE_DEVICE_ID_NEO_4_IBM:
  90. brd->maxports = 4;
  91. break;
  92. case PCI_DEVICE_ID_CLASSIC_8:
  93. case PCI_DEVICE_ID_CLASSIC_8_422:
  94. case PCI_DEVICE_ID_DIGI_NEO_8:
  95. case PCIE_DEVICE_ID_NEO_8:
  96. case PCIE_DEVICE_ID_NEO_8RJ45:
  97. brd->maxports = 8;
  98. break;
  99. default:
  100. brd->maxports = 1;
  101. break;
  102. }
  103. spin_lock_init(&brd->bd_intr_lock);
  104. /* store which revision we have */
  105. brd->rev = pdev->revision;
  106. brd->irq = pdev->irq;
  107. switch (pdev->device) {
  108. case PCI_DEVICE_ID_CLASSIC_4:
  109. case PCI_DEVICE_ID_CLASSIC_4_422:
  110. case PCI_DEVICE_ID_CLASSIC_8:
  111. case PCI_DEVICE_ID_CLASSIC_8_422:
  112. jsm_dbg(INIT, &brd->pci_dev,
  113. "jsm_found_board - Classic adapter\n");
  114. /*
  115. * For PCI ClassicBoards
  116. * PCI Local Address (.i.e. "resource" number) space
  117. * 0 PLX Memory Mapped Config
  118. * 1 PLX I/O Mapped Config
  119. * 2 I/O Mapped UARTs and Status
  120. * 3 Memory Mapped VPD
  121. * 4 Memory Mapped UARTs and Status
  122. */
  123. /* Get the PCI Base Address Registers */
  124. brd->membase = pci_resource_start(pdev, 4);
  125. brd->membase_end = pci_resource_end(pdev, 4);
  126. if (brd->membase & 0x1)
  127. brd->membase &= ~0x3;
  128. else
  129. brd->membase &= ~0xF;
  130. brd->iobase = pci_resource_start(pdev, 1);
  131. brd->iobase_end = pci_resource_end(pdev, 1);
  132. brd->iobase = ((unsigned int)(brd->iobase)) & 0xFFFE;
  133. /* Assign the board_ops struct */
  134. brd->bd_ops = &jsm_cls_ops;
  135. brd->bd_uart_offset = 0x8;
  136. brd->bd_dividend = 921600;
  137. brd->re_map_membase = ioremap(brd->membase,
  138. pci_resource_len(pdev, 4));
  139. if (!brd->re_map_membase) {
  140. dev_err(&pdev->dev,
  141. "Card has no PCI Memory resources, failing board.\n");
  142. rc = -ENOMEM;
  143. goto out_kfree_brd;
  144. }
  145. /*
  146. * Enable Local Interrupt 1 (0x1),
  147. * Local Interrupt 1 Polarity Active high (0x2),
  148. * Enable PCI interrupt (0x43)
  149. */
  150. outb(0x43, brd->iobase + 0x4c);
  151. break;
  152. case PCI_DEVICE_ID_NEO_2DB9:
  153. case PCI_DEVICE_ID_NEO_2DB9PRI:
  154. case PCI_DEVICE_ID_NEO_2RJ45:
  155. case PCI_DEVICE_ID_NEO_2RJ45PRI:
  156. case PCI_DEVICE_ID_NEO_2_422_485:
  157. case PCI_DEVICE_ID_NEO_4:
  158. case PCIE_DEVICE_ID_NEO_4:
  159. case PCIE_DEVICE_ID_NEO_4RJ45:
  160. case PCIE_DEVICE_ID_NEO_4_IBM:
  161. case PCI_DEVICE_ID_DIGI_NEO_8:
  162. case PCIE_DEVICE_ID_NEO_8:
  163. case PCIE_DEVICE_ID_NEO_8RJ45:
  164. jsm_dbg(INIT, &brd->pci_dev, "jsm_found_board - NEO adapter\n");
  165. /* get the PCI Base Address Registers */
  166. brd->membase = pci_resource_start(pdev, 0);
  167. brd->membase_end = pci_resource_end(pdev, 0);
  168. if (brd->membase & 1)
  169. brd->membase &= ~0x3;
  170. else
  171. brd->membase &= ~0xF;
  172. /* Assign the board_ops struct */
  173. brd->bd_ops = &jsm_neo_ops;
  174. brd->bd_uart_offset = 0x200;
  175. brd->bd_dividend = 921600;
  176. brd->re_map_membase = ioremap(brd->membase,
  177. pci_resource_len(pdev, 0));
  178. if (!brd->re_map_membase) {
  179. dev_err(&pdev->dev,
  180. "Card has no PCI Memory resources, failing board.\n");
  181. rc = -ENOMEM;
  182. goto out_kfree_brd;
  183. }
  184. break;
  185. default:
  186. return -ENXIO;
  187. }
  188. rc = request_irq(brd->irq, brd->bd_ops->intr, IRQF_SHARED, "JSM", brd);
  189. if (rc) {
  190. dev_warn(&pdev->dev, "Failed to hook IRQ %d\n", brd->irq);
  191. goto out_iounmap;
  192. }
  193. rc = jsm_tty_init(brd);
  194. if (rc < 0) {
  195. dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
  196. rc = -ENXIO;
  197. goto out_free_irq;
  198. }
  199. rc = jsm_uart_port_init(brd);
  200. if (rc < 0) {
  201. /* XXX: leaking all resources from jsm_tty_init here! */
  202. dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
  203. rc = -ENXIO;
  204. goto out_free_irq;
  205. }
  206. /* Log the information about the board */
  207. dev_info(&pdev->dev, "board %d: Digi Classic/Neo (rev %d), irq %d\n",
  208. adapter_count, brd->rev, brd->irq);
  209. pci_set_drvdata(pdev, brd);
  210. pci_save_state(pdev);
  211. return 0;
  212. out_free_irq:
  213. jsm_remove_uart_port(brd);
  214. free_irq(brd->irq, brd);
  215. out_iounmap:
  216. iounmap(brd->re_map_membase);
  217. out_kfree_brd:
  218. kfree(brd);
  219. out_release_regions:
  220. pci_release_regions(pdev);
  221. out_disable_device:
  222. pci_disable_device(pdev);
  223. out:
  224. return rc;
  225. }
  226. static void jsm_remove_one(struct pci_dev *pdev)
  227. {
  228. struct jsm_board *brd = pci_get_drvdata(pdev);
  229. int i = 0;
  230. switch (pdev->device) {
  231. case PCI_DEVICE_ID_CLASSIC_4:
  232. case PCI_DEVICE_ID_CLASSIC_4_422:
  233. case PCI_DEVICE_ID_CLASSIC_8:
  234. case PCI_DEVICE_ID_CLASSIC_8_422:
  235. /* Tell card not to interrupt anymore. */
  236. outb(0x0, brd->iobase + 0x4c);
  237. break;
  238. default:
  239. break;
  240. }
  241. jsm_remove_uart_port(brd);
  242. free_irq(brd->irq, brd);
  243. iounmap(brd->re_map_membase);
  244. /* Free all allocated channels structs */
  245. for (i = 0; i < brd->maxports; i++) {
  246. if (brd->channels[i]) {
  247. kfree(brd->channels[i]->ch_rqueue);
  248. kfree(brd->channels[i]->ch_equeue);
  249. kfree(brd->channels[i]);
  250. }
  251. }
  252. pci_release_regions(pdev);
  253. pci_disable_device(pdev);
  254. kfree(brd);
  255. }
  256. static struct pci_device_id jsm_pci_tbl[] = {
  257. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
  258. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
  259. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
  260. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
  261. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
  262. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
  263. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_4), 0, 0, 6 },
  264. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_1_422), 0, 0, 7 },
  265. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_1_422_485), 0, 0, 8 },
  266. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2_422_485), 0, 0, 9 },
  267. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_8), 0, 0, 10 },
  268. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4), 0, 0, 11 },
  269. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4RJ45), 0, 0, 12 },
  270. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_8RJ45), 0, 0, 13 },
  271. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_4), 0, 0, 14 },
  272. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_4_422), 0, 0, 15 },
  273. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_8), 0, 0, 16 },
  274. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_8_422), 0, 0, 17 },
  275. { 0, }
  276. };
  277. MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
  278. static struct pci_driver jsm_driver = {
  279. .name = "jsm",
  280. .id_table = jsm_pci_tbl,
  281. .probe = jsm_probe_one,
  282. .remove = jsm_remove_one,
  283. .err_handler = &jsm_err_handler,
  284. };
  285. static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
  286. pci_channel_state_t state)
  287. {
  288. struct jsm_board *brd = pci_get_drvdata(pdev);
  289. jsm_remove_uart_port(brd);
  290. return PCI_ERS_RESULT_NEED_RESET;
  291. }
  292. static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
  293. {
  294. int rc;
  295. rc = pci_enable_device(pdev);
  296. if (rc)
  297. return PCI_ERS_RESULT_DISCONNECT;
  298. pci_set_master(pdev);
  299. return PCI_ERS_RESULT_RECOVERED;
  300. }
  301. static void jsm_io_resume(struct pci_dev *pdev)
  302. {
  303. struct jsm_board *brd = pci_get_drvdata(pdev);
  304. pci_restore_state(pdev);
  305. pci_save_state(pdev);
  306. jsm_uart_port_init(brd);
  307. }
  308. static int __init jsm_init_module(void)
  309. {
  310. int rc;
  311. rc = uart_register_driver(&jsm_uart_driver);
  312. if (!rc) {
  313. rc = pci_register_driver(&jsm_driver);
  314. if (rc)
  315. uart_unregister_driver(&jsm_uart_driver);
  316. }
  317. return rc;
  318. }
  319. static void __exit jsm_exit_module(void)
  320. {
  321. pci_unregister_driver(&jsm_driver);
  322. uart_unregister_driver(&jsm_uart_driver);
  323. }
  324. module_init(jsm_init_module);
  325. module_exit(jsm_exit_module);