Lines Matching +full:dbg +full:- +full:halt
1 // SPDX-License-Identifier: GPL-2.0+
3 * inode.c -- user mode filesystem api for usb gadget controllers
5 * Copyright (C) 2003-2004 David Brownell
37 #undef DBG
49 * Key parts that must be USB-specific are protocols defining how the
55 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
60 * - Then, after a SET_CONFIGURATION control request, ep_config() is
63 * IN data or to read() OUT data. To halt the endpoint, a "wrong
85 /*----------------------------------------------------------------------*/
102 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
103 * - SETUP: read/write will transfer control data and succeed;
143 /* the rest is basically write-once */
160 refcount_inc (&data->count); in get_dev()
165 if (likely (!refcount_dec_and_test (&data->count))) in put_dev()
168 BUG_ON (waitqueue_active (&data->wait)); in put_dev()
179 dev->state = STATE_DEV_DISABLED; in dev_new()
180 refcount_set (&dev->count, 1); in dev_new()
181 spin_lock_init (&dev->lock); in dev_new()
182 INIT_LIST_HEAD (&dev->epfiles); in dev_new()
183 init_waitqueue_head (&dev->wait); in dev_new()
187 /*----------------------------------------------------------------------*/
202 /* must hold dev->lock before accessing ep or req */
215 refcount_inc (&data->count); in get_ep()
220 if (likely (!refcount_dec_and_test (&data->count))) in put_ep()
222 put_dev (data->dev); in put_ep()
224 BUG_ON (!list_empty (&data->epfiles)); in put_ep()
225 BUG_ON (waitqueue_active (&data->wait)); in put_ep()
229 /*----------------------------------------------------------------------*/
240 /*----------------------------------------------------------------------*/
246 /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
251 #define DBG(dev,fmt,args...) \ macro
254 #define DBG(dev,fmt,args...) \ macro
259 #define VDEBUG DBG
271 /*----------------------------------------------------------------------*/
275 * After opening, configure non-control endpoints. Then use normal
282 struct ep_data *epdata = ep->driver_data; in epio_complete()
284 if (!req->context) in epio_complete()
286 if (req->status) in epio_complete()
287 epdata->status = req->status; in epio_complete()
289 epdata->status = req->actual; in epio_complete()
290 complete ((struct completion *)req->context); in epio_complete()
294 * still need dev->lock to use epdata->ep.
302 if (!mutex_trylock(&epdata->lock)) in get_ready_ep()
304 if (epdata->state != STATE_EP_ENABLED && in get_ready_ep()
305 (!is_write || epdata->state != STATE_EP_READY)) { in get_ready_ep()
306 mutex_unlock(&epdata->lock); in get_ready_ep()
308 val = -EAGAIN; in get_ready_ep()
314 val = mutex_lock_interruptible(&epdata->lock); in get_ready_ep()
318 switch (epdata->state) { in get_ready_ep()
330 shortname, epdata, epdata->state); in get_ready_ep()
332 mutex_unlock(&epdata->lock); in get_ready_ep()
333 return -ENODEV; in get_ready_ep()
342 spin_lock_irq (&epdata->dev->lock); in ep_io()
343 if (likely (epdata->ep != NULL)) { in ep_io()
344 struct usb_request *req = epdata->req; in ep_io()
346 req->context = &done; in ep_io()
347 req->complete = epio_complete; in ep_io()
348 req->buf = buf; in ep_io()
349 req->length = len; in ep_io()
350 value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC); in ep_io()
352 value = -ENODEV; in ep_io()
353 spin_unlock_irq (&epdata->dev->lock); in ep_io()
358 spin_lock_irq (&epdata->dev->lock); in ep_io()
359 if (likely (epdata->ep != NULL)) { in ep_io()
360 DBG (epdata->dev, "%s i/o interrupted\n", in ep_io()
361 epdata->name); in ep_io()
362 usb_ep_dequeue (epdata->ep, epdata->req); in ep_io()
363 spin_unlock_irq (&epdata->dev->lock); in ep_io()
366 if (epdata->status == -ECONNRESET) in ep_io()
367 epdata->status = -EINTR; in ep_io()
369 spin_unlock_irq (&epdata->dev->lock); in ep_io()
371 DBG (epdata->dev, "endpoint gone\n"); in ep_io()
373 epdata->status = -ENODEV; in ep_io()
376 return epdata->status; in ep_io()
384 struct ep_data *data = fd->private_data; in ep_release()
387 value = mutex_lock_interruptible(&data->lock); in ep_release()
392 if (data->state != STATE_EP_UNBOUND) { in ep_release()
393 data->state = STATE_EP_DISABLED; in ep_release()
394 data->desc.bDescriptorType = 0; in ep_release()
395 data->hs_desc.bDescriptorType = 0; in ep_release()
396 usb_ep_disable(data->ep); in ep_release()
398 mutex_unlock(&data->lock); in ep_release()
405 struct ep_data *data = fd->private_data; in ep_ioctl()
408 if ((status = get_ready_ep (fd->f_flags, data, false)) < 0) in ep_ioctl()
411 spin_lock_irq (&data->dev->lock); in ep_ioctl()
412 if (likely (data->ep != NULL)) { in ep_ioctl()
415 status = usb_ep_fifo_status (data->ep); in ep_ioctl()
418 usb_ep_fifo_flush (data->ep); in ep_ioctl()
421 status = usb_ep_clear_halt (data->ep); in ep_ioctl()
424 status = -ENOTTY; in ep_ioctl()
427 status = -ENODEV; in ep_ioctl()
428 spin_unlock_irq (&data->dev->lock); in ep_ioctl()
429 mutex_unlock(&data->lock); in ep_ioctl()
433 /*----------------------------------------------------------------------*/
451 struct kiocb_priv *priv = iocb->private; in ep_aio_cancel()
456 epdata = priv->epdata; in ep_aio_cancel()
457 // spin_lock(&epdata->dev->lock); in ep_aio_cancel()
458 if (likely(epdata && epdata->ep && priv->req)) in ep_aio_cancel()
459 value = usb_ep_dequeue (epdata->ep, priv->req); in ep_aio_cancel()
461 value = -EINVAL; in ep_aio_cancel()
462 // spin_unlock(&epdata->dev->lock); in ep_aio_cancel()
471 struct mm_struct *mm = priv->mm; in ep_user_copy_worker()
472 struct kiocb *iocb = priv->iocb; in ep_user_copy_worker()
476 ret = copy_to_iter(priv->buf, priv->actual, &priv->to); in ep_user_copy_worker()
479 ret = -EFAULT; in ep_user_copy_worker()
482 iocb->ki_complete(iocb, ret); in ep_user_copy_worker()
484 kfree(priv->buf); in ep_user_copy_worker()
485 kfree(priv->to_free); in ep_user_copy_worker()
491 struct kiocb *iocb = req->context; in ep_aio_complete()
492 struct kiocb_priv *priv = iocb->private; in ep_aio_complete()
493 struct ep_data *epdata = priv->epdata; in ep_aio_complete()
496 spin_lock(&epdata->dev->lock); in ep_aio_complete()
497 priv->req = NULL; in ep_aio_complete()
498 priv->epdata = NULL; in ep_aio_complete()
504 if (priv->to_free == NULL || unlikely(req->actual == 0)) { in ep_aio_complete()
505 kfree(req->buf); in ep_aio_complete()
506 kfree(priv->to_free); in ep_aio_complete()
508 iocb->private = NULL; in ep_aio_complete()
509 iocb->ki_complete(iocb, in ep_aio_complete()
510 req->actual ? req->actual : (long)req->status); in ep_aio_complete()
513 if (unlikely(0 != req->status)) in ep_aio_complete()
514 DBG(epdata->dev, "%s fault %d len %d\n", in ep_aio_complete()
515 ep->name, req->status, req->actual); in ep_aio_complete()
517 priv->buf = req->buf; in ep_aio_complete()
518 priv->actual = req->actual; in ep_aio_complete()
519 INIT_WORK(&priv->work, ep_user_copy_worker); in ep_aio_complete()
520 schedule_work(&priv->work); in ep_aio_complete()
524 spin_unlock(&epdata->dev->lock); in ep_aio_complete()
537 iocb->private = priv; in ep_aio()
538 priv->iocb = iocb; in ep_aio()
542 priv->epdata = epdata; in ep_aio()
543 priv->actual = 0; in ep_aio()
544 priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */ in ep_aio()
549 spin_lock_irq(&epdata->dev->lock); in ep_aio()
550 value = -ENODEV; in ep_aio()
551 if (unlikely(epdata->ep == NULL)) in ep_aio()
554 req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC); in ep_aio()
555 value = -ENOMEM; in ep_aio()
559 priv->req = req; in ep_aio()
560 req->buf = buf; in ep_aio()
561 req->length = len; in ep_aio()
562 req->complete = ep_aio_complete; in ep_aio()
563 req->context = iocb; in ep_aio()
564 value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC); in ep_aio()
566 usb_ep_free_request(epdata->ep, req); in ep_aio()
569 spin_unlock_irq(&epdata->dev->lock); in ep_aio()
570 return -EIOCBQUEUED; in ep_aio()
573 spin_unlock_irq(&epdata->dev->lock); in ep_aio()
574 kfree(priv->to_free); in ep_aio()
583 struct file *file = iocb->ki_filp; in ep_read_iter()
584 struct ep_data *epdata = file->private_data; in ep_read_iter()
589 if ((value = get_ready_ep(file->f_flags, epdata, false)) < 0) in ep_read_iter()
592 /* halt any endpoint by doing a "wrong direction" i/o call */ in ep_read_iter()
593 if (usb_endpoint_dir_in(&epdata->desc)) { in ep_read_iter()
594 if (usb_endpoint_xfer_isoc(&epdata->desc) || in ep_read_iter()
596 mutex_unlock(&epdata->lock); in ep_read_iter()
597 return -EINVAL; in ep_read_iter()
599 DBG (epdata->dev, "%s halt\n", epdata->name); in ep_read_iter()
600 spin_lock_irq(&epdata->dev->lock); in ep_read_iter()
601 if (likely(epdata->ep != NULL)) in ep_read_iter()
602 usb_ep_set_halt(epdata->ep); in ep_read_iter()
603 spin_unlock_irq(&epdata->dev->lock); in ep_read_iter()
604 mutex_unlock(&epdata->lock); in ep_read_iter()
605 return -EBADMSG; in ep_read_iter()
610 mutex_unlock(&epdata->lock); in ep_read_iter()
611 return -ENOMEM; in ep_read_iter()
616 value = -EFAULT; in ep_read_iter()
619 value = -ENOMEM; in ep_read_iter()
622 priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL); in ep_read_iter()
623 if (!iter_is_ubuf(&priv->to) && !priv->to_free) { in ep_read_iter()
628 if (value == -EIOCBQUEUED) in ep_read_iter()
633 mutex_unlock(&epdata->lock); in ep_read_iter()
642 struct file *file = iocb->ki_filp; in ep_write_iter()
643 struct ep_data *epdata = file->private_data; in ep_write_iter()
649 if ((value = get_ready_ep(file->f_flags, epdata, true)) < 0) in ep_write_iter()
652 configured = epdata->state == STATE_EP_ENABLED; in ep_write_iter()
654 /* halt any endpoint by doing a "wrong direction" i/o call */ in ep_write_iter()
655 if (configured && !usb_endpoint_dir_in(&epdata->desc)) { in ep_write_iter()
656 if (usb_endpoint_xfer_isoc(&epdata->desc) || in ep_write_iter()
658 mutex_unlock(&epdata->lock); in ep_write_iter()
659 return -EINVAL; in ep_write_iter()
661 DBG (epdata->dev, "%s halt\n", epdata->name); in ep_write_iter()
662 spin_lock_irq(&epdata->dev->lock); in ep_write_iter()
663 if (likely(epdata->ep != NULL)) in ep_write_iter()
664 usb_ep_set_halt(epdata->ep); in ep_write_iter()
665 spin_unlock_irq(&epdata->dev->lock); in ep_write_iter()
666 mutex_unlock(&epdata->lock); in ep_write_iter()
667 return -EBADMSG; in ep_write_iter()
672 mutex_unlock(&epdata->lock); in ep_write_iter()
673 return -ENOMEM; in ep_write_iter()
677 value = -EFAULT; in ep_write_iter()
687 value = -ENOMEM; in ep_write_iter()
690 if (value == -EIOCBQUEUED) in ep_write_iter()
696 mutex_unlock(&epdata->lock); in ep_write_iter()
700 /*----------------------------------------------------------------------*/
733 if (data->state != STATE_EP_READY) { in ep_config()
734 value = -EL2HLT; in ep_config()
745 DBG(data->dev, "config %s, bad tag %d\n", data->name, tag); in ep_config()
749 len -= 4; in ep_config()
756 memcpy(&data->desc, buf, USB_DT_ENDPOINT_SIZE); in ep_config()
757 if (data->desc.bLength != USB_DT_ENDPOINT_SIZE in ep_config()
758 || data->desc.bDescriptorType != USB_DT_ENDPOINT) in ep_config()
763 memcpy(&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE, in ep_config()
765 if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE in ep_config()
766 || data->hs_desc.bDescriptorType in ep_config()
768 DBG(data->dev, "config %s, bad hs length or type\n", in ep_config()
769 data->name); in ep_config()
774 spin_lock_irq (&data->dev->lock); in ep_config()
775 if (data->dev->state == STATE_DEV_UNBOUND) { in ep_config()
776 value = -ENOENT; in ep_config()
779 ep = data->ep; in ep_config()
781 value = -ENODEV; in ep_config()
785 switch (data->dev->gadget->speed) { in ep_config()
788 ep->desc = &data->desc; in ep_config()
792 ep->desc = &data->hs_desc; in ep_config()
795 DBG(data->dev, "unconnected, %s init abandoned\n", in ep_config()
796 data->name); in ep_config()
797 value = -EINVAL; in ep_config()
802 data->state = STATE_EP_ENABLED; in ep_config()
806 spin_unlock_irq (&data->dev->lock); in ep_config()
809 data->desc.bDescriptorType = 0; in ep_config()
810 data->hs_desc.bDescriptorType = 0; in ep_config()
814 value = -EINVAL; in ep_config()
821 struct ep_data *data = inode->i_private; in ep_open()
822 int value = -EBUSY; in ep_open()
824 if (mutex_lock_interruptible(&data->lock) != 0) in ep_open()
825 return -EINTR; in ep_open()
826 spin_lock_irq (&data->dev->lock); in ep_open()
827 if (data->dev->state == STATE_DEV_UNBOUND) in ep_open()
828 value = -ENOENT; in ep_open()
829 else if (data->state == STATE_EP_DISABLED) { in ep_open()
831 data->state = STATE_EP_READY; in ep_open()
833 fd->private_data = data; in ep_open()
834 VDEBUG (data->dev, "%s ready\n", data->name); in ep_open()
836 DBG (data->dev, "%s state %d\n", in ep_open()
837 data->name, data->state); in ep_open()
838 spin_unlock_irq (&data->dev->lock); in ep_open()
839 mutex_unlock(&data->lock); in ep_open()
843 /*----------------------------------------------------------------------*/
849 * use this facility may be too simple-minded for real applications.
854 wake_up (&dev->wait); in ep0_readable()
855 kill_fasync (&dev->fasync, SIGIO, POLL_IN); in ep0_readable()
860 struct dev_data *dev = ep->driver_data; in clean_req()
862 if (req->buf != dev->rbuf) { in clean_req()
863 kfree(req->buf); in clean_req()
864 req->buf = dev->rbuf; in clean_req()
866 req->complete = epio_complete; in clean_req()
867 dev->setup_out_ready = 0; in clean_req()
872 struct dev_data *dev = ep->driver_data; in ep0_complete()
877 spin_lock_irqsave(&dev->lock, flags); in ep0_complete()
878 if (!dev->setup_in) { in ep0_complete()
879 dev->setup_out_error = (req->status != 0); in ep0_complete()
880 if (!dev->setup_out_error) in ep0_complete()
882 dev->setup_out_ready = 1; in ep0_complete()
887 if (free && req->buf != &dev->rbuf) in ep0_complete()
889 req->complete = epio_complete; in ep0_complete()
890 spin_unlock_irqrestore(&dev->lock, flags); in ep0_complete()
895 struct dev_data *dev = ep->driver_data; in setup_req()
897 if (dev->setup_out_ready) { in setup_req()
898 DBG (dev, "ep0 request busy!\n"); in setup_req()
899 return -EBUSY; in setup_req()
901 if (len > sizeof (dev->rbuf)) in setup_req()
902 req->buf = kmalloc(len, GFP_ATOMIC); in setup_req()
903 if (req->buf == NULL) { in setup_req()
904 req->buf = dev->rbuf; in setup_req()
905 return -ENOMEM; in setup_req()
907 req->complete = ep0_complete; in setup_req()
908 req->length = len; in setup_req()
909 req->zero = 0; in setup_req()
916 struct dev_data *dev = fd->private_data; in ep0_read()
920 spin_lock_irq (&dev->lock); in ep0_read()
921 if (dev->state <= STATE_DEV_OPENED) { in ep0_read()
922 retval = -EINVAL; in ep0_read()
927 if (dev->setup_abort) { in ep0_read()
928 dev->setup_abort = 0; in ep0_read()
929 retval = -EIDRM; in ep0_read()
934 if ((state = dev->state) == STATE_DEV_SETUP) { in ep0_read()
936 if (dev->setup_in) { /* stall IN */ in ep0_read()
938 (void) usb_ep_set_halt (dev->gadget->ep0); in ep0_read()
939 retval = -EL2HLT; in ep0_read()
940 dev->state = STATE_DEV_CONNECTED; in ep0_read()
943 struct usb_ep *ep = dev->gadget->ep0; in ep0_read()
944 struct usb_request *req = dev->req; in ep0_read()
947 ++dev->udc_usage; in ep0_read()
948 spin_unlock_irq (&dev->lock); in ep0_read()
950 spin_lock_irq (&dev->lock); in ep0_read()
951 --dev->udc_usage; in ep0_read()
953 dev->state = STATE_DEV_CONNECTED; in ep0_read()
956 if (dev->current_config) { in ep0_read()
959 if (gadget_is_dualspeed(dev->gadget) in ep0_read()
960 && (dev->gadget->speed in ep0_read()
962 power = dev->hs_config->bMaxPower; in ep0_read()
964 power = dev->config->bMaxPower; in ep0_read()
965 usb_gadget_vbus_draw(dev->gadget, 2 * power); in ep0_read()
969 if ((fd->f_flags & O_NONBLOCK) != 0 in ep0_read()
970 && !dev->setup_out_ready) { in ep0_read()
971 retval = -EAGAIN; in ep0_read()
974 spin_unlock_irq (&dev->lock); in ep0_read()
975 retval = wait_event_interruptible (dev->wait, in ep0_read()
976 dev->setup_out_ready != 0); in ep0_read()
979 spin_lock_irq (&dev->lock); in ep0_read()
983 if (dev->state != STATE_DEV_SETUP) { in ep0_read()
984 retval = -ECANCELED; in ep0_read()
987 dev->state = STATE_DEV_CONNECTED; in ep0_read()
989 if (dev->setup_out_error) in ep0_read()
990 retval = -EIO; in ep0_read()
992 len = min (len, (size_t)dev->req->actual); in ep0_read()
993 ++dev->udc_usage; in ep0_read()
994 spin_unlock_irq(&dev->lock); in ep0_read()
995 if (copy_to_user (buf, dev->req->buf, len)) in ep0_read()
996 retval = -EFAULT; in ep0_read()
999 spin_lock_irq(&dev->lock); in ep0_read()
1000 --dev->udc_usage; in ep0_read()
1001 clean_req (dev->gadget->ep0, dev->req); in ep0_read()
1009 if (len < sizeof dev->event [0]) { in ep0_read()
1010 retval = -EINVAL; in ep0_read()
1013 len -= len % sizeof (struct usb_gadgetfs_event); in ep0_read()
1014 dev->usermode_setup = 1; in ep0_read()
1018 if (dev->ev_next != 0) { in ep0_read()
1022 if (dev->ev_next < n) in ep0_read()
1023 n = dev->ev_next; in ep0_read()
1027 if (dev->event [i].type == GADGETFS_SETUP) { in ep0_read()
1028 dev->state = STATE_DEV_SETUP; in ep0_read()
1033 spin_unlock_irq (&dev->lock); in ep0_read()
1035 if (copy_to_user (buf, &dev->event, len)) in ep0_read()
1036 retval = -EFAULT; in ep0_read()
1043 spin_lock_irq (&dev->lock); in ep0_read()
1044 if (dev->ev_next > n) { in ep0_read()
1045 memmove(&dev->event[0], &dev->event[n], in ep0_read()
1047 * (dev->ev_next - n)); in ep0_read()
1049 dev->ev_next -= n; in ep0_read()
1050 spin_unlock_irq (&dev->lock); in ep0_read()
1054 if (fd->f_flags & O_NONBLOCK) { in ep0_read()
1055 retval = -EAGAIN; in ep0_read()
1061 DBG (dev, "fail %s, state %d\n", __func__, state); in ep0_read()
1062 retval = -ESRCH; in ep0_read()
1066 spin_unlock_irq (&dev->lock); in ep0_read()
1067 DBG (dev, "%s wait\n", __func__); in ep0_read()
1070 retval = wait_event_interruptible (dev->wait, in ep0_read()
1071 dev->ev_next != 0); in ep0_read()
1074 spin_lock_irq (&dev->lock); in ep0_read()
1079 spin_unlock_irq (&dev->lock); in ep0_read()
1092 if (dev->state == STATE_DEV_SETUP) in next_event()
1093 dev->setup_abort = 1; in next_event()
1096 dev->ev_next = 0; in next_event()
1101 for (i = 0; i != dev->ev_next; i++) { in next_event()
1102 if (dev->event [i].type != type) in next_event()
1104 DBG(dev, "discard old event[%d] %d\n", i, type); in next_event()
1105 dev->ev_next--; in next_event()
1106 if (i == dev->ev_next) in next_event()
1109 memmove (&dev->event [i], &dev->event [i + 1], in next_event()
1111 * (dev->ev_next - i)); in next_event()
1117 VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type); in next_event()
1118 event = &dev->event [dev->ev_next++]; in next_event()
1119 BUG_ON (dev->ev_next > N_EVENT); in next_event()
1121 event->type = type; in next_event()
1128 struct dev_data *dev = fd->private_data; in ep0_write()
1129 ssize_t retval = -ESRCH; in ep0_write()
1132 if (dev->setup_abort) { in ep0_write()
1133 dev->setup_abort = 0; in ep0_write()
1134 retval = -EIDRM; in ep0_write()
1137 } else if (dev->state == STATE_DEV_SETUP) { in ep0_write()
1139 len = min_t(size_t, len, dev->setup_wLength); in ep0_write()
1140 if (dev->setup_in) { in ep0_write()
1141 retval = setup_req (dev->gadget->ep0, dev->req, len); in ep0_write()
1143 dev->state = STATE_DEV_CONNECTED; in ep0_write()
1144 ++dev->udc_usage; in ep0_write()
1145 spin_unlock_irq (&dev->lock); in ep0_write()
1146 if (copy_from_user (dev->req->buf, buf, len)) in ep0_write()
1147 retval = -EFAULT; in ep0_write()
1149 if (len < dev->setup_wLength) in ep0_write()
1150 dev->req->zero = 1; in ep0_write()
1152 dev->gadget->ep0, dev->req, in ep0_write()
1155 spin_lock_irq(&dev->lock); in ep0_write()
1156 --dev->udc_usage; in ep0_write()
1158 clean_req (dev->gadget->ep0, dev->req); in ep0_write()
1166 } else if (dev->setup_can_stall) { in ep0_write()
1168 (void) usb_ep_set_halt (dev->gadget->ep0); in ep0_write()
1169 retval = -EL2HLT; in ep0_write()
1170 dev->state = STATE_DEV_CONNECTED; in ep0_write()
1172 DBG(dev, "bogus ep0out stall!\n"); in ep0_write()
1175 DBG (dev, "fail %s, state %d\n", __func__, dev->state); in ep0_write()
1183 struct dev_data *dev = fd->private_data; in ep0_fasync()
1186 return fasync_helper (f, fd, on, &dev->fasync); in ep0_fasync()
1194 struct dev_data *dev = fd->private_data; in dev_release()
1198 if (dev->gadget_registered) { in dev_release()
1200 dev->gadget_registered = false; in dev_release()
1208 kfree (dev->buf); in dev_release()
1209 dev->buf = NULL; in dev_release()
1212 spin_lock_irq(&dev->lock); in dev_release()
1213 dev->state = STATE_DEV_DISABLED; in dev_release()
1214 spin_unlock_irq(&dev->lock); in dev_release()
1223 struct dev_data *dev = fd->private_data; in ep0_poll()
1226 if (dev->state <= STATE_DEV_OPENED) in ep0_poll()
1229 poll_wait(fd, &dev->wait, wait); in ep0_poll()
1231 spin_lock_irq(&dev->lock); in ep0_poll()
1234 if (dev->setup_abort) { in ep0_poll()
1235 dev->setup_abort = 0; in ep0_poll()
1240 if (dev->state == STATE_DEV_SETUP) { in ep0_poll()
1241 if (dev->setup_in || dev->setup_can_stall) in ep0_poll()
1244 if (dev->ev_next != 0) in ep0_poll()
1248 spin_unlock_irq(&dev->lock); in ep0_poll()
1254 struct dev_data *dev = fd->private_data; in gadget_dev_ioctl()
1255 struct usb_gadget *gadget = dev->gadget; in gadget_dev_ioctl()
1256 long ret = -ENOTTY; in gadget_dev_ioctl()
1258 spin_lock_irq(&dev->lock); in gadget_dev_ioctl()
1259 if (dev->state == STATE_DEV_OPENED || in gadget_dev_ioctl()
1260 dev->state == STATE_DEV_UNBOUND) { in gadget_dev_ioctl()
1262 } else if (gadget->ops->ioctl) { in gadget_dev_ioctl()
1263 ++dev->udc_usage; in gadget_dev_ioctl()
1264 spin_unlock_irq(&dev->lock); in gadget_dev_ioctl()
1266 ret = gadget->ops->ioctl (gadget, code, value); in gadget_dev_ioctl()
1268 spin_lock_irq(&dev->lock); in gadget_dev_ioctl()
1269 --dev->udc_usage; in gadget_dev_ioctl()
1271 spin_unlock_irq(&dev->lock); in gadget_dev_ioctl()
1276 /*----------------------------------------------------------------------*/
1278 /* The in-kernel gadget driver handles most ep0 issues, in particular
1293 desc = dev->dev; in make_qualifier()
1294 qual.bDeviceClass = desc->bDeviceClass; in make_qualifier()
1295 qual.bDeviceSubClass = desc->bDeviceSubClass; in make_qualifier()
1296 qual.bDeviceProtocol = desc->bDeviceProtocol; in make_qualifier()
1299 qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket; in make_qualifier()
1304 memcpy (dev->rbuf, &qual, sizeof qual); in make_qualifier()
1315 return -EINVAL; in config_buf()
1317 if (gadget_is_dualspeed(dev->gadget)) { in config_buf()
1318 hs = (dev->gadget->speed == USB_SPEED_HIGH); in config_buf()
1323 dev->req->buf = dev->hs_config; in config_buf()
1324 len = le16_to_cpu(dev->hs_config->wTotalLength); in config_buf()
1326 dev->req->buf = dev->config; in config_buf()
1327 len = le16_to_cpu(dev->config->wTotalLength); in config_buf()
1329 ((u8 *)dev->req->buf) [1] = type; in config_buf()
1337 struct usb_request *req = dev->req; in gadgetfs_setup()
1338 int value = -EOPNOTSUPP; in gadgetfs_setup()
1340 u16 w_value = le16_to_cpu(ctrl->wValue); in gadgetfs_setup()
1341 u16 w_length = le16_to_cpu(ctrl->wLength); in gadgetfs_setup()
1344 if (ctrl->bRequestType & USB_DIR_IN) { in gadgetfs_setup()
1346 __le16 *temp = (__le16 *)&ctrl->wLength; in gadgetfs_setup()
1355 spin_lock (&dev->lock); in gadgetfs_setup()
1356 dev->setup_abort = 0; in gadgetfs_setup()
1357 if (dev->state == STATE_DEV_UNCONNECTED) { in gadgetfs_setup()
1359 && gadget->speed == USB_SPEED_HIGH in gadgetfs_setup()
1360 && dev->hs_config == NULL) { in gadgetfs_setup()
1361 spin_unlock(&dev->lock); in gadgetfs_setup()
1363 return -EINVAL; in gadgetfs_setup()
1366 dev->state = STATE_DEV_CONNECTED; in gadgetfs_setup()
1370 event->u.speed = gadget->speed; in gadgetfs_setup()
1378 } else if (dev->state == STATE_DEV_SETUP) in gadgetfs_setup()
1379 dev->setup_abort = 1; in gadgetfs_setup()
1381 req->buf = dev->rbuf; in gadgetfs_setup()
1382 req->context = NULL; in gadgetfs_setup()
1383 switch (ctrl->bRequest) { in gadgetfs_setup()
1386 if (ctrl->bRequestType != USB_DIR_IN) in gadgetfs_setup()
1391 value = min (w_length, (u16) sizeof *dev->dev); in gadgetfs_setup()
1392 dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket; in gadgetfs_setup()
1393 req->buf = dev->dev; in gadgetfs_setup()
1396 if (!dev->hs_config) in gadgetfs_setup()
1420 if (ctrl->bRequestType != 0) in gadgetfs_setup()
1424 dev->current_config = 0; in gadgetfs_setup()
1431 && gadget->speed == USB_SPEED_HIGH) { in gadgetfs_setup()
1432 config = dev->hs_config->bConfigurationValue; in gadgetfs_setup()
1433 power = dev->hs_config->bMaxPower; in gadgetfs_setup()
1435 config = dev->config->bConfigurationValue; in gadgetfs_setup()
1436 power = dev->config->bMaxPower; in gadgetfs_setup()
1441 dev->current_config = config; in gadgetfs_setup()
1456 INFO (dev, "configuration #%d\n", dev->current_config); in gadgetfs_setup()
1458 if (dev->usermode_setup) { in gadgetfs_setup()
1459 dev->setup_can_stall = 0; in gadgetfs_setup()
1468 if (ctrl->bRequestType != 0x80) in gadgetfs_setup()
1470 *(u8 *)req->buf = dev->current_config; in gadgetfs_setup()
1478 dev->usermode_setup ? "delegate" : "fail", in gadgetfs_setup()
1479 ctrl->bRequestType, ctrl->bRequest, in gadgetfs_setup()
1480 w_value, le16_to_cpu(ctrl->wIndex), w_length); in gadgetfs_setup()
1483 if (dev->usermode_setup) { in gadgetfs_setup()
1484 dev->setup_can_stall = 1; in gadgetfs_setup()
1486 dev->setup_in = (ctrl->bRequestType & USB_DIR_IN) in gadgetfs_setup()
1488 dev->setup_wLength = w_length; in gadgetfs_setup()
1489 dev->setup_out_ready = 0; in gadgetfs_setup()
1490 dev->setup_out_error = 0; in gadgetfs_setup()
1493 if (unlikely (!dev->setup_in && w_length)) { in gadgetfs_setup()
1494 value = setup_req (gadget->ep0, dev->req, in gadgetfs_setup()
1499 ++dev->udc_usage; in gadgetfs_setup()
1500 spin_unlock (&dev->lock); in gadgetfs_setup()
1501 value = usb_ep_queue (gadget->ep0, dev->req, in gadgetfs_setup()
1503 spin_lock (&dev->lock); in gadgetfs_setup()
1504 --dev->udc_usage; in gadgetfs_setup()
1506 clean_req (gadget->ep0, dev->req); in gadgetfs_setup()
1511 dev->setup_can_stall = 0; in gadgetfs_setup()
1516 event->u.setup = *ctrl; in gadgetfs_setup()
1518 spin_unlock (&dev->lock); in gadgetfs_setup()
1522 * proceeding with the status stage for 0-length in gadgetfs_setup()
1533 if (value >= 0 && dev->state != STATE_DEV_SETUP) { in gadgetfs_setup()
1534 req->length = value; in gadgetfs_setup()
1535 req->zero = value < w_length; in gadgetfs_setup()
1537 ++dev->udc_usage; in gadgetfs_setup()
1538 spin_unlock (&dev->lock); in gadgetfs_setup()
1539 value = usb_ep_queue (gadget->ep0, req, GFP_KERNEL); in gadgetfs_setup()
1540 spin_lock(&dev->lock); in gadgetfs_setup()
1541 --dev->udc_usage; in gadgetfs_setup()
1542 spin_unlock(&dev->lock); in gadgetfs_setup()
1544 DBG (dev, "ep_queue --> %d\n", value); in gadgetfs_setup()
1545 req->status = 0; in gadgetfs_setup()
1551 spin_unlock (&dev->lock); in gadgetfs_setup()
1557 DBG (dev, "%s %d\n", __func__, dev->state); in destroy_ep_files()
1559 /* dev->state must prevent interference */ in destroy_ep_files()
1560 spin_lock_irq (&dev->lock); in destroy_ep_files()
1561 while (!list_empty(&dev->epfiles)) { in destroy_ep_files()
1567 ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles); in destroy_ep_files()
1568 list_del_init (&ep->epfiles); in destroy_ep_files()
1569 spin_unlock_irq (&dev->lock); in destroy_ep_files()
1571 dentry = ep->dentry; in destroy_ep_files()
1572 ep->dentry = NULL; in destroy_ep_files()
1573 parent = d_inode(dentry->d_parent); in destroy_ep_files()
1576 mutex_lock(&ep->lock); in destroy_ep_files()
1577 if (ep->state == STATE_EP_ENABLED) in destroy_ep_files()
1578 (void) usb_ep_disable (ep->ep); in destroy_ep_files()
1579 ep->state = STATE_EP_UNBOUND; in destroy_ep_files()
1580 usb_ep_free_request (ep->ep, ep->req); in destroy_ep_files()
1581 ep->ep = NULL; in destroy_ep_files()
1582 mutex_unlock(&ep->lock); in destroy_ep_files()
1584 wake_up (&ep->wait); in destroy_ep_files()
1593 spin_lock_irq (&dev->lock); in destroy_ep_files()
1595 spin_unlock_irq (&dev->lock); in destroy_ep_files()
1608 gadget_for_each_ep (ep, dev->gadget) { in activate_ep_files()
1613 data->state = STATE_EP_DISABLED; in activate_ep_files()
1614 mutex_init(&data->lock); in activate_ep_files()
1615 init_waitqueue_head (&data->wait); in activate_ep_files()
1617 strncpy (data->name, ep->name, sizeof (data->name) - 1); in activate_ep_files()
1618 refcount_set (&data->count, 1); in activate_ep_files()
1619 data->dev = dev; in activate_ep_files()
1622 data->ep = ep; in activate_ep_files()
1623 ep->driver_data = data; in activate_ep_files()
1625 data->req = usb_ep_alloc_request (ep, GFP_KERNEL); in activate_ep_files()
1626 if (!data->req) in activate_ep_files()
1629 data->dentry = gadgetfs_create_file (dev->sb, data->name, in activate_ep_files()
1631 if (!data->dentry) in activate_ep_files()
1633 list_add_tail (&data->epfiles, &dev->epfiles); in activate_ep_files()
1638 usb_ep_free_request (ep, data->req); in activate_ep_files()
1643 DBG (dev, "%s enomem\n", __func__); in activate_ep_files()
1645 return -ENOMEM; in activate_ep_files()
1653 DBG (dev, "%s\n", __func__); in gadgetfs_unbind()
1655 spin_lock_irq (&dev->lock); in gadgetfs_unbind()
1656 dev->state = STATE_DEV_UNBOUND; in gadgetfs_unbind()
1657 while (dev->udc_usage > 0) { in gadgetfs_unbind()
1658 spin_unlock_irq(&dev->lock); in gadgetfs_unbind()
1660 spin_lock_irq(&dev->lock); in gadgetfs_unbind()
1662 spin_unlock_irq (&dev->lock); in gadgetfs_unbind()
1665 gadget->ep0->driver_data = NULL; in gadgetfs_unbind()
1669 if (dev->req) in gadgetfs_unbind()
1670 usb_ep_free_request (gadget->ep0, dev->req); in gadgetfs_unbind()
1671 DBG (dev, "%s done\n", __func__); in gadgetfs_unbind()
1683 return -ESRCH; in gadgetfs_bind()
1684 if (0 != strcmp (CHIP, gadget->name)) { in gadgetfs_bind()
1686 shortname, CHIP, gadget->name); in gadgetfs_bind()
1687 return -ENODEV; in gadgetfs_bind()
1691 dev->gadget = gadget; in gadgetfs_bind()
1692 gadget->ep0->driver_data = dev; in gadgetfs_bind()
1695 dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL); in gadgetfs_bind()
1696 if (!dev->req) in gadgetfs_bind()
1698 dev->req->context = NULL; in gadgetfs_bind()
1699 dev->req->complete = epio_complete; in gadgetfs_bind()
1704 INFO (dev, "bound to %s driver\n", gadget->name); in gadgetfs_bind()
1705 spin_lock_irq(&dev->lock); in gadgetfs_bind()
1706 dev->state = STATE_DEV_UNCONNECTED; in gadgetfs_bind()
1707 spin_unlock_irq(&dev->lock); in gadgetfs_bind()
1713 return -ENOMEM; in gadgetfs_bind()
1722 spin_lock_irqsave (&dev->lock, flags); in gadgetfs_disconnect()
1723 if (dev->state == STATE_DEV_UNCONNECTED) in gadgetfs_disconnect()
1725 dev->state = STATE_DEV_UNCONNECTED; in gadgetfs_disconnect()
1731 spin_unlock_irqrestore (&dev->lock, flags); in gadgetfs_disconnect()
1740 INFO (dev, "suspended from state %d\n", dev->state); in gadgetfs_suspend()
1741 spin_lock_irqsave(&dev->lock, flags); in gadgetfs_suspend()
1742 switch (dev->state) { in gadgetfs_suspend()
1752 spin_unlock_irqrestore(&dev->lock, flags); in gadgetfs_suspend()
1769 /*----------------------------------------------------------------------*/
1780 * would change to support features like multi-config devices
1784 * this one's optional except for high-speed hardware
1799 return config->bDescriptorType == USB_DT_CONFIG in is_valid_config()
1800 && config->bLength == USB_DT_CONFIG_SIZE in is_valid_config()
1802 && config->bConfigurationValue != 0 in is_valid_config()
1803 && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0 in is_valid_config()
1804 && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0; in is_valid_config()
1805 /* FIXME if gadget->is_otg, _must_ include an otg descriptor */ in is_valid_config()
1812 struct dev_data *dev = fd->private_data; in dev_config()
1818 spin_lock_irq(&dev->lock); in dev_config()
1819 if (dev->state > STATE_DEV_OPENED) { in dev_config()
1821 spin_unlock_irq(&dev->lock); in dev_config()
1824 spin_unlock_irq(&dev->lock); in dev_config()
1828 return -EINVAL; in dev_config()
1832 return -EFAULT; in dev_config()
1834 return -EINVAL; in dev_config()
1836 length -= 4; in dev_config()
1842 spin_lock_irq (&dev->lock); in dev_config()
1843 value = -EINVAL; in dev_config()
1844 if (dev->buf) { in dev_config()
1845 spin_unlock_irq(&dev->lock); in dev_config()
1849 dev->buf = kbuf; in dev_config()
1852 dev->config = (void *) kbuf; in dev_config()
1853 total = le16_to_cpu(dev->config->wTotalLength); in dev_config()
1854 if (!is_valid_config(dev->config, total) || in dev_config()
1855 total > length - USB_DT_DEVICE_SIZE) in dev_config()
1858 length -= total; in dev_config()
1862 dev->hs_config = (void *) kbuf; in dev_config()
1863 total = le16_to_cpu(dev->hs_config->wTotalLength); in dev_config()
1864 if (!is_valid_config(dev->hs_config, total) || in dev_config()
1865 total > length - USB_DT_DEVICE_SIZE) in dev_config()
1868 length -= total; in dev_config()
1870 dev->hs_config = NULL; in dev_config()
1878 dev->dev = (void *)kbuf; in dev_config()
1879 if (dev->dev->bLength != USB_DT_DEVICE_SIZE in dev_config()
1880 || dev->dev->bDescriptorType != USB_DT_DEVICE in dev_config()
1881 || dev->dev->bNumConfigurations != 1) in dev_config()
1883 dev->dev->bcdUSB = cpu_to_le16 (0x0200); in dev_config()
1886 spin_unlock_irq (&dev->lock); in dev_config()
1887 if (dev->hs_config) in dev_config()
1894 spin_lock_irq(&dev->lock); in dev_config()
1907 dev->gadget_registered = true; in dev_config()
1912 dev->config = NULL; in dev_config()
1913 dev->hs_config = NULL; in dev_config()
1914 dev->dev = NULL; in dev_config()
1915 spin_unlock_irq (&dev->lock); in dev_config()
1917 kfree (dev->buf); in dev_config()
1918 dev->buf = NULL; in dev_config()
1925 struct dev_data *dev = inode->i_private; in gadget_dev_open()
1926 int value = -EBUSY; in gadget_dev_open()
1928 spin_lock_irq(&dev->lock); in gadget_dev_open()
1929 if (dev->state == STATE_DEV_DISABLED) { in gadget_dev_open()
1930 dev->ev_next = 0; in gadget_dev_open()
1931 dev->state = STATE_DEV_OPENED; in gadget_dev_open()
1932 fd->private_data = dev; in gadget_dev_open()
1936 spin_unlock_irq(&dev->lock); in gadget_dev_open()
1951 /*----------------------------------------------------------------------*/
1981 inode->i_ino = get_next_ino(); in gadgetfs_make_inode()
1982 inode->i_mode = mode; in gadgetfs_make_inode()
1983 inode->i_uid = make_kuid(&init_user_ns, default_uid); in gadgetfs_make_inode()
1984 inode->i_gid = make_kgid(&init_user_ns, default_gid); in gadgetfs_make_inode()
1986 inode->i_private = data; in gadgetfs_make_inode()
1987 inode->i_fop = fops; in gadgetfs_make_inode()
1992 /* creates in fs root directory, so non-renamable and non-linkable.
2002 dentry = d_alloc_name(sb->s_root, name); in gadgetfs_create_file()
2031 rc = -ESRCH; in gadgetfs_fill_super()
2037 rc = -ENODEV; in gadgetfs_fill_super()
2042 sb->s_blocksize = PAGE_SIZE; in gadgetfs_fill_super()
2043 sb->s_blocksize_bits = PAGE_SHIFT; in gadgetfs_fill_super()
2044 sb->s_magic = GADGETFS_MAGIC; in gadgetfs_fill_super()
2045 sb->s_op = &gadget_fs_operations; in gadgetfs_fill_super()
2046 sb->s_time_gran = 1; in gadgetfs_fill_super()
2054 inode->i_op = &simple_dir_inode_operations; in gadgetfs_fill_super()
2055 if (!(sb->s_root = d_make_root (inode))) in gadgetfs_fill_super()
2065 dev->sb = sb; in gadgetfs_fill_super()
2066 dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations); in gadgetfs_fill_super()
2067 if (!dev->dentry) { in gadgetfs_fill_super()
2082 rc = -ENOMEM; in gadgetfs_fill_super()
2089 /* "mount -t gadgetfs path /dev/gadget" ends up here */
2101 fc->ops = &gadgetfs_context_ops; in gadgetfs_init_fs_context()
2119 /*----------------------------------------------------------------------*/
2129 /*----------------------------------------------------------------------*/