0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/fs.h>
0010 #include <linux/slab.h>
0011 #include <linux/init.h>
0012 #include <linux/device.h>
0013 #include <linux/list.h>
0014 #include <linux/poll.h>
0015 #include <linux/wait.h>
0016 #include <linux/kobject.h>
0017 #include <linux/mutex.h>
0018 #include <linux/completion.h>
0019 #include <linux/sysfs.h>
0020 #include <linux/kthread.h>
0021 #include <linux/dma-mapping.h>
0022 #include <linux/idr.h>
0023 #include <linux/most.h>
0024
0025 #define MAX_CHANNELS 64
0026 #define STRING_SIZE 80
0027
0028 static struct ida mdev_id;
0029 static int dummy_num_buffers;
0030 static struct list_head comp_list;
0031
0032 struct pipe {
0033 struct most_component *comp;
0034 int refs;
0035 int num_buffers;
0036 };
0037
0038 struct most_channel {
0039 struct device dev;
0040 struct completion cleanup;
0041 atomic_t mbo_ref;
0042 atomic_t mbo_nq_level;
0043 u16 channel_id;
0044 char name[STRING_SIZE];
0045 bool is_poisoned;
0046 struct mutex start_mutex;
0047 struct mutex nq_mutex;
0048 int is_starving;
0049 struct most_interface *iface;
0050 struct most_channel_config cfg;
0051 bool keep_mbo;
0052 bool enqueue_halt;
0053 struct list_head fifo;
0054 spinlock_t fifo_lock;
0055 struct list_head halt_fifo;
0056 struct list_head list;
0057 struct pipe pipe0;
0058 struct pipe pipe1;
0059 struct list_head trash_fifo;
0060 struct task_struct *hdm_enqueue_task;
0061 wait_queue_head_t hdm_fifo_wq;
0062
0063 };
0064
0065 #define to_channel(d) container_of(d, struct most_channel, dev)
0066
0067 struct interface_private {
0068 int dev_id;
0069 char name[STRING_SIZE];
0070 struct most_channel *channel[MAX_CHANNELS];
0071 struct list_head channel_list;
0072 };
0073
0074 static const struct {
0075 int most_ch_data_type;
0076 const char *name;
0077 } ch_data_type[] = {
0078 { MOST_CH_CONTROL, "control" },
0079 { MOST_CH_ASYNC, "async" },
0080 { MOST_CH_SYNC, "sync" },
0081 { MOST_CH_ISOC, "isoc"},
0082 { MOST_CH_ISOC, "isoc_avp"},
0083 };
0084
0085
0086
0087
0088
0089 #define list_pop_mbo(ptr) \
0090 ({ \
0091 struct mbo *_mbo = list_first_entry(ptr, struct mbo, list); \
0092 list_del(&_mbo->list); \
0093 _mbo; \
0094 })
0095
0096
0097
0098
0099
0100 static void most_free_mbo_coherent(struct mbo *mbo)
0101 {
0102 struct most_channel *c = mbo->context;
0103 u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
0104
0105 if (c->iface->dma_free)
0106 c->iface->dma_free(mbo, coherent_buf_size);
0107 else
0108 kfree(mbo->virt_address);
0109 kfree(mbo);
0110 if (atomic_sub_and_test(1, &c->mbo_ref))
0111 complete(&c->cleanup);
0112 }
0113
0114
0115
0116
0117
0118 static void flush_channel_fifos(struct most_channel *c)
0119 {
0120 unsigned long flags, hf_flags;
0121 struct mbo *mbo, *tmp;
0122
0123 if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
0124 return;
0125
0126 spin_lock_irqsave(&c->fifo_lock, flags);
0127 list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
0128 list_del(&mbo->list);
0129 spin_unlock_irqrestore(&c->fifo_lock, flags);
0130 most_free_mbo_coherent(mbo);
0131 spin_lock_irqsave(&c->fifo_lock, flags);
0132 }
0133 spin_unlock_irqrestore(&c->fifo_lock, flags);
0134
0135 spin_lock_irqsave(&c->fifo_lock, hf_flags);
0136 list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
0137 list_del(&mbo->list);
0138 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
0139 most_free_mbo_coherent(mbo);
0140 spin_lock_irqsave(&c->fifo_lock, hf_flags);
0141 }
0142 spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
0143
0144 if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
0145 dev_warn(&c->dev, "Channel or trash fifo not empty\n");
0146 }
0147
0148
0149
0150
0151
0152 static int flush_trash_fifo(struct most_channel *c)
0153 {
0154 struct mbo *mbo, *tmp;
0155 unsigned long flags;
0156
0157 spin_lock_irqsave(&c->fifo_lock, flags);
0158 list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
0159 list_del(&mbo->list);
0160 spin_unlock_irqrestore(&c->fifo_lock, flags);
0161 most_free_mbo_coherent(mbo);
0162 spin_lock_irqsave(&c->fifo_lock, flags);
0163 }
0164 spin_unlock_irqrestore(&c->fifo_lock, flags);
0165 return 0;
0166 }
0167
0168 static ssize_t available_directions_show(struct device *dev,
0169 struct device_attribute *attr,
0170 char *buf)
0171 {
0172 struct most_channel *c = to_channel(dev);
0173 unsigned int i = c->channel_id;
0174
0175 strcpy(buf, "");
0176 if (c->iface->channel_vector[i].direction & MOST_CH_RX)
0177 strcat(buf, "rx ");
0178 if (c->iface->channel_vector[i].direction & MOST_CH_TX)
0179 strcat(buf, "tx ");
0180 strcat(buf, "\n");
0181 return strlen(buf);
0182 }
0183
0184 static ssize_t available_datatypes_show(struct device *dev,
0185 struct device_attribute *attr,
0186 char *buf)
0187 {
0188 struct most_channel *c = to_channel(dev);
0189 unsigned int i = c->channel_id;
0190
0191 strcpy(buf, "");
0192 if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
0193 strcat(buf, "control ");
0194 if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
0195 strcat(buf, "async ");
0196 if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
0197 strcat(buf, "sync ");
0198 if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
0199 strcat(buf, "isoc ");
0200 strcat(buf, "\n");
0201 return strlen(buf);
0202 }
0203
0204 static ssize_t number_of_packet_buffers_show(struct device *dev,
0205 struct device_attribute *attr,
0206 char *buf)
0207 {
0208 struct most_channel *c = to_channel(dev);
0209 unsigned int i = c->channel_id;
0210
0211 return snprintf(buf, PAGE_SIZE, "%d\n",
0212 c->iface->channel_vector[i].num_buffers_packet);
0213 }
0214
0215 static ssize_t number_of_stream_buffers_show(struct device *dev,
0216 struct device_attribute *attr,
0217 char *buf)
0218 {
0219 struct most_channel *c = to_channel(dev);
0220 unsigned int i = c->channel_id;
0221
0222 return snprintf(buf, PAGE_SIZE, "%d\n",
0223 c->iface->channel_vector[i].num_buffers_streaming);
0224 }
0225
0226 static ssize_t size_of_packet_buffer_show(struct device *dev,
0227 struct device_attribute *attr,
0228 char *buf)
0229 {
0230 struct most_channel *c = to_channel(dev);
0231 unsigned int i = c->channel_id;
0232
0233 return snprintf(buf, PAGE_SIZE, "%d\n",
0234 c->iface->channel_vector[i].buffer_size_packet);
0235 }
0236
0237 static ssize_t size_of_stream_buffer_show(struct device *dev,
0238 struct device_attribute *attr,
0239 char *buf)
0240 {
0241 struct most_channel *c = to_channel(dev);
0242 unsigned int i = c->channel_id;
0243
0244 return snprintf(buf, PAGE_SIZE, "%d\n",
0245 c->iface->channel_vector[i].buffer_size_streaming);
0246 }
0247
0248 static ssize_t channel_starving_show(struct device *dev,
0249 struct device_attribute *attr,
0250 char *buf)
0251 {
0252 struct most_channel *c = to_channel(dev);
0253
0254 return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
0255 }
0256
0257 static ssize_t set_number_of_buffers_show(struct device *dev,
0258 struct device_attribute *attr,
0259 char *buf)
0260 {
0261 struct most_channel *c = to_channel(dev);
0262
0263 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
0264 }
0265
0266 static ssize_t set_buffer_size_show(struct device *dev,
0267 struct device_attribute *attr,
0268 char *buf)
0269 {
0270 struct most_channel *c = to_channel(dev);
0271
0272 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
0273 }
0274
0275 static ssize_t set_direction_show(struct device *dev,
0276 struct device_attribute *attr,
0277 char *buf)
0278 {
0279 struct most_channel *c = to_channel(dev);
0280
0281 if (c->cfg.direction & MOST_CH_TX)
0282 return snprintf(buf, PAGE_SIZE, "tx\n");
0283 else if (c->cfg.direction & MOST_CH_RX)
0284 return snprintf(buf, PAGE_SIZE, "rx\n");
0285 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
0286 }
0287
0288 static ssize_t set_datatype_show(struct device *dev,
0289 struct device_attribute *attr,
0290 char *buf)
0291 {
0292 int i;
0293 struct most_channel *c = to_channel(dev);
0294
0295 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
0296 if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
0297 return snprintf(buf, PAGE_SIZE, "%s",
0298 ch_data_type[i].name);
0299 }
0300 return snprintf(buf, PAGE_SIZE, "unconfigured\n");
0301 }
0302
0303 static ssize_t set_subbuffer_size_show(struct device *dev,
0304 struct device_attribute *attr,
0305 char *buf)
0306 {
0307 struct most_channel *c = to_channel(dev);
0308
0309 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
0310 }
0311
0312 static ssize_t set_packets_per_xact_show(struct device *dev,
0313 struct device_attribute *attr,
0314 char *buf)
0315 {
0316 struct most_channel *c = to_channel(dev);
0317
0318 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
0319 }
0320
0321 static ssize_t set_dbr_size_show(struct device *dev,
0322 struct device_attribute *attr, char *buf)
0323 {
0324 struct most_channel *c = to_channel(dev);
0325
0326 return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.dbr_size);
0327 }
0328
0329 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
0330 static umode_t channel_attr_is_visible(struct kobject *kobj,
0331 struct attribute *attr, int index)
0332 {
0333 struct device_attribute *dev_attr = to_dev_attr(attr);
0334 struct device *dev = kobj_to_dev(kobj);
0335 struct most_channel *c = to_channel(dev);
0336
0337 if (!strcmp(dev_attr->attr.name, "set_dbr_size") &&
0338 (c->iface->interface != ITYPE_MEDIALB_DIM2))
0339 return 0;
0340 if (!strcmp(dev_attr->attr.name, "set_packets_per_xact") &&
0341 (c->iface->interface != ITYPE_USB))
0342 return 0;
0343
0344 return attr->mode;
0345 }
0346
0347 #define DEV_ATTR(_name) (&dev_attr_##_name.attr)
0348
0349 static DEVICE_ATTR_RO(available_directions);
0350 static DEVICE_ATTR_RO(available_datatypes);
0351 static DEVICE_ATTR_RO(number_of_packet_buffers);
0352 static DEVICE_ATTR_RO(number_of_stream_buffers);
0353 static DEVICE_ATTR_RO(size_of_stream_buffer);
0354 static DEVICE_ATTR_RO(size_of_packet_buffer);
0355 static DEVICE_ATTR_RO(channel_starving);
0356 static DEVICE_ATTR_RO(set_buffer_size);
0357 static DEVICE_ATTR_RO(set_number_of_buffers);
0358 static DEVICE_ATTR_RO(set_direction);
0359 static DEVICE_ATTR_RO(set_datatype);
0360 static DEVICE_ATTR_RO(set_subbuffer_size);
0361 static DEVICE_ATTR_RO(set_packets_per_xact);
0362 static DEVICE_ATTR_RO(set_dbr_size);
0363
0364 static struct attribute *channel_attrs[] = {
0365 DEV_ATTR(available_directions),
0366 DEV_ATTR(available_datatypes),
0367 DEV_ATTR(number_of_packet_buffers),
0368 DEV_ATTR(number_of_stream_buffers),
0369 DEV_ATTR(size_of_stream_buffer),
0370 DEV_ATTR(size_of_packet_buffer),
0371 DEV_ATTR(channel_starving),
0372 DEV_ATTR(set_buffer_size),
0373 DEV_ATTR(set_number_of_buffers),
0374 DEV_ATTR(set_direction),
0375 DEV_ATTR(set_datatype),
0376 DEV_ATTR(set_subbuffer_size),
0377 DEV_ATTR(set_packets_per_xact),
0378 DEV_ATTR(set_dbr_size),
0379 NULL,
0380 };
0381
0382 static const struct attribute_group channel_attr_group = {
0383 .attrs = channel_attrs,
0384 .is_visible = channel_attr_is_visible,
0385 };
0386
0387 static const struct attribute_group *channel_attr_groups[] = {
0388 &channel_attr_group,
0389 NULL,
0390 };
0391
0392 static ssize_t description_show(struct device *dev,
0393 struct device_attribute *attr,
0394 char *buf)
0395 {
0396 struct most_interface *iface = dev_get_drvdata(dev);
0397
0398 return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
0399 }
0400
0401 static ssize_t interface_show(struct device *dev,
0402 struct device_attribute *attr,
0403 char *buf)
0404 {
0405 struct most_interface *iface = dev_get_drvdata(dev);
0406
0407 switch (iface->interface) {
0408 case ITYPE_LOOPBACK:
0409 return snprintf(buf, PAGE_SIZE, "loopback\n");
0410 case ITYPE_I2C:
0411 return snprintf(buf, PAGE_SIZE, "i2c\n");
0412 case ITYPE_I2S:
0413 return snprintf(buf, PAGE_SIZE, "i2s\n");
0414 case ITYPE_TSI:
0415 return snprintf(buf, PAGE_SIZE, "tsi\n");
0416 case ITYPE_HBI:
0417 return snprintf(buf, PAGE_SIZE, "hbi\n");
0418 case ITYPE_MEDIALB_DIM:
0419 return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
0420 case ITYPE_MEDIALB_DIM2:
0421 return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
0422 case ITYPE_USB:
0423 return snprintf(buf, PAGE_SIZE, "usb\n");
0424 case ITYPE_PCIE:
0425 return snprintf(buf, PAGE_SIZE, "pcie\n");
0426 }
0427 return snprintf(buf, PAGE_SIZE, "unknown\n");
0428 }
0429
0430 static DEVICE_ATTR_RO(description);
0431 static DEVICE_ATTR_RO(interface);
0432
0433 static struct attribute *interface_attrs[] = {
0434 DEV_ATTR(description),
0435 DEV_ATTR(interface),
0436 NULL,
0437 };
0438
0439 static const struct attribute_group interface_attr_group = {
0440 .attrs = interface_attrs,
0441 };
0442
0443 static const struct attribute_group *interface_attr_groups[] = {
0444 &interface_attr_group,
0445 NULL,
0446 };
0447
0448 static struct most_component *match_component(char *name)
0449 {
0450 struct most_component *comp;
0451
0452 list_for_each_entry(comp, &comp_list, list) {
0453 if (!strcmp(comp->name, name))
0454 return comp;
0455 }
0456 return NULL;
0457 }
0458
0459 struct show_links_data {
0460 int offs;
0461 char *buf;
0462 };
0463
0464 static int print_links(struct device *dev, void *data)
0465 {
0466 struct show_links_data *d = data;
0467 int offs = d->offs;
0468 char *buf = d->buf;
0469 struct most_channel *c;
0470 struct most_interface *iface = dev_get_drvdata(dev);
0471
0472 list_for_each_entry(c, &iface->p->channel_list, list) {
0473 if (c->pipe0.comp) {
0474 offs += scnprintf(buf + offs,
0475 PAGE_SIZE - offs,
0476 "%s:%s:%s\n",
0477 c->pipe0.comp->name,
0478 dev_name(iface->dev),
0479 dev_name(&c->dev));
0480 }
0481 if (c->pipe1.comp) {
0482 offs += scnprintf(buf + offs,
0483 PAGE_SIZE - offs,
0484 "%s:%s:%s\n",
0485 c->pipe1.comp->name,
0486 dev_name(iface->dev),
0487 dev_name(&c->dev));
0488 }
0489 }
0490 d->offs = offs;
0491 return 0;
0492 }
0493
0494 static int most_match(struct device *dev, struct device_driver *drv)
0495 {
0496 if (!strcmp(dev_name(dev), "most"))
0497 return 0;
0498 else
0499 return 1;
0500 }
0501
0502 static struct bus_type mostbus = {
0503 .name = "most",
0504 .match = most_match,
0505 };
0506
0507 static ssize_t links_show(struct device_driver *drv, char *buf)
0508 {
0509 struct show_links_data d = { .buf = buf };
0510
0511 bus_for_each_dev(&mostbus, NULL, &d, print_links);
0512 return d.offs;
0513 }
0514
0515 static ssize_t components_show(struct device_driver *drv, char *buf)
0516 {
0517 struct most_component *comp;
0518 int offs = 0;
0519
0520 list_for_each_entry(comp, &comp_list, list) {
0521 offs += scnprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
0522 comp->name);
0523 }
0524 return offs;
0525 }
0526
0527
0528
0529
0530
0531
0532 static struct most_channel *get_channel(char *mdev, char *mdev_ch)
0533 {
0534 struct device *dev = NULL;
0535 struct most_interface *iface;
0536 struct most_channel *c, *tmp;
0537
0538 dev = bus_find_device_by_name(&mostbus, NULL, mdev);
0539 if (!dev)
0540 return NULL;
0541 put_device(dev);
0542 iface = dev_get_drvdata(dev);
0543 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
0544 if (!strcmp(dev_name(&c->dev), mdev_ch))
0545 return c;
0546 }
0547 return NULL;
0548 }
0549
0550 static
0551 inline int link_channel_to_component(struct most_channel *c,
0552 struct most_component *comp,
0553 char *name,
0554 char *comp_param)
0555 {
0556 int ret;
0557 struct most_component **comp_ptr;
0558
0559 if (!c->pipe0.comp)
0560 comp_ptr = &c->pipe0.comp;
0561 else if (!c->pipe1.comp)
0562 comp_ptr = &c->pipe1.comp;
0563 else
0564 return -ENOSPC;
0565
0566 *comp_ptr = comp;
0567 ret = comp->probe_channel(c->iface, c->channel_id, &c->cfg, name,
0568 comp_param);
0569 if (ret) {
0570 *comp_ptr = NULL;
0571 return ret;
0572 }
0573 return 0;
0574 }
0575
0576 int most_set_cfg_buffer_size(char *mdev, char *mdev_ch, u16 val)
0577 {
0578 struct most_channel *c = get_channel(mdev, mdev_ch);
0579
0580 if (!c)
0581 return -ENODEV;
0582 c->cfg.buffer_size = val;
0583 return 0;
0584 }
0585
0586 int most_set_cfg_subbuffer_size(char *mdev, char *mdev_ch, u16 val)
0587 {
0588 struct most_channel *c = get_channel(mdev, mdev_ch);
0589
0590 if (!c)
0591 return -ENODEV;
0592 c->cfg.subbuffer_size = val;
0593 return 0;
0594 }
0595
0596 int most_set_cfg_dbr_size(char *mdev, char *mdev_ch, u16 val)
0597 {
0598 struct most_channel *c = get_channel(mdev, mdev_ch);
0599
0600 if (!c)
0601 return -ENODEV;
0602 c->cfg.dbr_size = val;
0603 return 0;
0604 }
0605
0606 int most_set_cfg_num_buffers(char *mdev, char *mdev_ch, u16 val)
0607 {
0608 struct most_channel *c = get_channel(mdev, mdev_ch);
0609
0610 if (!c)
0611 return -ENODEV;
0612 c->cfg.num_buffers = val;
0613 return 0;
0614 }
0615
0616 int most_set_cfg_datatype(char *mdev, char *mdev_ch, char *buf)
0617 {
0618 int i;
0619 struct most_channel *c = get_channel(mdev, mdev_ch);
0620
0621 if (!c)
0622 return -ENODEV;
0623 for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
0624 if (!strcmp(buf, ch_data_type[i].name)) {
0625 c->cfg.data_type = ch_data_type[i].most_ch_data_type;
0626 break;
0627 }
0628 }
0629
0630 if (i == ARRAY_SIZE(ch_data_type))
0631 dev_warn(&c->dev, "Invalid attribute settings\n");
0632 return 0;
0633 }
0634
0635 int most_set_cfg_direction(char *mdev, char *mdev_ch, char *buf)
0636 {
0637 struct most_channel *c = get_channel(mdev, mdev_ch);
0638
0639 if (!c)
0640 return -ENODEV;
0641 if (!strcmp(buf, "dir_rx")) {
0642 c->cfg.direction = MOST_CH_RX;
0643 } else if (!strcmp(buf, "rx")) {
0644 c->cfg.direction = MOST_CH_RX;
0645 } else if (!strcmp(buf, "dir_tx")) {
0646 c->cfg.direction = MOST_CH_TX;
0647 } else if (!strcmp(buf, "tx")) {
0648 c->cfg.direction = MOST_CH_TX;
0649 } else {
0650 dev_err(&c->dev, "Invalid direction\n");
0651 return -ENODATA;
0652 }
0653 return 0;
0654 }
0655
0656 int most_set_cfg_packets_xact(char *mdev, char *mdev_ch, u16 val)
0657 {
0658 struct most_channel *c = get_channel(mdev, mdev_ch);
0659
0660 if (!c)
0661 return -ENODEV;
0662 c->cfg.packets_per_xact = val;
0663 return 0;
0664 }
0665
0666 int most_cfg_complete(char *comp_name)
0667 {
0668 struct most_component *comp;
0669
0670 comp = match_component(comp_name);
0671 if (!comp)
0672 return -ENODEV;
0673
0674 return comp->cfg_complete();
0675 }
0676
0677 int most_add_link(char *mdev, char *mdev_ch, char *comp_name, char *link_name,
0678 char *comp_param)
0679 {
0680 struct most_channel *c = get_channel(mdev, mdev_ch);
0681 struct most_component *comp = match_component(comp_name);
0682
0683 if (!c || !comp)
0684 return -ENODEV;
0685
0686 return link_channel_to_component(c, comp, link_name, comp_param);
0687 }
0688
0689 int most_remove_link(char *mdev, char *mdev_ch, char *comp_name)
0690 {
0691 struct most_channel *c;
0692 struct most_component *comp;
0693
0694 comp = match_component(comp_name);
0695 if (!comp)
0696 return -ENODEV;
0697 c = get_channel(mdev, mdev_ch);
0698 if (!c)
0699 return -ENODEV;
0700
0701 if (comp->disconnect_channel(c->iface, c->channel_id))
0702 return -EIO;
0703 if (c->pipe0.comp == comp)
0704 c->pipe0.comp = NULL;
0705 if (c->pipe1.comp == comp)
0706 c->pipe1.comp = NULL;
0707 return 0;
0708 }
0709
0710 #define DRV_ATTR(_name) (&driver_attr_##_name.attr)
0711
0712 static DRIVER_ATTR_RO(links);
0713 static DRIVER_ATTR_RO(components);
0714
0715 static struct attribute *mc_attrs[] = {
0716 DRV_ATTR(links),
0717 DRV_ATTR(components),
0718 NULL,
0719 };
0720
0721 static const struct attribute_group mc_attr_group = {
0722 .attrs = mc_attrs,
0723 };
0724
0725 static const struct attribute_group *mc_attr_groups[] = {
0726 &mc_attr_group,
0727 NULL,
0728 };
0729
0730 static struct device_driver mostbus_driver = {
0731 .name = "most_core",
0732 .bus = &mostbus,
0733 .groups = mc_attr_groups,
0734 };
0735
0736 static inline void trash_mbo(struct mbo *mbo)
0737 {
0738 unsigned long flags;
0739 struct most_channel *c = mbo->context;
0740
0741 spin_lock_irqsave(&c->fifo_lock, flags);
0742 list_add(&mbo->list, &c->trash_fifo);
0743 spin_unlock_irqrestore(&c->fifo_lock, flags);
0744 }
0745
0746 static bool hdm_mbo_ready(struct most_channel *c)
0747 {
0748 bool empty;
0749
0750 if (c->enqueue_halt)
0751 return false;
0752
0753 spin_lock_irq(&c->fifo_lock);
0754 empty = list_empty(&c->halt_fifo);
0755 spin_unlock_irq(&c->fifo_lock);
0756
0757 return !empty;
0758 }
0759
0760 static void nq_hdm_mbo(struct mbo *mbo)
0761 {
0762 unsigned long flags;
0763 struct most_channel *c = mbo->context;
0764
0765 spin_lock_irqsave(&c->fifo_lock, flags);
0766 list_add_tail(&mbo->list, &c->halt_fifo);
0767 spin_unlock_irqrestore(&c->fifo_lock, flags);
0768 wake_up_interruptible(&c->hdm_fifo_wq);
0769 }
0770
0771 static int hdm_enqueue_thread(void *data)
0772 {
0773 struct most_channel *c = data;
0774 struct mbo *mbo;
0775 int ret;
0776 typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
0777
0778 while (likely(!kthread_should_stop())) {
0779 wait_event_interruptible(c->hdm_fifo_wq,
0780 hdm_mbo_ready(c) ||
0781 kthread_should_stop());
0782
0783 mutex_lock(&c->nq_mutex);
0784 spin_lock_irq(&c->fifo_lock);
0785 if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
0786 spin_unlock_irq(&c->fifo_lock);
0787 mutex_unlock(&c->nq_mutex);
0788 continue;
0789 }
0790
0791 mbo = list_pop_mbo(&c->halt_fifo);
0792 spin_unlock_irq(&c->fifo_lock);
0793
0794 if (c->cfg.direction == MOST_CH_RX)
0795 mbo->buffer_length = c->cfg.buffer_size;
0796
0797 ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
0798 mutex_unlock(&c->nq_mutex);
0799
0800 if (unlikely(ret)) {
0801 dev_err(&c->dev, "Buffer enqueue failed\n");
0802 nq_hdm_mbo(mbo);
0803 c->hdm_enqueue_task = NULL;
0804 return 0;
0805 }
0806 }
0807
0808 return 0;
0809 }
0810
0811 static int run_enqueue_thread(struct most_channel *c, int channel_id)
0812 {
0813 struct task_struct *task =
0814 kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
0815 channel_id);
0816
0817 if (IS_ERR(task))
0818 return PTR_ERR(task);
0819
0820 c->hdm_enqueue_task = task;
0821 return 0;
0822 }
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835 static void arm_mbo(struct mbo *mbo)
0836 {
0837 unsigned long flags;
0838 struct most_channel *c;
0839
0840 c = mbo->context;
0841
0842 if (c->is_poisoned) {
0843 trash_mbo(mbo);
0844 return;
0845 }
0846
0847 spin_lock_irqsave(&c->fifo_lock, flags);
0848 ++*mbo->num_buffers_ptr;
0849 list_add_tail(&mbo->list, &c->fifo);
0850 spin_unlock_irqrestore(&c->fifo_lock, flags);
0851
0852 if (c->pipe0.refs && c->pipe0.comp->tx_completion)
0853 c->pipe0.comp->tx_completion(c->iface, c->channel_id);
0854
0855 if (c->pipe1.refs && c->pipe1.comp->tx_completion)
0856 c->pipe1.comp->tx_completion(c->iface, c->channel_id);
0857 }
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872 static int arm_mbo_chain(struct most_channel *c, int dir,
0873 void (*compl)(struct mbo *))
0874 {
0875 unsigned int i;
0876 struct mbo *mbo;
0877 unsigned long flags;
0878 u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
0879
0880 atomic_set(&c->mbo_nq_level, 0);
0881
0882 for (i = 0; i < c->cfg.num_buffers; i++) {
0883 mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
0884 if (!mbo)
0885 goto flush_fifos;
0886
0887 mbo->context = c;
0888 mbo->ifp = c->iface;
0889 mbo->hdm_channel_id = c->channel_id;
0890 if (c->iface->dma_alloc) {
0891 mbo->virt_address =
0892 c->iface->dma_alloc(mbo, coherent_buf_size);
0893 } else {
0894 mbo->virt_address =
0895 kzalloc(coherent_buf_size, GFP_KERNEL);
0896 }
0897 if (!mbo->virt_address)
0898 goto release_mbo;
0899
0900 mbo->complete = compl;
0901 mbo->num_buffers_ptr = &dummy_num_buffers;
0902 if (dir == MOST_CH_RX) {
0903 nq_hdm_mbo(mbo);
0904 atomic_inc(&c->mbo_nq_level);
0905 } else {
0906 spin_lock_irqsave(&c->fifo_lock, flags);
0907 list_add_tail(&mbo->list, &c->fifo);
0908 spin_unlock_irqrestore(&c->fifo_lock, flags);
0909 }
0910 }
0911 return c->cfg.num_buffers;
0912
0913 release_mbo:
0914 kfree(mbo);
0915
0916 flush_fifos:
0917 flush_channel_fifos(c);
0918 return 0;
0919 }
0920
0921
0922
0923
0924
0925 void most_submit_mbo(struct mbo *mbo)
0926 {
0927 if (WARN_ONCE(!mbo || !mbo->context,
0928 "Bad buffer or missing channel reference\n"))
0929 return;
0930
0931 nq_hdm_mbo(mbo);
0932 }
0933 EXPORT_SYMBOL_GPL(most_submit_mbo);
0934
0935
0936
0937
0938
0939
0940
0941
0942 static void most_write_completion(struct mbo *mbo)
0943 {
0944 struct most_channel *c;
0945
0946 c = mbo->context;
0947 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
0948 trash_mbo(mbo);
0949 else
0950 arm_mbo(mbo);
0951 }
0952
0953 int channel_has_mbo(struct most_interface *iface, int id,
0954 struct most_component *comp)
0955 {
0956 struct most_channel *c = iface->p->channel[id];
0957 unsigned long flags;
0958 int empty;
0959
0960 if (unlikely(!c))
0961 return -EINVAL;
0962
0963 if (c->pipe0.refs && c->pipe1.refs &&
0964 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
0965 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
0966 return 0;
0967
0968 spin_lock_irqsave(&c->fifo_lock, flags);
0969 empty = list_empty(&c->fifo);
0970 spin_unlock_irqrestore(&c->fifo_lock, flags);
0971 return !empty;
0972 }
0973 EXPORT_SYMBOL_GPL(channel_has_mbo);
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984 struct mbo *most_get_mbo(struct most_interface *iface, int id,
0985 struct most_component *comp)
0986 {
0987 struct mbo *mbo;
0988 struct most_channel *c;
0989 unsigned long flags;
0990 int *num_buffers_ptr;
0991
0992 c = iface->p->channel[id];
0993 if (unlikely(!c))
0994 return NULL;
0995
0996 if (c->pipe0.refs && c->pipe1.refs &&
0997 ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
0998 (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
0999 return NULL;
1000
1001 if (comp == c->pipe0.comp)
1002 num_buffers_ptr = &c->pipe0.num_buffers;
1003 else if (comp == c->pipe1.comp)
1004 num_buffers_ptr = &c->pipe1.num_buffers;
1005 else
1006 num_buffers_ptr = &dummy_num_buffers;
1007
1008 spin_lock_irqsave(&c->fifo_lock, flags);
1009 if (list_empty(&c->fifo)) {
1010 spin_unlock_irqrestore(&c->fifo_lock, flags);
1011 return NULL;
1012 }
1013 mbo = list_pop_mbo(&c->fifo);
1014 --*num_buffers_ptr;
1015 spin_unlock_irqrestore(&c->fifo_lock, flags);
1016
1017 mbo->num_buffers_ptr = num_buffers_ptr;
1018 mbo->buffer_length = c->cfg.buffer_size;
1019 return mbo;
1020 }
1021 EXPORT_SYMBOL_GPL(most_get_mbo);
1022
1023
1024
1025
1026
1027 void most_put_mbo(struct mbo *mbo)
1028 {
1029 struct most_channel *c = mbo->context;
1030
1031 if (c->cfg.direction == MOST_CH_TX) {
1032 arm_mbo(mbo);
1033 return;
1034 }
1035 nq_hdm_mbo(mbo);
1036 atomic_inc(&c->mbo_nq_level);
1037 }
1038 EXPORT_SYMBOL_GPL(most_put_mbo);
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050 static void most_read_completion(struct mbo *mbo)
1051 {
1052 struct most_channel *c = mbo->context;
1053
1054 if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
1055 trash_mbo(mbo);
1056 return;
1057 }
1058
1059 if (mbo->status == MBO_E_INVAL) {
1060 nq_hdm_mbo(mbo);
1061 atomic_inc(&c->mbo_nq_level);
1062 return;
1063 }
1064
1065 if (atomic_sub_and_test(1, &c->mbo_nq_level))
1066 c->is_starving = 1;
1067
1068 if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
1069 c->pipe0.comp->rx_completion(mbo) == 0)
1070 return;
1071
1072 if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
1073 c->pipe1.comp->rx_completion(mbo) == 0)
1074 return;
1075
1076 most_put_mbo(mbo);
1077 }
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090 int most_start_channel(struct most_interface *iface, int id,
1091 struct most_component *comp)
1092 {
1093 int num_buffer;
1094 int ret;
1095 struct most_channel *c = iface->p->channel[id];
1096
1097 if (unlikely(!c))
1098 return -EINVAL;
1099
1100 mutex_lock(&c->start_mutex);
1101 if (c->pipe0.refs + c->pipe1.refs > 0)
1102 goto out;
1103
1104 if (!try_module_get(iface->mod)) {
1105 dev_err(&c->dev, "Failed to acquire HDM lock\n");
1106 mutex_unlock(&c->start_mutex);
1107 return -ENOLCK;
1108 }
1109
1110 c->cfg.extra_len = 0;
1111 if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
1112 dev_err(&c->dev, "Channel configuration failed. Go check settings...\n");
1113 ret = -EINVAL;
1114 goto err_put_module;
1115 }
1116
1117 init_waitqueue_head(&c->hdm_fifo_wq);
1118
1119 if (c->cfg.direction == MOST_CH_RX)
1120 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1121 most_read_completion);
1122 else
1123 num_buffer = arm_mbo_chain(c, c->cfg.direction,
1124 most_write_completion);
1125 if (unlikely(!num_buffer)) {
1126 ret = -ENOMEM;
1127 goto err_put_module;
1128 }
1129
1130 ret = run_enqueue_thread(c, id);
1131 if (ret)
1132 goto err_put_module;
1133
1134 c->is_starving = 0;
1135 c->pipe0.num_buffers = c->cfg.num_buffers / 2;
1136 c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
1137 atomic_set(&c->mbo_ref, num_buffer);
1138
1139 out:
1140 if (comp == c->pipe0.comp)
1141 c->pipe0.refs++;
1142 if (comp == c->pipe1.comp)
1143 c->pipe1.refs++;
1144 mutex_unlock(&c->start_mutex);
1145 return 0;
1146
1147 err_put_module:
1148 module_put(iface->mod);
1149 mutex_unlock(&c->start_mutex);
1150 return ret;
1151 }
1152 EXPORT_SYMBOL_GPL(most_start_channel);
1153
1154
1155
1156
1157
1158
1159
1160 int most_stop_channel(struct most_interface *iface, int id,
1161 struct most_component *comp)
1162 {
1163 struct most_channel *c;
1164
1165 if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
1166 pr_err("Bad interface or index out of range\n");
1167 return -EINVAL;
1168 }
1169 c = iface->p->channel[id];
1170 if (unlikely(!c))
1171 return -EINVAL;
1172
1173 mutex_lock(&c->start_mutex);
1174 if (c->pipe0.refs + c->pipe1.refs >= 2)
1175 goto out;
1176
1177 if (c->hdm_enqueue_task)
1178 kthread_stop(c->hdm_enqueue_task);
1179 c->hdm_enqueue_task = NULL;
1180
1181 if (iface->mod)
1182 module_put(iface->mod);
1183
1184 c->is_poisoned = true;
1185 if (c->iface->poison_channel(c->iface, c->channel_id)) {
1186 dev_err(&c->dev, "Failed to stop channel %d of interface %s\n", c->channel_id,
1187 c->iface->description);
1188 mutex_unlock(&c->start_mutex);
1189 return -EAGAIN;
1190 }
1191 flush_trash_fifo(c);
1192 flush_channel_fifos(c);
1193
1194 #ifdef CMPL_INTERRUPTIBLE
1195 if (wait_for_completion_interruptible(&c->cleanup)) {
1196 dev_err(&c->dev, "Interrupted while cleaning up channel %d\n", c->channel_id);
1197 mutex_unlock(&c->start_mutex);
1198 return -EINTR;
1199 }
1200 #else
1201 wait_for_completion(&c->cleanup);
1202 #endif
1203 c->is_poisoned = false;
1204
1205 out:
1206 if (comp == c->pipe0.comp)
1207 c->pipe0.refs--;
1208 if (comp == c->pipe1.comp)
1209 c->pipe1.refs--;
1210 mutex_unlock(&c->start_mutex);
1211 return 0;
1212 }
1213 EXPORT_SYMBOL_GPL(most_stop_channel);
1214
1215
1216
1217
1218
1219 int most_register_component(struct most_component *comp)
1220 {
1221 if (!comp) {
1222 pr_err("Bad component\n");
1223 return -EINVAL;
1224 }
1225 list_add_tail(&comp->list, &comp_list);
1226 return 0;
1227 }
1228 EXPORT_SYMBOL_GPL(most_register_component);
1229
1230 static int disconnect_channels(struct device *dev, void *data)
1231 {
1232 struct most_interface *iface;
1233 struct most_channel *c, *tmp;
1234 struct most_component *comp = data;
1235
1236 iface = dev_get_drvdata(dev);
1237 list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
1238 if (c->pipe0.comp == comp || c->pipe1.comp == comp)
1239 comp->disconnect_channel(c->iface, c->channel_id);
1240 if (c->pipe0.comp == comp)
1241 c->pipe0.comp = NULL;
1242 if (c->pipe1.comp == comp)
1243 c->pipe1.comp = NULL;
1244 }
1245 return 0;
1246 }
1247
1248
1249
1250
1251
1252 int most_deregister_component(struct most_component *comp)
1253 {
1254 if (!comp) {
1255 pr_err("Bad component\n");
1256 return -EINVAL;
1257 }
1258
1259 bus_for_each_dev(&mostbus, NULL, comp, disconnect_channels);
1260 list_del(&comp->list);
1261 return 0;
1262 }
1263 EXPORT_SYMBOL_GPL(most_deregister_component);
1264
1265 static void release_channel(struct device *dev)
1266 {
1267 struct most_channel *c = to_channel(dev);
1268
1269 kfree(c);
1270 }
1271
1272
1273
1274
1275
1276
1277
1278
1279 int most_register_interface(struct most_interface *iface)
1280 {
1281 unsigned int i;
1282 int id;
1283 struct most_channel *c;
1284
1285 if (!iface || !iface->enqueue || !iface->configure ||
1286 !iface->poison_channel || (iface->num_channels > MAX_CHANNELS))
1287 return -EINVAL;
1288
1289 id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
1290 if (id < 0) {
1291 dev_err(iface->dev, "Failed to allocate device ID\n");
1292 return id;
1293 }
1294
1295 iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
1296 if (!iface->p) {
1297 ida_simple_remove(&mdev_id, id);
1298 return -ENOMEM;
1299 }
1300
1301 INIT_LIST_HEAD(&iface->p->channel_list);
1302 iface->p->dev_id = id;
1303 strscpy(iface->p->name, iface->description, sizeof(iface->p->name));
1304 iface->dev->bus = &mostbus;
1305 iface->dev->groups = interface_attr_groups;
1306 dev_set_drvdata(iface->dev, iface);
1307 if (device_register(iface->dev)) {
1308 dev_err(iface->dev, "Failed to register interface device\n");
1309 kfree(iface->p);
1310 put_device(iface->dev);
1311 ida_simple_remove(&mdev_id, id);
1312 return -ENOMEM;
1313 }
1314
1315 for (i = 0; i < iface->num_channels; i++) {
1316 const char *name_suffix = iface->channel_vector[i].name_suffix;
1317
1318 c = kzalloc(sizeof(*c), GFP_KERNEL);
1319 if (!c)
1320 goto err_free_resources;
1321 if (!name_suffix)
1322 snprintf(c->name, STRING_SIZE, "ch%d", i);
1323 else
1324 snprintf(c->name, STRING_SIZE, "%s", name_suffix);
1325 c->dev.init_name = c->name;
1326 c->dev.parent = iface->dev;
1327 c->dev.groups = channel_attr_groups;
1328 c->dev.release = release_channel;
1329 iface->p->channel[i] = c;
1330 c->is_starving = 0;
1331 c->iface = iface;
1332 c->channel_id = i;
1333 c->keep_mbo = false;
1334 c->enqueue_halt = false;
1335 c->is_poisoned = false;
1336 c->cfg.direction = 0;
1337 c->cfg.data_type = 0;
1338 c->cfg.num_buffers = 0;
1339 c->cfg.buffer_size = 0;
1340 c->cfg.subbuffer_size = 0;
1341 c->cfg.packets_per_xact = 0;
1342 spin_lock_init(&c->fifo_lock);
1343 INIT_LIST_HEAD(&c->fifo);
1344 INIT_LIST_HEAD(&c->trash_fifo);
1345 INIT_LIST_HEAD(&c->halt_fifo);
1346 init_completion(&c->cleanup);
1347 atomic_set(&c->mbo_ref, 0);
1348 mutex_init(&c->start_mutex);
1349 mutex_init(&c->nq_mutex);
1350 list_add_tail(&c->list, &iface->p->channel_list);
1351 if (device_register(&c->dev)) {
1352 dev_err(&c->dev, "Failed to register channel device\n");
1353 goto err_free_most_channel;
1354 }
1355 }
1356 most_interface_register_notify(iface->description);
1357 return 0;
1358
1359 err_free_most_channel:
1360 put_device(&c->dev);
1361
1362 err_free_resources:
1363 while (i > 0) {
1364 c = iface->p->channel[--i];
1365 device_unregister(&c->dev);
1366 }
1367 kfree(iface->p);
1368 device_unregister(iface->dev);
1369 ida_simple_remove(&mdev_id, id);
1370 return -ENOMEM;
1371 }
1372 EXPORT_SYMBOL_GPL(most_register_interface);
1373
1374
1375
1376
1377
1378
1379
1380
1381 void most_deregister_interface(struct most_interface *iface)
1382 {
1383 int i;
1384 struct most_channel *c;
1385
1386 for (i = 0; i < iface->num_channels; i++) {
1387 c = iface->p->channel[i];
1388 if (c->pipe0.comp)
1389 c->pipe0.comp->disconnect_channel(c->iface,
1390 c->channel_id);
1391 if (c->pipe1.comp)
1392 c->pipe1.comp->disconnect_channel(c->iface,
1393 c->channel_id);
1394 c->pipe0.comp = NULL;
1395 c->pipe1.comp = NULL;
1396 list_del(&c->list);
1397 device_unregister(&c->dev);
1398 }
1399
1400 ida_simple_remove(&mdev_id, iface->p->dev_id);
1401 kfree(iface->p);
1402 device_unregister(iface->dev);
1403 }
1404 EXPORT_SYMBOL_GPL(most_deregister_interface);
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416 void most_stop_enqueue(struct most_interface *iface, int id)
1417 {
1418 struct most_channel *c = iface->p->channel[id];
1419
1420 if (!c)
1421 return;
1422
1423 mutex_lock(&c->nq_mutex);
1424 c->enqueue_halt = true;
1425 mutex_unlock(&c->nq_mutex);
1426 }
1427 EXPORT_SYMBOL_GPL(most_stop_enqueue);
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 void most_resume_enqueue(struct most_interface *iface, int id)
1438 {
1439 struct most_channel *c = iface->p->channel[id];
1440
1441 if (!c)
1442 return;
1443
1444 mutex_lock(&c->nq_mutex);
1445 c->enqueue_halt = false;
1446 mutex_unlock(&c->nq_mutex);
1447
1448 wake_up_interruptible(&c->hdm_fifo_wq);
1449 }
1450 EXPORT_SYMBOL_GPL(most_resume_enqueue);
1451
1452 static int __init most_init(void)
1453 {
1454 int err;
1455
1456 INIT_LIST_HEAD(&comp_list);
1457 ida_init(&mdev_id);
1458
1459 err = bus_register(&mostbus);
1460 if (err) {
1461 pr_err("Failed to register most bus\n");
1462 return err;
1463 }
1464 err = driver_register(&mostbus_driver);
1465 if (err) {
1466 pr_err("Failed to register core driver\n");
1467 goto err_unregister_bus;
1468 }
1469 configfs_init();
1470 return 0;
1471
1472 err_unregister_bus:
1473 bus_unregister(&mostbus);
1474 return err;
1475 }
1476
1477 static void __exit most_exit(void)
1478 {
1479 driver_unregister(&mostbus_driver);
1480 bus_unregister(&mostbus);
1481 ida_destroy(&mdev_id);
1482 }
1483
1484 subsys_initcall(most_init);
1485 module_exit(most_exit);
1486 MODULE_LICENSE("GPL");
1487 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1488 MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");