From cc070a847bc0b6fb66e4c05b5bd036d2ea80fb9a Mon Sep 17 00:00:00 2001 From: Hanno Zulla Date: Thu, 14 Jun 2018 16:29:08 +0200 Subject: HID: hid-sony.c: Use devm_ api to simplify sony_register_touchpad() HID: hid-sony.c: Use devm_ api to simplify sony_register_touchpad() Using devm_ calls, the resources of the Sony game devices's features are tied to the main device handle, making it easier to handle errors and teardown inside the device driver. Altogether, this reduces complexity of the driver source. Signed-off-by: Hanno Zulla Reviewed-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/hid-sony.c | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) (limited to 'drivers/hid') diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c index e475c5073c99..f14286e62e15 100644 --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -1353,7 +1353,7 @@ static int sony_register_touchpad(struct sony_sc *sc, int touch_count, char *name; int ret; - sc->touchpad = input_allocate_device(); + sc->touchpad = devm_input_allocate_device(&sc->hdev->dev); if (!sc->touchpad) return -ENOMEM; @@ -1370,11 +1370,9 @@ static int sony_register_touchpad(struct sony_sc *sc, int touch_count, * DS4 compatible non-Sony devices with different names. */ name_sz = strlen(sc->hdev->name) + sizeof(DS4_TOUCHPAD_SUFFIX); - name = kzalloc(name_sz, GFP_KERNEL); - if (!name) { - ret = -ENOMEM; - goto err; - } + name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL); + if (!name) + return -ENOMEM; snprintf(name, name_sz, "%s" DS4_TOUCHPAD_SUFFIX, sc->hdev->name); sc->touchpad->name = name; @@ -1403,34 +1401,13 @@ static int sony_register_touchpad(struct sony_sc *sc, int touch_count, ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER); if (ret < 0) - goto err; + return ret; ret = input_register_device(sc->touchpad); if (ret < 0) - goto err; + return ret; return 0; - -err: - kfree(sc->touchpad->name); - sc->touchpad->name = NULL; - - input_free_device(sc->touchpad); - sc->touchpad = NULL; - - return ret; -} - -static void sony_unregister_touchpad(struct sony_sc *sc) -{ - if (!sc->touchpad) - return; - - kfree(sc->touchpad->name); - sc->touchpad->name = NULL; - - input_unregister_device(sc->touchpad); - sc->touchpad = NULL; } static int sony_register_sensors(struct sony_sc *sc) @@ -2883,8 +2860,6 @@ err_stop: sony_leds_remove(sc); if (sc->quirks & SONY_BATTERY_SUPPORT) sony_battery_remove(sc); - if (sc->touchpad) - sony_unregister_touchpad(sc); if (sc->sensor_dev) sony_unregister_sensors(sc); sony_cancel_work_sync(sc); @@ -2971,9 +2946,6 @@ static void sony_remove(struct hid_device *hdev) if (sc->quirks & SONY_BATTERY_SUPPORT) sony_battery_remove(sc); - if (sc->touchpad) - sony_unregister_touchpad(sc); - if (sc->sensor_dev) sony_unregister_sensors(sc); -- cgit v1.2.3 From ea4a5fdc8d07df9115881f698c14bcfd7046a846 Mon Sep 17 00:00:00 2001 From: Hanno Zulla Date: Thu, 14 Jun 2018 16:30:02 +0200 Subject: HID: hid-sony.c: Use devm_ api to simplify sony_register_sensors() [PATCH 2/5] HID: hid-sony.c: Use devm_ api to simplify sony_register_sensors() Using devm_ calls, the resources of the Sony game devices's features are tied to the main device handle, making it easier to handle errors and teardown inside the device driver. Altogether, this reduces complexity of the driver source. Signed-off-by: Hanno Zulla Reviewed-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/hid-sony.c | 39 +++++---------------------------------- 1 file changed, 5 insertions(+), 34 deletions(-) (limited to 'drivers/hid') diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c index f14286e62e15..ebad1381e177 100644 --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -1417,7 +1417,7 @@ static int sony_register_sensors(struct sony_sc *sc) int ret; int range; - sc->sensor_dev = input_allocate_device(); + sc->sensor_dev = devm_input_allocate_device(&sc->hdev->dev); if (!sc->sensor_dev) return -ENOMEM; @@ -1434,11 +1434,9 @@ static int sony_register_sensors(struct sony_sc *sc) * DS4 compatible non-Sony devices with different names. */ name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX); - name = kzalloc(name_sz, GFP_KERNEL); - if (!name) { - ret = -ENOMEM; - goto err; - } + name = devm_kzalloc(&sc->hdev->dev, name_sz, GFP_KERNEL); + if (!name) + return -ENOMEM; snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name); sc->sensor_dev->name = name; @@ -1480,33 +1478,11 @@ static int sony_register_sensors(struct sony_sc *sc) ret = input_register_device(sc->sensor_dev); if (ret < 0) - goto err; + return ret; return 0; - -err: - kfree(sc->sensor_dev->name); - sc->sensor_dev->name = NULL; - - input_free_device(sc->sensor_dev); - sc->sensor_dev = NULL; - - return ret; } -static void sony_unregister_sensors(struct sony_sc *sc) -{ - if (!sc->sensor_dev) - return; - - kfree(sc->sensor_dev->name); - sc->sensor_dev->name = NULL; - - input_unregister_device(sc->sensor_dev); - sc->sensor_dev = NULL; -} - - /* * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller * to "operational". Without this, the ps3 controller will not report any @@ -2860,8 +2836,6 @@ err_stop: sony_leds_remove(sc); if (sc->quirks & SONY_BATTERY_SUPPORT) sony_battery_remove(sc); - if (sc->sensor_dev) - sony_unregister_sensors(sc); sony_cancel_work_sync(sc); kfree(sc->output_report_dmabuf); sony_remove_dev_list(sc); @@ -2946,9 +2920,6 @@ static void sony_remove(struct hid_device *hdev) if (sc->quirks & SONY_BATTERY_SUPPORT) sony_battery_remove(sc); - if (sc->sensor_dev) - sony_unregister_sensors(sc); - if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval); -- cgit v1.2.3 From f2d98e2c020e8f33813a746ac8a92a4582ac416a Mon Sep 17 00:00:00 2001 From: Hanno Zulla Date: Thu, 14 Jun 2018 16:30:46 +0200 Subject: HID: hid-sony.c: Use devm_ api to simplify sony_leds_init() [PATCH 3/5] HID: hid-sony.c: Use devm_ api to simplify sony_leds_init() Using devm_ calls, the resources of the Sony game devices's features are tied to the main device handle, making it easier to handle errors and teardown inside the device driver. Altogether, this reduces complexity of the driver source. Signed-off-by: Hanno Zulla Reviewed-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/hid-sony.c | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) (limited to 'drivers/hid') diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c index ebad1381e177..7c1c56d13122 100644 --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -1940,25 +1940,6 @@ static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on, return 0; } -static void sony_leds_remove(struct sony_sc *sc) -{ - struct led_classdev *led; - int n; - - BUG_ON(!(sc->quirks & SONY_LED_SUPPORT)); - - for (n = 0; n < sc->led_count; n++) { - led = sc->leds[n]; - sc->leds[n] = NULL; - if (!led) - continue; - led_classdev_unregister(led); - kfree(led); - } - - sc->led_count = 0; -} - static int sony_leds_init(struct sony_sc *sc) { struct hid_device *hdev = sc->hdev; @@ -2031,11 +2012,10 @@ static int sony_leds_init(struct sony_sc *sc) if (use_ds4_names) name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2; - led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL); + led = devm_kzalloc(&hdev->dev, sizeof(struct led_classdev) + name_sz, GFP_KERNEL); if (!led) { hid_err(hdev, "Couldn't allocate memory for LED %d\n", n); - ret = -ENOMEM; - goto error_leds; + return -ENOMEM; } name = (void *)(&led[1]); @@ -2056,21 +2036,14 @@ static int sony_leds_init(struct sony_sc *sc) sc->leds[n] = led; - ret = led_classdev_register(&hdev->dev, led); + ret = devm_led_classdev_register(&hdev->dev, led); if (ret) { hid_err(hdev, "Failed to register LED %d\n", n); - sc->leds[n] = NULL; - kfree(led); - goto error_leds; + return ret; } } - return ret; - -error_leds: - sony_leds_remove(sc); - - return ret; + return 0; } static void sixaxis_send_output_report(struct sony_sc *sc) @@ -2832,8 +2805,6 @@ err_stop: device_remove_file(&sc->hdev->dev, &dev_attr_firmware_version); if (sc->hw_version) device_remove_file(&sc->hdev->dev, &dev_attr_hardware_version); - if (sc->quirks & SONY_LED_SUPPORT) - sony_leds_remove(sc); if (sc->quirks & SONY_BATTERY_SUPPORT) sony_battery_remove(sc); sony_cancel_work_sync(sc); @@ -2914,9 +2885,6 @@ static void sony_remove(struct hid_device *hdev) hid_hw_close(hdev); - if (sc->quirks & SONY_LED_SUPPORT) - sony_leds_remove(sc); - if (sc->quirks & SONY_BATTERY_SUPPORT) sony_battery_remove(sc); -- cgit v1.2.3 From 8082d3f02261adbdc4eb3301fb99960162c3d142 Mon Sep 17 00:00:00 2001 From: Hanno Zulla Date: Thu, 14 Jun 2018 16:31:36 +0200 Subject: HID: hid-sony.c: Use devm_ api to simplify sony_battery_probe() HID: hid-sony.c: Use devm_ api to simplify sony_battery_probe() Using devm_ calls, the resources of the Sony game devices's features are tied to the main device handle, making it easier to handle errors and teardown inside the device driver. Altogether, this reduces complexity of the driver source. Signed-off-by: Hanno Zulla Reviewed-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/hid-sony.c | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) (limited to 'drivers/hid') diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c index 7c1c56d13122..9abdd77f16c9 100644 --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -2318,36 +2318,21 @@ static int sony_battery_probe(struct sony_sc *sc, int append_dev_id) sc->battery_desc.get_property = sony_battery_get_property; sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; sc->battery_desc.use_for_apm = 0; - sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt, - sc->mac_address, sc->device_id); + sc->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, + battery_str_fmt, sc->mac_address, sc->device_id); if (!sc->battery_desc.name) return -ENOMEM; - sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc, + sc->battery = devm_power_supply_register(&hdev->dev, &sc->battery_desc, &psy_cfg); if (IS_ERR(sc->battery)) { ret = PTR_ERR(sc->battery); hid_err(hdev, "Unable to register battery device\n"); - goto err_free; + return ret; } power_supply_powers(sc->battery, &hdev->dev); return 0; - -err_free: - kfree(sc->battery_desc.name); - sc->battery_desc.name = NULL; - return ret; -} - -static void sony_battery_remove(struct sony_sc *sc) -{ - if (!sc->battery_desc.name) - return; - - power_supply_unregister(sc->battery); - kfree(sc->battery_desc.name); - sc->battery_desc.name = NULL; } /* @@ -2805,8 +2790,6 @@ err_stop: device_remove_file(&sc->hdev->dev, &dev_attr_firmware_version); if (sc->hw_version) device_remove_file(&sc->hdev->dev, &dev_attr_hardware_version); - if (sc->quirks & SONY_BATTERY_SUPPORT) - sony_battery_remove(sc); sony_cancel_work_sync(sc); kfree(sc->output_report_dmabuf); sony_remove_dev_list(sc); @@ -2885,9 +2868,6 @@ static void sony_remove(struct hid_device *hdev) hid_hw_close(hdev); - if (sc->quirks & SONY_BATTERY_SUPPORT) - sony_battery_remove(sc); - if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval); -- cgit v1.2.3 From ea58c33d4d85504b4255dd74999603a6793bc56a Mon Sep 17 00:00:00 2001 From: Hanno Zulla Date: Thu, 14 Jun 2018 16:32:16 +0200 Subject: HID: hid-sony.c: Use devm_ api to simplify sc->output_report_dmabuf HID: hid-sony.c: Use devm_ api to simplify sc->output_report_dmabuf Using devm_ calls, the resources of the Sony game devices's features are tied to the main device handle, making it easier to handle errors and teardown inside the device driver. Altogether, this reduces complexity of the driver source. Signed-off-by: Hanno Zulla Reviewed-by: Benjamin Tissoires Signed-off-by: Jiri Kosina --- drivers/hid/hid-sony.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'drivers/hid') diff --git a/drivers/hid/hid-sony.c b/drivers/hid/hid-sony.c index 9abdd77f16c9..9671a4bad643 100644 --- a/drivers/hid/hid-sony.c +++ b/drivers/hid/hid-sony.c @@ -2202,16 +2202,20 @@ static int sony_allocate_output_report(struct sony_sc *sc) if ((sc->quirks & SIXAXIS_CONTROLLER) || (sc->quirks & NAVIGATION_CONTROLLER)) sc->output_report_dmabuf = - kmalloc(sizeof(union sixaxis_output_report_01), + devm_kmalloc(&sc->hdev->dev, + sizeof(union sixaxis_output_report_01), GFP_KERNEL); else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) - sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE, + sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev, + DS4_OUTPUT_REPORT_0x11_SIZE, GFP_KERNEL); else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) - sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE, + sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev, + DS4_OUTPUT_REPORT_0x05_SIZE, GFP_KERNEL); else if (sc->quirks & MOTION_CONTROLLER) - sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE, + sc->output_report_dmabuf = devm_kmalloc(&sc->hdev->dev, + MOTION_REPORT_0x02_SIZE, GFP_KERNEL); else return 0; @@ -2791,7 +2795,6 @@ err_stop: if (sc->hw_version) device_remove_file(&sc->hdev->dev, &dev_attr_hardware_version); sony_cancel_work_sync(sc); - kfree(sc->output_report_dmabuf); sony_remove_dev_list(sc); sony_release_device_id(sc); hid_hw_stop(hdev); @@ -2879,8 +2882,6 @@ static void sony_remove(struct hid_device *hdev) sony_cancel_work_sync(sc); - kfree(sc->output_report_dmabuf); - sony_remove_dev_list(sc); sony_release_device_id(sc); -- cgit v1.2.3