pn533.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  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_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. u16 gt_len;
  897. int rc;
  898. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  899. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
  900. param.tg = 1;
  901. param.next = 0;
  902. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
  903. sizeof(struct pn533_cmd_activate_param));
  904. dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
  905. pn533_tx_frame_finish(dev->out_frame);
  906. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  907. dev->in_maxlen);
  908. if (rc)
  909. return rc;
  910. resp = (struct pn533_cmd_activate_response *)
  911. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  912. rc = resp->status & PN533_CMD_RET_MASK;
  913. if (rc != PN533_CMD_RET_SUCCESS)
  914. return -EIO;
  915. /* ATR_RES general bytes are located at offset 16 */
  916. gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
  917. rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
  918. return rc;
  919. }
  920. static int pn533_activate_target(struct nfc_dev *nfc_dev, u32 target_idx,
  921. u32 protocol)
  922. {
  923. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  924. int rc;
  925. nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
  926. protocol);
  927. if (dev->poll_mod_count) {
  928. nfc_dev_err(&dev->interface->dev, "Cannot activate while"
  929. " polling");
  930. return -EBUSY;
  931. }
  932. if (dev->tgt_active_prot) {
  933. nfc_dev_err(&dev->interface->dev, "There is already an active"
  934. " target");
  935. return -EBUSY;
  936. }
  937. if (!dev->tgt_available_prots) {
  938. nfc_dev_err(&dev->interface->dev, "There is no available target"
  939. " to activate");
  940. return -EINVAL;
  941. }
  942. if (!(dev->tgt_available_prots & (1 << protocol))) {
  943. nfc_dev_err(&dev->interface->dev, "The target does not support"
  944. " the requested protocol %u", protocol);
  945. return -EINVAL;
  946. }
  947. if (protocol == NFC_PROTO_NFC_DEP) {
  948. rc = pn533_activate_target_nfcdep(dev);
  949. if (rc) {
  950. nfc_dev_err(&dev->interface->dev, "Error %d when"
  951. " activating target with"
  952. " NFC_DEP protocol", rc);
  953. return rc;
  954. }
  955. }
  956. dev->tgt_active_prot = protocol;
  957. dev->tgt_available_prots = 0;
  958. return 0;
  959. }
  960. static void pn533_deactivate_target(struct nfc_dev *nfc_dev, u32 target_idx)
  961. {
  962. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  963. u8 tg;
  964. u8 status;
  965. int rc;
  966. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  967. if (!dev->tgt_active_prot) {
  968. nfc_dev_err(&dev->interface->dev, "There is no active target");
  969. return;
  970. }
  971. dev->tgt_active_prot = 0;
  972. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
  973. tg = 1;
  974. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
  975. dev->out_frame->datalen += sizeof(u8);
  976. pn533_tx_frame_finish(dev->out_frame);
  977. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  978. dev->in_maxlen);
  979. if (rc) {
  980. nfc_dev_err(&dev->interface->dev, "Error when sending release"
  981. " command to the controller");
  982. return;
  983. }
  984. status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
  985. rc = status & PN533_CMD_RET_MASK;
  986. if (rc != PN533_CMD_RET_SUCCESS)
  987. nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
  988. " the target", rc);
  989. return;
  990. }
  991. #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
  992. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  993. static int pn533_data_exchange_tx_frame(struct pn533 *dev, struct sk_buff *skb)
  994. {
  995. int payload_len = skb->len;
  996. struct pn533_frame *out_frame;
  997. u8 tg;
  998. nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
  999. payload_len);
  1000. if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  1001. /* TODO: Implement support to multi-part data exchange */
  1002. nfc_dev_err(&dev->interface->dev, "Data length greater than the"
  1003. " max allowed: %d",
  1004. PN533_CMD_DATAEXCH_DATA_MAXLEN);
  1005. return -ENOSYS;
  1006. }
  1007. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
  1008. out_frame = (struct pn533_frame *) skb->data;
  1009. pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
  1010. tg = 1;
  1011. memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
  1012. out_frame->datalen += sizeof(u8);
  1013. /* The data is already in the out_frame, just update the datalen */
  1014. out_frame->datalen += payload_len;
  1015. pn533_tx_frame_finish(out_frame);
  1016. skb_put(skb, PN533_FRAME_TAIL_SIZE);
  1017. return 0;
  1018. }
  1019. struct pn533_data_exchange_arg {
  1020. struct sk_buff *skb_resp;
  1021. struct sk_buff *skb_out;
  1022. data_exchange_cb_t cb;
  1023. void *cb_context;
  1024. };
  1025. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1026. u8 *params, int params_len)
  1027. {
  1028. struct pn533_data_exchange_arg *arg = _arg;
  1029. struct sk_buff *skb_resp = arg->skb_resp;
  1030. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1031. int err = 0;
  1032. u8 status;
  1033. u8 cmd_ret;
  1034. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1035. dev_kfree_skb_irq(arg->skb_out);
  1036. if (params_len < 0) { /* error */
  1037. err = params_len;
  1038. goto error;
  1039. }
  1040. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1041. status = params[0];
  1042. cmd_ret = status & PN533_CMD_RET_MASK;
  1043. if (cmd_ret != PN533_CMD_RET_SUCCESS) {
  1044. nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
  1045. " exchanging data", cmd_ret);
  1046. err = -EIO;
  1047. goto error;
  1048. }
  1049. if (status & PN533_CMD_MI_MASK) {
  1050. /* TODO: Implement support to multi-part data exchange */
  1051. nfc_dev_err(&dev->interface->dev, "Multi-part message not yet"
  1052. " supported");
  1053. /* Prevent the other messages from controller */
  1054. pn533_send_ack(dev, GFP_ATOMIC);
  1055. err = -ENOSYS;
  1056. goto error;
  1057. }
  1058. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  1059. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
  1060. arg->cb(arg->cb_context, skb_resp, 0);
  1061. kfree(arg);
  1062. return 0;
  1063. error:
  1064. dev_kfree_skb_irq(skb_resp);
  1065. arg->cb(arg->cb_context, NULL, err);
  1066. kfree(arg);
  1067. return 0;
  1068. }
  1069. static int pn533_data_exchange(struct nfc_dev *nfc_dev, u32 target_idx,
  1070. struct sk_buff *skb,
  1071. data_exchange_cb_t cb,
  1072. void *cb_context)
  1073. {
  1074. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1075. struct pn533_frame *out_frame, *in_frame;
  1076. struct pn533_data_exchange_arg *arg;
  1077. struct sk_buff *skb_resp;
  1078. int skb_resp_len;
  1079. int rc;
  1080. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1081. if (!dev->tgt_active_prot) {
  1082. nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
  1083. " there is no active target");
  1084. rc = -EINVAL;
  1085. goto error;
  1086. }
  1087. rc = pn533_data_exchange_tx_frame(dev, skb);
  1088. if (rc)
  1089. goto error;
  1090. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1091. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1092. PN533_FRAME_TAIL_SIZE;
  1093. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  1094. if (!skb_resp) {
  1095. rc = -ENOMEM;
  1096. goto error;
  1097. }
  1098. in_frame = (struct pn533_frame *) skb_resp->data;
  1099. out_frame = (struct pn533_frame *) skb->data;
  1100. arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
  1101. if (!arg) {
  1102. rc = -ENOMEM;
  1103. goto free_skb_resp;
  1104. }
  1105. arg->skb_resp = skb_resp;
  1106. arg->skb_out = skb;
  1107. arg->cb = cb;
  1108. arg->cb_context = cb_context;
  1109. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
  1110. pn533_data_exchange_complete, arg,
  1111. GFP_KERNEL);
  1112. if (rc) {
  1113. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1114. " perform data_exchange", rc);
  1115. goto free_arg;
  1116. }
  1117. return 0;
  1118. free_arg:
  1119. kfree(arg);
  1120. free_skb_resp:
  1121. kfree_skb(skb_resp);
  1122. error:
  1123. kfree_skb(skb);
  1124. return rc;
  1125. }
  1126. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1127. u8 cfgdata_len)
  1128. {
  1129. int rc;
  1130. u8 *params;
  1131. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1132. pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
  1133. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1134. params[0] = cfgitem;
  1135. memcpy(&params[1], cfgdata, cfgdata_len);
  1136. dev->out_frame->datalen += (1 + cfgdata_len);
  1137. pn533_tx_frame_finish(dev->out_frame);
  1138. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1139. dev->in_maxlen);
  1140. return rc;
  1141. }
  1142. struct nfc_ops pn533_nfc_ops = {
  1143. .dev_up = NULL,
  1144. .dev_down = NULL,
  1145. .start_poll = pn533_start_poll,
  1146. .stop_poll = pn533_stop_poll,
  1147. .activate_target = pn533_activate_target,
  1148. .deactivate_target = pn533_deactivate_target,
  1149. .data_exchange = pn533_data_exchange,
  1150. };
  1151. static int pn533_probe(struct usb_interface *interface,
  1152. const struct usb_device_id *id)
  1153. {
  1154. struct pn533_fw_version *fw_ver;
  1155. struct pn533 *dev;
  1156. struct usb_host_interface *iface_desc;
  1157. struct usb_endpoint_descriptor *endpoint;
  1158. struct pn533_config_max_retries max_retries;
  1159. int in_endpoint = 0;
  1160. int out_endpoint = 0;
  1161. int rc = -ENOMEM;
  1162. int i;
  1163. u32 protocols;
  1164. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1165. if (!dev)
  1166. return -ENOMEM;
  1167. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  1168. dev->interface = interface;
  1169. sema_init(&dev->cmd_lock, 1);
  1170. iface_desc = interface->cur_altsetting;
  1171. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1172. endpoint = &iface_desc->endpoint[i].desc;
  1173. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1174. dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
  1175. in_endpoint = endpoint->bEndpointAddress;
  1176. }
  1177. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
  1178. dev->out_maxlen =
  1179. le16_to_cpu(endpoint->wMaxPacketSize);
  1180. out_endpoint = endpoint->bEndpointAddress;
  1181. }
  1182. }
  1183. if (!in_endpoint || !out_endpoint) {
  1184. nfc_dev_err(&interface->dev, "Could not find bulk-in or"
  1185. " bulk-out endpoint");
  1186. rc = -ENODEV;
  1187. goto error;
  1188. }
  1189. dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
  1190. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1191. dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
  1192. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1193. if (!dev->in_frame || !dev->out_frame ||
  1194. !dev->in_urb || !dev->out_urb)
  1195. goto error;
  1196. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  1197. usb_rcvbulkpipe(dev->udev, in_endpoint),
  1198. NULL, 0, NULL, dev);
  1199. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  1200. usb_sndbulkpipe(dev->udev, out_endpoint),
  1201. NULL, 0,
  1202. pn533_send_complete, dev);
  1203. tasklet_init(&dev->tasklet, pn533_tasklet_cmd_complete, (ulong)dev);
  1204. usb_set_intfdata(interface, dev);
  1205. pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
  1206. pn533_tx_frame_finish(dev->out_frame);
  1207. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1208. dev->in_maxlen);
  1209. if (rc)
  1210. goto kill_tasklet;
  1211. fw_ver = (struct pn533_fw_version *)
  1212. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1213. nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
  1214. " attached", fw_ver->ver, fw_ver->rev);
  1215. protocols = NFC_PROTO_JEWEL_MASK
  1216. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  1217. | NFC_PROTO_ISO14443_MASK
  1218. | NFC_PROTO_NFC_DEP_MASK;
  1219. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  1220. PN533_CMD_DATAEXCH_HEAD_LEN,
  1221. PN533_FRAME_TAIL_SIZE);
  1222. if (!dev->nfc_dev)
  1223. goto kill_tasklet;
  1224. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  1225. nfc_set_drvdata(dev->nfc_dev, dev);
  1226. rc = nfc_register_device(dev->nfc_dev);
  1227. if (rc)
  1228. goto free_nfc_dev;
  1229. max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
  1230. max_retries.mx_rty_psl = 2;
  1231. max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1232. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1233. (u8 *) &max_retries, sizeof(max_retries));
  1234. if (rc) {
  1235. nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
  1236. " config");
  1237. goto free_nfc_dev;
  1238. }
  1239. return 0;
  1240. free_nfc_dev:
  1241. nfc_free_device(dev->nfc_dev);
  1242. kill_tasklet:
  1243. tasklet_kill(&dev->tasklet);
  1244. error:
  1245. kfree(dev->in_frame);
  1246. usb_free_urb(dev->in_urb);
  1247. kfree(dev->out_frame);
  1248. usb_free_urb(dev->out_urb);
  1249. kfree(dev);
  1250. return rc;
  1251. }
  1252. static void pn533_disconnect(struct usb_interface *interface)
  1253. {
  1254. struct pn533 *dev;
  1255. dev = usb_get_intfdata(interface);
  1256. usb_set_intfdata(interface, NULL);
  1257. nfc_unregister_device(dev->nfc_dev);
  1258. nfc_free_device(dev->nfc_dev);
  1259. usb_kill_urb(dev->in_urb);
  1260. usb_kill_urb(dev->out_urb);
  1261. tasklet_kill(&dev->tasklet);
  1262. kfree(dev->in_frame);
  1263. usb_free_urb(dev->in_urb);
  1264. kfree(dev->out_frame);
  1265. usb_free_urb(dev->out_urb);
  1266. kfree(dev);
  1267. nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
  1268. }
  1269. static struct usb_driver pn533_driver = {
  1270. .name = "pn533",
  1271. .probe = pn533_probe,
  1272. .disconnect = pn533_disconnect,
  1273. .id_table = pn533_table,
  1274. };
  1275. static int __init pn533_init(void)
  1276. {
  1277. int rc;
  1278. rc = usb_register(&pn533_driver);
  1279. if (rc)
  1280. err("usb_register failed. Error number %d", rc);
  1281. return rc;
  1282. }
  1283. static void __exit pn533_exit(void)
  1284. {
  1285. usb_deregister(&pn533_driver);
  1286. }
  1287. module_init(pn533_init);
  1288. module_exit(pn533_exit);
  1289. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
  1290. " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  1291. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  1292. MODULE_VERSION(VERSION);
  1293. MODULE_LICENSE("GPL");