w1_ds28el15.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  1. /*
  2. * 1-Wire implementation for the ds23el15 chip
  3. *
  4. * Copyright (C) 2013 maximintergrated
  5. *
  6. *
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/types.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mutex.h>
  19. #include <linux/of.h>
  20. #include <linux/idr.h>
  21. #include <linux/delay.h>
  22. #include <linux/random.h>
  23. #include <linux/err.h>
  24. #include <linux/of.h>
  25. #include <linux/input.h>
  26. #include "../w1.h"
  27. #include "../w1_int.h"
  28. #include "../w1_family.h"
  29. #include "w1_ds28el15_sha256.h"
  30. // 1-Wire commands
  31. #define CMD_WRITE_MEMORY 0x55
  32. #define CMD_READ_MEMORY 0xF0
  33. #define CMD_LOAD_LOCK_SECRET 0x33
  34. #define CMD_COMPUTE_LOCK_SECRET 0x3C
  35. #define CMD_SELECT_SECRET 0x0F
  36. #define CMD_COMPUTE_PAGEMAC 0xA5
  37. #define CMD_READ_STATUS 0xAA
  38. #define CMD_WRITE_BLOCK_PROTECT 0xC3
  39. #define CMD_WRITE_AUTH_MEMORY 0x5A
  40. #define CMD_WRITE_AUTH_PROTECT 0xCC
  41. #define CMD_PIO_READ 0xDD
  42. #define CMD_PIO_WRITE 0x96
  43. #define CMD_RELEASE 0xAA
  44. #define BLOCK_READ_PROTECT 0x80
  45. #define BLOCK_WRITE_PROTECT 0x40
  46. #define BLOCK_EPROM_PROTECT 0x20
  47. #define BLOCK_WRITE_AUTH_PROTECT 0x10
  48. #define ROM_CMD_SKIP 0x3C
  49. #define ROM_CMD_RESUME 0xA5
  50. #define SELECT_SKIP 0
  51. #define SELECT_RESUME 1
  52. #define SELECT_MATCH 2
  53. #define SELECT_ODMATCH 3
  54. #define SELECT_SEARCH 4
  55. #define SELECT_READROM 5
  56. #define SELECT_ODSKIP 6
  57. #define PROT_BIT_AUTHWRITE 0x10
  58. #define PROT_BIT_EPROM 0x20
  59. #define PROT_BIT_WRITE 0x40
  60. #define PROT_BIT_READ 0x80
  61. #define DS28E15_FAMILY 0x17
  62. #define DS28E15_PAGES 2 // 2 pages, 4 blocks, 7 segment in each page. total 64 bytes
  63. #define PAGE_TO_BYTE 32 // 1 page = 32 bytes
  64. #define BLOCK_TO_BYTE 16 // 1 block = 16 bytes
  65. #define SEGMENT_TO_BYTE 4 // 1 segment = 4 bytes
  66. //#define LOW_VOLTAGE
  67. #ifdef LOW_VOLTAGE
  68. #define SHA_COMPUTATION_DELAY 4
  69. #define EEPROM_WRITE_DELAY 15
  70. #define SECRET_EEPROM_DELAY 200
  71. #else
  72. #define SHA_COMPUTATION_DELAY 3
  73. #define EEPROM_WRITE_DELAY 10
  74. #define SECRET_EEPROM_DELAY 90
  75. #endif
  76. #define ID_MIN 0
  77. #define ID_MAX 3
  78. #define CO_MIN 0
  79. #ifdef CONFIG_SEC_MEGA2LTE_COMMON
  80. #define CO_MAX 15
  81. #else
  82. #define CO_MAX 10
  83. #endif
  84. #define ID_DEFAULT 1
  85. #define CO_DEFAULT 1
  86. #define RETRY_LIMIT 10
  87. #ifdef CONFIG_W1_CF
  88. #define RETRY_LIMIT_CF 5
  89. #endif
  90. // misc state
  91. static unsigned short slave_crc16;
  92. static int special_mode = 0;
  93. static char special_values[2];
  94. static char rom_no[8];
  95. int verification = -1, id = 2, color;
  96. #ifdef CONFIG_W1_SN
  97. char g_sn[14];
  98. #endif
  99. #ifdef CONFIG_W1_CF
  100. int cf_node = -1;
  101. #endif
  102. #ifdef CONFIG_SEC_H_PROJECT
  103. extern int verified;
  104. #endif
  105. #define READ_EOP_BYTE(seg) (32-seg*4)
  106. static char w1_array[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  110. static u8 skip_setup = 1; // for test if the chip did not have secret code, we would need to write temp secret value
  111. static u8 init_verify = 1; // for inital verifying
  112. //module_param_array(w1_array, char, NULL, 0);
  113. //-----------------------------------------------------------------------------
  114. // ------ DS28EL15 Functions
  115. //-----------------------------------------------------------------------------
  116. //----------------------------------------------------------------------
  117. // Set or clear special mode flag
  118. //
  119. // 'enable' - '1' to enable special mode or '0' to clear
  120. //
  121. void set_special_mode(int enable, uchar *values)
  122. {
  123. special_mode= enable;
  124. special_values[0] = values[0];
  125. special_values[1] = values[1];
  126. }
  127. //--------------------------------------------------------------------------
  128. // Calculate a new CRC16 from the input data shorteger. Return the current
  129. // CRC16 and also update the global variable CRC16.
  130. //
  131. static short oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
  132. static unsigned short docrc16(unsigned short data)
  133. {
  134. data = (data ^ (slave_crc16 & 0xff)) & 0xff;
  135. slave_crc16 >>= 8;
  136. if (oddparity[data & 0xf] ^ oddparity[data >> 4])
  137. slave_crc16 ^= 0xc001;
  138. data <<= 6;
  139. slave_crc16 ^= data;
  140. data <<= 1;
  141. slave_crc16 ^= data;
  142. return slave_crc16;
  143. }
  144. //--------------------------------------------------------------------------
  145. // Compute MAC to write a 4 byte memory block using an authenticated
  146. // write.
  147. //
  148. // Parameters
  149. // page - page number where the block to write is located (0 to 15)
  150. // segment - segment number in page (0 to 7)
  151. // new_data - 4 byte buffer containing the data to write
  152. // old_data - 4 byte buffer containing the data to write
  153. // manid - 2 byte buffer containing the manufacturer ID (general device: 00h,00h)
  154. // mac - buffer to put the calculated mac into
  155. //
  156. // Returns: TRUE - mac calculated
  157. // FALSE - Failed to calculate
  158. //
  159. int calculate_write_authMAC256(int page, int segment, char *new_data, char *old_data, char *manid, char *mac)
  160. {
  161. char mt[64];
  162. // calculate MAC
  163. // clear
  164. memset(mt,0,64);
  165. // insert ROM number
  166. memcpy(&mt[32],rom_no,8);
  167. mt[43] = segment;
  168. mt[42] = page;
  169. mt[41] = manid[0];
  170. mt[40] = manid[1];
  171. // insert old data
  172. memcpy(&mt[44],old_data,4);
  173. // insert new data
  174. memcpy(&mt[48],new_data,4);
  175. // compute the mac
  176. return compute_mac256(mt, 55, &mac[0]);
  177. }
  178. //-----------------------------------------------------------------------------
  179. // ------ DS28EL15 Functions - 1 wire command
  180. //-----------------------------------------------------------------------------
  181. //--------------------------------------------------------------------------
  182. // Write a 4 byte memory block. The block location is selected by the
  183. // page number and offset blcok within the page. Multiple blocks can
  184. // be programmed without re-selecting the device using the continue flag.
  185. // This function does not use the Authenticated Write operation.
  186. //
  187. // Parameters
  188. // page - page number where the block to write is located (0, 1)
  189. // segment - segment number in page (0 to 7)
  190. // data - 4 byte buffer containing the data to write
  191. // contflag - Flag to indicate the write is continued from the last (=1)
  192. //
  193. // Returns: 0 - block written
  194. // else - Failed to write block (no presence or invalid CRC16)
  195. //
  196. int w1_ds28el15_write_block(struct w1_slave *sl, int page, int seg, uchar *data, int contflag)
  197. {
  198. uchar buf[256],cs;
  199. int cnt, i, offset;
  200. int length =4;
  201. cnt = 0;
  202. offset = 0;
  203. if (!sl)
  204. return -ENODEV;
  205. if (!contflag)
  206. {
  207. if (w1_reset_select_slave(sl))
  208. return -1;
  209. buf[cnt++] = CMD_WRITE_MEMORY;
  210. buf[cnt++] = (seg << 5) | page; // address
  211. // Send command
  212. w1_write_block(sl->master, &buf[0], 2);
  213. // Read CRC
  214. w1_read_block(sl->master, &buf[cnt], 2);
  215. cnt += 2;
  216. offset = cnt;
  217. }
  218. // add the data
  219. for (i = 0; i < length; i++)
  220. buf[cnt++] = data[i];
  221. // Send data
  222. w1_write_block(sl->master, data, length);
  223. // Read CRC
  224. w1_read_block(sl->master, &buf[cnt], 2);
  225. cnt += 2;
  226. // check the first CRC16
  227. if (!contflag)
  228. {
  229. slave_crc16 = 0;
  230. for (i = 0; i < offset; i++)
  231. docrc16(buf[i]);
  232. if (slave_crc16 != 0xB001)
  233. return -1;
  234. }
  235. // check the second CRC16
  236. slave_crc16 = 0;
  237. for (i = offset; i < cnt; i++)
  238. docrc16(buf[i]);
  239. if (slave_crc16 != 0xB001)
  240. return -1;
  241. // send release and strong pull-up
  242. buf[0] = CMD_RELEASE;
  243. w1_write_block(sl->master, &buf[0], 1);
  244. // now wait for EEPROM writing.
  245. msleep(EEPROM_WRITE_DELAY);
  246. // disable strong pullup
  247. // read the CS byte
  248. cs = w1_read_8(sl->master);
  249. if (cs == 0xAA)
  250. return 0;
  251. else
  252. return cs;
  253. }
  254. //--------------------------------------------------------------------------
  255. // Write a memory segment. The segment location is selected by the
  256. // page number and offset segment within the page. Multiple segments can
  257. // be programmed without re-selecting the device.
  258. // This function does not use the Authenticated Write operation.
  259. //
  260. // Parameters
  261. // page - page number where the block to write is located (0, 1)
  262. // seg - segment number in page (0 to 7)
  263. // data - 4 byte multiple buffer containing the data to write
  264. // length - length to write (4 multiple number, 4, 8, ..., 32)
  265. //
  266. // Returns: 0 - block written
  267. // else - Failed to write block (no presence or invalid CRC16)
  268. //
  269. int w1_ds28el15_write_memory(struct w1_slave *sl, int seg, int page, uchar *data, int length)
  270. {
  271. uchar buf[256];
  272. uchar cs=0;
  273. int i;
  274. if (!sl)
  275. return -ENODEV;
  276. // program one or more contiguous 4 byte segments of a memory block.
  277. if (length%4)
  278. return -EINVAL;
  279. memcpy(buf,data,length);
  280. for (i=0;i<length/4;i++) {
  281. cs = w1_ds28el15_write_block(sl, page, i, &buf[i*4], (i==0)? 0 : 1);
  282. }
  283. return cs;
  284. }
  285. //--------------------------------------------------------------------------
  286. // Write a 4 byte memory block using an authenticated write (with MAC).
  287. // The block location is selected by the
  288. // page number and offset blcok within the page. Multiple blocks can
  289. // be programmed without re-selecting the device using the continue flag.
  290. // This function does not use the Authenticated Write operation.
  291. //
  292. // Parameters
  293. // page - page number where the block to write is located (0, 1)
  294. // segment - segment number in page (0 to 7)
  295. // data - SHA input data for the Authenticated Write Memory including new_data, old_data, and manid
  296. // new_data - 4 byte buffer containing the data to write
  297. // old_data - 4 byte buffer containing the data to write
  298. // manid - 2 byte buffer containing the manufacturer ID (general device: 00h,00h)
  299. //
  300. // Restrictions
  301. // The memory block containing the targeted 4-byte segment must not be write protected.
  302. // The Read/Write Scratchpad command(w1_ds28el15_write_scratchpad) must have been issued once
  303. // in write mode after power-on reset to ensure proper setup of the SHA-256 engine.
  304. //
  305. // Returns: 0 - block written
  306. // else - Failed to write block (no presence or invalid CRC16)
  307. //
  308. int w1_ds28el15_write_authblock(struct w1_slave *sl, int page, int segment, uchar *data, int contflag)
  309. {
  310. uchar buf[256],cs;
  311. uchar new_data[4], old_data[4], manid[2];
  312. int cnt, i, offset;
  313. cnt = 0;
  314. offset = 0;
  315. if (!sl)
  316. return -ENODEV;
  317. memcpy(new_data, &data[0], 4);
  318. memcpy(old_data, &data[4], 4);
  319. memcpy(manid, &data[8], 2);
  320. if (!contflag) {
  321. if (w1_reset_select_slave(sl))
  322. return -1;
  323. buf[cnt++] = CMD_WRITE_AUTH_MEMORY;
  324. buf[cnt++] = (segment << 5) | page; // address
  325. // Send command
  326. w1_write_block(sl->master, &buf[0], 2);
  327. // Read the first CRC
  328. w1_read_block(sl->master, &buf[cnt], 2);
  329. cnt += 2;
  330. offset = cnt;
  331. }
  332. // add the data
  333. for (i = 0; i < 4; i++)
  334. buf[cnt++] = new_data[i];
  335. // Send data - first 4bytes
  336. w1_write_block(sl->master, new_data, 4);
  337. // read the second CRC
  338. w1_read_block(sl->master, &buf[cnt], 2);
  339. cnt += 2;
  340. // now wait for the MAC computation.
  341. msleep(SHA_COMPUTATION_DELAY);
  342. if (!contflag) {
  343. // check the first CRC16
  344. slave_crc16 = 0;
  345. for (i = 0; i < offset; i++)
  346. docrc16(buf[i]);
  347. if (slave_crc16 != 0xB001)
  348. return -1;
  349. }
  350. // check the second CRC16
  351. slave_crc16 = 0;
  352. for (i = offset; i < cnt; i++)
  353. docrc16(buf[i]);
  354. if (slave_crc16 != 0xB001)
  355. return -1;
  356. // compute the mac
  357. if (special_mode)
  358. {
  359. if (!calculate_write_authMAC256(page, segment, new_data, old_data, special_values, &buf[0]))
  360. return -1;
  361. }
  362. else
  363. {
  364. if (!calculate_write_authMAC256(page, segment, new_data, old_data, manid, &buf[0]))
  365. return -1;
  366. }
  367. // transmit MAC as a block - send the second 32bytes
  368. cnt=0;
  369. w1_write_block(sl->master, buf, 32);
  370. // calculate CRC on MAC
  371. slave_crc16 = 0;
  372. for (i = 0; i < 32; i++)
  373. docrc16(buf[i]);
  374. // append read of CRC16 and CS byte
  375. w1_read_block(sl->master, &buf[0], 3);
  376. cnt = 3;
  377. // ckeck CRC16
  378. for (i = 0; i < (cnt-1); i++)
  379. docrc16(buf[i]);
  380. if (slave_crc16 != 0xB001)
  381. return -1;
  382. // check CS
  383. if (buf[cnt-1] != 0xAA)
  384. return -1;
  385. // send release and strong pull-up
  386. buf[0] = CMD_RELEASE;
  387. w1_write_block(sl->master, &buf[0], 1);
  388. // now wait for the MAC computation.
  389. msleep(EEPROM_WRITE_DELAY);
  390. // disable strong pullup
  391. // read the CS byte
  392. cs = w1_read_8(sl->master);
  393. if (cs == 0xAA)
  394. return 0;
  395. else
  396. return cs;
  397. }
  398. //--------------------------------------------------------------------------
  399. // Write a 4 byte memory block using an authenticated write (with MAC).
  400. // The MAC must be pre-calculated.
  401. //
  402. // Parameters
  403. // page - page number where the block to write is located (0 to 15)
  404. // segment - segment number in page (0 to 7)
  405. // new_data - 4 byte buffer containing the data to write
  406. // mac - mac to use for the write
  407. //
  408. // Returns: 0 - block written
  409. // else - Failed to write block (no presence or invalid CRC16)
  410. //
  411. int w1_ds28el15_write_authblockMAC(struct w1_slave *sl, int page, int segment, uchar *new_data, uchar *mac)
  412. {
  413. uchar buf[256],cs;
  414. int cnt, i, offset;
  415. cnt = 0;
  416. offset = 0;
  417. if (!sl)
  418. return -ENODEV;
  419. // check if not continuing a previous block write
  420. if (w1_reset_select_slave(sl))
  421. return -1;
  422. buf[cnt++] = CMD_WRITE_AUTH_MEMORY;
  423. buf[cnt++] = (segment << 5) | page; // address
  424. // Send command
  425. w1_write_block(sl->master, &buf[0], 2);
  426. // Read CRC
  427. w1_read_block(sl->master, &buf[cnt], 2);
  428. cnt += 2;
  429. offset = cnt;
  430. // add the data
  431. for (i = 0; i < 4; i++)
  432. buf[cnt++] = new_data[i];
  433. // Send data
  434. w1_write_block(sl->master, new_data, 4);
  435. // read first CRC
  436. w1_read_block(sl->master, &buf[cnt], 2);
  437. cnt += 2;
  438. // now wait for the MAC computation.
  439. msleep(SHA_COMPUTATION_DELAY);
  440. // disable strong pullup
  441. // check the first CRC16
  442. slave_crc16 = 0;
  443. for (i = 0; i < offset; i++)
  444. docrc16(buf[i]);
  445. if (slave_crc16 != 0xB001)
  446. return -1;
  447. // check the second CRC16
  448. slave_crc16 = 0;
  449. for (i = offset; i < cnt; i++)
  450. docrc16(buf[i]);
  451. if (slave_crc16 != 0xB001)
  452. return -1;
  453. // transmit MAC as a block
  454. w1_write_block(sl->master, mac, 32);
  455. // calculate CRC on MAC
  456. slave_crc16 = 0;
  457. for (i = 0; i < 32; i++)
  458. docrc16(mac[i]);
  459. // append read of CRC16 and CS byte
  460. w1_read_block(sl->master, &buf[0], 3);
  461. cnt = 3;
  462. // ckeck CRC16
  463. for (i = 0; i < (cnt-1); i++)
  464. docrc16(buf[i]);
  465. if (slave_crc16 != 0xB001)
  466. return -1;
  467. // check CS
  468. if (buf[cnt-1] != 0xAA)
  469. return -1;
  470. // send release and strong pull-up
  471. buf[0] = CMD_RELEASE;
  472. w1_write_block(sl->master, &buf[0], 1);
  473. // now wait for the MAC computation.
  474. msleep(EEPROM_WRITE_DELAY);
  475. // disable strong pullup
  476. // read the CS byte
  477. cs = w1_read_8(sl->master);
  478. if (cs == 0xAA)
  479. return 0;
  480. else
  481. return cs;
  482. }
  483. #ifdef CONFIG_W1_CF
  484. //--------------------------------------------------------------------------
  485. // Read memory. Multiple pages can
  486. // be read without re-selecting the device using the continue flag.
  487. //
  488. // Parameters
  489. // seg - segment number(0~7) in page
  490. // page - page number where the block to read is located (0 to 15)
  491. // rdbuf - 32 byte buffer to contain the data to read
  492. // length - length to read (allow jsut segment(4bytes) unit) (4, 8, 16, ... , 64)
  493. //
  494. // Returns: 0 - block read and verified CRC
  495. // else - Failed to write block (no presence or invalid CRC16)
  496. //
  497. int w1_ds28el15_read_memory_check(struct w1_slave *sl, int seg, int page, uchar *rdbuf, int length)
  498. {
  499. uchar buf[256];
  500. int cnt, i, offset;
  501. cnt = 0;
  502. offset = 0;
  503. if (!sl)
  504. return -ENODEV;
  505. // Check presence detect
  506. if (w1_reset_overdrive_select_slave(sl))
  507. return -1;
  508. buf[cnt++] = CMD_READ_MEMORY;
  509. buf[cnt++] = (seg<<5) | page; // address
  510. // Send command
  511. w1_write_block(sl->master, &buf[0], 2);
  512. // Read CRC
  513. w1_read_block(sl->master, &buf[cnt], 2);
  514. cnt += 2;
  515. offset = cnt;
  516. // read data and CRC16
  517. w1_read_block(sl->master, &buf[cnt], length+2);
  518. cnt+=length+2;
  519. // check the first CRC16
  520. slave_crc16 = 0;
  521. for (i = 0; i < offset; i++)
  522. docrc16(buf[i]);
  523. if (slave_crc16 != 0xB001)
  524. return -2;
  525. if (READ_EOP_BYTE(seg) == length) {
  526. // check the second CRC16
  527. slave_crc16 = 0;
  528. for (i = offset; i < cnt; i++)
  529. docrc16(buf[i]);
  530. if (slave_crc16 != 0xB001)
  531. return -2;
  532. }
  533. // copy the data to the read buffer
  534. memcpy(rdbuf,&buf[offset],length);
  535. return 0;
  536. }
  537. #endif
  538. //--------------------------------------------------------------------------
  539. // Read memory. Multiple pages can
  540. // be read without re-selecting the device using the continue flag.
  541. //
  542. // Parameters
  543. // seg - segment number(0~7) in page
  544. // page - page number where the block to read is located (0 to 15)
  545. // rdbuf - 32 byte buffer to contain the data to read
  546. // length - length to read (allow jsut segment(4bytes) unit) (4, 8, 16, ... , 64)
  547. //
  548. // Returns: 0 - block read and verified CRC
  549. // else - Failed to write block (no presence or invalid CRC16)
  550. //
  551. int w1_ds28el15_read_memory(struct w1_slave *sl, int seg, int page, uchar *rdbuf, int length)
  552. {
  553. uchar buf[256];
  554. int cnt, i, offset;
  555. cnt = 0;
  556. offset = 0;
  557. if (!sl)
  558. return -ENODEV;
  559. // Check presence detect
  560. if (w1_reset_select_slave(sl))
  561. return -1;
  562. buf[cnt++] = CMD_READ_MEMORY;
  563. buf[cnt++] = (seg<<5) | page; // address
  564. // Send command
  565. w1_write_block(sl->master, &buf[0], 2);
  566. // Read CRC
  567. w1_read_block(sl->master, &buf[cnt], 2);
  568. cnt += 2;
  569. offset = cnt;
  570. // read data and CRC16
  571. w1_read_block(sl->master, &buf[cnt], length+2);
  572. cnt+=length+2;
  573. // check the first CRC16
  574. slave_crc16 = 0;
  575. for (i = 0; i < offset; i++)
  576. docrc16(buf[i]);
  577. if (slave_crc16 != 0xB001)
  578. return -1;
  579. if (READ_EOP_BYTE(seg) == length) {
  580. // check the second CRC16
  581. slave_crc16 = 0;
  582. for (i = offset; i < cnt; i++)
  583. docrc16(buf[i]);
  584. if (slave_crc16 != 0xB001)
  585. return -1;
  586. }
  587. // copy the data to the read buffer
  588. memcpy(rdbuf,&buf[offset],length);
  589. return 0;
  590. }
  591. //--------------------------------------------------------------------------
  592. // Read page and verify CRC. Multiple pages can
  593. // be read without re-selecting the device using the continue flag.
  594. //
  595. // Parameters
  596. // page - page number where the block to write is located (0 to 15)
  597. // rdbuf - 32 byte buffer to contain the data to read
  598. //
  599. // Returns: 0 - block read and verified CRC
  600. // else - Failed to write block (no presence or invalid CRC16)
  601. //
  602. int w1_ds28el15_read_page(struct w1_slave *sl, int page, uchar *rdbuf)
  603. {
  604. return w1_ds28el15_read_memory(sl, 0, page, rdbuf, 32);
  605. }
  606. //----------------------------------------------------------------------
  607. // Read the scratchpad (challenge or secret)
  608. //
  609. // 'rbbuf' - 32 byte buffer to contain the data to read
  610. //
  611. // Return: 0 - select complete
  612. // else - error during select, device not present
  613. //
  614. int w1_ds28el15_read_scratchpad(struct w1_slave *sl, uchar *rdbuf)
  615. {
  616. uchar buf[256];
  617. int cnt=0, i, offset;
  618. if (!sl)
  619. return -ENODEV;
  620. // select device for write
  621. if (w1_reset_select_slave(sl))
  622. return -1;
  623. buf[cnt++] = CMD_SELECT_SECRET;
  624. buf[cnt++] = 0x0F;
  625. // Send command
  626. w1_write_block(sl->master, &buf[0], 2);
  627. // Read CRC
  628. w1_read_block(sl->master, &buf[cnt], 2);
  629. cnt += 2;
  630. offset = cnt;
  631. // read data and CRC16
  632. w1_read_block(sl->master, &buf[cnt], 34);
  633. cnt+=34;
  634. // check first CRC16
  635. slave_crc16 = 0;
  636. for (i = 0; i < offset; i++)
  637. docrc16(buf[i]);
  638. if (slave_crc16 != 0xB001)
  639. return -1;
  640. // check the second CRC16
  641. slave_crc16 = 0;
  642. for (i = offset; i < cnt; i++)
  643. docrc16(buf[i]);
  644. if (slave_crc16 != 0xB001)
  645. return -1;
  646. // copy the data to the read buffer
  647. memcpy(rdbuf,&buf[offset],32);
  648. return 0;
  649. }
  650. //----------------------------------------------------------------------
  651. // Write the scratchpad (challenge or secret)
  652. //
  653. // 'data' - data to write to the scratchpad (32 bytes)
  654. //
  655. // Return: 0 - select complete
  656. // else - error during select, device not present
  657. //
  658. int w1_ds28el15_write_scratchpad(struct w1_slave *sl, uchar *data)
  659. {
  660. uchar buf[256];
  661. int cnt=0, i, offset;
  662. if (!sl)
  663. return -ENODEV;
  664. // select device for write
  665. if (w1_reset_select_slave(sl))
  666. return -1;
  667. buf[cnt++] = CMD_SELECT_SECRET;
  668. buf[cnt++] = 0x00;
  669. // Send command
  670. w1_write_block(sl->master, &buf[0], 2);
  671. // Read CRC
  672. w1_read_block(sl->master, &buf[cnt], 2);
  673. cnt += 2;
  674. offset = cnt;
  675. // add the data
  676. memcpy(&buf[cnt], data, 32);
  677. cnt+=32;
  678. // Send the data
  679. w1_write_block(sl->master, data, 32);
  680. // Read CRC
  681. w1_read_block(sl->master, &buf[cnt], 2);
  682. cnt += 2;
  683. // check first CRC16
  684. slave_crc16 = 0;
  685. for (i = 0; i < offset; i++)
  686. docrc16(buf[i]);
  687. if (slave_crc16 != 0xB001)
  688. return -2;
  689. // check the second CRC16
  690. slave_crc16 = 0;
  691. for (i = offset; i < cnt; i++)
  692. docrc16(buf[i]);
  693. if (slave_crc16 != 0xB001)
  694. return -3;
  695. return 0;
  696. }
  697. //----------------------------------------------------------------------
  698. // Load first secret operation on the DS28E25/DS28E22/DS28E15.
  699. //
  700. // 'lock' - option to lock the secret after the load (lock = 1)
  701. //
  702. // Restrictions
  703. // The Read/Write Scratchpad command(w1_ds28el15_write_scratchpad) must have been issued
  704. // in write mode prior to Load and Lock Secret to define the secret's value.
  705. //
  706. // Return: 0 - load complete
  707. // else - error during load, device not present
  708. //
  709. int w1_ds28el15_load_secret(struct w1_slave *sl, int lock)
  710. {
  711. uchar buf[256],cs;
  712. int cnt=0, i;
  713. if (!sl)
  714. return -ENODEV;
  715. // select device for write
  716. if (w1_reset_select_slave(sl))
  717. return -1;
  718. buf[cnt++] = CMD_LOAD_LOCK_SECRET;
  719. buf[cnt++] = (lock) ? 0xE0 : 0x00; // lock flag
  720. // Send command
  721. w1_write_block(sl->master, &buf[0], 2);
  722. // Read CRC
  723. w1_read_block(sl->master, &buf[cnt], 2);
  724. cnt += 2;
  725. // check CRC16
  726. slave_crc16 = 0;
  727. for (i = 0; i < cnt; i++)
  728. docrc16(buf[i]);
  729. if (slave_crc16 != 0xB001)
  730. return -1;
  731. // send release and strong pull-up
  732. buf[0] = CMD_RELEASE;
  733. w1_write_block(sl->master, &buf[0], 1);
  734. // now wait for the MAC computation.
  735. msleep(SECRET_EEPROM_DELAY);
  736. // disable strong pullup
  737. // read the CS byte
  738. cs = w1_read_8(sl->master);
  739. if (cs == 0xAA)
  740. return 0;
  741. else
  742. return cs;
  743. }
  744. //----------------------------------------------------------------------
  745. // Compute secret operation on the DS28E25/DS28E22/DS28E15.
  746. //
  747. // 'partial' - partial secret to load (32 bytes)
  748. // 'pbyte' - parameter byte including page_num and lock
  749. // 'page_num' - page number to read 0 - 16
  750. // 'lock' - option to lock the secret after the load (lock = 1)
  751. //
  752. // Restrictions
  753. // The Read/Write Scratchpad command(w1_ds28el15_write_scratchpad) must have been issued
  754. // in write mode prior to Compute and Lock Secret to define the partial secret.
  755. //
  756. // Return: 0 - compute complete
  757. // else - error during compute, device not present
  758. //
  759. int w1_ds28el15_compute_secret(struct w1_slave *sl, int pbyte)
  760. {
  761. uchar buf[256],cs;
  762. int cnt=0, i;
  763. int page_num, lock;
  764. if (!sl)
  765. return -ENODEV;
  766. page_num = pbyte & 0x01;
  767. lock = (pbyte & 0xE0) ? 1 : 0;
  768. // select device for write
  769. if (w1_reset_select_slave(sl))
  770. return -1;
  771. buf[cnt++] = CMD_COMPUTE_LOCK_SECRET;
  772. buf[cnt++] = (lock) ? (0xE0 | page_num) : page_num; // lock flag
  773. // Send command
  774. w1_write_block(sl->master, &buf[0], 2);
  775. // Read CRC
  776. w1_read_block(sl->master, &buf[cnt], 2);
  777. cnt += 2;
  778. // check CRC16
  779. slave_crc16 = 0;
  780. for (i = 0; i < cnt; i++)
  781. docrc16(buf[i]);
  782. if (slave_crc16 != 0xB001)
  783. return -1;
  784. // send release and strong pull-up
  785. buf[0] = CMD_RELEASE;
  786. w1_write_block(sl->master, &buf[0], 1);
  787. // now wait for the MAC computation.
  788. msleep(SHA_COMPUTATION_DELAY * 2 + SECRET_EEPROM_DELAY);
  789. // disable strong pullup
  790. // read the CS byte
  791. cs = w1_read_8(sl->master);
  792. if (cs == 0xAA)
  793. return 0;
  794. else
  795. return cs;
  796. }
  797. //--------------------------------------------------------------------------
  798. // Do Compute Page MAC command and return MAC. Optionally do
  799. // annonymous mode (anon != 0).
  800. //
  801. // Parameters
  802. // pbyte - parameter byte including page_num and anon
  803. // page_num - page number to read 0, 1
  804. // anon - Flag to indicate Annonymous mode if (anon != 0)
  805. // mac - 32 byte buffer for page data read
  806. //
  807. // Restrictions
  808. // The Read/Write Scratchpad command(w1_ds28el15_write_scratchpad) must have been issued
  809. // in write mode prior to Compute and Read Page MAC to define the challenge.
  810. //
  811. // Returns: 0 - page read has correct MAC
  812. // else - Failed to read page or incorrect MAC
  813. //
  814. int w1_ds28el15_compute_read_pageMAC(struct w1_slave *sl, int pbyte, uchar *mac)
  815. {
  816. uchar buf[256],cs;
  817. int cnt=0, i;
  818. if (!sl)
  819. return -ENODEV;
  820. // select device for write
  821. if (w1_reset_select_slave(sl))
  822. return -1;
  823. buf[cnt++] = CMD_COMPUTE_PAGEMAC;
  824. buf[cnt++] = pbyte;
  825. // Send command
  826. w1_write_block(sl->master, &buf[0], 2);
  827. // Read CRC
  828. w1_read_block(sl->master, &buf[cnt], 2);
  829. cnt += 2;
  830. // check CRC16
  831. slave_crc16 = 0;
  832. for (i = 0; i < cnt; i++)
  833. docrc16(buf[i]);
  834. if (slave_crc16 != 0xB001)
  835. return -2;
  836. // now wait for the MAC computation.
  837. msleep(SHA_COMPUTATION_DELAY * 2);
  838. // disable strong pullup
  839. // read the CS byte
  840. cs = w1_read_8(sl->master);
  841. if (cs != 0xAA)
  842. return -3;
  843. // read the MAC and CRC
  844. w1_read_block(sl->master, &buf[0], 34);
  845. // check CRC16
  846. slave_crc16 = 0;
  847. for (i = 0; i < 34; i++)
  848. docrc16(buf[i]);
  849. if (slave_crc16 != 0xB001)
  850. return -4;
  851. // copy MAC to return buffer
  852. memcpy(mac, buf, 32);
  853. return 0;
  854. }
  855. //--------------------------------------------------------------------------
  856. // Do Read Athenticated Page command and verify MAC. Optionally do
  857. // annonymous mode (anon != 0).
  858. //
  859. // Parameters
  860. // page_num - page number to read 0, 1
  861. // challange - 32 byte buffer containing the challenge
  862. // mac - 32 byte buffer for mac read
  863. // page_data - 32 byte buffer to contain the data to read
  864. // manid - 2 byte buffer containing the manufacturer ID (general device: 00h,00h)
  865. // skipread - Skip the read page and use the provided data in the 'page_data' buffer
  866. // anon - Flag to indicate Annonymous mode if (anon != 0)
  867. //
  868. // Returns: 0 - page read has correct MAC
  869. // else - Failed to read page or incorrect MAC
  870. //
  871. int w1_ds28el15_read_authverify(struct w1_slave *sl, int page_num, uchar *challenge, uchar *page_data, uchar *manid, int skipread, int anon)
  872. {
  873. uchar mac[32];
  874. uchar mt[128];
  875. int pbyte;
  876. int i = 0, rslt;
  877. if (!sl)
  878. return -ENODEV;
  879. if(w1_array[0] == 0) return 0;
  880. // check to see if we skip the read (use page_data)
  881. if (!skipread)
  882. {
  883. // read the page to get data
  884. while (i < RETRY_LIMIT) {
  885. rslt = w1_ds28el15_read_page(sl, page_num, page_data);
  886. if (rslt == 0)
  887. break;
  888. mdelay(10); /* wait 10ms */
  889. i++;
  890. }
  891. if (i > RETRY_LIMIT) {
  892. printk(KERN_ERR "w1_ds28el15_read_authverify err : -1F\n");
  893. return -1;
  894. }
  895. i = 0;
  896. }
  897. // The Read/Write Scratch pad command must have been issued in write mode prior
  898. // to Compute and Read Page MAC to define the challenge
  899. while (i < RETRY_LIMIT) {
  900. rslt = w1_ds28el15_write_scratchpad(sl, challenge);
  901. if (rslt == 0)
  902. break;
  903. mdelay(10); /* wait 10ms */
  904. i++;
  905. }
  906. if (i > RETRY_LIMIT) {
  907. printk(KERN_ERR "w1_ds28el15_read_authverify err : -2F\n");
  908. return -2;
  909. }
  910. i = 0;
  911. // have device compute mac
  912. pbyte = anon ? 0xE0 : 0x00;
  913. pbyte = pbyte | page_num;
  914. while (i < RETRY_LIMIT) {
  915. rslt = w1_ds28el15_compute_read_pageMAC(sl, pbyte, mac);
  916. if (rslt == 0)
  917. break;
  918. mdelay(10); /* wait 10ms */
  919. i++;
  920. }
  921. if (i > RETRY_LIMIT) {
  922. printk(KERN_ERR "w1_ds28el15_read_authverify err : -3\n");
  923. return -3;
  924. }
  925. // create buffer to compute and verify mac
  926. // clear
  927. memset(mt,0,128);
  928. // insert page data
  929. memcpy(&mt[0],page_data,32);
  930. // insert challenge
  931. memcpy(&mt[32],challenge,32);
  932. // insert ROM number or FF
  933. if (anon)
  934. memset(&mt[96],0xFF,8);
  935. else
  936. memcpy(&mt[96],rom_no,8);
  937. mt[106] = page_num;
  938. if (special_mode)
  939. {
  940. mt[105] = special_values[0];
  941. mt[104] = special_values[1];
  942. }
  943. else
  944. {
  945. mt[105] = manid[0];
  946. mt[104] = manid[1];
  947. }
  948. if (verify_mac256(mt, 119, mac) ==0) {
  949. printk(KERN_ERR "w1_ds28el15_compute_read_pageMAC err : -4\n");
  950. return -4;
  951. } else
  952. return 0;
  953. }
  954. //--------------------------------------------------------------------------
  955. // Verify provided MAC and page data. Optionally do
  956. // annonymous mode (anon != 0).
  957. //
  958. // Parameters
  959. // page_num - page number to read 0 - 16
  960. // challange - 32 byte buffer containing the challenge
  961. // page_data - 32 byte buffer to contain the data read
  962. // manid - 2 byte buffer containing the manufacturer ID (general device: 00h,00h)
  963. // mac - 32 byte buffer of mac read
  964. // anon - Flag to indicate Annonymous mode if (anon != 0)
  965. //
  966. // Returns: 0 - page read has correct MAC
  967. // else - Failed to read page or incorrect MAC
  968. //
  969. int w1_ds28el15_authverify(struct w1_slave *sl, int page_num, uchar *challenge, uchar *page_data, uchar *manid, uchar *mac, int anon)
  970. {
  971. uchar mt[128];
  972. // create buffer to compute and verify mac
  973. // clear
  974. memset(mt,0,128);
  975. // insert page data
  976. memcpy(&mt[0],page_data,32);
  977. // insert challenge
  978. memcpy(&mt[32],challenge,32);
  979. // insert ROM number or FF
  980. if (anon)
  981. memset(&mt[96],0xFF,8);
  982. else
  983. memcpy(&mt[96],rom_no,8);
  984. mt[106] = page_num;
  985. if (special_mode)
  986. {
  987. mt[105] = special_values[0];
  988. mt[104] = special_values[1];
  989. }
  990. else
  991. {
  992. mt[105] = manid[0];
  993. mt[104] = manid[1];
  994. }
  995. if (verify_mac256(mt, 119, mac) == 0)
  996. return -1;
  997. else
  998. return 0;
  999. }
  1000. //--------------------------------------------------------------------------
  1001. // Read status bytes, either personality or page protection.
  1002. //
  1003. // Parameters
  1004. // pbyte - include personality, allpages, and page_num
  1005. // personality - flag to indicate the read is the 4 personality bytes (1)
  1006. // or page page protection (0)
  1007. // allpages - flag to indicate if just one page (0) or all (1) page protection
  1008. // bytes.
  1009. // block_num - block number if reading protection 0 to 3
  1010. // rdbuf - 16 byte buffer personality bytes (length 4) or page protection
  1011. // (length 1 or 16)
  1012. //
  1013. // Returns: 0 - status read
  1014. // else - Failed to read status
  1015. //
  1016. int w1_ds28el15_read_status(struct w1_slave *sl, int pbyte, uchar *rdbuf)
  1017. {
  1018. uchar buf[256];
  1019. int cnt, i, offset,rdnum;
  1020. int personality, allpages, block_num;
  1021. if (!sl)
  1022. return -ENODEV;
  1023. personality = (pbyte & 0xE0) ? 1 : 0;
  1024. allpages = (pbyte == 0) ? 1 : 0;
  1025. block_num = pbyte & 0x03;
  1026. cnt = 0;
  1027. offset = 0;
  1028. if (w1_reset_select_slave(sl)){
  1029. pr_info("%s reset_select_slave error", __func__);
  1030. return -1;
  1031. }
  1032. buf[cnt++] = CMD_READ_STATUS;
  1033. if (personality)
  1034. buf[cnt++] = 0xE0;
  1035. else if (!allpages)
  1036. buf[cnt++] = block_num;
  1037. else
  1038. buf[cnt++] = 0;
  1039. // send the command
  1040. w1_write_block(sl->master, &buf[0], 2);
  1041. offset = cnt + 2;
  1042. // adjust data length
  1043. if ((personality) || (allpages))
  1044. rdnum = 8;
  1045. else
  1046. rdnum = 5;
  1047. // Read the bytes
  1048. w1_read_block(sl->master, &buf[cnt], rdnum);
  1049. cnt += rdnum;
  1050. // check the first CRC16
  1051. slave_crc16 = 0;
  1052. for (i = 0; i < offset; i++)
  1053. docrc16(buf[i]);
  1054. if (slave_crc16 != 0xB001){
  1055. pr_info("%s slave_crc16 is error 1.\n", __func__);
  1056. return -1;
  1057. }
  1058. if ((personality || allpages || (block_num == 1)))
  1059. {
  1060. // check the second CRC16
  1061. slave_crc16 = 0;
  1062. for (i = offset; i < cnt; i++)
  1063. docrc16(buf[i]);
  1064. if (slave_crc16 != 0xB001){
  1065. pr_info("%s slave_crc16 is error 2.\n", __func__);
  1066. return -1;
  1067. }
  1068. }
  1069. // copy the data to the read buffer
  1070. memcpy(rdbuf,&buf[offset],rdnum-4);
  1071. return 0;
  1072. }
  1073. //--------------------------------------------------------------------------
  1074. // Write page protection byte.
  1075. //
  1076. // Parameters
  1077. // block - block number (0 to 3) which covers two pages each
  1078. // prot - protection byte
  1079. //
  1080. // Returns: 0 - protection written
  1081. // else - Failed to set protection
  1082. //
  1083. int w1_ds28el15_write_blockprotection(struct w1_slave *sl, uchar prot)
  1084. {
  1085. uchar buf[256],cs;
  1086. int cnt=0, i;
  1087. if (!sl)
  1088. return -ENODEV;
  1089. // select device for write
  1090. if (w1_reset_select_slave(sl))
  1091. return -1;
  1092. buf[cnt++] = CMD_WRITE_BLOCK_PROTECT;
  1093. // compute parameter byte
  1094. buf[cnt++] = prot;
  1095. w1_write_block(sl->master, &buf[0], cnt);
  1096. // Read CRC
  1097. w1_read_block(sl->master, &buf[cnt], 2);
  1098. cnt += 2;
  1099. // check CRC16
  1100. slave_crc16 = 0;
  1101. for (i = 0; i < cnt; i++)
  1102. docrc16(buf[i]);
  1103. if (slave_crc16 != 0xB001)
  1104. return -1;
  1105. // sent release
  1106. // now wait for programming
  1107. msleep(EEPROM_WRITE_DELAY);
  1108. // disable strong pullup
  1109. // read the CS byte
  1110. cs = w1_read_8(sl->master);
  1111. if (cs == 0xAA)
  1112. return 0;
  1113. else
  1114. return cs;
  1115. }
  1116. //--------------------------------------------------------------------------
  1117. // Write page protection byte.
  1118. //
  1119. // Parameters
  1120. // data - input data for Authenticated Write Block Protection
  1121. // new_value - new protection byte(parameter byte) including block num
  1122. // old_value - old protection byte(parameter byte)
  1123. // manid - manufacturer ID
  1124. //
  1125. // Returns: 0 - protection written
  1126. // else - Failed to set protection
  1127. //
  1128. int w1_ds28el15_write_authblockprotection(struct w1_slave *sl, uchar *data)
  1129. {
  1130. uchar buf[256],cs,mt[64];
  1131. int cnt=0, i;
  1132. int new_value, old_value;
  1133. uchar manid[2];
  1134. if (!sl)
  1135. return -ENODEV;
  1136. new_value = data[0];
  1137. old_value = data[1];
  1138. manid[0] = data[2];
  1139. manid[1] = data[3];
  1140. // select device for write
  1141. if (w1_reset_select_slave(sl))
  1142. return -1;
  1143. buf[cnt++] = CMD_WRITE_AUTH_PROTECT;
  1144. buf[cnt++] = new_value;
  1145. // Send command
  1146. w1_write_block(sl->master, &buf[0], 2);
  1147. // read first CRC
  1148. w1_read_block(sl->master, &buf[cnt], 2);
  1149. // now wait for the MAC computation.
  1150. msleep(SHA_COMPUTATION_DELAY);
  1151. // disable strong pullup
  1152. // check CRC16
  1153. slave_crc16 = 0;
  1154. for (i = 0; i < cnt; i++)
  1155. docrc16(buf[i]);
  1156. if (slave_crc16 != 0xB001)
  1157. return -1;
  1158. // calculate MAC
  1159. // clear
  1160. memset(mt,0,64);
  1161. // insert ROM number
  1162. memcpy(&mt[32],rom_no,8);
  1163. // instert block and page
  1164. mt[43] = 0;
  1165. mt[42] = new_value & 0x0F;
  1166. // check on special mode
  1167. if (special_mode)
  1168. {
  1169. mt[41] = special_values[0];
  1170. mt[40] = special_values[1];
  1171. }
  1172. else
  1173. {
  1174. mt[41] = manid[0];
  1175. mt[40] = manid[1];
  1176. }
  1177. // old data
  1178. mt[44] = (old_value & PROT_BIT_AUTHWRITE) ? 0x01 : 0x00;
  1179. mt[45] = (old_value & PROT_BIT_EPROM) ? 0x01 : 0x00;
  1180. mt[46] = (old_value & PROT_BIT_WRITE) ? 0x01 : 0x00;
  1181. mt[47] = (old_value & PROT_BIT_READ) ? 0x01 : 0x00;
  1182. // new data
  1183. mt[48] = (new_value & PROT_BIT_AUTHWRITE) ? 0x01 : 0x00;
  1184. mt[49] = (new_value & PROT_BIT_EPROM) ? 0x01 : 0x00;
  1185. mt[50] = (new_value & PROT_BIT_WRITE) ? 0x01 : 0x00;
  1186. mt[51] = (new_value & PROT_BIT_READ) ? 0x01 : 0x00;
  1187. // compute the mac
  1188. compute_mac256(mt, 55, &buf[0]);
  1189. cnt = 32;
  1190. // send the MAC
  1191. w1_write_block(sl->master, &buf[0], 32);
  1192. // Read CRC and CS byte
  1193. w1_read_block(sl->master, &buf[cnt], 3);
  1194. cnt += 3;
  1195. // ckeck CRC16
  1196. slave_crc16 = 0;
  1197. for (i = 0; i < (cnt-1); i++)
  1198. docrc16(buf[i]);
  1199. if (slave_crc16 != 0xB001)
  1200. return -1;
  1201. // check CS
  1202. if (buf[cnt-1] != 0xAA)
  1203. return -1;
  1204. // send release and strong pull-up
  1205. // DATASHEET_CORRECTION - last bit in release is a read-zero so don't check echo of write byte
  1206. w1_write_8(sl->master, 0xAA);
  1207. // now wait for the MAC computation.
  1208. msleep(EEPROM_WRITE_DELAY);
  1209. // disable strong pullup
  1210. // read the CS byte
  1211. cs = w1_read_8(sl->master);
  1212. if (cs == 0xAA)
  1213. return 0;
  1214. else
  1215. return cs;
  1216. }
  1217. //-------------------------------------------------------------------------
  1218. // get_array_value
  1219. //
  1220. #ifdef CONFIG_OF_SUBCMDLINE_PARSE
  1221. static int get_array_value(void)
  1222. {
  1223. int i, ret;
  1224. char str[20], *buf, data;
  1225. pr_info("%s: W1 Read Array.\n", __func__);
  1226. ret = of_parse_args_on_subcmdline("array=",(char *)str);
  1227. if (ret) {
  1228. pr_err("%s: W1 Read Array Error\n", __func__);
  1229. return -1;
  1230. }
  1231. buf = str;
  1232. for(i=0;i<5;i++){
  1233. strncpy(&data,&buf[i*2],2);
  1234. sscanf(&data, "%x", (unsigned int *)&ret);
  1235. w1_array[i]=ret;
  1236. }
  1237. return 0;
  1238. }
  1239. #else
  1240. static int __init get_array_value(char *str)
  1241. {
  1242. int i, get[6];
  1243. pr_info("%s: W1 Read Array\n", __func__);
  1244. for(i=0;i<5;i++){
  1245. sscanf(str, "%x", &get[i]);
  1246. w1_array[i]=get[i];
  1247. str=str+3;
  1248. }
  1249. return 0;
  1250. }
  1251. __setup("array=", get_array_value);
  1252. #endif
  1253. //--------------------------------------------------------------------------
  1254. // w1_ds28el15_verifymac
  1255. //
  1256. // compare two mac data, Device mac and calculated mac and
  1257. // returned the result
  1258. // Returns: 0 - Success, have the same mac data in both of Device and AP.
  1259. // else - Failed, have the different mac data or verifying sequnce failed.
  1260. //
  1261. int w1_ds28el15_verifymac(struct w1_slave *sl)
  1262. {
  1263. int rslt,rt;
  1264. uchar buf[256], challenge[32], manid[2];
  1265. uchar memimage[512];
  1266. uchar master_secret[32];
  1267. #if 1
  1268. int i = 0;
  1269. #endif
  1270. // copy the secret code
  1271. memcpy(master_secret, w1_array, 32); // Set temp_secret to the master secret
  1272. rt = 0;
  1273. if(w1_array[0] == 0) {
  1274. pr_info("%s: w1_array[0] == 0\n", __func__);
  1275. goto success;
  1276. }
  1277. // Store the master secret and romid.
  1278. set_secret(master_secret);
  1279. set_romid(rom_no);
  1280. // read personality bytes to get manufacturer ID
  1281. #if 1
  1282. while (i < RETRY_LIMIT) {
  1283. rslt = w1_ds28el15_read_status(sl, 0xE0, buf);
  1284. if (rslt == 0)
  1285. break;
  1286. mdelay(10); /* wait 10ms */
  1287. i++;
  1288. }
  1289. i = 0;
  1290. #else
  1291. rslt = w1_ds28el15_read_status(sl, 0xE0, buf);
  1292. #endif
  1293. if (rslt == 0)
  1294. {
  1295. manid[0] = buf[3];
  1296. manid[1] = buf[2];
  1297. } else {
  1298. pr_info("%s : read_status error\n", __func__);
  1299. rt = -1;
  1300. goto success;
  1301. }
  1302. // if you want to use random value, insert code here.
  1303. get_random_bytes(challenge, 32); // random challenge
  1304. // Enhance verification status
  1305. while (i < RETRY_LIMIT) {
  1306. rslt = w1_ds28el15_read_authverify(sl, 0, challenge,
  1307. &memimage[0], manid, 0, 0);
  1308. if (rslt == 0)
  1309. break;
  1310. mdelay(10); /* wait 10ms */
  1311. i++;
  1312. }
  1313. printk(KERN_ERR "%s result : %d\n",__func__, rslt);
  1314. if (rslt){
  1315. pr_info("%s: read_authverify error\n", __func__);
  1316. rt = -1;
  1317. }
  1318. success:
  1319. return rt;
  1320. }
  1321. static int w1_ds28el15_setup_device(struct w1_slave *sl)
  1322. {
  1323. int rslt,rt;
  1324. uchar buf[256], challenge[32], manid[2];
  1325. uchar memimage[512];
  1326. uchar master_secret[32];
  1327. // hard code master secret
  1328. memcpy(master_secret, w1_array, 32); // Set temp_secret to the master secret
  1329. // ----- DS28EL25/DS28EL22/DS28EL15 Setup
  1330. printk(KERN_ERR "-------- DS28EL15 Setup Example\n");
  1331. rt = 0;
  1332. printk(KERN_ERR "Write scratchpad - master secret\n");
  1333. rslt = w1_ds28el15_write_scratchpad(sl, master_secret);
  1334. printk(KERN_ERR "result : %d\n", rslt);
  1335. if (rslt) rt = -1;
  1336. printk(KERN_ERR "Load the master secret\n");
  1337. rslt = w1_ds28el15_load_secret(sl, 0);
  1338. printk(KERN_ERR "result : %d\n",rslt);
  1339. if (rslt) rt = -1;
  1340. printk(KERN_ERR "Set the master secret in the Software SHA-256\n");
  1341. set_secret(master_secret);
  1342. set_romid(rom_no);
  1343. // fill image with random data to write
  1344. // read personality bytes to get manufacturer ID
  1345. printk(KERN_ERR " w1_ds28el15_read_status(\n");
  1346. rslt = w1_ds28el15_read_status(sl, 0xE0, buf);
  1347. if (rslt == 0) {
  1348. manid[0] = buf[3];
  1349. manid[1] = buf[2];
  1350. }
  1351. else{
  1352. rt = -1;
  1353. goto end;
  1354. }
  1355. printk(KERN_ERR "result : %d\n",rslt);
  1356. printk(KERN_ERR "Read-Authenticate with unique secret\n");
  1357. get_random_bytes(challenge, 32); // random challenge
  1358. rslt = w1_ds28el15_read_authverify(sl, 0, challenge, &memimage[0], manid, 0, 0);
  1359. printk(KERN_ERR "result : %d\n",rslt);
  1360. if (rslt) rt = -1;
  1361. printk(KERN_ERR "DS28EL15 Setup Example: %s\n",(rt) ? "FAIL" : "SUCCESS");
  1362. printk(KERN_ERR "--------------------------------------------------\n");
  1363. end:
  1364. return rt;
  1365. }
  1366. #if 0
  1367. static int w1_ds28el15_application(struct w1_slave *sl)
  1368. {
  1369. int i,rslt,rt;
  1370. uchar buf[256], challenge[32], manid[2], new_page[32];
  1371. uchar memimage[512];
  1372. uchar master_secret[32];
  1373. // ----- DS28EL25/DS28EL22/DS28EL15 Application Example
  1374. printk(KERN_ERR "\n-------- DS28EL15 Application Example\n");
  1375. // hard code master secret
  1376. for (i = 0; i < 32; i++)
  1377. master_secret[i] = w1_array[i]; // Set master secret to temp_secret
  1378. rt = 0;
  1379. printk(KERN_ERR "Set the master secret in the Software SHA-256\n");
  1380. set_secret(master_secret);
  1381. set_romid(rom_no);
  1382. // read personality bytes to get manufacturer ID
  1383. rslt = w1_ds28el15_read_status(sl, 0xE0, buf);
  1384. if (rslt == 0)
  1385. {
  1386. manid[0] = buf[3];
  1387. manid[1] = buf[2];
  1388. }
  1389. else
  1390. rt = -1;
  1391. printk(KERN_ERR "result : %d\n",rslt);
  1392. printk(KERN_ERR "Read-Authenticated page 0\n");
  1393. get_random_bytes(challenge, 32); // random challenge
  1394. rslt = w1_ds28el15_read_authverify(sl, 0, challenge, &memimage[0], manid, 0, 0);
  1395. printk(KERN_ERR "result : %d\n",rslt);
  1396. if (rslt) rt = -1;
  1397. // change data block 0 by inverting the current data
  1398. printk(KERN_ERR "Old data: ");
  1399. for (i = 0; i < 4; i++)
  1400. printk(KERN_ERR "%02X ",memimage[i]);
  1401. printk(KERN_ERR "\nNew data: ");
  1402. for (i = 0; i < 4; i++)
  1403. {
  1404. new_page[i] = ~memimage[i];
  1405. printk(KERN_ERR "%02X ",new_page[i]);
  1406. }
  1407. // Write new block
  1408. printk(KERN_ERR "\nWrite block at address 0000h\n");
  1409. memset(&buf[0], 0x00, 10); // use 10bytes(4+4+2) for data
  1410. memcpy(&buf[0], &new_page[0], 4);
  1411. memcpy(&buf[4], &memimage[0], 4);
  1412. memcpy(&buf[8], manid, 2);
  1413. rslt = w1_ds28el15_write_authblock(sl, 0, 0, buf, 0);
  1414. printk(KERN_ERR "result : %d\n",rslt);
  1415. if (rslt) rt = -1;
  1416. memcpy(&memimage[0], &new_page[0], 4);
  1417. // verify memory is correct by doing just a compute
  1418. printk(KERN_ERR "Read-Authenticate page 0 after write\n");
  1419. get_random_bytes(challenge, 32); // random challenge
  1420. rslt = w1_ds28el15_read_authverify(sl, 0, challenge, &memimage[0], manid, 1, 0);
  1421. printk(KERN_ERR "result : %d\n",rslt);
  1422. if (rslt) rt = -1;
  1423. printk(KERN_ERR "DS28EL15 Application Example: %s\n",(rt) ? "FAIL" : "SUCCESS");
  1424. printk(KERN_ERR "--------------------------------------------------------\n");
  1425. return rt;
  1426. }
  1427. #endif
  1428. static ssize_t w1_ds28el15_read_user_eeprom(struct device *device,
  1429. struct device_attribute *attr, char *buf);
  1430. static struct device_attribute w1_read_user_eeprom_attr =
  1431. __ATTR(read_eeprom, S_IRUGO, w1_ds28el15_read_user_eeprom, NULL);
  1432. // read 64 bytes user eeprom data
  1433. static ssize_t w1_ds28el15_read_user_eeprom(struct device *device,
  1434. struct device_attribute *attr, char *buf)
  1435. {
  1436. struct w1_slave *sl = dev_to_w1_slave(device);
  1437. u8 rdbuf[64];
  1438. int result, i, page_num;
  1439. ssize_t c = PAGE_SIZE;
  1440. // read page 0
  1441. page_num=0;
  1442. c -= snprintf(buf + PAGE_SIZE - c, c, "page %d read\n", page_num);
  1443. result = w1_ds28el15_read_page(sl, page_num, &rdbuf[0]);
  1444. if (result != 0) {
  1445. c -= snprintf(buf + PAGE_SIZE - c, c, "\npage %d read fail\n", page_num);
  1446. return 0;
  1447. }
  1448. for (i = 0; i < 32; ++i)
  1449. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rdbuf[i]);
  1450. c -= snprintf(buf + PAGE_SIZE - c, c, "\npage %d read done\n", page_num);
  1451. // read page 1
  1452. page_num=1;
  1453. result = w1_ds28el15_read_page(sl, page_num, &rdbuf[32]);
  1454. if (result != 0) {
  1455. c -= snprintf(buf + PAGE_SIZE - c, c, "\npage %d read fail\n", page_num);
  1456. return 0;
  1457. }
  1458. for (i = 32; i < 64; ++i)
  1459. c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rdbuf[i]);
  1460. c -= snprintf(buf + PAGE_SIZE - c, c, "\npage %d read done\n", page_num);
  1461. return PAGE_SIZE - c;
  1462. }
  1463. static ssize_t w1_ds28el15_check_color(struct device *device,
  1464. struct device_attribute *attr, char *buf);
  1465. static struct device_attribute w1_check_color_attr =
  1466. __ATTR(check_color, S_IRUGO, w1_ds28el15_check_color, NULL);
  1467. // check COLOR for cover
  1468. static ssize_t w1_ds28el15_check_color(struct device *device,
  1469. struct device_attribute *attr, char *buf)
  1470. {
  1471. // read cover color
  1472. return sprintf(buf, "%d\n", color);
  1473. }
  1474. #ifdef CONFIG_W1_CF
  1475. static ssize_t w1_ds28el15_cf(struct device *device,
  1476. struct device_attribute *attr, char *buf);
  1477. static struct device_attribute w1_cf_attr =
  1478. __ATTR(cf, S_IRUGO, w1_ds28el15_cf, NULL);
  1479. // check cf_node cover or not
  1480. static ssize_t w1_ds28el15_cf(struct device *device,
  1481. struct device_attribute *attr, char *buf)
  1482. {
  1483. // read mem
  1484. return sprintf(buf, "%d\n", cf_node);
  1485. }
  1486. #endif
  1487. static ssize_t w1_ds28el15_check_id(struct device *device,
  1488. struct device_attribute *attr, char *buf);
  1489. static struct device_attribute w1_check_id_attr =
  1490. __ATTR(check_id, S_IRUGO, w1_ds28el15_check_id, NULL);
  1491. // check ID for cover
  1492. static ssize_t w1_ds28el15_check_id(struct device *device,
  1493. struct device_attribute *attr, char *buf)
  1494. {
  1495. // read cover id
  1496. return sprintf(buf, "%d\n", id);
  1497. }
  1498. static ssize_t w1_ds28el15_verify_mac(struct device *device,
  1499. struct device_attribute *attr, char *buf);
  1500. static struct device_attribute w1_verifymac_attr =
  1501. __ATTR(verify_mac, S_IRUGO, w1_ds28el15_verify_mac, NULL);
  1502. // verified mac value between the device and AP
  1503. static ssize_t w1_ds28el15_verify_mac(struct device *device,
  1504. struct device_attribute *attr, char *buf)
  1505. {
  1506. struct w1_slave *sl = dev_to_w1_slave(device);
  1507. int result;
  1508. // verify mac
  1509. result = w1_ds28el15_verifymac(sl);
  1510. return sprintf(buf, "%d\n", result);
  1511. }
  1512. static int w1_ds28el15_get_buffer(struct w1_slave *sl, uchar *rdbuf, int retry_limit)
  1513. {
  1514. int ret = -1, retry = 0;
  1515. while((ret != 0) && (retry < retry_limit)) {
  1516. ret = w1_ds28el15_read_page(sl, 0, &rdbuf[0]);
  1517. if(ret != 0 )
  1518. pr_info("%s : error %d\n", __func__, ret);
  1519. retry++;
  1520. }
  1521. return ret;
  1522. }
  1523. #ifdef CONFIG_W1_SN
  1524. static const int sn_cdigit[19] = {
  1525. 0x0e, 0x0d, 0x1f, 0x0b, 0x1c,
  1526. 0x12, 0x0f, 0x1e, 0x0a, 0x13,
  1527. 0x14, 0x15, 0x19, 0x16, 0x17,
  1528. 0x20, 0x1b, 0x1d, 0x11};
  1529. static bool w1_ds28el15_check_digit(const uchar *sn)
  1530. {
  1531. int i, tmp1 = 0, tmp2 = 0;
  1532. int cdigit = sn[3];
  1533. if (cdigit == 0x1e)
  1534. return true;
  1535. for (i=4;i<10;i++)
  1536. tmp1 += sn[i];
  1537. tmp1 += sn[4]*5;
  1538. tmp2 = (tmp1 * sn[9] * sn[13]) % 19;
  1539. tmp1 = (sn[10] + sn[12]) * 3 + (sn[11] + sn[13]) * 6 + 14;
  1540. if (cdigit == sn_cdigit[((tmp1 + tmp2) % 19)])
  1541. return true;
  1542. else
  1543. return false;
  1544. }
  1545. static uchar w1_ds28el15_char_convert(uchar c)
  1546. {
  1547. char ctable[36] = {
  1548. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  1549. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
  1550. 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W',
  1551. 'X', 'Y', 'Z', 'I', 'O', 'U'};
  1552. return ctable[c];
  1553. }
  1554. static void w1_ds28el15_slave_sn(const uchar *rdbuf)
  1555. {
  1556. int i;
  1557. u8 sn[15];
  1558. sn[14] = 0;
  1559. if (w1_ds28el15_check_digit(&rdbuf[4])) {
  1560. for (i = 0 ; i < 14 ; i++)
  1561. sn[i] = w1_ds28el15_char_convert(rdbuf[i+4]);
  1562. pr_info("%s: %s\n", __func__, sn);
  1563. for (i = 0 ; i < 14 ; i++)
  1564. g_sn[i] = sn[13 - i];
  1565. } else {
  1566. for (i = 0 ; i < 14 ; i++)
  1567. sn[i] = w1_ds28el15_char_convert(rdbuf[i+4]);
  1568. pr_info("%s: sn is not good %s\n", __func__, sn);
  1569. }
  1570. }
  1571. #endif
  1572. static void w1_ds28el15_update_slave_info(struct w1_slave *sl) {
  1573. u8 rdbuf[32];
  1574. int ret, retry = 0;
  1575. ret = w1_ds28el15_get_buffer(sl, &rdbuf[0], 10);
  1576. if(ret != 0)
  1577. pr_info("%s : fail to get buffer %d\n", __func__, ret);
  1578. while((rdbuf[0] < sl->id_min) || (rdbuf[0] > sl->id_max) ||
  1579. (rdbuf[1] < sl->color_min) || (rdbuf[1] > sl->color_max)) {
  1580. printk(KERN_ERR "%s: out of range : %d %d \n", __func__, rdbuf[0], rdbuf[1]);
  1581. if(retry > 10) {
  1582. printk(KERN_ERR "%s: out of range over 10 times.\n", __func__);
  1583. pr_info("%s: Change ID(%d) & Color(%d) to Default Value(%d, %d)",
  1584. __func__, rdbuf[0], rdbuf[1], sl->id_default, sl->color_default);
  1585. rdbuf[0] = sl->id_default;
  1586. rdbuf[1] = sl->color_default;
  1587. break;
  1588. }
  1589. ret = w1_ds28el15_get_buffer(sl, &rdbuf[0], 10);
  1590. if(ret != 0)
  1591. pr_info("%s : fail to get buffer %d\n", __func__, ret);
  1592. retry++;
  1593. }
  1594. //read page 0, ID is 0 ~ 7 bit(rdbuf[0])
  1595. id = rdbuf[0];
  1596. // read page 0, COLOR is 8 ~ 15 bit(rdbuf[1])
  1597. color = rdbuf[1];
  1598. pr_info("%s Read ID(%d) & Color(%d) & Verification State(%d)\n",
  1599. __func__, id, color, verification);
  1600. #ifdef CONFIG_W1_SN
  1601. w1_ds28el15_slave_sn(&rdbuf[0]);
  1602. #endif
  1603. }
  1604. static int w1_parse_dt(struct w1_slave *sl)
  1605. {
  1606. struct device_node* np;
  1607. np = of_find_node_by_path("/soc/ds28el15");
  1608. if (!np) {
  1609. pr_err("%s: get ds28el15 node failed\n", __func__);
  1610. return -ENODEV;
  1611. }
  1612. of_property_read_u32(np, "ds28el15,id-min", &sl->id_min);
  1613. of_property_read_u32(np, "ds28el15,id-max", &sl->id_max);
  1614. of_property_read_u32(np, "ds28el15,id-default", &sl->id_default);
  1615. of_property_read_u32(np, "ds28el15,color-min", &sl->color_min);
  1616. of_property_read_u32(np, "ds28el15,color-max", &sl->color_max);
  1617. of_property_read_u32(np, "ds28el15,color-default", &sl->color_default);
  1618. pr_info("%s : id min[%d] max[%d] default[%d]\n", __func__,
  1619. sl->id_min, sl->id_max, sl->id_default);
  1620. pr_info("%s : color min[%d] max[%d] default[%d]\n", __func__,
  1621. sl->color_min, sl->color_max, sl->color_default);
  1622. return 0;
  1623. }
  1624. static void w1_set_default_range(struct w1_slave *sl)
  1625. {
  1626. pr_info("%s : get default range\n", __func__);
  1627. sl->id_min = ID_MIN;
  1628. sl->id_max = ID_MAX;
  1629. sl->id_default = ID_DEFAULT;
  1630. sl->color_min = CO_MIN;
  1631. sl->color_max = CO_MAX;
  1632. sl->color_default = CO_DEFAULT;
  1633. pr_info("%s : id min[%d] max[%d] default[%d]\n", __func__,
  1634. sl->id_min, sl->id_max, sl->id_default);
  1635. pr_info("%s : color min[%d] max[%d] default[%d]\n", __func__,
  1636. sl->color_min, sl->color_max, sl->color_default);
  1637. }
  1638. static int w1_ds28el15_add_slave(struct w1_slave *sl)
  1639. {
  1640. int err = 0;
  1641. #ifdef CONFIG_W1_CF
  1642. int count = 0, rst = 0;
  1643. u8 rdbuf[32];
  1644. #endif
  1645. printk(KERN_ERR "\nw1_ds28el15_add_slave start\n");
  1646. err = w1_parse_dt(sl);
  1647. if (err) {
  1648. printk(KERN_ERR "%s: w1_parse_dt error\n", __func__);
  1649. w1_set_default_range(sl);
  1650. }
  1651. #ifdef CONFIG_OF_SUBCMDLINE_PARSE
  1652. err = get_array_value();
  1653. if (err) {
  1654. printk(KERN_ERR "%s: w1_get_array_value error\n", __func__);
  1655. }
  1656. #endif
  1657. err = device_create_file(&sl->dev, &w1_read_user_eeprom_attr);
  1658. if (err) {
  1659. device_remove_file(&sl->dev, &w1_read_user_eeprom_attr);
  1660. printk(KERN_ERR "%s: w1_read_user_eeprom_attr error\n", __func__);
  1661. return err;
  1662. }
  1663. err = device_create_file(&sl->dev, &w1_verifymac_attr);
  1664. if (err) {
  1665. device_remove_file(&sl->dev, &w1_verifymac_attr);
  1666. printk(KERN_ERR "%s: w1_verifymac_attr error\n", __func__);
  1667. return err;
  1668. }
  1669. #ifdef CONFIG_W1_CF
  1670. err = device_create_file(&sl->dev, &w1_cf_attr);
  1671. if (err) {
  1672. device_remove_file(&sl->dev, &w1_cf_attr);
  1673. printk(KERN_ERR "%s: w1_cf_attr error\n", __func__);
  1674. return err;
  1675. }
  1676. #endif
  1677. err = device_create_file(&sl->dev, &w1_check_id_attr);
  1678. if (err) {
  1679. device_remove_file(&sl->dev, &w1_check_id_attr);
  1680. printk(KERN_ERR "%s: w1_check_id_attr error\n", __func__);
  1681. return err;
  1682. }
  1683. err = device_create_file(&sl->dev, &w1_check_color_attr);
  1684. if (err) {
  1685. device_remove_file(&sl->dev, &w1_check_color_attr);
  1686. printk(KERN_ERR "%s: w1_check_color_attr error\n", __func__);
  1687. return err;
  1688. }
  1689. // copy rom id to use mac calculation
  1690. memcpy(rom_no, (u8 *)&sl->reg_num, sizeof(sl->reg_num));
  1691. if (init_verify) {
  1692. if (skip_setup == 0) {
  1693. err = w1_ds28el15_setup_device(sl);
  1694. printk(KERN_ERR "w1_ds28el15_setup_device\n");
  1695. skip_setup = 1;
  1696. err = w1_ds28el15_verifymac(sl);
  1697. verification = err;
  1698. } else {
  1699. err = w1_ds28el15_verifymac(sl);
  1700. verification = err;
  1701. printk(KERN_ERR "w1_ds28el15_verifymac\n");
  1702. }
  1703. }
  1704. #ifdef CONFIG_SEC_H_PROJECT
  1705. pr_info("%s:verified(%d)", __func__, verified);
  1706. if(!verified)
  1707. #else
  1708. if(!verification)
  1709. #endif
  1710. {
  1711. #ifdef CONFIG_W1_CF
  1712. while (count < RETRY_LIMIT_CF) {
  1713. rst = w1_ds28el15_read_memory_check(sl, 0, 0, rdbuf, 32);
  1714. if (rst == 0)
  1715. break;
  1716. count++;
  1717. }
  1718. if (rst == -2)
  1719. cf_node = 1;
  1720. else
  1721. cf_node = 0;
  1722. pr_info("%s:COVER CLASS(%d)\n", __func__, cf_node);
  1723. #endif
  1724. #if defined(CONFIG_SEC_H_PROJECT)
  1725. w1_ds28el15_update_slave_info(sl);
  1726. #else
  1727. w1_ds28el15_update_slave_info(sl);
  1728. pr_info("%s:uevent send 1\n", __func__);
  1729. input_report_switch(sl->master->bus_master->input, SW_W1, 1);
  1730. input_sync(sl->master->bus_master->input);
  1731. #endif
  1732. }
  1733. printk(KERN_ERR "w1_ds28el15_add_slave end, skip_setup=%d, err=%d\n", skip_setup, err);
  1734. return err;
  1735. }
  1736. static void w1_ds28el15_remove_slave(struct w1_slave *sl)
  1737. {
  1738. device_remove_file(&sl->dev, &w1_read_user_eeprom_attr);
  1739. device_remove_file(&sl->dev, &w1_verifymac_attr);
  1740. #ifdef CONFIG_W1_CF
  1741. device_remove_file(&sl->dev, &w1_cf_attr);
  1742. #endif
  1743. device_remove_file(&sl->dev, &w1_check_id_attr);
  1744. device_remove_file(&sl->dev, &w1_check_color_attr);
  1745. verification = -1;
  1746. printk(KERN_ERR "\nw1_ds28el15_remove_slave\n");
  1747. }
  1748. static struct w1_family_ops w1_ds28el15_fops = {
  1749. .add_slave = w1_ds28el15_add_slave,
  1750. .remove_slave = w1_ds28el15_remove_slave,
  1751. };
  1752. static struct w1_family w1_ds28el15_family = {
  1753. .fid = W1_FAMILY_DS28EL15,
  1754. .fops = &w1_ds28el15_fops,
  1755. };
  1756. static int __init w1_ds28el15_init(void)
  1757. {
  1758. return w1_register_family(&w1_ds28el15_family);
  1759. }
  1760. static void __exit w1_ds28el15_exit(void)
  1761. {
  1762. w1_unregister_family(&w1_ds28el15_family);
  1763. }
  1764. late_initcall(w1_ds28el15_init);
  1765. module_exit(w1_ds28el15_exit);
  1766. MODULE_LICENSE("GPL");
  1767. MODULE_AUTHOR("Clark Kim <clark.kim@maximintegrated.com>");
  1768. MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS23EL15 DeepCover Secure Authenticator IC");