1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
// SPDX-License-Identifier: GPL-2.0
/*
* configfs.c - Implementation of configfs interface to the driver stack
*
* Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/configfs.h>
#include <linux/most.h>
#define MAX_STRING_SIZE 80
struct mdev_link {
struct config_item item;
struct list_head list;
bool create_link;
bool destroy_link;
u16 num_buffers;
u16 buffer_size;
u16 subbuffer_size;
u16 packets_per_xact;
u16 dbr_size;
char datatype[MAX_STRING_SIZE];
char direction[MAX_STRING_SIZE];
char name[MAX_STRING_SIZE];
char device[MAX_STRING_SIZE];
char channel[MAX_STRING_SIZE];
char comp[MAX_STRING_SIZE];
char comp_params[MAX_STRING_SIZE];
};
static struct list_head mdev_link_list;
static int set_cfg_buffer_size(struct mdev_link *link)
{
return most_set_cfg_buffer_size(link->device, link->channel,
link->buffer_size);
}
static int set_cfg_subbuffer_size(struct mdev_link *link)
{
return most_set_cfg_subbuffer_size(link->device, link->channel,
link->subbuffer_size);
}
static int set_cfg_dbr_size(struct mdev_link *link)
{
return most_set_cfg_dbr_size(link->device, link->channel,
link->dbr_size);
}
static int set_cfg_num_buffers(struct mdev_link *link)
{
return most_set_cfg_num_buffers(link->device, link->channel,
link->num_buffers);
}
static int set_cfg_packets_xact(struct mdev_link *link)
{
return most_set_cfg_packets_xact(link->device, link->channel,
link->packets_per_xact);
}
static int set_cfg_direction(struct mdev_link *link)
{
return most_set_cfg_direction(link->device, link->channel,
link->direction);
}
static int set_cfg_datatype(struct mdev_link *link)
{
return most_set_cfg_datatype(link->device, link->channel,
link->datatype);
}
static int (*set_config_val[])(struct mdev_link *link) = {
set_cfg_buffer_size,
set_cfg_subbuffer_size,
set_cfg_dbr_size,
set_cfg_num_buffers,
set_cfg_packets_xact,
set_cfg_direction,
set_cfg_datatype,
};
static struct mdev_link *to_mdev_link(struct config_item *item)
{
return container_of(item, struct mdev_link, item);
}
static int set_config_and_add_link(struct mdev_link *mdev_link)
{
int i;
int ret;
for (i = 0; i < ARRAY_SIZE(set_config_val); i++) {
ret = set_config_val[i](mdev_link);
if (ret < 0 && ret != -ENODEV) {
pr_err("Config failed\n");
return ret;
}
}
return most_add_link(mdev_link->device, mdev_link->channel,
mdev_link->comp, mdev_link->name,
mdev_link->comp_params);
}
static ssize_t mdev_link_create_link_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
bool tmp;
int ret;
ret = kstrtobool(page, &tmp);
if (ret)
return ret;
if (!tmp)
return count;
ret = set_config_and_add_link(mdev_link);
if (ret && ret != -ENODEV)
return ret;
list_add_tail(&mdev_link->list, &mdev_link_list);
mdev_link->create_link = tmp;
mdev_link->destroy_link = false;
return count;
}
static ssize_t mdev_link_destroy_link_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
bool tmp;
int ret;
ret = kstrtobool(page, &tmp);
if (ret)
return ret;
if (!tmp)
return count;
ret = most_remove_link(mdev_link->device, mdev_link->channel,
mdev_link->comp);
if (ret)
return ret;
if (!list_empty(&mdev_link_list))
list_del(&mdev_link->list);
mdev_link->destroy_link = tmp;
return count;
}
static ssize_t mdev_link_direction_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%s\n", to_mdev_link(item)->direction);
}
static ssize_t mdev_link_direction_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
if (!sysfs_streq(page, "dir_rx") && !sysfs_streq(page, "rx") &&
!sysfs_streq(page, "dir_tx") && !sysfs_streq(page, "tx"))
return -EINVAL;
strcpy(mdev_link->direction, page);
strim(mdev_link->direction);
return count;
}
static ssize_t mdev_link_datatype_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%s\n", to_mdev_link(item)->datatype);
}
static ssize_t mdev_link_datatype_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
if (!sysfs_streq(page, "control") && !sysfs_streq(page, "async") &&
!sysfs_streq(page, "sync") && !sysfs_streq(page, "isoc") &&
!sysfs_streq(page, "isoc_avp"))
return -EINVAL;
strcpy(mdev_link->datatype, page);
strim(mdev_link->datatype);
return count;
}
static ssize_t mdev_link_device_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%s\n", to_mdev_link(item)->device);
}
static ssize_t mdev_link_device_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
strlcpy(mdev_link->device, page, sizeof(mdev_link->device));
strim(mdev_link->device);
return count;
}
static ssize_t mdev_link_channel_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%s\n", to_mdev_link(item)->channel);
}
static ssize_t mdev_link_channel_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
strlcpy(mdev_link->channel, page, sizeof(mdev_link->channel));
strim(mdev_link->channel);
return count;
}
static ssize_t mdev_link_comp_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%s\n", to_mdev_link(item)->comp);
}
static ssize_t mdev_link_comp_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
strlcpy(mdev_link->comp, page, sizeof(mdev_link->comp));
strim(mdev_link->comp);
return count;
}
static ssize_t mdev_link_comp_params_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%s\n",
to_mdev_link(item)->comp_params);
}
static ssize_t mdev_link_comp_params_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
strlcpy(mdev_link->comp_params, page, sizeof(mdev_link->comp_params));
strim(mdev_link->comp_params);
return count;
}
static ssize_t mdev_link_num_buffers_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%d\n",
to_mdev_link(item)->num_buffers);
}
static ssize_t mdev_link_num_buffers_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
int ret;
ret = kstrtou16(page, 0, &mdev_link->num_buffers);
if (ret)
return ret;
return count;
}
static ssize_t mdev_link_buffer_size_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%d\n",
to_mdev_link(item)->buffer_size);
}
static ssize_t mdev_link_buffer_size_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
int ret;
ret = kstrtou16(page, 0, &mdev_link->buffer_size);
if (ret)
return ret;
return count;
}
static ssize_t mdev_link_subbuffer_size_show(struct config_item *item,
char *page)
{
return snprintf(page, PAGE_SIZE, "%d\n",
to_mdev_link(item)->subbuffer_size);
}
static ssize_t mdev_link_subbuffer_size_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
int ret;
ret = kstrtou16(page, 0, &mdev_link->subbuffer_size);
if (ret)
return ret;
return count;
}
static ssize_t mdev_link_packets_per_xact_show(struct config_item *item,
char *page)
{
return snprintf(page, PAGE_SIZE, "%d\n",
to_mdev_link(item)->packets_per_xact);
}
static ssize_t mdev_link_packets_per_xact_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
int ret;
ret = kstrtou16(page, 0, &mdev_link->packets_per_xact);
if (ret)
return ret;
return count;
}
static ssize_t mdev_link_dbr_size_show(struct config_item *item, char *page)
{
return snprintf(page, PAGE_SIZE, "%d\n", to_mdev_link(item)->dbr_size);
}
static ssize_t mdev_link_dbr_size_store(struct config_item *item,
const char *page, size_t count)
{
struct mdev_link *mdev_link = to_mdev_link(item);
int ret;
ret = kstrtou16(page, 0, &mdev_link->dbr_size);
if (ret)
return ret;
return count;
}
CONFIGFS_ATTR_WO(mdev_link_, create_link);
CONFIGFS_ATTR_WO(mdev_link_, destroy_link);
CONFIGFS_ATTR(mdev_link_, device);
CONFIGFS_ATTR(mdev_link_, channel);
CONFIGFS_ATTR(mdev_link_, comp);
CONFIGFS_ATTR(mdev_link_, comp_params);
CONFIGFS_ATTR(mdev_link_, num_buffers);
CONFIGFS_ATTR(mdev_link_, buffer_size);
CONFIGFS_ATTR(mdev_link_, subbuffer_size);
CONFIGFS_ATTR(mdev_link_, packets_per_xact);
CONFIGFS_ATTR(mdev_link_, datatype);
CONFIGFS_ATTR(mdev_link_, direction);
CONFIGFS_ATTR(mdev_link_, dbr_size);
static struct configfs_attribute *mdev_link_attrs[] = {
&mdev_link_attr_create_link,
&mdev_link_attr_destroy_link,
&mdev_link_attr_device,
&mdev_link_attr_channel,
&mdev_link_attr_comp,
&mdev_link_attr_comp_params,
&mdev_link_attr_num_buffers,
&mdev_link_attr_buffer_size,
&mdev_link_attr_subbuffer_size,
&mdev_link_attr_packets_per_xact,
&mdev_link_attr_datatype,
&mdev_link_attr_direction,
&mdev_link_attr_dbr_size,
NULL,
};
static void mdev_link_release(struct config_item *item)
{
struct mdev_link *mdev_link = to_mdev_link(item);
int ret;
if (mdev_link->destroy_link)
goto free_item;
ret = most_remove_link(mdev_link->device, mdev_link->channel,
mdev_link->comp);
if (ret) {
pr_err("Removing link failed.\n");
goto free_item;
}
if (!list_empty(&mdev_link_list))
list_del(&mdev_link->list);
free_item:
kfree(to_mdev_link(item));
}
static struct configfs_item_operations mdev_link_item_ops = {
.release = mdev_link_release,
};
static const struct config_item_type mdev_link_type = {
.ct_item_ops = &mdev_link_item_ops,
.ct_attrs = mdev_link_attrs,
.ct_owner = THIS_MODULE,
};
struct most_common {
struct config_group group;
struct module *mod;
struct configfs_subsystem subsys;
};
static struct most_common *to_most_common(struct configfs_subsystem *subsys)
{
return container_of(subsys, struct most_common, subsys);
}
static struct config_item *most_common_make_item(struct config_group *group,
const char *name)
{
struct mdev_link *mdev_link;
struct most_common *mc = to_most_common(group->cg_subsys);
mdev_link = kzalloc(sizeof(*mdev_link), GFP_KERNEL);
if (!mdev_link)
return ERR_PTR(-ENOMEM);
if (!try_module_get(mc->mod)) {
kfree(mdev_link);
return ERR_PTR(-ENOLCK);
}
config_item_init_type_name(&mdev_link->item, name,
&mdev_link_type);
if (!strcmp(group->cg_item.ci_namebuf, "most_cdev"))
strcpy(mdev_link->comp, "cdev");
else if (!strcmp(group->cg_item.ci_namebuf, "most_net"))
strcpy(mdev_link->comp, "net");
else if (!strcmp(group->cg_item.ci_namebuf, "most_video"))
strcpy(mdev_link->comp, "video");
strcpy(mdev_link->name, name);
return &mdev_link->item;
}
static void most_common_release(struct config_item *item)
{
struct config_group *group = to_config_group(item);
kfree(to_most_common(group->cg_subsys));
}
static struct configfs_item_operations most_common_item_ops = {
.release = most_common_release,
};
static void most_common_disconnect(struct config_group *group,
struct config_item *item)
{
struct most_common *mc = to_most_common(group->cg_subsys);
module_put(mc->mod);
}
static struct configfs_group_operations most_common_group_ops = {
.make_item = most_common_make_item,
.disconnect_notify = most_common_disconnect,
};
static const struct config_item_type most_common_type = {
.ct_item_ops = &most_common_item_ops,
.ct_group_ops = &most_common_group_ops,
.ct_owner = THIS_MODULE,
};
static struct most_common most_cdev = {
.subsys = {
.su_group = {
.cg_item = {
.ci_namebuf = "most_cdev",
.ci_type = &most_common_type,
},
},
},
};
static struct most_common most_net = {
.subsys = {
.su_group = {
.cg_item = {
.ci_namebuf = "most_net",
.ci_type = &most_common_type,
},
},
},
};
static struct most_common most_video = {
.subsys = {
.su_group = {
.cg_item = {
.ci_namebuf = "most_video",
.ci_type = &most_common_type,
},
},
},
};
struct most_snd_grp {
struct config_group group;
bool create_card;
struct list_head list;
};
static struct most_snd_grp *to_most_snd_grp(struct config_item *item)
{
return container_of(to_config_group(item), struct most_snd_grp, group);
}
static struct config_item *most_snd_grp_make_item(struct config_group *group,
const char *name)
{
struct mdev_link *mdev_link;
mdev_link = kzalloc(sizeof(*mdev_link), GFP_KERNEL);
if (!mdev_link)
return ERR_PTR(-ENOMEM);
config_item_init_type_name(&mdev_link->item, name, &mdev_link_type);
mdev_link->create_link = false;
strcpy(mdev_link->name, name);
strcpy(mdev_link->comp, "sound");
return &mdev_link->item;
}
static ssize_t most_snd_grp_create_card_store(struct config_item *item,
const char *page, size_t count)
{
struct most_snd_grp *snd_grp = to_most_snd_grp(item);
int ret;
bool tmp;
ret = kstrtobool(page, &tmp);
if (ret)
return ret;
if (tmp) {
ret = most_cfg_complete("sound");
if (ret)
return ret;
}
snd_grp->create_card = tmp;
return count;
}
CONFIGFS_ATTR_WO(most_snd_grp_, create_card);
static struct configfs_attribute *most_snd_grp_attrs[] = {
&most_snd_grp_attr_create_card,
NULL,
};
static void most_snd_grp_release(struct config_item *item)
{
struct most_snd_grp *group = to_most_snd_grp(item);
list_del(&group->list);
kfree(group);
}
static struct configfs_item_operations most_snd_grp_item_ops = {
.release = most_snd_grp_release,
};
static struct configfs_group_operations most_snd_grp_group_ops = {
.make_item = most_snd_grp_make_item,
};
static const struct config_item_type most_snd_grp_type = {
.ct_item_ops = &most_snd_grp_item_ops,
.ct_group_ops = &most_snd_grp_group_ops,
.ct_attrs = most_snd_grp_attrs,
.ct_owner = THIS_MODULE,
};
struct most_sound {
struct configfs_subsystem subsys;
struct list_head soundcard_list;
struct module *mod;
};
static struct config_group *most_sound_make_group(struct config_group *group,
const char *name)
{
struct most_snd_grp *most;
struct most_sound *ms = container_of(group->cg_subsys,
struct most_sound, subsys);
list_for_each_entry(most, &ms->soundcard_list, list) {
if (!most->create_card) {
pr_info("adapter configuration still in progress.\n");
return ERR_PTR(-EPROTO);
}
}
if (!try_module_get(ms->mod))
return ERR_PTR(-ENOLCK);
most = kzalloc(sizeof(*most), GFP_KERNEL);
if (!most) {
module_put(ms->mod);
return ERR_PTR(-ENOMEM);
}
config_group_init_type_name(&most->group, name, &most_snd_grp_type);
list_add_tail(&most->list, &ms->soundcard_list);
return &most->group;
}
static void most_sound_disconnect(struct config_group *group,
struct config_item *item)
{
struct most_sound *ms = container_of(group->cg_subsys,
struct most_sound, subsys);
module_put(ms->mod);
}
static struct configfs_group_operations most_sound_group_ops = {
.make_group = most_sound_make_group,
.disconnect_notify = most_sound_disconnect,
};
static const struct config_item_type most_sound_type = {
.ct_group_ops = &most_sound_group_ops,
.ct_owner = THIS_MODULE,
};
static struct most_sound most_sound_subsys = {
.subsys = {
.su_group = {
.cg_item = {
.ci_namebuf = "most_sound",
.ci_type = &most_sound_type,
},
},
},
};
int most_register_configfs_subsys(struct most_component *c)
{
int ret;
if (!strcmp(c->name, "cdev")) {
most_cdev.mod = c->mod;
ret = configfs_register_subsystem(&most_cdev.subsys);
} else if (!strcmp(c->name, "net")) {
most_net.mod = c->mod;
ret = configfs_register_subsystem(&most_net.subsys);
} else if (!strcmp(c->name, "video")) {
most_video.mod = c->mod;
ret = configfs_register_subsystem(&most_video.subsys);
} else if (!strcmp(c->name, "sound")) {
most_sound_subsys.mod = c->mod;
ret = configfs_register_subsystem(&most_sound_subsys.subsys);
} else {
return -ENODEV;
}
if (ret) {
pr_err("Error %d while registering subsystem %s\n",
ret, c->name);
}
return ret;
}
EXPORT_SYMBOL_GPL(most_register_configfs_subsys);
void most_interface_register_notify(const char *mdev)
{
bool register_snd_card = false;
struct mdev_link *mdev_link;
list_for_each_entry(mdev_link, &mdev_link_list, list) {
if (!strcmp(mdev_link->device, mdev)) {
set_config_and_add_link(mdev_link);
if (!strcmp(mdev_link->comp, "sound"))
register_snd_card = true;
}
}
if (register_snd_card)
most_cfg_complete("sound");
}
void most_deregister_configfs_subsys(struct most_component *c)
{
if (!strcmp(c->name, "cdev"))
configfs_unregister_subsystem(&most_cdev.subsys);
else if (!strcmp(c->name, "net"))
configfs_unregister_subsystem(&most_net.subsys);
else if (!strcmp(c->name, "video"))
configfs_unregister_subsystem(&most_video.subsys);
else if (!strcmp(c->name, "sound"))
configfs_unregister_subsystem(&most_sound_subsys.subsys);
}
EXPORT_SYMBOL_GPL(most_deregister_configfs_subsys);
int __init configfs_init(void)
{
config_group_init(&most_cdev.subsys.su_group);
mutex_init(&most_cdev.subsys.su_mutex);
config_group_init(&most_net.subsys.su_group);
mutex_init(&most_net.subsys.su_mutex);
config_group_init(&most_video.subsys.su_group);
mutex_init(&most_video.subsys.su_mutex);
config_group_init(&most_sound_subsys.subsys.su_group);
mutex_init(&most_sound_subsys.subsys.su_mutex);
INIT_LIST_HEAD(&most_sound_subsys.soundcard_list);
INIT_LIST_HEAD(&mdev_link_list);
return 0;
}
|