ACPI / scan: Add labels for PNP button devices
[sfrench/cifs-2.6.git] / drivers / staging / most / core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * core.c - Implementation of core module of MOST Linux driver stack
4  *
5  * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/list.h>
15 #include <linux/poll.h>
16 #include <linux/wait.h>
17 #include <linux/kobject.h>
18 #include <linux/mutex.h>
19 #include <linux/completion.h>
20 #include <linux/sysfs.h>
21 #include <linux/kthread.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/idr.h>
24 #include <most/core.h>
25
26 #define MAX_CHANNELS    64
27 #define STRING_SIZE     80
28
29 static struct ida mdev_id;
30 static int dummy_num_buffers;
31
32 static struct mostcore {
33         struct device dev;
34         struct device_driver drv;
35         struct bus_type bus;
36         struct list_head comp_list;
37 } mc;
38
39 #define to_driver(d) container_of(d, struct mostcore, drv)
40
41 struct pipe {
42         struct core_component *comp;
43         int refs;
44         int num_buffers;
45 };
46
47 struct most_channel {
48         struct device dev;
49         struct completion cleanup;
50         atomic_t mbo_ref;
51         atomic_t mbo_nq_level;
52         u16 channel_id;
53         char name[STRING_SIZE];
54         bool is_poisoned;
55         struct mutex start_mutex;
56         struct mutex nq_mutex; /* nq thread synchronization */
57         int is_starving;
58         struct most_interface *iface;
59         struct most_channel_config cfg;
60         bool keep_mbo;
61         bool enqueue_halt;
62         struct list_head fifo;
63         spinlock_t fifo_lock;
64         struct list_head halt_fifo;
65         struct list_head list;
66         struct pipe pipe0;
67         struct pipe pipe1;
68         struct list_head trash_fifo;
69         struct task_struct *hdm_enqueue_task;
70         wait_queue_head_t hdm_fifo_wq;
71
72 };
73
74 #define to_channel(d) container_of(d, struct most_channel, dev)
75
76 struct interface_private {
77         int dev_id;
78         char name[STRING_SIZE];
79         struct most_channel *channel[MAX_CHANNELS];
80         struct list_head channel_list;
81 };
82
83 static const struct {
84         int most_ch_data_type;
85         const char *name;
86 } ch_data_type[] = {
87         { MOST_CH_CONTROL, "control\n" },
88         { MOST_CH_ASYNC, "async\n" },
89         { MOST_CH_SYNC, "sync\n" },
90         { MOST_CH_ISOC, "isoc\n"},
91         { MOST_CH_ISOC, "isoc_avp\n"},
92 };
93
94 /**
95  * list_pop_mbo - retrieves the first MBO of the list and removes it
96  * @ptr: the list head to grab the MBO from.
97  */
98 #define list_pop_mbo(ptr)                                               \
99 ({                                                                      \
100         struct mbo *_mbo = list_first_entry(ptr, struct mbo, list);     \
101         list_del(&_mbo->list);                                          \
102         _mbo;                                                           \
103 })
104
105 /**
106  * most_free_mbo_coherent - free an MBO and its coherent buffer
107  * @mbo: most buffer
108  */
109 static void most_free_mbo_coherent(struct mbo *mbo)
110 {
111         struct most_channel *c = mbo->context;
112         u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
113
114         if (c->iface->dma_free)
115                 c->iface->dma_free(mbo, coherent_buf_size);
116         else
117                 kfree(mbo->virt_address);
118         kfree(mbo);
119         if (atomic_sub_and_test(1, &c->mbo_ref))
120                 complete(&c->cleanup);
121 }
122
123 /**
124  * flush_channel_fifos - clear the channel fifos
125  * @c: pointer to channel object
126  */
127 static void flush_channel_fifos(struct most_channel *c)
128 {
129         unsigned long flags, hf_flags;
130         struct mbo *mbo, *tmp;
131
132         if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
133                 return;
134
135         spin_lock_irqsave(&c->fifo_lock, flags);
136         list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
137                 list_del(&mbo->list);
138                 spin_unlock_irqrestore(&c->fifo_lock, flags);
139                 most_free_mbo_coherent(mbo);
140                 spin_lock_irqsave(&c->fifo_lock, flags);
141         }
142         spin_unlock_irqrestore(&c->fifo_lock, flags);
143
144         spin_lock_irqsave(&c->fifo_lock, hf_flags);
145         list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
146                 list_del(&mbo->list);
147                 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
148                 most_free_mbo_coherent(mbo);
149                 spin_lock_irqsave(&c->fifo_lock, hf_flags);
150         }
151         spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
152
153         if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
154                 pr_info("WARN: fifo | trash fifo not empty\n");
155 }
156
157 /**
158  * flush_trash_fifo - clear the trash fifo
159  * @c: pointer to channel object
160  */
161 static int flush_trash_fifo(struct most_channel *c)
162 {
163         struct mbo *mbo, *tmp;
164         unsigned long flags;
165
166         spin_lock_irqsave(&c->fifo_lock, flags);
167         list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
168                 list_del(&mbo->list);
169                 spin_unlock_irqrestore(&c->fifo_lock, flags);
170                 most_free_mbo_coherent(mbo);
171                 spin_lock_irqsave(&c->fifo_lock, flags);
172         }
173         spin_unlock_irqrestore(&c->fifo_lock, flags);
174         return 0;
175 }
176
177 static ssize_t available_directions_show(struct device *dev,
178                                          struct device_attribute *attr,
179                                          char *buf)
180 {
181         struct most_channel *c = to_channel(dev);
182         unsigned int i = c->channel_id;
183
184         strcpy(buf, "");
185         if (c->iface->channel_vector[i].direction & MOST_CH_RX)
186                 strcat(buf, "rx ");
187         if (c->iface->channel_vector[i].direction & MOST_CH_TX)
188                 strcat(buf, "tx ");
189         strcat(buf, "\n");
190         return strlen(buf);
191 }
192
193 static ssize_t available_datatypes_show(struct device *dev,
194                                         struct device_attribute *attr,
195                                         char *buf)
196 {
197         struct most_channel *c = to_channel(dev);
198         unsigned int i = c->channel_id;
199
200         strcpy(buf, "");
201         if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
202                 strcat(buf, "control ");
203         if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
204                 strcat(buf, "async ");
205         if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
206                 strcat(buf, "sync ");
207         if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
208                 strcat(buf, "isoc ");
209         strcat(buf, "\n");
210         return strlen(buf);
211 }
212
213 static ssize_t number_of_packet_buffers_show(struct device *dev,
214                                              struct device_attribute *attr,
215                                              char *buf)
216 {
217         struct most_channel *c = to_channel(dev);
218         unsigned int i = c->channel_id;
219
220         return snprintf(buf, PAGE_SIZE, "%d\n",
221                         c->iface->channel_vector[i].num_buffers_packet);
222 }
223
224 static ssize_t number_of_stream_buffers_show(struct device *dev,
225                                              struct device_attribute *attr,
226                                              char *buf)
227 {
228         struct most_channel *c = to_channel(dev);
229         unsigned int i = c->channel_id;
230
231         return snprintf(buf, PAGE_SIZE, "%d\n",
232                         c->iface->channel_vector[i].num_buffers_streaming);
233 }
234
235 static ssize_t size_of_packet_buffer_show(struct device *dev,
236                                           struct device_attribute *attr,
237                                           char *buf)
238 {
239         struct most_channel *c = to_channel(dev);
240         unsigned int i = c->channel_id;
241
242         return snprintf(buf, PAGE_SIZE, "%d\n",
243                         c->iface->channel_vector[i].buffer_size_packet);
244 }
245
246 static ssize_t size_of_stream_buffer_show(struct device *dev,
247                                           struct device_attribute *attr,
248                                           char *buf)
249 {
250         struct most_channel *c = to_channel(dev);
251         unsigned int i = c->channel_id;
252
253         return snprintf(buf, PAGE_SIZE, "%d\n",
254                         c->iface->channel_vector[i].buffer_size_streaming);
255 }
256
257 static ssize_t channel_starving_show(struct device *dev,
258                                      struct device_attribute *attr,
259                                      char *buf)
260 {
261         struct most_channel *c = to_channel(dev);
262
263         return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
264 }
265
266 static ssize_t set_number_of_buffers_show(struct device *dev,
267                                           struct device_attribute *attr,
268                                           char *buf)
269 {
270         struct most_channel *c = to_channel(dev);
271
272         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
273 }
274
275 static ssize_t set_number_of_buffers_store(struct device *dev,
276                                            struct device_attribute *attr,
277                                            const char *buf,
278                                            size_t count)
279 {
280         struct most_channel *c = to_channel(dev);
281         int ret = kstrtou16(buf, 0, &c->cfg.num_buffers);
282
283         if (ret)
284                 return ret;
285         return count;
286 }
287
288 static ssize_t set_buffer_size_show(struct device *dev,
289                                     struct device_attribute *attr,
290                                     char *buf)
291 {
292         struct most_channel *c = to_channel(dev);
293
294         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
295 }
296
297 static ssize_t set_buffer_size_store(struct device *dev,
298                                      struct device_attribute *attr,
299                                      const char *buf,
300                                      size_t count)
301 {
302         struct most_channel *c = to_channel(dev);
303         int ret = kstrtou16(buf, 0, &c->cfg.buffer_size);
304
305         if (ret)
306                 return ret;
307         return count;
308 }
309
310 static ssize_t set_direction_show(struct device *dev,
311                                   struct device_attribute *attr,
312                                   char *buf)
313 {
314         struct most_channel *c = to_channel(dev);
315
316         if (c->cfg.direction & MOST_CH_TX)
317                 return snprintf(buf, PAGE_SIZE, "tx\n");
318         else if (c->cfg.direction & MOST_CH_RX)
319                 return snprintf(buf, PAGE_SIZE, "rx\n");
320         return snprintf(buf, PAGE_SIZE, "unconfigured\n");
321 }
322
323 static ssize_t set_direction_store(struct device *dev,
324                                    struct device_attribute *attr,
325                                    const char *buf,
326                                    size_t count)
327 {
328         struct most_channel *c = to_channel(dev);
329
330         if (!strcmp(buf, "dir_rx\n")) {
331                 c->cfg.direction = MOST_CH_RX;
332         } else if (!strcmp(buf, "rx\n")) {
333                 c->cfg.direction = MOST_CH_RX;
334         } else if (!strcmp(buf, "dir_tx\n")) {
335                 c->cfg.direction = MOST_CH_TX;
336         } else if (!strcmp(buf, "tx\n")) {
337                 c->cfg.direction = MOST_CH_TX;
338         } else {
339                 pr_info("WARN: invalid attribute settings\n");
340                 return -EINVAL;
341         }
342         return count;
343 }
344
345 static ssize_t set_datatype_show(struct device *dev,
346                                  struct device_attribute *attr,
347                                  char *buf)
348 {
349         int i;
350         struct most_channel *c = to_channel(dev);
351
352         for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
353                 if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
354                         return snprintf(buf, PAGE_SIZE, "%s", ch_data_type[i].name);
355         }
356         return snprintf(buf, PAGE_SIZE, "unconfigured\n");
357 }
358
359 static ssize_t set_datatype_store(struct device *dev,
360                                   struct device_attribute *attr,
361                                   const char *buf,
362                                   size_t count)
363 {
364         int i;
365         struct most_channel *c = to_channel(dev);
366
367         for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
368                 if (!strcmp(buf, ch_data_type[i].name)) {
369                         c->cfg.data_type = ch_data_type[i].most_ch_data_type;
370                         break;
371                 }
372         }
373
374         if (i == ARRAY_SIZE(ch_data_type)) {
375                 pr_info("WARN: invalid attribute settings\n");
376                 return -EINVAL;
377         }
378         return count;
379 }
380
381 static ssize_t set_subbuffer_size_show(struct device *dev,
382                                        struct device_attribute *attr,
383                                        char *buf)
384 {
385         struct most_channel *c = to_channel(dev);
386
387         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
388 }
389
390 static ssize_t set_subbuffer_size_store(struct device *dev,
391                                         struct device_attribute *attr,
392                                         const char *buf,
393                                         size_t count)
394 {
395         struct most_channel *c = to_channel(dev);
396         int ret = kstrtou16(buf, 0, &c->cfg.subbuffer_size);
397
398         if (ret)
399                 return ret;
400         return count;
401 }
402
403 static ssize_t set_packets_per_xact_show(struct device *dev,
404                                          struct device_attribute *attr,
405                                          char *buf)
406 {
407         struct most_channel *c = to_channel(dev);
408
409         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
410 }
411
412 static ssize_t set_packets_per_xact_store(struct device *dev,
413                                           struct device_attribute *attr,
414                                           const char *buf,
415                                           size_t count)
416 {
417         struct most_channel *c = to_channel(dev);
418         int ret = kstrtou16(buf, 0, &c->cfg.packets_per_xact);
419
420         if (ret)
421                 return ret;
422         return count;
423 }
424
425 static ssize_t set_dbr_size_show(struct device *dev,
426                                  struct device_attribute *attr, char *buf)
427 {
428         struct most_channel *c = to_channel(dev);
429
430         return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.dbr_size);
431 }
432
433 static ssize_t set_dbr_size_store(struct device *dev,
434                                   struct device_attribute *attr,
435                                   const char *buf, size_t count)
436 {
437         struct most_channel *c = to_channel(dev);
438         int ret = kstrtou16(buf, 0, &c->cfg.dbr_size);
439
440         if (ret)
441                 return ret;
442         return count;
443 }
444
445 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
446 static umode_t channel_attr_is_visible(struct kobject *kobj,
447                                        struct attribute *attr, int index)
448 {
449         struct device_attribute *dev_attr = to_dev_attr(attr);
450         struct device *dev = kobj_to_dev(kobj);
451         struct most_channel *c = to_channel(dev);
452
453         if (!strcmp(dev_attr->attr.name, "set_dbr_size") &&
454             (c->iface->interface != ITYPE_MEDIALB_DIM2))
455                 return 0;
456         if (!strcmp(dev_attr->attr.name, "set_packets_per_xact") &&
457             (c->iface->interface != ITYPE_USB))
458                 return 0;
459
460         return attr->mode;
461 }
462
463 #define DEV_ATTR(_name)  (&dev_attr_##_name.attr)
464
465 static DEVICE_ATTR_RO(available_directions);
466 static DEVICE_ATTR_RO(available_datatypes);
467 static DEVICE_ATTR_RO(number_of_packet_buffers);
468 static DEVICE_ATTR_RO(number_of_stream_buffers);
469 static DEVICE_ATTR_RO(size_of_stream_buffer);
470 static DEVICE_ATTR_RO(size_of_packet_buffer);
471 static DEVICE_ATTR_RO(channel_starving);
472 static DEVICE_ATTR_RW(set_buffer_size);
473 static DEVICE_ATTR_RW(set_number_of_buffers);
474 static DEVICE_ATTR_RW(set_direction);
475 static DEVICE_ATTR_RW(set_datatype);
476 static DEVICE_ATTR_RW(set_subbuffer_size);
477 static DEVICE_ATTR_RW(set_packets_per_xact);
478 static DEVICE_ATTR_RW(set_dbr_size);
479
480 static struct attribute *channel_attrs[] = {
481         DEV_ATTR(available_directions),
482         DEV_ATTR(available_datatypes),
483         DEV_ATTR(number_of_packet_buffers),
484         DEV_ATTR(number_of_stream_buffers),
485         DEV_ATTR(size_of_stream_buffer),
486         DEV_ATTR(size_of_packet_buffer),
487         DEV_ATTR(channel_starving),
488         DEV_ATTR(set_buffer_size),
489         DEV_ATTR(set_number_of_buffers),
490         DEV_ATTR(set_direction),
491         DEV_ATTR(set_datatype),
492         DEV_ATTR(set_subbuffer_size),
493         DEV_ATTR(set_packets_per_xact),
494         DEV_ATTR(set_dbr_size),
495         NULL,
496 };
497
498 static struct attribute_group channel_attr_group = {
499         .attrs = channel_attrs,
500         .is_visible = channel_attr_is_visible,
501 };
502
503 static const struct attribute_group *channel_attr_groups[] = {
504         &channel_attr_group,
505         NULL,
506 };
507
508 static ssize_t description_show(struct device *dev,
509                                 struct device_attribute *attr,
510                                 char *buf)
511 {
512         struct most_interface *iface = to_most_interface(dev);
513
514         return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
515 }
516
517 static ssize_t interface_show(struct device *dev,
518                               struct device_attribute *attr,
519                               char *buf)
520 {
521         struct most_interface *iface = to_most_interface(dev);
522
523         switch (iface->interface) {
524         case ITYPE_LOOPBACK:
525                 return snprintf(buf, PAGE_SIZE, "loopback\n");
526         case ITYPE_I2C:
527                 return snprintf(buf, PAGE_SIZE, "i2c\n");
528         case ITYPE_I2S:
529                 return snprintf(buf, PAGE_SIZE, "i2s\n");
530         case ITYPE_TSI:
531                 return snprintf(buf, PAGE_SIZE, "tsi\n");
532         case ITYPE_HBI:
533                 return snprintf(buf, PAGE_SIZE, "hbi\n");
534         case ITYPE_MEDIALB_DIM:
535                 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
536         case ITYPE_MEDIALB_DIM2:
537                 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
538         case ITYPE_USB:
539                 return snprintf(buf, PAGE_SIZE, "usb\n");
540         case ITYPE_PCIE:
541                 return snprintf(buf, PAGE_SIZE, "pcie\n");
542         }
543         return snprintf(buf, PAGE_SIZE, "unknown\n");
544 }
545
546 static DEVICE_ATTR_RO(description);
547 static DEVICE_ATTR_RO(interface);
548
549 static struct attribute *interface_attrs[] = {
550         DEV_ATTR(description),
551         DEV_ATTR(interface),
552         NULL,
553 };
554
555 static struct attribute_group interface_attr_group = {
556         .attrs = interface_attrs,
557 };
558
559 static const struct attribute_group *interface_attr_groups[] = {
560         &interface_attr_group,
561         NULL,
562 };
563
564 static struct core_component *match_component(char *name)
565 {
566         struct core_component *comp;
567
568         list_for_each_entry(comp, &mc.comp_list, list) {
569                 if (!strcmp(comp->name, name))
570                         return comp;
571         }
572         return NULL;
573 }
574
575 struct show_links_data {
576         int offs;
577         char *buf;
578 };
579
580 static int print_links(struct device *dev, void *data)
581 {
582         struct show_links_data *d = data;
583         int offs = d->offs;
584         char *buf = d->buf;
585         struct most_channel *c;
586         struct most_interface *iface = to_most_interface(dev);
587
588         list_for_each_entry(c, &iface->p->channel_list, list) {
589                 if (c->pipe0.comp) {
590                         offs += snprintf(buf + offs,
591                                          PAGE_SIZE - offs,
592                                          "%s:%s:%s\n",
593                                          c->pipe0.comp->name,
594                                          dev_name(&iface->dev),
595                                          dev_name(&c->dev));
596                 }
597                 if (c->pipe1.comp) {
598                         offs += snprintf(buf + offs,
599                                          PAGE_SIZE - offs,
600                                          "%s:%s:%s\n",
601                                          c->pipe1.comp->name,
602                                          dev_name(&iface->dev),
603                                          dev_name(&c->dev));
604                 }
605         }
606         d->offs = offs;
607         return 0;
608 }
609
610 static ssize_t links_show(struct device_driver *drv, char *buf)
611 {
612         struct show_links_data d = { .buf = buf };
613
614         bus_for_each_dev(&mc.bus, NULL, &d, print_links);
615         return d.offs;
616 }
617
618 static ssize_t components_show(struct device_driver *drv, char *buf)
619 {
620         struct core_component *comp;
621         int offs = 0;
622
623         list_for_each_entry(comp, &mc.comp_list, list) {
624                 offs += snprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
625                                  comp->name);
626         }
627         return offs;
628 }
629
630 /**
631  * split_string - parses buf and extracts ':' separated substrings.
632  *
633  * @buf: complete string from attribute 'add_channel'
634  * @a: storage for 1st substring (=interface name)
635  * @b: storage for 2nd substring (=channel name)
636  * @c: storage for 3rd substring (=component name)
637  * @d: storage optional 4th substring (=user defined name)
638  *
639  * Examples:
640  *
641  * Input: "mdev0:ch6:cdev:my_channel\n" or
642  *        "mdev0:ch6:cdev:my_channel"
643  *
644  * Output: *a -> "mdev0", *b -> "ch6", *c -> "cdev" *d -> "my_channel"
645  *
646  * Input: "mdev1:ep81:cdev\n"
647  * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d -> ""
648  *
649  * Input: "mdev1:ep81"
650  * Output: *a -> "mdev1", *b -> "ep81", *c -> "cdev" *d == NULL
651  */
652 static int split_string(char *buf, char **a, char **b, char **c, char **d)
653 {
654         *a = strsep(&buf, ":");
655         if (!*a)
656                 return -EIO;
657
658         *b = strsep(&buf, ":\n");
659         if (!*b)
660                 return -EIO;
661
662         *c = strsep(&buf, ":\n");
663         if (!*c)
664                 return -EIO;
665
666         if (d)
667                 *d = strsep(&buf, ":\n");
668
669         return 0;
670 }
671
672 static int match_bus_dev(struct device *dev, void *data)
673 {
674         char *mdev_name = data;
675
676         return !strcmp(dev_name(dev), mdev_name);
677 }
678
679 /**
680  * get_channel - get pointer to channel
681  * @mdev: name of the device interface
682  * @mdev_ch: name of channel
683  */
684 static struct most_channel *get_channel(char *mdev, char *mdev_ch)
685 {
686         struct device *dev = NULL;
687         struct most_interface *iface;
688         struct most_channel *c, *tmp;
689
690         dev = bus_find_device(&mc.bus, NULL, mdev, match_bus_dev);
691         if (!dev)
692                 return NULL;
693         iface = to_most_interface(dev);
694         list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
695                 if (!strcmp(dev_name(&c->dev), mdev_ch))
696                         return c;
697         }
698         return NULL;
699 }
700
701 static
702 inline int link_channel_to_component(struct most_channel *c,
703                                      struct core_component *comp,
704                                      char *comp_param)
705 {
706         int ret;
707         struct core_component **comp_ptr;
708
709         if (!c->pipe0.comp)
710                 comp_ptr = &c->pipe0.comp;
711         else if (!c->pipe1.comp)
712                 comp_ptr = &c->pipe1.comp;
713         else
714                 return -ENOSPC;
715
716         *comp_ptr = comp;
717         ret = comp->probe_channel(c->iface, c->channel_id, &c->cfg, comp_param);
718         if (ret) {
719                 *comp_ptr = NULL;
720                 return ret;
721         }
722         return 0;
723 }
724
725 /**
726  * add_link_store - store function for add_link attribute
727  * @drv: device driver
728  * @buf: buffer
729  * @len: buffer length
730  *
731  * This parses the string given by buf and splits it into
732  * four substrings. Note: last substring is optional. In case a cdev
733  * component is loaded the optional 4th substring will make up the name of
734  * device node in the /dev directory. If omitted, the device node will
735  * inherit the channel's name within sysfs.
736  *
737  * Searches for (device, channel) pair and probes the component
738  *
739  * Example:
740  * (1) echo "mdev0:ch6:cdev:my_rxchannel" >add_link
741  * (2) echo "mdev1:ep81:cdev" >add_link
742  *
743  * (1) would create the device node /dev/my_rxchannel
744  * (2) would create the device node /dev/mdev1-ep81
745  */
746 static ssize_t add_link_store(struct device_driver *drv,
747                               const char *buf,
748                               size_t len)
749 {
750         struct most_channel *c;
751         struct core_component *comp;
752         char buffer[STRING_SIZE];
753         char *mdev;
754         char *mdev_ch;
755         char *comp_name;
756         char *comp_param;
757         char devnod_buf[STRING_SIZE];
758         int ret;
759         size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
760
761         strlcpy(buffer, buf, max_len);
762         ret = split_string(buffer, &mdev, &mdev_ch, &comp_name, &comp_param);
763         if (ret)
764                 return ret;
765         comp = match_component(comp_name);
766         if (!comp)
767                 return -ENODEV;
768         if (!comp_param || *comp_param == 0) {
769                 snprintf(devnod_buf, sizeof(devnod_buf), "%s-%s", mdev,
770                          mdev_ch);
771                 comp_param = devnod_buf;
772         }
773
774         c = get_channel(mdev, mdev_ch);
775         if (!c)
776                 return -ENODEV;
777
778         ret = link_channel_to_component(c, comp, comp_param);
779         if (ret)
780                 return ret;
781         return len;
782 }
783
784 /**
785  * remove_link_store - store function for remove_link attribute
786  * @drv: device driver
787  * @buf: buffer
788  * @len: buffer length
789  *
790  * Example:
791  * echo "mdev0:ep81" >remove_link
792  */
793 static ssize_t remove_link_store(struct device_driver *drv,
794                                  const char *buf,
795                                  size_t len)
796 {
797         struct most_channel *c;
798         struct core_component *comp;
799         char buffer[STRING_SIZE];
800         char *mdev;
801         char *mdev_ch;
802         char *comp_name;
803         int ret;
804         size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
805
806         strlcpy(buffer, buf, max_len);
807         ret = split_string(buffer, &mdev, &mdev_ch, &comp_name, NULL);
808         if (ret)
809                 return ret;
810         comp = match_component(comp_name);
811         if (!comp)
812                 return -ENODEV;
813         c = get_channel(mdev, mdev_ch);
814         if (!c)
815                 return -ENODEV;
816
817         if (comp->disconnect_channel(c->iface, c->channel_id))
818                 return -EIO;
819         if (c->pipe0.comp == comp)
820                 c->pipe0.comp = NULL;
821         if (c->pipe1.comp == comp)
822                 c->pipe1.comp = NULL;
823         return len;
824 }
825
826 #define DRV_ATTR(_name)  (&driver_attr_##_name.attr)
827
828 static DRIVER_ATTR_RO(links);
829 static DRIVER_ATTR_RO(components);
830 static DRIVER_ATTR_WO(add_link);
831 static DRIVER_ATTR_WO(remove_link);
832
833 static struct attribute *mc_attrs[] = {
834         DRV_ATTR(links),
835         DRV_ATTR(components),
836         DRV_ATTR(add_link),
837         DRV_ATTR(remove_link),
838         NULL,
839 };
840
841 static struct attribute_group mc_attr_group = {
842         .attrs = mc_attrs,
843 };
844
845 static const struct attribute_group *mc_attr_groups[] = {
846         &mc_attr_group,
847         NULL,
848 };
849
850 static int most_match(struct device *dev, struct device_driver *drv)
851 {
852         if (!strcmp(dev_name(dev), "most"))
853                 return 0;
854         else
855                 return 1;
856 }
857
858 static inline void trash_mbo(struct mbo *mbo)
859 {
860         unsigned long flags;
861         struct most_channel *c = mbo->context;
862
863         spin_lock_irqsave(&c->fifo_lock, flags);
864         list_add(&mbo->list, &c->trash_fifo);
865         spin_unlock_irqrestore(&c->fifo_lock, flags);
866 }
867
868 static bool hdm_mbo_ready(struct most_channel *c)
869 {
870         bool empty;
871
872         if (c->enqueue_halt)
873                 return false;
874
875         spin_lock_irq(&c->fifo_lock);
876         empty = list_empty(&c->halt_fifo);
877         spin_unlock_irq(&c->fifo_lock);
878
879         return !empty;
880 }
881
882 static void nq_hdm_mbo(struct mbo *mbo)
883 {
884         unsigned long flags;
885         struct most_channel *c = mbo->context;
886
887         spin_lock_irqsave(&c->fifo_lock, flags);
888         list_add_tail(&mbo->list, &c->halt_fifo);
889         spin_unlock_irqrestore(&c->fifo_lock, flags);
890         wake_up_interruptible(&c->hdm_fifo_wq);
891 }
892
893 static int hdm_enqueue_thread(void *data)
894 {
895         struct most_channel *c = data;
896         struct mbo *mbo;
897         int ret;
898         typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
899
900         while (likely(!kthread_should_stop())) {
901                 wait_event_interruptible(c->hdm_fifo_wq,
902                                          hdm_mbo_ready(c) ||
903                                          kthread_should_stop());
904
905                 mutex_lock(&c->nq_mutex);
906                 spin_lock_irq(&c->fifo_lock);
907                 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
908                         spin_unlock_irq(&c->fifo_lock);
909                         mutex_unlock(&c->nq_mutex);
910                         continue;
911                 }
912
913                 mbo = list_pop_mbo(&c->halt_fifo);
914                 spin_unlock_irq(&c->fifo_lock);
915
916                 if (c->cfg.direction == MOST_CH_RX)
917                         mbo->buffer_length = c->cfg.buffer_size;
918
919                 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
920                 mutex_unlock(&c->nq_mutex);
921
922                 if (unlikely(ret)) {
923                         pr_err("hdm enqueue failed\n");
924                         nq_hdm_mbo(mbo);
925                         c->hdm_enqueue_task = NULL;
926                         return 0;
927                 }
928         }
929
930         return 0;
931 }
932
933 static int run_enqueue_thread(struct most_channel *c, int channel_id)
934 {
935         struct task_struct *task =
936                 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
937                             channel_id);
938
939         if (IS_ERR(task))
940                 return PTR_ERR(task);
941
942         c->hdm_enqueue_task = task;
943         return 0;
944 }
945
946 /**
947  * arm_mbo - recycle MBO for further usage
948  * @mbo: most buffer
949  *
950  * This puts an MBO back to the list to have it ready for up coming
951  * tx transactions.
952  *
953  * In case the MBO belongs to a channel that recently has been
954  * poisoned, the MBO is scheduled to be trashed.
955  * Calls the completion handler of an attached component.
956  */
957 static void arm_mbo(struct mbo *mbo)
958 {
959         unsigned long flags;
960         struct most_channel *c;
961
962         c = mbo->context;
963
964         if (c->is_poisoned) {
965                 trash_mbo(mbo);
966                 return;
967         }
968
969         spin_lock_irqsave(&c->fifo_lock, flags);
970         ++*mbo->num_buffers_ptr;
971         list_add_tail(&mbo->list, &c->fifo);
972         spin_unlock_irqrestore(&c->fifo_lock, flags);
973
974         if (c->pipe0.refs && c->pipe0.comp->tx_completion)
975                 c->pipe0.comp->tx_completion(c->iface, c->channel_id);
976
977         if (c->pipe1.refs && c->pipe1.comp->tx_completion)
978                 c->pipe1.comp->tx_completion(c->iface, c->channel_id);
979 }
980
981 /**
982  * arm_mbo_chain - helper function that arms an MBO chain for the HDM
983  * @c: pointer to interface channel
984  * @dir: direction of the channel
985  * @compl: pointer to completion function
986  *
987  * This allocates buffer objects including the containing DMA coherent
988  * buffer and puts them in the fifo.
989  * Buffers of Rx channels are put in the kthread fifo, hence immediately
990  * submitted to the HDM.
991  *
992  * Returns the number of allocated and enqueued MBOs.
993  */
994 static int arm_mbo_chain(struct most_channel *c, int dir,
995                          void (*compl)(struct mbo *))
996 {
997         unsigned int i;
998         struct mbo *mbo;
999         unsigned long flags;
1000         u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
1001
1002         atomic_set(&c->mbo_nq_level, 0);
1003
1004         for (i = 0; i < c->cfg.num_buffers; i++) {
1005                 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
1006                 if (!mbo)
1007                         goto flush_fifos;
1008
1009                 mbo->context = c;
1010                 mbo->ifp = c->iface;
1011                 mbo->hdm_channel_id = c->channel_id;
1012                 if (c->iface->dma_alloc) {
1013                         mbo->virt_address =
1014                                 c->iface->dma_alloc(mbo, coherent_buf_size);
1015                 } else {
1016                         mbo->virt_address =
1017                                 kzalloc(coherent_buf_size, GFP_KERNEL);
1018                 }
1019                 if (!mbo->virt_address)
1020                         goto release_mbo;
1021
1022                 mbo->complete = compl;
1023                 mbo->num_buffers_ptr = &dummy_num_buffers;
1024                 if (dir == MOST_CH_RX) {
1025                         nq_hdm_mbo(mbo);
1026                         atomic_inc(&c->mbo_nq_level);
1027                 } else {
1028                         spin_lock_irqsave(&c->fifo_lock, flags);
1029                         list_add_tail(&mbo->list, &c->fifo);
1030                         spin_unlock_irqrestore(&c->fifo_lock, flags);
1031                 }
1032         }
1033         return c->cfg.num_buffers;
1034
1035 release_mbo:
1036         kfree(mbo);
1037
1038 flush_fifos:
1039         flush_channel_fifos(c);
1040         return 0;
1041 }
1042
1043 /**
1044  * most_submit_mbo - submits an MBO to fifo
1045  * @mbo: most buffer
1046  */
1047 void most_submit_mbo(struct mbo *mbo)
1048 {
1049         if (WARN_ONCE(!mbo || !mbo->context,
1050                       "bad mbo or missing channel reference\n"))
1051                 return;
1052
1053         nq_hdm_mbo(mbo);
1054 }
1055 EXPORT_SYMBOL_GPL(most_submit_mbo);
1056
1057 /**
1058  * most_write_completion - write completion handler
1059  * @mbo: most buffer
1060  *
1061  * This recycles the MBO for further usage. In case the channel has been
1062  * poisoned, the MBO is scheduled to be trashed.
1063  */
1064 static void most_write_completion(struct mbo *mbo)
1065 {
1066         struct most_channel *c;
1067
1068         c = mbo->context;
1069         if (mbo->status == MBO_E_INVAL)
1070                 pr_info("WARN: Tx MBO status: invalid\n");
1071         if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
1072                 trash_mbo(mbo);
1073         else
1074                 arm_mbo(mbo);
1075 }
1076
1077 int channel_has_mbo(struct most_interface *iface, int id,
1078                     struct core_component *comp)
1079 {
1080         struct most_channel *c = iface->p->channel[id];
1081         unsigned long flags;
1082         int empty;
1083
1084         if (unlikely(!c))
1085                 return -EINVAL;
1086
1087         if (c->pipe0.refs && c->pipe1.refs &&
1088             ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1089              (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1090                 return 0;
1091
1092         spin_lock_irqsave(&c->fifo_lock, flags);
1093         empty = list_empty(&c->fifo);
1094         spin_unlock_irqrestore(&c->fifo_lock, flags);
1095         return !empty;
1096 }
1097 EXPORT_SYMBOL_GPL(channel_has_mbo);
1098
1099 /**
1100  * most_get_mbo - get pointer to an MBO of pool
1101  * @iface: pointer to interface instance
1102  * @id: channel ID
1103  * @comp: driver component
1104  *
1105  * This attempts to get a free buffer out of the channel fifo.
1106  * Returns a pointer to MBO on success or NULL otherwise.
1107  */
1108 struct mbo *most_get_mbo(struct most_interface *iface, int id,
1109                          struct core_component *comp)
1110 {
1111         struct mbo *mbo;
1112         struct most_channel *c;
1113         unsigned long flags;
1114         int *num_buffers_ptr;
1115
1116         c = iface->p->channel[id];
1117         if (unlikely(!c))
1118                 return NULL;
1119
1120         if (c->pipe0.refs && c->pipe1.refs &&
1121             ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
1122              (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
1123                 return NULL;
1124
1125         if (comp == c->pipe0.comp)
1126                 num_buffers_ptr = &c->pipe0.num_buffers;
1127         else if (comp == c->pipe1.comp)
1128                 num_buffers_ptr = &c->pipe1.num_buffers;
1129         else
1130                 num_buffers_ptr = &dummy_num_buffers;
1131
1132         spin_lock_irqsave(&c->fifo_lock, flags);
1133         if (list_empty(&c->fifo)) {
1134                 spin_unlock_irqrestore(&c->fifo_lock, flags);
1135                 return NULL;
1136         }
1137         mbo = list_pop_mbo(&c->fifo);
1138         --*num_buffers_ptr;
1139         spin_unlock_irqrestore(&c->fifo_lock, flags);
1140
1141         mbo->num_buffers_ptr = num_buffers_ptr;
1142         mbo->buffer_length = c->cfg.buffer_size;
1143         return mbo;
1144 }
1145 EXPORT_SYMBOL_GPL(most_get_mbo);
1146
1147 /**
1148  * most_put_mbo - return buffer to pool
1149  * @mbo: most buffer
1150  */
1151 void most_put_mbo(struct mbo *mbo)
1152 {
1153         struct most_channel *c = mbo->context;
1154
1155         if (c->cfg.direction == MOST_CH_TX) {
1156                 arm_mbo(mbo);
1157                 return;
1158         }
1159         nq_hdm_mbo(mbo);
1160         atomic_inc(&c->mbo_nq_level);
1161 }
1162 EXPORT_SYMBOL_GPL(most_put_mbo);
1163
1164 /**
1165  * most_read_completion - read completion handler
1166  * @mbo: most buffer
1167  *
1168  * This function is called by the HDM when data has been received from the
1169  * hardware and copied to the buffer of the MBO.
1170  *
1171  * In case the channel has been poisoned it puts the buffer in the trash queue.
1172  * Otherwise, it passes the buffer to an component for further processing.
1173  */
1174 static void most_read_completion(struct mbo *mbo)
1175 {
1176         struct most_channel *c = mbo->context;
1177
1178         if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1179                 trash_mbo(mbo);
1180                 return;
1181         }
1182
1183         if (mbo->status == MBO_E_INVAL) {
1184                 nq_hdm_mbo(mbo);
1185                 atomic_inc(&c->mbo_nq_level);
1186                 return;
1187         }
1188
1189         if (atomic_sub_and_test(1, &c->mbo_nq_level))
1190                 c->is_starving = 1;
1191
1192         if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
1193             c->pipe0.comp->rx_completion(mbo) == 0)
1194                 return;
1195
1196         if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
1197             c->pipe1.comp->rx_completion(mbo) == 0)
1198                 return;
1199
1200         most_put_mbo(mbo);
1201 }
1202
1203 /**
1204  * most_start_channel - prepares a channel for communication
1205  * @iface: pointer to interface instance
1206  * @id: channel ID
1207  * @comp: driver component
1208  *
1209  * This prepares the channel for usage. Cross-checks whether the
1210  * channel's been properly configured.
1211  *
1212  * Returns 0 on success or error code otherwise.
1213  */
1214 int most_start_channel(struct most_interface *iface, int id,
1215                        struct core_component *comp)
1216 {
1217         int num_buffer;
1218         int ret;
1219         struct most_channel *c = iface->p->channel[id];
1220
1221         if (unlikely(!c))
1222                 return -EINVAL;
1223
1224         mutex_lock(&c->start_mutex);
1225         if (c->pipe0.refs + c->pipe1.refs > 0)
1226                 goto out; /* already started by another component */
1227
1228         if (!try_module_get(iface->mod)) {
1229                 pr_info("failed to acquire HDM lock\n");
1230                 mutex_unlock(&c->start_mutex);
1231                 return -ENOLCK;
1232         }
1233
1234         c->cfg.extra_len = 0;
1235         if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1236                 pr_info("channel configuration failed. Go check settings...\n");
1237                 ret = -EINVAL;
1238                 goto err_put_module;
1239         }
1240
1241         init_waitqueue_head(&c->hdm_fifo_wq);
1242
1243         if (c->cfg.direction == MOST_CH_RX)
1244                 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1245                                            most_read_completion);
1246         else
1247                 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1248                                            most_write_completion);
1249         if (unlikely(!num_buffer)) {
1250                 ret = -ENOMEM;
1251                 goto err_put_module;
1252         }
1253
1254         ret = run_enqueue_thread(c, id);
1255         if (ret)
1256                 goto err_put_module;
1257
1258         c->is_starving = 0;
1259         c->pipe0.num_buffers = c->cfg.num_buffers / 2;
1260         c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
1261         atomic_set(&c->mbo_ref, num_buffer);
1262
1263 out:
1264         if (comp == c->pipe0.comp)
1265                 c->pipe0.refs++;
1266         if (comp == c->pipe1.comp)
1267                 c->pipe1.refs++;
1268         mutex_unlock(&c->start_mutex);
1269         return 0;
1270
1271 err_put_module:
1272         module_put(iface->mod);
1273         mutex_unlock(&c->start_mutex);
1274         return ret;
1275 }
1276 EXPORT_SYMBOL_GPL(most_start_channel);
1277
1278 /**
1279  * most_stop_channel - stops a running channel
1280  * @iface: pointer to interface instance
1281  * @id: channel ID
1282  * @comp: driver component
1283  */
1284 int most_stop_channel(struct most_interface *iface, int id,
1285                       struct core_component *comp)
1286 {
1287         struct most_channel *c;
1288
1289         if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1290                 pr_err("Bad interface or index out of range\n");
1291                 return -EINVAL;
1292         }
1293         c = iface->p->channel[id];
1294         if (unlikely(!c))
1295                 return -EINVAL;
1296
1297         mutex_lock(&c->start_mutex);
1298         if (c->pipe0.refs + c->pipe1.refs >= 2)
1299                 goto out;
1300
1301         if (c->hdm_enqueue_task)
1302                 kthread_stop(c->hdm_enqueue_task);
1303         c->hdm_enqueue_task = NULL;
1304
1305         if (iface->mod)
1306                 module_put(iface->mod);
1307
1308         c->is_poisoned = true;
1309         if (c->iface->poison_channel(c->iface, c->channel_id)) {
1310                 pr_err("Cannot stop channel %d of mdev %s\n", c->channel_id,
1311                        c->iface->description);
1312                 mutex_unlock(&c->start_mutex);
1313                 return -EAGAIN;
1314         }
1315         flush_trash_fifo(c);
1316         flush_channel_fifos(c);
1317
1318 #ifdef CMPL_INTERRUPTIBLE
1319         if (wait_for_completion_interruptible(&c->cleanup)) {
1320                 pr_info("Interrupted while clean up ch %d\n", c->channel_id);
1321                 mutex_unlock(&c->start_mutex);
1322                 return -EINTR;
1323         }
1324 #else
1325         wait_for_completion(&c->cleanup);
1326 #endif
1327         c->is_poisoned = false;
1328
1329 out:
1330         if (comp == c->pipe0.comp)
1331                 c->pipe0.refs--;
1332         if (comp == c->pipe1.comp)
1333                 c->pipe1.refs--;
1334         mutex_unlock(&c->start_mutex);
1335         return 0;
1336 }
1337 EXPORT_SYMBOL_GPL(most_stop_channel);
1338
1339 /**
1340  * most_register_component - registers a driver component with the core
1341  * @comp: driver component
1342  */
1343 int most_register_component(struct core_component *comp)
1344 {
1345         if (!comp) {
1346                 pr_err("Bad component\n");
1347                 return -EINVAL;
1348         }
1349         list_add_tail(&comp->list, &mc.comp_list);
1350         pr_info("registered new core component %s\n", comp->name);
1351         return 0;
1352 }
1353 EXPORT_SYMBOL_GPL(most_register_component);
1354
1355 static int disconnect_channels(struct device *dev, void *data)
1356 {
1357         struct most_interface *iface;
1358         struct most_channel *c, *tmp;
1359         struct core_component *comp = data;
1360
1361         iface = to_most_interface(dev);
1362         list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
1363                 if (c->pipe0.comp == comp || c->pipe1.comp == comp)
1364                         comp->disconnect_channel(c->iface, c->channel_id);
1365                 if (c->pipe0.comp == comp)
1366                         c->pipe0.comp = NULL;
1367                 if (c->pipe1.comp == comp)
1368                         c->pipe1.comp = NULL;
1369         }
1370         return 0;
1371 }
1372
1373 /**
1374  * most_deregister_component - deregisters a driver component with the core
1375  * @comp: driver component
1376  */
1377 int most_deregister_component(struct core_component *comp)
1378 {
1379         if (!comp) {
1380                 pr_err("Bad component\n");
1381                 return -EINVAL;
1382         }
1383
1384         bus_for_each_dev(&mc.bus, NULL, comp, disconnect_channels);
1385         list_del(&comp->list);
1386         pr_info("deregistering component %s\n", comp->name);
1387         return 0;
1388 }
1389 EXPORT_SYMBOL_GPL(most_deregister_component);
1390
1391 static void release_interface(struct device *dev)
1392 {
1393         pr_info("releasing interface dev %s...\n", dev_name(dev));
1394 }
1395
1396 static void release_channel(struct device *dev)
1397 {
1398         pr_info("releasing channel dev %s...\n", dev_name(dev));
1399 }
1400
1401 /**
1402  * most_register_interface - registers an interface with core
1403  * @iface: device interface
1404  *
1405  * Allocates and initializes a new interface instance and all of its channels.
1406  * Returns a pointer to kobject or an error pointer.
1407  */
1408 int most_register_interface(struct most_interface *iface)
1409 {
1410         unsigned int i;
1411         int id;
1412         struct most_channel *c;
1413
1414         if (!iface || !iface->enqueue || !iface->configure ||
1415             !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1416                 pr_err("Bad interface or channel overflow\n");
1417                 return -EINVAL;
1418         }
1419
1420         id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1421         if (id < 0) {
1422                 pr_info("Failed to alloc mdev ID\n");
1423                 return id;
1424         }
1425
1426         iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
1427         if (!iface->p) {
1428                 ida_simple_remove(&mdev_id, id);
1429                 return -ENOMEM;
1430         }
1431
1432         INIT_LIST_HEAD(&iface->p->channel_list);
1433         iface->p->dev_id = id;
1434         snprintf(iface->p->name, STRING_SIZE, "mdev%d", id);
1435         iface->dev.init_name = iface->p->name;
1436         iface->dev.bus = &mc.bus;
1437         iface->dev.parent = &mc.dev;
1438         iface->dev.groups = interface_attr_groups;
1439         iface->dev.release = release_interface;
1440         if (device_register(&iface->dev)) {
1441                 pr_err("registering iface->dev failed\n");
1442                 kfree(iface->p);
1443                 ida_simple_remove(&mdev_id, id);
1444                 return -ENOMEM;
1445         }
1446
1447         for (i = 0; i < iface->num_channels; i++) {
1448                 const char *name_suffix = iface->channel_vector[i].name_suffix;
1449
1450                 c = kzalloc(sizeof(*c), GFP_KERNEL);
1451                 if (!c)
1452                         goto err_free_resources;
1453                 if (!name_suffix)
1454                         snprintf(c->name, STRING_SIZE, "ch%d", i);
1455                 else
1456                         snprintf(c->name, STRING_SIZE, "%s", name_suffix);
1457                 c->dev.init_name = c->name;
1458                 c->dev.parent = &iface->dev;
1459                 c->dev.groups = channel_attr_groups;
1460                 c->dev.release = release_channel;
1461                 iface->p->channel[i] = c;
1462                 c->is_starving = 0;
1463                 c->iface = iface;
1464                 c->channel_id = i;
1465                 c->keep_mbo = false;
1466                 c->enqueue_halt = false;
1467                 c->is_poisoned = false;
1468                 c->cfg.direction = 0;
1469                 c->cfg.data_type = 0;
1470                 c->cfg.num_buffers = 0;
1471                 c->cfg.buffer_size = 0;
1472                 c->cfg.subbuffer_size = 0;
1473                 c->cfg.packets_per_xact = 0;
1474                 spin_lock_init(&c->fifo_lock);
1475                 INIT_LIST_HEAD(&c->fifo);
1476                 INIT_LIST_HEAD(&c->trash_fifo);
1477                 INIT_LIST_HEAD(&c->halt_fifo);
1478                 init_completion(&c->cleanup);
1479                 atomic_set(&c->mbo_ref, 0);
1480                 mutex_init(&c->start_mutex);
1481                 mutex_init(&c->nq_mutex);
1482                 list_add_tail(&c->list, &iface->p->channel_list);
1483                 if (device_register(&c->dev)) {
1484                         pr_err("registering c->dev failed\n");
1485                         goto err_free_most_channel;
1486                 }
1487         }
1488         pr_info("registered new device mdev%d (%s)\n",
1489                 id, iface->description);
1490         return 0;
1491
1492 err_free_most_channel:
1493         kfree(c);
1494
1495 err_free_resources:
1496         while (i > 0) {
1497                 c = iface->p->channel[--i];
1498                 device_unregister(&c->dev);
1499                 kfree(c);
1500         }
1501         kfree(iface->p);
1502         device_unregister(&iface->dev);
1503         ida_simple_remove(&mdev_id, id);
1504         return -ENOMEM;
1505 }
1506 EXPORT_SYMBOL_GPL(most_register_interface);
1507
1508 /**
1509  * most_deregister_interface - deregisters an interface with core
1510  * @iface: device interface
1511  *
1512  * Before removing an interface instance from the list, all running
1513  * channels are stopped and poisoned.
1514  */
1515 void most_deregister_interface(struct most_interface *iface)
1516 {
1517         int i;
1518         struct most_channel *c;
1519
1520         pr_info("deregistering device %s (%s)\n", dev_name(&iface->dev),
1521                 iface->description);
1522         for (i = 0; i < iface->num_channels; i++) {
1523                 c = iface->p->channel[i];
1524                 if (c->pipe0.comp)
1525                         c->pipe0.comp->disconnect_channel(c->iface,
1526                                                         c->channel_id);
1527                 if (c->pipe1.comp)
1528                         c->pipe1.comp->disconnect_channel(c->iface,
1529                                                         c->channel_id);
1530                 c->pipe0.comp = NULL;
1531                 c->pipe1.comp = NULL;
1532                 list_del(&c->list);
1533                 device_unregister(&c->dev);
1534                 kfree(c);
1535         }
1536
1537         ida_simple_remove(&mdev_id, iface->p->dev_id);
1538         kfree(iface->p);
1539         device_unregister(&iface->dev);
1540 }
1541 EXPORT_SYMBOL_GPL(most_deregister_interface);
1542
1543 /**
1544  * most_stop_enqueue - prevents core from enqueueing MBOs
1545  * @iface: pointer to interface
1546  * @id: channel id
1547  *
1548  * This is called by an HDM that _cannot_ attend to its duties and
1549  * is imminent to get run over by the core. The core is not going to
1550  * enqueue any further packets unless the flagging HDM calls
1551  * most_resume enqueue().
1552  */
1553 void most_stop_enqueue(struct most_interface *iface, int id)
1554 {
1555         struct most_channel *c = iface->p->channel[id];
1556
1557         if (!c)
1558                 return;
1559
1560         mutex_lock(&c->nq_mutex);
1561         c->enqueue_halt = true;
1562         mutex_unlock(&c->nq_mutex);
1563 }
1564 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1565
1566 /**
1567  * most_resume_enqueue - allow core to enqueue MBOs again
1568  * @iface: pointer to interface
1569  * @id: channel id
1570  *
1571  * This clears the enqueue halt flag and enqueues all MBOs currently
1572  * sitting in the wait fifo.
1573  */
1574 void most_resume_enqueue(struct most_interface *iface, int id)
1575 {
1576         struct most_channel *c = iface->p->channel[id];
1577
1578         if (!c)
1579                 return;
1580
1581         mutex_lock(&c->nq_mutex);
1582         c->enqueue_halt = false;
1583         mutex_unlock(&c->nq_mutex);
1584
1585         wake_up_interruptible(&c->hdm_fifo_wq);
1586 }
1587 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1588
1589 static void release_most_sub(struct device *dev)
1590 {
1591         pr_info("releasing most_subsystem\n");
1592 }
1593
1594 static int __init most_init(void)
1595 {
1596         int err;
1597
1598         pr_info("init()\n");
1599         INIT_LIST_HEAD(&mc.comp_list);
1600         ida_init(&mdev_id);
1601
1602         mc.bus.name = "most",
1603         mc.bus.match = most_match,
1604         mc.drv.name = "most_core",
1605         mc.drv.bus = &mc.bus,
1606         mc.drv.groups = mc_attr_groups;
1607
1608         err = bus_register(&mc.bus);
1609         if (err) {
1610                 pr_info("Cannot register most bus\n");
1611                 return err;
1612         }
1613         err = driver_register(&mc.drv);
1614         if (err) {
1615                 pr_info("Cannot register core driver\n");
1616                 goto err_unregister_bus;
1617         }
1618         mc.dev.init_name = "most_bus";
1619         mc.dev.release = release_most_sub;
1620         if (device_register(&mc.dev)) {
1621                 err = -ENOMEM;
1622                 goto err_unregister_driver;
1623         }
1624
1625         return 0;
1626
1627 err_unregister_driver:
1628         driver_unregister(&mc.drv);
1629 err_unregister_bus:
1630         bus_unregister(&mc.bus);
1631         return err;
1632 }
1633
1634 static void __exit most_exit(void)
1635 {
1636         pr_info("exit core module\n");
1637         device_unregister(&mc.dev);
1638         driver_unregister(&mc.drv);
1639         bus_unregister(&mc.bus);
1640         ida_destroy(&mdev_id);
1641 }
1642
1643 module_init(most_init);
1644 module_exit(most_exit);
1645 MODULE_LICENSE("GPL");
1646 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1647 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");