sdio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Broadcom B43 wireless driver
  3. *
  4. * SDIO over Sonics Silicon Backplane bus glue for b43.
  5. *
  6. * Copyright (C) 2009 Albert Herranz
  7. * Copyright (C) 2009 Michael Buesch <m@bues.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/sdio_func.h>
  17. #include <linux/mmc/sdio_ids.h>
  18. #include <linux/slab.h>
  19. #include <linux/ssb/ssb.h>
  20. #include "sdio.h"
  21. #include "b43.h"
  22. #define HNBU_CHIPID 0x01 /* vendor & device id */
  23. #define B43_SDIO_BLOCK_SIZE 64 /* rx fifo max size in bytes */
  24. static const struct b43_sdio_quirk {
  25. u16 vendor;
  26. u16 device;
  27. unsigned int quirks;
  28. } b43_sdio_quirks[] = {
  29. { 0x14E4, 0x4318, SSB_QUIRK_SDIO_READ_AFTER_WRITE32, },
  30. { },
  31. };
  32. static unsigned int b43_sdio_get_quirks(u16 vendor, u16 device)
  33. {
  34. const struct b43_sdio_quirk *q;
  35. for (q = b43_sdio_quirks; q->quirks; q++) {
  36. if (vendor == q->vendor && device == q->device)
  37. return q->quirks;
  38. }
  39. return 0;
  40. }
  41. static void b43_sdio_interrupt_dispatcher(struct sdio_func *func)
  42. {
  43. struct b43_sdio *sdio = sdio_get_drvdata(func);
  44. struct b43_wldev *dev = sdio->irq_handler_opaque;
  45. if (unlikely(b43_status(dev) < B43_STAT_STARTED))
  46. return;
  47. sdio_release_host(func);
  48. sdio->irq_handler(dev);
  49. sdio_claim_host(func);
  50. }
  51. int b43_sdio_request_irq(struct b43_wldev *dev,
  52. void (*handler)(struct b43_wldev *dev))
  53. {
  54. struct ssb_bus *bus = dev->dev->sdev->bus;
  55. struct sdio_func *func = bus->host_sdio;
  56. struct b43_sdio *sdio = sdio_get_drvdata(func);
  57. int err;
  58. sdio->irq_handler_opaque = dev;
  59. sdio->irq_handler = handler;
  60. sdio_claim_host(func);
  61. err = sdio_claim_irq(func, b43_sdio_interrupt_dispatcher);
  62. sdio_release_host(func);
  63. return err;
  64. }
  65. void b43_sdio_free_irq(struct b43_wldev *dev)
  66. {
  67. struct ssb_bus *bus = dev->dev->sdev->bus;
  68. struct sdio_func *func = bus->host_sdio;
  69. struct b43_sdio *sdio = sdio_get_drvdata(func);
  70. sdio_claim_host(func);
  71. sdio_release_irq(func);
  72. sdio_release_host(func);
  73. sdio->irq_handler_opaque = NULL;
  74. sdio->irq_handler = NULL;
  75. }
  76. static int b43_sdio_probe(struct sdio_func *func,
  77. const struct sdio_device_id *id)
  78. {
  79. struct b43_sdio *sdio;
  80. struct sdio_func_tuple *tuple;
  81. u16 vendor = 0, device = 0;
  82. int error;
  83. /* Look for the card chip identifier. */
  84. tuple = func->tuples;
  85. while (tuple) {
  86. switch (tuple->code) {
  87. case 0x80:
  88. switch (tuple->data[0]) {
  89. case HNBU_CHIPID:
  90. if (tuple->size != 5)
  91. break;
  92. vendor = tuple->data[1] | (tuple->data[2]<<8);
  93. device = tuple->data[3] | (tuple->data[4]<<8);
  94. dev_info(&func->dev, "Chip ID %04x:%04x\n",
  95. vendor, device);
  96. break;
  97. default:
  98. break;
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. tuple = tuple->next;
  105. }
  106. if (!vendor || !device) {
  107. error = -ENODEV;
  108. goto out;
  109. }
  110. sdio_claim_host(func);
  111. error = sdio_set_block_size(func, B43_SDIO_BLOCK_SIZE);
  112. if (error) {
  113. dev_err(&func->dev, "failed to set block size to %u bytes,"
  114. " error %d\n", B43_SDIO_BLOCK_SIZE, error);
  115. goto err_release_host;
  116. }
  117. error = sdio_enable_func(func);
  118. if (error) {
  119. dev_err(&func->dev, "failed to enable func, error %d\n", error);
  120. goto err_release_host;
  121. }
  122. sdio_release_host(func);
  123. sdio = kzalloc(sizeof(*sdio), GFP_KERNEL);
  124. if (!sdio) {
  125. error = -ENOMEM;
  126. dev_err(&func->dev, "failed to allocate ssb bus\n");
  127. goto err_disable_func;
  128. }
  129. error = ssb_bus_sdiobus_register(&sdio->ssb, func,
  130. b43_sdio_get_quirks(vendor, device));
  131. if (error) {
  132. dev_err(&func->dev, "failed to register ssb sdio bus,"
  133. " error %d\n", error);
  134. goto err_free_ssb;
  135. }
  136. sdio_set_drvdata(func, sdio);
  137. return 0;
  138. err_free_ssb:
  139. kfree(sdio);
  140. err_disable_func:
  141. sdio_claim_host(func);
  142. sdio_disable_func(func);
  143. err_release_host:
  144. sdio_release_host(func);
  145. out:
  146. return error;
  147. }
  148. static void b43_sdio_remove(struct sdio_func *func)
  149. {
  150. struct b43_sdio *sdio = sdio_get_drvdata(func);
  151. ssb_bus_unregister(&sdio->ssb);
  152. sdio_claim_host(func);
  153. sdio_disable_func(func);
  154. sdio_release_host(func);
  155. kfree(sdio);
  156. sdio_set_drvdata(func, NULL);
  157. }
  158. static const struct sdio_device_id b43_sdio_ids[] = {
  159. { SDIO_DEVICE(0x02d0, 0x044b) }, /* Nintendo Wii WLAN daughter card */
  160. { SDIO_DEVICE(0x0092, 0x0004) }, /* C-guys, Inc. EW-CG1102GC */
  161. { },
  162. };
  163. static struct sdio_driver b43_sdio_driver = {
  164. .name = "b43-sdio",
  165. .id_table = b43_sdio_ids,
  166. .probe = b43_sdio_probe,
  167. .remove = b43_sdio_remove,
  168. };
  169. int b43_sdio_init(void)
  170. {
  171. return sdio_register_driver(&b43_sdio_driver);
  172. }
  173. void b43_sdio_exit(void)
  174. {
  175. sdio_unregister_driver(&b43_sdio_driver);
  176. }