sddr55.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:26k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Driver for SanDisk SDDR-55 SmartMedia reader
  2.  *
  3.  * $Id:$
  4.  *
  5.  * SDDR55 driver v0.1:
  6.  *
  7.  * First release
  8.  *
  9.  * Current development and maintenance by:
  10.  *   (c) 2002 Simon Munton
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify it
  13.  * under the terms of the GNU General Public License as published by the
  14.  * Free Software Foundation; either version 2, or (at your option) any
  15.  * later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful, but
  18.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20.  * General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License along
  23.  * with this program; if not, write to the Free Software Foundation, Inc.,
  24.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  */
  26. #include "transport.h"
  27. #include "protocol.h"
  28. #include "usb.h"
  29. #include "debug.h"
  30. #include "sddr55.h"
  31. #include <linux/sched.h>
  32. #include <linux/errno.h>
  33. #include <linux/slab.h>
  34. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  35. #define LSB_of(s) ((s)&0xFF)
  36. #define MSB_of(s) ((s)>>8)
  37. #define PAGESIZE  512
  38. #define set_sense_info(sk, asc, ascq)
  39.     do {
  40. info->sense_data[2] = sk;
  41. info->sense_data[12] = asc;
  42. info->sense_data[13] = ascq;
  43. } while (0)
  44. struct sddr55_card_info {
  45. unsigned long capacity; /* Size of card in bytes */
  46. int max_log_blks; /* maximum number of logical blocks */
  47. int pageshift; /* log2 of pagesize */
  48. int smallpageshift; /* 1 if pagesize == 256 */
  49. int blocksize; /* Size of block in pages */
  50. int blockshift; /* log2 of blocksize */
  51. int blockmask; /* 2^blockshift - 1 */
  52. int read_only; /* non zero if card is write protected */
  53. int force_read_only; /* non zero if we find a map error*/
  54. int *lba_to_pba; /* logical to physical map */
  55. int *pba_to_lba; /* physical to logical map */
  56. int fatal_error; /* set if we detect something nasty */
  57. unsigned long  last_access; /* number of jiffies since we last talked to device */
  58. unsigned char   sense_data[18];
  59. };
  60. #define NOT_ALLOCATED 0xffffffff
  61. #define BAD_BLOCK 0xffff
  62. #define CIS_BLOCK 0x400
  63. #define UNUSED_BLOCK 0x3ff
  64. static int sddr55_raw_bulk(struct us_data *us, 
  65. int direction,
  66. unsigned char *data,
  67. unsigned int len) {
  68. int result;
  69. int act_len;
  70. int pipe;
  71. if (direction == SCSI_DATA_READ)
  72. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  73. else
  74. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  75. result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);
  76. /* if we stall, we need to clear it before we go on */
  77. if (result == -EPIPE) {
  78. US_DEBUGP("EPIPE: clearing endpoint halt for"
  79. " pipe 0x%x, stalled at %d bytesn",
  80. pipe, act_len);
  81. usb_clear_halt(us->pusb_dev, pipe);
  82. }
  83. if (result) {
  84. /* NAK - that means we've retried a few times already */
  85. if (result == -ETIMEDOUT) {
  86. US_DEBUGP("usbat_raw_bulk():"
  87. " device NAKedn");
  88. return US_BULK_TRANSFER_FAILED;
  89. }
  90. /* -ENOENT -- we canceled this transfer */
  91. if (result == -ENOENT) {
  92. US_DEBUGP("usbat_raw_bulk():"
  93. " transfer abortedn");
  94. return US_BULK_TRANSFER_ABORTED;
  95. }
  96. if (result == -EPIPE) {
  97. US_DEBUGP("usbat_raw_bulk():"
  98. " output pipe stalledn");
  99. return US_BULK_TRANSFER_FAILED;
  100. }
  101. /* the catch-all case */
  102. US_DEBUGP("us_transfer_partial(): unknown errorn");
  103. return US_BULK_TRANSFER_FAILED;
  104. }
  105. if (act_len != len) {
  106. US_DEBUGP("Warning: Transferred only %d bytesn",
  107. act_len);
  108. return US_BULK_TRANSFER_SHORT;
  109. }
  110. US_DEBUGP("Transferred %d of %d bytesn", act_len, len);
  111. return US_BULK_TRANSFER_GOOD;
  112. }
  113. /*
  114.  * Note: direction must be set if command_len == 0.
  115.  */
  116. static int sddr55_bulk_transport(struct us_data *us,
  117.   int direction,
  118.   unsigned char *data,
  119.   unsigned int len) {
  120. int result = USB_STOR_TRANSPORT_GOOD;
  121. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  122. if (len==0)
  123. return USB_STOR_TRANSPORT_GOOD;
  124. info->last_access = jiffies;
  125. #ifdef CONFIG_USB_STORAGE_DEBUG
  126. if (direction == SCSI_DATA_WRITE) {
  127. int i;
  128. char string[64];
  129. /* Debug-print the first 48 bytes of the write transfer */
  130. strcpy(string, "wr: ");
  131. for (i=0; i<len && i<48; i++) {
  132. sprintf(string+strlen(string), "%02X ",
  133.   data[i]);
  134. if ((i%16)==15) {
  135. US_DEBUGP("%sn", string);
  136. strcpy(string, "wr: ");
  137. }
  138. }
  139. if ((i%16)!=0)
  140. US_DEBUGP("%sn", string);
  141. }
  142. #endif
  143. /* transfer the data */
  144. US_DEBUGP("SCM data %s transfer %dn",
  145.   ( direction==SCSI_DATA_READ ? "in" : "out"),
  146.   len);
  147. result = sddr55_raw_bulk(us, direction, data, len);
  148. #ifdef CONFIG_USB_STORAGE_DEBUG
  149. if (direction == SCSI_DATA_READ) {
  150. int i;
  151. char string[64];
  152. /* Debug-print the first 48 bytes of the read transfer */
  153. strcpy(string, "rd: ");
  154. for (i=0; i<len && i<48; i++) {
  155. sprintf(string+strlen(string), "%02X ",
  156.   data[i]);
  157. if ((i%16)==15) {
  158. US_DEBUGP("%sn", string);
  159. strcpy(string, "rd: ");
  160. }
  161. }
  162. if ((i%16)!=0)
  163. US_DEBUGP("%sn", string);
  164. }
  165. #endif
  166. return result;
  167. }
  168. /* check if card inserted, if there is, update read_only status
  169.  * return non zero if no card
  170.  */
  171. static int sddr55_status(struct us_data *us)
  172. {
  173. int result;
  174. unsigned char command[8] = {
  175. 0, 0, 0, 0, 0, 0xb0, 0, 0x80
  176. };
  177. unsigned char status[8];
  178. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  179. /* send command */
  180. result = sddr55_bulk_transport(us,
  181. SCSI_DATA_WRITE, command, 8);
  182. US_DEBUGP("Result for send_command in status %dn",
  183. result);
  184. if (result != US_BULK_TRANSFER_GOOD) {
  185. set_sense_info (4, 0, 0); /* hardware error */
  186. return result;
  187. }
  188. result = sddr55_bulk_transport(us,
  189. SCSI_DATA_READ, status, 4);
  190. /* expect to get short transfer if no card fitted */
  191. if (result == US_BULK_TRANSFER_SHORT) {
  192. /* had a short transfer, no card inserted, free map memory */
  193. if (info->lba_to_pba)
  194. kfree(info->lba_to_pba);
  195. if (info->pba_to_lba)
  196. kfree(info->pba_to_lba);
  197. info->lba_to_pba = NULL;
  198. info->pba_to_lba = NULL;
  199. info->fatal_error = 0;
  200. info->force_read_only = 0;
  201. set_sense_info (2, 0x3a, 0); /* not ready, medium not present */
  202. return result;
  203. }
  204. if (result != US_BULK_TRANSFER_GOOD) {
  205. set_sense_info (4, 0, 0); /* hardware error */
  206. return result;
  207. }
  208. /* check write protect status */
  209. info->read_only = (status[0] & 0x20);
  210. /* now read status */
  211. result = sddr55_bulk_transport(us,
  212. SCSI_DATA_READ, status, 2);
  213. if (result != US_BULK_TRANSFER_GOOD) {
  214. set_sense_info (4, 0, 0); /* hardware error */
  215. }
  216. return result;
  217. }
  218. static int sddr55_read_data(struct us_data *us,
  219. unsigned int lba,
  220. unsigned int page,
  221. unsigned short sectors,
  222. unsigned char *content,
  223. int use_sg) {
  224. int result;
  225. unsigned char command[8] = {
  226. 0, 0, 0, 0, 0, 0xb0, 0, 0x85
  227. };
  228. unsigned char status[8];
  229. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  230. unsigned int pba;
  231. unsigned long address;
  232. unsigned short pages;
  233. unsigned char *buffer = NULL;
  234. unsigned char *ptr;
  235. struct scatterlist *sg = NULL;
  236. int i;
  237. int len;
  238. int transferred;
  239. // If we're using scatter-gather, we have to create a new
  240. // buffer to read all of the data in first, since a
  241. // scatter-gather buffer could in theory start in the middle
  242. // of a page, which would be bad. A developer who wants a
  243. // challenge might want to write a limited-buffer
  244. // version of this code.
  245. len = sectors * PAGESIZE;
  246. if (use_sg) {
  247. sg = (struct scatterlist *)content;
  248. buffer = kmalloc(len, GFP_NOIO);
  249. if (buffer == NULL)
  250. return USB_STOR_TRANSPORT_ERROR;
  251. ptr = buffer;
  252. } else
  253. ptr = content;
  254. // This could be made much more efficient by checking for
  255. // contiguous LBA's. Another exercise left to the student.
  256. while (sectors>0) {
  257. /* have we got to end? */
  258. if (lba >= info->max_log_blks)
  259. break;
  260. pba = info->lba_to_pba[lba];
  261. // Read as many sectors as possible in this block
  262. pages = info->blocksize - page;
  263. if (pages > (sectors << info->smallpageshift))
  264. pages = (sectors << info->smallpageshift);
  265. US_DEBUGP("Read %02X pages, from PBA %04X"
  266. " (LBA %04X) page %02Xn",
  267. pages, pba, lba, page);
  268. if (pba == NOT_ALLOCATED) {
  269. /* no pba for this lba, fill with zeroes */
  270. memset (ptr, 0, pages << info->pageshift);
  271. } else {
  272. address = (pba << info->blockshift) + page;
  273. command[1] = LSB_of(address>>16);
  274. command[2] = LSB_of(address>>8);
  275. command[3] = LSB_of(address);
  276. command[6] = LSB_of(pages << (1 - info->smallpageshift));
  277. /* send command */
  278. result = sddr55_bulk_transport(us,
  279. SCSI_DATA_WRITE, command, 8);
  280. US_DEBUGP("Result for send_command in read_data %dn",
  281. result);
  282. if (result != US_BULK_TRANSFER_GOOD) {
  283. if (use_sg)
  284. kfree(buffer);
  285. return result;
  286. }
  287. /* read data */
  288. result = sddr55_bulk_transport(us,
  289. SCSI_DATA_READ, ptr,
  290. pages<<info->pageshift);
  291. if (result != US_BULK_TRANSFER_GOOD) {
  292. if (use_sg)
  293. kfree(buffer);
  294. return result;
  295. }
  296. /* now read status */
  297. result = sddr55_bulk_transport(us,
  298. SCSI_DATA_READ, status, 2);
  299. if (result != US_BULK_TRANSFER_GOOD) {
  300. if (use_sg)
  301. kfree(buffer);
  302. return result;
  303. }
  304. /* check status for error */
  305. if (status[0] == 0xff && status[1] == 0x4) {
  306. set_sense_info (3, 0x11, 0);
  307. if (use_sg)
  308. kfree(buffer);
  309. return USB_STOR_TRANSPORT_FAILED;
  310. }
  311. }
  312. page = 0;
  313. lba++;
  314. sectors -= pages >> info->smallpageshift;
  315. ptr += (pages << info->pageshift);
  316. }
  317. if (use_sg) {
  318. transferred = 0;
  319. for (i=0; i<use_sg && transferred<len; i++) {
  320. memcpy(sg[i].address, buffer+transferred,
  321. len-transferred > sg[i].length ?
  322. sg[i].length : len-transferred);
  323. transferred += sg[i].length;
  324. }
  325. kfree(buffer);
  326. }
  327. return USB_STOR_TRANSPORT_GOOD;
  328. }
  329. static int sddr55_write_data(struct us_data *us,
  330. unsigned int lba,
  331. unsigned int page,
  332. unsigned short sectors,
  333. unsigned char *content,
  334. int use_sg) {
  335. int result;
  336. unsigned char command[8] = {
  337. 0, 0, 0, 0, 0, 0xb0, 0, 0x86
  338. };
  339. unsigned char status[8];
  340. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  341. unsigned int pba;
  342. unsigned int new_pba;
  343. unsigned long address;
  344. unsigned short pages;
  345. unsigned char *buffer = NULL;
  346. unsigned char *ptr;
  347. struct scatterlist *sg = NULL;
  348. int i;
  349. int len;
  350. int transferred;
  351. /* check if we are allowed to write */
  352. if (info->read_only || info->force_read_only) {
  353. set_sense_info (7, 0x27, 0); /* read only */
  354. return USB_STOR_TRANSPORT_FAILED;
  355. }
  356. // If we're using scatter-gather, we have to create a new
  357. // buffer to write all of the data in first, since a
  358. // scatter-gather buffer could in theory start in the middle
  359. // of a page, which would be bad. A developer who wants a
  360. // challenge might want to write a limited-buffer
  361. // version of this code.
  362. len = sectors * PAGESIZE;
  363. if (use_sg) {
  364. sg = (struct scatterlist *)content;
  365. buffer = kmalloc(len, GFP_NOIO);
  366. if (buffer == NULL)
  367. return USB_STOR_TRANSPORT_ERROR;
  368. transferred = 0;
  369. for (i=0; i<use_sg && transferred<len; i++) {
  370. memcpy(buffer+transferred, sg[i].address,
  371. len-transferred > sg[i].length ?
  372. sg[i].length : len-transferred);
  373. transferred += sg[i].length;
  374. }
  375. ptr = buffer;
  376. } else
  377. ptr = content;
  378. while (sectors > 0) {
  379. /* have we got to end? */
  380. if (lba >= info->max_log_blks)
  381. break;
  382. pba = info->lba_to_pba[lba];
  383. // Write as many sectors as possible in this block
  384. pages = info->blocksize - page;
  385. if (pages > (sectors << info->smallpageshift))
  386. pages = (sectors << info->smallpageshift);
  387. US_DEBUGP("Write %02X pages, to PBA %04X"
  388. " (LBA %04X) page %02Xn",
  389. pages, pba, lba, page);
  390. command[4] = 0;
  391. if (pba == NOT_ALLOCATED) {
  392. /* no pba allocated for this lba, find a free pba to use */
  393. int max_pba = (info->max_log_blks / 250 ) * 256;
  394. int found_count = 0;
  395. int found_pba = -1;
  396. /* set pba to first block in zone lba is in */
  397. pba = (lba / 1000) * 1024;
  398. US_DEBUGP("No PBA for LBA %04Xn",lba);
  399. if (max_pba > 1024)
  400. max_pba = 1024;
  401. /* scan through the map lookiong for an unused block
  402.  * leave 16 unused blocks at start (or as many as possible)
  403.  * since the sddr55 seems to reuse a used block when it shouldn't
  404.  * if we don't leave space */
  405. for (i = 0; i < max_pba; i++, pba++) {
  406. if (info->pba_to_lba[pba] == UNUSED_BLOCK) {
  407. found_pba = pba;
  408. if (found_count++ > 16)
  409. break;
  410. }
  411. }
  412. pba = found_pba;
  413. if (pba == -1) {
  414. /* oh dear, couldn't find an unallocated block */
  415. US_DEBUGP("Couldn't find unallocated blockn");
  416. set_sense_info (3, 0x31, 0); /* medium error */
  417. if (use_sg)
  418. kfree(buffer);
  419. return USB_STOR_TRANSPORT_FAILED;
  420. }
  421. US_DEBUGP("Allocating PBA %04X for LBA %04Xn", pba, lba);
  422. /* set writing to unallocated block flag */
  423. command[4] = 0x40;
  424. }
  425. address = (pba << info->blockshift) + page;
  426. command[1] = LSB_of(address>>16);
  427. command[2] = LSB_of(address>>8); 
  428. command[3] = LSB_of(address);
  429. /* set the lba into the command, modulo 1000 */
  430. command[0] = LSB_of(lba % 1000);
  431. command[6] = MSB_of(lba % 1000);
  432. command[4] |= LSB_of(pages >> info->smallpageshift);
  433. /* send command */
  434. result = sddr55_bulk_transport(us,
  435. SCSI_DATA_WRITE, command, 8);
  436. if (result != US_BULK_TRANSFER_GOOD) {
  437. US_DEBUGP("Result for send_command in write_data %dn",
  438. result);
  439. set_sense_info (3, 0x3, 0); /* peripheral write error */
  440. if (use_sg)
  441. kfree(buffer);
  442. return result;
  443. }
  444. /* send the data */
  445. result = sddr55_bulk_transport(us,
  446. SCSI_DATA_WRITE, ptr,
  447. pages<<info->pageshift);
  448. if (result != US_BULK_TRANSFER_GOOD) {
  449. US_DEBUGP("Result for send_data in write_data %dn",
  450. result);
  451. set_sense_info (3, 0x3, 0); /* peripheral write error */
  452. if (use_sg)
  453. kfree(buffer);
  454. return result;
  455. }
  456. /* now read status */
  457. result = sddr55_bulk_transport(us,
  458. SCSI_DATA_READ, status, 6);
  459. if (result != US_BULK_TRANSFER_GOOD) {
  460. US_DEBUGP("Result for get_status in write_data %dn",
  461. result);
  462. set_sense_info (3, 0x3, 0); /* peripheral write error */
  463. if (use_sg)
  464. kfree(buffer);
  465. return result;
  466. }
  467. new_pba = (status[3] + (status[4] << 8) + (status[5] << 16)) >> info->blockshift;
  468. /* check status for error */
  469. if (status[0] == 0xff && status[1] == 0x4) {
  470. set_sense_info (3, 0x0c, 0);
  471. if (use_sg)
  472. kfree(buffer);
  473. info->pba_to_lba[new_pba] = BAD_BLOCK;
  474. return USB_STOR_TRANSPORT_FAILED;
  475. }
  476. US_DEBUGP("Updating maps for LBA %04X: old PBA %04X, new PBA %04Xn",
  477. lba, pba, new_pba);
  478. /* update the lba<->pba maps, note new_pba might be the same as pba */
  479. info->lba_to_pba[lba] = new_pba;
  480. info->pba_to_lba[pba] = UNUSED_BLOCK;
  481. /* check that new_pba wasn't already being used */
  482. if (info->pba_to_lba[new_pba] != UNUSED_BLOCK) {
  483. printk(KERN_ERR "sddr55 error: new PBA %04X already in use for LBA %04Xn",
  484. new_pba, info->pba_to_lba[new_pba]);
  485. info->fatal_error = 1;
  486. set_sense_info (3, 0x31, 0);
  487. if (use_sg)
  488. kfree(buffer);
  489. return USB_STOR_TRANSPORT_FAILED;
  490. }
  491. /* update the pba<->lba maps for new_pba */
  492. info->pba_to_lba[new_pba] = lba % 1000;
  493. page = 0;
  494. lba++;
  495. sectors -= pages >> info->smallpageshift;
  496. ptr += (pages << info->pageshift);
  497. }
  498. if (use_sg) {
  499. kfree(buffer);
  500. }
  501. return USB_STOR_TRANSPORT_GOOD;
  502. }
  503. static int sddr55_read_deviceID(struct us_data *us,
  504. unsigned char *manufacturerID,
  505. unsigned char *deviceID) {
  506. int result;
  507. unsigned char command[8] = {
  508. 0, 0, 0, 0, 0, 0xb0, 0, 0x84
  509. };
  510. unsigned char content[64];
  511. result = sddr55_bulk_transport(us, SCSI_DATA_WRITE, command, 8);
  512. US_DEBUGP("Result of send_control for device ID is %dn",
  513. result);
  514. if (result != US_BULK_TRANSFER_GOOD)
  515. return result;
  516. result = sddr55_bulk_transport(us,
  517. SCSI_DATA_READ, content, 4);
  518. if (result != US_BULK_TRANSFER_GOOD)
  519. return result;
  520. *manufacturerID = content[0];
  521. *deviceID = content[1];
  522. if (content[0] != 0xff) {
  523.      result = sddr55_bulk_transport(us,
  524. SCSI_DATA_READ, content, 2);
  525. }
  526. return result;
  527. }
  528. int sddr55_reset(struct us_data *us) {
  529. return 0;
  530. }
  531. static unsigned long sddr55_get_capacity(struct us_data *us) {
  532. unsigned char manufacturerID;
  533. unsigned char deviceID;
  534. int result;
  535. struct sddr55_card_info *info = (struct sddr55_card_info *)us->extra;
  536. US_DEBUGP("Reading capacity...n");
  537. result = sddr55_read_deviceID(us,
  538. &manufacturerID,
  539. &deviceID);
  540. US_DEBUGP("Result of read_deviceID is %dn",
  541. result);
  542. if (result != US_BULK_TRANSFER_GOOD)
  543. return 0;
  544. US_DEBUGP("Device ID = %02Xn", deviceID);
  545. US_DEBUGP("Manuf  ID = %02Xn", manufacturerID);
  546. info->pageshift = 9;
  547. info->smallpageshift = 0;
  548. info->blocksize = 16;
  549. info->blockshift = 4;
  550. info->blockmask = 15;
  551. switch (deviceID) {
  552. case 0x6e: // 1MB
  553. case 0xe8:
  554. case 0xec:
  555. info->pageshift = 8;
  556. info->smallpageshift = 1;
  557. return 0x00100000;
  558. case 0xea: // 2MB
  559. case 0x64:
  560. info->pageshift = 8;
  561. info->smallpageshift = 1;
  562. case 0x5d: // 5d is a ROM card with pagesize 512.
  563. return 0x00200000;
  564. case 0xe3: // 4MB
  565. case 0xe5:
  566. case 0x6b:
  567. case 0xd5:
  568. return 0x00400000;
  569. case 0xe6: // 8MB
  570. case 0xd6:
  571. return 0x00800000;
  572. case 0x73: // 16MB
  573. info->blocksize = 32;
  574. info->blockshift = 5;
  575. info->blockmask = 31;
  576. return 0x01000000;
  577. case 0x75: // 32MB
  578. info->blocksize = 32;
  579. info->blockshift = 5;
  580. info->blockmask = 31;
  581. return 0x02000000;
  582. case 0x76: // 64MB
  583. info->blocksize = 32;
  584. info->blockshift = 5;
  585. info->blockmask = 31;
  586. return 0x04000000;
  587. case 0x79: // 128MB
  588. info->blocksize = 32;
  589. info->blockshift = 5;
  590. info->blockmask = 31;
  591. return 0x08000000;
  592. default: // unknown
  593. return 0;
  594. }
  595. }
  596. static int sddr55_read_map(struct us_data *us) {
  597. struct sddr55_card_info *info = (struct sddr55_card_info *)(us->extra);
  598. int numblocks;
  599. unsigned char *buffer;
  600. unsigned char command[8] = { 0, 0, 0, 0, 0, 0xb0, 0, 0x8a};
  601. int i;
  602. unsigned short lba;
  603. unsigned short max_lba;
  604. int result;
  605. if (!info->capacity)
  606. return -1;
  607. numblocks = info->capacity >> (info->blockshift + info->pageshift);
  608. buffer = kmalloc( numblocks * 2, GFP_NOIO );
  609. if (!buffer)
  610. return -1;
  611. command[6] = numblocks * 2 / 256;
  612. result = sddr55_bulk_transport(us, SCSI_DATA_WRITE, command, 8);
  613. if ( result != US_BULK_TRANSFER_GOOD) {
  614. kfree (buffer);
  615. return -1;
  616. }
  617. result = sddr55_bulk_transport(us, SCSI_DATA_READ, buffer, numblocks * 2);
  618. if ( result != US_BULK_TRANSFER_GOOD) {
  619. kfree (buffer);
  620. return -1;
  621. }
  622. result = sddr55_bulk_transport(us, SCSI_DATA_READ, command, 2);
  623. if ( result != US_BULK_TRANSFER_GOOD) {
  624. kfree (buffer);
  625. return -1;
  626. }
  627. if (info->lba_to_pba)
  628. kfree(info->lba_to_pba);
  629. if (info->pba_to_lba)
  630. kfree(info->pba_to_lba);
  631. info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  632. info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  633. if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
  634. if (info->lba_to_pba != NULL)
  635. kfree(info->lba_to_pba);
  636. if (info->pba_to_lba != NULL)
  637. kfree(info->pba_to_lba);
  638. info->lba_to_pba = NULL;
  639. info->pba_to_lba = NULL;
  640. kfree(buffer);
  641. return -1;
  642. }
  643. memset(info->lba_to_pba, 0xff, numblocks*sizeof(int));
  644. memset(info->pba_to_lba, 0xff, numblocks*sizeof(int));
  645. /* set maximum lba */
  646. max_lba = info->max_log_blks;
  647. if (max_lba > 1000)
  648. max_lba = 1000;
  649. // Each block is 64 bytes of control data, so block i is located in
  650. // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
  651. for (i=0; i<numblocks; i++) {
  652. int zone = i / 1024;
  653. lba = short_pack(buffer[i * 2], buffer[i * 2 + 1]);
  654. /* Every 1024 physical blocks ("zone"), the LBA numbers
  655.  * go back to zero, but are within a higher
  656.  * block of LBA's. Also, there is a maximum of
  657.  * 1000 LBA's per zone. In other words, in PBA
  658.  * 1024-2047 you will find LBA 0-999 which are
  659.  * really LBA 1000-1999. Yes, this wastes 24
  660.  * physical blocks per zone. Go figure. 
  661.  * These devices can have blocks go bad, so there
  662.  * are 24 spare blocks to use when blocks do go bad.
  663.  */
  664. /* SDDR55 returns 0xffff for a bad block, and 0x400 for the 
  665.  * CIS block. (Is this true for cards 8MB or less??)
  666.  * Record these in the physical to logical map
  667.  */ 
  668. info->pba_to_lba[i] = lba;
  669. if (lba >= max_lba) {
  670. continue;
  671. }
  672. if (info->lba_to_pba[lba + zone * 1000] != NOT_ALLOCATED &&
  673.     !info->force_read_only) {
  674. printk("sddr55: map inconsistency at LBA %04Xn", lba + zone * 1000);
  675. info->force_read_only = 1;
  676. }
  677. if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
  678. US_DEBUGP("LBA %04X <-> PBA %04Xn", lba, i);
  679. info->lba_to_pba[lba + zone * 1000] = i;
  680. }
  681. kfree(buffer);
  682. return 0;
  683. }
  684. static void sddr55_card_info_destructor(void *extra) {
  685. struct sddr55_card_info *info = (struct sddr55_card_info *)extra;
  686. if (!extra)
  687. return;
  688. if (info->lba_to_pba)
  689. kfree(info->lba_to_pba);
  690. if (info->pba_to_lba)
  691. kfree(info->pba_to_lba);
  692. }
  693. /*
  694.  * Transport for the Sandisk SDDR-55
  695.  */
  696. int sddr55_transport(Scsi_Cmnd *srb, struct us_data *us)
  697. {
  698. int result;
  699. int i;
  700. unsigned char inquiry_response[36] = {
  701. 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
  702. };
  703. unsigned char mode_page_01[16] = { // write-protected for now
  704. 0x03, 0x00, 0x80, 0x00,
  705. 0x01, 0x0A,
  706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  707. };
  708. unsigned char *ptr;
  709. unsigned long capacity;
  710. unsigned int lba;
  711. unsigned int pba;
  712. unsigned int page;
  713. unsigned short pages;
  714. struct sddr55_card_info *info;
  715. if (!us->extra) {
  716. us->extra = kmalloc(
  717. sizeof(struct sddr55_card_info), GFP_NOIO);
  718. if (!us->extra)
  719. return USB_STOR_TRANSPORT_ERROR;
  720. memset(us->extra, 0, sizeof(struct sddr55_card_info));
  721. us->extra_destructor = sddr55_card_info_destructor;
  722. }
  723. info = (struct sddr55_card_info *)(us->extra);
  724. ptr = (unsigned char *)srb->request_buffer;
  725. if (srb->cmnd[0] == REQUEST_SENSE) {
  726. i = srb->cmnd[4];
  727. if (i > sizeof info->sense_data)
  728. i = sizeof info->sense_data;
  729. US_DEBUGP("SDDR55: request sense %02x/%02x/%02xn", info->sense_data[2], info->sense_data[12], info->sense_data[13]);
  730. info->sense_data[0] = 0x70;
  731. info->sense_data[7] = 10;
  732. memcpy (ptr, info->sense_data, i);
  733. memset (info->sense_data, 0, sizeof info->sense_data);
  734. return USB_STOR_TRANSPORT_GOOD;
  735. }
  736. memset (info->sense_data, 0, sizeof info->sense_data);
  737. /* Dummy up a response for INQUIRY since SDDR55 doesn't
  738.    respond to INQUIRY commands */
  739. if (srb->cmnd[0] == INQUIRY) {
  740. memset(inquiry_response+8, 0, 28);
  741. fill_inquiry_response(us, inquiry_response, 36);
  742. return USB_STOR_TRANSPORT_GOOD;
  743. }
  744. /* only check card status if the map isn't allocated, ie no card seen yet
  745.  * or if it's been over half a second since we last accessed it
  746.  */
  747. if (info->lba_to_pba == NULL || time_after(jiffies, info->last_access + HZ/2)) {
  748. /* check to see if a card is fitted */
  749. result = sddr55_status (us);
  750. if (result) {
  751. result = sddr55_status (us);
  752. if (!result) {
  753. set_sense_info (6, 0x28, 0); /* new media, set unit attention, not ready to ready */
  754. }
  755. return USB_STOR_TRANSPORT_FAILED;
  756. }
  757. }
  758. /* if we detected a problem with the map when writing, don't allow any more access */
  759. if (info->fatal_error) {
  760. set_sense_info (3, 0x31, 0);
  761. return USB_STOR_TRANSPORT_FAILED;
  762. }
  763. if (srb->cmnd[0] == READ_CAPACITY) {
  764. capacity = sddr55_get_capacity(us);
  765. if (!capacity) {
  766. set_sense_info (3, 0x30, 0); /* incompatible medium */
  767. return USB_STOR_TRANSPORT_FAILED;
  768. }
  769. info->capacity = capacity;
  770.                 /* figure out the maximum logical block number, allowing for the fact
  771.                  * that only 250 out of every 256 are used */
  772. info->max_log_blks = ((info->capacity >> (info->pageshift + info->blockshift)) / 256) * 250;
  773. /* Last page in the card, adjust as we only use 250 out of every 256 pages */
  774. capacity = (capacity / 256) * 250;
  775. capacity /= PAGESIZE;
  776. capacity--;
  777. ptr[0] = MSB_of(capacity>>16);
  778. ptr[1] = LSB_of(capacity>>16);
  779. ptr[2] = MSB_of(capacity&0xFFFF);
  780. ptr[3] = LSB_of(capacity&0xFFFF);
  781. // The page size
  782. ptr[4] = MSB_of(PAGESIZE>>16);
  783. ptr[5] = LSB_of(PAGESIZE>>16);
  784. ptr[6] = MSB_of(PAGESIZE&0xFFFF);
  785. ptr[7] = LSB_of(PAGESIZE&0xFFFF);
  786. sddr55_read_map(us);
  787. return USB_STOR_TRANSPORT_GOOD;
  788. }
  789. if (srb->cmnd[0] == MODE_SENSE) {
  790. mode_page_01[2] = (info->read_only || info->force_read_only) ? 0x80 : 0;
  791. if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
  792. US_DEBUGP(
  793.   "SDDR55: Dummy up request for mode page 1n");
  794. if (ptr==NULL || 
  795.   srb->request_bufflen<sizeof(mode_page_01)) {
  796. set_sense_info (5, 0x24, 0); /* invalid field in command */
  797. return USB_STOR_TRANSPORT_FAILED;
  798. }
  799. memcpy(ptr, mode_page_01, sizeof(mode_page_01));
  800. return USB_STOR_TRANSPORT_GOOD;
  801. } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
  802. US_DEBUGP(
  803.   "SDDR55: Dummy up request for all mode pagesn");
  804. if (ptr==NULL || 
  805.   srb->request_bufflen<sizeof(mode_page_01)) {
  806. set_sense_info (5, 0x24, 0); /* invalid field in command */
  807. return USB_STOR_TRANSPORT_FAILED;
  808. }
  809. memcpy(ptr, mode_page_01, sizeof(mode_page_01));
  810. return USB_STOR_TRANSPORT_GOOD;
  811. }
  812. set_sense_info (5, 0x24, 0); /* invalid field in command */
  813. return USB_STOR_TRANSPORT_FAILED;
  814. }
  815. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  816. US_DEBUGP(
  817.   "SDDR55: %s medium removal. Not that I can do"
  818.   " anything about it...n",
  819.   (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
  820. return USB_STOR_TRANSPORT_GOOD;
  821. }
  822. if (srb->cmnd[0] == READ_10 || srb->cmnd[0] == WRITE_10) {
  823. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  824. page <<= 16;
  825. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  826. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  827. page <<= info->smallpageshift;
  828. // convert page to block and page-within-block
  829. lba = page >> info->blockshift;
  830. page = page & info->blockmask;
  831. // locate physical block corresponding to logical block
  832. if (lba >= info->max_log_blks) {
  833. US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
  834.   "block %04Xn", lba, info->max_log_blks-1);
  835. set_sense_info (5, 0x24, 0); /* invalid field in command */
  836. return USB_STOR_TRANSPORT_FAILED;
  837. }
  838. pba = info->lba_to_pba[lba];
  839. if (srb->cmnd[0] == WRITE_10) {
  840. US_DEBUGP("WRITE_10: write block %04X (LBA %04X) page %01X"
  841.         " pages %dn",
  842.         pba, lba, page, pages);
  843. return sddr55_write_data(us, lba, page, pages, ptr, srb->use_sg);
  844. } else {
  845. US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
  846.         " pages %dn",
  847.         pba, lba, page, pages);
  848. return sddr55_read_data(us, lba, page, pages, ptr, srb->use_sg);
  849. }
  850. }
  851. if (srb->cmnd[0] == TEST_UNIT_READY) {
  852. return USB_STOR_TRANSPORT_GOOD;
  853. }
  854. if (srb->cmnd[0] == START_STOP) {
  855. return USB_STOR_TRANSPORT_GOOD;
  856. }
  857. set_sense_info (5, 0x20, 0); /* illegal command */
  858. return USB_STOR_TRANSPORT_FAILED; // FIXME: sense buffer?
  859. }