pn533.c 42 KB

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