]> git.proxmox.com Git - qemu.git/blobdiff - hw/bt-hci.c
Use glib memory allocation and free functions
[qemu.git] / hw / bt-hci.c
index 714e72671c802de0882dafb818dd9f27b38746ce..a3a7fb49e2ec9e9c2e4cdc5add2323feba0b8188 100644 (file)
@@ -15,9 +15,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
- * MA  02110-1301  USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "qemu-common.h"
@@ -87,6 +85,7 @@ struct bt_hci_link_s {
 };
 
 /* LMP layer emulation */
+#if 0
 static void bt_submit_lmp(struct bt_device_s *bt, int length, uint8_t *data)
 {
     int resp, resplen, error, op, tr;
@@ -386,7 +385,7 @@ static void bt_submit_lmp(struct bt_device_s *bt, int length, uint8_t *data)
     respdata[0] |= tr;
 }
 
-void bt_submit_raw_acl(struct bt_piconet_s *net, int length, uint8_t *data)
+static void bt_submit_raw_acl(struct bt_piconet_s *net, int length, uint8_t *data)
 {
     struct bt_device_s *slave;
     if (length < 1)
@@ -411,6 +410,7 @@ void bt_submit_raw_acl(struct bt_piconet_s *net, int length, uint8_t *data)
         break;
     }
 }
+#endif
 
 /* HCI layer emulation */
 
@@ -419,7 +419,7 @@ void bt_submit_raw_acl(struct bt_piconet_s *net, int length, uint8_t *data)
  * be continuously allocated.  We do it though, to preserve similar
  * behaviour between hosts.  Some things, like the BD_ADDR cannot be
  * preserved though (for example if a real hci is used).  */
-#ifdef WORDS_BIGENDIAN
+#ifdef HOST_WORDS_BIGENDIAN
 # define HNDL(raw)     bswap16(raw)
 #else
 # define HNDL(raw)     (raw)
