pn533.c 59 KB

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