ene_ub6250.c 66 KB

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