pn533.c 58 KB

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