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
14 * STM class implements generic infrastructure for System Trace Module devices
15 * as defined in MIPI STPv2 specification.
18 #include <linux/pm_runtime.h>
19 #include <linux/uaccess.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/compat.h>
24 #include <linux/kdev_t.h>
25 #include <linux/srcu.h>
26 #include <linux/slab.h>
27 #include <linux/stm.h>
32 #include <uapi/linux/stm.h>
34 static unsigned int stm_core_up
;
37 * The SRCU here makes sure that STM device doesn't disappear from under a
38 * stm_source_write() caller, which may want to have as little overhead as
41 static struct srcu_struct stm_source_srcu
;
43 static ssize_t
masters_show(struct device
*dev
,
44 struct device_attribute
*attr
,
47 struct stm_device
*stm
= to_stm_device(dev
);
50 ret
= sprintf(buf
, "%u %u\n", stm
->data
->sw_start
, stm
->data
->sw_end
);
55 static DEVICE_ATTR_RO(masters
);
57 static ssize_t
channels_show(struct device
*dev
,
58 struct device_attribute
*attr
,
61 struct stm_device
*stm
= to_stm_device(dev
);
64 ret
= sprintf(buf
, "%u\n", stm
->data
->sw_nchannels
);
69 static DEVICE_ATTR_RO(channels
);
71 static ssize_t
hw_override_show(struct device
*dev
,
72 struct device_attribute
*attr
,
75 struct stm_device
*stm
= to_stm_device(dev
);
78 ret
= sprintf(buf
, "%u\n", stm
->data
->hw_override
);
83 static DEVICE_ATTR_RO(hw_override
);
85 static struct attribute
*stm_attrs
[] = {
86 &dev_attr_masters
.attr
,
87 &dev_attr_channels
.attr
,
88 &dev_attr_hw_override
.attr
,
92 ATTRIBUTE_GROUPS(stm
);
94 static struct class stm_class
= {
96 .dev_groups
= stm_groups
,
99 static int stm_dev_match(struct device
*dev
, const void *data
)
101 const char *name
= data
;
103 return sysfs_streq(name
, dev_name(dev
));
107 * stm_find_device() - find stm device by name
108 * @buf: character buffer containing the name
110 * This is called when either policy gets assigned to an stm device or an
111 * stm_source device gets linked to an stm device.
113 * This grabs device's reference (get_device()) and module reference, both
114 * of which the calling path needs to make sure to drop with stm_put_device().
116 * Return: stm device pointer or null if lookup failed.
118 struct stm_device
*stm_find_device(const char *buf
)
120 struct stm_device
*stm
;
126 dev
= class_find_device(&stm_class
, NULL
, buf
, stm_dev_match
);
130 stm
= to_stm_device(dev
);
131 if (!try_module_get(stm
->owner
)) {
132 /* matches class_find_device() above */
141 * stm_put_device() - drop references on the stm device
142 * @stm: stm device, previously acquired by stm_find_device()
144 * This drops the module reference and device reference taken by
145 * stm_find_device() or stm_char_open().
147 void stm_put_device(struct stm_device
*stm
)
149 module_put(stm
->owner
);
150 put_device(&stm
->dev
);
154 * Internally we only care about software-writable masters here, that is the
155 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
156 * original master numbers to be visible externally, since they are the ones
157 * that will appear in the STP stream. Thus, the internal bookkeeping uses
158 * $master - stm_data->sw_start to reference master descriptors and such.
161 #define __stm_master(_s, _m) \
162 ((_s)->masters[(_m) - (_s)->data->sw_start])
164 static inline struct stp_master
*
165 stm_master(struct stm_device
*stm
, unsigned int idx
)
167 if (idx
< stm
->data
->sw_start
|| idx
> stm
->data
->sw_end
)
170 return __stm_master(stm
, idx
);
173 static int stp_master_alloc(struct stm_device
*stm
, unsigned int idx
)
175 struct stp_master
*master
;
178 size
= ALIGN(stm
->data
->sw_nchannels
, 8) / 8;
179 size
+= sizeof(struct stp_master
);
180 master
= kzalloc(size
, GFP_ATOMIC
);
184 master
->nr_free
= stm
->data
->sw_nchannels
;
185 __stm_master(stm
, idx
) = master
;
190 static void stp_master_free(struct stm_device
*stm
, unsigned int idx
)
192 struct stp_master
*master
= stm_master(stm
, idx
);
197 __stm_master(stm
, idx
) = NULL
;
201 static void stm_output_claim(struct stm_device
*stm
, struct stm_output
*output
)
203 struct stp_master
*master
= stm_master(stm
, output
->master
);
205 lockdep_assert_held(&stm
->mc_lock
);
206 lockdep_assert_held(&output
->lock
);
208 if (WARN_ON_ONCE(master
->nr_free
< output
->nr_chans
))
211 bitmap_allocate_region(&master
->chan_map
[0], output
->channel
,
212 ilog2(output
->nr_chans
));
214 master
->nr_free
-= output
->nr_chans
;
218 stm_output_disclaim(struct stm_device
*stm
, struct stm_output
*output
)
220 struct stp_master
*master
= stm_master(stm
, output
->master
);
222 lockdep_assert_held(&stm
->mc_lock
);
223 lockdep_assert_held(&output
->lock
);
225 bitmap_release_region(&master
->chan_map
[0], output
->channel
,
226 ilog2(output
->nr_chans
));
228 output
->nr_chans
= 0;
229 master
->nr_free
+= output
->nr_chans
;
233 * This is like bitmap_find_free_region(), except it can ignore @start bits
236 static int find_free_channels(unsigned long *bitmap
, unsigned int start
,
237 unsigned int end
, unsigned int width
)
242 for (pos
= start
; pos
< end
+ 1; pos
= ALIGN(pos
, width
)) {
243 pos
= find_next_zero_bit(bitmap
, end
+ 1, pos
);
244 if (pos
+ width
> end
+ 1)
247 if (pos
& (width
- 1))
250 for (i
= 1; i
< width
&& !test_bit(pos
+ i
, bitmap
); i
++)
260 stm_find_master_chan(struct stm_device
*stm
, unsigned int width
,
261 unsigned int *mstart
, unsigned int mend
,
262 unsigned int *cstart
, unsigned int cend
)
264 struct stp_master
*master
;
268 for (midx
= *mstart
; midx
<= mend
; midx
++) {
269 if (!stm_master(stm
, midx
)) {
270 err
= stp_master_alloc(stm
, midx
);
275 master
= stm_master(stm
, midx
);
277 if (!master
->nr_free
)
280 pos
= find_free_channels(master
->chan_map
, *cstart
, cend
,
293 static int stm_output_assign(struct stm_device
*stm
, unsigned int width
,
294 struct stp_policy_node
*policy_node
,
295 struct stm_output
*output
)
297 unsigned int midx
, cidx
, mend
, cend
;
300 if (width
> stm
->data
->sw_nchannels
)
304 stp_policy_node_get_ranges(policy_node
,
305 &midx
, &mend
, &cidx
, &cend
);
307 midx
= stm
->data
->sw_start
;
309 mend
= stm
->data
->sw_end
;
310 cend
= stm
->data
->sw_nchannels
- 1;
313 spin_lock(&stm
->mc_lock
);
314 spin_lock(&output
->lock
);
315 /* output is already assigned -- shouldn't happen */
316 if (WARN_ON_ONCE(output
->nr_chans
))
319 ret
= stm_find_master_chan(stm
, width
, &midx
, mend
, &cidx
, cend
);
323 output
->master
= midx
;
324 output
->channel
= cidx
;
325 output
->nr_chans
= width
;
326 stm_output_claim(stm
, output
);
327 dev_dbg(&stm
->dev
, "assigned %u:%u (+%u)\n", midx
, cidx
, width
);
331 spin_unlock(&output
->lock
);
332 spin_unlock(&stm
->mc_lock
);
337 static void stm_output_free(struct stm_device
*stm
, struct stm_output
*output
)
339 spin_lock(&stm
->mc_lock
);
340 spin_lock(&output
->lock
);
341 if (output
->nr_chans
)
342 stm_output_disclaim(stm
, output
);
343 spin_unlock(&output
->lock
);
344 spin_unlock(&stm
->mc_lock
);
347 static void stm_output_init(struct stm_output
*output
)
349 spin_lock_init(&output
->lock
);
352 static int major_match(struct device
*dev
, const void *data
)
354 unsigned int major
= *(unsigned int *)data
;
356 return MAJOR(dev
->devt
) == major
;
359 static int stm_char_open(struct inode
*inode
, struct file
*file
)
361 struct stm_file
*stmf
;
363 unsigned int major
= imajor(inode
);
366 dev
= class_find_device(&stm_class
, NULL
, &major
, major_match
);
370 stmf
= kzalloc(sizeof(*stmf
), GFP_KERNEL
);
375 stm_output_init(&stmf
->output
);
376 stmf
->stm
= to_stm_device(dev
);
378 if (!try_module_get(stmf
->stm
->owner
))
381 file
->private_data
= stmf
;
383 return nonseekable_open(inode
, file
);
388 /* matches class_find_device() above */
394 static int stm_char_release(struct inode
*inode
, struct file
*file
)
396 struct stm_file
*stmf
= file
->private_data
;
397 struct stm_device
*stm
= stmf
->stm
;
399 if (stm
->data
->unlink
)
400 stm
->data
->unlink(stm
->data
, stmf
->output
.master
,
401 stmf
->output
.channel
);
403 stm_output_free(stm
, &stmf
->output
);
406 * matches the stm_char_open()'s
407 * class_find_device() + try_module_get()
415 static int stm_file_assign(struct stm_file
*stmf
, char *id
, unsigned int width
)
417 struct stm_device
*stm
= stmf
->stm
;
420 stmf
->policy_node
= stp_policy_node_lookup(stm
, id
);
422 ret
= stm_output_assign(stm
, width
, stmf
->policy_node
, &stmf
->output
);
424 if (stmf
->policy_node
)
425 stp_policy_node_put(stmf
->policy_node
);
430 static ssize_t notrace
stm_write(struct stm_data
*data
, unsigned int master
,
431 unsigned int channel
, const char *buf
, size_t count
)
433 unsigned int flags
= STP_PACKET_TIMESTAMPED
;
434 const unsigned char *p
= buf
, nil
= 0;
438 for (pos
= 0, p
= buf
; count
> pos
; pos
+= sz
, p
+= sz
) {
439 sz
= min_t(unsigned int, count
- pos
, 8);
440 sz
= data
->packet(data
, master
, channel
, STP_PACKET_DATA
, flags
,
448 data
->packet(data
, master
, channel
, STP_PACKET_FLAG
, 0, 0, &nil
);
453 static ssize_t
stm_char_write(struct file
*file
, const char __user
*buf
,
454 size_t count
, loff_t
*ppos
)
456 struct stm_file
*stmf
= file
->private_data
;
457 struct stm_device
*stm
= stmf
->stm
;
461 if (count
+ 1 > PAGE_SIZE
)
462 count
= PAGE_SIZE
- 1;
465 * if no m/c have been assigned to this writer up to this
466 * point, use "default" policy entry
468 if (!stmf
->output
.nr_chans
) {
469 err
= stm_file_assign(stmf
, "default", 1);
471 * EBUSY means that somebody else just assigned this
472 * output, which is just fine for write()
474 if (err
&& err
!= -EBUSY
)
478 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
482 err
= copy_from_user(kbuf
, buf
, count
);
488 pm_runtime_get_sync(&stm
->dev
);
490 count
= stm_write(stm
->data
, stmf
->output
.master
, stmf
->output
.channel
,
493 pm_runtime_mark_last_busy(&stm
->dev
);
494 pm_runtime_put_autosuspend(&stm
->dev
);
500 static void stm_mmap_open(struct vm_area_struct
*vma
)
502 struct stm_file
*stmf
= vma
->vm_file
->private_data
;
503 struct stm_device
*stm
= stmf
->stm
;
505 pm_runtime_get(&stm
->dev
);
508 static void stm_mmap_close(struct vm_area_struct
*vma
)
510 struct stm_file
*stmf
= vma
->vm_file
->private_data
;
511 struct stm_device
*stm
= stmf
->stm
;
513 pm_runtime_mark_last_busy(&stm
->dev
);
514 pm_runtime_put_autosuspend(&stm
->dev
);
517 static const struct vm_operations_struct stm_mmap_vmops
= {
518 .open
= stm_mmap_open
,
519 .close
= stm_mmap_close
,
522 static int stm_char_mmap(struct file
*file
, struct vm_area_struct
*vma
)
524 struct stm_file
*stmf
= file
->private_data
;
525 struct stm_device
*stm
= stmf
->stm
;
526 unsigned long size
, phys
;
528 if (!stm
->data
->mmio_addr
)
534 size
= vma
->vm_end
- vma
->vm_start
;
536 if (stmf
->output
.nr_chans
* stm
->data
->sw_mmiosz
!= size
)
539 phys
= stm
->data
->mmio_addr(stm
->data
, stmf
->output
.master
,
540 stmf
->output
.channel
,
541 stmf
->output
.nr_chans
);
546 pm_runtime_get_sync(&stm
->dev
);
548 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
549 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
550 vma
->vm_ops
= &stm_mmap_vmops
;
551 vm_iomap_memory(vma
, phys
, size
);
556 static int stm_char_policy_set_ioctl(struct stm_file
*stmf
, void __user
*arg
)
558 struct stm_device
*stm
= stmf
->stm
;
559 struct stp_policy_id
*id
;
563 if (stmf
->output
.nr_chans
)
566 if (copy_from_user(&size
, arg
, sizeof(size
)))
569 if (size
>= PATH_MAX
+ sizeof(*id
))
573 * size + 1 to make sure the .id string at the bottom is terminated,
574 * which is also why memdup_user() is not useful here
576 id
= kzalloc(size
+ 1, GFP_KERNEL
);
580 if (copy_from_user(id
, arg
, size
)) {
585 if (id
->__reserved_0
|| id
->__reserved_1
)
589 id
->width
> PAGE_SIZE
/ stm
->data
->sw_mmiosz
)
592 ret
= stm_file_assign(stmf
, id
->id
, id
->width
);
597 ret
= stm
->data
->link(stm
->data
, stmf
->output
.master
,
598 stmf
->output
.channel
);
601 stm_output_free(stmf
->stm
, &stmf
->output
);
609 static int stm_char_policy_get_ioctl(struct stm_file
*stmf
, void __user
*arg
)
611 struct stp_policy_id id
= {
613 .master
= stmf
->output
.master
,
614 .channel
= stmf
->output
.channel
,
615 .width
= stmf
->output
.nr_chans
,
620 return copy_to_user(arg
, &id
, id
.size
) ? -EFAULT
: 0;
624 stm_char_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
626 struct stm_file
*stmf
= file
->private_data
;
627 struct stm_data
*stm_data
= stmf
->stm
->data
;
632 case STP_POLICY_ID_SET
:
633 err
= stm_char_policy_set_ioctl(stmf
, (void __user
*)arg
);
637 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
639 case STP_POLICY_ID_GET
:
640 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
642 case STP_SET_OPTIONS
:
643 if (copy_from_user(&options
, (u64 __user
*)arg
, sizeof(u64
)))
646 if (stm_data
->set_options
)
647 err
= stm_data
->set_options(stm_data
,
649 stmf
->output
.channel
,
650 stmf
->output
.nr_chans
,
663 stm_char_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
665 return stm_char_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
668 #define stm_char_compat_ioctl NULL
671 static const struct file_operations stm_fops
= {
672 .open
= stm_char_open
,
673 .release
= stm_char_release
,
674 .write
= stm_char_write
,
675 .mmap
= stm_char_mmap
,
676 .unlocked_ioctl
= stm_char_ioctl
,
677 .compat_ioctl
= stm_char_compat_ioctl
,
681 static void stm_device_release(struct device
*dev
)
683 struct stm_device
*stm
= to_stm_device(dev
);
688 int stm_register_device(struct device
*parent
, struct stm_data
*stm_data
,
689 struct module
*owner
)
691 struct stm_device
*stm
;
692 unsigned int nmasters
;
696 return -EPROBE_DEFER
;
698 if (!stm_data
->packet
|| !stm_data
->sw_nchannels
)
701 nmasters
= stm_data
->sw_end
- stm_data
->sw_start
+ 1;
702 stm
= kzalloc(sizeof(*stm
) + nmasters
* sizeof(void *), GFP_KERNEL
);
706 stm
->major
= register_chrdev(0, stm_data
->name
, &stm_fops
);
710 device_initialize(&stm
->dev
);
711 stm
->dev
.devt
= MKDEV(stm
->major
, 0);
712 stm
->dev
.class = &stm_class
;
713 stm
->dev
.parent
= parent
;
714 stm
->dev
.release
= stm_device_release
;
716 mutex_init(&stm
->link_mutex
);
717 spin_lock_init(&stm
->link_lock
);
718 INIT_LIST_HEAD(&stm
->link_list
);
720 /* initialize the object before it is accessible via sysfs */
721 spin_lock_init(&stm
->mc_lock
);
722 mutex_init(&stm
->policy_mutex
);
723 stm
->sw_nmasters
= nmasters
;
725 stm
->data
= stm_data
;
728 err
= kobject_set_name(&stm
->dev
.kobj
, "%s", stm_data
->name
);
732 err
= device_add(&stm
->dev
);
737 * Use delayed autosuspend to avoid bouncing back and forth
738 * on recurring character device writes, with the initial
739 * delay time of 2 seconds.
741 pm_runtime_no_callbacks(&stm
->dev
);
742 pm_runtime_use_autosuspend(&stm
->dev
);
743 pm_runtime_set_autosuspend_delay(&stm
->dev
, 2000);
744 pm_runtime_set_suspended(&stm
->dev
);
745 pm_runtime_enable(&stm
->dev
);
750 unregister_chrdev(stm
->major
, stm_data
->name
);
752 /* matches device_initialize() above */
753 put_device(&stm
->dev
);
759 EXPORT_SYMBOL_GPL(stm_register_device
);
761 static int __stm_source_link_drop(struct stm_source_device
*src
,
762 struct stm_device
*stm
);
764 void stm_unregister_device(struct stm_data
*stm_data
)
766 struct stm_device
*stm
= stm_data
->stm
;
767 struct stm_source_device
*src
, *iter
;
770 pm_runtime_dont_use_autosuspend(&stm
->dev
);
771 pm_runtime_disable(&stm
->dev
);
773 mutex_lock(&stm
->link_mutex
);
774 list_for_each_entry_safe(src
, iter
, &stm
->link_list
, link_entry
) {
775 ret
= __stm_source_link_drop(src
, stm
);
777 * src <-> stm link must not change under the same
778 * stm::link_mutex, so complain loudly if it has;
779 * also in this situation ret!=0 means this src is
780 * not connected to this stm and it should be otherwise
781 * safe to proceed with the tear-down of stm.
785 mutex_unlock(&stm
->link_mutex
);
787 synchronize_srcu(&stm_source_srcu
);
789 unregister_chrdev(stm
->major
, stm_data
->name
);
791 mutex_lock(&stm
->policy_mutex
);
793 stp_policy_unbind(stm
->policy
);
794 mutex_unlock(&stm
->policy_mutex
);
796 for (i
= stm
->data
->sw_start
; i
<= stm
->data
->sw_end
; i
++)
797 stp_master_free(stm
, i
);
799 device_unregister(&stm
->dev
);
800 stm_data
->stm
= NULL
;
802 EXPORT_SYMBOL_GPL(stm_unregister_device
);
805 * stm::link_list access serialization uses a spinlock and a mutex; holding
806 * either of them guarantees that the list is stable; modification requires
807 * holding both of them.
809 * Lock ordering is as follows:
816 * stm_source_link_add() - connect an stm_source device to an stm device
817 * @src: stm_source device
820 * This function establishes a link from stm_source to an stm device so that
821 * the former can send out trace data to the latter.
823 * Return: 0 on success, -errno otherwise.
825 static int stm_source_link_add(struct stm_source_device
*src
,
826 struct stm_device
*stm
)
831 mutex_lock(&stm
->link_mutex
);
832 spin_lock(&stm
->link_lock
);
833 spin_lock(&src
->link_lock
);
835 /* src->link is dereferenced under stm_source_srcu but not the list */
836 rcu_assign_pointer(src
->link
, stm
);
837 list_add_tail(&src
->link_entry
, &stm
->link_list
);
839 spin_unlock(&src
->link_lock
);
840 spin_unlock(&stm
->link_lock
);
841 mutex_unlock(&stm
->link_mutex
);
843 id
= kstrdup(src
->data
->name
, GFP_KERNEL
);
846 stp_policy_node_lookup(stm
, id
);
851 err
= stm_output_assign(stm
, src
->data
->nr_chans
,
852 src
->policy_node
, &src
->output
);
854 if (src
->policy_node
)
855 stp_policy_node_put(src
->policy_node
);
860 /* this is to notify the STM device that a new link has been made */
862 err
= stm
->data
->link(stm
->data
, src
->output
.master
,
863 src
->output
.channel
);
866 goto fail_free_output
;
868 /* this is to let the source carry out all necessary preparations */
870 src
->data
->link(src
->data
);
875 stm_output_free(stm
, &src
->output
);
878 mutex_lock(&stm
->link_mutex
);
879 spin_lock(&stm
->link_lock
);
880 spin_lock(&src
->link_lock
);
882 rcu_assign_pointer(src
->link
, NULL
);
883 list_del_init(&src
->link_entry
);
885 spin_unlock(&src
->link_lock
);
886 spin_unlock(&stm
->link_lock
);
887 mutex_unlock(&stm
->link_mutex
);
893 * __stm_source_link_drop() - detach stm_source from an stm device
894 * @src: stm_source device
897 * If @stm is @src::link, disconnect them from one another and put the
898 * reference on the @stm device.
900 * Caller must hold stm::link_mutex.
902 static int __stm_source_link_drop(struct stm_source_device
*src
,
903 struct stm_device
*stm
)
905 struct stm_device
*link
;
908 lockdep_assert_held(&stm
->link_mutex
);
910 /* for stm::link_list modification, we hold both mutex and spinlock */
911 spin_lock(&stm
->link_lock
);
912 spin_lock(&src
->link_lock
);
913 link
= srcu_dereference_check(src
->link
, &stm_source_srcu
, 1);
916 * The linked device may have changed since we last looked, because
917 * we weren't holding the src::link_lock back then; if this is the
918 * case, tell the caller to retry.
925 stm_output_free(link
, &src
->output
);
926 list_del_init(&src
->link_entry
);
927 pm_runtime_mark_last_busy(&link
->dev
);
928 pm_runtime_put_autosuspend(&link
->dev
);
929 /* matches stm_find_device() from stm_source_link_store() */
930 stm_put_device(link
);
931 rcu_assign_pointer(src
->link
, NULL
);
934 spin_unlock(&src
->link_lock
);
935 spin_unlock(&stm
->link_lock
);
938 * Call the unlink callbacks for both source and stm, when we know
939 * that we have actually performed the unlinking.
942 if (src
->data
->unlink
)
943 src
->data
->unlink(src
->data
);
945 if (stm
->data
->unlink
)
946 stm
->data
->unlink(stm
->data
, src
->output
.master
,
947 src
->output
.channel
);
954 * stm_source_link_drop() - detach stm_source from its stm device
955 * @src: stm_source device
957 * Unlinking means disconnecting from source's STM device; after this
958 * writes will be unsuccessful until it is linked to a new STM device.
960 * This will happen on "stm_source_link" sysfs attribute write to undo
961 * the existing link (if any), or on linked STM device's de-registration.
963 static void stm_source_link_drop(struct stm_source_device
*src
)
965 struct stm_device
*stm
;
969 idx
= srcu_read_lock(&stm_source_srcu
);
971 * The stm device will be valid for the duration of this
972 * read section, but the link may change before we grab
973 * the src::link_lock in __stm_source_link_drop().
975 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
979 mutex_lock(&stm
->link_mutex
);
980 ret
= __stm_source_link_drop(src
, stm
);
981 mutex_unlock(&stm
->link_mutex
);
984 srcu_read_unlock(&stm_source_srcu
, idx
);
986 /* if it did change, retry */
991 static ssize_t
stm_source_link_show(struct device
*dev
,
992 struct device_attribute
*attr
,
995 struct stm_source_device
*src
= to_stm_source_device(dev
);
996 struct stm_device
*stm
;
999 idx
= srcu_read_lock(&stm_source_srcu
);
1000 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
1001 ret
= sprintf(buf
, "%s\n",
1002 stm
? dev_name(&stm
->dev
) : "<none>");
1003 srcu_read_unlock(&stm_source_srcu
, idx
);
1008 static ssize_t
stm_source_link_store(struct device
*dev
,
1009 struct device_attribute
*attr
,
1010 const char *buf
, size_t count
)
1012 struct stm_source_device
*src
= to_stm_source_device(dev
);
1013 struct stm_device
*link
;
1016 stm_source_link_drop(src
);
1018 link
= stm_find_device(buf
);
1022 pm_runtime_get(&link
->dev
);
1024 err
= stm_source_link_add(src
, link
);
1026 pm_runtime_put_autosuspend(&link
->dev
);
1027 /* matches the stm_find_device() above */
1028 stm_put_device(link
);
1031 return err
? : count
;
1034 static DEVICE_ATTR_RW(stm_source_link
);
1036 static struct attribute
*stm_source_attrs
[] = {
1037 &dev_attr_stm_source_link
.attr
,
1041 ATTRIBUTE_GROUPS(stm_source
);
1043 static struct class stm_source_class
= {
1044 .name
= "stm_source",
1045 .dev_groups
= stm_source_groups
,
1048 static void stm_source_device_release(struct device
*dev
)
1050 struct stm_source_device
*src
= to_stm_source_device(dev
);
1056 * stm_source_register_device() - register an stm_source device
1057 * @parent: parent device
1058 * @data: device description structure
1060 * This will create a device of stm_source class that can write
1061 * data to an stm device once linked.
1063 * Return: 0 on success, -errno otherwise.
1065 int stm_source_register_device(struct device
*parent
,
1066 struct stm_source_data
*data
)
1068 struct stm_source_device
*src
;
1072 return -EPROBE_DEFER
;
1074 src
= kzalloc(sizeof(*src
), GFP_KERNEL
);
1078 device_initialize(&src
->dev
);
1079 src
->dev
.class = &stm_source_class
;
1080 src
->dev
.parent
= parent
;
1081 src
->dev
.release
= stm_source_device_release
;
1083 err
= kobject_set_name(&src
->dev
.kobj
, "%s", data
->name
);
1087 pm_runtime_no_callbacks(&src
->dev
);
1088 pm_runtime_forbid(&src
->dev
);
1090 err
= device_add(&src
->dev
);
1094 stm_output_init(&src
->output
);
1095 spin_lock_init(&src
->link_lock
);
1096 INIT_LIST_HEAD(&src
->link_entry
);
1103 put_device(&src
->dev
);
1108 EXPORT_SYMBOL_GPL(stm_source_register_device
);
1111 * stm_source_unregister_device() - unregister an stm_source device
1112 * @data: device description that was used to register the device
1114 * This will remove a previously created stm_source device from the system.
1116 void stm_source_unregister_device(struct stm_source_data
*data
)
1118 struct stm_source_device
*src
= data
->src
;
1120 stm_source_link_drop(src
);
1122 device_destroy(&stm_source_class
, src
->dev
.devt
);
1124 EXPORT_SYMBOL_GPL(stm_source_unregister_device
);
1126 int notrace
stm_source_write(struct stm_source_data
*data
,
1128 const char *buf
, size_t count
)
1130 struct stm_source_device
*src
= data
->src
;
1131 struct stm_device
*stm
;
1134 if (!src
->output
.nr_chans
)
1137 if (chan
>= src
->output
.nr_chans
)
1140 idx
= srcu_read_lock(&stm_source_srcu
);
1142 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
1144 count
= stm_write(stm
->data
, src
->output
.master
,
1145 src
->output
.channel
+ chan
,
1150 srcu_read_unlock(&stm_source_srcu
, idx
);
1154 EXPORT_SYMBOL_GPL(stm_source_write
);
1156 static int __init
stm_core_init(void)
1160 err
= class_register(&stm_class
);
1164 err
= class_register(&stm_source_class
);
1168 err
= stp_configfs_init();
1172 init_srcu_struct(&stm_source_srcu
);
1179 class_unregister(&stm_source_class
);
1181 class_unregister(&stm_class
);
1186 module_init(stm_core_init
);
1188 static void __exit
stm_core_exit(void)
1190 cleanup_srcu_struct(&stm_source_srcu
);
1191 class_unregister(&stm_source_class
);
1192 class_unregister(&stm_class
);
1193 stp_configfs_exit();
1196 module_exit(stm_core_exit
);
1198 MODULE_LICENSE("GPL v2");
1199 MODULE_DESCRIPTION("System Trace Module device class");
1200 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");