aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/file_storage.c
diff options
context:
space:
mode:
authorYuping Luo <Yuping.Luo@csr.com>2011-10-25 22:13:10 -0400
committerFelipe Balbi <balbi@ti.com>2011-12-12 04:44:59 -0500
commit144974e7f9e32b53b02f6c8632be45d8f43d6ab5 (patch)
treedd7df00f65b13f79ed028bbed2cffd83769faab5 /drivers/usb/gadget/file_storage.c
parentb4fcea2a71cafc59a749fa3ef88e51af8c2e3b37 (diff)
usb: gadget: mass_storage: support multi-luns with different logic block size
With Peiyu's patch "gadget: mass_storage: adapt logic block size to bound block devices" (http://www.spinics.net/lists/linux-usb/msg50791.html), now mass storage can adjust logic block size dynamically based on real devices. Then there is one issue caused by it, if two luns have different logic block size, mass storage can't work. Let's check the current software flow: 1. get_next_command(): call received_cbw(); 2. received_cbw(): update common->lun = cbw->Lun, but common->curlen is not updated; 3. do_scsi_command(): in READ_X and WRITE_X commands, common->data_size_from_cmnd is updated by common->curlun->blkbits; 4. check_command(): update common->curlun according to common->lun As you can see, the step 3 uses wrong common->curlun, then wrong common->curlun->blkbits. If the two luns have same blkbits, there isn't issue. Otherwise, both will fail. This patch moves the common->curlun update to step 1, then make sure step 3 gets right blkbits and data_size_from_cmnd. Cc: Peiyu Li <peiyu.li@csr.com> Signed-off-by: YuPing Luo <yuping.luo@csr.com> Signed-off-by: Barry Song <Baohua.Song@csr.com> Acked-by: Alan Stern <stern@rowland.harvard.edu> Acked-by: Michal Nazarewicz <mina86@mina86.com> Signed-off-by: Felipe Balbi <balbi@ti.com>
Diffstat (limited to 'drivers/usb/gadget/file_storage.c')
-rw-r--r--drivers/usb/gadget/file_storage.c62
1 files changed, 41 insertions, 21 deletions
diff --git a/drivers/usb/gadget/file_storage.c b/drivers/usb/gadget/file_storage.c
index 11b5196284ae..e8ff2f1c06cc 100644
--- a/drivers/usb/gadget/file_storage.c
+++ b/drivers/usb/gadget/file_storage.c
@@ -2297,19 +2297,17 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2297 DBG(fsg, "using LUN %d from CBW, " 2297 DBG(fsg, "using LUN %d from CBW, "
2298 "not LUN %d from CDB\n", 2298 "not LUN %d from CDB\n",
2299 fsg->lun, lun); 2299 fsg->lun, lun);
2300 } else 2300 }
2301 fsg->lun = lun; // Use LUN from the command
2302 2301
2303 /* Check the LUN */ 2302 /* Check the LUN */
2304 if (fsg->lun < fsg->nluns) { 2303 curlun = fsg->curlun;
2305 fsg->curlun = curlun = &fsg->luns[fsg->lun]; 2304 if (curlun) {
2306 if (fsg->cmnd[0] != REQUEST_SENSE) { 2305 if (fsg->cmnd[0] != REQUEST_SENSE) {
2307 curlun->sense_data = SS_NO_SENSE; 2306 curlun->sense_data = SS_NO_SENSE;
2308 curlun->sense_data_info = 0; 2307 curlun->sense_data_info = 0;
2309 curlun->info_valid = 0; 2308 curlun->info_valid = 0;
2310 } 2309 }
2311 } else { 2310 } else {
2312 fsg->curlun = curlun = NULL;
2313 fsg->bad_lun_okay = 0; 2311 fsg->bad_lun_okay = 0;
2314 2312
2315 /* INQUIRY and REQUEST SENSE commands are explicitly allowed 2313 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
@@ -2351,6 +2349,16 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2351 return 0; 2349 return 0;
2352} 2350}
2353 2351
2352/* wrapper of check_command for data size in blocks handling */
2353static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
2354 enum data_direction data_dir, unsigned int mask,
2355 int needs_medium, const char *name)
2356{
2357 if (fsg->curlun)
2358 fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
2359 return check_command(fsg, cmnd_size, data_dir,
2360 mask, needs_medium, name);
2361}
2354 2362
2355static int do_scsi_command(struct fsg_dev *fsg) 2363static int do_scsi_command(struct fsg_dev *fsg)
2356{ 2364{
@@ -2425,26 +2433,27 @@ static int do_scsi_command(struct fsg_dev *fsg)
2425 2433
2426 case READ_6: 2434 case READ_6:
2427 i = fsg->cmnd[4]; 2435 i = fsg->cmnd[4];
2428 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits; 2436 fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
2429 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2437 if ((reply = check_command_size_in_blocks(fsg, 6,
2438 DATA_DIR_TO_HOST,
2430 (7<<1) | (1<<4), 1, 2439 (7<<1) | (1<<4), 1,
2431 "READ(6)")) == 0) 2440 "READ(6)")) == 0)
2432 reply = do_read(fsg); 2441 reply = do_read(fsg);
2433 break; 2442 break;
2434 2443
2435 case READ_10: 2444 case READ_10:
2436 fsg->data_size_from_cmnd = 2445 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2437 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits; 2446 if ((reply = check_command_size_in_blocks(fsg, 10,
2438 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2447 DATA_DIR_TO_HOST,
2439 (1<<1) | (0xf<<2) | (3<<7), 1, 2448 (1<<1) | (0xf<<2) | (3<<7), 1,
2440 "READ(10)")) == 0) 2449 "READ(10)")) == 0)
2441 reply = do_read(fsg); 2450 reply = do_read(fsg);
2442 break; 2451 break;
2443 2452
2444 case READ_12: 2453 case READ_12:
2445 fsg->data_size_from_cmnd = 2454 fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
2446 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits; 2455 if ((reply = check_command_size_in_blocks(fsg, 12,
2447 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, 2456 DATA_DIR_TO_HOST,
2448 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2457 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2449 "READ(12)")) == 0) 2458 "READ(12)")) == 0)
2450 reply = do_read(fsg); 2459 reply = do_read(fsg);
@@ -2529,26 +2538,27 @@ static int do_scsi_command(struct fsg_dev *fsg)
2529 2538
2530 case WRITE_6: 2539 case WRITE_6:
2531 i = fsg->cmnd[4]; 2540 i = fsg->cmnd[4];
2532 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << fsg->curlun->blkbits; 2541 fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
2533 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 2542 if ((reply = check_command_size_in_blocks(fsg, 6,
2543 DATA_DIR_FROM_HOST,
2534 (7<<1) | (1<<4), 1, 2544 (7<<1) | (1<<4), 1,
2535 "WRITE(6)")) == 0) 2545 "WRITE(6)")) == 0)
2536 reply = do_write(fsg); 2546 reply = do_write(fsg);
2537 break; 2547 break;
2538 2548
2539 case WRITE_10: 2549 case WRITE_10:
2540 fsg->data_size_from_cmnd = 2550 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
2541 get_unaligned_be16(&fsg->cmnd[7]) << fsg->curlun->blkbits; 2551 if ((reply = check_command_size_in_blocks(fsg, 10,
2542 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2552 DATA_DIR_FROM_HOST,
2543 (1<<1) | (0xf<<2) | (3<<7), 1, 2553 (1<<1) | (0xf<<2) | (3<<7), 1,
2544 "WRITE(10)")) == 0) 2554 "WRITE(10)")) == 0)
2545 reply = do_write(fsg); 2555 reply = do_write(fsg);
2546 break; 2556 break;
2547 2557
2548 case WRITE_12: 2558 case WRITE_12:
2549 fsg->data_size_from_cmnd = 2559 fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
2550 get_unaligned_be32(&fsg->cmnd[6]) << fsg->curlun->blkbits; 2560 if ((reply = check_command_size_in_blocks(fsg, 12,
2551 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, 2561 DATA_DIR_FROM_HOST,
2552 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2562 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2553 "WRITE(12)")) == 0) 2563 "WRITE(12)")) == 0)
2554 reply = do_write(fsg); 2564 reply = do_write(fsg);
@@ -2715,7 +2725,17 @@ static int get_next_command(struct fsg_dev *fsg)
2715 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size); 2725 memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
2716 fsg->cbbuf_cmnd_size = 0; 2726 fsg->cbbuf_cmnd_size = 0;
2717 spin_unlock_irq(&fsg->lock); 2727 spin_unlock_irq(&fsg->lock);
2728
2729 /* Use LUN from the command */
2730 fsg->lun = fsg->cmnd[1] >> 5;
2718 } 2731 }
2732
2733 /* Update current lun */
2734 if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
2735 fsg->curlun = &fsg->luns[fsg->lun];
2736 else
2737 fsg->curlun = NULL;
2738
2719 return rc; 2739 return rc;
2720} 2740}
2721 2741