aboutsummaryrefslogtreecommitdiff
path: root/drivers/ata/sata_phy.c
blob: e5631a97951f8e3876937d39c053e5b5b414b3fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2010-2012 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com
 *
 * EXYNOS - SATA utility framework.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <linux/kernel.h>
#include <linux/export.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/list.h>
#include "sata_phy.h"

static LIST_HEAD(phy_list);
static DEFINE_SPINLOCK(phy_lock);

struct sata_phy *sata_get_phy(enum sata_phy_type type)
{
	struct sata_phy *x = NULL;
	unsigned long flag;

	if (list_empty(&phy_list))
		return x;

	spin_lock_irqsave(&phy_lock, flag);

	list_for_each_entry(x, &phy_list, head) {
		if (x->type == type) {
			get_device(x->dev);
			break;
		}
	}

	spin_unlock_irqrestore(&phy_lock, flag);
	return x;
}
EXPORT_SYMBOL(sata_get_phy);

int sata_add_phy(struct sata_phy *phy, enum sata_phy_type type)
{
	unsigned long flag;
	unsigned int ret = -EINVAL;
	struct sata_phy *x;

	spin_lock_irqsave(&phy_lock, flag);

	if (!phy)
		return ret;

	list_for_each_entry(x, &phy_list, head) {
		if (x->type == type) {
			dev_err(phy->dev, "transceiver type already exists\n");
			goto out;
		}
	}
	phy->type = type;
	list_add_tail(&phy->head, &phy_list);
	ret = 0;

 out:
	spin_unlock_irqrestore(&phy_lock, flag);
	return ret;
}
EXPORT_SYMBOL(sata_add_phy);

void sata_remove_phy(struct sata_phy *phy)
{
	unsigned long flag;
	struct sata_phy *x;

	spin_lock_irqsave(&phy_lock, flag);

	if (!phy)
		return;

	list_for_each_entry(x, &phy_list, head) {
		if (x->type == phy->type)
			list_del(&phy->head);
	}

	spin_unlock_irqrestore(&phy_lock, flag);
}
EXPORT_SYMBOL(sata_remove_phy);

void sata_put_phy(struct sata_phy *phy)
{
	unsigned long flag;

	spin_lock_irqsave(&phy_lock, flag);

	if (!phy)
		return;

	put_device(phy->dev);
	spin_unlock_irqrestore(&phy_lock, flag);

}
EXPORT_SYMBOL(sata_put_phy);