netlink.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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_u8(msg, NFC_ATTR_DEVICE_POWERED, dev->dev_up) ||
  286. nla_put_u8(msg, NFC_ATTR_RF_MODE, dev->rf_mode))
  287. goto nla_put_failure;
  288. return genlmsg_end(msg, hdr);
  289. nla_put_failure:
  290. genlmsg_cancel(msg, hdr);
  291. return -EMSGSIZE;
  292. }
  293. static int nfc_genl_dump_devices(struct sk_buff *skb,
  294. struct netlink_callback *cb)
  295. {
  296. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  297. struct nfc_dev *dev = (struct nfc_dev *) cb->args[1];
  298. bool first_call = false;
  299. if (!iter) {
  300. first_call = true;
  301. iter = kmalloc(sizeof(struct class_dev_iter), GFP_KERNEL);
  302. if (!iter)
  303. return -ENOMEM;
  304. cb->args[0] = (long) iter;
  305. }
  306. mutex_lock(&nfc_devlist_mutex);
  307. cb->seq = nfc_devlist_generation;
  308. if (first_call) {
  309. nfc_device_iter_init(iter);
  310. dev = nfc_device_iter_next(iter);
  311. }
  312. while (dev) {
  313. int rc;
  314. rc = nfc_genl_send_device(skb, dev, NETLINK_CB(cb->skb).portid,
  315. cb->nlh->nlmsg_seq, cb, NLM_F_MULTI);
  316. if (rc < 0)
  317. break;
  318. dev = nfc_device_iter_next(iter);
  319. }
  320. mutex_unlock(&nfc_devlist_mutex);
  321. cb->args[1] = (long) dev;
  322. return skb->len;
  323. }
  324. static int nfc_genl_dump_devices_done(struct netlink_callback *cb)
  325. {
  326. struct class_dev_iter *iter = (struct class_dev_iter *) cb->args[0];
  327. nfc_device_iter_exit(iter);
  328. kfree(iter);
  329. return 0;
  330. }
  331. int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
  332. u8 comm_mode, u8 rf_mode)
  333. {
  334. struct sk_buff *msg;
  335. void *hdr;
  336. pr_debug("DEP link is up\n");
  337. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  338. if (!msg)
  339. return -ENOMEM;
  340. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0, NFC_CMD_DEP_LINK_UP);
  341. if (!hdr)
  342. goto free_msg;
  343. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  344. goto nla_put_failure;
  345. if (rf_mode == NFC_RF_INITIATOR &&
  346. nla_put_u32(msg, NFC_ATTR_TARGET_INDEX, target_idx))
  347. goto nla_put_failure;
  348. if (nla_put_u8(msg, NFC_ATTR_COMM_MODE, comm_mode) ||
  349. nla_put_u8(msg, NFC_ATTR_RF_MODE, rf_mode))
  350. goto nla_put_failure;
  351. genlmsg_end(msg, hdr);
  352. dev->dep_link_up = true;
  353. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  354. return 0;
  355. nla_put_failure:
  356. genlmsg_cancel(msg, hdr);
  357. free_msg:
  358. nlmsg_free(msg);
  359. return -EMSGSIZE;
  360. }
  361. int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
  362. {
  363. struct sk_buff *msg;
  364. void *hdr;
  365. pr_debug("DEP link is down\n");
  366. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  367. if (!msg)
  368. return -ENOMEM;
  369. hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
  370. NFC_CMD_DEP_LINK_DOWN);
  371. if (!hdr)
  372. goto free_msg;
  373. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, dev->idx))
  374. goto nla_put_failure;
  375. genlmsg_end(msg, hdr);
  376. genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC);
  377. return 0;
  378. nla_put_failure:
  379. genlmsg_cancel(msg, hdr);
  380. free_msg:
  381. nlmsg_free(msg);
  382. return -EMSGSIZE;
  383. }
  384. static int nfc_genl_get_device(struct sk_buff *skb, struct genl_info *info)
  385. {
  386. struct sk_buff *msg;
  387. struct nfc_dev *dev;
  388. u32 idx;
  389. int rc = -ENOBUFS;
  390. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  391. return -EINVAL;
  392. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  393. dev = nfc_get_device(idx);
  394. if (!dev)
  395. return -ENODEV;
  396. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  397. if (!msg) {
  398. rc = -ENOMEM;
  399. goto out_putdev;
  400. }
  401. rc = nfc_genl_send_device(msg, dev, info->snd_portid, info->snd_seq,
  402. NULL, 0);
  403. if (rc < 0)
  404. goto out_free;
  405. nfc_put_device(dev);
  406. return genlmsg_reply(msg, info);
  407. out_free:
  408. nlmsg_free(msg);
  409. out_putdev:
  410. nfc_put_device(dev);
  411. return rc;
  412. }
  413. static int nfc_genl_dev_up(struct sk_buff *skb, struct genl_info *info)
  414. {
  415. struct nfc_dev *dev;
  416. int rc;
  417. u32 idx;
  418. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  419. return -EINVAL;
  420. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  421. dev = nfc_get_device(idx);
  422. if (!dev)
  423. return -ENODEV;
  424. rc = nfc_dev_up(dev);
  425. nfc_put_device(dev);
  426. return rc;
  427. }
  428. static int nfc_genl_dev_down(struct sk_buff *skb, struct genl_info *info)
  429. {
  430. struct nfc_dev *dev;
  431. int rc;
  432. u32 idx;
  433. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  434. return -EINVAL;
  435. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  436. dev = nfc_get_device(idx);
  437. if (!dev)
  438. return -ENODEV;
  439. rc = nfc_dev_down(dev);
  440. nfc_put_device(dev);
  441. return rc;
  442. }
  443. static int nfc_genl_start_poll(struct sk_buff *skb, struct genl_info *info)
  444. {
  445. struct nfc_dev *dev;
  446. int rc;
  447. u32 idx;
  448. u32 im_protocols = 0, tm_protocols = 0;
  449. pr_debug("Poll start\n");
  450. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  451. ((!info->attrs[NFC_ATTR_IM_PROTOCOLS] &&
  452. !info->attrs[NFC_ATTR_PROTOCOLS]) &&
  453. !info->attrs[NFC_ATTR_TM_PROTOCOLS]))
  454. return -EINVAL;
  455. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  456. if (info->attrs[NFC_ATTR_TM_PROTOCOLS])
  457. tm_protocols = nla_get_u32(info->attrs[NFC_ATTR_TM_PROTOCOLS]);
  458. if (info->attrs[NFC_ATTR_IM_PROTOCOLS])
  459. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_IM_PROTOCOLS]);
  460. else if (info->attrs[NFC_ATTR_PROTOCOLS])
  461. im_protocols = nla_get_u32(info->attrs[NFC_ATTR_PROTOCOLS]);
  462. dev = nfc_get_device(idx);
  463. if (!dev)
  464. return -ENODEV;
  465. mutex_lock(&dev->genl_data.genl_data_mutex);
  466. rc = nfc_start_poll(dev, im_protocols, tm_protocols);
  467. if (!rc)
  468. dev->genl_data.poll_req_portid = info->snd_portid;
  469. mutex_unlock(&dev->genl_data.genl_data_mutex);
  470. nfc_put_device(dev);
  471. return rc;
  472. }
  473. static int nfc_genl_stop_poll(struct sk_buff *skb, struct genl_info *info)
  474. {
  475. struct nfc_dev *dev;
  476. int rc;
  477. u32 idx;
  478. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  479. return -EINVAL;
  480. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  481. dev = nfc_get_device(idx);
  482. if (!dev)
  483. return -ENODEV;
  484. device_lock(&dev->dev);
  485. if (!dev->polling) {
  486. device_unlock(&dev->dev);
  487. return -EINVAL;
  488. }
  489. device_unlock(&dev->dev);
  490. mutex_lock(&dev->genl_data.genl_data_mutex);
  491. if (dev->genl_data.poll_req_portid != info->snd_portid) {
  492. rc = -EBUSY;
  493. goto out;
  494. }
  495. rc = nfc_stop_poll(dev);
  496. dev->genl_data.poll_req_portid = 0;
  497. out:
  498. mutex_unlock(&dev->genl_data.genl_data_mutex);
  499. nfc_put_device(dev);
  500. return rc;
  501. }
  502. static int nfc_genl_dep_link_up(struct sk_buff *skb, struct genl_info *info)
  503. {
  504. struct nfc_dev *dev;
  505. int rc, tgt_idx;
  506. u32 idx;
  507. u8 comm;
  508. pr_debug("DEP link up\n");
  509. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  510. !info->attrs[NFC_ATTR_COMM_MODE])
  511. return -EINVAL;
  512. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  513. if (!info->attrs[NFC_ATTR_TARGET_INDEX])
  514. tgt_idx = NFC_TARGET_IDX_ANY;
  515. else
  516. tgt_idx = nla_get_u32(info->attrs[NFC_ATTR_TARGET_INDEX]);
  517. comm = nla_get_u8(info->attrs[NFC_ATTR_COMM_MODE]);
  518. if (comm != NFC_COMM_ACTIVE && comm != NFC_COMM_PASSIVE)
  519. return -EINVAL;
  520. dev = nfc_get_device(idx);
  521. if (!dev)
  522. return -ENODEV;
  523. rc = nfc_dep_link_up(dev, tgt_idx, comm);
  524. nfc_put_device(dev);
  525. return rc;
  526. }
  527. static int nfc_genl_dep_link_down(struct sk_buff *skb, struct genl_info *info)
  528. {
  529. struct nfc_dev *dev;
  530. int rc;
  531. u32 idx;
  532. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  533. return -EINVAL;
  534. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  535. dev = nfc_get_device(idx);
  536. if (!dev)
  537. return -ENODEV;
  538. rc = nfc_dep_link_down(dev);
  539. nfc_put_device(dev);
  540. return rc;
  541. }
  542. static int nfc_genl_send_params(struct sk_buff *msg,
  543. struct nfc_llcp_local *local,
  544. u32 portid, u32 seq)
  545. {
  546. void *hdr;
  547. hdr = genlmsg_put(msg, portid, seq, &nfc_genl_family, 0,
  548. NFC_CMD_LLC_GET_PARAMS);
  549. if (!hdr)
  550. return -EMSGSIZE;
  551. if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, local->dev->idx) ||
  552. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_LTO, local->lto) ||
  553. nla_put_u8(msg, NFC_ATTR_LLC_PARAM_RW, local->rw) ||
  554. nla_put_u16(msg, NFC_ATTR_LLC_PARAM_MIUX, be16_to_cpu(local->miux)))
  555. goto nla_put_failure;
  556. return genlmsg_end(msg, hdr);
  557. nla_put_failure:
  558. genlmsg_cancel(msg, hdr);
  559. return -EMSGSIZE;
  560. }
  561. static int nfc_genl_llc_get_params(struct sk_buff *skb, struct genl_info *info)
  562. {
  563. struct nfc_dev *dev;
  564. struct nfc_llcp_local *local;
  565. int rc = 0;
  566. struct sk_buff *msg = NULL;
  567. u32 idx;
  568. if (!info->attrs[NFC_ATTR_DEVICE_INDEX])
  569. return -EINVAL;
  570. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  571. dev = nfc_get_device(idx);
  572. if (!dev)
  573. return -ENODEV;
  574. device_lock(&dev->dev);
  575. local = nfc_llcp_find_local(dev);
  576. if (!local) {
  577. rc = -ENODEV;
  578. goto exit;
  579. }
  580. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  581. if (!msg) {
  582. rc = -ENOMEM;
  583. goto exit;
  584. }
  585. rc = nfc_genl_send_params(msg, local, info->snd_portid, info->snd_seq);
  586. exit:
  587. device_unlock(&dev->dev);
  588. nfc_put_device(dev);
  589. if (rc < 0) {
  590. if (msg)
  591. nlmsg_free(msg);
  592. return rc;
  593. }
  594. return genlmsg_reply(msg, info);
  595. }
  596. static int nfc_genl_llc_set_params(struct sk_buff *skb, struct genl_info *info)
  597. {
  598. struct nfc_dev *dev;
  599. struct nfc_llcp_local *local;
  600. u8 rw = 0;
  601. u16 miux = 0;
  602. u32 idx;
  603. int rc = 0;
  604. if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
  605. (!info->attrs[NFC_ATTR_LLC_PARAM_LTO] &&
  606. !info->attrs[NFC_ATTR_LLC_PARAM_RW] &&
  607. !info->attrs[NFC_ATTR_LLC_PARAM_MIUX]))
  608. return -EINVAL;
  609. if (info->attrs[NFC_ATTR_LLC_PARAM_RW]) {
  610. rw = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_RW]);
  611. if (rw > LLCP_MAX_RW)
  612. return -EINVAL;
  613. }
  614. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX]) {
  615. miux = nla_get_u16(info->attrs[NFC_ATTR_LLC_PARAM_MIUX]);
  616. if (miux > LLCP_MAX_MIUX)
  617. return -EINVAL;
  618. }
  619. idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
  620. dev = nfc_get_device(idx);
  621. if (!dev)
  622. return -ENODEV;
  623. device_lock(&dev->dev);
  624. local = nfc_llcp_find_local(dev);
  625. if (!local) {
  626. nfc_put_device(dev);
  627. rc = -ENODEV;
  628. goto exit;
  629. }
  630. if (info->attrs[NFC_ATTR_LLC_PARAM_LTO]) {
  631. if (dev->dep_link_up) {
  632. rc = -EINPROGRESS;
  633. goto exit;
  634. }
  635. local->lto = nla_get_u8(info->attrs[NFC_ATTR_LLC_PARAM_LTO]);
  636. }
  637. if (info->attrs[NFC_ATTR_LLC_PARAM_RW])
  638. local->rw = rw;
  639. if (info->attrs[NFC_ATTR_LLC_PARAM_MIUX])
  640. local->miux = cpu_to_be16(miux);
  641. exit:
  642. device_unlock(&dev->dev);
  643. nfc_put_device(dev);
  644. return rc;
  645. }
  646. static struct genl_ops nfc_genl_ops[] = {
  647. {
  648. .cmd = NFC_CMD_GET_DEVICE,
  649. .doit = nfc_genl_get_device,
  650. .dumpit = nfc_genl_dump_devices,
  651. .done = nfc_genl_dump_devices_done,
  652. .policy = nfc_genl_policy,
  653. },
  654. {
  655. .cmd = NFC_CMD_DEV_UP,
  656. .doit = nfc_genl_dev_up,
  657. .policy = nfc_genl_policy,
  658. },
  659. {
  660. .cmd = NFC_CMD_DEV_DOWN,
  661. .doit = nfc_genl_dev_down,
  662. .policy = nfc_genl_policy,
  663. },
  664. {
  665. .cmd = NFC_CMD_START_POLL,
  666. .doit = nfc_genl_start_poll,
  667. .policy = nfc_genl_policy,
  668. },
  669. {
  670. .cmd = NFC_CMD_STOP_POLL,
  671. .doit = nfc_genl_stop_poll,
  672. .policy = nfc_genl_policy,
  673. },
  674. {
  675. .cmd = NFC_CMD_DEP_LINK_UP,
  676. .doit = nfc_genl_dep_link_up,
  677. .policy = nfc_genl_policy,
  678. },
  679. {
  680. .cmd = NFC_CMD_DEP_LINK_DOWN,
  681. .doit = nfc_genl_dep_link_down,
  682. .policy = nfc_genl_policy,
  683. },
  684. {
  685. .cmd = NFC_CMD_GET_TARGET,
  686. .dumpit = nfc_genl_dump_targets,
  687. .done = nfc_genl_dump_targets_done,
  688. .policy = nfc_genl_policy,
  689. },
  690. {
  691. .cmd = NFC_CMD_LLC_GET_PARAMS,
  692. .doit = nfc_genl_llc_get_params,
  693. .policy = nfc_genl_policy,
  694. },
  695. {
  696. .cmd = NFC_CMD_LLC_SET_PARAMS,
  697. .doit = nfc_genl_llc_set_params,
  698. .policy = nfc_genl_policy,
  699. },
  700. };
  701. struct urelease_work {
  702. struct work_struct w;
  703. int portid;
  704. };
  705. static void nfc_urelease_event_work(struct work_struct *work)
  706. {
  707. struct urelease_work *w = container_of(work, struct urelease_work, w);
  708. struct class_dev_iter iter;
  709. struct nfc_dev *dev;
  710. pr_debug("portid %d\n", w->portid);
  711. mutex_lock(&nfc_devlist_mutex);
  712. nfc_device_iter_init(&iter);
  713. dev = nfc_device_iter_next(&iter);
  714. while (dev) {
  715. mutex_lock(&dev->genl_data.genl_data_mutex);
  716. if (dev->genl_data.poll_req_portid == w->portid) {
  717. nfc_stop_poll(dev);
  718. dev->genl_data.poll_req_portid = 0;
  719. }
  720. mutex_unlock(&dev->genl_data.genl_data_mutex);
  721. dev = nfc_device_iter_next(&iter);
  722. }
  723. nfc_device_iter_exit(&iter);
  724. mutex_unlock(&nfc_devlist_mutex);
  725. kfree(w);
  726. }
  727. static int nfc_genl_rcv_nl_event(struct notifier_block *this,
  728. unsigned long event, void *ptr)
  729. {
  730. struct netlink_notify *n = ptr;
  731. struct urelease_work *w;
  732. if (event != NETLINK_URELEASE || n->protocol != NETLINK_GENERIC)
  733. goto out;
  734. pr_debug("NETLINK_URELEASE event from id %d\n", n->portid);
  735. w = kmalloc(sizeof(*w), GFP_ATOMIC);
  736. if (w) {
  737. INIT_WORK((struct work_struct *) w, nfc_urelease_event_work);
  738. w->portid = n->portid;
  739. schedule_work((struct work_struct *) w);
  740. }
  741. out:
  742. return NOTIFY_DONE;
  743. }
  744. void nfc_genl_data_init(struct nfc_genl_data *genl_data)
  745. {
  746. genl_data->poll_req_portid = 0;
  747. mutex_init(&genl_data->genl_data_mutex);
  748. }
  749. void nfc_genl_data_exit(struct nfc_genl_data *genl_data)
  750. {
  751. mutex_destroy(&genl_data->genl_data_mutex);
  752. }
  753. static struct notifier_block nl_notifier = {
  754. .notifier_call = nfc_genl_rcv_nl_event,
  755. };
  756. /**
  757. * nfc_genl_init() - Initialize netlink interface
  758. *
  759. * This initialization function registers the nfc netlink family.
  760. */
  761. int __init nfc_genl_init(void)
  762. {
  763. int rc;
  764. rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops,
  765. ARRAY_SIZE(nfc_genl_ops));
  766. if (rc)
  767. return rc;
  768. rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
  769. netlink_register_notifier(&nl_notifier);
  770. return rc;
  771. }
  772. /**
  773. * nfc_genl_exit() - Deinitialize netlink interface
  774. *
  775. * This exit function unregisters the nfc netlink family.
  776. */
  777. void nfc_genl_exit(void)
  778. {
  779. netlink_unregister_notifier(&nl_notifier);
  780. genl_unregister_family(&nfc_genl_family);
  781. }