ene_ub6250.c 66 KB

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