pn533.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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 *cmd;
  1022. struct pn533_cmd_jump_dep_response *resp;
  1023. struct nfc_target nfc_target;
  1024. u8 target_gt_len;
  1025. int rc;
  1026. if (params_len == -ENOENT) {
  1027. nfc_dev_dbg(&dev->interface->dev, "");
  1028. return 0;
  1029. }
  1030. if (params_len < 0) {
  1031. nfc_dev_err(&dev->interface->dev,
  1032. "Error %d when bringing DEP link up",
  1033. params_len);
  1034. return 0;
  1035. }
  1036. if (dev->tgt_available_prots &&
  1037. !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
  1038. nfc_dev_err(&dev->interface->dev,
  1039. "The target does not support DEP");
  1040. return -EINVAL;
  1041. }
  1042. resp = (struct pn533_cmd_jump_dep_response *) params;
  1043. cmd = (struct pn533_cmd_jump_dep *) arg;
  1044. rc = resp->status & PN533_CMD_RET_MASK;
  1045. if (rc != PN533_CMD_RET_SUCCESS) {
  1046. nfc_dev_err(&dev->interface->dev,
  1047. "Bringing DEP link up failed %d", rc);
  1048. return 0;
  1049. }
  1050. if (!dev->tgt_available_prots) {
  1051. nfc_dev_dbg(&dev->interface->dev, "Creating new target");
  1052. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1053. nfc_target.nfcid1_len = 10;
  1054. memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
  1055. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1056. if (rc)
  1057. return 0;
  1058. dev->tgt_available_prots = 0;
  1059. }
  1060. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1061. /* ATR_RES general bytes are located at offset 17 */
  1062. target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
  1063. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1064. resp->gt, target_gt_len);
  1065. if (rc == 0)
  1066. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1067. dev->nfc_dev->targets[0].idx,
  1068. !cmd->active, NFC_RF_INITIATOR);
  1069. return 0;
  1070. }
  1071. static int pn533_dep_link_up(struct nfc_dev *nfc_dev, int target_idx,
  1072. u8 comm_mode, u8* gb, size_t gb_len)
  1073. {
  1074. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1075. struct pn533_cmd_jump_dep *cmd;
  1076. u8 cmd_len;
  1077. int rc;
  1078. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1079. if (dev->poll_mod_count) {
  1080. nfc_dev_err(&dev->interface->dev,
  1081. "Cannot bring the DEP link up while polling");
  1082. return -EBUSY;
  1083. }
  1084. if (dev->tgt_active_prot) {
  1085. nfc_dev_err(&dev->interface->dev,
  1086. "There is already an active target");
  1087. return -EBUSY;
  1088. }
  1089. cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
  1090. cmd = kzalloc(cmd_len, GFP_KERNEL);
  1091. if (cmd == NULL)
  1092. return -ENOMEM;
  1093. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
  1094. cmd->active = !comm_mode;
  1095. cmd->baud = 0;
  1096. if (gb != NULL && gb_len > 0) {
  1097. cmd->next = 4; /* We have some Gi */
  1098. memcpy(cmd->gt, gb, gb_len);
  1099. } else {
  1100. cmd->next = 0;
  1101. }
  1102. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
  1103. dev->out_frame->datalen += cmd_len;
  1104. pn533_tx_frame_finish(dev->out_frame);
  1105. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1106. dev->in_maxlen, pn533_in_dep_link_up_complete,
  1107. cmd, GFP_KERNEL);
  1108. if (rc)
  1109. goto out;
  1110. out:
  1111. kfree(cmd);
  1112. return rc;
  1113. }
  1114. static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
  1115. {
  1116. pn533_deactivate_target(nfc_dev, 0);
  1117. return 0;
  1118. }
  1119. #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
  1120. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  1121. static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
  1122. {
  1123. int payload_len = skb->len;
  1124. struct pn533_frame *out_frame;
  1125. u8 tg;
  1126. nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
  1127. payload_len);
  1128. if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  1129. /* TODO: Implement support to multi-part data exchange */
  1130. nfc_dev_err(&dev->interface->dev, "Data length greater than the"
  1131. " max allowed: %d",
  1132. PN533_CMD_DATAEXCH_DATA_MAXLEN);
  1133. return -ENOSYS;
  1134. }
  1135. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
  1136. out_frame = (struct pn533_frame *) skb->data;
  1137. pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
  1138. tg = 1;
  1139. memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
  1140. out_frame->datalen += sizeof(u8);
  1141. /* The data is already in the out_frame, just update the datalen */
  1142. out_frame->datalen += payload_len;
  1143. pn533_tx_frame_finish(out_frame);
  1144. skb_put(skb, PN533_FRAME_TAIL_SIZE);
  1145. return 0;
  1146. }
  1147. struct pn533_data_exchange_arg {
  1148. struct sk_buff *skb_resp;
  1149. struct sk_buff *skb_out;
  1150. data_exchange_cb_t cb;
  1151. void *cb_context;
  1152. };
  1153. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1154. u8 *params, int params_len)
  1155. {
  1156. struct pn533_data_exchange_arg *arg = _arg;
  1157. struct sk_buff *skb_resp = arg->skb_resp;
  1158. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1159. int err = 0;
  1160. u8 status;
  1161. u8 cmd_ret;
  1162. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1163. dev_kfree_skb_irq(arg->skb_out);
  1164. if (params_len < 0) { /* error */
  1165. err = params_len;
  1166. goto error;
  1167. }
  1168. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1169. status = params[0];
  1170. cmd_ret = status & PN533_CMD_RET_MASK;
  1171. if (cmd_ret != PN533_CMD_RET_SUCCESS) {
  1172. nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
  1173. " exchanging data", cmd_ret);
  1174. err = -EIO;
  1175. goto error;
  1176. }
  1177. if (status & PN533_CMD_MI_MASK) {
  1178. /* TODO: Implement support to multi-part data exchange */
  1179. nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
  1180. " supported");
  1181. /* Prevent the other messages from controller */
  1182. pn533_send_ack(dev, GFP_ATOMIC);
  1183. err = -ENOSYS;
  1184. goto error;
  1185. }
  1186. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  1187. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
  1188. arg->cb(arg->cb_context, skb_resp, 0);
  1189. kfree(arg);
  1190. return 0;
  1191. error:
  1192. dev_kfree_skb_irq(skb_resp);
  1193. arg->cb(arg->cb_context, NULL, err);
  1194. kfree(arg);
  1195. return 0;
  1196. }
  1197. static int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
  1198. struct sk_buff *skb,
  1199. data_exchange_cb_t cb,
  1200. void *cb_context)
  1201. {
  1202. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1203. struct pn533_frame *out_frame, *in_frame;
  1204. struct pn533_data_exchange_arg *arg;
  1205. struct sk_buff *skb_resp;
  1206. int skb_resp_len;
  1207. int rc;
  1208. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1209. if (!dev->tgt_active_prot) {
  1210. nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
  1211. " there is no active target");
  1212. rc = -EINVAL;
  1213. goto error;
  1214. }
  1215. rc = pn533_data_exchange_tx_frame(dev, skb);
  1216. if (rc)
  1217. goto error;
  1218. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1219. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1220. PN533_FRAME_TAIL_SIZE;
  1221. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  1222. if (!skb_resp) {
  1223. rc = -ENOMEM;
  1224. goto error;
  1225. }
  1226. in_frame = (struct pn533_frame *) skb_resp->data;
  1227. out_frame = (struct pn533_frame *) skb->data;
  1228. arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
  1229. if (!arg) {
  1230. rc = -ENOMEM;
  1231. goto free_skb_resp;
  1232. }
  1233. arg->skb_resp = skb_resp;
  1234. arg->skb_out = skb;
  1235. arg->cb = cb;
  1236. arg->cb_context = cb_context;
  1237. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
  1238. pn533_data_exchange_complete, arg,
  1239. GFP_KERNEL);
  1240. if (rc) {
  1241. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1242. " perform data_exchange", rc);
  1243. goto free_arg;
  1244. }
  1245. return 0;
  1246. free_arg:
  1247. kfree(arg);
  1248. free_skb_resp:
  1249. kfree_skb(skb_resp);
  1250. error:
  1251. kfree_skb(skb);
  1252. return rc;
  1253. }
  1254. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1255. u8 cfgdata_len)
  1256. {
  1257. int rc;
  1258. u8 *params;
  1259. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1260. pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
  1261. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1262. params[0] = cfgitem;
  1263. memcpy(&params[1], cfgdata, cfgdata_len);
  1264. dev->out_frame->datalen += (1 + cfgdata_len);
  1265. pn533_tx_frame_finish(dev->out_frame);
  1266. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1267. dev->in_maxlen);
  1268. return rc;
  1269. }
  1270. struct nfc_ops pn533_nfc_ops = {
  1271. .dev_up = NULL,
  1272. .dev_down = NULL,
  1273. .dep_link_up = pn533_dep_link_up,
  1274. .dep_link_down = pn533_dep_link_down,
  1275. .start_poll = pn533_start_poll,
  1276. .stop_poll = pn533_stop_poll,
  1277. .activate_target = pn533_activate_target,
  1278. .deactivate_target = pn533_deactivate_target,
  1279. .data_exchange = pn533_data_exchange,
  1280. };
  1281. static int pn533_probe(struct usb_interface *interface,
  1282. const struct usb_device_id *id)
  1283. {
  1284. struct pn533_fw_version *fw_ver;
  1285. struct pn533 *dev;
  1286. struct usb_host_interface *iface_desc;
  1287. struct usb_endpoint_descriptor *endpoint;
  1288. struct pn533_config_max_retries max_retries;
  1289. int in_endpoint = 0;
  1290. int out_endpoint = 0;
  1291. int rc = -ENOMEM;
  1292. int i;
  1293. u32 protocols;
  1294. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1295. if (!dev)
  1296. return -ENOMEM;
  1297. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  1298. dev->interface = interface;
  1299. sema_init(&dev->cmd_lock, 1);
  1300. iface_desc = interface->cur_altsetting;
  1301. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1302. endpoint = &iface_desc->endpoint[i].desc;
  1303. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1304. dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
  1305. in_endpoint = endpoint->bEndpointAddress;
  1306. }
  1307. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
  1308. dev->out_maxlen =
  1309. le16_to_cpu(endpoint->wMaxPacketSize);
  1310. out_endpoint = endpoint->bEndpointAddress;
  1311. }
  1312. }
  1313. if (!in_endpoint || !out_endpoint) {
  1314. nfc_dev_err(&interface->dev, "Could not find bulk-in or"
  1315. " bulk-out endpoint");
  1316. rc = -ENODEV;
  1317. goto error;
  1318. }
  1319. dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
  1320. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1321. dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
  1322. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1323. if (!dev->in_frame || !dev->out_frame ||
  1324. !dev->in_urb || !dev->out_urb)
  1325. goto error;
  1326. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  1327. usb_rcvbulkpipe(dev->udev, in_endpoint),
  1328. NULL, 0, NULL, dev);
  1329. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  1330. usb_sndbulkpipe(dev->udev, out_endpoint),
  1331. NULL, 0,
  1332. pn533_send_complete, dev);
  1333. tasklet_init(&dev->tasklet, pn533_tasklet_cmd_complete, (ulong)dev);
  1334. usb_set_intfdata(interface, dev);
  1335. pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
  1336. pn533_tx_frame_finish(dev->out_frame);
  1337. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1338. dev->in_maxlen);
  1339. if (rc)
  1340. goto kill_tasklet;
  1341. fw_ver = (struct pn533_fw_version *)
  1342. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1343. nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
  1344. " attached", fw_ver->ver, fw_ver->rev);
  1345. protocols = NFC_PROTO_JEWEL_MASK
  1346. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  1347. | NFC_PROTO_ISO14443_MASK
  1348. | NFC_PROTO_NFC_DEP_MASK;
  1349. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  1350. PN533_CMD_DATAEXCH_HEAD_LEN,
  1351. PN533_FRAME_TAIL_SIZE);
  1352. if (!dev->nfc_dev)
  1353. goto kill_tasklet;
  1354. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  1355. nfc_set_drvdata(dev->nfc_dev, dev);
  1356. rc = nfc_register_device(dev->nfc_dev);
  1357. if (rc)
  1358. goto free_nfc_dev;
  1359. max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
  1360. max_retries.mx_rty_psl = 2;
  1361. max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1362. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1363. (u8 *) &max_retries, sizeof(max_retries));
  1364. if (rc) {
  1365. nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
  1366. " config");
  1367. goto free_nfc_dev;
  1368. }
  1369. return 0;
  1370. free_nfc_dev:
  1371. nfc_free_device(dev->nfc_dev);
  1372. kill_tasklet:
  1373. tasklet_kill(&dev->tasklet);
  1374. error:
  1375. kfree(dev->in_frame);
  1376. usb_free_urb(dev->in_urb);
  1377. kfree(dev->out_frame);
  1378. usb_free_urb(dev->out_urb);
  1379. kfree(dev);
  1380. return rc;
  1381. }
  1382. static void pn533_disconnect(struct usb_interface *interface)
  1383. {
  1384. struct pn533 *dev;
  1385. dev = usb_get_intfdata(interface);
  1386. usb_set_intfdata(interface, NULL);
  1387. nfc_unregister_device(dev->nfc_dev);
  1388. nfc_free_device(dev->nfc_dev);
  1389. usb_kill_urb(dev->in_urb);
  1390. usb_kill_urb(dev->out_urb);
  1391. tasklet_kill(&dev->tasklet);
  1392. kfree(dev->in_frame);
  1393. usb_free_urb(dev->in_urb);
  1394. kfree(dev->out_frame);
  1395. usb_free_urb(dev->out_urb);
  1396. kfree(dev);
  1397. nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
  1398. }
  1399. static struct usb_driver pn533_driver = {
  1400. .name = "pn533",
  1401. .probe = pn533_probe,
  1402. .disconnect = pn533_disconnect,
  1403. .id_table = pn533_table,
  1404. };
  1405. module_usb_driver(pn533_driver);
  1406. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
  1407. " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  1408. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  1409. MODULE_VERSION(VERSION);
  1410. MODULE_LICENSE("GPL");