stm class: Introduce an abstraction for System Trace Module devices
[linux-stable.git] / drivers / hwtracing / stm / core.c
blobb79c42c62557fea294e9156717024b5fd108159f
1 /*
2 * System Trace Module (STM) infrastructure
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * STM class implements generic infrastructure for System Trace Module devices
15 * as defined in MIPI STPv2 specification.
18 #include <linux/uaccess.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/compat.h>
23 #include <linux/kdev_t.h>
24 #include <linux/srcu.h>
25 #include <linux/slab.h>
26 #include <linux/stm.h>
27 #include <linux/fs.h>
28 #include <linux/mm.h>
29 #include "stm.h"
31 #include <uapi/linux/stm.h>
33 static unsigned int stm_core_up;
36 * The SRCU here makes sure that STM device doesn't disappear from under a
37 * stm_source_write() caller, which may want to have as little overhead as
38 * possible.
40 static struct srcu_struct stm_source_srcu;
42 static ssize_t masters_show(struct device *dev,
43 struct device_attribute *attr,
44 char *buf)
46 struct stm_device *stm = to_stm_device(dev);
47 int ret;
49 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
51 return ret;
54 static DEVICE_ATTR_RO(masters);
56 static ssize_t channels_show(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
60 struct stm_device *stm = to_stm_device(dev);
61 int ret;
63 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
65 return ret;
68 static DEVICE_ATTR_RO(channels);
70 static struct attribute *stm_attrs[] = {
71 &dev_attr_masters.attr,
72 &dev_attr_channels.attr,
73 NULL,
76 ATTRIBUTE_GROUPS(stm);
78 static struct class stm_class = {
79 .name = "stm",
80 .dev_groups = stm_groups,
83 static int stm_dev_match(struct device *dev, const void *data)
85 const char *name = data;
87 return sysfs_streq(name, dev_name(dev));
90 /**
91 * stm_find_device() - find stm device by name
92 * @buf: character buffer containing the name
94 * This is called when either policy gets assigned to an stm device or an
95 * stm_source device gets linked to an stm device.
97 * This grabs device's reference (get_device()) and module reference, both
98 * of which the calling path needs to make sure to drop with stm_put_device().
100 * Return: stm device pointer or null if lookup failed.
102 struct stm_device *stm_find_device(const char *buf)
104 struct stm_device *stm;
105 struct device *dev;
107 if (!stm_core_up)
108 return NULL;
110 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
111 if (!dev)
112 return NULL;
114 stm = to_stm_device(dev);
115 if (!try_module_get(stm->owner)) {
116 put_device(dev);
117 return NULL;
120 return stm;
124 * stm_put_device() - drop references on the stm device
125 * @stm: stm device, previously acquired by stm_find_device()
127 * This drops the module reference and device reference taken by
128 * stm_find_device().
130 void stm_put_device(struct stm_device *stm)
132 module_put(stm->owner);
133 put_device(&stm->dev);
137 * Internally we only care about software-writable masters here, that is the
138 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
139 * original master numbers to be visible externally, since they are the ones
140 * that will appear in the STP stream. Thus, the internal bookkeeping uses
141 * $master - stm_data->sw_start to reference master descriptors and such.
144 #define __stm_master(_s, _m) \
145 ((_s)->masters[(_m) - (_s)->data->sw_start])
147 static inline struct stp_master *
148 stm_master(struct stm_device *stm, unsigned int idx)
150 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
151 return NULL;
153 return __stm_master(stm, idx);
156 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
158 struct stp_master *master;
159 size_t size;
161 size = ALIGN(stm->data->sw_nchannels, 8) / 8;
162 size += sizeof(struct stp_master);
163 master = kzalloc(size, GFP_ATOMIC);
164 if (!master)
165 return -ENOMEM;
167 master->nr_free = stm->data->sw_nchannels;
168 __stm_master(stm, idx) = master;
170 return 0;
173 static void stp_master_free(struct stm_device *stm, unsigned int idx)
175 struct stp_master *master = stm_master(stm, idx);
177 if (!master)
178 return;
180 __stm_master(stm, idx) = NULL;
181 kfree(master);
184 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
186 struct stp_master *master = stm_master(stm, output->master);
188 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
189 return;
191 bitmap_allocate_region(&master->chan_map[0], output->channel,
192 ilog2(output->nr_chans));
194 master->nr_free -= output->nr_chans;
197 static void
198 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
200 struct stp_master *master = stm_master(stm, output->master);
202 bitmap_release_region(&master->chan_map[0], output->channel,
203 ilog2(output->nr_chans));
205 output->nr_chans = 0;
206 master->nr_free += output->nr_chans;
210 * This is like bitmap_find_free_region(), except it can ignore @start bits
211 * at the beginning.
213 static int find_free_channels(unsigned long *bitmap, unsigned int start,
214 unsigned int end, unsigned int width)
216 unsigned int pos;
217 int i;
219 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
220 pos = find_next_zero_bit(bitmap, end + 1, pos);
221 if (pos + width > end + 1)
222 break;
224 if (pos & (width - 1))
225 continue;
227 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
229 if (i == width)
230 return pos;
233 return -1;
236 static unsigned int
237 stm_find_master_chan(struct stm_device *stm, unsigned int width,
238 unsigned int *mstart, unsigned int mend,
239 unsigned int *cstart, unsigned int cend)
241 struct stp_master *master;
242 unsigned int midx;
243 int pos, err;
245 for (midx = *mstart; midx <= mend; midx++) {
246 if (!stm_master(stm, midx)) {
247 err = stp_master_alloc(stm, midx);
248 if (err)
249 return err;
252 master = stm_master(stm, midx);
254 if (!master->nr_free)
255 continue;
257 pos = find_free_channels(master->chan_map, *cstart, cend,
258 width);
259 if (pos < 0)
260 continue;
262 *mstart = midx;
263 *cstart = pos;
264 return 0;
267 return -ENOSPC;
270 static int stm_output_assign(struct stm_device *stm, unsigned int width,
271 struct stp_policy_node *policy_node,
272 struct stm_output *output)
274 unsigned int midx, cidx, mend, cend;
275 int ret = -EINVAL;
277 if (width > stm->data->sw_nchannels)
278 return -EINVAL;
280 if (policy_node) {
281 stp_policy_node_get_ranges(policy_node,
282 &midx, &mend, &cidx, &cend);
283 } else {
284 midx = stm->data->sw_start;
285 cidx = 0;
286 mend = stm->data->sw_end;
287 cend = stm->data->sw_nchannels - 1;
290 spin_lock(&stm->mc_lock);
291 /* output is already assigned -- shouldn't happen */
292 if (WARN_ON_ONCE(output->nr_chans))
293 goto unlock;
295 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
296 if (ret)
297 goto unlock;
299 output->master = midx;
300 output->channel = cidx;
301 output->nr_chans = width;
302 stm_output_claim(stm, output);
303 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
305 ret = 0;
306 unlock:
307 spin_unlock(&stm->mc_lock);
309 return ret;
312 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
314 spin_lock(&stm->mc_lock);
315 if (output->nr_chans)
316 stm_output_disclaim(stm, output);
317 spin_unlock(&stm->mc_lock);
320 static int major_match(struct device *dev, const void *data)
322 unsigned int major = *(unsigned int *)data;
324 return MAJOR(dev->devt) == major;
327 static int stm_char_open(struct inode *inode, struct file *file)
329 struct stm_file *stmf;
330 struct device *dev;
331 unsigned int major = imajor(inode);
332 int err = -ENODEV;
334 dev = class_find_device(&stm_class, NULL, &major, major_match);
335 if (!dev)
336 return -ENODEV;
338 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
339 if (!stmf)
340 return -ENOMEM;
342 stmf->stm = to_stm_device(dev);
344 if (!try_module_get(stmf->stm->owner))
345 goto err_free;
347 file->private_data = stmf;
349 return nonseekable_open(inode, file);
351 err_free:
352 kfree(stmf);
354 return err;
357 static int stm_char_release(struct inode *inode, struct file *file)
359 struct stm_file *stmf = file->private_data;
361 stm_output_free(stmf->stm, &stmf->output);
362 stm_put_device(stmf->stm);
363 kfree(stmf);
365 return 0;
368 static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
370 struct stm_device *stm = stmf->stm;
371 int ret;
373 stmf->policy_node = stp_policy_node_lookup(stm, id);
375 ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
377 if (stmf->policy_node)
378 stp_policy_node_put(stmf->policy_node);
380 return ret;
383 static void stm_write(struct stm_data *data, unsigned int master,
384 unsigned int channel, const char *buf, size_t count)
386 unsigned int flags = STP_PACKET_TIMESTAMPED;
387 const unsigned char *p = buf, nil = 0;
388 size_t pos;
389 ssize_t sz;
391 for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
392 sz = min_t(unsigned int, count - pos, 8);
393 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
394 sz, p);
395 flags = 0;
398 data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
401 static ssize_t stm_char_write(struct file *file, const char __user *buf,
402 size_t count, loff_t *ppos)
404 struct stm_file *stmf = file->private_data;
405 struct stm_device *stm = stmf->stm;
406 char *kbuf;
407 int err;
410 * if no m/c have been assigned to this writer up to this
411 * point, use "default" policy entry
413 if (!stmf->output.nr_chans) {
414 err = stm_file_assign(stmf, "default", 1);
416 * EBUSY means that somebody else just assigned this
417 * output, which is just fine for write()
419 if (err && err != -EBUSY)
420 return err;
423 kbuf = kmalloc(count + 1, GFP_KERNEL);
424 if (!kbuf)
425 return -ENOMEM;
427 err = copy_from_user(kbuf, buf, count);
428 if (err) {
429 kfree(kbuf);
430 return -EFAULT;
433 stm_write(stm->data, stmf->output.master, stmf->output.channel, kbuf,
434 count);
436 kfree(kbuf);
438 return count;
441 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
443 struct stm_file *stmf = file->private_data;
444 struct stm_device *stm = stmf->stm;
445 unsigned long size, phys;
447 if (!stm->data->mmio_addr)
448 return -EOPNOTSUPP;
450 if (vma->vm_pgoff)
451 return -EINVAL;
453 size = vma->vm_end - vma->vm_start;
455 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
456 return -EINVAL;
458 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
459 stmf->output.channel,
460 stmf->output.nr_chans);
462 if (!phys)
463 return -EINVAL;
465 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
466 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
467 vm_iomap_memory(vma, phys, size);
469 return 0;
472 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
474 struct stm_device *stm = stmf->stm;
475 struct stp_policy_id *id;
476 int ret = -EINVAL;
477 u32 size;
479 if (stmf->output.nr_chans)
480 return -EBUSY;
482 if (copy_from_user(&size, arg, sizeof(size)))
483 return -EFAULT;
485 if (size >= PATH_MAX + sizeof(*id))
486 return -EINVAL;
489 * size + 1 to make sure the .id string at the bottom is terminated,
490 * which is also why memdup_user() is not useful here
492 id = kzalloc(size + 1, GFP_KERNEL);
493 if (!id)
494 return -ENOMEM;
496 if (copy_from_user(id, arg, size)) {
497 ret = -EFAULT;
498 goto err_free;
501 if (id->__reserved_0 || id->__reserved_1)
502 goto err_free;
504 if (id->width < 1 ||
505 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
506 goto err_free;
508 ret = stm_file_assign(stmf, id->id, id->width);
509 if (ret)
510 goto err_free;
512 ret = 0;
514 if (stm->data->link)
515 ret = stm->data->link(stm->data, stmf->output.master,
516 stmf->output.channel);
518 if (ret) {
519 stm_output_free(stmf->stm, &stmf->output);
520 stm_put_device(stmf->stm);
523 err_free:
524 kfree(id);
526 return ret;
529 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
531 struct stp_policy_id id = {
532 .size = sizeof(id),
533 .master = stmf->output.master,
534 .channel = stmf->output.channel,
535 .width = stmf->output.nr_chans,
536 .__reserved_0 = 0,
537 .__reserved_1 = 0,
540 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
543 static long
544 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
546 struct stm_file *stmf = file->private_data;
547 struct stm_data *stm_data = stmf->stm->data;
548 int err = -ENOTTY;
549 u64 options;
551 switch (cmd) {
552 case STP_POLICY_ID_SET:
553 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
554 if (err)
555 return err;
557 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
559 case STP_POLICY_ID_GET:
560 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
562 case STP_SET_OPTIONS:
563 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
564 return -EFAULT;
566 if (stm_data->set_options)
567 err = stm_data->set_options(stm_data,
568 stmf->output.master,
569 stmf->output.channel,
570 stmf->output.nr_chans,
571 options);
573 break;
574 default:
575 break;
578 return err;
581 #ifdef CONFIG_COMPAT
582 static long
583 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
585 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
587 #else
588 #define stm_char_compat_ioctl NULL
589 #endif
591 static const struct file_operations stm_fops = {
592 .open = stm_char_open,
593 .release = stm_char_release,
594 .write = stm_char_write,
595 .mmap = stm_char_mmap,
596 .unlocked_ioctl = stm_char_ioctl,
597 .compat_ioctl = stm_char_compat_ioctl,
598 .llseek = no_llseek,
601 static void stm_device_release(struct device *dev)
603 struct stm_device *stm = to_stm_device(dev);
605 kfree(stm);
608 int stm_register_device(struct device *parent, struct stm_data *stm_data,
609 struct module *owner)
611 struct stm_device *stm;
612 unsigned int nmasters;
613 int err = -ENOMEM;
615 if (!stm_core_up)
616 return -EPROBE_DEFER;
618 if (!stm_data->packet || !stm_data->sw_nchannels)
619 return -EINVAL;
621 nmasters = stm_data->sw_end - stm_data->sw_start;
622 stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
623 if (!stm)
624 return -ENOMEM;
626 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
627 if (stm->major < 0)
628 goto err_free;
630 device_initialize(&stm->dev);
631 stm->dev.devt = MKDEV(stm->major, 0);
632 stm->dev.class = &stm_class;
633 stm->dev.parent = parent;
634 stm->dev.release = stm_device_release;
636 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
637 if (err)
638 goto err_device;
640 err = device_add(&stm->dev);
641 if (err)
642 goto err_device;
644 spin_lock_init(&stm->link_lock);
645 INIT_LIST_HEAD(&stm->link_list);
647 spin_lock_init(&stm->mc_lock);
648 mutex_init(&stm->policy_mutex);
649 stm->sw_nmasters = nmasters;
650 stm->owner = owner;
651 stm->data = stm_data;
652 stm_data->stm = stm;
654 return 0;
656 err_device:
657 put_device(&stm->dev);
658 err_free:
659 kfree(stm);
661 return err;
663 EXPORT_SYMBOL_GPL(stm_register_device);
665 static void __stm_source_link_drop(struct stm_source_device *src,
666 struct stm_device *stm);
668 void stm_unregister_device(struct stm_data *stm_data)
670 struct stm_device *stm = stm_data->stm;
671 struct stm_source_device *src, *iter;
672 int i;
674 spin_lock(&stm->link_lock);
675 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
676 __stm_source_link_drop(src, stm);
678 spin_unlock(&stm->link_lock);
680 synchronize_srcu(&stm_source_srcu);
682 unregister_chrdev(stm->major, stm_data->name);
684 mutex_lock(&stm->policy_mutex);
685 if (stm->policy)
686 stp_policy_unbind(stm->policy);
687 mutex_unlock(&stm->policy_mutex);
689 for (i = 0; i < stm->sw_nmasters; i++)
690 stp_master_free(stm, i);
692 device_unregister(&stm->dev);
693 stm_data->stm = NULL;
695 EXPORT_SYMBOL_GPL(stm_unregister_device);
698 * stm_source_link_add() - connect an stm_source device to an stm device
699 * @src: stm_source device
700 * @stm: stm device
702 * This function establishes a link from stm_source to an stm device so that
703 * the former can send out trace data to the latter.
705 * Return: 0 on success, -errno otherwise.
707 static int stm_source_link_add(struct stm_source_device *src,
708 struct stm_device *stm)
710 char *id;
711 int err;
713 spin_lock(&stm->link_lock);
714 spin_lock(&src->link_lock);
716 /* src->link is dereferenced under stm_source_srcu but not the list */
717 rcu_assign_pointer(src->link, stm);
718 list_add_tail(&src->link_entry, &stm->link_list);
720 spin_unlock(&src->link_lock);
721 spin_unlock(&stm->link_lock);
723 id = kstrdup(src->data->name, GFP_KERNEL);
724 if (id) {
725 src->policy_node =
726 stp_policy_node_lookup(stm, id);
728 kfree(id);
731 err = stm_output_assign(stm, src->data->nr_chans,
732 src->policy_node, &src->output);
734 if (src->policy_node)
735 stp_policy_node_put(src->policy_node);
737 if (err)
738 goto fail_detach;
740 /* this is to notify the STM device that a new link has been made */
741 if (stm->data->link)
742 err = stm->data->link(stm->data, src->output.master,
743 src->output.channel);
745 if (err)
746 goto fail_free_output;
748 /* this is to let the source carry out all necessary preparations */
749 if (src->data->link)
750 src->data->link(src->data);
752 return 0;
754 fail_free_output:
755 stm_output_free(stm, &src->output);
756 stm_put_device(stm);
758 fail_detach:
759 spin_lock(&stm->link_lock);
760 spin_lock(&src->link_lock);
762 rcu_assign_pointer(src->link, NULL);
763 list_del_init(&src->link_entry);
765 spin_unlock(&src->link_lock);
766 spin_unlock(&stm->link_lock);
768 return err;
772 * __stm_source_link_drop() - detach stm_source from an stm device
773 * @src: stm_source device
774 * @stm: stm device
776 * If @stm is @src::link, disconnect them from one another and put the
777 * reference on the @stm device.
779 * Caller must hold stm::link_lock.
781 static void __stm_source_link_drop(struct stm_source_device *src,
782 struct stm_device *stm)
784 spin_lock(&src->link_lock);
785 if (WARN_ON_ONCE(src->link != stm)) {
786 spin_unlock(&src->link_lock);
787 return;
790 stm_output_free(src->link, &src->output);
791 /* caller must hold stm::link_lock */
792 list_del_init(&src->link_entry);
793 /* matches stm_find_device() from stm_source_link_store() */
794 stm_put_device(src->link);
795 rcu_assign_pointer(src->link, NULL);
797 spin_unlock(&src->link_lock);
801 * stm_source_link_drop() - detach stm_source from its stm device
802 * @src: stm_source device
804 * Unlinking means disconnecting from source's STM device; after this
805 * writes will be unsuccessful until it is linked to a new STM device.
807 * This will happen on "stm_source_link" sysfs attribute write to undo
808 * the existing link (if any), or on linked STM device's de-registration.
810 static void stm_source_link_drop(struct stm_source_device *src)
812 struct stm_device *stm;
813 int idx;
815 idx = srcu_read_lock(&stm_source_srcu);
816 stm = srcu_dereference(src->link, &stm_source_srcu);
818 if (stm) {
819 if (src->data->unlink)
820 src->data->unlink(src->data);
822 spin_lock(&stm->link_lock);
823 __stm_source_link_drop(src, stm);
824 spin_unlock(&stm->link_lock);
827 srcu_read_unlock(&stm_source_srcu, idx);
830 static ssize_t stm_source_link_show(struct device *dev,
831 struct device_attribute *attr,
832 char *buf)
834 struct stm_source_device *src = to_stm_source_device(dev);
835 struct stm_device *stm;
836 int idx, ret;
838 idx = srcu_read_lock(&stm_source_srcu);
839 stm = srcu_dereference(src->link, &stm_source_srcu);
840 ret = sprintf(buf, "%s\n",
841 stm ? dev_name(&stm->dev) : "<none>");
842 srcu_read_unlock(&stm_source_srcu, idx);
844 return ret;
847 static ssize_t stm_source_link_store(struct device *dev,
848 struct device_attribute *attr,
849 const char *buf, size_t count)
851 struct stm_source_device *src = to_stm_source_device(dev);
852 struct stm_device *link;
853 int err;
855 stm_source_link_drop(src);
857 link = stm_find_device(buf);
858 if (!link)
859 return -EINVAL;
861 err = stm_source_link_add(src, link);
862 if (err)
863 stm_put_device(link);
865 return err ? : count;
868 static DEVICE_ATTR_RW(stm_source_link);
870 static struct attribute *stm_source_attrs[] = {
871 &dev_attr_stm_source_link.attr,
872 NULL,
875 ATTRIBUTE_GROUPS(stm_source);
877 static struct class stm_source_class = {
878 .name = "stm_source",
879 .dev_groups = stm_source_groups,
882 static void stm_source_device_release(struct device *dev)
884 struct stm_source_device *src = to_stm_source_device(dev);
886 kfree(src);
890 * stm_source_register_device() - register an stm_source device
891 * @parent: parent device
892 * @data: device description structure
894 * This will create a device of stm_source class that can write
895 * data to an stm device once linked.
897 * Return: 0 on success, -errno otherwise.
899 int stm_source_register_device(struct device *parent,
900 struct stm_source_data *data)
902 struct stm_source_device *src;
903 int err;
905 if (!stm_core_up)
906 return -EPROBE_DEFER;
908 src = kzalloc(sizeof(*src), GFP_KERNEL);
909 if (!src)
910 return -ENOMEM;
912 device_initialize(&src->dev);
913 src->dev.class = &stm_source_class;
914 src->dev.parent = parent;
915 src->dev.release = stm_source_device_release;
917 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
918 if (err)
919 goto err;
921 err = device_add(&src->dev);
922 if (err)
923 goto err;
925 spin_lock_init(&src->link_lock);
926 INIT_LIST_HEAD(&src->link_entry);
927 src->data = data;
928 data->src = src;
930 return 0;
932 err:
933 put_device(&src->dev);
934 kfree(src);
936 return err;
938 EXPORT_SYMBOL_GPL(stm_source_register_device);
941 * stm_source_unregister_device() - unregister an stm_source device
942 * @data: device description that was used to register the device
944 * This will remove a previously created stm_source device from the system.
946 void stm_source_unregister_device(struct stm_source_data *data)
948 struct stm_source_device *src = data->src;
950 stm_source_link_drop(src);
952 device_destroy(&stm_source_class, src->dev.devt);
954 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
956 int stm_source_write(struct stm_source_data *data, unsigned int chan,
957 const char *buf, size_t count)
959 struct stm_source_device *src = data->src;
960 struct stm_device *stm;
961 int idx;
963 if (!src->output.nr_chans)
964 return -ENODEV;
966 if (chan >= src->output.nr_chans)
967 return -EINVAL;
969 idx = srcu_read_lock(&stm_source_srcu);
971 stm = srcu_dereference(src->link, &stm_source_srcu);
972 if (stm)
973 stm_write(stm->data, src->output.master,
974 src->output.channel + chan,
975 buf, count);
976 else
977 count = -ENODEV;
979 srcu_read_unlock(&stm_source_srcu, idx);
981 return count;
983 EXPORT_SYMBOL_GPL(stm_source_write);
985 static int __init stm_core_init(void)
987 int err;
989 err = class_register(&stm_class);
990 if (err)
991 return err;
993 err = class_register(&stm_source_class);
994 if (err)
995 goto err_stm;
997 err = stp_configfs_init();
998 if (err)
999 goto err_src;
1001 init_srcu_struct(&stm_source_srcu);
1003 stm_core_up++;
1005 return 0;
1007 err_src:
1008 class_unregister(&stm_source_class);
1009 err_stm:
1010 class_unregister(&stm_class);
1012 return err;
1015 module_init(stm_core_init);
1017 static void __exit stm_core_exit(void)
1019 cleanup_srcu_struct(&stm_source_srcu);
1020 class_unregister(&stm_source_class);
1021 class_unregister(&stm_class);
1022 stp_configfs_exit();
1025 module_exit(stm_core_exit);
1027 MODULE_LICENSE("GPL v2");
1028 MODULE_DESCRIPTION("System Trace Module device class");
1029 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");