2 * core.c - Implementation of core module of MOST Linux driver stack
4 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * This file is licensed under GPLv2.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/device.h>
20 #include <linux/list.h>
21 #include <linux/poll.h>
22 #include <linux/wait.h>
23 #include <linux/kobject.h>
24 #include <linux/mutex.h>
25 #include <linux/completion.h>
26 #include <linux/sysfs.h>
27 #include <linux/kthread.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/idr.h>
32 #define MAX_CHANNELS 64
33 #define STRING_SIZE 80
35 static struct class *most_class;
36 static struct device *core_dev;
37 static struct ida mdev_id;
38 static int dummy_num_buffers;
40 struct most_c_aim_obj {
48 struct completion cleanup;
50 atomic_t mbo_nq_level;
53 struct mutex start_mutex;
54 struct mutex nq_mutex; /* nq thread synchronization */
56 struct most_interface *iface;
57 struct most_inst_obj *inst;
58 struct most_channel_config cfg;
61 struct list_head fifo;
63 struct list_head halt_fifo;
64 struct list_head list;
65 struct most_c_aim_obj aim0;
66 struct most_c_aim_obj aim1;
67 struct list_head trash_fifo;
68 struct task_struct *hdm_enqueue_task;
69 wait_queue_head_t hdm_fifo_wq;
72 #define to_c_obj(d) container_of(d, struct most_c_obj, kobj)
74 struct most_inst_obj {
76 struct most_interface *iface;
77 struct list_head channel_list;
78 struct most_c_obj *channel[MAX_CHANNELS];
80 struct list_head list;
84 int most_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"},
94 #define to_inst_obj(d) container_of(d, struct most_inst_obj, kobj)
97 * list_pop_mbo - retrieves the first MBO of the list and removes it
98 * @ptr: the list head to grab the MBO from.
100 #define list_pop_mbo(ptr) \
102 struct mbo *_mbo = list_first_entry(ptr, struct mbo, list); \
103 list_del(&_mbo->list); \
108 * ___C H A N N E L___
112 * struct most_c_attr - to access the attributes of a channel object
113 * @attr: attributes of a channel
114 * @show: pointer to the show function
115 * @store: pointer to the store function
118 struct attribute attr;
119 ssize_t (*show)(struct most_c_obj *d,
120 struct most_c_attr *attr,
122 ssize_t (*store)(struct most_c_obj *d,
123 struct most_c_attr *attr,
128 #define to_channel_attr(a) container_of(a, struct most_c_attr, attr)
130 #define MOST_CHNL_ATTR(_name, _mode, _show, _store) \
131 struct most_c_attr most_chnl_attr_##_name = \
132 __ATTR(_name, _mode, _show, _store)
135 * channel_attr_show - show function of channel object
136 * @kobj: pointer to its kobject
137 * @attr: pointer to its attributes
140 static ssize_t channel_attr_show(struct kobject *kobj, struct attribute *attr,
143 struct most_c_attr *channel_attr = to_channel_attr(attr);
144 struct most_c_obj *c_obj = to_c_obj(kobj);
146 if (!channel_attr->show)
149 return channel_attr->show(c_obj, channel_attr, buf);
153 * channel_attr_store - store function of channel object
154 * @kobj: pointer to its kobject
155 * @attr: pointer to its attributes
157 * @len: length of buffer
159 static ssize_t channel_attr_store(struct kobject *kobj,
160 struct attribute *attr,
164 struct most_c_attr *channel_attr = to_channel_attr(attr);
165 struct most_c_obj *c_obj = to_c_obj(kobj);
167 if (!channel_attr->store)
169 return channel_attr->store(c_obj, channel_attr, buf, len);
172 static const struct sysfs_ops most_channel_sysfs_ops = {
173 .show = channel_attr_show,
174 .store = channel_attr_store,
178 * most_free_mbo_coherent - free an MBO and its coherent buffer
179 * @mbo: buffer to be released
182 static void most_free_mbo_coherent(struct mbo *mbo)
184 struct most_c_obj *c = mbo->context;
185 u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
187 dma_free_coherent(NULL, coherent_buf_size, mbo->virt_address,
190 if (atomic_sub_and_test(1, &c->mbo_ref))
191 complete(&c->cleanup);
195 * flush_channel_fifos - clear the channel fifos
196 * @c: pointer to channel object
198 static void flush_channel_fifos(struct most_c_obj *c)
200 unsigned long flags, hf_flags;
201 struct mbo *mbo, *tmp;
203 if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
206 spin_lock_irqsave(&c->fifo_lock, flags);
207 list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
208 list_del(&mbo->list);
209 spin_unlock_irqrestore(&c->fifo_lock, flags);
210 most_free_mbo_coherent(mbo);
211 spin_lock_irqsave(&c->fifo_lock, flags);
213 spin_unlock_irqrestore(&c->fifo_lock, flags);
215 spin_lock_irqsave(&c->fifo_lock, hf_flags);
216 list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
217 list_del(&mbo->list);
218 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
219 most_free_mbo_coherent(mbo);
220 spin_lock_irqsave(&c->fifo_lock, hf_flags);
222 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
224 if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
225 pr_info("WARN: fifo | trash fifo not empty\n");
229 * flush_trash_fifo - clear the trash fifo
230 * @c: pointer to channel object
232 static int flush_trash_fifo(struct most_c_obj *c)
234 struct mbo *mbo, *tmp;
237 spin_lock_irqsave(&c->fifo_lock, flags);
238 list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
239 list_del(&mbo->list);
240 spin_unlock_irqrestore(&c->fifo_lock, flags);
241 most_free_mbo_coherent(mbo);
242 spin_lock_irqsave(&c->fifo_lock, flags);
244 spin_unlock_irqrestore(&c->fifo_lock, flags);
249 * most_channel_release - release function of channel object
250 * @kobj: pointer to channel's kobject
252 static void most_channel_release(struct kobject *kobj)
254 struct most_c_obj *c = to_c_obj(kobj);
259 static ssize_t show_available_directions(struct most_c_obj *c,
260 struct most_c_attr *attr,
263 unsigned int i = c->channel_id;
266 if (c->iface->channel_vector[i].direction & MOST_CH_RX)
268 if (c->iface->channel_vector[i].direction & MOST_CH_TX)
274 static ssize_t show_available_datatypes(struct most_c_obj *c,
275 struct most_c_attr *attr,
278 unsigned int i = c->channel_id;
281 if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
282 strcat(buf, "control ");
283 if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
284 strcat(buf, "async ");
285 if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
286 strcat(buf, "sync ");
287 if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
288 strcat(buf, "isoc ");
294 ssize_t show_number_of_packet_buffers(struct most_c_obj *c,
295 struct most_c_attr *attr,
298 unsigned int i = c->channel_id;
300 return snprintf(buf, PAGE_SIZE, "%d\n",
301 c->iface->channel_vector[i].num_buffers_packet);
305 ssize_t show_number_of_stream_buffers(struct most_c_obj *c,
306 struct most_c_attr *attr,
309 unsigned int i = c->channel_id;
311 return snprintf(buf, PAGE_SIZE, "%d\n",
312 c->iface->channel_vector[i].num_buffers_streaming);
316 ssize_t show_size_of_packet_buffer(struct most_c_obj *c,
317 struct most_c_attr *attr,
320 unsigned int i = c->channel_id;
322 return snprintf(buf, PAGE_SIZE, "%d\n",
323 c->iface->channel_vector[i].buffer_size_packet);
327 ssize_t show_size_of_stream_buffer(struct most_c_obj *c,
328 struct most_c_attr *attr,
331 unsigned int i = c->channel_id;
333 return snprintf(buf, PAGE_SIZE, "%d\n",
334 c->iface->channel_vector[i].buffer_size_streaming);
337 static ssize_t show_channel_starving(struct most_c_obj *c,
338 struct most_c_attr *attr,
341 return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
344 #define create_show_channel_attribute(val) \
345 static MOST_CHNL_ATTR(val, S_IRUGO, show_##val, NULL)
347 create_show_channel_attribute(available_directions);
348 create_show_channel_attribute(available_datatypes);
349 create_show_channel_attribute(number_of_packet_buffers);
350 create_show_channel_attribute(number_of_stream_buffers);
351 create_show_channel_attribute(size_of_stream_buffer);
352 create_show_channel_attribute(size_of_packet_buffer);
353 create_show_channel_attribute(channel_starving);
355 static ssize_t show_set_number_of_buffers(struct most_c_obj *c,
356 struct most_c_attr *attr,
359 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
362 static ssize_t store_set_number_of_buffers(struct most_c_obj *c,
363 struct most_c_attr *attr,
367 int ret = kstrtou16(buf, 0, &c->cfg.num_buffers);
374 static ssize_t show_set_buffer_size(struct most_c_obj *c,
375 struct most_c_attr *attr,
378 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
381 static ssize_t store_set_buffer_size(struct most_c_obj *c,
382 struct most_c_attr *attr,
386 int ret = kstrtou16(buf, 0, &c->cfg.buffer_size);
393 static ssize_t show_set_direction(struct most_c_obj *c,
394 struct most_c_attr *attr,
397 if (c->cfg.direction & MOST_CH_TX)
398 return snprintf(buf, PAGE_SIZE, "tx\n");
399 else if (c->cfg.direction & MOST_CH_RX)
400 return snprintf(buf, PAGE_SIZE, "rx\n");
401 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
404 static ssize_t store_set_direction(struct most_c_obj *c,
405 struct most_c_attr *attr,
409 if (!strcmp(buf, "dir_rx\n")) {
410 c->cfg.direction = MOST_CH_RX;
411 } else if (!strcmp(buf, "rx\n")) {
412 c->cfg.direction = MOST_CH_RX;
413 } else if (!strcmp(buf, "dir_tx\n")) {
414 c->cfg.direction = MOST_CH_TX;
415 } else if (!strcmp(buf, "tx\n")) {
416 c->cfg.direction = MOST_CH_TX;
418 pr_info("WARN: invalid attribute settings\n");
424 static ssize_t show_set_datatype(struct most_c_obj *c,
425 struct most_c_attr *attr,
430 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
431 if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
432 return snprintf(buf, PAGE_SIZE, ch_data_type[i].name);
434 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
437 static ssize_t store_set_datatype(struct most_c_obj *c,
438 struct most_c_attr *attr,
444 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
445 if (!strcmp(buf, ch_data_type[i].name)) {
446 c->cfg.data_type = ch_data_type[i].most_ch_data_type;
451 if (i == ARRAY_SIZE(ch_data_type)) {
452 pr_info("WARN: invalid attribute settings\n");
458 static ssize_t show_set_subbuffer_size(struct most_c_obj *c,
459 struct most_c_attr *attr,
462 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
465 static ssize_t store_set_subbuffer_size(struct most_c_obj *c,
466 struct most_c_attr *attr,
470 int ret = kstrtou16(buf, 0, &c->cfg.subbuffer_size);
477 static ssize_t show_set_packets_per_xact(struct most_c_obj *c,
478 struct most_c_attr *attr,
481 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
484 static ssize_t store_set_packets_per_xact(struct most_c_obj *c,
485 struct most_c_attr *attr,
489 int ret = kstrtou16(buf, 0, &c->cfg.packets_per_xact);
496 #define create_channel_attribute(value) \
497 static MOST_CHNL_ATTR(value, S_IRUGO | S_IWUSR, \
501 create_channel_attribute(set_buffer_size);
502 create_channel_attribute(set_number_of_buffers);
503 create_channel_attribute(set_direction);
504 create_channel_attribute(set_datatype);
505 create_channel_attribute(set_subbuffer_size);
506 create_channel_attribute(set_packets_per_xact);
509 * most_channel_def_attrs - array of default attributes of channel object
511 static struct attribute *most_channel_def_attrs[] = {
512 &most_chnl_attr_available_directions.attr,
513 &most_chnl_attr_available_datatypes.attr,
514 &most_chnl_attr_number_of_packet_buffers.attr,
515 &most_chnl_attr_number_of_stream_buffers.attr,
516 &most_chnl_attr_size_of_packet_buffer.attr,
517 &most_chnl_attr_size_of_stream_buffer.attr,
518 &most_chnl_attr_set_number_of_buffers.attr,
519 &most_chnl_attr_set_buffer_size.attr,
520 &most_chnl_attr_set_direction.attr,
521 &most_chnl_attr_set_datatype.attr,
522 &most_chnl_attr_set_subbuffer_size.attr,
523 &most_chnl_attr_set_packets_per_xact.attr,
524 &most_chnl_attr_channel_starving.attr,
528 static struct kobj_type most_channel_ktype = {
529 .sysfs_ops = &most_channel_sysfs_ops,
530 .release = most_channel_release,
531 .default_attrs = most_channel_def_attrs,
534 static struct kset *most_channel_kset;
537 * create_most_c_obj - allocates a channel object
538 * @name: name of the channel object
539 * @parent: parent kobject
541 * This create a channel object and registers it with sysfs.
542 * Returns a pointer to the object or NULL when something went wrong.
544 static struct most_c_obj *
545 create_most_c_obj(const char *name, struct kobject *parent)
547 struct most_c_obj *c;
550 c = kzalloc(sizeof(*c), GFP_KERNEL);
553 c->kobj.kset = most_channel_kset;
554 retval = kobject_init_and_add(&c->kobj, &most_channel_ktype, parent,
557 kobject_put(&c->kobj);
560 kobject_uevent(&c->kobj, KOBJ_ADD);
565 * ___I N S T A N C E___
567 #define MOST_INST_ATTR(_name, _mode, _show, _store) \
568 struct most_inst_attribute most_inst_attr_##_name = \
569 __ATTR(_name, _mode, _show, _store)
571 static struct list_head instance_list;
574 * struct most_inst_attribute - to access the attributes of instance object
575 * @attr: attributes of an instance
576 * @show: pointer to the show function
577 * @store: pointer to the store function
579 struct most_inst_attribute {
580 struct attribute attr;
581 ssize_t (*show)(struct most_inst_obj *d,
582 struct most_inst_attribute *attr,
584 ssize_t (*store)(struct most_inst_obj *d,
585 struct most_inst_attribute *attr,
590 #define to_instance_attr(a) \
591 container_of(a, struct most_inst_attribute, attr)
594 * instance_attr_show - show function for an instance object
595 * @kobj: pointer to kobject
596 * @attr: pointer to attribute struct
599 static ssize_t instance_attr_show(struct kobject *kobj,
600 struct attribute *attr,
603 struct most_inst_attribute *instance_attr;
604 struct most_inst_obj *instance_obj;
606 instance_attr = to_instance_attr(attr);
607 instance_obj = to_inst_obj(kobj);
609 if (!instance_attr->show)
612 return instance_attr->show(instance_obj, instance_attr, buf);
616 * instance_attr_store - store function for an instance object
617 * @kobj: pointer to kobject
618 * @attr: pointer to attribute struct
620 * @len: length of buffer
622 static ssize_t instance_attr_store(struct kobject *kobj,
623 struct attribute *attr,
627 struct most_inst_attribute *instance_attr;
628 struct most_inst_obj *instance_obj;
630 instance_attr = to_instance_attr(attr);
631 instance_obj = to_inst_obj(kobj);
633 if (!instance_attr->store)
636 return instance_attr->store(instance_obj, instance_attr, buf, len);
639 static const struct sysfs_ops most_inst_sysfs_ops = {
640 .show = instance_attr_show,
641 .store = instance_attr_store,
645 * most_inst_release - release function for instance object
646 * @kobj: pointer to instance's kobject
648 * This frees the allocated memory for the instance object
650 static void most_inst_release(struct kobject *kobj)
652 struct most_inst_obj *inst = to_inst_obj(kobj);
657 static ssize_t show_description(struct most_inst_obj *instance_obj,
658 struct most_inst_attribute *attr,
661 return snprintf(buf, PAGE_SIZE, "%s\n",
662 instance_obj->iface->description);
665 static ssize_t show_interface(struct most_inst_obj *instance_obj,
666 struct most_inst_attribute *attr,
669 switch (instance_obj->iface->interface) {
671 return snprintf(buf, PAGE_SIZE, "loopback\n");
673 return snprintf(buf, PAGE_SIZE, "i2c\n");
675 return snprintf(buf, PAGE_SIZE, "i2s\n");
677 return snprintf(buf, PAGE_SIZE, "tsi\n");
679 return snprintf(buf, PAGE_SIZE, "hbi\n");
680 case ITYPE_MEDIALB_DIM:
681 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
682 case ITYPE_MEDIALB_DIM2:
683 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
685 return snprintf(buf, PAGE_SIZE, "usb\n");
687 return snprintf(buf, PAGE_SIZE, "pcie\n");
689 return snprintf(buf, PAGE_SIZE, "unknown\n");
692 #define create_inst_attribute(value) \
693 static MOST_INST_ATTR(value, S_IRUGO, show_##value, NULL)
695 create_inst_attribute(description);
696 create_inst_attribute(interface);
698 static struct attribute *most_inst_def_attrs[] = {
699 &most_inst_attr_description.attr,
700 &most_inst_attr_interface.attr,
704 static struct kobj_type most_inst_ktype = {
705 .sysfs_ops = &most_inst_sysfs_ops,
706 .release = most_inst_release,
707 .default_attrs = most_inst_def_attrs,
710 static struct kset *most_inst_kset;
713 * create_most_inst_obj - creates an instance object
714 * @name: name of the object to be created
716 * This allocates memory for an instance structure, assigns the proper kset
717 * and registers it with sysfs.
719 * Returns a pointer to the instance object or NULL when something went wrong.
721 static struct most_inst_obj *create_most_inst_obj(const char *name)
723 struct most_inst_obj *inst;
726 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
729 inst->kobj.kset = most_inst_kset;
730 retval = kobject_init_and_add(&inst->kobj, &most_inst_ktype, NULL,
733 kobject_put(&inst->kobj);
736 kobject_uevent(&inst->kobj, KOBJ_ADD);
741 * destroy_most_inst_obj - MOST instance release function
742 * @inst: pointer to the instance object
744 * This decrements the reference counter of the instance object.
745 * If the reference count turns zero, its release function is called
747 static void destroy_most_inst_obj(struct most_inst_obj *inst)
749 struct most_c_obj *c, *tmp;
751 list_for_each_entry_safe(c, tmp, &inst->channel_list, list) {
753 flush_channel_fifos(c);
754 kobject_put(&c->kobj);
756 kobject_put(&inst->kobj);
762 struct most_aim_obj {
764 struct list_head list;
765 struct most_aim *driver;
766 char add_link[STRING_SIZE];
767 char remove_link[STRING_SIZE];
770 #define to_aim_obj(d) container_of(d, struct most_aim_obj, kobj)
772 static struct list_head aim_list;
775 * struct most_aim_attribute - to access the attributes of AIM object
776 * @attr: attributes of an AIM
777 * @show: pointer to the show function
778 * @store: pointer to the store function
780 struct most_aim_attribute {
781 struct attribute attr;
782 ssize_t (*show)(struct most_aim_obj *d,
783 struct most_aim_attribute *attr,
785 ssize_t (*store)(struct most_aim_obj *d,
786 struct most_aim_attribute *attr,
791 #define to_aim_attr(a) container_of(a, struct most_aim_attribute, attr)
794 * aim_attr_show - show function of an AIM object
795 * @kobj: pointer to kobject
796 * @attr: pointer to attribute struct
799 static ssize_t aim_attr_show(struct kobject *kobj,
800 struct attribute *attr,
803 struct most_aim_attribute *aim_attr;
804 struct most_aim_obj *aim_obj;
806 aim_attr = to_aim_attr(attr);
807 aim_obj = to_aim_obj(kobj);
812 return aim_attr->show(aim_obj, aim_attr, buf);
816 * aim_attr_store - store function of an AIM object
817 * @kobj: pointer to kobject
818 * @attr: pointer to attribute struct
820 * @len: length of buffer
822 static ssize_t aim_attr_store(struct kobject *kobj,
823 struct attribute *attr,
827 struct most_aim_attribute *aim_attr;
828 struct most_aim_obj *aim_obj;
830 aim_attr = to_aim_attr(attr);
831 aim_obj = to_aim_obj(kobj);
833 if (!aim_attr->store)
835 return aim_attr->store(aim_obj, aim_attr, buf, len);
838 static const struct sysfs_ops most_aim_sysfs_ops = {
839 .show = aim_attr_show,
840 .store = aim_attr_store,
844 * most_aim_release - AIM release function
845 * @kobj: pointer to AIM's kobject
847 static void most_aim_release(struct kobject *kobj)
849 struct most_aim_obj *aim_obj = to_aim_obj(kobj);
854 static ssize_t show_add_link(struct most_aim_obj *aim_obj,
855 struct most_aim_attribute *attr,
858 struct most_c_obj *c;
859 struct most_inst_obj *i;
862 list_for_each_entry(i, &instance_list, list) {
863 list_for_each_entry(c, &i->channel_list, list) {
864 if (c->aim0.ptr == aim_obj->driver ||
865 c->aim1.ptr == aim_obj->driver) {
866 offs += snprintf(buf + offs, PAGE_SIZE - offs,
868 kobject_name(&i->kobj),
869 kobject_name(&c->kobj));
878 * split_string - parses and changes string in the buffer buf and
879 * splits it into two mandatory and one optional substrings.
881 * @buf: complete string from attribute 'add_channel'
882 * @a: address of pointer to 1st substring (=instance name)
883 * @b: address of pointer to 2nd substring (=channel name)
884 * @c: optional address of pointer to 3rd substring (=user defined name)
888 * Input: "mdev0:ch0@ep_81:my_channel\n" or
889 * "mdev0:ch0@ep_81:my_channel"
891 * Output: *a -> "mdev0", *b -> "ch0@ep_81", *c -> "my_channel"
893 * Input: "mdev0:ch0@ep_81\n"
894 * Output: *a -> "mdev0", *b -> "ch0@ep_81", *c -> ""
896 * Input: "mdev0:ch0@ep_81"
897 * Output: *a -> "mdev0", *b -> "ch0@ep_81", *c == NULL
899 static int split_string(char *buf, char **a, char **b, char **c)
901 *a = strsep(&buf, ":");
905 *b = strsep(&buf, ":\n");
910 *c = strsep(&buf, ":\n");
916 * get_channel_by_name - get pointer to channel object
917 * @mdev: name of the device instance
918 * @mdev_ch: name of the respective channel
920 * This retrieves the pointer to a channel object.
923 most_c_obj *get_channel_by_name(char *mdev, char *mdev_ch)
925 struct most_c_obj *c, *tmp;
926 struct most_inst_obj *i, *i_tmp;
929 list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
930 if (!strcmp(kobject_name(&i->kobj), mdev)) {
935 if (unlikely(!found))
936 return ERR_PTR(-EIO);
938 list_for_each_entry_safe(c, tmp, &i->channel_list, list) {
939 if (!strcmp(kobject_name(&c->kobj), mdev_ch)) {
944 if (unlikely(found < 2))
945 return ERR_PTR(-EIO);
950 * store_add_link - store() function for add_link attribute
951 * @aim_obj: pointer to AIM object
952 * @attr: its attributes
954 * @len: buffer length
956 * This parses the string given by buf and splits it into
957 * three substrings. Note: third substring is optional. In case a cdev
958 * AIM is loaded the optional 3rd substring will make up the name of
959 * device node in the /dev directory. If omitted, the device node will
960 * inherit the channel's name within sysfs.
962 * Searches for a pair of device and channel and probes the AIM
965 * (1) echo -n -e "mdev0:ch0@ep_81:my_rxchannel\n" >add_link
966 * (2) echo -n -e "mdev0:ch0@ep_81\n" >add_link
968 * (1) would create the device node /dev/my_rxchannel
969 * (2) would create the device node /dev/mdev0-ch0@ep_81
971 static ssize_t store_add_link(struct most_aim_obj *aim_obj,
972 struct most_aim_attribute *attr,
976 struct most_c_obj *c;
977 struct most_aim **aim_ptr;
978 char buffer[STRING_SIZE];
982 char devnod_buf[STRING_SIZE];
984 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
986 strlcpy(buffer, buf, max_len);
987 strlcpy(aim_obj->add_link, buf, max_len);
989 ret = split_string(buffer, &mdev, &mdev_ch, &mdev_devnod);
993 if (!mdev_devnod || *mdev_devnod == 0) {
994 snprintf(devnod_buf, sizeof(devnod_buf), "%s-%s", mdev,
996 mdev_devnod = devnod_buf;
999 c = get_channel_by_name(mdev, mdev_ch);
1004 aim_ptr = &c->aim0.ptr;
1005 else if (!c->aim1.ptr)
1006 aim_ptr = &c->aim1.ptr;
1010 *aim_ptr = aim_obj->driver;
1011 ret = aim_obj->driver->probe_channel(c->iface, c->channel_id,
1012 &c->cfg, &c->kobj, mdev_devnod);
1021 static struct most_aim_attribute most_aim_attr_add_link =
1022 __ATTR(add_link, S_IRUGO | S_IWUSR, show_add_link, store_add_link);
1024 static ssize_t show_remove_link(struct most_aim_obj *aim_obj,
1025 struct most_aim_attribute *attr,
1028 return snprintf(buf, PAGE_SIZE, "%s\n", aim_obj->remove_link);
1032 * store_remove_link - store function for remove_link attribute
1033 * @aim_obj: pointer to AIM object
1034 * @attr: its attributes
1036 * @len: buffer length
1039 * echo -n -e "mdev0:ch0@ep_81\n" >remove_link
1041 static ssize_t store_remove_link(struct most_aim_obj *aim_obj,
1042 struct most_aim_attribute *attr,
1046 struct most_c_obj *c;
1047 char buffer[STRING_SIZE];
1051 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
1053 strlcpy(buffer, buf, max_len);
1054 strlcpy(aim_obj->remove_link, buf, max_len);
1055 ret = split_string(buffer, &mdev, &mdev_ch, NULL);
1059 c = get_channel_by_name(mdev, mdev_ch);
1063 if (aim_obj->driver->disconnect_channel(c->iface, c->channel_id))
1065 if (c->aim0.ptr == aim_obj->driver)
1067 if (c->aim1.ptr == aim_obj->driver)
1072 static struct most_aim_attribute most_aim_attr_remove_link =
1073 __ATTR(remove_link, S_IRUGO | S_IWUSR, show_remove_link,
1076 static struct attribute *most_aim_def_attrs[] = {
1077 &most_aim_attr_add_link.attr,
1078 &most_aim_attr_remove_link.attr,
1082 static struct kobj_type most_aim_ktype = {
1083 .sysfs_ops = &most_aim_sysfs_ops,
1084 .release = most_aim_release,
1085 .default_attrs = most_aim_def_attrs,
1088 static struct kset *most_aim_kset;
1091 * create_most_aim_obj - creates an AIM object
1092 * @name: name of the AIM
1094 * This creates an AIM object assigns the proper kset and registers
1096 * Returns a pointer to the object or NULL if something went wrong.
1098 static struct most_aim_obj *create_most_aim_obj(const char *name)
1100 struct most_aim_obj *most_aim;
1103 most_aim = kzalloc(sizeof(*most_aim), GFP_KERNEL);
1106 most_aim->kobj.kset = most_aim_kset;
1107 retval = kobject_init_and_add(&most_aim->kobj, &most_aim_ktype,
1110 kobject_put(&most_aim->kobj);
1113 kobject_uevent(&most_aim->kobj, KOBJ_ADD);
1118 * destroy_most_aim_obj - AIM release function
1119 * @p: pointer to AIM object
1121 * This decrements the reference counter of the AIM object. If the
1122 * reference count turns zero, its release function will be called.
1124 static void destroy_most_aim_obj(struct most_aim_obj *p)
1126 kobject_put(&p->kobj);
1134 * Instantiation of the MOST bus
1136 static struct bus_type most_bus = {
1141 * Instantiation of the core driver
1143 static struct device_driver mostcore = {
1148 static inline void trash_mbo(struct mbo *mbo)
1150 unsigned long flags;
1151 struct most_c_obj *c = mbo->context;
1153 spin_lock_irqsave(&c->fifo_lock, flags);
1154 list_add(&mbo->list, &c->trash_fifo);
1155 spin_unlock_irqrestore(&c->fifo_lock, flags);
1158 static bool hdm_mbo_ready(struct most_c_obj *c)
1162 if (c->enqueue_halt)
1165 spin_lock_irq(&c->fifo_lock);
1166 empty = list_empty(&c->halt_fifo);
1167 spin_unlock_irq(&c->fifo_lock);
1172 static void nq_hdm_mbo(struct mbo *mbo)
1174 unsigned long flags;
1175 struct most_c_obj *c = mbo->context;
1177 spin_lock_irqsave(&c->fifo_lock, flags);
1178 list_add_tail(&mbo->list, &c->halt_fifo);
1179 spin_unlock_irqrestore(&c->fifo_lock, flags);
1180 wake_up_interruptible(&c->hdm_fifo_wq);
1183 static int hdm_enqueue_thread(void *data)
1185 struct most_c_obj *c = data;
1188 typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
1190 while (likely(!kthread_should_stop())) {
1191 wait_event_interruptible(c->hdm_fifo_wq,
1193 kthread_should_stop());
1195 mutex_lock(&c->nq_mutex);
1196 spin_lock_irq(&c->fifo_lock);
1197 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
1198 spin_unlock_irq(&c->fifo_lock);
1199 mutex_unlock(&c->nq_mutex);
1203 mbo = list_pop_mbo(&c->halt_fifo);
1204 spin_unlock_irq(&c->fifo_lock);
1206 if (c->cfg.direction == MOST_CH_RX)
1207 mbo->buffer_length = c->cfg.buffer_size;
1209 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
1210 mutex_unlock(&c->nq_mutex);
1212 if (unlikely(ret)) {
1213 pr_err("hdm enqueue failed\n");
1215 c->hdm_enqueue_task = NULL;
1223 static int run_enqueue_thread(struct most_c_obj *c, int channel_id)
1225 struct task_struct *task =
1226 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
1230 return PTR_ERR(task);
1232 c->hdm_enqueue_task = task;
1237 * arm_mbo - recycle MBO for further usage
1238 * @mbo: buffer object
1240 * This puts an MBO back to the list to have it ready for up coming
1243 * In case the MBO belongs to a channel that recently has been
1244 * poisoned, the MBO is scheduled to be trashed.
1245 * Calls the completion handler of an attached AIM.
1247 static void arm_mbo(struct mbo *mbo)
1249 unsigned long flags;
1250 struct most_c_obj *c;
1252 BUG_ON((!mbo) || (!mbo->context));
1255 if (c->is_poisoned) {
1260 spin_lock_irqsave(&c->fifo_lock, flags);
1261 ++*mbo->num_buffers_ptr;
1262 list_add_tail(&mbo->list, &c->fifo);
1263 spin_unlock_irqrestore(&c->fifo_lock, flags);
1265 if (c->aim0.refs && c->aim0.ptr->tx_completion)
1266 c->aim0.ptr->tx_completion(c->iface, c->channel_id);
1268 if (c->aim1.refs && c->aim1.ptr->tx_completion)
1269 c->aim1.ptr->tx_completion(c->iface, c->channel_id);
1273 * arm_mbo_chain - helper function that arms an MBO chain for the HDM
1274 * @c: pointer to interface channel
1275 * @dir: direction of the channel
1276 * @compl: pointer to completion function
1278 * This allocates buffer objects including the containing DMA coherent
1279 * buffer and puts them in the fifo.
1280 * Buffers of Rx channels are put in the kthread fifo, hence immediately
1281 * submitted to the HDM.
1283 * Returns the number of allocated and enqueued MBOs.
1285 static int arm_mbo_chain(struct most_c_obj *c, int dir,
1286 void (*compl)(struct mbo *))
1291 u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
1293 atomic_set(&c->mbo_nq_level, 0);
1295 for (i = 0; i < c->cfg.num_buffers; i++) {
1296 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
1302 mbo->ifp = c->iface;
1303 mbo->hdm_channel_id = c->channel_id;
1304 mbo->virt_address = dma_alloc_coherent(NULL,
1308 if (!mbo->virt_address) {
1309 pr_info("WARN: No DMA coherent buffer.\n");
1313 mbo->complete = compl;
1314 mbo->num_buffers_ptr = &dummy_num_buffers;
1315 if (dir == MOST_CH_RX) {
1317 atomic_inc(&c->mbo_nq_level);
1331 * most_submit_mbo - submits an MBO to fifo
1332 * @mbo: pointer to the MBO
1334 void most_submit_mbo(struct mbo *mbo)
1336 if (WARN_ONCE(!mbo || !mbo->context,
1337 "bad mbo or missing channel reference\n"))
1342 EXPORT_SYMBOL_GPL(most_submit_mbo);
1345 * most_write_completion - write completion handler
1346 * @mbo: pointer to MBO
1348 * This recycles the MBO for further usage. In case the channel has been
1349 * poisoned, the MBO is scheduled to be trashed.
1351 static void most_write_completion(struct mbo *mbo)
1353 struct most_c_obj *c;
1355 BUG_ON((!mbo) || (!mbo->context));
1358 if (mbo->status == MBO_E_INVAL)
1359 pr_info("WARN: Tx MBO status: invalid\n");
1360 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
1367 * get_channel_by_iface - get pointer to channel object
1368 * @iface: pointer to interface instance
1371 * This retrieves a pointer to a channel of the given interface and channel ID.
1374 most_c_obj *get_channel_by_iface(struct most_interface *iface, int id)
1376 struct most_inst_obj *i;
1378 if (unlikely(!iface)) {
1379 pr_err("Bad interface\n");
1382 if (unlikely((id < 0) || (id >= iface->num_channels))) {
1383 pr_err("Channel index (%d) out of range\n", id);
1388 pr_err("interface is not registered\n");
1391 return i->channel[id];
1394 int channel_has_mbo(struct most_interface *iface, int id, struct most_aim *aim)
1396 struct most_c_obj *c = get_channel_by_iface(iface, id);
1397 unsigned long flags;
1403 if (c->aim0.refs && c->aim1.refs &&
1404 ((aim == c->aim0.ptr && c->aim0.num_buffers <= 0) ||
1405 (aim == c->aim1.ptr && c->aim1.num_buffers <= 0)))
1408 spin_lock_irqsave(&c->fifo_lock, flags);
1409 empty = list_empty(&c->fifo);
1410 spin_unlock_irqrestore(&c->fifo_lock, flags);
1413 EXPORT_SYMBOL_GPL(channel_has_mbo);
1416 * most_get_mbo - get pointer to an MBO of pool
1417 * @iface: pointer to interface instance
1420 * This attempts to get a free buffer out of the channel fifo.
1421 * Returns a pointer to MBO on success or NULL otherwise.
1423 struct mbo *most_get_mbo(struct most_interface *iface, int id,
1424 struct most_aim *aim)
1427 struct most_c_obj *c;
1428 unsigned long flags;
1429 int *num_buffers_ptr;
1431 c = get_channel_by_iface(iface, id);
1435 if (c->aim0.refs && c->aim1.refs &&
1436 ((aim == c->aim0.ptr && c->aim0.num_buffers <= 0) ||
1437 (aim == c->aim1.ptr && c->aim1.num_buffers <= 0)))
1440 if (aim == c->aim0.ptr)
1441 num_buffers_ptr = &c->aim0.num_buffers;
1442 else if (aim == c->aim1.ptr)
1443 num_buffers_ptr = &c->aim1.num_buffers;
1445 num_buffers_ptr = &dummy_num_buffers;
1447 spin_lock_irqsave(&c->fifo_lock, flags);
1448 if (list_empty(&c->fifo)) {
1449 spin_unlock_irqrestore(&c->fifo_lock, flags);
1452 mbo = list_pop_mbo(&c->fifo);
1454 spin_unlock_irqrestore(&c->fifo_lock, flags);
1456 mbo->num_buffers_ptr = num_buffers_ptr;
1457 mbo->buffer_length = c->cfg.buffer_size;
1460 EXPORT_SYMBOL_GPL(most_get_mbo);
1463 * most_put_mbo - return buffer to pool
1464 * @mbo: buffer object
1466 void most_put_mbo(struct mbo *mbo)
1468 struct most_c_obj *c = mbo->context;
1470 if (c->cfg.direction == MOST_CH_TX) {
1475 atomic_inc(&c->mbo_nq_level);
1477 EXPORT_SYMBOL_GPL(most_put_mbo);
1480 * most_read_completion - read completion handler
1481 * @mbo: pointer to MBO
1483 * This function is called by the HDM when data has been received from the
1484 * hardware and copied to the buffer of the MBO.
1486 * In case the channel has been poisoned it puts the buffer in the trash queue.
1487 * Otherwise, it passes the buffer to an AIM for further processing.
1489 static void most_read_completion(struct mbo *mbo)
1491 struct most_c_obj *c = mbo->context;
1493 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1498 if (mbo->status == MBO_E_INVAL) {
1500 atomic_inc(&c->mbo_nq_level);
1504 if (atomic_sub_and_test(1, &c->mbo_nq_level))
1507 if (c->aim0.refs && c->aim0.ptr->rx_completion &&
1508 c->aim0.ptr->rx_completion(mbo) == 0)
1511 if (c->aim1.refs && c->aim1.ptr->rx_completion &&
1512 c->aim1.ptr->rx_completion(mbo) == 0)
1519 * most_start_channel - prepares a channel for communication
1520 * @iface: pointer to interface instance
1523 * This prepares the channel for usage. Cross-checks whether the
1524 * channel's been properly configured.
1526 * Returns 0 on success or error code otherwise.
1528 int most_start_channel(struct most_interface *iface, int id,
1529 struct most_aim *aim)
1533 struct most_c_obj *c = get_channel_by_iface(iface, id);
1538 mutex_lock(&c->start_mutex);
1539 if (c->aim0.refs + c->aim1.refs > 0)
1540 goto out; /* already started by other aim */
1542 if (!try_module_get(iface->mod)) {
1543 pr_info("failed to acquire HDM lock\n");
1544 mutex_unlock(&c->start_mutex);
1548 c->cfg.extra_len = 0;
1549 if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1550 pr_info("channel configuration failed. Go check settings...\n");
1555 init_waitqueue_head(&c->hdm_fifo_wq);
1557 if (c->cfg.direction == MOST_CH_RX)
1558 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1559 most_read_completion);
1561 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1562 most_write_completion);
1563 if (unlikely(!num_buffer)) {
1564 pr_info("failed to allocate memory\n");
1569 ret = run_enqueue_thread(c, id);
1574 c->aim0.num_buffers = c->cfg.num_buffers / 2;
1575 c->aim1.num_buffers = c->cfg.num_buffers - c->aim0.num_buffers;
1576 atomic_set(&c->mbo_ref, num_buffer);
1579 if (aim == c->aim0.ptr)
1581 if (aim == c->aim1.ptr)
1583 mutex_unlock(&c->start_mutex);
1587 module_put(iface->mod);
1588 mutex_unlock(&c->start_mutex);
1591 EXPORT_SYMBOL_GPL(most_start_channel);
1594 * most_stop_channel - stops a running channel
1595 * @iface: pointer to interface instance
1598 int most_stop_channel(struct most_interface *iface, int id,
1599 struct most_aim *aim)
1601 struct most_c_obj *c;
1603 if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1604 pr_err("Bad interface or index out of range\n");
1607 c = get_channel_by_iface(iface, id);
1611 mutex_lock(&c->start_mutex);
1612 if (c->aim0.refs + c->aim1.refs >= 2)
1615 if (c->hdm_enqueue_task)
1616 kthread_stop(c->hdm_enqueue_task);
1617 c->hdm_enqueue_task = NULL;
1620 module_put(iface->mod);
1622 c->is_poisoned = true;
1623 if (c->iface->poison_channel(c->iface, c->channel_id)) {
1624 pr_err("Cannot stop channel %d of mdev %s\n", c->channel_id,
1625 c->iface->description);
1626 mutex_unlock(&c->start_mutex);
1629 flush_trash_fifo(c);
1630 flush_channel_fifos(c);
1632 #ifdef CMPL_INTERRUPTIBLE
1633 if (wait_for_completion_interruptible(&c->cleanup)) {
1634 pr_info("Interrupted while clean up ch %d\n", c->channel_id);
1635 mutex_unlock(&c->start_mutex);
1639 wait_for_completion(&c->cleanup);
1641 c->is_poisoned = false;
1644 if (aim == c->aim0.ptr)
1646 if (aim == c->aim1.ptr)
1648 mutex_unlock(&c->start_mutex);
1651 EXPORT_SYMBOL_GPL(most_stop_channel);
1654 * most_register_aim - registers an AIM (driver) with the core
1655 * @aim: instance of AIM to be registered
1657 int most_register_aim(struct most_aim *aim)
1659 struct most_aim_obj *aim_obj;
1662 pr_err("Bad driver\n");
1665 aim_obj = create_most_aim_obj(aim->name);
1667 pr_info("failed to alloc driver object\n");
1670 aim_obj->driver = aim;
1671 aim->context = aim_obj;
1672 pr_info("registered new application interfacing module %s\n",
1674 list_add_tail(&aim_obj->list, &aim_list);
1677 EXPORT_SYMBOL_GPL(most_register_aim);
1680 * most_deregister_aim - deregisters an AIM (driver) with the core
1681 * @aim: AIM to be removed
1683 int most_deregister_aim(struct most_aim *aim)
1685 struct most_aim_obj *aim_obj;
1686 struct most_c_obj *c, *tmp;
1687 struct most_inst_obj *i, *i_tmp;
1690 pr_err("Bad driver\n");
1694 aim_obj = aim->context;
1696 pr_info("driver not registered.\n");
1699 list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
1700 list_for_each_entry_safe(c, tmp, &i->channel_list, list) {
1701 if (c->aim0.ptr == aim || c->aim1.ptr == aim)
1702 aim->disconnect_channel(
1703 c->iface, c->channel_id);
1704 if (c->aim0.ptr == aim)
1706 if (c->aim1.ptr == aim)
1710 list_del(&aim_obj->list);
1711 destroy_most_aim_obj(aim_obj);
1712 pr_info("deregistering application interfacing module %s\n", aim->name);
1715 EXPORT_SYMBOL_GPL(most_deregister_aim);
1718 * most_register_interface - registers an interface with core
1719 * @iface: pointer to the instance of the interface description.
1721 * Allocates and initializes a new interface instance and all of its channels.
1722 * Returns a pointer to kobject or an error pointer.
1724 struct kobject *most_register_interface(struct most_interface *iface)
1728 char name[STRING_SIZE];
1729 char channel_name[STRING_SIZE];
1730 struct most_c_obj *c;
1731 struct most_inst_obj *inst;
1733 if (!iface || !iface->enqueue || !iface->configure ||
1734 !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1735 pr_err("Bad interface or channel overflow\n");
1736 return ERR_PTR(-EINVAL);
1739 id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1741 pr_info("Failed to alloc mdev ID\n");
1744 snprintf(name, STRING_SIZE, "mdev%d", id);
1746 inst = create_most_inst_obj(name);
1748 pr_info("Failed to allocate interface instance\n");
1749 ida_simple_remove(&mdev_id, id);
1750 return ERR_PTR(-ENOMEM);
1754 INIT_LIST_HEAD(&inst->channel_list);
1755 inst->iface = iface;
1757 list_add_tail(&inst->list, &instance_list);
1759 for (i = 0; i < iface->num_channels; i++) {
1760 const char *name_suffix = iface->channel_vector[i].name_suffix;
1763 snprintf(channel_name, STRING_SIZE, "ch%d", i);
1764 else if (name_suffix[0] == '@')
1765 snprintf(channel_name, STRING_SIZE, "ch%d%s", i,
1768 snprintf(channel_name, STRING_SIZE, "%s", name_suffix);
1770 /* this increments the reference count of this instance */
1771 c = create_most_c_obj(channel_name, &inst->kobj);
1774 inst->channel[i] = c;
1779 c->keep_mbo = false;
1780 c->enqueue_halt = false;
1781 c->is_poisoned = false;
1782 c->cfg.direction = 0;
1783 c->cfg.data_type = 0;
1784 c->cfg.num_buffers = 0;
1785 c->cfg.buffer_size = 0;
1786 c->cfg.subbuffer_size = 0;
1787 c->cfg.packets_per_xact = 0;
1788 spin_lock_init(&c->fifo_lock);
1789 INIT_LIST_HEAD(&c->fifo);
1790 INIT_LIST_HEAD(&c->trash_fifo);
1791 INIT_LIST_HEAD(&c->halt_fifo);
1792 init_completion(&c->cleanup);
1793 atomic_set(&c->mbo_ref, 0);
1794 mutex_init(&c->start_mutex);
1795 mutex_init(&c->nq_mutex);
1796 list_add_tail(&c->list, &inst->channel_list);
1798 pr_info("registered new MOST device mdev%d (%s)\n",
1799 inst->dev_id, iface->description);
1803 pr_info("Failed allocate channel(s)\n");
1804 list_del(&inst->list);
1805 ida_simple_remove(&mdev_id, id);
1806 destroy_most_inst_obj(inst);
1807 return ERR_PTR(-ENOMEM);
1809 EXPORT_SYMBOL_GPL(most_register_interface);
1812 * most_deregister_interface - deregisters an interface with core
1813 * @iface: pointer to the interface instance description.
1815 * Before removing an interface instance from the list, all running
1816 * channels are stopped and poisoned.
1818 void most_deregister_interface(struct most_interface *iface)
1820 struct most_inst_obj *i = iface->priv;
1821 struct most_c_obj *c;
1824 pr_info("Bad Interface\n");
1827 pr_info("deregistering MOST device %s (%s)\n", i->kobj.name,
1828 iface->description);
1830 list_for_each_entry(c, &i->channel_list, list) {
1832 c->aim0.ptr->disconnect_channel(c->iface,
1835 c->aim1.ptr->disconnect_channel(c->iface,
1841 ida_simple_remove(&mdev_id, i->dev_id);
1843 destroy_most_inst_obj(i);
1845 EXPORT_SYMBOL_GPL(most_deregister_interface);
1848 * most_stop_enqueue - prevents core from enqueueing MBOs
1849 * @iface: pointer to interface
1852 * This is called by an HDM that _cannot_ attend to its duties and
1853 * is imminent to get run over by the core. The core is not going to
1854 * enqueue any further packets unless the flagging HDM calls
1855 * most_resume enqueue().
1857 void most_stop_enqueue(struct most_interface *iface, int id)
1859 struct most_c_obj *c = get_channel_by_iface(iface, id);
1864 mutex_lock(&c->nq_mutex);
1865 c->enqueue_halt = true;
1866 mutex_unlock(&c->nq_mutex);
1868 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1871 * most_resume_enqueue - allow core to enqueue MBOs again
1872 * @iface: pointer to interface
1875 * This clears the enqueue halt flag and enqueues all MBOs currently
1876 * sitting in the wait fifo.
1878 void most_resume_enqueue(struct most_interface *iface, int id)
1880 struct most_c_obj *c = get_channel_by_iface(iface, id);
1885 mutex_lock(&c->nq_mutex);
1886 c->enqueue_halt = false;
1887 mutex_unlock(&c->nq_mutex);
1889 wake_up_interruptible(&c->hdm_fifo_wq);
1891 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1893 static int __init most_init(void)
1897 pr_info("init()\n");
1898 INIT_LIST_HEAD(&instance_list);
1899 INIT_LIST_HEAD(&aim_list);
1902 err = bus_register(&most_bus);
1904 pr_info("Cannot register most bus\n");
1908 most_class = class_create(THIS_MODULE, "most");
1909 if (IS_ERR(most_class)) {
1910 pr_info("No udev support.\n");
1911 err = PTR_ERR(most_class);
1915 err = driver_register(&mostcore);
1917 pr_info("Cannot register core driver\n");
1921 core_dev = device_create(most_class, NULL, 0, NULL, "mostcore");
1922 if (IS_ERR(core_dev)) {
1923 err = PTR_ERR(core_dev);
1927 most_aim_kset = kset_create_and_add("aims", NULL, &core_dev->kobj);
1928 if (!most_aim_kset) {
1930 goto exit_class_container;
1933 most_inst_kset = kset_create_and_add("devices", NULL, &core_dev->kobj);
1934 if (!most_inst_kset) {
1936 goto exit_driver_kset;
1942 kset_unregister(most_aim_kset);
1943 exit_class_container:
1944 device_destroy(most_class, 0);
1946 driver_unregister(&mostcore);
1948 class_destroy(most_class);
1950 bus_unregister(&most_bus);
1954 static void __exit most_exit(void)
1956 struct most_inst_obj *i, *i_tmp;
1957 struct most_aim_obj *d, *d_tmp;
1959 pr_info("exit core module\n");
1960 list_for_each_entry_safe(d, d_tmp, &aim_list, list) {
1961 destroy_most_aim_obj(d);
1964 list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
1966 destroy_most_inst_obj(i);
1968 kset_unregister(most_inst_kset);
1969 kset_unregister(most_aim_kset);
1970 device_destroy(most_class, 0);
1971 driver_unregister(&mostcore);
1972 class_destroy(most_class);
1973 bus_unregister(&most_bus);
1974 ida_destroy(&mdev_id);
1977 module_init(most_init);
1978 module_exit(most_exit);
1979 MODULE_LICENSE("GPL");
1980 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1981 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");