0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/module.h>
0017 #include <linux/kernel.h>
0018 #include <net/bluetooth/bluetooth.h>
0019 #include <net/bluetooth/hci_core.h>
0020 #include <asm/unaligned.h>
0021 #include <net/rsi_91x.h>
0022
0023 #define RSI_DMA_ALIGN 8
0024 #define RSI_FRAME_DESC_SIZE 16
0025 #define RSI_HEADROOM_FOR_BT_HAL (RSI_FRAME_DESC_SIZE + RSI_DMA_ALIGN)
0026
0027 struct rsi_hci_adapter {
0028 void *priv;
0029 struct rsi_proto_ops *proto_ops;
0030 struct hci_dev *hdev;
0031 };
0032
0033 static int rsi_hci_open(struct hci_dev *hdev)
0034 {
0035 return 0;
0036 }
0037
0038 static int rsi_hci_close(struct hci_dev *hdev)
0039 {
0040 return 0;
0041 }
0042
0043 static int rsi_hci_flush(struct hci_dev *hdev)
0044 {
0045 return 0;
0046 }
0047
0048 static int rsi_hci_send_pkt(struct hci_dev *hdev, struct sk_buff *skb)
0049 {
0050 struct rsi_hci_adapter *h_adapter = hci_get_drvdata(hdev);
0051 struct sk_buff *new_skb = NULL;
0052
0053 switch (hci_skb_pkt_type(skb)) {
0054 case HCI_COMMAND_PKT:
0055 hdev->stat.cmd_tx++;
0056 break;
0057 case HCI_ACLDATA_PKT:
0058 hdev->stat.acl_tx++;
0059 break;
0060 case HCI_SCODATA_PKT:
0061 hdev->stat.sco_tx++;
0062 break;
0063 }
0064
0065 if (skb_headroom(skb) < RSI_HEADROOM_FOR_BT_HAL) {
0066
0067 new_skb = skb_realloc_headroom(skb, RSI_HEADROOM_FOR_BT_HAL);
0068 if (unlikely(!new_skb))
0069 return -ENOMEM;
0070 bt_cb(new_skb)->pkt_type = hci_skb_pkt_type(skb);
0071 kfree_skb(skb);
0072 skb = new_skb;
0073 if (!IS_ALIGNED((unsigned long)skb->data, RSI_DMA_ALIGN)) {
0074 u8 *skb_data = skb->data;
0075 int skb_len = skb->len;
0076
0077 skb_push(skb, RSI_DMA_ALIGN);
0078 skb_pull(skb, PTR_ALIGN(skb->data,
0079 RSI_DMA_ALIGN) - skb->data);
0080 memmove(skb->data, skb_data, skb_len);
0081 skb_trim(skb, skb_len);
0082 }
0083 }
0084
0085 return h_adapter->proto_ops->coex_send_pkt(h_adapter->priv, skb,
0086 RSI_BT_Q);
0087 }
0088
0089 static int rsi_hci_recv_pkt(void *priv, const u8 *pkt)
0090 {
0091 struct rsi_hci_adapter *h_adapter = priv;
0092 struct hci_dev *hdev = h_adapter->hdev;
0093 struct sk_buff *skb;
0094 int pkt_len = get_unaligned_le16(pkt) & 0x0fff;
0095
0096 skb = dev_alloc_skb(pkt_len);
0097 if (!skb)
0098 return -ENOMEM;
0099
0100 memcpy(skb->data, pkt + RSI_FRAME_DESC_SIZE, pkt_len);
0101 skb_put(skb, pkt_len);
0102 h_adapter->hdev->stat.byte_rx += skb->len;
0103
0104 hci_skb_pkt_type(skb) = pkt[14];
0105
0106 return hci_recv_frame(hdev, skb);
0107 }
0108
0109 static int rsi_hci_attach(void *priv, struct rsi_proto_ops *ops)
0110 {
0111 struct rsi_hci_adapter *h_adapter = NULL;
0112 struct hci_dev *hdev;
0113 int err = 0;
0114
0115 h_adapter = kzalloc(sizeof(*h_adapter), GFP_KERNEL);
0116 if (!h_adapter)
0117 return -ENOMEM;
0118
0119 h_adapter->priv = priv;
0120 ops->set_bt_context(priv, h_adapter);
0121 h_adapter->proto_ops = ops;
0122
0123 hdev = hci_alloc_dev();
0124 if (!hdev) {
0125 BT_ERR("Failed to alloc HCI device");
0126 goto err;
0127 }
0128
0129 h_adapter->hdev = hdev;
0130
0131 if (ops->get_host_intf(priv) == RSI_HOST_INTF_SDIO)
0132 hdev->bus = HCI_SDIO;
0133 else
0134 hdev->bus = HCI_USB;
0135
0136 hci_set_drvdata(hdev, h_adapter);
0137 hdev->dev_type = HCI_PRIMARY;
0138 hdev->open = rsi_hci_open;
0139 hdev->close = rsi_hci_close;
0140 hdev->flush = rsi_hci_flush;
0141 hdev->send = rsi_hci_send_pkt;
0142
0143 err = hci_register_dev(hdev);
0144 if (err < 0) {
0145 BT_ERR("HCI registration failed with errcode %d", err);
0146 hci_free_dev(hdev);
0147 goto err;
0148 }
0149
0150 return 0;
0151 err:
0152 h_adapter->hdev = NULL;
0153 kfree(h_adapter);
0154 return -EINVAL;
0155 }
0156
0157 static void rsi_hci_detach(void *priv)
0158 {
0159 struct rsi_hci_adapter *h_adapter = priv;
0160 struct hci_dev *hdev;
0161
0162 if (!h_adapter)
0163 return;
0164
0165 hdev = h_adapter->hdev;
0166 if (hdev) {
0167 hci_unregister_dev(hdev);
0168 hci_free_dev(hdev);
0169 h_adapter->hdev = NULL;
0170 }
0171
0172 kfree(h_adapter);
0173 }
0174
0175 const struct rsi_mod_ops rsi_bt_ops = {
0176 .attach = rsi_hci_attach,
0177 .detach = rsi_hci_detach,
0178 .recv_pkt = rsi_hci_recv_pkt,
0179 };
0180 EXPORT_SYMBOL(rsi_bt_ops);
0181
0182 static int rsi_91x_bt_module_init(void)
0183 {
0184 return 0;
0185 }
0186
0187 static void rsi_91x_bt_module_exit(void)
0188 {
0189 return;
0190 }
0191
0192 module_init(rsi_91x_bt_module_init);
0193 module_exit(rsi_91x_bt_module_exit);
0194 MODULE_AUTHOR("Redpine Signals Inc");
0195 MODULE_DESCRIPTION("RSI BT driver");
0196 MODULE_LICENSE("Dual BSD/GPL");