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