Skip to content

Commit 16d0e1d

Browse files
Jiri Slaby (SUSE)Jiri Kosina
authored andcommitted
HID: hid-prodikeys: remove struct pk_device
First, quirks was unused in struct pk_device. And I see no reason for this additional level of indirection. struct pcmidi_snd is far enough for the driver. Unless I am missing something? So drop struct pk_device and convert all the users to use struct pcmidi_snd directly. No need for doubled kmalloc+kfrees now. Found by https://github.com/jirislaby/clang-struct. Signed-off-by: Jiri Slaby (SUSE) <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Benjamin Tissoires <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 22ec898 commit 16d0e1d

File tree

1 file changed

+40
-73
lines changed

1 file changed

+40
-73
lines changed

drivers/hid/hid-prodikeys.c

Lines changed: 40 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@
3232

3333
struct pcmidi_snd;
3434

35-
struct pk_device {
36-
unsigned long quirks;
37-
38-
struct hid_device *hdev;
39-
struct pcmidi_snd *pm; /* pcmidi device context */
40-
};
41-
4235
struct pcmidi_sustain {
4336
unsigned long in_use;
4437
struct pcmidi_snd *pm;
@@ -50,7 +43,7 @@ struct pcmidi_sustain {
5043

5144
#define PCMIDI_SUSTAINED_MAX 32
5245
struct pcmidi_snd {
53-
struct pk_device *pk;
46+
struct hid_device *hdev;
5447
unsigned short ifnum;
5548
struct hid_report *pcmidi_report6;
5649
struct input_dev *input_ep82;
@@ -98,11 +91,11 @@ static ssize_t show_channel(struct device *dev,
9891
struct device_attribute *attr, char *buf)
9992
{
10093
struct hid_device *hdev = to_hid_device(dev);
101-
struct pk_device *pk = hid_get_drvdata(hdev);
94+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
10295

103-
dbg_hid("pcmidi sysfs read channel=%u\n", pk->pm->midi_channel);
96+
dbg_hid("pcmidi sysfs read channel=%u\n", pm->midi_channel);
10497

105-
return sprintf(buf, "%u (min:%u, max:%u)\n", pk->pm->midi_channel,
98+
return sprintf(buf, "%u (min:%u, max:%u)\n", pm->midi_channel,
10699
PCMIDI_CHANNEL_MIN, PCMIDI_CHANNEL_MAX);
107100
}
108101

@@ -111,13 +104,13 @@ static ssize_t store_channel(struct device *dev,
111104
struct device_attribute *attr, const char *buf, size_t count)
112105
{
113106
struct hid_device *hdev = to_hid_device(dev);
114-
struct pk_device *pk = hid_get_drvdata(hdev);
107+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
115108

116109
unsigned channel = 0;
117110

118111
if (sscanf(buf, "%u", &channel) > 0 && channel <= PCMIDI_CHANNEL_MAX) {
119112
dbg_hid("pcmidi sysfs write channel=%u\n", channel);
120-
pk->pm->midi_channel = channel;
113+
pm->midi_channel = channel;
121114
return strlen(buf);
122115
}
123116
return -EINVAL;
@@ -135,11 +128,11 @@ static ssize_t show_sustain(struct device *dev,
135128
struct device_attribute *attr, char *buf)
136129
{
137130
struct hid_device *hdev = to_hid_device(dev);
138-
struct pk_device *pk = hid_get_drvdata(hdev);
131+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
139132

140-
dbg_hid("pcmidi sysfs read sustain=%u\n", pk->pm->midi_sustain);
133+
dbg_hid("pcmidi sysfs read sustain=%u\n", pm->midi_sustain);
141134

142-
return sprintf(buf, "%u (off:%u, max:%u (ms))\n", pk->pm->midi_sustain,
135+
return sprintf(buf, "%u (off:%u, max:%u (ms))\n", pm->midi_sustain,
143136
PCMIDI_SUSTAIN_MIN, PCMIDI_SUSTAIN_MAX);
144137
}
145138

@@ -148,15 +141,14 @@ static ssize_t store_sustain(struct device *dev,
148141
struct device_attribute *attr, const char *buf, size_t count)
149142
{
150143
struct hid_device *hdev = to_hid_device(dev);
151-
struct pk_device *pk = hid_get_drvdata(hdev);
144+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
152145

153146
unsigned sustain = 0;
154147

155148
if (sscanf(buf, "%u", &sustain) > 0 && sustain <= PCMIDI_SUSTAIN_MAX) {
156149
dbg_hid("pcmidi sysfs write sustain=%u\n", sustain);
157-
pk->pm->midi_sustain = sustain;
158-
pk->pm->midi_sustain_mode =
159-
(0 == sustain || !pk->pm->midi_mode) ? 0 : 1;
150+
pm->midi_sustain = sustain;
151+
pm->midi_sustain_mode = (0 == sustain || !pm->midi_mode) ? 0 : 1;
160152
return strlen(buf);
161153
}
162154
return -EINVAL;
@@ -174,11 +166,11 @@ static ssize_t show_octave(struct device *dev,
174166
struct device_attribute *attr, char *buf)
175167
{
176168
struct hid_device *hdev = to_hid_device(dev);
177-
struct pk_device *pk = hid_get_drvdata(hdev);
169+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
178170

179-
dbg_hid("pcmidi sysfs read octave=%d\n", pk->pm->midi_octave);
171+
dbg_hid("pcmidi sysfs read octave=%d\n", pm->midi_octave);
180172

181-
return sprintf(buf, "%d (min:%d, max:%d)\n", pk->pm->midi_octave,
173+
return sprintf(buf, "%d (min:%d, max:%d)\n", pm->midi_octave,
182174
PCMIDI_OCTAVE_MIN, PCMIDI_OCTAVE_MAX);
183175
}
184176

@@ -187,14 +179,14 @@ static ssize_t store_octave(struct device *dev,
187179
struct device_attribute *attr, const char *buf, size_t count)
188180
{
189181
struct hid_device *hdev = to_hid_device(dev);
190-
struct pk_device *pk = hid_get_drvdata(hdev);
182+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
191183

192184
int octave = 0;
193185

194186
if (sscanf(buf, "%d", &octave) > 0 &&
195187
octave >= PCMIDI_OCTAVE_MIN && octave <= PCMIDI_OCTAVE_MAX) {
196188
dbg_hid("pcmidi sysfs write octave=%d\n", octave);
197-
pk->pm->midi_octave = octave;
189+
pm->midi_octave = octave;
198190
return strlen(buf);
199191
}
200192
return -EINVAL;
@@ -268,7 +260,7 @@ static void stop_sustain_timers(struct pcmidi_snd *pm)
268260

269261
static int pcmidi_get_output_report(struct pcmidi_snd *pm)
270262
{
271-
struct hid_device *hdev = pm->pk->hdev;
263+
struct hid_device *hdev = pm->hdev;
272264
struct hid_report *report;
273265

274266
list_for_each_entry(report,
@@ -293,7 +285,7 @@ static int pcmidi_get_output_report(struct pcmidi_snd *pm)
293285

294286
static void pcmidi_submit_output_report(struct pcmidi_snd *pm, int state)
295287
{
296-
struct hid_device *hdev = pm->pk->hdev;
288+
struct hid_device *hdev = pm->hdev;
297289
struct hid_report *report = pm->pcmidi_report6;
298290
report->field[0]->value[0] = 0x01;
299291
report->field[0]->value[1] = state;
@@ -620,7 +612,7 @@ static int pcmidi_snd_initialise(struct pcmidi_snd *pm)
620612

621613
/* Setup sound card */
622614

623-
err = snd_card_new(&pm->pk->hdev->dev, index[dev], id[dev],
615+
err = snd_card_new(&pm->hdev->dev, index[dev], id[dev],
624616
THIS_MODULE, 0, &card);
625617
if (err < 0) {
626618
pk_error("failed to create pc-midi sound card\n");
@@ -658,23 +650,23 @@ static int pcmidi_snd_initialise(struct pcmidi_snd *pm)
658650
&pcmidi_in_ops);
659651

660652
/* create sysfs variables */
661-
err = device_create_file(&pm->pk->hdev->dev,
653+
err = device_create_file(&pm->hdev->dev,
662654
sysfs_device_attr_channel);
663655
if (err < 0) {
664656
pk_error("failed to create sysfs attribute channel: error %d\n",
665657
err);
666658
goto fail;
667659
}
668660

669-
err = device_create_file(&pm->pk->hdev->dev,
661+
err = device_create_file(&pm->hdev->dev,
670662
sysfs_device_attr_sustain);
671663
if (err < 0) {
672664
pk_error("failed to create sysfs attribute sustain: error %d\n",
673665
err);
674666
goto fail_attr_sustain;
675667
}
676668

677-
err = device_create_file(&pm->pk->hdev->dev,
669+
err = device_create_file(&pm->hdev->dev,
678670
sysfs_device_attr_octave);
679671
if (err < 0) {
680672
pk_error("failed to create sysfs attribute octave: error %d\n",
@@ -704,11 +696,11 @@ static int pcmidi_snd_initialise(struct pcmidi_snd *pm)
704696

705697
fail_register:
706698
stop_sustain_timers(pm);
707-
device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_octave);
699+
device_remove_file(&pm->hdev->dev, sysfs_device_attr_octave);
708700
fail_attr_octave:
709-
device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_sustain);
701+
device_remove_file(&pm->hdev->dev, sysfs_device_attr_sustain);
710702
fail_attr_sustain:
711-
device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_channel);
703+
device_remove_file(&pm->hdev->dev, sysfs_device_attr_channel);
712704
fail:
713705
if (pm->card) {
714706
snd_card_free(pm->card);
@@ -722,12 +714,9 @@ static int pcmidi_snd_terminate(struct pcmidi_snd *pm)
722714
if (pm->card) {
723715
stop_sustain_timers(pm);
724716

725-
device_remove_file(&pm->pk->hdev->dev,
726-
sysfs_device_attr_channel);
727-
device_remove_file(&pm->pk->hdev->dev,
728-
sysfs_device_attr_sustain);
729-
device_remove_file(&pm->pk->hdev->dev,
730-
sysfs_device_attr_octave);
717+
device_remove_file(&pm->hdev->dev, sysfs_device_attr_channel);
718+
device_remove_file(&pm->hdev->dev, sysfs_device_attr_sustain);
719+
device_remove_file(&pm->hdev->dev, sysfs_device_attr_octave);
731720

732721
snd_card_disconnect(pm->card);
733722
snd_card_free_when_closed(pm->card);
@@ -757,10 +746,7 @@ static int pk_input_mapping(struct hid_device *hdev, struct hid_input *hi,
757746
struct hid_field *field, struct hid_usage *usage,
758747
unsigned long **bit, int *max)
759748
{
760-
struct pk_device *pk = hid_get_drvdata(hdev);
761-
struct pcmidi_snd *pm;
762-
763-
pm = pk->pm;
749+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
764750

765751
if (HID_UP_MSVENDOR == (usage->hid & HID_USAGE_PAGE) &&
766752
1 == pm->ifnum) {
@@ -775,16 +761,16 @@ static int pk_input_mapping(struct hid_device *hdev, struct hid_input *hi,
775761
static int pk_raw_event(struct hid_device *hdev, struct hid_report *report,
776762
u8 *data, int size)
777763
{
778-
struct pk_device *pk = hid_get_drvdata(hdev);
764+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
779765
int ret = 0;
780766

781-
if (1 == pk->pm->ifnum) {
767+
if (1 == pm->ifnum) {
782768
if (report->id == data[0])
783769
switch (report->id) {
784770
case 0x01: /* midi keys (qwerty)*/
785771
case 0x03: /* midi keyboard (musical)*/
786772
case 0x04: /* extra/midi keys (qwerty)*/
787-
ret = pcmidi_handle_report(pk->pm,
773+
ret = pcmidi_handle_report(pm,
788774
report->id, data, size);
789775
break;
790776
}
@@ -799,35 +785,24 @@ static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)
799785
struct usb_interface *intf;
800786
unsigned short ifnum;
801787
unsigned long quirks = id->driver_data;
802-
struct pk_device *pk;
803-
struct pcmidi_snd *pm = NULL;
788+
struct pcmidi_snd *pm;
804789

805790
if (!hid_is_usb(hdev))
806791
return -EINVAL;
807792

808793
intf = to_usb_interface(hdev->dev.parent);
809794
ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
810795

811-
pk = kzalloc(sizeof(*pk), GFP_KERNEL);
812-
if (pk == NULL) {
813-
hid_err(hdev, "can't alloc descriptor\n");
814-
return -ENOMEM;
815-
}
816-
817-
pk->hdev = hdev;
818-
819796
pm = kzalloc(sizeof(*pm), GFP_KERNEL);
820797
if (pm == NULL) {
821798
hid_err(hdev, "can't alloc descriptor\n");
822-
ret = -ENOMEM;
823-
goto err_free_pk;
799+
return -ENOMEM;
824800
}
825801

826-
pm->pk = pk;
827-
pk->pm = pm;
802+
pm->hdev = hdev;
828803
pm->ifnum = ifnum;
829804

830-
hid_set_drvdata(hdev, pk);
805+
hid_set_drvdata(hdev, pm);
831806

832807
ret = hid_parse(hdev);
833808
if (ret) {
@@ -854,26 +829,18 @@ static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)
854829
hid_hw_stop(hdev);
855830
err_free:
856831
kfree(pm);
857-
err_free_pk:
858-
kfree(pk);
859832

860833
return ret;
861834
}
862835

863836
static void pk_remove(struct hid_device *hdev)
864837
{
865-
struct pk_device *pk = hid_get_drvdata(hdev);
866-
struct pcmidi_snd *pm;
867-
868-
pm = pk->pm;
869-
if (pm) {
870-
pcmidi_snd_terminate(pm);
871-
kfree(pm);
872-
}
838+
struct pcmidi_snd *pm = hid_get_drvdata(hdev);
873839

840+
pcmidi_snd_terminate(pm);
874841
hid_hw_stop(hdev);
875842

876-
kfree(pk);
843+
kfree(pm);
877844
}
878845

879846
static const struct hid_device_id pk_devices[] = {

0 commit comments

Comments
 (0)