ene_ub6250.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. 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. struct SD_STATUS {
  86. u8 Insert:1;
  87. u8 Ready:1;
  88. u8 MediaChange:1;
  89. u8 IsMMC:1;
  90. u8 HiCapacity:1;
  91. u8 HiSpeed:1;
  92. u8 WtP:1;
  93. u8 Reserved:1;
  94. };
  95. struct MS_STATUS {
  96. u8 Insert:1;
  97. u8 Ready:1;
  98. u8 MediaChange:1;
  99. u8 IsMSPro:1;
  100. u8 IsMSPHG:1;
  101. u8 Reserved1:1;
  102. u8 WtP:1;
  103. u8 Reserved2:1;
  104. };
  105. struct SM_STATUS {
  106. u8 Insert:1;
  107. u8 Ready:1;
  108. u8 MediaChange:1;
  109. u8 Reserved:3;
  110. u8 WtP:1;
  111. u8 IsMS:1;
  112. };
  113. /* SD Block Length */
  114. /* 2^9 = 512 Bytes, The HW maximum read/write data length */
  115. #define SD_BLOCK_LEN 9
  116. struct ene_ub6250_info {
  117. /* for 6250 code */
  118. struct SD_STATUS SD_Status;
  119. struct MS_STATUS MS_Status;
  120. struct SM_STATUS SM_Status;
  121. /* ----- SD Control Data ---------------- */
  122. /*SD_REGISTER SD_Regs; */
  123. u16 SD_Block_Mult;
  124. u8 SD_READ_BL_LEN;
  125. u16 SD_C_SIZE;
  126. u8 SD_C_SIZE_MULT;
  127. /* SD/MMC New spec. */
  128. u8 SD_SPEC_VER;
  129. u8 SD_CSD_VER;
  130. u8 SD20_HIGH_CAPACITY;
  131. u32 HC_C_SIZE;
  132. u8 MMC_SPEC_VER;
  133. u8 MMC_BusWidth;
  134. u8 MMC_HIGH_CAPACITY;
  135. /*----- MS Control Data ---------------- */
  136. bool MS_SWWP;
  137. u32 MSP_TotalBlock;
  138. /*MS_LibControl MS_Lib;*/
  139. bool MS_IsRWPage;
  140. u16 MS_Model;
  141. /*----- SM Control Data ---------------- */
  142. u8 SM_DeviceID;
  143. u8 SM_CardID;
  144. unsigned char *testbuf;
  145. u8 BIN_FLAG;
  146. u32 bl_num;
  147. int SrbStatus;
  148. /*------Power Managerment ---------------*/
  149. bool Power_IsResum;
  150. };
  151. static int ene_sd_init(struct us_data *us);
  152. static int ene_load_bincode(struct us_data *us, unsigned char flag);
  153. static void ene_ub6250_info_destructor(void *extra)
  154. {
  155. if (!extra)
  156. return;
  157. }
  158. static int ene_send_scsi_cmd(struct us_data *us, u8 fDir, void *buf, int use_sg)
  159. {
  160. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  161. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  162. int result;
  163. unsigned int residue;
  164. unsigned int cswlen = 0, partial = 0;
  165. unsigned int transfer_length = bcb->DataTransferLength;
  166. /* US_DEBUGP("transport --- ene_send_scsi_cmd\n"); */
  167. /* send cmd to out endpoint */
  168. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  169. bcb, US_BULK_CB_WRAP_LEN, NULL);
  170. if (result != USB_STOR_XFER_GOOD) {
  171. US_DEBUGP("send cmd to out endpoint fail ---\n");
  172. return USB_STOR_TRANSPORT_ERROR;
  173. }
  174. if (buf) {
  175. unsigned int pipe = fDir;
  176. if (fDir == FDIR_READ)
  177. pipe = us->recv_bulk_pipe;
  178. else
  179. pipe = us->send_bulk_pipe;
  180. /* Bulk */
  181. if (use_sg) {
  182. result = usb_stor_bulk_srb(us, pipe, us->srb);
  183. } else {
  184. result = usb_stor_bulk_transfer_sg(us, pipe, buf,
  185. transfer_length, 0, &partial);
  186. }
  187. if (result != USB_STOR_XFER_GOOD) {
  188. US_DEBUGP("data transfer fail ---\n");
  189. return USB_STOR_TRANSPORT_ERROR;
  190. }
  191. }
  192. /* Get CSW for device status */
  193. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
  194. US_BULK_CS_WRAP_LEN, &cswlen);
  195. if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
  196. US_DEBUGP("Received 0-length CSW; retrying...\n");
  197. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  198. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  199. }
  200. if (result == USB_STOR_XFER_STALLED) {
  201. /* get the status again */
  202. US_DEBUGP("Attempting to get CSW (2nd try)...\n");
  203. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  204. bcs, US_BULK_CS_WRAP_LEN, NULL);
  205. }
  206. if (result != USB_STOR_XFER_GOOD)
  207. return USB_STOR_TRANSPORT_ERROR;
  208. /* check bulk status */
  209. residue = le32_to_cpu(bcs->Residue);
  210. /* try to compute the actual residue, based on how much data
  211. * was really transferred and what the device tells us */
  212. if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
  213. residue = min(residue, transfer_length);
  214. if (us->srb != NULL)
  215. scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
  216. (int)residue));
  217. }
  218. if (bcs->Status != US_BULK_STAT_OK)
  219. return USB_STOR_TRANSPORT_ERROR;
  220. return USB_STOR_TRANSPORT_GOOD;
  221. }
  222. static int sd_scsi_test_unit_ready(struct us_data *us, struct scsi_cmnd *srb)
  223. {
  224. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  225. if (info->SD_Status.Insert && info->SD_Status.Ready)
  226. return USB_STOR_TRANSPORT_GOOD;
  227. else {
  228. ene_sd_init(us);
  229. return USB_STOR_TRANSPORT_GOOD;
  230. }
  231. return USB_STOR_TRANSPORT_GOOD;
  232. }
  233. static int sd_scsi_inquiry(struct us_data *us, struct scsi_cmnd *srb)
  234. {
  235. unsigned char data_ptr[36] = {
  236. 0x00, 0x80, 0x02, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x55,
  237. 0x53, 0x42, 0x32, 0x2E, 0x30, 0x20, 0x20, 0x43, 0x61,
  238. 0x72, 0x64, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72, 0x20,
  239. 0x20, 0x20, 0x20, 0x20, 0x20, 0x30, 0x31, 0x30, 0x30 };
  240. usb_stor_set_xfer_buf(data_ptr, 36, srb);
  241. return USB_STOR_TRANSPORT_GOOD;
  242. }
  243. static int sd_scsi_mode_sense(struct us_data *us, struct scsi_cmnd *srb)
  244. {
  245. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  246. unsigned char mediaNoWP[12] = {
  247. 0x0b, 0x00, 0x00, 0x08, 0x00, 0x00,
  248. 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
  249. unsigned char mediaWP[12] = {
  250. 0x0b, 0x00, 0x80, 0x08, 0x00, 0x00,
  251. 0x71, 0xc0, 0x00, 0x00, 0x02, 0x00 };
  252. if (info->SD_Status.WtP)
  253. usb_stor_set_xfer_buf(mediaWP, 12, srb);
  254. else
  255. usb_stor_set_xfer_buf(mediaNoWP, 12, srb);
  256. return USB_STOR_TRANSPORT_GOOD;
  257. }
  258. static int sd_scsi_read_capacity(struct us_data *us, struct scsi_cmnd *srb)
  259. {
  260. u32 bl_num;
  261. u16 bl_len;
  262. unsigned int offset = 0;
  263. unsigned char buf[8];
  264. struct scatterlist *sg = NULL;
  265. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  266. US_DEBUGP("sd_scsi_read_capacity\n");
  267. if (info->SD_Status.HiCapacity) {
  268. bl_len = 0x200;
  269. if (info->SD_Status.IsMMC)
  270. bl_num = info->HC_C_SIZE-1;
  271. else
  272. bl_num = (info->HC_C_SIZE + 1) * 1024 - 1;
  273. } else {
  274. bl_len = 1<<(info->SD_READ_BL_LEN);
  275. bl_num = info->SD_Block_Mult * (info->SD_C_SIZE + 1)
  276. * (1 << (info->SD_C_SIZE_MULT + 2)) - 1;
  277. }
  278. info->bl_num = bl_num;
  279. US_DEBUGP("bl_len = %x\n", bl_len);
  280. US_DEBUGP("bl_num = %x\n", bl_num);
  281. /*srb->request_bufflen = 8; */
  282. buf[0] = (bl_num >> 24) & 0xff;
  283. buf[1] = (bl_num >> 16) & 0xff;
  284. buf[2] = (bl_num >> 8) & 0xff;
  285. buf[3] = (bl_num >> 0) & 0xff;
  286. buf[4] = (bl_len >> 24) & 0xff;
  287. buf[5] = (bl_len >> 16) & 0xff;
  288. buf[6] = (bl_len >> 8) & 0xff;
  289. buf[7] = (bl_len >> 0) & 0xff;
  290. usb_stor_access_xfer_buf(buf, 8, srb, &sg, &offset, TO_XFER_BUF);
  291. return USB_STOR_TRANSPORT_GOOD;
  292. }
  293. static int sd_scsi_read(struct us_data *us, struct scsi_cmnd *srb)
  294. {
  295. int result;
  296. unsigned char *cdb = srb->cmnd;
  297. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  298. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  299. u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
  300. ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
  301. u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
  302. u32 bnByte = bn * 0x200;
  303. u32 blenByte = blen * 0x200;
  304. if (bn > info->bl_num)
  305. return USB_STOR_TRANSPORT_ERROR;
  306. result = ene_load_bincode(us, SD_RW_PATTERN);
  307. if (result != USB_STOR_XFER_GOOD) {
  308. US_DEBUGP("Load SD RW pattern Fail !!\n");
  309. return USB_STOR_TRANSPORT_ERROR;
  310. }
  311. if (info->SD_Status.HiCapacity)
  312. bnByte = bn;
  313. /* set up the command wrapper */
  314. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  315. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  316. bcb->DataTransferLength = blenByte;
  317. bcb->Flags = 0x80;
  318. bcb->CDB[0] = 0xF1;
  319. bcb->CDB[5] = (unsigned char)(bnByte);
  320. bcb->CDB[4] = (unsigned char)(bnByte>>8);
  321. bcb->CDB[3] = (unsigned char)(bnByte>>16);
  322. bcb->CDB[2] = (unsigned char)(bnByte>>24);
  323. result = ene_send_scsi_cmd(us, FDIR_READ, scsi_sglist(srb), 1);
  324. return result;
  325. }
  326. static int sd_scsi_write(struct us_data *us, struct scsi_cmnd *srb)
  327. {
  328. int result;
  329. unsigned char *cdb = srb->cmnd;
  330. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  331. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  332. u32 bn = ((cdb[2] << 24) & 0xff000000) | ((cdb[3] << 16) & 0x00ff0000) |
  333. ((cdb[4] << 8) & 0x0000ff00) | ((cdb[5] << 0) & 0x000000ff);
  334. u16 blen = ((cdb[7] << 8) & 0xff00) | ((cdb[8] << 0) & 0x00ff);
  335. u32 bnByte = bn * 0x200;
  336. u32 blenByte = blen * 0x200;
  337. if (bn > info->bl_num)
  338. return USB_STOR_TRANSPORT_ERROR;
  339. result = ene_load_bincode(us, SD_RW_PATTERN);
  340. if (result != USB_STOR_XFER_GOOD) {
  341. US_DEBUGP("Load SD RW pattern Fail !!\n");
  342. return USB_STOR_TRANSPORT_ERROR;
  343. }
  344. if (info->SD_Status.HiCapacity)
  345. bnByte = bn;
  346. /* set up the command wrapper */
  347. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  348. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  349. bcb->DataTransferLength = blenByte;
  350. bcb->Flags = 0x00;
  351. bcb->CDB[0] = 0xF0;
  352. bcb->CDB[5] = (unsigned char)(bnByte);
  353. bcb->CDB[4] = (unsigned char)(bnByte>>8);
  354. bcb->CDB[3] = (unsigned char)(bnByte>>16);
  355. bcb->CDB[2] = (unsigned char)(bnByte>>24);
  356. result = ene_send_scsi_cmd(us, FDIR_WRITE, scsi_sglist(srb), 1);
  357. return result;
  358. }
  359. static int ene_get_card_type(struct us_data *us, u16 index, void *buf)
  360. {
  361. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  362. int result;
  363. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  364. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  365. bcb->DataTransferLength = 0x01;
  366. bcb->Flags = 0x80;
  367. bcb->CDB[0] = 0xED;
  368. bcb->CDB[2] = (unsigned char)(index>>8);
  369. bcb->CDB[3] = (unsigned char)index;
  370. result = ene_send_scsi_cmd(us, FDIR_READ, buf, 0);
  371. return result;
  372. }
  373. static int ene_get_card_status(struct us_data *us, u8 *buf)
  374. {
  375. u16 tmpreg;
  376. u32 reg4b;
  377. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  378. /*US_DEBUGP("transport --- ENE_ReadSDReg\n");*/
  379. reg4b = *(u32 *)&buf[0x18];
  380. info->SD_READ_BL_LEN = (u8)((reg4b >> 8) & 0x0f);
  381. tmpreg = (u16) reg4b;
  382. reg4b = *(u32 *)(&buf[0x14]);
  383. if (info->SD_Status.HiCapacity && !info->SD_Status.IsMMC)
  384. info->HC_C_SIZE = (reg4b >> 8) & 0x3fffff;
  385. info->SD_C_SIZE = ((tmpreg & 0x03) << 10) | (u16)(reg4b >> 22);
  386. info->SD_C_SIZE_MULT = (u8)(reg4b >> 7) & 0x07;
  387. if (info->SD_Status.HiCapacity && info->SD_Status.IsMMC)
  388. info->HC_C_SIZE = *(u32 *)(&buf[0x100]);
  389. if (info->SD_READ_BL_LEN > SD_BLOCK_LEN) {
  390. info->SD_Block_Mult = 1 << (info->SD_READ_BL_LEN-SD_BLOCK_LEN);
  391. info->SD_READ_BL_LEN = SD_BLOCK_LEN;
  392. } else {
  393. info->SD_Block_Mult = 1;
  394. }
  395. return USB_STOR_TRANSPORT_GOOD;
  396. }
  397. static int ene_load_bincode(struct us_data *us, unsigned char flag)
  398. {
  399. int err;
  400. char *fw_name = NULL;
  401. unsigned char *buf = NULL;
  402. const struct firmware *sd_fw = NULL;
  403. int result = USB_STOR_TRANSPORT_ERROR;
  404. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  405. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  406. if (info->BIN_FLAG == flag)
  407. return USB_STOR_TRANSPORT_GOOD;
  408. switch (flag) {
  409. /* For SD */
  410. case SD_INIT1_PATTERN:
  411. US_DEBUGP("SD_INIT1_PATTERN\n");
  412. fw_name = "ene-ub6250/sd_init1.bin";
  413. break;
  414. case SD_INIT2_PATTERN:
  415. US_DEBUGP("SD_INIT2_PATTERN\n");
  416. fw_name = "ene-ub6250/sd_init2.bin";
  417. break;
  418. case SD_RW_PATTERN:
  419. US_DEBUGP("SD_RDWR_PATTERN\n");
  420. fw_name = "ene-ub6250/sd_rdwr.bin";
  421. break;
  422. default:
  423. US_DEBUGP("----------- Unknown PATTERN ----------\n");
  424. goto nofw;
  425. }
  426. err = request_firmware(&sd_fw, fw_name, &us->pusb_dev->dev);
  427. if (err) {
  428. US_DEBUGP("load firmware %s failed\n", fw_name);
  429. goto nofw;
  430. }
  431. buf = kmalloc(sd_fw->size, GFP_KERNEL);
  432. if (buf == NULL) {
  433. US_DEBUGP("Malloc memory for fireware failed!\n");
  434. goto nofw;
  435. }
  436. memcpy(buf, sd_fw->data, sd_fw->size);
  437. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  438. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  439. bcb->DataTransferLength = sd_fw->size;
  440. bcb->Flags = 0x00;
  441. bcb->CDB[0] = 0xEF;
  442. result = ene_send_scsi_cmd(us, FDIR_WRITE, buf, 0);
  443. info->BIN_FLAG = flag;
  444. kfree(buf);
  445. nofw:
  446. if (sd_fw != NULL) {
  447. release_firmware(sd_fw);
  448. sd_fw = NULL;
  449. }
  450. return result;
  451. }
  452. static int ene_sd_init(struct us_data *us)
  453. {
  454. int result;
  455. u8 buf[0x200];
  456. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  457. struct ene_ub6250_info *info = (struct ene_ub6250_info *) us->extra;
  458. US_DEBUGP("transport --- ENE_SDInit\n");
  459. /* SD Init Part-1 */
  460. result = ene_load_bincode(us, SD_INIT1_PATTERN);
  461. if (result != USB_STOR_XFER_GOOD) {
  462. US_DEBUGP("Load SD Init Code Part-1 Fail !!\n");
  463. return USB_STOR_TRANSPORT_ERROR;
  464. }
  465. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  466. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  467. bcb->Flags = 0x80;
  468. bcb->CDB[0] = 0xF2;
  469. result = ene_send_scsi_cmd(us, FDIR_READ, NULL, 0);
  470. if (result != USB_STOR_XFER_GOOD) {
  471. US_DEBUGP("Exection SD Init Code Fail !!\n");
  472. return USB_STOR_TRANSPORT_ERROR;
  473. }
  474. /* SD Init Part-2 */
  475. result = ene_load_bincode(us, SD_INIT2_PATTERN);
  476. if (result != USB_STOR_XFER_GOOD) {
  477. US_DEBUGP("Load SD Init Code Part-2 Fail !!\n");
  478. return USB_STOR_TRANSPORT_ERROR;
  479. }
  480. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  481. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  482. bcb->DataTransferLength = 0x200;
  483. bcb->Flags = 0x80;
  484. bcb->CDB[0] = 0xF1;
  485. result = ene_send_scsi_cmd(us, FDIR_READ, &buf, 0);
  486. if (result != USB_STOR_XFER_GOOD) {
  487. US_DEBUGP("Exection SD Init Code Fail !!\n");
  488. return USB_STOR_TRANSPORT_ERROR;
  489. }
  490. info->SD_Status = *(struct SD_STATUS *)&buf[0];
  491. if (info->SD_Status.Insert && info->SD_Status.Ready) {
  492. ene_get_card_status(us, (unsigned char *)&buf);
  493. US_DEBUGP("Insert = %x\n", info->SD_Status.Insert);
  494. US_DEBUGP("Ready = %x\n", info->SD_Status.Ready);
  495. US_DEBUGP("IsMMC = %x\n", info->SD_Status.IsMMC);
  496. US_DEBUGP("HiCapacity = %x\n", info->SD_Status.HiCapacity);
  497. US_DEBUGP("HiSpeed = %x\n", info->SD_Status.HiSpeed);
  498. US_DEBUGP("WtP = %x\n", info->SD_Status.WtP);
  499. } else {
  500. US_DEBUGP("SD Card Not Ready --- %x\n", buf[0]);
  501. return USB_STOR_TRANSPORT_ERROR;
  502. }
  503. return USB_STOR_TRANSPORT_GOOD;
  504. }
  505. static int ene_init(struct us_data *us)
  506. {
  507. int result;
  508. u8 misc_reg03 = 0;
  509. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  510. result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
  511. if (result != USB_STOR_XFER_GOOD)
  512. return USB_STOR_TRANSPORT_ERROR;
  513. if (misc_reg03 & 0x01) {
  514. if (!info->SD_Status.Ready) {
  515. result = ene_sd_init(us);
  516. if (result != USB_STOR_XFER_GOOD)
  517. return USB_STOR_TRANSPORT_ERROR;
  518. }
  519. }
  520. return result;
  521. }
  522. /*----- sd_scsi_irp() ---------*/
  523. static int sd_scsi_irp(struct us_data *us, struct scsi_cmnd *srb)
  524. {
  525. int result;
  526. struct ene_ub6250_info *info = (struct ene_ub6250_info *)us->extra;
  527. info->SrbStatus = SS_SUCCESS;
  528. switch (srb->cmnd[0]) {
  529. case TEST_UNIT_READY:
  530. result = sd_scsi_test_unit_ready(us, srb);
  531. break; /* 0x00 */
  532. case INQUIRY:
  533. result = sd_scsi_inquiry(us, srb);
  534. break; /* 0x12 */
  535. case MODE_SENSE:
  536. result = sd_scsi_mode_sense(us, srb);
  537. break; /* 0x1A */
  538. /*
  539. case START_STOP:
  540. result = SD_SCSI_Start_Stop(us, srb);
  541. break; //0x1B
  542. */
  543. case READ_CAPACITY:
  544. result = sd_scsi_read_capacity(us, srb);
  545. break; /* 0x25 */
  546. case READ_10:
  547. result = sd_scsi_read(us, srb);
  548. break; /* 0x28 */
  549. case WRITE_10:
  550. result = sd_scsi_write(us, srb);
  551. break; /* 0x2A */
  552. default:
  553. info->SrbStatus = SS_ILLEGAL_REQUEST;
  554. result = USB_STOR_TRANSPORT_FAILED;
  555. break;
  556. }
  557. return result;
  558. }
  559. static int ene_transport(struct scsi_cmnd *srb, struct us_data *us)
  560. {
  561. int result = 0;
  562. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  563. /*US_DEBUG(usb_stor_show_command(srb)); */
  564. scsi_set_resid(srb, 0);
  565. if (unlikely(!info->SD_Status.Ready))
  566. result = ene_init(us);
  567. else
  568. result = sd_scsi_irp(us, srb);
  569. return 0;
  570. }
  571. static int ene_ub6250_probe(struct usb_interface *intf,
  572. const struct usb_device_id *id)
  573. {
  574. int result;
  575. u8 misc_reg03 = 0;
  576. struct us_data *us;
  577. result = usb_stor_probe1(&us, intf, id,
  578. (id - ene_ub6250_usb_ids) + ene_ub6250_unusual_dev_list);
  579. if (result)
  580. return result;
  581. /* FIXME: where should the code alloc extra buf ? */
  582. if (!us->extra) {
  583. us->extra = kzalloc(sizeof(struct ene_ub6250_info), GFP_KERNEL);
  584. if (!us->extra)
  585. return -ENOMEM;
  586. us->extra_destructor = ene_ub6250_info_destructor;
  587. }
  588. us->transport_name = "ene_ub6250";
  589. us->transport = ene_transport;
  590. us->max_lun = 0;
  591. result = usb_stor_probe2(us);
  592. if (result)
  593. return result;
  594. /* probe card type */
  595. result = ene_get_card_type(us, REG_CARD_STATUS, &misc_reg03);
  596. if (result != USB_STOR_XFER_GOOD) {
  597. usb_stor_disconnect(intf);
  598. return USB_STOR_TRANSPORT_ERROR;
  599. }
  600. if (!(misc_reg03 & 0x01)) {
  601. result = -ENODEV;
  602. printk(KERN_NOTICE "ums_eneub6250: The driver only supports SD\
  603. card. To use SM/MS card, please build driver/stagging/keucr\n");
  604. usb_stor_disconnect(intf);
  605. }
  606. return result;
  607. }
  608. #ifdef CONFIG_PM
  609. static int ene_ub6250_resume(struct usb_interface *iface)
  610. {
  611. u8 tmp = 0;
  612. struct us_data *us = usb_get_intfdata(iface);
  613. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  614. mutex_lock(&us->dev_mutex);
  615. US_DEBUGP("%s\n", __func__);
  616. if (us->suspend_resume_hook)
  617. (us->suspend_resume_hook)(us, US_RESUME);
  618. mutex_unlock(&us->dev_mutex);
  619. info->Power_IsResum = true;
  620. /*info->SD_Status.Ready = 0; */
  621. info->SD_Status = *(struct SD_STATUS *)&tmp;
  622. info->MS_Status = *(struct MS_STATUS *)&tmp;
  623. info->SM_Status = *(struct SM_STATUS *)&tmp;
  624. return 0;
  625. }
  626. static int ene_ub6250_reset_resume(struct usb_interface *iface)
  627. {
  628. u8 tmp = 0;
  629. struct us_data *us = usb_get_intfdata(iface);
  630. struct ene_ub6250_info *info = (struct ene_ub6250_info *)(us->extra);
  631. US_DEBUGP("%s\n", __func__);
  632. /* Report the reset to the SCSI core */
  633. usb_stor_reset_resume(iface);
  634. /* FIXME: Notify the subdrivers that they need to reinitialize
  635. * the device */
  636. info->Power_IsResum = true;
  637. /*info->SD_Status.Ready = 0; */
  638. info->SD_Status = *(struct SD_STATUS *)&tmp;
  639. info->MS_Status = *(struct MS_STATUS *)&tmp;
  640. info->SM_Status = *(struct SM_STATUS *)&tmp;
  641. return 0;
  642. }
  643. #else
  644. #define ene_ub6250_resume NULL
  645. #define ene_ub6250_reset_resume NULL
  646. #endif
  647. static struct usb_driver ene_ub6250_driver = {
  648. .name = "ums_eneub6250",
  649. .probe = ene_ub6250_probe,
  650. .disconnect = usb_stor_disconnect,
  651. .suspend = usb_stor_suspend,
  652. .resume = ene_ub6250_resume,
  653. .reset_resume = ene_ub6250_reset_resume,
  654. .pre_reset = usb_stor_pre_reset,
  655. .post_reset = usb_stor_post_reset,
  656. .id_table = ene_ub6250_usb_ids,
  657. .soft_unbind = 1,
  658. };
  659. static int __init ene_ub6250_init(void)
  660. {
  661. return usb_register(&ene_ub6250_driver);
  662. }
  663. static void __exit ene_ub6250_exit(void)
  664. {
  665. usb_deregister(&ene_ub6250_driver);
  666. }
  667. module_init(ene_ub6250_init);
  668. module_exit(ene_ub6250_exit);