Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 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 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the |
| 16 | * Free Software Foundation, Inc., |
| 17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #define pr_fmt(fmt) "llcp: %s: " fmt, __func__ |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/nfc.h> |
| 26 | |
| 27 | #include <net/nfc/nfc.h> |
| 28 | |
| 29 | #include "../nfc.h" |
| 30 | #include "llcp.h" |
| 31 | |
| 32 | static u8 llcp_tlv_length[LLCP_TLV_MAX] = { |
| 33 | 0, |
| 34 | 1, /* VERSION */ |
| 35 | 2, /* MIUX */ |
| 36 | 2, /* WKS */ |
| 37 | 1, /* LTO */ |
| 38 | 1, /* RW */ |
| 39 | 0, /* SN */ |
| 40 | 1, /* OPT */ |
| 41 | 0, /* SDREQ */ |
| 42 | 2, /* SDRES */ |
| 43 | |
| 44 | }; |
| 45 | |
| 46 | static u8 llcp_tlv8(u8 *tlv, u8 type) |
| 47 | { |
| 48 | if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]]) |
| 49 | return 0; |
| 50 | |
| 51 | return tlv[2]; |
| 52 | } |
| 53 | |
| 54 | static u8 llcp_tlv16(u8 *tlv, u8 type) |
| 55 | { |
| 56 | if (tlv[0] != type || tlv[1] != llcp_tlv_length[tlv[0]]) |
| 57 | return 0; |
| 58 | |
| 59 | return be16_to_cpu(*((__be16 *)(tlv + 2))); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static u8 llcp_tlv_version(u8 *tlv) |
| 64 | { |
| 65 | return llcp_tlv8(tlv, LLCP_TLV_VERSION); |
| 66 | } |
| 67 | |
| 68 | static u16 llcp_tlv_miux(u8 *tlv) |
| 69 | { |
| 70 | return llcp_tlv16(tlv, LLCP_TLV_MIUX) & 0x7f; |
| 71 | } |
| 72 | |
| 73 | static u16 llcp_tlv_wks(u8 *tlv) |
| 74 | { |
| 75 | return llcp_tlv16(tlv, LLCP_TLV_WKS); |
| 76 | } |
| 77 | |
| 78 | static u16 llcp_tlv_lto(u8 *tlv) |
| 79 | { |
| 80 | return llcp_tlv8(tlv, LLCP_TLV_LTO); |
| 81 | } |
| 82 | |
| 83 | static u8 llcp_tlv_opt(u8 *tlv) |
| 84 | { |
| 85 | return llcp_tlv8(tlv, LLCP_TLV_OPT); |
| 86 | } |
| 87 | |
| 88 | static u8 llcp_tlv_rw(u8 *tlv) |
| 89 | { |
| 90 | return llcp_tlv8(tlv, LLCP_TLV_RW) & 0xf; |
| 91 | } |
| 92 | |
| 93 | u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length) |
| 94 | { |
| 95 | u8 *tlv, length; |
| 96 | |
| 97 | pr_debug("type %d\n", type); |
| 98 | |
| 99 | if (type >= LLCP_TLV_MAX) |
| 100 | return NULL; |
| 101 | |
| 102 | length = llcp_tlv_length[type]; |
| 103 | if (length == 0 && value_length == 0) |
| 104 | return NULL; |
Samuel Ortiz | 324b0af | 2012-04-10 19:43:15 +0200 | [diff] [blame] | 105 | else if (length == 0) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 106 | length = value_length; |
| 107 | |
| 108 | *tlv_length = 2 + length; |
| 109 | tlv = kzalloc(2 + length, GFP_KERNEL); |
| 110 | if (tlv == NULL) |
| 111 | return tlv; |
| 112 | |
| 113 | tlv[0] = type; |
| 114 | tlv[1] = length; |
| 115 | memcpy(tlv + 2, value, length); |
| 116 | |
| 117 | return tlv; |
| 118 | } |
| 119 | |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 120 | int nfc_llcp_parse_gb_tlv(struct nfc_llcp_local *local, |
| 121 | u8 *tlv_array, u16 tlv_array_len) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 122 | { |
| 123 | u8 *tlv = tlv_array, type, length, offset = 0; |
| 124 | |
| 125 | pr_debug("TLV array length %d\n", tlv_array_len); |
| 126 | |
| 127 | if (local == NULL) |
| 128 | return -ENODEV; |
| 129 | |
| 130 | while (offset < tlv_array_len) { |
| 131 | type = tlv[0]; |
| 132 | length = tlv[1]; |
| 133 | |
| 134 | pr_debug("type 0x%x length %d\n", type, length); |
| 135 | |
| 136 | switch (type) { |
| 137 | case LLCP_TLV_VERSION: |
| 138 | local->remote_version = llcp_tlv_version(tlv); |
| 139 | break; |
| 140 | case LLCP_TLV_MIUX: |
| 141 | local->remote_miu = llcp_tlv_miux(tlv) + 128; |
| 142 | break; |
| 143 | case LLCP_TLV_WKS: |
| 144 | local->remote_wks = llcp_tlv_wks(tlv); |
| 145 | break; |
| 146 | case LLCP_TLV_LTO: |
| 147 | local->remote_lto = llcp_tlv_lto(tlv) * 10; |
| 148 | break; |
| 149 | case LLCP_TLV_OPT: |
| 150 | local->remote_opt = llcp_tlv_opt(tlv); |
| 151 | break; |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 152 | default: |
| 153 | pr_err("Invalid gt tlv value 0x%x\n", type); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | offset += length + 2; |
| 158 | tlv += length + 2; |
| 159 | } |
| 160 | |
| 161 | pr_debug("version 0x%x miu %d lto %d opt 0x%x wks 0x%x\n", |
| 162 | local->remote_version, local->remote_miu, |
| 163 | local->remote_lto, local->remote_opt, |
| 164 | local->remote_wks); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int nfc_llcp_parse_connection_tlv(struct nfc_llcp_sock *sock, |
| 170 | u8 *tlv_array, u16 tlv_array_len) |
| 171 | { |
| 172 | u8 *tlv = tlv_array, type, length, offset = 0; |
| 173 | |
| 174 | pr_debug("TLV array length %d\n", tlv_array_len); |
| 175 | |
| 176 | if (sock == NULL) |
| 177 | return -ENOTCONN; |
| 178 | |
| 179 | while (offset < tlv_array_len) { |
| 180 | type = tlv[0]; |
| 181 | length = tlv[1]; |
| 182 | |
| 183 | pr_debug("type 0x%x length %d\n", type, length); |
| 184 | |
| 185 | switch (type) { |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame^] | 186 | case LLCP_TLV_MIUX: |
| 187 | sock->miu = llcp_tlv_miux(tlv) + 128; |
| 188 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 189 | case LLCP_TLV_RW: |
Samuel Ortiz | 7a06e58 | 2012-05-07 22:03:34 +0200 | [diff] [blame] | 190 | sock->rw = llcp_tlv_rw(tlv); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 191 | break; |
Samuel Ortiz | 9dda50f | 2012-03-05 01:03:49 +0100 | [diff] [blame] | 192 | case LLCP_TLV_SN: |
| 193 | break; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 194 | default: |
| 195 | pr_err("Invalid gt tlv value 0x%x\n", type); |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | offset += length + 2; |
| 200 | tlv += length + 2; |
| 201 | } |
| 202 | |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame^] | 203 | pr_debug("sock %p rw %d miu %d\n", sock, sock->rw, sock->miu); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static struct sk_buff *llcp_add_header(struct sk_buff *pdu, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 209 | u8 dsap, u8 ssap, u8 ptype) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 210 | { |
| 211 | u8 header[2]; |
| 212 | |
| 213 | pr_debug("ptype 0x%x dsap 0x%x ssap 0x%x\n", ptype, dsap, ssap); |
| 214 | |
| 215 | header[0] = (u8)((dsap << 2) | (ptype >> 2)); |
| 216 | header[1] = (u8)((ptype << 6) | ssap); |
| 217 | |
| 218 | pr_debug("header 0x%x 0x%x\n", header[0], header[1]); |
| 219 | |
| 220 | memcpy(skb_put(pdu, LLCP_HEADER_SIZE), header, LLCP_HEADER_SIZE); |
| 221 | |
| 222 | return pdu; |
| 223 | } |
| 224 | |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 225 | static struct sk_buff *llcp_add_tlv(struct sk_buff *pdu, u8 *tlv, |
| 226 | u8 tlv_length) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 227 | { |
| 228 | /* XXX Add an skb length check */ |
| 229 | |
| 230 | if (tlv == NULL) |
| 231 | return NULL; |
| 232 | |
| 233 | memcpy(skb_put(pdu, tlv_length), tlv, tlv_length); |
| 234 | |
| 235 | return pdu; |
| 236 | } |
| 237 | |
| 238 | static struct sk_buff *llcp_allocate_pdu(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 239 | u8 cmd, u16 size) |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 240 | { |
| 241 | struct sk_buff *skb; |
| 242 | int err; |
| 243 | |
| 244 | if (sock->ssap == 0) |
| 245 | return NULL; |
| 246 | |
| 247 | skb = nfc_alloc_send_skb(sock->dev, &sock->sk, MSG_DONTWAIT, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 248 | size + LLCP_HEADER_SIZE, &err); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 249 | if (skb == NULL) { |
| 250 | pr_err("Could not allocate PDU\n"); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | skb = llcp_add_header(skb, sock->dsap, sock->ssap, cmd); |
| 255 | |
| 256 | return skb; |
| 257 | } |
| 258 | |
| 259 | int nfc_llcp_disconnect(struct nfc_llcp_sock *sock) |
| 260 | { |
| 261 | struct sk_buff *skb; |
| 262 | struct nfc_dev *dev; |
| 263 | struct nfc_llcp_local *local; |
| 264 | u16 size = 0; |
| 265 | |
| 266 | pr_debug("Sending DISC\n"); |
| 267 | |
| 268 | local = sock->local; |
| 269 | if (local == NULL) |
| 270 | return -ENODEV; |
| 271 | |
| 272 | dev = sock->dev; |
| 273 | if (dev == NULL) |
| 274 | return -ENODEV; |
| 275 | |
| 276 | size += LLCP_HEADER_SIZE; |
| 277 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 278 | |
| 279 | skb = alloc_skb(size, GFP_ATOMIC); |
| 280 | if (skb == NULL) |
| 281 | return -ENOMEM; |
| 282 | |
| 283 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 284 | |
Samuel Ortiz | ffc2931 | 2012-04-10 19:43:16 +0200 | [diff] [blame] | 285 | skb = llcp_add_header(skb, sock->dsap, sock->ssap, LLCP_PDU_DISC); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 286 | |
| 287 | skb_queue_tail(&local->tx_queue, skb); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int nfc_llcp_send_symm(struct nfc_dev *dev) |
| 293 | { |
| 294 | struct sk_buff *skb; |
| 295 | struct nfc_llcp_local *local; |
| 296 | u16 size = 0; |
| 297 | |
| 298 | pr_debug("Sending SYMM\n"); |
| 299 | |
| 300 | local = nfc_llcp_find_local(dev); |
| 301 | if (local == NULL) |
| 302 | return -ENODEV; |
| 303 | |
| 304 | size += LLCP_HEADER_SIZE; |
| 305 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 306 | |
| 307 | skb = alloc_skb(size, GFP_KERNEL); |
| 308 | if (skb == NULL) |
| 309 | return -ENOMEM; |
| 310 | |
| 311 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 312 | |
| 313 | skb = llcp_add_header(skb, 0, 0, LLCP_PDU_SYMM); |
| 314 | |
| 315 | return nfc_data_exchange(dev, local->target_idx, skb, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 316 | nfc_llcp_recv, local); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | int nfc_llcp_send_connect(struct nfc_llcp_sock *sock) |
| 320 | { |
| 321 | struct nfc_llcp_local *local; |
| 322 | struct sk_buff *skb; |
| 323 | u8 *service_name_tlv = NULL, service_name_tlv_length; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 324 | u8 *miux_tlv = NULL, miux_tlv_length; |
| 325 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
| 326 | __be16 miux; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 327 | int err; |
| 328 | u16 size = 0; |
| 329 | |
| 330 | pr_debug("Sending CONNECT\n"); |
| 331 | |
| 332 | local = sock->local; |
| 333 | if (local == NULL) |
| 334 | return -ENODEV; |
| 335 | |
| 336 | if (sock->service_name != NULL) { |
| 337 | service_name_tlv = nfc_llcp_build_tlv(LLCP_TLV_SN, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 338 | sock->service_name, |
| 339 | sock->service_name_len, |
| 340 | &service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 341 | size += service_name_tlv_length; |
| 342 | } |
| 343 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 344 | miux = cpu_to_be16(LLCP_MAX_MIUX); |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 345 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
| 346 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 347 | size += miux_tlv_length; |
| 348 | |
| 349 | rw = LLCP_MAX_RW; |
| 350 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length); |
| 351 | size += rw_tlv_length; |
| 352 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 353 | pr_debug("SKB size %d SN length %zu\n", size, sock->service_name_len); |
| 354 | |
| 355 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CONNECT, size); |
| 356 | if (skb == NULL) { |
| 357 | err = -ENOMEM; |
| 358 | goto error_tlv; |
| 359 | } |
| 360 | |
| 361 | if (service_name_tlv != NULL) |
| 362 | skb = llcp_add_tlv(skb, service_name_tlv, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 363 | service_name_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 364 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 365 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 366 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
| 367 | |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 368 | skb_queue_tail(&local->tx_queue, skb); |
| 369 | |
| 370 | return 0; |
| 371 | |
| 372 | error_tlv: |
| 373 | pr_err("error %d\n", err); |
| 374 | |
| 375 | kfree(service_name_tlv); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 376 | kfree(miux_tlv); |
| 377 | kfree(rw_tlv); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 378 | |
| 379 | return err; |
| 380 | } |
| 381 | |
| 382 | int nfc_llcp_send_cc(struct nfc_llcp_sock *sock) |
| 383 | { |
| 384 | struct nfc_llcp_local *local; |
| 385 | struct sk_buff *skb; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 386 | u8 *miux_tlv = NULL, miux_tlv_length; |
| 387 | u8 *rw_tlv = NULL, rw_tlv_length, rw; |
| 388 | __be16 miux; |
| 389 | int err; |
| 390 | u16 size = 0; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 391 | |
| 392 | pr_debug("Sending CC\n"); |
| 393 | |
| 394 | local = sock->local; |
| 395 | if (local == NULL) |
| 396 | return -ENODEV; |
| 397 | |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 398 | miux = cpu_to_be16(LLCP_MAX_MIUX); |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 399 | miux_tlv = nfc_llcp_build_tlv(LLCP_TLV_MIUX, (u8 *)&miux, 0, |
| 400 | &miux_tlv_length); |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 401 | size += miux_tlv_length; |
| 402 | |
| 403 | rw = LLCP_MAX_RW; |
| 404 | rw_tlv = nfc_llcp_build_tlv(LLCP_TLV_RW, &rw, 0, &rw_tlv_length); |
| 405 | size += rw_tlv_length; |
| 406 | |
| 407 | skb = llcp_allocate_pdu(sock, LLCP_PDU_CC, size); |
| 408 | if (skb == NULL) { |
| 409 | err = -ENOMEM; |
| 410 | goto error_tlv; |
| 411 | } |
| 412 | |
| 413 | skb = llcp_add_tlv(skb, miux_tlv, miux_tlv_length); |
| 414 | skb = llcp_add_tlv(skb, rw_tlv, rw_tlv_length); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 415 | |
| 416 | skb_queue_tail(&local->tx_queue, skb); |
| 417 | |
| 418 | return 0; |
Samuel Ortiz | eda21f1 | 2012-03-05 01:03:43 +0100 | [diff] [blame] | 419 | |
| 420 | error_tlv: |
| 421 | pr_err("error %d\n", err); |
| 422 | |
| 423 | kfree(miux_tlv); |
| 424 | kfree(rw_tlv); |
| 425 | |
| 426 | return err; |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason) |
| 430 | { |
| 431 | struct sk_buff *skb; |
| 432 | struct nfc_dev *dev; |
| 433 | u16 size = 1; /* Reason code */ |
| 434 | |
| 435 | pr_debug("Sending DM reason 0x%x\n", reason); |
| 436 | |
| 437 | if (local == NULL) |
| 438 | return -ENODEV; |
| 439 | |
| 440 | dev = local->dev; |
| 441 | if (dev == NULL) |
| 442 | return -ENODEV; |
| 443 | |
| 444 | size += LLCP_HEADER_SIZE; |
| 445 | size += dev->tx_headroom + dev->tx_tailroom + NFC_HEADER_SIZE; |
| 446 | |
| 447 | skb = alloc_skb(size, GFP_KERNEL); |
| 448 | if (skb == NULL) |
| 449 | return -ENOMEM; |
| 450 | |
| 451 | skb_reserve(skb, dev->tx_headroom + NFC_HEADER_SIZE); |
| 452 | |
Samuel Ortiz | ffc2931 | 2012-04-10 19:43:16 +0200 | [diff] [blame] | 453 | skb = llcp_add_header(skb, dsap, ssap, LLCP_PDU_DM); |
Samuel Ortiz | d646960 | 2011-12-14 16:43:12 +0100 | [diff] [blame] | 454 | |
| 455 | memcpy(skb_put(skb, 1), &reason, 1); |
| 456 | |
| 457 | skb_queue_head(&local->tx_queue, skb); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock) |
| 463 | { |
| 464 | struct sk_buff *skb; |
| 465 | struct nfc_llcp_local *local; |
| 466 | |
| 467 | pr_debug("Send DISC\n"); |
| 468 | |
| 469 | local = sock->local; |
| 470 | if (local == NULL) |
| 471 | return -ENODEV; |
| 472 | |
| 473 | skb = llcp_allocate_pdu(sock, LLCP_PDU_DISC, 0); |
| 474 | if (skb == NULL) |
| 475 | return -ENOMEM; |
| 476 | |
| 477 | skb_queue_head(&local->tx_queue, skb); |
| 478 | |
| 479 | return 0; |
| 480 | } |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 481 | |
| 482 | int nfc_llcp_send_i_frame(struct nfc_llcp_sock *sock, |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 483 | struct msghdr *msg, size_t len) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 484 | { |
| 485 | struct sk_buff *pdu; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 486 | struct sock *sk = &sock->sk; |
| 487 | struct nfc_llcp_local *local; |
| 488 | size_t frag_len = 0, remaining_len; |
| 489 | u8 *msg_data, *msg_ptr; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 490 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 491 | pr_debug("Send I frame len %zd\n", len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 492 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 493 | local = sock->local; |
| 494 | if (local == NULL) |
| 495 | return -ENODEV; |
| 496 | |
| 497 | msg_data = kzalloc(len, GFP_KERNEL); |
| 498 | if (msg_data == NULL) |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 499 | return -ENOMEM; |
| 500 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 501 | if (memcpy_fromiovec(msg_data, msg->msg_iov, len)) { |
Samuel Ortiz | 427a2eb | 2012-03-05 01:03:52 +0100 | [diff] [blame] | 502 | kfree(msg_data); |
| 503 | return -EFAULT; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 506 | remaining_len = len; |
| 507 | msg_ptr = msg_data; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 508 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 509 | while (remaining_len > 0) { |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 510 | |
Samuel Ortiz | 93d7e49 | 2012-05-14 17:37:32 +0200 | [diff] [blame^] | 511 | frag_len = min_t(size_t, sock->miu, remaining_len); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 512 | |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 513 | pr_debug("Fragment %zd bytes remaining %zd", |
| 514 | frag_len, remaining_len); |
| 515 | |
| 516 | pdu = llcp_allocate_pdu(sock, LLCP_PDU_I, |
| 517 | frag_len + LLCP_SEQUENCE_SIZE); |
| 518 | if (pdu == NULL) |
| 519 | return -ENOMEM; |
| 520 | |
| 521 | skb_put(pdu, LLCP_SEQUENCE_SIZE); |
| 522 | |
| 523 | memcpy(skb_put(pdu, frag_len), msg_ptr, frag_len); |
| 524 | |
Samuel Ortiz | bdbc59b | 2012-05-10 19:45:52 +0200 | [diff] [blame] | 525 | skb_queue_tail(&sock->tx_queue, pdu); |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 526 | |
| 527 | lock_sock(sk); |
| 528 | |
| 529 | nfc_llcp_queue_i_frames(sock); |
| 530 | |
| 531 | release_sock(sk); |
| 532 | |
| 533 | remaining_len -= frag_len; |
Samuel Ortiz | b4838d1 | 2012-04-10 19:43:03 +0200 | [diff] [blame] | 534 | msg_ptr += frag_len; |
Samuel Ortiz | e65b0f4 | 2012-03-05 01:03:44 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | kfree(msg_data); |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 538 | |
Samuel Ortiz | 43472ff | 2012-05-07 12:31:21 +0200 | [diff] [blame] | 539 | return len; |
Samuel Ortiz | 53a0ac2 | 2012-03-05 01:03:37 +0100 | [diff] [blame] | 540 | } |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 541 | |
| 542 | int nfc_llcp_send_rr(struct nfc_llcp_sock *sock) |
| 543 | { |
| 544 | struct sk_buff *skb; |
| 545 | struct nfc_llcp_local *local; |
| 546 | |
| 547 | pr_debug("Send rr nr %d\n", sock->recv_n); |
| 548 | |
| 549 | local = sock->local; |
| 550 | if (local == NULL) |
| 551 | return -ENODEV; |
| 552 | |
| 553 | skb = llcp_allocate_pdu(sock, LLCP_PDU_RR, LLCP_SEQUENCE_SIZE); |
| 554 | if (skb == NULL) |
| 555 | return -ENOMEM; |
| 556 | |
| 557 | skb_put(skb, LLCP_SEQUENCE_SIZE); |
| 558 | |
Samuel Ortiz | 279cf17 | 2012-04-10 19:43:14 +0200 | [diff] [blame] | 559 | skb->data[2] = sock->recv_n; |
Samuel Ortiz | d094afa | 2012-03-05 01:03:42 +0100 | [diff] [blame] | 560 | |
| 561 | skb_queue_head(&local->tx_queue, skb); |
| 562 | |
| 563 | return 0; |
| 564 | } |