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 *class_glue_dir;
37 static struct ida mdev_id;
39 static int dummy_num_buffers;
41 struct most_c_aim_obj {
49 struct completion cleanup;
51 atomic_t mbo_nq_level;
54 struct mutex start_mutex;
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 struct mutex stop_task_mutex;
70 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 {
77 struct most_interface *iface;
78 struct list_head channel_list;
79 struct most_c_obj *channel[MAX_CHANNELS];
81 struct list_head list;
83 #define to_inst_obj(d) container_of(d, struct most_inst_obj, kobj)
86 * list_pop_mbo - retrieves the first MBO of the list and removes it
87 * @ptr: the list head to grab the MBO from.
89 #define list_pop_mbo(ptr) \
91 struct mbo *_mbo = list_first_entry(ptr, struct mbo, list); \
92 list_del(&_mbo->list); \
96 static struct mutex deregister_mutex;
103 * struct most_c_attr - to access the attributes of a channel object
104 * @attr: attributes of a channel
105 * @show: pointer to the show function
106 * @store: pointer to the store function
109 struct attribute attr;
110 ssize_t (*show)(struct most_c_obj *d,
111 struct most_c_attr *attr,
113 ssize_t (*store)(struct most_c_obj *d,
114 struct most_c_attr *attr,
118 #define to_channel_attr(a) container_of(a, struct most_c_attr, attr)
120 #define MOST_CHNL_ATTR(_name, _mode, _show, _store) \
121 struct most_c_attr most_chnl_attr_##_name = \
122 __ATTR(_name, _mode, _show, _store)
125 * channel_attr_show - show function of channel object
126 * @kobj: pointer to its kobject
127 * @attr: pointer to its attributes
130 static ssize_t channel_attr_show(struct kobject *kobj, struct attribute *attr,
133 struct most_c_attr *channel_attr = to_channel_attr(attr);
134 struct most_c_obj *c_obj = to_c_obj(kobj);
136 if (!channel_attr->show)
139 return channel_attr->show(c_obj, channel_attr, buf);
143 * channel_attr_store - store function of channel object
144 * @kobj: pointer to its kobject
145 * @attr: pointer to its attributes
147 * @len: length of buffer
149 static ssize_t channel_attr_store(struct kobject *kobj,
150 struct attribute *attr,
154 struct most_c_attr *channel_attr = to_channel_attr(attr);
155 struct most_c_obj *c_obj = to_c_obj(kobj);
157 if (!channel_attr->store)
159 return channel_attr->store(c_obj, channel_attr, buf, len);
162 static const struct sysfs_ops most_channel_sysfs_ops = {
163 .show = channel_attr_show,
164 .store = channel_attr_store,
168 * most_free_mbo_coherent - free an MBO and its coherent buffer
169 * @mbo: buffer to be released
172 static void most_free_mbo_coherent(struct mbo *mbo)
174 struct most_c_obj *c = mbo->context;
175 u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
177 dma_free_coherent(NULL, coherent_buf_size, mbo->virt_address,
180 if (atomic_sub_and_test(1, &c->mbo_ref))
181 complete(&c->cleanup);
185 * flush_channel_fifos - clear the channel fifos
186 * @c: pointer to channel object
188 static void flush_channel_fifos(struct most_c_obj *c)
190 unsigned long flags, hf_flags;
191 struct mbo *mbo, *tmp;
193 if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
196 spin_lock_irqsave(&c->fifo_lock, flags);
197 list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
198 list_del(&mbo->list);
199 spin_unlock_irqrestore(&c->fifo_lock, flags);
200 most_free_mbo_coherent(mbo);
201 spin_lock_irqsave(&c->fifo_lock, flags);
203 spin_unlock_irqrestore(&c->fifo_lock, flags);
205 spin_lock_irqsave(&c->fifo_lock, hf_flags);
206 list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
207 list_del(&mbo->list);
208 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
209 most_free_mbo_coherent(mbo);
210 spin_lock_irqsave(&c->fifo_lock, hf_flags);
212 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
214 if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
215 pr_info("WARN: fifo | trash fifo not empty\n");
219 * flush_trash_fifo - clear the trash fifo
220 * @c: pointer to channel object
222 static int flush_trash_fifo(struct most_c_obj *c)
224 struct mbo *mbo, *tmp;
227 spin_lock_irqsave(&c->fifo_lock, flags);
228 list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
229 list_del(&mbo->list);
230 spin_unlock_irqrestore(&c->fifo_lock, flags);
231 most_free_mbo_coherent(mbo);
232 spin_lock_irqsave(&c->fifo_lock, flags);
234 spin_unlock_irqrestore(&c->fifo_lock, flags);
239 * most_channel_release - release function of channel object
240 * @kobj: pointer to channel's kobject
242 static void most_channel_release(struct kobject *kobj)
244 struct most_c_obj *c = to_c_obj(kobj);
249 static ssize_t show_available_directions(struct most_c_obj *c,
250 struct most_c_attr *attr,
253 unsigned int i = c->channel_id;
256 if (c->iface->channel_vector[i].direction & MOST_CH_RX)
257 strcat(buf, "dir_rx ");
258 if (c->iface->channel_vector[i].direction & MOST_CH_TX)
259 strcat(buf, "dir_tx ");
261 return strlen(buf) + 1;
264 static ssize_t show_available_datatypes(struct most_c_obj *c,
265 struct most_c_attr *attr,
268 unsigned int i = c->channel_id;
271 if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
272 strcat(buf, "control ");
273 if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
274 strcat(buf, "async ");
275 if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
276 strcat(buf, "sync ");
277 if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC_AVP)
278 strcat(buf, "isoc_avp ");
280 return strlen(buf) + 1;
284 ssize_t show_number_of_packet_buffers(struct most_c_obj *c,
285 struct most_c_attr *attr,
288 unsigned int i = c->channel_id;
290 return snprintf(buf, PAGE_SIZE, "%d\n",
291 c->iface->channel_vector[i].num_buffers_packet);
295 ssize_t show_number_of_stream_buffers(struct most_c_obj *c,
296 struct most_c_attr *attr,
299 unsigned int i = c->channel_id;
301 return snprintf(buf, PAGE_SIZE, "%d\n",
302 c->iface->channel_vector[i].num_buffers_streaming);
306 ssize_t show_size_of_packet_buffer(struct most_c_obj *c,
307 struct most_c_attr *attr,
310 unsigned int i = c->channel_id;
312 return snprintf(buf, PAGE_SIZE, "%d\n",
313 c->iface->channel_vector[i].buffer_size_packet);
317 ssize_t show_size_of_stream_buffer(struct most_c_obj *c,
318 struct most_c_attr *attr,
321 unsigned int i = c->channel_id;
323 return snprintf(buf, PAGE_SIZE, "%d\n",
324 c->iface->channel_vector[i].buffer_size_streaming);
327 static ssize_t show_channel_starving(struct most_c_obj *c,
328 struct most_c_attr *attr,
331 return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
335 #define create_show_channel_attribute(val) \
336 static MOST_CHNL_ATTR(val, S_IRUGO, show_##val, NULL)
338 create_show_channel_attribute(available_directions);
339 create_show_channel_attribute(available_datatypes);
340 create_show_channel_attribute(number_of_packet_buffers);
341 create_show_channel_attribute(number_of_stream_buffers);
342 create_show_channel_attribute(size_of_stream_buffer);
343 create_show_channel_attribute(size_of_packet_buffer);
344 create_show_channel_attribute(channel_starving);
346 static ssize_t show_set_number_of_buffers(struct most_c_obj *c,
347 struct most_c_attr *attr,
350 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
353 static ssize_t store_set_number_of_buffers(struct most_c_obj *c,
354 struct most_c_attr *attr,
358 int ret = kstrtou16(buf, 0, &c->cfg.num_buffers);
365 static ssize_t show_set_buffer_size(struct most_c_obj *c,
366 struct most_c_attr *attr,
369 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
372 static ssize_t store_set_buffer_size(struct most_c_obj *c,
373 struct most_c_attr *attr,
377 int ret = kstrtou16(buf, 0, &c->cfg.buffer_size);
384 static ssize_t show_set_direction(struct most_c_obj *c,
385 struct most_c_attr *attr,
388 if (c->cfg.direction & MOST_CH_TX)
389 return snprintf(buf, PAGE_SIZE, "dir_tx\n");
390 else if (c->cfg.direction & MOST_CH_RX)
391 return snprintf(buf, PAGE_SIZE, "dir_rx\n");
392 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
395 static ssize_t store_set_direction(struct most_c_obj *c,
396 struct most_c_attr *attr,
400 if (!strcmp(buf, "dir_rx\n"))
401 c->cfg.direction = MOST_CH_RX;
402 else if (!strcmp(buf, "dir_tx\n"))
403 c->cfg.direction = MOST_CH_TX;
405 pr_info("WARN: invalid attribute settings\n");
411 static ssize_t show_set_datatype(struct most_c_obj *c,
412 struct most_c_attr *attr,
415 if (c->cfg.data_type & MOST_CH_CONTROL)
416 return snprintf(buf, PAGE_SIZE, "control\n");
417 else if (c->cfg.data_type & MOST_CH_ASYNC)
418 return snprintf(buf, PAGE_SIZE, "async\n");
419 else if (c->cfg.data_type & MOST_CH_SYNC)
420 return snprintf(buf, PAGE_SIZE, "sync\n");
421 else if (c->cfg.data_type & MOST_CH_ISOC_AVP)
422 return snprintf(buf, PAGE_SIZE, "isoc_avp\n");
423 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
426 static ssize_t store_set_datatype(struct most_c_obj *c,
427 struct most_c_attr *attr,
431 if (!strcmp(buf, "control\n"))
432 c->cfg.data_type = MOST_CH_CONTROL;
433 else if (!strcmp(buf, "async\n"))
434 c->cfg.data_type = MOST_CH_ASYNC;
435 else if (!strcmp(buf, "sync\n"))
436 c->cfg.data_type = MOST_CH_SYNC;
437 else if (!strcmp(buf, "isoc_avp\n"))
438 c->cfg.data_type = MOST_CH_ISOC_AVP;
440 pr_info("WARN: invalid attribute settings\n");
446 static ssize_t show_set_subbuffer_size(struct most_c_obj *c,
447 struct most_c_attr *attr,
450 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
453 static ssize_t store_set_subbuffer_size(struct most_c_obj *c,
454 struct most_c_attr *attr,
458 int ret = kstrtou16(buf, 0, &c->cfg.subbuffer_size);
465 static ssize_t show_set_packets_per_xact(struct most_c_obj *c,
466 struct most_c_attr *attr,
469 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
472 static ssize_t store_set_packets_per_xact(struct most_c_obj *c,
473 struct most_c_attr *attr,
477 int ret = kstrtou16(buf, 0, &c->cfg.packets_per_xact);
484 #define create_channel_attribute(value) \
485 static MOST_CHNL_ATTR(value, S_IRUGO | S_IWUSR, \
489 create_channel_attribute(set_buffer_size);
490 create_channel_attribute(set_number_of_buffers);
491 create_channel_attribute(set_direction);
492 create_channel_attribute(set_datatype);
493 create_channel_attribute(set_subbuffer_size);
494 create_channel_attribute(set_packets_per_xact);
498 * most_channel_def_attrs - array of default attributes of channel object
500 static struct attribute *most_channel_def_attrs[] = {
501 &most_chnl_attr_available_directions.attr,
502 &most_chnl_attr_available_datatypes.attr,
503 &most_chnl_attr_number_of_packet_buffers.attr,
504 &most_chnl_attr_number_of_stream_buffers.attr,
505 &most_chnl_attr_size_of_packet_buffer.attr,
506 &most_chnl_attr_size_of_stream_buffer.attr,
507 &most_chnl_attr_set_number_of_buffers.attr,
508 &most_chnl_attr_set_buffer_size.attr,
509 &most_chnl_attr_set_direction.attr,
510 &most_chnl_attr_set_datatype.attr,
511 &most_chnl_attr_set_subbuffer_size.attr,
512 &most_chnl_attr_set_packets_per_xact.attr,
513 &most_chnl_attr_channel_starving.attr,
517 static struct kobj_type most_channel_ktype = {
518 .sysfs_ops = &most_channel_sysfs_ops,
519 .release = most_channel_release,
520 .default_attrs = most_channel_def_attrs,
523 static struct kset *most_channel_kset;
526 * create_most_c_obj - allocates a channel object
527 * @name: name of the channel object
528 * @parent: parent kobject
530 * This create a channel object and registers it with sysfs.
531 * Returns a pointer to the object or NULL when something went wrong.
533 static struct most_c_obj *
534 create_most_c_obj(const char *name, struct kobject *parent)
536 struct most_c_obj *c;
539 c = kzalloc(sizeof(*c), GFP_KERNEL);
542 c->kobj.kset = most_channel_kset;
543 retval = kobject_init_and_add(&c->kobj, &most_channel_ktype, parent,
546 kobject_put(&c->kobj);
549 kobject_uevent(&c->kobj, KOBJ_ADD);
554 * destroy_most_c_obj - channel release function
555 * @c: pointer to channel object
557 * This decrements the reference counter of the channel object.
558 * If the reference count turns zero, its release function is called.
560 static void destroy_most_c_obj(struct most_c_obj *c)
563 c->aim0.ptr->disconnect_channel(c->iface, c->channel_id);
565 c->aim1.ptr->disconnect_channel(c->iface, c->channel_id);
569 mutex_lock(&deregister_mutex);
571 flush_channel_fifos(c);
572 mutex_unlock(&deregister_mutex);
573 kobject_put(&c->kobj);
577 * ___I N S T A N C E___
579 #define MOST_INST_ATTR(_name, _mode, _show, _store) \
580 struct most_inst_attribute most_inst_attr_##_name = \
581 __ATTR(_name, _mode, _show, _store)
583 static struct list_head instance_list;
586 * struct most_inst_attribute - to access the attributes of instance object
587 * @attr: attributes of an instance
588 * @show: pointer to the show function
589 * @store: pointer to the store function
591 struct most_inst_attribute {
592 struct attribute attr;
593 ssize_t (*show)(struct most_inst_obj *d,
594 struct most_inst_attribute *attr,
596 ssize_t (*store)(struct most_inst_obj *d,
597 struct most_inst_attribute *attr,
601 #define to_instance_attr(a) \
602 container_of(a, struct most_inst_attribute, attr)
605 * instance_attr_show - show function for an instance object
606 * @kobj: pointer to kobject
607 * @attr: pointer to attribute struct
610 static ssize_t instance_attr_show(struct kobject *kobj,
611 struct attribute *attr,
614 struct most_inst_attribute *instance_attr;
615 struct most_inst_obj *instance_obj;
617 instance_attr = to_instance_attr(attr);
618 instance_obj = to_inst_obj(kobj);
620 if (!instance_attr->show)
623 return instance_attr->show(instance_obj, instance_attr, buf);
627 * instance_attr_store - store function for an instance object
628 * @kobj: pointer to kobject
629 * @attr: pointer to attribute struct
631 * @len: length of buffer
633 static ssize_t instance_attr_store(struct kobject *kobj,
634 struct attribute *attr,
638 struct most_inst_attribute *instance_attr;
639 struct most_inst_obj *instance_obj;
641 instance_attr = to_instance_attr(attr);
642 instance_obj = to_inst_obj(kobj);
644 if (!instance_attr->store)
647 return instance_attr->store(instance_obj, instance_attr, buf, len);
650 static const struct sysfs_ops most_inst_sysfs_ops = {
651 .show = instance_attr_show,
652 .store = instance_attr_store,
656 * most_inst_release - release function for instance object
657 * @kobj: pointer to instance's kobject
659 * This frees the allocated memory for the instance object
661 static void most_inst_release(struct kobject *kobj)
663 struct most_inst_obj *inst = to_inst_obj(kobj);
668 static ssize_t show_description(struct most_inst_obj *instance_obj,
669 struct most_inst_attribute *attr,
672 return snprintf(buf, PAGE_SIZE, "%s\n",
673 instance_obj->iface->description);
676 static ssize_t show_interface(struct most_inst_obj *instance_obj,
677 struct most_inst_attribute *attr,
680 switch (instance_obj->iface->interface) {
682 return snprintf(buf, PAGE_SIZE, "loopback\n");
684 return snprintf(buf, PAGE_SIZE, "i2c\n");
686 return snprintf(buf, PAGE_SIZE, "i2s\n");
688 return snprintf(buf, PAGE_SIZE, "tsi\n");
690 return snprintf(buf, PAGE_SIZE, "hbi\n");
691 case ITYPE_MEDIALB_DIM:
692 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
693 case ITYPE_MEDIALB_DIM2:
694 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
696 return snprintf(buf, PAGE_SIZE, "usb\n");
698 return snprintf(buf, PAGE_SIZE, "pcie\n");
700 return snprintf(buf, PAGE_SIZE, "unknown\n");
703 #define create_inst_attribute(value) \
704 static MOST_INST_ATTR(value, S_IRUGO, show_##value, NULL)
706 create_inst_attribute(description);
707 create_inst_attribute(interface);
709 static struct attribute *most_inst_def_attrs[] = {
710 &most_inst_attr_description.attr,
711 &most_inst_attr_interface.attr,
715 static struct kobj_type most_inst_ktype = {
716 .sysfs_ops = &most_inst_sysfs_ops,
717 .release = most_inst_release,
718 .default_attrs = most_inst_def_attrs,
721 static struct kset *most_inst_kset;
725 * create_most_inst_obj - creates an instance object
726 * @name: name of the object to be created
728 * This allocates memory for an instance structure, assigns the proper kset
729 * and registers it with sysfs.
731 * Returns a pointer to the instance object or NULL when something went wrong.
733 static struct most_inst_obj *create_most_inst_obj(const char *name)
735 struct most_inst_obj *inst;
738 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
741 inst->kobj.kset = most_inst_kset;
742 retval = kobject_init_and_add(&inst->kobj, &most_inst_ktype, NULL,
745 kobject_put(&inst->kobj);
748 kobject_uevent(&inst->kobj, KOBJ_ADD);
753 * destroy_most_inst_obj - MOST instance release function
754 * @inst: pointer to the instance object
756 * This decrements the reference counter of the instance object.
757 * If the reference count turns zero, its release function is called
759 static void destroy_most_inst_obj(struct most_inst_obj *inst)
761 struct most_c_obj *c, *tmp;
763 /* need to destroy channels first, since
764 * each channel incremented the
765 * reference count of the inst->kobj
767 list_for_each_entry_safe(c, tmp, &inst->channel_list, list) {
768 destroy_most_c_obj(c);
770 kobject_put(&inst->kobj);
776 struct most_aim_obj {
778 struct list_head list;
779 struct most_aim *driver;
780 char add_link[STRING_SIZE];
781 char remove_link[STRING_SIZE];
783 #define to_aim_obj(d) container_of(d, struct most_aim_obj, kobj)
785 static struct list_head aim_list;
789 * struct most_aim_attribute - to access the attributes of AIM object
790 * @attr: attributes of an AIM
791 * @show: pointer to the show function
792 * @store: pointer to the store function
794 struct most_aim_attribute {
795 struct attribute attr;
796 ssize_t (*show)(struct most_aim_obj *d,
797 struct most_aim_attribute *attr,
799 ssize_t (*store)(struct most_aim_obj *d,
800 struct most_aim_attribute *attr,
804 #define to_aim_attr(a) container_of(a, struct most_aim_attribute, attr)
807 * aim_attr_show - show function of an AIM object
808 * @kobj: pointer to kobject
809 * @attr: pointer to attribute struct
812 static ssize_t aim_attr_show(struct kobject *kobj,
813 struct attribute *attr,
816 struct most_aim_attribute *aim_attr;
817 struct most_aim_obj *aim_obj;
819 aim_attr = to_aim_attr(attr);
820 aim_obj = to_aim_obj(kobj);
825 return aim_attr->show(aim_obj, aim_attr, buf);
829 * aim_attr_store - store function of an AIM object
830 * @kobj: pointer to kobject
831 * @attr: pointer to attribute struct
833 * @len: length of buffer
835 static ssize_t aim_attr_store(struct kobject *kobj,
836 struct attribute *attr,
840 struct most_aim_attribute *aim_attr;
841 struct most_aim_obj *aim_obj;
843 aim_attr = to_aim_attr(attr);
844 aim_obj = to_aim_obj(kobj);
846 if (!aim_attr->store)
848 return aim_attr->store(aim_obj, aim_attr, buf, len);
851 static const struct sysfs_ops most_aim_sysfs_ops = {
852 .show = aim_attr_show,
853 .store = aim_attr_store,
857 * most_aim_release - AIM release function
858 * @kobj: pointer to AIM's kobject
860 static void most_aim_release(struct kobject *kobj)
862 struct most_aim_obj *aim_obj = to_aim_obj(kobj);
867 static ssize_t show_add_link(struct most_aim_obj *aim_obj,
868 struct most_aim_attribute *attr,
871 return snprintf(buf, PAGE_SIZE, "%s\n", aim_obj->add_link);
875 * split_string - parses and changes string in the buffer buf and
876 * splits it into two mandatory and one optional substrings.
878 * @buf: complete string from attribute 'add_channel'
879 * @a: address of pointer to 1st substring (=instance name)
880 * @b: address of pointer to 2nd substring (=channel name)
881 * @c: optional address of pointer to 3rd substring (=user defined name)
885 * Input: "mdev0:ch0@ep_81:my_channel\n" or
886 * "mdev0:ch0@ep_81:my_channel"
888 * Output: *a -> "mdev0", *b -> "ch0@ep_81", *c -> "my_channel"
890 * Input: "mdev0:ch0@ep_81\n"
891 * Output: *a -> "mdev0", *b -> "ch0@ep_81", *c -> ""
893 * Input: "mdev0:ch0@ep_81"
894 * Output: *a -> "mdev0", *b -> "ch0@ep_81", *c == NULL
896 static int split_string(char *buf, char **a, char **b, char **c)
898 *a = strsep(&buf, ":");
902 *b = strsep(&buf, ":\n");
907 *c = strsep(&buf, ":\n");
913 * get_channel_by_name - get pointer to channel object
914 * @mdev: name of the device instance
915 * @mdev_ch: name of the respective channel
917 * This retrieves the pointer to a channel object.
920 most_c_obj *get_channel_by_name(char *mdev, char *mdev_ch)
922 struct most_c_obj *c, *tmp;
923 struct most_inst_obj *i, *i_tmp;
926 list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
927 if (!strcmp(kobject_name(&i->kobj), mdev)) {
932 if (unlikely(!found))
933 return ERR_PTR(-EIO);
935 list_for_each_entry_safe(c, tmp, &i->channel_list, list) {
936 if (!strcmp(kobject_name(&c->kobj), mdev_ch)) {
941 if (unlikely(2 > found))
942 return ERR_PTR(-EIO);
947 * store_add_link - store() function for add_link attribute
948 * @aim_obj: pointer to AIM object
949 * @attr: its attributes
951 * @len: buffer length
953 * This parses the string given by buf and splits it into
954 * three substrings. Note: third substring is optional. In case a cdev
955 * AIM is loaded the optional 3rd substring will make up the name of
956 * device node in the /dev directory. If omitted, the device node will
957 * inherit the channel's name within sysfs.
959 * Searches for a pair of device and channel and probes the AIM
962 * (1) echo -n -e "mdev0:ch0@ep_81:my_rxchannel\n" >add_link
963 * (2) echo -n -e "mdev0:ch0@ep_81\n" >add_link
965 * (1) would create the device node /dev/my_rxchannel
966 * (2) would create the device node /dev/mdev0-ch0@ep_81
968 static ssize_t store_add_link(struct most_aim_obj *aim_obj,
969 struct most_aim_attribute *attr,
973 struct most_c_obj *c;
974 struct most_aim **aim_ptr;
975 char buffer[STRING_SIZE];
979 char devnod_buf[STRING_SIZE];
981 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
983 strlcpy(buffer, buf, max_len);
984 strlcpy(aim_obj->add_link, buf, max_len);
986 ret = split_string(buffer, &mdev, &mdev_ch, &mdev_devnod);
990 if (!mdev_devnod || *mdev_devnod == 0) {
991 snprintf(devnod_buf, sizeof(devnod_buf), "%s-%s", mdev, mdev_ch);
992 mdev_devnod = devnod_buf;
995 c = get_channel_by_name(mdev, mdev_ch);
1000 aim_ptr = &c->aim0.ptr;
1001 else if (!c->aim1.ptr)
1002 aim_ptr = &c->aim1.ptr;
1006 ret = aim_obj->driver->probe_channel(c->iface, c->channel_id,
1007 &c->cfg, &c->kobj, mdev_devnod);
1010 *aim_ptr = aim_obj->driver;
1014 static struct most_aim_attribute most_aim_attr_add_link =
1015 __ATTR(add_link, S_IRUGO | S_IWUSR, show_add_link, store_add_link);
1017 static ssize_t show_remove_link(struct most_aim_obj *aim_obj,
1018 struct most_aim_attribute *attr,
1021 return snprintf(buf, PAGE_SIZE, "%s\n", aim_obj->remove_link);
1025 * store_remove_link - store function for remove_link attribute
1026 * @aim_obj: pointer to AIM object
1027 * @attr: its attributes
1029 * @len: buffer length
1032 * echo -n -e "mdev0:ch0@ep_81\n" >remove_link
1034 static ssize_t store_remove_link(struct most_aim_obj *aim_obj,
1035 struct most_aim_attribute *attr,
1039 struct most_c_obj *c;
1040 char buffer[STRING_SIZE];
1044 size_t max_len = min_t(size_t, len + 1, STRING_SIZE);
1046 strlcpy(buffer, buf, max_len);
1047 strlcpy(aim_obj->remove_link, buf, max_len);
1048 ret = split_string(buffer, &mdev, &mdev_ch, NULL);
1052 c = get_channel_by_name(mdev, mdev_ch);
1056 if (c->aim0.ptr == aim_obj->driver)
1058 if (c->aim1.ptr == aim_obj->driver)
1060 if (aim_obj->driver->disconnect_channel(c->iface, c->channel_id))
1065 static struct most_aim_attribute most_aim_attr_remove_link =
1066 __ATTR(remove_link, S_IRUGO | S_IWUSR, show_remove_link, store_remove_link);
1068 static struct attribute *most_aim_def_attrs[] = {
1069 &most_aim_attr_add_link.attr,
1070 &most_aim_attr_remove_link.attr,
1074 static struct kobj_type most_aim_ktype = {
1075 .sysfs_ops = &most_aim_sysfs_ops,
1076 .release = most_aim_release,
1077 .default_attrs = most_aim_def_attrs,
1080 static struct kset *most_aim_kset;
1083 * create_most_aim_obj - creates an AIM object
1084 * @name: name of the AIM
1086 * This creates an AIM object assigns the proper kset and registers
1088 * Returns a pointer to the object or NULL if something went wrong.
1090 static struct most_aim_obj *create_most_aim_obj(const char *name)
1092 struct most_aim_obj *most_aim;
1095 most_aim = kzalloc(sizeof(*most_aim), GFP_KERNEL);
1098 most_aim->kobj.kset = most_aim_kset;
1099 retval = kobject_init_and_add(&most_aim->kobj, &most_aim_ktype,
1102 kobject_put(&most_aim->kobj);
1105 kobject_uevent(&most_aim->kobj, KOBJ_ADD);
1110 * destroy_most_aim_obj - AIM release function
1111 * @p: pointer to AIM object
1113 * This decrements the reference counter of the AIM object. If the
1114 * reference count turns zero, its release function will be called.
1116 static void destroy_most_aim_obj(struct most_aim_obj *p)
1118 kobject_put(&p->kobj);
1127 * Instantiation of the MOST bus
1129 static struct bus_type most_bus = {
1134 * Instantiation of the core driver
1136 static struct device_driver mostcore = {
1141 static inline void trash_mbo(struct mbo *mbo)
1143 unsigned long flags;
1144 struct most_c_obj *c = mbo->context;
1146 spin_lock_irqsave(&c->fifo_lock, flags);
1147 list_add(&mbo->list, &c->trash_fifo);
1148 spin_unlock_irqrestore(&c->fifo_lock, flags);
1151 static struct mbo *get_hdm_mbo(struct most_c_obj *c)
1153 unsigned long flags;
1156 spin_lock_irqsave(&c->fifo_lock, flags);
1157 if (c->enqueue_halt || list_empty(&c->halt_fifo))
1160 mbo = list_pop_mbo(&c->halt_fifo);
1161 spin_unlock_irqrestore(&c->fifo_lock, flags);
1165 static void nq_hdm_mbo(struct mbo *mbo)
1167 unsigned long flags;
1168 struct most_c_obj *c = mbo->context;
1170 spin_lock_irqsave(&c->fifo_lock, flags);
1171 list_add_tail(&mbo->list, &c->halt_fifo);
1172 spin_unlock_irqrestore(&c->fifo_lock, flags);
1173 wake_up_interruptible(&c->hdm_fifo_wq);
1176 static int hdm_enqueue_thread(void *data)
1178 struct most_c_obj *c = data;
1180 typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
1182 while (likely(!kthread_should_stop())) {
1183 wait_event_interruptible(c->hdm_fifo_wq,
1184 (mbo = get_hdm_mbo(c))
1185 || kthread_should_stop());
1190 if (c->cfg.direction == MOST_CH_RX)
1191 mbo->buffer_length = c->cfg.buffer_size;
1193 if (unlikely(enqueue(mbo->ifp, mbo->hdm_channel_id, mbo))) {
1194 pr_err("hdm enqueue failed\n");
1196 c->hdm_enqueue_task = NULL;
1204 static int run_enqueue_thread(struct most_c_obj *c, int channel_id)
1206 struct task_struct *task =
1207 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
1211 return PTR_ERR(task);
1213 c->hdm_enqueue_task = task;
1218 * arm_mbo - recycle MBO for further usage
1219 * @mbo: buffer object
1221 * This puts an MBO back to the list to have it ready for up coming
1224 * In case the MBO belongs to a channel that recently has been
1225 * poisoned, the MBO is scheduled to be trashed.
1226 * Calls the completion handler of an attached AIM.
1228 static void arm_mbo(struct mbo *mbo)
1230 unsigned long flags;
1231 struct most_c_obj *c;
1233 BUG_ON((!mbo) || (!mbo->context));
1236 if (c->is_poisoned) {
1241 spin_lock_irqsave(&c->fifo_lock, flags);
1242 ++*mbo->num_buffers_ptr;
1243 list_add_tail(&mbo->list, &c->fifo);
1244 spin_unlock_irqrestore(&c->fifo_lock, flags);
1246 if (c->aim0.refs && c->aim0.ptr->tx_completion)
1247 c->aim0.ptr->tx_completion(c->iface, c->channel_id);
1249 if (c->aim1.refs && c->aim1.ptr->tx_completion)
1250 c->aim1.ptr->tx_completion(c->iface, c->channel_id);
1254 * arm_mbo_chain - helper function that arms an MBO chain for the HDM
1255 * @c: pointer to interface channel
1256 * @dir: direction of the channel
1257 * @compl: pointer to completion function
1259 * This allocates buffer objects including the containing DMA coherent
1260 * buffer and puts them in the fifo.
1261 * Buffers of Rx channels are put in the kthread fifo, hence immediately
1262 * submitted to the HDM.
1264 * Returns the number of allocated and enqueued MBOs.
1266 static int arm_mbo_chain(struct most_c_obj *c, int dir,
1267 void (*compl)(struct mbo *))
1272 u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
1274 atomic_set(&c->mbo_nq_level, 0);
1276 for (i = 0; i < c->cfg.num_buffers; i++) {
1277 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
1279 pr_info("WARN: Allocation of MBO failed.\n");
1284 mbo->ifp = c->iface;
1285 mbo->hdm_channel_id = c->channel_id;
1286 mbo->virt_address = dma_alloc_coherent(NULL,
1290 if (!mbo->virt_address) {
1291 pr_info("WARN: No DMA coherent buffer.\n");
1295 mbo->complete = compl;
1296 mbo->num_buffers_ptr = &dummy_num_buffers;
1297 if (dir == MOST_CH_RX) {
1299 atomic_inc(&c->mbo_nq_level);
1313 * most_submit_mbo - submits an MBO to fifo
1314 * @mbo: pointer to the MBO
1317 int most_submit_mbo(struct mbo *mbo)
1319 struct most_c_obj *c;
1320 struct most_inst_obj *i;
1322 if (unlikely((!mbo) || (!mbo->context))) {
1323 pr_err("Bad MBO or missing channel reference\n");
1329 if (unlikely(atomic_read(&i->tainted)))
1335 EXPORT_SYMBOL_GPL(most_submit_mbo);
1338 * most_write_completion - write completion handler
1339 * @mbo: pointer to MBO
1341 * This recycles the MBO for further usage. In case the channel has been
1342 * poisoned, the MBO is scheduled to be trashed.
1344 static void most_write_completion(struct mbo *mbo)
1346 struct most_c_obj *c;
1348 BUG_ON((!mbo) || (!mbo->context));
1351 if (mbo->status == MBO_E_INVAL)
1352 pr_info("WARN: Tx MBO status: invalid\n");
1353 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
1360 * get_channel_by_iface - get pointer to channel object
1361 * @iface: pointer to interface instance
1364 * This retrieves a pointer to a channel of the given interface and channel ID.
1367 most_c_obj *get_channel_by_iface(struct most_interface *iface, int id)
1369 struct most_inst_obj *i;
1371 if (unlikely(!iface)) {
1372 pr_err("Bad interface\n");
1375 if (unlikely((id < 0) || (id >= iface->num_channels))) {
1376 pr_err("Channel index (%d) out of range\n", id);
1381 pr_err("interface is not registered\n");
1384 return i->channel[id];
1387 int channel_has_mbo(struct most_interface *iface, int id)
1389 struct most_c_obj *c = get_channel_by_iface(iface, id);
1390 unsigned long flags;
1396 spin_lock_irqsave(&c->fifo_lock, flags);
1397 empty = list_empty(&c->fifo);
1398 spin_unlock_irqrestore(&c->fifo_lock, flags);
1401 EXPORT_SYMBOL_GPL(channel_has_mbo);
1404 * most_get_mbo - get pointer to an MBO of pool
1405 * @iface: pointer to interface instance
1408 * This attempts to get a free buffer out of the channel fifo.
1409 * Returns a pointer to MBO on success or NULL otherwise.
1411 struct mbo *most_get_mbo(struct most_interface *iface, int id,
1412 struct most_aim *aim)
1415 struct most_c_obj *c;
1416 unsigned long flags;
1417 int *num_buffers_ptr;
1419 c = get_channel_by_iface(iface, id);
1423 if (c->aim0.refs && c->aim1.refs &&
1424 ((aim == c->aim0.ptr && c->aim0.num_buffers <= 0) ||
1425 (aim == c->aim1.ptr && c->aim1.num_buffers <= 0)))
1428 if (aim == c->aim0.ptr)
1429 num_buffers_ptr = &c->aim0.num_buffers;
1430 else if (aim == c->aim1.ptr)
1431 num_buffers_ptr = &c->aim1.num_buffers;
1433 num_buffers_ptr = &dummy_num_buffers;
1435 spin_lock_irqsave(&c->fifo_lock, flags);
1436 if (list_empty(&c->fifo)) {
1437 spin_unlock_irqrestore(&c->fifo_lock, flags);
1440 mbo = list_pop_mbo(&c->fifo);
1442 spin_unlock_irqrestore(&c->fifo_lock, flags);
1444 mbo->num_buffers_ptr = num_buffers_ptr;
1445 mbo->buffer_length = c->cfg.buffer_size;
1448 EXPORT_SYMBOL_GPL(most_get_mbo);
1452 * most_put_mbo - return buffer to pool
1453 * @mbo: buffer object
1455 void most_put_mbo(struct mbo *mbo)
1457 struct most_c_obj *c;
1458 struct most_inst_obj *i;
1463 if (unlikely(atomic_read(&i->tainted))) {
1464 mbo->status = MBO_E_CLOSE;
1468 if (c->cfg.direction == MOST_CH_TX) {
1473 atomic_inc(&c->mbo_nq_level);
1475 EXPORT_SYMBOL_GPL(most_put_mbo);
1478 * most_read_completion - read completion handler
1479 * @mbo: pointer to MBO
1481 * This function is called by the HDM when data has been received from the
1482 * hardware and copied to the buffer of the MBO.
1484 * In case the channel has been poisoned it puts the buffer in the trash queue.
1485 * Otherwise, it passes the buffer to an AIM for further processing.
1487 static void most_read_completion(struct mbo *mbo)
1489 struct most_c_obj *c = mbo->context;
1491 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1496 if (mbo->status == MBO_E_INVAL) {
1498 atomic_inc(&c->mbo_nq_level);
1502 if (atomic_sub_and_test(1, &c->mbo_nq_level)) {
1503 pr_info("WARN: rx device out of buffers\n");
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);
1549 c->cfg.extra_len = 0;
1550 if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1551 pr_info("channel configuration failed. Go check settings...\n");
1556 init_waitqueue_head(&c->hdm_fifo_wq);
1558 if (c->cfg.direction == MOST_CH_RX)
1559 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1560 most_read_completion);
1562 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1563 most_write_completion);
1564 if (unlikely(0 == num_buffer)) {
1565 pr_info("failed to allocate memory\n");
1570 ret = run_enqueue_thread(c, id);
1575 c->aim0.num_buffers = c->cfg.num_buffers / 2;
1576 c->aim1.num_buffers = c->cfg.num_buffers - c->aim0.num_buffers;
1577 atomic_set(&c->mbo_ref, num_buffer);
1580 if (aim == c->aim0.ptr)
1582 if (aim == c->aim1.ptr)
1584 mutex_unlock(&c->start_mutex);
1589 module_put(iface->mod);
1591 mutex_unlock(&c->start_mutex);
1594 EXPORT_SYMBOL_GPL(most_start_channel);
1597 * most_stop_channel - stops a running channel
1598 * @iface: pointer to interface instance
1601 int most_stop_channel(struct most_interface *iface, int id,
1602 struct most_aim *aim)
1604 struct most_c_obj *c;
1606 if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1607 pr_err("Bad interface or index out of range\n");
1610 c = get_channel_by_iface(iface, id);
1614 mutex_lock(&c->start_mutex);
1615 if (c->aim0.refs + c->aim1.refs >= 2)
1618 mutex_lock(&c->stop_task_mutex);
1619 if (c->hdm_enqueue_task)
1620 kthread_stop(c->hdm_enqueue_task);
1621 c->hdm_enqueue_task = NULL;
1622 mutex_unlock(&c->stop_task_mutex);
1624 mutex_lock(&deregister_mutex);
1625 if (atomic_read(&c->inst->tainted)) {
1626 mutex_unlock(&deregister_mutex);
1627 mutex_unlock(&c->start_mutex);
1630 mutex_unlock(&deregister_mutex);
1632 if (iface->mod && modref) {
1633 module_put(iface->mod);
1637 c->is_poisoned = true;
1638 if (c->iface->poison_channel(c->iface, c->channel_id)) {
1639 pr_err("Cannot stop channel %d of mdev %s\n", c->channel_id,
1640 c->iface->description);
1641 mutex_unlock(&c->start_mutex);
1644 flush_trash_fifo(c);
1645 flush_channel_fifos(c);
1647 #ifdef CMPL_INTERRUPTIBLE
1648 if (wait_for_completion_interruptible(&c->cleanup)) {
1649 pr_info("Interrupted while clean up ch %d\n", c->channel_id);
1650 mutex_unlock(&c->start_mutex);
1654 wait_for_completion(&c->cleanup);
1656 c->is_poisoned = false;
1659 if (aim == c->aim0.ptr)
1661 if (aim == c->aim1.ptr)
1663 mutex_unlock(&c->start_mutex);
1666 EXPORT_SYMBOL_GPL(most_stop_channel);
1669 * most_register_aim - registers an AIM (driver) with the core
1670 * @aim: instance of AIM to be registered
1672 int most_register_aim(struct most_aim *aim)
1674 struct most_aim_obj *aim_obj;
1677 pr_err("Bad driver\n");
1680 aim_obj = create_most_aim_obj(aim->name);
1682 pr_info("failed to alloc driver object\n");
1685 aim_obj->driver = aim;
1686 aim->context = aim_obj;
1687 pr_info("registered new application interfacing module %s\n",
1689 list_add_tail(&aim_obj->list, &aim_list);
1692 EXPORT_SYMBOL_GPL(most_register_aim);
1695 * most_deregister_aim - deregisters an AIM (driver) with the core
1696 * @aim: AIM to be removed
1698 int most_deregister_aim(struct most_aim *aim)
1700 struct most_aim_obj *aim_obj;
1701 struct most_c_obj *c, *tmp;
1702 struct most_inst_obj *i, *i_tmp;
1705 pr_err("Bad driver\n");
1709 aim_obj = aim->context;
1711 pr_info("driver not registered.\n");
1714 list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
1715 list_for_each_entry_safe(c, tmp, &i->channel_list, list) {
1716 if (c->aim0.ptr == aim || c->aim1.ptr == aim)
1717 aim->disconnect_channel(
1718 c->iface, c->channel_id);
1719 if (c->aim0.ptr == aim)
1721 if (c->aim1.ptr == aim)
1725 list_del(&aim_obj->list);
1726 destroy_most_aim_obj(aim_obj);
1727 pr_info("deregistering application interfacing module %s\n", aim->name);
1730 EXPORT_SYMBOL_GPL(most_deregister_aim);
1733 * most_register_interface - registers an interface with core
1734 * @iface: pointer to the instance of the interface description.
1736 * Allocates and initializes a new interface instance and all of its channels.
1737 * Returns a pointer to kobject or an error pointer.
1739 struct kobject *most_register_interface(struct most_interface *iface)
1743 char name[STRING_SIZE];
1744 char channel_name[STRING_SIZE];
1745 struct most_c_obj *c;
1746 struct most_inst_obj *inst;
1748 if (!iface || !iface->enqueue || !iface->configure ||
1749 !iface->poison_channel || (iface->num_channels > MAX_CHANNELS)) {
1750 pr_err("Bad interface or channel overflow\n");
1751 return ERR_PTR(-EINVAL);
1754 id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1756 pr_info("Failed to alloc mdev ID\n");
1759 snprintf(name, STRING_SIZE, "mdev%d", id);
1761 inst = create_most_inst_obj(name);
1763 pr_info("Failed to allocate interface instance\n");
1764 return ERR_PTR(-ENOMEM);
1768 INIT_LIST_HEAD(&inst->channel_list);
1769 inst->iface = iface;
1771 atomic_set(&inst->tainted, 0);
1772 list_add_tail(&inst->list, &instance_list);
1774 for (i = 0; i < iface->num_channels; i++) {
1775 const char *name_suffix = iface->channel_vector[i].name_suffix;
1778 snprintf(channel_name, STRING_SIZE, "ch%d", i);
1779 else if (name_suffix[0] == '@')
1780 snprintf(channel_name, STRING_SIZE, "ch%d%s", i,
1783 snprintf(channel_name, STRING_SIZE, "%s", name_suffix);
1785 /* this increments the reference count of this instance */
1786 c = create_most_c_obj(channel_name, &inst->kobj);
1789 inst->channel[i] = c;
1794 c->keep_mbo = false;
1795 c->enqueue_halt = false;
1796 c->is_poisoned = false;
1797 c->cfg.direction = 0;
1798 c->cfg.data_type = 0;
1799 c->cfg.num_buffers = 0;
1800 c->cfg.buffer_size = 0;
1801 c->cfg.subbuffer_size = 0;
1802 c->cfg.packets_per_xact = 0;
1803 spin_lock_init(&c->fifo_lock);
1804 INIT_LIST_HEAD(&c->fifo);
1805 INIT_LIST_HEAD(&c->trash_fifo);
1806 INIT_LIST_HEAD(&c->halt_fifo);
1807 init_completion(&c->cleanup);
1808 atomic_set(&c->mbo_ref, 0);
1809 mutex_init(&c->start_mutex);
1810 mutex_init(&c->stop_task_mutex);
1811 list_add_tail(&c->list, &inst->channel_list);
1813 pr_info("registered new MOST device mdev%d (%s)\n",
1814 inst->dev_id, iface->description);
1818 pr_info("Failed allocate channel(s)\n");
1819 list_del(&inst->list);
1820 destroy_most_inst_obj(inst);
1821 return ERR_PTR(-ENOMEM);
1823 EXPORT_SYMBOL_GPL(most_register_interface);
1826 * most_deregister_interface - deregisters an interface with core
1827 * @iface: pointer to the interface instance description.
1829 * Before removing an interface instance from the list, all running
1830 * channels are stopped and poisoned.
1832 void most_deregister_interface(struct most_interface *iface)
1834 struct most_inst_obj *i = iface->priv;
1835 struct most_c_obj *c;
1837 mutex_lock(&deregister_mutex);
1839 pr_info("Bad Interface\n");
1840 mutex_unlock(&deregister_mutex);
1843 pr_info("deregistering MOST device %s (%s)\n", i->kobj.name,
1844 iface->description);
1846 atomic_set(&i->tainted, 1);
1847 mutex_unlock(&deregister_mutex);
1850 if (iface->mod && modref)
1851 module_put(iface->mod);
1855 list_for_each_entry(c, &i->channel_list, list) {
1856 if (c->aim0.refs + c->aim1.refs <= 0)
1859 mutex_lock(&c->stop_task_mutex);
1860 if (c->hdm_enqueue_task)
1861 kthread_stop(c->hdm_enqueue_task);
1862 c->hdm_enqueue_task = NULL;
1863 mutex_unlock(&c->stop_task_mutex);
1865 if (iface->poison_channel(iface, c->channel_id))
1866 pr_err("Can't poison channel %d\n", c->channel_id);
1868 ida_simple_remove(&mdev_id, i->dev_id);
1870 destroy_most_inst_obj(i);
1872 EXPORT_SYMBOL_GPL(most_deregister_interface);
1875 * most_stop_enqueue - prevents core from enqueueing MBOs
1876 * @iface: pointer to interface
1879 * This is called by an HDM that _cannot_ attend to its duties and
1880 * is imminent to get run over by the core. The core is not going to
1881 * enqueue any further packets unless the flagging HDM calls
1882 * most_resume enqueue().
1884 void most_stop_enqueue(struct most_interface *iface, int id)
1886 struct most_c_obj *c = get_channel_by_iface(iface, id);
1889 c->enqueue_halt = true;
1891 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1894 * most_resume_enqueue - allow core to enqueue MBOs again
1895 * @iface: pointer to interface
1898 * This clears the enqueue halt flag and enqueues all MBOs currently
1899 * sitting in the wait fifo.
1901 void most_resume_enqueue(struct most_interface *iface, int id)
1903 struct most_c_obj *c = get_channel_by_iface(iface, id);
1907 c->enqueue_halt = false;
1909 wake_up_interruptible(&c->hdm_fifo_wq);
1911 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1913 static int __init most_init(void)
1915 pr_info("init()\n");
1916 INIT_LIST_HEAD(&instance_list);
1917 INIT_LIST_HEAD(&aim_list);
1918 mutex_init(&deregister_mutex);
1921 if (bus_register(&most_bus)) {
1922 pr_info("Cannot register most bus\n");
1926 most_class = class_create(THIS_MODULE, "most");
1927 if (IS_ERR(most_class)) {
1928 pr_info("No udev support.\n");
1931 if (driver_register(&mostcore)) {
1932 pr_info("Cannot register core driver\n");
1937 device_create(most_class, NULL, 0, NULL, "mostcore");
1938 if (!class_glue_dir)
1942 kset_create_and_add("aims", NULL, &class_glue_dir->kobj);
1944 goto exit_class_container;
1947 kset_create_and_add("devices", NULL, &class_glue_dir->kobj);
1948 if (!most_inst_kset)
1949 goto exit_driver_kset;
1954 kset_unregister(most_aim_kset);
1955 exit_class_container:
1956 device_destroy(most_class, 0);
1958 driver_unregister(&mostcore);
1960 class_destroy(most_class);
1962 bus_unregister(&most_bus);
1967 static void __exit most_exit(void)
1969 struct most_inst_obj *i, *i_tmp;
1970 struct most_aim_obj *d, *d_tmp;
1972 pr_info("exit core module\n");
1973 list_for_each_entry_safe(d, d_tmp, &aim_list, list) {
1974 destroy_most_aim_obj(d);
1977 list_for_each_entry_safe(i, i_tmp, &instance_list, list) {
1979 destroy_most_inst_obj(i);
1981 kset_unregister(most_inst_kset);
1982 kset_unregister(most_aim_kset);
1983 device_destroy(most_class, 0);
1984 driver_unregister(&mostcore);
1985 class_destroy(most_class);
1986 bus_unregister(&most_bus);
1987 ida_destroy(&mdev_id);
1990 module_init(most_init);
1991 module_exit(most_exit);
1992 MODULE_LICENSE("GPL");
1993 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1994 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");