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