Lines Matching +full:- +full:- +full:retry +full:- +full:all +full:- +full:errors
1 // SPDX-License-Identifier: GPL-2.0
3 * USB Skeleton driver - 2.2
5 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
7 * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
37 #define MAX_TRANSFER (PAGE_SIZE - 512)
46 /* Structure to hold all of our device specific stuff */
59 int errors; /* the last request tanked */ member
61 spinlock_t err_lock; /* lock for errors */
76 usb_free_urb(dev->bulk_in_urb); in skel_delete()
77 usb_put_intf(dev->interface); in skel_delete()
78 usb_put_dev(dev->udev); in skel_delete()
79 kfree(dev->bulk_in_buffer); in skel_delete()
94 pr_err("%s - error, can't find device for minor %d\n", in skel_open()
96 retval = -ENODEV; in skel_open()
102 retval = -ENODEV; in skel_open()
111 kref_get(&dev->kref); in skel_open()
114 file->private_data = dev; in skel_open()
124 dev = file->private_data; in skel_release()
126 return -ENODEV; in skel_release()
129 usb_autopm_put_interface(dev->interface); in skel_release()
132 kref_put(&dev->kref, skel_delete); in skel_release()
141 dev = file->private_data; in skel_flush()
143 return -ENODEV; in skel_flush()
146 mutex_lock(&dev->io_mutex); in skel_flush()
149 /* read out errors, leave subsequent opens a clean slate */ in skel_flush()
150 spin_lock_irq(&dev->err_lock); in skel_flush()
151 res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0; in skel_flush()
152 dev->errors = 0; in skel_flush()
153 spin_unlock_irq(&dev->err_lock); in skel_flush()
155 mutex_unlock(&dev->io_mutex); in skel_flush()
165 dev = urb->context; in skel_read_bulk_callback()
167 spin_lock_irqsave(&dev->err_lock, flags); in skel_read_bulk_callback()
168 /* sync/async unlink faults aren't errors */ in skel_read_bulk_callback()
169 if (urb->status) { in skel_read_bulk_callback()
170 if (!(urb->status == -ENOENT || in skel_read_bulk_callback()
171 urb->status == -ECONNRESET || in skel_read_bulk_callback()
172 urb->status == -ESHUTDOWN)) in skel_read_bulk_callback()
173 dev_err(&dev->interface->dev, in skel_read_bulk_callback()
174 "%s - nonzero write bulk status received: %d\n", in skel_read_bulk_callback()
175 __func__, urb->status); in skel_read_bulk_callback()
177 dev->errors = urb->status; in skel_read_bulk_callback()
179 dev->bulk_in_filled = urb->actual_length; in skel_read_bulk_callback()
181 dev->ongoing_read = 0; in skel_read_bulk_callback()
182 spin_unlock_irqrestore(&dev->err_lock, flags); in skel_read_bulk_callback()
184 wake_up_interruptible(&dev->bulk_in_wait); in skel_read_bulk_callback()
192 usb_fill_bulk_urb(dev->bulk_in_urb, in skel_do_read_io()
193 dev->udev, in skel_do_read_io()
194 usb_rcvbulkpipe(dev->udev, in skel_do_read_io()
195 dev->bulk_in_endpointAddr), in skel_do_read_io()
196 dev->bulk_in_buffer, in skel_do_read_io()
197 min(dev->bulk_in_size, count), in skel_do_read_io()
201 spin_lock_irq(&dev->err_lock); in skel_do_read_io()
202 dev->ongoing_read = 1; in skel_do_read_io()
203 spin_unlock_irq(&dev->err_lock); in skel_do_read_io()
206 dev->bulk_in_filled = 0; in skel_do_read_io()
207 dev->bulk_in_copied = 0; in skel_do_read_io()
210 rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL); in skel_do_read_io()
212 dev_err(&dev->interface->dev, in skel_do_read_io()
213 "%s - failed submitting read urb, error %d\n", in skel_do_read_io()
215 rv = (rv == -ENOMEM) ? rv : -EIO; in skel_do_read_io()
216 spin_lock_irq(&dev->err_lock); in skel_do_read_io()
217 dev->ongoing_read = 0; in skel_do_read_io()
218 spin_unlock_irq(&dev->err_lock); in skel_do_read_io()
231 dev = file->private_data; in skel_read()
237 rv = mutex_lock_interruptible(&dev->io_mutex); in skel_read()
241 if (dev->disconnected) { /* disconnect() was called */ in skel_read()
242 rv = -ENODEV; in skel_read()
247 retry: in skel_read()
248 spin_lock_irq(&dev->err_lock); in skel_read()
249 ongoing_io = dev->ongoing_read; in skel_read()
250 spin_unlock_irq(&dev->err_lock); in skel_read()
254 if (file->f_flags & O_NONBLOCK) { in skel_read()
255 rv = -EAGAIN; in skel_read()
262 rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read)); in skel_read()
267 /* errors must be reported */ in skel_read()
268 rv = dev->errors; in skel_read()
271 dev->errors = 0; in skel_read()
273 rv = (rv == -EPIPE) ? rv : -EIO; in skel_read()
283 if (dev->bulk_in_filled) { in skel_read()
285 size_t available = dev->bulk_in_filled - dev->bulk_in_copied; in skel_read()
290 * all data has been used in skel_read()
297 goto retry; in skel_read()
305 dev->bulk_in_buffer + dev->bulk_in_copied, in skel_read()
307 rv = -EFAULT; in skel_read()
311 dev->bulk_in_copied += chunk; in skel_read()
318 skel_do_read_io(dev, count - chunk); in skel_read()
325 goto retry; in skel_read()
328 mutex_unlock(&dev->io_mutex); in skel_read()
337 dev = urb->context; in skel_write_bulk_callback()
339 /* sync/async unlink faults aren't errors */ in skel_write_bulk_callback()
340 if (urb->status) { in skel_write_bulk_callback()
341 if (!(urb->status == -ENOENT || in skel_write_bulk_callback()
342 urb->status == -ECONNRESET || in skel_write_bulk_callback()
343 urb->status == -ESHUTDOWN)) in skel_write_bulk_callback()
344 dev_err(&dev->interface->dev, in skel_write_bulk_callback()
345 "%s - nonzero write bulk status received: %d\n", in skel_write_bulk_callback()
346 __func__, urb->status); in skel_write_bulk_callback()
348 spin_lock_irqsave(&dev->err_lock, flags); in skel_write_bulk_callback()
349 dev->errors = urb->status; in skel_write_bulk_callback()
350 spin_unlock_irqrestore(&dev->err_lock, flags); in skel_write_bulk_callback()
354 usb_free_coherent(urb->dev, urb->transfer_buffer_length, in skel_write_bulk_callback()
355 urb->transfer_buffer, urb->transfer_dma); in skel_write_bulk_callback()
356 up(&dev->limit_sem); in skel_write_bulk_callback()
368 dev = file->private_data; in skel_write()
375 * limit the number of URBs in flight to stop a user from using up all in skel_write()
378 if (!(file->f_flags & O_NONBLOCK)) { in skel_write()
379 if (down_interruptible(&dev->limit_sem)) { in skel_write()
380 retval = -ERESTARTSYS; in skel_write()
384 if (down_trylock(&dev->limit_sem)) { in skel_write()
385 retval = -EAGAIN; in skel_write()
390 spin_lock_irq(&dev->err_lock); in skel_write()
391 retval = dev->errors; in skel_write()
394 dev->errors = 0; in skel_write()
396 retval = (retval == -EPIPE) ? retval : -EIO; in skel_write()
398 spin_unlock_irq(&dev->err_lock); in skel_write()
405 retval = -ENOMEM; in skel_write()
409 buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL, in skel_write()
410 &urb->transfer_dma); in skel_write()
412 retval = -ENOMEM; in skel_write()
417 retval = -EFAULT; in skel_write()
422 mutex_lock(&dev->io_mutex); in skel_write()
423 if (dev->disconnected) { /* disconnect() was called */ in skel_write()
424 mutex_unlock(&dev->io_mutex); in skel_write()
425 retval = -ENODEV; in skel_write()
430 usb_fill_bulk_urb(urb, dev->udev, in skel_write()
431 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), in skel_write()
433 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; in skel_write()
434 usb_anchor_urb(urb, &dev->submitted); in skel_write()
438 mutex_unlock(&dev->io_mutex); in skel_write()
440 dev_err(&dev->interface->dev, in skel_write()
441 "%s - failed submitting write urb, error %d\n", in skel_write()
459 usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma); in skel_write()
462 up(&dev->limit_sem); in skel_write()
498 return -ENOMEM; in skel_probe()
500 kref_init(&dev->kref); in skel_probe()
501 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT); in skel_probe()
502 mutex_init(&dev->io_mutex); in skel_probe()
503 spin_lock_init(&dev->err_lock); in skel_probe()
504 init_usb_anchor(&dev->submitted); in skel_probe()
505 init_waitqueue_head(&dev->bulk_in_wait); in skel_probe()
507 dev->udev = usb_get_dev(interface_to_usbdev(interface)); in skel_probe()
508 dev->interface = usb_get_intf(interface); in skel_probe()
511 /* use only the first bulk-in and bulk-out endpoints */ in skel_probe()
512 retval = usb_find_common_endpoints(interface->cur_altsetting, in skel_probe()
515 dev_err(&interface->dev, in skel_probe()
516 "Could not find both bulk-in and bulk-out endpoints\n"); in skel_probe()
520 dev->bulk_in_size = usb_endpoint_maxp(bulk_in); in skel_probe()
521 dev->bulk_in_endpointAddr = bulk_in->bEndpointAddress; in skel_probe()
522 dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL); in skel_probe()
523 if (!dev->bulk_in_buffer) { in skel_probe()
524 retval = -ENOMEM; in skel_probe()
527 dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL); in skel_probe()
528 if (!dev->bulk_in_urb) { in skel_probe()
529 retval = -ENOMEM; in skel_probe()
533 dev->bulk_out_endpointAddr = bulk_out->bEndpointAddress; in skel_probe()
542 dev_err(&interface->dev, in skel_probe()
549 dev_info(&interface->dev, in skel_probe()
550 "USB Skeleton device now attached to USBSkel-%d", in skel_probe()
551 interface->minor); in skel_probe()
556 kref_put(&dev->kref, skel_delete); in skel_probe()
564 int minor = interface->minor; in skel_disconnect()
572 mutex_lock(&dev->io_mutex); in skel_disconnect()
573 dev->disconnected = 1; in skel_disconnect()
574 mutex_unlock(&dev->io_mutex); in skel_disconnect()
576 usb_kill_urb(dev->bulk_in_urb); in skel_disconnect()
577 usb_kill_anchored_urbs(&dev->submitted); in skel_disconnect()
580 kref_put(&dev->kref, skel_delete); in skel_disconnect()
582 dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor); in skel_disconnect()
589 time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000); in skel_draw_down()
591 usb_kill_anchored_urbs(&dev->submitted); in skel_draw_down()
592 usb_kill_urb(dev->bulk_in_urb); in skel_draw_down()
614 mutex_lock(&dev->io_mutex); in skel_pre_reset()
624 /* we are sure no URBs are active - no locking needed */ in skel_post_reset()
625 dev->errors = -EPIPE; in skel_post_reset()
626 mutex_unlock(&dev->io_mutex); in skel_post_reset()