wusb.c 5.4 KB

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