ene_ub6250.c 67 KB

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