wusb.c 6.1 KB

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