wusb.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Wireless Host Controller (WHC) WUSB operations.
  3. *
  4. * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/uwb/umc.h>
  21. #include "../../wusbcore/wusbhc.h"
  22. #include "whcd.h"
  23. static int whc_update_di(struct whc *whc, int idx)
  24. {
  25. int offset = idx / 32;
  26. u32 bit = 1 << (idx % 32);
  27. le_writel(bit, whc->base + WUSBDIBUPDATED + offset);
  28. return whci_wait_for(&whc->umc->dev,
  29. whc->base + WUSBDIBUPDATED + offset, bit, 0,
  30. 100, "DI update");
  31. }
  32. /*
  33. * WHCI starts MMCs based on there being a valid GTK so these need
  34. * only start/stop the asynchronous and periodic schedules and send a
  35. * channel stop command.
  36. */
  37. int whc_wusbhc_start(struct wusbhc *wusbhc)
  38. {
  39. struct whc *whc = wusbhc_to_whc(wusbhc);
  40. asl_start(whc);
  41. pzl_start(whc);
  42. return 0;
  43. }
  44. void whc_wusbhc_stop(struct wusbhc *wusbhc, int delay)
  45. {
  46. struct whc *whc = wusbhc_to_whc(wusbhc);
  47. u32 stop_time, now_time;
  48. int ret;
  49. pzl_stop(whc);
  50. asl_stop(whc);
  51. now_time = le_readl(whc->base + WUSBTIME) & WUSBTIME_CHANNEL_TIME_MASK;
  52. stop_time = (now_time + ((delay * 8) << 7)) & 0x00ffffff;
  53. ret = whc_do_gencmd(whc, WUSBGENCMDSTS_CHAN_STOP, stop_time, NULL, 0);
  54. if (ret == 0)
  55. msleep(delay);
  56. }
  57. int whc_mmcie_add(struct wusbhc *wusbhc, u8 interval, u8 repeat_cnt,
  58. u8 handle, struct wuie_hdr *wuie)
  59. {
  60. struct whc *whc = wusbhc_to_whc(wusbhc);
  61. u32 params;
  62. params = (interval << 24)
  63. | (repeat_cnt << 16)
  64. | (wuie->bLength << 8)
  65. | handle;
  66. return whc_do_gencmd(whc, WUSBGENCMDSTS_MMCIE_ADD, params, wuie, wuie->bLength);
  67. }
  68. int whc_mmcie_rm(struct wusbhc *wusbhc, u8 handle)
  69. {
  70. struct whc *whc = wusbhc_to_whc(wusbhc);
  71. u32 params;
  72. params = handle;
  73. return whc_do_gencmd(whc, WUSBGENCMDSTS_MMCIE_RM, params, NULL, 0);
  74. }
  75. int whc_bwa_set(struct wusbhc *wusbhc, s8 stream_index, const struct uwb_mas_bm *mas_bm)
  76. {
  77. struct whc *whc = wusbhc_to_whc(wusbhc);
  78. if (stream_index >= 0)
  79. whc_write_wusbcmd(whc, WUSBCMD_WUSBSI_MASK, WUSBCMD_WUSBSI(stream_index));
  80. return whc_do_gencmd(whc, WUSBGENCMDSTS_SET_MAS, 0, (void *)mas_bm, sizeof(*mas_bm));
  81. }
  82. int whc_dev_info_set(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
  83. {
  84. struct whc *whc = wusbhc_to_whc(wusbhc);
  85. int idx = wusb_dev->port_idx;
  86. struct di_buf_entry *di = &whc->di_buf[idx];
  87. int ret;
  88. mutex_lock(&whc->mutex);
  89. uwb_mas_bm_copy_le(di->availability_info, &wusb_dev->availability);
  90. di->addr_sec_info &= ~(WHC_DI_DISABLE | WHC_DI_DEV_ADDR_MASK);
  91. di->addr_sec_info |= WHC_DI_DEV_ADDR(wusb_dev->addr);
  92. ret = whc_update_di(whc, idx);
  93. mutex_unlock(&whc->mutex);
  94. return ret;
  95. }
  96. /*
  97. * Set the number of Device Notification Time Slots (DNTS) and enable
  98. * device notifications.
  99. */
  100. int whc_set_num_dnts(struct wusbhc *wusbhc, u8 interval, u8 slots)
  101. {
  102. struct whc *whc = wusbhc_to_whc(wusbhc);
  103. u32 dntsctrl;
  104. dntsctrl = WUSBDNTSCTRL_ACTIVE
  105. | WUSBDNTSCTRL_INTERVAL(interval)
  106. | WUSBDNTSCTRL_SLOTS(slots);
  107. le_writel(dntsctrl, whc->base + WUSBDNTSCTRL);
  108. return 0;
  109. }
  110. static int whc_set_key(struct whc *whc, u8 key_index, uint32_t tkid,
  111. const void *key, size_t key_size, bool is_gtk)
  112. {
  113. uint32_t setkeycmd;
  114. uint32_t seckey[4];
  115. int i;
  116. int ret;
  117. memcpy(seckey, key, key_size);
  118. setkeycmd = WUSBSETSECKEYCMD_SET | WUSBSETSECKEYCMD_IDX(key_index);
  119. if (is_gtk)
  120. setkeycmd |= WUSBSETSECKEYCMD_GTK;
  121. le_writel(tkid, whc->base + WUSBTKID);
  122. for (i = 0; i < 4; i++)
  123. le_writel(seckey[i], whc->base + WUSBSECKEY + 4*i);
  124. le_writel(setkeycmd, whc->base + WUSBSETSECKEYCMD);
  125. ret = whci_wait_for(&whc->umc->dev, whc->base + WUSBSETSECKEYCMD,
  126. WUSBSETSECKEYCMD_SET, 0, 100, "set key");
  127. return ret;
  128. }
  129. /**
  130. * whc_set_ptk - set the PTK to use for a device.
  131. *
  132. * The index into the key table for this PTK is the same as the
  133. * device's port index.
  134. */
  135. int whc_set_ptk(struct wusbhc *wusbhc, u8 port_idx, u32 tkid,
  136. const void *ptk, size_t key_size)
  137. {
  138. struct whc *whc = wusbhc_to_whc(wusbhc);
  139. struct di_buf_entry *di = &whc->di_buf[port_idx];
  140. int ret;
  141. mutex_lock(&whc->mutex);
  142. if (ptk) {
  143. ret = whc_set_key(whc, port_idx, tkid, ptk, key_size, false);
  144. if (ret)
  145. goto out;
  146. di->addr_sec_info &= ~WHC_DI_KEY_IDX_MASK;
  147. di->addr_sec_info |= WHC_DI_SECURE | WHC_DI_KEY_IDX(port_idx);
  148. } else
  149. di->addr_sec_info &= ~WHC_DI_SECURE;
  150. ret = whc_update_di(whc, port_idx);
  151. out:
  152. mutex_unlock(&whc->mutex);
  153. return ret;
  154. }
  155. /**
  156. * whc_set_gtk - set the GTK for subsequent broadcast packets
  157. *
  158. * The GTK is stored in the last entry in the key table (the previous
  159. * N_DEVICES entries are for the per-device PTKs).
  160. */
  161. int whc_set_gtk(struct wusbhc *wusbhc, u32 tkid,
  162. const void *gtk, size_t key_size)
  163. {
  164. struct whc *whc = wusbhc_to_whc(wusbhc);
  165. int ret;
  166. mutex_lock(&whc->mutex);
  167. ret = whc_set_key(whc, whc->n_devices, tkid, gtk, key_size, true);
  168. mutex_unlock(&whc->mutex);
  169. return ret;
  170. }
  171. int whc_set_cluster_id(struct whc *whc, u8 bcid)
  172. {
  173. whc_write_wusbcmd(whc, WUSBCMD_BCID_MASK, WUSBCMD_BCID(bcid));
  174. return 0;
  175. }