Commit 6aae84769a0be095daf94d34fdd61b12d59a7022

Authored by Seung-Woo Kim
Committed by Marek Vasut
1 parent afa314d3a3

gadget: f_thor: Fix memory leaks of usb request and its buffer

There are memory leaks of usb request and its buffer for ep0,
in_ep, and out ep. Fix memory leaks of usb request and its buffer.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>

Showing 1 changed file with 32 additions and 33 deletions Side-by-side Diff

drivers/usb/gadget/f_thor.c
... ... @@ -620,22 +620,6 @@
620 620 status, req->actual, req->length);
621 621 }
622 622  
623   -static struct usb_request *thor_start_ep(struct usb_ep *ep)
624   -{
625   - struct usb_request *req;
626   -
627   - req = alloc_ep_req(ep, THOR_PACKET_SIZE);
628   - debug("%s: ep:%p req:%p\n", __func__, ep, req);
629   -
630   - if (!req)
631   - return NULL;
632   -
633   - memset(req->buf, 0, req->length);
634   - req->complete = thor_rx_tx_complete;
635   -
636   - return req;
637   -}
638   -
639 623 static void thor_setup_complete(struct usb_ep *ep, struct usb_request *req)
640 624 {
641 625 if (req->status || req->actual != req->length)
... ... @@ -752,6 +736,13 @@
752 736 return 0;
753 737 }
754 738  
  739 +static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  740 +{
  741 + if (req->buf)
  742 + free(req->buf);
  743 + usb_ep_free_request(ep, req);
  744 +}
  745 +
755 746 static int thor_func_bind(struct usb_configuration *c, struct usb_function *f)
756 747 {
757 748 struct usb_gadget *gadget = c->cdev->gadget;
758 749  
759 750  
... ... @@ -860,21 +851,18 @@
860 851 return 0;
861 852  
862 853 fail:
  854 + if (dev->req)
  855 + free_ep_req(gadget->ep0, dev->req);
863 856 free(dev);
864 857 return status;
865 858 }
866 859  
867   -static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
868   -{
869   - free(req->buf);
870   - usb_ep_free_request(ep, req);
871   -}
872   -
873 860 static void thor_unbind(struct usb_configuration *c, struct usb_function *f)
874 861 {
875 862 struct f_thor *f_thor = func_to_thor(f);
876 863 struct thor_dev *dev = f_thor->dev;
877 864  
  865 + free_ep_req(dev->gadget->ep0, dev->req);
878 866 free(dev);
879 867 memset(thor_func, 0, sizeof(*thor_func));
880 868 thor_func = NULL;
... ... @@ -895,8 +883,6 @@
895 883 }
896 884  
897 885 if (dev->out_ep->driver_data) {
898   - free(dev->out_req->buf);
899   - dev->out_req->buf = NULL;
900 886 usb_ep_free_request(dev->out_ep, dev->out_req);
901 887 usb_ep_disable(dev->out_ep);
902 888 dev->out_ep->driver_data = NULL;
903 889  
904 890  
905 891  
906 892  
... ... @@ -924,16 +910,17 @@
924 910  
925 911 result = usb_ep_enable(ep, d);
926 912 if (result)
927   - goto exit;
  913 + goto err;
928 914  
929 915 ep->driver_data = cdev; /* claim */
930   - req = thor_start_ep(ep);
  916 + req = alloc_ep_req(ep, THOR_PACKET_SIZE);
931 917 if (!req) {
932   - usb_ep_disable(ep);
933 918 result = -EIO;
934   - goto exit;
  919 + goto err_disable_in_ep;
935 920 }
936 921  
  922 + memset(req->buf, 0, req->length);
  923 + req->complete = thor_rx_tx_complete;
937 924 dev->in_req = req;
938 925 ep = dev->out_ep;
939 926 d = ep_desc(gadget, &hs_out_desc, &fs_out_desc);
940 927  
941 928  
942 929  
943 930  
944 931  
... ... @@ -941,22 +928,34 @@
941 928  
942 929 result = usb_ep_enable(ep, d);
943 930 if (result)
944   - goto exit;
  931 + goto err_free_in_req;
945 932  
946 933 ep->driver_data = cdev; /* claim */
947   - req = thor_start_ep(ep);
  934 + req = usb_ep_alloc_request(ep, 0);
948 935 if (!req) {
949   - usb_ep_disable(ep);
950 936 result = -EIO;
951   - goto exit;
  937 + goto err_disable_out_ep;
952 938 }
953 939  
  940 + req->complete = thor_rx_tx_complete;
954 941 dev->out_req = req;
955 942 /* ACM control EP */
956 943 ep = dev->int_ep;
957 944 ep->driver_data = cdev; /* claim */
958 945  
959   - exit:
  946 + return 0;
  947 +
  948 + err_disable_out_ep:
  949 + usb_ep_disable(dev->out_ep);
  950 +
  951 + err_free_in_req:
  952 + free_ep_req(dev->in_ep, dev->in_req);
  953 + dev->in_req = NULL;
  954 +
  955 + err_disable_in_ep:
  956 + usb_ep_disable(dev->in_ep);
  957 +
  958 + err:
960 959 return result;
961 960 }
962 961