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 | |
| 32 | static struct genl_multicast_group nfc_genl_event_mcgrp = { |
| 33 | .name = NFC_GENL_MCAST_EVENT_NAME, |
| 34 | }; |
| 35 | |
H Hartley Sweeten | e5fe4cf | 2012-05-07 12:31:28 +0200 | [diff] [blame] | 36 | static struct genl_family nfc_genl_family = { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 37 | .id = GENL_ID_GENERATE, |
| 38 | .hdrsize = 0, |
| 39 | .name = NFC_GENL_NAME, |
| 40 | .version = NFC_GENL_VERSION, |
| 41 | .maxattr = NFC_ATTR_MAX, |
| 42 | }; |
| 43 | |
| 44 | static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = { |
| 45 | [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 }, |
| 46 | [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING, |
| 47 | .len = NFC_DEVICE_NAME_MAXSIZE }, |
| 48 | [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 }, |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 49 | [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 }, |
| 50 | [NFC_ATTR_RF_MODE] = { .type = NLA_U8 }, |
Samuel Ortiz | c970a1a | 2012-03-05 01:03:34 +0100 | [diff] [blame] | 51 | [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 }, |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 52 | [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 }, |
| 53 | [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | 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] | 57 | struct netlink_callback *cb, int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 58 | { |
| 59 | void *hdr; |
| 60 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 61 | 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] | 62 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 63 | if (!hdr) |
| 64 | return -EMSGSIZE; |
| 65 | |
| 66 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 67 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 68 | if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) || |
| 69 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) || |
| 70 | nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) || |
| 71 | nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res)) |
| 72 | goto nla_put_failure; |
| 73 | if (target->nfcid1_len > 0 && |
| 74 | nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, |
| 75 | target->nfcid1)) |
| 76 | goto nla_put_failure; |
| 77 | if (target->sensb_res_len > 0 && |
| 78 | nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, |
| 79 | target->sensb_res)) |
| 80 | goto nla_put_failure; |
| 81 | if (target->sensf_res_len > 0 && |
| 82 | nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, |
| 83 | target->sensf_res)) |
| 84 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 85 | |
| 86 | return genlmsg_end(msg, hdr); |
| 87 | |
| 88 | nla_put_failure: |
| 89 | genlmsg_cancel(msg, hdr); |
| 90 | return -EMSGSIZE; |
| 91 | } |
| 92 | |
| 93 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) |
| 94 | { |
| 95 | struct nfc_dev *dev; |
| 96 | int rc; |
| 97 | u32 idx; |
| 98 | |
| 99 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 100 | nfc_genl_family.attrbuf, |
| 101 | nfc_genl_family.maxattr, |
| 102 | nfc_genl_policy); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 103 | if (rc < 0) |
| 104 | return ERR_PTR(rc); |
| 105 | |
| 106 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) |
| 107 | return ERR_PTR(-EINVAL); |
| 108 | |
| 109 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); |
| 110 | |
| 111 | dev = nfc_get_device(idx); |
| 112 | if (!dev) |
| 113 | return ERR_PTR(-ENODEV); |
| 114 | |
| 115 | return dev; |
| 116 | } |
| 117 | |
| 118 | static int nfc_genl_dump_targets(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 119 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 120 | { |
| 121 | int i = cb->args[0]; |
| 122 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 123 | int rc; |
| 124 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 125 | if (!dev) { |
| 126 | dev = __get_device_from_cb(cb); |
| 127 | if (IS_ERR(dev)) |
| 128 | return PTR_ERR(dev); |
| 129 | |
| 130 | cb->args[1] = (long) dev; |
| 131 | } |
| 132 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 133 | device_lock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 134 | |
| 135 | cb->seq = dev->targets_generation; |
| 136 | |
| 137 | while (i < dev->n_targets) { |
| 138 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 139 | NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 140 | if (rc < 0) |
| 141 | break; |
| 142 | |
| 143 | i++; |
| 144 | } |
| 145 | |
Eric Lapuyade | d4ccb13 | 2012-05-07 12:31:15 +0200 | [diff] [blame] | 146 | device_unlock(&dev->dev); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 147 | |
| 148 | cb->args[0] = i; |
| 149 | |
| 150 | return skb->len; |
| 151 | } |
| 152 | |
| 153 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) |
| 154 | { |
| 155 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 156 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 157 | if (dev) |
| 158 | nfc_put_device(dev); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | int nfc_genl_targets_found(struct nfc_dev *dev) |
| 164 | { |
| 165 | struct sk_buff *msg; |
| 166 | void *hdr; |
| 167 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 168 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 169 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 170 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 171 | if (!msg) |
| 172 | return -ENOMEM; |
| 173 | |
| 174 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 175 | NFC_EVENT_TARGETS_FOUND); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 176 | if (!hdr) |
| 177 | goto free_msg; |
| 178 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 179 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 180 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 181 | |
| 182 | genlmsg_end(msg, hdr); |
| 183 | |
| 184 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 185 | |
| 186 | nla_put_failure: |
| 187 | genlmsg_cancel(msg, hdr); |
| 188 | free_msg: |
| 189 | nlmsg_free(msg); |
| 190 | return -EMSGSIZE; |
| 191 | } |
| 192 | |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 193 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 194 | { |
| 195 | struct sk_buff *msg; |
| 196 | void *hdr; |
| 197 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 198 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 199 | if (!msg) |
| 200 | return -ENOMEM; |
| 201 | |
| 202 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 203 | NFC_EVENT_TARGET_LOST); |
| 204 | if (!hdr) |
| 205 | goto free_msg; |
| 206 | |
John W. Linville | 59ef43e | 2012-04-18 14:17:13 -0400 | [diff] [blame] | 207 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 208 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 209 | goto nla_put_failure; |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame] | 210 | |
| 211 | genlmsg_end(msg, hdr); |
| 212 | |
| 213 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 214 | |
| 215 | return 0; |
| 216 | |
| 217 | nla_put_failure: |
| 218 | genlmsg_cancel(msg, hdr); |
| 219 | free_msg: |
| 220 | nlmsg_free(msg); |
| 221 | return -EMSGSIZE; |
| 222 | } |
| 223 | |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 224 | int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol) |
| 225 | { |
| 226 | struct sk_buff *msg; |
| 227 | void *hdr; |
| 228 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 229 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 230 | if (!msg) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 234 | NFC_EVENT_TM_ACTIVATED); |
| 235 | if (!hdr) |
| 236 | goto free_msg; |
| 237 | |
| 238 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 239 | goto nla_put_failure; |
| 240 | if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol)) |
| 241 | goto nla_put_failure; |
| 242 | |
| 243 | genlmsg_end(msg, hdr); |
| 244 | |
| 245 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 246 | |
| 247 | return 0; |
| 248 | |
| 249 | nla_put_failure: |
| 250 | genlmsg_cancel(msg, hdr); |
| 251 | free_msg: |
| 252 | nlmsg_free(msg); |
| 253 | return -EMSGSIZE; |
| 254 | } |
| 255 | |
| 256 | int nfc_genl_tm_deactivated(struct nfc_dev *dev) |
| 257 | { |
| 258 | struct sk_buff *msg; |
| 259 | void *hdr; |
| 260 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 261 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Samuel Ortiz | fc40a8c | 2012-06-01 13:21:13 +0200 | [diff] [blame] | 262 | if (!msg) |
| 263 | return -ENOMEM; |
| 264 | |
| 265 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 266 | NFC_EVENT_TM_DEACTIVATED); |
| 267 | if (!hdr) |
| 268 | goto free_msg; |
| 269 | |
| 270 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 271 | goto nla_put_failure; |
| 272 | |
| 273 | genlmsg_end(msg, hdr); |
| 274 | |
| 275 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 276 | |
| 277 | return 0; |
| 278 | |
| 279 | nla_put_failure: |
| 280 | genlmsg_cancel(msg, hdr); |
| 281 | free_msg: |
| 282 | nlmsg_free(msg); |
| 283 | return -EMSGSIZE; |
| 284 | } |
| 285 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 286 | int nfc_genl_device_added(struct nfc_dev *dev) |
| 287 | { |
| 288 | struct sk_buff *msg; |
| 289 | void *hdr; |
| 290 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 291 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 292 | if (!msg) |
| 293 | return -ENOMEM; |
| 294 | |
| 295 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 296 | NFC_EVENT_DEVICE_ADDED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 297 | if (!hdr) |
| 298 | goto free_msg; |
| 299 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 300 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 301 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 302 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
| 303 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) |
| 304 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 305 | |
| 306 | genlmsg_end(msg, hdr); |
| 307 | |
| 308 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 309 | |
| 310 | return 0; |
| 311 | |
| 312 | nla_put_failure: |
| 313 | genlmsg_cancel(msg, hdr); |
| 314 | free_msg: |
| 315 | nlmsg_free(msg); |
| 316 | return -EMSGSIZE; |
| 317 | } |
| 318 | |
| 319 | int nfc_genl_device_removed(struct nfc_dev *dev) |
| 320 | { |
| 321 | struct sk_buff *msg; |
| 322 | void *hdr; |
| 323 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 324 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 325 | if (!msg) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 329 | NFC_EVENT_DEVICE_REMOVED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 330 | if (!hdr) |
| 331 | goto free_msg; |
| 332 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 333 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 334 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 335 | |
| 336 | genlmsg_end(msg, hdr); |
| 337 | |
| 338 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 339 | |
| 340 | return 0; |
| 341 | |
| 342 | nla_put_failure: |
| 343 | genlmsg_cancel(msg, hdr); |
| 344 | free_msg: |
| 345 | nlmsg_free(msg); |
| 346 | return -EMSGSIZE; |
| 347 | } |
| 348 | |
| 349 | 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] | 350 | u32 portid, u32 seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 351 | struct netlink_callback *cb, |
| 352 | int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 353 | { |
| 354 | void *hdr; |
| 355 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 356 | hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 357 | NFC_CMD_GET_DEVICE); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 358 | if (!hdr) |
| 359 | return -EMSGSIZE; |
| 360 | |
| 361 | if (cb) |
| 362 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 363 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 364 | if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) || |
| 365 | nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) || |
| 366 | nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) || |
| 367 | nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up)) |
| 368 | goto nla_put_failure; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 369 | |
| 370 | return genlmsg_end(msg, hdr); |
| 371 | |
| 372 | nla_put_failure: |
| 373 | genlmsg_cancel(msg, hdr); |
| 374 | return -EMSGSIZE; |
| 375 | } |
| 376 | |
| 377 | static int nfc_genl_dump_devices(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 378 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 379 | { |
| 380 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 381 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 382 | bool first_call = false; |
| 383 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 384 | if (!iter) { |
| 385 | first_call = true; |
| 386 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 387 | if (!iter) |
| 388 | return -ENOMEM; |
| 389 | cb->args[0] = (long) iter; |
| 390 | } |
| 391 | |
| 392 | mutex_lock(&nfc_devlist_mutex); |
| 393 | |
| 394 | cb->seq = nfc_devlist_generation; |
| 395 | |
| 396 | if (first_call) { |
| 397 | nfc_device_iter_init(iter); |
| 398 | dev = nfc_device_iter_next(iter); |
| 399 | } |
| 400 | |
| 401 | while (dev) { |
| 402 | int rc; |
| 403 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 404 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 405 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 406 | if (rc < 0) |
| 407 | break; |
| 408 | |
| 409 | dev = nfc_device_iter_next(iter); |
| 410 | } |
| 411 | |
| 412 | mutex_unlock(&nfc_devlist_mutex); |
| 413 | |
| 414 | cb->args[1] = (long) dev; |
| 415 | |
| 416 | return skb->len; |
| 417 | } |
| 418 | |
| 419 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) |
| 420 | { |
| 421 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 422 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 423 | nfc_device_iter_exit(iter); |
| 424 | kfree(iter); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 429 | 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] | 430 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 431 | { |
| 432 | struct sk_buff *msg; |
| 433 | void *hdr; |
| 434 | |
| 435 | pr_debug("DEP link is up\n"); |
| 436 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 437 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 438 | if (!msg) |
| 439 | return -ENOMEM; |
| 440 | |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 441 | 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] | 442 | if (!hdr) |
| 443 | goto free_msg; |
| 444 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 445 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 446 | goto nla_put_failure; |
| 447 | if (rf_mode == NFC_RF_INITIATOR && |
| 448 | nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx)) |
| 449 | goto nla_put_failure; |
| 450 | if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) || |
| 451 | nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode)) |
| 452 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 453 | |
| 454 | genlmsg_end(msg, hdr); |
| 455 | |
| 456 | dev->dep_link_up = true; |
| 457 | |
| 458 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 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_dep_link_down_event(struct nfc_dev *dev) |
| 470 | { |
| 471 | struct sk_buff *msg; |
| 472 | void *hdr; |
| 473 | |
| 474 | pr_debug("DEP link is down\n"); |
| 475 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 476 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 477 | if (!msg) |
| 478 | return -ENOMEM; |
| 479 | |
| 480 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 481 | NFC_CMD_DEP_LINK_DOWN); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 482 | if (!hdr) |
| 483 | goto free_msg; |
| 484 | |
David S. Miller | 1e6428d | 2012-03-29 23:23:57 -0400 | [diff] [blame] | 485 | if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx)) |
| 486 | goto nla_put_failure; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 487 | |
| 488 | genlmsg_end(msg, hdr); |
| 489 | |
| 490 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 491 | |
| 492 | return 0; |
| 493 | |
| 494 | nla_put_failure: |
| 495 | genlmsg_cancel(msg, hdr); |
| 496 | free_msg: |
| 497 | nlmsg_free(msg); |
| 498 | return -EMSGSIZE; |
| 499 | } |
| 500 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 501 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
| 502 | { |
| 503 | struct sk_buff *msg; |
| 504 | struct nfc_dev *dev; |
| 505 | u32 idx; |
| 506 | int rc = -ENOBUFS; |
| 507 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 508 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 509 | return -EINVAL; |
| 510 | |
| 511 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 512 | |
| 513 | dev = nfc_get_device(idx); |
| 514 | if (!dev) |
| 515 | return -ENODEV; |
| 516 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 517 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 518 | if (!msg) { |
| 519 | rc = -ENOMEM; |
| 520 | goto out_putdev; |
| 521 | } |
| 522 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 523 | 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] | 524 | NULL, 0); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 525 | if (rc < 0) |
| 526 | goto out_free; |
| 527 | |
| 528 | nfc_put_device(dev); |
| 529 | |
| 530 | return genlmsg_reply(msg, info); |
| 531 | |
| 532 | out_free: |
| 533 | nlmsg_free(msg); |
| 534 | out_putdev: |
| 535 | nfc_put_device(dev); |
| 536 | return rc; |
| 537 | } |
| 538 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 539 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
| 540 | { |
| 541 | struct nfc_dev *dev; |
| 542 | int rc; |
| 543 | u32 idx; |
| 544 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 545 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 546 | return -EINVAL; |
| 547 | |
| 548 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 549 | |
| 550 | dev = nfc_get_device(idx); |
| 551 | if (!dev) |
| 552 | return -ENODEV; |
| 553 | |
| 554 | rc = nfc_dev_up(dev); |
| 555 | |
| 556 | nfc_put_device(dev); |
| 557 | return rc; |
| 558 | } |
| 559 | |
| 560 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) |
| 561 | { |
| 562 | struct nfc_dev *dev; |
| 563 | int rc; |
| 564 | u32 idx; |
| 565 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 566 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 567 | return -EINVAL; |
| 568 | |
| 569 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 570 | |
| 571 | dev = nfc_get_device(idx); |
| 572 | if (!dev) |
| 573 | return -ENODEV; |
| 574 | |
| 575 | rc = nfc_dev_down(dev); |
| 576 | |
| 577 | nfc_put_device(dev); |
| 578 | return rc; |
| 579 | } |
| 580 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 581 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
| 582 | { |
| 583 | struct nfc_dev *dev; |
| 584 | int rc; |
| 585 | u32 idx; |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 586 | u32 im_protocols = 0, tm_protocols = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 587 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 588 | pr_debug("Poll start\n"); |
| 589 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 590 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 591 | ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] && |
| 592 | !info->attrs[NFC_ATTR_PROTOCOLS]) && |
| 593 | !info->attrs[NFC_ATTR_TM_PROTOCOLS])) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 594 | return -EINVAL; |
| 595 | |
| 596 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 597 | |
| 598 | if (info->attrs[NFC_ATTR_TM_PROTOCOLS]) |
| 599 | tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]); |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 600 | |
| 601 | if (info->attrs[NFC_ATTR_IM_PROTOCOLS]) |
| 602 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]); |
Samuel Ortiz | 5e50ee3 | 2012-05-31 11:48:58 +0200 | [diff] [blame] | 603 | else if (info->attrs[NFC_ATTR_PROTOCOLS]) |
| 604 | im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 605 | |
| 606 | dev = nfc_get_device(idx); |
| 607 | if (!dev) |
| 608 | return -ENODEV; |
| 609 | |
| 610 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 611 | |
Samuel Ortiz | fe7c580 | 2012-05-15 15:57:06 +0200 | [diff] [blame] | 612 | rc = nfc_start_poll(dev, im_protocols, tm_protocols); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 613 | if (!rc) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 614 | dev->genl_data.poll_req_portid = info->snd_portid; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 615 | |
| 616 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 617 | |
| 618 | nfc_put_device(dev); |
| 619 | return rc; |
| 620 | } |
| 621 | |
| 622 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) |
| 623 | { |
| 624 | struct nfc_dev *dev; |
| 625 | int rc; |
| 626 | u32 idx; |
| 627 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 628 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 629 | return -EINVAL; |
| 630 | |
| 631 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 632 | |
| 633 | dev = nfc_get_device(idx); |
| 634 | if (!dev) |
| 635 | return -ENODEV; |
| 636 | |
Samuel Ortiz | a831b91 | 2012-06-28 16:41:57 +0200 | [diff] [blame] | 637 | device_lock(&dev->dev); |
| 638 | |
| 639 | if (!dev->polling) { |
| 640 | device_unlock(&dev->dev); |
| 641 | return -EINVAL; |
| 642 | } |
| 643 | |
| 644 | device_unlock(&dev->dev); |
| 645 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 646 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 647 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 648 | if (dev->genl_data.poll_req_portid != info->snd_portid) { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 649 | rc = -EBUSY; |
| 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | rc = nfc_stop_poll(dev); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 654 | dev->genl_data.poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 655 | |
| 656 | out: |
| 657 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 658 | nfc_put_device(dev); |
| 659 | return rc; |
| 660 | } |
| 661 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 662 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
| 663 | { |
| 664 | struct nfc_dev *dev; |
| 665 | int rc, tgt_idx; |
| 666 | u32 idx; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 667 | u8 comm; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 668 | |
| 669 | pr_debug("DEP link up\n"); |
| 670 | |
| 671 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 672 | !info->attrs[NFC_ATTR_COMM_MODE]) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 673 | return -EINVAL; |
| 674 | |
| 675 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 676 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) |
| 677 | tgt_idx = NFC_TARGET_IDX_ANY; |
| 678 | else |
| 679 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 680 | |
| 681 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 682 | |
| 683 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) |
| 684 | return -EINVAL; |
| 685 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 686 | dev = nfc_get_device(idx); |
| 687 | if (!dev) |
| 688 | return -ENODEV; |
| 689 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 690 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 691 | |
| 692 | nfc_put_device(dev); |
| 693 | |
| 694 | return rc; |
| 695 | } |
| 696 | |
| 697 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) |
| 698 | { |
| 699 | struct nfc_dev *dev; |
| 700 | int rc; |
| 701 | u32 idx; |
| 702 | |
| 703 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 704 | return -EINVAL; |
| 705 | |
| 706 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 707 | |
| 708 | dev = nfc_get_device(idx); |
| 709 | if (!dev) |
| 710 | return -ENODEV; |
| 711 | |
| 712 | rc = nfc_dep_link_down(dev); |
| 713 | |
| 714 | nfc_put_device(dev); |
| 715 | return rc; |
| 716 | } |
| 717 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 718 | static struct genl_ops nfc_genl_ops[] = { |
| 719 | { |
| 720 | .cmd = NFC_CMD_GET_DEVICE, |
| 721 | .doit = nfc_genl_get_device, |
| 722 | .dumpit = nfc_genl_dump_devices, |
| 723 | .done = nfc_genl_dump_devices_done, |
| 724 | .policy = nfc_genl_policy, |
| 725 | }, |
| 726 | { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 727 | .cmd = NFC_CMD_DEV_UP, |
| 728 | .doit = nfc_genl_dev_up, |
| 729 | .policy = nfc_genl_policy, |
| 730 | }, |
| 731 | { |
| 732 | .cmd = NFC_CMD_DEV_DOWN, |
| 733 | .doit = nfc_genl_dev_down, |
| 734 | .policy = nfc_genl_policy, |
| 735 | }, |
| 736 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 737 | .cmd = NFC_CMD_START_POLL, |
| 738 | .doit = nfc_genl_start_poll, |
| 739 | .policy = nfc_genl_policy, |
| 740 | }, |
| 741 | { |
| 742 | .cmd = NFC_CMD_STOP_POLL, |
| 743 | .doit = nfc_genl_stop_poll, |
| 744 | .policy = nfc_genl_policy, |
| 745 | }, |
| 746 | { |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 747 | .cmd = NFC_CMD_DEP_LINK_UP, |
| 748 | .doit = nfc_genl_dep_link_up, |
| 749 | .policy = nfc_genl_policy, |
| 750 | }, |
| 751 | { |
| 752 | .cmd = NFC_CMD_DEP_LINK_DOWN, |
| 753 | .doit = nfc_genl_dep_link_down, |
| 754 | .policy = nfc_genl_policy, |
| 755 | }, |
| 756 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 757 | .cmd = NFC_CMD_GET_TARGET, |
| 758 | .dumpit = nfc_genl_dump_targets, |
| 759 | .done = nfc_genl_dump_targets_done, |
| 760 | .policy = nfc_genl_policy, |
| 761 | }, |
| 762 | }; |
| 763 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 764 | |
| 765 | struct urelease_work { |
| 766 | struct work_struct w; |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 767 | int portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 768 | }; |
| 769 | |
| 770 | static void nfc_urelease_event_work(struct work_struct *work) |
| 771 | { |
| 772 | struct urelease_work *w = container_of(work, struct urelease_work, w); |
| 773 | struct class_dev_iter iter; |
| 774 | struct nfc_dev *dev; |
| 775 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 776 | pr_debug("portid %d\n", w->portid); |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 777 | |
| 778 | mutex_lock(&nfc_devlist_mutex); |
| 779 | |
| 780 | nfc_device_iter_init(&iter); |
| 781 | dev = nfc_device_iter_next(&iter); |
| 782 | |
| 783 | while (dev) { |
| 784 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 785 | |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 786 | if (dev->genl_data.poll_req_portid == w->portid) { |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 787 | nfc_stop_poll(dev); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 788 | dev->genl_data.poll_req_portid = 0; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 792 | |
| 793 | dev = nfc_device_iter_next(&iter); |
| 794 | } |
| 795 | |
| 796 | nfc_device_iter_exit(&iter); |
| 797 | |
| 798 | mutex_unlock(&nfc_devlist_mutex); |
| 799 | |
| 800 | kfree(w); |
| 801 | } |
| 802 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 803 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 804 | unsigned long event, void *ptr) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 805 | { |
| 806 | struct netlink_notify *n = ptr; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 807 | struct urelease_work *w; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 808 | |
| 809 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) |
| 810 | goto out; |
| 811 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 812 | pr_debug("NETLINK_URELEASE event from id %d\n", n->portid); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 813 | |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 814 | w = kmalloc(sizeof(*w), GFP_ATOMIC); |
| 815 | if (w) { |
| 816 | INIT_WORK((struct work_struct *) w, nfc_urelease_event_work); |
John W. Linville | c487606 | 2012-09-28 11:11:16 -0400 | [diff] [blame] | 817 | w->portid = n->portid; |
Szymon Janc | 3c0cc8a | 2012-09-26 14:17:12 +0200 | [diff] [blame] | 818 | schedule_work((struct work_struct *) w); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 819 | } |
| 820 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 821 | out: |
| 822 | return NOTIFY_DONE; |
| 823 | } |
| 824 | |
| 825 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) |
| 826 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 827 | genl_data->poll_req_portid = 0; |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 828 | mutex_init(&genl_data->genl_data_mutex); |
| 829 | } |
| 830 | |
| 831 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) |
| 832 | { |
| 833 | mutex_destroy(&genl_data->genl_data_mutex); |
| 834 | } |
| 835 | |
| 836 | static struct notifier_block nl_notifier = { |
| 837 | .notifier_call = nfc_genl_rcv_nl_event, |
| 838 | }; |
| 839 | |
| 840 | /** |
| 841 | * nfc_genl_init() - Initialize netlink interface |
| 842 | * |
| 843 | * This initialization function registers the nfc netlink family. |
| 844 | */ |
| 845 | int __init nfc_genl_init(void) |
| 846 | { |
| 847 | int rc; |
| 848 | |
| 849 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 850 | ARRAY_SIZE(nfc_genl_ops)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 851 | if (rc) |
| 852 | return rc; |
| 853 | |
| 854 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); |
| 855 | |
| 856 | netlink_register_notifier(&nl_notifier); |
| 857 | |
| 858 | return rc; |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * nfc_genl_exit() - Deinitialize netlink interface |
| 863 | * |
| 864 | * This exit function unregisters the nfc netlink family. |
| 865 | */ |
| 866 | void nfc_genl_exit(void) |
| 867 | { |
| 868 | netlink_unregister_notifier(&nl_notifier); |
| 869 | genl_unregister_family(&nfc_genl_family); |
| 870 | } |