rsi_91x_usb_ops.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) 2014 Redpine Signals Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. *
  16. */
  17. #include <linux/firmware.h>
  18. #include "rsi_usb.h"
  19. /**
  20. * rsi_copy_to_card() - This function includes the actual funtionality of
  21. * copying the TA firmware to the card.Basically this
  22. * function includes opening the TA file,reading the TA
  23. * file and writing their values in blocks of data.
  24. * @common: Pointer to the driver private structure.
  25. * @fw: Pointer to the firmware value to be written.
  26. * @len: length of firmware file.
  27. * @num_blocks: Number of blocks to be written to the card.
  28. *
  29. * Return: 0 on success and -1 on failure.
  30. */
  31. static int rsi_copy_to_card(struct rsi_common *common,
  32. const u8 *fw,
  33. u32 len,
  34. u32 num_blocks)
  35. {
  36. struct rsi_hw *adapter = common->priv;
  37. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  38. u32 indx, ii;
  39. u32 block_size = dev->tx_blk_size;
  40. u32 lsb_address;
  41. u32 base_address;
  42. base_address = TA_LOAD_ADDRESS;
  43. for (indx = 0, ii = 0; ii < num_blocks; ii++, indx += block_size) {
  44. lsb_address = base_address;
  45. if (rsi_usb_write_register_multiple(adapter,
  46. lsb_address,
  47. (u8 *)(fw + indx),
  48. block_size)) {
  49. rsi_dbg(ERR_ZONE,
  50. "%s: Unable to load %s blk\n", __func__,
  51. FIRMWARE_RSI9113);
  52. return -EIO;
  53. }
  54. rsi_dbg(INIT_ZONE, "%s: loading block: %d\n", __func__, ii);
  55. base_address += block_size;
  56. }
  57. if (len % block_size) {
  58. lsb_address = base_address;
  59. if (rsi_usb_write_register_multiple(adapter,
  60. lsb_address,
  61. (u8 *)(fw + indx),
  62. len % block_size)) {
  63. rsi_dbg(ERR_ZONE,
  64. "%s: Unable to load %s blk\n", __func__,
  65. FIRMWARE_RSI9113);
  66. return -EIO;
  67. }
  68. }
  69. rsi_dbg(INIT_ZONE,
  70. "%s: Succesfully loaded %s instructions\n", __func__,
  71. FIRMWARE_RSI9113);
  72. rsi_dbg(INIT_ZONE, "%s: loaded firmware\n", __func__);
  73. return 0;
  74. }
  75. /**
  76. * rsi_usb_rx_thread() - This is a kernel thread to receive the packets from
  77. * the USB device.
  78. * @common: Pointer to the driver private structure.
  79. *
  80. * Return: None.
  81. */
  82. void rsi_usb_rx_thread(struct rsi_common *common)
  83. {
  84. struct rsi_hw *adapter = common->priv;
  85. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  86. int status;
  87. do {
  88. rsi_wait_event(&dev->rx_thread.event, EVENT_WAIT_FOREVER);
  89. if (atomic_read(&dev->rx_thread.thread_done))
  90. goto out;
  91. mutex_lock(&common->tx_rxlock);
  92. status = rsi_read_pkt(common, 0);
  93. if (status) {
  94. rsi_dbg(ERR_ZONE, "%s: Failed To read data", __func__);
  95. mutex_unlock(&common->tx_rxlock);
  96. return;
  97. }
  98. mutex_unlock(&common->tx_rxlock);
  99. rsi_reset_event(&dev->rx_thread.event);
  100. if (adapter->rx_urb_submit(adapter)) {
  101. rsi_dbg(ERR_ZONE,
  102. "%s: Failed in urb submission", __func__);
  103. return;
  104. }
  105. } while (1);
  106. out:
  107. rsi_dbg(INFO_ZONE, "%s: Terminated thread\n", __func__);
  108. complete_and_exit(&dev->rx_thread.completion, 0);
  109. }
  110. /**
  111. * rsi_load_ta_instructions() - This function includes the actual funtionality
  112. * of loading the TA firmware.This function also
  113. * includes opening the TA file,reading the TA
  114. * file and writing their value in blocks of data.
  115. * @common: Pointer to the driver private structure.
  116. *
  117. * Return: status: 0 on success, -1 on failure.
  118. */
  119. static int rsi_load_ta_instructions(struct rsi_common *common)
  120. {
  121. struct rsi_hw *adapter = common->priv;
  122. struct rsi_91x_usbdev *dev = (struct rsi_91x_usbdev *)adapter->rsi_dev;
  123. const struct firmware *fw_entry = NULL;
  124. u32 block_size = dev->tx_blk_size;
  125. const u8 *fw;
  126. u32 num_blocks, len;
  127. int status = 0;
  128. status = request_firmware(&fw_entry, FIRMWARE_RSI9113, adapter->device);
  129. if (status < 0) {
  130. rsi_dbg(ERR_ZONE, "%s Firmware file %s not found\n",
  131. __func__, FIRMWARE_RSI9113);
  132. return status;
  133. }
  134. /* Copy firmware into DMA-accessible memory */
  135. fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
  136. if (!fw) {
  137. status = -ENOMEM;
  138. goto out;
  139. }
  140. len = fw_entry->size;
  141. if (len % 4)
  142. len += (4 - (len % 4));
  143. num_blocks = (len / block_size);
  144. rsi_dbg(INIT_ZONE, "%s: Instruction size:%d\n", __func__, len);
  145. rsi_dbg(INIT_ZONE, "%s: num blocks: %d\n", __func__, num_blocks);
  146. status = rsi_copy_to_card(common, fw, len, num_blocks);
  147. kfree(fw);
  148. out:
  149. release_firmware(fw_entry);
  150. return status;
  151. }
  152. /**
  153. * rsi_device_init() - This Function Initializes The HAL.
  154. * @common: Pointer to the driver private structure.
  155. *
  156. * Return: 0 on success, -1 on failure.
  157. */
  158. int rsi_usb_device_init(struct rsi_common *common)
  159. {
  160. if (rsi_load_ta_instructions(common))
  161. return -EIO;
  162. return 0;
  163. }