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