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 | |
Mark A. Greer | f5f6872 | 2014-01-14 17:52:11 -0700 | [diff] [blame] | 97 | if (target->is_iso15693) { |
| 98 | if (nla_put_u8(msg, NFC_ATTR_TARGET_ISO15693_DSFID, |
| 99 | target->iso15693_dsfid) || |
| 100 | nla_put(msg, NFC_ATTR_TARGET_ISO15693_UID, |
| 101 | sizeof(target->iso15693_uid), target->iso15693_uid)) |
| 102 | goto nla_put_failure; |
| 103 | } |
| 104 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 105 | genlmsg_end(msg, hdr); |
| 106 | return 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 107 | |
| 108 | nla_put_failure: |
| 109 | genlmsg_cancel(msg, hdr); |
| 110 | return -EMSGSIZE; |
| 111 | } |
| 112 | |
| 113 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) |
| 114 | { |
| 115 | struct nfc_dev *dev; |
| 116 | int rc; |
| 117 | u32 idx; |
| 118 | |
| 119 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 120 | nfc_genl_family.attrbuf, |
| 121 | nfc_genl_family.maxattr, |
| 122 | nfc_genl_policy); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 123 | if (rc < 0) |
| 124 | return ERR_PTR(rc); |
| 125 | |
| 126 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) |
| 127 | return ERR_PTR(-EINVAL); |
| 128 | |
| 129 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); |
| 130 | |
| 131 | dev = nfc_get_device(idx); |
| 132 | if (!dev) |
| 133 | return ERR_PTR(-ENODEV); |
| 134 | |
| 135 | return dev; |
| 136 | } |
| 137 | |
| 138 | static int nfc_genl_dump_targets(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 139 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 140 | { |
| 141 | int i = cb->args[0]; |
| 142 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 143 | int rc; |
| 144 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 145 | if (!dev) { |
| 146 | dev = __get_device_from_cb(cb); |
| 147 | if (IS_ERR(dev)) |
| 148 | return PTR_ERR(dev); |
| 149 | |
| 150 | cb->args[1] = (long) dev; |
| 151 | } |
| 152 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 153 | device_lock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 154 | |
| 155 | cb->seq = dev->targets_generation; |
| 156 | |
| 157 | while (i < dev->n_targets) { |
| 158 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 159 | NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 160 | if (rc < 0) |
| 161 | break; |
| 162 | |
| 163 | i++; |
| 164 | } |
| 165 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 166 | device_unlock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 167 | |
| 168 | cb->args[0] = i; |
| 169 | |
| 170 | return skb->len; |
| 171 | } |
| 172 | |
| 173 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) |
| 174 | { |
| 175 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 176 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 177 | if (dev) |
| 178 | nfc_put_device(dev); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int nfc_genl_targets_found(struct nfc_dev *dev) |
| 184 | { |
| 185 | struct sk_buff *msg; |
| 186 | void *hdr; |
| 187 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 188 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 189 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 190 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 191 | if (!msg) |
| 192 | return -ENOMEM; |
| 193 | |
| 194 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 195 | NFC_EVENT_TARGETS_FOUND); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 196 | if (!hdr) |
| 197 | goto free_msg; |
| 198 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 199 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 200 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 201 | |
| 202 | genlmsg_end(msg, hdr); |
| 203 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 204 | return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 205 | |
| 206 | nla_put_failure: |
| 207 | genlmsg_cancel(msg, hdr); |
| 208 | free_msg: |
| 209 | nlmsg_free(msg); |
| 210 | return -EMSGSIZE; |
| 211 | } |
| 212 | |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 213 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 214 | { |
| 215 | struct sk_buff *msg; |
| 216 | void *hdr; |
| 217 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 218 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 219 | if (!msg) |
| 220 | return -ENOMEM; |
| 221 | |
| 222 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 223 | NFC_EVENT_TARGET_LOST); |
| 224 | if (!hdr) |
| 225 | goto free_msg; |
| 226 | |
John W. Linville | 59ef43e | 2012-04-18 14:17:13 -0400 | [diff] [blame] | 227 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 228 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 229 | goto nla_put_failure; |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 230 | |
| 231 | genlmsg_end(msg, hdr); |
| 232 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 233 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | |
| 237 | nla_put_failure: |
| 238 | genlmsg_cancel(msg, hdr); |
| 239 | free_msg: |
| 240 | nlmsg_free(msg); |
| 241 | return -EMSGSIZE; |
| 242 | } |
| 243 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 244 | int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) |
| 245 | { |
| 246 | struct sk_buff *msg; |
| 247 | void *hdr; |
| 248 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 249 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 250 | if (!msg) |
| 251 | return -ENOMEM; |
| 252 | |
| 253 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 254 | NFC_EVENT_TM_ACTIVATED); |
| 255 | if (!hdr) |
| 256 | goto free_msg; |
| 257 | |
| 258 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 259 | goto nla_put_failure; |
| 260 | if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) |
| 261 | goto nla_put_failure; |
| 262 | |
| 263 | genlmsg_end(msg, hdr); |
| 264 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 265 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | |
| 269 | nla_put_failure: |
| 270 | genlmsg_cancel(msg, hdr); |
| 271 | free_msg: |
| 272 | nlmsg_free(msg); |
| 273 | return -EMSGSIZE; |
| 274 | } |
| 275 | |
| 276 | int nfc_genl_tm_deactivated(struct nfc_dev *dev) |
| 277 | { |
| 278 | struct sk_buff *msg; |
| 279 | void *hdr; |
| 280 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 281 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 282 | if (!msg) |
| 283 | return -ENOMEM; |
| 284 | |
| 285 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 286 | NFC_EVENT_TM_DEACTIVATED); |
| 287 | if (!hdr) |
| 288 | goto free_msg; |
| 289 | |
| 290 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 291 | goto nla_put_failure; |
| 292 | |
| 293 | genlmsg_end(msg, hdr); |
| 294 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 295 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | |
| 299 | nla_put_failure: |
| 300 | genlmsg_cancel(msg, hdr); |
| 301 | free_msg: |
| 302 | nlmsg_free(msg); |
| 303 | return -EMSGSIZE; |
| 304 | } |
| 305 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 306 | int nfc_genl_device_added(struct nfc_dev *dev) |
| 307 | { |
| 308 | struct sk_buff *msg; |
| 309 | void *hdr; |
| 310 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 311 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 312 | if (!msg) |
| 313 | return -ENOMEM; |
| 314 | |
| 315 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 316 | NFC_EVENT_DEVICE_ADDED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 317 | if (!hdr) |
| 318 | goto free_msg; |
| 319 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 320 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 321 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 322 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
| 323 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) |
| 324 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 325 | |
| 326 | genlmsg_end(msg, hdr); |
| 327 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 328 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 329 | |
| 330 | return 0; |
| 331 | |
| 332 | nla_put_failure: |
| 333 | genlmsg_cancel(msg, hdr); |
| 334 | free_msg: |
| 335 | nlmsg_free(msg); |
| 336 | return -EMSGSIZE; |
| 337 | } |
| 338 | |
| 339 | int nfc_genl_device_removed(struct nfc_dev *dev) |
| 340 | { |
| 341 | struct sk_buff *msg; |
| 342 | void *hdr; |
| 343 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 344 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 345 | if (!msg) |
| 346 | return -ENOMEM; |
| 347 | |
| 348 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 349 | NFC_EVENT_DEVICE_REMOVED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 350 | if (!hdr) |
| 351 | goto free_msg; |
| 352 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 353 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 354 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 355 | |
| 356 | genlmsg_end(msg, hdr); |
| 357 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 358 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | |
| 362 | nla_put_failure: |
| 363 | genlmsg_cancel(msg, hdr); |
| 364 | free_msg: |
| 365 | nlmsg_free(msg); |
| 366 | return -EMSGSIZE; |
| 367 | } |
| 368 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 369 | int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list) |
| 370 | { |
| 371 | struct sk_buff *msg; |
| 372 | struct nlattr *sdp_attr, *uri_attr; |
| 373 | struct nfc_llcp_sdp_tlv *sdres; |
| 374 | struct hlist_node *n; |
| 375 | void *hdr; |
| 376 | int rc = -EMSGSIZE; |
| 377 | int i; |
| 378 | |
| 379 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 380 | if (!msg) |
| 381 | return -ENOMEM; |
| 382 | |
| 383 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 384 | NFC_EVENT_LLC_SDRES); |
| 385 | if (!hdr) |
| 386 | goto free_msg; |
| 387 | |
| 388 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 389 | goto nla_put_failure; |
| 390 | |
| 391 | sdp_attr = nla_nest_start(msg, NFC_ATTR_LLC_SDP); |
| 392 | if (sdp_attr == NULL) { |
| 393 | rc = -ENOMEM; |
| 394 | goto nla_put_failure; |
| 395 | } |
| 396 | |
| 397 | i = 1; |
| 398 | hlist_for_each_entry_safe(sdres, n, sdres_list, node) { |
| 399 | pr_debug("uri: %s, sap: %d\n", sdres->uri, sdres->sap); |
| 400 | |
| 401 | uri_attr = nla_nest_start(msg, i++); |
| 402 | if (uri_attr == NULL) { |
| 403 | rc = -ENOMEM; |
| 404 | goto nla_put_failure; |
| 405 | } |
| 406 | |
| 407 | if (nla_put_u8(msg, NFC_SDP_ATTR_SAP, sdres->sap)) |
| 408 | goto nla_put_failure; |
| 409 | |
| 410 | if (nla_put_string(msg, NFC_SDP_ATTR_URI, sdres->uri)) |
| 411 | goto nla_put_failure; |
| 412 | |
| 413 | nla_nest_end(msg, uri_attr); |
| 414 | |
| 415 | hlist_del(&sdres->node); |
| 416 | |
| 417 | nfc_llcp_free_sdp_tlv(sdres); |
| 418 | } |
| 419 | |
| 420 | nla_nest_end(msg, sdp_attr); |
| 421 | |
| 422 | genlmsg_end(msg, hdr); |
| 423 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 424 | return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 425 | |
| 426 | nla_put_failure: |
| 427 | genlmsg_cancel(msg, hdr); |
| 428 | |
| 429 | free_msg: |
| 430 | nlmsg_free(msg); |
| 431 | |
| 432 | nfc_llcp_free_sdp_tlv_list(sdres_list); |
| 433 | |
| 434 | return rc; |
| 435 | } |
| 436 | |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 437 | int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type) |
| 438 | { |
| 439 | struct sk_buff *msg; |
| 440 | void *hdr; |
| 441 | |
| 442 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 443 | if (!msg) |
| 444 | return -ENOMEM; |
| 445 | |
| 446 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 447 | NFC_EVENT_SE_ADDED); |
| 448 | if (!hdr) |
| 449 | goto free_msg; |
| 450 | |
| 451 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 452 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx) || |
| 453 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, type)) |
| 454 | goto nla_put_failure; |
| 455 | |
| 456 | genlmsg_end(msg, hdr); |
| 457 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 458 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 459 | |
| 460 | return 0; |
| 461 | |
| 462 | nla_put_failure: |
| 463 | genlmsg_cancel(msg, hdr); |
| 464 | free_msg: |
| 465 | nlmsg_free(msg); |
| 466 | return -EMSGSIZE; |
| 467 | } |
| 468 | |
| 469 | int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx) |
| 470 | { |
| 471 | struct sk_buff *msg; |
| 472 | void *hdr; |
| 473 | |
| 474 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 475 | if (!msg) |
| 476 | return -ENOMEM; |
| 477 | |
| 478 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 479 | NFC_EVENT_SE_REMOVED); |
| 480 | if (!hdr) |
| 481 | goto free_msg; |
| 482 | |
| 483 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 484 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se_idx)) |
| 485 | goto nla_put_failure; |
| 486 | |
| 487 | genlmsg_end(msg, hdr); |
| 488 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 489 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 2757c372 | 2013-05-10 15:47:37 +0200 | [diff] [blame] | 490 | |
| 491 | return 0; |
| 492 | |
| 493 | nla_put_failure: |
| 494 | genlmsg_cancel(msg, hdr); |
| 495 | free_msg: |
| 496 | nlmsg_free(msg); |
| 497 | return -EMSGSIZE; |
| 498 | } |
| 499 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 500 | 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] | 501 | u32 portid, u32 seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 502 | struct netlink_callback *cb, |
| 503 | int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 504 | { |
| 505 | void *hdr; |
| 506 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 507 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 508 | NFC_CMD_GET_DEVICE); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 509 | if (!hdr) |
| 510 | return -EMSGSIZE; |
| 511 | |
| 512 | if (cb) |
| 513 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 514 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 515 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 516 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 517 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
Thierry Escande | 7ad3939 | 2012-10-05 11:19:58 +0200 | [diff] [blame] | 518 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) || |
| 519 | nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode)) |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 520 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 521 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 522 | genlmsg_end(msg, hdr); |
| 523 | return 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 524 | |
| 525 | nla_put_failure: |
| 526 | genlmsg_cancel(msg, hdr); |
| 527 | return -EMSGSIZE; |
| 528 | } |
| 529 | |
| 530 | static int nfc_genl_dump_devices(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 531 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 532 | { |
| 533 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 534 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 535 | bool first_call = false; |
| 536 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 537 | if (!iter) { |
| 538 | first_call = true; |
| 539 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 540 | if (!iter) |
| 541 | return -ENOMEM; |
| 542 | cb->args[0] = (long) iter; |
| 543 | } |
| 544 | |
| 545 | mutex_lock(&nfc_devlist_mutex); |
| 546 | |
| 547 | cb->seq = nfc_devlist_generation; |
| 548 | |
| 549 | if (first_call) { |
| 550 | nfc_device_iter_init(iter); |
| 551 | dev = nfc_device_iter_next(iter); |
| 552 | } |
| 553 | |
| 554 | while (dev) { |
| 555 | int rc; |
| 556 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 557 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 558 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 559 | if (rc < 0) |
| 560 | break; |
| 561 | |
| 562 | dev = nfc_device_iter_next(iter); |
| 563 | } |
| 564 | |
| 565 | mutex_unlock(&nfc_devlist_mutex); |
| 566 | |
| 567 | cb->args[1] = (long) dev; |
| 568 | |
| 569 | return skb->len; |
| 570 | } |
| 571 | |
| 572 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) |
| 573 | { |
| 574 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 575 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 576 | nfc_device_iter_exit(iter); |
| 577 | kfree(iter); |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 582 | 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] | 583 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 584 | { |
| 585 | struct sk_buff *msg; |
| 586 | void *hdr; |
| 587 | |
| 588 | pr_debug("DEP link is up\n"); |
| 589 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 590 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 591 | if (!msg) |
| 592 | return -ENOMEM; |
| 593 | |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 594 | 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] | 595 | if (!hdr) |
| 596 | goto free_msg; |
| 597 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 598 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 599 | goto nla_put_failure; |
| 600 | if (rf_mode == NFC_RF_INITIATOR && |
| 601 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 602 | goto nla_put_failure; |
| 603 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || |
| 604 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) |
| 605 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 606 | |
| 607 | genlmsg_end(msg, hdr); |
| 608 | |
| 609 | dev->dep_link_up = true; |
| 610 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 611 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | |
| 615 | nla_put_failure: |
| 616 | genlmsg_cancel(msg, hdr); |
| 617 | free_msg: |
| 618 | nlmsg_free(msg); |
| 619 | return -EMSGSIZE; |
| 620 | } |
| 621 | |
| 622 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) |
| 623 | { |
| 624 | struct sk_buff *msg; |
| 625 | void *hdr; |
| 626 | |
| 627 | pr_debug("DEP link is down\n"); |
| 628 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 629 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 630 | if (!msg) |
| 631 | return -ENOMEM; |
| 632 | |
| 633 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 634 | NFC_CMD_DEP_LINK_DOWN); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 635 | if (!hdr) |
| 636 | goto free_msg; |
| 637 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 638 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 639 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 640 | |
| 641 | genlmsg_end(msg, hdr); |
| 642 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 643 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 644 | |
| 645 | return 0; |
| 646 | |
| 647 | nla_put_failure: |
| 648 | genlmsg_cancel(msg, hdr); |
| 649 | free_msg: |
| 650 | nlmsg_free(msg); |
| 651 | return -EMSGSIZE; |
| 652 | } |
| 653 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 654 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
| 655 | { |
| 656 | struct sk_buff *msg; |
| 657 | struct nfc_dev *dev; |
| 658 | u32 idx; |
| 659 | int rc = -ENOBUFS; |
| 660 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 661 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 662 | return -EINVAL; |
| 663 | |
| 664 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 665 | |
| 666 | dev = nfc_get_device(idx); |
| 667 | if (!dev) |
| 668 | return -ENODEV; |
| 669 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 670 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 671 | if (!msg) { |
| 672 | rc = -ENOMEM; |
| 673 | goto out_putdev; |
| 674 | } |
| 675 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 676 | 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] | 677 | NULL, 0); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 678 | if (rc < 0) |
| 679 | goto out_free; |
| 680 | |
| 681 | nfc_put_device(dev); |
| 682 | |
| 683 | return genlmsg_reply(msg, info); |
| 684 | |
| 685 | out_free: |
| 686 | nlmsg_free(msg); |
| 687 | out_putdev: |
| 688 | nfc_put_device(dev); |
| 689 | return rc; |
| 690 | } |
| 691 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 692 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
| 693 | { |
| 694 | struct nfc_dev *dev; |
| 695 | int rc; |
| 696 | u32 idx; |
| 697 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 698 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 699 | return -EINVAL; |
| 700 | |
| 701 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 702 | |
| 703 | dev = nfc_get_device(idx); |
| 704 | if (!dev) |
| 705 | return -ENODEV; |
| 706 | |
| 707 | rc = nfc_dev_up(dev); |
| 708 | |
| 709 | nfc_put_device(dev); |
| 710 | return rc; |
| 711 | } |
| 712 | |
| 713 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) |
| 714 | { |
| 715 | struct nfc_dev *dev; |
| 716 | int rc; |
| 717 | u32 idx; |
| 718 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 719 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 720 | return -EINVAL; |
| 721 | |
| 722 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 723 | |
| 724 | dev = nfc_get_device(idx); |
| 725 | if (!dev) |
| 726 | return -ENODEV; |
| 727 | |
| 728 | rc = nfc_dev_down(dev); |
| 729 | |
| 730 | nfc_put_device(dev); |
| 731 | return rc; |
| 732 | } |
| 733 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 734 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
| 735 | { |
| 736 | struct nfc_dev *dev; |
| 737 | int rc; |
| 738 | u32 idx; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 739 | u32 im_protocols = 0, tm_protocols = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 740 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 741 | pr_debug("Poll start\n"); |
| 742 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 743 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 744 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
| 745 | !info->attrs[NFC_ATTR_PROTOCOLS]) && |
Szymon Janc | 0f45077 | 2012-10-17 15:23:39 +0200 | [diff] [blame] | 746 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 747 | return -EINVAL; |
| 748 | |
| 749 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 750 | |
| 751 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) |
| 752 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 753 | |
| 754 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) |
| 755 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); |
Samuel Ortiz | 5e50ee3 | 2012-05-31 11:48:58 +0200 | [diff] [blame] | 756 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
| 757 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 758 | |
| 759 | dev = nfc_get_device(idx); |
| 760 | if (!dev) |
| 761 | return -ENODEV; |
| 762 | |
| 763 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 764 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 765 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 766 | if (!rc) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 767 | dev->genl_data.poll_req_portid = info->snd_portid; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 768 | |
| 769 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 770 | |
| 771 | nfc_put_device(dev); |
| 772 | return rc; |
| 773 | } |
| 774 | |
| 775 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) |
| 776 | { |
| 777 | struct nfc_dev *dev; |
| 778 | int rc; |
| 779 | u32 idx; |
| 780 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 781 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 782 | return -EINVAL; |
| 783 | |
| 784 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 785 | |
| 786 | dev = nfc_get_device(idx); |
| 787 | if (!dev) |
| 788 | return -ENODEV; |
| 789 | |
Samuel Ortiz | a831b91 | 2012-06-28 16:41:57 +0200 | [diff] [blame] | 790 | device_lock(&dev->dev); |
| 791 | |
| 792 | if (!dev->polling) { |
| 793 | device_unlock(&dev->dev); |
| 794 | return -EINVAL; |
| 795 | } |
| 796 | |
| 797 | device_unlock(&dev->dev); |
| 798 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 799 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 800 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 801 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 802 | rc = -EBUSY; |
| 803 | goto out; |
| 804 | } |
| 805 | |
| 806 | rc = nfc_stop_poll(dev); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 807 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 808 | |
| 809 | out: |
| 810 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 811 | nfc_put_device(dev); |
| 812 | return rc; |
| 813 | } |
| 814 | |
Christophe Ricard | 3682f49 | 2014-12-02 21:27:50 +0100 | [diff] [blame] | 815 | static int nfc_genl_activate_target(struct sk_buff *skb, struct genl_info *info) |
| 816 | { |
| 817 | struct nfc_dev *dev; |
| 818 | u32 device_idx, target_idx, protocol; |
| 819 | int rc; |
| 820 | |
| 821 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 822 | return -EINVAL; |
| 823 | |
| 824 | device_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 825 | |
| 826 | dev = nfc_get_device(device_idx); |
| 827 | if (!dev) |
| 828 | return -ENODEV; |
| 829 | |
| 830 | target_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 831 | protocol = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
| 832 | |
| 833 | nfc_deactivate_target(dev, target_idx); |
| 834 | rc = nfc_activate_target(dev, target_idx, protocol); |
| 835 | |
| 836 | nfc_put_device(dev); |
| 837 | return 0; |
| 838 | } |
| 839 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 840 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
| 841 | { |
| 842 | struct nfc_dev *dev; |
| 843 | int rc, tgt_idx; |
| 844 | u32 idx; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 845 | u8 comm; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 846 | |
| 847 | pr_debug("DEP link up\n"); |
| 848 | |
| 849 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 850 | !info->attrs[NFC_ATTR_COMM_MODE]) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 851 | return -EINVAL; |
| 852 | |
| 853 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 854 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) |
| 855 | tgt_idx = NFC_TARGET_IDX_ANY; |
| 856 | else |
| 857 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 858 | |
| 859 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 860 | |
| 861 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) |
| 862 | return -EINVAL; |
| 863 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 864 | dev = nfc_get_device(idx); |
| 865 | if (!dev) |
| 866 | return -ENODEV; |
| 867 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 868 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 869 | |
| 870 | nfc_put_device(dev); |
| 871 | |
| 872 | return rc; |
| 873 | } |
| 874 | |
| 875 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) |
| 876 | { |
| 877 | struct nfc_dev *dev; |
| 878 | int rc; |
| 879 | u32 idx; |
| 880 | |
| 881 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 882 | return -EINVAL; |
| 883 | |
| 884 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 885 | |
| 886 | dev = nfc_get_device(idx); |
| 887 | if (!dev) |
| 888 | return -ENODEV; |
| 889 | |
| 890 | rc = nfc_dep_link_down(dev); |
| 891 | |
| 892 | nfc_put_device(dev); |
| 893 | return rc; |
| 894 | } |
| 895 | |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 896 | static int nfc_genl_send_params(struct sk_buff *msg, |
| 897 | struct nfc_llcp_local *local, |
| 898 | u32 portid, u32 seq) |
| 899 | { |
| 900 | void *hdr; |
| 901 | |
| 902 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0, |
| 903 | NFC_CMD_LLC_GET_PARAMS); |
| 904 | if (!hdr) |
| 905 | return -EMSGSIZE; |
| 906 | |
| 907 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) || |
| 908 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) || |
| 909 | nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) || |
| 910 | nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux))) |
| 911 | goto nla_put_failure; |
| 912 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 913 | genlmsg_end(msg, hdr); |
| 914 | return 0; |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 915 | |
| 916 | nla_put_failure: |
| 917 | |
| 918 | genlmsg_cancel(msg, hdr); |
| 919 | return -EMSGSIZE; |
| 920 | } |
| 921 | |
| 922 | static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info) |
| 923 | { |
| 924 | struct nfc_dev *dev; |
| 925 | struct nfc_llcp_local *local; |
| 926 | int rc = 0; |
| 927 | struct sk_buff *msg = NULL; |
| 928 | u32 idx; |
| 929 | |
| 930 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 931 | return -EINVAL; |
| 932 | |
| 933 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 934 | |
| 935 | dev = nfc_get_device(idx); |
| 936 | if (!dev) |
| 937 | return -ENODEV; |
| 938 | |
| 939 | device_lock(&dev->dev); |
| 940 | |
| 941 | local = nfc_llcp_find_local(dev); |
| 942 | if (!local) { |
| 943 | rc = -ENODEV; |
| 944 | goto exit; |
| 945 | } |
| 946 | |
| 947 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 948 | if (!msg) { |
| 949 | rc = -ENOMEM; |
| 950 | goto exit; |
| 951 | } |
| 952 | |
| 953 | rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq); |
| 954 | |
| 955 | exit: |
| 956 | device_unlock(&dev->dev); |
| 957 | |
| 958 | nfc_put_device(dev); |
| 959 | |
| 960 | if (rc < 0) { |
| 961 | if (msg) |
| 962 | nlmsg_free(msg); |
| 963 | |
| 964 | return rc; |
| 965 | } |
| 966 | |
| 967 | return genlmsg_reply(msg, info); |
| 968 | } |
| 969 | |
| 970 | static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info) |
| 971 | { |
| 972 | struct nfc_dev *dev; |
| 973 | struct nfc_llcp_local *local; |
| 974 | u8 rw = 0; |
| 975 | u16 miux = 0; |
| 976 | u32 idx; |
| 977 | int rc = 0; |
| 978 | |
| 979 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 980 | (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] && |
| 981 | !info->attrs[NFC_ATTR_LLC_PARAM_RW] && |
| 982 | !info->attrs[NFC_ATTR_LLC_PARAM_MIUX])) |
| 983 | return -EINVAL; |
| 984 | |
| 985 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) { |
| 986 | rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]); |
| 987 | |
| 988 | if (rw > LLCP_MAX_RW) |
| 989 | return -EINVAL; |
| 990 | } |
| 991 | |
| 992 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) { |
| 993 | miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]); |
| 994 | |
| 995 | if (miux > LLCP_MAX_MIUX) |
| 996 | return -EINVAL; |
| 997 | } |
| 998 | |
| 999 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1000 | |
| 1001 | dev = nfc_get_device(idx); |
| 1002 | if (!dev) |
| 1003 | return -ENODEV; |
| 1004 | |
| 1005 | device_lock(&dev->dev); |
| 1006 | |
| 1007 | local = nfc_llcp_find_local(dev); |
| 1008 | if (!local) { |
| 1009 | nfc_put_device(dev); |
| 1010 | rc = -ENODEV; |
| 1011 | goto exit; |
| 1012 | } |
| 1013 | |
| 1014 | if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) { |
| 1015 | if (dev->dep_link_up) { |
| 1016 | rc = -EINPROGRESS; |
| 1017 | goto exit; |
| 1018 | } |
| 1019 | |
| 1020 | local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]); |
| 1021 | } |
| 1022 | |
| 1023 | if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) |
| 1024 | local->rw = rw; |
| 1025 | |
| 1026 | if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) |
| 1027 | local->miux = cpu_to_be16(miux); |
| 1028 | |
| 1029 | exit: |
| 1030 | device_unlock(&dev->dev); |
| 1031 | |
| 1032 | nfc_put_device(dev); |
| 1033 | |
| 1034 | return rc; |
| 1035 | } |
| 1036 | |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1037 | static int nfc_genl_llc_sdreq(struct sk_buff *skb, struct genl_info *info) |
| 1038 | { |
| 1039 | struct nfc_dev *dev; |
| 1040 | struct nfc_llcp_local *local; |
| 1041 | struct nlattr *attr, *sdp_attrs[NFC_SDP_ATTR_MAX+1]; |
| 1042 | u32 idx; |
| 1043 | u8 tid; |
| 1044 | char *uri; |
| 1045 | int rc = 0, rem; |
| 1046 | size_t uri_len, tlvs_len; |
| 1047 | struct hlist_head sdreq_list; |
| 1048 | struct nfc_llcp_sdp_tlv *sdreq; |
| 1049 | |
| 1050 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1051 | !info->attrs[NFC_ATTR_LLC_SDP]) |
| 1052 | return -EINVAL; |
| 1053 | |
| 1054 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1055 | |
| 1056 | dev = nfc_get_device(idx); |
| 1057 | if (!dev) { |
| 1058 | rc = -ENODEV; |
| 1059 | goto exit; |
| 1060 | } |
| 1061 | |
| 1062 | device_lock(&dev->dev); |
| 1063 | |
| 1064 | if (dev->dep_link_up == false) { |
| 1065 | rc = -ENOLINK; |
| 1066 | goto exit; |
| 1067 | } |
| 1068 | |
| 1069 | local = nfc_llcp_find_local(dev); |
| 1070 | if (!local) { |
| 1071 | nfc_put_device(dev); |
| 1072 | rc = -ENODEV; |
| 1073 | goto exit; |
| 1074 | } |
| 1075 | |
| 1076 | INIT_HLIST_HEAD(&sdreq_list); |
| 1077 | |
| 1078 | tlvs_len = 0; |
| 1079 | |
| 1080 | nla_for_each_nested(attr, info->attrs[NFC_ATTR_LLC_SDP], rem) { |
| 1081 | rc = nla_parse_nested(sdp_attrs, NFC_SDP_ATTR_MAX, attr, |
| 1082 | nfc_sdp_genl_policy); |
| 1083 | |
| 1084 | if (rc != 0) { |
| 1085 | rc = -EINVAL; |
| 1086 | goto exit; |
| 1087 | } |
| 1088 | |
| 1089 | if (!sdp_attrs[NFC_SDP_ATTR_URI]) |
| 1090 | continue; |
| 1091 | |
| 1092 | uri_len = nla_len(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 1093 | if (uri_len == 0) |
| 1094 | continue; |
| 1095 | |
| 1096 | uri = nla_data(sdp_attrs[NFC_SDP_ATTR_URI]); |
| 1097 | if (uri == NULL || *uri == 0) |
| 1098 | continue; |
| 1099 | |
| 1100 | tid = local->sdreq_next_tid++; |
| 1101 | |
| 1102 | sdreq = nfc_llcp_build_sdreq_tlv(tid, uri, uri_len); |
| 1103 | if (sdreq == NULL) { |
| 1104 | rc = -ENOMEM; |
| 1105 | goto exit; |
| 1106 | } |
| 1107 | |
| 1108 | tlvs_len += sdreq->tlv_len; |
| 1109 | |
| 1110 | hlist_add_head(&sdreq->node, &sdreq_list); |
| 1111 | } |
| 1112 | |
| 1113 | if (hlist_empty(&sdreq_list)) { |
| 1114 | rc = -EINVAL; |
| 1115 | goto exit; |
| 1116 | } |
| 1117 | |
| 1118 | rc = nfc_llcp_send_snl_sdreq(local, &sdreq_list, tlvs_len); |
| 1119 | exit: |
| 1120 | device_unlock(&dev->dev); |
| 1121 | |
| 1122 | nfc_put_device(dev); |
| 1123 | |
| 1124 | return rc; |
| 1125 | } |
| 1126 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1127 | 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] | 1128 | { |
| 1129 | struct nfc_dev *dev; |
| 1130 | int rc; |
| 1131 | u32 idx; |
| 1132 | char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1]; |
| 1133 | |
| 1134 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 1135 | return -EINVAL; |
| 1136 | |
| 1137 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1138 | |
| 1139 | dev = nfc_get_device(idx); |
| 1140 | if (!dev) |
| 1141 | return -ENODEV; |
| 1142 | |
| 1143 | nla_strlcpy(firmware_name, info->attrs[NFC_ATTR_FIRMWARE_NAME], |
| 1144 | sizeof(firmware_name)); |
| 1145 | |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1146 | rc = nfc_fw_download(dev, firmware_name); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1147 | |
| 1148 | nfc_put_device(dev); |
| 1149 | return rc; |
| 1150 | } |
| 1151 | |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 1152 | int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name, |
| 1153 | u32 result) |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1154 | { |
| 1155 | struct sk_buff *msg; |
| 1156 | void *hdr; |
| 1157 | |
| 1158 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1159 | if (!msg) |
| 1160 | return -ENOMEM; |
| 1161 | |
| 1162 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1163 | NFC_CMD_FW_DOWNLOAD); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1164 | if (!hdr) |
| 1165 | goto free_msg; |
| 1166 | |
| 1167 | if (nla_put_string(msg, NFC_ATTR_FIRMWARE_NAME, firmware_name) || |
Eric Lapuyade | 352a5f5 | 2013-07-19 14:57:55 +0200 | [diff] [blame] | 1168 | nla_put_u32(msg, NFC_ATTR_FIRMWARE_DOWNLOAD_STATUS, result) || |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1169 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 1170 | goto nla_put_failure; |
| 1171 | |
| 1172 | genlmsg_end(msg, hdr); |
| 1173 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1174 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1175 | |
| 1176 | return 0; |
| 1177 | |
| 1178 | nla_put_failure: |
| 1179 | genlmsg_cancel(msg, hdr); |
| 1180 | free_msg: |
| 1181 | nlmsg_free(msg); |
| 1182 | return -EMSGSIZE; |
| 1183 | } |
| 1184 | |
Samuel Ortiz | be08565 | 2013-05-10 17:07:32 +0200 | [diff] [blame] | 1185 | static int nfc_genl_enable_se(struct sk_buff *skb, struct genl_info *info) |
| 1186 | { |
| 1187 | struct nfc_dev *dev; |
| 1188 | int rc; |
| 1189 | u32 idx, se_idx; |
| 1190 | |
| 1191 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1192 | !info->attrs[NFC_ATTR_SE_INDEX]) |
| 1193 | return -EINVAL; |
| 1194 | |
| 1195 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1196 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1197 | |
| 1198 | dev = nfc_get_device(idx); |
| 1199 | if (!dev) |
| 1200 | return -ENODEV; |
| 1201 | |
| 1202 | rc = nfc_enable_se(dev, se_idx); |
| 1203 | |
| 1204 | nfc_put_device(dev); |
| 1205 | return rc; |
| 1206 | } |
| 1207 | |
| 1208 | static int nfc_genl_disable_se(struct sk_buff *skb, struct genl_info *info) |
| 1209 | { |
| 1210 | struct nfc_dev *dev; |
| 1211 | int rc; |
| 1212 | u32 idx, se_idx; |
| 1213 | |
| 1214 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1215 | !info->attrs[NFC_ATTR_SE_INDEX]) |
| 1216 | return -EINVAL; |
| 1217 | |
| 1218 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1219 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1220 | |
| 1221 | dev = nfc_get_device(idx); |
| 1222 | if (!dev) |
| 1223 | return -ENODEV; |
| 1224 | |
| 1225 | rc = nfc_disable_se(dev, se_idx); |
| 1226 | |
| 1227 | nfc_put_device(dev); |
| 1228 | return rc; |
| 1229 | } |
| 1230 | |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1231 | static int nfc_genl_send_se(struct sk_buff *msg, struct nfc_dev *dev, |
| 1232 | u32 portid, u32 seq, |
| 1233 | struct netlink_callback *cb, |
| 1234 | int flags) |
| 1235 | { |
| 1236 | void *hdr; |
| 1237 | struct nfc_se *se, *n; |
| 1238 | |
| 1239 | list_for_each_entry_safe(se, n, &dev->secure_elements, list) { |
| 1240 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
| 1241 | NFC_CMD_GET_SE); |
| 1242 | if (!hdr) |
| 1243 | goto nla_put_failure; |
| 1244 | |
| 1245 | if (cb) |
| 1246 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 1247 | |
| 1248 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 1249 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, se->idx) || |
| 1250 | nla_put_u8(msg, NFC_ATTR_SE_TYPE, se->type)) |
| 1251 | goto nla_put_failure; |
| 1252 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1253 | genlmsg_end(msg, hdr); |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | return 0; |
| 1257 | |
| 1258 | nla_put_failure: |
| 1259 | genlmsg_cancel(msg, hdr); |
| 1260 | return -EMSGSIZE; |
| 1261 | } |
| 1262 | |
| 1263 | static int nfc_genl_dump_ses(struct sk_buff *skb, |
| 1264 | struct netlink_callback *cb) |
| 1265 | { |
| 1266 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 1267 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 1268 | bool first_call = false; |
| 1269 | |
| 1270 | if (!iter) { |
| 1271 | first_call = true; |
| 1272 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 1273 | if (!iter) |
| 1274 | return -ENOMEM; |
| 1275 | cb->args[0] = (long) iter; |
| 1276 | } |
| 1277 | |
| 1278 | mutex_lock(&nfc_devlist_mutex); |
| 1279 | |
| 1280 | cb->seq = nfc_devlist_generation; |
| 1281 | |
| 1282 | if (first_call) { |
| 1283 | nfc_device_iter_init(iter); |
| 1284 | dev = nfc_device_iter_next(iter); |
| 1285 | } |
| 1286 | |
| 1287 | while (dev) { |
| 1288 | int rc; |
| 1289 | |
| 1290 | rc = nfc_genl_send_se(skb, dev, NETLINK_CB(cb->skb).portid, |
| 1291 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
| 1292 | if (rc < 0) |
| 1293 | break; |
| 1294 | |
| 1295 | dev = nfc_device_iter_next(iter); |
| 1296 | } |
| 1297 | |
| 1298 | mutex_unlock(&nfc_devlist_mutex); |
| 1299 | |
| 1300 | cb->args[1] = (long) dev; |
| 1301 | |
| 1302 | return skb->len; |
| 1303 | } |
| 1304 | |
| 1305 | static int nfc_genl_dump_ses_done(struct netlink_callback *cb) |
| 1306 | { |
| 1307 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 1308 | |
| 1309 | nfc_device_iter_exit(iter); |
| 1310 | kfree(iter); |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
Christophe Ricard | cd96db6 | 2014-12-02 21:27:51 +0100 | [diff] [blame] | 1315 | static int nfc_se_io(struct nfc_dev *dev, u32 se_idx, |
| 1316 | u8 *apdu, size_t apdu_length, |
| 1317 | se_io_cb_t cb, void *cb_context) |
| 1318 | { |
| 1319 | struct nfc_se *se; |
| 1320 | int rc; |
| 1321 | |
| 1322 | pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); |
| 1323 | |
| 1324 | device_lock(&dev->dev); |
| 1325 | |
| 1326 | if (!device_is_registered(&dev->dev)) { |
| 1327 | rc = -ENODEV; |
| 1328 | goto error; |
| 1329 | } |
| 1330 | |
| 1331 | if (!dev->dev_up) { |
| 1332 | rc = -ENODEV; |
| 1333 | goto error; |
| 1334 | } |
| 1335 | |
| 1336 | if (!dev->ops->se_io) { |
| 1337 | rc = -EOPNOTSUPP; |
| 1338 | goto error; |
| 1339 | } |
| 1340 | |
| 1341 | se = nfc_find_se(dev, se_idx); |
| 1342 | if (!se) { |
| 1343 | rc = -EINVAL; |
| 1344 | goto error; |
| 1345 | } |
| 1346 | |
| 1347 | if (se->state != NFC_SE_ENABLED) { |
| 1348 | rc = -ENODEV; |
| 1349 | goto error; |
| 1350 | } |
| 1351 | |
| 1352 | rc = dev->ops->se_io(dev, se_idx, apdu, |
| 1353 | apdu_length, cb, cb_context); |
| 1354 | |
| 1355 | error: |
| 1356 | device_unlock(&dev->dev); |
| 1357 | return rc; |
| 1358 | } |
| 1359 | |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1360 | struct se_io_ctx { |
| 1361 | u32 dev_idx; |
| 1362 | u32 se_idx; |
| 1363 | }; |
| 1364 | |
Samuel Ortiz | ddc1a70 | 2013-10-07 14:18:44 +0200 | [diff] [blame] | 1365 | 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] | 1366 | { |
| 1367 | struct se_io_ctx *ctx = context; |
| 1368 | struct sk_buff *msg; |
| 1369 | void *hdr; |
| 1370 | |
| 1371 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1372 | if (!msg) { |
| 1373 | kfree(ctx); |
| 1374 | return; |
| 1375 | } |
| 1376 | |
| 1377 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 1378 | NFC_CMD_SE_IO); |
| 1379 | if (!hdr) |
| 1380 | goto free_msg; |
| 1381 | |
| 1382 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) || |
| 1383 | nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) || |
| 1384 | nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu)) |
| 1385 | goto nla_put_failure; |
| 1386 | |
| 1387 | genlmsg_end(msg, hdr); |
| 1388 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1389 | genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL); |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1390 | |
| 1391 | kfree(ctx); |
| 1392 | |
| 1393 | return; |
| 1394 | |
| 1395 | nla_put_failure: |
| 1396 | genlmsg_cancel(msg, hdr); |
| 1397 | free_msg: |
| 1398 | nlmsg_free(msg); |
| 1399 | kfree(ctx); |
| 1400 | |
| 1401 | return; |
| 1402 | } |
| 1403 | |
| 1404 | static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info) |
| 1405 | { |
| 1406 | struct nfc_dev *dev; |
| 1407 | struct se_io_ctx *ctx; |
| 1408 | u32 dev_idx, se_idx; |
| 1409 | u8 *apdu; |
| 1410 | size_t apdu_len; |
| 1411 | |
| 1412 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
| 1413 | !info->attrs[NFC_ATTR_SE_INDEX] || |
| 1414 | !info->attrs[NFC_ATTR_SE_APDU]) |
| 1415 | return -EINVAL; |
| 1416 | |
| 1417 | dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 1418 | se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]); |
| 1419 | |
| 1420 | dev = nfc_get_device(dev_idx); |
| 1421 | if (!dev) |
| 1422 | return -ENODEV; |
| 1423 | |
| 1424 | if (!dev->ops || !dev->ops->se_io) |
| 1425 | return -ENOTSUPP; |
| 1426 | |
| 1427 | apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]); |
| 1428 | if (apdu_len == 0) |
| 1429 | return -EINVAL; |
| 1430 | |
| 1431 | apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]); |
| 1432 | if (!apdu) |
| 1433 | return -EINVAL; |
| 1434 | |
| 1435 | ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL); |
| 1436 | if (!ctx) |
| 1437 | return -ENOMEM; |
| 1438 | |
| 1439 | ctx->dev_idx = dev_idx; |
| 1440 | ctx->se_idx = se_idx; |
| 1441 | |
Christophe Ricard | cd96db6 | 2014-12-02 21:27:51 +0100 | [diff] [blame] | 1442 | return nfc_se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx); |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 1445 | static const struct genl_ops nfc_genl_ops[] = { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1446 | { |
| 1447 | .cmd = NFC_CMD_GET_DEVICE, |
| 1448 | .doit = nfc_genl_get_device, |
| 1449 | .dumpit = nfc_genl_dump_devices, |
| 1450 | .done = nfc_genl_dump_devices_done, |
| 1451 | .policy = nfc_genl_policy, |
| 1452 | }, |
| 1453 | { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 1454 | .cmd = NFC_CMD_DEV_UP, |
| 1455 | .doit = nfc_genl_dev_up, |
| 1456 | .policy = nfc_genl_policy, |
| 1457 | }, |
| 1458 | { |
| 1459 | .cmd = NFC_CMD_DEV_DOWN, |
| 1460 | .doit = nfc_genl_dev_down, |
| 1461 | .policy = nfc_genl_policy, |
| 1462 | }, |
| 1463 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1464 | .cmd = NFC_CMD_START_POLL, |
| 1465 | .doit = nfc_genl_start_poll, |
| 1466 | .policy = nfc_genl_policy, |
| 1467 | }, |
| 1468 | { |
| 1469 | .cmd = NFC_CMD_STOP_POLL, |
| 1470 | .doit = nfc_genl_stop_poll, |
| 1471 | .policy = nfc_genl_policy, |
| 1472 | }, |
| 1473 | { |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 1474 | .cmd = NFC_CMD_DEP_LINK_UP, |
| 1475 | .doit = nfc_genl_dep_link_up, |
| 1476 | .policy = nfc_genl_policy, |
| 1477 | }, |
| 1478 | { |
| 1479 | .cmd = NFC_CMD_DEP_LINK_DOWN, |
| 1480 | .doit = nfc_genl_dep_link_down, |
| 1481 | .policy = nfc_genl_policy, |
| 1482 | }, |
| 1483 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1484 | .cmd = NFC_CMD_GET_TARGET, |
| 1485 | .dumpit = nfc_genl_dump_targets, |
| 1486 | .done = nfc_genl_dump_targets_done, |
| 1487 | .policy = nfc_genl_policy, |
| 1488 | }, |
Thierry Escande | 52feb44 | 2012-10-17 14:43:39 +0200 | [diff] [blame] | 1489 | { |
| 1490 | .cmd = NFC_CMD_LLC_GET_PARAMS, |
| 1491 | .doit = nfc_genl_llc_get_params, |
| 1492 | .policy = nfc_genl_policy, |
| 1493 | }, |
| 1494 | { |
| 1495 | .cmd = NFC_CMD_LLC_SET_PARAMS, |
| 1496 | .doit = nfc_genl_llc_set_params, |
| 1497 | .policy = nfc_genl_policy, |
| 1498 | }, |
Thierry Escande | d9b8d8e | 2013-02-15 10:43:06 +0100 | [diff] [blame] | 1499 | { |
| 1500 | .cmd = NFC_CMD_LLC_SDREQ, |
| 1501 | .doit = nfc_genl_llc_sdreq, |
| 1502 | .policy = nfc_genl_policy, |
| 1503 | }, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1504 | { |
Samuel Ortiz | 9ea7187 | 2013-07-31 01:19:43 +0200 | [diff] [blame] | 1505 | .cmd = NFC_CMD_FW_DOWNLOAD, |
| 1506 | .doit = nfc_genl_fw_download, |
Eric Lapuyade | 9674da8 | 2013-04-29 17:13:27 +0200 | [diff] [blame] | 1507 | .policy = nfc_genl_policy, |
| 1508 | }, |
Samuel Ortiz | be08565 | 2013-05-10 17:07:32 +0200 | [diff] [blame] | 1509 | { |
| 1510 | .cmd = NFC_CMD_ENABLE_SE, |
| 1511 | .doit = nfc_genl_enable_se, |
| 1512 | .policy = nfc_genl_policy, |
| 1513 | }, |
| 1514 | { |
| 1515 | .cmd = NFC_CMD_DISABLE_SE, |
| 1516 | .doit = nfc_genl_disable_se, |
| 1517 | .policy = nfc_genl_policy, |
| 1518 | }, |
Samuel Ortiz | ac22ac4 | 2013-07-24 18:10:50 +0200 | [diff] [blame] | 1519 | { |
| 1520 | .cmd = NFC_CMD_GET_SE, |
| 1521 | .dumpit = nfc_genl_dump_ses, |
| 1522 | .done = nfc_genl_dump_ses_done, |
| 1523 | .policy = nfc_genl_policy, |
| 1524 | }, |
Samuel Ortiz | 5ce3f32 | 2013-08-28 00:47:24 +0200 | [diff] [blame] | 1525 | { |
| 1526 | .cmd = NFC_CMD_SE_IO, |
| 1527 | .doit = nfc_genl_se_io, |
| 1528 | .policy = nfc_genl_policy, |
| 1529 | }, |
Christophe Ricard | 3682f49 | 2014-12-02 21:27:50 +0100 | [diff] [blame] | 1530 | { |
| 1531 | .cmd = NFC_CMD_ACTIVATE_TARGET, |
| 1532 | .doit = nfc_genl_activate_target, |
| 1533 | .policy = nfc_genl_policy, |
| 1534 | }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1535 | }; |
| 1536 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1537 | |
| 1538 | struct urelease_work { |
| 1539 | struct work_struct w; |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1540 | int portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1541 | }; |
| 1542 | |
| 1543 | static void nfc_urelease_event_work(struct work_struct *work) |
| 1544 | { |
| 1545 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
| 1546 | struct class_dev_iter iter; |
| 1547 | struct nfc_dev *dev; |
| 1548 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1549 | pr_debug("portid %d\n", w->portid); |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1550 | |
| 1551 | mutex_lock(&nfc_devlist_mutex); |
| 1552 | |
| 1553 | nfc_device_iter_init(&iter); |
| 1554 | dev = nfc_device_iter_next(&iter); |
| 1555 | |
| 1556 | while (dev) { |
| 1557 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 1558 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1559 | if (dev->genl_data.poll_req_portid == w->portid) { |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1560 | nfc_stop_poll(dev); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1561 | dev->genl_data.poll_req_portid = 0; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 1565 | |
| 1566 | dev = nfc_device_iter_next(&iter); |
| 1567 | } |
| 1568 | |
| 1569 | nfc_device_iter_exit(&iter); |
| 1570 | |
| 1571 | mutex_unlock(&nfc_devlist_mutex); |
| 1572 | |
| 1573 | kfree(w); |
| 1574 | } |
| 1575 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1576 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 1577 | unsigned long event, void *ptr) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1578 | { |
| 1579 | struct netlink_notify *n = ptr; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1580 | struct urelease_work *w; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1581 | |
| 1582 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) |
| 1583 | goto out; |
| 1584 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1585 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1586 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1587 | w = kmalloc(sizeof(*w), GFP_ATOMIC); |
| 1588 | if (w) { |
| 1589 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 1590 | w->portid = n->portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 1591 | schedule_work((struct work_struct *) w); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1592 | } |
| 1593 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1594 | out: |
| 1595 | return NOTIFY_DONE; |
| 1596 | } |
| 1597 | |
| 1598 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) |
| 1599 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1600 | genl_data->poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1601 | mutex_init(&genl_data->genl_data_mutex); |
| 1602 | } |
| 1603 | |
| 1604 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) |
| 1605 | { |
| 1606 | mutex_destroy(&genl_data->genl_data_mutex); |
| 1607 | } |
| 1608 | |
| 1609 | static struct notifier_block nl_notifier = { |
| 1610 | .notifier_call = nfc_genl_rcv_nl_event, |
| 1611 | }; |
| 1612 | |
| 1613 | /** |
| 1614 | * nfc_genl_init() - Initialize netlink interface |
| 1615 | * |
| 1616 | * This initialization function registers the nfc netlink family. |
| 1617 | */ |
| 1618 | int __init nfc_genl_init(void) |
| 1619 | { |
| 1620 | int rc; |
| 1621 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1622 | rc = genl_register_family_with_ops_groups(&nfc_genl_family, |
| 1623 | nfc_genl_ops, |
| 1624 | nfc_genl_mcgrps); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1625 | if (rc) |
| 1626 | return rc; |
| 1627 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1628 | netlink_register_notifier(&nl_notifier); |
| 1629 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1630 | return 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * nfc_genl_exit() - Deinitialize netlink interface |
| 1635 | * |
| 1636 | * This exit function unregisters the nfc netlink family. |
| 1637 | */ |
| 1638 | void nfc_genl_exit(void) |
| 1639 | { |
| 1640 | netlink_unregister_notifier(&nl_notifier); |
| 1641 | genl_unregister_family(&nfc_genl_family); |
| 1642 | } |