blob: f243744866238a21fb4e1a304726c366ffabcae0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -03002 * USB Skeleton driver - 2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
9 *
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030010 * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
Alan Stern121e2872006-07-01 22:06:36 -040011 * but has been rewritten to be easier to read and use.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/kref.h>
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -070020#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/usb.h>
Alan Stern121e2872006-07-01 22:06:36 -040022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24
25/* Define these values to match your devices */
26#define USB_SKEL_VENDOR_ID 0xfff0
27#define USB_SKEL_PRODUCT_ID 0xfff0
28
29/* table of devices that work with this driver */
Németh Márton1bd4f292010-01-10 15:33:33 +010030static const struct usb_device_id skel_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
32 { } /* Terminating entry */
33};
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030034MODULE_DEVICE_TABLE(usb, skel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
37/* Get a minor range for your devices from the usb maintainer */
38#define USB_SKEL_MINOR_BASE 192
39
Oliver Neukumff906512005-12-21 19:27:29 +010040/* our private defines. if this grows any larger, use your own .h file */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030041#define MAX_TRANSFER (PAGE_SIZE - 512)
Oliver Neukumba35e022007-03-01 14:31:02 +010042/* MAX_TRANSFER is chosen so that the VM is not stressed by
43 allocations > PAGE_SIZE and the number of packets in a page
44 is an integer 512 is the largest possible packet on EHCI */
Oliver Neukumff906512005-12-21 19:27:29 +010045#define WRITES_IN_FLIGHT 8
Oliver Neukumba35e022007-03-01 14:31:02 +010046/* arbitrarily chosen */
Oliver Neukumff906512005-12-21 19:27:29 +010047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* Structure to hold all of our device specific stuff */
49struct usb_skel {
Oliver Neukumba35e022007-03-01 14:31:02 +010050 struct usb_device *udev; /* the usb device for this device */
51 struct usb_interface *interface; /* the interface for this device */
Oliver Neukumff906512005-12-21 19:27:29 +010052 struct semaphore limit_sem; /* limiting the number of writes in progress */
Oliver Neukum403dfb52007-05-25 13:42:56 +020053 struct usb_anchor submitted; /* in case we need to retract our submissions */
Oliver Neukume7389cc2009-09-09 17:06:53 +020054 struct urb *bulk_in_urb; /* the urb to read data with */
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030055 unsigned char *bulk_in_buffer; /* the buffer to receive data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 size_t bulk_in_size; /* the size of the receive buffer */
Oliver Neukume7389cc2009-09-09 17:06:53 +020057 size_t bulk_in_filled; /* number of bytes in the buffer */
58 size_t bulk_in_copied; /* already copied to user space */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
60 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
Oliver Neukum403dfb52007-05-25 13:42:56 +020061 int errors; /* the last request tanked */
Oliver Neukume7389cc2009-09-09 17:06:53 +020062 bool ongoing_read; /* a read is going on */
Oliver Neukum403dfb52007-05-25 13:42:56 +020063 spinlock_t err_lock; /* lock for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct kref kref;
Alan Stern121e2872006-07-01 22:06:36 -040065 struct mutex io_mutex; /* synchronize I/O with disconnect */
Johan Hovold46b9de82019-10-09 19:09:42 +020066 unsigned long disconnected:1;
Du Xingc79041a2013-03-20 20:47:46 +080067 wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69#define to_skel_dev(d) container_of(d, struct usb_skel, kref)
70
71static struct usb_driver skel_driver;
Oliver Neukum403dfb52007-05-25 13:42:56 +020072static void skel_draw_down(struct usb_skel *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static void skel_delete(struct kref *kref)
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030075{
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct usb_skel *dev = to_skel_dev(kref);
77
Oliver Neukume7389cc2009-09-09 17:06:53 +020078 usb_free_urb(dev->bulk_in_urb);
Johan Hovold6bddbe72019-10-01 10:49:05 +020079 usb_put_intf(dev->interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 usb_put_dev(dev->udev);
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030081 kfree(dev->bulk_in_buffer);
82 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static int skel_open(struct inode *inode, struct file *file)
86{
87 struct usb_skel *dev;
88 struct usb_interface *interface;
89 int subminor;
90 int retval = 0;
91
92 subminor = iminor(inode);
93
94 interface = usb_find_interface(&skel_driver, subminor);
95 if (!interface) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -070096 pr_err("%s - error, can't find device for minor %d\n",
97 __func__, subminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 retval = -ENODEV;
99 goto exit;
100 }
101
102 dev = usb_get_intfdata(interface);
103 if (!dev) {
104 retval = -ENODEV;
105 goto exit;
106 }
107
Constantine Shulyupine8cebb92012-10-10 16:14:00 +0200108 retval = usb_autopm_get_interface(interface);
109 if (retval)
110 goto exit;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* increment our usage count for the device */
113 kref_get(&dev->kref);
114
115 /* save our object in the file's private structure */
116 file->private_data = dev;
117
118exit:
119 return retval;
120}
121
122static int skel_release(struct inode *inode, struct file *file)
123{
124 struct usb_skel *dev;
125
Joe Perchese53e8412010-07-12 13:50:13 -0700126 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (dev == NULL)
128 return -ENODEV;
129
Alan Stern01d883d2006-08-30 15:47:18 -0400130 /* allow the device to be autosuspended */
Johan Hovold6bddbe72019-10-01 10:49:05 +0200131 usb_autopm_put_interface(dev->interface);
Alan Stern01d883d2006-08-30 15:47:18 -0400132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* decrement the count on our device */
134 kref_put(&dev->kref, skel_delete);
135 return 0;
136}
137
Oliver Neukum403dfb52007-05-25 13:42:56 +0200138static int skel_flush(struct file *file, fl_owner_t id)
139{
140 struct usb_skel *dev;
141 int res;
142
Joe Perchese53e8412010-07-12 13:50:13 -0700143 dev = file->private_data;
Oliver Neukum403dfb52007-05-25 13:42:56 +0200144 if (dev == NULL)
145 return -ENODEV;
146
147 /* wait for io to stop */
148 mutex_lock(&dev->io_mutex);
149 skel_draw_down(dev);
150
151 /* read out errors, leave subsequent opens a clean slate */
152 spin_lock_irq(&dev->err_lock);
153 res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;
154 dev->errors = 0;
155 spin_unlock_irq(&dev->err_lock);
156
157 mutex_unlock(&dev->io_mutex);
158
159 return res;
160}
161
Oliver Neukume7389cc2009-09-09 17:06:53 +0200162static void skel_read_bulk_callback(struct urb *urb)
163{
164 struct usb_skel *dev;
165
166 dev = urb->context;
167
168 spin_lock(&dev->err_lock);
169 /* sync/async unlink faults aren't errors */
170 if (urb->status) {
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700171 if (!(urb->status == -ENOENT ||
Oliver Neukume7389cc2009-09-09 17:06:53 +0200172 urb->status == -ECONNRESET ||
173 urb->status == -ESHUTDOWN))
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700174 dev_err(&dev->interface->dev,
175 "%s - nonzero write bulk status received: %d\n",
176 __func__, urb->status);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200177
178 dev->errors = urb->status;
179 } else {
180 dev->bulk_in_filled = urb->actual_length;
181 }
182 dev->ongoing_read = 0;
183 spin_unlock(&dev->err_lock);
184
Du Xingc79041a2013-03-20 20:47:46 +0800185 wake_up_interruptible(&dev->bulk_in_wait);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200186}
187
188static int skel_do_read_io(struct usb_skel *dev, size_t count)
189{
190 int rv;
191
192 /* prepare a read */
193 usb_fill_bulk_urb(dev->bulk_in_urb,
194 dev->udev,
195 usb_rcvbulkpipe(dev->udev,
196 dev->bulk_in_endpointAddr),
197 dev->bulk_in_buffer,
198 min(dev->bulk_in_size, count),
199 skel_read_bulk_callback,
200 dev);
201 /* tell everybody to leave the URB alone */
202 spin_lock_irq(&dev->err_lock);
203 dev->ongoing_read = 1;
204 spin_unlock_irq(&dev->err_lock);
205
Du Xingc79041a2013-03-20 20:47:46 +0800206 /* submit bulk in urb, which means no data to deliver */
207 dev->bulk_in_filled = 0;
208 dev->bulk_in_copied = 0;
209
Oliver Neukume7389cc2009-09-09 17:06:53 +0200210 /* do it */
211 rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
212 if (rv < 0) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700213 dev_err(&dev->interface->dev,
214 "%s - failed submitting read urb, error %d\n",
Oliver Neukume7389cc2009-09-09 17:06:53 +0200215 __func__, rv);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200216 rv = (rv == -ENOMEM) ? rv : -EIO;
217 spin_lock_irq(&dev->err_lock);
218 dev->ongoing_read = 0;
219 spin_unlock_irq(&dev->err_lock);
220 }
221
222 return rv;
223}
224
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700225static ssize_t skel_read(struct file *file, char *buffer, size_t count,
226 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct usb_skel *dev;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200229 int rv;
230 bool ongoing_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Joe Perchese53e8412010-07-12 13:50:13 -0700232 dev = file->private_data;
Alan Stern121e2872006-07-01 22:06:36 -0400233
Oliver Neukume7389cc2009-09-09 17:06:53 +0200234 /* if we cannot read at all, return EOF */
235 if (!dev->bulk_in_urb || !count)
236 return 0;
237
238 /* no concurrent readers */
239 rv = mutex_lock_interruptible(&dev->io_mutex);
240 if (rv < 0)
241 return rv;
242
Johan Hovold46b9de82019-10-09 19:09:42 +0200243 if (dev->disconnected) { /* disconnect() was called */
Oliver Neukume7389cc2009-09-09 17:06:53 +0200244 rv = -ENODEV;
Alan Stern121e2872006-07-01 22:06:36 -0400245 goto exit;
246 }
247
Oliver Neukume7389cc2009-09-09 17:06:53 +0200248 /* if IO is under way, we must not touch things */
249retry:
250 spin_lock_irq(&dev->err_lock);
251 ongoing_io = dev->ongoing_read;
252 spin_unlock_irq(&dev->err_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Oliver Neukume7389cc2009-09-09 17:06:53 +0200254 if (ongoing_io) {
Oliver Neukum8cd01662009-09-09 17:08:50 +0200255 /* nonblocking IO shall not wait */
256 if (file->f_flags & O_NONBLOCK) {
257 rv = -EAGAIN;
258 goto exit;
259 }
Oliver Neukume7389cc2009-09-09 17:06:53 +0200260 /*
261 * IO may take forever
262 * hence wait in an interruptible state
263 */
Du Xingc79041a2013-03-20 20:47:46 +0800264 rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read));
Oliver Neukume7389cc2009-09-09 17:06:53 +0200265 if (rv < 0)
266 goto exit;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200267 }
268
269 /* errors must be reported */
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700270 rv = dev->errors;
271 if (rv < 0) {
Oliver Neukume7389cc2009-09-09 17:06:53 +0200272 /* any error is reported once */
273 dev->errors = 0;
274 /* to preserve notifications about reset */
275 rv = (rv == -EPIPE) ? rv : -EIO;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200276 /* report it */
277 goto exit;
278 }
279
280 /*
281 * if the buffer is filled we may satisfy the read
282 * else we need to start IO
283 */
284
285 if (dev->bulk_in_filled) {
286 /* we had read data */
287 size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
288 size_t chunk = min(available, count);
289
290 if (!available) {
291 /*
292 * all data has been used
293 * actual IO needs to be done
294 */
295 rv = skel_do_read_io(dev, count);
296 if (rv < 0)
297 goto exit;
298 else
299 goto retry;
300 }
301 /*
302 * data is available
303 * chunk tells us how much shall be copied
304 */
305
306 if (copy_to_user(buffer,
307 dev->bulk_in_buffer + dev->bulk_in_copied,
308 chunk))
309 rv = -EFAULT;
310 else
311 rv = chunk;
312
313 dev->bulk_in_copied += chunk;
314
315 /*
316 * if we are asked for more than we have,
317 * we start IO but don't wait
318 */
319 if (available < count)
320 skel_do_read_io(dev, count - chunk);
321 } else {
322 /* no data in the buffer */
323 rv = skel_do_read_io(dev, count);
324 if (rv < 0)
325 goto exit;
Chen Wang140983c2013-07-19 10:15:18 +0800326 else
Oliver Neukume7389cc2009-09-09 17:06:53 +0200327 goto retry;
328 }
Alan Stern121e2872006-07-01 22:06:36 -0400329exit:
330 mutex_unlock(&dev->io_mutex);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200331 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
David Howells7d12e782006-10-05 14:55:46 +0100334static void skel_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct usb_skel *dev;
337
Ming Leicdc97792008-02-24 18:41:47 +0800338 dev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* sync/async unlink faults aren't errors */
Oliver Neukum403dfb52007-05-25 13:42:56 +0200341 if (urb->status) {
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700342 if (!(urb->status == -ENOENT ||
Oliver Neukum403dfb52007-05-25 13:42:56 +0200343 urb->status == -ECONNRESET ||
344 urb->status == -ESHUTDOWN))
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700345 dev_err(&dev->interface->dev,
346 "%s - nonzero write bulk status received: %d\n",
347 __func__, urb->status);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200348
349 spin_lock(&dev->err_lock);
350 dev->errors = urb->status;
351 spin_unlock(&dev->err_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 /* free up our allocated buffer */
Daniel Mack997ea582010-04-12 13:17:25 +0200355 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
356 urb->transfer_buffer, urb->transfer_dma);
Oliver Neukumff906512005-12-21 19:27:29 +0100357 up(&dev->limit_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700360static ssize_t skel_write(struct file *file, const char *user_buffer,
361 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct usb_skel *dev;
364 int retval = 0;
365 struct urb *urb = NULL;
366 char *buf = NULL;
Sam Bishopc8dd7702005-12-22 17:11:02 -0700367 size_t writesize = min(count, (size_t)MAX_TRANSFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Joe Perchese53e8412010-07-12 13:50:13 -0700369 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* verify that we actually have some data to write */
372 if (count == 0)
373 goto exit;
374
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700375 /*
376 * limit the number of URBs in flight to stop a user from using up all
377 * RAM
378 */
Julia Lawall4de84052009-09-19 09:13:43 +0200379 if (!(file->f_flags & O_NONBLOCK)) {
Oliver Neukum79819982009-09-09 10:23:35 +0200380 if (down_interruptible(&dev->limit_sem)) {
381 retval = -ERESTARTSYS;
382 goto exit;
383 }
384 } else {
385 if (down_trylock(&dev->limit_sem)) {
386 retval = -EAGAIN;
387 goto exit;
388 }
Sam Bishopc8dd7702005-12-22 17:11:02 -0700389 }
Oliver Neukumff906512005-12-21 19:27:29 +0100390
Oliver Neukum403dfb52007-05-25 13:42:56 +0200391 spin_lock_irq(&dev->err_lock);
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700392 retval = dev->errors;
393 if (retval < 0) {
Oliver Neukum403dfb52007-05-25 13:42:56 +0200394 /* any error is reported once */
395 dev->errors = 0;
396 /* to preserve notifications about reset */
397 retval = (retval == -EPIPE) ? retval : -EIO;
398 }
399 spin_unlock_irq(&dev->err_lock);
400 if (retval < 0)
401 goto error;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* create a urb, and a buffer for it, and copy the data to the urb */
404 urb = usb_alloc_urb(0, GFP_KERNEL);
405 if (!urb) {
406 retval = -ENOMEM;
407 goto error;
408 }
409
Daniel Mack997ea582010-04-12 13:17:25 +0200410 buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
411 &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!buf) {
413 retval = -ENOMEM;
414 goto error;
415 }
416
Oliver Neukumff906512005-12-21 19:27:29 +0100417 if (copy_from_user(buf, user_buffer, writesize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 retval = -EFAULT;
419 goto error;
420 }
421
Oliver Neukumba35e022007-03-01 14:31:02 +0100422 /* this lock makes sure we don't submit URBs to gone devices */
423 mutex_lock(&dev->io_mutex);
Johan Hovold46b9de82019-10-09 19:09:42 +0200424 if (dev->disconnected) { /* disconnect() was called */
Oliver Neukumba35e022007-03-01 14:31:02 +0100425 mutex_unlock(&dev->io_mutex);
426 retval = -ENODEV;
427 goto error;
428 }
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* initialize the urb properly */
431 usb_fill_bulk_urb(urb, dev->udev,
432 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
Oliver Neukumff906512005-12-21 19:27:29 +0100433 buf, writesize, skel_write_bulk_callback, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Oliver Neukum403dfb52007-05-25 13:42:56 +0200435 usb_anchor_urb(urb, &dev->submitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* send the data out the bulk port */
438 retval = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukumba35e022007-03-01 14:31:02 +0100439 mutex_unlock(&dev->io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (retval) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700441 dev_err(&dev->interface->dev,
442 "%s - failed submitting write urb, error %d\n",
443 __func__, retval);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200444 goto error_unanchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700447 /*
448 * release our reference to this urb, the USB core will eventually free
449 * it entirely
450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 usb_free_urb(urb);
452
Oliver Neukumba35e022007-03-01 14:31:02 +0100453
Oliver Neukumff906512005-12-21 19:27:29 +0100454 return writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Oliver Neukum403dfb52007-05-25 13:42:56 +0200456error_unanchor:
457 usb_unanchor_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458error:
Alan Stern121e2872006-07-01 22:06:36 -0400459 if (urb) {
Daniel Mack997ea582010-04-12 13:17:25 +0200460 usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);
Alan Stern121e2872006-07-01 22:06:36 -0400461 usb_free_urb(urb);
462 }
Oliver Neukumff906512005-12-21 19:27:29 +0100463 up(&dev->limit_sem);
Alan Stern121e2872006-07-01 22:06:36 -0400464
465exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return retval;
467}
468
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300469static const struct file_operations skel_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 .owner = THIS_MODULE,
471 .read = skel_read,
472 .write = skel_write,
473 .open = skel_open,
474 .release = skel_release,
Oliver Neukum403dfb52007-05-25 13:42:56 +0200475 .flush = skel_flush,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200476 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477};
478
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300479/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500481 * and to have the device registered with the driver core
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483static struct usb_class_driver skel_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700484 .name = "skel%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .fops = &skel_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 .minor_base = USB_SKEL_MINOR_BASE,
487};
488
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700489static int skel_probe(struct usb_interface *interface,
490 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300492 struct usb_skel *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct usb_host_interface *iface_desc;
494 struct usb_endpoint_descriptor *endpoint;
495 size_t buffer_size;
496 int i;
497 int retval = -ENOMEM;
498
499 /* allocate memory for our device state and initialize it */
Oliver Neukumff906512005-12-21 19:27:29 +0100500 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Wolfram Sangdc0c32c2016-08-25 19:39:33 +0200501 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 kref_init(&dev->kref);
Oliver Neukumff906512005-12-21 19:27:29 +0100504 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
Alan Stern121e2872006-07-01 22:06:36 -0400505 mutex_init(&dev->io_mutex);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200506 spin_lock_init(&dev->err_lock);
507 init_usb_anchor(&dev->submitted);
Du Xingc79041a2013-03-20 20:47:46 +0800508 init_waitqueue_head(&dev->bulk_in_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 dev->udev = usb_get_dev(interface_to_usbdev(interface));
Johan Hovold6bddbe72019-10-01 10:49:05 +0200511 dev->interface = usb_get_intf(interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* set up the endpoint information */
514 /* use only the first bulk-in and bulk-out endpoints */
515 iface_desc = interface->cur_altsetting;
516 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
517 endpoint = &iface_desc->endpoint[i].desc;
518
519 if (!dev->bulk_in_endpointAddr &&
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300520 usb_endpoint_is_bulk_in(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* we found a bulk in endpoint */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700522 buffer_size = usb_endpoint_maxp(endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 dev->bulk_in_size = buffer_size;
524 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
525 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
Wolfram Sangdc0c32c2016-08-25 19:39:33 +0200526 if (!dev->bulk_in_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto error;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200528 dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sang2bd07d32016-08-11 23:14:47 +0200529 if (!dev->bulk_in_urb)
Oliver Neukume7389cc2009-09-09 17:06:53 +0200530 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
533 if (!dev->bulk_out_endpointAddr &&
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300534 usb_endpoint_is_bulk_out(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* we found a bulk out endpoint */
536 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
537 }
538 }
539 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700540 dev_err(&interface->dev,
541 "Could not find both bulk-in and bulk-out endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto error;
543 }
544
545 /* save our data pointer in this interface device */
546 usb_set_intfdata(interface, dev);
547
548 /* we can register the device now, as it is ready */
549 retval = usb_register_dev(interface, &skel_class);
550 if (retval) {
551 /* something prevented us from registering this driver */
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700552 dev_err(&interface->dev,
553 "Not able to get a minor for this device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 usb_set_intfdata(interface, NULL);
555 goto error;
556 }
557
558 /* let the user know what node this device is now attached to */
Matt Kraaia5f5ea22009-02-06 19:38:51 -0800559 dev_info(&interface->dev,
560 "USB Skeleton device now attached to USBSkel-%d",
561 interface->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
563
564error:
565 if (dev)
Oliver Neukumba35e022007-03-01 14:31:02 +0100566 /* this frees allocated memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 kref_put(&dev->kref, skel_delete);
568 return retval;
569}
570
571static void skel_disconnect(struct usb_interface *interface)
572{
573 struct usb_skel *dev;
574 int minor = interface->minor;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 dev = usb_get_intfdata(interface);
Greg Kroah-Hartman52a74992012-01-24 12:02:38 -0800577 usb_set_intfdata(interface, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* give back our minor */
580 usb_deregister_dev(interface, &skel_class);
581
Alan Stern121e2872006-07-01 22:06:36 -0400582 /* prevent more I/O from starting */
583 mutex_lock(&dev->io_mutex);
Johan Hovold46b9de82019-10-09 19:09:42 +0200584 dev->disconnected = 1;
Alan Stern121e2872006-07-01 22:06:36 -0400585 mutex_unlock(&dev->io_mutex);
586
Oliver Neukume73c7242007-06-11 14:54:02 +0200587 usb_kill_anchored_urbs(&dev->submitted);
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* decrement our usage count */
590 kref_put(&dev->kref, skel_delete);
591
Matt Kraaia5f5ea22009-02-06 19:38:51 -0800592 dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Oliver Neukum403dfb52007-05-25 13:42:56 +0200595static void skel_draw_down(struct usb_skel *dev)
596{
597 int time;
598
599 time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
600 if (!time)
601 usb_kill_anchored_urbs(&dev->submitted);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200602 usb_kill_urb(dev->bulk_in_urb);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200603}
604
Oliver Neukum758f7e162007-06-11 14:55:08 +0200605static int skel_suspend(struct usb_interface *intf, pm_message_t message)
606{
607 struct usb_skel *dev = usb_get_intfdata(intf);
608
609 if (!dev)
610 return 0;
611 skel_draw_down(dev);
612 return 0;
613}
614
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700615static int skel_resume(struct usb_interface *intf)
Oliver Neukum758f7e162007-06-11 14:55:08 +0200616{
617 return 0;
618}
619
Oliver Neukum87d093e2007-06-11 14:55:51 +0200620static int skel_pre_reset(struct usb_interface *intf)
621{
622 struct usb_skel *dev = usb_get_intfdata(intf);
623
624 mutex_lock(&dev->io_mutex);
625 skel_draw_down(dev);
626
627 return 0;
628}
629
630static int skel_post_reset(struct usb_interface *intf)
631{
632 struct usb_skel *dev = usb_get_intfdata(intf);
633
634 /* we are sure no URBs are active - no locking needed */
635 dev->errors = -EPIPE;
636 mutex_unlock(&dev->io_mutex);
637
638 return 0;
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static struct usb_driver skel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 .name = "skeleton",
643 .probe = skel_probe,
644 .disconnect = skel_disconnect,
Oliver Neukum758f7e162007-06-11 14:55:08 +0200645 .suspend = skel_suspend,
646 .resume = skel_resume,
Oliver Neukum87d093e2007-06-11 14:55:51 +0200647 .pre_reset = skel_pre_reset,
648 .post_reset = skel_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .id_table = skel_table,
Oliver Neukumba35e022007-03-01 14:31:02 +0100650 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651};
652
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800653module_usb_driver(skel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655MODULE_LICENSE("GPL");