aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
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
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')
-rw-r--r--drivers/usb/gadget/f_mass_storage.c58
-rw-r--r--drivers/usb/gadget/file_storage.c62
2 files changed, 76 insertions, 44 deletions
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index c39d58860fa0..860e15ac8f24 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -1873,17 +1873,14 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1873 common->lun, lun); 1873 common->lun, lun);
1874 1874
1875 /* Check the LUN */ 1875 /* Check the LUN */
1876 if (common->lun < common->nluns) { 1876 curlun = common->curlun;
1877 curlun = &common->luns[common->lun]; 1877 if (curlun) {
1878 common->curlun = curlun;
1879 if (common->cmnd[0] != REQUEST_SENSE) { 1878 if (common->cmnd[0] != REQUEST_SENSE) {
1880 curlun->sense_data = SS_NO_SENSE; 1879 curlun->sense_data = SS_NO_SENSE;
1881 curlun->sense_data_info = 0; 1880 curlun->sense_data_info = 0;
1882 curlun->info_valid = 0; 1881 curlun->info_valid = 0;
1883 } 1882 }
1884 } else { 1883 } else {
1885 common->curlun = NULL;
1886 curlun = NULL;
1887 common->bad_lun_okay = 0; 1884 common->bad_lun_okay = 0;
1888 1885
1889 /* 1886 /*
@@ -1929,6 +1926,17 @@ static int check_command(struct fsg_common *common, int cmnd_size,
1929 return 0; 1926 return 0;
1930} 1927}
1931 1928
1929/* wrapper of check_command for data size in blocks handling */
1930static int check_command_size_in_blocks(struct fsg_common *common,
1931 int cmnd_size, enum data_direction data_dir,
1932 unsigned int mask, int needs_medium, const char *name)
1933{
1934 if (common->curlun)
1935 common->data_size_from_cmnd <<= common->curlun->blkbits;
1936 return check_command(common, cmnd_size, data_dir,
1937 mask, needs_medium, name);
1938}
1939
1932static int do_scsi_command(struct fsg_common *common) 1940static int do_scsi_command(struct fsg_common *common)
1933{ 1941{
1934 struct fsg_buffhd *bh; 1942 struct fsg_buffhd *bh;
@@ -2011,9 +2019,9 @@ static int do_scsi_command(struct fsg_common *common)
2011 2019
2012 case READ_6: 2020 case READ_6:
2013 i = common->cmnd[4]; 2021 i = common->cmnd[4];
2014 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 2022 common->data_size_from_cmnd = (i == 0) ? 256 : i;
2015 common->curlun->blkbits; 2023 reply = check_command_size_in_blocks(common, 6,
2016 reply = check_command(common, 6, DATA_DIR_TO_HOST, 2024 DATA_DIR_TO_HOST,
2017 (7<<1) | (1<<4), 1, 2025 (7<<1) | (1<<4), 1,
2018 "READ(6)"); 2026 "READ(6)");
2019 if (reply == 0) 2027 if (reply == 0)
@@ -2022,9 +2030,9 @@ static int do_scsi_command(struct fsg_common *common)
2022 2030
2023 case READ_10: 2031 case READ_10:
2024 common->data_size_from_cmnd = 2032 common->data_size_from_cmnd =
2025 get_unaligned_be16(&common->cmnd[7]) << 2033 get_unaligned_be16(&common->cmnd[7]);
2026 common->curlun->blkbits; 2034 reply = check_command_size_in_blocks(common, 10,
2027 reply = check_command(common, 10, DATA_DIR_TO_HOST, 2035 DATA_DIR_TO_HOST,
2028 (1<<1) | (0xf<<2) | (3<<7), 1, 2036 (1<<1) | (0xf<<2) | (3<<7), 1,
2029 "READ(10)"); 2037 "READ(10)");
2030 if (reply == 0) 2038 if (reply == 0)
@@ -2033,9 +2041,9 @@ static int do_scsi_command(struct fsg_common *common)
2033 2041
2034 case READ_12: 2042 case READ_12:
2035 common->data_size_from_cmnd = 2043 common->data_size_from_cmnd =
2036 get_unaligned_be32(&common->cmnd[6]) << 2044 get_unaligned_be32(&common->cmnd[6]);
2037 common->curlun->blkbits; 2045 reply = check_command_size_in_blocks(common, 12,
2038 reply = check_command(common, 12, DATA_DIR_TO_HOST, 2046 DATA_DIR_TO_HOST,
2039 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2047 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2040 "READ(12)"); 2048 "READ(12)");
2041 if (reply == 0) 2049 if (reply == 0)
@@ -2134,9 +2142,9 @@ static int do_scsi_command(struct fsg_common *common)
2134 2142
2135 case WRITE_6: 2143 case WRITE_6:
2136 i = common->cmnd[4]; 2144 i = common->cmnd[4];
2137 common->data_size_from_cmnd = (i == 0 ? 256 : i) << 2145 common->data_size_from_cmnd = (i == 0) ? 256 : i;
2138 common->curlun->blkbits; 2146 reply = check_command_size_in_blocks(common, 6,
2139 reply = check_command(common, 6, DATA_DIR_FROM_HOST, 2147 DATA_DIR_FROM_HOST,
2140 (7<<1) | (1<<4), 1, 2148 (7<<1) | (1<<4), 1,
2141 "WRITE(6)"); 2149 "WRITE(6)");
2142 if (reply == 0) 2150 if (reply == 0)
@@ -2145,9 +2153,9 @@ static int do_scsi_command(struct fsg_common *common)
2145 2153
2146 case WRITE_10: 2154 case WRITE_10:
2147 common->data_size_from_cmnd = 2155 common->data_size_from_cmnd =
2148 get_unaligned_be16(&common->cmnd[7]) << 2156 get_unaligned_be16(&common->cmnd[7]);
2149 common->curlun->blkbits; 2157 reply = check_command_size_in_blocks(common, 10,
2150 reply = check_command(common, 10, DATA_DIR_FROM_HOST, 2158 DATA_DIR_FROM_HOST,
2151 (1<<1) | (0xf<<2) | (3<<7), 1, 2159 (1<<1) | (0xf<<2) | (3<<7), 1,
2152 "WRITE(10)"); 2160 "WRITE(10)");
2153 if (reply == 0) 2161 if (reply == 0)
@@ -2156,9 +2164,9 @@ static int do_scsi_command(struct fsg_common *common)
2156 2164
2157 case WRITE_12: 2165 case WRITE_12:
2158 common->data_size_from_cmnd = 2166 common->data_size_from_cmnd =
2159 get_unaligned_be32(&common->cmnd[6]) << 2167 get_unaligned_be32(&common->cmnd[6]);
2160 common->curlun->blkbits; 2168 reply = check_command_size_in_blocks(common, 12,
2161 reply = check_command(common, 12, DATA_DIR_FROM_HOST, 2169 DATA_DIR_FROM_HOST,
2162 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2170 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2163 "WRITE(12)"); 2171 "WRITE(12)");
2164 if (reply == 0) 2172 if (reply == 0)
@@ -2273,6 +2281,10 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2273 if (common->data_size == 0) 2281 if (common->data_size == 0)
2274 common->data_dir = DATA_DIR_NONE; 2282 common->data_dir = DATA_DIR_NONE;
2275 common->lun = cbw->Lun; 2283 common->lun = cbw->Lun;
2284 if (common->lun >= 0 && common->lun < common->nluns)
2285 common->curlun = &common->luns[common->lun];
2286 else
2287 common->curlun = NULL;
2276 common->tag = cbw->Tag; 2288 common->tag = cbw->Tag;
2277 return 0; 2289 return 0;
2278} 2290}
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