Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
0004  * Copyright (C) 2015-2016 Samsung Electronics
0005  *               Igor Kotrasinski <i.kotrasinsk@samsung.com>
0006  *
0007  * Based on dummy_hcd.c, which is:
0008  * Copyright (C) 2003 David Brownell
0009  * Copyright (C) 2003-2005 Alan Stern
0010  */
0011 
0012 #include <linux/usb.h>
0013 #include <linux/timer.h>
0014 #include <linux/usb/ch9.h>
0015 
0016 #include "vudc.h"
0017 
0018 #define DEV_REQUEST (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
0019 #define DEV_INREQUEST   (DEV_REQUEST | USB_DIR_IN)
0020 #define INTF_REQUEST    (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
0021 #define INTF_INREQUEST  (INTF_REQUEST | USB_DIR_IN)
0022 #define EP_REQUEST  (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
0023 #define EP_INREQUEST    (EP_REQUEST | USB_DIR_IN)
0024 
0025 static int get_frame_limit(enum usb_device_speed speed)
0026 {
0027     switch (speed) {
0028     case USB_SPEED_LOW:
0029         return 8 /*bytes*/ * 12 /*packets*/;
0030     case USB_SPEED_FULL:
0031         return 64 /*bytes*/ * 19 /*packets*/;
0032     case USB_SPEED_HIGH:
0033         return 512 /*bytes*/ * 13 /*packets*/ * 8 /*uframes*/;
0034     case USB_SPEED_SUPER:
0035         /* Bus speed is 500000 bytes/ms, so use a little less */
0036         return 490000;
0037     default:
0038         /* error */
0039         return -1;
0040     }
0041 
0042 }
0043 
0044 /*
0045  * handle_control_request() - handles all control transfers
0046  * @udc: pointer to vudc
0047  * @urb: the urb request to handle
0048  * @setup: pointer to the setup data for a USB device control
0049  *   request
0050  * @status: pointer to request handling status
0051  *
0052  * Return 0 - if the request was handled
0053  *    1 - if the request wasn't handles
0054  *    error code on error
0055  *
0056  * Adapted from drivers/usb/gadget/udc/dummy_hcd.c
0057  */
0058 static int handle_control_request(struct vudc *udc, struct urb *urb,
0059                   struct usb_ctrlrequest *setup,
0060                   int *status)
0061 {
0062     struct vep  *ep2;
0063     int     ret_val = 1;
0064     unsigned int    w_index;
0065     unsigned int    w_value;
0066 
0067     w_index = le16_to_cpu(setup->wIndex);
0068     w_value = le16_to_cpu(setup->wValue);
0069     switch (setup->bRequest) {
0070     case USB_REQ_SET_ADDRESS:
0071         if (setup->bRequestType != DEV_REQUEST)
0072             break;
0073         udc->address = w_value;
0074         ret_val = 0;
0075         *status = 0;
0076         break;
0077     case USB_REQ_SET_FEATURE:
0078         if (setup->bRequestType == DEV_REQUEST) {
0079             ret_val = 0;
0080             switch (w_value) {
0081             case USB_DEVICE_REMOTE_WAKEUP:
0082                 break;
0083             case USB_DEVICE_B_HNP_ENABLE:
0084                 udc->gadget.b_hnp_enable = 1;
0085                 break;
0086             case USB_DEVICE_A_HNP_SUPPORT:
0087                 udc->gadget.a_hnp_support = 1;
0088                 break;
0089             case USB_DEVICE_A_ALT_HNP_SUPPORT:
0090                 udc->gadget.a_alt_hnp_support = 1;
0091                 break;
0092             default:
0093                 ret_val = -EOPNOTSUPP;
0094             }
0095             if (ret_val == 0) {
0096                 udc->devstatus |= (1 << w_value);
0097                 *status = 0;
0098             }
0099         } else if (setup->bRequestType == EP_REQUEST) {
0100             /* endpoint halt */
0101             ep2 = vudc_find_endpoint(udc, w_index);
0102             if (!ep2 || ep2->ep.name == udc->ep[0].ep.name) {
0103                 ret_val = -EOPNOTSUPP;
0104                 break;
0105             }
0106             ep2->halted = 1;
0107             ret_val = 0;
0108             *status = 0;
0109         }
0110         break;
0111     case USB_REQ_CLEAR_FEATURE:
0112         if (setup->bRequestType == DEV_REQUEST) {
0113             ret_val = 0;
0114             switch (w_value) {
0115             case USB_DEVICE_REMOTE_WAKEUP:
0116                 w_value = USB_DEVICE_REMOTE_WAKEUP;
0117                 break;
0118 
0119             case USB_DEVICE_U1_ENABLE:
0120             case USB_DEVICE_U2_ENABLE:
0121             case USB_DEVICE_LTM_ENABLE:
0122                 ret_val = -EOPNOTSUPP;
0123                 break;
0124             default:
0125                 ret_val = -EOPNOTSUPP;
0126                 break;
0127             }
0128             if (ret_val == 0) {
0129                 udc->devstatus &= ~(1 << w_value);
0130                 *status = 0;
0131             }
0132         } else if (setup->bRequestType == EP_REQUEST) {
0133             /* endpoint halt */
0134             ep2 = vudc_find_endpoint(udc, w_index);
0135             if (!ep2) {
0136                 ret_val = -EOPNOTSUPP;
0137                 break;
0138             }
0139             if (!ep2->wedged)
0140                 ep2->halted = 0;
0141             ret_val = 0;
0142             *status = 0;
0143         }
0144         break;
0145     case USB_REQ_GET_STATUS:
0146         if (setup->bRequestType == DEV_INREQUEST
0147                 || setup->bRequestType == INTF_INREQUEST
0148                 || setup->bRequestType == EP_INREQUEST) {
0149             char *buf;
0150             /*
0151              * device: remote wakeup, selfpowered
0152              * interface: nothing
0153              * endpoint: halt
0154              */
0155             buf = (char *)urb->transfer_buffer;
0156             if (urb->transfer_buffer_length > 0) {
0157                 if (setup->bRequestType == EP_INREQUEST) {
0158                     ep2 = vudc_find_endpoint(udc, w_index);
0159                     if (!ep2) {
0160                         ret_val = -EOPNOTSUPP;
0161                         break;
0162                     }
0163                     buf[0] = ep2->halted;
0164                 } else if (setup->bRequestType ==
0165                        DEV_INREQUEST) {
0166                     buf[0] = (u8)udc->devstatus;
0167                 } else
0168                     buf[0] = 0;
0169             }
0170             if (urb->transfer_buffer_length > 1)
0171                 buf[1] = 0;
0172             urb->actual_length = min_t(u32, 2,
0173                 urb->transfer_buffer_length);
0174             ret_val = 0;
0175             *status = 0;
0176         }
0177         break;
0178     }
0179     return ret_val;
0180 }
0181 
0182 /* Adapted from dummy_hcd.c ; caller must hold lock */
0183 static int transfer(struct vudc *udc,
0184         struct urb *urb, struct vep *ep, int limit)
0185 {
0186     struct vrequest *req;
0187     int sent = 0;
0188 top:
0189     /* if there's no request queued, the device is NAKing; return */
0190     list_for_each_entry(req, &ep->req_queue, req_entry) {
0191         unsigned int    host_len, dev_len, len;
0192         void        *ubuf_pos, *rbuf_pos;
0193         int     is_short, to_host;
0194         int     rescan = 0;
0195 
0196         /*
0197          * 1..N packets of ep->ep.maxpacket each ... the last one
0198          * may be short (including zero length).
0199          *
0200          * writer can send a zlp explicitly (length 0) or implicitly
0201          * (length mod maxpacket zero, and 'zero' flag); they always
0202          * terminate reads.
0203          */
0204         host_len = urb->transfer_buffer_length - urb->actual_length;
0205         dev_len = req->req.length - req->req.actual;
0206         len = min(host_len, dev_len);
0207 
0208         to_host = usb_pipein(urb->pipe);
0209         if (unlikely(len == 0))
0210             is_short = 1;
0211         else {
0212             /* send multiple of maxpacket first, then remainder */
0213             if (len >= ep->ep.maxpacket) {
0214                 is_short = 0;
0215                 if (len % ep->ep.maxpacket > 0)
0216                     rescan = 1;
0217                 len -= len % ep->ep.maxpacket;
0218             } else {
0219                 is_short = 1;
0220             }
0221 
0222             ubuf_pos = urb->transfer_buffer + urb->actual_length;
0223             rbuf_pos = req->req.buf + req->req.actual;
0224 
0225             if (urb->pipe & USB_DIR_IN)
0226                 memcpy(ubuf_pos, rbuf_pos, len);
0227             else
0228                 memcpy(rbuf_pos, ubuf_pos, len);
0229 
0230             urb->actual_length += len;
0231             req->req.actual += len;
0232             sent += len;
0233         }
0234 
0235         /*
0236          * short packets terminate, maybe with overflow/underflow.
0237          * it's only really an error to write too much.
0238          *
0239          * partially filling a buffer optionally blocks queue advances
0240          * (so completion handlers can clean up the queue) but we don't
0241          * need to emulate such data-in-flight.
0242          */
0243         if (is_short) {
0244             if (host_len == dev_len) {
0245                 req->req.status = 0;
0246                 urb->status = 0;
0247             } else if (to_host) {
0248                 req->req.status = 0;
0249                 if (dev_len > host_len)
0250                     urb->status = -EOVERFLOW;
0251                 else
0252                     urb->status = 0;
0253             } else {
0254                 urb->status = 0;
0255                 if (host_len > dev_len)
0256                     req->req.status = -EOVERFLOW;
0257                 else
0258                     req->req.status = 0;
0259             }
0260 
0261         /* many requests terminate without a short packet */
0262         /* also check if we need to send zlp */
0263         } else {
0264             if (req->req.length == req->req.actual) {
0265                 if (req->req.zero && to_host)
0266                     rescan = 1;
0267                 else
0268                     req->req.status = 0;
0269             }
0270             if (urb->transfer_buffer_length == urb->actual_length) {
0271                 if (urb->transfer_flags & URB_ZERO_PACKET &&
0272                     !to_host)
0273                     rescan = 1;
0274                 else
0275                     urb->status = 0;
0276             }
0277         }
0278 
0279         /* device side completion --> continuable */
0280         if (req->req.status != -EINPROGRESS) {
0281 
0282             list_del_init(&req->req_entry);
0283             spin_unlock(&udc->lock);
0284             usb_gadget_giveback_request(&ep->ep, &req->req);
0285             spin_lock(&udc->lock);
0286 
0287             /* requests might have been unlinked... */
0288             rescan = 1;
0289         }
0290 
0291         /* host side completion --> terminate */
0292         if (urb->status != -EINPROGRESS)
0293             break;
0294 
0295         /* rescan to continue with any other queued i/o */
0296         if (rescan)
0297             goto top;
0298     }
0299     return sent;
0300 }
0301 
0302 static void v_timer(struct timer_list *t)
0303 {
0304     struct vudc *udc = from_timer(udc, t, tr_timer.timer);
0305     struct transfer_timer *timer = &udc->tr_timer;
0306     struct urbp *urb_p, *tmp;
0307     unsigned long flags;
0308     struct usb_ep *_ep;
0309     struct vep *ep;
0310     int ret = 0;
0311     int total, limit;
0312 
0313     spin_lock_irqsave(&udc->lock, flags);
0314 
0315     total = get_frame_limit(udc->gadget.speed);
0316     if (total < 0) {    /* unknown speed, or not set yet */
0317         timer->state = VUDC_TR_IDLE;
0318         spin_unlock_irqrestore(&udc->lock, flags);
0319         return;
0320     }
0321     /* is it next frame now? */
0322     if (time_after(jiffies, timer->frame_start + msecs_to_jiffies(1))) {
0323         timer->frame_limit = total;
0324         /* FIXME: how to make it accurate? */
0325         timer->frame_start = jiffies;
0326     } else {
0327         total = timer->frame_limit;
0328     }
0329 
0330     /* We have to clear ep0 flags separately as it's not on the list */
0331     udc->ep[0].already_seen = 0;
0332     list_for_each_entry(_ep, &udc->gadget.ep_list, ep_list) {
0333         ep = to_vep(_ep);
0334         ep->already_seen = 0;
0335     }
0336 
0337 restart:
0338     list_for_each_entry_safe(urb_p, tmp, &udc->urb_queue, urb_entry) {
0339         struct urb *urb = urb_p->urb;
0340 
0341         ep = urb_p->ep;
0342         if (urb->unlinked)
0343             goto return_urb;
0344         if (timer->state != VUDC_TR_RUNNING)
0345             continue;
0346 
0347         if (!ep) {
0348             urb->status = -EPROTO;
0349             goto return_urb;
0350         }
0351 
0352         /* Used up bandwidth? */
0353         if (total <= 0 && ep->type == USB_ENDPOINT_XFER_BULK)
0354             continue;
0355 
0356         if (ep->already_seen)
0357             continue;
0358         ep->already_seen = 1;
0359         if (ep == &udc->ep[0] && urb_p->new) {
0360             ep->setup_stage = 1;
0361             urb_p->new = 0;
0362         }
0363         if (ep->halted && !ep->setup_stage) {
0364             urb->status = -EPIPE;
0365             goto return_urb;
0366         }
0367 
0368         if (ep == &udc->ep[0] && ep->setup_stage) {
0369             /* TODO - flush any stale requests */
0370             ep->setup_stage = 0;
0371             ep->halted = 0;
0372 
0373             ret = handle_control_request(udc, urb,
0374                 (struct usb_ctrlrequest *) urb->setup_packet,
0375                 (&urb->status));
0376             if (ret > 0) {
0377                 spin_unlock(&udc->lock);
0378                 ret = udc->driver->setup(&udc->gadget,
0379                     (struct usb_ctrlrequest *)
0380                     urb->setup_packet);
0381                 spin_lock(&udc->lock);
0382             }
0383             if (ret >= 0) {
0384                 /* no delays (max 64kb data stage) */
0385                 limit = 64 * 1024;
0386                 goto treat_control_like_bulk;
0387             } else {
0388                 urb->status = -EPIPE;
0389                 urb->actual_length = 0;
0390                 goto return_urb;
0391             }
0392         }
0393 
0394         limit = total;
0395         switch (ep->type) {
0396         case USB_ENDPOINT_XFER_ISOC:
0397             /* TODO: support */
0398             urb->status = -EXDEV;
0399             break;
0400 
0401         case USB_ENDPOINT_XFER_INT:
0402             /*
0403              * TODO: figure out bandwidth guarantees
0404              * for now, give unlimited bandwidth
0405              */
0406             limit += urb->transfer_buffer_length;
0407             fallthrough;
0408         default:
0409 treat_control_like_bulk:
0410             total -= transfer(udc, urb, ep, limit);
0411         }
0412         if (urb->status == -EINPROGRESS)
0413             continue;
0414 
0415 return_urb:
0416         if (ep)
0417             ep->already_seen = ep->setup_stage = 0;
0418 
0419         spin_lock(&udc->lock_tx);
0420         list_del(&urb_p->urb_entry);
0421         if (!urb->unlinked) {
0422             v_enqueue_ret_submit(udc, urb_p);
0423         } else {
0424             v_enqueue_ret_unlink(udc, urb_p->seqnum,
0425                          urb->unlinked);
0426             free_urbp_and_urb(urb_p);
0427         }
0428         wake_up(&udc->tx_waitq);
0429         spin_unlock(&udc->lock_tx);
0430 
0431         goto restart;
0432     }
0433 
0434     /* TODO - also wait on empty usb_request queues? */
0435     if (list_empty(&udc->urb_queue))
0436         timer->state = VUDC_TR_IDLE;
0437     else
0438         mod_timer(&timer->timer,
0439               timer->frame_start + msecs_to_jiffies(1));
0440 
0441     spin_unlock_irqrestore(&udc->lock, flags);
0442 }
0443 
0444 /* All timer functions are run with udc->lock held */
0445 
0446 void v_init_timer(struct vudc *udc)
0447 {
0448     struct transfer_timer *t = &udc->tr_timer;
0449 
0450     timer_setup(&t->timer, v_timer, 0);
0451     t->state = VUDC_TR_STOPPED;
0452 }
0453 
0454 void v_start_timer(struct vudc *udc)
0455 {
0456     struct transfer_timer *t = &udc->tr_timer;
0457 
0458     dev_dbg(&udc->pdev->dev, "timer start");
0459     switch (t->state) {
0460     case VUDC_TR_RUNNING:
0461         return;
0462     case VUDC_TR_IDLE:
0463         return v_kick_timer(udc, jiffies);
0464     case VUDC_TR_STOPPED:
0465         t->state = VUDC_TR_IDLE;
0466         t->frame_start = jiffies;
0467         t->frame_limit = get_frame_limit(udc->gadget.speed);
0468         return v_kick_timer(udc, jiffies);
0469     }
0470 }
0471 
0472 void v_kick_timer(struct vudc *udc, unsigned long time)
0473 {
0474     struct transfer_timer *t = &udc->tr_timer;
0475 
0476     dev_dbg(&udc->pdev->dev, "timer kick");
0477     switch (t->state) {
0478     case VUDC_TR_RUNNING:
0479         return;
0480     case VUDC_TR_IDLE:
0481         t->state = VUDC_TR_RUNNING;
0482         fallthrough;
0483     case VUDC_TR_STOPPED:
0484         /* we may want to kick timer to unqueue urbs */
0485         mod_timer(&t->timer, time);
0486     }
0487 }
0488 
0489 void v_stop_timer(struct vudc *udc)
0490 {
0491     struct transfer_timer *t = &udc->tr_timer;
0492 
0493     /* timer itself will take care of stopping */
0494     dev_dbg(&udc->pdev->dev, "timer stop");
0495     t->state = VUDC_TR_STOPPED;
0496 }