pn533.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  1. /*
  2. * Driver for NXP PN533 NFC Chip - core functions
  3. *
  4. * Copyright (C) 2011 Instituto Nokia de Tecnologia
  5. * Copyright (C) 2012-2013 Tieto Poland
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/device.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/nfc.h>
  25. #include <linux/netdevice.h>
  26. #include <net/nfc/nfc.h>
  27. #include "pn533.h"
  28. #define VERSION "0.3"
  29. /* How much time we spend listening for initiators */
  30. #define PN533_LISTEN_TIME 2
  31. /* Delay between each poll frame (ms) */
  32. #define PN533_POLL_INTERVAL 10
  33. /* structs for pn533 commands */
  34. /* PN533_CMD_GET_FIRMWARE_VERSION */
  35. struct pn533_fw_version {
  36. u8 ic;
  37. u8 ver;
  38. u8 rev;
  39. u8 support;
  40. };
  41. /* PN533_CMD_RF_CONFIGURATION */
  42. #define PN533_CFGITEM_RF_FIELD 0x01
  43. #define PN533_CFGITEM_TIMING 0x02
  44. #define PN533_CFGITEM_MAX_RETRIES 0x05
  45. #define PN533_CFGITEM_PASORI 0x82
  46. #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
  47. #define PN533_CFGITEM_RF_FIELD_ON 0x1
  48. #define PN533_CFGITEM_RF_FIELD_OFF 0x0
  49. #define PN533_CONFIG_TIMING_102 0xb
  50. #define PN533_CONFIG_TIMING_204 0xc
  51. #define PN533_CONFIG_TIMING_409 0xd
  52. #define PN533_CONFIG_TIMING_819 0xe
  53. #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
  54. #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
  55. struct pn533_config_max_retries {
  56. u8 mx_rty_atr;
  57. u8 mx_rty_psl;
  58. u8 mx_rty_passive_act;
  59. } __packed;
  60. struct pn533_config_timing {
  61. u8 rfu;
  62. u8 atr_res_timeout;
  63. u8 dep_timeout;
  64. } __packed;
  65. /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
  66. /* felica commands opcode */
  67. #define PN533_FELICA_OPC_SENSF_REQ 0
  68. #define PN533_FELICA_OPC_SENSF_RES 1
  69. /* felica SENSF_REQ parameters */
  70. #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
  71. #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
  72. #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
  73. #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
  74. /* type B initiator_data values */
  75. #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
  76. #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
  77. #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
  78. union pn533_cmd_poll_initdata {
  79. struct {
  80. u8 afi;
  81. u8 polling_method;
  82. } __packed type_b;
  83. struct {
  84. u8 opcode;
  85. __be16 sc;
  86. u8 rc;
  87. u8 tsn;
  88. } __packed felica;
  89. };
  90. struct pn533_poll_modulations {
  91. struct {
  92. u8 maxtg;
  93. u8 brty;
  94. union pn533_cmd_poll_initdata initiator_data;
  95. } __packed data;
  96. u8 len;
  97. };
  98. static const struct pn533_poll_modulations poll_mod[] = {
  99. [PN533_POLL_MOD_106KBPS_A] = {
  100. .data = {
  101. .maxtg = 1,
  102. .brty = 0,
  103. },
  104. .len = 2,
  105. },
  106. [PN533_POLL_MOD_212KBPS_FELICA] = {
  107. .data = {
  108. .maxtg = 1,
  109. .brty = 1,
  110. .initiator_data.felica = {
  111. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  112. .sc = PN533_FELICA_SENSF_SC_ALL,
  113. .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
  114. .tsn = 0x03,
  115. },
  116. },
  117. .len = 7,
  118. },
  119. [PN533_POLL_MOD_424KBPS_FELICA] = {
  120. .data = {
  121. .maxtg = 1,
  122. .brty = 2,
  123. .initiator_data.felica = {
  124. .opcode = PN533_FELICA_OPC_SENSF_REQ,
  125. .sc = PN533_FELICA_SENSF_SC_ALL,
  126. .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
  127. .tsn = 0x03,
  128. },
  129. },
  130. .len = 7,
  131. },
  132. [PN533_POLL_MOD_106KBPS_JEWEL] = {
  133. .data = {
  134. .maxtg = 1,
  135. .brty = 4,
  136. },
  137. .len = 2,
  138. },
  139. [PN533_POLL_MOD_847KBPS_B] = {
  140. .data = {
  141. .maxtg = 1,
  142. .brty = 8,
  143. .initiator_data.type_b = {
  144. .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
  145. .polling_method =
  146. PN533_TYPE_B_POLL_METHOD_TIMESLOT,
  147. },
  148. },
  149. .len = 3,
  150. },
  151. [PN533_LISTEN_MOD] = {
  152. .len = 0,
  153. },
  154. };
  155. /* PN533_CMD_IN_ATR */
  156. struct pn533_cmd_activate_response {
  157. u8 status;
  158. u8 nfcid3t[10];
  159. u8 didt;
  160. u8 bst;
  161. u8 brt;
  162. u8 to;
  163. u8 ppt;
  164. /* optional */
  165. u8 gt[];
  166. } __packed;
  167. struct pn533_cmd_jump_dep_response {
  168. u8 status;
  169. u8 tg;
  170. u8 nfcid3t[10];
  171. u8 didt;
  172. u8 bst;
  173. u8 brt;
  174. u8 to;
  175. u8 ppt;
  176. /* optional */
  177. u8 gt[];
  178. } __packed;
  179. /* PN533_TG_INIT_AS_TARGET */
  180. #define PN533_INIT_TARGET_PASSIVE 0x1
  181. #define PN533_INIT_TARGET_DEP 0x2
  182. #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
  183. #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
  184. #define PN533_INIT_TARGET_RESP_DEP 0x4
  185. /* The rule: value(high byte) + value(low byte) + checksum = 0 */
  186. static inline u8 pn533_ext_checksum(u16 value)
  187. {
  188. return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
  189. }
  190. /* The rule: value + checksum = 0 */
  191. static inline u8 pn533_std_checksum(u8 value)
  192. {
  193. return ~value + 1;
  194. }
  195. /* The rule: sum(data elements) + checksum = 0 */
  196. static u8 pn533_std_data_checksum(u8 *data, int datalen)
  197. {
  198. u8 sum = 0;
  199. int i;
  200. for (i = 0; i < datalen; i++)
  201. sum += data[i];
  202. return pn533_std_checksum(sum);
  203. }
  204. static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
  205. {
  206. struct pn533_std_frame *frame = _frame;
  207. frame->preamble = 0;
  208. frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
  209. PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
  210. PN533_FRAME_CMD(frame) = cmd_code;
  211. frame->datalen = 2;
  212. }
  213. static void pn533_std_tx_frame_finish(void *_frame)
  214. {
  215. struct pn533_std_frame *frame = _frame;
  216. frame->datalen_checksum = pn533_std_checksum(frame->datalen);
  217. PN533_STD_FRAME_CHECKSUM(frame) =
  218. pn533_std_data_checksum(frame->data, frame->datalen);
  219. PN533_STD_FRAME_POSTAMBLE(frame) = 0;
  220. }
  221. static void pn533_std_tx_update_payload_len(void *_frame, int len)
  222. {
  223. struct pn533_std_frame *frame = _frame;
  224. frame->datalen += len;
  225. }
  226. static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
  227. {
  228. u8 checksum;
  229. struct pn533_std_frame *stdf = _frame;
  230. if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
  231. return false;
  232. if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
  233. /* Standard frame code */
  234. dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
  235. checksum = pn533_std_checksum(stdf->datalen);
  236. if (checksum != stdf->datalen_checksum)
  237. return false;
  238. checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
  239. if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
  240. return false;
  241. } else {
  242. /* Extended */
  243. struct pn533_ext_frame *eif = _frame;
  244. dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
  245. checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
  246. if (checksum != eif->datalen_checksum)
  247. return false;
  248. /* check data checksum */
  249. checksum = pn533_std_data_checksum(eif->data,
  250. be16_to_cpu(eif->datalen));
  251. if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
  252. return false;
  253. }
  254. return true;
  255. }
  256. bool pn533_rx_frame_is_ack(void *_frame)
  257. {
  258. struct pn533_std_frame *frame = _frame;
  259. if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
  260. return false;
  261. if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
  262. return false;
  263. return true;
  264. }
  265. EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
  266. static inline int pn533_std_rx_frame_size(void *frame)
  267. {
  268. struct pn533_std_frame *f = frame;
  269. /* check for Extended Information frame */
  270. if (PN533_STD_IS_EXTENDED(f)) {
  271. struct pn533_ext_frame *eif = frame;
  272. return sizeof(struct pn533_ext_frame)
  273. + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
  274. }
  275. return sizeof(struct pn533_std_frame) + f->datalen +
  276. PN533_STD_FRAME_TAIL_LEN;
  277. }
  278. static u8 pn533_std_get_cmd_code(void *frame)
  279. {
  280. struct pn533_std_frame *f = frame;
  281. struct pn533_ext_frame *eif = frame;
  282. if (PN533_STD_IS_EXTENDED(f))
  283. return PN533_FRAME_CMD(eif);
  284. else
  285. return PN533_FRAME_CMD(f);
  286. }
  287. bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
  288. {
  289. return (dev->ops->get_cmd_code(frame) ==
  290. PN533_CMD_RESPONSE(dev->cmd->code));
  291. }
  292. EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response);
  293. static struct pn533_frame_ops pn533_std_frame_ops = {
  294. .tx_frame_init = pn533_std_tx_frame_init,
  295. .tx_frame_finish = pn533_std_tx_frame_finish,
  296. .tx_update_payload_len = pn533_std_tx_update_payload_len,
  297. .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
  298. .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
  299. .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
  300. .rx_frame_size = pn533_std_rx_frame_size,
  301. .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
  302. .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
  303. .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
  304. .get_cmd_code = pn533_std_get_cmd_code,
  305. };
  306. static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
  307. struct sk_buff *skb)
  308. {
  309. /* payload is already there, just update datalen */
  310. int payload_len = skb->len;
  311. struct pn533_frame_ops *ops = dev->ops;
  312. skb_push(skb, ops->tx_header_len);
  313. skb_put(skb, ops->tx_tail_len);
  314. ops->tx_frame_init(skb->data, cmd_code);
  315. ops->tx_update_payload_len(skb->data, payload_len);
  316. ops->tx_frame_finish(skb->data);
  317. }
  318. static int pn533_send_async_complete(struct pn533 *dev)
  319. {
  320. struct pn533_cmd *cmd = dev->cmd;
  321. int status = cmd->status;
  322. struct sk_buff *req = cmd->req;
  323. struct sk_buff *resp = cmd->resp;
  324. int rc;
  325. dev_kfree_skb(req);
  326. if (status < 0) {
  327. rc = cmd->complete_cb(dev, cmd->complete_cb_context,
  328. ERR_PTR(status));
  329. dev_kfree_skb(resp);
  330. goto done;
  331. }
  332. skb_pull(resp, dev->ops->rx_header_len);
  333. skb_trim(resp, resp->len - dev->ops->rx_tail_len);
  334. rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
  335. done:
  336. kfree(cmd);
  337. dev->cmd = NULL;
  338. return rc;
  339. }
  340. static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
  341. struct sk_buff *req,
  342. pn533_send_async_complete_t complete_cb,
  343. void *complete_cb_context)
  344. {
  345. struct pn533_cmd *cmd;
  346. int rc = 0;
  347. dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
  348. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  349. if (!cmd)
  350. return -ENOMEM;
  351. cmd->code = cmd_code;
  352. cmd->req = req;
  353. cmd->complete_cb = complete_cb;
  354. cmd->complete_cb_context = complete_cb_context;
  355. pn533_build_cmd_frame(dev, cmd_code, req);
  356. mutex_lock(&dev->cmd_lock);
  357. if (!dev->cmd_pending) {
  358. rc = dev->phy_ops->send_frame(dev, req);
  359. if (rc)
  360. goto error;
  361. dev->cmd_pending = 1;
  362. dev->cmd = cmd;
  363. goto unlock;
  364. }
  365. dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
  366. __func__, cmd_code);
  367. INIT_LIST_HEAD(&cmd->queue);
  368. list_add_tail(&cmd->queue, &dev->cmd_queue);
  369. goto unlock;
  370. error:
  371. kfree(cmd);
  372. unlock:
  373. mutex_unlock(&dev->cmd_lock);
  374. return rc;
  375. }
  376. static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
  377. struct sk_buff *req,
  378. pn533_send_async_complete_t complete_cb,
  379. void *complete_cb_context)
  380. {
  381. int rc;
  382. rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
  383. complete_cb_context);
  384. return rc;
  385. }
  386. static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
  387. struct sk_buff *req,
  388. pn533_send_async_complete_t complete_cb,
  389. void *complete_cb_context)
  390. {
  391. int rc;
  392. rc = __pn533_send_async(dev, cmd_code, req, complete_cb,
  393. complete_cb_context);
  394. return rc;
  395. }
  396. /*
  397. * pn533_send_cmd_direct_async
  398. *
  399. * The function sends a piority cmd directly to the chip omiting the cmd
  400. * queue. It's intended to be used by chaining mechanism of received responses
  401. * where the host has to request every single chunk of data before scheduling
  402. * next cmd from the queue.
  403. */
  404. static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
  405. struct sk_buff *req,
  406. pn533_send_async_complete_t complete_cb,
  407. void *complete_cb_context)
  408. {
  409. struct pn533_cmd *cmd;
  410. int rc;
  411. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  412. if (!cmd)
  413. return -ENOMEM;
  414. cmd->code = cmd_code;
  415. cmd->req = req;
  416. cmd->complete_cb = complete_cb;
  417. cmd->complete_cb_context = complete_cb_context;
  418. pn533_build_cmd_frame(dev, cmd_code, req);
  419. rc = dev->phy_ops->send_frame(dev, req);
  420. if (rc < 0)
  421. kfree(cmd);
  422. else
  423. dev->cmd = cmd;
  424. return rc;
  425. }
  426. static void pn533_wq_cmd_complete(struct work_struct *work)
  427. {
  428. struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
  429. int rc;
  430. rc = pn533_send_async_complete(dev);
  431. if (rc != -EINPROGRESS)
  432. queue_work(dev->wq, &dev->cmd_work);
  433. }
  434. static void pn533_wq_cmd(struct work_struct *work)
  435. {
  436. struct pn533 *dev = container_of(work, struct pn533, cmd_work);
  437. struct pn533_cmd *cmd;
  438. int rc;
  439. mutex_lock(&dev->cmd_lock);
  440. if (list_empty(&dev->cmd_queue)) {
  441. dev->cmd_pending = 0;
  442. mutex_unlock(&dev->cmd_lock);
  443. return;
  444. }
  445. cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
  446. list_del(&cmd->queue);
  447. mutex_unlock(&dev->cmd_lock);
  448. rc = dev->phy_ops->send_frame(dev, cmd->req);
  449. if (rc < 0) {
  450. dev_kfree_skb(cmd->req);
  451. kfree(cmd);
  452. return;
  453. }
  454. dev->cmd = cmd;
  455. }
  456. struct pn533_sync_cmd_response {
  457. struct sk_buff *resp;
  458. struct completion done;
  459. };
  460. static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
  461. struct sk_buff *resp)
  462. {
  463. struct pn533_sync_cmd_response *arg = _arg;
  464. arg->resp = resp;
  465. complete(&arg->done);
  466. return 0;
  467. }
  468. /* pn533_send_cmd_sync
  469. *
  470. * Please note the req parameter is freed inside the function to
  471. * limit a number of return value interpretations by the caller.
  472. *
  473. * 1. negative in case of error during TX path -> req should be freed
  474. *
  475. * 2. negative in case of error during RX path -> req should not be freed
  476. * as it's been already freed at the beginning of RX path by
  477. * async_complete_cb.
  478. *
  479. * 3. valid pointer in case of succesfult RX path
  480. *
  481. * A caller has to check a return value with IS_ERR macro. If the test pass,
  482. * the returned pointer is valid.
  483. *
  484. */
  485. static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
  486. struct sk_buff *req)
  487. {
  488. int rc;
  489. struct pn533_sync_cmd_response arg;
  490. init_completion(&arg.done);
  491. rc = pn533_send_cmd_async(dev, cmd_code, req,
  492. pn533_send_sync_complete, &arg);
  493. if (rc) {
  494. dev_kfree_skb(req);
  495. return ERR_PTR(rc);
  496. }
  497. wait_for_completion(&arg.done);
  498. return arg.resp;
  499. }
  500. static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
  501. {
  502. struct sk_buff *skb;
  503. skb = alloc_skb(dev->ops->tx_header_len +
  504. size +
  505. dev->ops->tx_tail_len, GFP_KERNEL);
  506. if (skb)
  507. skb_reserve(skb, dev->ops->tx_header_len);
  508. return skb;
  509. }
  510. struct pn533_target_type_a {
  511. __be16 sens_res;
  512. u8 sel_res;
  513. u8 nfcid_len;
  514. u8 nfcid_data[];
  515. } __packed;
  516. #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
  517. #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
  518. #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
  519. #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
  520. #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
  521. #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
  522. #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
  523. #define PN533_TYPE_A_SEL_PROT_MIFARE 0
  524. #define PN533_TYPE_A_SEL_PROT_ISO14443 1
  525. #define PN533_TYPE_A_SEL_PROT_DEP 2
  526. #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
  527. static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
  528. int target_data_len)
  529. {
  530. u8 ssd;
  531. u8 platconf;
  532. if (target_data_len < sizeof(struct pn533_target_type_a))
  533. return false;
  534. /*
  535. * The length check of nfcid[] and ats[] are not being performed because
  536. * the values are not being used
  537. */
  538. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  539. ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
  540. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
  541. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  542. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  543. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  544. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  545. return false;
  546. /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
  547. if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
  548. return false;
  549. return true;
  550. }
  551. static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
  552. int tgt_data_len)
  553. {
  554. struct pn533_target_type_a *tgt_type_a;
  555. tgt_type_a = (struct pn533_target_type_a *)tgt_data;
  556. if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
  557. return -EPROTO;
  558. switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
  559. case PN533_TYPE_A_SEL_PROT_MIFARE:
  560. nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
  561. break;
  562. case PN533_TYPE_A_SEL_PROT_ISO14443:
  563. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
  564. break;
  565. case PN533_TYPE_A_SEL_PROT_DEP:
  566. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  567. break;
  568. case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
  569. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
  570. NFC_PROTO_NFC_DEP_MASK;
  571. break;
  572. }
  573. nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
  574. nfc_tgt->sel_res = tgt_type_a->sel_res;
  575. nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
  576. memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
  577. return 0;
  578. }
  579. struct pn533_target_felica {
  580. u8 pol_res;
  581. u8 opcode;
  582. u8 nfcid2[NFC_NFCID2_MAXSIZE];
  583. u8 pad[8];
  584. /* optional */
  585. u8 syst_code[];
  586. } __packed;
  587. #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
  588. #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
  589. static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
  590. int target_data_len)
  591. {
  592. if (target_data_len < sizeof(struct pn533_target_felica))
  593. return false;
  594. if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
  595. return false;
  596. return true;
  597. }
  598. static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
  599. int tgt_data_len)
  600. {
  601. struct pn533_target_felica *tgt_felica;
  602. tgt_felica = (struct pn533_target_felica *)tgt_data;
  603. if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
  604. return -EPROTO;
  605. if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
  606. (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
  607. nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  608. else
  609. nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
  610. memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
  611. nfc_tgt->sensf_res_len = 9;
  612. memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
  613. nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
  614. return 0;
  615. }
  616. struct pn533_target_jewel {
  617. __be16 sens_res;
  618. u8 jewelid[4];
  619. } __packed;
  620. static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
  621. int target_data_len)
  622. {
  623. u8 ssd;
  624. u8 platconf;
  625. if (target_data_len < sizeof(struct pn533_target_jewel))
  626. return false;
  627. /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
  628. ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
  629. platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
  630. if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  631. platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
  632. (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
  633. platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
  634. return false;
  635. return true;
  636. }
  637. static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
  638. int tgt_data_len)
  639. {
  640. struct pn533_target_jewel *tgt_jewel;
  641. tgt_jewel = (struct pn533_target_jewel *)tgt_data;
  642. if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
  643. return -EPROTO;
  644. nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
  645. nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
  646. nfc_tgt->nfcid1_len = 4;
  647. memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
  648. return 0;
  649. }
  650. struct pn533_type_b_prot_info {
  651. u8 bitrate;
  652. u8 fsci_type;
  653. u8 fwi_adc_fo;
  654. } __packed;
  655. #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
  656. #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
  657. #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
  658. struct pn533_type_b_sens_res {
  659. u8 opcode;
  660. u8 nfcid[4];
  661. u8 appdata[4];
  662. struct pn533_type_b_prot_info prot_info;
  663. } __packed;
  664. #define PN533_TYPE_B_OPC_SENSB_RES 0x50
  665. struct pn533_target_type_b {
  666. struct pn533_type_b_sens_res sensb_res;
  667. u8 attrib_res_len;
  668. u8 attrib_res[];
  669. } __packed;
  670. static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
  671. int target_data_len)
  672. {
  673. if (target_data_len < sizeof(struct pn533_target_type_b))
  674. return false;
  675. if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
  676. return false;
  677. if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
  678. PN533_TYPE_B_PROT_TYPE_RFU_MASK)
  679. return false;
  680. return true;
  681. }
  682. static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
  683. int tgt_data_len)
  684. {
  685. struct pn533_target_type_b *tgt_type_b;
  686. tgt_type_b = (struct pn533_target_type_b *)tgt_data;
  687. if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
  688. return -EPROTO;
  689. nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
  690. return 0;
  691. }
  692. static void pn533_poll_reset_mod_list(struct pn533 *dev);
  693. static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
  694. int tgdata_len)
  695. {
  696. struct nfc_target nfc_tgt;
  697. int rc;
  698. dev_dbg(dev->dev, "%s: modulation=%d\n",
  699. __func__, dev->poll_mod_curr);
  700. if (tg != 1)
  701. return -EPROTO;
  702. memset(&nfc_tgt, 0, sizeof(struct nfc_target));
  703. switch (dev->poll_mod_curr) {
  704. case PN533_POLL_MOD_106KBPS_A:
  705. rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
  706. break;
  707. case PN533_POLL_MOD_212KBPS_FELICA:
  708. case PN533_POLL_MOD_424KBPS_FELICA:
  709. rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
  710. break;
  711. case PN533_POLL_MOD_106KBPS_JEWEL:
  712. rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
  713. break;
  714. case PN533_POLL_MOD_847KBPS_B:
  715. rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
  716. break;
  717. default:
  718. nfc_err(dev->dev,
  719. "Unknown current poll modulation\n");
  720. return -EPROTO;
  721. }
  722. if (rc)
  723. return rc;
  724. if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
  725. dev_dbg(dev->dev,
  726. "The Tg found doesn't have the desired protocol\n");
  727. return -EAGAIN;
  728. }
  729. dev_dbg(dev->dev,
  730. "Target found - supported protocols: 0x%x\n",
  731. nfc_tgt.supported_protocols);
  732. dev->tgt_available_prots = nfc_tgt.supported_protocols;
  733. pn533_poll_reset_mod_list(dev);
  734. nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
  735. return 0;
  736. }
  737. static inline void pn533_poll_next_mod(struct pn533 *dev)
  738. {
  739. dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
  740. }
  741. static void pn533_poll_reset_mod_list(struct pn533 *dev)
  742. {
  743. dev->poll_mod_count = 0;
  744. }
  745. static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
  746. {
  747. dev->poll_mod_active[dev->poll_mod_count] =
  748. (struct pn533_poll_modulations *)&poll_mod[mod_index];
  749. dev->poll_mod_count++;
  750. }
  751. static void pn533_poll_create_mod_list(struct pn533 *dev,
  752. u32 im_protocols, u32 tm_protocols)
  753. {
  754. pn533_poll_reset_mod_list(dev);
  755. if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
  756. (im_protocols & NFC_PROTO_ISO14443_MASK) ||
  757. (im_protocols & NFC_PROTO_NFC_DEP_MASK))
  758. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
  759. if (im_protocols & NFC_PROTO_FELICA_MASK ||
  760. im_protocols & NFC_PROTO_NFC_DEP_MASK) {
  761. pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
  762. pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
  763. }
  764. if (im_protocols & NFC_PROTO_JEWEL_MASK)
  765. pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
  766. if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
  767. pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
  768. if (tm_protocols)
  769. pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
  770. }
  771. static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
  772. {
  773. u8 nbtg, tg, *tgdata;
  774. int rc, tgdata_len;
  775. /* Toggle the DEP polling */
  776. if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
  777. dev->poll_dep = 1;
  778. nbtg = resp->data[0];
  779. tg = resp->data[1];
  780. tgdata = &resp->data[2];
  781. tgdata_len = resp->len - 2; /* nbtg + tg */
  782. if (nbtg) {
  783. rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
  784. /* We must stop the poll after a valid target found */
  785. if (rc == 0)
  786. return 0;
  787. }
  788. return -EAGAIN;
  789. }
  790. static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
  791. {
  792. struct sk_buff *skb;
  793. u8 *felica, *nfcid3, *gb;
  794. u8 *gbytes = dev->gb;
  795. size_t gbytes_len = dev->gb_len;
  796. u8 felica_params[18] = {0x1, 0xfe, /* DEP */
  797. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
  798. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  799. 0xff, 0xff}; /* System code */
  800. u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
  801. 0x0, 0x0, 0x0,
  802. 0x40}; /* SEL_RES for DEP */
  803. unsigned int skb_len = 36 + /*
  804. * mode (1), mifare (6),
  805. * felica (18), nfcid3 (10), gb_len (1)
  806. */
  807. gbytes_len +
  808. 1; /* len Tk*/
  809. skb = pn533_alloc_skb(dev, skb_len);
  810. if (!skb)
  811. return NULL;
  812. /* DEP support only */
  813. *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
  814. /* MIFARE params */
  815. memcpy(skb_put(skb, 6), mifare_params, 6);
  816. /* Felica params */
  817. felica = skb_put(skb, 18);
  818. memcpy(felica, felica_params, 18);
  819. get_random_bytes(felica + 2, 6);
  820. /* NFCID3 */
  821. nfcid3 = skb_put(skb, 10);
  822. memset(nfcid3, 0, 10);
  823. memcpy(nfcid3, felica, 8);
  824. /* General bytes */
  825. *skb_put(skb, 1) = gbytes_len;
  826. gb = skb_put(skb, gbytes_len);
  827. memcpy(gb, gbytes, gbytes_len);
  828. /* Len Tk */
  829. *skb_put(skb, 1) = 0;
  830. return skb;
  831. }
  832. static void pn533_wq_tm_mi_recv(struct work_struct *work);
  833. static struct sk_buff *pn533_build_response(struct pn533 *dev);
  834. static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
  835. struct sk_buff *resp)
  836. {
  837. struct sk_buff *skb;
  838. u8 status, ret, mi;
  839. int rc;
  840. dev_dbg(dev->dev, "%s\n", __func__);
  841. if (IS_ERR(resp)) {
  842. skb_queue_purge(&dev->resp_q);
  843. return PTR_ERR(resp);
  844. }
  845. status = resp->data[0];
  846. ret = status & PN533_CMD_RET_MASK;
  847. mi = status & PN533_CMD_MI_MASK;
  848. skb_pull(resp, sizeof(status));
  849. if (ret != PN533_CMD_RET_SUCCESS) {
  850. rc = -EIO;
  851. goto error;
  852. }
  853. skb_queue_tail(&dev->resp_q, resp);
  854. if (mi) {
  855. queue_work(dev->wq, &dev->mi_tm_rx_work);
  856. return -EINPROGRESS;
  857. }
  858. skb = pn533_build_response(dev);
  859. if (!skb) {
  860. rc = -EIO;
  861. goto error;
  862. }
  863. return nfc_tm_data_received(dev->nfc_dev, skb);
  864. error:
  865. nfc_tm_deactivated(dev->nfc_dev);
  866. dev->tgt_mode = 0;
  867. skb_queue_purge(&dev->resp_q);
  868. dev_kfree_skb(resp);
  869. return rc;
  870. }
  871. static void pn533_wq_tm_mi_recv(struct work_struct *work)
  872. {
  873. struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
  874. struct sk_buff *skb;
  875. int rc;
  876. dev_dbg(dev->dev, "%s\n", __func__);
  877. skb = pn533_alloc_skb(dev, 0);
  878. if (!skb)
  879. return;
  880. rc = pn533_send_cmd_direct_async(dev,
  881. PN533_CMD_TG_GET_DATA,
  882. skb,
  883. pn533_tm_get_data_complete,
  884. NULL);
  885. if (rc < 0)
  886. dev_kfree_skb(skb);
  887. }
  888. static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
  889. struct sk_buff *resp);
  890. static void pn533_wq_tm_mi_send(struct work_struct *work)
  891. {
  892. struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
  893. struct sk_buff *skb;
  894. int rc;
  895. dev_dbg(dev->dev, "%s\n", __func__);
  896. /* Grab the first skb in the queue */
  897. skb = skb_dequeue(&dev->fragment_skb);
  898. if (skb == NULL) { /* No more data */
  899. /* Reset the queue for future use */
  900. skb_queue_head_init(&dev->fragment_skb);
  901. goto error;
  902. }
  903. /* last entry - remove MI bit */
  904. if (skb_queue_len(&dev->fragment_skb) == 0) {
  905. rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
  906. skb, pn533_tm_send_complete, NULL);
  907. } else
  908. rc = pn533_send_cmd_direct_async(dev,
  909. PN533_CMD_TG_SET_META_DATA,
  910. skb, pn533_tm_send_complete, NULL);
  911. if (rc == 0) /* success */
  912. return;
  913. dev_err(dev->dev,
  914. "Error %d when trying to perform set meta data_exchange", rc);
  915. dev_kfree_skb(skb);
  916. error:
  917. dev->phy_ops->send_ack(dev, GFP_KERNEL);
  918. queue_work(dev->wq, &dev->cmd_work);
  919. }
  920. static void pn533_wq_tg_get_data(struct work_struct *work)
  921. {
  922. struct pn533 *dev = container_of(work, struct pn533, tg_work);
  923. struct sk_buff *skb;
  924. int rc;
  925. dev_dbg(dev->dev, "%s\n", __func__);
  926. skb = pn533_alloc_skb(dev, 0);
  927. if (!skb)
  928. return;
  929. rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
  930. pn533_tm_get_data_complete, NULL);
  931. if (rc < 0)
  932. dev_kfree_skb(skb);
  933. }
  934. #define ATR_REQ_GB_OFFSET 17
  935. static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
  936. {
  937. u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
  938. size_t gb_len;
  939. int rc;
  940. dev_dbg(dev->dev, "%s\n", __func__);
  941. if (resp->len < ATR_REQ_GB_OFFSET + 1)
  942. return -EINVAL;
  943. mode = resp->data[0];
  944. cmd = &resp->data[1];
  945. dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
  946. mode, resp->len);
  947. if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
  948. PN533_INIT_TARGET_RESP_ACTIVE)
  949. comm_mode = NFC_COMM_ACTIVE;
  950. if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
  951. return -EOPNOTSUPP;
  952. gb = cmd + ATR_REQ_GB_OFFSET;
  953. gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
  954. rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
  955. comm_mode, gb, gb_len);
  956. if (rc < 0) {
  957. nfc_err(dev->dev,
  958. "Error when signaling target activation\n");
  959. return rc;
  960. }
  961. dev->tgt_mode = 1;
  962. queue_work(dev->wq, &dev->tg_work);
  963. return 0;
  964. }
  965. static void pn533_listen_mode_timer(unsigned long data)
  966. {
  967. struct pn533 *dev = (struct pn533 *)data;
  968. dev_dbg(dev->dev, "Listen mode timeout\n");
  969. dev->cancel_listen = 1;
  970. pn533_poll_next_mod(dev);
  971. queue_delayed_work(dev->wq, &dev->poll_work,
  972. msecs_to_jiffies(PN533_POLL_INTERVAL));
  973. }
  974. static int pn533_rf_complete(struct pn533 *dev, void *arg,
  975. struct sk_buff *resp)
  976. {
  977. int rc = 0;
  978. dev_dbg(dev->dev, "%s\n", __func__);
  979. if (IS_ERR(resp)) {
  980. rc = PTR_ERR(resp);
  981. nfc_err(dev->dev, "RF setting error %d\n", rc);
  982. return rc;
  983. }
  984. queue_delayed_work(dev->wq, &dev->poll_work,
  985. msecs_to_jiffies(PN533_POLL_INTERVAL));
  986. dev_kfree_skb(resp);
  987. return rc;
  988. }
  989. static void pn533_wq_rf(struct work_struct *work)
  990. {
  991. struct pn533 *dev = container_of(work, struct pn533, rf_work);
  992. struct sk_buff *skb;
  993. int rc;
  994. dev_dbg(dev->dev, "%s\n", __func__);
  995. skb = pn533_alloc_skb(dev, 2);
  996. if (!skb)
  997. return;
  998. *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
  999. *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
  1000. rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
  1001. pn533_rf_complete, NULL);
  1002. if (rc < 0) {
  1003. dev_kfree_skb(skb);
  1004. nfc_err(dev->dev, "RF setting error %d\n", rc);
  1005. }
  1006. }
  1007. static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
  1008. struct sk_buff *resp)
  1009. {
  1010. struct pn533_cmd_jump_dep_response *rsp;
  1011. struct nfc_target nfc_target;
  1012. u8 target_gt_len;
  1013. int rc;
  1014. if (IS_ERR(resp))
  1015. return PTR_ERR(resp);
  1016. rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
  1017. rc = rsp->status & PN533_CMD_RET_MASK;
  1018. if (rc != PN533_CMD_RET_SUCCESS) {
  1019. /* Not target found, turn radio off */
  1020. queue_work(dev->wq, &dev->rf_work);
  1021. dev_kfree_skb(resp);
  1022. return 0;
  1023. }
  1024. dev_dbg(dev->dev, "Creating new target");
  1025. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1026. nfc_target.nfcid1_len = 10;
  1027. memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
  1028. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1029. if (rc)
  1030. goto error;
  1031. dev->tgt_available_prots = 0;
  1032. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1033. /* ATR_RES general bytes are located at offset 17 */
  1034. target_gt_len = resp->len - 17;
  1035. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1036. rsp->gt, target_gt_len);
  1037. if (!rc) {
  1038. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1039. dev->nfc_dev->targets[0].idx,
  1040. 0, NFC_RF_INITIATOR);
  1041. if (!rc)
  1042. pn533_poll_reset_mod_list(dev);
  1043. }
  1044. error:
  1045. dev_kfree_skb(resp);
  1046. return rc;
  1047. }
  1048. #define PASSIVE_DATA_LEN 5
  1049. static int pn533_poll_dep(struct nfc_dev *nfc_dev)
  1050. {
  1051. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1052. struct sk_buff *skb;
  1053. int rc, skb_len;
  1054. u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
  1055. u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
  1056. dev_dbg(dev->dev, "%s", __func__);
  1057. if (!dev->gb) {
  1058. dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
  1059. if (!dev->gb || !dev->gb_len) {
  1060. dev->poll_dep = 0;
  1061. queue_work(dev->wq, &dev->rf_work);
  1062. }
  1063. }
  1064. skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
  1065. skb_len += PASSIVE_DATA_LEN;
  1066. /* NFCID3 */
  1067. skb_len += NFC_NFCID3_MAXSIZE;
  1068. nfcid3[0] = 0x1;
  1069. nfcid3[1] = 0xfe;
  1070. get_random_bytes(nfcid3 + 2, 6);
  1071. skb = pn533_alloc_skb(dev, skb_len);
  1072. if (!skb)
  1073. return -ENOMEM;
  1074. *skb_put(skb, 1) = 0x01; /* Active */
  1075. *skb_put(skb, 1) = 0x02; /* 424 kbps */
  1076. next = skb_put(skb, 1); /* Next */
  1077. *next = 0;
  1078. /* Copy passive data */
  1079. memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
  1080. *next |= 1;
  1081. /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
  1082. memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
  1083. NFC_NFCID3_MAXSIZE);
  1084. *next |= 2;
  1085. memcpy(skb_put(skb, dev->gb_len), dev->gb, dev->gb_len);
  1086. *next |= 4; /* We have some Gi */
  1087. rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
  1088. pn533_poll_dep_complete, NULL);
  1089. if (rc < 0)
  1090. dev_kfree_skb(skb);
  1091. return rc;
  1092. }
  1093. static int pn533_poll_complete(struct pn533 *dev, void *arg,
  1094. struct sk_buff *resp)
  1095. {
  1096. struct pn533_poll_modulations *cur_mod;
  1097. int rc;
  1098. dev_dbg(dev->dev, "%s\n", __func__);
  1099. if (IS_ERR(resp)) {
  1100. rc = PTR_ERR(resp);
  1101. nfc_err(dev->dev, "%s Poll complete error %d\n",
  1102. __func__, rc);
  1103. if (rc == -ENOENT) {
  1104. if (dev->poll_mod_count != 0)
  1105. return rc;
  1106. goto stop_poll;
  1107. } else if (rc < 0) {
  1108. nfc_err(dev->dev,
  1109. "Error %d when running poll\n", rc);
  1110. goto stop_poll;
  1111. }
  1112. }
  1113. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1114. if (cur_mod->len == 0) { /* Target mode */
  1115. del_timer(&dev->listen_timer);
  1116. rc = pn533_init_target_complete(dev, resp);
  1117. goto done;
  1118. }
  1119. /* Initiator mode */
  1120. rc = pn533_start_poll_complete(dev, resp);
  1121. if (!rc)
  1122. goto done;
  1123. if (!dev->poll_mod_count) {
  1124. dev_dbg(dev->dev, "Polling has been stopped\n");
  1125. goto done;
  1126. }
  1127. pn533_poll_next_mod(dev);
  1128. /* Not target found, turn radio off */
  1129. queue_work(dev->wq, &dev->rf_work);
  1130. done:
  1131. dev_kfree_skb(resp);
  1132. return rc;
  1133. stop_poll:
  1134. nfc_err(dev->dev, "Polling operation has been stopped\n");
  1135. pn533_poll_reset_mod_list(dev);
  1136. dev->poll_protocols = 0;
  1137. return rc;
  1138. }
  1139. static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
  1140. struct pn533_poll_modulations *mod)
  1141. {
  1142. struct sk_buff *skb;
  1143. skb = pn533_alloc_skb(dev, mod->len);
  1144. if (!skb)
  1145. return NULL;
  1146. memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
  1147. return skb;
  1148. }
  1149. static int pn533_send_poll_frame(struct pn533 *dev)
  1150. {
  1151. struct pn533_poll_modulations *mod;
  1152. struct sk_buff *skb;
  1153. int rc;
  1154. u8 cmd_code;
  1155. mod = dev->poll_mod_active[dev->poll_mod_curr];
  1156. dev_dbg(dev->dev, "%s mod len %d\n",
  1157. __func__, mod->len);
  1158. if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep) {
  1159. dev->poll_dep = 0;
  1160. return pn533_poll_dep(dev->nfc_dev);
  1161. }
  1162. if (mod->len == 0) { /* Listen mode */
  1163. cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
  1164. skb = pn533_alloc_poll_tg_frame(dev);
  1165. } else { /* Polling mode */
  1166. cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
  1167. skb = pn533_alloc_poll_in_frame(dev, mod);
  1168. }
  1169. if (!skb) {
  1170. nfc_err(dev->dev, "Failed to allocate skb\n");
  1171. return -ENOMEM;
  1172. }
  1173. rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
  1174. NULL);
  1175. if (rc < 0) {
  1176. dev_kfree_skb(skb);
  1177. nfc_err(dev->dev, "Polling loop error %d\n", rc);
  1178. }
  1179. return rc;
  1180. }
  1181. static void pn533_wq_poll(struct work_struct *work)
  1182. {
  1183. struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
  1184. struct pn533_poll_modulations *cur_mod;
  1185. int rc;
  1186. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1187. dev_dbg(dev->dev,
  1188. "%s cancel_listen %d modulation len %d\n",
  1189. __func__, dev->cancel_listen, cur_mod->len);
  1190. if (dev->cancel_listen == 1) {
  1191. dev->cancel_listen = 0;
  1192. dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
  1193. }
  1194. rc = pn533_send_poll_frame(dev);
  1195. if (rc)
  1196. return;
  1197. if (cur_mod->len == 0 && dev->poll_mod_count > 1)
  1198. mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
  1199. }
  1200. static int pn533_start_poll(struct nfc_dev *nfc_dev,
  1201. u32 im_protocols, u32 tm_protocols)
  1202. {
  1203. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1204. struct pn533_poll_modulations *cur_mod;
  1205. u8 rand_mod;
  1206. int rc;
  1207. dev_dbg(dev->dev,
  1208. "%s: im protocols 0x%x tm protocols 0x%x\n",
  1209. __func__, im_protocols, tm_protocols);
  1210. if (dev->tgt_active_prot) {
  1211. nfc_err(dev->dev,
  1212. "Cannot poll with a target already activated\n");
  1213. return -EBUSY;
  1214. }
  1215. if (dev->tgt_mode) {
  1216. nfc_err(dev->dev,
  1217. "Cannot poll while already being activated\n");
  1218. return -EBUSY;
  1219. }
  1220. if (tm_protocols) {
  1221. dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
  1222. if (dev->gb == NULL)
  1223. tm_protocols = 0;
  1224. }
  1225. pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
  1226. dev->poll_protocols = im_protocols;
  1227. dev->listen_protocols = tm_protocols;
  1228. /* Do not always start polling from the same modulation */
  1229. get_random_bytes(&rand_mod, sizeof(rand_mod));
  1230. rand_mod %= dev->poll_mod_count;
  1231. dev->poll_mod_curr = rand_mod;
  1232. cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
  1233. rc = pn533_send_poll_frame(dev);
  1234. /* Start listen timer */
  1235. if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
  1236. mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
  1237. return rc;
  1238. }
  1239. static void pn533_stop_poll(struct nfc_dev *nfc_dev)
  1240. {
  1241. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1242. del_timer(&dev->listen_timer);
  1243. if (!dev->poll_mod_count) {
  1244. dev_dbg(dev->dev,
  1245. "Polling operation was not running\n");
  1246. return;
  1247. }
  1248. dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
  1249. flush_delayed_work(&dev->poll_work);
  1250. pn533_poll_reset_mod_list(dev);
  1251. }
  1252. static int pn533_activate_target_nfcdep(struct pn533 *dev)
  1253. {
  1254. struct pn533_cmd_activate_response *rsp;
  1255. u16 gt_len;
  1256. int rc;
  1257. struct sk_buff *skb;
  1258. struct sk_buff *resp;
  1259. dev_dbg(dev->dev, "%s\n", __func__);
  1260. skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
  1261. if (!skb)
  1262. return -ENOMEM;
  1263. *skb_put(skb, sizeof(u8)) = 1; /* TG */
  1264. *skb_put(skb, sizeof(u8)) = 0; /* Next */
  1265. resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
  1266. if (IS_ERR(resp))
  1267. return PTR_ERR(resp);
  1268. rsp = (struct pn533_cmd_activate_response *)resp->data;
  1269. rc = rsp->status & PN533_CMD_RET_MASK;
  1270. if (rc != PN533_CMD_RET_SUCCESS) {
  1271. nfc_err(dev->dev,
  1272. "Target activation failed (error 0x%x)\n", rc);
  1273. dev_kfree_skb(resp);
  1274. return -EIO;
  1275. }
  1276. /* ATR_RES general bytes are located at offset 16 */
  1277. gt_len = resp->len - 16;
  1278. rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
  1279. dev_kfree_skb(resp);
  1280. return rc;
  1281. }
  1282. static int pn533_activate_target(struct nfc_dev *nfc_dev,
  1283. struct nfc_target *target, u32 protocol)
  1284. {
  1285. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1286. int rc;
  1287. dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
  1288. if (dev->poll_mod_count) {
  1289. nfc_err(dev->dev,
  1290. "Cannot activate while polling\n");
  1291. return -EBUSY;
  1292. }
  1293. if (dev->tgt_active_prot) {
  1294. nfc_err(dev->dev,
  1295. "There is already an active target\n");
  1296. return -EBUSY;
  1297. }
  1298. if (!dev->tgt_available_prots) {
  1299. nfc_err(dev->dev,
  1300. "There is no available target to activate\n");
  1301. return -EINVAL;
  1302. }
  1303. if (!(dev->tgt_available_prots & (1 << protocol))) {
  1304. nfc_err(dev->dev,
  1305. "Target doesn't support requested proto %u\n",
  1306. protocol);
  1307. return -EINVAL;
  1308. }
  1309. if (protocol == NFC_PROTO_NFC_DEP) {
  1310. rc = pn533_activate_target_nfcdep(dev);
  1311. if (rc) {
  1312. nfc_err(dev->dev,
  1313. "Activating target with DEP failed %d\n", rc);
  1314. return rc;
  1315. }
  1316. }
  1317. dev->tgt_active_prot = protocol;
  1318. dev->tgt_available_prots = 0;
  1319. return 0;
  1320. }
  1321. static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
  1322. struct sk_buff *resp)
  1323. {
  1324. int rc = 0;
  1325. dev_dbg(dev->dev, "%s\n", __func__);
  1326. if (IS_ERR(resp)) {
  1327. rc = PTR_ERR(resp);
  1328. nfc_err(dev->dev, "Target release error %d\n", rc);
  1329. return rc;
  1330. }
  1331. rc = resp->data[0] & PN533_CMD_RET_MASK;
  1332. if (rc != PN533_CMD_RET_SUCCESS)
  1333. nfc_err(dev->dev,
  1334. "Error 0x%x when releasing the target\n", rc);
  1335. dev_kfree_skb(resp);
  1336. return rc;
  1337. }
  1338. static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
  1339. struct nfc_target *target, u8 mode)
  1340. {
  1341. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1342. struct sk_buff *skb;
  1343. int rc;
  1344. dev_dbg(dev->dev, "%s\n", __func__);
  1345. if (!dev->tgt_active_prot) {
  1346. nfc_err(dev->dev, "There is no active target\n");
  1347. return;
  1348. }
  1349. dev->tgt_active_prot = 0;
  1350. skb_queue_purge(&dev->resp_q);
  1351. skb = pn533_alloc_skb(dev, sizeof(u8));
  1352. if (!skb)
  1353. return;
  1354. *skb_put(skb, 1) = 1; /* TG*/
  1355. rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
  1356. pn533_deactivate_target_complete, NULL);
  1357. if (rc < 0) {
  1358. dev_kfree_skb(skb);
  1359. nfc_err(dev->dev, "Target release error %d\n", rc);
  1360. }
  1361. }
  1362. static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
  1363. struct sk_buff *resp)
  1364. {
  1365. struct pn533_cmd_jump_dep_response *rsp;
  1366. u8 target_gt_len;
  1367. int rc;
  1368. u8 active = *(u8 *)arg;
  1369. kfree(arg);
  1370. if (IS_ERR(resp))
  1371. return PTR_ERR(resp);
  1372. if (dev->tgt_available_prots &&
  1373. !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
  1374. nfc_err(dev->dev,
  1375. "The target does not support DEP\n");
  1376. rc = -EINVAL;
  1377. goto error;
  1378. }
  1379. rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
  1380. rc = rsp->status & PN533_CMD_RET_MASK;
  1381. if (rc != PN533_CMD_RET_SUCCESS) {
  1382. nfc_err(dev->dev,
  1383. "Bringing DEP link up failed (error 0x%x)\n", rc);
  1384. goto error;
  1385. }
  1386. if (!dev->tgt_available_prots) {
  1387. struct nfc_target nfc_target;
  1388. dev_dbg(dev->dev, "Creating new target\n");
  1389. nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
  1390. nfc_target.nfcid1_len = 10;
  1391. memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
  1392. rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
  1393. if (rc)
  1394. goto error;
  1395. dev->tgt_available_prots = 0;
  1396. }
  1397. dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
  1398. /* ATR_RES general bytes are located at offset 17 */
  1399. target_gt_len = resp->len - 17;
  1400. rc = nfc_set_remote_general_bytes(dev->nfc_dev,
  1401. rsp->gt, target_gt_len);
  1402. if (rc == 0)
  1403. rc = nfc_dep_link_is_up(dev->nfc_dev,
  1404. dev->nfc_dev->targets[0].idx,
  1405. !active, NFC_RF_INITIATOR);
  1406. error:
  1407. dev_kfree_skb(resp);
  1408. return rc;
  1409. }
  1410. static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
  1411. static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
  1412. u8 comm_mode, u8 *gb, size_t gb_len)
  1413. {
  1414. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1415. struct sk_buff *skb;
  1416. int rc, skb_len;
  1417. u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
  1418. u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
  1419. dev_dbg(dev->dev, "%s\n", __func__);
  1420. if (dev->poll_mod_count) {
  1421. nfc_err(dev->dev,
  1422. "Cannot bring the DEP link up while polling\n");
  1423. return -EBUSY;
  1424. }
  1425. if (dev->tgt_active_prot) {
  1426. nfc_err(dev->dev,
  1427. "There is already an active target\n");
  1428. return -EBUSY;
  1429. }
  1430. skb_len = 3 + gb_len; /* ActPass + BR + Next */
  1431. skb_len += PASSIVE_DATA_LEN;
  1432. /* NFCID3 */
  1433. skb_len += NFC_NFCID3_MAXSIZE;
  1434. if (target && !target->nfcid2_len) {
  1435. nfcid3[0] = 0x1;
  1436. nfcid3[1] = 0xfe;
  1437. get_random_bytes(nfcid3 + 2, 6);
  1438. }
  1439. skb = pn533_alloc_skb(dev, skb_len);
  1440. if (!skb)
  1441. return -ENOMEM;
  1442. *skb_put(skb, 1) = !comm_mode; /* ActPass */
  1443. *skb_put(skb, 1) = 0x02; /* 424 kbps */
  1444. next = skb_put(skb, 1); /* Next */
  1445. *next = 0;
  1446. /* Copy passive data */
  1447. memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
  1448. *next |= 1;
  1449. /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
  1450. if (target && target->nfcid2_len)
  1451. memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
  1452. target->nfcid2_len);
  1453. else
  1454. memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
  1455. NFC_NFCID3_MAXSIZE);
  1456. *next |= 2;
  1457. if (gb != NULL && gb_len > 0) {
  1458. memcpy(skb_put(skb, gb_len), gb, gb_len);
  1459. *next |= 4; /* We have some Gi */
  1460. } else {
  1461. *next = 0;
  1462. }
  1463. arg = kmalloc(sizeof(*arg), GFP_KERNEL);
  1464. if (!arg) {
  1465. dev_kfree_skb(skb);
  1466. return -ENOMEM;
  1467. }
  1468. *arg = !comm_mode;
  1469. pn533_rf_field(dev->nfc_dev, 0);
  1470. rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
  1471. pn533_in_dep_link_up_complete, arg);
  1472. if (rc < 0) {
  1473. dev_kfree_skb(skb);
  1474. kfree(arg);
  1475. }
  1476. return rc;
  1477. }
  1478. static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
  1479. {
  1480. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1481. dev_dbg(dev->dev, "%s\n", __func__);
  1482. pn533_poll_reset_mod_list(dev);
  1483. if (dev->tgt_mode || dev->tgt_active_prot)
  1484. dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
  1485. dev->tgt_active_prot = 0;
  1486. dev->tgt_mode = 0;
  1487. skb_queue_purge(&dev->resp_q);
  1488. return 0;
  1489. }
  1490. struct pn533_data_exchange_arg {
  1491. data_exchange_cb_t cb;
  1492. void *cb_context;
  1493. };
  1494. static struct sk_buff *pn533_build_response(struct pn533 *dev)
  1495. {
  1496. struct sk_buff *skb, *tmp, *t;
  1497. unsigned int skb_len = 0, tmp_len = 0;
  1498. dev_dbg(dev->dev, "%s\n", __func__);
  1499. if (skb_queue_empty(&dev->resp_q))
  1500. return NULL;
  1501. if (skb_queue_len(&dev->resp_q) == 1) {
  1502. skb = skb_dequeue(&dev->resp_q);
  1503. goto out;
  1504. }
  1505. skb_queue_walk_safe(&dev->resp_q, tmp, t)
  1506. skb_len += tmp->len;
  1507. dev_dbg(dev->dev, "%s total length %d\n",
  1508. __func__, skb_len);
  1509. skb = alloc_skb(skb_len, GFP_KERNEL);
  1510. if (skb == NULL)
  1511. goto out;
  1512. skb_put(skb, skb_len);
  1513. skb_queue_walk_safe(&dev->resp_q, tmp, t) {
  1514. memcpy(skb->data + tmp_len, tmp->data, tmp->len);
  1515. tmp_len += tmp->len;
  1516. }
  1517. out:
  1518. skb_queue_purge(&dev->resp_q);
  1519. return skb;
  1520. }
  1521. static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
  1522. struct sk_buff *resp)
  1523. {
  1524. struct pn533_data_exchange_arg *arg = _arg;
  1525. struct sk_buff *skb;
  1526. int rc = 0;
  1527. u8 status, ret, mi;
  1528. dev_dbg(dev->dev, "%s\n", __func__);
  1529. if (IS_ERR(resp)) {
  1530. rc = PTR_ERR(resp);
  1531. goto _error;
  1532. }
  1533. status = resp->data[0];
  1534. ret = status & PN533_CMD_RET_MASK;
  1535. mi = status & PN533_CMD_MI_MASK;
  1536. skb_pull(resp, sizeof(status));
  1537. if (ret != PN533_CMD_RET_SUCCESS) {
  1538. nfc_err(dev->dev,
  1539. "Exchanging data failed (error 0x%x)\n", ret);
  1540. rc = -EIO;
  1541. goto error;
  1542. }
  1543. skb_queue_tail(&dev->resp_q, resp);
  1544. if (mi) {
  1545. dev->cmd_complete_mi_arg = arg;
  1546. queue_work(dev->wq, &dev->mi_rx_work);
  1547. return -EINPROGRESS;
  1548. }
  1549. /* Prepare for the next round */
  1550. if (skb_queue_len(&dev->fragment_skb) > 0) {
  1551. dev->cmd_complete_dep_arg = arg;
  1552. queue_work(dev->wq, &dev->mi_tx_work);
  1553. return -EINPROGRESS;
  1554. }
  1555. skb = pn533_build_response(dev);
  1556. if (!skb) {
  1557. rc = -ENOMEM;
  1558. goto error;
  1559. }
  1560. arg->cb(arg->cb_context, skb, 0);
  1561. kfree(arg);
  1562. return 0;
  1563. error:
  1564. dev_kfree_skb(resp);
  1565. _error:
  1566. skb_queue_purge(&dev->resp_q);
  1567. arg->cb(arg->cb_context, NULL, rc);
  1568. kfree(arg);
  1569. return rc;
  1570. }
  1571. /*
  1572. * Receive an incoming pn533 frame. skb contains only header and payload.
  1573. * If skb == NULL, it is a notification that the link below is dead.
  1574. */
  1575. void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
  1576. {
  1577. if (!dev->cmd)
  1578. goto sched_wq;
  1579. dev->cmd->status = status;
  1580. if (status != 0) {
  1581. dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
  1582. goto sched_wq;
  1583. }
  1584. if (skb == NULL) {
  1585. pr_err("NULL Frame -> link is dead\n");
  1586. goto sched_wq;
  1587. }
  1588. if (pn533_rx_frame_is_ack(skb->data)) {
  1589. dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
  1590. dev_kfree_skb(skb);
  1591. return;
  1592. }
  1593. print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
  1594. dev->ops->rx_frame_size(skb->data), false);
  1595. if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
  1596. nfc_err(dev->dev, "Received an invalid frame\n");
  1597. dev->cmd->status = -EIO;
  1598. } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
  1599. nfc_err(dev->dev, "It it not the response to the last command\n");
  1600. dev->cmd->status = -EIO;
  1601. }
  1602. dev->cmd->resp = skb;
  1603. sched_wq:
  1604. queue_work(dev->wq, &dev->cmd_complete_work);
  1605. }
  1606. EXPORT_SYMBOL(pn533_recv_frame);
  1607. /* Split the Tx skb into small chunks */
  1608. static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
  1609. {
  1610. struct sk_buff *frag;
  1611. int frag_size;
  1612. do {
  1613. /* Remaining size */
  1614. if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
  1615. frag_size = PN533_CMD_DATAFRAME_MAXLEN;
  1616. else
  1617. frag_size = skb->len;
  1618. /* Allocate and reserve */
  1619. frag = pn533_alloc_skb(dev, frag_size);
  1620. if (!frag) {
  1621. skb_queue_purge(&dev->fragment_skb);
  1622. break;
  1623. }
  1624. if (!dev->tgt_mode) {
  1625. /* Reserve the TG/MI byte */
  1626. skb_reserve(frag, 1);
  1627. /* MI + TG */
  1628. if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
  1629. *skb_push(frag, sizeof(u8)) =
  1630. (PN533_CMD_MI_MASK | 1);
  1631. else
  1632. *skb_push(frag, sizeof(u8)) = 1; /* TG */
  1633. }
  1634. memcpy(skb_put(frag, frag_size), skb->data, frag_size);
  1635. /* Reduce the size of incoming buffer */
  1636. skb_pull(skb, frag_size);
  1637. /* Add this to skb_queue */
  1638. skb_queue_tail(&dev->fragment_skb, frag);
  1639. } while (skb->len > 0);
  1640. dev_kfree_skb(skb);
  1641. return skb_queue_len(&dev->fragment_skb);
  1642. }
  1643. static int pn533_transceive(struct nfc_dev *nfc_dev,
  1644. struct nfc_target *target, struct sk_buff *skb,
  1645. data_exchange_cb_t cb, void *cb_context)
  1646. {
  1647. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1648. struct pn533_data_exchange_arg *arg = NULL;
  1649. int rc;
  1650. dev_dbg(dev->dev, "%s\n", __func__);
  1651. if (!dev->tgt_active_prot) {
  1652. nfc_err(dev->dev,
  1653. "Can't exchange data if there is no active target\n");
  1654. rc = -EINVAL;
  1655. goto error;
  1656. }
  1657. arg = kmalloc(sizeof(*arg), GFP_KERNEL);
  1658. if (!arg) {
  1659. rc = -ENOMEM;
  1660. goto error;
  1661. }
  1662. arg->cb = cb;
  1663. arg->cb_context = cb_context;
  1664. switch (dev->device_type) {
  1665. case PN533_DEVICE_PASORI:
  1666. if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
  1667. rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
  1668. skb,
  1669. pn533_data_exchange_complete,
  1670. arg);
  1671. break;
  1672. }
  1673. default:
  1674. /* jumbo frame ? */
  1675. if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  1676. rc = pn533_fill_fragment_skbs(dev, skb);
  1677. if (rc <= 0)
  1678. goto error;
  1679. skb = skb_dequeue(&dev->fragment_skb);
  1680. if (!skb) {
  1681. rc = -EIO;
  1682. goto error;
  1683. }
  1684. } else {
  1685. *skb_push(skb, sizeof(u8)) = 1; /* TG */
  1686. }
  1687. rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
  1688. skb, pn533_data_exchange_complete,
  1689. arg);
  1690. break;
  1691. }
  1692. if (rc < 0) /* rc from send_async */
  1693. goto error;
  1694. return 0;
  1695. error:
  1696. kfree(arg);
  1697. dev_kfree_skb(skb);
  1698. return rc;
  1699. }
  1700. static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
  1701. struct sk_buff *resp)
  1702. {
  1703. u8 status;
  1704. dev_dbg(dev->dev, "%s\n", __func__);
  1705. if (IS_ERR(resp))
  1706. return PTR_ERR(resp);
  1707. status = resp->data[0];
  1708. /* Prepare for the next round */
  1709. if (skb_queue_len(&dev->fragment_skb) > 0) {
  1710. queue_work(dev->wq, &dev->mi_tm_tx_work);
  1711. return -EINPROGRESS;
  1712. }
  1713. dev_kfree_skb(resp);
  1714. if (status != 0) {
  1715. nfc_tm_deactivated(dev->nfc_dev);
  1716. dev->tgt_mode = 0;
  1717. return 0;
  1718. }
  1719. queue_work(dev->wq, &dev->tg_work);
  1720. return 0;
  1721. }
  1722. static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
  1723. {
  1724. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1725. int rc;
  1726. dev_dbg(dev->dev, "%s\n", __func__);
  1727. /* let's split in multiple chunks if size's too big */
  1728. if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
  1729. rc = pn533_fill_fragment_skbs(dev, skb);
  1730. if (rc <= 0)
  1731. goto error;
  1732. /* get the first skb */
  1733. skb = skb_dequeue(&dev->fragment_skb);
  1734. if (!skb) {
  1735. rc = -EIO;
  1736. goto error;
  1737. }
  1738. rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
  1739. pn533_tm_send_complete, NULL);
  1740. } else {
  1741. /* Send th skb */
  1742. rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
  1743. pn533_tm_send_complete, NULL);
  1744. }
  1745. error:
  1746. if (rc < 0) {
  1747. dev_kfree_skb(skb);
  1748. skb_queue_purge(&dev->fragment_skb);
  1749. }
  1750. return rc;
  1751. }
  1752. static void pn533_wq_mi_recv(struct work_struct *work)
  1753. {
  1754. struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
  1755. struct sk_buff *skb;
  1756. int rc;
  1757. dev_dbg(dev->dev, "%s\n", __func__);
  1758. skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
  1759. if (!skb)
  1760. goto error;
  1761. switch (dev->device_type) {
  1762. case PN533_DEVICE_PASORI:
  1763. if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
  1764. rc = pn533_send_cmd_direct_async(dev,
  1765. PN533_CMD_IN_COMM_THRU,
  1766. skb,
  1767. pn533_data_exchange_complete,
  1768. dev->cmd_complete_mi_arg);
  1769. break;
  1770. }
  1771. default:
  1772. *skb_put(skb, sizeof(u8)) = 1; /*TG*/
  1773. rc = pn533_send_cmd_direct_async(dev,
  1774. PN533_CMD_IN_DATA_EXCHANGE,
  1775. skb,
  1776. pn533_data_exchange_complete,
  1777. dev->cmd_complete_mi_arg);
  1778. break;
  1779. }
  1780. if (rc == 0) /* success */
  1781. return;
  1782. nfc_err(dev->dev,
  1783. "Error %d when trying to perform data_exchange\n", rc);
  1784. dev_kfree_skb(skb);
  1785. kfree(dev->cmd_complete_mi_arg);
  1786. error:
  1787. dev->phy_ops->send_ack(dev, GFP_KERNEL);
  1788. queue_work(dev->wq, &dev->cmd_work);
  1789. }
  1790. static void pn533_wq_mi_send(struct work_struct *work)
  1791. {
  1792. struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
  1793. struct sk_buff *skb;
  1794. int rc;
  1795. dev_dbg(dev->dev, "%s\n", __func__);
  1796. /* Grab the first skb in the queue */
  1797. skb = skb_dequeue(&dev->fragment_skb);
  1798. if (skb == NULL) { /* No more data */
  1799. /* Reset the queue for future use */
  1800. skb_queue_head_init(&dev->fragment_skb);
  1801. goto error;
  1802. }
  1803. switch (dev->device_type) {
  1804. case PN533_DEVICE_PASORI:
  1805. if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
  1806. rc = -EIO;
  1807. break;
  1808. }
  1809. rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
  1810. skb,
  1811. pn533_data_exchange_complete,
  1812. dev->cmd_complete_dep_arg);
  1813. break;
  1814. default:
  1815. /* Still some fragments? */
  1816. rc = pn533_send_cmd_direct_async(dev,
  1817. PN533_CMD_IN_DATA_EXCHANGE,
  1818. skb,
  1819. pn533_data_exchange_complete,
  1820. dev->cmd_complete_dep_arg);
  1821. break;
  1822. }
  1823. if (rc == 0) /* success */
  1824. return;
  1825. nfc_err(dev->dev,
  1826. "Error %d when trying to perform data_exchange\n", rc);
  1827. dev_kfree_skb(skb);
  1828. kfree(dev->cmd_complete_dep_arg);
  1829. error:
  1830. dev->phy_ops->send_ack(dev, GFP_KERNEL);
  1831. queue_work(dev->wq, &dev->cmd_work);
  1832. }
  1833. static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
  1834. u8 cfgdata_len)
  1835. {
  1836. struct sk_buff *skb;
  1837. struct sk_buff *resp;
  1838. int skb_len;
  1839. dev_dbg(dev->dev, "%s\n", __func__);
  1840. skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
  1841. skb = pn533_alloc_skb(dev, skb_len);
  1842. if (!skb)
  1843. return -ENOMEM;
  1844. *skb_put(skb, sizeof(cfgitem)) = cfgitem;
  1845. memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
  1846. resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
  1847. if (IS_ERR(resp))
  1848. return PTR_ERR(resp);
  1849. dev_kfree_skb(resp);
  1850. return 0;
  1851. }
  1852. static int pn533_get_firmware_version(struct pn533 *dev,
  1853. struct pn533_fw_version *fv)
  1854. {
  1855. struct sk_buff *skb;
  1856. struct sk_buff *resp;
  1857. skb = pn533_alloc_skb(dev, 0);
  1858. if (!skb)
  1859. return -ENOMEM;
  1860. resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
  1861. if (IS_ERR(resp))
  1862. return PTR_ERR(resp);
  1863. fv->ic = resp->data[0];
  1864. fv->ver = resp->data[1];
  1865. fv->rev = resp->data[2];
  1866. fv->support = resp->data[3];
  1867. dev_kfree_skb(resp);
  1868. return 0;
  1869. }
  1870. static int pn533_pasori_fw_reset(struct pn533 *dev)
  1871. {
  1872. struct sk_buff *skb;
  1873. struct sk_buff *resp;
  1874. dev_dbg(dev->dev, "%s\n", __func__);
  1875. skb = pn533_alloc_skb(dev, sizeof(u8));
  1876. if (!skb)
  1877. return -ENOMEM;
  1878. *skb_put(skb, sizeof(u8)) = 0x1;
  1879. resp = pn533_send_cmd_sync(dev, 0x18, skb);
  1880. if (IS_ERR(resp))
  1881. return PTR_ERR(resp);
  1882. dev_kfree_skb(resp);
  1883. return 0;
  1884. }
  1885. static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
  1886. {
  1887. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1888. u8 rf_field = !!rf;
  1889. int rc;
  1890. rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
  1891. rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
  1892. (u8 *)&rf_field, 1);
  1893. if (rc) {
  1894. nfc_err(dev->dev, "Error on setting RF field\n");
  1895. return rc;
  1896. }
  1897. return rc;
  1898. }
  1899. static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
  1900. {
  1901. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1902. struct sk_buff *skb;
  1903. struct sk_buff *resp;
  1904. skb = pn533_alloc_skb(dev, 1);
  1905. if (!skb)
  1906. return -ENOMEM;
  1907. *skb_put(skb, 1) = 0x01;
  1908. resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
  1909. if (IS_ERR(resp))
  1910. return PTR_ERR(resp);
  1911. dev_kfree_skb(resp);
  1912. return 0;
  1913. }
  1914. static int pn533_dev_up(struct nfc_dev *nfc_dev)
  1915. {
  1916. struct pn533 *dev = nfc_get_drvdata(nfc_dev);
  1917. if (dev->device_type == PN533_DEVICE_PN532) {
  1918. int rc = pn532_sam_configuration(nfc_dev);
  1919. if (rc)
  1920. return rc;
  1921. }
  1922. return pn533_rf_field(nfc_dev, 1);
  1923. }
  1924. static int pn533_dev_down(struct nfc_dev *nfc_dev)
  1925. {
  1926. return pn533_rf_field(nfc_dev, 0);
  1927. }
  1928. static struct nfc_ops pn533_nfc_ops = {
  1929. .dev_up = pn533_dev_up,
  1930. .dev_down = pn533_dev_down,
  1931. .dep_link_up = pn533_dep_link_up,
  1932. .dep_link_down = pn533_dep_link_down,
  1933. .start_poll = pn533_start_poll,
  1934. .stop_poll = pn533_stop_poll,
  1935. .activate_target = pn533_activate_target,
  1936. .deactivate_target = pn533_deactivate_target,
  1937. .im_transceive = pn533_transceive,
  1938. .tm_send = pn533_tm_send,
  1939. };
  1940. static int pn533_setup(struct pn533 *dev)
  1941. {
  1942. struct pn533_config_max_retries max_retries;
  1943. struct pn533_config_timing timing;
  1944. u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
  1945. int rc;
  1946. switch (dev->device_type) {
  1947. case PN533_DEVICE_STD:
  1948. case PN533_DEVICE_PASORI:
  1949. case PN533_DEVICE_ACR122U:
  1950. case PN533_DEVICE_PN532:
  1951. max_retries.mx_rty_atr = 0x2;
  1952. max_retries.mx_rty_psl = 0x1;
  1953. max_retries.mx_rty_passive_act =
  1954. PN533_CONFIG_MAX_RETRIES_NO_RETRY;
  1955. timing.rfu = PN533_CONFIG_TIMING_102;
  1956. timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
  1957. timing.dep_timeout = PN533_CONFIG_TIMING_204;
  1958. break;
  1959. default:
  1960. nfc_err(dev->dev, "Unknown device type %d\n",
  1961. dev->device_type);
  1962. return -EINVAL;
  1963. }
  1964. rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
  1965. (u8 *)&max_retries, sizeof(max_retries));
  1966. if (rc) {
  1967. nfc_err(dev->dev,
  1968. "Error on setting MAX_RETRIES config\n");
  1969. return rc;
  1970. }
  1971. rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
  1972. (u8 *)&timing, sizeof(timing));
  1973. if (rc) {
  1974. nfc_err(dev->dev, "Error on setting RF timings\n");
  1975. return rc;
  1976. }
  1977. switch (dev->device_type) {
  1978. case PN533_DEVICE_STD:
  1979. case PN533_DEVICE_PN532:
  1980. break;
  1981. case PN533_DEVICE_PASORI:
  1982. pn533_pasori_fw_reset(dev);
  1983. rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
  1984. pasori_cfg, 3);
  1985. if (rc) {
  1986. nfc_err(dev->dev,
  1987. "Error while settings PASORI config\n");
  1988. return rc;
  1989. }
  1990. pn533_pasori_fw_reset(dev);
  1991. break;
  1992. }
  1993. return 0;
  1994. }
  1995. struct pn533 *pn533_register_device(u32 device_type,
  1996. u32 protocols,
  1997. enum pn533_protocol_type protocol_type,
  1998. void *phy,
  1999. struct pn533_phy_ops *phy_ops,
  2000. struct pn533_frame_ops *fops,
  2001. struct device *dev,
  2002. struct device *parent)
  2003. {
  2004. struct pn533_fw_version fw_ver;
  2005. struct pn533 *priv;
  2006. int rc = -ENOMEM;
  2007. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  2008. if (!priv)
  2009. return ERR_PTR(-ENOMEM);
  2010. priv->phy = phy;
  2011. priv->phy_ops = phy_ops;
  2012. priv->dev = dev;
  2013. if (fops != NULL)
  2014. priv->ops = fops;
  2015. else
  2016. priv->ops = &pn533_std_frame_ops;
  2017. priv->protocol_type = protocol_type;
  2018. priv->device_type = device_type;
  2019. mutex_init(&priv->cmd_lock);
  2020. INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
  2021. INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
  2022. INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
  2023. INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
  2024. INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
  2025. INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
  2026. INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
  2027. INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
  2028. INIT_WORK(&priv->rf_work, pn533_wq_rf);
  2029. priv->wq = alloc_ordered_workqueue("pn533", 0);
  2030. if (priv->wq == NULL)
  2031. goto error;
  2032. init_timer(&priv->listen_timer);
  2033. priv->listen_timer.data = (unsigned long) priv;
  2034. priv->listen_timer.function = pn533_listen_mode_timer;
  2035. skb_queue_head_init(&priv->resp_q);
  2036. skb_queue_head_init(&priv->fragment_skb);
  2037. INIT_LIST_HEAD(&priv->cmd_queue);
  2038. memset(&fw_ver, 0, sizeof(fw_ver));
  2039. rc = pn533_get_firmware_version(priv, &fw_ver);
  2040. if (rc < 0)
  2041. goto destroy_wq;
  2042. nfc_info(dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
  2043. fw_ver.ic, fw_ver.ver, fw_ver.rev);
  2044. priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
  2045. priv->ops->tx_header_len +
  2046. PN533_CMD_DATAEXCH_HEAD_LEN,
  2047. priv->ops->tx_tail_len);
  2048. if (!priv->nfc_dev) {
  2049. rc = -ENOMEM;
  2050. goto destroy_wq;
  2051. }
  2052. nfc_set_parent_dev(priv->nfc_dev, parent);
  2053. nfc_set_drvdata(priv->nfc_dev, priv);
  2054. rc = nfc_register_device(priv->nfc_dev);
  2055. if (rc)
  2056. goto free_nfc_dev;
  2057. rc = pn533_setup(priv);
  2058. if (rc)
  2059. goto unregister_nfc_dev;
  2060. return priv;
  2061. unregister_nfc_dev:
  2062. nfc_unregister_device(priv->nfc_dev);
  2063. free_nfc_dev:
  2064. nfc_free_device(priv->nfc_dev);
  2065. destroy_wq:
  2066. destroy_workqueue(priv->wq);
  2067. error:
  2068. kfree(priv);
  2069. return ERR_PTR(rc);
  2070. }
  2071. EXPORT_SYMBOL_GPL(pn533_register_device);
  2072. void pn533_unregister_device(struct pn533 *priv)
  2073. {
  2074. struct pn533_cmd *cmd, *n;
  2075. nfc_unregister_device(priv->nfc_dev);
  2076. nfc_free_device(priv->nfc_dev);
  2077. flush_delayed_work(&priv->poll_work);
  2078. destroy_workqueue(priv->wq);
  2079. skb_queue_purge(&priv->resp_q);
  2080. del_timer(&priv->listen_timer);
  2081. list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
  2082. list_del(&cmd->queue);
  2083. kfree(cmd);
  2084. }
  2085. kfree(priv);
  2086. }
  2087. EXPORT_SYMBOL_GPL(pn533_unregister_device);
  2088. MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
  2089. MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
  2090. MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
  2091. MODULE_DESCRIPTION("PN533 driver ver " VERSION);
  2092. MODULE_VERSION(VERSION);
  2093. MODULE_LICENSE("GPL");