blob: bb0bd732e29ab7369fb3865a4682a596b5f4459a [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 */
Du Xingc79041a2013-03-20 20:47:46 +080066 wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68#define to_skel_dev(d) container_of(d, struct usb_skel, kref)
69
70static struct usb_driver skel_driver;
Oliver Neukum403dfb52007-05-25 13:42:56 +020071static void skel_draw_down(struct usb_skel *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static void skel_delete(struct kref *kref)
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -030074{
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct usb_skel *dev = to_skel_dev(kref);
76
Oliver Neukume7389cc2009-09-09 17:06:53 +020077 usb_free_urb(dev->bulk_in_urb);
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 */
129 mutex_lock(&dev->io_mutex);
Ming Leie28dbb02011-12-16 22:20:44 +0800130 if (dev->interface)
Alan Stern01d883d2006-08-30 15:47:18 -0400131 usb_autopm_put_interface(dev->interface);
132 mutex_unlock(&dev->io_mutex);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* decrement the count on our device */
135 kref_put(&dev->kref, skel_delete);
136 return 0;
137}
138
Oliver Neukum403dfb52007-05-25 13:42:56 +0200139static int skel_flush(struct file *file, fl_owner_t id)
140{
141 struct usb_skel *dev;
142 int res;
143
Joe Perchese53e8412010-07-12 13:50:13 -0700144 dev = file->private_data;
Oliver Neukum403dfb52007-05-25 13:42:56 +0200145 if (dev == NULL)
146 return -ENODEV;
147
148 /* wait for io to stop */
149 mutex_lock(&dev->io_mutex);
150 skel_draw_down(dev);
151
152 /* read out errors, leave subsequent opens a clean slate */
153 spin_lock_irq(&dev->err_lock);
154 res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;
155 dev->errors = 0;
156 spin_unlock_irq(&dev->err_lock);
157
158 mutex_unlock(&dev->io_mutex);
159
160 return res;
161}
162
Oliver Neukume7389cc2009-09-09 17:06:53 +0200163static void skel_read_bulk_callback(struct urb *urb)
164{
165 struct usb_skel *dev;
166
167 dev = urb->context;
168
169 spin_lock(&dev->err_lock);
170 /* sync/async unlink faults aren't errors */
171 if (urb->status) {
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700172 if (!(urb->status == -ENOENT ||
Oliver Neukume7389cc2009-09-09 17:06:53 +0200173 urb->status == -ECONNRESET ||
174 urb->status == -ESHUTDOWN))
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700175 dev_err(&dev->interface->dev,
176 "%s - nonzero write bulk status received: %d\n",
177 __func__, urb->status);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200178
179 dev->errors = urb->status;
180 } else {
181 dev->bulk_in_filled = urb->actual_length;
182 }
183 dev->ongoing_read = 0;
184 spin_unlock(&dev->err_lock);
185
Du Xingc79041a2013-03-20 20:47:46 +0800186 wake_up_interruptible(&dev->bulk_in_wait);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200187}
188
189static int skel_do_read_io(struct usb_skel *dev, size_t count)
190{
191 int rv;
192
193 /* prepare a read */
194 usb_fill_bulk_urb(dev->bulk_in_urb,
195 dev->udev,
196 usb_rcvbulkpipe(dev->udev,
197 dev->bulk_in_endpointAddr),
198 dev->bulk_in_buffer,
199 min(dev->bulk_in_size, count),
200 skel_read_bulk_callback,
201 dev);
202 /* tell everybody to leave the URB alone */
203 spin_lock_irq(&dev->err_lock);
204 dev->ongoing_read = 1;
205 spin_unlock_irq(&dev->err_lock);
206
Du Xingc79041a2013-03-20 20:47:46 +0800207 /* submit bulk in urb, which means no data to deliver */
208 dev->bulk_in_filled = 0;
209 dev->bulk_in_copied = 0;
210
Oliver Neukume7389cc2009-09-09 17:06:53 +0200211 /* do it */
212 rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
213 if (rv < 0) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700214 dev_err(&dev->interface->dev,
215 "%s - failed submitting read urb, error %d\n",
Oliver Neukume7389cc2009-09-09 17:06:53 +0200216 __func__, rv);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200217 rv = (rv == -ENOMEM) ? rv : -EIO;
218 spin_lock_irq(&dev->err_lock);
219 dev->ongoing_read = 0;
220 spin_unlock_irq(&dev->err_lock);
221 }
222
223 return rv;
224}
225
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700226static ssize_t skel_read(struct file *file, char *buffer, size_t count,
227 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct usb_skel *dev;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200230 int rv;
231 bool ongoing_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Joe Perchese53e8412010-07-12 13:50:13 -0700233 dev = file->private_data;
Alan Stern121e2872006-07-01 22:06:36 -0400234
Oliver Neukume7389cc2009-09-09 17:06:53 +0200235 /* if we cannot read at all, return EOF */
236 if (!dev->bulk_in_urb || !count)
237 return 0;
238
239 /* no concurrent readers */
240 rv = mutex_lock_interruptible(&dev->io_mutex);
241 if (rv < 0)
242 return rv;
243
Alan Stern121e2872006-07-01 22:06:36 -0400244 if (!dev->interface) { /* disconnect() was called */
Oliver Neukume7389cc2009-09-09 17:06:53 +0200245 rv = -ENODEV;
Alan Stern121e2872006-07-01 22:06:36 -0400246 goto exit;
247 }
248
Oliver Neukume7389cc2009-09-09 17:06:53 +0200249 /* if IO is under way, we must not touch things */
250retry:
251 spin_lock_irq(&dev->err_lock);
252 ongoing_io = dev->ongoing_read;
253 spin_unlock_irq(&dev->err_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Oliver Neukume7389cc2009-09-09 17:06:53 +0200255 if (ongoing_io) {
Oliver Neukum8cd01662009-09-09 17:08:50 +0200256 /* nonblocking IO shall not wait */
257 if (file->f_flags & O_NONBLOCK) {
258 rv = -EAGAIN;
259 goto exit;
260 }
Oliver Neukume7389cc2009-09-09 17:06:53 +0200261 /*
262 * IO may take forever
263 * hence wait in an interruptible state
264 */
Du Xingc79041a2013-03-20 20:47:46 +0800265 rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read));
Oliver Neukume7389cc2009-09-09 17:06:53 +0200266 if (rv < 0)
267 goto exit;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200268 }
269
270 /* errors must be reported */
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700271 rv = dev->errors;
272 if (rv < 0) {
Oliver Neukume7389cc2009-09-09 17:06:53 +0200273 /* any error is reported once */
274 dev->errors = 0;
275 /* to preserve notifications about reset */
276 rv = (rv == -EPIPE) ? rv : -EIO;
Oliver Neukume7389cc2009-09-09 17:06:53 +0200277 /* report it */
278 goto exit;
279 }
280
281 /*
282 * if the buffer is filled we may satisfy the read
283 * else we need to start IO
284 */
285
286 if (dev->bulk_in_filled) {
287 /* we had read data */
288 size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
289 size_t chunk = min(available, count);
290
291 if (!available) {
292 /*
293 * all data has been used
294 * actual IO needs to be done
295 */
296 rv = skel_do_read_io(dev, count);
297 if (rv < 0)
298 goto exit;
299 else
300 goto retry;
301 }
302 /*
303 * data is available
304 * chunk tells us how much shall be copied
305 */
306
307 if (copy_to_user(buffer,
308 dev->bulk_in_buffer + dev->bulk_in_copied,
309 chunk))
310 rv = -EFAULT;
311 else
312 rv = chunk;
313
314 dev->bulk_in_copied += chunk;
315
316 /*
317 * if we are asked for more than we have,
318 * we start IO but don't wait
319 */
320 if (available < count)
321 skel_do_read_io(dev, count - chunk);
322 } else {
323 /* no data in the buffer */
324 rv = skel_do_read_io(dev, count);
325 if (rv < 0)
326 goto exit;
Chen Wang140983c2013-07-19 10:15:18 +0800327 else
Oliver Neukume7389cc2009-09-09 17:06:53 +0200328 goto retry;
329 }
Alan Stern121e2872006-07-01 22:06:36 -0400330exit:
331 mutex_unlock(&dev->io_mutex);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200332 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
David Howells7d12e782006-10-05 14:55:46 +0100335static void skel_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct usb_skel *dev;
338
Ming Leicdc97792008-02-24 18:41:47 +0800339 dev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* sync/async unlink faults aren't errors */
Oliver Neukum403dfb52007-05-25 13:42:56 +0200342 if (urb->status) {
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700343 if (!(urb->status == -ENOENT ||
Oliver Neukum403dfb52007-05-25 13:42:56 +0200344 urb->status == -ECONNRESET ||
345 urb->status == -ESHUTDOWN))
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700346 dev_err(&dev->interface->dev,
347 "%s - nonzero write bulk status received: %d\n",
348 __func__, urb->status);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200349
350 spin_lock(&dev->err_lock);
351 dev->errors = urb->status;
352 spin_unlock(&dev->err_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 /* free up our allocated buffer */
Daniel Mack997ea582010-04-12 13:17:25 +0200356 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
357 urb->transfer_buffer, urb->transfer_dma);
Oliver Neukumff906512005-12-21 19:27:29 +0100358 up(&dev->limit_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700361static ssize_t skel_write(struct file *file, const char *user_buffer,
362 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct usb_skel *dev;
365 int retval = 0;
366 struct urb *urb = NULL;
367 char *buf = NULL;
Sam Bishopc8dd7702005-12-22 17:11:02 -0700368 size_t writesize = min(count, (size_t)MAX_TRANSFER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Joe Perchese53e8412010-07-12 13:50:13 -0700370 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* verify that we actually have some data to write */
373 if (count == 0)
374 goto exit;
375
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700376 /*
377 * limit the number of URBs in flight to stop a user from using up all
378 * RAM
379 */
Julia Lawall4de84052009-09-19 09:13:43 +0200380 if (!(file->f_flags & O_NONBLOCK)) {
Oliver Neukum79819982009-09-09 10:23:35 +0200381 if (down_interruptible(&dev->limit_sem)) {
382 retval = -ERESTARTSYS;
383 goto exit;
384 }
385 } else {
386 if (down_trylock(&dev->limit_sem)) {
387 retval = -EAGAIN;
388 goto exit;
389 }
Sam Bishopc8dd7702005-12-22 17:11:02 -0700390 }
Oliver Neukumff906512005-12-21 19:27:29 +0100391
Oliver Neukum403dfb52007-05-25 13:42:56 +0200392 spin_lock_irq(&dev->err_lock);
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700393 retval = dev->errors;
394 if (retval < 0) {
Oliver Neukum403dfb52007-05-25 13:42:56 +0200395 /* any error is reported once */
396 dev->errors = 0;
397 /* to preserve notifications about reset */
398 retval = (retval == -EPIPE) ? retval : -EIO;
399 }
400 spin_unlock_irq(&dev->err_lock);
401 if (retval < 0)
402 goto error;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* create a urb, and a buffer for it, and copy the data to the urb */
405 urb = usb_alloc_urb(0, GFP_KERNEL);
406 if (!urb) {
407 retval = -ENOMEM;
408 goto error;
409 }
410
Daniel Mack997ea582010-04-12 13:17:25 +0200411 buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
412 &urb->transfer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (!buf) {
414 retval = -ENOMEM;
415 goto error;
416 }
417
Oliver Neukumff906512005-12-21 19:27:29 +0100418 if (copy_from_user(buf, user_buffer, writesize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 retval = -EFAULT;
420 goto error;
421 }
422
Oliver Neukumba35e022007-03-01 14:31:02 +0100423 /* this lock makes sure we don't submit URBs to gone devices */
424 mutex_lock(&dev->io_mutex);
425 if (!dev->interface) { /* disconnect() was called */
426 mutex_unlock(&dev->io_mutex);
427 retval = -ENODEV;
428 goto error;
429 }
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* initialize the urb properly */
432 usb_fill_bulk_urb(urb, dev->udev,
433 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
Oliver Neukumff906512005-12-21 19:27:29 +0100434 buf, writesize, skel_write_bulk_callback, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Oliver Neukum403dfb52007-05-25 13:42:56 +0200436 usb_anchor_urb(urb, &dev->submitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* send the data out the bulk port */
439 retval = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukumba35e022007-03-01 14:31:02 +0100440 mutex_unlock(&dev->io_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (retval) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700442 dev_err(&dev->interface->dev,
443 "%s - failed submitting write urb, error %d\n",
444 __func__, retval);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200445 goto error_unanchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700448 /*
449 * release our reference to this urb, the USB core will eventually free
450 * it entirely
451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 usb_free_urb(urb);
453
Oliver Neukumba35e022007-03-01 14:31:02 +0100454
Oliver Neukumff906512005-12-21 19:27:29 +0100455 return writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Oliver Neukum403dfb52007-05-25 13:42:56 +0200457error_unanchor:
458 usb_unanchor_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459error:
Alan Stern121e2872006-07-01 22:06:36 -0400460 if (urb) {
Daniel Mack997ea582010-04-12 13:17:25 +0200461 usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);
Alan Stern121e2872006-07-01 22:06:36 -0400462 usb_free_urb(urb);
463 }
Oliver Neukumff906512005-12-21 19:27:29 +0100464 up(&dev->limit_sem);
Alan Stern121e2872006-07-01 22:06:36 -0400465
466exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return retval;
468}
469
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300470static const struct file_operations skel_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 .owner = THIS_MODULE,
472 .read = skel_read,
473 .write = skel_write,
474 .open = skel_open,
475 .release = skel_release,
Oliver Neukum403dfb52007-05-25 13:42:56 +0200476 .flush = skel_flush,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200477 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478};
479
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300480/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartman595b14c2006-01-18 17:36:58 -0500482 * and to have the device registered with the driver core
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
484static struct usb_class_driver skel_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700485 .name = "skel%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 .fops = &skel_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 .minor_base = USB_SKEL_MINOR_BASE,
488};
489
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700490static int skel_probe(struct usb_interface *interface,
491 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Luiz Fernando N. Capitulinoc0704542006-08-14 22:44:29 -0300493 struct usb_skel *dev;
Johan Hovoldaf59f892017-03-17 11:35:47 +0100494 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
495 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* allocate memory for our device state and initialize it */
Oliver Neukumff906512005-12-21 19:27:29 +0100498 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Wolfram Sangdc0c32c2016-08-25 19:39:33 +0200499 if (!dev)
Johan Hovoldaf59f892017-03-17 11:35:47 +0100500 return -ENOMEM;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 kref_init(&dev->kref);
Oliver Neukumff906512005-12-21 19:27:29 +0100503 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
Alan Stern121e2872006-07-01 22:06:36 -0400504 mutex_init(&dev->io_mutex);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200505 spin_lock_init(&dev->err_lock);
506 init_usb_anchor(&dev->submitted);
Du Xingc79041a2013-03-20 20:47:46 +0800507 init_waitqueue_head(&dev->bulk_in_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 dev->udev = usb_get_dev(interface_to_usbdev(interface));
510 dev->interface = interface;
511
512 /* set up the endpoint information */
513 /* use only the first bulk-in and bulk-out endpoints */
Johan Hovoldaf59f892017-03-17 11:35:47 +0100514 retval = usb_find_common_endpoints(interface->cur_altsetting,
515 &bulk_in, &bulk_out, NULL, NULL);
516 if (retval) {
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700517 dev_err(&interface->dev,
518 "Could not find both bulk-in and bulk-out endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto error;
520 }
521
Johan Hovoldaf59f892017-03-17 11:35:47 +0100522 dev->bulk_in_size = usb_endpoint_maxp(bulk_in);
523 dev->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
524 dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL);
525 if (!dev->bulk_in_buffer) {
526 retval = -ENOMEM;
527 goto error;
528 }
529 dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
530 if (!dev->bulk_in_urb) {
531 retval = -ENOMEM;
532 goto error;
533 }
534
535 dev->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* save our data pointer in this interface device */
538 usb_set_intfdata(interface, dev);
539
540 /* we can register the device now, as it is ready */
541 retval = usb_register_dev(interface, &skel_class);
542 if (retval) {
543 /* something prevented us from registering this driver */
Greg Kroah-Hartman4212cd72012-04-27 11:24:45 -0700544 dev_err(&interface->dev,
545 "Not able to get a minor for this device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 usb_set_intfdata(interface, NULL);
547 goto error;
548 }
549
550 /* let the user know what node this device is now attached to */
Matt Kraaia5f5ea22009-02-06 19:38:51 -0800551 dev_info(&interface->dev,
552 "USB Skeleton device now attached to USBSkel-%d",
553 interface->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555
556error:
Johan Hovoldaf59f892017-03-17 11:35:47 +0100557 /* this frees allocated memory */
558 kref_put(&dev->kref, skel_delete);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return retval;
561}
562
563static void skel_disconnect(struct usb_interface *interface)
564{
565 struct usb_skel *dev;
566 int minor = interface->minor;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 dev = usb_get_intfdata(interface);
Greg Kroah-Hartman52a74992012-01-24 12:02:38 -0800569 usb_set_intfdata(interface, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* give back our minor */
572 usb_deregister_dev(interface, &skel_class);
573
Alan Stern121e2872006-07-01 22:06:36 -0400574 /* prevent more I/O from starting */
575 mutex_lock(&dev->io_mutex);
576 dev->interface = NULL;
577 mutex_unlock(&dev->io_mutex);
578
Oliver Neukume73c7242007-06-11 14:54:02 +0200579 usb_kill_anchored_urbs(&dev->submitted);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* decrement our usage count */
582 kref_put(&dev->kref, skel_delete);
583
Matt Kraaia5f5ea22009-02-06 19:38:51 -0800584 dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Oliver Neukum403dfb52007-05-25 13:42:56 +0200587static void skel_draw_down(struct usb_skel *dev)
588{
589 int time;
590
591 time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
592 if (!time)
593 usb_kill_anchored_urbs(&dev->submitted);
Oliver Neukume7389cc2009-09-09 17:06:53 +0200594 usb_kill_urb(dev->bulk_in_urb);
Oliver Neukum403dfb52007-05-25 13:42:56 +0200595}
596
Oliver Neukum758f7e162007-06-11 14:55:08 +0200597static int skel_suspend(struct usb_interface *intf, pm_message_t message)
598{
599 struct usb_skel *dev = usb_get_intfdata(intf);
600
601 if (!dev)
602 return 0;
603 skel_draw_down(dev);
604 return 0;
605}
606
Greg Kroah-Hartman3ae9da12009-09-11 16:07:30 -0700607static int skel_resume(struct usb_interface *intf)
Oliver Neukum758f7e162007-06-11 14:55:08 +0200608{
609 return 0;
610}
611
Oliver Neukum87d093e2007-06-11 14:55:51 +0200612static int skel_pre_reset(struct usb_interface *intf)
613{
614 struct usb_skel *dev = usb_get_intfdata(intf);
615
616 mutex_lock(&dev->io_mutex);
617 skel_draw_down(dev);
618
619 return 0;
620}
621
622static int skel_post_reset(struct usb_interface *intf)
623{
624 struct usb_skel *dev = usb_get_intfdata(intf);
625
626 /* we are sure no URBs are active - no locking needed */
627 dev->errors = -EPIPE;
628 mutex_unlock(&dev->io_mutex);
629
630 return 0;
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static struct usb_driver skel_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .name = "skeleton",
635 .probe = skel_probe,
636 .disconnect = skel_disconnect,
Oliver Neukum758f7e162007-06-11 14:55:08 +0200637 .suspend = skel_suspend,
638 .resume = skel_resume,
Oliver Neukum87d093e2007-06-11 14:55:51 +0200639 .pre_reset = skel_pre_reset,
640 .post_reset = skel_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 .id_table = skel_table,
Oliver Neukumba35e022007-03-01 14:31:02 +0100642 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643};
644
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800645module_usb_driver(skel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647MODULE_LICENSE("GPL");