Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

HID: trivial devm conversion for special hid drivers

It is safe to use devres allocation within the hid subsystem:
- the devres release is called _after_ the call to .remove(), meaning
that no freed pointers will exists while removing the device
- if a .probe() fails, devres releases all the allocated ressources
before going to the next driver: there will not be ghost ressources
attached to a hid device if several drivers are probed.

Given that, we can clean up a little some of the HID drivers. These ones
are trivial:
- there is only one kzalloc in the driver
- the .remove() callback contains only one kfree on top of hid_hw_stop()
- the error path in the probe is easy enough to be manually checked

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>

authored by

Benjamin Tissoires and committed by
Jiri Kosina
abf832bf 3366dd9f

+16 -66
+4 -17
drivers/hid/hid-a4tech.c
··· 90 90 struct a4tech_sc *a4; 91 91 int ret; 92 92 93 - a4 = kzalloc(sizeof(*a4), GFP_KERNEL); 93 + a4 = devm_kzalloc(&hdev->dev, sizeof(*a4), GFP_KERNEL); 94 94 if (a4 == NULL) { 95 95 hid_err(hdev, "can't alloc device descriptor\n"); 96 - ret = -ENOMEM; 97 - goto err_free; 96 + return -ENOMEM; 98 97 } 99 98 100 99 a4->quirks = id->driver_data; ··· 103 104 ret = hid_parse(hdev); 104 105 if (ret) { 105 106 hid_err(hdev, "parse failed\n"); 106 - goto err_free; 107 + return ret; 107 108 } 108 109 109 110 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 110 111 if (ret) { 111 112 hid_err(hdev, "hw start failed\n"); 112 - goto err_free; 113 + return ret; 113 114 } 114 115 115 116 return 0; 116 - err_free: 117 - kfree(a4); 118 - return ret; 119 - } 120 - 121 - static void a4_remove(struct hid_device *hdev) 122 - { 123 - struct a4tech_sc *a4 = hid_get_drvdata(hdev); 124 - 125 - hid_hw_stop(hdev); 126 - kfree(a4); 127 117 } 128 118 129 119 static const struct hid_device_id a4_devices[] = { ··· 132 144 .input_mapped = a4_input_mapped, 133 145 .event = a4_event, 134 146 .probe = a4_probe, 135 - .remove = a4_remove, 136 147 }; 137 148 module_hid_driver(a4_driver); 138 149
+3 -13
drivers/hid/hid-apple.c
··· 349 349 unsigned int connect_mask = HID_CONNECT_DEFAULT; 350 350 int ret; 351 351 352 - asc = kzalloc(sizeof(*asc), GFP_KERNEL); 352 + asc = devm_kzalloc(&hdev->dev, sizeof(*asc), GFP_KERNEL); 353 353 if (asc == NULL) { 354 354 hid_err(hdev, "can't alloc apple descriptor\n"); 355 355 return -ENOMEM; ··· 362 362 ret = hid_parse(hdev); 363 363 if (ret) { 364 364 hid_err(hdev, "parse failed\n"); 365 - goto err_free; 365 + return ret; 366 366 } 367 367 368 368 if (quirks & APPLE_HIDDEV) ··· 373 373 ret = hid_hw_start(hdev, connect_mask); 374 374 if (ret) { 375 375 hid_err(hdev, "hw start failed\n"); 376 - goto err_free; 376 + return ret; 377 377 } 378 378 379 379 return 0; 380 - err_free: 381 - kfree(asc); 382 - return ret; 383 - } 384 - 385 - static void apple_remove(struct hid_device *hdev) 386 - { 387 - hid_hw_stop(hdev); 388 - kfree(hid_get_drvdata(hdev)); 389 380 } 390 381 391 382 static const struct hid_device_id apple_devices[] = { ··· 536 545 .id_table = apple_devices, 537 546 .report_fixup = apple_report_fixup, 538 547 .probe = apple_probe, 539 - .remove = apple_remove, 540 548 .event = apple_event, 541 549 .input_mapping = apple_input_mapping, 542 550 .input_mapped = apple_input_mapped,
+3 -14
drivers/hid/hid-magicmouse.c
··· 484 484 struct hid_report *report; 485 485 int ret; 486 486 487 - msc = kzalloc(sizeof(*msc), GFP_KERNEL); 487 + msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL); 488 488 if (msc == NULL) { 489 489 hid_err(hdev, "can't alloc magicmouse descriptor\n"); 490 490 return -ENOMEM; ··· 498 498 ret = hid_parse(hdev); 499 499 if (ret) { 500 500 hid_err(hdev, "magicmouse hid parse failed\n"); 501 - goto err_free; 501 + return ret; 502 502 } 503 503 504 504 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 505 505 if (ret) { 506 506 hid_err(hdev, "magicmouse hw start failed\n"); 507 - goto err_free; 507 + return ret; 508 508 } 509 509 510 510 if (!msc->input) { ··· 548 548 return 0; 549 549 err_stop_hw: 550 550 hid_hw_stop(hdev); 551 - err_free: 552 - kfree(msc); 553 551 return ret; 554 - } 555 - 556 - static void magicmouse_remove(struct hid_device *hdev) 557 - { 558 - struct magicmouse_sc *msc = hid_get_drvdata(hdev); 559 - 560 - hid_hw_stop(hdev); 561 - kfree(msc); 562 552 } 563 553 564 554 static const struct hid_device_id magic_mice[] = { ··· 564 574 .name = "magicmouse", 565 575 .id_table = magic_mice, 566 576 .probe = magicmouse_probe, 567 - .remove = magicmouse_remove, 568 577 .raw_event = magicmouse_raw_event, 569 578 .input_mapping = magicmouse_input_mapping, 570 579 .input_configured = magicmouse_input_configured,
+3 -6
drivers/hid/hid-sony.c
··· 623 623 struct sony_sc *sc; 624 624 unsigned int connect_mask = HID_CONNECT_DEFAULT; 625 625 626 - sc = kzalloc(sizeof(*sc), GFP_KERNEL); 626 + sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL); 627 627 if (sc == NULL) { 628 628 hid_err(hdev, "can't alloc sony descriptor\n"); 629 629 return -ENOMEM; ··· 635 635 ret = hid_parse(hdev); 636 636 if (ret) { 637 637 hid_err(hdev, "parse failed\n"); 638 - goto err_free; 638 + return ret; 639 639 } 640 640 641 641 if (sc->quirks & VAIO_RDESC_CONSTANT) ··· 648 648 ret = hid_hw_start(hdev, connect_mask); 649 649 if (ret) { 650 650 hid_err(hdev, "hw start failed\n"); 651 - goto err_free; 651 + return ret; 652 652 } 653 653 654 654 if (sc->quirks & SIXAXIS_CONTROLLER_USB) { ··· 668 668 return 0; 669 669 err_stop: 670 670 hid_hw_stop(hdev); 671 - err_free: 672 - kfree(sc); 673 671 return ret; 674 672 } 675 673 ··· 679 681 buzz_remove(hdev); 680 682 681 683 hid_hw_stop(hdev); 682 - kfree(sc); 683 684 } 684 685 685 686 static const struct hid_device_id sony_devices[] = {
+3 -16
drivers/hid/hid-zydacron.c
··· 169 169 int ret; 170 170 struct zc_device *zc; 171 171 172 - zc = kzalloc(sizeof(*zc), GFP_KERNEL); 172 + zc = devm_kzalloc(&hdev->dev, sizeof(*zc), GFP_KERNEL); 173 173 if (zc == NULL) { 174 174 hid_err(hdev, "can't alloc descriptor\n"); 175 175 return -ENOMEM; ··· 180 180 ret = hid_parse(hdev); 181 181 if (ret) { 182 182 hid_err(hdev, "parse failed\n"); 183 - goto err_free; 183 + return ret; 184 184 } 185 185 186 186 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 187 187 if (ret) { 188 188 hid_err(hdev, "hw start failed\n"); 189 - goto err_free; 189 + return ret; 190 190 } 191 191 192 192 return 0; 193 - err_free: 194 - kfree(zc); 195 - 196 - return ret; 197 - } 198 - 199 - static void zc_remove(struct hid_device *hdev) 200 - { 201 - struct zc_device *zc = hid_get_drvdata(hdev); 202 - 203 - hid_hw_stop(hdev); 204 - kfree(zc); 205 193 } 206 194 207 195 static const struct hid_device_id zc_devices[] = { ··· 205 217 .input_mapping = zc_input_mapping, 206 218 .raw_event = zc_raw_event, 207 219 .probe = zc_probe, 208 - .remove = zc_remove, 209 220 }; 210 221 module_hid_driver(zc_driver); 211 222