Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Instituto Nokia de Tecnologia |
| 3 | * |
| 4 | * Authors: |
| 5 | * Lauro Ramos Venancio <lauro.venancio@openbossa.org> |
| 6 | * Aloisio Almeida Jr <aloisio.almeida@openbossa.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the |
| 20 | * Free Software Foundation, Inc., |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | */ |
| 23 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 24 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 25 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 26 | #include <net/genetlink.h> |
| 27 | #include <linux/nfc.h> |
| 28 | #include <linux/slab.h> |
| 29 | |
| 30 | #include "nfc.h" |
| 31 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 32 | #include "llcp/llcp.h" |
| 33 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 34 | static struct genl_multicast_group nfc_genl_event_mcgrp = { |
| 35 | .name = NFC_GENL_MCAST_EVENT_NAME, |
| 36 | }; |
| 37 | |
H Hartley Sweeten | e5fe4cf | 2012-05-07 12:31:28 +0200 | [diff] [blame] | 38 | static struct genl_family nfc_genl_family = { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 39 | .id = GENL_ID_GENERATE, |
| 40 | .hdrsize = 0, |
| 41 | .name = NFC_GENL_NAME, |
| 42 | .version = NFC_GENL_VERSION, |
| 43 | .maxattr = NFC_ATTR_MAX, |
| 44 | }; |
| 45 | |
| 46 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { |
| 47 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, |
| 48 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, |
| 49 | .len = NFC_DEVICE_NAME_MAXSIZE }, |
| 50 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 51 | [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, |
| 52 | [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, |
Samuel Ortiz | c970a1a | 2012-03-05 01:03:34 +0100 | [diff] [blame] | 53 | [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 54 | [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, |
| 55 | [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, |
Thierry Escande | 8af362d | 2013-02-15 10:42:52 +0100 | [diff] [blame] | 56 | [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 }, |
| 57 | [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, |
| 58 | [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 59 | [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, |
| 60 | }; |
| 61 | |
| 62 | static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { |
| 63 | [NFC_SDP_ATTR_URI] = { .type = NLA_STRING }, |
| 64 | [NFC_SDP_ATTR_SAP] = { .type = NLA_U8 }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 68 | struct netlink_callback *cb, int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 69 | { |
| 70 | void *hdr; |
| 71 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 72 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 73 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 74 | if (!hdr) |
| 75 | return -EMSGSIZE; |
| 76 | |
| 77 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 78 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 79 | if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || |
| 80 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || |
| 81 | nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || |
| 82 | nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) |
| 83 | goto nla_put_failure; |
| 84 | if (target->nfcid1_len > 0 && |
| 85 | nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, |
| 86 | target->nfcid1)) |
| 87 | goto nla_put_failure; |
| 88 | if (target->sensb_res_len > 0 && |
| 89 | nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, |
| 90 | target->sensb_res)) |
| 91 | goto nla_put_failure; |
| 92 | if (target->sensf_res_len > 0 && |
| 93 | nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, |
| 94 | target->sensf_res)) |
| 95 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 96 | |
| 97 | return genlmsg_end(msg, hdr); |
| 98 | |
| 99 | nla_put_failure: |
| 100 | genlmsg_cancel(msg, hdr); |
| 101 | return -EMSGSIZE; |
| 102 | } |
| 103 | |
| 104 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) |
| 105 | { |
| 106 | struct nfc_dev *dev; |
| 107 | int rc; |
| 108 | u32 idx; |
| 109 | |
| 110 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 111 | nfc_genl_family.attrbuf, |
| 112 | nfc_genl_family.maxattr, |
| 113 | nfc_genl_policy); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 114 | if (rc < 0) |
| 115 | return ERR_PTR(rc); |
| 116 | |
| 117 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) |
| 118 | return ERR_PTR(-EINVAL); |
| 119 | |
| 120 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); |
| 121 | |
| 122 | dev = nfc_get_device(idx); |
| 123 | if (!dev) |
| 124 | return ERR_PTR(-ENODEV); |
| 125 | |
| 126 | return dev; |
| 127 | } |
| 128 | |
| 129 | static int nfc_genl_dump_targets(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 130 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 131 | { |
| 132 | int i = cb->args[0]; |
| 133 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 134 | int rc; |
| 135 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 136 | if (!dev) { |
| 137 | dev = __get_device_from_cb(cb); |
| 138 | if (IS_ERR(dev)) |
| 139 | return PTR_ERR(dev); |
| 140 | |
| 141 | cb->args[1] = (long) dev; |
| 142 | } |
| 143 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 144 | device_lock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 145 | |
| 146 | cb->seq = dev->targets_generation; |
| 147 | |
| 148 | while (i < dev->n_targets) { |
| 149 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 150 | NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 151 | if (rc < 0) |
| 152 | break; |
| 153 | |
| 154 | i++; |
| 155 | } |
| 156 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 157 | device_unlock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 158 | |
| 159 | cb->args[0] = i; |
| 160 | |
| 161 | return skb->len; |
| 162 | } |
| 163 | |
| 164 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) |
| 165 | { |
| 166 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 167 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 168 | if (dev) |
| 169 | nfc_put_device(dev); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int nfc_genl_targets_found(struct nfc_dev *dev) |
| 175 | { |
| 176 | struct sk_buff *msg; |
| 177 | void *hdr; |
| 178 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 179 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 180 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 181 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 182 | if (!msg) |
| 183 | return -ENOMEM; |
| 184 | |
| 185 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 186 | NFC_EVENT_TARGETS_FOUND); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 187 | if (!hdr) |
| 188 | goto free_msg; |
| 189 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 190 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 191 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 192 | |
| 193 | genlmsg_end(msg, hdr); |
| 194 | |
| 195 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 196 | |
| 197 | nla_put_failure: |
| 198 | genlmsg_cancel(msg, hdr); |
| 199 | free_msg: |
| 200 | nlmsg_free(msg); |
| 201 | return -EMSGSIZE; |
| 202 | } |
| 203 | |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 204 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 205 | { |
| 206 | struct sk_buff *msg; |
| 207 | void *hdr; |
| 208 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 209 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 210 | if (!msg) |
| 211 | return -ENOMEM; |
| 212 | |
| 213 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 214 | NFC_EVENT_TARGET_LOST); |
| 215 | if (!hdr) |
| 216 | goto free_msg; |
| 217 | |
John W. Linville | 59ef43e | 2012-04-18 14:17:13 -0400 | [diff] [blame] | 218 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 219 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 220 | goto nla_put_failure; |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 221 | |
| 222 | genlmsg_end(msg, hdr); |
| 223 | |
| 224 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 225 | |
| 226 | return 0; |
| 227 | |
| 228 | nla_put_failure: |
| 229 | genlmsg_cancel(msg, hdr); |
| 230 | free_msg: |
| 231 | nlmsg_free(msg); |
| 232 | return -EMSGSIZE; |
| 233 | } |
| 234 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 235 | int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) |
| 236 | { |
| 237 | struct sk_buff *msg; |
| 238 | void *hdr; |
| 239 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 240 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 241 | if (!msg) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 245 | NFC_EVENT_TM_ACTIVATED); |
| 246 | if (!hdr) |
| 247 | goto free_msg; |
| 248 | |
| 249 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 250 | goto nla_put_failure; |
| 251 | if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) |
| 252 | goto nla_put_failure; |
| 253 | |
| 254 | genlmsg_end(msg, hdr); |
| 255 | |
| 256 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 257 | |
| 258 | return 0; |
| 259 | |
| 260 | nla_put_failure: |
| 261 | genlmsg_cancel(msg, hdr); |
| 262 | free_msg: |
| 263 | nlmsg_free(msg); |
| 264 | return -EMSGSIZE; |
| 265 | } |
| 266 | |
| 267 | int nfc_genl_tm_deactivated(struct nfc_dev *dev) |
| 268 | { |
| 269 | struct sk_buff *msg; |
| 270 | void *hdr; |
| 271 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 272 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 273 | if (!msg) |
| 274 | return -ENOMEM; |
| 275 | |
| 276 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 277 | NFC_EVENT_TM_DEACTIVATED); |
| 278 | if (!hdr) |
| 279 | goto free_msg; |
| 280 | |
| 281 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 282 | goto nla_put_failure; |
| 283 | |
| 284 | genlmsg_end(msg, hdr); |
| 285 | |
| 286 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 287 | |
| 288 | return 0; |
| 289 | |
| 290 | nla_put_failure: |
| 291 | genlmsg_cancel(msg, hdr); |
| 292 | free_msg: |
| 293 | nlmsg_free(msg); |
| 294 | return -EMSGSIZE; |
| 295 | } |
| 296 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 297 | int nfc_genl_device_added(struct nfc_dev *dev) |
| 298 | { |
| 299 | struct sk_buff *msg; |
| 300 | void *hdr; |
| 301 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 302 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 303 | if (!msg) |
| 304 | return -ENOMEM; |
| 305 | |
| 306 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 307 | NFC_EVENT_DEVICE_ADDED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 308 | if (!hdr) |
| 309 | goto free_msg; |
| 310 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 311 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 312 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 313 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
| 314 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) |
| 315 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 316 | |
| 317 | genlmsg_end(msg, hdr); |
| 318 | |
| 319 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 320 | |
| 321 | return 0; |
| 322 | |
| 323 | nla_put_failure: |
| 324 | genlmsg_cancel(msg, hdr); |
| 325 | free_msg: |
| 326 | nlmsg_free(msg); |
| 327 | return -EMSGSIZE; |
| 328 | } |
| 329 | |
| 330 | int nfc_genl_device_removed(struct nfc_dev *dev) |
| 331 | { |
| 332 | struct sk_buff *msg; |
| 333 | void *hdr; |
| 334 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 335 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 336 | if (!msg) |
| 337 | return -ENOMEM; |
| 338 | |
| 339 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 340 | NFC_EVENT_DEVICE_REMOVED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 341 | if (!hdr) |
| 342 | goto free_msg; |
| 343 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 344 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 345 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 346 | |
| 347 | genlmsg_end(msg, hdr); |
| 348 | |
| 349 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 350 | |
| 351 | return 0; |
| 352 | |
| 353 | nla_put_failure: |
| 354 | genlmsg_cancel(msg, hdr); |
| 355 | free_msg: |
| 356 | nlmsg_free(msg); |
| 357 | return -EMSGSIZE; |
| 358 | } |
| 359 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 360 | int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list) |
| 361 | { |
| 362 | struct sk_buff *msg; |
| 363 | struct nlattr *sdp_attr, *uri_attr; |
| 364 | struct nfc_llcp_sdp_tlv *sdres; |
| 365 | struct hlist_node *n; |
| 366 | void *hdr; |
| 367 | int rc = -EMSGSIZE; |
| 368 | int i; |
| 369 | |
| 370 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 371 | if (!msg) |
| 372 | return -ENOMEM; |
| 373 | |
| 374 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 375 | NFC_EVENT_LLC_SDRES); |
| 376 | if (!hdr) |
| 377 | goto free_msg; |
| 378 | |
| 379 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 380 | goto nla_put_failure; |
| 381 | |
| 382 | sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP); |
| 383 | if (sdp_attr == NULL) { |
| 384 | rc = -ENOMEM; |
| 385 | goto nla_put_failure; |
| 386 | } |
| 387 | |
| 388 | i = 1; |
| 389 | hlist_for_each_entry_safe(sdres, n, sdres_list, node) { |
| 390 | pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap); |
| 391 | |
| 392 | uri_attr = nla_nest_start(msg, i++); |
| 393 | if (uri_attr == NULL) { |
| 394 | rc = -ENOMEM; |
| 395 | goto nla_put_failure; |
| 396 | } |
| 397 | |
| 398 | if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap)) |
| 399 | goto nla_put_failure; |
| 400 | |
| 401 | if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri)) |
| 402 | goto nla_put_failure; |
| 403 | |
| 404 | nla_nest_end(msg, uri_attr); |
| 405 | |
| 406 | hlist_del(&sdres->node); |
| 407 | |
| 408 | nfc_llcp_free_sdp_tlv(sdres); |
| 409 | } |
| 410 | |
| 411 | nla_nest_end(msg, sdp_attr); |
| 412 | |
| 413 | genlmsg_end(msg, hdr); |
| 414 | |
| 415 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 416 | |
| 417 | nla_put_failure: |
| 418 | genlmsg_cancel(msg, hdr); |
| 419 | |
| 420 | free_msg: |
| 421 | nlmsg_free(msg); |
| 422 | |
| 423 | nfc_llcp_free_sdp_tlv_list(sdres_list); |
| 424 | |
| 425 | return rc; |
| 426 | } |
| 427 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 428 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 429 | u32 portid, u32 seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 430 | struct netlink_callback *cb, |
| 431 | int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 432 | { |
| 433 | void *hdr; |
| 434 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 435 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 436 | NFC_CMD_GET_DEVICE); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 437 | if (!hdr) |
| 438 | return -EMSGSIZE; |
| 439 | |
| 440 | if (cb) |
| 441 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 442 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 443 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 444 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 445 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
Samuel Ortiz | 390a1bd | 2012-12-19 19:11:32 +0100 | [diff] [blame] | 446 | nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) || |
Thierry Escande | 7ad3939 | 2012-10-05 11:19:58 +0200 | [diff] [blame] | 447 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || |
| 448 | nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 449 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 450 | |
| 451 | return genlmsg_end(msg, hdr); |
| 452 | |
| 453 | nla_put_failure: |
| 454 | genlmsg_cancel(msg, hdr); |
| 455 | return -EMSGSIZE; |
| 456 | } |
| 457 | |
| 458 | static int nfc_genl_dump_devices(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 459 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 460 | { |
| 461 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 462 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 463 | bool first_call = false; |
| 464 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 465 | if (!iter) { |
| 466 | first_call = true; |
| 467 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 468 | if (!iter) |
| 469 | return -ENOMEM; |
| 470 | cb->args[0] = (long) iter; |
| 471 | } |
| 472 | |
| 473 | mutex_lock(&nfc_devlist_mutex); |
| 474 | |
| 475 | cb->seq = nfc_devlist_generation; |
| 476 | |
| 477 | if (first_call) { |
| 478 | nfc_device_iter_init(iter); |
| 479 | dev = nfc_device_iter_next(iter); |
| 480 | } |
| 481 | |
| 482 | while (dev) { |
| 483 | int rc; |
| 484 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 485 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 486 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 487 | if (rc < 0) |
| 488 | break; |
| 489 | |
| 490 | dev = nfc_device_iter_next(iter); |
| 491 | } |
| 492 | |
| 493 | mutex_unlock(&nfc_devlist_mutex); |
| 494 | |
| 495 | cb->args[1] = (long) dev; |
| 496 | |
| 497 | return skb->len; |
| 498 | } |
| 499 | |
| 500 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) |
| 501 | { |
| 502 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 503 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 504 | nfc_device_iter_exit(iter); |
| 505 | kfree(iter); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 510 | int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 511 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 512 | { |
| 513 | struct sk_buff *msg; |
| 514 | void *hdr; |
| 515 | |
| 516 | pr_debug("DEP link is up\n"); |
| 517 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 518 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 519 | if (!msg) |
| 520 | return -ENOMEM; |
| 521 | |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 522 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 523 | if (!hdr) |
| 524 | goto free_msg; |
| 525 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 526 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 527 | goto nla_put_failure; |
| 528 | if (rf_mode == NFC_RF_INITIATOR && |
| 529 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 530 | goto nla_put_failure; |
| 531 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || |
| 532 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) |
| 533 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 534 | |
| 535 | genlmsg_end(msg, hdr); |
| 536 | |
| 537 | dev->dep_link_up = true; |
| 538 | |
| 539 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 540 | |
| 541 | return 0; |
| 542 | |
| 543 | nla_put_failure: |
| 544 | genlmsg_cancel(msg, hdr); |
| 545 | free_msg: |
| 546 | nlmsg_free(msg); |
| 547 | return -EMSGSIZE; |
| 548 | } |
| 549 | |
| 550 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) |
| 551 | { |
| 552 | struct sk_buff *msg; |
| 553 | void *hdr; |
| 554 | |
| 555 | pr_debug("DEP link is down\n"); |
| 556 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 557 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 558 | if (!msg) |
| 559 | return -ENOMEM; |
| 560 | |
| 561 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 562 | NFC_CMD_DEP_LINK_DOWN); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 563 | if (!hdr) |
| 564 | goto free_msg; |
| 565 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 566 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 567 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 568 | |
| 569 | genlmsg_end(msg, hdr); |
| 570 | |
| 571 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 572 | |
| 573 | return 0; |
| 574 | |
| 575 | nla_put_failure: |
| 576 | genlmsg_cancel(msg, hdr); |
| 577 | free_msg: |
| 578 | nlmsg_free(msg); |
| 579 | return -EMSGSIZE; |
| 580 | } |
| 581 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 582 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
| 583 | { |
| 584 | struct sk_buff *msg; |
| 585 | struct nfc_dev *dev; |
| 586 | u32 idx; |
| 587 | int rc = -ENOBUFS; |
| 588 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 589 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 593 | |
| 594 | dev = nfc_get_device(idx); |
| 595 | if (!dev) |
| 596 | return -ENODEV; |
| 597 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 598 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 599 | if (!msg) { |
| 600 | rc = -ENOMEM; |
| 601 | goto out_putdev; |
| 602 | } |
| 603 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 604 | rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 605 | NULL, 0); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 606 | if (rc < 0) |
| 607 | goto out_free; |
| 608 | |
| 609 | nfc_put_device(dev); |
| 610 | |
| 611 | return genlmsg_reply(msg, info); |
| 612 | |
| 613 | out_free: |
| 614 | nlmsg_free(msg); |
| 615 | out_putdev: |
| 616 | nfc_put_device(dev); |
| 617 | return rc; |
| 618 | } |
| 619 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 620 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
| 621 | { |
| 622 | struct nfc_dev *dev; |
| 623 | int rc; |
| 624 | u32 idx; |
| 625 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 626 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 627 | return -EINVAL; |
| 628 | |
| 629 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 630 | |
| 631 | dev = nfc_get_device(idx); |
| 632 | if (!dev) |
| 633 | return -ENODEV; |
| 634 | |
| 635 | rc = nfc_dev_up(dev); |
| 636 | |
| 637 | nfc_put_device(dev); |
| 638 | return rc; |
| 639 | } |
| 640 | |
| 641 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) |
| 642 | { |
| 643 | struct nfc_dev *dev; |
| 644 | int rc; |
| 645 | u32 idx; |
| 646 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 647 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 648 | return -EINVAL; |
| 649 | |
| 650 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 651 | |
| 652 | dev = nfc_get_device(idx); |
| 653 | if (!dev) |
| 654 | return -ENODEV; |
| 655 | |
| 656 | rc = nfc_dev_down(dev); |
| 657 | |
| 658 | nfc_put_device(dev); |
| 659 | return rc; |
| 660 | } |
| 661 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 662 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
| 663 | { |
| 664 | struct nfc_dev *dev; |
| 665 | int rc; |
| 666 | u32 idx; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 667 | u32 im_protocols = 0, tm_protocols = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 668 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 669 | pr_debug("Poll start\n"); |
| 670 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 671 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 672 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
| 673 | !info->attrs[NFC_ATTR_PROTOCOLS]) && |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 674 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 675 | return -EINVAL; |
| 676 | |
| 677 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 678 | |
| 679 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) |
| 680 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 681 | |
| 682 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) |
| 683 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); |
Samuel Ortiz | 5e50ee3 | 2012-05-31 11:48:58 +0200 | [diff] [blame] | 684 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
| 685 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 686 | |
| 687 | dev = nfc_get_device(idx); |
| 688 | if (!dev) |
| 689 | return -ENODEV; |
| 690 | |
| 691 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 692 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 693 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 694 | if (!rc) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 695 | dev->genl_data.poll_req_portid = info->snd_portid; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 696 | |
| 697 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 698 | |
| 699 | nfc_put_device(dev); |
| 700 | return rc; |
| 701 | } |
| 702 | |
| 703 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) |
| 704 | { |
| 705 | struct nfc_dev *dev; |
| 706 | int rc; |
| 707 | u32 idx; |
| 708 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 709 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 710 | return -EINVAL; |
| 711 | |
| 712 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 713 | |
| 714 | dev = nfc_get_device(idx); |
| 715 | if (!dev) |
| 716 | return -ENODEV; |
| 717 | |
Samuel Ortiz | a831b91 | 2012-06-28 16:41:57 +0200 | [diff] [blame] | 718 | device_lock(&dev->dev); |
| 719 | |
| 720 | if (!dev->polling) { |
| 721 | device_unlock(&dev->dev); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | device_unlock(&dev->dev); |
| 726 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 727 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 728 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 729 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 730 | rc = -EBUSY; |
| 731 | goto out; |
| 732 | } |
| 733 | |
| 734 | rc = nfc_stop_poll(dev); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 735 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 736 | |
| 737 | out: |
| 738 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 739 | nfc_put_device(dev); |
| 740 | return rc; |
| 741 | } |
| 742 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 743 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
| 744 | { |
| 745 | struct nfc_dev *dev; |
| 746 | int rc, tgt_idx; |
| 747 | u32 idx; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 748 | u8 comm; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 749 | |
| 750 | pr_debug("DEP link up\n"); |
| 751 | |
| 752 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 753 | !info->attrs[NFC_ATTR_COMM_MODE]) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 754 | return -EINVAL; |
| 755 | |
| 756 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 757 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) |
| 758 | tgt_idx = NFC_TARGET_IDX_ANY; |
| 759 | else |
| 760 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 761 | |
| 762 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 763 | |
| 764 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) |
| 765 | return -EINVAL; |
| 766 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 767 | dev = nfc_get_device(idx); |
| 768 | if (!dev) |
| 769 | return -ENODEV; |
| 770 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 771 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 772 | |
| 773 | nfc_put_device(dev); |
| 774 | |
| 775 | return rc; |
| 776 | } |
| 777 | |
| 778 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) |
| 779 | { |
| 780 | struct nfc_dev *dev; |
| 781 | int rc; |
| 782 | u32 idx; |
| 783 | |
| 784 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 785 | return -EINVAL; |
| 786 | |
| 787 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 788 | |
| 789 | dev = nfc_get_device(idx); |
| 790 | if (!dev) |
| 791 | return -ENODEV; |
| 792 | |
| 793 | rc = nfc_dep_link_down(dev); |
| 794 | |
| 795 | nfc_put_device(dev); |
| 796 | return rc; |
| 797 | } |
| 798 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 799 | static int nfc_genl_send_params(struct sk_buff *msg, |
| 800 | struct nfc_llcp_local *local, |
| 801 | u32 portid, u32 seq) |
| 802 | { |
| 803 | void *hdr; |
| 804 | |
| 805 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, |
| 806 | NFC_CMD_LLC_GET_PARAMS); |
| 807 | if (!hdr) |
| 808 | return -EMSGSIZE; |
| 809 | |
| 810 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || |
| 811 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || |
| 812 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || |
| 813 | nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) |
| 814 | goto nla_put_failure; |
| 815 | |
| 816 | return genlmsg_end(msg, hdr); |
| 817 | |
| 818 | nla_put_failure: |
| 819 | |
| 820 | genlmsg_cancel(msg, hdr); |
| 821 | return -EMSGSIZE; |
| 822 | } |
| 823 | |
| 824 | static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) |
| 825 | { |
| 826 | struct nfc_dev *dev; |
| 827 | struct nfc_llcp_local *local; |
| 828 | int rc = 0; |
| 829 | struct sk_buff *msg = NULL; |
| 830 | u32 idx; |
| 831 | |
| 832 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 833 | return -EINVAL; |
| 834 | |
| 835 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 836 | |
| 837 | dev = nfc_get_device(idx); |
| 838 | if (!dev) |
| 839 | return -ENODEV; |
| 840 | |
| 841 | device_lock(&dev->dev); |
| 842 | |
| 843 | local = nfc_llcp_find_local(dev); |
| 844 | if (!local) { |
| 845 | rc = -ENODEV; |
| 846 | goto exit; |
| 847 | } |
| 848 | |
| 849 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 850 | if (!msg) { |
| 851 | rc = -ENOMEM; |
| 852 | goto exit; |
| 853 | } |
| 854 | |
| 855 | rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); |
| 856 | |
| 857 | exit: |
| 858 | device_unlock(&dev->dev); |
| 859 | |
| 860 | nfc_put_device(dev); |
| 861 | |
| 862 | if (rc < 0) { |
| 863 | if (msg) |
| 864 | nlmsg_free(msg); |
| 865 | |
| 866 | return rc; |
| 867 | } |
| 868 | |
| 869 | return genlmsg_reply(msg, info); |
| 870 | } |
| 871 | |
| 872 | static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) |
| 873 | { |
| 874 | struct nfc_dev *dev; |
| 875 | struct nfc_llcp_local *local; |
| 876 | u8 rw = 0; |
| 877 | u16 miux = 0; |
| 878 | u32 idx; |
| 879 | int rc = 0; |
| 880 | |
| 881 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 882 | (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && |
| 883 | !info->attrs[NFC_ATTR_LLC_PARAM_RW] && |
| 884 | !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) |
| 885 | return -EINVAL; |
| 886 | |
| 887 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { |
| 888 | rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); |
| 889 | |
| 890 | if (rw > LLCP_MAX_RW) |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | |
| 894 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { |
| 895 | miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); |
| 896 | |
| 897 | if (miux > LLCP_MAX_MIUX) |
| 898 | return -EINVAL; |
| 899 | } |
| 900 | |
| 901 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 902 | |
| 903 | dev = nfc_get_device(idx); |
| 904 | if (!dev) |
| 905 | return -ENODEV; |
| 906 | |
| 907 | device_lock(&dev->dev); |
| 908 | |
| 909 | local = nfc_llcp_find_local(dev); |
| 910 | if (!local) { |
| 911 | nfc_put_device(dev); |
| 912 | rc = -ENODEV; |
| 913 | goto exit; |
| 914 | } |
| 915 | |
| 916 | if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { |
| 917 | if (dev->dep_link_up) { |
| 918 | rc = -EINPROGRESS; |
| 919 | goto exit; |
| 920 | } |
| 921 | |
| 922 | local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); |
| 923 | } |
| 924 | |
| 925 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) |
| 926 | local->rw = rw; |
| 927 | |
| 928 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) |
| 929 | local->miux = cpu_to_be16(miux); |
| 930 | |
| 931 | exit: |
| 932 | device_unlock(&dev->dev); |
| 933 | |
| 934 | nfc_put_device(dev); |
| 935 | |
| 936 | return rc; |
| 937 | } |
| 938 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 939 | static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) |
| 940 | { |
| 941 | struct nfc_dev *dev; |
| 942 | struct nfc_llcp_local *local; |
| 943 | struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; |
| 944 | u32 idx; |
| 945 | u8 tid; |
| 946 | char *uri; |
| 947 | int rc = 0, rem; |
| 948 | size_t uri_len, tlvs_len; |
| 949 | struct hlist_head sdreq_list; |
| 950 | struct nfc_llcp_sdp_tlv *sdreq; |
| 951 | |
| 952 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 953 | !info->attrs[NFC_ATTR_LLC_SDP]) |
| 954 | return -EINVAL; |
| 955 | |
| 956 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 957 | |
| 958 | dev = nfc_get_device(idx); |
| 959 | if (!dev) { |
| 960 | rc = -ENODEV; |
| 961 | goto exit; |
| 962 | } |
| 963 | |
| 964 | device_lock(&dev->dev); |
| 965 | |
| 966 | if (dev->dep_link_up == false) { |
| 967 | rc = -ENOLINK; |
| 968 | goto exit; |
| 969 | } |
| 970 | |
| 971 | local = nfc_llcp_find_local(dev); |
| 972 | if (!local) { |
| 973 | nfc_put_device(dev); |
| 974 | rc = -ENODEV; |
| 975 | goto exit; |
| 976 | } |
| 977 | |
| 978 | INIT_HLIST_HEAD(&sdreq_list); |
| 979 | |
| 980 | tlvs_len = 0; |
| 981 | |
| 982 | nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { |
| 983 | rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr, |
| 984 | nfc_sdp_genl_policy); |
| 985 | |
| 986 | if (rc != 0) { |
| 987 | rc = -EINVAL; |
| 988 | goto exit; |
| 989 | } |
| 990 | |
| 991 | if (!sdp_attrs[NFC_SDP_ATTR_URI]) |
| 992 | continue; |
| 993 | |
| 994 | uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 995 | if (uri_len == 0) |
| 996 | continue; |
| 997 | |
| 998 | uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 999 | if (uri == NULL || *uri == 0) |
| 1000 | continue; |
| 1001 | |
| 1002 | tid = local->sdreq_next_tid++; |
| 1003 | |
| 1004 | sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); |
| 1005 | if (sdreq == NULL) { |
| 1006 | rc = -ENOMEM; |
| 1007 | goto exit; |
| 1008 | } |
| 1009 | |
| 1010 | tlvs_len += sdreq->tlv_len; |
| 1011 | |
| 1012 | hlist_add_head(&sdreq->node, &sdreq_list); |
| 1013 | } |
| 1014 | |
| 1015 | if (hlist_empty(&sdreq_list)) { |
| 1016 | rc = -EINVAL; |
| 1017 | goto exit; |
| 1018 | } |
| 1019 | |
| 1020 | rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); |
| 1021 | exit: |
| 1022 | device_unlock(&dev->dev); |
| 1023 | |
| 1024 | nfc_put_device(dev); |
| 1025 | |
| 1026 | return rc; |
| 1027 | } |
| 1028 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1029 | static struct genl_ops nfc_genl_ops[] = { |
| 1030 | { |
| 1031 | .cmd = NFC_CMD_GET_DEVICE, |
| 1032 | .doit = nfc_genl_get_device, |
| 1033 | .dumpit = nfc_genl_dump_devices, |
| 1034 | .done = nfc_genl_dump_devices_done, |
| 1035 | .policy = nfc_genl_policy, |
| 1036 | }, |
| 1037 | { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 1038 | .cmd = NFC_CMD_DEV_UP, |
| 1039 | .doit = nfc_genl_dev_up, |
| 1040 | .policy = nfc_genl_policy, |
| 1041 | }, |
| 1042 | { |
| 1043 | .cmd = NFC_CMD_DEV_DOWN, |
| 1044 | .doit = nfc_genl_dev_down, |
| 1045 | .policy = nfc_genl_policy, |
| 1046 | }, |
| 1047 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1048 | .cmd = NFC_CMD_START_POLL, |
| 1049 | .doit = nfc_genl_start_poll, |
| 1050 | .policy = nfc_genl_policy, |
| 1051 | }, |
| 1052 | { |
| 1053 | .cmd = NFC_CMD_STOP_POLL, |
| 1054 | .doit = nfc_genl_stop_poll, |
| 1055 | .policy = nfc_genl_policy, |
| 1056 | }, |
| 1057 | { |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 1058 | .cmd = NFC_CMD_DEP_LINK_UP, |
| 1059 | .doit = nfc_genl_dep_link_up, |
| 1060 | .policy = nfc_genl_policy, |
| 1061 | }, |
| 1062 | { |
| 1063 | .cmd = NFC_CMD_DEP_LINK_DOWN, |
| 1064 | .doit = nfc_genl_dep_link_down, |
| 1065 | .policy = nfc_genl_policy, |
| 1066 | }, |
| 1067 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1068 | .cmd = NFC_CMD_GET_TARGET, |
| 1069 | .dumpit = nfc_genl_dump_targets, |
| 1070 | .done = nfc_genl_dump_targets_done, |
| 1071 | .policy = nfc_genl_policy, |
| 1072 | }, |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 1073 | { |
| 1074 | .cmd = NFC_CMD_LLC_GET_PARAMS, |
| 1075 | .doit = nfc_genl_llc_get_params, |
| 1076 | .policy = nfc_genl_policy, |
| 1077 | }, |
| 1078 | { |
| 1079 | .cmd = NFC_CMD_LLC_SET_PARAMS, |
| 1080 | .doit = nfc_genl_llc_set_params, |
| 1081 | .policy = nfc_genl_policy, |
| 1082 | }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1083 | { |
| 1084 | .cmd = NFC_CMD_LLC_SDREQ, |
| 1085 | .doit = nfc_genl_llc_sdreq, |
| 1086 | .policy = nfc_genl_policy, |
| 1087 | }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1088 | }; |
| 1089 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1090 | |
| 1091 | struct urelease_work { |
| 1092 | struct work_struct w; |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1093 | int portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1094 | }; |
| 1095 | |
| 1096 | static void nfc_urelease_event_work(struct work_struct *work) |
| 1097 | { |
| 1098 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
| 1099 | struct class_dev_iter iter; |
| 1100 | struct nfc_dev *dev; |
| 1101 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1102 | pr_debug("portid %d\n", w->portid); |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1103 | |
| 1104 | mutex_lock(&nfc_devlist_mutex); |
| 1105 | |
| 1106 | nfc_device_iter_init(&iter); |
| 1107 | dev = nfc_device_iter_next(&iter); |
| 1108 | |
| 1109 | while (dev) { |
| 1110 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 1111 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1112 | if (dev->genl_data.poll_req_portid == w->portid) { |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1113 | nfc_stop_poll(dev); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1114 | dev->genl_data.poll_req_portid = 0; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 1118 | |
| 1119 | dev = nfc_device_iter_next(&iter); |
| 1120 | } |
| 1121 | |
| 1122 | nfc_device_iter_exit(&iter); |
| 1123 | |
| 1124 | mutex_unlock(&nfc_devlist_mutex); |
| 1125 | |
| 1126 | kfree(w); |
| 1127 | } |
| 1128 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1129 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 1130 | unsigned long event, void *ptr) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1131 | { |
| 1132 | struct netlink_notify *n = ptr; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1133 | struct urelease_work *w; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1134 | |
| 1135 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) |
| 1136 | goto out; |
| 1137 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1138 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1139 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1140 | w = kmalloc(sizeof(*w), GFP_ATOMIC); |
| 1141 | if (w) { |
| 1142 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1143 | w->portid = n->portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1144 | schedule_work((struct work_struct *) w); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1145 | } |
| 1146 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1147 | out: |
| 1148 | return NOTIFY_DONE; |
| 1149 | } |
| 1150 | |
| 1151 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) |
| 1152 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1153 | genl_data->poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1154 | mutex_init(&genl_data->genl_data_mutex); |
| 1155 | } |
| 1156 | |
| 1157 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) |
| 1158 | { |
| 1159 | mutex_destroy(&genl_data->genl_data_mutex); |
| 1160 | } |
| 1161 | |
| 1162 | static struct notifier_block nl_notifier = { |
| 1163 | .notifier_call = nfc_genl_rcv_nl_event, |
| 1164 | }; |
| 1165 | |
| 1166 | /** |
| 1167 | * nfc_genl_init() - Initialize netlink interface |
| 1168 | * |
| 1169 | * This initialization function registers the nfc netlink family. |
| 1170 | */ |
| 1171 | int __init nfc_genl_init(void) |
| 1172 | { |
| 1173 | int rc; |
| 1174 | |
| 1175 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 1176 | ARRAY_SIZE(nfc_genl_ops)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1177 | if (rc) |
| 1178 | return rc; |
| 1179 | |
| 1180 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); |
| 1181 | |
| 1182 | netlink_register_notifier(&nl_notifier); |
| 1183 | |
| 1184 | return rc; |
| 1185 | } |
| 1186 | |
| 1187 | /** |
| 1188 | * nfc_genl_exit() - Deinitialize netlink interface |
| 1189 | * |
| 1190 | * This exit function unregisters the nfc netlink family. |
| 1191 | */ |
| 1192 | void nfc_genl_exit(void) |
| 1193 | { |
| 1194 | netlink_unregister_notifier(&nl_notifier); |
| 1195 | genl_unregister_family(&nfc_genl_family); |
| 1196 | } |