ene_ub6250.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. /*
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the
  5. * Free Software Foundation; either version 2, or (at your option) any
  6. * later version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/jiffies.h>
  18. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <scsi/scsi.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <linux/firmware.h>
  24. #include "usb.h"
  25. #include "transport.h"
  26. #include "protocol.h"
  27. #include "debug.h"
  28. #define SD_INIT1_FIRMWARE "ene-ub6250/sd_init1.bin"
  29. #define SD_INIT2_FIRMWARE "ene-ub6250/sd_init2.bin"
  30. #define SD_RW_FIRMWARE "ene-ub6250/sd_rdwr.bin"
  31. #define MS_INIT_FIRMWARE "ene-ub6250/ms_init.bin"
  32. #define MSP_RW_FIRMWARE "ene-ub6250/msp_rdwr.bin"
  33. #define MS_RW_FIRMWARE "ene-ub6250/ms_rdwr.bin"
  34. MODULE_DESCRIPTION("Driver for ENE UB6250 reader");
  35. MODULE_LICENSE("GPL");
  36. MODULE_FIRMWARE(SD_INIT1_FIRMWARE);
  37. MODULE_FIRMWARE(SD_INIT2_FIRMWARE);
  38. MODULE_FIRMWARE(SD_RW_FIRMWARE);
  39. MODULE_FIRMWARE(MS_INIT_FIRMWARE);
  40. MODULE_FIRMWARE(MSP_RW_FIRMWARE);
  41. MODULE_FIRMWARE(MS_RW_FIRMWARE);
  42. /*
  43. * The table of devices
  44. */
  45. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  46. vendorName, productName, useProtocol, useTransport, \
  47. initFunction, flags) \
  48. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  49. .driver_info = (flags)}
  50. static struct usb_device_id ene_ub6250_usb_ids[] = {
  51. # include "unusual_ene_ub6250.h"
  52. { } /* Terminating entry */
  53. };
  54. MODULE_DEVICE_TABLE(usb, ene_ub6250_usb_ids);
  55. #undef UNUSUAL_DEV
  56. /*
  57. * The flags table
  58. */
  59. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  60. vendor_name, product_name, use_protocol, use_transport, \
  61. init_function, Flags) \
  62. { \
  63. .vendorName = vendor_name, \
  64. .productName = product_name, \
  65. .useProtocol = use_protocol, \
  66. .useTransport = use_transport, \
  67. .initFunction = init_function, \
  68. }
  69. static struct us_unusual_dev ene_ub6250_unusual_dev_list[] = {
  70. # include "unusual_ene_ub6250.h"
  71. { } /* Terminating entry */
  72. };
  73. #undef UNUSUAL_DEV
  74. /* ENE bin code len */
  75. #define ENE_BIN_CODE_LEN 0x800
  76. /* EnE HW Register */
  77. #define REG_CARD_STATUS 0xFF83
  78. #define REG_HW_TRAP1 0xFF89
  79. /* SRB Status */
  80. #define SS_SUCCESS 0x00 /* No Sense */
  81. #define SS_NOT_READY 0x02
  82. #define SS_MEDIUM_ERR 0x03
  83. #define SS_HW_ERR 0x04
  84. #define SS_ILLEGAL_REQUEST 0x05
  85. #define SS_UNIT_ATTENTION 0x06
  86. /* ENE Load FW Pattern */
  87. #define SD_INIT1_PATTERN 1
  88. #define SD_INIT2_PATTERN 2
  89. #define SD_RW_PATTERN 3
  90. #define MS_INIT_PATTERN 4
  91. #define MSP_RW_PATTERN 5
  92. #define MS_RW_PATTERN 6
  93. #define SM_INIT_PATTERN 7
  94. #define SM_RW_PATTERN 8
  95. #define FDIR_WRITE 0
  96. #define FDIR_READ 1
  97. /* For MS Card */
  98. /* Status Register 1 */
  99. #define MS_REG_ST1_MB 0x80 /* media busy */
  100. #define MS_REG_ST1_FB1 0x40 /* flush busy 1 */
  101. #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
  102. #define MS_REG_ST1_UCDT 0x10 /* unable to correct data */
  103. #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
  104. #define MS_REG_ST1_UCEX 0x04 /* unable to correct extra */
  105. #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
  106. #define MS_REG_ST1_UCFG 0x01 /* unable to correct overwrite flag */
  107. #define MS_REG_ST1_DEFAULT (MS_REG_ST1_MB | MS_REG_ST1_FB1 | MS_REG_ST1_DTER | MS_REG_ST1_UCDT | MS_REG_ST1_EXER | MS_REG_ST1_UCEX | MS_REG_ST1_FGER | MS_REG_ST1_UCFG)
  108. /* Overwrite Area */
  109. #define MS_REG_OVR_BKST 0x80 /* block status */
  110. #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
  111. #define MS_REG_OVR_BKST_NG 0x00 /* NG */
  112. #define MS_REG_OVR_PGST0 0x40 /* page status */
  113. #define MS_REG_OVR_PGST1 0x20
  114. #define MS_REG_OVR_PGST_MASK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1)
  115. #define MS_REG_OVR_PGST_OK (MS_REG_OVR_PGST0 | MS_REG_OVR_PGST1) /* OK */
  116. #define MS_REG_OVR_PGST_NG MS_REG_OVR_PGST1 /* NG */
  117. #define MS_REG_OVR_PGST_DATA_ERROR 0x00 /* data error */
  118. #define MS_REG_OVR_UDST 0x10 /* update status */
  119. #define MS_REG_OVR_UDST_UPDATING 0x00 /* updating */
  120. #define MS_REG_OVR_UDST_NO_UPDATE MS_REG_OVR_UDST
  121. #define MS_REG_OVR_RESERVED 0x08
  122. #define MS_REG_OVR_DEFAULT (MS_REG_OVR_BKST_OK | MS_REG_OVR_PGST_OK | MS_REG_OVR_UDST_NO_UPDATE | MS_REG_OVR_RESERVED)
  123. /* Management Flag */
  124. #define MS_REG_MNG_SCMS0 0x20 /* serial copy management system */
  125. #define MS_REG_MNG_SCMS1 0x10
  126. #define MS_REG_MNG_SCMS_MASK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
  127. #define MS_REG_MNG_SCMS_COPY_OK (MS_REG_MNG_SCMS0 | MS_REG_MNG_SCMS1)
  128. #define MS_REG_MNG_SCMS_ONE_COPY MS_REG_MNG_SCMS1
  129. #define MS_REG_MNG_SCMS_NO_COPY 0x00
  130. #define MS_REG_MNG_ATFLG 0x08 /* address transfer table flag */
  131. #define MS_REG_MNG_ATFLG_OTHER MS_REG_MNG_ATFLG /* other */
  132. #define MS_REG_MNG_ATFLG_ATTBL 0x00 /* address transfer table */
  133. #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
  134. #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
  135. #define MS_REG_MNG_SYSFLG_BOOT 0x00 /* system block */
  136. #define MS_REG_MNG_RESERVED 0xc3
  137. #define MS_REG_MNG_DEFAULT (MS_REG_MNG_SCMS_COPY_OK | MS_REG_MNG_ATFLG_OTHER | MS_REG_MNG_SYSFLG_USER | MS_REG_MNG_RESERVED)
  138. #define MS_MAX_PAGES_PER_BLOCK 32
  139. #define MS_MAX_INITIAL_ERROR_BLOCKS 10
  140. #define MS_LIB_BITS_PER_BYTE 8
  141. #define MS_SYSINF_FORMAT_FAT 1
  142. #define MS_SYSINF_USAGE_GENERAL 0
  143. #define MS_SYSINF_MSCLASS_TYPE_1 1
  144. #define MS_SYSINF_PAGE_SIZE MS_BYTES_PER_PAGE /* fixed */
  145. #define MS_SYSINF_CARDTYPE_RDONLY 1
  146. #define MS_SYSINF_CARDTYPE_RDWR 2
  147. #define MS_SYSINF_CARDTYPE_HYBRID 3
  148. #define MS_SYSINF_SECURITY 0x01
  149. #define MS_SYSINF_SECURITY_NO_SUPPORT MS_SYSINF_SECURITY
  150. #define MS_SYSINF_SECURITY_SUPPORT 0
  151. #define MS_SYSINF_RESERVED1 1
  152. #define MS_SYSINF_RESERVED2 1
  153. #define MS_SYSENT_TYPE_INVALID_BLOCK 0x01
  154. #define MS_SYSENT_TYPE_CIS_IDI 0x0a /* CIS/IDI */
  155. #define SIZE_OF_KIRO 1024
  156. #define BYTE_MASK 0xff
  157. /* ms error code */
  158. #define MS_STATUS_WRITE_PROTECT 0x0106
  159. #define MS_STATUS_SUCCESS 0x0000
  160. #define MS_ERROR_FLASH_READ 0x8003
  161. #define MS_ERROR_FLASH_ERASE 0x8005
  162. #define MS_LB_ERROR 0xfff0
  163. #define MS_LB_BOOT_BLOCK 0xfff1
  164. #define MS_LB_INITIAL_ERROR 0xfff2
  165. #define MS_STATUS_SUCCESS_WITH_ECC 0xfff3
  166. #define MS_LB_ACQUIRED_ERROR 0xfff4
  167. #define MS_LB_NOT_USED_ERASED 0xfff5
  168. #define MS_NOCARD_ERROR 0xfff8
  169. #define MS_NO_MEMORY_ERROR 0xfff9
  170. #define MS_STATUS_INT_ERROR 0xfffa
  171. #define MS_STATUS_ERROR 0xfffe
  172. #define MS_LB_NOT_USED 0xffff
  173. #define MS_REG_MNG_SYSFLG 0x04 /* system flag */
  174. #define MS_REG_MNG_SYSFLG_USER MS_REG_MNG_SYSFLG /* user block */
  175. #define MS_BOOT_BLOCK_ID 0x0001
  176. #define MS_BOOT_BLOCK_FORMAT_VERSION 0x0100
  177. #define MS_BOOT_BLOCK_DATA_ENTRIES 2
  178. #define MS_NUMBER_OF_SYSTEM_ENTRY 4
  179. #define MS_NUMBER_OF_BOOT_BLOCK 2
  180. #define MS_BYTES_PER_PAGE 512
  181. #define MS_LOGICAL_BLOCKS_PER_SEGMENT 496
  182. #define MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT 494
  183. #define MS_PHYSICAL_BLOCKS_PER_SEGMENT 0x200 /* 512 */
  184. #define MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK 0x1ff
  185. /* overwrite area */
  186. #define MS_REG_OVR_BKST 0x80 /* block status */
  187. #define MS_REG_OVR_BKST_OK MS_REG_OVR_BKST /* OK */
  188. #define MS_REG_OVR_BKST_NG 0x00 /* NG */
  189. /* Status Register 1 */
  190. #define MS_REG_ST1_DTER 0x20 /* error on data(corrected) */
  191. #define MS_REG_ST1_EXER 0x08 /* error on extra(corrected) */
  192. #define MS_REG_ST1_FGER 0x02 /* error on overwrite flag(corrected) */
  193. /* MemoryStick Register */
  194. /* Status Register 0 */
  195. #define MS_REG_ST0_WP 0x01 /* write protected */
  196. #define MS_REG_ST0_WP_ON MS_REG_ST0_WP
  197. #define MS_LIB_CTRL_RDONLY 0
  198. #define MS_LIB_CTRL_WRPROTECT 1
  199. /*dphy->log table */
  200. #define ms_libconv_to_logical(pdx, PhyBlock) (((PhyBlock) >= (pdx)->MS_Lib.NumberOfPhyBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Phy2LogMap[PhyBlock])
  201. #define ms_libconv_to_physical(pdx, LogBlock) (((LogBlock) >= (pdx)->MS_Lib.NumberOfLogBlock) ? MS_STATUS_ERROR : (pdx)->MS_Lib.Log2PhyMap[LogBlock])
  202. #define ms_lib_ctrl_set(pdx, Flag) ((pdx)->MS_Lib.flags |= (1 << (Flag)))
  203. #define ms_lib_ctrl_reset(pdx, Flag) ((pdx)->MS_Lib.flags &= ~(1 << (Flag)))
  204. #define ms_lib_ctrl_check(pdx, Flag) ((pdx)->MS_Lib.flags & (1 << (Flag)))
  205. #define ms_lib_iswritable(pdx) ((ms_lib_ctrl_check((pdx), MS_LIB_CTRL_RDONLY) == 0) && (ms_lib_ctrl_check(pdx, MS_LIB_CTRL_WRPROTECT) == 0))
  206. #define ms_lib_clear_pagemap(pdx) memset((pdx)->MS_Lib.pagemap, 0, sizeof((pdx)->MS_Lib.pagemap))
  207. #define memstick_logaddr(logadr1, logadr0) ((((u16)(logadr1)) << 8) | (logadr0))
  208. struct SD_STATUS {
  209. u8 Insert:1;
  210. u8 Ready:1;
  211. u8 MediaChange:1;
  212. u8 IsMMC:1;
  213. u8 HiCapacity:1;
  214. u8 HiSpeed:1;
  215. u8 WtP:1;
  216. u8 Reserved:1;
  217. };
  218. struct MS_STATUS {
  219. u8 Insert:1;
  220. u8 Ready:1;
  221. u8 MediaChange:1;
  222. u8 IsMSPro:1;
  223. u8 IsMSPHG:1;
  224. u8 Reserved1:1;
  225. u8 WtP:1;
  226. u8 Reserved2:1;
  227. };
  228. struct SM_STATUS {
  229. u8 Insert:1;
  230. u8 Ready:1;
  231. u8 MediaChange:1;
  232. u8 Reserved:3;
  233. u8 WtP:1;
  234. u8 IsMS:1;
  235. };
  236. struct ms_bootblock_cis {
  237. u8 bCistplDEVICE[6]; /* 0 */
  238. u8 bCistplDEVICE0C[6]; /* 6 */
  239. u8 bCistplJEDECC[4]; /* 12 */
  240. u8 bCistplMANFID[6]; /* 16 */
  241. u8 bCistplVER1[32]; /* 22 */
  242. u8 bCistplFUNCID[4]; /* 54 */
  243. u8 bCistplFUNCE0[4]; /* 58 */
  244. u8 bCistplFUNCE1[5]; /* 62 */
  245. u8 bCistplCONF[7]; /* 67 */
  246. u8 bCistplCFTBLENT0[10];/* 74 */
  247. u8 bCistplCFTBLENT1[8]; /* 84 */
  248. u8 bCistplCFTBLENT2[12];/* 92 */
  249. u8 bCistplCFTBLENT3[8]; /* 104 */
  250. u8 bCistplCFTBLENT4[17];/* 112 */
  251. u8 bCistplCFTBLENT5[8]; /* 129 */
  252. u8 bCistplCFTBLENT6[17];/* 137 */
  253. u8 bCistplCFTBLENT7[8]; /* 154 */
  254. u8 bCistplNOLINK[3]; /* 162 */
  255. } ;
  256. struct ms_bootblock_idi {
  257. #define MS_IDI_GENERAL_CONF 0x848A
  258. u16 wIDIgeneralConfiguration; /* 0 */
  259. u16 wIDInumberOfCylinder; /* 1 */
  260. u16 wIDIreserved0; /* 2 */
  261. u16 wIDInumberOfHead; /* 3 */
  262. u16 wIDIbytesPerTrack; /* 4 */
  263. u16 wIDIbytesPerSector; /* 5 */
  264. u16 wIDIsectorsPerTrack; /* 6 */
  265. u16 wIDItotalSectors[2]; /* 7-8 high,low */
  266. u16 wIDIreserved1[11]; /* 9-19 */
  267. u16 wIDIbufferType; /* 20 */
  268. u16 wIDIbufferSize; /* 21 */
  269. u16 wIDIlongCmdECC; /* 22 */
  270. u16 wIDIfirmVersion[4]; /* 23-26 */
  271. u16 wIDImodelName[20]; /* 27-46 */
  272. u16 wIDIreserved2; /* 47 */
  273. u16 wIDIlongWordSupported; /* 48 */
  274. u16 wIDIdmaSupported; /* 49 */
  275. u16 wIDIreserved3; /* 50 */
  276. u16 wIDIpioTiming; /* 51 */
  277. u16 wIDIdmaTiming; /* 52 */
  278. u16 wIDItransferParameter; /* 53 */
  279. u16 wIDIformattedCylinder; /* 54 */
  280. u16 wIDIformattedHead; /* 55 */
  281. u16 wIDIformattedSectorsPerTrack;/* 56 */
  282. u16 wIDIformattedTotalSectors[2];/* 57-58 */
  283. u16 wIDImultiSector; /* 59 */
  284. u16 wIDIlbaSectors[2]; /* 60-61 */
  285. u16 wIDIsingleWordDMA; /* 62 */
  286. u16 wIDImultiWordDMA; /* 63 */
  287. u16 wIDIreserved4[192]; /* 64-255 */
  288. };
  289. struct ms_bootblock_sysent_rec {
  290. u32 dwStart;
  291. u32 dwSize;
  292. u8 bType;
  293. u8 bReserved[3];
  294. };
  295. struct ms_bootblock_sysent {
  296. struct ms_bootblock_sysent_rec entry[MS_NUMBER_OF_SYSTEM_ENTRY];
  297. };
  298. struct ms_bootblock_sysinf {
  299. u8 bMsClass; /* must be 1 */
  300. u8 bCardType; /* see below */
  301. u16 wBlockSize; /* n KB */
  302. u16 wBlockNumber; /* number of physical block */
  303. u16 wTotalBlockNumber; /* number of logical block */
  304. u16 wPageSize; /* must be 0x200 */
  305. u8 bExtraSize; /* 0x10 */
  306. u8 bSecuritySupport;
  307. u8 bAssemblyDate[8];
  308. u8 bFactoryArea[4];
  309. u8 bAssemblyMakerCode;
  310. u8 bAssemblyMachineCode[3];
  311. u16 wMemoryMakerCode;
  312. u16 wMemoryDeviceCode;
  313. u16 wMemorySize;
  314. u8 bReserved1;
  315. u8 bReserved2;
  316. u8 bVCC;
  317. u8 bVPP;
  318. u16 wControllerChipNumber;
  319. u16 wControllerFunction; /* New MS */
  320. u8 bReserved3[9]; /* New MS */
  321. u8 bParallelSupport; /* New MS */
  322. u16 wFormatValue; /* New MS */
  323. u8 bFormatType;
  324. u8 bUsage;
  325. u8 bDeviceType;
  326. u8 bReserved4[22];
  327. u8 bFUValue3;
  328. u8 bFUValue4;
  329. u8 bReserved5[15];
  330. };
  331. struct ms_bootblock_header {
  332. u16 wBlockID;
  333. u16 wFormatVersion;
  334. u8 bReserved1[184];
  335. u8 bNumberOfDataEntry;
  336. u8 bReserved2[179];
  337. };
  338. struct ms_bootblock_page0 {
  339. struct ms_bootblock_header header;
  340. struct ms_bootblock_sysent sysent;
  341. struct ms_bootblock_sysinf sysinf;
  342. };
  343. struct ms_bootblock_cis_idi {
  344. union {
  345. struct ms_bootblock_cis cis;
  346. u8 dmy[256];
  347. } cis;
  348. union {
  349. struct ms_bootblock_idi idi;
  350. u8 dmy[256];
  351. } idi;
  352. };
  353. /* ENE MS Lib struct */
  354. struct ms_lib_type_extdat {
  355. u8 reserved;
  356. u8 intr;
  357. u8 status0;
  358. u8 status1;
  359. u8 ovrflg;
  360. u8 mngflg;
  361. u16 logadr;
  362. };
  363. struct ms_lib_ctrl {
  364. u32 flags;
  365. u32 BytesPerSector;
  366. u32 NumberOfCylinder;
  367. u32 SectorsPerCylinder;
  368. u16 cardType; /* R/W, RO, Hybrid */
  369. u16 blockSize;
  370. u16 PagesPerBlock;
  371. u16 NumberOfPhyBlock;
  372. u16 NumberOfLogBlock;
  373. u16 NumberOfSegment;
  374. u16 *Phy2LogMap; /* phy2log table */
  375. u16 *Log2PhyMap; /* log2phy table */
  376. u16 wrtblk;
  377. unsigned char *pagemap[(MS_MAX_PAGES_PER_BLOCK + (MS_LIB_BITS_PER_BYTE-1)) / MS_LIB_BITS_PER_BYTE];
  378. unsigned char *blkpag;
  379. struct ms_lib_type_extdat *blkext;
  380. unsigned char copybuf[512];
  381. };
  382. /* SD Block Length */
  383. /* 2^9 = 512 Bytes, The HW maximum read/write data length */
  384. #define SD_BLOCK_LEN 9
  385. struct ene_ub6250_info {
  386. /* for 6250 code */
  387. struct SD_STATUS SD_Status;
  388. struct MS_STATUS MS_Status;
  389. struct SM_STATUS SM_Status;
  390. /* ----- SD Control Data ---------------- */
  391. /*SD_REGISTER SD_Regs; */
  392. u16 SD_Block_Mult;
  393. u8 SD_READ_BL_LEN;
  394. u16 SD_C_SIZE;
  395. u8 SD_C_SIZE_MULT;
  396. /* SD/MMC New spec. */
  397. u8 SD_SPEC_VER;
  398. u8 SD_CSD_VER;
  399. u8 SD20_HIGH_CAPACITY;
  400. u32 HC_C_SIZE;
  401. u8 MMC_SPEC_VER;
  402. u8 MMC_BusWidth;
  403. u8 MMC_HIGH_CAPACITY;
  404. /*----- MS Control Data ---------------- */
  405. bool MS_SWWP;
  406. u32 MSP_TotalBlock;
  407. struct ms_lib_ctrl MS_Lib;
  408. bool MS_IsRWPage;
  409. u16 MS_Model;
  410. /*----- SM Control Data ---------------- */
  411. u8 SM_DeviceID;
  412. u8 SM_CardID;
  413. unsigned char *testbuf;
  414. u8 BIN_FLAG;
  415. u32 bl_num;
  416. int SrbStatus;
  417. /*------Power Managerment ---------------*/
  418. bool Power_IsResum;
  419. };
  420. static int ene_sd_init(struct us_data *us);
  421. static int ene_ms_init(struct us_data *us);
  422. static int ene_load_bincode(struct us_data *us, unsigned char flag);
  423. static void ene_ub6250_info_destructor(void *extra)
  424. {
  425. if (!extra)
  426. return;
  427. }
  428. static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
  429. {
  430. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  431. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  432. int result;
  433. unsigned int residue;
  434. unsigned int cswlen = 0, partial = 0;
  435. unsigned int transfer_length = bcb->DataTransferLength;
  436. /* US_DEBUGP("transport --- ene_send_scsi_cmd\n"); */
  437. /* send cmd to out endpoint */
  438. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  439. bcb, US_BULK_CB_WRAP_LEN, NULL);
  440. if (result != USB_STOR_XFER_GOOD) {
  441. US_DEBUGP("send cmd to out endpoint fail ---\n");
  442. return USB_STOR_TRANSPORT_ERROR;
  443. }
  444. if (buf) {
  445. unsigned int pipe = fDir;
  446. if (fDir == FDIR_READ)
  447. pipe = us->recv_bulk_pipe;
  448. else
  449. pipe = us->send_bulk_pipe;
  450. /* Bulk */
  451. if (use_sg) {
  452. result = usb_stor_bulk_srb(us, pipe, us->srb);
  453. } else {
  454. result = usb_stor_bulk_transfer_sg(us, pipe, buf,
  455. transfer_length, 0, &partial);
  456. }
  457. if (result != USB_STOR_XFER_GOOD) {
  458. US_DEBUGP("data transfer fail ---\n");
  459. return USB_STOR_TRANSPORT_ERROR;
  460. }
  461. }
  462. /* Get CSW for device status */
  463. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
  464. US_BULK_CS_WRAP_LEN, &cswlen);
  465. if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
  466. US_DEBUGP("Received 0-length CSW; retrying...\n");
  467. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  468. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  469. }
  470. if (result == USB_STOR_XFER_STALLED) {
  471. /* get the status again */
  472. US_DEBUGP("Attempting to get CSW (2nd try)...\n");
  473. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  474. bcs, US_BULK_CS_WRAP_LEN, NULL);
  475. }
  476. if (result != USB_STOR_XFER_GOOD)
  477. return USB_STOR_TRANSPORT_ERROR;
  478. /* check bulk status */
  479. residue = le32_to_cpu(bcs->Residue);
  480. /* try to compute the actual residue, based on how much data
  481. * was really transferred and what the device tells us */
  482. if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
  483. residue = min(residue, transfer_length);
  484. if (us->srb != NULL)
  485. scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
  486. (int)residue));
  487. }
  488. if (bcs->Status != US_BULK_STAT_OK)
  489. return USB_STOR_TRANSPORT_ERROR;
  490. return USB_STOR_TRANSPORT_GOOD;
  491. }
  492. static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
  493. {
  494. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  495. if (info->SD_Status.Insert && info->SD_Status.Ready)
  496. return USB_STOR_TRANSPORT_GOOD;
  497. else {
  498. ene_sd_init(us);
  499. return USB_STOR_TRANSPORT_GOOD;
  500. }
  501. return USB_STOR_TRANSPORT_GOOD;
  502. }
  503. static int sd_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
  504. {
  505. unsigned char data_ptr[36] = {
  506. 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
  507. 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
  508. 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
  509. 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
  510. usb_stor_set_xfer_buf(data_ptr, 36, srb);
  511. return USB_STOR_TRANSPORT_GOOD;
  512. }
  513. static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
  514. {
  515. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  516. unsigned char mediaNoWP[12] = {
  517. 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
  518. 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
  519. unsigned char mediaWP[12] = {
  520. 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
  521. 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
  522. if (info->SD_Status.WtP)
  523. usb_stor_set_xfer_buf(mediaWP, 12, srb);
  524. else
  525. usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
  526. return USB_STOR_TRANSPORT_GOOD;
  527. }
  528. static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
  529. {
  530. u32 bl_num;
  531. u32 bl_len;
  532. unsigned int offset = 0;
  533. unsigned char buf[8];
  534. struct scatterlist *sg = NULL;
  535. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  536. US_DEBUGP("sd_scsi_read_capacity\n");
  537. if (info->SD_Status.HiCapacity) {
  538. bl_len = 0x200;
  539. if (info->SD_Status.IsMMC)
  540. bl_num = info->HC_C_SIZE-1;
  541. else
  542. bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
  543. } else {
  544. bl_len = 1 << (info->SD_READ_BL_LEN);
  545. bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
  546. * (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
  547. }
  548. info->bl_num = bl_num;
  549. US_DEBUGP("bl_len = %x\n", bl_len);
  550. US_DEBUGP("bl_num = %x\n", bl_num);
  551. /*srb->request_bufflen = 8; */
  552. buf[0] = (bl_num >> 24) & 0xff;
  553. buf[1] = (bl_num >> 16) & 0xff;
  554. buf[2] = (bl_num >> 8) & 0xff;
  555. buf[3] = (bl_num >> 0) & 0xff;
  556. buf[4] = (bl_len >> 24) & 0xff;
  557. buf[5] = (bl_len >> 16) & 0xff;
  558. buf[6] = (bl_len >> 8) & 0xff;
  559. buf[7] = (bl_len >> 0) & 0xff;
  560. usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
  561. return USB_STOR_TRANSPORT_GOOD;
  562. }
  563. static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
  564. {
  565. int result;
  566. unsigned char *cdb = srb->cmnd;
  567. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  568. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  569. u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
  570. ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
  571. u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
  572. u32 bnByte = bn * 0x200;
  573. u32 blenByte = blen * 0x200;
  574. if (bn > info->bl_num)
  575. return USB_STOR_TRANSPORT_ERROR;
  576. result = ene_load_bincode(us, SD_RW_PATTERN);
  577. if (result != USB_STOR_XFER_GOOD) {
  578. US_DEBUGP("Load SD RW pattern Fail !!\n");
  579. return USB_STOR_TRANSPORT_ERROR;
  580. }
  581. if (info->SD_Status.HiCapacity)
  582. bnByte = bn;
  583. /* set up the command wrapper */
  584. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  585. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  586. bcb->DataTransferLength = blenByte;
  587. bcb->Flags = US_BULK_FLAG_IN;
  588. bcb->CDB[0] = 0xF1;
  589. bcb->CDB[5] = (unsigned char)(bnByte);
  590. bcb->CDB[4] = (unsigned char)(bnByte>>8);
  591. bcb->CDB[3] = (unsigned char)(bnByte>>16);
  592. bcb->CDB[2] = (unsigned char)(bnByte>>24);
  593. result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
  594. return result;
  595. }
  596. static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
  597. {
  598. int result;
  599. unsigned char *cdb = srb->cmnd;
  600. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  601. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  602. u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
  603. ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
  604. u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
  605. u32 bnByte = bn * 0x200;
  606. u32 blenByte = blen * 0x200;
  607. if (bn > info->bl_num)
  608. return USB_STOR_TRANSPORT_ERROR;
  609. result = ene_load_bincode(us, SD_RW_PATTERN);
  610. if (result != USB_STOR_XFER_GOOD) {
  611. US_DEBUGP("Load SD RW pattern Fail !!\n");
  612. return USB_STOR_TRANSPORT_ERROR;
  613. }
  614. if (info->SD_Status.HiCapacity)
  615. bnByte = bn;
  616. /* set up the command wrapper */
  617. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  618. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  619. bcb->DataTransferLength = blenByte;
  620. bcb->Flags = 0x00;
  621. bcb->CDB[0] = 0xF0;
  622. bcb->CDB[5] = (unsigned char)(bnByte);
  623. bcb->CDB[4] = (unsigned char)(bnByte>>8);
  624. bcb->CDB[3] = (unsigned char)(bnByte>>16);
  625. bcb->CDB[2] = (unsigned char)(bnByte>>24);
  626. result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
  627. return result;
  628. }
  629. /*
  630. * ENE MS Card
  631. */
  632. static int ms_lib_set_logicalpair(struct us_data *us, u16 logblk, u16 phyblk)
  633. {
  634. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  635. if ((logblk >= info->MS_Lib.NumberOfLogBlock) || (phyblk >= info->MS_Lib.NumberOfPhyBlock))
  636. return (u32)-1;
  637. info->MS_Lib.Phy2LogMap[phyblk] = logblk;
  638. info->MS_Lib.Log2PhyMap[logblk] = phyblk;
  639. return 0;
  640. }
  641. static int ms_lib_set_logicalblockmark(struct us_data *us, u16 phyblk, u16 mark)
  642. {
  643. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  644. if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
  645. return (u32)-1;
  646. info->MS_Lib.Phy2LogMap[phyblk] = mark;
  647. return 0;
  648. }
  649. static int ms_lib_set_initialerrorblock(struct us_data *us, u16 phyblk)
  650. {
  651. return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_INITIAL_ERROR);
  652. }
  653. static int ms_lib_set_bootblockmark(struct us_data *us, u16 phyblk)
  654. {
  655. return ms_lib_set_logicalblockmark(us, phyblk, MS_LB_BOOT_BLOCK);
  656. }
  657. static int ms_lib_free_logicalmap(struct us_data *us)
  658. {
  659. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  660. kfree(info->MS_Lib.Phy2LogMap);
  661. info->MS_Lib.Phy2LogMap = NULL;
  662. kfree(info->MS_Lib.Log2PhyMap);
  663. info->MS_Lib.Log2PhyMap = NULL;
  664. return 0;
  665. }
  666. static int ms_lib_alloc_logicalmap(struct us_data *us)
  667. {
  668. u32 i;
  669. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  670. info->MS_Lib.Phy2LogMap = kmalloc(info->MS_Lib.NumberOfPhyBlock * sizeof(u16), GFP_KERNEL);
  671. info->MS_Lib.Log2PhyMap = kmalloc(info->MS_Lib.NumberOfLogBlock * sizeof(u16), GFP_KERNEL);
  672. if ((info->MS_Lib.Phy2LogMap == NULL) || (info->MS_Lib.Log2PhyMap == NULL)) {
  673. ms_lib_free_logicalmap(us);
  674. return (u32)-1;
  675. }
  676. for (i = 0; i < info->MS_Lib.NumberOfPhyBlock; i++)
  677. info->MS_Lib.Phy2LogMap[i] = MS_LB_NOT_USED;
  678. for (i = 0; i < info->MS_Lib.NumberOfLogBlock; i++)
  679. info->MS_Lib.Log2PhyMap[i] = MS_LB_NOT_USED;
  680. return 0;
  681. }
  682. static void ms_lib_clear_writebuf(struct us_data *us)
  683. {
  684. int i;
  685. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  686. info->MS_Lib.wrtblk = (u16)-1;
  687. ms_lib_clear_pagemap(info);
  688. if (info->MS_Lib.blkpag)
  689. memset(info->MS_Lib.blkpag, 0xff, info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector);
  690. if (info->MS_Lib.blkext) {
  691. for (i = 0; i < info->MS_Lib.PagesPerBlock; i++) {
  692. info->MS_Lib.blkext[i].status1 = MS_REG_ST1_DEFAULT;
  693. info->MS_Lib.blkext[i].ovrflg = MS_REG_OVR_DEFAULT;
  694. info->MS_Lib.blkext[i].mngflg = MS_REG_MNG_DEFAULT;
  695. info->MS_Lib.blkext[i].logadr = MS_LB_NOT_USED;
  696. }
  697. }
  698. }
  699. static int ms_count_freeblock(struct us_data *us, u16 PhyBlock)
  700. {
  701. u32 Ende, Count;
  702. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  703. Ende = PhyBlock + MS_PHYSICAL_BLOCKS_PER_SEGMENT;
  704. for (Count = 0; PhyBlock < Ende; PhyBlock++) {
  705. switch (info->MS_Lib.Phy2LogMap[PhyBlock]) {
  706. case MS_LB_NOT_USED:
  707. case MS_LB_NOT_USED_ERASED:
  708. Count++;
  709. default:
  710. break;
  711. }
  712. }
  713. return Count;
  714. }
  715. static int ms_read_readpage(struct us_data *us, u32 PhyBlockAddr,
  716. u8 PageNum, u32 *PageBuf, struct ms_lib_type_extdat *ExtraDat)
  717. {
  718. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  719. int result;
  720. u8 ExtBuf[4];
  721. u32 bn = PhyBlockAddr * 0x20 + PageNum;
  722. /* printk(KERN_INFO "MS --- MS_ReaderReadPage,
  723. PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
  724. result = ene_load_bincode(us, MS_RW_PATTERN);
  725. if (result != USB_STOR_XFER_GOOD)
  726. return USB_STOR_TRANSPORT_ERROR;
  727. /* Read Page Data */
  728. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  729. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  730. bcb->DataTransferLength = 0x200;
  731. bcb->Flags = US_BULK_FLAG_IN;
  732. bcb->CDB[0] = 0xF1;
  733. bcb->CDB[1] = 0x02; /* in init.c ENE_MSInit() is 0x01 */
  734. bcb->CDB[5] = (unsigned char)(bn);
  735. bcb->CDB[4] = (unsigned char)(bn>>8);
  736. bcb->CDB[3] = (unsigned char)(bn>>16);
  737. bcb->CDB[2] = (unsigned char)(bn>>24);
  738. result = ene_send_scsi_cmd(us, FDIR_READ, PageBuf, 0);
  739. if (result != USB_STOR_XFER_GOOD)
  740. return USB_STOR_TRANSPORT_ERROR;
  741. /* Read Extra Data */
  742. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  743. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  744. bcb->DataTransferLength = 0x4;
  745. bcb->Flags = US_BULK_FLAG_IN;
  746. bcb->CDB[0] = 0xF1;
  747. bcb->CDB[1] = 0x03;
  748. bcb->CDB[5] = (unsigned char)(PageNum);
  749. bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
  750. bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
  751. bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
  752. bcb->CDB[6] = 0x01;
  753. result = ene_send_scsi_cmd(us, FDIR_READ, &ExtBuf, 0);
  754. if (result != USB_STOR_XFER_GOOD)
  755. return USB_STOR_TRANSPORT_ERROR;
  756. ExtraDat->reserved = 0;
  757. ExtraDat->intr = 0x80; /* Not yet,fireware support */
  758. ExtraDat->status0 = 0x10; /* Not yet,fireware support */
  759. ExtraDat->status1 = 0x00; /* Not yet,fireware support */
  760. ExtraDat->ovrflg = ExtBuf[0];
  761. ExtraDat->mngflg = ExtBuf[1];
  762. ExtraDat->logadr = memstick_logaddr(ExtBuf[2], ExtBuf[3]);
  763. return USB_STOR_TRANSPORT_GOOD;
  764. }
  765. static int ms_lib_process_bootblock(struct us_data *us, u16 PhyBlock, u8 *PageData)
  766. {
  767. struct ms_bootblock_sysent *SysEntry;
  768. struct ms_bootblock_sysinf *SysInfo;
  769. u32 i, result;
  770. u8 PageNumber;
  771. u8 *PageBuffer;
  772. struct ms_lib_type_extdat ExtraData;
  773. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  774. PageBuffer = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
  775. if (PageBuffer == NULL)
  776. return (u32)-1;
  777. result = (u32)-1;
  778. SysInfo = &(((struct ms_bootblock_page0 *)PageData)->sysinf);
  779. if ((SysInfo->bMsClass != MS_SYSINF_MSCLASS_TYPE_1) ||
  780. (be16_to_cpu(SysInfo->wPageSize) != MS_SYSINF_PAGE_SIZE) ||
  781. ((SysInfo->bSecuritySupport & MS_SYSINF_SECURITY) == MS_SYSINF_SECURITY_SUPPORT) ||
  782. (SysInfo->bReserved1 != MS_SYSINF_RESERVED1) ||
  783. (SysInfo->bReserved2 != MS_SYSINF_RESERVED2) ||
  784. (SysInfo->bFormatType != MS_SYSINF_FORMAT_FAT) ||
  785. (SysInfo->bUsage != MS_SYSINF_USAGE_GENERAL))
  786. goto exit;
  787. /* */
  788. switch (info->MS_Lib.cardType = SysInfo->bCardType) {
  789. case MS_SYSINF_CARDTYPE_RDONLY:
  790. ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY);
  791. break;
  792. case MS_SYSINF_CARDTYPE_RDWR:
  793. ms_lib_ctrl_reset(info, MS_LIB_CTRL_RDONLY);
  794. break;
  795. case MS_SYSINF_CARDTYPE_HYBRID:
  796. default:
  797. goto exit;
  798. }
  799. info->MS_Lib.blockSize = be16_to_cpu(SysInfo->wBlockSize);
  800. info->MS_Lib.NumberOfPhyBlock = be16_to_cpu(SysInfo->wBlockNumber);
  801. info->MS_Lib.NumberOfLogBlock = be16_to_cpu(SysInfo->wTotalBlockNumber)-2;
  802. info->MS_Lib.PagesPerBlock = info->MS_Lib.blockSize * SIZE_OF_KIRO / MS_BYTES_PER_PAGE;
  803. info->MS_Lib.NumberOfSegment = info->MS_Lib.NumberOfPhyBlock / MS_PHYSICAL_BLOCKS_PER_SEGMENT;
  804. info->MS_Model = be16_to_cpu(SysInfo->wMemorySize);
  805. /*Allocate to all number of logicalblock and physicalblock */
  806. if (ms_lib_alloc_logicalmap(us))
  807. goto exit;
  808. /* Mark the book block */
  809. ms_lib_set_bootblockmark(us, PhyBlock);
  810. SysEntry = &(((struct ms_bootblock_page0 *)PageData)->sysent);
  811. for (i = 0; i < MS_NUMBER_OF_SYSTEM_ENTRY; i++) {
  812. u32 EntryOffset, EntrySize;
  813. EntryOffset = be32_to_cpu(SysEntry->entry[i].dwStart);
  814. if (EntryOffset == 0xffffff)
  815. continue;
  816. EntrySize = be32_to_cpu(SysEntry->entry[i].dwSize);
  817. if (EntrySize == 0)
  818. continue;
  819. if (EntryOffset + MS_BYTES_PER_PAGE + EntrySize > info->MS_Lib.blockSize * (u32)SIZE_OF_KIRO)
  820. continue;
  821. if (i == 0) {
  822. u8 PrevPageNumber = 0;
  823. u16 phyblk;
  824. if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_INVALID_BLOCK)
  825. goto exit;
  826. while (EntrySize > 0) {
  827. PageNumber = (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1);
  828. if (PageNumber != PrevPageNumber) {
  829. switch (ms_read_readpage(us, PhyBlock, PageNumber, (u32 *)PageBuffer, &ExtraData)) {
  830. case MS_STATUS_SUCCESS:
  831. break;
  832. case MS_STATUS_WRITE_PROTECT:
  833. case MS_ERROR_FLASH_READ:
  834. case MS_STATUS_ERROR:
  835. default:
  836. goto exit;
  837. }
  838. PrevPageNumber = PageNumber;
  839. }
  840. phyblk = be16_to_cpu(*(u16 *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)));
  841. if (phyblk < 0x0fff)
  842. ms_lib_set_initialerrorblock(us, phyblk);
  843. EntryOffset += 2;
  844. EntrySize -= 2;
  845. }
  846. } else if (i == 1) { /* CIS/IDI */
  847. struct ms_bootblock_idi *idi;
  848. if (SysEntry->entry[i].bType != MS_SYSENT_TYPE_CIS_IDI)
  849. goto exit;
  850. switch (ms_read_readpage(us, PhyBlock, (u8)(EntryOffset / MS_BYTES_PER_PAGE + 1), (u32 *)PageBuffer, &ExtraData)) {
  851. case MS_STATUS_SUCCESS:
  852. break;
  853. case MS_STATUS_WRITE_PROTECT:
  854. case MS_ERROR_FLASH_READ:
  855. case MS_STATUS_ERROR:
  856. default:
  857. goto exit;
  858. }
  859. idi = &((struct ms_bootblock_cis_idi *)(PageBuffer + (EntryOffset % MS_BYTES_PER_PAGE)))->idi.idi;
  860. if (le16_to_cpu(idi->wIDIgeneralConfiguration) != MS_IDI_GENERAL_CONF)
  861. goto exit;
  862. info->MS_Lib.BytesPerSector = le16_to_cpu(idi->wIDIbytesPerSector);
  863. if (info->MS_Lib.BytesPerSector != MS_BYTES_PER_PAGE)
  864. goto exit;
  865. }
  866. } /* End for .. */
  867. result = 0;
  868. exit:
  869. if (result)
  870. ms_lib_free_logicalmap(us);
  871. kfree(PageBuffer);
  872. result = 0;
  873. return result;
  874. }
  875. static void ms_lib_free_writebuf(struct us_data *us)
  876. {
  877. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  878. info->MS_Lib.wrtblk = (u16)-1; /* set to -1 */
  879. /* memset((fdoExt)->MS_Lib.pagemap, 0, sizeof((fdoExt)->MS_Lib.pagemap)) */
  880. ms_lib_clear_pagemap(info); /* (pdx)->MS_Lib.pagemap memset 0 in ms.h */
  881. if (info->MS_Lib.blkpag) {
  882. kfree((u8 *)(info->MS_Lib.blkpag)); /* Arnold test ... */
  883. info->MS_Lib.blkpag = NULL;
  884. }
  885. if (info->MS_Lib.blkext) {
  886. kfree((u8 *)(info->MS_Lib.blkext)); /* Arnold test ... */
  887. info->MS_Lib.blkext = NULL;
  888. }
  889. }
  890. static void ms_lib_free_allocatedarea(struct us_data *us)
  891. {
  892. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  893. ms_lib_free_writebuf(us); /* Free MS_Lib.pagemap */
  894. ms_lib_free_logicalmap(us); /* kfree MS_Lib.Phy2LogMap and MS_Lib.Log2PhyMap */
  895. /* set struct us point flag to 0 */
  896. info->MS_Lib.flags = 0;
  897. info->MS_Lib.BytesPerSector = 0;
  898. info->MS_Lib.SectorsPerCylinder = 0;
  899. info->MS_Lib.cardType = 0;
  900. info->MS_Lib.blockSize = 0;
  901. info->MS_Lib.PagesPerBlock = 0;
  902. info->MS_Lib.NumberOfPhyBlock = 0;
  903. info->MS_Lib.NumberOfLogBlock = 0;
  904. }
  905. static int ms_lib_alloc_writebuf(struct us_data *us)
  906. {
  907. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  908. info->MS_Lib.wrtblk = (u16)-1;
  909. info->MS_Lib.blkpag = kmalloc(info->MS_Lib.PagesPerBlock * info->MS_Lib.BytesPerSector, GFP_KERNEL);
  910. info->MS_Lib.blkext = kmalloc(info->MS_Lib.PagesPerBlock * sizeof(struct ms_lib_type_extdat), GFP_KERNEL);
  911. if ((info->MS_Lib.blkpag == NULL) || (info->MS_Lib.blkext == NULL)) {
  912. ms_lib_free_writebuf(us);
  913. return (u32)-1;
  914. }
  915. ms_lib_clear_writebuf(us);
  916. return 0;
  917. }
  918. static int ms_lib_force_setlogical_pair(struct us_data *us, u16 logblk, u16 phyblk)
  919. {
  920. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  921. if (logblk == MS_LB_NOT_USED)
  922. return 0;
  923. if ((logblk >= info->MS_Lib.NumberOfLogBlock) ||
  924. (phyblk >= info->MS_Lib.NumberOfPhyBlock))
  925. return (u32)-1;
  926. info->MS_Lib.Phy2LogMap[phyblk] = logblk;
  927. info->MS_Lib.Log2PhyMap[logblk] = phyblk;
  928. return 0;
  929. }
  930. static int ms_read_copyblock(struct us_data *us, u16 oldphy, u16 newphy,
  931. u16 PhyBlockAddr, u8 PageNum, unsigned char *buf, u16 len)
  932. {
  933. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  934. int result;
  935. /* printk(KERN_INFO "MS_ReaderCopyBlock --- PhyBlockAddr = %x,
  936. PageNum = %x\n", PhyBlockAddr, PageNum); */
  937. result = ene_load_bincode(us, MS_RW_PATTERN);
  938. if (result != USB_STOR_XFER_GOOD)
  939. return USB_STOR_TRANSPORT_ERROR;
  940. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  941. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  942. bcb->DataTransferLength = 0x200*len;
  943. bcb->Flags = 0x00;
  944. bcb->CDB[0] = 0xF0;
  945. bcb->CDB[1] = 0x08;
  946. bcb->CDB[4] = (unsigned char)(oldphy);
  947. bcb->CDB[3] = (unsigned char)(oldphy>>8);
  948. bcb->CDB[2] = 0; /* (BYTE)(oldphy>>16) */
  949. bcb->CDB[7] = (unsigned char)(newphy);
  950. bcb->CDB[6] = (unsigned char)(newphy>>8);
  951. bcb->CDB[5] = 0; /* (BYTE)(newphy>>16) */
  952. bcb->CDB[9] = (unsigned char)(PhyBlockAddr);
  953. bcb->CDB[8] = (unsigned char)(PhyBlockAddr>>8);
  954. bcb->CDB[10] = PageNum;
  955. result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
  956. if (result != USB_STOR_XFER_GOOD)
  957. return USB_STOR_TRANSPORT_ERROR;
  958. return USB_STOR_TRANSPORT_GOOD;
  959. }
  960. static int ms_read_eraseblock(struct us_data *us, u32 PhyBlockAddr)
  961. {
  962. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  963. int result;
  964. u32 bn = PhyBlockAddr;
  965. /* printk(KERN_INFO "MS --- ms_read_eraseblock,
  966. PhyBlockAddr = %x\n", PhyBlockAddr); */
  967. result = ene_load_bincode(us, MS_RW_PATTERN);
  968. if (result != USB_STOR_XFER_GOOD)
  969. return USB_STOR_TRANSPORT_ERROR;
  970. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  971. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  972. bcb->DataTransferLength = 0x200;
  973. bcb->Flags = US_BULK_FLAG_IN;
  974. bcb->CDB[0] = 0xF2;
  975. bcb->CDB[1] = 0x06;
  976. bcb->CDB[4] = (unsigned char)(bn);
  977. bcb->CDB[3] = (unsigned char)(bn>>8);
  978. bcb->CDB[2] = (unsigned char)(bn>>16);
  979. result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
  980. if (result != USB_STOR_XFER_GOOD)
  981. return USB_STOR_TRANSPORT_ERROR;
  982. return USB_STOR_TRANSPORT_GOOD;
  983. }
  984. static int ms_lib_check_disableblock(struct us_data *us, u16 PhyBlock)
  985. {
  986. unsigned char *PageBuf = NULL;
  987. u16 result = MS_STATUS_SUCCESS;
  988. u16 blk, index = 0;
  989. struct ms_lib_type_extdat extdat;
  990. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  991. PageBuf = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
  992. if (PageBuf == NULL) {
  993. result = MS_NO_MEMORY_ERROR;
  994. goto exit;
  995. }
  996. ms_read_readpage(us, PhyBlock, 1, (u32 *)PageBuf, &extdat);
  997. do {
  998. blk = be16_to_cpu(PageBuf[index]);
  999. if (blk == MS_LB_NOT_USED)
  1000. break;
  1001. if (blk == info->MS_Lib.Log2PhyMap[0]) {
  1002. result = MS_ERROR_FLASH_READ;
  1003. break;
  1004. }
  1005. index++;
  1006. } while (1);
  1007. exit:
  1008. kfree(PageBuf);
  1009. return result;
  1010. }
  1011. static int ms_lib_setacquired_errorblock(struct us_data *us, u16 phyblk)
  1012. {
  1013. u16 log;
  1014. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1015. if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
  1016. return (u32)-1;
  1017. log = info->MS_Lib.Phy2LogMap[phyblk];
  1018. if (log < info->MS_Lib.NumberOfLogBlock)
  1019. info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
  1020. if (info->MS_Lib.Phy2LogMap[phyblk] != MS_LB_INITIAL_ERROR)
  1021. info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_ACQUIRED_ERROR;
  1022. return 0;
  1023. }
  1024. static int ms_lib_overwrite_extra(struct us_data *us, u32 PhyBlockAddr,
  1025. u8 PageNum, u8 OverwriteFlag)
  1026. {
  1027. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1028. int result;
  1029. /* printk("MS --- MS_LibOverwriteExtra,
  1030. PhyBlockAddr = %x, PageNum = %x\n", PhyBlockAddr, PageNum); */
  1031. result = ene_load_bincode(us, MS_RW_PATTERN);
  1032. if (result != USB_STOR_XFER_GOOD)
  1033. return USB_STOR_TRANSPORT_ERROR;
  1034. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1035. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1036. bcb->DataTransferLength = 0x4;
  1037. bcb->Flags = US_BULK_FLAG_IN;
  1038. bcb->CDB[0] = 0xF2;
  1039. bcb->CDB[1] = 0x05;
  1040. bcb->CDB[5] = (unsigned char)(PageNum);
  1041. bcb->CDB[4] = (unsigned char)(PhyBlockAddr);
  1042. bcb->CDB[3] = (unsigned char)(PhyBlockAddr>>8);
  1043. bcb->CDB[2] = (unsigned char)(PhyBlockAddr>>16);
  1044. bcb->CDB[6] = OverwriteFlag;
  1045. bcb->CDB[7] = 0xFF;
  1046. bcb->CDB[8] = 0xFF;
  1047. bcb->CDB[9] = 0xFF;
  1048. result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
  1049. if (result != USB_STOR_XFER_GOOD)
  1050. return USB_STOR_TRANSPORT_ERROR;
  1051. return USB_STOR_TRANSPORT_GOOD;
  1052. }
  1053. static int ms_lib_error_phyblock(struct us_data *us, u16 phyblk)
  1054. {
  1055. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1056. if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
  1057. return MS_STATUS_ERROR;
  1058. ms_lib_setacquired_errorblock(us, phyblk);
  1059. if (ms_lib_iswritable(info))
  1060. return ms_lib_overwrite_extra(us, phyblk, 0, (u8)(~MS_REG_OVR_BKST & BYTE_MASK));
  1061. return MS_STATUS_SUCCESS;
  1062. }
  1063. static int ms_lib_erase_phyblock(struct us_data *us, u16 phyblk)
  1064. {
  1065. u16 log;
  1066. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1067. if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
  1068. return MS_STATUS_ERROR;
  1069. log = info->MS_Lib.Phy2LogMap[phyblk];
  1070. if (log < info->MS_Lib.NumberOfLogBlock)
  1071. info->MS_Lib.Log2PhyMap[log] = MS_LB_NOT_USED;
  1072. info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED;
  1073. if (ms_lib_iswritable(info)) {
  1074. switch (ms_read_eraseblock(us, phyblk)) {
  1075. case MS_STATUS_SUCCESS:
  1076. info->MS_Lib.Phy2LogMap[phyblk] = MS_LB_NOT_USED_ERASED;
  1077. return MS_STATUS_SUCCESS;
  1078. case MS_ERROR_FLASH_ERASE:
  1079. case MS_STATUS_INT_ERROR:
  1080. ms_lib_error_phyblock(us, phyblk);
  1081. return MS_ERROR_FLASH_ERASE;
  1082. case MS_STATUS_ERROR:
  1083. default:
  1084. ms_lib_ctrl_set(info, MS_LIB_CTRL_RDONLY); /* MS_LibCtrlSet will used by ENE_MSInit ,need check, and why us to info*/
  1085. ms_lib_setacquired_errorblock(us, phyblk);
  1086. return MS_STATUS_ERROR;
  1087. }
  1088. }
  1089. ms_lib_setacquired_errorblock(us, phyblk);
  1090. return MS_STATUS_SUCCESS;
  1091. }
  1092. static int ms_lib_read_extra(struct us_data *us, u32 PhyBlock,
  1093. u8 PageNum, struct ms_lib_type_extdat *ExtraDat)
  1094. {
  1095. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1096. int result;
  1097. u8 ExtBuf[4];
  1098. /* printk("MS_LibReadExtra --- PhyBlock = %x, PageNum = %x\n", PhyBlock, PageNum); */
  1099. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1100. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1101. bcb->DataTransferLength = 0x4;
  1102. bcb->Flags = US_BULK_FLAG_IN;
  1103. bcb->CDB[0] = 0xF1;
  1104. bcb->CDB[1] = 0x03;
  1105. bcb->CDB[5] = (unsigned char)(PageNum);
  1106. bcb->CDB[4] = (unsigned char)(PhyBlock);
  1107. bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
  1108. bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
  1109. bcb->CDB[6] = 0x01;
  1110. result = ene_send_scsi_cmd(us, FDIR_READ, &ExtBuf, 0);
  1111. if (result != USB_STOR_XFER_GOOD)
  1112. return USB_STOR_TRANSPORT_ERROR;
  1113. ExtraDat->reserved = 0;
  1114. ExtraDat->intr = 0x80; /* Not yet, waiting for fireware support */
  1115. ExtraDat->status0 = 0x10; /* Not yet, waiting for fireware support */
  1116. ExtraDat->status1 = 0x00; /* Not yet, waiting for fireware support */
  1117. ExtraDat->ovrflg = ExtBuf[0];
  1118. ExtraDat->mngflg = ExtBuf[1];
  1119. ExtraDat->logadr = memstick_logaddr(ExtBuf[2], ExtBuf[3]);
  1120. return USB_STOR_TRANSPORT_GOOD;
  1121. }
  1122. static int ms_libsearch_block_from_physical(struct us_data *us, u16 phyblk)
  1123. {
  1124. u16 Newblk;
  1125. u16 blk;
  1126. struct ms_lib_type_extdat extdat; /* need check */
  1127. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1128. if (phyblk >= info->MS_Lib.NumberOfPhyBlock)
  1129. return MS_LB_ERROR;
  1130. for (blk = phyblk + 1; blk != phyblk; blk++) {
  1131. if ((blk & MS_PHYSICAL_BLOCKS_PER_SEGMENT_MASK) == 0)
  1132. blk -= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
  1133. Newblk = info->MS_Lib.Phy2LogMap[blk];
  1134. if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED_ERASED) {
  1135. return blk;
  1136. } else if (info->MS_Lib.Phy2LogMap[blk] == MS_LB_NOT_USED) {
  1137. switch (ms_lib_read_extra(us, blk, 0, &extdat)) {
  1138. case MS_STATUS_SUCCESS:
  1139. case MS_STATUS_SUCCESS_WITH_ECC:
  1140. break;
  1141. case MS_NOCARD_ERROR:
  1142. return MS_NOCARD_ERROR;
  1143. case MS_STATUS_INT_ERROR:
  1144. return MS_LB_ERROR;
  1145. case MS_ERROR_FLASH_READ:
  1146. default:
  1147. ms_lib_setacquired_errorblock(us, blk);
  1148. continue;
  1149. } /* End switch */
  1150. if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
  1151. ms_lib_setacquired_errorblock(us, blk);
  1152. continue;
  1153. }
  1154. switch (ms_lib_erase_phyblock(us, blk)) {
  1155. case MS_STATUS_SUCCESS:
  1156. return blk;
  1157. case MS_STATUS_ERROR:
  1158. return MS_LB_ERROR;
  1159. case MS_ERROR_FLASH_ERASE:
  1160. default:
  1161. ms_lib_error_phyblock(us, blk);
  1162. break;
  1163. }
  1164. }
  1165. } /* End for */
  1166. return MS_LB_ERROR;
  1167. }
  1168. static int ms_libsearch_block_from_logical(struct us_data *us, u16 logblk)
  1169. {
  1170. u16 phyblk;
  1171. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1172. phyblk = ms_libconv_to_physical(info, logblk);
  1173. if (phyblk >= MS_LB_ERROR) {
  1174. if (logblk >= info->MS_Lib.NumberOfLogBlock)
  1175. return MS_LB_ERROR;
  1176. phyblk = (logblk + MS_NUMBER_OF_BOOT_BLOCK) / MS_LOGICAL_BLOCKS_PER_SEGMENT;
  1177. phyblk *= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
  1178. phyblk += MS_PHYSICAL_BLOCKS_PER_SEGMENT - 1;
  1179. }
  1180. return ms_libsearch_block_from_physical(us, phyblk);
  1181. }
  1182. static int ms_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
  1183. {
  1184. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  1185. /* pr_info("MS_SCSI_Test_Unit_Ready\n"); */
  1186. if (info->MS_Status.Insert && info->MS_Status.Ready) {
  1187. return USB_STOR_TRANSPORT_GOOD;
  1188. } else {
  1189. ene_ms_init(us);
  1190. return USB_STOR_TRANSPORT_GOOD;
  1191. }
  1192. return USB_STOR_TRANSPORT_GOOD;
  1193. }
  1194. static int ms_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
  1195. {
  1196. /* pr_info("MS_SCSI_Inquiry\n"); */
  1197. unsigned char data_ptr[36] = {
  1198. 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
  1199. 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
  1200. 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
  1201. 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30};
  1202. usb_stor_set_xfer_buf(data_ptr, 36, srb);
  1203. return USB_STOR_TRANSPORT_GOOD;
  1204. }
  1205. static int ms_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
  1206. {
  1207. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1208. unsigned char mediaNoWP[12] = {
  1209. 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
  1210. 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
  1211. unsigned char mediaWP[12] = {
  1212. 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
  1213. 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
  1214. if (info->MS_Status.WtP)
  1215. usb_stor_set_xfer_buf(mediaWP, 12, srb);
  1216. else
  1217. usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
  1218. return USB_STOR_TRANSPORT_GOOD;
  1219. }
  1220. static int ms_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
  1221. {
  1222. u32 bl_num;
  1223. u16 bl_len;
  1224. unsigned int offset = 0;
  1225. unsigned char buf[8];
  1226. struct scatterlist *sg = NULL;
  1227. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1228. US_DEBUGP("ms_scsi_read_capacity\n");
  1229. bl_len = 0x200;
  1230. if (info->MS_Status.IsMSPro)
  1231. bl_num = info->MSP_TotalBlock - 1;
  1232. else
  1233. bl_num = info->MS_Lib.NumberOfLogBlock * info->MS_Lib.blockSize * 2 - 1;
  1234. info->bl_num = bl_num;
  1235. US_DEBUGP("bl_len = %x\n", bl_len);
  1236. US_DEBUGP("bl_num = %x\n", bl_num);
  1237. /*srb->request_bufflen = 8; */
  1238. buf[0] = (bl_num >> 24) & 0xff;
  1239. buf[1] = (bl_num >> 16) & 0xff;
  1240. buf[2] = (bl_num >> 8) & 0xff;
  1241. buf[3] = (bl_num >> 0) & 0xff;
  1242. buf[4] = (bl_len >> 24) & 0xff;
  1243. buf[5] = (bl_len >> 16) & 0xff;
  1244. buf[6] = (bl_len >> 8) & 0xff;
  1245. buf[7] = (bl_len >> 0) & 0xff;
  1246. usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
  1247. return USB_STOR_TRANSPORT_GOOD;
  1248. }
  1249. static void ms_lib_phy_to_log_range(u16 PhyBlock, u16 *LogStart, u16 *LogEnde)
  1250. {
  1251. PhyBlock /= MS_PHYSICAL_BLOCKS_PER_SEGMENT;
  1252. if (PhyBlock) {
  1253. *LogStart = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT + (PhyBlock - 1) * MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
  1254. *LogEnde = *LogStart + MS_LOGICAL_BLOCKS_PER_SEGMENT;/*496*/
  1255. } else {
  1256. *LogStart = 0;
  1257. *LogEnde = MS_LOGICAL_BLOCKS_IN_1ST_SEGMENT;/*494*/
  1258. }
  1259. }
  1260. static int ms_lib_read_extrablock(struct us_data *us, u32 PhyBlock,
  1261. u8 PageNum, u8 blen, void *buf)
  1262. {
  1263. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1264. int result;
  1265. /* printk("MS_LibReadExtraBlock --- PhyBlock = %x,
  1266. PageNum = %x, blen = %x\n", PhyBlock, PageNum, blen); */
  1267. /* Read Extra Data */
  1268. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1269. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1270. bcb->DataTransferLength = 0x4 * blen;
  1271. bcb->Flags = US_BULK_FLAG_IN;
  1272. bcb->CDB[0] = 0xF1;
  1273. bcb->CDB[1] = 0x03;
  1274. bcb->CDB[5] = (unsigned char)(PageNum);
  1275. bcb->CDB[4] = (unsigned char)(PhyBlock);
  1276. bcb->CDB[3] = (unsigned char)(PhyBlock>>8);
  1277. bcb->CDB[2] = (unsigned char)(PhyBlock>>16);
  1278. bcb->CDB[6] = blen;
  1279. result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
  1280. if (result != USB_STOR_XFER_GOOD)
  1281. return USB_STOR_TRANSPORT_ERROR;
  1282. return USB_STOR_TRANSPORT_GOOD;
  1283. }
  1284. static int ms_lib_scan_logicalblocknumber(struct us_data *us, u16 btBlk1st)
  1285. {
  1286. u16 PhyBlock, newblk, i;
  1287. u16 LogStart, LogEnde;
  1288. struct ms_lib_type_extdat extdat;
  1289. u8 buf[0x200];
  1290. u32 count = 0, index = 0;
  1291. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1292. for (PhyBlock = 0; PhyBlock < info->MS_Lib.NumberOfPhyBlock;) {
  1293. ms_lib_phy_to_log_range(PhyBlock, &LogStart, &LogEnde);
  1294. for (i = 0; i < MS_PHYSICAL_BLOCKS_PER_SEGMENT; i++, PhyBlock++) {
  1295. switch (ms_libconv_to_logical(info, PhyBlock)) {
  1296. case MS_STATUS_ERROR:
  1297. continue;
  1298. default:
  1299. break;
  1300. }
  1301. if (count == PhyBlock) {
  1302. ms_lib_read_extrablock(us, PhyBlock, 0, 0x80, &buf);
  1303. count += 0x80;
  1304. }
  1305. index = (PhyBlock % 0x80) * 4;
  1306. extdat.ovrflg = buf[index];
  1307. extdat.mngflg = buf[index+1];
  1308. extdat.logadr = memstick_logaddr(buf[index+2], buf[index+3]);
  1309. if ((extdat.ovrflg & MS_REG_OVR_BKST) != MS_REG_OVR_BKST_OK) {
  1310. ms_lib_setacquired_errorblock(us, PhyBlock);
  1311. continue;
  1312. }
  1313. if ((extdat.mngflg & MS_REG_MNG_ATFLG) == MS_REG_MNG_ATFLG_ATTBL) {
  1314. ms_lib_erase_phyblock(us, PhyBlock);
  1315. continue;
  1316. }
  1317. if (extdat.logadr != MS_LB_NOT_USED) {
  1318. if ((extdat.logadr < LogStart) || (LogEnde <= extdat.logadr)) {
  1319. ms_lib_erase_phyblock(us, PhyBlock);
  1320. continue;
  1321. }
  1322. newblk = ms_libconv_to_physical(info, extdat.logadr);
  1323. if (newblk != MS_LB_NOT_USED) {
  1324. if (extdat.logadr == 0) {
  1325. ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
  1326. if (ms_lib_check_disableblock(us, btBlk1st)) {
  1327. ms_lib_set_logicalpair(us, extdat.logadr, newblk);
  1328. continue;
  1329. }
  1330. }
  1331. ms_lib_read_extra(us, newblk, 0, &extdat);
  1332. if ((extdat.ovrflg & MS_REG_OVR_UDST) == MS_REG_OVR_UDST_UPDATING) {
  1333. ms_lib_erase_phyblock(us, PhyBlock);
  1334. continue;
  1335. } else {
  1336. ms_lib_erase_phyblock(us, newblk);
  1337. }
  1338. }
  1339. ms_lib_set_logicalpair(us, extdat.logadr, PhyBlock);
  1340. }
  1341. }
  1342. } /* End for ... */
  1343. return MS_STATUS_SUCCESS;
  1344. }
  1345. static int ms_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
  1346. {
  1347. int result;
  1348. unsigned char *cdb = srb->cmnd;
  1349. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1350. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1351. u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
  1352. ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
  1353. u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
  1354. u32 blenByte = blen * 0x200;
  1355. if (bn > info->bl_num)
  1356. return USB_STOR_TRANSPORT_ERROR;
  1357. if (info->MS_Status.IsMSPro) {
  1358. result = ene_load_bincode(us, MSP_RW_PATTERN);
  1359. if (result != USB_STOR_XFER_GOOD) {
  1360. US_DEBUGP("Load MPS RW pattern Fail !!\n");
  1361. return USB_STOR_TRANSPORT_ERROR;
  1362. }
  1363. /* set up the command wrapper */
  1364. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1365. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1366. bcb->DataTransferLength = blenByte;
  1367. bcb->Flags = US_BULK_FLAG_IN;
  1368. bcb->CDB[0] = 0xF1;
  1369. bcb->CDB[1] = 0x02;
  1370. bcb->CDB[5] = (unsigned char)(bn);
  1371. bcb->CDB[4] = (unsigned char)(bn>>8);
  1372. bcb->CDB[3] = (unsigned char)(bn>>16);
  1373. bcb->CDB[2] = (unsigned char)(bn>>24);
  1374. result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
  1375. } else {
  1376. void *buf;
  1377. int offset = 0;
  1378. u16 phyblk, logblk;
  1379. u8 PageNum;
  1380. u16 len;
  1381. u32 blkno;
  1382. buf = kmalloc(blenByte, GFP_KERNEL);
  1383. if (buf == NULL)
  1384. return USB_STOR_TRANSPORT_ERROR;
  1385. result = ene_load_bincode(us, MS_RW_PATTERN);
  1386. if (result != USB_STOR_XFER_GOOD) {
  1387. pr_info("Load MS RW pattern Fail !!\n");
  1388. result = USB_STOR_TRANSPORT_ERROR;
  1389. goto exit;
  1390. }
  1391. logblk = (u16)(bn / info->MS_Lib.PagesPerBlock);
  1392. PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
  1393. while (1) {
  1394. if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
  1395. len = info->MS_Lib.PagesPerBlock-PageNum;
  1396. else
  1397. len = blen;
  1398. phyblk = ms_libconv_to_physical(info, logblk);
  1399. blkno = phyblk * 0x20 + PageNum;
  1400. /* set up the command wrapper */
  1401. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1402. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1403. bcb->DataTransferLength = 0x200 * len;
  1404. bcb->Flags = US_BULK_FLAG_IN;
  1405. bcb->CDB[0] = 0xF1;
  1406. bcb->CDB[1] = 0x02;
  1407. bcb->CDB[5] = (unsigned char)(blkno);
  1408. bcb->CDB[4] = (unsigned char)(blkno>>8);
  1409. bcb->CDB[3] = (unsigned char)(blkno>>16);
  1410. bcb->CDB[2] = (unsigned char)(blkno>>24);
  1411. result = ene_send_scsi_cmd(us, FDIR_READ, buf+offset, 0);
  1412. if (result != USB_STOR_XFER_GOOD) {
  1413. pr_info("MS_SCSI_Read --- result = %x\n", result);
  1414. result = USB_STOR_TRANSPORT_ERROR;
  1415. goto exit;
  1416. }
  1417. blen -= len;
  1418. if (blen <= 0)
  1419. break;
  1420. logblk++;
  1421. PageNum = 0;
  1422. offset += MS_BYTES_PER_PAGE*len;
  1423. }
  1424. usb_stor_set_xfer_buf(buf, blenByte, srb);
  1425. exit:
  1426. kfree(buf);
  1427. }
  1428. return result;
  1429. }
  1430. static int ms_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
  1431. {
  1432. int result;
  1433. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1434. unsigned char *cdb = srb->cmnd;
  1435. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1436. u32 bn = ((cdb[2] << 24) & 0xff000000) |
  1437. ((cdb[3] << 16) & 0x00ff0000) |
  1438. ((cdb[4] << 8) & 0x0000ff00) |
  1439. ((cdb[5] << 0) & 0x000000ff);
  1440. u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
  1441. u32 blenByte = blen * 0x200;
  1442. if (bn > info->bl_num)
  1443. return USB_STOR_TRANSPORT_ERROR;
  1444. if (info->MS_Status.IsMSPro) {
  1445. result = ene_load_bincode(us, MSP_RW_PATTERN);
  1446. if (result != USB_STOR_XFER_GOOD) {
  1447. pr_info("Load MSP RW pattern Fail !!\n");
  1448. return USB_STOR_TRANSPORT_ERROR;
  1449. }
  1450. /* set up the command wrapper */
  1451. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1452. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1453. bcb->DataTransferLength = blenByte;
  1454. bcb->Flags = 0x00;
  1455. bcb->CDB[0] = 0xF0;
  1456. bcb->CDB[1] = 0x04;
  1457. bcb->CDB[5] = (unsigned char)(bn);
  1458. bcb->CDB[4] = (unsigned char)(bn>>8);
  1459. bcb->CDB[3] = (unsigned char)(bn>>16);
  1460. bcb->CDB[2] = (unsigned char)(bn>>24);
  1461. result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
  1462. } else {
  1463. void *buf;
  1464. int offset = 0;
  1465. u16 PhyBlockAddr;
  1466. u8 PageNum;
  1467. u16 len, oldphy, newphy;
  1468. buf = kmalloc(blenByte, GFP_KERNEL);
  1469. if (buf == NULL)
  1470. return USB_STOR_TRANSPORT_ERROR;
  1471. usb_stor_set_xfer_buf(buf, blenByte, srb);
  1472. result = ene_load_bincode(us, MS_RW_PATTERN);
  1473. if (result != USB_STOR_XFER_GOOD) {
  1474. pr_info("Load MS RW pattern Fail !!\n");
  1475. result = USB_STOR_TRANSPORT_ERROR;
  1476. goto exit;
  1477. }
  1478. PhyBlockAddr = (u16)(bn / info->MS_Lib.PagesPerBlock);
  1479. PageNum = (u8)(bn % info->MS_Lib.PagesPerBlock);
  1480. while (1) {
  1481. if (blen > (info->MS_Lib.PagesPerBlock-PageNum))
  1482. len = info->MS_Lib.PagesPerBlock-PageNum;
  1483. else
  1484. len = blen;
  1485. oldphy = ms_libconv_to_physical(info, PhyBlockAddr); /* need check us <-> info */
  1486. newphy = ms_libsearch_block_from_logical(us, PhyBlockAddr);
  1487. result = ms_read_copyblock(us, oldphy, newphy, PhyBlockAddr, PageNum, buf+offset, len);
  1488. if (result != USB_STOR_XFER_GOOD) {
  1489. pr_info("MS_SCSI_Write --- result = %x\n", result);
  1490. result = USB_STOR_TRANSPORT_ERROR;
  1491. goto exit;
  1492. }
  1493. info->MS_Lib.Phy2LogMap[oldphy] = MS_LB_NOT_USED_ERASED;
  1494. ms_lib_force_setlogical_pair(us, PhyBlockAddr, newphy);
  1495. blen -= len;
  1496. if (blen <= 0)
  1497. break;
  1498. PhyBlockAddr++;
  1499. PageNum = 0;
  1500. offset += MS_BYTES_PER_PAGE*len;
  1501. }
  1502. exit:
  1503. kfree(buf);
  1504. }
  1505. return result;
  1506. }
  1507. /*
  1508. * ENE MS Card
  1509. */
  1510. static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
  1511. {
  1512. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1513. int result;
  1514. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1515. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1516. bcb->DataTransferLength = 0x01;
  1517. bcb->Flags = US_BULK_FLAG_IN;
  1518. bcb->CDB[0] = 0xED;
  1519. bcb->CDB[2] = (unsigned char)(index>>8);
  1520. bcb->CDB[3] = (unsigned char)index;
  1521. result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
  1522. return result;
  1523. }
  1524. static int ene_get_card_status(struct us_data *us, u8 *buf)
  1525. {
  1526. u16 tmpreg;
  1527. u32 reg4b;
  1528. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1529. /*US_DEBUGP("transport --- ENE_ReadSDReg\n");*/
  1530. reg4b = *(u32 *)&buf[0x18];
  1531. info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
  1532. tmpreg = (u16) reg4b;
  1533. reg4b = *(u32 *)(&buf[0x14]);
  1534. if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
  1535. info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
  1536. info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
  1537. info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
  1538. if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
  1539. info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
  1540. if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
  1541. info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
  1542. info->SD_READ_BL_LEN = SD_BLOCK_LEN;
  1543. } else {
  1544. info->SD_Block_Mult = 1;
  1545. }
  1546. return USB_STOR_TRANSPORT_GOOD;
  1547. }
  1548. static int ene_load_bincode(struct us_data *us, unsigned char flag)
  1549. {
  1550. int err;
  1551. char *fw_name = NULL;
  1552. unsigned char *buf = NULL;
  1553. const struct firmware *sd_fw = NULL;
  1554. int result = USB_STOR_TRANSPORT_ERROR;
  1555. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1556. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1557. if (info->BIN_FLAG == flag)
  1558. return USB_STOR_TRANSPORT_GOOD;
  1559. switch (flag) {
  1560. /* For SD */
  1561. case SD_INIT1_PATTERN:
  1562. US_DEBUGP("SD_INIT1_PATTERN\n");
  1563. fw_name = SD_INIT1_FIRMWARE;
  1564. break;
  1565. case SD_INIT2_PATTERN:
  1566. US_DEBUGP("SD_INIT2_PATTERN\n");
  1567. fw_name = SD_INIT2_FIRMWARE;
  1568. break;
  1569. case SD_RW_PATTERN:
  1570. US_DEBUGP("SD_RW_PATTERN\n");
  1571. fw_name = SD_RW_FIRMWARE;
  1572. break;
  1573. /* For MS */
  1574. case MS_INIT_PATTERN:
  1575. US_DEBUGP("MS_INIT_PATTERN\n");
  1576. fw_name = MS_INIT_FIRMWARE;
  1577. break;
  1578. case MSP_RW_PATTERN:
  1579. US_DEBUGP("MSP_RW_PATTERN\n");
  1580. fw_name = MSP_RW_FIRMWARE;
  1581. break;
  1582. case MS_RW_PATTERN:
  1583. US_DEBUGP("MS_RW_PATTERN\n");
  1584. fw_name = MS_RW_FIRMWARE;
  1585. break;
  1586. default:
  1587. US_DEBUGP("----------- Unknown PATTERN ----------\n");
  1588. goto nofw;
  1589. }
  1590. err = request_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
  1591. if (err) {
  1592. US_DEBUGP("load firmware %s failed\n", fw_name);
  1593. goto nofw;
  1594. }
  1595. buf = kmalloc(sd_fw->size, GFP_KERNEL);
  1596. if (buf == NULL) {
  1597. US_DEBUGP("Malloc memory for fireware failed!\n");
  1598. goto nofw;
  1599. }
  1600. memcpy(buf, sd_fw->data, sd_fw->size);
  1601. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1602. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1603. bcb->DataTransferLength = sd_fw->size;
  1604. bcb->Flags = 0x00;
  1605. bcb->CDB[0] = 0xEF;
  1606. result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
  1607. info->BIN_FLAG = flag;
  1608. kfree(buf);
  1609. nofw:
  1610. release_firmware(sd_fw);
  1611. return result;
  1612. }
  1613. static int ms_card_init(struct us_data *us)
  1614. {
  1615. u32 result;
  1616. u16 TmpBlock;
  1617. unsigned char *PageBuffer0 = NULL, *PageBuffer1 = NULL;
  1618. struct ms_lib_type_extdat extdat;
  1619. u16 btBlk1st, btBlk2nd;
  1620. u32 btBlk1stErred;
  1621. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1622. printk(KERN_INFO "MS_CardInit start\n");
  1623. ms_lib_free_allocatedarea(us); /* Clean buffer and set struct us_data flag to 0 */
  1624. /* get two PageBuffer */
  1625. PageBuffer0 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
  1626. PageBuffer1 = kmalloc(MS_BYTES_PER_PAGE, GFP_KERNEL);
  1627. if ((PageBuffer0 == NULL) || (PageBuffer1 == NULL)) {
  1628. result = MS_NO_MEMORY_ERROR;
  1629. goto exit;
  1630. }
  1631. btBlk1st = btBlk2nd = MS_LB_NOT_USED;
  1632. btBlk1stErred = 0;
  1633. for (TmpBlock = 0; TmpBlock < MS_MAX_INITIAL_ERROR_BLOCKS+2; TmpBlock++) {
  1634. switch (ms_read_readpage(us, TmpBlock, 0, (u32 *)PageBuffer0, &extdat)) {
  1635. case MS_STATUS_SUCCESS:
  1636. break;
  1637. case MS_STATUS_INT_ERROR:
  1638. break;
  1639. case MS_STATUS_ERROR:
  1640. default:
  1641. continue;
  1642. }
  1643. if ((extdat.ovrflg & MS_REG_OVR_BKST) == MS_REG_OVR_BKST_NG)
  1644. continue;
  1645. if (((extdat.mngflg & MS_REG_MNG_SYSFLG) == MS_REG_MNG_SYSFLG_USER) ||
  1646. (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wBlockID) != MS_BOOT_BLOCK_ID) ||
  1647. (be16_to_cpu(((struct ms_bootblock_page0 *)PageBuffer0)->header.wFormatVersion) != MS_BOOT_BLOCK_FORMAT_VERSION) ||
  1648. (((struct ms_bootblock_page0 *)PageBuffer0)->header.bNumberOfDataEntry != MS_BOOT_BLOCK_DATA_ENTRIES))
  1649. continue;
  1650. if (btBlk1st != MS_LB_NOT_USED) {
  1651. btBlk2nd = TmpBlock;
  1652. break;
  1653. }
  1654. btBlk1st = TmpBlock;
  1655. memcpy(PageBuffer1, PageBuffer0, MS_BYTES_PER_PAGE);
  1656. if (extdat.status1 & (MS_REG_ST1_DTER | MS_REG_ST1_EXER | MS_REG_ST1_FGER))
  1657. btBlk1stErred = 1;
  1658. }
  1659. if (btBlk1st == MS_LB_NOT_USED) {
  1660. result = MS_STATUS_ERROR;
  1661. goto exit;
  1662. }
  1663. /* write protect */
  1664. if ((extdat.status0 & MS_REG_ST0_WP) == MS_REG_ST0_WP_ON)
  1665. ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
  1666. result = MS_STATUS_ERROR;
  1667. /* 1st Boot Block */
  1668. if (btBlk1stErred == 0)
  1669. result = ms_lib_process_bootblock(us, btBlk1st, PageBuffer1);
  1670. /* 1st */
  1671. /* 2nd Boot Block */
  1672. if (result && (btBlk2nd != MS_LB_NOT_USED))
  1673. result = ms_lib_process_bootblock(us, btBlk2nd, PageBuffer0);
  1674. if (result) {
  1675. result = MS_STATUS_ERROR;
  1676. goto exit;
  1677. }
  1678. for (TmpBlock = 0; TmpBlock < btBlk1st; TmpBlock++)
  1679. info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
  1680. info->MS_Lib.Phy2LogMap[btBlk1st] = MS_LB_BOOT_BLOCK;
  1681. if (btBlk2nd != MS_LB_NOT_USED) {
  1682. for (TmpBlock = btBlk1st + 1; TmpBlock < btBlk2nd; TmpBlock++)
  1683. info->MS_Lib.Phy2LogMap[TmpBlock] = MS_LB_INITIAL_ERROR;
  1684. info->MS_Lib.Phy2LogMap[btBlk2nd] = MS_LB_BOOT_BLOCK;
  1685. }
  1686. result = ms_lib_scan_logicalblocknumber(us, btBlk1st);
  1687. if (result)
  1688. goto exit;
  1689. for (TmpBlock = MS_PHYSICAL_BLOCKS_PER_SEGMENT;
  1690. TmpBlock < info->MS_Lib.NumberOfPhyBlock;
  1691. TmpBlock += MS_PHYSICAL_BLOCKS_PER_SEGMENT) {
  1692. if (ms_count_freeblock(us, TmpBlock) == 0) {
  1693. ms_lib_ctrl_set(info, MS_LIB_CTRL_WRPROTECT);
  1694. break;
  1695. }
  1696. }
  1697. /* write */
  1698. if (ms_lib_alloc_writebuf(us)) {
  1699. result = MS_NO_MEMORY_ERROR;
  1700. goto exit;
  1701. }
  1702. result = MS_STATUS_SUCCESS;
  1703. exit:
  1704. kfree(PageBuffer1);
  1705. kfree(PageBuffer0);
  1706. printk(KERN_INFO "MS_CardInit end\n");
  1707. return result;
  1708. }
  1709. static int ene_ms_init(struct us_data *us)
  1710. {
  1711. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1712. int result;
  1713. u8 buf[0x200];
  1714. u16 MSP_BlockSize, MSP_UserAreaBlocks;
  1715. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1716. printk(KERN_INFO "transport --- ENE_MSInit\n");
  1717. /* the same part to test ENE */
  1718. result = ene_load_bincode(us, MS_INIT_PATTERN);
  1719. if (result != USB_STOR_XFER_GOOD) {
  1720. printk(KERN_ERR "Load MS Init Code Fail !!\n");
  1721. return USB_STOR_TRANSPORT_ERROR;
  1722. }
  1723. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1724. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1725. bcb->DataTransferLength = 0x200;
  1726. bcb->Flags = US_BULK_FLAG_IN;
  1727. bcb->CDB[0] = 0xF1;
  1728. bcb->CDB[1] = 0x01;
  1729. result = ene_send_scsi_cmd(us, FDIR_READ, &buf, 0);
  1730. if (result != USB_STOR_XFER_GOOD) {
  1731. printk(KERN_ERR "Execution MS Init Code Fail !!\n");
  1732. return USB_STOR_TRANSPORT_ERROR;
  1733. }
  1734. /* the same part to test ENE */
  1735. info->MS_Status = *(struct MS_STATUS *)&buf[0];
  1736. if (info->MS_Status.Insert && info->MS_Status.Ready) {
  1737. printk(KERN_INFO "Insert = %x\n", info->MS_Status.Insert);
  1738. printk(KERN_INFO "Ready = %x\n", info->MS_Status.Ready);
  1739. printk(KERN_INFO "IsMSPro = %x\n", info->MS_Status.IsMSPro);
  1740. printk(KERN_INFO "IsMSPHG = %x\n", info->MS_Status.IsMSPHG);
  1741. printk(KERN_INFO "WtP= %x\n", info->MS_Status.WtP);
  1742. if (info->MS_Status.IsMSPro) {
  1743. MSP_BlockSize = (buf[6] << 8) | buf[7];
  1744. MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
  1745. info->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
  1746. } else {
  1747. ms_card_init(us); /* Card is MS (to ms.c)*/
  1748. }
  1749. US_DEBUGP("MS Init Code OK !!\n");
  1750. } else {
  1751. US_DEBUGP("MS Card Not Ready --- %x\n", buf[0]);
  1752. return USB_STOR_TRANSPORT_ERROR;
  1753. }
  1754. return USB_STOR_TRANSPORT_GOOD;
  1755. }
  1756. static int ene_sd_init(struct us_data *us)
  1757. {
  1758. int result;
  1759. u8 buf[0x200];
  1760. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  1761. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  1762. US_DEBUGP("transport --- ENE_SDInit\n");
  1763. /* SD Init Part-1 */
  1764. result = ene_load_bincode(us, SD_INIT1_PATTERN);
  1765. if (result != USB_STOR_XFER_GOOD) {
  1766. US_DEBUGP("Load SD Init Code Part-1 Fail !!\n");
  1767. return USB_STOR_TRANSPORT_ERROR;
  1768. }
  1769. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1770. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1771. bcb->Flags = US_BULK_FLAG_IN;
  1772. bcb->CDB[0] = 0xF2;
  1773. result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
  1774. if (result != USB_STOR_XFER_GOOD) {
  1775. US_DEBUGP("Execution SD Init Code Fail !!\n");
  1776. return USB_STOR_TRANSPORT_ERROR;
  1777. }
  1778. /* SD Init Part-2 */
  1779. result = ene_load_bincode(us, SD_INIT2_PATTERN);
  1780. if (result != USB_STOR_XFER_GOOD) {
  1781. US_DEBUGP("Load SD Init Code Part-2 Fail !!\n");
  1782. return USB_STOR_TRANSPORT_ERROR;
  1783. }
  1784. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  1785. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  1786. bcb->DataTransferLength = 0x200;
  1787. bcb->Flags = US_BULK_FLAG_IN;
  1788. bcb->CDB[0] = 0xF1;
  1789. result = ene_send_scsi_cmd(us, FDIR_READ, &buf, 0);
  1790. if (result != USB_STOR_XFER_GOOD) {
  1791. US_DEBUGP("Execution SD Init Code Fail !!\n");
  1792. return USB_STOR_TRANSPORT_ERROR;
  1793. }
  1794. info->SD_Status = *(struct SD_STATUS *)&buf[0];
  1795. if (info->SD_Status.Insert && info->SD_Status.Ready) {
  1796. ene_get_card_status(us, (unsigned char *)&buf);
  1797. US_DEBUGP("Insert = %x\n", info->SD_Status.Insert);
  1798. US_DEBUGP("Ready = %x\n", info->SD_Status.Ready);
  1799. US_DEBUGP("IsMMC = %x\n", info->SD_Status.IsMMC);
  1800. US_DEBUGP("HiCapacity = %x\n", info->SD_Status.HiCapacity);
  1801. US_DEBUGP("HiSpeed = %x\n", info->SD_Status.HiSpeed);
  1802. US_DEBUGP("WtP = %x\n", info->SD_Status.WtP);
  1803. } else {
  1804. US_DEBUGP("SD Card Not Ready --- %x\n", buf[0]);
  1805. return USB_STOR_TRANSPORT_ERROR;
  1806. }
  1807. return USB_STOR_TRANSPORT_GOOD;
  1808. }
  1809. static int ene_init(struct us_data *us)
  1810. {
  1811. int result;
  1812. u8 misc_reg03 = 0;
  1813. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  1814. result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
  1815. if (result != USB_STOR_XFER_GOOD)
  1816. return USB_STOR_TRANSPORT_ERROR;
  1817. if (misc_reg03 & 0x01) {
  1818. if (!info->SD_Status.Ready) {
  1819. result = ene_sd_init(us);
  1820. if (result != USB_STOR_XFER_GOOD)
  1821. return USB_STOR_TRANSPORT_ERROR;
  1822. }
  1823. }
  1824. if (misc_reg03 & 0x02) {
  1825. if (!info->MS_Status.Ready) {
  1826. result = ene_ms_init(us);
  1827. if (result != USB_STOR_XFER_GOOD)
  1828. return USB_STOR_TRANSPORT_ERROR;
  1829. }
  1830. }
  1831. return result;
  1832. }
  1833. /*----- sd_scsi_irp() ---------*/
  1834. static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
  1835. {
  1836. int result;
  1837. struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
  1838. info->SrbStatus = SS_SUCCESS;
  1839. switch (srb->cmnd[0]) {
  1840. case TEST_UNIT_READY:
  1841. result = sd_scsi_test_unit_ready(us, srb);
  1842. break; /* 0x00 */
  1843. case INQUIRY:
  1844. result = sd_scsi_inquiry(us, srb);
  1845. break; /* 0x12 */
  1846. case MODE_SENSE:
  1847. result = sd_scsi_mode_sense(us, srb);
  1848. break; /* 0x1A */
  1849. /*
  1850. case START_STOP:
  1851. result = SD_SCSI_Start_Stop(us, srb);
  1852. break; //0x1B
  1853. */
  1854. case READ_CAPACITY:
  1855. result = sd_scsi_read_capacity(us, srb);
  1856. break; /* 0x25 */
  1857. case READ_10:
  1858. result = sd_scsi_read(us, srb);
  1859. break; /* 0x28 */
  1860. case WRITE_10:
  1861. result = sd_scsi_write(us, srb);
  1862. break; /* 0x2A */
  1863. default:
  1864. info->SrbStatus = SS_ILLEGAL_REQUEST;
  1865. result = USB_STOR_TRANSPORT_FAILED;
  1866. break;
  1867. }
  1868. return result;
  1869. }
  1870. /*
  1871. * ms_scsi_irp()
  1872. */
  1873. static int ms_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
  1874. {
  1875. int result;
  1876. struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
  1877. info->SrbStatus = SS_SUCCESS;
  1878. switch (srb->cmnd[0]) {
  1879. case TEST_UNIT_READY:
  1880. result = ms_scsi_test_unit_ready(us, srb);
  1881. break; /* 0x00 */
  1882. case INQUIRY:
  1883. result = ms_scsi_inquiry(us, srb);
  1884. break; /* 0x12 */
  1885. case MODE_SENSE:
  1886. result = ms_scsi_mode_sense(us, srb);
  1887. break; /* 0x1A */
  1888. case READ_CAPACITY:
  1889. result = ms_scsi_read_capacity(us, srb);
  1890. break; /* 0x25 */
  1891. case READ_10:
  1892. result = ms_scsi_read(us, srb);
  1893. break; /* 0x28 */
  1894. case WRITE_10:
  1895. result = ms_scsi_write(us, srb);
  1896. break; /* 0x2A */
  1897. default:
  1898. info->SrbStatus = SS_ILLEGAL_REQUEST;
  1899. result = USB_STOR_TRANSPORT_FAILED;
  1900. break;
  1901. }
  1902. return result;
  1903. }
  1904. static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
  1905. {
  1906. int result = 0;
  1907. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  1908. /*US_DEBUG(usb_stor_show_command(srb)); */
  1909. scsi_set_resid(srb, 0);
  1910. if (unlikely(!(info->SD_Status.Ready || info->MS_Status.Ready))) {
  1911. result = ene_init(us);
  1912. } else {
  1913. if (info->SD_Status.Ready)
  1914. result = sd_scsi_irp(us, srb);
  1915. if (info->MS_Status.Ready)
  1916. result = ms_scsi_irp(us, srb);
  1917. }
  1918. return 0;
  1919. }
  1920. static int ene_ub6250_probe(struct usb_interface *intf,
  1921. const struct usb_device_id *id)
  1922. {
  1923. int result;
  1924. u8 misc_reg03 = 0;
  1925. struct us_data *us;
  1926. result = usb_stor_probe1(&us, intf, id,
  1927. (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list);
  1928. if (result)
  1929. return result;
  1930. /* FIXME: where should the code alloc extra buf ? */
  1931. if (!us->extra) {
  1932. us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
  1933. if (!us->extra)
  1934. return -ENOMEM;
  1935. us->extra_destructor = ene_ub6250_info_destructor;
  1936. }
  1937. us->transport_name = "ene_ub6250";
  1938. us->transport = ene_transport;
  1939. us->max_lun = 0;
  1940. result = usb_stor_probe2(us);
  1941. if (result)
  1942. return result;
  1943. /* probe card type */
  1944. result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
  1945. if (result != USB_STOR_XFER_GOOD) {
  1946. usb_stor_disconnect(intf);
  1947. return USB_STOR_TRANSPORT_ERROR;
  1948. }
  1949. if (!(misc_reg03 & 0x01)) {
  1950. pr_info("ums_eneub6250: The driver only supports SD/MS card. "
  1951. "To use SM card, please build driver/staging/keucr\n");
  1952. }
  1953. return result;
  1954. }
  1955. #ifdef CONFIG_PM
  1956. static int ene_ub6250_resume(struct usb_interface *iface)
  1957. {
  1958. u8 tmp = 0;
  1959. struct us_data *us = usb_get_intfdata(iface);
  1960. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  1961. mutex_lock(&us->dev_mutex);
  1962. US_DEBUGP("%s\n", __func__);
  1963. if (us->suspend_resume_hook)
  1964. (us->suspend_resume_hook)(us, US_RESUME);
  1965. mutex_unlock(&us->dev_mutex);
  1966. info->Power_IsResum = true;
  1967. /*info->SD_Status.Ready = 0; */
  1968. info->SD_Status = *(struct SD_STATUS *)&tmp;
  1969. info->MS_Status = *(struct MS_STATUS *)&tmp;
  1970. info->SM_Status = *(struct SM_STATUS *)&tmp;
  1971. return 0;
  1972. }
  1973. static int ene_ub6250_reset_resume(struct usb_interface *iface)
  1974. {
  1975. u8 tmp = 0;
  1976. struct us_data *us = usb_get_intfdata(iface);
  1977. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  1978. US_DEBUGP("%s\n", __func__);
  1979. /* Report the reset to the SCSI core */
  1980. usb_stor_reset_resume(iface);
  1981. /* FIXME: Notify the subdrivers that they need to reinitialize
  1982. * the device */
  1983. info->Power_IsResum = true;
  1984. /*info->SD_Status.Ready = 0; */
  1985. info->SD_Status = *(struct SD_STATUS *)&tmp;
  1986. info->MS_Status = *(struct MS_STATUS *)&tmp;
  1987. info->SM_Status = *(struct SM_STATUS *)&tmp;
  1988. return 0;
  1989. }
  1990. #else
  1991. #define ene_ub6250_resume NULL
  1992. #define ene_ub6250_reset_resume NULL
  1993. #endif
  1994. static struct usb_driver ene_ub6250_driver = {
  1995. .name = "ums_eneub6250",
  1996. .probe = ene_ub6250_probe,
  1997. .disconnect = usb_stor_disconnect,
  1998. .suspend = usb_stor_suspend,
  1999. .resume = ene_ub6250_resume,
  2000. .reset_resume = ene_ub6250_reset_resume,
  2001. .pre_reset = usb_stor_pre_reset,
  2002. .post_reset = usb_stor_post_reset,
  2003. .id_table = ene_ub6250_usb_ids,
  2004. .soft_unbind = 1,
  2005. .no_dynamic_id = 1,
  2006. };
  2007. module_usb_driver(ene_ub6250_driver);