@@ -444,7 +444,7 @@ static inline uint8_t *bt_hci_event_start(struct bt_hci_s *hci,
     mask_byte = (evt - 1) >> 3;
     mask = 1 << ((evt - 1) & 3);
     if (mask & bt_event_reserved_mask[mask_byte] & ~hci->event_mask[mask_byte])
-        return 0;
+        return NULL;
 
     packet = hci->evt_packet(hci->opaque);
     packet[0] = evt;
@@ -562,9 +562,11 @@ static void bt_hci_inquiry_result(struct bt_hci_s *hci,
 
     switch (hci->lm.inquiry_mode) {
     case 0x00:
-        return bt_hci_inquiry_result_standard(hci, slave);
+        bt_hci_inquiry_result_standard(hci, slave);
+        return;
     case 0x01:
-        return bt_hci_inquiry_result_with_rssi(hci, slave);
+        bt_hci_inquiry_result_with_rssi(hci, slave);
+        return;
     default:
         fprintf(stderr, "%s: bad inquiry mode %02x\n", __FUNCTION__,
                         hci->lm.inquiry_mode);
@@ -574,8 +576,8 @@ static void bt_hci_inquiry_result(struct bt_hci_s *hci,
 
 static void bt_hci_mod_timer_1280ms(QEMUTimer *timer, int period)
 {
-    qemu_mod_timer(timer, qemu_get_clock(vm_clock) +
-                    muldiv64(period << 7, ticks_per_sec, 100));
+    qemu_mod_timer(timer, qemu_get_clock_ns(vm_clock) +
+                   muldiv64(period << 7, get_ticks_per_sec(), 100));
 }
 
 static void bt_hci_inquiry_start(struct bt_hci_s *hci, int length)
@@ -655,14 +657,14 @@ static void bt_hci_lmp_link_establish(struct bt_hci_s *hci,
     if (master) {
         link->acl_mode = acl_active;
         hci->lm.handle[hci->lm.last_handle].acl_mode_timer =
-                qemu_new_timer(vm_clock, bt_hci_mode_tick, link);
+                qemu_new_timer_ns(vm_clock, bt_hci_mode_tick, link);
     }
 }
 
 static void bt_hci_lmp_link_teardown(struct bt_hci_s *hci, uint16_t handle)
 {
     handle &= ~HCI_HANDLE_OFFSET;
-    hci->lm.handle[handle].link = 0;
+    hci->lm.handle[handle].link = NULL;
 
     if (bt_hci_role_master(hci, handle)) {
         qemu_del_timer(hci->lm.handle[handle].acl_mode_timer);
@@ -719,7 +721,7 @@ static void bt_hci_connection_reject_event(struct bt_hci_s *hci,
 static void bt_hci_connection_accept(struct bt_hci_s *hci,
                 struct bt_device_s *host)
 {
-    struct bt_hci_link_s *link = qemu_mallocz(sizeof(struct bt_hci_link_s));
+    struct bt_hci_link_s *link = g_malloc0(sizeof(struct bt_hci_link_s));
     evt_conn_complete params;
     uint16_t handle;
     uint8_t status = HCI_SUCCESS;
@@ -734,7 +736,7 @@ static void bt_hci_connection_accept(struct bt_hci_s *hci,
             tries);
 
     if (!tries) {
-        qemu_free(link);
+        g_free(link);
         bt_hci_connection_reject(hci, host, HCI_REJECTED_LIMITED_RESOURCES);
         status = HCI_NO_CONNECTION;
         goto complete;
@@ -769,9 +771,11 @@ static void bt_hci_lmp_connection_request(struct bt_link_s *link)
     struct bt_hci_s *hci = hci_from_device(link->slave);
     evt_conn_request params;
 
-    if (hci->conn_req_host)
-        return bt_hci_connection_reject(hci, link->host,
-                        HCI_REJECTED_LIMITED_RESOURCES);
+    if (hci->conn_req_host) {
+        bt_hci_connection_reject(hci, link->host,
+                                 HCI_REJECTED_LIMITED_RESOURCES);
+        return;
+    }
     hci->conn_req_host = link->host;
     /* TODO: if masked and auto-accept, then auto-accept,
      * if masked and not auto-accept, then auto-reject */
@@ -889,7 +893,7 @@ static void bt_hci_disconnect(struct bt_hci_s *hci,
 
     /* We are the slave, we get to clean this burden */
     link = (struct bt_hci_link_s *) btlink;
-    qemu_free(link);
+    g_free(link);
 
 complete:
     bt_hci_lmp_link_teardown(hci, handle);
@@ -924,7 +928,7 @@ static void bt_hci_lmp_disconnect_slave(struct bt_link_s *btlink)
     uint16_t handle = link->handle;
     evt_disconn_complete params;
 
-    qemu_free(link);
+    g_free(link);
 
     bt_hci_lmp_link_teardown(hci, handle);
 
@@ -990,13 +994,12 @@ static int bt_hci_features_req(struct bt_hci_s *hci, uint16_t handle)
 
 static int bt_hci_version_req(struct bt_hci_s *hci, uint16_t handle)
 {
-    struct bt_device_s *slave;
     evt_read_remote_version_complete params;
 
     if (bt_hci_handle_bad(hci, handle))
         return -ENODEV;
 
-    slave = bt_hci_remote_dev(hci, handle);
+    bt_hci_remote_dev(hci, handle);
 
     bt_hci_event_status(hci, HCI_SUCCESS);
 
@@ -1081,8 +1084,8 @@ static int bt_hci_mode_change(struct bt_hci_s *hci, uint16_t handle,
 
     bt_hci_event_status(hci, HCI_SUCCESS);
 
-    qemu_mod_timer(link->acl_mode_timer, qemu_get_clock(vm_clock) +
-                            muldiv64(interval * 625, ticks_per_sec, 1000000));
+    qemu_mod_timer(link->acl_mode_timer, qemu_get_clock_ns(vm_clock) +
+                   muldiv64(interval * 625, get_ticks_per_sec(), 1000000));
     bt_hci_lmp_mode_change_master(hci, link->link, mode, interval);
 
     return 0;
@@ -1118,7 +1121,7 @@ static void bt_hci_mode_tick(void *opaque)
     bt_hci_lmp_mode_change_master(hci, link, acl_active, 0);
 }
 
-void bt_hci_reset(struct bt_hci_s *hci)
+static void bt_hci_reset(struct bt_hci_s *hci)
 {
     hci->acl_len = 0;
     hci->last_cmd = 0;
@@ -1135,8 +1138,8 @@ void bt_hci_reset(struct bt_hci_s *hci)
     hci->device.inquiry_scan = 0;
     hci->device.page_scan = 0;
     if (hci->device.lmp_name)
-        free((void *) hci->device.lmp_name);
-    hci->device.lmp_name = 0;
+        g_free((void *) hci->device.lmp_name);
+    hci->device.lmp_name = NULL;
     hci->device.class[0] = 0x00;
     hci->device.class[1] = 0x00;
     hci->device.class[2] = 0x00;
@@ -1385,7 +1388,7 @@ static inline void bt_hci_event_complete_read_local_name(struct bt_hci_s *hci)
     params.status = HCI_SUCCESS;
     memset(params.name, 0, sizeof(params.name));
     if (hci->device.lmp_name)
-        pstrcpy(params.name, sizeof(params.name), hci->device.lmp_name);
+        strncpy(params.name, hci->device.lmp_name, sizeof(params.name));
 
     bt_hci_event_complete(hci, &params, READ_LOCAL_NAME_RP_SIZE);
 }
@@ -1615,7 +1618,7 @@ static void bt_submit_hci(struct HCIInfo *info,
 
         bt_hci_event_status(hci, HCI_SUCCESS);
         bt_hci_connection_accept(hci, hci->conn_req_host);
-        hci->conn_req_host = 0;
+        hci->conn_req_host = NULL;
         break;
 
     case cmd_opcode_pack(OGF_LINK_CTL, OCF_REJECT_CONN_REQ):
@@ -1632,7 +1635,7 @@ static void bt_submit_hci(struct HCIInfo *info,
         bt_hci_connection_reject(hci, hci->conn_req_host,
                         PARAM(reject_conn_req, reason));
         bt_hci_connection_reject_event(hci, &hci->conn_req_host->bd_addr);
-        hci->conn_req_host = 0;
+        hci->conn_req_host = NULL;
         break;
 
     case cmd_opcode_pack(OGF_LINK_CTL, OCF_AUTH_REQUESTED):
@@ -1813,8 +1816,8 @@ static void bt_submit_hci(struct HCIInfo *info,
         LENGTH_CHECK(change_local_name);
 
         if (hci->device.lmp_name)
-            free((void *) hci->device.lmp_name);
-        hci->device.lmp_name = strndup(PARAM(change_local_name, name),
+            g_free((void *) hci->device.lmp_name);
+        hci->device.lmp_name = g_strndup(PARAM(change_local_name, name),
                         sizeof(PARAM(change_local_name, name)));
         bt_hci_event_complete_status(hci, HCI_SUCCESS);
         break;
@@ -2076,7 +2079,6 @@ static void bt_submit_sco(struct HCIInfo *info,
                 const uint8_t *data, int length)
 {
     struct bt_hci_s *hci = hci_from_info(info);
-    struct bt_link_s *link;
     uint16_t handle;
     int datalen;
 
@@ -2085,7 +2087,6 @@ static void bt_submit_sco(struct HCIInfo *info,
 
     handle = acl_handle((data[1] << 8) | data[0]);
     datalen = data[2];
-    data += 3;
     length -= 3;
 
     if (bt_hci_handle_bad(hci, handle)) {
@@ -2093,7 +2094,6 @@ static void bt_submit_sco(struct HCIInfo *info,
                         __FUNCTION__, handle);
         return;
     }
-    handle &= ~HCI_HANDLE_OFFSET;
 
     if (datalen > length) {
         fprintf(stderr, "%s: SCO packet too short (%iB < %iB)\n",
@@ -2101,7 +2101,6 @@ static void bt_submit_sco(struct HCIInfo *info,
         return;
     }
 
-    link = hci->lm.handle[handle].link;
     /* TODO */
 
     /* TODO: increase counter and send EVT_NUM_COMP_PKTS if synchronous
@@ -2123,7 +2122,7 @@ static void bt_hci_evt_submit(void *opaque, int len)
     /* TODO: notify upper layer */
     struct bt_hci_s *s = opaque;
 
-    return s->info.evt_recv(s->info.opaque, s->evt_buf, len);
+    s->info.evt_recv(s->info.opaque, s->evt_buf, len);
 }
 
 static int bt_hci_bdaddr_set(struct HCIInfo *info, const uint8_t *bd_addr)
@@ -2139,17 +2138,17 @@ static void bt_hci_destroy(struct bt_device_s *dev)
 {
     struct bt_hci_s *hci = hci_from_device(dev);
 
-    return bt_hci_done(&hci->info);
+    bt_hci_done(&hci->info);
 }
 
 struct HCIInfo *bt_new_hci(struct bt_scatternet_s *net)
 {
-    struct bt_hci_s *s = qemu_mallocz(sizeof(struct bt_hci_s));
+    struct bt_hci_s *s = g_malloc0(sizeof(struct bt_hci_s));
 
-    s->lm.inquiry_done = qemu_new_timer(vm_clock, bt_hci_inquiry_done, s);
-    s->lm.inquiry_next = qemu_new_timer(vm_clock, bt_hci_inquiry_next, s);
+    s->lm.inquiry_done = qemu_new_timer_ns(vm_clock, bt_hci_inquiry_done, s);
+    s->lm.inquiry_next = qemu_new_timer_ns(vm_clock, bt_hci_inquiry_next, s);
     s->conn_accept_timer =
-            qemu_new_timer(vm_clock, bt_hci_conn_accept_timeout, s);
+            qemu_new_timer_ns(vm_clock, bt_hci_conn_accept_timeout, s);
 
     s->evt_packet = bt_hci_evt_packet;
     s->evt_submit = bt_hci_evt_submit;
@@ -2189,14 +2188,16 @@ static void bt_hci_done(struct HCIInfo *info)
     bt_device_done(&hci->device);
 
     if (hci->device.lmp_name)
-        free((void *) hci->device.lmp_name);
+        g_free((void *) hci->device.lmp_name);
 
     /* Be gentle and send DISCONNECT to all connected peers and those
      * currently waiting for us to accept or reject a connection request.
      * This frees the links.  */
-    if (hci->conn_req_host)
-        return bt_hci_connection_reject(hci,
-                        hci->conn_req_host, HCI_OE_POWER_OFF);
+    if (hci->conn_req_host) {
+        bt_hci_connection_reject(hci,
+                                 hci->conn_req_host, HCI_OE_POWER_OFF);
+        return;
+    }
 
     for (handle = HCI_HANDLE_OFFSET;
                     handle < (HCI_HANDLE_OFFSET | HCI_HANDLES_MAX); handle ++)
@@ -2216,5 +2217,5 @@ static void bt_hci_done(struct HCIInfo *info)
     qemu_free_timer(hci->lm.inquiry_next);
     qemu_free_timer(hci->conn_accept_timer);
 
-    qemu_free(hci);
+    g_free(hci);
 }