ene_ub6250.c 68 KB

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