diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 56a4c2da..d5902685 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -498,6 +498,7 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, if (ret) return ret; + dep->endpoint.maxpacket = usb_endpoint_maxp(desc); dep->endpoint.desc = desc; dep->comp_desc = comp_desc; dep->type = usb_endpoint_type(desc); @@ -1613,6 +1614,12 @@ static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc, int ret; usb_ep_set_maxpacket_limit(&dep->endpoint, 512); + /* + * The endpoint is not enabled yet, so maxburst would stay zero + * and DWC3_DEPCFG_BURST_SIZE() would underflow in SuperSpeed + * mode. One packet per burst is safe for every transfer type. + */ + dep->endpoint.maxburst = 1; dep->endpoint.max_streams = 15; dep->endpoint.ops = &dwc3_gadget_ep_ops; list_add_tail(&dep->endpoint.ep_list, diff --git a/drivers/usb/dwc3/gadget.h b/drivers/usb/dwc3/gadget.h index 7806ce59..13183eea 100644 --- a/drivers/usb/dwc3/gadget.h +++ b/drivers/usb/dwc3/gadget.h @@ -43,7 +43,7 @@ struct dwc3; #define DWC3_DEPCFG_EP_TYPE(n) ((n) << 1) #define DWC3_DEPCFG_MAX_PACKET_SIZE(n) ((n) << 3) #define DWC3_DEPCFG_FIFO_NUMBER(n) ((n) << 17) -#define DWC3_DEPCFG_BURST_SIZE(n) ((n) << 22) +#define DWC3_DEPCFG_BURST_SIZE(n) (((n) & 0xf) << 22) #define DWC3_DEPCFG_DATA_SEQ_NUM(n) ((n) << 26) /* This applies for core versions earlier than 1.94a */ #define DWC3_DEPCFG_IGN_SEQ_NUM (1 << 31) diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 45f0504b..cb95d298 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -279,8 +279,6 @@ static const char fsg_string_interface[] = "Mass Storage"; /*-------------------------------------------------------------------------*/ #define GFP_ATOMIC ((gfp_t) 0) -#define PAGE_CACHE_SHIFT 12 -#define PAGE_CACHE_SIZE (1 << PAGE_CACHE_SHIFT) #define kthread_create(...) __builtin_return_address(0) #define wait_for_completion(...) do {} while (0) @@ -697,7 +695,6 @@ static int do_read(struct fsg_common *common) u32 amount_left; loff_t file_offset; unsigned int amount; - unsigned int partial_page; ssize_t nread; /* Get the starting Logical Block Address and check that it's @@ -728,19 +725,8 @@ static int do_read(struct fsg_common *common) for (;;) { - /* Figure out how much we need to read: - * Try to read the remaining amount. - * But don't read more than the buffer size. - * And don't try to read past the end of the file. - * Finally, if we're not at a page boundary, don't read past - * the next page. - * If this means reading 0 then we were asked to read past - * the end of file. */ + /* Keep intermediate Bulk IN requests aligned to maxpacket. */ amount = min(amount_left, FSG_BUFLEN); - partial_page = file_offset & (PAGE_CACHE_SIZE - 1); - if (partial_page > 0) - amount = min(amount, (unsigned int) PAGE_CACHE_SIZE - - partial_page); /* Wait for the next buffer to become available */ bh = common->next_buffhd_to_fill; @@ -822,9 +808,8 @@ static int do_write(struct fsg_common *common) struct fsg_buffhd *bh; int get_some_more; u32 amount_left_to_req, amount_left_to_write; - loff_t usb_offset, file_offset; + loff_t file_offset; unsigned int amount; - unsigned int partial_page; ssize_t nwritten; int rc; @@ -849,14 +834,16 @@ static int do_write(struct fsg_common *common) return -EINVAL; } } - if (lba >= curlun->num_sectors) { + if (lba >= curlun->num_sectors || + DIV_ROUND_UP((u64)common->data_size_from_cmnd, SECTOR_SIZE) > + curlun->num_sectors - lba) { curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; return -EINVAL; } /* Carry out the file writes */ get_some_more = 1; - file_offset = usb_offset = ((loff_t) lba) << 9; + file_offset = ((loff_t) lba) << 9; amount_left_to_req = common->data_size_from_cmnd; amount_left_to_write = common->data_size_from_cmnd; @@ -866,49 +853,21 @@ static int do_write(struct fsg_common *common) bh = common->next_buffhd_to_fill; if (bh->state == BUF_STATE_EMPTY && get_some_more) { - /* Figure out how much we want to get: - * Try to get the remaining amount. - * But don't get more than the buffer size. - * And don't try to go past the end of the file. - * If we're not at a page boundary, - * don't go past the next page. - * If this means getting 0, then we were asked - * to write past the end of file. - * Finally, round down to a block boundary. */ + /* + * The controller may round the request up to the + * endpoint packet size, so keep the intended length + * separate from the length handed to the UDC. + */ amount = min(amount_left_to_req, FSG_BUFLEN); - partial_page = usb_offset & (PAGE_CACHE_SIZE - 1); - if (partial_page > 0) - amount = min(amount, - (unsigned int) PAGE_CACHE_SIZE - partial_page); - - if (amount == 0) { - get_some_more = 0; - curlun->sense_data = - SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; - curlun->info_valid = 1; - continue; - } - amount -= (amount & 511); - if (amount == 0) { - - /* Why were we were asked to transfer a - * partial block? */ - get_some_more = 0; - continue; - } /* Get the next buffer */ - usb_offset += amount; common->usb_amount_left -= amount; amount_left_to_req -= amount; if (amount_left_to_req == 0) get_some_more = 0; - /* amount is always divisible by 512, hence by - * the bulk-out maxpacket size */ - bh->outreq->length = amount; - bh->bulk_out_intended_length = amount; - bh->outreq->short_not_ok = 1; + set_bulk_out_req_length(common, bh, amount); + bh->outreq->short_not_ok = bh->outreq->length == amount; START_TRANSFER_OR(common, bulk_out, bh->outreq, &bh->outreq_busy, &bh->state) /* Don't know what to do if @@ -927,37 +886,31 @@ static int do_write(struct fsg_common *common) bh->state = BUF_STATE_EMPTY; /* Did something go wrong with the transfer? */ - if (bh->outreq->status != 0) { + if (bh->outreq->status != 0 || + bh->outreq->actual > bh->bulk_out_intended_length) { curlun->sense_data = SS_COMMUNICATION_FAILURE; curlun->info_valid = 1; break; } - amount = bh->outreq->actual; + /* The rounded receive length is not a disk write length. */ + if (bh->outreq->actual < bh->bulk_out_intended_length) + common->short_packet_received = 1; + amount = round_down(bh->outreq->actual, SECTOR_SIZE); + if (amount == 0) + break; /* 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; + nwritten = rc > 0 ? rc * SECTOR_SIZE : 0; 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; @@ -972,10 +925,8 @@ static int do_write(struct fsg_common *common) } /* Did the host decide to stop early? */ - if (bh->outreq->actual != bh->outreq->length) { - common->short_packet_received = 1; + if (common->short_packet_received) break; - } continue; } @@ -1467,7 +1418,7 @@ static int throw_away_data(struct fsg_common *common) common->next_buffhd_to_drain = bh->next; /* A short packet or an error ends everything */ - if (bh->outreq->actual != bh->outreq->length || + if (bh->outreq->actual != bh->bulk_out_intended_length || bh->outreq->status != 0) { raise_exception(common, FSG_STATE_ABORT_BULK_OUT); @@ -1482,11 +1433,8 @@ static int throw_away_data(struct fsg_common *common) && common->usb_amount_left > 0) { amount = min(common->usb_amount_left, FSG_BUFLEN); - /* amount is always divisible by 512, hence by - * the bulk-out maxpacket size */ - bh->outreq->length = amount; - bh->bulk_out_intended_length = amount; - bh->outreq->short_not_ok = 1; + set_bulk_out_req_length(common, bh, amount); + bh->outreq->short_not_ok = bh->outreq->length == amount; START_TRANSFER_OR(common, bulk_out, bh->outreq, &bh->outreq_busy, &bh->state) /* Don't know what to do if @@ -2224,15 +2172,17 @@ reset: fsg = common->fsg; /* Enable the endpoints */ - d = fsg_ep_desc(common->gadget, - &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc); + d = fsg_ep_desc(common->gadget, fsg->bulk_in, + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc, + &fsg_ss_bulk_in_desc); rc = enable_endpoint(common, fsg->bulk_in, d); if (rc) goto reset; fsg->bulk_in_enabled = 1; - d = fsg_ep_desc(common->gadget, - &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc); + d = fsg_ep_desc(common->gadget, fsg->bulk_out, + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc, + &fsg_ss_bulk_out_desc); rc = enable_endpoint(common, fsg->bulk_out, d); if (rc) goto reset; @@ -2650,6 +2600,7 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f) free(fsg->function.descriptors); free(fsg->function.hs_descriptors); + free(fsg->function.ss_descriptors); kfree(fsg); } @@ -2693,13 +2644,31 @@ static int fsg_bind(struct usb_configuration *c, struct usb_function *f) fsg_hs_bulk_out_desc.bEndpointAddress = fsg_fs_bulk_out_desc.bEndpointAddress; f->hs_descriptors = usb_copy_descriptors(fsg_hs_function); - if (unlikely(!f->hs_descriptors)) { - free(f->descriptors); - return -ENOMEM; - } + if (unlikely(!f->hs_descriptors)) + goto free_descriptors; + } + + if (gadget_is_superspeed(gadget)) { + /* Assume endpoint addresses are the same for all speeds */ + fsg_ss_bulk_in_desc.bEndpointAddress = + fsg_fs_bulk_in_desc.bEndpointAddress; + fsg_ss_bulk_out_desc.bEndpointAddress = + fsg_fs_bulk_out_desc.bEndpointAddress; + f->ss_descriptors = usb_copy_descriptors(fsg_ss_function); + if (unlikely(!f->ss_descriptors)) + goto free_descriptors; } return 0; +free_descriptors: + free(f->ss_descriptors); + f->ss_descriptors = NULL; + free(f->hs_descriptors); + f->hs_descriptors = NULL; + free(f->descriptors); + f->descriptors = NULL; + return -ENOMEM; + autoconf_fail: ERROR(fsg, "unable to autoconfigure all endpoints\n"); return -ENOTSUPP; diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c index 5674e8fe..2261c1f0 100644 --- a/drivers/usb/gadget/storage_common.c +++ b/drivers/usb/gadget/storage_common.c @@ -531,13 +531,61 @@ static struct usb_descriptor_header *fsg_hs_function[] = { NULL, }; +/* + * SuperSpeed bulk endpoints. FSG_NO_OTG and FSG_NO_INTR_EP are set by the + * mass storage function, so the SuperSpeed interface description only carries + * the two bulk endpoints and their companion descriptors. + */ +static struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = cpu_to_le16(1024), +}; + +static struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = cpu_to_le16(1024), +}; + +/* Both bulk endpoints use a single packet per burst and no streams. */ +static struct usb_ss_ep_comp_descriptor fsg_ss_bulk_comp_desc = { + .bLength = sizeof(fsg_ss_bulk_comp_desc), + .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, + + .bMaxBurst = 0, +}; + +static struct usb_descriptor_header *fsg_ss_function[] = { + (struct usb_descriptor_header *) &fsg_intf_desc, + (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc, + (struct usb_descriptor_header *) &fsg_ss_bulk_comp_desc, + (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc, + (struct usb_descriptor_header *) &fsg_ss_bulk_comp_desc, + NULL, +}; + /* Maxpacket and other transfer characteristics vary by speed. */ -static struct usb_endpoint_descriptor * -fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs, - struct usb_endpoint_descriptor *hs) +static const struct usb_endpoint_descriptor * +fsg_ep_desc(struct usb_gadget *g, struct usb_ep *ep, + const struct usb_endpoint_descriptor *fs, + const struct usb_endpoint_descriptor *hs, + const struct usb_endpoint_descriptor *ss) { + ep->comp_desc = NULL; + + if (gadget_is_superspeed(g) && g->speed >= USB_SPEED_SUPER) { + ep->comp_desc = &fsg_ss_bulk_comp_desc; + return ss; + } + if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) return hs; + return fs; }