pn533.c 38 KB

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