ene_ub6250.c 67 KB

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