pn533.c 58 KB

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