pn533.c 58 KB

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