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/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>
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
40 static struct srcu_struct stm_source_srcu
;
42 static ssize_t
masters_show(struct device
*dev
,
43 struct device_attribute
*attr
,
46 struct stm_device
*stm
= to_stm_device(dev
);
49 ret
= sprintf(buf
, "%u %u\n", stm
->data
->sw_start
, stm
->data
->sw_end
);
54 static DEVICE_ATTR_RO(masters
);
56 static ssize_t
channels_show(struct device
*dev
,
57 struct device_attribute
*attr
,
60 struct stm_device
*stm
= to_stm_device(dev
);
63 ret
= sprintf(buf
, "%u\n", stm
->data
->sw_nchannels
);
68 static DEVICE_ATTR_RO(channels
);
70 static struct attribute
*stm_attrs
[] = {
71 &dev_attr_masters
.attr
,
72 &dev_attr_channels
.attr
,
76 ATTRIBUTE_GROUPS(stm
);
78 static struct class stm_class
= {
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
));
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
;
110 dev
= class_find_device(&stm_class
, NULL
, buf
, stm_dev_match
);
114 stm
= to_stm_device(dev
);
115 if (!try_module_get(stm
->owner
)) {
116 /* matches class_find_device() above */
125 * stm_put_device() - drop references on the stm device
126 * @stm: stm device, previously acquired by stm_find_device()
128 * This drops the module reference and device reference taken by
129 * stm_find_device() or stm_char_open().
131 void stm_put_device(struct stm_device
*stm
)
133 module_put(stm
->owner
);
134 put_device(&stm
->dev
);
138 * Internally we only care about software-writable masters here, that is the
139 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
140 * original master numbers to be visible externally, since they are the ones
141 * that will appear in the STP stream. Thus, the internal bookkeeping uses
142 * $master - stm_data->sw_start to reference master descriptors and such.
145 #define __stm_master(_s, _m) \
146 ((_s)->masters[(_m) - (_s)->data->sw_start])
148 static inline struct stp_master
*
149 stm_master(struct stm_device
*stm
, unsigned int idx
)
151 if (idx
< stm
->data
->sw_start
|| idx
> stm
->data
->sw_end
)
154 return __stm_master(stm
, idx
);
157 static int stp_master_alloc(struct stm_device
*stm
, unsigned int idx
)
159 struct stp_master
*master
;
162 size
= ALIGN(stm
->data
->sw_nchannels
, 8) / 8;
163 size
+= sizeof(struct stp_master
);
164 master
= kzalloc(size
, GFP_ATOMIC
);
168 master
->nr_free
= stm
->data
->sw_nchannels
;
169 __stm_master(stm
, idx
) = master
;
174 static void stp_master_free(struct stm_device
*stm
, unsigned int idx
)
176 struct stp_master
*master
= stm_master(stm
, idx
);
181 __stm_master(stm
, idx
) = NULL
;
185 static void stm_output_claim(struct stm_device
*stm
, struct stm_output
*output
)
187 struct stp_master
*master
= stm_master(stm
, output
->master
);
189 lockdep_assert_held(&stm
->mc_lock
);
190 lockdep_assert_held(&output
->lock
);
192 if (WARN_ON_ONCE(master
->nr_free
< output
->nr_chans
))
195 bitmap_allocate_region(&master
->chan_map
[0], output
->channel
,
196 ilog2(output
->nr_chans
));
198 master
->nr_free
-= output
->nr_chans
;
202 stm_output_disclaim(struct stm_device
*stm
, struct stm_output
*output
)
204 struct stp_master
*master
= stm_master(stm
, output
->master
);
206 lockdep_assert_held(&stm
->mc_lock
);
207 lockdep_assert_held(&output
->lock
);
209 bitmap_release_region(&master
->chan_map
[0], output
->channel
,
210 ilog2(output
->nr_chans
));
212 output
->nr_chans
= 0;
213 master
->nr_free
+= output
->nr_chans
;
217 * This is like bitmap_find_free_region(), except it can ignore @start bits
220 static int find_free_channels(unsigned long *bitmap
, unsigned int start
,
221 unsigned int end
, unsigned int width
)
226 for (pos
= start
; pos
< end
+ 1; pos
= ALIGN(pos
, width
)) {
227 pos
= find_next_zero_bit(bitmap
, end
+ 1, pos
);
228 if (pos
+ width
> end
+ 1)
231 if (pos
& (width
- 1))
234 for (i
= 1; i
< width
&& !test_bit(pos
+ i
, bitmap
); i
++)
244 stm_find_master_chan(struct stm_device
*stm
, unsigned int width
,
245 unsigned int *mstart
, unsigned int mend
,
246 unsigned int *cstart
, unsigned int cend
)
248 struct stp_master
*master
;
252 for (midx
= *mstart
; midx
<= mend
; midx
++) {
253 if (!stm_master(stm
, midx
)) {
254 err
= stp_master_alloc(stm
, midx
);
259 master
= stm_master(stm
, midx
);
261 if (!master
->nr_free
)
264 pos
= find_free_channels(master
->chan_map
, *cstart
, cend
,
277 static int stm_output_assign(struct stm_device
*stm
, unsigned int width
,
278 struct stp_policy_node
*policy_node
,
279 struct stm_output
*output
)
281 unsigned int midx
, cidx
, mend
, cend
;
284 if (width
> stm
->data
->sw_nchannels
)
288 stp_policy_node_get_ranges(policy_node
,
289 &midx
, &mend
, &cidx
, &cend
);
291 midx
= stm
->data
->sw_start
;
293 mend
= stm
->data
->sw_end
;
294 cend
= stm
->data
->sw_nchannels
- 1;
297 spin_lock(&stm
->mc_lock
);
298 spin_lock(&output
->lock
);
299 /* output is already assigned -- shouldn't happen */
300 if (WARN_ON_ONCE(output
->nr_chans
))
303 ret
= stm_find_master_chan(stm
, width
, &midx
, mend
, &cidx
, cend
);
307 output
->master
= midx
;
308 output
->channel
= cidx
;
309 output
->nr_chans
= width
;
310 stm_output_claim(stm
, output
);
311 dev_dbg(&stm
->dev
, "assigned %u:%u (+%u)\n", midx
, cidx
, width
);
315 spin_unlock(&output
->lock
);
316 spin_unlock(&stm
->mc_lock
);
321 static void stm_output_free(struct stm_device
*stm
, struct stm_output
*output
)
323 spin_lock(&stm
->mc_lock
);
324 spin_lock(&output
->lock
);
325 if (output
->nr_chans
)
326 stm_output_disclaim(stm
, output
);
327 spin_unlock(&output
->lock
);
328 spin_unlock(&stm
->mc_lock
);
331 static void stm_output_init(struct stm_output
*output
)
333 spin_lock_init(&output
->lock
);
336 static int major_match(struct device
*dev
, const void *data
)
338 unsigned int major
= *(unsigned int *)data
;
340 return MAJOR(dev
->devt
) == major
;
343 static int stm_char_open(struct inode
*inode
, struct file
*file
)
345 struct stm_file
*stmf
;
347 unsigned int major
= imajor(inode
);
350 dev
= class_find_device(&stm_class
, NULL
, &major
, major_match
);
354 stmf
= kzalloc(sizeof(*stmf
), GFP_KERNEL
);
358 stm_output_init(&stmf
->output
);
359 stmf
->stm
= to_stm_device(dev
);
361 if (!try_module_get(stmf
->stm
->owner
))
364 file
->private_data
= stmf
;
366 return nonseekable_open(inode
, file
);
369 /* matches class_find_device() above */
376 static int stm_char_release(struct inode
*inode
, struct file
*file
)
378 struct stm_file
*stmf
= file
->private_data
;
379 struct stm_device
*stm
= stmf
->stm
;
381 if (stm
->data
->unlink
)
382 stm
->data
->unlink(stm
->data
, stmf
->output
.master
,
383 stmf
->output
.channel
);
385 stm_output_free(stm
, &stmf
->output
);
388 * matches the stm_char_open()'s
389 * class_find_device() + try_module_get()
397 static int stm_file_assign(struct stm_file
*stmf
, char *id
, unsigned int width
)
399 struct stm_device
*stm
= stmf
->stm
;
402 stmf
->policy_node
= stp_policy_node_lookup(stm
, id
);
404 ret
= stm_output_assign(stm
, width
, stmf
->policy_node
, &stmf
->output
);
406 if (stmf
->policy_node
)
407 stp_policy_node_put(stmf
->policy_node
);
412 static ssize_t
stm_write(struct stm_data
*data
, unsigned int master
,
413 unsigned int channel
, const char *buf
, size_t count
)
415 unsigned int flags
= STP_PACKET_TIMESTAMPED
;
416 const unsigned char *p
= buf
, nil
= 0;
420 for (pos
= 0, p
= buf
; count
> pos
; pos
+= sz
, p
+= sz
) {
421 sz
= min_t(unsigned int, count
- pos
, 8);
422 sz
= data
->packet(data
, master
, channel
, STP_PACKET_DATA
, flags
,
430 data
->packet(data
, master
, channel
, STP_PACKET_FLAG
, 0, 0, &nil
);
435 static ssize_t
stm_char_write(struct file
*file
, const char __user
*buf
,
436 size_t count
, loff_t
*ppos
)
438 struct stm_file
*stmf
= file
->private_data
;
439 struct stm_device
*stm
= stmf
->stm
;
443 if (count
+ 1 > PAGE_SIZE
)
444 count
= PAGE_SIZE
- 1;
447 * if no m/c have been assigned to this writer up to this
448 * point, use "default" policy entry
450 if (!stmf
->output
.nr_chans
) {
451 err
= stm_file_assign(stmf
, "default", 1);
453 * EBUSY means that somebody else just assigned this
454 * output, which is just fine for write()
456 if (err
&& err
!= -EBUSY
)
460 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
464 err
= copy_from_user(kbuf
, buf
, count
);
470 count
= stm_write(stm
->data
, stmf
->output
.master
, stmf
->output
.channel
,
478 static int stm_char_mmap(struct file
*file
, struct vm_area_struct
*vma
)
480 struct stm_file
*stmf
= file
->private_data
;
481 struct stm_device
*stm
= stmf
->stm
;
482 unsigned long size
, phys
;
484 if (!stm
->data
->mmio_addr
)
490 size
= vma
->vm_end
- vma
->vm_start
;
492 if (stmf
->output
.nr_chans
* stm
->data
->sw_mmiosz
!= size
)
495 phys
= stm
->data
->mmio_addr(stm
->data
, stmf
->output
.master
,
496 stmf
->output
.channel
,
497 stmf
->output
.nr_chans
);
502 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
503 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
504 vm_iomap_memory(vma
, phys
, size
);
509 static int stm_char_policy_set_ioctl(struct stm_file
*stmf
, void __user
*arg
)
511 struct stm_device
*stm
= stmf
->stm
;
512 struct stp_policy_id
*id
;
516 if (stmf
->output
.nr_chans
)
519 if (copy_from_user(&size
, arg
, sizeof(size
)))
522 if (size
>= PATH_MAX
+ sizeof(*id
))
526 * size + 1 to make sure the .id string at the bottom is terminated,
527 * which is also why memdup_user() is not useful here
529 id
= kzalloc(size
+ 1, GFP_KERNEL
);
533 if (copy_from_user(id
, arg
, size
)) {
538 if (id
->__reserved_0
|| id
->__reserved_1
)
542 id
->width
> PAGE_SIZE
/ stm
->data
->sw_mmiosz
)
545 ret
= stm_file_assign(stmf
, id
->id
, id
->width
);
552 ret
= stm
->data
->link(stm
->data
, stmf
->output
.master
,
553 stmf
->output
.channel
);
556 stm_output_free(stmf
->stm
, &stmf
->output
);
564 static int stm_char_policy_get_ioctl(struct stm_file
*stmf
, void __user
*arg
)
566 struct stp_policy_id id
= {
568 .master
= stmf
->output
.master
,
569 .channel
= stmf
->output
.channel
,
570 .width
= stmf
->output
.nr_chans
,
575 return copy_to_user(arg
, &id
, id
.size
) ? -EFAULT
: 0;
579 stm_char_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
581 struct stm_file
*stmf
= file
->private_data
;
582 struct stm_data
*stm_data
= stmf
->stm
->data
;
587 case STP_POLICY_ID_SET
:
588 err
= stm_char_policy_set_ioctl(stmf
, (void __user
*)arg
);
592 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
594 case STP_POLICY_ID_GET
:
595 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
597 case STP_SET_OPTIONS
:
598 if (copy_from_user(&options
, (u64 __user
*)arg
, sizeof(u64
)))
601 if (stm_data
->set_options
)
602 err
= stm_data
->set_options(stm_data
,
604 stmf
->output
.channel
,
605 stmf
->output
.nr_chans
,
618 stm_char_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
620 return stm_char_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
623 #define stm_char_compat_ioctl NULL
626 static const struct file_operations stm_fops
= {
627 .open
= stm_char_open
,
628 .release
= stm_char_release
,
629 .write
= stm_char_write
,
630 .mmap
= stm_char_mmap
,
631 .unlocked_ioctl
= stm_char_ioctl
,
632 .compat_ioctl
= stm_char_compat_ioctl
,
636 static void stm_device_release(struct device
*dev
)
638 struct stm_device
*stm
= to_stm_device(dev
);
643 int stm_register_device(struct device
*parent
, struct stm_data
*stm_data
,
644 struct module
*owner
)
646 struct stm_device
*stm
;
647 unsigned int nmasters
;
651 return -EPROBE_DEFER
;
653 if (!stm_data
->packet
|| !stm_data
->sw_nchannels
)
656 nmasters
= stm_data
->sw_end
- stm_data
->sw_start
+ 1;
657 stm
= kzalloc(sizeof(*stm
) + nmasters
* sizeof(void *), GFP_KERNEL
);
661 stm
->major
= register_chrdev(0, stm_data
->name
, &stm_fops
);
665 device_initialize(&stm
->dev
);
666 stm
->dev
.devt
= MKDEV(stm
->major
, 0);
667 stm
->dev
.class = &stm_class
;
668 stm
->dev
.parent
= parent
;
669 stm
->dev
.release
= stm_device_release
;
671 err
= kobject_set_name(&stm
->dev
.kobj
, "%s", stm_data
->name
);
675 err
= device_add(&stm
->dev
);
679 mutex_init(&stm
->link_mutex
);
680 spin_lock_init(&stm
->link_lock
);
681 INIT_LIST_HEAD(&stm
->link_list
);
683 spin_lock_init(&stm
->mc_lock
);
684 mutex_init(&stm
->policy_mutex
);
685 stm
->sw_nmasters
= nmasters
;
687 stm
->data
= stm_data
;
693 /* matches device_initialize() above */
694 put_device(&stm
->dev
);
700 EXPORT_SYMBOL_GPL(stm_register_device
);
702 static int __stm_source_link_drop(struct stm_source_device
*src
,
703 struct stm_device
*stm
);
705 void stm_unregister_device(struct stm_data
*stm_data
)
707 struct stm_device
*stm
= stm_data
->stm
;
708 struct stm_source_device
*src
, *iter
;
711 mutex_lock(&stm
->link_mutex
);
712 list_for_each_entry_safe(src
, iter
, &stm
->link_list
, link_entry
) {
713 ret
= __stm_source_link_drop(src
, stm
);
715 * src <-> stm link must not change under the same
716 * stm::link_mutex, so complain loudly if it has;
717 * also in this situation ret!=0 means this src is
718 * not connected to this stm and it should be otherwise
719 * safe to proceed with the tear-down of stm.
723 mutex_unlock(&stm
->link_mutex
);
725 synchronize_srcu(&stm_source_srcu
);
727 unregister_chrdev(stm
->major
, stm_data
->name
);
729 mutex_lock(&stm
->policy_mutex
);
731 stp_policy_unbind(stm
->policy
);
732 mutex_unlock(&stm
->policy_mutex
);
734 for (i
= stm
->data
->sw_start
; i
<= stm
->data
->sw_end
; i
++)
735 stp_master_free(stm
, i
);
737 device_unregister(&stm
->dev
);
738 stm_data
->stm
= NULL
;
740 EXPORT_SYMBOL_GPL(stm_unregister_device
);
743 * stm::link_list access serialization uses a spinlock and a mutex; holding
744 * either of them guarantees that the list is stable; modification requires
745 * holding both of them.
747 * Lock ordering is as follows:
754 * stm_source_link_add() - connect an stm_source device to an stm device
755 * @src: stm_source device
758 * This function establishes a link from stm_source to an stm device so that
759 * the former can send out trace data to the latter.
761 * Return: 0 on success, -errno otherwise.
763 static int stm_source_link_add(struct stm_source_device
*src
,
764 struct stm_device
*stm
)
769 mutex_lock(&stm
->link_mutex
);
770 spin_lock(&stm
->link_lock
);
771 spin_lock(&src
->link_lock
);
773 /* src->link is dereferenced under stm_source_srcu but not the list */
774 rcu_assign_pointer(src
->link
, stm
);
775 list_add_tail(&src
->link_entry
, &stm
->link_list
);
777 spin_unlock(&src
->link_lock
);
778 spin_unlock(&stm
->link_lock
);
779 mutex_unlock(&stm
->link_mutex
);
781 id
= kstrdup(src
->data
->name
, GFP_KERNEL
);
784 stp_policy_node_lookup(stm
, id
);
789 err
= stm_output_assign(stm
, src
->data
->nr_chans
,
790 src
->policy_node
, &src
->output
);
792 if (src
->policy_node
)
793 stp_policy_node_put(src
->policy_node
);
798 /* this is to notify the STM device that a new link has been made */
800 err
= stm
->data
->link(stm
->data
, src
->output
.master
,
801 src
->output
.channel
);
804 goto fail_free_output
;
806 /* this is to let the source carry out all necessary preparations */
808 src
->data
->link(src
->data
);
813 stm_output_free(stm
, &src
->output
);
816 mutex_lock(&stm
->link_mutex
);
817 spin_lock(&stm
->link_lock
);
818 spin_lock(&src
->link_lock
);
820 rcu_assign_pointer(src
->link
, NULL
);
821 list_del_init(&src
->link_entry
);
823 spin_unlock(&src
->link_lock
);
824 spin_unlock(&stm
->link_lock
);
825 mutex_unlock(&stm
->link_mutex
);
831 * __stm_source_link_drop() - detach stm_source from an stm device
832 * @src: stm_source device
835 * If @stm is @src::link, disconnect them from one another and put the
836 * reference on the @stm device.
838 * Caller must hold stm::link_mutex.
840 static int __stm_source_link_drop(struct stm_source_device
*src
,
841 struct stm_device
*stm
)
843 struct stm_device
*link
;
846 lockdep_assert_held(&stm
->link_mutex
);
848 /* for stm::link_list modification, we hold both mutex and spinlock */
849 spin_lock(&stm
->link_lock
);
850 spin_lock(&src
->link_lock
);
851 link
= srcu_dereference_check(src
->link
, &stm_source_srcu
, 1);
854 * The linked device may have changed since we last looked, because
855 * we weren't holding the src::link_lock back then; if this is the
856 * case, tell the caller to retry.
863 stm_output_free(link
, &src
->output
);
864 list_del_init(&src
->link_entry
);
865 /* matches stm_find_device() from stm_source_link_store() */
866 stm_put_device(link
);
867 rcu_assign_pointer(src
->link
, NULL
);
870 spin_unlock(&src
->link_lock
);
871 spin_unlock(&stm
->link_lock
);
874 * Call the unlink callbacks for both source and stm, when we know
875 * that we have actually performed the unlinking.
878 if (src
->data
->unlink
)
879 src
->data
->unlink(src
->data
);
881 if (stm
->data
->unlink
)
882 stm
->data
->unlink(stm
->data
, src
->output
.master
,
883 src
->output
.channel
);
890 * stm_source_link_drop() - detach stm_source from its stm device
891 * @src: stm_source device
893 * Unlinking means disconnecting from source's STM device; after this
894 * writes will be unsuccessful until it is linked to a new STM device.
896 * This will happen on "stm_source_link" sysfs attribute write to undo
897 * the existing link (if any), or on linked STM device's de-registration.
899 static void stm_source_link_drop(struct stm_source_device
*src
)
901 struct stm_device
*stm
;
905 idx
= srcu_read_lock(&stm_source_srcu
);
907 * The stm device will be valid for the duration of this
908 * read section, but the link may change before we grab
909 * the src::link_lock in __stm_source_link_drop().
911 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
915 mutex_lock(&stm
->link_mutex
);
916 ret
= __stm_source_link_drop(src
, stm
);
917 mutex_unlock(&stm
->link_mutex
);
920 srcu_read_unlock(&stm_source_srcu
, idx
);
922 /* if it did change, retry */
927 static ssize_t
stm_source_link_show(struct device
*dev
,
928 struct device_attribute
*attr
,
931 struct stm_source_device
*src
= to_stm_source_device(dev
);
932 struct stm_device
*stm
;
935 idx
= srcu_read_lock(&stm_source_srcu
);
936 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
937 ret
= sprintf(buf
, "%s\n",
938 stm
? dev_name(&stm
->dev
) : "<none>");
939 srcu_read_unlock(&stm_source_srcu
, idx
);
944 static ssize_t
stm_source_link_store(struct device
*dev
,
945 struct device_attribute
*attr
,
946 const char *buf
, size_t count
)
948 struct stm_source_device
*src
= to_stm_source_device(dev
);
949 struct stm_device
*link
;
952 stm_source_link_drop(src
);
954 link
= stm_find_device(buf
);
958 err
= stm_source_link_add(src
, link
);
960 /* matches the stm_find_device() above */
961 stm_put_device(link
);
964 return err
? : count
;
967 static DEVICE_ATTR_RW(stm_source_link
);
969 static struct attribute
*stm_source_attrs
[] = {
970 &dev_attr_stm_source_link
.attr
,
974 ATTRIBUTE_GROUPS(stm_source
);
976 static struct class stm_source_class
= {
977 .name
= "stm_source",
978 .dev_groups
= stm_source_groups
,
981 static void stm_source_device_release(struct device
*dev
)
983 struct stm_source_device
*src
= to_stm_source_device(dev
);
989 * stm_source_register_device() - register an stm_source device
990 * @parent: parent device
991 * @data: device description structure
993 * This will create a device of stm_source class that can write
994 * data to an stm device once linked.
996 * Return: 0 on success, -errno otherwise.
998 int stm_source_register_device(struct device
*parent
,
999 struct stm_source_data
*data
)
1001 struct stm_source_device
*src
;
1005 return -EPROBE_DEFER
;
1007 src
= kzalloc(sizeof(*src
), GFP_KERNEL
);
1011 device_initialize(&src
->dev
);
1012 src
->dev
.class = &stm_source_class
;
1013 src
->dev
.parent
= parent
;
1014 src
->dev
.release
= stm_source_device_release
;
1016 err
= kobject_set_name(&src
->dev
.kobj
, "%s", data
->name
);
1020 err
= device_add(&src
->dev
);
1024 stm_output_init(&src
->output
);
1025 spin_lock_init(&src
->link_lock
);
1026 INIT_LIST_HEAD(&src
->link_entry
);
1033 put_device(&src
->dev
);
1038 EXPORT_SYMBOL_GPL(stm_source_register_device
);
1041 * stm_source_unregister_device() - unregister an stm_source device
1042 * @data: device description that was used to register the device
1044 * This will remove a previously created stm_source device from the system.
1046 void stm_source_unregister_device(struct stm_source_data
*data
)
1048 struct stm_source_device
*src
= data
->src
;
1050 stm_source_link_drop(src
);
1052 device_destroy(&stm_source_class
, src
->dev
.devt
);
1054 EXPORT_SYMBOL_GPL(stm_source_unregister_device
);
1056 int stm_source_write(struct stm_source_data
*data
, unsigned int chan
,
1057 const char *buf
, size_t count
)
1059 struct stm_source_device
*src
= data
->src
;
1060 struct stm_device
*stm
;
1063 if (!src
->output
.nr_chans
)
1066 if (chan
>= src
->output
.nr_chans
)
1069 idx
= srcu_read_lock(&stm_source_srcu
);
1071 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
1073 count
= stm_write(stm
->data
, src
->output
.master
,
1074 src
->output
.channel
+ chan
,
1079 srcu_read_unlock(&stm_source_srcu
, idx
);
1083 EXPORT_SYMBOL_GPL(stm_source_write
);
1085 static int __init
stm_core_init(void)
1089 err
= class_register(&stm_class
);
1093 err
= class_register(&stm_source_class
);
1097 err
= stp_configfs_init();
1101 init_srcu_struct(&stm_source_srcu
);
1108 class_unregister(&stm_source_class
);
1110 class_unregister(&stm_class
);
1115 module_init(stm_core_init
);
1117 static void __exit
stm_core_exit(void)
1119 cleanup_srcu_struct(&stm_source_srcu
);
1120 class_unregister(&stm_source_class
);
1121 class_unregister(&stm_class
);
1122 stp_configfs_exit();
1125 module_exit(stm_core_exit
);
1127 MODULE_LICENSE("GPL v2");
1128 MODULE_DESCRIPTION("System Trace Module device class");
1129 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");