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