jsm_driver.c 7.3 KB

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