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 | |
| 36 | struct genl_family nfc_genl_family = { |
| 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 }, |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | 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] | 55 | struct netlink_callback *cb, int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 56 | { |
| 57 | void *hdr; |
| 58 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 59 | hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 60 | &nfc_genl_family, flags, NFC_CMD_GET_TARGET); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 61 | if (!hdr) |
| 62 | return -EMSGSIZE; |
| 63 | |
| 64 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 65 | |
| 66 | NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target->idx); |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 67 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 68 | NLA_PUT_U16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res); |
| 69 | NLA_PUT_U8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res); |
Ilan Elias | 288e071 | 2011-12-22 11:51:54 +0200 | [diff] [blame] | 70 | if (target->nfcid1_len > 0) |
| 71 | NLA_PUT(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 72 | target->nfcid1); |
Ilan Elias | d5a2ca6 | 2012-01-17 11:06:43 +0200 | [diff] [blame] | 73 | if (target->sensb_res_len > 0) |
| 74 | NLA_PUT(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 75 | target->sensb_res); |
Ilan Elias | d5a2ca6 | 2012-01-17 11:06:43 +0200 | [diff] [blame] | 76 | if (target->sensf_res_len > 0) |
| 77 | NLA_PUT(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 78 | target->sensf_res); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 79 | |
| 80 | return genlmsg_end(msg, hdr); |
| 81 | |
| 82 | nla_put_failure: |
| 83 | genlmsg_cancel(msg, hdr); |
| 84 | return -EMSGSIZE; |
| 85 | } |
| 86 | |
| 87 | static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb) |
| 88 | { |
| 89 | struct nfc_dev *dev; |
| 90 | int rc; |
| 91 | u32 idx; |
| 92 | |
| 93 | rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 94 | nfc_genl_family.attrbuf, |
| 95 | nfc_genl_family.maxattr, |
| 96 | nfc_genl_policy); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 97 | if (rc < 0) |
| 98 | return ERR_PTR(rc); |
| 99 | |
| 100 | if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]) |
| 101 | return ERR_PTR(-EINVAL); |
| 102 | |
| 103 | idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]); |
| 104 | |
| 105 | dev = nfc_get_device(idx); |
| 106 | if (!dev) |
| 107 | return ERR_PTR(-ENODEV); |
| 108 | |
| 109 | return dev; |
| 110 | } |
| 111 | |
| 112 | static int nfc_genl_dump_targets(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 113 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 114 | { |
| 115 | int i = cb->args[0]; |
| 116 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 117 | int rc; |
| 118 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 119 | if (!dev) { |
| 120 | dev = __get_device_from_cb(cb); |
| 121 | if (IS_ERR(dev)) |
| 122 | return PTR_ERR(dev); |
| 123 | |
| 124 | cb->args[1] = (long) dev; |
| 125 | } |
| 126 | |
| 127 | spin_lock_bh(&dev->targets_lock); |
| 128 | |
| 129 | cb->seq = dev->targets_generation; |
| 130 | |
| 131 | while (i < dev->n_targets) { |
| 132 | rc = nfc_genl_send_target(skb, &dev->targets[i], cb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 133 | NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 134 | if (rc < 0) |
| 135 | break; |
| 136 | |
| 137 | i++; |
| 138 | } |
| 139 | |
| 140 | spin_unlock_bh(&dev->targets_lock); |
| 141 | |
| 142 | cb->args[0] = i; |
| 143 | |
| 144 | return skb->len; |
| 145 | } |
| 146 | |
| 147 | static int nfc_genl_dump_targets_done(struct netlink_callback *cb) |
| 148 | { |
| 149 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 150 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 151 | if (dev) |
| 152 | nfc_put_device(dev); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int nfc_genl_targets_found(struct nfc_dev *dev) |
| 158 | { |
| 159 | struct sk_buff *msg; |
| 160 | void *hdr; |
| 161 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 162 | dev->genl_data.poll_req_pid = 0; |
| 163 | |
| 164 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 165 | if (!msg) |
| 166 | return -ENOMEM; |
| 167 | |
| 168 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 169 | NFC_EVENT_TARGETS_FOUND); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 170 | if (!hdr) |
| 171 | goto free_msg; |
| 172 | |
| 173 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); |
| 174 | |
| 175 | genlmsg_end(msg, hdr); |
| 176 | |
| 177 | return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 178 | |
| 179 | nla_put_failure: |
| 180 | genlmsg_cancel(msg, hdr); |
| 181 | free_msg: |
| 182 | nlmsg_free(msg); |
| 183 | return -EMSGSIZE; |
| 184 | } |
| 185 | |
Samuel Ortiz | 8112a5c | 2012-04-10 19:43:04 +0200 | [diff] [blame^] | 186 | int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx) |
| 187 | { |
| 188 | struct sk_buff *msg; |
| 189 | void *hdr; |
| 190 | |
| 191 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 192 | if (!msg) |
| 193 | return -ENOMEM; |
| 194 | |
| 195 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
| 196 | NFC_EVENT_TARGET_LOST); |
| 197 | if (!hdr) |
| 198 | goto free_msg; |
| 199 | |
| 200 | NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)); |
| 201 | NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx); |
| 202 | |
| 203 | genlmsg_end(msg, hdr); |
| 204 | |
| 205 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 206 | |
| 207 | return 0; |
| 208 | |
| 209 | nla_put_failure: |
| 210 | genlmsg_cancel(msg, hdr); |
| 211 | free_msg: |
| 212 | nlmsg_free(msg); |
| 213 | return -EMSGSIZE; |
| 214 | } |
| 215 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 216 | int nfc_genl_device_added(struct nfc_dev *dev) |
| 217 | { |
| 218 | struct sk_buff *msg; |
| 219 | void *hdr; |
| 220 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 221 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 222 | if (!msg) |
| 223 | return -ENOMEM; |
| 224 | |
| 225 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 226 | NFC_EVENT_DEVICE_ADDED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 227 | if (!hdr) |
| 228 | goto free_msg; |
| 229 | |
| 230 | NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)); |
| 231 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); |
| 232 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols); |
Samuel Ortiz | c970a1a | 2012-03-05 01:03:34 +0100 | [diff] [blame] | 233 | NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 234 | |
| 235 | genlmsg_end(msg, hdr); |
| 236 | |
| 237 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 238 | |
| 239 | return 0; |
| 240 | |
| 241 | nla_put_failure: |
| 242 | genlmsg_cancel(msg, hdr); |
| 243 | free_msg: |
| 244 | nlmsg_free(msg); |
| 245 | return -EMSGSIZE; |
| 246 | } |
| 247 | |
| 248 | int nfc_genl_device_removed(struct nfc_dev *dev) |
| 249 | { |
| 250 | struct sk_buff *msg; |
| 251 | void *hdr; |
| 252 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 253 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 254 | if (!msg) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 258 | NFC_EVENT_DEVICE_REMOVED); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 259 | if (!hdr) |
| 260 | goto free_msg; |
| 261 | |
| 262 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); |
| 263 | |
| 264 | genlmsg_end(msg, hdr); |
| 265 | |
| 266 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); |
| 267 | |
| 268 | return 0; |
| 269 | |
| 270 | nla_put_failure: |
| 271 | genlmsg_cancel(msg, hdr); |
| 272 | free_msg: |
| 273 | nlmsg_free(msg); |
| 274 | return -EMSGSIZE; |
| 275 | } |
| 276 | |
| 277 | static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 278 | u32 pid, u32 seq, |
| 279 | struct netlink_callback *cb, |
| 280 | int flags) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 281 | { |
| 282 | void *hdr; |
| 283 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 284 | hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 285 | NFC_CMD_GET_DEVICE); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 286 | if (!hdr) |
| 287 | return -EMSGSIZE; |
| 288 | |
| 289 | if (cb) |
| 290 | genl_dump_check_consistent(cb, hdr, &nfc_genl_family); |
| 291 | |
| 292 | NLA_PUT_STRING(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)); |
| 293 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); |
| 294 | NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols); |
Samuel Ortiz | c970a1a | 2012-03-05 01:03:34 +0100 | [diff] [blame] | 295 | NLA_PUT_U8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 296 | |
| 297 | return genlmsg_end(msg, hdr); |
| 298 | |
| 299 | nla_put_failure: |
| 300 | genlmsg_cancel(msg, hdr); |
| 301 | return -EMSGSIZE; |
| 302 | } |
| 303 | |
| 304 | static int nfc_genl_dump_devices(struct sk_buff *skb, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 305 | struct netlink_callback *cb) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 306 | { |
| 307 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 308 | struct nfc_dev *dev = (struct nfc_dev *) cb->args[1]; |
| 309 | bool first_call = false; |
| 310 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 311 | if (!iter) { |
| 312 | first_call = true; |
| 313 | iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL); |
| 314 | if (!iter) |
| 315 | return -ENOMEM; |
| 316 | cb->args[0] = (long) iter; |
| 317 | } |
| 318 | |
| 319 | mutex_lock(&nfc_devlist_mutex); |
| 320 | |
| 321 | cb->seq = nfc_devlist_generation; |
| 322 | |
| 323 | if (first_call) { |
| 324 | nfc_device_iter_init(iter); |
| 325 | dev = nfc_device_iter_next(iter); |
| 326 | } |
| 327 | |
| 328 | while (dev) { |
| 329 | int rc; |
| 330 | |
| 331 | rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 332 | cb->nlh->nlmsg_seq, cb, NLM_F_MULTI); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 333 | if (rc < 0) |
| 334 | break; |
| 335 | |
| 336 | dev = nfc_device_iter_next(iter); |
| 337 | } |
| 338 | |
| 339 | mutex_unlock(&nfc_devlist_mutex); |
| 340 | |
| 341 | cb->args[1] = (long) dev; |
| 342 | |
| 343 | return skb->len; |
| 344 | } |
| 345 | |
| 346 | static int nfc_genl_dump_devices_done(struct netlink_callback *cb) |
| 347 | { |
| 348 | struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0]; |
| 349 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 350 | nfc_device_iter_exit(iter); |
| 351 | kfree(iter); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 356 | 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] | 357 | u8 comm_mode, u8 rf_mode) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 358 | { |
| 359 | struct sk_buff *msg; |
| 360 | void *hdr; |
| 361 | |
| 362 | pr_debug("DEP link is up\n"); |
| 363 | |
| 364 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 365 | if (!msg) |
| 366 | return -ENOMEM; |
| 367 | |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 368 | 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] | 369 | if (!hdr) |
| 370 | goto free_msg; |
| 371 | |
| 372 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); |
| 373 | if (rf_mode == NFC_RF_INITIATOR) |
| 374 | NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx); |
| 375 | NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode); |
| 376 | NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode); |
| 377 | |
| 378 | genlmsg_end(msg, hdr); |
| 379 | |
| 380 | dev->dep_link_up = true; |
| 381 | |
| 382 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 383 | |
| 384 | return 0; |
| 385 | |
| 386 | nla_put_failure: |
| 387 | genlmsg_cancel(msg, hdr); |
| 388 | free_msg: |
| 389 | nlmsg_free(msg); |
| 390 | return -EMSGSIZE; |
| 391 | } |
| 392 | |
| 393 | int nfc_genl_dep_link_down_event(struct nfc_dev *dev) |
| 394 | { |
| 395 | struct sk_buff *msg; |
| 396 | void *hdr; |
| 397 | |
| 398 | pr_debug("DEP link is down\n"); |
| 399 | |
| 400 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
| 401 | if (!msg) |
| 402 | return -ENOMEM; |
| 403 | |
| 404 | hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 405 | NFC_CMD_DEP_LINK_DOWN); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 406 | if (!hdr) |
| 407 | goto free_msg; |
| 408 | |
| 409 | NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx); |
| 410 | |
| 411 | genlmsg_end(msg, hdr); |
| 412 | |
| 413 | genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); |
| 414 | |
| 415 | return 0; |
| 416 | |
| 417 | nla_put_failure: |
| 418 | genlmsg_cancel(msg, hdr); |
| 419 | free_msg: |
| 420 | nlmsg_free(msg); |
| 421 | return -EMSGSIZE; |
| 422 | } |
| 423 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 424 | static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info) |
| 425 | { |
| 426 | struct sk_buff *msg; |
| 427 | struct nfc_dev *dev; |
| 428 | u32 idx; |
| 429 | int rc = -ENOBUFS; |
| 430 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 431 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 432 | return -EINVAL; |
| 433 | |
| 434 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 435 | |
| 436 | dev = nfc_get_device(idx); |
| 437 | if (!dev) |
| 438 | return -ENODEV; |
| 439 | |
| 440 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 441 | if (!msg) { |
| 442 | rc = -ENOMEM; |
| 443 | goto out_putdev; |
| 444 | } |
| 445 | |
| 446 | rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 447 | NULL, 0); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 448 | if (rc < 0) |
| 449 | goto out_free; |
| 450 | |
| 451 | nfc_put_device(dev); |
| 452 | |
| 453 | return genlmsg_reply(msg, info); |
| 454 | |
| 455 | out_free: |
| 456 | nlmsg_free(msg); |
| 457 | out_putdev: |
| 458 | nfc_put_device(dev); |
| 459 | return rc; |
| 460 | } |
| 461 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 462 | static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info) |
| 463 | { |
| 464 | struct nfc_dev *dev; |
| 465 | int rc; |
| 466 | u32 idx; |
| 467 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 468 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 469 | return -EINVAL; |
| 470 | |
| 471 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 472 | |
| 473 | dev = nfc_get_device(idx); |
| 474 | if (!dev) |
| 475 | return -ENODEV; |
| 476 | |
| 477 | rc = nfc_dev_up(dev); |
| 478 | |
| 479 | nfc_put_device(dev); |
| 480 | return rc; |
| 481 | } |
| 482 | |
| 483 | static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info) |
| 484 | { |
| 485 | struct nfc_dev *dev; |
| 486 | int rc; |
| 487 | u32 idx; |
| 488 | |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 489 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 490 | return -EINVAL; |
| 491 | |
| 492 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 493 | |
| 494 | dev = nfc_get_device(idx); |
| 495 | if (!dev) |
| 496 | return -ENODEV; |
| 497 | |
| 498 | rc = nfc_dev_down(dev); |
| 499 | |
| 500 | nfc_put_device(dev); |
| 501 | return rc; |
| 502 | } |
| 503 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 504 | static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info) |
| 505 | { |
| 506 | struct nfc_dev *dev; |
| 507 | int rc; |
| 508 | u32 idx; |
| 509 | u32 protocols; |
| 510 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 511 | pr_debug("Poll start\n"); |
| 512 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 513 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 514 | !info->attrs[NFC_ATTR_PROTOCOLS]) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 515 | return -EINVAL; |
| 516 | |
| 517 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 518 | protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]); |
| 519 | |
| 520 | dev = nfc_get_device(idx); |
| 521 | if (!dev) |
| 522 | return -ENODEV; |
| 523 | |
| 524 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 525 | |
| 526 | rc = nfc_start_poll(dev, protocols); |
| 527 | if (!rc) |
| 528 | dev->genl_data.poll_req_pid = info->snd_pid; |
| 529 | |
| 530 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 531 | |
| 532 | nfc_put_device(dev); |
| 533 | return rc; |
| 534 | } |
| 535 | |
| 536 | static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info) |
| 537 | { |
| 538 | struct nfc_dev *dev; |
| 539 | int rc; |
| 540 | u32 idx; |
| 541 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 542 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 546 | |
| 547 | dev = nfc_get_device(idx); |
| 548 | if (!dev) |
| 549 | return -ENODEV; |
| 550 | |
| 551 | mutex_lock(&dev->genl_data.genl_data_mutex); |
| 552 | |
| 553 | if (dev->genl_data.poll_req_pid != info->snd_pid) { |
| 554 | rc = -EBUSY; |
| 555 | goto out; |
| 556 | } |
| 557 | |
| 558 | rc = nfc_stop_poll(dev); |
| 559 | dev->genl_data.poll_req_pid = 0; |
| 560 | |
| 561 | out: |
| 562 | mutex_unlock(&dev->genl_data.genl_data_mutex); |
| 563 | nfc_put_device(dev); |
| 564 | return rc; |
| 565 | } |
| 566 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 567 | static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info) |
| 568 | { |
| 569 | struct nfc_dev *dev; |
| 570 | int rc, tgt_idx; |
| 571 | u32 idx; |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 572 | u8 comm; |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 573 | |
| 574 | pr_debug("DEP link up\n"); |
| 575 | |
| 576 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX] || |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 577 | !info->attrs[NFC_ATTR_COMM_MODE]) |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 578 | return -EINVAL; |
| 579 | |
| 580 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 581 | if (!info->attrs[NFC_ATTR_TARGET_INDEX]) |
| 582 | tgt_idx = NFC_TARGET_IDX_ANY; |
| 583 | else |
| 584 | tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]); |
| 585 | |
| 586 | comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 587 | |
| 588 | if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE) |
| 589 | return -EINVAL; |
| 590 | |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 591 | dev = nfc_get_device(idx); |
| 592 | if (!dev) |
| 593 | return -ENODEV; |
| 594 | |
Samuel Ortiz | 47807d3 | 2012-03-05 01:03:50 +0100 | [diff] [blame] | 595 | rc = nfc_dep_link_up(dev, tgt_idx, comm); |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 596 | |
| 597 | nfc_put_device(dev); |
| 598 | |
| 599 | return rc; |
| 600 | } |
| 601 | |
| 602 | static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info) |
| 603 | { |
| 604 | struct nfc_dev *dev; |
| 605 | int rc; |
| 606 | u32 idx; |
| 607 | |
| 608 | if (!info->attrs[NFC_ATTR_DEVICE_INDEX]) |
| 609 | return -EINVAL; |
| 610 | |
| 611 | idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]); |
| 612 | |
| 613 | dev = nfc_get_device(idx); |
| 614 | if (!dev) |
| 615 | return -ENODEV; |
| 616 | |
| 617 | rc = nfc_dep_link_down(dev); |
| 618 | |
| 619 | nfc_put_device(dev); |
| 620 | return rc; |
| 621 | } |
| 622 | |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 623 | static struct genl_ops nfc_genl_ops[] = { |
| 624 | { |
| 625 | .cmd = NFC_CMD_GET_DEVICE, |
| 626 | .doit = nfc_genl_get_device, |
| 627 | .dumpit = nfc_genl_dump_devices, |
| 628 | .done = nfc_genl_dump_devices_done, |
| 629 | .policy = nfc_genl_policy, |
| 630 | }, |
| 631 | { |
Ilan Elias | 8b3fe7b | 2011-09-18 11:19:33 +0300 | [diff] [blame] | 632 | .cmd = NFC_CMD_DEV_UP, |
| 633 | .doit = nfc_genl_dev_up, |
| 634 | .policy = nfc_genl_policy, |
| 635 | }, |
| 636 | { |
| 637 | .cmd = NFC_CMD_DEV_DOWN, |
| 638 | .doit = nfc_genl_dev_down, |
| 639 | .policy = nfc_genl_policy, |
| 640 | }, |
| 641 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 642 | .cmd = NFC_CMD_START_POLL, |
| 643 | .doit = nfc_genl_start_poll, |
| 644 | .policy = nfc_genl_policy, |
| 645 | }, |
| 646 | { |
| 647 | .cmd = NFC_CMD_STOP_POLL, |
| 648 | .doit = nfc_genl_stop_poll, |
| 649 | .policy = nfc_genl_policy, |
| 650 | }, |
| 651 | { |
Samuel Ortiz | 1ed28f6 | 2011-12-14 16:43:09 +0100 | [diff] [blame] | 652 | .cmd = NFC_CMD_DEP_LINK_UP, |
| 653 | .doit = nfc_genl_dep_link_up, |
| 654 | .policy = nfc_genl_policy, |
| 655 | }, |
| 656 | { |
| 657 | .cmd = NFC_CMD_DEP_LINK_DOWN, |
| 658 | .doit = nfc_genl_dep_link_down, |
| 659 | .policy = nfc_genl_policy, |
| 660 | }, |
| 661 | { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 662 | .cmd = NFC_CMD_GET_TARGET, |
| 663 | .dumpit = nfc_genl_dump_targets, |
| 664 | .done = nfc_genl_dump_targets_done, |
| 665 | .policy = nfc_genl_policy, |
| 666 | }, |
| 667 | }; |
| 668 | |
| 669 | static int nfc_genl_rcv_nl_event(struct notifier_block *this, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 670 | unsigned long event, void *ptr) |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 671 | { |
| 672 | struct netlink_notify *n = ptr; |
| 673 | struct class_dev_iter iter; |
| 674 | struct nfc_dev *dev; |
| 675 | |
| 676 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC) |
| 677 | goto out; |
| 678 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 679 | pr_debug("NETLINK_URELEASE event from id %d\n", n->pid); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 680 | |
| 681 | nfc_device_iter_init(&iter); |
| 682 | dev = nfc_device_iter_next(&iter); |
| 683 | |
| 684 | while (dev) { |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 685 | if (dev->genl_data.poll_req_pid == n->pid) { |
| 686 | nfc_stop_poll(dev); |
| 687 | dev->genl_data.poll_req_pid = 0; |
| 688 | } |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 689 | dev = nfc_device_iter_next(&iter); |
| 690 | } |
| 691 | |
| 692 | nfc_device_iter_exit(&iter); |
| 693 | |
| 694 | out: |
| 695 | return NOTIFY_DONE; |
| 696 | } |
| 697 | |
| 698 | void nfc_genl_data_init(struct nfc_genl_data *genl_data) |
| 699 | { |
| 700 | genl_data->poll_req_pid = 0; |
| 701 | mutex_init(&genl_data->genl_data_mutex); |
| 702 | } |
| 703 | |
| 704 | void nfc_genl_data_exit(struct nfc_genl_data *genl_data) |
| 705 | { |
| 706 | mutex_destroy(&genl_data->genl_data_mutex); |
| 707 | } |
| 708 | |
| 709 | static struct notifier_block nl_notifier = { |
| 710 | .notifier_call = nfc_genl_rcv_nl_event, |
| 711 | }; |
| 712 | |
| 713 | /** |
| 714 | * nfc_genl_init() - Initialize netlink interface |
| 715 | * |
| 716 | * This initialization function registers the nfc netlink family. |
| 717 | */ |
| 718 | int __init nfc_genl_init(void) |
| 719 | { |
| 720 | int rc; |
| 721 | |
| 722 | rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, |
Samuel Ortiz | 0a40acb | 2012-03-05 01:03:53 +0100 | [diff] [blame] | 723 | ARRAY_SIZE(nfc_genl_ops)); |
Lauro Ramos Venancio | 4d12b8b | 2011-07-01 19:31:34 -0300 | [diff] [blame] | 724 | if (rc) |
| 725 | return rc; |
| 726 | |
| 727 | rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp); |
| 728 | |
| 729 | netlink_register_notifier(&nl_notifier); |
| 730 | |
| 731 | return rc; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * nfc_genl_exit() - Deinitialize netlink interface |
| 736 | * |
| 737 | * This exit function unregisters the nfc netlink family. |
| 738 | */ |
| 739 | void nfc_genl_exit(void) |
| 740 | { |
| 741 | netlink_unregister_notifier(&nl_notifier); |
| 742 | genl_unregister_family(&nfc_genl_family); |
| 743 | } |