31
31
#include <linux/types.h>
32
32
#include <linux/ctype.h>
33
33
#include <linux/cdev.h>
34
+ #include <linux/kref.h>
34
35
35
36
#include <asm/byteorder.h>
36
37
#include <linux/io.h>
@@ -64,7 +65,7 @@ struct printer_dev {
64
65
struct usb_gadget * gadget ;
65
66
s8 interface ;
66
67
struct usb_ep * in_ep , * out_ep ;
67
-
68
+ struct kref kref ;
68
69
struct list_head rx_reqs ; /* List of free RX structs */
69
70
struct list_head rx_reqs_active ; /* List of Active RX xfers */
70
71
struct list_head rx_buffers ; /* List of completed xfers */
@@ -218,6 +219,13 @@ static inline struct usb_endpoint_descriptor *ep_desc(struct usb_gadget *gadget,
218
219
219
220
/*-------------------------------------------------------------------------*/
220
221
222
+ static void printer_dev_free (struct kref * kref )
223
+ {
224
+ struct printer_dev * dev = container_of (kref , struct printer_dev , kref );
225
+
226
+ kfree (dev );
227
+ }
228
+
221
229
static struct usb_request *
222
230
printer_req_alloc (struct usb_ep * ep , unsigned len , gfp_t gfp_flags )
223
231
{
@@ -353,6 +361,7 @@ printer_open(struct inode *inode, struct file *fd)
353
361
354
362
spin_unlock_irqrestore (& dev -> lock , flags );
355
363
364
+ kref_get (& dev -> kref );
356
365
DBG (dev , "printer_open returned %x\n" , ret );
357
366
return ret ;
358
367
}
@@ -370,6 +379,7 @@ printer_close(struct inode *inode, struct file *fd)
370
379
dev -> printer_status &= ~PRINTER_SELECTED ;
371
380
spin_unlock_irqrestore (& dev -> lock , flags );
372
381
382
+ kref_put (& dev -> kref , printer_dev_free );
373
383
DBG (dev , "printer_close\n" );
374
384
375
385
return 0 ;
@@ -1386,7 +1396,8 @@ static void gprinter_free(struct usb_function *f)
1386
1396
struct f_printer_opts * opts ;
1387
1397
1388
1398
opts = container_of (f -> fi , struct f_printer_opts , func_inst );
1389
- kfree (dev );
1399
+
1400
+ kref_put (& dev -> kref , printer_dev_free );
1390
1401
mutex_lock (& opts -> lock );
1391
1402
-- opts -> refcnt ;
1392
1403
mutex_unlock (& opts -> lock );
@@ -1455,6 +1466,7 @@ static struct usb_function *gprinter_alloc(struct usb_function_instance *fi)
1455
1466
return ERR_PTR (- ENOMEM );
1456
1467
}
1457
1468
1469
+ kref_init (& dev -> kref );
1458
1470
++ opts -> refcnt ;
1459
1471
dev -> minor = opts -> minor ;
1460
1472
dev -> pnp_string = opts -> pnp_string ;
0 commit comments