From patchwork Thu Jul 22 18:38:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1508840 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=secospa.onmicrosoft.com header.i=@secospa.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-secospa-onmicrosoft-com header.b=pApJHCFR; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GW1T14sxSz9sRR for ; Fri, 23 Jul 2021 04:39:33 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BA59482BDF; Thu, 22 Jul 2021 20:39:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=secospa.onmicrosoft.com header.i=@secospa.onmicrosoft.com header.b="pApJHCFR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8F63C82C61; Thu, 22 Jul 2021 20:39:23 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FORGED_SPF_HELO,MSGID_FROM_MTA_HEADER,SPF_HELO_PASS autolearn=no autolearn_force=no version=3.4.2 Received: from EUR02-VE1-obe.outbound.protection.outlook.com (mail-ve1eur02on0603.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe06::603]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 22EE182BDF for ; Thu, 22 Jul 2021 20:39:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=seco.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sean.anderson@seco.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VaI1u+Nd+gbuFi6SDBufmUKp8LyP78Qq6M6xeIkN1sj48Bi451kcjZuuYluikuAAc6ZX7yQ0EJ7Vj7sTd9F80spb9WFE7CHwASXRmsAoeiWmZ4OohtooG5PSd6xrZxO8kdLktaNcorhzUgW0mGbjg5n3Aoqp4f/WdGO4QcL4FKuAk1JVzG8hf299TtlM9xfC5rhH4TsWB2g4eWXyz3JY4XxhYWxZ8t9s6kJa/Ec/yXzzLIQ2LcnYuUfd5nIMtlPJXZiINN96k3uRyygYqfxjaYSmyYo2yxAqdyk8IlMqIadPr6BLhgJjU1/tNA1nudWiGvQyGNTwbmzhX3JubYA4/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xh32uOKR8MJmPq/1XwQ7x1cllhARkg9pJo2LC1yryNg=; b=FOlD5FPOvsTMtM2adANJgjMPgJDtD+nxOdxlfA/B63rxI1qmY8+77kXJJ2r3BsiyZK76mw5TtOr2YfLPcx7jDOr2UPrX06GjaQszKscnbX8YIEMh7GlvD8x8SmgND0PvPUqCuPx+PaR98INwqObLqbgGr1nJnb9Isma1uWg2GcAuDlD4S/L3N6zNyHqRcuur9MNKVviN5wXNBr3DX6GmIdZu5zEnDwH/Xj0m3Wya1hcOfL/FXruuVY7cIitMHBheBPHvaIyy/UhDON2HOIGPKyIAOPgHh7sS0WFzomZCTnEnXpwbzsqvvmBwZddRSYyLFs5eII6V9I2r2BWzFWFA9A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=seco.com; dmarc=pass action=none header.from=seco.com; dkim=pass header.d=seco.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secospa.onmicrosoft.com; s=selector2-secospa-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xh32uOKR8MJmPq/1XwQ7x1cllhARkg9pJo2LC1yryNg=; b=pApJHCFRNEalLaTTeu3TTJN28vsQK0jvtHyUVK854wlN/pHemqtd0FGXeb/1BsiuWyeQQ8IYmIPkMgVbL6oXYnAwl3huQaa6kA0f1n4oFj3wSm4NgBQrG5h184BYgMNwm4zvbMcuEWTndvh7PqQ3PMJ2rEbqDZfvKJjIFLh/o64= Authentication-Results: lists.denx.de; dkim=none (message not signed) header.d=none;lists.denx.de; dmarc=none action=none header.from=seco.com; Received: from DB7PR03MB4523.eurprd03.prod.outlook.com (2603:10a6:10:19::27) by DB7PR03MB4619.eurprd03.prod.outlook.com (2603:10a6:10:14::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.24; Thu, 22 Jul 2021 18:39:17 +0000 Received: from DB7PR03MB4523.eurprd03.prod.outlook.com ([fe80::dc6c:815b:2062:d1f1]) by DB7PR03MB4523.eurprd03.prod.outlook.com ([fe80::dc6c:815b:2062:d1f1%7]) with mapi id 15.20.4352.025; Thu, 22 Jul 2021 18:39:17 +0000 From: Sean Anderson To: u-boot@lists.denx.de, Lukasz Majewski Cc: Andre Przywara , Marek Szyprowski , Peng Fan , Bryan O'Donoghue , Marek Vasut , Jun Li , Maxime Ripard , Ye Li , Jagan Teki , Sean Anderson Subject: [PATCH v2 1/3] usb: f_mass_storage: Check bh->state in sleep_thread Date: Thu, 22 Jul 2021 14:38:08 -0400 Message-Id: <20210722183811.76441-1-sean.anderson@seco.com> X-Mailer: git-send-email 2.25.1 X-ClientProxiedBy: MN2PR01CA0047.prod.exchangelabs.com (2603:10b6:208:23f::16) To DB7PR03MB4523.eurprd03.prod.outlook.com (2603:10a6:10:19::27) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from plantagenet.inhand.com (50.195.82.171) by MN2PR01CA0047.prod.exchangelabs.com (2603:10b6:208:23f::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.26 via Frontend Transport; Thu, 22 Jul 2021 18:39:15 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: dd22e0be-1a8d-4cd9-dd5a-08d94d4002c5 X-MS-TrafficTypeDiagnostic: DB7PR03MB4619: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:291; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8XyK8et5z1yJgpWhowrwxjd9wZMIdTK5RmUsyVKbfnZQMwh9QL7GYlcDFldH6EJJcU1LK3TQFl71A3RYqkGBrkTha3uvmZRD8aP4eMU1TSIXCkVI4ncq/wPbN6dLnftLJGk6o6bIqazd8PYV+F4ben2PU9Tt/e9QmfLWiZMe0fjgYMIiCMlv7wCmD1XYNkhnY4qyg/8hFjFHcjP78tSYBBFIDFZcZR3gO0XBTJTh9Wm6S+WIHeqg6aKvkHq25KB9WqcClzpdbd5WnaCNrdom2rfroItFkAF0VAGPnTSDFsxBBrHOs8702nSS1ewwrIhTEmRC+2Bpj1ZgaJjNNldAIZiB3Zbb/PTeaOO4J6zujX04bIvjjyjzDKpDUY//q3s3SXc8ImRX9OJJGgINlxPX4MU3n+3Xgb8FOA8Qm7Yw5GJfoc/FCHNQQKLeAy63YdP+kPpbFAnt+MvSPC71iMEDPkzuIgr81c3SuY1a059uo67mS2o7ksqwPQ+M2890YHw6YHsssBGgTsw55e1p4N1BIt6va0fOl9+VuJDMw/UqyywgBLIvGVZuH62ahJ8hLLte416NcV6fmigbCwKIU1hb0vrp59Rd267+6C7v3hEMzmM6IBk59Lfer9surP5neWat4CXoRKMqVcGYLZhknzA+NUstr/PPX5eg63S/7zyq0DlQbs8+OZLleepodr4hVUfq4teXpNtITLJsP3n/MvlozA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB7PR03MB4523.eurprd03.prod.outlook.com; PTR:; CAT:NONE; SFS:(136003)(346002)(396003)(376002)(39830400003)(366004)(107886003)(2616005)(316002)(6506007)(66476007)(66946007)(86362001)(956004)(66556008)(478600001)(44832011)(83380400001)(186003)(26005)(52116002)(6486002)(38350700002)(8676002)(38100700002)(8936002)(6666004)(4326008)(5660300002)(30864003)(6512007)(7416002)(54906003)(2906002)(36756003)(1076003)(6916009); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ANAD/mEN/908BNa67g6aAAnNOniR2/T/D4NKE7RcaBu33uVVGefWJWmPgKXfFV2wx1/G9obZPmm9HNVYHpr9srLmeSq5mro6uPaffDHsunEkeLM1+q3YBJferHCucaytQyLzC3Ga+X2AT1w9LaqFjuU2wKW+UlbATjWyPimwhTCOI6BxoG8L9ukK4WvJ+h4BU1jvQ5D5CscO/GOs1tGTnCwGjflSuBNmAoWdfXMvXRw4CeIjeV2OnFt0iymUCuZziUjM4105GpCBd5QnYC9N7Dz7/mhrBslxpCIaR1xRrpM1O8Ry8VcCI2lLiI1oHfug7VElMuCgOCLj9/MUbT5xFfBli7idF6OwvOdiU2FCD6c+Z+diT5108DZIeRs9eMJz6aQ4Irw4cwJrA77ZQcCTx2KyFn9LTeJIhcO4+w4K/RiGt/lsWKGDwOfP+uMJsy9x8hVJLGyx8lyDZ8TJ1nFwyDzFKCyY3o9Cq/8jI/f2DpGidDDhBC1XHSq6nrauvN4rE4ntKuQn2SCPwPfrWkGvIZqP6N1iW14nqDzBG64ajOmsUcfSqWSwtTJ7OkwQ6BmXs4ZC9m5ANwQiQsxj0jZ5U9qRZgQllA2ibVtlrhzjr6LM4P2LUI+Z99/mqGSBtwBvfZ5CKpwOJx+GkJA8sVeN+6B2HWuMn7obQMjOqyjB67Y3lMNTFNzZXeaZmr7oxzvDRsgrSzlnrs2vCKXxiDkx+0jpqduwNtdxsKcwSZkgeRXldFp8kGXz/LbxxM0V5sokxa64QNVEiBya4UBnlXpyLsY3J1fcL9D2Y4nhBC5TdzxMv5dadKT2b/6M75mfpxYDmLL0pVDQOrgKJBJ/1knNR3xZcMMV6415FTMj+3spmU3iE/EL8bO81HIUI8pUJqwhJjY/StDTRk0K1STgOxO+u5C6yYXqCtwC5Cjp5dDoD2hjsqyj2KPE6UcWUQlK0hloWeVEku1guxf6HtZ+NNeRNDVbFxN/XkBGTeLIrdObVnIRDD7sswWM83wP61W/wruuVpfn2Pb4BU7TmPmCxgkk3zCAdA7+K0+phmdOdoLHwp29VhmxXxuugbXFulevG3nDpiAmMuv3kZ94+tnODR9ZDacr4l/38l500MS0ZsTKB+srGAJzD9x9x/APZnWdDq+YWIORQiK387Z8X+5uJ5rBSp3Wx9z26bf0hMHN7AjPJo4t5VjXI+moA3yo+EB0i88yU1QONBOtiELX9SBnywqSLtnsmQBJvMdgv3JF3mYBDAOjhHW+RyLHmDJFusp6ZMyAK5AeJl+8ZLmna9uJ6LG/pYHuCk8xCmjD9tCeA/YnF2mBZk4DDYT8msIo8Ag3AMOD X-OriginatorOrg: seco.com X-MS-Exchange-CrossTenant-Network-Message-Id: dd22e0be-1a8d-4cd9-dd5a-08d94d4002c5 X-MS-Exchange-CrossTenant-AuthSource: DB7PR03MB4523.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jul 2021 18:39:17.0622 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bebe97c3-6438-442e-ade3-ff17aa50e733 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: w0atc8DoVmHYfwlUTv5k3CnHZ1tbW9P6N6bgTN8z5JEDmJmpUWhPZzLMRBES1RiRav8vV0f7cX0Lv89VAl0YBA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB7PR03MB4619 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Every caller of sleep_thread except one wraps it in a second loop which waits for a buffer head to be filled. Since sleep_thread is already (infinitely) looping, we can move this check down. Some parts of the code have been reorganized to better take advantage of this property and and be structured closer to their Linux counterparts. In addition, sleep_thread now returns -EINTR if we need to wake up, mirroring Linux. This takes advantage of the existing logic regarding sleep_thread, which typically returns immediately on error. With this change, any exception causes the current operation to be backed out of immediately. Lastly, we only clear thread_wakeup_needed in handle_exception, mirroring Linux's signal-handling logic. Any subsequent calls to sleep_thread will still wake up. While these changes are conceptually different, their implementation is interdependent, so they are all performed at once. These changes are primarily inspired by Linux commit 225785aec726 ("USB: f_mass_storage: improve memory barriers and synchronization"). Signed-off-by: Sean Anderson --- Changes in v2: - Fix some checkpatch lint - Make comment for sleep thread match loop condition drivers/usb/gadget/f_mass_storage.c | 228 ++++++++++++---------------- drivers/usb/gadget/storage_common.c | 2 +- 2 files changed, 101 insertions(+), 129 deletions(-) diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 45f0504b6e..6f39c24503 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -652,15 +652,17 @@ static void busy_indicator(void) state = 0; } -static int sleep_thread(struct fsg_common *common) +static int sleep_thread(struct fsg_common *common, struct fsg_buffhd *bh) { - int rc = 0; int i = 0, k = 0; - /* Wait until a signal arrives or we are woken up */ - for (;;) { - if (common->thread_wakeup_needed) - break; + /* + * Wait until a signal arrives, we are woken up, or the buffer is no + * longer busy. + */ + while (!bh || bh->state == BUF_STATE_BUSY) { + if (exception_in_progress(common)) + return -EINTR; if (++i == 20000) { busy_indicator(); @@ -682,8 +684,7 @@ static int sleep_thread(struct fsg_common *common) usb_gadget_handle_interrupts(controller_index); } - common->thread_wakeup_needed = 0; - return rc; + return 0; } /*-------------------------------------------------------------------------*/ @@ -744,11 +745,9 @@ static int do_read(struct fsg_common *common) /* Wait for the next buffer to become available */ bh = common->next_buffhd_to_fill; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; /* If we were asked to read past the end of file, * end with an empty buffer. */ @@ -922,67 +921,63 @@ static int do_write(struct fsg_common *common) bh = common->next_buffhd_to_drain; if (bh->state == BUF_STATE_EMPTY && !get_some_more) break; /* We stopped early */ - if (bh->state == BUF_STATE_FULL) { - common->next_buffhd_to_drain = bh->next; - bh->state = BUF_STATE_EMPTY; - /* Did something go wrong with the transfer? */ - if (bh->outreq->status != 0) { - curlun->sense_data = SS_COMMUNICATION_FAILURE; - curlun->info_valid = 1; - break; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; - amount = bh->outreq->actual; + common->next_buffhd_to_drain = bh->next; + bh->state = BUF_STATE_EMPTY; - /* Perform the write */ - rc = ums[common->lun].write_sector(&ums[common->lun], - file_offset / SECTOR_SIZE, - amount / SECTOR_SIZE, - (char __user *)bh->buf); - if (!rc) - return -EIO; - nwritten = rc * SECTOR_SIZE; - - VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, - (unsigned long long) file_offset, - (int) nwritten); - - if (nwritten < 0) { - LDBG(curlun, "error in file write: %d\n", - (int) nwritten); - nwritten = 0; - } else if (nwritten < amount) { - LDBG(curlun, "partial file write: %d/%u\n", - (int) nwritten, amount); - nwritten -= (nwritten & 511); - /* Round down to a block */ - } - file_offset += nwritten; - amount_left_to_write -= nwritten; - common->residue -= nwritten; - - /* If an error occurred, report it and its position */ - if (nwritten < amount) { - printf("nwritten:%zd amount:%u\n", nwritten, - amount); - curlun->sense_data = SS_WRITE_ERROR; - curlun->info_valid = 1; - break; - } + /* Did something go wrong with the transfer? */ + if (bh->outreq->status != 0) { + curlun->sense_data = SS_COMMUNICATION_FAILURE; + curlun->info_valid = 1; + break; + } - /* Did the host decide to stop early? */ - if (bh->outreq->actual != bh->outreq->length) { - common->short_packet_received = 1; - break; - } - continue; + amount = bh->outreq->actual; + + /* Perform the write */ + rc = ums[common->lun].write_sector(&ums[common->lun], + file_offset / SECTOR_SIZE, + amount / SECTOR_SIZE, + (char __user *)bh->buf); + if (!rc) + return -EIO; + nwritten = rc * SECTOR_SIZE; + + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount, + (unsigned long long)file_offset, (int)nwritten); + + if (nwritten < 0) { + LDBG(curlun, "error in file write: %d\n", + (int)nwritten); + nwritten = 0; + } else if (nwritten < amount) { + LDBG(curlun, "partial file write: %d/%u\n", + (int)nwritten, amount); + nwritten -= (nwritten & 511); + /* Round down to a block */ } + file_offset += nwritten; + amount_left_to_write -= nwritten; + common->residue -= nwritten; - /* Wait for something to happen */ - rc = sleep_thread(common); - if (rc) - return rc; + /* If an error occurred, report it and its position */ + if (nwritten < amount) { + printf("nwritten:%zd amount:%u\n", nwritten, + amount); + curlun->sense_data = SS_WRITE_ERROR; + curlun->info_valid = 1; + break; + } + + /* Did the host decide to stop early? */ + if (bh->outreq->actual != bh->outreq->length) { + common->short_packet_received = 1; + break; + } } return -EIO; /* No default reply */ @@ -1430,13 +1425,10 @@ static int pad_with_zeros(struct fsg_dev *fsg) bh->state = BUF_STATE_EMPTY; /* For the first iteration */ fsg->common->usb_amount_left = nkeep + fsg->common->residue; while (fsg->common->usb_amount_left > 0) { - /* Wait for the next buffer to be free */ - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(fsg->common); - if (rc) - return rc; - } + rc = sleep_thread(fsg->common, bh); + if (rc) + return rc; nsend = min(fsg->common->usb_amount_left, FSG_BUFLEN); memset(bh->buf + nkeep, 0, nsend - nkeep); @@ -1461,23 +1453,7 @@ static int throw_away_data(struct fsg_common *common) bh->state != BUF_STATE_EMPTY || common->usb_amount_left > 0; bh = common->next_buffhd_to_drain) { - /* Throw away the data in a filled buffer */ - if (bh->state == BUF_STATE_FULL) { - bh->state = BUF_STATE_EMPTY; - common->next_buffhd_to_drain = bh->next; - - /* A short packet or an error ends everything */ - if (bh->outreq->actual != bh->outreq->length || - bh->outreq->status != 0) { - raise_exception(common, - FSG_STATE_ABORT_BULK_OUT); - return -EINTR; - } - continue; - } - /* Try to submit another request if we need one */ - bh = common->next_buffhd_to_fill; if (bh->state == BUF_STATE_EMPTY && common->usb_amount_left > 0) { amount = min(common->usb_amount_left, FSG_BUFLEN); @@ -1494,13 +1470,24 @@ static int throw_away_data(struct fsg_common *common) return -EIO; common->next_buffhd_to_fill = bh->next; common->usb_amount_left -= amount; - continue; } - /* Otherwise wait for something to happen */ - rc = sleep_thread(common); + /* Wait for the data to be received */ + rc = sleep_thread(common, bh); if (rc) return rc; + + /* Throw away the data in a filled buffer */ + bh->state = BUF_STATE_EMPTY; + common->next_buffhd_to_drain = bh->next; + + /* A short packet or an error ends everything */ + if (bh->outreq->actual != bh->outreq->length || + bh->outreq->status != 0) { + raise_exception(common, + FSG_STATE_ABORT_BULK_OUT); + return -EINTR; + } } return 0; } @@ -1613,11 +1600,9 @@ static int send_status(struct fsg_common *common) /* Wait for the next buffer to become available */ bh = common->next_buffhd_to_fill; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; if (curlun) sd = curlun->sense_data; @@ -1790,11 +1775,9 @@ static int do_scsi_command(struct fsg_common *common) /* Wait for the next buffer to become available for data or status */ bh = common->next_buffhd_to_fill; common->next_buffhd_to_drain = bh; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; common->phase_error = 0; common->short_packet_received = 0; @@ -2118,11 +2101,9 @@ static int get_next_command(struct fsg_common *common) /* Wait for the next buffer to become available */ bh = common->next_buffhd_to_fill; - while (bh->state != BUF_STATE_EMPTY) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; /* Queue a request to read a Bulk-only CBW */ set_bulk_out_req_length(common, bh, USB_BULK_CB_WRAP_LEN); @@ -2137,11 +2118,9 @@ static int get_next_command(struct fsg_common *common) * next_buffhd_to_fill. */ /* Wait for the CBW to arrive */ - while (bh->state != BUF_STATE_FULL) { - rc = sleep_thread(common); - if (rc) - return rc; - } + rc = sleep_thread(common, bh); + if (rc) + return rc; rc = fsg_is_set(common) ? received_cbw(common->fsg, bh) : -EIO; bh->state = BUF_STATE_EMPTY; @@ -2291,6 +2270,7 @@ static void handle_exception(struct fsg_common *common) struct fsg_lun *curlun; unsigned int exception_req_tag; + common->thread_wakeup_needed = 0; /* Cancel all the pending transfers */ if (common->fsg) { for (i = 0; i < FSG_NUM_BUFFERS; ++i) { @@ -2300,18 +2280,9 @@ static void handle_exception(struct fsg_common *common) if (bh->outreq_busy) usb_ep_dequeue(common->fsg->bulk_out, bh->outreq); - } - /* Wait until everything is idle */ - for (;;) { - int num_active = 0; - for (i = 0; i < FSG_NUM_BUFFERS; ++i) { - bh = &common->buffhds[i]; - num_active += bh->inreq_busy + bh->outreq_busy; - } - if (num_active == 0) - break; - if (sleep_thread(common)) + /* Wait until the transfer is idle */ + if (sleep_thread(common, bh)) return; } @@ -2403,15 +2374,16 @@ int fsg_main_thread(void *common_) } if (!common->running) { - ret = sleep_thread(common); - if (ret) + ret = sleep_thread(common, NULL); + if (ret != -EINTR) return ret; - continue; } ret = get_next_command(common); - if (ret) + if (ret == -EINTR) + continue; + else if (ret) return ret; if (!exception_in_progress(common)) diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c index 5674e8fe49..723549ef09 100644 --- a/drivers/usb/gadget/storage_common.c +++ b/drivers/usb/gadget/storage_common.c @@ -318,7 +318,7 @@ static struct fsg_lun *fsg_lun_from_dev(struct device *dev) enum fsg_buffer_state { BUF_STATE_EMPTY = 0, BUF_STATE_FULL, - BUF_STATE_BUSY + BUF_STATE_BUSY, }; struct fsg_buffhd {