Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved. |
| 3 | Copyright (c) 2011,2012 Intel Corp. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 and |
| 7 | only version 2 as published by the Free Software Foundation. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <net/bluetooth/bluetooth.h> |
| 16 | #include <net/bluetooth/hci_core.h> |
| 17 | #include <net/bluetooth/l2cap.h> |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 18 | #include <net/bluetooth/a2mp.h> |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 19 | |
Andrei Emeltchenko | f6d3c6e | 2012-05-29 13:59:03 +0300 | [diff] [blame] | 20 | /* A2MP build & send command helper functions */ |
| 21 | static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data) |
| 22 | { |
| 23 | struct a2mp_cmd *cmd; |
| 24 | int plen; |
| 25 | |
| 26 | plen = sizeof(*cmd) + len; |
| 27 | cmd = kzalloc(plen, GFP_KERNEL); |
| 28 | if (!cmd) |
| 29 | return NULL; |
| 30 | |
| 31 | cmd->code = code; |
| 32 | cmd->ident = ident; |
| 33 | cmd->len = cpu_to_le16(len); |
| 34 | |
| 35 | memcpy(cmd->data, data, len); |
| 36 | |
| 37 | return cmd; |
| 38 | } |
| 39 | |
| 40 | static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, |
| 41 | void *data) |
| 42 | { |
| 43 | struct l2cap_chan *chan = mgr->a2mp_chan; |
| 44 | struct a2mp_cmd *cmd; |
| 45 | u16 total_len = len + sizeof(*cmd); |
| 46 | struct kvec iv; |
| 47 | struct msghdr msg; |
| 48 | |
| 49 | cmd = __a2mp_build(code, ident, len, data); |
| 50 | if (!cmd) |
| 51 | return; |
| 52 | |
| 53 | iv.iov_base = cmd; |
| 54 | iv.iov_len = total_len; |
| 55 | |
| 56 | memset(&msg, 0, sizeof(msg)); |
| 57 | |
| 58 | msg.msg_iov = (struct iovec *) &iv; |
| 59 | msg.msg_iovlen = 1; |
| 60 | |
| 61 | l2cap_chan_send(chan, &msg, total_len, 0); |
| 62 | |
| 63 | kfree(cmd); |
| 64 | } |
| 65 | |
Andrei Emeltchenko | 21dbd2c | 2012-05-29 13:59:08 +0300 | [diff] [blame^] | 66 | /* Processing A2MP messages */ |
| 67 | static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb, |
| 68 | struct a2mp_cmd *hdr) |
| 69 | { |
| 70 | struct a2mp_cmd_rej *rej = (void *) skb->data; |
| 71 | |
| 72 | if (le16_to_cpu(hdr->len) < sizeof(*rej)) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason)); |
| 76 | |
| 77 | skb_pull(skb, sizeof(*rej)); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 82 | /* Handle A2MP signalling */ |
| 83 | static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
| 84 | { |
| 85 | struct a2mp_cmd *hdr = (void *) skb->data; |
| 86 | struct amp_mgr *mgr = chan->data; |
| 87 | int err = 0; |
| 88 | |
| 89 | amp_mgr_get(mgr); |
| 90 | |
| 91 | while (skb->len >= sizeof(*hdr)) { |
| 92 | struct a2mp_cmd *hdr = (void *) skb->data; |
| 93 | u16 len = le16_to_cpu(hdr->len); |
| 94 | |
| 95 | BT_DBG("code 0x%02x id %d len %d", hdr->code, hdr->ident, len); |
| 96 | |
| 97 | skb_pull(skb, sizeof(*hdr)); |
| 98 | |
| 99 | if (len > skb->len || !hdr->ident) { |
| 100 | err = -EINVAL; |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | mgr->ident = hdr->ident; |
| 105 | |
| 106 | switch (hdr->code) { |
| 107 | case A2MP_COMMAND_REJ: |
Andrei Emeltchenko | 21dbd2c | 2012-05-29 13:59:08 +0300 | [diff] [blame^] | 108 | a2mp_command_rej(mgr, skb, hdr); |
| 109 | break; |
| 110 | |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 111 | case A2MP_DISCOVER_REQ: |
| 112 | case A2MP_CHANGE_NOTIFY: |
| 113 | case A2MP_GETINFO_REQ: |
| 114 | case A2MP_GETAMPASSOC_REQ: |
| 115 | case A2MP_CREATEPHYSLINK_REQ: |
| 116 | case A2MP_DISCONNPHYSLINK_REQ: |
| 117 | case A2MP_CHANGE_RSP: |
| 118 | case A2MP_DISCOVER_RSP: |
| 119 | case A2MP_GETINFO_RSP: |
| 120 | case A2MP_GETAMPASSOC_RSP: |
| 121 | case A2MP_CREATEPHYSLINK_RSP: |
| 122 | case A2MP_DISCONNPHYSLINK_RSP: |
| 123 | default: |
| 124 | BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code); |
| 125 | err = -EINVAL; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (err) { |
| 131 | struct a2mp_cmd_rej rej; |
| 132 | rej.reason = __constant_cpu_to_le16(0); |
| 133 | |
| 134 | BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err); |
| 135 | |
| 136 | a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej), |
| 137 | &rej); |
| 138 | } |
| 139 | |
| 140 | /* Always free skb and return success error code to prevent |
| 141 | from sending L2CAP Disconnect over A2MP channel */ |
| 142 | kfree_skb(skb); |
| 143 | |
| 144 | amp_mgr_put(mgr); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 149 | static void a2mp_chan_close_cb(struct l2cap_chan *chan) |
| 150 | { |
| 151 | l2cap_chan_destroy(chan); |
| 152 | } |
| 153 | |
| 154 | static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state) |
| 155 | { |
| 156 | struct amp_mgr *mgr = chan->data; |
| 157 | |
| 158 | if (!mgr) |
| 159 | return; |
| 160 | |
| 161 | BT_DBG("chan %p state %s", chan, state_to_string(state)); |
| 162 | |
| 163 | chan->state = state; |
| 164 | |
| 165 | switch (state) { |
| 166 | case BT_CLOSED: |
| 167 | if (mgr) |
| 168 | amp_mgr_put(mgr); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan, |
| 174 | unsigned long len, int nb) |
| 175 | { |
| 176 | return bt_skb_alloc(len, GFP_KERNEL); |
| 177 | } |
| 178 | |
| 179 | static struct l2cap_chan *a2mp_chan_no_new_conn_cb(struct l2cap_chan *chan) |
| 180 | { |
| 181 | BT_ERR("new_connection for chan %p not implemented", chan); |
| 182 | |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | static void a2mp_chan_no_teardown_cb(struct l2cap_chan *chan, int err) |
| 187 | { |
| 188 | BT_ERR("teardown for chan %p not implemented", chan); |
| 189 | } |
| 190 | |
| 191 | static void a2mp_chan_no_ready(struct l2cap_chan *chan) |
| 192 | { |
| 193 | BT_ERR("ready for chan %p not implemented", chan); |
| 194 | } |
| 195 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 196 | static struct l2cap_ops a2mp_chan_ops = { |
| 197 | .name = "L2CAP A2MP channel", |
Andrei Emeltchenko | 6b44d9b | 2012-05-29 13:59:07 +0300 | [diff] [blame] | 198 | .recv = a2mp_chan_recv_cb, |
Andrei Emeltchenko | 46d5c90 | 2012-05-29 13:59:04 +0300 | [diff] [blame] | 199 | .close = a2mp_chan_close_cb, |
| 200 | .state_change = a2mp_chan_state_change_cb, |
| 201 | .alloc_skb = a2mp_chan_alloc_skb_cb, |
| 202 | |
| 203 | /* Not implemented for A2MP */ |
| 204 | .new_connection = a2mp_chan_no_new_conn_cb, |
| 205 | .teardown = a2mp_chan_no_teardown_cb, |
| 206 | .ready = a2mp_chan_no_ready, |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn) |
| 210 | { |
| 211 | struct l2cap_chan *chan; |
| 212 | int err; |
| 213 | |
| 214 | chan = l2cap_chan_create(); |
| 215 | if (!chan) |
| 216 | return NULL; |
| 217 | |
| 218 | BT_DBG("chan %p", chan); |
| 219 | |
| 220 | hci_conn_hold(conn->hcon); |
| 221 | |
| 222 | chan->omtu = L2CAP_A2MP_DEFAULT_MTU; |
| 223 | chan->imtu = L2CAP_A2MP_DEFAULT_MTU; |
| 224 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 225 | |
| 226 | chan->ops = &a2mp_chan_ops; |
| 227 | |
| 228 | l2cap_chan_set_defaults(chan); |
| 229 | chan->remote_max_tx = chan->max_tx; |
| 230 | chan->remote_tx_win = chan->tx_win; |
| 231 | |
| 232 | chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; |
| 233 | chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; |
| 234 | |
| 235 | skb_queue_head_init(&chan->tx_q); |
| 236 | |
| 237 | chan->mode = L2CAP_MODE_ERTM; |
| 238 | |
| 239 | err = l2cap_ertm_init(chan); |
| 240 | if (err < 0) { |
| 241 | l2cap_chan_del(chan, 0); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | chan->conf_state = 0; |
| 246 | |
| 247 | l2cap_chan_add(conn, chan); |
| 248 | |
| 249 | chan->remote_mps = chan->omtu; |
| 250 | chan->mps = chan->omtu; |
| 251 | |
| 252 | chan->state = BT_CONNECTED; |
| 253 | |
| 254 | return chan; |
| 255 | } |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 256 | |
| 257 | /* AMP Manager functions */ |
| 258 | void amp_mgr_get(struct amp_mgr *mgr) |
| 259 | { |
| 260 | BT_DBG("mgr %p", mgr); |
| 261 | |
| 262 | kref_get(&mgr->kref); |
| 263 | } |
| 264 | |
| 265 | static void amp_mgr_destroy(struct kref *kref) |
| 266 | { |
| 267 | struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref); |
| 268 | |
| 269 | BT_DBG("mgr %p", mgr); |
| 270 | |
| 271 | kfree(mgr); |
| 272 | } |
| 273 | |
| 274 | int amp_mgr_put(struct amp_mgr *mgr) |
| 275 | { |
| 276 | BT_DBG("mgr %p", mgr); |
| 277 | |
| 278 | return kref_put(&mgr->kref, &_mgr_destroy); |
| 279 | } |
| 280 | |
| 281 | static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn) |
| 282 | { |
| 283 | struct amp_mgr *mgr; |
| 284 | struct l2cap_chan *chan; |
| 285 | |
| 286 | mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); |
| 287 | if (!mgr) |
| 288 | return NULL; |
| 289 | |
| 290 | BT_DBG("conn %p mgr %p", conn, mgr); |
| 291 | |
| 292 | mgr->l2cap_conn = conn; |
| 293 | |
| 294 | chan = a2mp_chan_open(conn); |
| 295 | if (!chan) { |
| 296 | kfree(mgr); |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | mgr->a2mp_chan = chan; |
| 301 | chan->data = mgr; |
| 302 | |
| 303 | conn->hcon->amp_mgr = mgr; |
| 304 | |
| 305 | kref_init(&mgr->kref); |
| 306 | |
| 307 | return mgr; |
| 308 | } |