forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
kernel/drivers/usb/gadget/function/f_hid.c
....@@ -71,7 +71,7 @@
7171 wait_queue_head_t write_queue;
7272 struct usb_request *req;
7373
74
- int minor;
74
+ struct device dev;
7575 struct cdev cdev;
7676 struct usb_function func;
7777
....@@ -82,6 +82,14 @@
8282 static inline struct f_hidg *func_to_hidg(struct usb_function *f)
8383 {
8484 return container_of(f, struct f_hidg, func);
85
+}
86
+
87
+static void hidg_release(struct device *dev)
88
+{
89
+ struct f_hidg *hidg = container_of(dev, struct f_hidg, dev);
90
+
91
+ kfree(hidg->set_report_buf);
92
+ kfree(hidg);
8593 }
8694
8795 /*-------------------------------------------------------------------------*/
....@@ -288,9 +296,6 @@
288296 if (!count)
289297 return 0;
290298
291
- if (!access_ok(VERIFY_WRITE, buffer, count))
292
- return -EFAULT;
293
-
294299 spin_lock_irqsave(&hidg->read_spinlock, flags);
295300
296301 #define READ_COND_INTOUT (!list_empty(&hidg->completed_out_req))
....@@ -429,9 +434,6 @@
429434 unsigned long flags;
430435 ssize_t status = -ENOMEM;
431436
432
- if (!access_ok(VERIFY_READ, buffer, count))
433
- return -EFAULT;
434
-
435437 spin_lock_irqsave(&hidg->write_spinlock, flags);
436438
437439 if (!hidg->req) {
....@@ -487,7 +489,7 @@
487489 }
488490
489491 req->status = 0;
490
- req->zero = ((count % hidg->in_ep->maxpacket) == 0);
492
+ req->zero = 0;
491493 req->length = count;
492494 req->complete = f_hidg_req_complete;
493495 req->context = hidg;
....@@ -593,7 +595,7 @@
593595 break;
594596 default:
595597 ERROR(cdev, "Set report failed %d\n", req->status);
596
- /* FALLTHROUGH */
598
+ fallthrough;
597599 case -ECONNABORTED: /* hardware forced ep reset */
598600 case -ECONNRESET: /* request dequeued */
599601 case -ESHUTDOWN: /* disconnect from host */
....@@ -910,9 +912,7 @@
910912 struct usb_ep *ep;
911913 struct f_hidg *hidg = func_to_hidg(f);
912914 struct usb_string *us;
913
- struct device *device;
914915 int status;
915
- dev_t dev;
916916
917917 /* maybe allocate device-global string IDs, and patch descriptors */
918918 us = usb_gstrings_attach(c->cdev, ct_func_strings,
....@@ -1005,21 +1005,11 @@
10051005
10061006 /* create char device */
10071007 cdev_init(&hidg->cdev, &f_hidg_fops);
1008
- dev = MKDEV(major, hidg->minor);
1009
- status = cdev_add(&hidg->cdev, dev, 1);
1008
+ status = cdev_device_add(&hidg->cdev, &hidg->dev);
10101009 if (status)
10111010 goto fail_free_descs;
10121011
1013
- device = device_create(hidg_class, NULL, dev, NULL,
1014
- "%s%d", "hidg", hidg->minor);
1015
- if (IS_ERR(device)) {
1016
- status = PTR_ERR(device);
1017
- goto del;
1018
- }
1019
-
10201012 return 0;
1021
-del:
1022
- cdev_del(&hidg->cdev);
10231013 fail_free_descs:
10241014 usb_free_all_descriptors(f);
10251015 fail:
....@@ -1250,9 +1240,7 @@
12501240
12511241 hidg = func_to_hidg(f);
12521242 opts = container_of(f->fi, struct f_hid_opts, func_inst);
1253
- kfree(hidg->report_desc);
1254
- kfree(hidg->set_report_buf);
1255
- kfree(hidg);
1243
+ put_device(&hidg->dev);
12561244 mutex_lock(&opts->lock);
12571245 --opts->refcnt;
12581246 mutex_unlock(&opts->lock);
....@@ -1262,8 +1250,7 @@
12621250 {
12631251 struct f_hidg *hidg = func_to_hidg(f);
12641252
1265
- device_destroy(hidg_class, MKDEV(major, hidg->minor));
1266
- cdev_del(&hidg->cdev);
1253
+ cdev_device_del(&hidg->cdev, &hidg->dev);
12671254
12681255 usb_free_all_descriptors(f);
12691256 }
....@@ -1272,6 +1259,7 @@
12721259 {
12731260 struct f_hidg *hidg;
12741261 struct f_hid_opts *opts;
1262
+ int ret;
12751263
12761264 /* allocate and initialize one new instance */
12771265 hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
....@@ -1283,17 +1271,28 @@
12831271 mutex_lock(&opts->lock);
12841272 ++opts->refcnt;
12851273
1286
- hidg->minor = opts->minor;
1274
+ device_initialize(&hidg->dev);
1275
+ hidg->dev.release = hidg_release;
1276
+ hidg->dev.class = hidg_class;
1277
+ hidg->dev.devt = MKDEV(major, opts->minor);
1278
+ ret = dev_set_name(&hidg->dev, "hidg%d", opts->minor);
1279
+ if (ret) {
1280
+ --opts->refcnt;
1281
+ mutex_unlock(&opts->lock);
1282
+ return ERR_PTR(ret);
1283
+ }
1284
+
12871285 hidg->bInterfaceSubClass = opts->subclass;
12881286 hidg->bInterfaceProtocol = opts->protocol;
12891287 hidg->report_length = opts->report_length;
12901288 hidg->report_desc_length = opts->report_desc_length;
12911289 if (opts->report_desc) {
1292
- hidg->report_desc = kmemdup(opts->report_desc,
1293
- opts->report_desc_length,
1294
- GFP_KERNEL);
1290
+ hidg->report_desc = devm_kmemdup(&hidg->dev, opts->report_desc,
1291
+ opts->report_desc_length,
1292
+ GFP_KERNEL);
12951293 if (!hidg->report_desc) {
1296
- kfree(hidg);
1294
+ put_device(&hidg->dev);
1295
+ --opts->refcnt;
12971296 mutex_unlock(&opts->lock);
12981297 return ERR_PTR(-ENOMEM);
12991298 }