netlink.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. #include "llcp/llcp.h"
  29. static struct genl_multicast_group nfc_genl_event_mcgrp = {
  30. .name = NFC_GENL_MCAST_EVENT_NAME,
  31. };
  32. static struct genl_family nfc_genl_family = {
  33. .id = GENL_ID_GENERATE,
  34. .hdrsize = 0,
  35. .name = NFC_GENL_NAME,
  36. .version = NFC_GENL_VERSION,
  37. .maxattr = NFC_ATTR_MAX,
  38. };
  39. static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
  40. [NFC_ATTR_DEVICE_INDEX] = { .type = NLA_U32 },
  41. [NFC_ATTR_DEVICE_NAME] = { .type = NLA_STRING,
  42. .len = NFC_DEVICE_NAME_MAXSIZE },
  43. [NFC_ATTR_PROTOCOLS] = { .type = NLA_U32 },
  44. [NFC_ATTR_COMM_MODE] = { .type = NLA_U8 },
  45. [NFC_ATTR_RF_MODE] = { .type = NLA_U8 },
  46. [NFC_ATTR_DEVICE_POWERED] = { .type = NLA_U8 },
  47. [NFC_ATTR_IM_PROTOCOLS] = { .type = NLA_U32 },
  48. [NFC_ATTR_TM_PROTOCOLS] = { .type = NLA_U32 },
  49. };
  50. static int nfc_genl_send_target(struct sk_buff *msg, struct nfc_target *target,
  51. struct netlink_callback *cb, int flags)
  52. {
  53. void *hdr;
  54. hdr = genlmsg_put(msg, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  55. &nfc_genl_family, flags, NFC_CMD_GET_TARGET);
  56. if (!hdr)
  57. return -EMSGSIZE;
  58. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  59. if (nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target->idx) ||
  60. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, target->supported_protocols) ||
  61. nla_put_u16(msg, NFC_ATTR_TARGET_SENS_RES, target->sens_res) ||
  62. nla_put_u8(msg, NFC_ATTR_TARGET_SEL_RES, target->sel_res))
  63. goto nla_put_failure;
  64. if (target->nfcid1_len > 0 &&
  65. nla_put(msg, NFC_ATTR_TARGET_NFCID1, target->nfcid1_len,
  66. target->nfcid1))
  67. goto nla_put_failure;
  68. if (target->sensb_res_len > 0 &&
  69. nla_put(msg, NFC_ATTR_TARGET_SENSB_RES, target->sensb_res_len,
  70. target->sensb_res))
  71. goto nla_put_failure;
  72. if (target->sensf_res_len > 0 &&
  73. nla_put(msg, NFC_ATTR_TARGET_SENSF_RES, target->sensf_res_len,
  74. target->sensf_res))
  75. goto nla_put_failure;
  76. return genlmsg_end(msg, hdr);
  77. nla_put_failure:
  78. genlmsg_cancel(msg, hdr);
  79. return -EMSGSIZE;
  80. }
  81. static struct nfc_dev *__get_device_from_cb(struct netlink_callback *cb)
  82. {
  83. struct nfc_dev *dev;
  84. int rc;
  85. u32 idx;
  86. rc = nlmsg_parse(cb->nlh, GENL_HDRLEN + nfc_genl_family.hdrsize,
  87. nfc_genl_family.attrbuf,
  88. nfc_genl_family.maxattr,
  89. nfc_genl_policy);
  90. if (rc < 0)
  91. return ERR_PTR(rc);
  92. if (!nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX])
  93. return ERR_PTR(-EINVAL);
  94. idx = nla_get_u32(nfc_genl_family.attrbuf[NFC_ATTR_DEVICE_INDEX]);
  95. dev = nfc_get_device(idx);
  96. if (!dev)
  97. return ERR_PTR(-ENODEV);
  98. return dev;
  99. }
  100. static int nfc_genl_dump_targets(struct sk_buff *skb,
  101. struct netlink_callback *cb)
  102. {
  103. int i = cb->args[0];
  104. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  105. int rc;
  106. if (!dev) {
  107. dev = __get_device_from_cb(cb);
  108. if (IS_ERR(dev))
  109. return PTR_ERR(dev);
  110. cb->args[1] = (long) dev;
  111. }
  112. device_lock(&dev->dev);
  113. cb->seq = dev->targets_generation;
  114. while (i < dev->n_targets) {
  115. rc = nfc_genl_send_target(skb, &dev->targets[i], cb,
  116. NLM_F_MULTI);
  117. if (rc < 0)
  118. break;
  119. i++;
  120. }
  121. device_unlock(&dev->dev);
  122. cb->args[0] = i;
  123. return skb->len;
  124. }
  125. static int nfc_genl_dump_targets_done(struct netlink_callback *cb)
  126. {
  127. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  128. if (dev)
  129. nfc_put_device(dev);
  130. return 0;
  131. }
  132. int nfc_genl_targets_found(struct nfc_dev *dev)
  133. {
  134. struct sk_buff *msg;
  135. void *hdr;
  136. dev->genl_data.poll_req_portid = 0;
  137. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  138. if (!msg)
  139. return -ENOMEM;
  140. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  141. NFC_EVENT_TARGETS_FOUND);
  142. if (!hdr)
  143. goto free_msg;
  144. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  145. goto nla_put_failure;
  146. genlmsg_end(msg, hdr);
  147. return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  148. nla_put_failure:
  149. genlmsg_cancel(msg, hdr);
  150. free_msg:
  151. nlmsg_free(msg);
  152. return -EMSGSIZE;
  153. }
  154. int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
  155. {
  156. struct sk_buff *msg;
  157. void *hdr;
  158. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  159. if (!msg)
  160. return -ENOMEM;
  161. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  162. NFC_EVENT_TARGET_LOST);
  163. if (!hdr)
  164. goto free_msg;
  165. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  166. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  167. goto nla_put_failure;
  168. genlmsg_end(msg, hdr);
  169. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  170. return 0;
  171. nla_put_failure:
  172. genlmsg_cancel(msg, hdr);
  173. free_msg:
  174. nlmsg_free(msg);
  175. return -EMSGSIZE;
  176. }
  177. int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
  178. {
  179. struct sk_buff *msg;
  180. void *hdr;
  181. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  182. if (!msg)
  183. return -ENOMEM;
  184. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  185. NFC_EVENT_TM_ACTIVATED);
  186. if (!hdr)
  187. goto free_msg;
  188. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  189. goto nla_put_failure;
  190. if (nla_put_u32(msg, NFC_ATTR_TM_PROTOCOLS, protocol))
  191. goto nla_put_failure;
  192. genlmsg_end(msg, hdr);
  193. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  194. return 0;
  195. nla_put_failure:
  196. genlmsg_cancel(msg, hdr);
  197. free_msg:
  198. nlmsg_free(msg);
  199. return -EMSGSIZE;
  200. }
  201. int nfc_genl_tm_deactivated(struct nfc_dev *dev)
  202. {
  203. struct sk_buff *msg;
  204. void *hdr;
  205. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  206. if (!msg)
  207. return -ENOMEM;
  208. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  209. NFC_EVENT_TM_DEACTIVATED);
  210. if (!hdr)
  211. goto free_msg;
  212. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  213. goto nla_put_failure;
  214. genlmsg_end(msg, hdr);
  215. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  216. return 0;
  217. nla_put_failure:
  218. genlmsg_cancel(msg, hdr);
  219. free_msg:
  220. nlmsg_free(msg);
  221. return -EMSGSIZE;
  222. }
  223. int nfc_genl_device_added(struct nfc_dev *dev)
  224. {
  225. struct sk_buff *msg;
  226. void *hdr;
  227. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  228. if (!msg)
  229. return -ENOMEM;
  230. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  231. NFC_EVENT_DEVICE_ADDED);
  232. if (!hdr)
  233. goto free_msg;
  234. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  235. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  236. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  237. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up))
  238. goto nla_put_failure;
  239. genlmsg_end(msg, hdr);
  240. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  241. return 0;
  242. nla_put_failure:
  243. genlmsg_cancel(msg, hdr);
  244. free_msg:
  245. nlmsg_free(msg);
  246. return -EMSGSIZE;
  247. }
  248. int nfc_genl_device_removed(struct nfc_dev *dev)
  249. {
  250. struct sk_buff *msg;
  251. void *hdr;
  252. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  253. if (!msg)
  254. return -ENOMEM;
  255. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  256. NFC_EVENT_DEVICE_REMOVED);
  257. if (!hdr)
  258. goto free_msg;
  259. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  260. goto nla_put_failure;
  261. genlmsg_end(msg, hdr);
  262. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL);
  263. return 0;
  264. nla_put_failure:
  265. genlmsg_cancel(msg, hdr);
  266. free_msg:
  267. nlmsg_free(msg);
  268. return -EMSGSIZE;
  269. }
  270. static int nfc_genl_send_device(struct sk_buff *msg, struct nfc_dev *dev,
  271. u32 portid, u32 seq,
  272. struct netlink_callback *cb,
  273. int flags)
  274. {
  275. void *hdr;
  276. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, flags,
  277. NFC_CMD_GET_DEVICE);
  278. if (!hdr)
  279. return -EMSGSIZE;
  280. if (cb)
  281. genl_dump_check_consistent(cb, hdr, &nfc_genl_family);
  282. if (nla_put_string(msg, NFC_ATTR_DEVICE_NAME, nfc_device_name(dev)) ||
  283. nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx) ||
  284. nla_put_u32(msg, NFC_ATTR_PROTOCOLS, dev->supported_protocols) ||
  285. nla_put_u32(msg, NFC_ATTR_SE, dev->supported_se) ||
  286. nla_put_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
  287. nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
  288. goto nla_put_failure;
  289. return genlmsg_end(msg, hdr);
  290. nla_put_failure:
  291. genlmsg_cancel(msg, hdr);
  292. return -EMSGSIZE;
  293. }
  294. static int nfc_genl_dump_devices(struct sk_buff *skb,
  295. struct netlink_callback *cb)
  296. {
  297. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  298. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  299. bool first_call = false;
  300. if (!iter) {
  301. first_call = true;
  302. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  303. if (!iter)
  304. return -ENOMEM;
  305. cb->args[0] = (long) iter;
  306. }
  307. mutex_lock(&nfc_devlist_mutex);
  308. cb->seq = nfc_devlist_generation;
  309. if (first_call) {
  310. nfc_device_iter_init(iter);
  311. dev = nfc_device_iter_next(iter);
  312. }
  313. while (dev) {
  314. int rc;
  315. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
  316. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  317. if (rc < 0)
  318. break;
  319. dev = nfc_device_iter_next(iter);
  320. }
  321. mutex_unlock(&nfc_devlist_mutex);
  322. cb->args[1] = (long) dev;
  323. return skb->len;
  324. }
  325. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  326. {
  327. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  328. nfc_device_iter_exit(iter);
  329. kfree(iter);
  330. return 0;
  331. }
  332. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  333. u8 comm_mode, u8 rf_mode)
  334. {
  335. struct sk_buff *msg;
  336. void *hdr;
  337. pr_debug("DEP link is up\n");
  338. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  339. if (!msg)
  340. return -ENOMEM;
  341. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
  342. if (!hdr)
  343. goto free_msg;
  344. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  345. goto nla_put_failure;
  346. if (rf_mode == NFC_RF_INITIATOR &&
  347. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  348. goto nla_put_failure;
  349. if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
  350. nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
  351. goto nla_put_failure;
  352. genlmsg_end(msg, hdr);
  353. dev->dep_link_up = true;
  354. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  355. return 0;
  356. nla_put_failure:
  357. genlmsg_cancel(msg, hdr);
  358. free_msg:
  359. nlmsg_free(msg);
  360. return -EMSGSIZE;
  361. }
  362. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  363. {
  364. struct sk_buff *msg;
  365. void *hdr;
  366. pr_debug("DEP link is down\n");
  367. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  368. if (!msg)
  369. return -ENOMEM;
  370. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  371. NFC_CMD_DEP_LINK_DOWN);
  372. if (!hdr)
  373. goto free_msg;
  374. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  375. goto nla_put_failure;
  376. genlmsg_end(msg, hdr);
  377. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  378. return 0;
  379. nla_put_failure:
  380. genlmsg_cancel(msg, hdr);
  381. free_msg:
  382. nlmsg_free(msg);
  383. return -EMSGSIZE;
  384. }
  385. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  386. {
  387. struct sk_buff *msg;
  388. struct nfc_dev *dev;
  389. u32 idx;
  390. int rc = -ENOBUFS;
  391. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  392. return -EINVAL;
  393. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  394. dev = nfc_get_device(idx);
  395. if (!dev)
  396. return -ENODEV;
  397. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  398. if (!msg) {
  399. rc = -ENOMEM;
  400. goto out_putdev;
  401. }
  402. rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
  403. NULL, 0);
  404. if (rc < 0)
  405. goto out_free;
  406. nfc_put_device(dev);
  407. return genlmsg_reply(msg, info);
  408. out_free:
  409. nlmsg_free(msg);
  410. out_putdev:
  411. nfc_put_device(dev);
  412. return rc;
  413. }
  414. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  415. {
  416. struct nfc_dev *dev;
  417. int rc;
  418. u32 idx;
  419. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  420. return -EINVAL;
  421. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  422. dev = nfc_get_device(idx);
  423. if (!dev)
  424. return -ENODEV;
  425. rc = nfc_dev_up(dev);
  426. nfc_put_device(dev);
  427. return rc;
  428. }
  429. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  430. {
  431. struct nfc_dev *dev;
  432. int rc;
  433. u32 idx;
  434. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  435. return -EINVAL;
  436. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  437. dev = nfc_get_device(idx);
  438. if (!dev)
  439. return -ENODEV;
  440. rc = nfc_dev_down(dev);
  441. nfc_put_device(dev);
  442. return rc;
  443. }
  444. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  445. {
  446. struct nfc_dev *dev;
  447. int rc;
  448. u32 idx;
  449. u32 im_protocols = 0, tm_protocols = 0;
  450. pr_debug("Poll start\n");
  451. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  452. ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
  453. !info->attrs[NFC_ATTR_PROTOCOLS]) &&
  454. !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
  455. return -EINVAL;
  456. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  457. if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
  458. tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
  459. if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
  460. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
  461. else if (info->attrs[NFC_ATTR_PROTOCOLS])
  462. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  463. dev = nfc_get_device(idx);
  464. if (!dev)
  465. return -ENODEV;
  466. mutex_lock(&dev->genl_data.genl_data_mutex);
  467. rc = nfc_start_poll(dev, im_protocols, tm_protocols);
  468. if (!rc)
  469. dev->genl_data.poll_req_portid = info->snd_portid;
  470. mutex_unlock(&dev->genl_data.genl_data_mutex);
  471. nfc_put_device(dev);
  472. return rc;
  473. }
  474. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  475. {
  476. struct nfc_dev *dev;
  477. int rc;
  478. u32 idx;
  479. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  480. return -EINVAL;
  481. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  482. dev = nfc_get_device(idx);
  483. if (!dev)
  484. return -ENODEV;
  485. device_lock(&dev->dev);
  486. if (!dev->polling) {
  487. device_unlock(&dev->dev);
  488. return -EINVAL;
  489. }
  490. device_unlock(&dev->dev);
  491. mutex_lock(&dev->genl_data.genl_data_mutex);
  492. if (dev->genl_data.poll_req_portid != info->snd_portid) {
  493. rc = -EBUSY;
  494. goto out;
  495. }
  496. rc = nfc_stop_poll(dev);
  497. dev->genl_data.poll_req_portid = 0;
  498. out:
  499. mutex_unlock(&dev->genl_data.genl_data_mutex);
  500. nfc_put_device(dev);
  501. return rc;
  502. }
  503. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  504. {
  505. struct nfc_dev *dev;
  506. int rc, tgt_idx;
  507. u32 idx;
  508. u8 comm;
  509. pr_debug("DEP link up\n");
  510. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  511. !info->attrs[NFC_ATTR_COMM_MODE])
  512. return -EINVAL;
  513. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  514. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  515. tgt_idx = NFC_TARGET_IDX_ANY;
  516. else
  517. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  518. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  519. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  520. return -EINVAL;
  521. dev = nfc_get_device(idx);
  522. if (!dev)
  523. return -ENODEV;
  524. rc = nfc_dep_link_up(dev, tgt_idx, comm);
  525. nfc_put_device(dev);
  526. return rc;
  527. }
  528. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  529. {
  530. struct nfc_dev *dev;
  531. int rc;
  532. u32 idx;
  533. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  534. return -EINVAL;
  535. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  536. dev = nfc_get_device(idx);
  537. if (!dev)
  538. return -ENODEV;
  539. rc = nfc_dep_link_down(dev);
  540. nfc_put_device(dev);
  541. return rc;
  542. }
  543. static int nfc_genl_send_params(struct sk_buff *msg,
  544. struct nfc_llcp_local *local,
  545. u32 portid, u32 seq)
  546. {
  547. void *hdr;
  548. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
  549. NFC_CMD_LLC_GET_PARAMS);
  550. if (!hdr)
  551. return -EMSGSIZE;
  552. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
  553. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
  554. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
  555. nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
  556. goto nla_put_failure;
  557. return genlmsg_end(msg, hdr);
  558. nla_put_failure:
  559. genlmsg_cancel(msg, hdr);
  560. return -EMSGSIZE;
  561. }
  562. static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
  563. {
  564. struct nfc_dev *dev;
  565. struct nfc_llcp_local *local;
  566. int rc = 0;
  567. struct sk_buff *msg = NULL;
  568. u32 idx;
  569. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  570. return -EINVAL;
  571. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  572. dev = nfc_get_device(idx);
  573. if (!dev)
  574. return -ENODEV;
  575. device_lock(&dev->dev);
  576. local = nfc_llcp_find_local(dev);
  577. if (!local) {
  578. rc = -ENODEV;
  579. goto exit;
  580. }
  581. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  582. if (!msg) {
  583. rc = -ENOMEM;
  584. goto exit;
  585. }
  586. rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
  587. exit:
  588. device_unlock(&dev->dev);
  589. nfc_put_device(dev);
  590. if (rc < 0) {
  591. if (msg)
  592. nlmsg_free(msg);
  593. return rc;
  594. }
  595. return genlmsg_reply(msg, info);
  596. }
  597. static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
  598. {
  599. struct nfc_dev *dev;
  600. struct nfc_llcp_local *local;
  601. u8 rw = 0;
  602. u16 miux = 0;
  603. u32 idx;
  604. int rc = 0;
  605. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  606. (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
  607. !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
  608. !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
  609. return -EINVAL;
  610. if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
  611. rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
  612. if (rw > LLCP_MAX_RW)
  613. return -EINVAL;
  614. }
  615. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
  616. miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
  617. if (miux > LLCP_MAX_MIUX)
  618. return -EINVAL;
  619. }
  620. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  621. dev = nfc_get_device(idx);
  622. if (!dev)
  623. return -ENODEV;
  624. device_lock(&dev->dev);
  625. local = nfc_llcp_find_local(dev);
  626. if (!local) {
  627. nfc_put_device(dev);
  628. rc = -ENODEV;
  629. goto exit;
  630. }
  631. if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
  632. if (dev->dep_link_up) {
  633. rc = -EINPROGRESS;
  634. goto exit;
  635. }
  636. local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
  637. }
  638. if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
  639. local->rw = rw;
  640. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
  641. local->miux = cpu_to_be16(miux);
  642. exit:
  643. device_unlock(&dev->dev);
  644. nfc_put_device(dev);
  645. return rc;
  646. }
  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. {
  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. {
  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. {
  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. {
  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. .cmd = NFC_CMD_LLC_GET_PARAMS,
  693. .doit = nfc_genl_llc_get_params,
  694. .policy = nfc_genl_policy,
  695. },
  696. {
  697. .cmd = NFC_CMD_LLC_SET_PARAMS,
  698. .doit = nfc_genl_llc_set_params,
  699. .policy = nfc_genl_policy,
  700. },
  701. };
  702. struct urelease_work {
  703. struct work_struct w;
  704. int portid;
  705. };
  706. static void nfc_urelease_event_work(struct work_struct *work)
  707. {
  708. struct urelease_work *w = container_of(work, struct urelease_work, w);
  709. struct class_dev_iter iter;
  710. struct nfc_dev *dev;
  711. pr_debug("portid %d\n", w->portid);
  712. mutex_lock(&nfc_devlist_mutex);
  713. nfc_device_iter_init(&iter);
  714. dev = nfc_device_iter_next(&iter);
  715. while (dev) {
  716. mutex_lock(&dev->genl_data.genl_data_mutex);
  717. if (dev->genl_data.poll_req_portid == w->portid) {
  718. nfc_stop_poll(dev);
  719. dev->genl_data.poll_req_portid = 0;
  720. }
  721. mutex_unlock(&dev->genl_data.genl_data_mutex);
  722. dev = nfc_device_iter_next(&iter);
  723. }
  724. nfc_device_iter_exit(&iter);
  725. mutex_unlock(&nfc_devlist_mutex);
  726. kfree(w);
  727. }
  728. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  729. unsigned long event, void *ptr)
  730. {
  731. struct netlink_notify *n = ptr;
  732. struct urelease_work *w;
  733. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  734. goto out;
  735. pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
  736. w = kmalloc(sizeof(*w), GFP_ATOMIC);
  737. if (w) {
  738. INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
  739. w->portid = n->portid;
  740. schedule_work((struct work_struct *) w);
  741. }
  742. out:
  743. return NOTIFY_DONE;
  744. }
  745. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  746. {
  747. genl_data->poll_req_portid = 0;
  748. mutex_init(&genl_data->genl_data_mutex);
  749. }
  750. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  751. {
  752. mutex_destroy(&genl_data->genl_data_mutex);
  753. }
  754. static struct notifier_block nl_notifier = {
  755. .notifier_call = nfc_genl_rcv_nl_event,
  756. };
  757. /**
  758. * nfc_genl_init() - Initialize netlink interface
  759. *
  760. * This initialization function registers the nfc netlink family.
  761. */
  762. int __init nfc_genl_init(void)
  763. {
  764. int rc;
  765. rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
  766. ARRAY_SIZE(nfc_genl_ops));
  767. if (rc)
  768. return rc;
  769. rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
  770. netlink_register_notifier(&nl_notifier);
  771. return rc;
  772. }
  773. /**
  774. * nfc_genl_exit() - Deinitialize netlink interface
  775. *
  776. * This exit function unregisters the nfc netlink family.
  777. */
  778. void nfc_genl_exit(void)
  779. {
  780. netlink_unregister_notifier(&nl_notifier);
  781. genl_unregister_family(&nfc_genl_family);
  782. }