pn533.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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. #include <linux/device.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/usb.h>
  28. #include <linux/nfc.h>
  29. #include <linux/netdevice.h>
  30. #include <net/nfc.h>
  31. #define VERSION "0.1"
  32. #define PN533_VENDOR_ID 0x4CC
  33. #define PN533_PRODUCT_ID 0x2533
  34. #define SCM_VENDOR_ID 0x4E6
  35. #define SCL3711_PRODUCT_ID 0x5591
  36. static const struct usb_device_id pn533_table[] = {
  37. { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID) },
  38. { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID) },
  39. { }
  40. };
  41. MODULE_DEVICE_TABLE(usb, pn533_table);
  42. /* frame definitions */
  43. #define PN533_FRAME_TAIL_SIZE 2
  44. #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
  45. PN533_FRAME_TAIL_SIZE)
  46. #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
  47. #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
  48. #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
  49. /* start of frame */
  50. #define PN533_SOF 0x00FF
  51. /* frame identifier: in/out/error */
  52. #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
  53. #define PN533_DIR_OUT 0xD4
  54. #define PN533_DIR_IN 0xD5
  55. /* PN533 Commands */
  56. #define PN533_FRAME_CMD(f) (f->data[1])
  57. #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
  58. #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
  59. #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
  60. #define PN533_CMD_RF_CONFIGURATION 0x32
  61. #define PN533_CMD_IN_DATA_EXCHANGE 0x40
  62. #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
  63. #define PN533_CMD_IN_ATR 0x50
  64. #define PN533_CMD_IN_RELEASE 0x52
  65. #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
  66. /* PN533 Return codes */
  67. #define PN533_CMD_RET_MASK 0x3F
  68. #define PN533_CMD_MI_MASK 0x40
  69. #define PN533_CMD_RET_SUCCESS 0x00
  70. struct pn533;
  71. typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
  72. u8 *params, int params_len);
  73. /* structs for pn533 commands */
  74. /* PN533_CMD_GET_FIRMWARE_VERSION */
  75. struct pn533_fw_version {
  76. u8 ic;
  77. u8 ver;
  78. u8 rev;
  79. u8 support;
  80. };
  81. /* PN533_CMD_RF_CONFIGURATION */
  82. #define PN533_CFGITEM_MAX_RETRIES 0x05
  83. #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
  84. #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
  85. struct pn533_config_max_retries {
  86. u8 mx_rty_atr;
  87. u8 mx_rty_psl;
  88. u8 mx_rty_passive_act;
  89. } __packed;
  90. /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
  91. /* felica commands opcode */
  92. #define PN533_FELICA_OPC_SENSF_REQ 0
  93. #define PN533_FELICA_OPC_SENSF_RES 1
  94. /* felica SENSF_REQ parameters */
  95. #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
  96. #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
  97. #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
  98. #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
  99. /* type B initiator_data values */
  100. #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
  101. #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
  102. #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
  103. union pn533_cmd_poll_initdata {
  104. struct {
  105. u8 afi;
  106. u8 polling_method;
  107. } __packed type_b;
  108. struct {
  109. u8 opcode;
  110. __be16 sc;
  111. u8 rc;
  112. u8 tsn;
  113. } __packed felica;
  114. };
  115. /* Poll modulations */
  116. enum {
  117. PN533_POLL_MOD_106KBPS_A,
  118. PN533_POLL_MOD_212KBPS_FELICA,
  119. PN533_POLL_MOD_424KBPS_FELICA,
  120. PN533_POLL_MOD_106KBPS_JEWEL,
  121. PN533_POLL_MOD_847KBPS_B,
  122. __PN533_POLL_MOD_AFTER_LAST,
  123. };
  124. #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
  125. struct pn533_poll_modulations {
  126. struct {
  127. u8 maxtg;
  128. u8 brty;
  129. union pn533_cmd_poll_initdata initiator_data;
  130. } __packed data;
  131. u8 len;
  132. };
  133. const struct pn533_poll_modulations poll_mod[] = {
  134. [PN533_POLL_MOD_106KBPS_A] = {
  135. .data = {
  136. .maxtg = 1,
  137. .brty = 0,
  138. },
  139. .len = 2,
  140. },
  141. [PN533_POLL_MOD_212KBPS_FELICA] = {
  142. .data = {
  143. .maxtg = 1,
  144. .brty = 1,
  145. .initiator_data.felica = {
  146. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  147. .sc = PN533_FELICA_SENSF_SC_ALL,
  148. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  149. .tsn = 0,
  150. },
  151. },
  152. .len = 7,
  153. },
  154. [PN533_POLL_MOD_424KBPS_FELICA] = {
  155. .data = {
  156. .maxtg = 1,
  157. .brty = 2,
  158. .initiator_data.felica = {
  159. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  160. .sc = PN533_FELICA_SENSF_SC_ALL,
  161. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  162. .tsn = 0,
  163. },
  164. },
  165. .len = 7,
  166. },
  167. [PN533_POLL_MOD_106KBPS_JEWEL] = {
  168. .data = {
  169. .maxtg = 1,
  170. .brty = 4,
  171. },
  172. .len = 2,
  173. },
  174. [PN533_POLL_MOD_847KBPS_B] = {
  175. .data = {
  176. .maxtg = 1,
  177. .brty = 8,
  178. .initiator_data.type_b = {
  179. .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
  180. .polling_method =
  181. PN533_TYPE_B_POLL_METHOD_TIMESLOT,
  182. },
  183. },
  184. .len = 3,
  185. },
  186. };
  187. /* PN533_CMD_IN_ATR */
  188. struct pn533_cmd_activate_param {
  189. u8 tg;
  190. u8 next;
  191. } __packed;
  192. struct pn533_cmd_activate_response {
  193. u8 status;
  194. u8 nfcid3t[10];
  195. u8 didt;
  196. u8 bst;
  197. u8 brt;
  198. u8 to;
  199. u8 ppt;
  200. /* optional */
  201. u8 gt[];
  202. } __packed;
  203. struct pn533 {
  204. struct usb_device *udev;
  205. struct usb_interface *interface;
  206. struct nfc_dev *nfc_dev;
  207. struct urb *out_urb;
  208. int out_maxlen;
  209. struct pn533_frame *out_frame;
  210. struct urb *in_urb;
  211. int in_maxlen;
  212. struct pn533_frame *in_frame;
  213. struct tasklet_struct tasklet;
  214. struct pn533_frame *tklt_in_frame;
  215. int tklt_in_error;
  216. pn533_cmd_complete_t cmd_complete;
  217. void *cmd_complete_arg;
  218. struct semaphore cmd_lock;
  219. u8 cmd;
  220. struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
  221. u8 poll_mod_count;
  222. u8 poll_mod_curr;
  223. u32 poll_protocols;
  224. u8 tgt_available_prots;
  225. u8 tgt_active_prot;
  226. };
  227. struct pn533_frame {
  228. u8 preamble;
  229. __be16 start_frame;
  230. u8 datalen;
  231. u8 datalen_checksum;
  232. u8 data[];
  233. } __packed;
  234. /* The rule: value + checksum = 0 */
  235. static inline u8 pn533_checksum(u8 value)
  236. {
  237. return ~value + 1;
  238. }
  239. /* The rule: sum(data elements) + checksum = 0 */
  240. static u8 pn533_data_checksum(u8 *data, int datalen)
  241. {
  242. u8 sum = 0;
  243. int i;
  244. for (i = 0; i < datalen; i++)
  245. sum += data[i];
  246. return pn533_checksum(sum);
  247. }
  248. /**
  249. * pn533_tx_frame_ack - create a ack frame
  250. * @frame: The frame to be set as ack
  251. *
  252. * Ack is different type of standard frame. As a standard frame, it has
  253. * preamble and start_frame. However the checksum of this frame must fail,
  254. * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
  255. * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
  256. * After datalen_checksum field, the postamble is placed.
  257. */
  258. static void pn533_tx_frame_ack(struct pn533_frame *frame)
  259. {
  260. frame->preamble = 0;
  261. frame->start_frame = cpu_to_be16(PN533_SOF);
  262. frame->datalen = 0;
  263. frame->datalen_checksum = 0xFF;
  264. /* data[0] is used as postamble */
  265. frame->data[0] = 0;
  266. }
  267. static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
  268. {
  269. frame->preamble = 0;
  270. frame->start_frame = cpu_to_be16(PN533_SOF);
  271. PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
  272. PN533_FRAME_CMD(frame) = cmd;
  273. frame->datalen = 2;
  274. }
  275. static void pn533_tx_frame_finish(struct pn533_frame *frame)
  276. {
  277. frame->datalen_checksum = pn533_checksum(frame->datalen);
  278. PN533_FRAME_CHECKSUM(frame) =
  279. pn533_data_checksum(frame->data, frame->datalen);
  280. PN533_FRAME_POSTAMBLE(frame) = 0;
  281. }
  282. static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
  283. {
  284. u8 checksum;
  285. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  286. return false;
  287. checksum = pn533_checksum(frame->datalen);
  288. if (checksum != frame->datalen_checksum)
  289. return false;
  290. checksum = pn533_data_checksum(frame->data, frame->datalen);
  291. if (checksum != PN533_FRAME_CHECKSUM(frame))
  292. return false;
  293. return true;
  294. }
  295. static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
  296. {
  297. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  298. return false;
  299. if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
  300. return false;
  301. return true;
  302. }
  303. static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
  304. {
  305. return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
  306. }
  307. static void pn533_tasklet_cmd_complete(unsigned long arg)
  308. {
  309. struct pn533 *dev = (struct pn533 *) arg;
  310. struct pn533_frame *in_frame = dev->tklt_in_frame;
  311. int rc;
  312. if (dev->tklt_in_error)
  313. rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
  314. dev->tklt_in_error);
  315. else
  316. rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
  317. PN533_FRAME_CMD_PARAMS_PTR(in_frame),
  318. PN533_FRAME_CMD_PARAMS_LEN(in_frame));
  319. if (rc != -EINPROGRESS)
  320. up(&dev->cmd_lock);
  321. }
  322. static void pn533_recv_response(struct urb *urb)
  323. {
  324. struct pn533 *dev = urb->context;
  325. struct pn533_frame *in_frame;
  326. dev->tklt_in_frame = NULL;
  327. switch (urb->status) {
  328. case 0:
  329. /* success */
  330. break;
  331. case -ECONNRESET:
  332. case -ENOENT:
  333. case -ESHUTDOWN:
  334. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  335. " status: %d", urb->status);
  336. dev->tklt_in_error = urb->status;
  337. goto sched_tasklet;
  338. default:
  339. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  340. " %d", urb->status);
  341. dev->tklt_in_error = urb->status;
  342. goto sched_tasklet;
  343. }
  344. in_frame = dev->in_urb->transfer_buffer;
  345. if (!pn533_rx_frame_is_valid(in_frame)) {
  346. nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
  347. dev->tklt_in_error = -EIO;
  348. goto sched_tasklet;
  349. }
  350. if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
  351. nfc_dev_err(&dev->interface->dev, "The received frame is not "
  352. "response to the last command");
  353. dev->tklt_in_error = -EIO;
  354. goto sched_tasklet;
  355. }
  356. nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
  357. dev->tklt_in_error = 0;
  358. dev->tklt_in_frame = in_frame;
  359. sched_tasklet:
  360. tasklet_schedule(&dev->tasklet);
  361. }
  362. static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
  363. {
  364. dev->in_urb->complete = pn533_recv_response;
  365. return usb_submit_urb(dev->in_urb, flags);
  366. }
  367. static void pn533_recv_ack(struct urb *urb)
  368. {
  369. struct pn533 *dev = urb->context;
  370. struct pn533_frame *in_frame;
  371. int rc;
  372. switch (urb->status) {
  373. case 0:
  374. /* success */
  375. break;
  376. case -ECONNRESET:
  377. case -ENOENT:
  378. case -ESHUTDOWN:
  379. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  380. " status: %d", urb->status);
  381. dev->tklt_in_error = urb->status;
  382. goto sched_tasklet;
  383. default:
  384. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  385. " %d", urb->status);
  386. dev->tklt_in_error = urb->status;
  387. goto sched_tasklet;
  388. }
  389. in_frame = dev->in_urb->transfer_buffer;
  390. if (!pn533_rx_frame_is_ack(in_frame)) {
  391. nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
  392. dev->tklt_in_error = -EIO;
  393. goto sched_tasklet;
  394. }
  395. nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
  396. rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
  397. if (rc) {
  398. nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
  399. " result %d", rc);
  400. dev->tklt_in_error = rc;
  401. goto sched_tasklet;
  402. }
  403. return;
  404. sched_tasklet:
  405. dev->tklt_in_frame = NULL;
  406. tasklet_schedule(&dev->tasklet);
  407. }
  408. static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
  409. {
  410. dev->in_urb->complete = pn533_recv_ack;
  411. return usb_submit_urb(dev->in_urb, flags);
  412. }
  413. static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
  414. {
  415. int rc;
  416. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  417. pn533_tx_frame_ack(dev->out_frame);
  418. dev->out_urb->transfer_buffer = dev->out_frame;
  419. dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
  420. rc = usb_submit_urb(dev->out_urb, flags);
  421. return rc;
  422. }
  423. static int __pn533_send_cmd_frame_async(struct pn533 *dev,
  424. struct pn533_frame *out_frame,
  425. struct pn533_frame *in_frame,
  426. int in_frame_len,
  427. pn533_cmd_complete_t cmd_complete,
  428. void *arg, gfp_t flags)
  429. {
  430. int rc;
  431. nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
  432. PN533_FRAME_CMD(out_frame));
  433. dev->cmd = PN533_FRAME_CMD(out_frame);
  434. dev->cmd_complete = cmd_complete;
  435. dev->cmd_complete_arg = arg;
  436. dev->out_urb->transfer_buffer = out_frame;
  437. dev->out_urb->transfer_buffer_length =
  438. PN533_FRAME_SIZE(out_frame);
  439. dev->in_urb->transfer_buffer = in_frame;
  440. dev->in_urb->transfer_buffer_length = in_frame_len;
  441. rc = usb_submit_urb(dev->out_urb, flags);
  442. if (rc)
  443. return rc;
  444. rc = pn533_submit_urb_for_ack(dev, flags);
  445. if (rc)
  446. goto error;
  447. return 0;
  448. error:
  449. usb_unlink_urb(dev->out_urb);
  450. return rc;
  451. }
  452. static int pn533_send_cmd_frame_async(struct pn533 *dev,
  453. struct pn533_frame *out_frame,
  454. struct pn533_frame *in_frame,
  455. int in_frame_len,
  456. pn533_cmd_complete_t cmd_complete,
  457. void *arg, gfp_t flags)
  458. {
  459. int rc;
  460. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  461. if (down_trylock(&dev->cmd_lock))
  462. return -EBUSY;
  463. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  464. in_frame_len, cmd_complete, arg, flags);
  465. if (rc)
  466. goto error;
  467. return 0;
  468. error:
  469. up(&dev->cmd_lock);
  470. return rc;
  471. }
  472. struct pn533_sync_cmd_response {
  473. int rc;
  474. struct completion done;
  475. };
  476. static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
  477. u8 *params, int params_len)
  478. {
  479. struct pn533_sync_cmd_response *arg = _arg;
  480. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  481. arg->rc = 0;
  482. if (params_len < 0) /* error */
  483. arg->rc = params_len;
  484. complete(&arg->done);
  485. return 0;
  486. }
  487. static int pn533_send_cmd_frame_sync(struct pn533 *dev,
  488. struct pn533_frame *out_frame,
  489. struct pn533_frame *in_frame,
  490. int in_frame_len)
  491. {
  492. int rc;
  493. struct pn533_sync_cmd_response arg;
  494. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  495. init_completion(&arg.done);
  496. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
  497. pn533_sync_cmd_complete, &arg, GFP_KERNEL);
  498. if (rc)
  499. return rc;
  500. wait_for_completion(&arg.done);
  501. return arg.rc;
  502. }
  503. static void pn533_send_complete(struct urb *urb)
  504. {
  505. struct pn533 *dev = urb->context;
  506. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  507. switch (urb->status) {
  508. case 0:
  509. /* success */
  510. break;
  511. case -ECONNRESET:
  512. case -ENOENT:
  513. case -ESHUTDOWN:
  514. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  515. " status: %d", urb->status);
  516. break;
  517. default:
  518. nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
  519. " %d", urb->status);
  520. }
  521. }
  522. struct pn533_target_type_a {
  523. __be16 sens_res;
  524. u8 sel_res;
  525. u8 nfcid_len;
  526. u8 nfcid_data[];
  527. } __packed;
  528. #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
  529. #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
  530. #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
  531. #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
  532. #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
  533. #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  534. #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
  535. #define PN533_TYPE_A_SEL_PROT_MIFARE 0
  536. #define PN533_TYPE_A_SEL_PROT_ISO14443 1
  537. #define PN533_TYPE_A_SEL_PROT_DEP 2
  538. #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
  539. static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
  540. int target_data_len)
  541. {
  542. u8 ssd;
  543. u8 platconf;
  544. if (target_data_len < sizeof(struct pn533_target_type_a))
  545. return false;
  546. /* The lenght check of nfcid[] and ats[] are not being performed because
  547. the values are not being used */
  548. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  549. ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
  550. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
  551. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  552. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  553. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  554. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  555. return false;
  556. /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
  557. if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
  558. return false;
  559. return true;
  560. }
  561. static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
  562. int tgt_data_len)
  563. {
  564. struct pn533_target_type_a *tgt_type_a;
  565. tgt_type_a = (struct pn533_target_type_a *) tgt_data;
  566. if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
  567. return -EPROTO;
  568. switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
  569. case PN533_TYPE_A_SEL_PROT_MIFARE:
  570. nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
  571. break;
  572. case PN533_TYPE_A_SEL_PROT_ISO14443:
  573. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  574. break;
  575. case PN533_TYPE_A_SEL_PROT_DEP:
  576. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  577. break;
  578. case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
  579. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
  580. NFC_PROTO_NFC_DEP_MASK;
  581. break;
  582. }
  583. nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
  584. nfc_tgt->sel_res = tgt_type_a->sel_res;
  585. return 0;
  586. }
  587. struct pn533_target_felica {
  588. u8 pol_res;
  589. u8 opcode;
  590. u8 nfcid2[8];
  591. u8 pad[8];
  592. /* optional */
  593. u8 syst_code[];
  594. } __packed;
  595. #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
  596. #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
  597. static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
  598. int target_data_len)
  599. {
  600. if (target_data_len < sizeof(struct pn533_target_felica))
  601. return false;
  602. if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
  603. return false;
  604. return true;
  605. }
  606. static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
  607. int tgt_data_len)
  608. {
  609. struct pn533_target_felica *tgt_felica;
  610. tgt_felica = (struct pn533_target_felica *) tgt_data;
  611. if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
  612. return -EPROTO;
  613. if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
  614. tgt_felica->nfcid2[1] ==
  615. PN533_FELICA_SENSF_NFCID2_DEP_B2)
  616. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  617. else
  618. nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
  619. return 0;
  620. }
  621. struct pn533_target_jewel {
  622. __be16 sens_res;
  623. u8 jewelid[4];
  624. } __packed;
  625. static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
  626. int target_data_len)
  627. {
  628. u8 ssd;
  629. u8 platconf;
  630. if (target_data_len < sizeof(struct pn533_target_jewel))
  631. return false;
  632. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  633. ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
  634. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
  635. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  636. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  637. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  638. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  639. return false;
  640. return true;
  641. }
  642. static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
  643. int tgt_data_len)
  644. {
  645. struct pn533_target_jewel *tgt_jewel;
  646. tgt_jewel = (struct pn533_target_jewel *) tgt_data;
  647. if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
  648. return -EPROTO;
  649. nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
  650. nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
  651. return 0;
  652. }
  653. struct pn533_type_b_prot_info {
  654. u8 bitrate;
  655. u8 fsci_type;
  656. u8 fwi_adc_fo;
  657. } __packed;
  658. #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
  659. #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
  660. #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
  661. struct pn533_type_b_sens_res {
  662. u8 opcode;
  663. u8 nfcid[4];
  664. u8 appdata[4];
  665. struct pn533_type_b_prot_info prot_info;
  666. } __packed;
  667. #define PN533_TYPE_B_OPC_SENSB_RES 0x50
  668. struct pn533_target_type_b {
  669. struct pn533_type_b_sens_res sensb_res;
  670. u8 attrib_res_len;
  671. u8 attrib_res[];
  672. } __packed;
  673. static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
  674. int target_data_len)
  675. {
  676. if (target_data_len < sizeof(struct pn533_target_type_b))
  677. return false;
  678. if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
  679. return false;
  680. if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
  681. PN533_TYPE_B_PROT_TYPE_RFU_MASK)
  682. return false;
  683. return true;
  684. }
  685. static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
  686. int tgt_data_len)
  687. {
  688. struct pn533_target_type_b *tgt_type_b;
  689. tgt_type_b = (struct pn533_target_type_b *) tgt_data;
  690. if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
  691. return -EPROTO;
  692. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  693. return 0;
  694. }
  695. struct pn533_poll_response {
  696. u8 nbtg;
  697. u8 tg;
  698. u8 target_data[];
  699. } __packed;
  700. static int pn533_target_found(struct pn533 *dev,
  701. struct pn533_poll_response *resp, int resp_len)
  702. {
  703. int target_data_len;
  704. struct nfc_target nfc_tgt;
  705. int rc;
  706. nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
  707. dev->poll_mod_curr);
  708. if (resp->tg != 1)
  709. return -EPROTO;
  710. target_data_len = resp_len - sizeof(struct pn533_poll_response);
  711. switch (dev->poll_mod_curr) {
  712. case PN533_POLL_MOD_106KBPS_A:
  713. rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
  714. target_data_len);
  715. break;
  716. case PN533_POLL_MOD_212KBPS_FELICA:
  717. case PN533_POLL_MOD_424KBPS_FELICA:
  718. rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
  719. target_data_len);
  720. break;
  721. case PN533_POLL_MOD_106KBPS_JEWEL:
  722. rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
  723. target_data_len);
  724. break;
  725. case PN533_POLL_MOD_847KBPS_B:
  726. rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
  727. target_data_len);
  728. break;
  729. default:
  730. nfc_dev_err(&dev->interface->dev, "Unknown current poll"
  731. " modulation");
  732. return -EPROTO;
  733. }
  734. if (rc)
  735. return rc;
  736. if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
  737. nfc_dev_dbg(&dev->interface->dev, "The target found does not"
  738. " have the desired protocol");
  739. return -EAGAIN;
  740. }
  741. nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
  742. "0x%x", nfc_tgt.supported_protocols);
  743. dev->tgt_available_prots = nfc_tgt.supported_protocols;
  744. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  745. return 0;
  746. }
  747. static void pn533_poll_reset_mod_list(struct pn533 *dev)
  748. {
  749. dev->poll_mod_count = 0;
  750. }
  751. static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
  752. {
  753. dev->poll_mod_active[dev->poll_mod_count] =
  754. (struct pn533_poll_modulations *) &poll_mod[mod_index];
  755. dev->poll_mod_count++;
  756. }
  757. static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
  758. {
  759. pn533_poll_reset_mod_list(dev);
  760. if (protocols & NFC_PROTO_MIFARE_MASK
  761. || protocols & NFC_PROTO_ISO14443_MASK
  762. || protocols & NFC_PROTO_NFC_DEP_MASK)
  763. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
  764. if (protocols & NFC_PROTO_FELICA_MASK
  765. || protocols & NFC_PROTO_NFC_DEP_MASK) {
  766. pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
  767. pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
  768. }
  769. if (protocols & NFC_PROTO_JEWEL_MASK)
  770. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
  771. if (protocols & NFC_PROTO_ISO14443_MASK)
  772. pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
  773. }
  774. static void pn533_start_poll_frame(struct pn533_frame *frame,
  775. struct pn533_poll_modulations *mod)
  776. {
  777. pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
  778. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
  779. frame->datalen += mod->len;
  780. pn533_tx_frame_finish(frame);
  781. }
  782. static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
  783. u8 *params, int params_len)
  784. {
  785. struct pn533_poll_response *resp;
  786. struct pn533_poll_modulations *next_mod;
  787. int rc;
  788. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  789. if (params_len == -ENOENT) {
  790. nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
  791. " stopped");
  792. goto stop_poll;
  793. }
  794. if (params_len < 0) {
  795. nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
  796. params_len);
  797. goto stop_poll;
  798. }
  799. resp = (struct pn533_poll_response *) params;
  800. if (resp->nbtg) {
  801. rc = pn533_target_found(dev, resp, params_len);
  802. /* We must stop the poll after a valid target found */
  803. if (rc == 0)
  804. goto stop_poll;
  805. if (rc != -EAGAIN)
  806. nfc_dev_err(&dev->interface->dev, "The target found is"
  807. " not valid - continuing to poll");
  808. }
  809. dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
  810. next_mod = dev->poll_mod_active[dev->poll_mod_curr];
  811. nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
  812. dev->poll_mod_curr);
  813. pn533_start_poll_frame(dev->out_frame, next_mod);
  814. /* Don't need to down the semaphore again */
  815. rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  816. dev->in_maxlen, pn533_start_poll_complete,
  817. NULL, GFP_ATOMIC);
  818. if (rc == -EPERM) {
  819. nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
  820. " because poll has been stopped");
  821. goto stop_poll;
  822. }
  823. if (rc) {
  824. nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
  825. " next modulation", rc);
  826. goto stop_poll;
  827. }
  828. /* Inform caller function to do not up the semaphore */
  829. return -EINPROGRESS;
  830. stop_poll:
  831. pn533_poll_reset_mod_list(dev);
  832. dev->poll_protocols = 0;
  833. return 0;
  834. }
  835. static int pn533_start_poll(struct nfc_dev *nfc_dev, u32 protocols)
  836. {
  837. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  838. struct pn533_poll_modulations *start_mod;
  839. int rc;
  840. nfc_dev_dbg(&dev->interface->dev, "%s - protocols=0x%x", __func__,
  841. protocols);
  842. if (dev->poll_mod_count) {
  843. nfc_dev_err(&dev->interface->dev, "Polling operation already"
  844. " active");
  845. return -EBUSY;
  846. }
  847. if (dev->tgt_active_prot) {
  848. nfc_dev_err(&dev->interface->dev, "Cannot poll with a target"
  849. " already activated");
  850. return -EBUSY;
  851. }
  852. pn533_poll_create_mod_list(dev, protocols);
  853. if (!dev->poll_mod_count) {
  854. nfc_dev_err(&dev->interface->dev, "No valid protocols"
  855. " specified");
  856. rc = -EINVAL;
  857. goto error;
  858. }
  859. nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
  860. dev->poll_mod_count);
  861. dev->poll_mod_curr = 0;
  862. start_mod = dev->poll_mod_active[dev->poll_mod_curr];
  863. pn533_start_poll_frame(dev->out_frame, start_mod);
  864. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  865. dev->in_maxlen, pn533_start_poll_complete,
  866. NULL, GFP_KERNEL);
  867. if (rc) {
  868. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  869. " start poll", rc);
  870. goto error;
  871. }
  872. dev->poll_protocols = protocols;
  873. return 0;
  874. error:
  875. pn533_poll_reset_mod_list(dev);
  876. return rc;
  877. }
  878. static void pn533_stop_poll(struct nfc_dev *nfc_dev)
  879. {
  880. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  881. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  882. if (!dev->poll_mod_count) {
  883. nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
  884. " running");
  885. return;
  886. }
  887. /* An ack will cancel the last issued command (poll) */
  888. pn533_send_ack(dev, GFP_KERNEL);
  889. /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
  890. usb_kill_urb(dev->in_urb);
  891. }
  892. static int pn533_activate_target_nfcdep(struct pn533 *dev)
  893. {
  894. struct pn533_cmd_activate_param param;
  895. struct pn533_cmd_activate_response *resp;
  896. int rc;
  897. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  898. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
  899. param.tg = 1;
  900. param.next = 0;
  901. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
  902. sizeof(struct pn533_cmd_activate_param));
  903. dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
  904. pn533_tx_frame_finish(dev->out_frame);
  905. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  906. dev->in_maxlen);
  907. if (rc)
  908. return rc;
  909. resp = (struct pn533_cmd_activate_response *)
  910. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  911. rc = resp->status & PN533_CMD_RET_MASK;
  912. if (rc != PN533_CMD_RET_SUCCESS)
  913. return -EIO;
  914. return 0;
  915. }
  916. static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
  917. u32 protocol)
  918. {
  919. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  920. int rc;
  921. nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
  922. protocol);
  923. if (dev->poll_mod_count) {
  924. nfc_dev_err(&dev->interface->dev, "Cannot activate while"
  925. " polling");
  926. return -EBUSY;
  927. }
  928. if (dev->tgt_active_prot) {
  929. nfc_dev_err(&dev->interface->dev, "There is already an active"
  930. " target");
  931. return -EBUSY;
  932. }
  933. if (!dev->tgt_available_prots) {
  934. nfc_dev_err(&dev->interface->dev, "There is no available target"
  935. " to activate");
  936. return -EINVAL;
  937. }
  938. if (!(dev->tgt_available_prots & (1 << protocol))) {
  939. nfc_dev_err(&dev->interface->dev, "The target does not support"
  940. " the requested protocol %u", protocol);
  941. return -EINVAL;
  942. }
  943. if (protocol == NFC_PROTO_NFC_DEP) {
  944. rc = pn533_activate_target_nfcdep(dev);
  945. if (rc) {
  946. nfc_dev_err(&dev->interface->dev, "Error %d when"
  947. " activating target with"
  948. " NFC_DEP protocol", rc);
  949. return rc;
  950. }
  951. }
  952. dev->tgt_active_prot = protocol;
  953. dev->tgt_available_prots = 0;
  954. return 0;
  955. }
  956. static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
  957. {
  958. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  959. u8 tg;
  960. u8 status;
  961. int rc;
  962. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  963. if (!dev->tgt_active_prot) {
  964. nfc_dev_err(&dev->interface->dev, "There is no active target");
  965. return;
  966. }
  967. dev->tgt_active_prot = 0;
  968. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
  969. tg = 1;
  970. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
  971. dev->out_frame->datalen += sizeof(u8);
  972. pn533_tx_frame_finish(dev->out_frame);
  973. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  974. dev->in_maxlen);
  975. if (rc) {
  976. nfc_dev_err(&dev->interface->dev, "Error when sending release"
  977. " command to the controller");
  978. return;
  979. }
  980. status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
  981. rc = status & PN533_CMD_RET_MASK;
  982. if (rc != PN533_CMD_RET_SUCCESS)
  983. nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
  984. " the target", rc);
  985. return;
  986. }
  987. #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
  988. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  989. static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
  990. {
  991. int payload_len = skb->len;
  992. struct pn533_frame *out_frame;
  993. struct sk_buff *discarded;
  994. u8 tg;
  995. nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
  996. payload_len);
  997. if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  998. /* TODO: Implement support to multi-part data exchange */
  999. nfc_dev_err(&dev->interface->dev, "Data length greater than the"
  1000. " max allowed: %d",
  1001. PN533_CMD_DATAEXCH_DATA_MAXLEN);
  1002. return -ENOSYS;
  1003. }
  1004. /* Reserving header space */
  1005. if (skb_cow_head(skb, PN533_CMD_DATAEXCH_HEAD_LEN)) {
  1006. nfc_dev_err(&dev->interface->dev, "Error to add header data");
  1007. return -ENOMEM;
  1008. }
  1009. /* Reserving tail space, see pn533_tx_frame_finish */
  1010. if (skb_cow_data(skb, PN533_FRAME_TAIL_SIZE, &discarded) < 0) {
  1011. nfc_dev_err(&dev->interface->dev, "Error to add tail data");
  1012. return -ENOMEM;
  1013. }
  1014. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
  1015. out_frame = (struct pn533_frame *) skb->data;
  1016. pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
  1017. tg = 1;
  1018. memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
  1019. out_frame->datalen += sizeof(u8);
  1020. /* The data is already in the out_frame, just update the datalen */
  1021. out_frame->datalen += payload_len;
  1022. pn533_tx_frame_finish(out_frame);
  1023. skb_put(skb, PN533_FRAME_TAIL_SIZE);
  1024. return 0;
  1025. }
  1026. struct pn533_data_exchange_arg {
  1027. struct sk_buff *skb_resp;
  1028. struct sk_buff *skb_out;
  1029. data_exchange_cb_t cb;
  1030. void *cb_context;
  1031. };
  1032. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1033. u8 *params, int params_len)
  1034. {
  1035. struct pn533_data_exchange_arg *arg = _arg;
  1036. struct sk_buff *skb_resp = arg->skb_resp;
  1037. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1038. int err = 0;
  1039. u8 status;
  1040. u8 cmd_ret;
  1041. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1042. dev_kfree_skb_irq(arg->skb_out);
  1043. if (params_len < 0) { /* error */
  1044. err = params_len;
  1045. goto error;
  1046. }
  1047. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1048. status = params[0];
  1049. cmd_ret = status & PN533_CMD_RET_MASK;
  1050. if (cmd_ret != PN533_CMD_RET_SUCCESS) {
  1051. nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
  1052. " exchanging data", cmd_ret);
  1053. err = -EIO;
  1054. goto error;
  1055. }
  1056. if (status & PN533_CMD_MI_MASK) {
  1057. /* TODO: Implement support to multi-part data exchange */
  1058. nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
  1059. " supported");
  1060. /* Prevent the other messages from controller */
  1061. pn533_send_ack(dev, GFP_ATOMIC);
  1062. err = -ENOSYS;
  1063. goto error;
  1064. }
  1065. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  1066. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
  1067. arg->cb(arg->cb_context, skb_resp, 0);
  1068. kfree(arg);
  1069. return 0;
  1070. error:
  1071. dev_kfree_skb_irq(skb_resp);
  1072. arg->cb(arg->cb_context, NULL, err);
  1073. kfree(arg);
  1074. return 0;
  1075. }
  1076. int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
  1077. struct sk_buff *skb,
  1078. data_exchange_cb_t cb,
  1079. void *cb_context)
  1080. {
  1081. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1082. struct pn533_frame *out_frame, *in_frame;
  1083. struct pn533_data_exchange_arg *arg;
  1084. struct sk_buff *skb_resp;
  1085. int skb_resp_len;
  1086. int rc;
  1087. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1088. if (!dev->tgt_active_prot) {
  1089. nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
  1090. " there is no active target");
  1091. rc = -EINVAL;
  1092. goto error;
  1093. }
  1094. rc = pn533_data_exchange_tx_frame(dev, skb);
  1095. if (rc)
  1096. goto error;
  1097. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1098. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1099. PN533_FRAME_TAIL_SIZE;
  1100. skb_resp = nfc_alloc_skb(skb_resp_len, GFP_KERNEL);
  1101. if (!skb_resp) {
  1102. rc = -ENOMEM;
  1103. goto error;
  1104. }
  1105. in_frame = (struct pn533_frame *) skb_resp->data;
  1106. out_frame = (struct pn533_frame *) skb->data;
  1107. arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
  1108. if (!arg) {
  1109. rc = -ENOMEM;
  1110. goto free_skb_resp;
  1111. }
  1112. arg->skb_resp = skb_resp;
  1113. arg->skb_out = skb;
  1114. arg->cb = cb;
  1115. arg->cb_context = cb_context;
  1116. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
  1117. pn533_data_exchange_complete, arg,
  1118. GFP_KERNEL);
  1119. if (rc) {
  1120. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1121. " perform data_exchange", rc);
  1122. goto free_arg;
  1123. }
  1124. return 0;
  1125. free_arg:
  1126. kfree(arg);
  1127. free_skb_resp:
  1128. kfree_skb(skb_resp);
  1129. error:
  1130. kfree_skb(skb);
  1131. return rc;
  1132. }
  1133. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1134. u8 cfgdata_len)
  1135. {
  1136. int rc;
  1137. u8 *params;
  1138. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1139. pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
  1140. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1141. params[0] = cfgitem;
  1142. memcpy(&params[1], cfgdata, cfgdata_len);
  1143. dev->out_frame->datalen += (1 + cfgdata_len);
  1144. pn533_tx_frame_finish(dev->out_frame);
  1145. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1146. dev->in_maxlen);
  1147. return rc;
  1148. }
  1149. struct nfc_ops pn533_nfc_ops = {
  1150. .start_poll = pn533_start_poll,
  1151. .stop_poll = pn533_stop_poll,
  1152. .activate_target = pn533_activate_target,
  1153. .deactivate_target = pn533_deactivate_target,
  1154. .data_exchange = pn533_data_exchange,
  1155. };
  1156. static int pn533_probe(struct usb_interface *interface,
  1157. const struct usb_device_id *id)
  1158. {
  1159. struct pn533_fw_version *fw_ver;
  1160. struct pn533 *dev;
  1161. struct usb_host_interface *iface_desc;
  1162. struct usb_endpoint_descriptor *endpoint;
  1163. struct pn533_config_max_retries max_retries;
  1164. int in_endpoint = 0;
  1165. int out_endpoint = 0;
  1166. int rc = -ENOMEM;
  1167. int i;
  1168. u32 protocols;
  1169. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1170. if (!dev)
  1171. return -ENOMEM;
  1172. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  1173. dev->interface = interface;
  1174. sema_init(&dev->cmd_lock, 1);
  1175. iface_desc = interface->cur_altsetting;
  1176. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1177. endpoint = &iface_desc->endpoint[i].desc;
  1178. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1179. dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
  1180. in_endpoint = endpoint->bEndpointAddress;
  1181. }
  1182. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
  1183. dev->out_maxlen =
  1184. le16_to_cpu(endpoint->wMaxPacketSize);
  1185. out_endpoint = endpoint->bEndpointAddress;
  1186. }
  1187. }
  1188. if (!in_endpoint || !out_endpoint) {
  1189. nfc_dev_err(&interface->dev, "Could not find bulk-in or"
  1190. " bulk-out endpoint");
  1191. rc = -ENODEV;
  1192. goto error;
  1193. }
  1194. dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
  1195. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1196. dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
  1197. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1198. if (!dev->in_frame || !dev->out_frame ||
  1199. !dev->in_urb || !dev->out_urb)
  1200. goto error;
  1201. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  1202. usb_rcvbulkpipe(dev->udev, in_endpoint),
  1203. NULL, 0, NULL, dev);
  1204. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  1205. usb_sndbulkpipe(dev->udev, out_endpoint),
  1206. NULL, 0,
  1207. pn533_send_complete, dev);
  1208. tasklet_init(&dev->tasklet, pn533_tasklet_cmd_complete, (ulong)dev);
  1209. usb_set_intfdata(interface, dev);
  1210. pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
  1211. pn533_tx_frame_finish(dev->out_frame);
  1212. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1213. dev->in_maxlen);
  1214. if (rc)
  1215. goto kill_tasklet;
  1216. fw_ver = (struct pn533_fw_version *)
  1217. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1218. nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
  1219. " attached", fw_ver->ver, fw_ver->rev);
  1220. protocols = NFC_PROTO_JEWEL_MASK
  1221. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  1222. | NFC_PROTO_ISO14443_MASK
  1223. | NFC_PROTO_NFC_DEP_MASK;
  1224. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols);
  1225. if (!dev->nfc_dev)
  1226. goto kill_tasklet;
  1227. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  1228. nfc_set_drvdata(dev->nfc_dev, dev);
  1229. rc = nfc_register_device(dev->nfc_dev);
  1230. if (rc)
  1231. goto free_nfc_dev;
  1232. max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
  1233. max_retries.mx_rty_psl = 2;
  1234. max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1235. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1236. (u8 *) &max_retries, sizeof(max_retries));
  1237. if (rc) {
  1238. nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
  1239. " config");
  1240. goto free_nfc_dev;
  1241. }
  1242. return 0;
  1243. free_nfc_dev:
  1244. nfc_free_device(dev->nfc_dev);
  1245. kill_tasklet:
  1246. tasklet_kill(&dev->tasklet);
  1247. error:
  1248. kfree(dev->in_frame);
  1249. usb_free_urb(dev->in_urb);
  1250. kfree(dev->out_frame);
  1251. usb_free_urb(dev->out_urb);
  1252. kfree(dev);
  1253. return rc;
  1254. }
  1255. static void pn533_disconnect(struct usb_interface *interface)
  1256. {
  1257. struct pn533 *dev;
  1258. dev = usb_get_intfdata(interface);
  1259. usb_set_intfdata(interface, NULL);
  1260. nfc_unregister_device(dev->nfc_dev);
  1261. nfc_free_device(dev->nfc_dev);
  1262. usb_kill_urb(dev->in_urb);
  1263. usb_kill_urb(dev->out_urb);
  1264. tasklet_kill(&dev->tasklet);
  1265. kfree(dev->in_frame);
  1266. usb_free_urb(dev->in_urb);
  1267. kfree(dev->out_frame);
  1268. usb_free_urb(dev->out_urb);
  1269. kfree(dev);
  1270. nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
  1271. }
  1272. static struct usb_driver pn533_driver = {
  1273. .name = "pn533",
  1274. .probe = pn533_probe,
  1275. .disconnect = pn533_disconnect,
  1276. .id_table = pn533_table,
  1277. };
  1278. static int __init pn533_init(void)
  1279. {
  1280. int rc;
  1281. rc = usb_register(&pn533_driver);
  1282. if (rc)
  1283. err("usb_register failed. Error number %d", rc);
  1284. return rc;
  1285. }
  1286. static void __exit pn533_exit(void)
  1287. {
  1288. usb_deregister(&pn533_driver);
  1289. }
  1290. module_init(pn533_init);
  1291. module_exit(pn533_exit);
  1292. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
  1293. " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  1294. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  1295. MODULE_VERSION(VERSION);
  1296. MODULE_LICENSE("GPL");