pn533.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  1. /*
  2. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  3. *
  4. * Authors:
  5. * Lauro Ramos Venancio <lauro.venancio@openbossa.org>
  6. * Aloisio Almeida Jr <aloisio.almeida@openbossa.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <linux/device.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/usb.h>
  28. #include <linux/nfc.h>
  29. #include <linux/netdevice.h>
  30. #include <net/nfc/nfc.h>
  31. #define VERSION "0.1"
  32. #define PN533_VENDOR_ID 0x4CC
  33. #define PN533_PRODUCT_ID 0x2533
  34. #define SCM_VENDOR_ID 0x4E6
  35. #define SCL3711_PRODUCT_ID 0x5591
  36. static const struct usb_device_id pn533_table[] = {
  37. { USB_DEVICE(PN533_VENDOR_ID, PN533_PRODUCT_ID) },
  38. { USB_DEVICE(SCM_VENDOR_ID, SCL3711_PRODUCT_ID) },
  39. { }
  40. };
  41. MODULE_DEVICE_TABLE(usb, pn533_table);
  42. /* frame definitions */
  43. #define PN533_FRAME_TAIL_SIZE 2
  44. #define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \
  45. PN533_FRAME_TAIL_SIZE)
  46. #define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1)
  47. #define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
  48. #define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
  49. /* start of frame */
  50. #define PN533_SOF 0x00FF
  51. /* frame identifier: in/out/error */
  52. #define PN533_FRAME_IDENTIFIER(f) (f->data[0])
  53. #define PN533_DIR_OUT 0xD4
  54. #define PN533_DIR_IN 0xD5
  55. /* PN533 Commands */
  56. #define PN533_FRAME_CMD(f) (f->data[1])
  57. #define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
  58. #define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
  59. #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
  60. #define PN533_CMD_RF_CONFIGURATION 0x32
  61. #define PN533_CMD_IN_DATA_EXCHANGE 0x40
  62. #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
  63. #define PN533_CMD_IN_ATR 0x50
  64. #define PN533_CMD_IN_RELEASE 0x52
  65. #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
  66. #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
  67. #define PN533_CMD_TG_GET_DATA 0x86
  68. #define PN533_CMD_TG_SET_DATA 0x8e
  69. #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
  70. /* PN533 Return codes */
  71. #define PN533_CMD_RET_MASK 0x3F
  72. #define PN533_CMD_MI_MASK 0x40
  73. #define PN533_CMD_RET_SUCCESS 0x00
  74. /* PN533 status codes */
  75. #define PN533_STATUS_TARGET_RELEASED 0x29
  76. struct pn533;
  77. typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg,
  78. u8 *params, int params_len);
  79. /* structs for pn533 commands */
  80. /* PN533_CMD_GET_FIRMWARE_VERSION */
  81. struct pn533_fw_version {
  82. u8 ic;
  83. u8 ver;
  84. u8 rev;
  85. u8 support;
  86. };
  87. /* PN533_CMD_RF_CONFIGURATION */
  88. #define PN533_CFGITEM_MAX_RETRIES 0x05
  89. #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
  90. #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
  91. struct pn533_config_max_retries {
  92. u8 mx_rty_atr;
  93. u8 mx_rty_psl;
  94. u8 mx_rty_passive_act;
  95. } __packed;
  96. /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
  97. /* felica commands opcode */
  98. #define PN533_FELICA_OPC_SENSF_REQ 0
  99. #define PN533_FELICA_OPC_SENSF_RES 1
  100. /* felica SENSF_REQ parameters */
  101. #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
  102. #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
  103. #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
  104. #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
  105. /* type B initiator_data values */
  106. #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
  107. #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
  108. #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
  109. union pn533_cmd_poll_initdata {
  110. struct {
  111. u8 afi;
  112. u8 polling_method;
  113. } __packed type_b;
  114. struct {
  115. u8 opcode;
  116. __be16 sc;
  117. u8 rc;
  118. u8 tsn;
  119. } __packed felica;
  120. };
  121. /* Poll modulations */
  122. enum {
  123. PN533_POLL_MOD_106KBPS_A,
  124. PN533_POLL_MOD_212KBPS_FELICA,
  125. PN533_POLL_MOD_424KBPS_FELICA,
  126. PN533_POLL_MOD_106KBPS_JEWEL,
  127. PN533_POLL_MOD_847KBPS_B,
  128. __PN533_POLL_MOD_AFTER_LAST,
  129. };
  130. #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
  131. struct pn533_poll_modulations {
  132. struct {
  133. u8 maxtg;
  134. u8 brty;
  135. union pn533_cmd_poll_initdata initiator_data;
  136. } __packed data;
  137. u8 len;
  138. };
  139. const struct pn533_poll_modulations poll_mod[] = {
  140. [PN533_POLL_MOD_106KBPS_A] = {
  141. .data = {
  142. .maxtg = 1,
  143. .brty = 0,
  144. },
  145. .len = 2,
  146. },
  147. [PN533_POLL_MOD_212KBPS_FELICA] = {
  148. .data = {
  149. .maxtg = 1,
  150. .brty = 1,
  151. .initiator_data.felica = {
  152. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  153. .sc = PN533_FELICA_SENSF_SC_ALL,
  154. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  155. .tsn = 0,
  156. },
  157. },
  158. .len = 7,
  159. },
  160. [PN533_POLL_MOD_424KBPS_FELICA] = {
  161. .data = {
  162. .maxtg = 1,
  163. .brty = 2,
  164. .initiator_data.felica = {
  165. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  166. .sc = PN533_FELICA_SENSF_SC_ALL,
  167. .rc = PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE,
  168. .tsn = 0,
  169. },
  170. },
  171. .len = 7,
  172. },
  173. [PN533_POLL_MOD_106KBPS_JEWEL] = {
  174. .data = {
  175. .maxtg = 1,
  176. .brty = 4,
  177. },
  178. .len = 2,
  179. },
  180. [PN533_POLL_MOD_847KBPS_B] = {
  181. .data = {
  182. .maxtg = 1,
  183. .brty = 8,
  184. .initiator_data.type_b = {
  185. .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
  186. .polling_method =
  187. PN533_TYPE_B_POLL_METHOD_TIMESLOT,
  188. },
  189. },
  190. .len = 3,
  191. },
  192. };
  193. /* PN533_CMD_IN_ATR */
  194. struct pn533_cmd_activate_param {
  195. u8 tg;
  196. u8 next;
  197. } __packed;
  198. struct pn533_cmd_activate_response {
  199. u8 status;
  200. u8 nfcid3t[10];
  201. u8 didt;
  202. u8 bst;
  203. u8 brt;
  204. u8 to;
  205. u8 ppt;
  206. /* optional */
  207. u8 gt[];
  208. } __packed;
  209. /* PN533_CMD_IN_JUMP_FOR_DEP */
  210. struct pn533_cmd_jump_dep {
  211. u8 active;
  212. u8 baud;
  213. u8 next;
  214. u8 gt[];
  215. } __packed;
  216. struct pn533_cmd_jump_dep_response {
  217. u8 status;
  218. u8 tg;
  219. u8 nfcid3t[10];
  220. u8 didt;
  221. u8 bst;
  222. u8 brt;
  223. u8 to;
  224. u8 ppt;
  225. /* optional */
  226. u8 gt[];
  227. } __packed;
  228. /* PN533_TG_INIT_AS_TARGET */
  229. #define PN533_INIT_TARGET_PASSIVE 0x1
  230. #define PN533_INIT_TARGET_DEP 0x2
  231. #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
  232. #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
  233. #define PN533_INIT_TARGET_RESP_DEP 0x4
  234. struct pn533_cmd_init_target {
  235. u8 mode;
  236. u8 mifare[6];
  237. u8 felica[18];
  238. u8 nfcid3[10];
  239. u8 gb_len;
  240. u8 gb[];
  241. } __packed;
  242. struct pn533_cmd_init_target_response {
  243. u8 mode;
  244. u8 cmd[];
  245. } __packed;
  246. struct pn533 {
  247. struct usb_device *udev;
  248. struct usb_interface *interface;
  249. struct nfc_dev *nfc_dev;
  250. struct urb *out_urb;
  251. int out_maxlen;
  252. struct pn533_frame *out_frame;
  253. struct urb *in_urb;
  254. int in_maxlen;
  255. struct pn533_frame *in_frame;
  256. struct sk_buff_head resp_q;
  257. struct workqueue_struct *wq;
  258. struct work_struct cmd_work;
  259. struct work_struct mi_work;
  260. struct work_struct tg_work;
  261. struct pn533_frame *wq_in_frame;
  262. int wq_in_error;
  263. pn533_cmd_complete_t cmd_complete;
  264. void *cmd_complete_arg;
  265. struct semaphore cmd_lock;
  266. u8 cmd;
  267. struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
  268. u8 poll_mod_count;
  269. u8 poll_mod_curr;
  270. u32 poll_protocols;
  271. u8 tgt_available_prots;
  272. u8 tgt_active_prot;
  273. };
  274. struct pn533_frame {
  275. u8 preamble;
  276. __be16 start_frame;
  277. u8 datalen;
  278. u8 datalen_checksum;
  279. u8 data[];
  280. } __packed;
  281. /* The rule: value + checksum = 0 */
  282. static inline u8 pn533_checksum(u8 value)
  283. {
  284. return ~value + 1;
  285. }
  286. /* The rule: sum(data elements) + checksum = 0 */
  287. static u8 pn533_data_checksum(u8 *data, int datalen)
  288. {
  289. u8 sum = 0;
  290. int i;
  291. for (i = 0; i < datalen; i++)
  292. sum += data[i];
  293. return pn533_checksum(sum);
  294. }
  295. /**
  296. * pn533_tx_frame_ack - create a ack frame
  297. * @frame: The frame to be set as ack
  298. *
  299. * Ack is different type of standard frame. As a standard frame, it has
  300. * preamble and start_frame. However the checksum of this frame must fail,
  301. * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
  302. * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
  303. * After datalen_checksum field, the postamble is placed.
  304. */
  305. static void pn533_tx_frame_ack(struct pn533_frame *frame)
  306. {
  307. frame->preamble = 0;
  308. frame->start_frame = cpu_to_be16(PN533_SOF);
  309. frame->datalen = 0;
  310. frame->datalen_checksum = 0xFF;
  311. /* data[0] is used as postamble */
  312. frame->data[0] = 0;
  313. }
  314. static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
  315. {
  316. frame->preamble = 0;
  317. frame->start_frame = cpu_to_be16(PN533_SOF);
  318. PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
  319. PN533_FRAME_CMD(frame) = cmd;
  320. frame->datalen = 2;
  321. }
  322. static void pn533_tx_frame_finish(struct pn533_frame *frame)
  323. {
  324. frame->datalen_checksum = pn533_checksum(frame->datalen);
  325. PN533_FRAME_CHECKSUM(frame) =
  326. pn533_data_checksum(frame->data, frame->datalen);
  327. PN533_FRAME_POSTAMBLE(frame) = 0;
  328. }
  329. static bool pn533_rx_frame_is_valid(struct pn533_frame *frame)
  330. {
  331. u8 checksum;
  332. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  333. return false;
  334. checksum = pn533_checksum(frame->datalen);
  335. if (checksum != frame->datalen_checksum)
  336. return false;
  337. checksum = pn533_data_checksum(frame->data, frame->datalen);
  338. if (checksum != PN533_FRAME_CHECKSUM(frame))
  339. return false;
  340. return true;
  341. }
  342. static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
  343. {
  344. if (frame->start_frame != cpu_to_be16(PN533_SOF))
  345. return false;
  346. if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
  347. return false;
  348. return true;
  349. }
  350. static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd)
  351. {
  352. return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd));
  353. }
  354. static void pn533_wq_cmd_complete(struct work_struct *work)
  355. {
  356. struct pn533 *dev = container_of(work, struct pn533, cmd_work);
  357. struct pn533_frame *in_frame;
  358. int rc;
  359. in_frame = dev->wq_in_frame;
  360. if (dev->wq_in_error)
  361. rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
  362. dev->wq_in_error);
  363. else
  364. rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
  365. PN533_FRAME_CMD_PARAMS_PTR(in_frame),
  366. PN533_FRAME_CMD_PARAMS_LEN(in_frame));
  367. if (rc != -EINPROGRESS)
  368. up(&dev->cmd_lock);
  369. }
  370. static void pn533_recv_response(struct urb *urb)
  371. {
  372. struct pn533 *dev = urb->context;
  373. struct pn533_frame *in_frame;
  374. dev->wq_in_frame = NULL;
  375. switch (urb->status) {
  376. case 0:
  377. /* success */
  378. break;
  379. case -ECONNRESET:
  380. case -ENOENT:
  381. case -ESHUTDOWN:
  382. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  383. " status: %d", urb->status);
  384. dev->wq_in_error = urb->status;
  385. goto sched_wq;
  386. default:
  387. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  388. " %d", urb->status);
  389. dev->wq_in_error = urb->status;
  390. goto sched_wq;
  391. }
  392. in_frame = dev->in_urb->transfer_buffer;
  393. if (!pn533_rx_frame_is_valid(in_frame)) {
  394. nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
  395. dev->wq_in_error = -EIO;
  396. goto sched_wq;
  397. }
  398. if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) {
  399. nfc_dev_err(&dev->interface->dev, "The received frame is not "
  400. "response to the last command");
  401. dev->wq_in_error = -EIO;
  402. goto sched_wq;
  403. }
  404. nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
  405. dev->wq_in_error = 0;
  406. dev->wq_in_frame = in_frame;
  407. sched_wq:
  408. queue_work(dev->wq, &dev->cmd_work);
  409. }
  410. static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
  411. {
  412. dev->in_urb->complete = pn533_recv_response;
  413. return usb_submit_urb(dev->in_urb, flags);
  414. }
  415. static void pn533_recv_ack(struct urb *urb)
  416. {
  417. struct pn533 *dev = urb->context;
  418. struct pn533_frame *in_frame;
  419. int rc;
  420. switch (urb->status) {
  421. case 0:
  422. /* success */
  423. break;
  424. case -ECONNRESET:
  425. case -ENOENT:
  426. case -ESHUTDOWN:
  427. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  428. " status: %d", urb->status);
  429. dev->wq_in_error = urb->status;
  430. goto sched_wq;
  431. default:
  432. nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:"
  433. " %d", urb->status);
  434. dev->wq_in_error = urb->status;
  435. goto sched_wq;
  436. }
  437. in_frame = dev->in_urb->transfer_buffer;
  438. if (!pn533_rx_frame_is_ack(in_frame)) {
  439. nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
  440. dev->wq_in_error = -EIO;
  441. goto sched_wq;
  442. }
  443. nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
  444. rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
  445. if (rc) {
  446. nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with"
  447. " result %d", rc);
  448. dev->wq_in_error = rc;
  449. goto sched_wq;
  450. }
  451. return;
  452. sched_wq:
  453. dev->wq_in_frame = NULL;
  454. queue_work(dev->wq, &dev->cmd_work);
  455. }
  456. static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
  457. {
  458. dev->in_urb->complete = pn533_recv_ack;
  459. return usb_submit_urb(dev->in_urb, flags);
  460. }
  461. static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
  462. {
  463. int rc;
  464. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  465. pn533_tx_frame_ack(dev->out_frame);
  466. dev->out_urb->transfer_buffer = dev->out_frame;
  467. dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
  468. rc = usb_submit_urb(dev->out_urb, flags);
  469. return rc;
  470. }
  471. static int __pn533_send_cmd_frame_async(struct pn533 *dev,
  472. struct pn533_frame *out_frame,
  473. struct pn533_frame *in_frame,
  474. int in_frame_len,
  475. pn533_cmd_complete_t cmd_complete,
  476. void *arg, gfp_t flags)
  477. {
  478. int rc;
  479. nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x",
  480. PN533_FRAME_CMD(out_frame));
  481. dev->cmd = PN533_FRAME_CMD(out_frame);
  482. dev->cmd_complete = cmd_complete;
  483. dev->cmd_complete_arg = arg;
  484. dev->out_urb->transfer_buffer = out_frame;
  485. dev->out_urb->transfer_buffer_length =
  486. PN533_FRAME_SIZE(out_frame);
  487. dev->in_urb->transfer_buffer = in_frame;
  488. dev->in_urb->transfer_buffer_length = in_frame_len;
  489. rc = usb_submit_urb(dev->out_urb, flags);
  490. if (rc)
  491. return rc;
  492. rc = pn533_submit_urb_for_ack(dev, flags);
  493. if (rc)
  494. goto error;
  495. return 0;
  496. error:
  497. usb_unlink_urb(dev->out_urb);
  498. return rc;
  499. }
  500. static int pn533_send_cmd_frame_async(struct pn533 *dev,
  501. struct pn533_frame *out_frame,
  502. struct pn533_frame *in_frame,
  503. int in_frame_len,
  504. pn533_cmd_complete_t cmd_complete,
  505. void *arg, gfp_t flags)
  506. {
  507. int rc;
  508. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  509. if (down_trylock(&dev->cmd_lock))
  510. return -EBUSY;
  511. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  512. in_frame_len, cmd_complete, arg, flags);
  513. if (rc)
  514. goto error;
  515. return 0;
  516. error:
  517. up(&dev->cmd_lock);
  518. return rc;
  519. }
  520. struct pn533_sync_cmd_response {
  521. int rc;
  522. struct completion done;
  523. };
  524. static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg,
  525. u8 *params, int params_len)
  526. {
  527. struct pn533_sync_cmd_response *arg = _arg;
  528. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  529. arg->rc = 0;
  530. if (params_len < 0) /* error */
  531. arg->rc = params_len;
  532. complete(&arg->done);
  533. return 0;
  534. }
  535. static int pn533_send_cmd_frame_sync(struct pn533 *dev,
  536. struct pn533_frame *out_frame,
  537. struct pn533_frame *in_frame,
  538. int in_frame_len)
  539. {
  540. int rc;
  541. struct pn533_sync_cmd_response arg;
  542. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  543. init_completion(&arg.done);
  544. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len,
  545. pn533_sync_cmd_complete, &arg, GFP_KERNEL);
  546. if (rc)
  547. return rc;
  548. wait_for_completion(&arg.done);
  549. return arg.rc;
  550. }
  551. static void pn533_send_complete(struct urb *urb)
  552. {
  553. struct pn533 *dev = urb->context;
  554. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  555. switch (urb->status) {
  556. case 0:
  557. /* success */
  558. break;
  559. case -ECONNRESET:
  560. case -ENOENT:
  561. case -ESHUTDOWN:
  562. nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with"
  563. " status: %d", urb->status);
  564. break;
  565. default:
  566. nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:"
  567. " %d", urb->status);
  568. }
  569. }
  570. struct pn533_target_type_a {
  571. __be16 sens_res;
  572. u8 sel_res;
  573. u8 nfcid_len;
  574. u8 nfcid_data[];
  575. } __packed;
  576. #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
  577. #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
  578. #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
  579. #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
  580. #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
  581. #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  582. #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
  583. #define PN533_TYPE_A_SEL_PROT_MIFARE 0
  584. #define PN533_TYPE_A_SEL_PROT_ISO14443 1
  585. #define PN533_TYPE_A_SEL_PROT_DEP 2
  586. #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
  587. static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
  588. int target_data_len)
  589. {
  590. u8 ssd;
  591. u8 platconf;
  592. if (target_data_len < sizeof(struct pn533_target_type_a))
  593. return false;
  594. /* The lenght check of nfcid[] and ats[] are not being performed because
  595. the values are not being used */
  596. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  597. ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
  598. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
  599. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  600. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  601. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  602. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  603. return false;
  604. /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
  605. if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
  606. return false;
  607. return true;
  608. }
  609. static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
  610. int tgt_data_len)
  611. {
  612. struct pn533_target_type_a *tgt_type_a;
  613. tgt_type_a = (struct pn533_target_type_a *) tgt_data;
  614. if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
  615. return -EPROTO;
  616. switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
  617. case PN533_TYPE_A_SEL_PROT_MIFARE:
  618. nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
  619. break;
  620. case PN533_TYPE_A_SEL_PROT_ISO14443:
  621. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  622. break;
  623. case PN533_TYPE_A_SEL_PROT_DEP:
  624. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  625. break;
  626. case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
  627. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
  628. NFC_PROTO_NFC_DEP_MASK;
  629. break;
  630. }
  631. nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
  632. nfc_tgt->sel_res = tgt_type_a->sel_res;
  633. nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
  634. memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
  635. return 0;
  636. }
  637. struct pn533_target_felica {
  638. u8 pol_res;
  639. u8 opcode;
  640. u8 nfcid2[8];
  641. u8 pad[8];
  642. /* optional */
  643. u8 syst_code[];
  644. } __packed;
  645. #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
  646. #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
  647. static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
  648. int target_data_len)
  649. {
  650. if (target_data_len < sizeof(struct pn533_target_felica))
  651. return false;
  652. if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
  653. return false;
  654. return true;
  655. }
  656. static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
  657. int tgt_data_len)
  658. {
  659. struct pn533_target_felica *tgt_felica;
  660. tgt_felica = (struct pn533_target_felica *) tgt_data;
  661. if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
  662. return -EPROTO;
  663. if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 &&
  664. tgt_felica->nfcid2[1] ==
  665. PN533_FELICA_SENSF_NFCID2_DEP_B2)
  666. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  667. else
  668. nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
  669. memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
  670. nfc_tgt->sensf_res_len = 9;
  671. return 0;
  672. }
  673. struct pn533_target_jewel {
  674. __be16 sens_res;
  675. u8 jewelid[4];
  676. } __packed;
  677. static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
  678. int target_data_len)
  679. {
  680. u8 ssd;
  681. u8 platconf;
  682. if (target_data_len < sizeof(struct pn533_target_jewel))
  683. return false;
  684. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  685. ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
  686. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
  687. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  688. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  689. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  690. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  691. return false;
  692. return true;
  693. }
  694. static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
  695. int tgt_data_len)
  696. {
  697. struct pn533_target_jewel *tgt_jewel;
  698. tgt_jewel = (struct pn533_target_jewel *) tgt_data;
  699. if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
  700. return -EPROTO;
  701. nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
  702. nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
  703. nfc_tgt->nfcid1_len = 4;
  704. memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
  705. return 0;
  706. }
  707. struct pn533_type_b_prot_info {
  708. u8 bitrate;
  709. u8 fsci_type;
  710. u8 fwi_adc_fo;
  711. } __packed;
  712. #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
  713. #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
  714. #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
  715. struct pn533_type_b_sens_res {
  716. u8 opcode;
  717. u8 nfcid[4];
  718. u8 appdata[4];
  719. struct pn533_type_b_prot_info prot_info;
  720. } __packed;
  721. #define PN533_TYPE_B_OPC_SENSB_RES 0x50
  722. struct pn533_target_type_b {
  723. struct pn533_type_b_sens_res sensb_res;
  724. u8 attrib_res_len;
  725. u8 attrib_res[];
  726. } __packed;
  727. static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
  728. int target_data_len)
  729. {
  730. if (target_data_len < sizeof(struct pn533_target_type_b))
  731. return false;
  732. if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
  733. return false;
  734. if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
  735. PN533_TYPE_B_PROT_TYPE_RFU_MASK)
  736. return false;
  737. return true;
  738. }
  739. static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
  740. int tgt_data_len)
  741. {
  742. struct pn533_target_type_b *tgt_type_b;
  743. tgt_type_b = (struct pn533_target_type_b *) tgt_data;
  744. if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
  745. return -EPROTO;
  746. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  747. return 0;
  748. }
  749. struct pn533_poll_response {
  750. u8 nbtg;
  751. u8 tg;
  752. u8 target_data[];
  753. } __packed;
  754. static int pn533_target_found(struct pn533 *dev,
  755. struct pn533_poll_response *resp, int resp_len)
  756. {
  757. int target_data_len;
  758. struct nfc_target nfc_tgt;
  759. int rc;
  760. nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
  761. dev->poll_mod_curr);
  762. if (resp->tg != 1)
  763. return -EPROTO;
  764. memset(&nfc_tgt, 0, sizeof(struct nfc_target));
  765. target_data_len = resp_len - sizeof(struct pn533_poll_response);
  766. switch (dev->poll_mod_curr) {
  767. case PN533_POLL_MOD_106KBPS_A:
  768. rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data,
  769. target_data_len);
  770. break;
  771. case PN533_POLL_MOD_212KBPS_FELICA:
  772. case PN533_POLL_MOD_424KBPS_FELICA:
  773. rc = pn533_target_found_felica(&nfc_tgt, resp->target_data,
  774. target_data_len);
  775. break;
  776. case PN533_POLL_MOD_106KBPS_JEWEL:
  777. rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data,
  778. target_data_len);
  779. break;
  780. case PN533_POLL_MOD_847KBPS_B:
  781. rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data,
  782. target_data_len);
  783. break;
  784. default:
  785. nfc_dev_err(&dev->interface->dev, "Unknown current poll"
  786. " modulation");
  787. return -EPROTO;
  788. }
  789. if (rc)
  790. return rc;
  791. if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
  792. nfc_dev_dbg(&dev->interface->dev, "The target found does not"
  793. " have the desired protocol");
  794. return -EAGAIN;
  795. }
  796. nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: "
  797. "0x%x", nfc_tgt.supported_protocols);
  798. dev->tgt_available_prots = nfc_tgt.supported_protocols;
  799. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  800. return 0;
  801. }
  802. static void pn533_poll_reset_mod_list(struct pn533 *dev)
  803. {
  804. dev->poll_mod_count = 0;
  805. }
  806. static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
  807. {
  808. dev->poll_mod_active[dev->poll_mod_count] =
  809. (struct pn533_poll_modulations *) &poll_mod[mod_index];
  810. dev->poll_mod_count++;
  811. }
  812. static void pn533_poll_create_mod_list(struct pn533 *dev, u32 protocols)
  813. {
  814. pn533_poll_reset_mod_list(dev);
  815. if (protocols & NFC_PROTO_MIFARE_MASK
  816. || protocols & NFC_PROTO_ISO14443_MASK
  817. || protocols & NFC_PROTO_NFC_DEP_MASK)
  818. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
  819. if (protocols & NFC_PROTO_FELICA_MASK
  820. || protocols & NFC_PROTO_NFC_DEP_MASK) {
  821. pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
  822. pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
  823. }
  824. if (protocols & NFC_PROTO_JEWEL_MASK)
  825. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
  826. if (protocols & NFC_PROTO_ISO14443_MASK)
  827. pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
  828. }
  829. static void pn533_start_poll_frame(struct pn533_frame *frame,
  830. struct pn533_poll_modulations *mod)
  831. {
  832. pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
  833. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len);
  834. frame->datalen += mod->len;
  835. pn533_tx_frame_finish(frame);
  836. }
  837. static int pn533_start_poll_complete(struct pn533 *dev, void *arg,
  838. u8 *params, int params_len)
  839. {
  840. struct pn533_poll_response *resp;
  841. struct pn533_poll_modulations *next_mod;
  842. int rc;
  843. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  844. if (params_len == -ENOENT) {
  845. nfc_dev_dbg(&dev->interface->dev, "Polling operation has been"
  846. " stopped");
  847. goto stop_poll;
  848. }
  849. if (params_len < 0) {
  850. nfc_dev_err(&dev->interface->dev, "Error %d when running poll",
  851. params_len);
  852. goto stop_poll;
  853. }
  854. resp = (struct pn533_poll_response *) params;
  855. if (resp->nbtg) {
  856. rc = pn533_target_found(dev, resp, params_len);
  857. /* We must stop the poll after a valid target found */
  858. if (rc == 0)
  859. goto stop_poll;
  860. if (rc != -EAGAIN)
  861. nfc_dev_err(&dev->interface->dev, "The target found is"
  862. " not valid - continuing to poll");
  863. }
  864. dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
  865. next_mod = dev->poll_mod_active[dev->poll_mod_curr];
  866. nfc_dev_dbg(&dev->interface->dev, "Polling next modulation (0x%x)",
  867. dev->poll_mod_curr);
  868. pn533_start_poll_frame(dev->out_frame, next_mod);
  869. /* Don't need to down the semaphore again */
  870. rc = __pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  871. dev->in_maxlen, pn533_start_poll_complete,
  872. NULL, GFP_ATOMIC);
  873. if (rc == -EPERM) {
  874. nfc_dev_dbg(&dev->interface->dev, "Cannot poll next modulation"
  875. " because poll has been stopped");
  876. goto stop_poll;
  877. }
  878. if (rc) {
  879. nfc_dev_err(&dev->interface->dev, "Error %d when trying to poll"
  880. " next modulation", rc);
  881. goto stop_poll;
  882. }
  883. /* Inform caller function to do not up the semaphore */
  884. return -EINPROGRESS;
  885. stop_poll:
  886. pn533_poll_reset_mod_list(dev);
  887. dev->poll_protocols = 0;
  888. return 0;
  889. }
  890. static int pn533_init_target_frame(struct pn533_frame *frame,
  891. u8 *gb, size_t gb_len)
  892. {
  893. struct pn533_cmd_init_target *cmd;
  894. size_t cmd_len;
  895. cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1;
  896. cmd = kzalloc(cmd_len, GFP_KERNEL);
  897. if (cmd == NULL)
  898. return -ENOMEM;
  899. pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET);
  900. /* DEP support only */
  901. cmd->mode |= PN533_INIT_TARGET_DEP;
  902. get_random_bytes(cmd->nfcid3, 10);
  903. cmd->gb_len = gb_len;
  904. memcpy(cmd->gb, gb, gb_len);
  905. /* Len Tk */
  906. cmd->gb[gb_len] = 0;
  907. memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
  908. frame->datalen += cmd_len;
  909. pn533_tx_frame_finish(frame);
  910. return 0;
  911. }
  912. #define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3)
  913. #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
  914. static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
  915. u8 *params, int params_len)
  916. {
  917. struct sk_buff *skb_resp = arg;
  918. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  919. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  920. if (params_len < 0) {
  921. nfc_dev_err(&dev->interface->dev,
  922. "Error %d when starting as a target",
  923. params_len);
  924. return params_len;
  925. }
  926. if (params_len > 0 && params[0] != 0) {
  927. nfc_tm_deactivated(dev->nfc_dev);
  928. kfree_skb(skb_resp);
  929. return 0;
  930. }
  931. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  932. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  933. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
  934. return nfc_tm_data_received(dev->nfc_dev, skb_resp);
  935. }
  936. static void pn533_wq_tg_get_data(struct work_struct *work)
  937. {
  938. struct pn533 *dev = container_of(work, struct pn533, tg_work);
  939. struct pn533_frame *in_frame;
  940. struct sk_buff *skb_resp;
  941. size_t skb_resp_len;
  942. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  943. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  944. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  945. PN533_FRAME_TAIL_SIZE;
  946. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  947. if (!skb_resp)
  948. return;
  949. in_frame = (struct pn533_frame *)skb_resp->data;
  950. pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA);
  951. pn533_tx_frame_finish(dev->out_frame);
  952. pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
  953. skb_resp_len,
  954. pn533_tm_get_data_complete,
  955. skb_resp, GFP_KERNEL);
  956. return;
  957. }
  958. #define ATR_REQ_GB_OFFSET 17
  959. static int pn533_init_target_complete(struct pn533 *dev, void *arg,
  960. u8 *params, int params_len)
  961. {
  962. struct pn533_cmd_init_target_response *resp;
  963. u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
  964. size_t gb_len;
  965. int rc;
  966. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  967. if (params_len < 0) {
  968. nfc_dev_err(&dev->interface->dev,
  969. "Error %d when starting as a target",
  970. params_len);
  971. return params_len;
  972. }
  973. if (params_len < ATR_REQ_GB_OFFSET + 1)
  974. return -EINVAL;
  975. resp = (struct pn533_cmd_init_target_response *) params;
  976. nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n",
  977. resp->mode, params_len);
  978. frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK;
  979. if (frame == PN533_INIT_TARGET_RESP_ACTIVE)
  980. comm_mode = NFC_COMM_ACTIVE;
  981. /* Again, only DEP */
  982. if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
  983. return -EOPNOTSUPP;
  984. gb = resp->cmd + ATR_REQ_GB_OFFSET;
  985. gb_len = params_len - (ATR_REQ_GB_OFFSET + 1);
  986. rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  987. comm_mode, gb, gb_len);
  988. if (rc < 0) {
  989. nfc_dev_err(&dev->interface->dev,
  990. "Error when signaling target activation");
  991. return rc;
  992. }
  993. queue_work(dev->wq, &dev->tg_work);
  994. return 0;
  995. }
  996. static int pn533_init_target(struct nfc_dev *nfc_dev, u32 protocols)
  997. {
  998. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  999. u8 *gb;
  1000. size_t gb_len;
  1001. int rc;
  1002. pn533_poll_reset_mod_list(dev);
  1003. gb = nfc_get_local_general_bytes(nfc_dev, &gb_len);
  1004. if (gb == NULL)
  1005. return -ENOMEM;
  1006. rc = pn533_init_target_frame(dev->out_frame, gb, gb_len);
  1007. if (rc < 0)
  1008. return rc;
  1009. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1010. dev->in_maxlen,
  1011. pn533_init_target_complete,
  1012. NULL, GFP_KERNEL);
  1013. if (rc)
  1014. nfc_dev_err(&dev->interface->dev,
  1015. "Error %d when trying to initiate as a target", rc);
  1016. dev->poll_mod_count++;
  1017. return rc;
  1018. }
  1019. static int pn533_start_im_poll(struct nfc_dev *nfc_dev, u32 protocols)
  1020. {
  1021. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1022. struct pn533_poll_modulations *start_mod;
  1023. int rc;
  1024. if (dev->poll_mod_count) {
  1025. nfc_dev_err(&dev->interface->dev, "Polling operation already"
  1026. " active");
  1027. return -EBUSY;
  1028. }
  1029. pn533_poll_create_mod_list(dev, protocols);
  1030. if (!dev->poll_mod_count) {
  1031. nfc_dev_err(&dev->interface->dev, "No valid protocols"
  1032. " specified");
  1033. rc = -EINVAL;
  1034. goto error;
  1035. }
  1036. nfc_dev_dbg(&dev->interface->dev, "It will poll %d modulations types",
  1037. dev->poll_mod_count);
  1038. dev->poll_mod_curr = 0;
  1039. start_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1040. pn533_start_poll_frame(dev->out_frame, start_mod);
  1041. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1042. dev->in_maxlen, pn533_start_poll_complete,
  1043. NULL, GFP_KERNEL);
  1044. if (rc) {
  1045. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1046. " start poll", rc);
  1047. goto error;
  1048. }
  1049. dev->poll_protocols = protocols;
  1050. return 0;
  1051. error:
  1052. pn533_poll_reset_mod_list(dev);
  1053. return rc;
  1054. }
  1055. static int pn533_start_poll(struct nfc_dev *nfc_dev,
  1056. u32 im_protocols, u32 tm_protocols)
  1057. {
  1058. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1059. nfc_dev_dbg(&dev->interface->dev,
  1060. "%s: im protocols 0x%x tm protocols 0x%x",
  1061. __func__, im_protocols, tm_protocols);
  1062. if (dev->tgt_active_prot) {
  1063. nfc_dev_err(&dev->interface->dev,
  1064. "Cannot poll with a target already activated");
  1065. return -EBUSY;
  1066. }
  1067. if (im_protocols)
  1068. return pn533_start_im_poll(nfc_dev, im_protocols);
  1069. if (tm_protocols)
  1070. return pn533_init_target(nfc_dev, tm_protocols);
  1071. return -EINVAL;
  1072. }
  1073. static void pn533_stop_poll(struct nfc_dev *nfc_dev)
  1074. {
  1075. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1076. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1077. if (!dev->poll_mod_count) {
  1078. nfc_dev_dbg(&dev->interface->dev, "Polling operation was not"
  1079. " running");
  1080. return;
  1081. }
  1082. /* An ack will cancel the last issued command (poll) */
  1083. pn533_send_ack(dev, GFP_KERNEL);
  1084. /* prevent pn533_start_poll_complete to issue a new poll meanwhile */
  1085. usb_kill_urb(dev->in_urb);
  1086. pn533_poll_reset_mod_list(dev);
  1087. }
  1088. static int pn533_activate_target_nfcdep(struct pn533 *dev)
  1089. {
  1090. struct pn533_cmd_activate_param param;
  1091. struct pn533_cmd_activate_response *resp;
  1092. u16 gt_len;
  1093. int rc;
  1094. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1095. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR);
  1096. param.tg = 1;
  1097. param.next = 0;
  1098. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param,
  1099. sizeof(struct pn533_cmd_activate_param));
  1100. dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
  1101. pn533_tx_frame_finish(dev->out_frame);
  1102. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1103. dev->in_maxlen);
  1104. if (rc)
  1105. return rc;
  1106. resp = (struct pn533_cmd_activate_response *)
  1107. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1108. rc = resp->status & PN533_CMD_RET_MASK;
  1109. if (rc != PN533_CMD_RET_SUCCESS)
  1110. return -EIO;
  1111. /* ATR_RES general bytes are located at offset 16 */
  1112. gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16;
  1113. rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len);
  1114. return rc;
  1115. }
  1116. static int pn533_activate_target(struct nfc_dev *nfc_dev,
  1117. struct nfc_target *target, u32 protocol)
  1118. {
  1119. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1120. int rc;
  1121. nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
  1122. protocol);
  1123. if (dev->poll_mod_count) {
  1124. nfc_dev_err(&dev->interface->dev, "Cannot activate while"
  1125. " polling");
  1126. return -EBUSY;
  1127. }
  1128. if (dev->tgt_active_prot) {
  1129. nfc_dev_err(&dev->interface->dev, "There is already an active"
  1130. " target");
  1131. return -EBUSY;
  1132. }
  1133. if (!dev->tgt_available_prots) {
  1134. nfc_dev_err(&dev->interface->dev, "There is no available target"
  1135. " to activate");
  1136. return -EINVAL;
  1137. }
  1138. if (!(dev->tgt_available_prots & (1 << protocol))) {
  1139. nfc_dev_err(&dev->interface->dev, "The target does not support"
  1140. " the requested protocol %u", protocol);
  1141. return -EINVAL;
  1142. }
  1143. if (protocol == NFC_PROTO_NFC_DEP) {
  1144. rc = pn533_activate_target_nfcdep(dev);
  1145. if (rc) {
  1146. nfc_dev_err(&dev->interface->dev, "Error %d when"
  1147. " activating target with"
  1148. " NFC_DEP protocol", rc);
  1149. return rc;
  1150. }
  1151. }
  1152. dev->tgt_active_prot = protocol;
  1153. dev->tgt_available_prots = 0;
  1154. return 0;
  1155. }
  1156. static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
  1157. struct nfc_target *target)
  1158. {
  1159. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1160. u8 tg;
  1161. u8 status;
  1162. int rc;
  1163. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1164. if (!dev->tgt_active_prot) {
  1165. nfc_dev_err(&dev->interface->dev, "There is no active target");
  1166. return;
  1167. }
  1168. dev->tgt_active_prot = 0;
  1169. skb_queue_purge(&dev->resp_q);
  1170. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE);
  1171. tg = 1;
  1172. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
  1173. dev->out_frame->datalen += sizeof(u8);
  1174. pn533_tx_frame_finish(dev->out_frame);
  1175. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1176. dev->in_maxlen);
  1177. if (rc) {
  1178. nfc_dev_err(&dev->interface->dev, "Error when sending release"
  1179. " command to the controller");
  1180. return;
  1181. }
  1182. status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0];
  1183. rc = status & PN533_CMD_RET_MASK;
  1184. if (rc != PN533_CMD_RET_SUCCESS)
  1185. nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing"
  1186. " the target", rc);
  1187. return;
  1188. }
  1189. static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
  1190. u8 *params, int params_len)
  1191. {
  1192. struct pn533_cmd_jump_dep *cmd;
  1193. struct pn533_cmd_jump_dep_response *resp;
  1194. struct nfc_target nfc_target;
  1195. u8 target_gt_len;
  1196. int rc;
  1197. if (params_len == -ENOENT) {
  1198. nfc_dev_dbg(&dev->interface->dev, "");
  1199. return 0;
  1200. }
  1201. if (params_len < 0) {
  1202. nfc_dev_err(&dev->interface->dev,
  1203. "Error %d when bringing DEP link up",
  1204. params_len);
  1205. return 0;
  1206. }
  1207. if (dev->tgt_available_prots &&
  1208. !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
  1209. nfc_dev_err(&dev->interface->dev,
  1210. "The target does not support DEP");
  1211. return -EINVAL;
  1212. }
  1213. resp = (struct pn533_cmd_jump_dep_response *) params;
  1214. cmd = (struct pn533_cmd_jump_dep *) arg;
  1215. rc = resp->status & PN533_CMD_RET_MASK;
  1216. if (rc != PN533_CMD_RET_SUCCESS) {
  1217. nfc_dev_err(&dev->interface->dev,
  1218. "Bringing DEP link up failed %d", rc);
  1219. return 0;
  1220. }
  1221. if (!dev->tgt_available_prots) {
  1222. nfc_dev_dbg(&dev->interface->dev, "Creating new target");
  1223. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1224. nfc_target.nfcid1_len = 10;
  1225. memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len);
  1226. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1227. if (rc)
  1228. return 0;
  1229. dev->tgt_available_prots = 0;
  1230. }
  1231. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1232. /* ATR_RES general bytes are located at offset 17 */
  1233. target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17;
  1234. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1235. resp->gt, target_gt_len);
  1236. if (rc == 0)
  1237. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1238. dev->nfc_dev->targets[0].idx,
  1239. !cmd->active, NFC_RF_INITIATOR);
  1240. return 0;
  1241. }
  1242. static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  1243. u8 comm_mode, u8* gb, size_t gb_len)
  1244. {
  1245. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1246. struct pn533_cmd_jump_dep *cmd;
  1247. u8 cmd_len;
  1248. int rc;
  1249. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1250. if (dev->poll_mod_count) {
  1251. nfc_dev_err(&dev->interface->dev,
  1252. "Cannot bring the DEP link up while polling");
  1253. return -EBUSY;
  1254. }
  1255. if (dev->tgt_active_prot) {
  1256. nfc_dev_err(&dev->interface->dev,
  1257. "There is already an active target");
  1258. return -EBUSY;
  1259. }
  1260. cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len;
  1261. cmd = kzalloc(cmd_len, GFP_KERNEL);
  1262. if (cmd == NULL)
  1263. return -ENOMEM;
  1264. pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP);
  1265. cmd->active = !comm_mode;
  1266. cmd->baud = 0;
  1267. if (gb != NULL && gb_len > 0) {
  1268. cmd->next = 4; /* We have some Gi */
  1269. memcpy(cmd->gt, gb, gb_len);
  1270. } else {
  1271. cmd->next = 0;
  1272. }
  1273. memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len);
  1274. dev->out_frame->datalen += cmd_len;
  1275. pn533_tx_frame_finish(dev->out_frame);
  1276. rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame,
  1277. dev->in_maxlen, pn533_in_dep_link_up_complete,
  1278. cmd, GFP_KERNEL);
  1279. if (rc)
  1280. goto out;
  1281. out:
  1282. kfree(cmd);
  1283. return rc;
  1284. }
  1285. static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
  1286. {
  1287. pn533_deactivate_target(nfc_dev, 0);
  1288. return 0;
  1289. }
  1290. static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
  1291. bool target)
  1292. {
  1293. int payload_len = skb->len;
  1294. struct pn533_frame *out_frame;
  1295. u8 tg;
  1296. nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
  1297. payload_len);
  1298. if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  1299. /* TODO: Implement support to multi-part data exchange */
  1300. nfc_dev_err(&dev->interface->dev, "Data length greater than the"
  1301. " max allowed: %d",
  1302. PN533_CMD_DATAEXCH_DATA_MAXLEN);
  1303. return -ENOSYS;
  1304. }
  1305. if (target == true) {
  1306. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
  1307. out_frame = (struct pn533_frame *) skb->data;
  1308. pn533_tx_frame_init(out_frame, PN533_CMD_IN_DATA_EXCHANGE);
  1309. tg = 1;
  1310. memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame), &tg, sizeof(u8));
  1311. out_frame->datalen += sizeof(u8);
  1312. } else {
  1313. skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
  1314. out_frame = (struct pn533_frame *) skb->data;
  1315. pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
  1316. }
  1317. /* The data is already in the out_frame, just update the datalen */
  1318. out_frame->datalen += payload_len;
  1319. pn533_tx_frame_finish(out_frame);
  1320. skb_put(skb, PN533_FRAME_TAIL_SIZE);
  1321. return 0;
  1322. }
  1323. struct pn533_data_exchange_arg {
  1324. struct sk_buff *skb_resp;
  1325. struct sk_buff *skb_out;
  1326. data_exchange_cb_t cb;
  1327. void *cb_context;
  1328. };
  1329. static struct sk_buff *pn533_build_response(struct pn533 *dev)
  1330. {
  1331. struct sk_buff *skb, *tmp, *t;
  1332. unsigned int skb_len = 0, tmp_len = 0;
  1333. nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__);
  1334. if (skb_queue_empty(&dev->resp_q))
  1335. return NULL;
  1336. if (skb_queue_len(&dev->resp_q) == 1) {
  1337. skb = skb_dequeue(&dev->resp_q);
  1338. goto out;
  1339. }
  1340. skb_queue_walk_safe(&dev->resp_q, tmp, t)
  1341. skb_len += tmp->len;
  1342. nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
  1343. __func__, skb_len);
  1344. skb = alloc_skb(skb_len, GFP_KERNEL);
  1345. if (skb == NULL)
  1346. goto out;
  1347. skb_put(skb, skb_len);
  1348. skb_queue_walk_safe(&dev->resp_q, tmp, t) {
  1349. memcpy(skb->data + tmp_len, tmp->data, tmp->len);
  1350. tmp_len += tmp->len;
  1351. }
  1352. out:
  1353. skb_queue_purge(&dev->resp_q);
  1354. return skb;
  1355. }
  1356. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1357. u8 *params, int params_len)
  1358. {
  1359. struct pn533_data_exchange_arg *arg = _arg;
  1360. struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp;
  1361. struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
  1362. int err = 0;
  1363. u8 status;
  1364. u8 cmd_ret;
  1365. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1366. dev_kfree_skb(arg->skb_out);
  1367. if (params_len < 0) { /* error */
  1368. err = params_len;
  1369. goto error;
  1370. }
  1371. status = params[0];
  1372. cmd_ret = status & PN533_CMD_RET_MASK;
  1373. if (cmd_ret != PN533_CMD_RET_SUCCESS) {
  1374. nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when"
  1375. " exchanging data", cmd_ret);
  1376. err = -EIO;
  1377. goto error;
  1378. }
  1379. skb_put(skb_resp, PN533_FRAME_SIZE(in_frame));
  1380. skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
  1381. skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
  1382. skb_queue_tail(&dev->resp_q, skb_resp);
  1383. if (status & PN533_CMD_MI_MASK) {
  1384. queue_work(dev->wq, &dev->mi_work);
  1385. return -EINPROGRESS;
  1386. }
  1387. skb = pn533_build_response(dev);
  1388. if (skb == NULL)
  1389. goto error;
  1390. arg->cb(arg->cb_context, skb, 0);
  1391. kfree(arg);
  1392. return 0;
  1393. error:
  1394. skb_queue_purge(&dev->resp_q);
  1395. dev_kfree_skb(skb_resp);
  1396. arg->cb(arg->cb_context, NULL, err);
  1397. kfree(arg);
  1398. return 0;
  1399. }
  1400. static int pn533_transceive(struct nfc_dev *nfc_dev,
  1401. struct nfc_target *target, struct sk_buff *skb,
  1402. data_exchange_cb_t cb, void *cb_context)
  1403. {
  1404. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1405. struct pn533_frame *out_frame, *in_frame;
  1406. struct pn533_data_exchange_arg *arg;
  1407. struct sk_buff *skb_resp;
  1408. int skb_resp_len;
  1409. int rc;
  1410. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1411. if (!dev->tgt_active_prot) {
  1412. nfc_dev_err(&dev->interface->dev, "Cannot exchange data if"
  1413. " there is no active target");
  1414. rc = -EINVAL;
  1415. goto error;
  1416. }
  1417. rc = pn533_build_tx_frame(dev, skb, true);
  1418. if (rc)
  1419. goto error;
  1420. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1421. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1422. PN533_FRAME_TAIL_SIZE;
  1423. skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
  1424. if (!skb_resp) {
  1425. rc = -ENOMEM;
  1426. goto error;
  1427. }
  1428. in_frame = (struct pn533_frame *) skb_resp->data;
  1429. out_frame = (struct pn533_frame *) skb->data;
  1430. arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
  1431. if (!arg) {
  1432. rc = -ENOMEM;
  1433. goto free_skb_resp;
  1434. }
  1435. arg->skb_resp = skb_resp;
  1436. arg->skb_out = skb;
  1437. arg->cb = cb;
  1438. arg->cb_context = cb_context;
  1439. rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len,
  1440. pn533_data_exchange_complete, arg,
  1441. GFP_KERNEL);
  1442. if (rc) {
  1443. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1444. " perform data_exchange", rc);
  1445. goto free_arg;
  1446. }
  1447. return 0;
  1448. free_arg:
  1449. kfree(arg);
  1450. free_skb_resp:
  1451. kfree_skb(skb_resp);
  1452. error:
  1453. kfree_skb(skb);
  1454. return rc;
  1455. }
  1456. static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
  1457. u8 *params, int params_len)
  1458. {
  1459. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1460. if (params_len < 0) {
  1461. nfc_dev_err(&dev->interface->dev,
  1462. "Error %d when sending data",
  1463. params_len);
  1464. return params_len;
  1465. }
  1466. if (params_len > 0 && params[0] != 0) {
  1467. nfc_tm_deactivated(dev->nfc_dev);
  1468. return 0;
  1469. }
  1470. queue_work(dev->wq, &dev->tg_work);
  1471. return 0;
  1472. }
  1473. static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  1474. {
  1475. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1476. struct pn533_frame *out_frame;
  1477. int rc;
  1478. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1479. rc = pn533_build_tx_frame(dev, skb, false);
  1480. if (rc)
  1481. goto error;
  1482. out_frame = (struct pn533_frame *) skb->data;
  1483. rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
  1484. dev->in_maxlen, pn533_tm_send_complete,
  1485. NULL, GFP_KERNEL);
  1486. if (rc) {
  1487. nfc_dev_err(&dev->interface->dev,
  1488. "Error %d when trying to send data", rc);
  1489. goto error;
  1490. }
  1491. return 0;
  1492. error:
  1493. kfree_skb(skb);
  1494. return rc;
  1495. }
  1496. static void pn533_wq_mi_recv(struct work_struct *work)
  1497. {
  1498. struct pn533 *dev = container_of(work, struct pn533, mi_work);
  1499. struct sk_buff *skb_cmd;
  1500. struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg;
  1501. struct pn533_frame *out_frame, *in_frame;
  1502. struct sk_buff *skb_resp;
  1503. int skb_resp_len;
  1504. int rc;
  1505. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1506. /* This is a zero payload size skb */
  1507. skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE,
  1508. GFP_KERNEL);
  1509. if (skb_cmd == NULL)
  1510. goto error_cmd;
  1511. skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
  1512. rc = pn533_build_tx_frame(dev, skb_cmd, true);
  1513. if (rc)
  1514. goto error_frame;
  1515. skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
  1516. PN533_CMD_DATAEXCH_DATA_MAXLEN +
  1517. PN533_FRAME_TAIL_SIZE;
  1518. skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL);
  1519. if (!skb_resp) {
  1520. rc = -ENOMEM;
  1521. goto error_frame;
  1522. }
  1523. in_frame = (struct pn533_frame *) skb_resp->data;
  1524. out_frame = (struct pn533_frame *) skb_cmd->data;
  1525. arg->skb_resp = skb_resp;
  1526. arg->skb_out = skb_cmd;
  1527. rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame,
  1528. skb_resp_len,
  1529. pn533_data_exchange_complete,
  1530. dev->cmd_complete_arg, GFP_KERNEL);
  1531. if (!rc)
  1532. return;
  1533. nfc_dev_err(&dev->interface->dev, "Error %d when trying to"
  1534. " perform data_exchange", rc);
  1535. kfree_skb(skb_resp);
  1536. error_frame:
  1537. kfree_skb(skb_cmd);
  1538. error_cmd:
  1539. pn533_send_ack(dev, GFP_KERNEL);
  1540. kfree(arg);
  1541. up(&dev->cmd_lock);
  1542. }
  1543. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1544. u8 cfgdata_len)
  1545. {
  1546. int rc;
  1547. u8 *params;
  1548. nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
  1549. pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION);
  1550. params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame);
  1551. params[0] = cfgitem;
  1552. memcpy(&params[1], cfgdata, cfgdata_len);
  1553. dev->out_frame->datalen += (1 + cfgdata_len);
  1554. pn533_tx_frame_finish(dev->out_frame);
  1555. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1556. dev->in_maxlen);
  1557. return rc;
  1558. }
  1559. struct nfc_ops pn533_nfc_ops = {
  1560. .dev_up = NULL,
  1561. .dev_down = NULL,
  1562. .dep_link_up = pn533_dep_link_up,
  1563. .dep_link_down = pn533_dep_link_down,
  1564. .start_poll = pn533_start_poll,
  1565. .stop_poll = pn533_stop_poll,
  1566. .activate_target = pn533_activate_target,
  1567. .deactivate_target = pn533_deactivate_target,
  1568. .im_transceive = pn533_transceive,
  1569. .tm_send = pn533_tm_send,
  1570. };
  1571. static int pn533_probe(struct usb_interface *interface,
  1572. const struct usb_device_id *id)
  1573. {
  1574. struct pn533_fw_version *fw_ver;
  1575. struct pn533 *dev;
  1576. struct usb_host_interface *iface_desc;
  1577. struct usb_endpoint_descriptor *endpoint;
  1578. struct pn533_config_max_retries max_retries;
  1579. int in_endpoint = 0;
  1580. int out_endpoint = 0;
  1581. int rc = -ENOMEM;
  1582. int i;
  1583. u32 protocols;
  1584. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1585. if (!dev)
  1586. return -ENOMEM;
  1587. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  1588. dev->interface = interface;
  1589. sema_init(&dev->cmd_lock, 1);
  1590. iface_desc = interface->cur_altsetting;
  1591. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1592. endpoint = &iface_desc->endpoint[i].desc;
  1593. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1594. dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
  1595. in_endpoint = endpoint->bEndpointAddress;
  1596. }
  1597. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) {
  1598. dev->out_maxlen =
  1599. le16_to_cpu(endpoint->wMaxPacketSize);
  1600. out_endpoint = endpoint->bEndpointAddress;
  1601. }
  1602. }
  1603. if (!in_endpoint || !out_endpoint) {
  1604. nfc_dev_err(&interface->dev, "Could not find bulk-in or"
  1605. " bulk-out endpoint");
  1606. rc = -ENODEV;
  1607. goto error;
  1608. }
  1609. dev->in_frame = kmalloc(dev->in_maxlen, GFP_KERNEL);
  1610. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1611. dev->out_frame = kmalloc(dev->out_maxlen, GFP_KERNEL);
  1612. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1613. if (!dev->in_frame || !dev->out_frame ||
  1614. !dev->in_urb || !dev->out_urb)
  1615. goto error;
  1616. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  1617. usb_rcvbulkpipe(dev->udev, in_endpoint),
  1618. NULL, 0, NULL, dev);
  1619. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  1620. usb_sndbulkpipe(dev->udev, out_endpoint),
  1621. NULL, 0,
  1622. pn533_send_complete, dev);
  1623. INIT_WORK(&dev->cmd_work, pn533_wq_cmd_complete);
  1624. INIT_WORK(&dev->mi_work, pn533_wq_mi_recv);
  1625. INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
  1626. dev->wq = alloc_workqueue("pn533",
  1627. WQ_NON_REENTRANT | WQ_UNBOUND | WQ_MEM_RECLAIM,
  1628. 1);
  1629. if (dev->wq == NULL)
  1630. goto error;
  1631. skb_queue_head_init(&dev->resp_q);
  1632. usb_set_intfdata(interface, dev);
  1633. pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION);
  1634. pn533_tx_frame_finish(dev->out_frame);
  1635. rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
  1636. dev->in_maxlen);
  1637. if (rc)
  1638. goto destroy_wq;
  1639. fw_ver = (struct pn533_fw_version *)
  1640. PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
  1641. nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
  1642. " attached", fw_ver->ver, fw_ver->rev);
  1643. protocols = NFC_PROTO_JEWEL_MASK
  1644. | NFC_PROTO_MIFARE_MASK | NFC_PROTO_FELICA_MASK
  1645. | NFC_PROTO_ISO14443_MASK
  1646. | NFC_PROTO_NFC_DEP_MASK;
  1647. dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  1648. PN533_CMD_DATAEXCH_HEAD_LEN,
  1649. PN533_FRAME_TAIL_SIZE);
  1650. if (!dev->nfc_dev)
  1651. goto destroy_wq;
  1652. nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
  1653. nfc_set_drvdata(dev->nfc_dev, dev);
  1654. rc = nfc_register_device(dev->nfc_dev);
  1655. if (rc)
  1656. goto free_nfc_dev;
  1657. max_retries.mx_rty_atr = PN533_CONFIG_MAX_RETRIES_ENDLESS;
  1658. max_retries.mx_rty_psl = 2;
  1659. max_retries.mx_rty_passive_act = PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1660. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1661. (u8 *) &max_retries, sizeof(max_retries));
  1662. if (rc) {
  1663. nfc_dev_err(&dev->interface->dev, "Error on setting MAX_RETRIES"
  1664. " config");
  1665. goto unregister_nfc_dev;
  1666. }
  1667. return 0;
  1668. unregister_nfc_dev:
  1669. nfc_unregister_device(dev->nfc_dev);
  1670. free_nfc_dev:
  1671. nfc_free_device(dev->nfc_dev);
  1672. destroy_wq:
  1673. destroy_workqueue(dev->wq);
  1674. error:
  1675. kfree(dev->in_frame);
  1676. usb_free_urb(dev->in_urb);
  1677. kfree(dev->out_frame);
  1678. usb_free_urb(dev->out_urb);
  1679. kfree(dev);
  1680. return rc;
  1681. }
  1682. static void pn533_disconnect(struct usb_interface *interface)
  1683. {
  1684. struct pn533 *dev;
  1685. dev = usb_get_intfdata(interface);
  1686. usb_set_intfdata(interface, NULL);
  1687. nfc_unregister_device(dev->nfc_dev);
  1688. nfc_free_device(dev->nfc_dev);
  1689. usb_kill_urb(dev->in_urb);
  1690. usb_kill_urb(dev->out_urb);
  1691. destroy_workqueue(dev->wq);
  1692. skb_queue_purge(&dev->resp_q);
  1693. kfree(dev->in_frame);
  1694. usb_free_urb(dev->in_urb);
  1695. kfree(dev->out_frame);
  1696. usb_free_urb(dev->out_urb);
  1697. kfree(dev);
  1698. nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
  1699. }
  1700. static struct usb_driver pn533_driver = {
  1701. .name = "pn533",
  1702. .probe = pn533_probe,
  1703. .disconnect = pn533_disconnect,
  1704. .id_table = pn533_table,
  1705. };
  1706. module_usb_driver(pn533_driver);
  1707. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>,"
  1708. " Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  1709. MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
  1710. MODULE_VERSION(VERSION);
  1711. MODULE_LICENSE("GPL");