blob: 2dc58766273a721cbedbd54fd28452f7ba1308b0 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -03003 * USB Skeleton driver - 2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
6 *
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -03007 * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
Alan Stern121e2872006-07-01 22:06:36 -04008 * but has been rewritten to be easier to read and use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/module.h>
15#include <linux/kref.h>
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -070016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/usb.h>
Alan Stern121e2872006-07-01 22:06:36 -040018#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20
21/* Define these values to match your devices */
22#define USB_SKEL_VENDOR_ID 0xfff0
23#define USB_SKEL_PRODUCT_ID 0xfff0
24
25/* table of devices that work with this driver */
Németh Márton1bd4f292010-01-10 15:33:33 +010026static const struct usb_device_id skel_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
28 { } /* Terminating entry */
29};
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030030MODULE_DEVICE_TABLE(usb, skel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32
33/* Get a minor range for your devices from the usb maintainer */
34#define USB_SKEL_MINOR_BASE 192
35
Oliver Neukumff906512005-12-21 19:27:29 +010036/* our private defines. if this grows any larger, use your own .h file */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030037#define MAX_TRANSFER (PAGE_SIZE - 512)
Jacob Huismana599e482019-08-15 18:42:10 +010038/*
39 * MAX_TRANSFER is chosen so that the VM is not stressed by
40 * allocations > PAGE_SIZE and the number of packets in a page
41 * is an integer 512 is the largest possible packet on EHCI
42 */
Oliver Neukumff906512005-12-21 19:27:29 +010043#define WRITES_IN_FLIGHT 8
Oliver Neukumba35e022007-03-01 14:31:02 +010044/* arbitrarily chosen */
Oliver Neukumff906512005-12-21 19:27:29 +010045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* Structure to hold all of our device specific stuff */
47struct usb_skel {
Oliver Neukumba35e022007-03-01 14:31:02 +010048 struct usb_device *udev; /* the usb device for this device */
49 struct usb_interface *interface; /* the interface for this device */
Oliver Neukumff906512005-12-21 19:27:29 +010050 struct semaphore limit_sem; /* limiting the number of writes in progress */
Oliver Neukum403dfb52007-05-25 13:42:56 +020051 struct usb_anchor submitted; /* in case we need to retract our submissions */
Oliver Neukume7389cc2009-09-09 17:06:53 +020052 struct urb *bulk_in_urb; /* the urb to read data with */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030053 unsigned char *bulk_in_buffer; /* the buffer to receive data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 size_t bulk_in_size; /* the size of the receive buffer */
Oliver Neukume7389cc2009-09-09 17:06:53 +020055 size_t bulk_in_filled; /* number of bytes in the buffer */
56 size_t bulk_in_copied; /* already copied to user space */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
58 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
Oliver Neukum403dfb52007-05-25 13:42:56 +020059 int errors; /* the last request tanked */
Oliver Neukume7389cc2009-09-09 17:06:53 +020060 bool ongoing_read; /* a read is going on */
Oliver Neukum403dfb52007-05-25 13:42:56 +020061 spinlock_t err_lock; /* lock for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct kref kref;
Alan Stern121e2872006-07-01 22:06:36 -040063 struct mutex io_mutex; /* synchronize I/O with disconnect */
Johan Hovoldbed5ef22019-10-09 19:09:42 +020064 unsigned long disconnected:1;
Du Xingc79041a2013-03-20 20:47:46 +080065 wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67#define to_skel_dev(d) container_of(d, struct usb_skel, kref)
68
69static struct usb_driver skel_driver;
Oliver Neukum403dfb52007-05-25 13:42:56 +020070static void skel_draw_down(struct usb_skel *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static void skel_delete(struct kref *kref)
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030073{
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct usb_skel *dev = to_skel_dev(kref);
75
Oliver Neukume7389cc2009-09-09 17:06:53 +020076 usb_free_urb(dev->bulk_in_urb);
Johan Hovold5c290a52019-10-01 10:49:05 +020077 usb_put_intf(dev->interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 usb_put_dev(dev->udev);
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030079 kfree(dev->bulk_in_buffer);
80 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static int skel_open(struct inode *inode, struct file *file)
84{
85 struct usb_skel *dev;
86 struct usb_interface *interface;
87 int subminor;
88 int retval = 0;
89
90 subminor = iminor(inode);
91
92 interface = usb_find_interface(&skel_driver, subminor);
93 if (!interface) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -070094 pr_err("%s - error, can't find device for minor %d\n",
95 __func__, subminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 retval = -ENODEV;
97 goto exit;
98 }
99
100 dev = usb_get_intfdata(interface);
101 if (!dev) {
102 retval = -ENODEV;
103 goto exit;
104 }
105
Constantine Shulyupine8cebb92012-10-10 16:14:00 +0200106 retval = usb_autopm_get_interface(interface);
107 if (retval)
108 goto exit;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* increment our usage count for the device */
111 kref_get(&dev->kref);
112
113 /* save our object in the file's private structure */
114 file->private_data = dev;
115
116exit:
117 return retval;
118}
119
120static int skel_release(struct inode *inode, struct file *file)
121{
122 struct usb_skel *dev;
123
Joe Perchese53e8412010-07-12 13:50:13 -0700124 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (dev == NULL)
126 return -ENODEV;
127
Alan Stern01d883d2006-08-30 15:47:18 -0400128 /* allow the device to be autosuspended */
Johan Hovold5c290a52019-10-01 10:49:05 +0200129 usb_autopm_put_interface(dev->interface);
Alan Stern01d883d2006-08-30 15:47:18 -0400130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /* decrement the count on our device */
132 kref_put(&dev->kref, skel_delete);
133 return 0;
134}
135
Oliver Neukum403dfb52007-05-25 13:42:56 +0200136static int skel_flush(struct file *file, fl_owner_t id)
137{
138 struct usb_skel *dev;
139 int res;
140
Joe Perchese53e8412010-07-12 13:50:13 -0700141 dev = file->private_data;
Oliver Neukum403dfb52007-05-25 13:42:56 +0200142 if (dev == NULL)
143 return -ENODEV;
144
145 /* wait for io to stop */
146 mutex_lock(&dev->io_mutex);
147 skel_draw_down(dev);
148
149 /* read out errors, leave subsequent opens a clean slate */
150 spin_lock_irq(&dev->err_lock);
151 res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;
152 dev->errors = 0;
153 spin_unlock_irq(&dev->err_lock);
154
155 mutex_unlock(&dev->io_mutex);
156
157 return res;
158}
159
Oliver Neukume7389cc2009-09-09 17:06:53 +0200160static void skel_read_bulk_callback(struct urb *urb)
161{
162 struct usb_skel *dev;
John Ogness8982c842018-06-25 00:08:39 +0200163 unsigned long flags;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200164
165 dev = urb->context;
166
John Ogness8982c842018-06-25 00:08:39 +0200167 spin_lock_irqsave(&dev->err_lock, flags);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200168 /* sync/async unlink faults aren't errors */
169 if (urb->status) {
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700170 if (!(urb->status == -ENOENT ||
Oliver Neukume7389cc2009-09-09 17:06:53 +0200171 urb->status == -ECONNRESET ||
172 urb->status == -ESHUTDOWN))
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700173 dev_err(&dev->interface->dev,
174 "%s - nonzero write bulk status received: %d\n",
175 __func__, urb->status);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200176
177 dev->errors = urb->status;
178 } else {
179 dev->bulk_in_filled = urb->actual_length;
180 }
181 dev->ongoing_read = 0;
John Ogness8982c842018-06-25 00:08:39 +0200182 spin_unlock_irqrestore(&dev->err_lock, flags);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200183
Du Xingc79041a2013-03-20 20:47:46 +0800184 wake_up_interruptible(&dev->bulk_in_wait);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200185}
186
187static int skel_do_read_io(struct usb_skel *dev, size_t count)
188{
189 int rv;
190
191 /* prepare a read */
192 usb_fill_bulk_urb(dev->bulk_in_urb,
193 dev->udev,
194 usb_rcvbulkpipe(dev->udev,
195 dev->bulk_in_endpointAddr),
196 dev->bulk_in_buffer,
197 min(dev->bulk_in_size, count),
198 skel_read_bulk_callback,
199 dev);
200 /* tell everybody to leave the URB alone */
201 spin_lock_irq(&dev->err_lock);
202 dev->ongoing_read = 1;
203 spin_unlock_irq(&dev->err_lock);
204
Du Xingc79041a2013-03-20 20:47:46 +0800205 /* submit bulk in urb, which means no data to deliver */
206 dev->bulk_in_filled = 0;
207 dev->bulk_in_copied = 0;
208
Oliver Neukume7389cc2009-09-09 17:06:53 +0200209 /* do it */
210 rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
211 if (rv < 0) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700212 dev_err(&dev->interface->dev,
213 "%s - failed submitting read urb, error %d\n",
Oliver Neukume7389cc2009-09-09 17:06:53 +0200214 __func__, rv);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200215 rv = (rv == -ENOMEM) ? rv : -EIO;
216 spin_lock_irq(&dev->err_lock);
217 dev->ongoing_read = 0;
218 spin_unlock_irq(&dev->err_lock);
219 }
220
221 return rv;
222}
223
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700224static ssize_t skel_read(struct file *file, char *buffer, size_t count,
225 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct usb_skel *dev;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200228 int rv;
229 bool ongoing_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Joe Perchese53e8412010-07-12 13:50:13 -0700231 dev = file->private_data;
Alan Stern121e2872006-07-01 22:06:36 -0400232
Johan Hovold369dca42019-10-09 19:09:44 +0200233 if (!count)
Oliver Neukume7389cc2009-09-09 17:06:53 +0200234 return 0;
235
236 /* no concurrent readers */
237 rv = mutex_lock_interruptible(&dev->io_mutex);
238 if (rv < 0)
239 return rv;
240
Johan Hovoldbed5ef22019-10-09 19:09:42 +0200241 if (dev->disconnected) { /* disconnect() was called */
Oliver Neukume7389cc2009-09-09 17:06:53 +0200242 rv = -ENODEV;
Alan Stern121e2872006-07-01 22:06:36 -0400243 goto exit;
244 }
245
Oliver Neukume7389cc2009-09-09 17:06:53 +0200246 /* if IO is under way, we must not touch things */
247retry:
248 spin_lock_irq(&dev->err_lock);
249 ongoing_io = dev->ongoing_read;
250 spin_unlock_irq(&dev->err_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Oliver Neukume7389cc2009-09-09 17:06:53 +0200252 if (ongoing_io) {
Oliver Neukum8cd01662009-09-09 17:08:50 +0200253 /* nonblocking IO shall not wait */
254 if (file->f_flags & O_NONBLOCK) {
255 rv = -EAGAIN;
256 goto exit;
257 }
Oliver Neukume7389cc2009-09-09 17:06:53 +0200258 /*
259 * IO may take forever
260 * hence wait in an interruptible state
261 */
Du Xingc79041a2013-03-20 20:47:46 +0800262 rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read));
Oliver Neukume7389cc2009-09-09 17:06:53 +0200263 if (rv < 0)
264 goto exit;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200265 }
266
267 /* errors must be reported */
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700268 rv = dev->errors;
269 if (rv < 0) {
Oliver Neukume7389cc2009-09-09 17:06:53 +0200270 /* any error is reported once */
271 dev->errors = 0;
272 /* to preserve notifications about reset */
273 rv = (rv == -EPIPE) ? rv : -EIO;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200274 /* report it */
275 goto exit;
276 }
277
278 /*
279 * if the buffer is filled we may satisfy the read
280 * else we need to start IO
281 */
282
283 if (dev->bulk_in_filled) {
284 /* we had read data */
285 size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
286 size_t chunk = min(available, count);
287
288 if (!available) {
289 /*
290 * all data has been used
291 * actual IO needs to be done
292 */
293 rv = skel_do_read_io(dev, count);
294 if (rv < 0)
295 goto exit;
296 else
297 goto retry;
298 }
299 /*
300 * data is available
301 * chunk tells us how much shall be copied
302 */
303
304 if (copy_to_user(buffer,
305 dev->bulk_in_buffer + dev->bulk_in_copied,
306 chunk))
307 rv = -EFAULT;
308 else
309 rv = chunk;
310
311 dev->bulk_in_copied += chunk;
312
313 /*
314 * if we are asked for more than we have,
315 * we start IO but don't wait
316 */
317 if (available < count)
318 skel_do_read_io(dev, count - chunk);
319 } else {
320 /* no data in the buffer */
321 rv = skel_do_read_io(dev, count);
322 if (rv < 0)
323 goto exit;
Chen Wang140983c2013-07-19 10:15:18 +0800324 else
Oliver Neukume7389cc2009-09-09 17:06:53 +0200325 goto retry;
326 }
Alan Stern121e2872006-07-01 22:06:36 -0400327exit:
328 mutex_unlock(&dev->io_mutex);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200329 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
David Howells7d12e782006-10-05 14:55:46 +0100332static void skel_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct usb_skel *dev;
John Ogness8982c842018-06-25 00:08:39 +0200335 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Ming Leicdc97792008-02-24 18:41:47 +0800337 dev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* sync/async unlink faults aren't errors */
Oliver Neukum403dfb52007-05-25 13:42:56 +0200340 if (urb->status) {
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700341 if (!(urb->status == -ENOENT ||
Oliver Neukum403dfb52007-05-25 13:42:56 +0200342 urb->status == -ECONNRESET ||
343 urb->status == -ESHUTDOWN))
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700344 dev_err(&dev->interface->dev,
345 "%s - nonzero write bulk status received: %d\n",
346 __func__, urb->status);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200347
John Ogness8982c842018-06-25 00:08:39 +0200348 spin_lock_irqsave(&dev->err_lock, flags);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200349 dev->errors = urb->status;
John Ogness8982c842018-06-25 00:08:39 +0200350 spin_unlock_irqrestore(&dev->err_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
353 /* free up our allocated buffer */
Daniel Mack997ea582010-04-12 13:17:25 +0200354 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
355 urb->transfer_buffer, urb->transfer_dma);
Oliver Neukumff906512005-12-21 19:27:29 +0100356 up(&dev->limit_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700359static ssize_t skel_write(struct file *file, const char *user_buffer,
360 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct usb_skel *dev;
363 int retval = 0;
364 struct urb *urb = NULL;
365 char *buf = NULL;
Sam Bishopc8dd7702005-12-22 17:11:02 -0700366 size_t writesize = min(count, (size_t)MAX_TRANSFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Joe Perchese53e8412010-07-12 13:50:13 -0700368 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* verify that we actually have some data to write */
371 if (count == 0)
372 goto exit;
373
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700374 /*
375 * limit the number of URBs in flight to stop a user from using up all
376 * RAM
377 */
Julia Lawall4de84052009-09-19 09:13:43 +0200378 if (!(file->f_flags & O_NONBLOCK)) {
Oliver Neukum79819982009-09-09 10:23:35 +0200379 if (down_interruptible(&dev->limit_sem)) {
380 retval = -ERESTARTSYS;
381 goto exit;
382 }
383 } else {
384 if (down_trylock(&dev->limit_sem)) {
385 retval = -EAGAIN;
386 goto exit;
387 }
Sam Bishopc8dd7702005-12-22 17:11:02 -0700388 }
Oliver Neukumff906512005-12-21 19:27:29 +0100389
Oliver Neukum403dfb52007-05-25 13:42:56 +0200390 spin_lock_irq(&dev->err_lock);
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700391 retval = dev->errors;
392 if (retval < 0) {
Oliver Neukum403dfb52007-05-25 13:42:56 +0200393 /* any error is reported once */
394 dev->errors = 0;
395 /* to preserve notifications about reset */
396 retval = (retval == -EPIPE) ? retval : -EIO;
397 }
398 spin_unlock_irq(&dev->err_lock);
399 if (retval < 0)
400 goto error;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* create a urb, and a buffer for it, and copy the data to the urb */
403 urb = usb_alloc_urb(0, GFP_KERNEL);
404 if (!urb) {
405 retval = -ENOMEM;
406 goto error;
407 }
408
Daniel Mack997ea582010-04-12 13:17:25 +0200409 buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
410 &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!buf) {
412 retval = -ENOMEM;
413 goto error;
414 }
415
Oliver Neukumff906512005-12-21 19:27:29 +0100416 if (copy_from_user(buf, user_buffer, writesize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 retval = -EFAULT;
418 goto error;
419 }
420
Oliver Neukumba35e022007-03-01 14:31:02 +0100421 /* this lock makes sure we don't submit URBs to gone devices */
422 mutex_lock(&dev->io_mutex);
Johan Hovoldbed5ef22019-10-09 19:09:42 +0200423 if (dev->disconnected) { /* disconnect() was called */
Oliver Neukumba35e022007-03-01 14:31:02 +0100424 mutex_unlock(&dev->io_mutex);
425 retval = -ENODEV;
426 goto error;
427 }
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* initialize the urb properly */
430 usb_fill_bulk_urb(urb, dev->udev,
431 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
Oliver Neukumff906512005-12-21 19:27:29 +0100432 buf, writesize, skel_write_bulk_callback, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Oliver Neukum403dfb52007-05-25 13:42:56 +0200434 usb_anchor_urb(urb, &dev->submitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* send the data out the bulk port */
437 retval = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukumba35e022007-03-01 14:31:02 +0100438 mutex_unlock(&dev->io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (retval) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700440 dev_err(&dev->interface->dev,
441 "%s - failed submitting write urb, error %d\n",
442 __func__, retval);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200443 goto error_unanchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700446 /*
447 * release our reference to this urb, the USB core will eventually free
448 * it entirely
449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 usb_free_urb(urb);
451
Oliver Neukumba35e022007-03-01 14:31:02 +0100452
Oliver Neukumff906512005-12-21 19:27:29 +0100453 return writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Oliver Neukum403dfb52007-05-25 13:42:56 +0200455error_unanchor:
456 usb_unanchor_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457error:
Alan Stern121e2872006-07-01 22:06:36 -0400458 if (urb) {
Daniel Mack997ea582010-04-12 13:17:25 +0200459 usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);
Alan Stern121e2872006-07-01 22:06:36 -0400460 usb_free_urb(urb);
461 }
Oliver Neukumff906512005-12-21 19:27:29 +0100462 up(&dev->limit_sem);
Alan Stern121e2872006-07-01 22:06:36 -0400463
464exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return retval;
466}
467
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300468static const struct file_operations skel_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 .owner = THIS_MODULE,
470 .read = skel_read,
471 .write = skel_write,
472 .open = skel_open,
473 .release = skel_release,
Oliver Neukum403dfb52007-05-25 13:42:56 +0200474 .flush = skel_flush,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200475 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
477
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500480 * and to have the device registered with the driver core
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
482static struct usb_class_driver skel_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700483 .name = "skel%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 .fops = &skel_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .minor_base = USB_SKEL_MINOR_BASE,
486};
487
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700488static int skel_probe(struct usb_interface *interface,
489 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300491 struct usb_skel *dev;
Johan Hovoldaf59f892017-03-17 11:35:47 +0100492 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
493 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* allocate memory for our device state and initialize it */
Oliver Neukumff906512005-12-21 19:27:29 +0100496 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Wolfram Sangdc0c32c2016-08-25 19:39:33 +0200497 if (!dev)
Johan Hovoldaf59f892017-03-17 11:35:47 +0100498 return -ENOMEM;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 kref_init(&dev->kref);
Oliver Neukumff906512005-12-21 19:27:29 +0100501 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
Alan Stern121e2872006-07-01 22:06:36 -0400502 mutex_init(&dev->io_mutex);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200503 spin_lock_init(&dev->err_lock);
504 init_usb_anchor(&dev->submitted);
Du Xingc79041a2013-03-20 20:47:46 +0800505 init_waitqueue_head(&dev->bulk_in_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 dev->udev = usb_get_dev(interface_to_usbdev(interface));
Johan Hovold5c290a52019-10-01 10:49:05 +0200508 dev->interface = usb_get_intf(interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* set up the endpoint information */
511 /* use only the first bulk-in and bulk-out endpoints */
Johan Hovoldaf59f892017-03-17 11:35:47 +0100512 retval = usb_find_common_endpoints(interface->cur_altsetting,
513 &bulk_in, &bulk_out, NULL, NULL);
514 if (retval) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700515 dev_err(&interface->dev,
516 "Could not find both bulk-in and bulk-out endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto error;
518 }
519
Johan Hovoldaf59f892017-03-17 11:35:47 +0100520 dev->bulk_in_size = usb_endpoint_maxp(bulk_in);
521 dev->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
522 dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL);
523 if (!dev->bulk_in_buffer) {
524 retval = -ENOMEM;
525 goto error;
526 }
527 dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
528 if (!dev->bulk_in_urb) {
529 retval = -ENOMEM;
530 goto error;
531 }
532
533 dev->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* save our data pointer in this interface device */
536 usb_set_intfdata(interface, dev);
537
538 /* we can register the device now, as it is ready */
539 retval = usb_register_dev(interface, &skel_class);
540 if (retval) {
541 /* something prevented us from registering this driver */
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700542 dev_err(&interface->dev,
543 "Not able to get a minor for this device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 usb_set_intfdata(interface, NULL);
545 goto error;
546 }
547
548 /* let the user know what node this device is now attached to */
Matt Kraaia5f5ea22009-02-06 19:38:51 -0800549 dev_info(&interface->dev,
550 "USB Skeleton device now attached to USBSkel-%d",
551 interface->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 0;
553
554error:
Johan Hovoldaf59f892017-03-17 11:35:47 +0100555 /* this frees allocated memory */
556 kref_put(&dev->kref, skel_delete);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return retval;
559}
560
561static void skel_disconnect(struct usb_interface *interface)
562{
563 struct usb_skel *dev;
564 int minor = interface->minor;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dev = usb_get_intfdata(interface);
Greg Kroah-Hartman52a74992012-01-24 12:02:38 -0800567 usb_set_intfdata(interface, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* give back our minor */
570 usb_deregister_dev(interface, &skel_class);
571
Alan Stern121e2872006-07-01 22:06:36 -0400572 /* prevent more I/O from starting */
573 mutex_lock(&dev->io_mutex);
Johan Hovoldbed5ef22019-10-09 19:09:42 +0200574 dev->disconnected = 1;
Alan Stern121e2872006-07-01 22:06:36 -0400575 mutex_unlock(&dev->io_mutex);
576
Johan Hovold63530012019-10-09 19:09:43 +0200577 usb_kill_urb(dev->bulk_in_urb);
Oliver Neukume73c7242007-06-11 14:54:02 +0200578 usb_kill_anchored_urbs(&dev->submitted);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* decrement our usage count */
581 kref_put(&dev->kref, skel_delete);
582
Matt Kraaia5f5ea22009-02-06 19:38:51 -0800583 dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Oliver Neukum403dfb52007-05-25 13:42:56 +0200586static void skel_draw_down(struct usb_skel *dev)
587{
588 int time;
589
590 time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
591 if (!time)
592 usb_kill_anchored_urbs(&dev->submitted);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200593 usb_kill_urb(dev->bulk_in_urb);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200594}
595
Oliver Neukum758f7e162007-06-11 14:55:08 +0200596static int skel_suspend(struct usb_interface *intf, pm_message_t message)
597{
598 struct usb_skel *dev = usb_get_intfdata(intf);
599
600 if (!dev)
601 return 0;
602 skel_draw_down(dev);
603 return 0;
604}
605
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700606static int skel_resume(struct usb_interface *intf)
Oliver Neukum758f7e162007-06-11 14:55:08 +0200607{
608 return 0;
609}
610
Oliver Neukum87d093e2007-06-11 14:55:51 +0200611static int skel_pre_reset(struct usb_interface *intf)
612{
613 struct usb_skel *dev = usb_get_intfdata(intf);
614
615 mutex_lock(&dev->io_mutex);
616 skel_draw_down(dev);
617
618 return 0;
619}
620
621static int skel_post_reset(struct usb_interface *intf)
622{
623 struct usb_skel *dev = usb_get_intfdata(intf);
624
625 /* we are sure no URBs are active - no locking needed */
626 dev->errors = -EPIPE;
627 mutex_unlock(&dev->io_mutex);
628
629 return 0;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static struct usb_driver skel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 .name = "skeleton",
634 .probe = skel_probe,
635 .disconnect = skel_disconnect,
Oliver Neukum758f7e162007-06-11 14:55:08 +0200636 .suspend = skel_suspend,
637 .resume = skel_resume,
Oliver Neukum87d093e2007-06-11 14:55:51 +0200638 .pre_reset = skel_pre_reset,
639 .post_reset = skel_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .id_table = skel_table,
Oliver Neukumba35e022007-03-01 14:31:02 +0100641 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642};
643
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800644module_usb_driver(skel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Marcus Folkessone4c48352018-03-01 08:55:53 +0100646MODULE_LICENSE("GPL v2");