···3737 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.3838 */39394040-4140/*4241 * The Mass Storage Function acts as a USB Mass Storage device,4342 * appearing to the host as a disk drive or as a CD-ROM drive. In···184185 * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.185186 */186187187187-188188/*189189 * Driver Design190190 *···273275/* #define VERBOSE_DEBUG */274276/* #define DUMP_MSGS */275277276276-277278#include <linux/blkdev.h>278279#include <linux/completion.h>279280#include <linux/dcache.h>···297300#include "gadget_chips.h"298301299302300300-301303/*------------------------------------------------------------------------*/302304303305#define FSG_DRIVER_DESC "Mass Storage Function"304306#define FSG_DRIVER_VERSION "2009/09/11"305307306308static const char fsg_string_interface[] = "Mass Storage";307307-308309309310#define FSG_NO_INTR_EP 1310311#define FSG_NO_DEVICE_STRINGS 1···319324320325/* FSF callback functions */321326struct fsg_operations {322322- /* Callback function to call when thread exits. If no327327+ /*328328+ * Callback function to call when thread exits. If no323329 * callback is set or it returns value lower then zero MSF324330 * will force eject all LUNs it operates on (including those325331 * marked as non-removable or with prevent_medium_removal flag326326- * set). */332332+ * set).333333+ */327334 int (*thread_exits)(struct fsg_common *common);328335329329- /* Called prior to ejection. Negative return means error,336336+ /*337337+ * Called prior to ejection. Negative return means error,330338 * zero means to continue with ejection, positive means not to331331- * eject. */339339+ * eject.340340+ */332341 int (*pre_eject)(struct fsg_common *common,333342 struct fsg_lun *lun, int num);334334- /* Called after ejection. Negative return means error, zero335335- * or positive is just a success. */343343+ /*344344+ * Called after ejection. Negative return means error, zero345345+ * or positive is just a success.346346+ */336347 int (*post_eject)(struct fsg_common *common,337348 struct fsg_lun *lun, int num);338349};339339-340350341351/* Data shared by all the FSG instances. */342352struct fsg_common {···398398 /* Gadget's private data. */399399 void *private_data;400400401401- /* Vendor (8 chars), product (16 chars), release (4402402- * hexadecimal digits) and NUL byte */401401+ /*402402+ * Vendor (8 chars), product (16 chars), release (4403403+ * hexadecimal digits) and NUL byte404404+ */403405 char inquiry_string[8 + 16 + 4 + 1];404406405407 struct kref ref;406408};407407-408409409410struct fsg_config {410411 unsigned nluns;···432431 char can_stall;433432};434433435435-436434struct fsg_dev {437435 struct usb_function function;438436 struct usb_gadget *gadget; /* Copy of cdev->gadget */···449449 struct usb_ep *bulk_out;450450};451451452452-453452static inline int __fsg_is_set(struct fsg_common *common,454453 const char *func, unsigned line)455454{···461462462463#define fsg_is_set(common) likely(__fsg_is_set(common, __func__, __LINE__))463464464464-465465static inline struct fsg_dev *fsg_from_func(struct usb_function *f)466466{467467 return container_of(f, struct fsg_dev, function);468468}469469-470469471470typedef void (*fsg_routine_t)(struct fsg_dev *);472471···475478476479/* Make bulk-out requests be divisible by the maxpacket size */477480static void set_bulk_out_req_length(struct fsg_common *common,478478- struct fsg_buffhd *bh, unsigned int length)481481+ struct fsg_buffhd *bh, unsigned int length)479482{480483 unsigned int rem;481484···485488 length += common->bulk_out_maxpacket - rem;486489 bh->outreq->length = length;487490}491491+488492489493/*-------------------------------------------------------------------------*/490494···517519 wake_up_process(common->thread_task);518520}519521520520-521522static void raise_exception(struct fsg_common *common, enum fsg_state new_state)522523{523524 unsigned long flags;524525525525- /* Do nothing if a higher-priority exception is already in progress.526526+ /*527527+ * Do nothing if a higher-priority exception is already in progress.526528 * If a lower-or-equal priority exception is in progress, preempt it527527- * and notify the main thread by sending it a signal. */529529+ * and notify the main thread by sending it a signal.530530+ */528531 spin_lock_irqsave(&common->lock, flags);529532 if (common->state <= new_state) {530533 common->exception_req_tag = common->ep0_req_tag;···554555 return rc;555556}556557558558+557559/*-------------------------------------------------------------------------*/558560559559-/* Bulk and interrupt endpoint completion handlers.560560- * These always run in_irq. */561561+/* Completion handlers. These always run in_irq. */561562562563static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)563564{···566567567568 if (req->status || req->actual != req->length)568569 DBG(common, "%s --> %d, %u/%u\n", __func__,569569- req->status, req->actual, req->length);570570+ req->status, req->actual, req->length);570571 if (req->status == -ECONNRESET) /* Request was cancelled */571572 usb_ep_fifo_flush(ep);572573···587588 dump_msg(common, "bulk-out", req->buf, req->actual);588589 if (req->status || req->actual != bh->bulk_out_intended_length)589590 DBG(common, "%s --> %d, %u/%u\n", __func__,590590- req->status, req->actual,591591- bh->bulk_out_intended_length);591591+ req->status, req->actual, bh->bulk_out_intended_length);592592 if (req->status == -ECONNRESET) /* Request was cancelled */593593 usb_ep_fifo_flush(ep);594594···600602 spin_unlock(&common->lock);601603}602604603603-604604-/*-------------------------------------------------------------------------*/605605-606606-/* Ep0 class-specific handlers. These always run in_irq. */607607-608605static int fsg_setup(struct usb_function *f,609609- const struct usb_ctrlrequest *ctrl)606606+ const struct usb_ctrlrequest *ctrl)610607{611608 struct fsg_dev *fsg = fsg_from_func(f);612609 struct usb_request *req = fsg->common->ep0req;···621628 if (w_index != fsg->interface_number || w_value != 0)622629 return -EDOM;623630624624- /* Raise an exception to stop the current operation625625- * and reinitialize our state. */631631+ /*632632+ * Raise an exception to stop the current operation633633+ * and reinitialize our state.634634+ */626635 DBG(fsg, "bulk reset request\n");627636 raise_exception(fsg->common, FSG_STATE_RESET);628637 return DELAYED_STATUS;···636641 if (w_index != fsg->interface_number || w_value != 0)637642 return -EDOM;638643 VDBG(fsg, "get max LUN\n");639639- *(u8 *) req->buf = fsg->common->nluns - 1;644644+ *(u8 *)req->buf = fsg->common->nluns - 1;640645641646 /* Respond with data/status */642647 req->length = min((u16)1, w_length);···644649 }645650646651 VDBG(fsg,647647- "unknown class-specific control req "648648- "%02x.%02x v%04x i%04x l%u\n",652652+ "unknown class-specific control req %02x.%02x v%04x i%04x l%u\n",649653 ctrl->bRequestType, ctrl->bRequest,650654 le16_to_cpu(ctrl->wValue), w_index, w_length);651655 return -EOPNOTSUPP;···655661656662/* All the following routines run in process context */657663658658-659664/* Use this for bulk or interrupt transfers, not ep0 */660665static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,661661- struct usb_request *req, int *pbusy,662662- enum fsg_buffer_state *state)666666+ struct usb_request *req, int *pbusy,667667+ enum fsg_buffer_state *state)663668{664669 int rc;665670···676683677684 /* We can't do much more than wait for a reset */678685679679- /* Note: currently the net2280 driver fails zero-length680680- * submissions if DMA is enabled. */681681- if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&682682- req->length == 0))686686+ /*687687+ * Note: currently the net2280 driver fails zero-length688688+ * submissions if DMA is enabled.689689+ */690690+ if (rc != -ESHUTDOWN &&691691+ !(rc == -EOPNOTSUPP && req->length == 0))683692 WARNING(fsg, "error in submission: %s --> %d\n",684684- ep->name, rc);693693+ ep->name, rc);685694 }686695}687696···741746 unsigned int partial_page;742747 ssize_t nread;743748744744- /* Get the starting Logical Block Address and check that it's745745- * not too big */749749+ /*750750+ * Get the starting Logical Block Address and check that it's751751+ * not too big.752752+ */746753 if (common->cmnd[0] == READ_6)747754 lba = get_unaligned_be24(&common->cmnd[1]);748755 else {749756 lba = get_unaligned_be32(&common->cmnd[2]);750757751751- /* We allow DPO (Disable Page Out = don't save data in the758758+ /*759759+ * We allow DPO (Disable Page Out = don't save data in the752760 * cache) and FUA (Force Unit Access = don't read from the753753- * cache), but we don't implement them. */761761+ * cache), but we don't implement them.762762+ */754763 if ((common->cmnd[1] & ~0x18) != 0) {755764 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;756765 return -EINVAL;···772773 return -EIO; /* No default reply */773774774775 for (;;) {775775-776776- /* Figure out how much we need to read:776776+ /*777777+ * Figure out how much we need to read:777778 * Try to read the remaining amount.778779 * But don't read more than the buffer size.779780 * And don't try to read past the end of the file.780781 * Finally, if we're not at a page boundary, don't read past781782 * the next page.782783 * If this means reading 0 then we were asked to read past783783- * the end of file. */784784+ * the end of file.785785+ */784786 amount = min(amount_left, FSG_BUFLEN);785785- amount = min((loff_t) amount,786786- curlun->file_length - file_offset);787787+ amount = min((loff_t)amount,788788+ curlun->file_length - file_offset);787789 partial_page = file_offset & (PAGE_CACHE_SIZE - 1);788790 if (partial_page > 0)789789- amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -790790- partial_page);791791+ amount = min(amount, (unsigned int)PAGE_CACHE_SIZE -792792+ partial_page);791793792794 /* Wait for the next buffer to become available */793795 bh = common->next_buffhd_to_fill;···798798 return rc;799799 }800800801801- /* If we were asked to read past the end of file,802802- * end with an empty buffer. */801801+ /*802802+ * If we were asked to read past the end of file,803803+ * end with an empty buffer.804804+ */803805 if (amount == 0) {804806 curlun->sense_data =805807 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;···815813 /* Perform the read */816814 file_offset_tmp = file_offset;817815 nread = vfs_read(curlun->filp,818818- (char __user *) bh->buf,819819- amount, &file_offset_tmp);816816+ (char __user *)bh->buf,817817+ amount, &file_offset_tmp);820818 VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,821821- (unsigned long long) file_offset,822822- (int) nread);819819+ (unsigned long long)file_offset, (int)nread);823820 if (signal_pending(current))824821 return -EINTR;825822826823 if (nread < 0) {827827- LDBG(curlun, "error in file read: %d\n",828828- (int) nread);824824+ LDBG(curlun, "error in file read: %d\n", (int)nread);829825 nread = 0;830826 } else if (nread < amount) {831827 LDBG(curlun, "partial file read: %d/%u\n",832832- (int) nread, amount);828828+ (int)nread, amount);833829 nread -= (nread & 511); /* Round down to a block */834830 }835831 file_offset += nread;···882882 curlun->filp->f_flags &= ~O_SYNC; /* Default is not to wait */883883 spin_unlock(&curlun->filp->f_lock);884884885885- /* Get the starting Logical Block Address and check that it's886886- * not too big */885885+ /*886886+ * Get the starting Logical Block Address and check that it's887887+ * not too big888888+ */887889 if (common->cmnd[0] == WRITE_6)888890 lba = get_unaligned_be24(&common->cmnd[1]);889891 else {890892 lba = get_unaligned_be32(&common->cmnd[2]);891893892892- /* We allow DPO (Disable Page Out = don't save data in the894894+ /*895895+ * We allow DPO (Disable Page Out = don't save data in the893896 * cache) and FUA (Force Unit Access = write directly to the894897 * medium). We don't implement DPO; we implement FUA by895895- * performing synchronous output. */898898+ * performing synchronous output.899899+ */896900 if (common->cmnd[1] & ~0x18) {897901 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;898902 return -EINVAL;···924920 bh = common->next_buffhd_to_fill;925921 if (bh->state == BUF_STATE_EMPTY && get_some_more) {926922927927- /* Figure out how much we want to get:923923+ /*924924+ * Figure out how much we want to get:928925 * Try to get the remaining amount.929926 * But don't get more than the buffer size.930927 * And don't try to go past the end of the file.···933928 * don't go past the next page.934929 * If this means getting 0, then we were asked935930 * to write past the end of file.936936- * Finally, round down to a block boundary. */931931+ * Finally, round down to a block boundary.932932+ */937933 amount = min(amount_left_to_req, FSG_BUFLEN);938938- amount = min((loff_t) amount, curlun->file_length -939939- usb_offset);934934+ amount = min((loff_t)amount,935935+ curlun->file_length - usb_offset);940936 partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);941937 if (partial_page > 0)942938 amount = min(amount,943943- (unsigned int) PAGE_CACHE_SIZE - partial_page);939939+ (unsigned int)PAGE_CACHE_SIZE - partial_page);944940945941 if (amount == 0) {946942 get_some_more = 0;···951945 curlun->info_valid = 1;952946 continue;953947 }954954- amount -= (amount & 511);948948+ amount -= amount & 511;955949 if (amount == 0) {956950957957- /* Why were we were asked to transfer a958958- * partial block? */951951+ /*952952+ * Why were we were asked to transfer a953953+ * partial block?954954+ */959955 get_some_more = 0;960956 continue;961957 }···969961 if (amount_left_to_req == 0)970962 get_some_more = 0;971963972972- /* amount is always divisible by 512, hence by973973- * the bulk-out maxpacket size */964964+ /*965965+ * amount is always divisible by 512, hence by966966+ * the bulk-out maxpacket size967967+ */974968 bh->outreq->length = amount;975969 bh->bulk_out_intended_length = amount;976970 bh->outreq->short_not_ok = 1;977971 if (!start_out_transfer(common, bh))978978- /* Don't know what to do if979979- * common->fsg is NULL */972972+ /* Dunno what to do if common->fsg is NULL */980973 return -EIO;981974 common->next_buffhd_to_fill = bh->next;982975 continue;···1003994 amount = bh->outreq->actual;1004995 if (curlun->file_length - file_offset < amount) {1005996 LERROR(curlun,10061006- "write %u @ %llu beyond end %llu\n",10071007- amount, (unsigned long long) file_offset,10081008- (unsigned long long) curlun->file_length);997997+ "write %u @ %llu beyond end %llu\n",998998+ amount, (unsigned long long)file_offset,999999+ (unsigned long long)curlun->file_length);10091000 amount = curlun->file_length - file_offset;10101001 }1011100210121003 /* Perform the write */10131004 file_offset_tmp = file_offset;10141005 nwritten = vfs_write(curlun->filp,10151015- (char __user *) bh->buf,10161016- amount, &file_offset_tmp);10061006+ (char __user *)bh->buf,10071007+ amount, &file_offset_tmp);10171008 VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,10181018- (unsigned long long) file_offset,10191019- (int) nwritten);10091009+ (unsigned long long)file_offset, (int)nwritten);10201010 if (signal_pending(current))10211011 return -EINTR; /* Interrupted! */1022101210231013 if (nwritten < 0) {10241014 LDBG(curlun, "error in file write: %d\n",10251025- (int) nwritten);10151015+ (int)nwritten);10261016 nwritten = 0;10271017 } else if (nwritten < amount) {10281018 LDBG(curlun, "partial file write: %d/%u\n",10291029- (int) nwritten, amount);10191019+ (int)nwritten, amount);10301020 nwritten -= (nwritten & 511);10311021 /* Round down to a block */10321022 }···10981090 unsigned int amount;10991091 ssize_t nread;1100109211011101- /* Get the starting Logical Block Address and check that it's11021102- * not too big */10931093+ /*10941094+ * Get the starting Logical Block Address and check that it's10951095+ * not too big.10961096+ */11031097 lba = get_unaligned_be32(&common->cmnd[2]);11041098 if (lba >= curlun->num_sectors) {11051099 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;11061100 return -EINVAL;11071101 }1108110211091109- /* We allow DPO (Disable Page Out = don't save data in the11101110- * cache) but we don't implement it. */11031103+ /*11041104+ * We allow DPO (Disable Page Out = don't save data in the11051105+ * cache) but we don't implement it.11061106+ */11111107 if (common->cmnd[1] & ~0x10) {11121108 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;11131109 return -EINVAL;···1136112411371125 /* Just try to read the requested blocks */11381126 while (amount_left > 0) {11391139-11401140- /* Figure out how much we need to read:11271127+ /*11281128+ * Figure out how much we need to read:11411129 * Try to read the remaining amount, but not more than11421130 * the buffer size.11431131 * And don't try to read past the end of the file.11441132 * If this means reading 0 then we were asked to read11451145- * past the end of file. */11331133+ * past the end of file.11341134+ */11461135 amount = min(amount_left, FSG_BUFLEN);11471147- amount = min((loff_t) amount,11481148- curlun->file_length - file_offset);11361136+ amount = min((loff_t)amount,11371137+ curlun->file_length - file_offset);11491138 if (amount == 0) {11501139 curlun->sense_data =11511140 SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;···11671154 return -EINTR;1168115511691156 if (nread < 0) {11701170- LDBG(curlun, "error in file verify: %d\n",11711171- (int) nread);11571157+ LDBG(curlun, "error in file verify: %d\n", (int)nread);11721158 nread = 0;11731159 } else if (nread < amount) {11741160 LDBG(curlun, "partial file verify: %d/%u\n",11751175- (int) nread, amount);11761176- nread -= (nread & 511); /* Round down to a sector */11611161+ (int)nread, amount);11621162+ nread -= nread & 511; /* Round down to a sector */11771163 }11781164 if (nread == 0) {11791165 curlun->sense_data = SS_UNRECOVERED_READ_ERROR;···12131201 memcpy(buf + 8, common->inquiry_string, sizeof common->inquiry_string);12141202 return 36;12151203}12161216-1217120412181205static int do_request_sense(struct fsg_common *common, struct fsg_buffhd *bh)12191206{···12671256 return 18;12681257}1269125812701270-12711259static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)12721260{12731261 struct fsg_lun *curlun = common->curlun;12741262 u32 lba = get_unaligned_be32(&common->cmnd[2]);12751263 int pmi = common->cmnd[8];12761276- u8 *buf = (u8 *) bh->buf;12641264+ u8 *buf = (u8 *)bh->buf;1277126512781266 /* Check the PMI and LBA fields */12791267 if (pmi > 1 || (pmi == 0 && lba != 0)) {···12861276 return 8;12871277}1288127812891289-12901279static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)12911280{12921281 struct fsg_lun *curlun = common->curlun;12931282 int msf = common->cmnd[1] & 0x02;12941283 u32 lba = get_unaligned_be32(&common->cmnd[2]);12951295- u8 *buf = (u8 *) bh->buf;12841284+ u8 *buf = (u8 *)bh->buf;1296128512971286 if (common->cmnd[1] & ~0x02) { /* Mask away MSF */12981287 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;···13081299 return 8;13091300}1310130113111311-13121302static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh)13131303{13141304 struct fsg_lun *curlun = common->curlun;13151305 int msf = common->cmnd[1] & 0x02;13161306 int start_track = common->cmnd[6];13171317- u8 *buf = (u8 *) bh->buf;13071307+ u8 *buf = (u8 *)bh->buf;1318130813191309 if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */13201310 start_track > 1) {···13341326 store_cdrom_address(&buf[16], msf, curlun->num_sectors);13351327 return 20;13361328}13371337-1338132913391330static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)13401331{···13591352 changeable_values = (pc == 1);13601353 all_pages = (page_code == 0x3f);1361135413621362- /* Write the mode parameter header. Fixed values are: default13551355+ /*13561356+ * Write the mode parameter header. Fixed values are: default13631357 * medium type, no cache control (DPOFUA), and no block descriptors.13641358 * The only variable value is the WriteProtect bit. We will fill in13651365- * the mode data length later. */13591359+ * the mode data length later.13601360+ */13661361 memset(buf, 0, 8);13671362 if (mscmnd == MODE_SENSE) {13681363 buf[2] = (curlun->ro ? 0x80 : 0x00); /* WP, DPOFUA */···1378136913791370 /* No block descriptors */1380137113811381- /* The mode pages, in numerical order. The only page we support13821382- * is the Caching page. */13721372+ /*13731373+ * The mode pages, in numerical order. The only page we support13741374+ * is the Caching page.13751375+ */13831376 if (page_code == 0x08 || all_pages) {13841377 valid_page = 1;13851378 buf[0] = 0x08; /* Page code */···14031392 buf += 12;14041393 }1405139414061406- /* Check that a valid page was requested and the mode data length14071407- * isn't too long. */13951395+ /*13961396+ * Check that a valid page was requested and the mode data length13971397+ * isn't too long.13981398+ */14081399 len = buf - buf0;14091400 if (!valid_page || len > limit) {14101401 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;···14201407 put_unaligned_be16(len - 2, buf0);14211408 return len;14221409}14231423-1424141014251411static int do_start_stop(struct fsg_common *common)14261412{···14401428 loej = common->cmnd[4] & 0x02;14411429 start = common->cmnd[4] & 0x01;1442143014431443- /* Our emulation doesn't support mounting; the medium is14441444- * available for use as soon as it is loaded. */14311431+ /*14321432+ * Our emulation doesn't support mounting; the medium is14331433+ * available for use as soon as it is loaded.14341434+ */14451435 if (start) {14461436 if (!fsg_lun_is_open(curlun)) {14471437 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;···14841470 : 0;14851471}1486147214871487-14881473static int do_prevent_allow(struct fsg_common *common)14891474{14901475 struct fsg_lun *curlun = common->curlun;···15081495 return 0;15091496}1510149715111511-15121498static int do_read_format_capacities(struct fsg_common *common,15131499 struct fsg_buffhd *bh)15141500{···15241512 buf[4] = 0x02; /* Current capacity */15251513 return 12;15261514}15271527-1528151515291516static int do_mode_select(struct fsg_common *common, struct fsg_buffhd *bh)15301517{···16061595 bh->inreq->length = nsend;16071596 bh->inreq->zero = 0;16081597 start_transfer(fsg, fsg->bulk_in, bh->inreq,16091609- &bh->inreq_busy, &bh->state);15981598+ &bh->inreq_busy, &bh->state);16101599 bh = fsg->common->next_buffhd_to_fill = bh->next;16111600 fsg->common->usb_amount_left -= nsend;16121601 nkeep = 0;···1632162116331622 /* A short packet or an error ends everything */16341623 if (bh->outreq->actual != bh->outreq->length ||16351635- bh->outreq->status != 0) {16241624+ bh->outreq->status != 0) {16361625 raise_exception(common,16371626 FSG_STATE_ABORT_BULK_OUT);16381627 return -EINTR;···16461635 && common->usb_amount_left > 0) {16471636 amount = min(common->usb_amount_left, FSG_BUFLEN);1648163716491649- /* amount is always divisible by 512, hence by16501650- * the bulk-out maxpacket size */16381638+ /*16391639+ * amount is always divisible by 512, hence by16401640+ * the bulk-out maxpacket size.16411641+ */16511642 bh->outreq->length = amount;16521643 bh->bulk_out_intended_length = amount;16531644 bh->outreq->short_not_ok = 1;16541645 if (!start_out_transfer(common, bh))16551655- /* Don't know what to do if16561656- * common->fsg is NULL */16461646+ /* Dunno what to do if common->fsg is NULL */16571647 return -EIO;16581648 common->next_buffhd_to_fill = bh->next;16591649 common->usb_amount_left -= amount;···16691657 return 0;16701658}1671165916721672-16731660static int finish_reply(struct fsg_common *common)16741661{16751662 struct fsg_buffhd *bh = common->next_buffhd_to_fill;···16781667 case DATA_DIR_NONE:16791668 break; /* Nothing to send */1680166916811681- /* If we don't know whether the host wants to read or write,16701670+ /*16711671+ * If we don't know whether the host wants to read or write,16821672 * this must be CB or CBI with an unknown command. We mustn't16831673 * try to send or receive any data. So stall both bulk pipes16841684- * if we can and wait for a reset. */16741674+ * if we can and wait for a reset.16751675+ */16851676 case DATA_DIR_UNKNOWN:16861677 if (!common->can_stall) {16871678 /* Nothing */···17081695 return -EIO;17091696 common->next_buffhd_to_fill = bh->next;1710169717111711- /* For Bulk-only, if we're allowed to stall then send the16981698+ /*16991699+ * For Bulk-only, if we're allowed to stall then send the17121700 * short packet and halt the bulk-in endpoint. If we can't17131713- * stall, pad out the remaining data with 0's. */17011701+ * stall, pad out the remaining data with 0's.17021702+ */17141703 } else if (common->can_stall) {17151704 bh->inreq->zero = 1;17161705 if (!start_in_transfer(common, bh))···17301715 }17311716 break;1732171717331733- /* We have processed all we want from the data the host has sent.17341734- * There may still be outstanding bulk-out requests. */17181718+ /*17191719+ * We have processed all we want from the data the host has sent.17201720+ * There may still be outstanding bulk-out requests.17211721+ */17351722 case DATA_DIR_FROM_HOST:17361723 if (common->residue == 0) {17371724 /* Nothing to receive */···17431726 raise_exception(common, FSG_STATE_ABORT_BULK_OUT);17441727 rc = -EINTR;1745172817461746- /* We haven't processed all the incoming data. Even though17291729+ /*17301730+ * We haven't processed all the incoming data. Even though17471731 * we may be allowed to stall, doing so would cause a race.17481732 * The controller may already have ACK'ed all the remaining17491733 * bulk-out packets, in which case the host wouldn't see a17501734 * STALL. Not realizing the endpoint was halted, it wouldn't17511751- * clear the halt -- leading to problems later on. */17351735+ * clear the halt -- leading to problems later on.17361736+ */17521737#if 017531738 } else if (common->can_stall) {17541739 if (fsg_is_set(common))···17601741 rc = -EINTR;17611742#endif1762174317631763- /* We can't stall. Read in the excess data and throw it17641764- * all away. */17441744+ /*17451745+ * We can't stall. Read in the excess data and throw it17461746+ * all away.17471747+ */17651748 } else {17661749 rc = throw_away_data(common);17671750 }···17711750 }17721751 return rc;17731752}17741774-1775175317761754static int send_status(struct fsg_common *common)17771755{···1830181018311811/*-------------------------------------------------------------------------*/1832181218331833-/* Check whether the command is properly formed and whether its data size18341834- * and direction agree with the values we already have. */18131813+/*18141814+ * Check whether the command is properly formed and whether its data size18151815+ * and direction agree with the values we already have.18161816+ */18351817static int check_command(struct fsg_common *common, int cmnd_size,18361836- enum data_direction data_dir, unsigned int mask,18371837- int needs_medium, const char *name)18181818+ enum data_direction data_dir, unsigned int mask,18191819+ int needs_medium, const char *name)18381820{18391821 int i;18401822 int lun = common->cmnd[1] >> 5;···18471825 hdlen[0] = 0;18481826 if (common->data_dir != DATA_DIR_UNKNOWN)18491827 sprintf(hdlen, ", H%c=%u", dirletter[(int) common->data_dir],18501850- common->data_size);18281828+ common->data_size);18511829 VDBG(common, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",18521830 name, cmnd_size, dirletter[(int) data_dir],18531831 common->data_size_from_cmnd, common->cmnd_size, hdlen);1854183218551855- /* We can't reply at all until we know the correct data direction18561856- * and size. */18331833+ /*18341834+ * We can't reply at all until we know the correct data direction18351835+ * and size.18361836+ */18571837 if (common->data_size_from_cmnd == 0)18581838 data_dir = DATA_DIR_NONE;18591839 if (common->data_size < common->data_size_from_cmnd) {18601860- /* Host data size < Device data size is a phase error.18401840+ /*18411841+ * Host data size < Device data size is a phase error.18611842 * Carry out the command, but only transfer as much as18621862- * we are allowed. */18431843+ * we are allowed.18441844+ */18631845 common->data_size_from_cmnd = common->data_size;18641846 common->phase_error = 1;18651847 }···18711845 common->usb_amount_left = common->data_size;1872184618731847 /* Conflicting data directions is a phase error */18741874- if (common->data_dir != data_dir18751875- && common->data_size_from_cmnd > 0) {18481848+ if (common->data_dir != data_dir && common->data_size_from_cmnd > 0) {18761849 common->phase_error = 1;18771850 return -EINVAL;18781851 }···18791854 /* Verify the length of the command itself */18801855 if (cmnd_size != common->cmnd_size) {1881185618821882- /* Special case workaround: There are plenty of buggy SCSI18571857+ /*18581858+ * Special case workaround: There are plenty of buggy SCSI18831859 * implementations. Many have issues with cbw->Length18841860 * field passing a wrong command size. For those cases we18851861 * always try to work around the problem by using the length···19221896 curlun = NULL;19231897 common->bad_lun_okay = 0;1924189819251925- /* INQUIRY and REQUEST SENSE commands are explicitly allowed19261926- * to use unsupported LUNs; all others may not. */18991899+ /*19001900+ * INQUIRY and REQUEST SENSE commands are explicitly allowed19011901+ * to use unsupported LUNs; all others may not.19021902+ */19271903 if (common->cmnd[0] != INQUIRY &&19281904 common->cmnd[0] != REQUEST_SENSE) {19291905 DBG(common, "unsupported LUN %d\n", common->lun);···19331905 }19341906 }1935190719361936- /* If a unit attention condition exists, only INQUIRY and19371937- * REQUEST SENSE commands are allowed; anything else must fail. */19081908+ /*19091909+ * If a unit attention condition exists, only INQUIRY and19101910+ * REQUEST SENSE commands are allowed; anything else must fail.19111911+ */19381912 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&19391939- common->cmnd[0] != INQUIRY &&19401940- common->cmnd[0] != REQUEST_SENSE) {19131913+ common->cmnd[0] != INQUIRY &&19141914+ common->cmnd[0] != REQUEST_SENSE) {19411915 curlun->sense_data = curlun->unit_attention_data;19421916 curlun->unit_attention_data = SS_NO_SENSE;19431917 return -EINVAL;···1964193419651935 return 0;19661936}19671967-1968193719691938static int do_scsi_command(struct fsg_common *common)19701939{···21522123 "TEST UNIT READY");21532124 break;2154212521552155- /* Although optional, this command is used by MS-Windows. We21562156- * support a minimal version: BytChk must be 0. */21262126+ /*21272127+ * Although optional, this command is used by MS-Windows. We21282128+ * support a minimal version: BytChk must be 0.21292129+ */21572130 case VERIFY:21582131 common->data_size_from_cmnd = 0;21592132 reply = check_command(common, 10, DATA_DIR_NONE,···21952164 reply = do_write(common);21962165 break;2197216621982198- /* Some mandatory commands that we recognize but don't implement.21672167+ /*21682168+ * Some mandatory commands that we recognize but don't implement.21992169 * They don't mean much in this setting. It's left as an exercise22002170 * for anyone interested to implement RESERVE and RELEASE in terms22012201- * of Posix locks. */21712171+ * of Posix locks.21722172+ */22022173 case FORMAT_UNIT:22032174 case RELEASE:22042175 case RESERVE:···22282195 if (reply == -EINVAL)22292196 reply = 0; /* Error reply length */22302197 if (reply >= 0 && common->data_dir == DATA_DIR_TO_HOST) {22312231- reply = min((u32) reply, common->data_size_from_cmnd);21982198+ reply = min((u32)reply, common->data_size_from_cmnd);22322199 bh->inreq->length = reply;22332200 bh->state = BUF_STATE_FULL;22342201 common->residue -= reply;···22582225 req->actual,22592226 le32_to_cpu(cbw->Signature));2260222722612261- /* The Bulk-only spec says we MUST stall the IN endpoint22282228+ /*22292229+ * The Bulk-only spec says we MUST stall the IN endpoint22622230 * (6.6.1), so it's unavoidable. It also says we must22632231 * retain this state until the next reset, but there's22642232 * no way to tell the controller driver it should ignore···22672233 *22682234 * We aren't required to halt the OUT endpoint; instead22692235 * we can simply accept and discard any data received22702270- * until the next reset. */22362236+ * until the next reset.22372237+ */22712238 wedge_bulk_in_endpoint(fsg);22722239 set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);22732240 return -EINVAL;···22812246 "cmdlen %u\n",22822247 cbw->Lun, cbw->Flags, cbw->Length);2283224822842284- /* We can do anything we want here, so let's stall the22852285- * bulk pipes if we are allowed to. */22492249+ /*22502250+ * We can do anything we want here, so let's stall the22512251+ * bulk pipes if we are allowed to.22522252+ */22862253 if (common->can_stall) {22872254 fsg_set_halt(fsg, fsg->bulk_out);22882255 halt_bulk_in_endpoint(fsg);···23072270 return 0;23082271}2309227223102310-23112273static int get_next_command(struct fsg_common *common)23122274{23132275 struct fsg_buffhd *bh;···23272291 /* Don't know what to do if common->fsg is NULL */23282292 return -EIO;2329229323302330- /* We will drain the buffer in software, which means we22942294+ /*22952295+ * We will drain the buffer in software, which means we23312296 * can reuse it for the next filling. No need to advance23322332- * next_buffhd_to_fill. */22972297+ * next_buffhd_to_fill.22982298+ */2333229923342300 /* Wait for the CBW to arrive */23352301 while (bh->state != BUF_STATE_FULL) {···2462242424632425/****************************** ALT CONFIGS ******************************/2464242624652465-24662427static int fsg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)24672428{24682429 struct fsg_dev *fsg = fsg_from_func(f);···24892452 struct fsg_lun *curlun;24902453 unsigned int exception_req_tag;2491245424922492- /* Clear the existing signals. Anything but SIGUSR1 is converted24932493- * into a high-priority EXIT exception. */24552455+ /*24562456+ * Clear the existing signals. Anything but SIGUSR1 is converted24572457+ * into a high-priority EXIT exception.24582458+ */24942459 for (;;) {24952460 int sig =24962461 dequeue_signal_lock(current, ¤t->blocked, &info);···25362497 usb_ep_fifo_flush(common->fsg->bulk_out);25372498 }2538249925392539- /* Reset the I/O buffer states and pointers, the SCSI25402540- * state, and the exception. Then invoke the handler. */25002500+ /*25012501+ * Reset the I/O buffer states and pointers, the SCSI25022502+ * state, and the exception. Then invoke the handler.25032503+ */25412504 spin_lock_irq(&common->lock);2542250525432506 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {···25772536 break;2578253725792538 case FSG_STATE_RESET:25802580- /* In case we were forced against our will to halt a25392539+ /*25402540+ * In case we were forced against our will to halt a25812541 * bulk endpoint, clear the halt now. (The SuperH UDC25822582- * requires this.) */25422542+ * requires this.)25432543+ */25832544 if (!fsg_is_set(common))25842545 break;25852546 if (test_and_clear_bit(IGNORE_BULK_OUT,···25912548 if (common->ep0_req_tag == exception_req_tag)25922549 ep0_queue(common); /* Complete the status stage */2593255025942594- /* Technically this should go here, but it would only be25512551+ /*25522552+ * Technically this should go here, but it would only be25952553 * a waste of time. Ditto for the INTERFACE_CHANGE and25962596- * CONFIG_CHANGE cases. */25542554+ * CONFIG_CHANGE cases.25552555+ */25972556 /* for (i = 0; i < common->nluns; ++i) */25982557 /* common->luns[i].unit_attention_data = */25992558 /* SS_RESET_OCCURRED; */···26302585{26312586 struct fsg_common *common = common_;2632258726332633- /* Allow the thread to be killed by a signal, but set the signal mask26342634- * to block everything but INT, TERM, KILL, and USR1. */25882588+ /*25892589+ * Allow the thread to be killed by a signal, but set the signal mask25902590+ * to block everything but INT, TERM, KILL, and USR1.25912591+ */26352592 allow_signal(SIGINT);26362593 allow_signal(SIGTERM);26372594 allow_signal(SIGKILL);···26422595 /* Allow the thread to be frozen */26432596 set_freezable();2644259726452645- /* Arrange for userspace references to be interpreted as kernel25982598+ /*25992599+ * Arrange for userspace references to be interpreted as kernel26462600 * pointers. That way we can pass a kernel pointer to a routine26472647- * that expects a __user pointer and it will work okay. */26012601+ * that expects a __user pointer and it will work okay.26022602+ */26482603 set_fs(get_ds());2649260426502605 /* The main loop */···27382689 kref_put(&common->ref, fsg_common_release);27392690}2740269127412741-27422692static struct fsg_common *fsg_common_init(struct fsg_common *common,27432693 struct usb_composite_dev *cdev,27442694 struct fsg_config *cfg)···27832735 fsg_intf_desc.iInterface = rc;27842736 }2785273727862786- /* Create the LUNs, open their backing files, and register the27872787- * LUN devices in sysfs. */27382738+ /*27392739+ * Create the LUNs, open their backing files, and register the27402740+ * LUN devices in sysfs.27412741+ */27882742 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);27892743 if (unlikely(!curlun)) {27902744 rc = -ENOMEM;···28402790 }28412791 common->nluns = nluns;2842279228432843-28442793 /* Data buffers cyclic list */28452794 bh = common->buffhds;28462795 i = FSG_NUM_BUFFERS;···28552806 }28562807 } while (--i);28572808 bh->next = common->buffhds;28582858-2859280928602810 /* Prepare inquiryString */28612811 if (cfg->release != 0xffff) {···28772829 : "File-CD Gadget"),28782830 i);2879283128802880-28812881- /* Some peripheral controllers are known not to be able to28322832+ /*28332833+ * Some peripheral controllers are known not to be able to28822834 * halt bulk endpoints correctly. If one of them is present,28832835 * disable stalls.28842836 */28852837 common->can_stall = cfg->can_stall &&28862838 !(gadget_is_at91(common->gadget));2887283928882888-28892840 spin_lock_init(&common->lock);28902841 kref_init(&common->ref);28912891-2892284228932843 /* Tell the thread to start working */28942844 common->thread_task =···28982852 }28992853 init_completion(&common->thread_notifier);29002854 init_waitqueue_head(&common->fsg_wait);29012901-2902285529032856 /* Information */29042857 INFO(common, FSG_DRIVER_DESC ", version: " FSG_DRIVER_VERSION "\n");···2931288629322887 return common;2933288829342934-29352889error_luns:29362890 common->nluns = i + 1;29372891error_release:29382892 common->state = FSG_STATE_TERMINATED; /* The thread is dead */29392939- /* Call fsg_common_release() directly, ref might be not29402940- * initialised */28932893+ /* Call fsg_common_release() directly, ref might be not initialised. */29412894 fsg_common_release(&common->ref);29422895 return ERR_PTR(rc);29432896}29442944-2945289729462898static void fsg_common_release(struct kref *ref)29472899{···2981293929822940/*-------------------------------------------------------------------------*/2983294129842984-29852942static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)29862943{29872944 struct fsg_dev *fsg = fsg_from_func(f);···29992958 usb_free_descriptors(fsg->function.hs_descriptors);30002959 kfree(fsg);30012960}30023002-3003296130042962static int fsg_bind(struct usb_configuration *c, struct usb_function *f)30052963{···30823042 fsg->function.disable = fsg_disable;3083304330843044 fsg->common = common;30853085- /* Our caller holds a reference to common structure so we30453045+ /*30463046+ * Our caller holds a reference to common structure so we30863047 * don't have to be worry about it being freed until we return30873048 * from this function. So instead of incrementing counter now30883049 * and decrement in error recovery we increment it only when30893089- * call to usb_add_function() was successful. */30503050+ * call to usb_add_function() was successful.30513051+ */3090305230913053 rc = usb_add_function(c, &fsg->function);30923054 if (unlikely(rc))···30993057}3100305831013059static inline int __deprecated __maybe_unused31023102-fsg_add(struct usb_composite_dev *cdev,31033103- struct usb_configuration *c,30603060+fsg_add(struct usb_composite_dev *cdev, struct usb_configuration *c,31043061 struct fsg_common *common)31053062{31063063 return fsg_bind_config(cdev, c, common);···310730663108306731093068/************************* Module parameters *************************/31103110-3111306931123070struct fsg_module_parameters {31133071 char *file[FSG_MAX_LUNS];···31203080 unsigned int luns; /* nluns */31213081 int stall; /* can_stall */31223082};31233123-3124308331253084#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \31263085 module_param_array_named(prefix ## name, params.name, type, \···31473108 "number of LUNs"); \31483109 _FSG_MODULE_PARAM(prefix, params, stall, bool, \31493110 "false to prevent bulk stalls")31503150-3151311131523112static void31533113fsg_config_from_params(struct fsg_config *cfg,
+1-1
drivers/usb/gadget/mass_storage.c
···102102};103103FSG_MODULE_PARAMETERS(/* no prefix */, mod_data);104104105105-static unsigned long msg_registered = 0;105105+static unsigned long msg_registered;106106static void msg_cleanup(void);107107108108static int msg_thread_exits(struct fsg_common *common)