wusb.c 4.9 KB

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