init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include <linux/sched.h>
  2. #include <linux/errno.h>
  3. #include <linux/slab.h>
  4. #include <scsi/scsi.h>
  5. #include <scsi/scsi_eh.h>
  6. #include <scsi/scsi_device.h>
  7. #include "usb.h"
  8. #include "scsiglue.h"
  9. #include "transport.h"
  10. #include "init.h"
  11. /*
  12. * ENE_InitMedia():
  13. */
  14. int ENE_InitMedia(struct us_data *us)
  15. {
  16. int result;
  17. BYTE MiscReg03 = 0;
  18. printk(KERN_INFO "--- Init Media ---\n");
  19. result = ENE_Read_BYTE(us, REG_CARD_STATUS, &MiscReg03);
  20. if (result != USB_STOR_XFER_GOOD) {
  21. printk(KERN_ERR "Read register fail !!\n");
  22. return USB_STOR_TRANSPORT_ERROR;
  23. }
  24. printk(KERN_INFO "MiscReg03 = %x\n", MiscReg03);
  25. if (MiscReg03 & 0x02) {
  26. if (!us->SM_Status.Ready && !us->MS_Status.Ready) {
  27. result = ENE_SMInit(us);
  28. if (result != USB_STOR_XFER_GOOD) {
  29. result = ENE_MSInit(us);
  30. if (result != USB_STOR_XFER_GOOD)
  31. return USB_STOR_TRANSPORT_ERROR;
  32. }
  33. }
  34. }
  35. return result;
  36. }
  37. /*
  38. * ENE_Read_BYTE() :
  39. */
  40. int ENE_Read_BYTE(struct us_data *us, WORD index, void *buf)
  41. {
  42. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  43. int result;
  44. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  45. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  46. bcb->DataTransferLength = 0x01;
  47. bcb->Flags = 0x80;
  48. bcb->CDB[0] = 0xED;
  49. bcb->CDB[2] = (BYTE)(index>>8);
  50. bcb->CDB[3] = (BYTE)index;
  51. result = ENE_SendScsiCmd(us, FDIR_READ, buf, 0);
  52. return result;
  53. }
  54. /*
  55. * ENE_MSInit():
  56. */
  57. int ENE_MSInit(struct us_data *us)
  58. {
  59. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  60. int result;
  61. BYTE buf[0x200];
  62. WORD MSP_BlockSize, MSP_UserAreaBlocks;
  63. printk(KERN_INFO "transport --- ENE_MSInit\n");
  64. result = ENE_LoadBinCode(us, MS_INIT_PATTERN);
  65. if (result != USB_STOR_XFER_GOOD) {
  66. printk(KERN_ERR "Load MS Init Code Fail !!\n");
  67. return USB_STOR_TRANSPORT_ERROR;
  68. }
  69. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  70. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  71. bcb->DataTransferLength = 0x200;
  72. bcb->Flags = 0x80;
  73. bcb->CDB[0] = 0xF1;
  74. bcb->CDB[1] = 0x01;
  75. result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
  76. if (result != USB_STOR_XFER_GOOD) {
  77. printk(KERN_ERR "Execution MS Init Code Fail !!\n");
  78. return USB_STOR_TRANSPORT_ERROR;
  79. }
  80. us->MS_Status = *(PMS_STATUS)&buf[0];
  81. if (us->MS_Status.Insert && us->MS_Status.Ready) {
  82. printk(KERN_INFO "Insert = %x\n", us->MS_Status.Insert);
  83. printk(KERN_INFO "Ready = %x\n", us->MS_Status.Ready);
  84. printk(KERN_INFO "IsMSPro = %x\n", us->MS_Status.IsMSPro);
  85. printk(KERN_INFO "IsMSPHG = %x\n", us->MS_Status.IsMSPHG);
  86. printk(KERN_INFO "WtP = %x\n", us->MS_Status.WtP);
  87. if (us->MS_Status.IsMSPro) {
  88. MSP_BlockSize = (buf[6] << 8) | buf[7];
  89. MSP_UserAreaBlocks = (buf[10] << 8) | buf[11];
  90. us->MSP_TotalBlock = MSP_BlockSize * MSP_UserAreaBlocks;
  91. } else {
  92. MS_CardInit(us);
  93. }
  94. printk(KERN_INFO "MS Init Code OK !!\n");
  95. } else {
  96. printk(KERN_INFO "MS Card Not Ready --- %x\n", buf[0]);
  97. return USB_STOR_TRANSPORT_ERROR;
  98. }
  99. return USB_STOR_TRANSPORT_GOOD;
  100. }
  101. /*
  102. *ENE_SMInit()
  103. */
  104. int ENE_SMInit(struct us_data *us)
  105. {
  106. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  107. int result;
  108. BYTE buf[0x200];
  109. printk(KERN_INFO "transport --- ENE_SMInit\n");
  110. result = ENE_LoadBinCode(us, SM_INIT_PATTERN);
  111. if (result != USB_STOR_XFER_GOOD) {
  112. printk(KERN_INFO "Load SM Init Code Fail !!\n");
  113. return USB_STOR_TRANSPORT_ERROR;
  114. }
  115. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  116. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  117. bcb->DataTransferLength = 0x200;
  118. bcb->Flags = 0x80;
  119. bcb->CDB[0] = 0xF1;
  120. bcb->CDB[1] = 0x01;
  121. result = ENE_SendScsiCmd(us, FDIR_READ, &buf, 0);
  122. if (result != USB_STOR_XFER_GOOD) {
  123. printk(KERN_ERR
  124. "Execution SM Init Code Fail !! result = %x\n", result);
  125. return USB_STOR_TRANSPORT_ERROR;
  126. }
  127. us->SM_Status = *(PSM_STATUS)&buf[0];
  128. us->SM_DeviceID = buf[1];
  129. us->SM_CardID = buf[2];
  130. if (us->SM_Status.Insert && us->SM_Status.Ready) {
  131. printk(KERN_INFO "Insert = %x\n", us->SM_Status.Insert);
  132. printk(KERN_INFO "Ready = %x\n", us->SM_Status.Ready);
  133. printk(KERN_INFO "WtP = %x\n", us->SM_Status.WtP);
  134. printk(KERN_INFO "DeviceID = %x\n", us->SM_DeviceID);
  135. printk(KERN_INFO "CardID = %x\n", us->SM_CardID);
  136. MediaChange = 1;
  137. Check_D_MediaFmt(us);
  138. } else {
  139. printk(KERN_ERR "SM Card Not Ready --- %x\n", buf[0]);
  140. return USB_STOR_TRANSPORT_ERROR;
  141. }
  142. return USB_STOR_TRANSPORT_GOOD;
  143. }
  144. /*
  145. * ENE_LoadBinCode()
  146. */
  147. int ENE_LoadBinCode(struct us_data *us, BYTE flag)
  148. {
  149. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  150. int result;
  151. /* void *buf; */
  152. PBYTE buf;
  153. /* printk(KERN_INFO "transport --- ENE_LoadBinCode\n"); */
  154. if (us->BIN_FLAG == flag)
  155. return USB_STOR_TRANSPORT_GOOD;
  156. buf = kmalloc(0x800, GFP_KERNEL);
  157. if (buf == NULL)
  158. return USB_STOR_TRANSPORT_ERROR;
  159. switch (flag) {
  160. /* For MS */
  161. case MS_INIT_PATTERN:
  162. printk(KERN_INFO "MS_INIT_PATTERN\n");
  163. memcpy(buf, MS_Init, 0x800);
  164. break;
  165. case MSP_RW_PATTERN:
  166. printk(KERN_INFO "MSP_RW_PATTERN\n");
  167. memcpy(buf, MSP_Rdwr, 0x800);
  168. break;
  169. case MS_RW_PATTERN:
  170. printk(KERN_INFO "MS_RW_PATTERN\n");
  171. memcpy(buf, MS_Rdwr, 0x800);
  172. break;
  173. /* For SS */
  174. case SM_INIT_PATTERN:
  175. printk(KERN_INFO "SM_INIT_PATTERN\n");
  176. memcpy(buf, SM_Init, 0x800);
  177. break;
  178. case SM_RW_PATTERN:
  179. printk(KERN_INFO "SM_RW_PATTERN\n");
  180. memcpy(buf, SM_Rdwr, 0x800);
  181. break;
  182. }
  183. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  184. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  185. bcb->DataTransferLength = 0x800;
  186. bcb->Flags = 0x00;
  187. bcb->CDB[0] = 0xEF;
  188. result = ENE_SendScsiCmd(us, FDIR_WRITE, buf, 0);
  189. kfree(buf);
  190. us->BIN_FLAG = flag;
  191. return result;
  192. }
  193. /*
  194. * ENE_SendScsiCmd():
  195. */
  196. int ENE_SendScsiCmd(struct us_data *us, BYTE fDir, void *buf, int use_sg)
  197. {
  198. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  199. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  200. int result;
  201. unsigned int transfer_length = bcb->DataTransferLength,
  202. cswlen = 0, partial = 0;
  203. unsigned int residue;
  204. /* printk(KERN_INFO "transport --- ENE_SendScsiCmd\n"); */
  205. /* send cmd to out endpoint */
  206. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  207. bcb, US_BULK_CB_WRAP_LEN, NULL);
  208. if (result != USB_STOR_XFER_GOOD) {
  209. printk(KERN_ERR "send cmd to out endpoint fail ---\n");
  210. return USB_STOR_TRANSPORT_ERROR;
  211. }
  212. if (buf) {
  213. unsigned int pipe = fDir;
  214. if (fDir == FDIR_READ)
  215. pipe = us->recv_bulk_pipe;
  216. else
  217. pipe = us->send_bulk_pipe;
  218. /* Bulk */
  219. if (use_sg)
  220. result = usb_stor_bulk_srb(us, pipe, us->srb);
  221. else
  222. result = usb_stor_bulk_transfer_sg(us, pipe, buf,
  223. transfer_length, 0, &partial);
  224. if (result != USB_STOR_XFER_GOOD) {
  225. printk(KERN_ERR "data transfer fail ---\n");
  226. return USB_STOR_TRANSPORT_ERROR;
  227. }
  228. }
  229. /* Get CSW for device status */
  230. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
  231. US_BULK_CS_WRAP_LEN, &cswlen);
  232. if (result == USB_STOR_XFER_SHORT && cswlen == 0) {
  233. printk(KERN_WARNING "Received 0-length CSW; retrying...\n");
  234. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  235. bcs, US_BULK_CS_WRAP_LEN, &cswlen);
  236. }
  237. if (result == USB_STOR_XFER_STALLED) {
  238. /* get the status again */
  239. printk(KERN_WARNING "Attempting to get CSW (2nd try)...\n");
  240. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  241. bcs, US_BULK_CS_WRAP_LEN, NULL);
  242. }
  243. if (result != USB_STOR_XFER_GOOD)
  244. return USB_STOR_TRANSPORT_ERROR;
  245. /* check bulk status */
  246. residue = le32_to_cpu(bcs->Residue);
  247. /*
  248. * try to compute the actual residue, based on how much data
  249. * was really transferred and what the device tells us
  250. */
  251. if (residue && !(us->fflags & US_FL_IGNORE_RESIDUE)) {
  252. residue = min(residue, transfer_length);
  253. if (us->srb)
  254. scsi_set_resid(us->srb, max(scsi_get_resid(us->srb),
  255. (int) residue));
  256. }
  257. if (bcs->Status != US_BULK_STAT_OK)
  258. return USB_STOR_TRANSPORT_ERROR;
  259. return USB_STOR_TRANSPORT_GOOD;
  260. }
  261. /*
  262. * ENE_Read_Data()
  263. */
  264. int ENE_Read_Data(struct us_data *us, void *buf, unsigned int length)
  265. {
  266. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  267. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  268. int result;
  269. /* printk(KERN_INFO "transport --- ENE_Read_Data\n"); */
  270. /* set up the command wrapper */
  271. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  272. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  273. bcb->DataTransferLength = length;
  274. bcb->Flags = 0x80;
  275. bcb->CDB[0] = 0xED;
  276. bcb->CDB[2] = 0xFF;
  277. bcb->CDB[3] = 0x81;
  278. /* send cmd to out endpoint */
  279. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
  280. US_BULK_CB_WRAP_LEN, NULL);
  281. if (result != USB_STOR_XFER_GOOD)
  282. return USB_STOR_TRANSPORT_ERROR;
  283. /* R/W data */
  284. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  285. buf, length, NULL);
  286. if (result != USB_STOR_XFER_GOOD)
  287. return USB_STOR_TRANSPORT_ERROR;
  288. /* Get CSW for device status */
  289. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
  290. US_BULK_CS_WRAP_LEN, NULL);
  291. if (result != USB_STOR_XFER_GOOD)
  292. return USB_STOR_TRANSPORT_ERROR;
  293. if (bcs->Status != US_BULK_STAT_OK)
  294. return USB_STOR_TRANSPORT_ERROR;
  295. return USB_STOR_TRANSPORT_GOOD;
  296. }
  297. /*
  298. * ENE_Write_Data():
  299. */
  300. int ENE_Write_Data(struct us_data *us, void *buf, unsigned int length)
  301. {
  302. struct bulk_cb_wrap *bcb = (struct bulk_cb_wrap *) us->iobuf;
  303. struct bulk_cs_wrap *bcs = (struct bulk_cs_wrap *) us->iobuf;
  304. int result;
  305. /* printk("transport --- ENE_Write_Data\n"); */
  306. /* set up the command wrapper */
  307. memset(bcb, 0, sizeof(struct bulk_cb_wrap));
  308. bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
  309. bcb->DataTransferLength = length;
  310. bcb->Flags = 0x00;
  311. bcb->CDB[0] = 0xEE;
  312. bcb->CDB[2] = 0xFF;
  313. bcb->CDB[3] = 0x81;
  314. /* send cmd to out endpoint */
  315. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, bcb,
  316. US_BULK_CB_WRAP_LEN, NULL);
  317. if (result != USB_STOR_XFER_GOOD)
  318. return USB_STOR_TRANSPORT_ERROR;
  319. /* R/W data */
  320. result = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  321. buf, length, NULL);
  322. if (result != USB_STOR_XFER_GOOD)
  323. return USB_STOR_TRANSPORT_ERROR;
  324. /* Get CSW for device status */
  325. result = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, bcs,
  326. US_BULK_CS_WRAP_LEN, NULL);
  327. if (result != USB_STOR_XFER_GOOD)
  328. return USB_STOR_TRANSPORT_ERROR;
  329. if (bcs->Status != US_BULK_STAT_OK)
  330. return USB_STOR_TRANSPORT_ERROR;
  331. return USB_STOR_TRANSPORT_GOOD;
  332. }
  333. /*
  334. * usb_stor_print_cmd():
  335. */
  336. void usb_stor_print_cmd(struct scsi_cmnd *srb)
  337. {
  338. PBYTE Cdb = srb->cmnd;
  339. DWORD cmd = Cdb[0];
  340. DWORD bn = ((Cdb[2] << 24) & 0xff000000) |
  341. ((Cdb[3] << 16) & 0x00ff0000) |
  342. ((Cdb[4] << 8) & 0x0000ff00) |
  343. ((Cdb[5] << 0) & 0x000000ff);
  344. WORD blen = ((Cdb[7] << 8) & 0xff00) | ((Cdb[8] << 0) & 0x00ff);
  345. switch (cmd) {
  346. case TEST_UNIT_READY:
  347. /* printk(KERN_INFO
  348. "scsi cmd %X --- SCSIOP_TEST_UNIT_READY\n", cmd); */
  349. break;
  350. case INQUIRY:
  351. printk(KERN_INFO "scsi cmd %X --- SCSIOP_INQUIRY\n", cmd);
  352. break;
  353. case MODE_SENSE:
  354. printk(KERN_INFO "scsi cmd %X --- SCSIOP_MODE_SENSE\n", cmd);
  355. break;
  356. case START_STOP:
  357. printk(KERN_INFO "scsi cmd %X --- SCSIOP_START_STOP\n", cmd);
  358. break;
  359. case READ_CAPACITY:
  360. printk(KERN_INFO "scsi cmd %X --- SCSIOP_READ_CAPACITY\n", cmd);
  361. break;
  362. case READ_10:
  363. /* printk(KERN_INFO
  364. "scsi cmd %X --- SCSIOP_READ,bn = %X, blen = %X\n"
  365. ,cmd, bn, blen); */
  366. break;
  367. case WRITE_10:
  368. /* printk(KERN_INFO
  369. "scsi cmd %X --- SCSIOP_WRITE,
  370. bn = %X, blen = %X\n" , cmd, bn, blen); */
  371. break;
  372. case ALLOW_MEDIUM_REMOVAL:
  373. printk(KERN_INFO
  374. "scsi cmd %X --- SCSIOP_ALLOW_MEDIUM_REMOVAL\n", cmd);
  375. break;
  376. default:
  377. printk(KERN_INFO "scsi cmd %X --- Other cmd\n", cmd);
  378. break;
  379. }
  380. bn = 0;
  381. blen = 0;
  382. }