netlink.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
  24. #include <net/genetlink.h>
  25. #include <linux/nfc.h>
  26. #include <linux/slab.h>
  27. #include "nfc.h"
  28. static struct genl_multicast_group nfc_genl_event_mcgrp = {
  29. .name = NFC_GENL_MCAST_EVENT_NAME,
  30. };
  31. static struct genl_family nfc_genl_family = {
  32. .id = GENL_ID_GENERATE,
  33. .hdrsize = 0,
  34. .name = NFC_GENL_NAME,
  35. .version = NFC_GENL_VERSION,
  36. .maxattr = NFC_ATTR_MAX,
  37. };
  38. static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  39. [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  40. [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  41. .len = NFC_DEVICE_NAME_MAXSIZE },
  42. [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  43. [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
  44. [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
  45. [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
  46. };
  47. static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  48. struct netlink_callback *cb, int flags)
  49. {
  50. void *hdr;
  51. hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  52. &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
  53. if (!hdr)
  54. return -EMSGSIZE;
  55. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  56. if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
  57. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
  58. nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
  59. nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
  60. goto nla_put_failure;
  61. if (target->nfcid1_len > 0 &&
  62. nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
  63. target->nfcid1))
  64. goto nla_put_failure;
  65. if (target->sensb_res_len > 0 &&
  66. nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
  67. target->sensb_res))
  68. goto nla_put_failure;
  69. if (target->sensf_res_len > 0 &&
  70. nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
  71. target->sensf_res))
  72. goto nla_put_failure;
  73. return genlmsg_end(msg, hdr);
  74. nla_put_failure:
  75. genlmsg_cancel(msg, hdr);
  76. return -EMSGSIZE;
  77. }
  78. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  79. {
  80. struct nfc_dev *dev;
  81. int rc;
  82. u32 idx;
  83. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  84. nfc_genl_family.attrbuf,
  85. nfc_genl_family.maxattr,
  86. nfc_genl_policy);
  87. if (rc < 0)
  88. return ERR_PTR(rc);
  89. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  90. return ERR_PTR(-EINVAL);
  91. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  92. dev = nfc_get_device(idx);
  93. if (!dev)
  94. return ERR_PTR(-ENODEV);
  95. return dev;
  96. }
  97. static int nfc_genl_dump_targets(struct sk_buff *skb,
  98. struct netlink_callback *cb)
  99. {
  100. int i = cb->args[0];
  101. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  102. int rc;
  103. if (!dev) {
  104. dev = __get_device_from_cb(cb);
  105. if (IS_ERR(dev))
  106. return PTR_ERR(dev);
  107. cb->args[1] = (long) dev;
  108. }
  109. device_lock(&dev->dev);
  110. cb->seq = dev->targets_generation;
  111. while (i < dev->n_targets) {
  112. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  113. NLM_F_MULTI);
  114. if (rc < 0)
  115. break;
  116. i++;
  117. }
  118. device_unlock(&dev->dev);
  119. cb->args[0] = i;
  120. return skb->len;
  121. }
  122. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  123. {
  124. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  125. if (dev)
  126. nfc_put_device(dev);
  127. return 0;
  128. }
  129. int nfc_genl_targets_found(struct nfc_dev *dev)
  130. {
  131. struct sk_buff *msg;
  132. void *hdr;
  133. dev->genl_data.poll_req_pid = 0;
  134. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  135. if (!msg)
  136. return -ENOMEM;
  137. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  138. NFC_EVENT_TARGETS_FOUND);
  139. if (!hdr)
  140. goto free_msg;
  141. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  142. goto nla_put_failure;
  143. genlmsg_end(msg, hdr);
  144. return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  145. nla_put_failure:
  146. genlmsg_cancel(msg, hdr);
  147. free_msg:
  148. nlmsg_free(msg);
  149. return -EMSGSIZE;
  150. }
  151. int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
  152. {
  153. struct sk_buff *msg;
  154. void *hdr;
  155. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  156. if (!msg)
  157. return -ENOMEM;
  158. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  159. NFC_EVENT_TARGET_LOST);
  160. if (!hdr)
  161. goto free_msg;
  162. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  163. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  164. goto nla_put_failure;
  165. genlmsg_end(msg, hdr);
  166. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  167. return 0;
  168. nla_put_failure:
  169. genlmsg_cancel(msg, hdr);
  170. free_msg:
  171. nlmsg_free(msg);
  172. return -EMSGSIZE;
  173. }
  174. int nfc_genl_device_added(struct nfc_dev *dev)
  175. {
  176. struct sk_buff *msg;
  177. void *hdr;
  178. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  179. if (!msg)
  180. return -ENOMEM;
  181. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  182. NFC_EVENT_DEVICE_ADDED);
  183. if (!hdr)
  184. goto free_msg;
  185. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  186. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  187. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  188. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
  189. goto nla_put_failure;
  190. genlmsg_end(msg, hdr);
  191. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  192. return 0;
  193. nla_put_failure:
  194. genlmsg_cancel(msg, hdr);
  195. free_msg:
  196. nlmsg_free(msg);
  197. return -EMSGSIZE;
  198. }
  199. int nfc_genl_device_removed(struct nfc_dev *dev)
  200. {
  201. struct sk_buff *msg;
  202. void *hdr;
  203. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  204. if (!msg)
  205. return -ENOMEM;
  206. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  207. NFC_EVENT_DEVICE_REMOVED);
  208. if (!hdr)
  209. goto free_msg;
  210. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  211. goto nla_put_failure;
  212. genlmsg_end(msg, hdr);
  213. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  214. return 0;
  215. nla_put_failure:
  216. genlmsg_cancel(msg, hdr);
  217. free_msg:
  218. nlmsg_free(msg);
  219. return -EMSGSIZE;
  220. }
  221. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  222. u32 pid, u32 seq,
  223. struct netlink_callback *cb,
  224. int flags)
  225. {
  226. void *hdr;
  227. hdr = genlmsg_put(msg, pid, seq, &nfc_genl_family, flags,
  228. NFC_CMD_GET_DEVICE);
  229. if (!hdr)
  230. return -EMSGSIZE;
  231. if (cb)
  232. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  233. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  234. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  235. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  236. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
  237. goto nla_put_failure;
  238. return genlmsg_end(msg, hdr);
  239. nla_put_failure:
  240. genlmsg_cancel(msg, hdr);
  241. return -EMSGSIZE;
  242. }
  243. static int nfc_genl_dump_devices(struct sk_buff *skb,
  244. struct netlink_callback *cb)
  245. {
  246. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  247. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  248. bool first_call = false;
  249. if (!iter) {
  250. first_call = true;
  251. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  252. if (!iter)
  253. return -ENOMEM;
  254. cb->args[0] = (long) iter;
  255. }
  256. mutex_lock(&nfc_devlist_mutex);
  257. cb->seq = nfc_devlist_generation;
  258. if (first_call) {
  259. nfc_device_iter_init(iter);
  260. dev = nfc_device_iter_next(iter);
  261. }
  262. while (dev) {
  263. int rc;
  264. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).pid,
  265. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  266. if (rc < 0)
  267. break;
  268. dev = nfc_device_iter_next(iter);
  269. }
  270. mutex_unlock(&nfc_devlist_mutex);
  271. cb->args[1] = (long) dev;
  272. return skb->len;
  273. }
  274. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  275. {
  276. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  277. nfc_device_iter_exit(iter);
  278. kfree(iter);
  279. return 0;
  280. }
  281. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  282. u8 comm_mode, u8 rf_mode)
  283. {
  284. struct sk_buff *msg;
  285. void *hdr;
  286. pr_debug("DEP link is up\n");
  287. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  288. if (!msg)
  289. return -ENOMEM;
  290. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
  291. if (!hdr)
  292. goto free_msg;
  293. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  294. goto nla_put_failure;
  295. if (rf_mode == NFC_RF_INITIATOR &&
  296. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  297. goto nla_put_failure;
  298. if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
  299. nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
  300. goto nla_put_failure;
  301. genlmsg_end(msg, hdr);
  302. dev->dep_link_up = true;
  303. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  304. return 0;
  305. nla_put_failure:
  306. genlmsg_cancel(msg, hdr);
  307. free_msg:
  308. nlmsg_free(msg);
  309. return -EMSGSIZE;
  310. }
  311. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  312. {
  313. struct sk_buff *msg;
  314. void *hdr;
  315. pr_debug("DEP link is down\n");
  316. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  317. if (!msg)
  318. return -ENOMEM;
  319. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  320. NFC_CMD_DEP_LINK_DOWN);
  321. if (!hdr)
  322. goto free_msg;
  323. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  324. goto nla_put_failure;
  325. genlmsg_end(msg, hdr);
  326. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  327. return 0;
  328. nla_put_failure:
  329. genlmsg_cancel(msg, hdr);
  330. free_msg:
  331. nlmsg_free(msg);
  332. return -EMSGSIZE;
  333. }
  334. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  335. {
  336. struct sk_buff *msg;
  337. struct nfc_dev *dev;
  338. u32 idx;
  339. int rc = -ENOBUFS;
  340. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  341. return -EINVAL;
  342. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  343. dev = nfc_get_device(idx);
  344. if (!dev)
  345. return -ENODEV;
  346. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  347. if (!msg) {
  348. rc = -ENOMEM;
  349. goto out_putdev;
  350. }
  351. rc = nfc_genl_send_device(msg, dev, info->snd_pid, info->snd_seq,
  352. NULL, 0);
  353. if (rc < 0)
  354. goto out_free;
  355. nfc_put_device(dev);
  356. return genlmsg_reply(msg, info);
  357. out_free:
  358. nlmsg_free(msg);
  359. out_putdev:
  360. nfc_put_device(dev);
  361. return rc;
  362. }
  363. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  364. {
  365. struct nfc_dev *dev;
  366. int rc;
  367. u32 idx;
  368. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  369. return -EINVAL;
  370. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  371. dev = nfc_get_device(idx);
  372. if (!dev)
  373. return -ENODEV;
  374. rc = nfc_dev_up(dev);
  375. nfc_put_device(dev);
  376. return rc;
  377. }
  378. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  379. {
  380. struct nfc_dev *dev;
  381. int rc;
  382. u32 idx;
  383. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  384. return -EINVAL;
  385. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  386. dev = nfc_get_device(idx);
  387. if (!dev)
  388. return -ENODEV;
  389. rc = nfc_dev_down(dev);
  390. nfc_put_device(dev);
  391. return rc;
  392. }
  393. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  394. {
  395. struct nfc_dev *dev;
  396. int rc;
  397. u32 idx;
  398. u32 protocols;
  399. pr_debug("Poll start\n");
  400. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  401. !info->attrs[NFC_ATTR_PROTOCOLS])
  402. return -EINVAL;
  403. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  404. protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  405. dev = nfc_get_device(idx);
  406. if (!dev)
  407. return -ENODEV;
  408. mutex_lock(&dev->genl_data.genl_data_mutex);
  409. rc = nfc_start_poll(dev, protocols);
  410. if (!rc)
  411. dev->genl_data.poll_req_pid = info->snd_pid;
  412. mutex_unlock(&dev->genl_data.genl_data_mutex);
  413. nfc_put_device(dev);
  414. return rc;
  415. }
  416. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  417. {
  418. struct nfc_dev *dev;
  419. int rc;
  420. u32 idx;
  421. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  422. return -EINVAL;
  423. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  424. dev = nfc_get_device(idx);
  425. if (!dev)
  426. return -ENODEV;
  427. mutex_lock(&dev->genl_data.genl_data_mutex);
  428. if (dev->genl_data.poll_req_pid != info->snd_pid) {
  429. rc = -EBUSY;
  430. goto out;
  431. }
  432. rc = nfc_stop_poll(dev);
  433. dev->genl_data.poll_req_pid = 0;
  434. out:
  435. mutex_unlock(&dev->genl_data.genl_data_mutex);
  436. nfc_put_device(dev);
  437. return rc;
  438. }
  439. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  440. {
  441. struct nfc_dev *dev;
  442. int rc, tgt_idx;
  443. u32 idx;
  444. u8 comm;
  445. pr_debug("DEP link up\n");
  446. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  447. !info->attrs[NFC_ATTR_COMM_MODE])
  448. return -EINVAL;
  449. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  450. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  451. tgt_idx = NFC_TARGET_IDX_ANY;
  452. else
  453. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  454. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  455. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  456. return -EINVAL;
  457. dev = nfc_get_device(idx);
  458. if (!dev)
  459. return -ENODEV;
  460. rc = nfc_dep_link_up(dev, tgt_idx, comm);
  461. nfc_put_device(dev);
  462. return rc;
  463. }
  464. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  465. {
  466. struct nfc_dev *dev;
  467. int rc;
  468. u32 idx;
  469. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  470. return -EINVAL;
  471. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  472. dev = nfc_get_device(idx);
  473. if (!dev)
  474. return -ENODEV;
  475. rc = nfc_dep_link_down(dev);
  476. nfc_put_device(dev);
  477. return rc;
  478. }
  479. static struct genl_ops nfc_genl_ops[] = {
  480. {
  481. .cmd = NFC_CMD_GET_DEVICE,
  482. .doit = nfc_genl_get_device,
  483. .dumpit = nfc_genl_dump_devices,
  484. .done = nfc_genl_dump_devices_done,
  485. .policy = nfc_genl_policy,
  486. },
  487. {
  488. .cmd = NFC_CMD_DEV_UP,
  489. .doit = nfc_genl_dev_up,
  490. .policy = nfc_genl_policy,
  491. },
  492. {
  493. .cmd = NFC_CMD_DEV_DOWN,
  494. .doit = nfc_genl_dev_down,
  495. .policy = nfc_genl_policy,
  496. },
  497. {
  498. .cmd = NFC_CMD_START_POLL,
  499. .doit = nfc_genl_start_poll,
  500. .policy = nfc_genl_policy,
  501. },
  502. {
  503. .cmd = NFC_CMD_STOP_POLL,
  504. .doit = nfc_genl_stop_poll,
  505. .policy = nfc_genl_policy,
  506. },
  507. {
  508. .cmd = NFC_CMD_DEP_LINK_UP,
  509. .doit = nfc_genl_dep_link_up,
  510. .policy = nfc_genl_policy,
  511. },
  512. {
  513. .cmd = NFC_CMD_DEP_LINK_DOWN,
  514. .doit = nfc_genl_dep_link_down,
  515. .policy = nfc_genl_policy,
  516. },
  517. {
  518. .cmd = NFC_CMD_GET_TARGET,
  519. .dumpit = nfc_genl_dump_targets,
  520. .done = nfc_genl_dump_targets_done,
  521. .policy = nfc_genl_policy,
  522. },
  523. };
  524. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  525. unsigned long event, void *ptr)
  526. {
  527. struct netlink_notify *n = ptr;
  528. struct class_dev_iter iter;
  529. struct nfc_dev *dev;
  530. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  531. goto out;
  532. pr_debug("NETLINK_URELEASE event from id %d\n", n->pid);
  533. nfc_device_iter_init(&iter);
  534. dev = nfc_device_iter_next(&iter);
  535. while (dev) {
  536. if (dev->genl_data.poll_req_pid == n->pid) {
  537. nfc_stop_poll(dev);
  538. dev->genl_data.poll_req_pid = 0;
  539. }
  540. dev = nfc_device_iter_next(&iter);
  541. }
  542. nfc_device_iter_exit(&iter);
  543. out:
  544. return NOTIFY_DONE;
  545. }
  546. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  547. {
  548. genl_data->poll_req_pid = 0;
  549. mutex_init(&genl_data->genl_data_mutex);
  550. }
  551. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  552. {
  553. mutex_destroy(&genl_data->genl_data_mutex);
  554. }
  555. static struct notifier_block nl_notifier = {
  556. .notifier_call = nfc_genl_rcv_nl_event,
  557. };
  558. /**
  559. * nfc_genl_init() - Initialize netlink interface
  560. *
  561. * This initialization function registers the nfc netlink family.
  562. */
  563. int __init nfc_genl_init(void)
  564. {
  565. int rc;
  566. rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
  567. ARRAY_SIZE(nfc_genl_ops));
  568. if (rc)
  569. return rc;
  570. rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
  571. netlink_register_notifier(&nl_notifier);
  572. return rc;
  573. }
  574. /**
  575. * nfc_genl_exit() - Deinitialize netlink interface
  576. *
  577. * This exit function unregisters the nfc netlink family.
  578. */
  579. void nfc_genl_exit(void)
  580. {
  581. netlink_unregister_notifier(&nl_notifier);
  582. genl_unregister_family(&nfc_genl_family);
  583. }