/drivers/staging/sbe-2t3e3/module.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 210 lines · 160 code · 34 blank · 16 comment · 20 complexity · 650b08617375533acd88fc28f7a7a108 MD5 · raw file

  1. /*
  2. * SBE 2T3E3 synchronous serial card driver for Linux
  3. *
  4. * Copyright (C) 2009-2010 Krzysztof Halasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. * This code is based on a driver written by SBE Inc.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/pci.h>
  17. #include <linux/hdlc.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/interrupt.h>
  20. #include "2t3e3.h"
  21. static void check_leds(unsigned long arg)
  22. {
  23. struct card *card = (struct card *)arg;
  24. struct channel *channel0 = &card->channels[0];
  25. static int blinker;
  26. update_led(channel0, ++blinker);
  27. if (has_two_ports(channel0->pdev))
  28. update_led(&card->channels[1], blinker);
  29. card->timer.expires = jiffies + HZ / 10;
  30. add_timer(&card->timer);
  31. }
  32. static void t3e3_remove_channel(struct channel *channel)
  33. {
  34. struct pci_dev *pdev = channel->pdev;
  35. struct net_device *dev = channel->dev;
  36. /* system hangs if board asserts irq while module is unloaded */
  37. cpld_stop_intr(channel);
  38. free_irq(dev->irq, dev);
  39. dc_drop_descriptor_list(channel);
  40. unregister_hdlc_device(dev);
  41. free_netdev(dev);
  42. pci_release_regions(pdev);
  43. pci_disable_device(pdev);
  44. pci_set_drvdata(pdev, NULL);
  45. }
  46. static int __devinit t3e3_init_channel(struct channel *channel, struct pci_dev *pdev, struct card *card)
  47. {
  48. struct net_device *dev;
  49. unsigned int val;
  50. int err;
  51. err = pci_enable_device(pdev);
  52. if (err)
  53. return err;
  54. err = pci_request_regions(pdev, "SBE 2T3E3");
  55. if (err)
  56. goto disable;
  57. dev = alloc_hdlcdev(channel);
  58. if (!dev) {
  59. printk(KERN_ERR "SBE 2T3E3" ": Out of memory\n");
  60. goto free_regions;
  61. }
  62. t3e3_sc_init(channel);
  63. dev_to_priv(dev) = channel;
  64. channel->pdev = pdev;
  65. channel->dev = dev;
  66. channel->card = card;
  67. channel->addr = pci_resource_start(pdev, 0);
  68. if (pdev->subsystem_device == PCI_SUBDEVICE_ID_SBE_2T3E3_P1)
  69. channel->h.slot = 1;
  70. else
  71. channel->h.slot = 0;
  72. if (setup_device(dev, channel))
  73. goto free_regions;
  74. pci_read_config_dword(channel->pdev, 0x40, &val); /* mask sleep mode */
  75. pci_write_config_dword(channel->pdev, 0x40, val & 0x3FFFFFFF);
  76. pci_read_config_byte(channel->pdev, PCI_CACHE_LINE_SIZE, &channel->h.cache_size);
  77. pci_read_config_dword(channel->pdev, PCI_COMMAND, &channel->h.command);
  78. t3e3_init(channel);
  79. if (request_irq(dev->irq, &t3e3_intr, IRQF_SHARED, dev->name, dev)) {
  80. printk(KERN_WARNING "%s: could not get irq: %d\n", dev->name, dev->irq);
  81. goto free_regions;
  82. }
  83. pci_set_drvdata(pdev, channel);
  84. return 0;
  85. free_regions:
  86. pci_release_regions(pdev);
  87. disable:
  88. pci_disable_device(pdev);
  89. return err;
  90. }
  91. static void __devexit t3e3_remove_card(struct pci_dev *pdev)
  92. {
  93. struct channel *channel0 = pci_get_drvdata(pdev);
  94. struct card *card = channel0->card;
  95. del_timer(&card->timer);
  96. if (has_two_ports(channel0->pdev)) {
  97. t3e3_remove_channel(&card->channels[1]);
  98. pci_dev_put(card->channels[1].pdev);
  99. }
  100. t3e3_remove_channel(channel0);
  101. kfree(card);
  102. }
  103. static int __devinit t3e3_init_card(struct pci_dev *pdev, const struct pci_device_id *ent)
  104. {
  105. /* pdev points to channel #0 */
  106. struct pci_dev *pdev1 = NULL;
  107. struct card *card;
  108. int channels = 1, err;
  109. if (has_two_ports(pdev)) {
  110. while ((pdev1 = pci_get_subsys(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
  111. PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_2T3E3_P1,
  112. pdev1)))
  113. if (pdev1->bus == pdev->bus &&
  114. pdev1->devfn == pdev->devfn + 8 /* next device on the same bus */)
  115. break; /* found the second channel */
  116. if (!pdev1) {
  117. printk(KERN_ERR "SBE 2T3E3" ": Can't find the second channel\n");
  118. return -EFAULT;
  119. }
  120. channels = 2;
  121. /* holds the reference for pdev1 */
  122. }
  123. card = kzalloc(sizeof(struct card) + channels * sizeof(struct channel), GFP_KERNEL);
  124. if (!card) {
  125. printk(KERN_ERR "SBE 2T3E3" ": Out of memory\n");
  126. return -ENOBUFS;
  127. }
  128. spin_lock_init(&card->bootrom_lock);
  129. card->bootrom_addr = pci_resource_start(pdev, 0);
  130. err = t3e3_init_channel(&card->channels[0], pdev, card);
  131. if (err)
  132. goto free_card;
  133. if (channels == 2) {
  134. err = t3e3_init_channel(&card->channels[1], pdev1, card);
  135. if (err) {
  136. t3e3_remove_channel(&card->channels[0]);
  137. goto free_card;
  138. }
  139. }
  140. /* start LED timer */
  141. init_timer(&card->timer);
  142. card->timer.function = check_leds;
  143. card->timer.expires = jiffies + HZ / 10;
  144. card->timer.data = (unsigned long)card;
  145. add_timer(&card->timer);
  146. return 0;
  147. free_card:
  148. kfree(card);
  149. return err;
  150. }
  151. static struct pci_device_id t3e3_pci_tbl[] __devinitdata = {
  152. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
  153. PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_T3E3, 0, 0, 0 },
  154. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142,
  155. PCI_VENDOR_ID_SBE, PCI_SUBDEVICE_ID_SBE_2T3E3_P0, 0, 0, 0 },
  156. /* channel 1 will be initialized after channel 0 */
  157. { 0, }
  158. };
  159. static struct pci_driver t3e3_pci_driver = {
  160. .name = "SBE T3E3",
  161. .id_table = t3e3_pci_tbl,
  162. .probe = t3e3_init_card,
  163. .remove = t3e3_remove_card,
  164. };
  165. static int __init t3e3_init_module(void)
  166. {
  167. return pci_register_driver(&t3e3_pci_driver);
  168. }
  169. static void __exit t3e3_cleanup_module(void)
  170. {
  171. pci_unregister_driver(&t3e3_pci_driver);
  172. }
  173. module_init(t3e3_init_module);
  174. module_exit(t3e3_cleanup_module);
  175. MODULE_LICENSE("GPL");
  176. MODULE_DEVICE_TABLE(pci, t3e3_pci_tbl);