Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Cadence CDNSP DRD Driver.
0004  *
0005  * Copyright (C) 2020 Cadence.
0006  *
0007  * Author: Pawel Laszczak <pawell@cadence.com>
0008  *
0009  */
0010 
0011 #include <linux/usb/composite.h>
0012 #include <linux/usb/gadget.h>
0013 #include <linux/list.h>
0014 
0015 #include "cdnsp-gadget.h"
0016 #include "cdnsp-trace.h"
0017 
0018 static void cdnsp_ep0_stall(struct cdnsp_device *pdev)
0019 {
0020     struct cdnsp_request *preq;
0021     struct cdnsp_ep *pep;
0022 
0023     pep = &pdev->eps[0];
0024     preq = next_request(&pep->pending_list);
0025 
0026     if (pdev->three_stage_setup) {
0027         cdnsp_halt_endpoint(pdev, pep, true);
0028 
0029         if (preq)
0030             cdnsp_gadget_giveback(pep, preq, -ECONNRESET);
0031     } else {
0032         pep->ep_state |= EP0_HALTED_STATUS;
0033 
0034         if (preq)
0035             list_del(&preq->list);
0036 
0037         cdnsp_status_stage(pdev);
0038     }
0039 }
0040 
0041 static int cdnsp_ep0_delegate_req(struct cdnsp_device *pdev,
0042                   struct usb_ctrlrequest *ctrl)
0043 {
0044     int ret;
0045 
0046     spin_unlock(&pdev->lock);
0047     ret = pdev->gadget_driver->setup(&pdev->gadget, ctrl);
0048     spin_lock(&pdev->lock);
0049 
0050     return ret;
0051 }
0052 
0053 static int cdnsp_ep0_set_config(struct cdnsp_device *pdev,
0054                 struct usb_ctrlrequest *ctrl)
0055 {
0056     enum usb_device_state state = pdev->gadget.state;
0057     u32 cfg;
0058     int ret;
0059 
0060     cfg = le16_to_cpu(ctrl->wValue);
0061 
0062     switch (state) {
0063     case USB_STATE_ADDRESS:
0064         trace_cdnsp_ep0_set_config("from Address state");
0065         break;
0066     case USB_STATE_CONFIGURED:
0067         trace_cdnsp_ep0_set_config("from Configured state");
0068         break;
0069     default:
0070         dev_err(pdev->dev, "Set Configuration - bad device state\n");
0071         return -EINVAL;
0072     }
0073 
0074     ret = cdnsp_ep0_delegate_req(pdev, ctrl);
0075     if (ret)
0076         return ret;
0077 
0078     if (!cfg)
0079         usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
0080 
0081     return 0;
0082 }
0083 
0084 static int cdnsp_ep0_set_address(struct cdnsp_device *pdev,
0085                  struct usb_ctrlrequest *ctrl)
0086 {
0087     enum usb_device_state state = pdev->gadget.state;
0088     struct cdnsp_slot_ctx *slot_ctx;
0089     unsigned int slot_state;
0090     int ret;
0091     u32 addr;
0092 
0093     addr = le16_to_cpu(ctrl->wValue);
0094 
0095     if (addr > 127) {
0096         dev_err(pdev->dev, "Invalid device address %d\n", addr);
0097         return -EINVAL;
0098     }
0099 
0100     slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
0101 
0102     if (state == USB_STATE_CONFIGURED) {
0103         dev_err(pdev->dev, "Can't Set Address from Configured State\n");
0104         return -EINVAL;
0105     }
0106 
0107     pdev->device_address = le16_to_cpu(ctrl->wValue);
0108 
0109     slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
0110     slot_state = GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state));
0111     if (slot_state == SLOT_STATE_ADDRESSED)
0112         cdnsp_reset_device(pdev);
0113 
0114     /*set device address*/
0115     ret = cdnsp_setup_device(pdev, SETUP_CONTEXT_ADDRESS);
0116     if (ret)
0117         return ret;
0118 
0119     if (addr)
0120         usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
0121     else
0122         usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
0123 
0124     return 0;
0125 }
0126 
0127 int cdnsp_status_stage(struct cdnsp_device *pdev)
0128 {
0129     pdev->ep0_stage = CDNSP_STATUS_STAGE;
0130     pdev->ep0_preq.request.length = 0;
0131 
0132     return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
0133 }
0134 
0135 static int cdnsp_w_index_to_ep_index(u16 wIndex)
0136 {
0137     if (!(wIndex & USB_ENDPOINT_NUMBER_MASK))
0138         return 0;
0139 
0140     return ((wIndex & USB_ENDPOINT_NUMBER_MASK) * 2) +
0141         (wIndex & USB_ENDPOINT_DIR_MASK ? 1 : 0) - 1;
0142 }
0143 
0144 static int cdnsp_ep0_handle_status(struct cdnsp_device *pdev,
0145                    struct usb_ctrlrequest *ctrl)
0146 {
0147     struct cdnsp_ep *pep;
0148     __le16 *response;
0149     int ep_sts = 0;
0150     u16 status = 0;
0151     u32 recipient;
0152 
0153     recipient = ctrl->bRequestType & USB_RECIP_MASK;
0154 
0155     switch (recipient) {
0156     case USB_RECIP_DEVICE:
0157         status = pdev->gadget.is_selfpowered;
0158         status |= pdev->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
0159 
0160         if (pdev->gadget.speed >= USB_SPEED_SUPER) {
0161             status |= pdev->u1_allowed << USB_DEV_STAT_U1_ENABLED;
0162             status |= pdev->u2_allowed << USB_DEV_STAT_U2_ENABLED;
0163         }
0164         break;
0165     case USB_RECIP_INTERFACE:
0166         /*
0167          * Function Remote Wake Capable D0
0168          * Function Remote Wakeup   D1
0169          */
0170         return cdnsp_ep0_delegate_req(pdev, ctrl);
0171     case USB_RECIP_ENDPOINT:
0172         ep_sts = cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex));
0173         pep = &pdev->eps[ep_sts];
0174         ep_sts = GET_EP_CTX_STATE(pep->out_ctx);
0175 
0176         /* check if endpoint is stalled */
0177         if (ep_sts == EP_STATE_HALTED)
0178             status =  BIT(USB_ENDPOINT_HALT);
0179         break;
0180     default:
0181         return -EINVAL;
0182     }
0183 
0184     response = (__le16 *)pdev->setup_buf;
0185     *response = cpu_to_le16(status);
0186 
0187     pdev->ep0_preq.request.length = sizeof(*response);
0188     pdev->ep0_preq.request.buf = pdev->setup_buf;
0189 
0190     return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
0191 }
0192 
0193 static void cdnsp_enter_test_mode(struct cdnsp_device *pdev)
0194 {
0195     u32 temp;
0196 
0197     temp = readl(&pdev->active_port->regs->portpmsc) & ~GENMASK(31, 28);
0198     temp |= PORT_TEST_MODE(pdev->test_mode);
0199     writel(temp, &pdev->active_port->regs->portpmsc);
0200 }
0201 
0202 static int cdnsp_ep0_handle_feature_device(struct cdnsp_device *pdev,
0203                        struct usb_ctrlrequest *ctrl,
0204                        int set)
0205 {
0206     enum usb_device_state state;
0207     enum usb_device_speed speed;
0208     u16 tmode;
0209 
0210     state = pdev->gadget.state;
0211     speed = pdev->gadget.speed;
0212 
0213     switch (le16_to_cpu(ctrl->wValue)) {
0214     case USB_DEVICE_REMOTE_WAKEUP:
0215         pdev->may_wakeup = !!set;
0216         trace_cdnsp_may_wakeup(set);
0217         break;
0218     case USB_DEVICE_U1_ENABLE:
0219         if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
0220             return -EINVAL;
0221 
0222         pdev->u1_allowed = !!set;
0223         trace_cdnsp_u1(set);
0224         break;
0225     case USB_DEVICE_U2_ENABLE:
0226         if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
0227             return -EINVAL;
0228 
0229         pdev->u2_allowed = !!set;
0230         trace_cdnsp_u2(set);
0231         break;
0232     case USB_DEVICE_LTM_ENABLE:
0233         return -EINVAL;
0234     case USB_DEVICE_TEST_MODE:
0235         if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
0236             return -EINVAL;
0237 
0238         tmode = le16_to_cpu(ctrl->wIndex);
0239 
0240         if (!set || (tmode & 0xff) != 0)
0241             return -EINVAL;
0242 
0243         tmode = tmode >> 8;
0244 
0245         if (tmode > USB_TEST_FORCE_ENABLE || tmode < USB_TEST_J)
0246             return -EINVAL;
0247 
0248         pdev->test_mode = tmode;
0249 
0250         /*
0251          * Test mode must be set before Status Stage but controller
0252          * will start testing sequence after Status Stage.
0253          */
0254         cdnsp_enter_test_mode(pdev);
0255         break;
0256     default:
0257         return -EINVAL;
0258     }
0259 
0260     return 0;
0261 }
0262 
0263 static int cdnsp_ep0_handle_feature_intf(struct cdnsp_device *pdev,
0264                      struct usb_ctrlrequest *ctrl,
0265                      int set)
0266 {
0267     u16 wValue, wIndex;
0268     int ret;
0269 
0270     wValue = le16_to_cpu(ctrl->wValue);
0271     wIndex = le16_to_cpu(ctrl->wIndex);
0272 
0273     switch (wValue) {
0274     case USB_INTRF_FUNC_SUSPEND:
0275         ret = cdnsp_ep0_delegate_req(pdev, ctrl);
0276         if (ret)
0277             return ret;
0278 
0279         /*
0280          * Remote wakeup is enabled when any function within a device
0281          * is enabled for function remote wakeup.
0282          */
0283         if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
0284             pdev->may_wakeup++;
0285         else
0286             if (pdev->may_wakeup > 0)
0287                 pdev->may_wakeup--;
0288 
0289         return 0;
0290     default:
0291         return -EINVAL;
0292     }
0293 
0294     return 0;
0295 }
0296 
0297 static int cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device *pdev,
0298                          struct usb_ctrlrequest *ctrl,
0299                          int set)
0300 {
0301     struct cdnsp_ep *pep;
0302     u16 wValue;
0303 
0304     wValue = le16_to_cpu(ctrl->wValue);
0305     pep = &pdev->eps[cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex))];
0306 
0307     switch (wValue) {
0308     case USB_ENDPOINT_HALT:
0309         if (!set && (pep->ep_state & EP_WEDGE)) {
0310             /* Resets Sequence Number */
0311             cdnsp_halt_endpoint(pdev, pep, 0);
0312             cdnsp_halt_endpoint(pdev, pep, 1);
0313             break;
0314         }
0315 
0316         return cdnsp_halt_endpoint(pdev, pep, set);
0317     default:
0318         dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue);
0319         return -EINVAL;
0320     }
0321 
0322     return 0;
0323 }
0324 
0325 static int cdnsp_ep0_handle_feature(struct cdnsp_device *pdev,
0326                     struct usb_ctrlrequest *ctrl,
0327                     int set)
0328 {
0329     switch (ctrl->bRequestType & USB_RECIP_MASK) {
0330     case USB_RECIP_DEVICE:
0331         return cdnsp_ep0_handle_feature_device(pdev, ctrl, set);
0332     case USB_RECIP_INTERFACE:
0333         return cdnsp_ep0_handle_feature_intf(pdev, ctrl, set);
0334     case USB_RECIP_ENDPOINT:
0335         return cdnsp_ep0_handle_feature_endpoint(pdev, ctrl, set);
0336     default:
0337         return -EINVAL;
0338     }
0339 }
0340 
0341 static int cdnsp_ep0_set_sel(struct cdnsp_device *pdev,
0342                  struct usb_ctrlrequest *ctrl)
0343 {
0344     enum usb_device_state state = pdev->gadget.state;
0345     u16 wLength;
0346 
0347     if (state == USB_STATE_DEFAULT)
0348         return -EINVAL;
0349 
0350     wLength = le16_to_cpu(ctrl->wLength);
0351 
0352     if (wLength != 6) {
0353         dev_err(pdev->dev, "Set SEL should be 6 bytes, got %d\n",
0354             wLength);
0355         return -EINVAL;
0356     }
0357 
0358     /*
0359      * To handle Set SEL we need to receive 6 bytes from Host. So let's
0360      * queue a usb_request for 6 bytes.
0361      */
0362     pdev->ep0_preq.request.length = 6;
0363     pdev->ep0_preq.request.buf = pdev->setup_buf;
0364 
0365     return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
0366 }
0367 
0368 static int cdnsp_ep0_set_isoch_delay(struct cdnsp_device *pdev,
0369                      struct usb_ctrlrequest *ctrl)
0370 {
0371     if (le16_to_cpu(ctrl->wIndex) || le16_to_cpu(ctrl->wLength))
0372         return -EINVAL;
0373 
0374     pdev->gadget.isoch_delay = le16_to_cpu(ctrl->wValue);
0375 
0376     return 0;
0377 }
0378 
0379 static int cdnsp_ep0_std_request(struct cdnsp_device *pdev,
0380                  struct usb_ctrlrequest *ctrl)
0381 {
0382     int ret;
0383 
0384     switch (ctrl->bRequest) {
0385     case USB_REQ_GET_STATUS:
0386         ret = cdnsp_ep0_handle_status(pdev, ctrl);
0387         break;
0388     case USB_REQ_CLEAR_FEATURE:
0389         ret = cdnsp_ep0_handle_feature(pdev, ctrl, 0);
0390         break;
0391     case USB_REQ_SET_FEATURE:
0392         ret = cdnsp_ep0_handle_feature(pdev, ctrl, 1);
0393         break;
0394     case USB_REQ_SET_ADDRESS:
0395         ret = cdnsp_ep0_set_address(pdev, ctrl);
0396         break;
0397     case USB_REQ_SET_CONFIGURATION:
0398         ret = cdnsp_ep0_set_config(pdev, ctrl);
0399         break;
0400     case USB_REQ_SET_SEL:
0401         ret = cdnsp_ep0_set_sel(pdev, ctrl);
0402         break;
0403     case USB_REQ_SET_ISOCH_DELAY:
0404         ret = cdnsp_ep0_set_isoch_delay(pdev, ctrl);
0405         break;
0406     case USB_REQ_SET_INTERFACE:
0407         /*
0408          * Add request into pending list to block sending status stage
0409          * by libcomposite.
0410          */
0411         list_add_tail(&pdev->ep0_preq.list,
0412                   &pdev->ep0_preq.pep->pending_list);
0413 
0414         ret = cdnsp_ep0_delegate_req(pdev, ctrl);
0415         if (ret == -EBUSY)
0416             ret = 0;
0417 
0418         list_del(&pdev->ep0_preq.list);
0419         break;
0420     default:
0421         ret = cdnsp_ep0_delegate_req(pdev, ctrl);
0422         break;
0423     }
0424 
0425     return ret;
0426 }
0427 
0428 void cdnsp_setup_analyze(struct cdnsp_device *pdev)
0429 {
0430     struct usb_ctrlrequest *ctrl = &pdev->setup;
0431     int ret = 0;
0432     u16 len;
0433 
0434     trace_cdnsp_ctrl_req(ctrl);
0435 
0436     if (!pdev->gadget_driver)
0437         goto out;
0438 
0439     if (pdev->gadget.state == USB_STATE_NOTATTACHED) {
0440         dev_err(pdev->dev, "ERR: Setup detected in unattached state\n");
0441         ret = -EINVAL;
0442         goto out;
0443     }
0444 
0445     /* Restore the ep0 to Stopped/Running state. */
0446     if (pdev->eps[0].ep_state & EP_HALTED) {
0447         trace_cdnsp_ep0_halted("Restore to normal state");
0448         cdnsp_halt_endpoint(pdev, &pdev->eps[0], 0);
0449     }
0450 
0451     /*
0452      * Finishing previous SETUP transfer by removing request from
0453      * list and informing upper layer
0454      */
0455     if (!list_empty(&pdev->eps[0].pending_list)) {
0456         struct cdnsp_request    *req;
0457 
0458         trace_cdnsp_ep0_request("Remove previous");
0459         req = next_request(&pdev->eps[0].pending_list);
0460         cdnsp_ep_dequeue(&pdev->eps[0], req);
0461     }
0462 
0463     len = le16_to_cpu(ctrl->wLength);
0464     if (!len) {
0465         pdev->three_stage_setup = false;
0466         pdev->ep0_expect_in = false;
0467     } else {
0468         pdev->three_stage_setup = true;
0469         pdev->ep0_expect_in = !!(ctrl->bRequestType & USB_DIR_IN);
0470     }
0471 
0472     if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
0473         ret = cdnsp_ep0_std_request(pdev, ctrl);
0474     else
0475         ret = cdnsp_ep0_delegate_req(pdev, ctrl);
0476 
0477     if (!len)
0478         pdev->ep0_stage = CDNSP_STATUS_STAGE;
0479 
0480     if (ret == USB_GADGET_DELAYED_STATUS) {
0481         trace_cdnsp_ep0_status_stage("delayed");
0482         return;
0483     }
0484 out:
0485     if (ret < 0)
0486         cdnsp_ep0_stall(pdev);
0487     else if (pdev->ep0_stage == CDNSP_STATUS_STAGE)
0488         cdnsp_status_stage(pdev);
0489 }