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 |
Jeff Kirsher | 98b32de | 2013-12-06 08:56:16 -0800 | [diff] [blame] | 19 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 20 | */ |
| 21 | |
Samuel Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 23 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 24 | #include <net/genetlink.h> |
| 25 | #include <linux/nfc.h> |
| 26 | #include <linux/slab.h> |
| 27 | |
| 28 | #include "nfc.h" |
Samuel Ortiz | 30cc458 | 2013-04-26 11:49:40 +0200 | [diff] [blame] | 29 | #include "llcp.h" |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 30 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 31 | static const struct genl_multicast_group nfc_genl_mcgrps[] = { |
| 32 | { .name = NFC_GENL_MCAST_EVENT_NAME, }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 33 | }; |
| 34 | |
H Hartley Sweeten | e5fe4cf | 2012-05-07 12:31:28 +0200 | [diff] [blame] | 35 | static struct genl_family nfc_genl_family = { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 36 | .id = GENL_ID_GENERATE, |
| 37 | .hdrsize = 0, |
| 38 | .name = NFC_GENL_NAME, |
| 39 | .version = NFC_GENL_VERSION, |
| 40 | .maxattr = NFC_ATTR_MAX, |
| 41 | }; |
| 42 | |
| 43 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { |
| 44 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, |
| 45 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, |
| 46 | .len = NFC_DEVICE_NAME_MAXSIZE }, |
| 47 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 48 | [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, |
| 49 | [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, |
Samuel Ortiz | c970a1a | 2012-03-05 01:03:34 +0100 | [diff] [blame] | 50 | [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 51 | [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, |
| 52 | [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, |
Thierry Escande | 8af362d | 2013-02-15 10:42:52 +0100 | [diff] [blame] | 53 | [NFC_ATTR_LLC_PARAM_LTO] = { .type = NLA_U8 }, |
| 54 | [NFC_ATTR_LLC_PARAM_RW] = { .type = NLA_U8 }, |
| 55 | [NFC_ATTR_LLC_PARAM_MIUX] = { .type = NLA_U16 }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 56 | [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 57 | [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING, |
| 58 | .len = NFC_FIRMWARE_NAME_MAXSIZE }, |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 59 | [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 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 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 195 | return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 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 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 224 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 256 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 286 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 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 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 319 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 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 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 349 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 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 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 415 | return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 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 | |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 428 | int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type) |
| 429 | { |
| 430 | struct sk_buff *msg; |
| 431 | void *hdr; |
| 432 | |
| 433 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 434 | if (!msg) |
| 435 | return -ENOMEM; |
| 436 | |
| 437 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 438 | NFC_EVENT_SE_ADDED); |
| 439 | if (!hdr) |
| 440 | goto free_msg; |
| 441 | |
| 442 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 443 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || |
| 444 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, type)) |
| 445 | goto nla_put_failure; |
| 446 | |
| 447 | genlmsg_end(msg, hdr); |
| 448 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 449 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 450 | |
| 451 | return 0; |
| 452 | |
| 453 | nla_put_failure: |
| 454 | genlmsg_cancel(msg, hdr); |
| 455 | free_msg: |
| 456 | nlmsg_free(msg); |
| 457 | return -EMSGSIZE; |
| 458 | } |
| 459 | |
| 460 | int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx) |
| 461 | { |
| 462 | struct sk_buff *msg; |
| 463 | void *hdr; |
| 464 | |
| 465 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 466 | if (!msg) |
| 467 | return -ENOMEM; |
| 468 | |
| 469 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 470 | NFC_EVENT_SE_REMOVED); |
| 471 | if (!hdr) |
| 472 | goto free_msg; |
| 473 | |
| 474 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 475 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx)) |
| 476 | goto nla_put_failure; |
| 477 | |
| 478 | genlmsg_end(msg, hdr); |
| 479 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 480 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 481 | |
| 482 | return 0; |
| 483 | |
| 484 | nla_put_failure: |
| 485 | genlmsg_cancel(msg, hdr); |
| 486 | free_msg: |
| 487 | nlmsg_free(msg); |
| 488 | return -EMSGSIZE; |
| 489 | } |
| 490 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 491 | 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] | 492 | u32 portid, u32 seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 493 | struct netlink_callback *cb, |
| 494 | int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 495 | { |
| 496 | void *hdr; |
| 497 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 498 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 499 | NFC_CMD_GET_DEVICE); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 500 | if (!hdr) |
| 501 | return -EMSGSIZE; |
| 502 | |
| 503 | if (cb) |
| 504 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 505 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 506 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 507 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 508 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
Thierry Escande | 7ad3939 | 2012-10-05 11:19:58 +0200 | [diff] [blame] | 509 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || |
| 510 | nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 511 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 512 | |
| 513 | return genlmsg_end(msg, hdr); |
| 514 | |
| 515 | nla_put_failure: |
| 516 | genlmsg_cancel(msg, hdr); |
| 517 | return -EMSGSIZE; |
| 518 | } |
| 519 | |
| 520 | static int nfc_genl_dump_devices(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 521 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 522 | { |
| 523 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 524 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 525 | bool first_call = false; |
| 526 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 527 | if (!iter) { |
| 528 | first_call = true; |
| 529 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 530 | if (!iter) |
| 531 | return -ENOMEM; |
| 532 | cb->args[0] = (long) iter; |
| 533 | } |
| 534 | |
| 535 | mutex_lock(&nfc_devlist_mutex); |
| 536 | |
| 537 | cb->seq = nfc_devlist_generation; |
| 538 | |
| 539 | if (first_call) { |
| 540 | nfc_device_iter_init(iter); |
| 541 | dev = nfc_device_iter_next(iter); |
| 542 | } |
| 543 | |
| 544 | while (dev) { |
| 545 | int rc; |
| 546 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 547 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 548 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 549 | if (rc < 0) |
| 550 | break; |
| 551 | |
| 552 | dev = nfc_device_iter_next(iter); |
| 553 | } |
| 554 | |
| 555 | mutex_unlock(&nfc_devlist_mutex); |
| 556 | |
| 557 | cb->args[1] = (long) dev; |
| 558 | |
| 559 | return skb->len; |
| 560 | } |
| 561 | |
| 562 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) |
| 563 | { |
| 564 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 565 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 566 | nfc_device_iter_exit(iter); |
| 567 | kfree(iter); |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 572 | 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] | 573 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 574 | { |
| 575 | struct sk_buff *msg; |
| 576 | void *hdr; |
| 577 | |
| 578 | pr_debug("DEP link is up\n"); |
| 579 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 580 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 581 | if (!msg) |
| 582 | return -ENOMEM; |
| 583 | |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 584 | 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] | 585 | if (!hdr) |
| 586 | goto free_msg; |
| 587 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 588 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 589 | goto nla_put_failure; |
| 590 | if (rf_mode == NFC_RF_INITIATOR && |
| 591 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 592 | goto nla_put_failure; |
| 593 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || |
| 594 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) |
| 595 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 596 | |
| 597 | genlmsg_end(msg, hdr); |
| 598 | |
| 599 | dev->dep_link_up = true; |
| 600 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 601 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 602 | |
| 603 | return 0; |
| 604 | |
| 605 | nla_put_failure: |
| 606 | genlmsg_cancel(msg, hdr); |
| 607 | free_msg: |
| 608 | nlmsg_free(msg); |
| 609 | return -EMSGSIZE; |
| 610 | } |
| 611 | |
| 612 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) |
| 613 | { |
| 614 | struct sk_buff *msg; |
| 615 | void *hdr; |
| 616 | |
| 617 | pr_debug("DEP link is down\n"); |
| 618 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 619 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 620 | if (!msg) |
| 621 | return -ENOMEM; |
| 622 | |
| 623 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 624 | NFC_CMD_DEP_LINK_DOWN); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 625 | if (!hdr) |
| 626 | goto free_msg; |
| 627 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 628 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 629 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 630 | |
| 631 | genlmsg_end(msg, hdr); |
| 632 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 633 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 634 | |
| 635 | return 0; |
| 636 | |
| 637 | nla_put_failure: |
| 638 | genlmsg_cancel(msg, hdr); |
| 639 | free_msg: |
| 640 | nlmsg_free(msg); |
| 641 | return -EMSGSIZE; |
| 642 | } |
| 643 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 644 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
| 645 | { |
| 646 | struct sk_buff *msg; |
| 647 | struct nfc_dev *dev; |
| 648 | u32 idx; |
| 649 | int rc = -ENOBUFS; |
| 650 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 651 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 655 | |
| 656 | dev = nfc_get_device(idx); |
| 657 | if (!dev) |
| 658 | return -ENODEV; |
| 659 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 660 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 661 | if (!msg) { |
| 662 | rc = -ENOMEM; |
| 663 | goto out_putdev; |
| 664 | } |
| 665 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 666 | 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] | 667 | NULL, 0); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 668 | if (rc < 0) |
| 669 | goto out_free; |
| 670 | |
| 671 | nfc_put_device(dev); |
| 672 | |
| 673 | return genlmsg_reply(msg, info); |
| 674 | |
| 675 | out_free: |
| 676 | nlmsg_free(msg); |
| 677 | out_putdev: |
| 678 | nfc_put_device(dev); |
| 679 | return rc; |
| 680 | } |
| 681 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 682 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
| 683 | { |
| 684 | struct nfc_dev *dev; |
| 685 | int rc; |
| 686 | u32 idx; |
| 687 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 688 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 689 | return -EINVAL; |
| 690 | |
| 691 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 692 | |
| 693 | dev = nfc_get_device(idx); |
| 694 | if (!dev) |
| 695 | return -ENODEV; |
| 696 | |
| 697 | rc = nfc_dev_up(dev); |
| 698 | |
| 699 | nfc_put_device(dev); |
| 700 | return rc; |
| 701 | } |
| 702 | |
| 703 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) |
| 704 | { |
| 705 | struct nfc_dev *dev; |
| 706 | int rc; |
| 707 | u32 idx; |
| 708 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +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 | |
| 718 | rc = nfc_dev_down(dev); |
| 719 | |
| 720 | nfc_put_device(dev); |
| 721 | return rc; |
| 722 | } |
| 723 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 724 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
| 725 | { |
| 726 | struct nfc_dev *dev; |
| 727 | int rc; |
| 728 | u32 idx; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 729 | u32 im_protocols = 0, tm_protocols = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 730 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 731 | pr_debug("Poll start\n"); |
| 732 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 733 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 734 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
| 735 | !info->attrs[NFC_ATTR_PROTOCOLS]) && |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 736 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 737 | return -EINVAL; |
| 738 | |
| 739 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 740 | |
| 741 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) |
| 742 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 743 | |
| 744 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) |
| 745 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); |
Samuel Ortiz | 5e50ee3 | 2012-05-31 11:48:58 +0200 | [diff] [blame] | 746 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
| 747 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 748 | |
| 749 | dev = nfc_get_device(idx); |
| 750 | if (!dev) |
| 751 | return -ENODEV; |
| 752 | |
| 753 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 754 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 755 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 756 | if (!rc) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 757 | dev->genl_data.poll_req_portid = info->snd_portid; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 758 | |
| 759 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 760 | |
| 761 | nfc_put_device(dev); |
| 762 | return rc; |
| 763 | } |
| 764 | |
| 765 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) |
| 766 | { |
| 767 | struct nfc_dev *dev; |
| 768 | int rc; |
| 769 | u32 idx; |
| 770 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 771 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 772 | return -EINVAL; |
| 773 | |
| 774 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 775 | |
| 776 | dev = nfc_get_device(idx); |
| 777 | if (!dev) |
| 778 | return -ENODEV; |
| 779 | |
Samuel Ortiz | a831b91 | 2012-06-28 16:41:57 +0200 | [diff] [blame] | 780 | device_lock(&dev->dev); |
| 781 | |
| 782 | if (!dev->polling) { |
| 783 | device_unlock(&dev->dev); |
| 784 | return -EINVAL; |
| 785 | } |
| 786 | |
| 787 | device_unlock(&dev->dev); |
| 788 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 789 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 790 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 791 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 792 | rc = -EBUSY; |
| 793 | goto out; |
| 794 | } |
| 795 | |
| 796 | rc = nfc_stop_poll(dev); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 797 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 798 | |
| 799 | out: |
| 800 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 801 | nfc_put_device(dev); |
| 802 | return rc; |
| 803 | } |
| 804 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 805 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
| 806 | { |
| 807 | struct nfc_dev *dev; |
| 808 | int rc, tgt_idx; |
| 809 | u32 idx; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 810 | u8 comm; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 811 | |
| 812 | pr_debug("DEP link up\n"); |
| 813 | |
| 814 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 815 | !info->attrs[NFC_ATTR_COMM_MODE]) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 816 | return -EINVAL; |
| 817 | |
| 818 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 819 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) |
| 820 | tgt_idx = NFC_TARGET_IDX_ANY; |
| 821 | else |
| 822 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 823 | |
| 824 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 825 | |
| 826 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) |
| 827 | return -EINVAL; |
| 828 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 829 | dev = nfc_get_device(idx); |
| 830 | if (!dev) |
| 831 | return -ENODEV; |
| 832 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 833 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 834 | |
| 835 | nfc_put_device(dev); |
| 836 | |
| 837 | return rc; |
| 838 | } |
| 839 | |
| 840 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) |
| 841 | { |
| 842 | struct nfc_dev *dev; |
| 843 | int rc; |
| 844 | u32 idx; |
| 845 | |
| 846 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 847 | return -EINVAL; |
| 848 | |
| 849 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 850 | |
| 851 | dev = nfc_get_device(idx); |
| 852 | if (!dev) |
| 853 | return -ENODEV; |
| 854 | |
| 855 | rc = nfc_dep_link_down(dev); |
| 856 | |
| 857 | nfc_put_device(dev); |
| 858 | return rc; |
| 859 | } |
| 860 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 861 | static int nfc_genl_send_params(struct sk_buff *msg, |
| 862 | struct nfc_llcp_local *local, |
| 863 | u32 portid, u32 seq) |
| 864 | { |
| 865 | void *hdr; |
| 866 | |
| 867 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, |
| 868 | NFC_CMD_LLC_GET_PARAMS); |
| 869 | if (!hdr) |
| 870 | return -EMSGSIZE; |
| 871 | |
| 872 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || |
| 873 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || |
| 874 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || |
| 875 | nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) |
| 876 | goto nla_put_failure; |
| 877 | |
| 878 | return genlmsg_end(msg, hdr); |
| 879 | |
| 880 | nla_put_failure: |
| 881 | |
| 882 | genlmsg_cancel(msg, hdr); |
| 883 | return -EMSGSIZE; |
| 884 | } |
| 885 | |
| 886 | static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) |
| 887 | { |
| 888 | struct nfc_dev *dev; |
| 889 | struct nfc_llcp_local *local; |
| 890 | int rc = 0; |
| 891 | struct sk_buff *msg = NULL; |
| 892 | u32 idx; |
| 893 | |
| 894 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 895 | return -EINVAL; |
| 896 | |
| 897 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 898 | |
| 899 | dev = nfc_get_device(idx); |
| 900 | if (!dev) |
| 901 | return -ENODEV; |
| 902 | |
| 903 | device_lock(&dev->dev); |
| 904 | |
| 905 | local = nfc_llcp_find_local(dev); |
| 906 | if (!local) { |
| 907 | rc = -ENODEV; |
| 908 | goto exit; |
| 909 | } |
| 910 | |
| 911 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 912 | if (!msg) { |
| 913 | rc = -ENOMEM; |
| 914 | goto exit; |
| 915 | } |
| 916 | |
| 917 | rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); |
| 918 | |
| 919 | exit: |
| 920 | device_unlock(&dev->dev); |
| 921 | |
| 922 | nfc_put_device(dev); |
| 923 | |
| 924 | if (rc < 0) { |
| 925 | if (msg) |
| 926 | nlmsg_free(msg); |
| 927 | |
| 928 | return rc; |
| 929 | } |
| 930 | |
| 931 | return genlmsg_reply(msg, info); |
| 932 | } |
| 933 | |
| 934 | static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) |
| 935 | { |
| 936 | struct nfc_dev *dev; |
| 937 | struct nfc_llcp_local *local; |
| 938 | u8 rw = 0; |
| 939 | u16 miux = 0; |
| 940 | u32 idx; |
| 941 | int rc = 0; |
| 942 | |
| 943 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 944 | (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && |
| 945 | !info->attrs[NFC_ATTR_LLC_PARAM_RW] && |
| 946 | !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) |
| 947 | return -EINVAL; |
| 948 | |
| 949 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { |
| 950 | rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); |
| 951 | |
| 952 | if (rw > LLCP_MAX_RW) |
| 953 | return -EINVAL; |
| 954 | } |
| 955 | |
| 956 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { |
| 957 | miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); |
| 958 | |
| 959 | if (miux > LLCP_MAX_MIUX) |
| 960 | return -EINVAL; |
| 961 | } |
| 962 | |
| 963 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 964 | |
| 965 | dev = nfc_get_device(idx); |
| 966 | if (!dev) |
| 967 | return -ENODEV; |
| 968 | |
| 969 | device_lock(&dev->dev); |
| 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 | if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { |
| 979 | if (dev->dep_link_up) { |
| 980 | rc = -EINPROGRESS; |
| 981 | goto exit; |
| 982 | } |
| 983 | |
| 984 | local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); |
| 985 | } |
| 986 | |
| 987 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) |
| 988 | local->rw = rw; |
| 989 | |
| 990 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) |
| 991 | local->miux = cpu_to_be16(miux); |
| 992 | |
| 993 | exit: |
| 994 | device_unlock(&dev->dev); |
| 995 | |
| 996 | nfc_put_device(dev); |
| 997 | |
| 998 | return rc; |
| 999 | } |
| 1000 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1001 | static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) |
| 1002 | { |
| 1003 | struct nfc_dev *dev; |
| 1004 | struct nfc_llcp_local *local; |
| 1005 | struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; |
| 1006 | u32 idx; |
| 1007 | u8 tid; |
| 1008 | char *uri; |
| 1009 | int rc = 0, rem; |
| 1010 | size_t uri_len, tlvs_len; |
| 1011 | struct hlist_head sdreq_list; |
| 1012 | struct nfc_llcp_sdp_tlv *sdreq; |
| 1013 | |
| 1014 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1015 | !info->attrs[NFC_ATTR_LLC_SDP]) |
| 1016 | return -EINVAL; |
| 1017 | |
| 1018 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1019 | |
| 1020 | dev = nfc_get_device(idx); |
| 1021 | if (!dev) { |
| 1022 | rc = -ENODEV; |
| 1023 | goto exit; |
| 1024 | } |
| 1025 | |
| 1026 | device_lock(&dev->dev); |
| 1027 | |
| 1028 | if (dev->dep_link_up == false) { |
| 1029 | rc = -ENOLINK; |
| 1030 | goto exit; |
| 1031 | } |
| 1032 | |
| 1033 | local = nfc_llcp_find_local(dev); |
| 1034 | if (!local) { |
| 1035 | nfc_put_device(dev); |
| 1036 | rc = -ENODEV; |
| 1037 | goto exit; |
| 1038 | } |
| 1039 | |
| 1040 | INIT_HLIST_HEAD(&sdreq_list); |
| 1041 | |
| 1042 | tlvs_len = 0; |
| 1043 | |
| 1044 | nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { |
| 1045 | rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr, |
| 1046 | nfc_sdp_genl_policy); |
| 1047 | |
| 1048 | if (rc != 0) { |
| 1049 | rc = -EINVAL; |
| 1050 | goto exit; |
| 1051 | } |
| 1052 | |
| 1053 | if (!sdp_attrs[NFC_SDP_ATTR_URI]) |
| 1054 | continue; |
| 1055 | |
| 1056 | uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 1057 | if (uri_len == 0) |
| 1058 | continue; |
| 1059 | |
| 1060 | uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 1061 | if (uri == NULL || *uri == 0) |
| 1062 | continue; |
| 1063 | |
| 1064 | tid = local->sdreq_next_tid++; |
| 1065 | |
| 1066 | sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); |
| 1067 | if (sdreq == NULL) { |
| 1068 | rc = -ENOMEM; |
| 1069 | goto exit; |
| 1070 | } |
| 1071 | |
| 1072 | tlvs_len += sdreq->tlv_len; |
| 1073 | |
| 1074 | hlist_add_head(&sdreq->node, &sdreq_list); |
| 1075 | } |
| 1076 | |
| 1077 | if (hlist_empty(&sdreq_list)) { |
| 1078 | rc = -EINVAL; |
| 1079 | goto exit; |
| 1080 | } |
| 1081 | |
| 1082 | rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); |
| 1083 | exit: |
| 1084 | device_unlock(&dev->dev); |
| 1085 | |
| 1086 | nfc_put_device(dev); |
| 1087 | |
| 1088 | return rc; |
| 1089 | } |
| 1090 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1091 | static int nfc_genl_fw_download(struct sk_buff *skb, struct genl_info *info) |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1092 | { |
| 1093 | struct nfc_dev *dev; |
| 1094 | int rc; |
| 1095 | u32 idx; |
| 1096 | char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1]; |
| 1097 | |
| 1098 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 1099 | return -EINVAL; |
| 1100 | |
| 1101 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1102 | |
| 1103 | dev = nfc_get_device(idx); |
| 1104 | if (!dev) |
| 1105 | return -ENODEV; |
| 1106 | |
| 1107 | nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME], |
| 1108 | sizeof(firmware_name)); |
| 1109 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1110 | rc = nfc_fw_download(dev, firmware_name); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1111 | |
| 1112 | nfc_put_device(dev); |
| 1113 | return rc; |
| 1114 | } |
| 1115 | |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 1116 | int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name, |
| 1117 | u32 result) |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1118 | { |
| 1119 | struct sk_buff *msg; |
| 1120 | void *hdr; |
| 1121 | |
| 1122 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1123 | if (!msg) |
| 1124 | return -ENOMEM; |
| 1125 | |
| 1126 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1127 | NFC_CMD_FW_DOWNLOAD); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1128 | if (!hdr) |
| 1129 | goto free_msg; |
| 1130 | |
| 1131 | if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) || |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 1132 | nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) || |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1133 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 1134 | goto nla_put_failure; |
| 1135 | |
| 1136 | genlmsg_end(msg, hdr); |
| 1137 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1138 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1139 | |
| 1140 | return 0; |
| 1141 | |
| 1142 | nla_put_failure: |
| 1143 | genlmsg_cancel(msg, hdr); |
| 1144 | free_msg: |
| 1145 | nlmsg_free(msg); |
| 1146 | return -EMSGSIZE; |
| 1147 | } |
| 1148 | |
Samuel Ortiz | be08565 | 2013-05-10 17:07:32 +0200 | [diff] [blame] | 1149 | static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info) |
| 1150 | { |
| 1151 | struct nfc_dev *dev; |
| 1152 | int rc; |
| 1153 | u32 idx, se_idx; |
| 1154 | |
| 1155 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1156 | !info->attrs[NFC_ATTR_SE_INDEX]) |
| 1157 | return -EINVAL; |
| 1158 | |
| 1159 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1160 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1161 | |
| 1162 | dev = nfc_get_device(idx); |
| 1163 | if (!dev) |
| 1164 | return -ENODEV; |
| 1165 | |
| 1166 | rc = nfc_enable_se(dev, se_idx); |
| 1167 | |
| 1168 | nfc_put_device(dev); |
| 1169 | return rc; |
| 1170 | } |
| 1171 | |
| 1172 | static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info) |
| 1173 | { |
| 1174 | struct nfc_dev *dev; |
| 1175 | int rc; |
| 1176 | u32 idx, se_idx; |
| 1177 | |
| 1178 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1179 | !info->attrs[NFC_ATTR_SE_INDEX]) |
| 1180 | return -EINVAL; |
| 1181 | |
| 1182 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1183 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1184 | |
| 1185 | dev = nfc_get_device(idx); |
| 1186 | if (!dev) |
| 1187 | return -ENODEV; |
| 1188 | |
| 1189 | rc = nfc_disable_se(dev, se_idx); |
| 1190 | |
| 1191 | nfc_put_device(dev); |
| 1192 | return rc; |
| 1193 | } |
| 1194 | |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1195 | static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev, |
| 1196 | u32 portid, u32 seq, |
| 1197 | struct netlink_callback *cb, |
| 1198 | int flags) |
| 1199 | { |
| 1200 | void *hdr; |
| 1201 | struct nfc_se *se, *n; |
| 1202 | |
| 1203 | list_for_each_entry_safe(se, n, &dev->secure_elements, list) { |
| 1204 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
| 1205 | NFC_CMD_GET_SE); |
| 1206 | if (!hdr) |
| 1207 | goto nla_put_failure; |
| 1208 | |
| 1209 | if (cb) |
| 1210 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 1211 | |
| 1212 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 1213 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) || |
| 1214 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) |
| 1215 | goto nla_put_failure; |
| 1216 | |
| 1217 | if (genlmsg_end(msg, hdr) < 0) |
| 1218 | goto nla_put_failure; |
| 1219 | } |
| 1220 | |
| 1221 | return 0; |
| 1222 | |
| 1223 | nla_put_failure: |
| 1224 | genlmsg_cancel(msg, hdr); |
| 1225 | return -EMSGSIZE; |
| 1226 | } |
| 1227 | |
| 1228 | static int nfc_genl_dump_ses(struct sk_buff *skb, |
| 1229 | struct netlink_callback *cb) |
| 1230 | { |
| 1231 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 1232 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 1233 | bool first_call = false; |
| 1234 | |
| 1235 | if (!iter) { |
| 1236 | first_call = true; |
| 1237 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 1238 | if (!iter) |
| 1239 | return -ENOMEM; |
| 1240 | cb->args[0] = (long) iter; |
| 1241 | } |
| 1242 | |
| 1243 | mutex_lock(&nfc_devlist_mutex); |
| 1244 | |
| 1245 | cb->seq = nfc_devlist_generation; |
| 1246 | |
| 1247 | if (first_call) { |
| 1248 | nfc_device_iter_init(iter); |
| 1249 | dev = nfc_device_iter_next(iter); |
| 1250 | } |
| 1251 | |
| 1252 | while (dev) { |
| 1253 | int rc; |
| 1254 | |
| 1255 | rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid, |
| 1256 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
| 1257 | if (rc < 0) |
| 1258 | break; |
| 1259 | |
| 1260 | dev = nfc_device_iter_next(iter); |
| 1261 | } |
| 1262 | |
| 1263 | mutex_unlock(&nfc_devlist_mutex); |
| 1264 | |
| 1265 | cb->args[1] = (long) dev; |
| 1266 | |
| 1267 | return skb->len; |
| 1268 | } |
| 1269 | |
| 1270 | static int nfc_genl_dump_ses_done(struct netlink_callback *cb) |
| 1271 | { |
| 1272 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 1273 | |
| 1274 | nfc_device_iter_exit(iter); |
| 1275 | kfree(iter); |
| 1276 | |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1280 | struct se_io_ctx { |
| 1281 | u32 dev_idx; |
| 1282 | u32 se_idx; |
| 1283 | }; |
| 1284 | |
Samuel Ortiz | ddc1a70 | 2013-10-07 14:18:44 +0200 | [diff] [blame] | 1285 | static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err) |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1286 | { |
| 1287 | struct se_io_ctx *ctx = context; |
| 1288 | struct sk_buff *msg; |
| 1289 | void *hdr; |
| 1290 | |
| 1291 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1292 | if (!msg) { |
| 1293 | kfree(ctx); |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 1298 | NFC_CMD_SE_IO); |
| 1299 | if (!hdr) |
| 1300 | goto free_msg; |
| 1301 | |
| 1302 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) || |
| 1303 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) || |
| 1304 | nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu)) |
| 1305 | goto nla_put_failure; |
| 1306 | |
| 1307 | genlmsg_end(msg, hdr); |
| 1308 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1309 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1310 | |
| 1311 | kfree(ctx); |
| 1312 | |
| 1313 | return; |
| 1314 | |
| 1315 | nla_put_failure: |
| 1316 | genlmsg_cancel(msg, hdr); |
| 1317 | free_msg: |
| 1318 | nlmsg_free(msg); |
| 1319 | kfree(ctx); |
| 1320 | |
| 1321 | return; |
| 1322 | } |
| 1323 | |
| 1324 | static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) |
| 1325 | { |
| 1326 | struct nfc_dev *dev; |
| 1327 | struct se_io_ctx *ctx; |
| 1328 | u32 dev_idx, se_idx; |
| 1329 | u8 *apdu; |
| 1330 | size_t apdu_len; |
| 1331 | |
| 1332 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1333 | !info->attrs[NFC_ATTR_SE_INDEX] || |
| 1334 | !info->attrs[NFC_ATTR_SE_APDU]) |
| 1335 | return -EINVAL; |
| 1336 | |
| 1337 | dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1338 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1339 | |
| 1340 | dev = nfc_get_device(dev_idx); |
| 1341 | if (!dev) |
| 1342 | return -ENODEV; |
| 1343 | |
| 1344 | if (!dev->ops || !dev->ops->se_io) |
| 1345 | return -ENOTSUPP; |
| 1346 | |
| 1347 | apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]); |
| 1348 | if (apdu_len == 0) |
| 1349 | return -EINVAL; |
| 1350 | |
| 1351 | apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]); |
| 1352 | if (!apdu) |
| 1353 | return -EINVAL; |
| 1354 | |
| 1355 | ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL); |
| 1356 | if (!ctx) |
| 1357 | return -ENOMEM; |
| 1358 | |
| 1359 | ctx->dev_idx = dev_idx; |
| 1360 | ctx->se_idx = se_idx; |
| 1361 | |
| 1362 | return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); |
| 1363 | } |
| 1364 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 1365 | static const struct genl_ops nfc_genl_ops[] = { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1366 | { |
| 1367 | .cmd = NFC_CMD_GET_DEVICE, |
| 1368 | .doit = nfc_genl_get_device, |
| 1369 | .dumpit = nfc_genl_dump_devices, |
| 1370 | .done = nfc_genl_dump_devices_done, |
| 1371 | .policy = nfc_genl_policy, |
| 1372 | }, |
| 1373 | { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 1374 | .cmd = NFC_CMD_DEV_UP, |
| 1375 | .doit = nfc_genl_dev_up, |
| 1376 | .policy = nfc_genl_policy, |
| 1377 | }, |
| 1378 | { |
| 1379 | .cmd = NFC_CMD_DEV_DOWN, |
| 1380 | .doit = nfc_genl_dev_down, |
| 1381 | .policy = nfc_genl_policy, |
| 1382 | }, |
| 1383 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1384 | .cmd = NFC_CMD_START_POLL, |
| 1385 | .doit = nfc_genl_start_poll, |
| 1386 | .policy = nfc_genl_policy, |
| 1387 | }, |
| 1388 | { |
| 1389 | .cmd = NFC_CMD_STOP_POLL, |
| 1390 | .doit = nfc_genl_stop_poll, |
| 1391 | .policy = nfc_genl_policy, |
| 1392 | }, |
| 1393 | { |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 1394 | .cmd = NFC_CMD_DEP_LINK_UP, |
| 1395 | .doit = nfc_genl_dep_link_up, |
| 1396 | .policy = nfc_genl_policy, |
| 1397 | }, |
| 1398 | { |
| 1399 | .cmd = NFC_CMD_DEP_LINK_DOWN, |
| 1400 | .doit = nfc_genl_dep_link_down, |
| 1401 | .policy = nfc_genl_policy, |
| 1402 | }, |
| 1403 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1404 | .cmd = NFC_CMD_GET_TARGET, |
| 1405 | .dumpit = nfc_genl_dump_targets, |
| 1406 | .done = nfc_genl_dump_targets_done, |
| 1407 | .policy = nfc_genl_policy, |
| 1408 | }, |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 1409 | { |
| 1410 | .cmd = NFC_CMD_LLC_GET_PARAMS, |
| 1411 | .doit = nfc_genl_llc_get_params, |
| 1412 | .policy = nfc_genl_policy, |
| 1413 | }, |
| 1414 | { |
| 1415 | .cmd = NFC_CMD_LLC_SET_PARAMS, |
| 1416 | .doit = nfc_genl_llc_set_params, |
| 1417 | .policy = nfc_genl_policy, |
| 1418 | }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1419 | { |
| 1420 | .cmd = NFC_CMD_LLC_SDREQ, |
| 1421 | .doit = nfc_genl_llc_sdreq, |
| 1422 | .policy = nfc_genl_policy, |
| 1423 | }, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1424 | { |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1425 | .cmd = NFC_CMD_FW_DOWNLOAD, |
| 1426 | .doit = nfc_genl_fw_download, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1427 | .policy = nfc_genl_policy, |
| 1428 | }, |
Samuel Ortiz | be08565 | 2013-05-10 17:07:32 +0200 | [diff] [blame] | 1429 | { |
| 1430 | .cmd = NFC_CMD_ENABLE_SE, |
| 1431 | .doit = nfc_genl_enable_se, |
| 1432 | .policy = nfc_genl_policy, |
| 1433 | }, |
| 1434 | { |
| 1435 | .cmd = NFC_CMD_DISABLE_SE, |
| 1436 | .doit = nfc_genl_disable_se, |
| 1437 | .policy = nfc_genl_policy, |
| 1438 | }, |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1439 | { |
| 1440 | .cmd = NFC_CMD_GET_SE, |
| 1441 | .dumpit = nfc_genl_dump_ses, |
| 1442 | .done = nfc_genl_dump_ses_done, |
| 1443 | .policy = nfc_genl_policy, |
| 1444 | }, |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1445 | { |
| 1446 | .cmd = NFC_CMD_SE_IO, |
| 1447 | .doit = nfc_genl_se_io, |
| 1448 | .policy = nfc_genl_policy, |
| 1449 | }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1450 | }; |
| 1451 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1452 | |
| 1453 | struct urelease_work { |
| 1454 | struct work_struct w; |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1455 | int portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1456 | }; |
| 1457 | |
| 1458 | static void nfc_urelease_event_work(struct work_struct *work) |
| 1459 | { |
| 1460 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
| 1461 | struct class_dev_iter iter; |
| 1462 | struct nfc_dev *dev; |
| 1463 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1464 | pr_debug("portid %d\n", w->portid); |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1465 | |
| 1466 | mutex_lock(&nfc_devlist_mutex); |
| 1467 | |
| 1468 | nfc_device_iter_init(&iter); |
| 1469 | dev = nfc_device_iter_next(&iter); |
| 1470 | |
| 1471 | while (dev) { |
| 1472 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 1473 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1474 | if (dev->genl_data.poll_req_portid == w->portid) { |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1475 | nfc_stop_poll(dev); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1476 | dev->genl_data.poll_req_portid = 0; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 1480 | |
| 1481 | dev = nfc_device_iter_next(&iter); |
| 1482 | } |
| 1483 | |
| 1484 | nfc_device_iter_exit(&iter); |
| 1485 | |
| 1486 | mutex_unlock(&nfc_devlist_mutex); |
| 1487 | |
| 1488 | kfree(w); |
| 1489 | } |
| 1490 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1491 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 1492 | unsigned long event, void *ptr) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1493 | { |
| 1494 | struct netlink_notify *n = ptr; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1495 | struct urelease_work *w; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1496 | |
| 1497 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) |
| 1498 | goto out; |
| 1499 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1500 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1501 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1502 | w = kmalloc(sizeof(*w), GFP_ATOMIC); |
| 1503 | if (w) { |
| 1504 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1505 | w->portid = n->portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1506 | schedule_work((struct work_struct *) w); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1507 | } |
| 1508 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1509 | out: |
| 1510 | return NOTIFY_DONE; |
| 1511 | } |
| 1512 | |
| 1513 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) |
| 1514 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1515 | genl_data->poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1516 | mutex_init(&genl_data->genl_data_mutex); |
| 1517 | } |
| 1518 | |
| 1519 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) |
| 1520 | { |
| 1521 | mutex_destroy(&genl_data->genl_data_mutex); |
| 1522 | } |
| 1523 | |
| 1524 | static struct notifier_block nl_notifier = { |
| 1525 | .notifier_call = nfc_genl_rcv_nl_event, |
| 1526 | }; |
| 1527 | |
| 1528 | /** |
| 1529 | * nfc_genl_init() - Initialize netlink interface |
| 1530 | * |
| 1531 | * This initialization function registers the nfc netlink family. |
| 1532 | */ |
| 1533 | int __init nfc_genl_init(void) |
| 1534 | { |
| 1535 | int rc; |
| 1536 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1537 | rc = genl_register_family_with_ops_groups(&nfc_genl_family, |
| 1538 | nfc_genl_ops, |
| 1539 | nfc_genl_mcgrps); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1540 | if (rc) |
| 1541 | return rc; |
| 1542 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1543 | netlink_register_notifier(&nl_notifier); |
| 1544 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1545 | return 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | /** |
| 1549 | * nfc_genl_exit() - Deinitialize netlink interface |
| 1550 | * |
| 1551 | * This exit function unregisters the nfc netlink family. |
| 1552 | */ |
| 1553 | void nfc_genl_exit(void) |
| 1554 | { |
| 1555 | netlink_unregister_notifier(&nl_notifier); |
| 1556 | genl_unregister_family(&nfc_genl_family); |
| 1557 | } |