scan.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Ultra Wide Band
  3. * Scanning management
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. *
  24. * FIXME: docs
  25. * FIXME: there are issues here on how BEACON and SCAN on USB RCI deal
  26. * with each other. Currently seems that START_BEACON while
  27. * SCAN_ONLY will cancel the scan, so we need to update the
  28. * state here. Clarification request sent by email on
  29. * 10/05/2005.
  30. * 10/28/2005 No clear answer heard--maybe we'll hack the API
  31. * so that when we start beaconing, if the HC is
  32. * scanning in a mode not compatible with beaconing
  33. * we just fail.
  34. */
  35. #include <linux/device.h>
  36. #include <linux/err.h>
  37. #include <linux/slab.h>
  38. #include <linux/stat.h>
  39. #include "uwb-internal.h"
  40. /**
  41. * Start/stop scanning in a radio controller
  42. *
  43. * @rc: UWB Radio Controller
  44. * @channel: Channel to scan; encodings in WUSB1.0[Table 5.12]
  45. * @type: Type of scanning to do.
  46. * @bpst_offset: value at which to start scanning (if type ==
  47. * UWB_SCAN_ONLY_STARTTIME)
  48. * @returns: 0 if ok, < 0 errno code on error
  49. *
  50. * We put the command on kmalloc'ed memory as some arches cannot do
  51. * USB from the stack. The reply event is copied from an stage buffer,
  52. * so it can be in the stack. See WUSB1.0[8.6.2.4] for more details.
  53. */
  54. int uwb_rc_scan(struct uwb_rc *rc,
  55. unsigned channel, enum uwb_scan_type type,
  56. unsigned bpst_offset)
  57. {
  58. int result;
  59. struct uwb_rc_cmd_scan *cmd;
  60. struct uwb_rc_evt_confirm reply;
  61. result = -ENOMEM;
  62. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  63. if (cmd == NULL)
  64. goto error_kzalloc;
  65. mutex_lock(&rc->uwb_dev.mutex);
  66. cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
  67. cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SCAN);
  68. cmd->bChannelNumber = channel;
  69. cmd->bScanState = type;
  70. cmd->wStartTime = cpu_to_le16(bpst_offset);
  71. reply.rceb.bEventType = UWB_RC_CET_GENERAL;
  72. reply.rceb.wEvent = UWB_RC_CMD_SCAN;
  73. result = uwb_rc_cmd(rc, "SCAN", &cmd->rccb, sizeof(*cmd),
  74. &reply.rceb, sizeof(reply));
  75. if (result < 0)
  76. goto error_cmd;
  77. if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
  78. dev_err(&rc->uwb_dev.dev,
  79. "SCAN: command execution failed: %s (%d)\n",
  80. uwb_rc_strerror(reply.bResultCode), reply.bResultCode);
  81. result = -EIO;
  82. goto error_cmd;
  83. }
  84. rc->scanning = channel;
  85. rc->scan_type = type;
  86. error_cmd:
  87. mutex_unlock(&rc->uwb_dev.mutex);
  88. kfree(cmd);
  89. error_kzalloc:
  90. return result;
  91. }
  92. /*
  93. * Print scanning state
  94. */
  95. static ssize_t uwb_rc_scan_show(struct device *dev,
  96. struct device_attribute *attr, char *buf)
  97. {
  98. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  99. struct uwb_rc *rc = uwb_dev->rc;
  100. ssize_t result;
  101. mutex_lock(&rc->uwb_dev.mutex);
  102. result = sprintf(buf, "%d %d\n", rc->scanning, rc->scan_type);
  103. mutex_unlock(&rc->uwb_dev.mutex);
  104. return result;
  105. }
  106. /*
  107. *
  108. */
  109. static ssize_t uwb_rc_scan_store(struct device *dev,
  110. struct device_attribute *attr,
  111. const char *buf, size_t size)
  112. {
  113. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  114. struct uwb_rc *rc = uwb_dev->rc;
  115. unsigned channel;
  116. unsigned type;
  117. unsigned bpst_offset = 0;
  118. ssize_t result = -EINVAL;
  119. result = sscanf(buf, "%u %u %u\n", &channel, &type, &bpst_offset);
  120. if (result >= 2 && type < UWB_SCAN_TOP)
  121. result = uwb_rc_scan(rc, channel, type, bpst_offset);
  122. return result < 0 ? result : size;
  123. }
  124. /** Radio Control sysfs interface (declaration) */
  125. DEVICE_ATTR(scan, S_IRUGO | S_IWUSR, uwb_rc_scan_show, uwb_rc_scan_store);