ene_ub6250.c 67 KB

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