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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Driver for SanDisk SDDR-09 SmartMedia reader
  2.  *
  3.  * $Id: sddr09.c,v 1.23 2002/02/25 00:40:13 mdharm Exp $
  4.  *
  5.  * SDDR09 driver v0.1:
  6.  *
  7.  * First release
  8.  *
  9.  * Current development and maintenance by:
  10.  *   (c) 2000, 2001 Robert Baruch (autophile@starband.net)
  11.  *
  12.  * Developed with the assistance of:
  13.  *   (c) 2002 Alan Stern <stern@rowland.org>
  14.  *
  15.  * The SanDisk SDDR-09 SmartMedia reader uses the Shuttle EUSB-01 chip.
  16.  * This chip is a programmable USB controller. In the SDDR-09, it has
  17.  * been programmed to obey a certain limited set of SCSI commands. This
  18.  * driver translates the "real" SCSI commands to the SDDR-09 SCSI
  19.  * commands.
  20.  *
  21.  * This program is free software; you can redistribute it and/or modify it
  22.  * under the terms of the GNU General Public License as published by the
  23.  * Free Software Foundation; either version 2, or (at your option) any
  24.  * later version.
  25.  *
  26.  * This program is distributed in the hope that it will be useful, but
  27.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  28.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  29.  * General Public License for more details.
  30.  *
  31.  * You should have received a copy of the GNU General Public License along
  32.  * with this program; if not, write to the Free Software Foundation, Inc.,
  33.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  34.  */
  35. #include "transport.h"
  36. #include "protocol.h"
  37. #include "usb.h"
  38. #include "debug.h"
  39. #include "sddr09.h"
  40. #include <linux/sched.h>
  41. #include <linux/errno.h>
  42. #include <linux/slab.h>
  43. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  44. #define LSB_of(s) ((s)&0xFF)
  45. #define MSB_of(s) ((s)>>8)
  46. /*
  47.  * Send a control message and wait for the response.
  48.  *
  49.  * us - the pointer to the us_data structure for the device to use
  50.  *
  51.  * request - the URB Setup Packet's first 6 bytes. The first byte always
  52.  *  corresponds to the request type, and the second byte always corresponds
  53.  *  to the request.  The other 4 bytes do not correspond to value and index,
  54.  *  since they are used in a custom way by the SCM protocol.
  55.  *
  56.  * xfer_data - a buffer from which to get, or to which to store, any data
  57.  *  that gets send or received, respectively, with the URB. Even though
  58.  *  it looks like we allocate a buffer in this code for the data, xfer_data
  59.  *  must contain enough allocated space.
  60.  *
  61.  * xfer_len - the number of bytes to send or receive with the URB.
  62.  *
  63.  */
  64. static int sddr09_send_control(struct us_data *us,
  65. int pipe,
  66. unsigned char request,
  67. unsigned char requesttype,
  68. unsigned short value,
  69. unsigned short index,
  70. unsigned char *xfer_data,
  71. unsigned int xfer_len) {
  72. int result;
  73. // If data is going to be sent or received with the URB,
  74. // then allocate a buffer for it. If data is to be sent,
  75. // copy the data into the buffer.
  76. /*
  77. if (xfer_len > 0) {
  78. buffer = kmalloc(xfer_len, GFP_NOIO);
  79. if (!(command[0] & USB_DIR_IN))
  80. memcpy(buffer, xfer_data, xfer_len);
  81. }
  82. */
  83. // Send the URB to the device and wait for a response.
  84. /* Why are request and request type reversed in this call? */
  85. result = usb_stor_control_msg(us, pipe,
  86. request, requesttype, value, index,
  87. xfer_data, xfer_len);
  88. // If data was sent or received with the URB, free the buffer we
  89. // allocated earlier, but not before reading the data out of the
  90. // buffer if we wanted to receive data.
  91. /*
  92. if (xfer_len > 0) {
  93. if (command[0] & USB_DIR_IN)
  94. memcpy(xfer_data, buffer, xfer_len);
  95. kfree(buffer);
  96. }
  97. */
  98. // Check the return code for the command.
  99. if (result < 0) {
  100. /* if the command was aborted, indicate that */
  101. if (result == -ENOENT)
  102. return USB_STOR_TRANSPORT_ABORTED;
  103. /* a stall is a fatal condition from the device */
  104. if (result == -EPIPE) {
  105. US_DEBUGP("-- Stall on control pipe. Clearingn");
  106. result = usb_stor_clear_halt(us, pipe);
  107. US_DEBUGP("-- usb_stor_clear_halt() returns %dn", result);
  108. return USB_STOR_TRANSPORT_FAILED;
  109. }
  110. /* Uh oh... serious problem here */
  111. return USB_STOR_TRANSPORT_ERROR;
  112. }
  113. return USB_STOR_TRANSPORT_GOOD;
  114. }
  115. static int sddr09_raw_bulk(struct us_data *us, 
  116. int direction,
  117. unsigned char *data,
  118. unsigned int len) {
  119. int result;
  120. int act_len;
  121. int pipe;
  122. if (direction == SCSI_DATA_READ)
  123. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  124. else
  125. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  126. result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);
  127.         /* if we stall, we need to clear it before we go on */
  128.         if (result == -EPIPE) {
  129.                 US_DEBUGP("EPIPE: clearing endpoint halt for"
  130. " pipe 0x%x, stalled at %d bytesn",
  131. pipe, act_len);
  132.                 usb_stor_clear_halt(us, pipe);
  133.         }
  134. if (result) {
  135.                 /* NAK - that means we've retried a few times already */
  136.                 if (result == -ETIMEDOUT) {
  137.                         US_DEBUGP("usbat_raw_bulk():"
  138. " device NAKedn");
  139.                         return US_BULK_TRANSFER_FAILED;
  140.                 }
  141.                 /* -ENOENT -- we canceled this transfer */
  142.                 if (result == -ENOENT) {
  143.                         US_DEBUGP("usbat_raw_bulk():"
  144. " transfer abortedn");
  145.                         return US_BULK_TRANSFER_ABORTED;
  146.                 }
  147. if (result == -EPIPE) {
  148. US_DEBUGP("usbat_raw_bulk():"
  149. " output pipe stalledn");
  150. return USB_STOR_TRANSPORT_FAILED;
  151. }
  152.                 /* the catch-all case */
  153.                 US_DEBUGP("us_transfer_partial(): unknown errorn");
  154.                 return US_BULK_TRANSFER_FAILED;
  155.         }
  156. if (act_len != len) {
  157. US_DEBUGP("Warning: Transferred only %d bytesn",
  158. act_len);
  159. return US_BULK_TRANSFER_SHORT;
  160. }
  161. US_DEBUGP("Transferred %d of %d bytesn", act_len, len);
  162. return US_BULK_TRANSFER_GOOD;
  163. }
  164. /*
  165.  * Note: direction must be set if command_len == 0.
  166.  */
  167. static int sddr09_bulk_transport(struct us_data *us,
  168.   int direction,
  169.   unsigned char *data,
  170.   unsigned int len,
  171.   int use_sg) {
  172. int result = USB_STOR_TRANSPORT_GOOD;
  173. int transferred = 0;
  174. int i;
  175. struct scatterlist *sg;
  176. char string[64];
  177. if (len==0)
  178. return USB_STOR_TRANSPORT_GOOD;
  179. /* transfer the data */
  180. if (direction == SCSI_DATA_WRITE) {
  181. /* Debug-print the first 48 bytes of the write transfer */
  182. if (!use_sg) {
  183. strcpy(string, "wr: ");
  184. for (i=0; i<len && i<48; i++) {
  185. sprintf(string+strlen(string), "%02X ",
  186.   data[i]);
  187. if ((i%16)==15) {
  188. US_DEBUGP("%sn", string);
  189. strcpy(string, "wr: ");
  190. }
  191. }
  192. if ((i%16)!=0)
  193. US_DEBUGP("%sn", string);
  194. }
  195. }
  196. US_DEBUGP("SCM data %s transfer %d sg buffers %dn",
  197.   ( direction==SCSI_DATA_READ ? "in" : "out"),
  198.   len, use_sg);
  199. if (!use_sg)
  200. result = sddr09_raw_bulk(us, direction, data, len);
  201. else {
  202. sg = (struct scatterlist *)data;
  203. for (i=0; i<use_sg && transferred<len; i++) {
  204. result = sddr09_raw_bulk(us, direction,
  205. sg[i].address, 
  206. len-transferred > sg[i].length ?
  207. sg[i].length : len-transferred);
  208. if (result!=US_BULK_TRANSFER_GOOD)
  209. break;
  210. transferred += sg[i].length;
  211. }
  212. }
  213. if (direction == SCSI_DATA_READ) {
  214. /* Debug-print the first 48 bytes of the read transfer */
  215. if (!use_sg) {
  216. strcpy(string, "rd: ");
  217. for (i=0; i<len && i<48; i++) {
  218. sprintf(string+strlen(string), "%02X ",
  219.   data[i]);
  220. if ((i%16)==15) {
  221. US_DEBUGP("%sn", string);
  222. strcpy(string, "rd: ");
  223. }
  224. }
  225. if ((i%16)!=0)
  226. US_DEBUGP("%sn", string);
  227. }
  228. }
  229. return result;
  230. }
  231. int sddr09_read_data(struct us_data *us,
  232. unsigned long address,
  233. unsigned short sectors,
  234. unsigned char *content,
  235. int use_sg) {
  236. int result;
  237. unsigned char command[12] = {
  238. 0xe8, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  239. };
  240. struct sddr09_card_info *info = (struct sddr09_card_info *)us->extra;
  241. unsigned int lba;
  242. unsigned int pba;
  243. unsigned short page;
  244. unsigned short pages;
  245. unsigned char *buffer = NULL;
  246. unsigned char *ptr;
  247. struct scatterlist *sg = NULL;
  248. int i;
  249. int len;
  250. int transferred;
  251. // If we're using scatter-gather, we have to create a new
  252. // buffer to read all of the data in first, since a
  253. // scatter-gather buffer could in theory start in the middle
  254. // of a page, which would be bad. A developer who wants a
  255. // challenge might want to write a limited-buffer
  256. // version of this code.
  257. len = sectors*info->pagesize;
  258. if (use_sg) {
  259. sg = (struct scatterlist *)content;
  260. buffer = kmalloc(len, GFP_NOIO);
  261. if (buffer == NULL)
  262. return USB_STOR_TRANSPORT_ERROR;
  263. ptr = buffer;
  264. } else
  265. ptr = content;
  266. // Figure out the initial LBA and page
  267. pba = address >> (info->pageshift + info->blockshift);
  268. lba = info->pba_to_lba[pba];
  269. page = (address >> info->pageshift) & info->blockmask;
  270. // This could be made much more efficient by checking for
  271. // contiguous LBA's. Another exercise left to the student.
  272. while (sectors>0) {
  273. pba = info->lba_to_pba[lba];
  274. // Read as many sectors as possible in this block
  275. pages = info->blocksize - page;
  276. if (pages > sectors)
  277. pages = sectors;
  278. US_DEBUGP("Read %02X pages, from PBA %04X"
  279. " (LBA %04X) page %02Xn",
  280. pages, pba, lba, page);
  281. address = ( (pba << info->blockshift) + page ) << 
  282. info->pageshift;
  283. // Unlike in the documentation, the address is in
  284. // words of 2 bytes.
  285. command[2] = MSB_of(address>>17);
  286. command[3] = LSB_of(address>>17); 
  287. command[4] = MSB_of((address>>1)&0xFFFF);
  288. command[5] = LSB_of((address>>1)&0xFFFF); 
  289. command[10] = MSB_of(pages);
  290. command[11] = LSB_of(pages);
  291. result = sddr09_send_control(us,
  292. usb_sndctrlpipe(us->pusb_dev,0),
  293. 0,
  294. 0x41,
  295. 0,
  296. 0,
  297. command,
  298. 12);
  299. US_DEBUGP("Result for send_control in read_data %dn",
  300. result);
  301. if (result != USB_STOR_TRANSPORT_GOOD) {
  302. if (use_sg)
  303. kfree(buffer);
  304. return result;
  305. }
  306. result = sddr09_bulk_transport(us,
  307. SCSI_DATA_READ, ptr,
  308. pages<<info->pageshift, 0);
  309. if (result != USB_STOR_TRANSPORT_GOOD) {
  310. if (use_sg)
  311. kfree(buffer);
  312. return result;
  313. }
  314. page = 0;
  315. lba++;
  316. sectors -= pages;
  317. ptr += (pages << info->pageshift);
  318. }
  319. if (use_sg) {
  320. transferred = 0;
  321. for (i=0; i<use_sg && transferred<len; i++) {
  322. memcpy(sg[i].address, buffer+transferred,
  323. len-transferred > sg[i].length ?
  324. sg[i].length : len-transferred);
  325. transferred += sg[i].length;
  326. }
  327. kfree(buffer);
  328. }
  329. return USB_STOR_TRANSPORT_GOOD;
  330. }
  331. int sddr09_read_control(struct us_data *us,
  332. unsigned long address,
  333. unsigned short blocks,
  334. unsigned char *content,
  335. int use_sg) {
  336. // Unlike in the documentation, the last two bytes are the
  337. // number of blocks, not sectors.
  338. int result;
  339. unsigned char command[12] = {
  340. 0xe8, 0x21, MSB_of(address>>16),
  341. LSB_of(address>>16), MSB_of(address&0xFFFF),
  342. LSB_of(address&0xFFFF), 0, 0, 0, 0,
  343. MSB_of(blocks), LSB_of(blocks)
  344. };
  345. US_DEBUGP("Read control address %08lX blocks %04Xn",
  346. address, blocks);
  347. result = sddr09_send_control(us,
  348. usb_sndctrlpipe(us->pusb_dev,0),
  349. 0,
  350. 0x41,
  351. 0,
  352. 0,
  353. command,
  354. 12);
  355. US_DEBUGP("Result for send_control in read_control %dn",
  356. result);
  357. if (result != USB_STOR_TRANSPORT_GOOD)
  358. return result;
  359. result = sddr09_bulk_transport(us,
  360. SCSI_DATA_READ, content,
  361. blocks<<6, use_sg); // 0x40 bytes per block
  362. US_DEBUGP("Result for bulk read in read_control %dn",
  363. result);
  364. return result;
  365. }
  366. int sddr09_read_deviceID(struct us_data *us,
  367. unsigned char *manufacturerID,
  368. unsigned char *deviceID) {
  369. int result;
  370. unsigned char command[12] = {
  371. 0xed, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  372. };
  373. unsigned char content[64];
  374. result = sddr09_send_control(us,
  375. usb_sndctrlpipe(us->pusb_dev,0),
  376. 0,
  377. 0x41,
  378. 0,
  379. 0,
  380. command,
  381. 12);
  382. US_DEBUGP("Result of send_control for device ID is %dn",
  383. result);
  384. if (result != USB_STOR_TRANSPORT_GOOD)
  385. return result;
  386. result = sddr09_bulk_transport(us,
  387. SCSI_DATA_READ, content,
  388. 64, 0);
  389. *manufacturerID = content[0];
  390. *deviceID = content[1];
  391. return result;
  392. }
  393. int sddr09_read_status(struct us_data *us,
  394. unsigned char *status) {
  395. int result;
  396. unsigned char command[12] = {
  397. 0xec, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  398. };
  399. result = sddr09_send_control(us,
  400. usb_sndctrlpipe(us->pusb_dev,0),
  401. 0,
  402. 0x41,
  403. 0,
  404. 0,
  405. command,
  406. 12);
  407. if (result != USB_STOR_TRANSPORT_GOOD)
  408. return result;
  409. result = sddr09_bulk_transport(us,
  410. SCSI_DATA_READ, status,
  411. 1, 0);
  412. return result;
  413. }
  414. int sddr09_reset(struct us_data *us) {
  415. int result;
  416. unsigned char command[12] = {
  417. 0xeb, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  418. };
  419. result = sddr09_send_control(us,
  420. usb_sndctrlpipe(us->pusb_dev,0),
  421. 0,
  422. 0x41,
  423. 0,
  424. 0,
  425. command,
  426. 12);
  427. return result;
  428. }
  429. unsigned long sddr09_get_capacity(struct us_data *us,
  430. unsigned int *pagesize, unsigned int *blocksize) {
  431. unsigned char manufacturerID;
  432. unsigned char deviceID;
  433. int result;
  434. US_DEBUGP("Reading capacity...n");
  435. result = sddr09_read_deviceID(us,
  436. &manufacturerID,
  437. &deviceID);
  438. US_DEBUGP("Result of read_deviceID is %dn",
  439. result);
  440. if (result != USB_STOR_TRANSPORT_GOOD)
  441. return 0;
  442. US_DEBUGP("Device ID = %02Xn", deviceID);
  443. US_DEBUGP("Manuf  ID = %02Xn", manufacturerID);
  444. *pagesize = 512;
  445. *blocksize = 16;
  446. switch (deviceID) {
  447. case 0x6e: // 1MB
  448. case 0xe8:
  449. case 0xec:
  450. *pagesize = 256;
  451. return 0x00100000;
  452. case 0xea: // 2MB
  453. case 0x5d: // 5d is a ROM card with pagesize 512.
  454. case 0x64:
  455. if (deviceID!=0x5D)
  456. *pagesize = 256;
  457. return 0x00200000;
  458. case 0xe3: // 4MB
  459. case 0xe5:
  460. case 0x6b:
  461. case 0xd5:
  462. return 0x00400000;
  463. case 0xe6: // 8MB
  464. case 0xd6:
  465. return 0x00800000;
  466. case 0x73: // 16MB
  467. *blocksize = 32;
  468. return 0x01000000;
  469. case 0x75: // 32MB
  470. *blocksize = 32;
  471. return 0x02000000;
  472. case 0x76: // 64MB
  473. *blocksize = 32;
  474. return 0x04000000;
  475. case 0x79: // 128MB
  476. *blocksize = 32;
  477. return 0x08000000;
  478. default: // unknown
  479. return 0;
  480. }
  481. }
  482. int sddr09_read_map(struct us_data *us) {
  483. struct scatterlist *sg;
  484. struct sddr09_card_info *info = (struct sddr09_card_info *)(us->extra);
  485. int numblocks;
  486. int i;
  487. unsigned char *ptr;
  488. unsigned short lba;
  489. unsigned char parity;
  490. unsigned char fast_parity[16] = {
  491. 0, 1, 1, 0, 1, 0, 0, 1,
  492. 1, 0, 0, 1, 0, 1, 1, 0
  493. };
  494. int result;
  495. int alloc_len;
  496. int alloc_blocks;
  497. if (!info->capacity)
  498. return -1;
  499. // read 64 (1<<6) bytes for every block 
  500. // ( 1 << ( blockshift + pageshift ) bytes)
  501. //  of capacity:
  502. // (1<<6)*capacity/(1<<(b+p)) =
  503. // ((1<<6)*capacity)>>(b+p) =
  504. // capacity>>(b+p-6)
  505. alloc_len = info->capacity >> 
  506. (info->blockshift + info->pageshift - 6);
  507. // Allocate a number of scatterlist structures according to
  508. // the number of 128k blocks in the alloc_len. Adding 128k-1
  509. // and then dividing by 128k gives the correct number of blocks.
  510. // 128k = 1<<17
  511. alloc_blocks = (alloc_len + (1<<17) - 1) >> 17;
  512. sg = kmalloc(alloc_blocks*sizeof(struct scatterlist),
  513. GFP_NOIO);
  514. if (sg == NULL)
  515. return 0;
  516. for (i=0; i<alloc_blocks; i++) {
  517. if (i<alloc_blocks-1) {
  518. sg[i].address = kmalloc( (1<<17), GFP_NOIO );
  519. sg[i].page = NULL;
  520. sg[i].length = (1<<17);
  521. } else {
  522. sg[i].address = kmalloc(alloc_len, GFP_NOIO);
  523. sg[i].page = NULL;
  524. sg[i].length = alloc_len;
  525. }
  526. alloc_len -= sg[i].length;
  527. }
  528. for (i=0; i<alloc_blocks; i++)
  529. if (sg[i].address == NULL) {
  530. for (i=0; i<alloc_blocks; i++)
  531. if (sg[i].address != NULL)
  532. kfree(sg[i].address);
  533. kfree(sg);
  534. return 0;
  535. }
  536. numblocks = info->capacity >> (info->blockshift + info->pageshift);
  537. if ( (result = sddr09_read_control(us, 0, numblocks,
  538. (unsigned char *)sg, alloc_blocks)) !=
  539. USB_STOR_TRANSPORT_GOOD) {
  540. for (i=0; i<alloc_blocks; i++)
  541. kfree(sg[i].address);
  542. kfree(sg);
  543. return -1;
  544. }
  545. if (info->lba_to_pba)
  546. kfree(info->lba_to_pba);
  547. if (info->pba_to_lba)
  548. kfree(info->pba_to_lba);
  549. info->lba_to_pba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  550. info->pba_to_lba = kmalloc(numblocks*sizeof(int), GFP_NOIO);
  551. if (info->lba_to_pba == NULL || info->pba_to_lba == NULL) {
  552. if (info->lba_to_pba != NULL)
  553. kfree(info->lba_to_pba);
  554. if (info->pba_to_lba != NULL)
  555. kfree(info->pba_to_lba);
  556. info->lba_to_pba = NULL;
  557. info->pba_to_lba = NULL;
  558. for (i=0; i<alloc_blocks; i++)
  559. kfree(sg[i].address);
  560. kfree(sg);
  561. return 0;
  562. }
  563. memset(info->lba_to_pba, 0, numblocks*sizeof(int));
  564. memset(info->pba_to_lba, 0, numblocks*sizeof(int));
  565. // Each block is 64 bytes of control data, so block i is located in
  566. // scatterlist block i*64/128k = i*(2^6)*(2^-17) = i*(2^-11)
  567. for (i=0; i<numblocks; i++) {
  568. ptr = sg[i>>11].address+((i&0x7ff)<<6);
  569. if (ptr[0]!=0xFF || ptr[1]!=0xFF || ptr[2]!=0xFF ||
  570.     ptr[3]!=0xFF || ptr[4]!=0xFF || ptr[5]!=0xFF) {
  571. US_DEBUGP("PBA %04X has no logical mapping: reserved area = "
  572.   "%02X%02X%02X%02X data status %02X block status %02Xn",
  573.   i, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
  574. continue;
  575. }
  576. if ((ptr[6]>>4)!=0x01) {
  577. US_DEBUGP("PBA %04X has invalid address field %02X%02X/%02X%02Xn",
  578.   i, ptr[6], ptr[7], ptr[11], ptr[12]);
  579. continue;
  580. }
  581. /* ensure even parity */
  582. lba = short_pack(ptr[7], ptr[6]);
  583. parity = 1; // the parity of 0x1000
  584. parity ^= fast_parity[lba & 0x000F];
  585. parity ^= fast_parity[(lba>>4) & 0x000F];
  586. parity ^= fast_parity[(lba>>8) & 0x000F];
  587. if (parity) { /* bad parity bit */
  588. US_DEBUGP("Bad parity in LBA for block %04Xn", i);
  589. continue;
  590. }
  591. lba = (lba&0x07FF)>>1;
  592. /* Every 1024 physical blocks ("zone"), the LBA numbers
  593.  * go back to zero, but are within a higher
  594.  * block of LBA's. Also, there is a maximum of
  595.  * 1000 LBA's per zone. In other words, in PBA
  596.  * 1024-2047 you will find LBA 0-999 which are
  597.  * really LBA 1000-1999. Yes, this wastes 24
  598.  * physical blocks per zone. Go figure.
  599.  */
  600. lba += 1000*(i/0x400);
  601. if (lba>=numblocks) {
  602. US_DEBUGP("Bad LBA %04X for block %04Xn", lba, i);
  603. continue;
  604. }
  605. if (lba<0x10 || (lba>=0x3E0 && lba<0x3EF))
  606. US_DEBUGP("LBA %04X <-> PBA %04Xn", lba, i);
  607. info->pba_to_lba[i] = lba;
  608. info->lba_to_pba[lba] = i;
  609. }
  610. for (i=0; i<alloc_blocks; i++)
  611. kfree(sg[i].address);
  612. kfree(sg);
  613. return 0;
  614. }
  615. /*
  616. static int init_sddr09(struct us_data *us) {
  617. int result;
  618. unsigned char data[14];
  619. unsigned char command[8] = {
  620. 0xc1, 0x01, 0, 0, 0, 0, 0, 0
  621. };
  622. unsigned char command2[8] = {
  623. 0x41, 0, 0, 0, 0, 0, 0, 0
  624. };
  625. unsigned char tur[12] = {
  626. 0x03, 0x20, 0, 0, 0x0e, 0, 0, 0, 0, 0, 0, 0
  627. };
  628. // What the hey is all this for? Doesn't seem to
  629. // affect the device, so we won't do device inits.
  630. if ( (result = sddr09_send_control(us, command, data, 2)) !=
  631. USB_STOR_TRANSPORT_GOOD)
  632. return result;
  633. US_DEBUGP("SDDR09: %02X %02Xn", data[0], data[1]);
  634. command[1] = 0x08;
  635. if ( (result = sddr09_send_control(us, command, data, 2)) !=
  636. USB_STOR_TRANSPORT_GOOD)
  637. return result;
  638. US_DEBUGP("SDDR09: %02X %02Xn", data[0], data[1]);
  639. if ( (result = sddr09_send_control(us, command2, tur, 12)) !=
  640. USB_STOR_TRANSPORT_GOOD) {
  641. US_DEBUGP("SDDR09: request sense failedn");
  642. return result;
  643. }
  644. if ( (result = sddr09_raw_bulk(
  645. us, SCSI_DATA_READ, data, 14)) !=
  646. USB_STOR_TRANSPORT_GOOD) {
  647. US_DEBUGP("SDDR09: request sense bulk in failedn");
  648. return result;
  649. }
  650. US_DEBUGP("SDDR09: request sense workedn");
  651. return result;
  652. }
  653. */
  654. void sddr09_card_info_destructor(void *extra) {
  655. struct sddr09_card_info *info = (struct sddr09_card_info *)extra;
  656. if (!extra)
  657. return;
  658. if (info->lba_to_pba)
  659. kfree(info->lba_to_pba);
  660. if (info->pba_to_lba)
  661. kfree(info->pba_to_lba);
  662. }
  663. /*
  664.  * Transport for the Sandisk SDDR-09
  665.  */
  666. int sddr09_transport(Scsi_Cmnd *srb, struct us_data *us)
  667. {
  668. int result;
  669. int i;
  670. char string[64];
  671. unsigned char inquiry_response[36] = {
  672. 0x00, 0x80, 0x00, 0x02, 0x1F, 0x00, 0x00, 0x00
  673. };
  674. unsigned char mode_page_01[16] = { // write-protected for now
  675. 0x03, 0x00, 0x80, 0x00,
  676. 0x01, 0x0A,
  677. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  678. };
  679. unsigned char *ptr;
  680. unsigned long capacity;
  681. unsigned int lba;
  682. unsigned int pba;
  683. unsigned int page;
  684. unsigned short pages;
  685. struct sddr09_card_info *info = (struct sddr09_card_info *)(us->extra);
  686. if (!us->extra) {
  687. us->extra = kmalloc(
  688. sizeof(struct sddr09_card_info), GFP_NOIO);
  689. if (!us->extra)
  690. return USB_STOR_TRANSPORT_ERROR;
  691. memset(us->extra, 0, sizeof(struct sddr09_card_info));
  692. us->extra_destructor = sddr09_card_info_destructor;
  693. }
  694. ptr = (unsigned char *)srb->request_buffer;
  695. /* Dummy up a response for INQUIRY since SDDR09 doesn't
  696.    respond to INQUIRY commands */
  697. if (srb->cmnd[0] == INQUIRY) {
  698. memset(inquiry_response+8, 0, 28);
  699. fill_inquiry_response(us, inquiry_response, 36);
  700. return USB_STOR_TRANSPORT_GOOD;
  701. }
  702. if (srb->cmnd[0] == READ_CAPACITY) {
  703. capacity = sddr09_get_capacity(us, &info->pagesize,
  704. &info->blocksize);
  705. if (!capacity)
  706. return USB_STOR_TRANSPORT_FAILED;
  707. info->capacity = capacity;
  708. for (info->pageshift=1; 
  709. (info->pagesize>>info->pageshift);
  710. info->pageshift++);
  711. info->pageshift--;
  712. for (info->blockshift=1; 
  713. (info->blocksize>>info->blockshift);
  714. info->blockshift++);
  715. info->blockshift--;
  716. info->blockmask = (1<<info->blockshift)-1;
  717. // Last page in the card
  718. capacity /= info->pagesize;
  719. capacity--;
  720. ptr[0] = MSB_of(capacity>>16);
  721. ptr[1] = LSB_of(capacity>>16);
  722. ptr[2] = MSB_of(capacity&0xFFFF);
  723. ptr[3] = LSB_of(capacity&0xFFFF);
  724. // The page size
  725. ptr[4] = MSB_of(info->pagesize>>16);
  726. ptr[5] = LSB_of(info->pagesize>>16);
  727. ptr[6] = MSB_of(info->pagesize&0xFFFF);
  728. ptr[7] = LSB_of(info->pagesize&0xFFFF);
  729. sddr09_read_map(us);
  730. return USB_STOR_TRANSPORT_GOOD;
  731. }
  732. if (srb->cmnd[0] == MODE_SENSE) {
  733. // Read-write error recovery page: there needs to
  734. // be a check for write-protect here
  735. if ( (srb->cmnd[2] & 0x3F) == 0x01 ) {
  736. US_DEBUGP(
  737.   "SDDR09: Dummy up request for mode page 1n");
  738. if (ptr==NULL || 
  739.   srb->request_bufflen<sizeof(mode_page_01))
  740. return USB_STOR_TRANSPORT_ERROR;
  741. memcpy(ptr, mode_page_01, sizeof(mode_page_01));
  742. return USB_STOR_TRANSPORT_GOOD;
  743. } else if ( (srb->cmnd[2] & 0x3F) == 0x3F ) {
  744. US_DEBUGP(
  745.   "SDDR09: Dummy up request for all mode pagesn");
  746. if (ptr==NULL || 
  747.   srb->request_bufflen<sizeof(mode_page_01))
  748. return USB_STOR_TRANSPORT_ERROR;
  749. memcpy(ptr, mode_page_01, sizeof(mode_page_01));
  750. return USB_STOR_TRANSPORT_GOOD;
  751. }
  752. // FIXME: sense buffer?
  753. return USB_STOR_TRANSPORT_ERROR;
  754. }
  755. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  756. US_DEBUGP(
  757.   "SDDR09: %s medium removal. Not that I can do"
  758.   " anything about it...n",
  759.   (srb->cmnd[4]&0x03) ? "Prevent" : "Allow");
  760. return USB_STOR_TRANSPORT_GOOD;
  761. }
  762. if (srb->cmnd[0] == READ_10) {
  763. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  764. page <<= 16;
  765. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  766. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  767. // convert page to block and page-within-block
  768. lba = page >> info->blockshift;
  769. page = page & info->blockmask;
  770. // locate physical block corresponding to logical block
  771. if (lba >=
  772. (info->capacity >> 
  773. (info->pageshift + info->blockshift) ) ) {
  774. US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
  775.   "block %04lXn", lba,
  776.   (info->capacity >> (info->pageshift + info->blockshift))-1);
  777. // FIXME: sense buffer?
  778. return USB_STOR_TRANSPORT_ERROR;
  779. }
  780. pba = info->lba_to_pba[lba];
  781. // if pba is 0, either it's really 0, in which case
  782. // the pba-to-lba map for pba 0 will be the lba,
  783. // or that lba doesn't exist.
  784. if (pba==0 && info->pba_to_lba[0] != lba) {
  785. // FIXME: sense buffer?
  786. US_DEBUGP("Error: Requested LBA %04X has no physical block "
  787.   "mapping.n", lba);
  788. return USB_STOR_TRANSPORT_ERROR;
  789. }
  790. US_DEBUGP("READ_10: read block %04X (LBA %04X) page %01X"
  791. " pages %dn",
  792. pba, lba, page, pages);
  793. return sddr09_read_data(us,
  794. ( (pba << info->blockshift) + page) << info->pageshift,
  795. pages, ptr, srb->use_sg);
  796. }
  797. // Pass TEST_UNIT_READY and REQUEST_SENSE through
  798. if (srb->cmnd[0] != TEST_UNIT_READY &&
  799.     srb->cmnd[0] != REQUEST_SENSE)
  800. return USB_STOR_TRANSPORT_ERROR; // FIXME: sense buffer?
  801. for (; srb->cmd_len<12; srb->cmd_len++)
  802. srb->cmnd[srb->cmd_len] = 0;
  803. srb->cmnd[1] = 0x20;
  804. string[0] = 0;
  805. for (i=0; i<12; i++)
  806.   sprintf(string+strlen(string), "%02X ", srb->cmnd[i]);
  807. US_DEBUGP("SDDR09: Send control for command %sn",
  808. string);
  809. if ( (result = sddr09_send_control(us,
  810. usb_sndctrlpipe(us->pusb_dev,0),
  811. 0,
  812. 0x41,
  813. 0,
  814. 0,
  815. srb->cmnd,
  816. 12)) != USB_STOR_TRANSPORT_GOOD)
  817. return result;
  818. US_DEBUGP("SDDR09: Control for command OKn");
  819. if (srb->request_bufflen == 0)
  820. return USB_STOR_TRANSPORT_GOOD;
  821. if (srb->sc_data_direction == SCSI_DATA_WRITE ||
  822.     srb->sc_data_direction == SCSI_DATA_READ) {
  823. US_DEBUGP("SDDR09: %s %d bytesn",
  824. srb->sc_data_direction==SCSI_DATA_WRITE ?
  825.   "sending" : "receiving",
  826. srb->request_bufflen);
  827. result = sddr09_bulk_transport(us,
  828. srb->sc_data_direction,
  829. srb->request_buffer, 
  830. srb->request_bufflen, srb->use_sg);
  831. return result;
  832. return USB_STOR_TRANSPORT_GOOD;
  833. }