port100.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * Sony NFC Port-100 Series driver
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * Partly based/Inspired by Stephen Tiedemann's nfcpy
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/usb.h>
  19. #include <net/nfc/digital.h>
  20. #define VERSION "0.1"
  21. #define SONY_VENDOR_ID 0x054c
  22. #define RCS380_PRODUCT_ID 0x06c1
  23. #define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
  24. NFC_PROTO_MIFARE_MASK | \
  25. NFC_PROTO_FELICA_MASK | \
  26. NFC_PROTO_NFC_DEP_MASK | \
  27. NFC_PROTO_ISO14443_MASK | \
  28. NFC_PROTO_ISO14443_B_MASK)
  29. #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
  30. NFC_DIGITAL_DRV_CAPS_TG_CRC)
  31. /* Standard port100 frame definitions */
  32. #define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
  33. + 2) /* data[0] CC, data[1] SCC */
  34. #define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
  35. #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
  36. /*
  37. * Max extended frame payload len, excluding CC and SCC
  38. * which are already in PORT100_FRAME_HEADER_LEN.
  39. */
  40. #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
  41. #define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
  42. Postamble (1) */
  43. static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = {
  44. 0x00, 0x00, 0xff, 0x00, 0xff, 0x00
  45. };
  46. #define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
  47. #define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
  48. /* start of frame */
  49. #define PORT100_FRAME_SOF 0x00FF
  50. #define PORT100_FRAME_EXT 0xFFFF
  51. #define PORT100_FRAME_ACK 0x00FF
  52. /* Port-100 command: in or out */
  53. #define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
  54. #define PORT100_FRAME_DIR_OUT 0xD6
  55. #define PORT100_FRAME_DIR_IN 0xD7
  56. /* Port-100 sub-command */
  57. #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
  58. #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
  59. #define PORT100_CMD_GET_COMMAND_TYPE 0x28
  60. #define PORT100_CMD_SET_COMMAND_TYPE 0x2A
  61. #define PORT100_CMD_IN_SET_RF 0x00
  62. #define PORT100_CMD_IN_SET_PROTOCOL 0x02
  63. #define PORT100_CMD_IN_COMM_RF 0x04
  64. #define PORT100_CMD_TG_SET_RF 0x40
  65. #define PORT100_CMD_TG_SET_PROTOCOL 0x42
  66. #define PORT100_CMD_TG_SET_RF_OFF 0x46
  67. #define PORT100_CMD_TG_COMM_RF 0x48
  68. #define PORT100_CMD_SWITCH_RF 0x06
  69. #define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
  70. #define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
  71. ((mask) & (0x01 << (cmd_type)))
  72. #define PORT100_CMD_TYPE_0 0
  73. #define PORT100_CMD_TYPE_1 1
  74. #define PORT100_CMD_STATUS_OK 0x00
  75. #define PORT100_CMD_STATUS_TIMEOUT 0x80
  76. #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
  77. #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
  78. struct port100;
  79. typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
  80. struct sk_buff *resp);
  81. /**
  82. * Setting sets structure for in_set_rf command
  83. *
  84. * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
  85. * This table contains multiple RF setting sets required for RF
  86. * communication.
  87. *
  88. * @in_*_comm_type: Theses fields set the communication type to be used.
  89. */
  90. struct port100_in_rf_setting {
  91. u8 in_send_set_number;
  92. u8 in_send_comm_type;
  93. u8 in_recv_set_number;
  94. u8 in_recv_comm_type;
  95. } __packed;
  96. #define PORT100_COMM_TYPE_IN_212F 0x01
  97. #define PORT100_COMM_TYPE_IN_424F 0x02
  98. #define PORT100_COMM_TYPE_IN_106A 0x03
  99. #define PORT100_COMM_TYPE_IN_106B 0x07
  100. static const struct port100_in_rf_setting in_rf_settings[] = {
  101. [NFC_DIGITAL_RF_TECH_212F] = {
  102. .in_send_set_number = 1,
  103. .in_send_comm_type = PORT100_COMM_TYPE_IN_212F,
  104. .in_recv_set_number = 15,
  105. .in_recv_comm_type = PORT100_COMM_TYPE_IN_212F,
  106. },
  107. [NFC_DIGITAL_RF_TECH_424F] = {
  108. .in_send_set_number = 1,
  109. .in_send_comm_type = PORT100_COMM_TYPE_IN_424F,
  110. .in_recv_set_number = 15,
  111. .in_recv_comm_type = PORT100_COMM_TYPE_IN_424F,
  112. },
  113. [NFC_DIGITAL_RF_TECH_106A] = {
  114. .in_send_set_number = 2,
  115. .in_send_comm_type = PORT100_COMM_TYPE_IN_106A,
  116. .in_recv_set_number = 15,
  117. .in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
  118. },
  119. [NFC_DIGITAL_RF_TECH_106B] = {
  120. .in_send_set_number = 3,
  121. .in_send_comm_type = PORT100_COMM_TYPE_IN_106B,
  122. .in_recv_set_number = 15,
  123. .in_recv_comm_type = PORT100_COMM_TYPE_IN_106B,
  124. },
  125. /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
  126. [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
  127. };
  128. /**
  129. * Setting sets structure for tg_set_rf command
  130. *
  131. * @tg_set_number: Represents the entry index in the port-100 RF Base Table.
  132. * This table contains multiple RF setting sets required for RF
  133. * communication. this field is used for both send and receive
  134. * settings.
  135. *
  136. * @tg_comm_type: Sets the communication type to be used to send and receive
  137. * data.
  138. */
  139. struct port100_tg_rf_setting {
  140. u8 tg_set_number;
  141. u8 tg_comm_type;
  142. } __packed;
  143. #define PORT100_COMM_TYPE_TG_106A 0x0B
  144. #define PORT100_COMM_TYPE_TG_212F 0x0C
  145. #define PORT100_COMM_TYPE_TG_424F 0x0D
  146. static const struct port100_tg_rf_setting tg_rf_settings[] = {
  147. [NFC_DIGITAL_RF_TECH_106A] = {
  148. .tg_set_number = 8,
  149. .tg_comm_type = PORT100_COMM_TYPE_TG_106A,
  150. },
  151. [NFC_DIGITAL_RF_TECH_212F] = {
  152. .tg_set_number = 8,
  153. .tg_comm_type = PORT100_COMM_TYPE_TG_212F,
  154. },
  155. [NFC_DIGITAL_RF_TECH_424F] = {
  156. .tg_set_number = 8,
  157. .tg_comm_type = PORT100_COMM_TYPE_TG_424F,
  158. },
  159. /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
  160. [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
  161. };
  162. #define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
  163. #define PORT100_IN_PROT_ADD_CRC 0x01
  164. #define PORT100_IN_PROT_CHECK_CRC 0x02
  165. #define PORT100_IN_PROT_MULTI_CARD 0x03
  166. #define PORT100_IN_PROT_ADD_PARITY 0x04
  167. #define PORT100_IN_PROT_CHECK_PARITY 0x05
  168. #define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06
  169. #define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07
  170. #define PORT100_IN_PROT_CRYPTO1 0x08
  171. #define PORT100_IN_PROT_ADD_SOF 0x09
  172. #define PORT100_IN_PROT_CHECK_SOF 0x0A
  173. #define PORT100_IN_PROT_ADD_EOF 0x0B
  174. #define PORT100_IN_PROT_CHECK_EOF 0x0C
  175. #define PORT100_IN_PROT_DEAF_TIME 0x0E
  176. #define PORT100_IN_PROT_CRM 0x0F
  177. #define PORT100_IN_PROT_CRM_MIN_LEN 0x10
  178. #define PORT100_IN_PROT_T1_TAG_FRAME 0x11
  179. #define PORT100_IN_PROT_RFCA 0x12
  180. #define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
  181. #define PORT100_IN_PROT_END 0x14
  182. #define PORT100_IN_MAX_NUM_PROTOCOLS 19
  183. #define PORT100_TG_PROT_TU 0x00
  184. #define PORT100_TG_PROT_RF_OFF 0x01
  185. #define PORT100_TG_PROT_CRM 0x02
  186. #define PORT100_TG_PROT_END 0x03
  187. #define PORT100_TG_MAX_NUM_PROTOCOLS 3
  188. struct port100_protocol {
  189. u8 number;
  190. u8 value;
  191. } __packed;
  192. static struct port100_protocol
  193. in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
  194. [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
  195. { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
  196. { PORT100_IN_PROT_ADD_CRC, 0 },
  197. { PORT100_IN_PROT_CHECK_CRC, 0 },
  198. { PORT100_IN_PROT_MULTI_CARD, 0 },
  199. { PORT100_IN_PROT_ADD_PARITY, 0 },
  200. { PORT100_IN_PROT_CHECK_PARITY, 1 },
  201. { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
  202. { PORT100_IN_PROT_VALID_BIT_NUMBER, 7 },
  203. { PORT100_IN_PROT_CRYPTO1, 0 },
  204. { PORT100_IN_PROT_ADD_SOF, 0 },
  205. { PORT100_IN_PROT_CHECK_SOF, 0 },
  206. { PORT100_IN_PROT_ADD_EOF, 0 },
  207. { PORT100_IN_PROT_CHECK_EOF, 0 },
  208. { PORT100_IN_PROT_DEAF_TIME, 4 },
  209. { PORT100_IN_PROT_CRM, 0 },
  210. { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
  211. { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
  212. { PORT100_IN_PROT_RFCA, 0 },
  213. { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
  214. { PORT100_IN_PROT_END, 0 },
  215. },
  216. [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
  217. { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
  218. { PORT100_IN_PROT_ADD_CRC, 0 },
  219. { PORT100_IN_PROT_CHECK_CRC, 0 },
  220. { PORT100_IN_PROT_MULTI_CARD, 0 },
  221. { PORT100_IN_PROT_ADD_PARITY, 1 },
  222. { PORT100_IN_PROT_CHECK_PARITY, 1 },
  223. { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
  224. { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
  225. { PORT100_IN_PROT_CRYPTO1, 0 },
  226. { PORT100_IN_PROT_ADD_SOF, 0 },
  227. { PORT100_IN_PROT_CHECK_SOF, 0 },
  228. { PORT100_IN_PROT_ADD_EOF, 0 },
  229. { PORT100_IN_PROT_CHECK_EOF, 0 },
  230. { PORT100_IN_PROT_DEAF_TIME, 4 },
  231. { PORT100_IN_PROT_CRM, 0 },
  232. { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
  233. { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
  234. { PORT100_IN_PROT_RFCA, 0 },
  235. { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
  236. { PORT100_IN_PROT_END, 0 },
  237. },
  238. [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
  239. { PORT100_IN_PROT_INITIAL_GUARD_TIME, 6 },
  240. { PORT100_IN_PROT_ADD_CRC, 1 },
  241. { PORT100_IN_PROT_CHECK_CRC, 1 },
  242. { PORT100_IN_PROT_MULTI_CARD, 0 },
  243. { PORT100_IN_PROT_ADD_PARITY, 1 },
  244. { PORT100_IN_PROT_CHECK_PARITY, 1 },
  245. { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
  246. { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
  247. { PORT100_IN_PROT_CRYPTO1, 0 },
  248. { PORT100_IN_PROT_ADD_SOF, 0 },
  249. { PORT100_IN_PROT_CHECK_SOF, 0 },
  250. { PORT100_IN_PROT_ADD_EOF, 0 },
  251. { PORT100_IN_PROT_CHECK_EOF, 0 },
  252. { PORT100_IN_PROT_DEAF_TIME, 4 },
  253. { PORT100_IN_PROT_CRM, 0 },
  254. { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
  255. { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
  256. { PORT100_IN_PROT_RFCA, 0 },
  257. { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
  258. { PORT100_IN_PROT_END, 0 },
  259. },
  260. [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
  261. /* nfc_digital_framing_nfca_short */
  262. { PORT100_IN_PROT_ADD_CRC, 2 },
  263. { PORT100_IN_PROT_CHECK_CRC, 2 },
  264. { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
  265. { PORT100_IN_PROT_T1_TAG_FRAME, 2 },
  266. { PORT100_IN_PROT_END, 0 },
  267. },
  268. [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
  269. /* nfc_digital_framing_nfca_standard */
  270. { PORT100_IN_PROT_ADD_CRC, 1 },
  271. { PORT100_IN_PROT_CHECK_CRC, 0 },
  272. { PORT100_IN_PROT_END, 0 },
  273. },
  274. [NFC_DIGITAL_FRAMING_NFCA_T4T] = {
  275. /* nfc_digital_framing_nfca_standard_with_crc_a */
  276. { PORT100_IN_PROT_END, 0 },
  277. },
  278. [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
  279. /* nfc_digital_framing_nfca_standard */
  280. { PORT100_IN_PROT_END, 0 },
  281. },
  282. [NFC_DIGITAL_FRAMING_NFCF] = {
  283. { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 },
  284. { PORT100_IN_PROT_ADD_CRC, 1 },
  285. { PORT100_IN_PROT_CHECK_CRC, 1 },
  286. { PORT100_IN_PROT_MULTI_CARD, 0 },
  287. { PORT100_IN_PROT_ADD_PARITY, 0 },
  288. { PORT100_IN_PROT_CHECK_PARITY, 0 },
  289. { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
  290. { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
  291. { PORT100_IN_PROT_CRYPTO1, 0 },
  292. { PORT100_IN_PROT_ADD_SOF, 0 },
  293. { PORT100_IN_PROT_CHECK_SOF, 0 },
  294. { PORT100_IN_PROT_ADD_EOF, 0 },
  295. { PORT100_IN_PROT_CHECK_EOF, 0 },
  296. { PORT100_IN_PROT_DEAF_TIME, 4 },
  297. { PORT100_IN_PROT_CRM, 0 },
  298. { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
  299. { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
  300. { PORT100_IN_PROT_RFCA, 0 },
  301. { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
  302. { PORT100_IN_PROT_END, 0 },
  303. },
  304. [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
  305. /* nfc_digital_framing_nfcf */
  306. { PORT100_IN_PROT_END, 0 },
  307. },
  308. [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
  309. /* nfc_digital_framing_nfcf */
  310. { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 },
  311. { PORT100_IN_PROT_ADD_CRC, 1 },
  312. { PORT100_IN_PROT_CHECK_CRC, 1 },
  313. { PORT100_IN_PROT_MULTI_CARD, 0 },
  314. { PORT100_IN_PROT_ADD_PARITY, 0 },
  315. { PORT100_IN_PROT_CHECK_PARITY, 0 },
  316. { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
  317. { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
  318. { PORT100_IN_PROT_CRYPTO1, 0 },
  319. { PORT100_IN_PROT_ADD_SOF, 0 },
  320. { PORT100_IN_PROT_CHECK_SOF, 0 },
  321. { PORT100_IN_PROT_ADD_EOF, 0 },
  322. { PORT100_IN_PROT_CHECK_EOF, 0 },
  323. { PORT100_IN_PROT_DEAF_TIME, 4 },
  324. { PORT100_IN_PROT_CRM, 0 },
  325. { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
  326. { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
  327. { PORT100_IN_PROT_RFCA, 0 },
  328. { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
  329. { PORT100_IN_PROT_END, 0 },
  330. },
  331. [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
  332. { PORT100_IN_PROT_END, 0 },
  333. },
  334. [NFC_DIGITAL_FRAMING_NFCB] = {
  335. { PORT100_IN_PROT_INITIAL_GUARD_TIME, 20 },
  336. { PORT100_IN_PROT_ADD_CRC, 1 },
  337. { PORT100_IN_PROT_CHECK_CRC, 1 },
  338. { PORT100_IN_PROT_MULTI_CARD, 0 },
  339. { PORT100_IN_PROT_ADD_PARITY, 0 },
  340. { PORT100_IN_PROT_CHECK_PARITY, 0 },
  341. { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
  342. { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
  343. { PORT100_IN_PROT_CRYPTO1, 0 },
  344. { PORT100_IN_PROT_ADD_SOF, 1 },
  345. { PORT100_IN_PROT_CHECK_SOF, 1 },
  346. { PORT100_IN_PROT_ADD_EOF, 1 },
  347. { PORT100_IN_PROT_CHECK_EOF, 1 },
  348. { PORT100_IN_PROT_DEAF_TIME, 4 },
  349. { PORT100_IN_PROT_CRM, 0 },
  350. { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
  351. { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
  352. { PORT100_IN_PROT_RFCA, 0 },
  353. { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
  354. { PORT100_IN_PROT_END, 0 },
  355. },
  356. [NFC_DIGITAL_FRAMING_NFCB_T4T] = {
  357. /* nfc_digital_framing_nfcb */
  358. { PORT100_IN_PROT_END, 0 },
  359. },
  360. /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
  361. [NFC_DIGITAL_FRAMING_LAST] = {
  362. { PORT100_IN_PROT_END, 0 },
  363. },
  364. };
  365. static struct port100_protocol
  366. tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
  367. [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
  368. { PORT100_TG_PROT_END, 0 },
  369. },
  370. [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
  371. { PORT100_TG_PROT_END, 0 },
  372. },
  373. [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
  374. { PORT100_TG_PROT_END, 0 },
  375. },
  376. [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
  377. { PORT100_TG_PROT_END, 0 },
  378. },
  379. [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
  380. { PORT100_TG_PROT_END, 0 },
  381. },
  382. [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
  383. { PORT100_TG_PROT_TU, 1 },
  384. { PORT100_TG_PROT_RF_OFF, 0 },
  385. { PORT100_TG_PROT_CRM, 7 },
  386. { PORT100_TG_PROT_END, 0 },
  387. },
  388. [NFC_DIGITAL_FRAMING_NFCF] = {
  389. { PORT100_TG_PROT_END, 0 },
  390. },
  391. [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
  392. { PORT100_TG_PROT_END, 0 },
  393. },
  394. [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
  395. { PORT100_TG_PROT_TU, 1 },
  396. { PORT100_TG_PROT_RF_OFF, 0 },
  397. { PORT100_TG_PROT_CRM, 7 },
  398. { PORT100_TG_PROT_END, 0 },
  399. },
  400. [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
  401. { PORT100_TG_PROT_RF_OFF, 1 },
  402. { PORT100_TG_PROT_END, 0 },
  403. },
  404. /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
  405. [NFC_DIGITAL_FRAMING_LAST] = {
  406. { PORT100_TG_PROT_END, 0 },
  407. },
  408. };
  409. struct port100 {
  410. struct nfc_digital_dev *nfc_digital_dev;
  411. int skb_headroom;
  412. int skb_tailroom;
  413. struct usb_device *udev;
  414. struct usb_interface *interface;
  415. struct urb *out_urb;
  416. struct urb *in_urb;
  417. /* This mutex protects the out_urb and avoids to submit a new command
  418. * through port100_send_frame_async() while the previous one is being
  419. * canceled through port100_abort_cmd().
  420. */
  421. struct mutex out_urb_lock;
  422. struct work_struct cmd_complete_work;
  423. u8 cmd_type;
  424. /* The digital stack serializes commands to be sent. There is no need
  425. * for any queuing/locking mechanism at driver level.
  426. */
  427. struct port100_cmd *cmd;
  428. bool cmd_cancel;
  429. struct completion cmd_cancel_done;
  430. };
  431. struct port100_cmd {
  432. u8 code;
  433. int status;
  434. struct sk_buff *req;
  435. struct sk_buff *resp;
  436. int resp_len;
  437. port100_send_async_complete_t complete_cb;
  438. void *complete_cb_context;
  439. };
  440. struct port100_frame {
  441. u8 preamble;
  442. __be16 start_frame;
  443. __be16 extended_frame;
  444. __le16 datalen;
  445. u8 datalen_checksum;
  446. u8 data[];
  447. } __packed;
  448. struct port100_ack_frame {
  449. u8 preamble;
  450. __be16 start_frame;
  451. __be16 ack_frame;
  452. u8 postambule;
  453. } __packed;
  454. struct port100_cb_arg {
  455. nfc_digital_cmd_complete_t complete_cb;
  456. void *complete_arg;
  457. u8 mdaa;
  458. };
  459. struct port100_tg_comm_rf_cmd {
  460. __le16 guard_time;
  461. __le16 send_timeout;
  462. u8 mdaa;
  463. u8 nfca_param[6];
  464. u8 nfcf_param[18];
  465. u8 mf_halted;
  466. u8 arae_flag;
  467. __le16 recv_timeout;
  468. u8 data[];
  469. } __packed;
  470. struct port100_tg_comm_rf_res {
  471. u8 comm_type;
  472. u8 ar_status;
  473. u8 target_activated;
  474. __le32 status;
  475. u8 data[];
  476. } __packed;
  477. /* The rule: value + checksum = 0 */
  478. static inline u8 port100_checksum(u16 value)
  479. {
  480. return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
  481. }
  482. /* The rule: sum(data elements) + checksum = 0 */
  483. static u8 port100_data_checksum(u8 *data, int datalen)
  484. {
  485. u8 sum = 0;
  486. int i;
  487. for (i = 0; i < datalen; i++)
  488. sum += data[i];
  489. return port100_checksum(sum);
  490. }
  491. static void port100_tx_frame_init(void *_frame, u8 cmd_code)
  492. {
  493. struct port100_frame *frame = _frame;
  494. frame->preamble = 0;
  495. frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
  496. frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
  497. PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
  498. PORT100_FRAME_CMD(frame) = cmd_code;
  499. frame->datalen = cpu_to_le16(2);
  500. }
  501. static void port100_tx_frame_finish(void *_frame)
  502. {
  503. struct port100_frame *frame = _frame;
  504. frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
  505. PORT100_FRAME_CHECKSUM(frame) =
  506. port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
  507. PORT100_FRAME_POSTAMBLE(frame) = 0;
  508. }
  509. static void port100_tx_update_payload_len(void *_frame, int len)
  510. {
  511. struct port100_frame *frame = _frame;
  512. frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
  513. }
  514. static bool port100_rx_frame_is_valid(void *_frame)
  515. {
  516. u8 checksum;
  517. struct port100_frame *frame = _frame;
  518. if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
  519. frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
  520. return false;
  521. checksum = port100_checksum(le16_to_cpu(frame->datalen));
  522. if (checksum != frame->datalen_checksum)
  523. return false;
  524. checksum = port100_data_checksum(frame->data,
  525. le16_to_cpu(frame->datalen));
  526. if (checksum != PORT100_FRAME_CHECKSUM(frame))
  527. return false;
  528. return true;
  529. }
  530. static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
  531. {
  532. return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
  533. frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
  534. }
  535. static inline int port100_rx_frame_size(void *frame)
  536. {
  537. struct port100_frame *f = frame;
  538. return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
  539. PORT100_FRAME_TAIL_LEN;
  540. }
  541. static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
  542. {
  543. struct port100_frame *f = frame;
  544. return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
  545. }
  546. static void port100_recv_response(struct urb *urb)
  547. {
  548. struct port100 *dev = urb->context;
  549. struct port100_cmd *cmd = dev->cmd;
  550. u8 *in_frame;
  551. cmd->status = urb->status;
  552. switch (urb->status) {
  553. case 0:
  554. break; /* success */
  555. case -ECONNRESET:
  556. case -ENOENT:
  557. nfc_err(&dev->interface->dev,
  558. "The urb has been canceled (status %d)\n", urb->status);
  559. goto sched_wq;
  560. case -ESHUTDOWN:
  561. default:
  562. nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
  563. urb->status);
  564. goto sched_wq;
  565. }
  566. in_frame = dev->in_urb->transfer_buffer;
  567. if (!port100_rx_frame_is_valid(in_frame)) {
  568. nfc_err(&dev->interface->dev, "Received an invalid frame\n");
  569. cmd->status = -EIO;
  570. goto sched_wq;
  571. }
  572. print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
  573. port100_rx_frame_size(in_frame), false);
  574. if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
  575. nfc_err(&dev->interface->dev,
  576. "It's not the response to the last command\n");
  577. cmd->status = -EIO;
  578. goto sched_wq;
  579. }
  580. sched_wq:
  581. schedule_work(&dev->cmd_complete_work);
  582. }
  583. static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
  584. {
  585. dev->in_urb->complete = port100_recv_response;
  586. return usb_submit_urb(dev->in_urb, flags);
  587. }
  588. static void port100_recv_ack(struct urb *urb)
  589. {
  590. struct port100 *dev = urb->context;
  591. struct port100_cmd *cmd = dev->cmd;
  592. struct port100_ack_frame *in_frame;
  593. int rc;
  594. cmd->status = urb->status;
  595. switch (urb->status) {
  596. case 0:
  597. break; /* success */
  598. case -ECONNRESET:
  599. case -ENOENT:
  600. nfc_err(&dev->interface->dev,
  601. "The urb has been stopped (status %d)\n", urb->status);
  602. goto sched_wq;
  603. case -ESHUTDOWN:
  604. default:
  605. nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
  606. urb->status);
  607. goto sched_wq;
  608. }
  609. in_frame = dev->in_urb->transfer_buffer;
  610. if (!port100_rx_frame_is_ack(in_frame)) {
  611. nfc_err(&dev->interface->dev, "Received an invalid ack\n");
  612. cmd->status = -EIO;
  613. goto sched_wq;
  614. }
  615. rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
  616. if (rc) {
  617. nfc_err(&dev->interface->dev,
  618. "usb_submit_urb failed with result %d\n", rc);
  619. cmd->status = rc;
  620. goto sched_wq;
  621. }
  622. return;
  623. sched_wq:
  624. schedule_work(&dev->cmd_complete_work);
  625. }
  626. static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
  627. {
  628. dev->in_urb->complete = port100_recv_ack;
  629. return usb_submit_urb(dev->in_urb, flags);
  630. }
  631. static int port100_send_ack(struct port100 *dev)
  632. {
  633. int rc = 0;
  634. mutex_lock(&dev->out_urb_lock);
  635. /*
  636. * If prior cancel is in-flight (dev->cmd_cancel == true), we
  637. * can skip to send cancel. Then this will wait the prior
  638. * cancel, or merged into the next cancel rarely if next
  639. * cancel was started before waiting done. In any case, this
  640. * will be waked up soon or later.
  641. */
  642. if (!dev->cmd_cancel) {
  643. reinit_completion(&dev->cmd_cancel_done);
  644. usb_kill_urb(dev->out_urb);
  645. dev->out_urb->transfer_buffer = ack_frame;
  646. dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
  647. rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
  648. /*
  649. * Set the cmd_cancel flag only if the URB has been
  650. * successfully submitted. It will be reset by the out
  651. * URB completion callback port100_send_complete().
  652. */
  653. dev->cmd_cancel = !rc;
  654. }
  655. mutex_unlock(&dev->out_urb_lock);
  656. if (!rc)
  657. wait_for_completion(&dev->cmd_cancel_done);
  658. return rc;
  659. }
  660. static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
  661. struct sk_buff *in, int in_len)
  662. {
  663. int rc;
  664. mutex_lock(&dev->out_urb_lock);
  665. /* A command cancel frame as been sent through dev->out_urb. Don't try
  666. * to submit a new one.
  667. */
  668. if (dev->cmd_cancel) {
  669. rc = -EAGAIN;
  670. goto exit;
  671. }
  672. dev->out_urb->transfer_buffer = out->data;
  673. dev->out_urb->transfer_buffer_length = out->len;
  674. dev->in_urb->transfer_buffer = in->data;
  675. dev->in_urb->transfer_buffer_length = in_len;
  676. print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
  677. out->data, out->len, false);
  678. rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
  679. if (rc)
  680. goto exit;
  681. rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
  682. if (rc)
  683. usb_unlink_urb(dev->out_urb);
  684. exit:
  685. mutex_unlock(&dev->out_urb_lock);
  686. return rc;
  687. }
  688. static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
  689. struct sk_buff *skb)
  690. {
  691. /* payload is already there, just update datalen */
  692. int payload_len = skb->len;
  693. skb_push(skb, PORT100_FRAME_HEADER_LEN);
  694. skb_put(skb, PORT100_FRAME_TAIL_LEN);
  695. port100_tx_frame_init(skb->data, cmd_code);
  696. port100_tx_update_payload_len(skb->data, payload_len);
  697. port100_tx_frame_finish(skb->data);
  698. }
  699. static void port100_send_async_complete(struct port100 *dev)
  700. {
  701. struct port100_cmd *cmd = dev->cmd;
  702. int status = cmd->status;
  703. struct sk_buff *req = cmd->req;
  704. struct sk_buff *resp = cmd->resp;
  705. dev_kfree_skb(req);
  706. dev->cmd = NULL;
  707. if (status < 0) {
  708. cmd->complete_cb(dev, cmd->complete_cb_context,
  709. ERR_PTR(status));
  710. dev_kfree_skb(resp);
  711. goto done;
  712. }
  713. skb_put(resp, port100_rx_frame_size(resp->data));
  714. skb_pull(resp, PORT100_FRAME_HEADER_LEN);
  715. skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
  716. cmd->complete_cb(dev, cmd->complete_cb_context, resp);
  717. done:
  718. kfree(cmd);
  719. }
  720. static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
  721. struct sk_buff *req,
  722. port100_send_async_complete_t complete_cb,
  723. void *complete_cb_context)
  724. {
  725. struct port100_cmd *cmd;
  726. struct sk_buff *resp;
  727. int rc;
  728. int resp_len = PORT100_FRAME_HEADER_LEN +
  729. PORT100_FRAME_MAX_PAYLOAD_LEN +
  730. PORT100_FRAME_TAIL_LEN;
  731. if (dev->cmd) {
  732. nfc_err(&dev->interface->dev,
  733. "A command is still in process\n");
  734. return -EBUSY;
  735. }
  736. resp = alloc_skb(resp_len, GFP_KERNEL);
  737. if (!resp)
  738. return -ENOMEM;
  739. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  740. if (!cmd) {
  741. dev_kfree_skb(resp);
  742. return -ENOMEM;
  743. }
  744. cmd->code = cmd_code;
  745. cmd->req = req;
  746. cmd->resp = resp;
  747. cmd->resp_len = resp_len;
  748. cmd->complete_cb = complete_cb;
  749. cmd->complete_cb_context = complete_cb_context;
  750. port100_build_cmd_frame(dev, cmd_code, req);
  751. dev->cmd = cmd;
  752. rc = port100_send_frame_async(dev, req, resp, resp_len);
  753. if (rc) {
  754. kfree(cmd);
  755. dev_kfree_skb(resp);
  756. dev->cmd = NULL;
  757. }
  758. return rc;
  759. }
  760. struct port100_sync_cmd_response {
  761. struct sk_buff *resp;
  762. struct completion done;
  763. };
  764. static void port100_wq_cmd_complete(struct work_struct *work)
  765. {
  766. struct port100 *dev = container_of(work, struct port100,
  767. cmd_complete_work);
  768. port100_send_async_complete(dev);
  769. }
  770. static void port100_send_sync_complete(struct port100 *dev, void *_arg,
  771. struct sk_buff *resp)
  772. {
  773. struct port100_sync_cmd_response *arg = _arg;
  774. arg->resp = resp;
  775. complete(&arg->done);
  776. }
  777. static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
  778. struct sk_buff *req)
  779. {
  780. int rc;
  781. struct port100_sync_cmd_response arg;
  782. init_completion(&arg.done);
  783. rc = port100_send_cmd_async(dev, cmd_code, req,
  784. port100_send_sync_complete, &arg);
  785. if (rc) {
  786. dev_kfree_skb(req);
  787. return ERR_PTR(rc);
  788. }
  789. wait_for_completion(&arg.done);
  790. return arg.resp;
  791. }
  792. static void port100_send_complete(struct urb *urb)
  793. {
  794. struct port100 *dev = urb->context;
  795. if (dev->cmd_cancel) {
  796. complete_all(&dev->cmd_cancel_done);
  797. dev->cmd_cancel = false;
  798. }
  799. switch (urb->status) {
  800. case 0:
  801. break; /* success */
  802. case -ECONNRESET:
  803. case -ENOENT:
  804. nfc_err(&dev->interface->dev,
  805. "The urb has been stopped (status %d)\n", urb->status);
  806. break;
  807. case -ESHUTDOWN:
  808. default:
  809. nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
  810. urb->status);
  811. }
  812. }
  813. static void port100_abort_cmd(struct nfc_digital_dev *ddev)
  814. {
  815. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  816. /* An ack will cancel the last issued command */
  817. port100_send_ack(dev);
  818. /* cancel the urb request */
  819. usb_kill_urb(dev->in_urb);
  820. }
  821. static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
  822. {
  823. struct sk_buff *skb;
  824. skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
  825. GFP_KERNEL);
  826. if (skb)
  827. skb_reserve(skb, dev->skb_headroom);
  828. return skb;
  829. }
  830. static int port100_set_command_type(struct port100 *dev, u8 command_type)
  831. {
  832. struct sk_buff *skb;
  833. struct sk_buff *resp;
  834. int rc;
  835. skb = port100_alloc_skb(dev, 1);
  836. if (!skb)
  837. return -ENOMEM;
  838. *skb_put(skb, sizeof(u8)) = command_type;
  839. resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
  840. if (IS_ERR(resp))
  841. return PTR_ERR(resp);
  842. rc = resp->data[0];
  843. dev_kfree_skb(resp);
  844. return rc;
  845. }
  846. static u64 port100_get_command_type_mask(struct port100 *dev)
  847. {
  848. struct sk_buff *skb;
  849. struct sk_buff *resp;
  850. u64 mask;
  851. skb = port100_alloc_skb(dev, 0);
  852. if (!skb)
  853. return -ENOMEM;
  854. resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
  855. if (IS_ERR(resp))
  856. return PTR_ERR(resp);
  857. if (resp->len < 8)
  858. mask = 0;
  859. else
  860. mask = be64_to_cpu(*(__be64 *)resp->data);
  861. dev_kfree_skb(resp);
  862. return mask;
  863. }
  864. static u16 port100_get_firmware_version(struct port100 *dev)
  865. {
  866. struct sk_buff *skb;
  867. struct sk_buff *resp;
  868. u16 fw_ver;
  869. skb = port100_alloc_skb(dev, 0);
  870. if (!skb)
  871. return 0;
  872. resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
  873. skb);
  874. if (IS_ERR(resp))
  875. return 0;
  876. fw_ver = le16_to_cpu(*(__le16 *)resp->data);
  877. dev_kfree_skb(resp);
  878. return fw_ver;
  879. }
  880. static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
  881. {
  882. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  883. struct sk_buff *skb, *resp;
  884. skb = port100_alloc_skb(dev, 1);
  885. if (!skb)
  886. return -ENOMEM;
  887. *skb_put(skb, 1) = on ? 1 : 0;
  888. /* Cancel the last command if the device is being switched off */
  889. if (!on)
  890. port100_abort_cmd(ddev);
  891. resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
  892. if (IS_ERR(resp))
  893. return PTR_ERR(resp);
  894. dev_kfree_skb(resp);
  895. return 0;
  896. }
  897. static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
  898. {
  899. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  900. struct sk_buff *skb;
  901. struct sk_buff *resp;
  902. int rc;
  903. if (rf >= NFC_DIGITAL_RF_TECH_LAST)
  904. return -EINVAL;
  905. skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
  906. if (!skb)
  907. return -ENOMEM;
  908. memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
  909. &in_rf_settings[rf],
  910. sizeof(struct port100_in_rf_setting));
  911. resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
  912. if (IS_ERR(resp))
  913. return PTR_ERR(resp);
  914. rc = resp->data[0];
  915. dev_kfree_skb(resp);
  916. return rc;
  917. }
  918. static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
  919. {
  920. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  921. struct port100_protocol *protocols;
  922. struct sk_buff *skb;
  923. struct sk_buff *resp;
  924. int num_protocols;
  925. size_t size;
  926. int rc;
  927. if (param >= NFC_DIGITAL_FRAMING_LAST)
  928. return -EINVAL;
  929. protocols = in_protocols[param];
  930. num_protocols = 0;
  931. while (protocols[num_protocols].number != PORT100_IN_PROT_END)
  932. num_protocols++;
  933. if (!num_protocols)
  934. return 0;
  935. size = sizeof(struct port100_protocol) * num_protocols;
  936. skb = port100_alloc_skb(dev, size);
  937. if (!skb)
  938. return -ENOMEM;
  939. memcpy(skb_put(skb, size), protocols, size);
  940. resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
  941. if (IS_ERR(resp))
  942. return PTR_ERR(resp);
  943. rc = resp->data[0];
  944. dev_kfree_skb(resp);
  945. return rc;
  946. }
  947. static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
  948. int param)
  949. {
  950. if (type == NFC_DIGITAL_CONFIG_RF_TECH)
  951. return port100_in_set_rf(ddev, param);
  952. if (type == NFC_DIGITAL_CONFIG_FRAMING)
  953. return port100_in_set_framing(ddev, param);
  954. return -EINVAL;
  955. }
  956. static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
  957. struct sk_buff *resp)
  958. {
  959. struct port100_cb_arg *cb_arg = arg;
  960. nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
  961. u32 status;
  962. int rc;
  963. if (IS_ERR(resp)) {
  964. rc = PTR_ERR(resp);
  965. goto exit;
  966. }
  967. if (resp->len < 4) {
  968. nfc_err(&dev->interface->dev,
  969. "Invalid packet length received\n");
  970. rc = -EIO;
  971. goto error;
  972. }
  973. status = le32_to_cpu(*(__le32 *)resp->data);
  974. skb_pull(resp, sizeof(u32));
  975. if (status == PORT100_CMD_STATUS_TIMEOUT) {
  976. rc = -ETIMEDOUT;
  977. goto error;
  978. }
  979. if (status != PORT100_CMD_STATUS_OK) {
  980. nfc_err(&dev->interface->dev,
  981. "in_comm_rf failed with status 0x%08x\n", status);
  982. rc = -EIO;
  983. goto error;
  984. }
  985. /* Remove collision bits byte */
  986. skb_pull(resp, 1);
  987. goto exit;
  988. error:
  989. kfree_skb(resp);
  990. resp = ERR_PTR(rc);
  991. exit:
  992. cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
  993. kfree(cb_arg);
  994. }
  995. static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
  996. struct sk_buff *skb, u16 _timeout,
  997. nfc_digital_cmd_complete_t cb, void *arg)
  998. {
  999. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  1000. struct port100_cb_arg *cb_arg;
  1001. __le16 timeout;
  1002. cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
  1003. if (!cb_arg)
  1004. return -ENOMEM;
  1005. cb_arg->complete_cb = cb;
  1006. cb_arg->complete_arg = arg;
  1007. timeout = cpu_to_le16(_timeout * 10);
  1008. memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
  1009. return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
  1010. port100_in_comm_rf_complete, cb_arg);
  1011. }
  1012. static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
  1013. {
  1014. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  1015. struct sk_buff *skb;
  1016. struct sk_buff *resp;
  1017. int rc;
  1018. if (rf >= NFC_DIGITAL_RF_TECH_LAST)
  1019. return -EINVAL;
  1020. skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
  1021. if (!skb)
  1022. return -ENOMEM;
  1023. memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
  1024. &tg_rf_settings[rf],
  1025. sizeof(struct port100_tg_rf_setting));
  1026. resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
  1027. if (IS_ERR(resp))
  1028. return PTR_ERR(resp);
  1029. rc = resp->data[0];
  1030. dev_kfree_skb(resp);
  1031. return rc;
  1032. }
  1033. static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
  1034. {
  1035. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  1036. struct port100_protocol *protocols;
  1037. struct sk_buff *skb;
  1038. struct sk_buff *resp;
  1039. int rc;
  1040. int num_protocols;
  1041. size_t size;
  1042. if (param >= NFC_DIGITAL_FRAMING_LAST)
  1043. return -EINVAL;
  1044. protocols = tg_protocols[param];
  1045. num_protocols = 0;
  1046. while (protocols[num_protocols].number != PORT100_TG_PROT_END)
  1047. num_protocols++;
  1048. if (!num_protocols)
  1049. return 0;
  1050. size = sizeof(struct port100_protocol) * num_protocols;
  1051. skb = port100_alloc_skb(dev, size);
  1052. if (!skb)
  1053. return -ENOMEM;
  1054. memcpy(skb_put(skb, size), protocols, size);
  1055. resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
  1056. if (IS_ERR(resp))
  1057. return PTR_ERR(resp);
  1058. rc = resp->data[0];
  1059. dev_kfree_skb(resp);
  1060. return rc;
  1061. }
  1062. static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
  1063. int param)
  1064. {
  1065. if (type == NFC_DIGITAL_CONFIG_RF_TECH)
  1066. return port100_tg_set_rf(ddev, param);
  1067. if (type == NFC_DIGITAL_CONFIG_FRAMING)
  1068. return port100_tg_set_framing(ddev, param);
  1069. return -EINVAL;
  1070. }
  1071. static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
  1072. {
  1073. u8 mask;
  1074. switch (dev->cmd_type) {
  1075. case PORT100_CMD_TYPE_0:
  1076. mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
  1077. break;
  1078. case PORT100_CMD_TYPE_1:
  1079. mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
  1080. PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
  1081. break;
  1082. default:
  1083. nfc_err(&dev->interface->dev, "Unknown command type\n");
  1084. return false;
  1085. }
  1086. return ((tgt_activated & mask) == mask);
  1087. }
  1088. static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
  1089. struct sk_buff *resp)
  1090. {
  1091. u32 status;
  1092. struct port100_cb_arg *cb_arg = arg;
  1093. nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
  1094. struct port100_tg_comm_rf_res *hdr;
  1095. if (IS_ERR(resp))
  1096. goto exit;
  1097. hdr = (struct port100_tg_comm_rf_res *)resp->data;
  1098. status = le32_to_cpu(hdr->status);
  1099. if (cb_arg->mdaa &&
  1100. !port100_tg_target_activated(dev, hdr->target_activated)) {
  1101. kfree_skb(resp);
  1102. resp = ERR_PTR(-ETIMEDOUT);
  1103. goto exit;
  1104. }
  1105. skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
  1106. if (status != PORT100_CMD_STATUS_OK) {
  1107. kfree_skb(resp);
  1108. if (status == PORT100_CMD_STATUS_TIMEOUT)
  1109. resp = ERR_PTR(-ETIMEDOUT);
  1110. else
  1111. resp = ERR_PTR(-EIO);
  1112. }
  1113. exit:
  1114. cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
  1115. kfree(cb_arg);
  1116. }
  1117. static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
  1118. struct sk_buff *skb, u16 timeout,
  1119. nfc_digital_cmd_complete_t cb, void *arg)
  1120. {
  1121. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  1122. struct port100_tg_comm_rf_cmd *hdr;
  1123. struct port100_cb_arg *cb_arg;
  1124. cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
  1125. if (!cb_arg)
  1126. return -ENOMEM;
  1127. cb_arg->complete_cb = cb;
  1128. cb_arg->complete_arg = arg;
  1129. skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
  1130. hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
  1131. memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
  1132. hdr->guard_time = cpu_to_le16(500);
  1133. hdr->send_timeout = cpu_to_le16(0xFFFF);
  1134. hdr->recv_timeout = cpu_to_le16(timeout);
  1135. return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
  1136. port100_tg_comm_rf_complete, cb_arg);
  1137. }
  1138. static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
  1139. struct digital_tg_mdaa_params *params,
  1140. u16 timeout,
  1141. nfc_digital_cmd_complete_t cb, void *arg)
  1142. {
  1143. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  1144. struct port100_tg_comm_rf_cmd *hdr;
  1145. struct port100_cb_arg *cb_arg;
  1146. struct sk_buff *skb;
  1147. int rc;
  1148. rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
  1149. NFC_DIGITAL_RF_TECH_106A);
  1150. if (rc)
  1151. return rc;
  1152. rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
  1153. NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
  1154. if (rc)
  1155. return rc;
  1156. cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
  1157. if (!cb_arg)
  1158. return -ENOMEM;
  1159. cb_arg->complete_cb = cb;
  1160. cb_arg->complete_arg = arg;
  1161. cb_arg->mdaa = 1;
  1162. skb = port100_alloc_skb(dev, 0);
  1163. if (!skb) {
  1164. kfree(cb_arg);
  1165. return -ENOMEM;
  1166. }
  1167. skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
  1168. hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
  1169. memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
  1170. hdr->guard_time = 0;
  1171. hdr->send_timeout = cpu_to_le16(0xFFFF);
  1172. hdr->mdaa = 1;
  1173. hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
  1174. hdr->nfca_param[1] = params->sens_res & 0xFF;
  1175. memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
  1176. hdr->nfca_param[5] = params->sel_res;
  1177. memcpy(hdr->nfcf_param, params->nfcid2, 8);
  1178. hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
  1179. hdr->nfcf_param[17] = params->sc & 0xFF;
  1180. hdr->recv_timeout = cpu_to_le16(timeout);
  1181. return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
  1182. port100_tg_comm_rf_complete, cb_arg);
  1183. }
  1184. static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
  1185. nfc_digital_cmd_complete_t cb, void *arg)
  1186. {
  1187. struct port100 *dev = nfc_digital_get_drvdata(ddev);
  1188. struct sk_buff *skb;
  1189. skb = port100_alloc_skb(dev, 0);
  1190. if (!skb)
  1191. return -ENOMEM;
  1192. return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
  1193. }
  1194. static struct nfc_digital_ops port100_digital_ops = {
  1195. .in_configure_hw = port100_in_configure_hw,
  1196. .in_send_cmd = port100_in_send_cmd,
  1197. .tg_listen_mdaa = port100_listen_mdaa,
  1198. .tg_listen = port100_listen,
  1199. .tg_configure_hw = port100_tg_configure_hw,
  1200. .tg_send_cmd = port100_tg_send_cmd,
  1201. .switch_rf = port100_switch_rf,
  1202. .abort_cmd = port100_abort_cmd,
  1203. };
  1204. static const struct usb_device_id port100_table[] = {
  1205. { USB_DEVICE(SONY_VENDOR_ID, RCS380_PRODUCT_ID), },
  1206. { }
  1207. };
  1208. MODULE_DEVICE_TABLE(usb, port100_table);
  1209. static int port100_probe(struct usb_interface *interface,
  1210. const struct usb_device_id *id)
  1211. {
  1212. struct port100 *dev;
  1213. int rc;
  1214. struct usb_host_interface *iface_desc;
  1215. struct usb_endpoint_descriptor *endpoint;
  1216. int in_endpoint;
  1217. int out_endpoint;
  1218. u16 fw_version;
  1219. u64 cmd_type_mask;
  1220. int i;
  1221. dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
  1222. if (!dev)
  1223. return -ENOMEM;
  1224. mutex_init(&dev->out_urb_lock);
  1225. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  1226. dev->interface = interface;
  1227. usb_set_intfdata(interface, dev);
  1228. in_endpoint = out_endpoint = 0;
  1229. iface_desc = interface->cur_altsetting;
  1230. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1231. endpoint = &iface_desc->endpoint[i].desc;
  1232. if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
  1233. in_endpoint = endpoint->bEndpointAddress;
  1234. if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
  1235. out_endpoint = endpoint->bEndpointAddress;
  1236. }
  1237. if (!in_endpoint || !out_endpoint) {
  1238. nfc_err(&interface->dev,
  1239. "Could not find bulk-in or bulk-out endpoint\n");
  1240. rc = -ENODEV;
  1241. goto error;
  1242. }
  1243. dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
  1244. dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  1245. if (!dev->in_urb || !dev->out_urb) {
  1246. nfc_err(&interface->dev, "Could not allocate USB URBs\n");
  1247. rc = -ENOMEM;
  1248. goto error;
  1249. }
  1250. usb_fill_bulk_urb(dev->in_urb, dev->udev,
  1251. usb_rcvbulkpipe(dev->udev, in_endpoint),
  1252. NULL, 0, NULL, dev);
  1253. usb_fill_bulk_urb(dev->out_urb, dev->udev,
  1254. usb_sndbulkpipe(dev->udev, out_endpoint),
  1255. NULL, 0, port100_send_complete, dev);
  1256. dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
  1257. PORT100_COMM_RF_HEAD_MAX_LEN;
  1258. dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
  1259. init_completion(&dev->cmd_cancel_done);
  1260. INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
  1261. /* The first thing to do with the Port-100 is to set the command type
  1262. * to be used. If supported we use command type 1. 0 otherwise.
  1263. */
  1264. cmd_type_mask = port100_get_command_type_mask(dev);
  1265. if (!cmd_type_mask) {
  1266. nfc_err(&interface->dev,
  1267. "Could not get supported command types\n");
  1268. rc = -ENODEV;
  1269. goto error;
  1270. }
  1271. if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
  1272. dev->cmd_type = PORT100_CMD_TYPE_1;
  1273. else
  1274. dev->cmd_type = PORT100_CMD_TYPE_0;
  1275. rc = port100_set_command_type(dev, dev->cmd_type);
  1276. if (rc) {
  1277. nfc_err(&interface->dev,
  1278. "The device does not support command type %u\n",
  1279. dev->cmd_type);
  1280. goto error;
  1281. }
  1282. fw_version = port100_get_firmware_version(dev);
  1283. if (!fw_version)
  1284. nfc_err(&interface->dev,
  1285. "Could not get device firmware version\n");
  1286. nfc_info(&interface->dev,
  1287. "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
  1288. (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
  1289. dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
  1290. PORT100_PROTOCOLS,
  1291. PORT100_CAPABILITIES,
  1292. dev->skb_headroom,
  1293. dev->skb_tailroom);
  1294. if (!dev->nfc_digital_dev) {
  1295. nfc_err(&interface->dev,
  1296. "Could not allocate nfc_digital_dev\n");
  1297. rc = -ENOMEM;
  1298. goto error;
  1299. }
  1300. nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
  1301. nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
  1302. rc = nfc_digital_register_device(dev->nfc_digital_dev);
  1303. if (rc) {
  1304. nfc_err(&interface->dev,
  1305. "Could not register digital device\n");
  1306. goto free_nfc_dev;
  1307. }
  1308. return 0;
  1309. free_nfc_dev:
  1310. nfc_digital_free_device(dev->nfc_digital_dev);
  1311. error:
  1312. usb_free_urb(dev->in_urb);
  1313. usb_free_urb(dev->out_urb);
  1314. usb_put_dev(dev->udev);
  1315. return rc;
  1316. }
  1317. static void port100_disconnect(struct usb_interface *interface)
  1318. {
  1319. struct port100 *dev;
  1320. dev = usb_get_intfdata(interface);
  1321. usb_set_intfdata(interface, NULL);
  1322. nfc_digital_unregister_device(dev->nfc_digital_dev);
  1323. nfc_digital_free_device(dev->nfc_digital_dev);
  1324. usb_kill_urb(dev->in_urb);
  1325. usb_kill_urb(dev->out_urb);
  1326. usb_free_urb(dev->in_urb);
  1327. usb_free_urb(dev->out_urb);
  1328. usb_put_dev(dev->udev);
  1329. kfree(dev->cmd);
  1330. nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected\n");
  1331. }
  1332. static struct usb_driver port100_driver = {
  1333. .name = "port100",
  1334. .probe = port100_probe,
  1335. .disconnect = port100_disconnect,
  1336. .id_table = port100_table,
  1337. };
  1338. module_usb_driver(port100_driver);
  1339. MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
  1340. MODULE_VERSION(VERSION);
  1341. MODULE_LICENSE("GPL");