aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-10-01 15:10:44 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-10-01 15:10:44 -0400
commit06d2fe153b9b35e57221e35831a26918f462db68 (patch)
treef77cb72dfba7f2a47ceb93e120abd9399a24a1b9
parent3aebd34b1200a902a8662da8845824a02f00772e (diff)
parente0f21e6d52cc245e7d4f7e02ca4b7b6571660ec2 (diff)
Merge tag 'driver-core-3.6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core
Pull driver core merge from Greg Kroah-Hartman: "Here is the big driver core update for 3.7-rc1. A number of firmware_class.c updates (as you saw a month or so ago), and some hyper-v updates and some printk fixes as well. All patches that are outside of the drivers/base area have been acked by the respective maintainers, and have all been in the linux-next tree for a while. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>" * tag 'driver-core-3.6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core: (95 commits) memory: tegra{20,30}-mc: Fix reading incorrect register in mc_readl() device.h: Add missing inline to #ifndef CONFIG_PRINTK dev_vprintk_emit memory: emif: Add ifdef CONFIG_DEBUG_FS guard for emif_debugfs_[init|exit] Documentation: Fixes some translation error in Documentation/zh_CN/gpio.txt Documentation: Remove 3 byte redundant code at the head of the Documentation/zh_CN/arm/booting Documentation: Chinese translation of Documentation/video4linux/omap3isp.txt device and dynamic_debug: Use dev_vprintk_emit and dev_printk_emit dev: Add dev_vprintk_emit and dev_printk_emit netdev_printk/netif_printk: Remove a superfluous logging colon netdev_printk/dynamic_netdev_dbg: Directly call printk_emit dev_dbg/dynamic_debug: Update to use printk_emit, optimize stack driver-core: Shut up dev_dbg_reatelimited() without DEBUG tools/hv: Parse /etc/os-release tools/hv: Check for read/write errors tools/hv: Fix exit() error code tools/hv: Fix file handle leak Tools: hv: Implement the KVP verb - KVP_OP_GET_IP_INFO Tools: hv: Rename the function kvp_get_ip_address() Tools: hv: Implement the KVP verb - KVP_OP_SET_IP_INFO Tools: hv: Add an example script to configure an interface ...
-rw-r--r--Documentation/ABI/testing/sysfs-class-extcon22
-rw-r--r--Documentation/filesystems/debugfs.txt4
-rw-r--r--Documentation/kobject.txt6
-rw-r--r--Documentation/zh_CN/arm/Booting175
-rw-r--r--Documentation/zh_CN/basic_profiling.txt71
-rw-r--r--Documentation/zh_CN/filesystems/sysfs.txt372
-rw-r--r--Documentation/zh_CN/gpio.txt658
-rw-r--r--Documentation/zh_CN/video4linux/omap3isp.txt277
-rw-r--r--Documentation/zh_CN/video4linux/v4l2-framework.txt983
-rw-r--r--MAINTAINERS4
-rw-r--r--arch/tile/Kconfig8
-rw-r--r--arch/xtensa/Kconfig18
-rw-r--r--drivers/base/core.c105
-rw-r--r--drivers/base/devres.c42
-rw-r--r--drivers/base/firmware_class.c838
-rw-r--r--drivers/base/platform.c41
-rw-r--r--drivers/base/power/main.c22
-rw-r--r--drivers/extcon/Kconfig8
-rw-r--r--drivers/extcon/Makefile5
-rw-r--r--drivers/extcon/extcon-adc-jack.c198
-rw-r--r--drivers/extcon/extcon-arizona.c78
-rw-r--r--drivers/extcon/extcon-class.c (renamed from drivers/extcon/extcon_class.c)12
-rw-r--r--drivers/extcon/extcon-gpio.c (renamed from drivers/extcon/extcon_gpio.c)0
-rw-r--r--drivers/extcon/extcon-max77693.c8
-rw-r--r--drivers/firmware/efivars.c17
-rw-r--r--drivers/hv/hv.c34
-rw-r--r--drivers/hv/hv_kvp.c253
-rw-r--r--drivers/hv/hv_util.c4
-rw-r--r--drivers/hv/hyperv_vmbus.h47
-rw-r--r--drivers/hv/vmbus_drv.c42
-rw-r--r--drivers/memory/Makefile3
-rw-r--r--drivers/memory/emif.c205
-rw-r--r--drivers/memory/of_memory.c153
-rw-r--r--drivers/memory/of_memory.h36
-rw-r--r--drivers/memory/tegra20-mc.c10
-rw-r--r--drivers/memory/tegra30-mc.c22
-rw-r--r--drivers/vme/bridges/vme_ca91cx42.c20
-rw-r--r--drivers/vme/bridges/vme_tsi148.c10
-rw-r--r--fs/debugfs/inode.c8
-rw-r--r--fs/sysfs/symlink.c2
-rw-r--r--include/linux/device.h85
-rw-r--r--include/linux/extcon.h18
-rw-r--r--include/linux/extcon/extcon-adc-jack.h71
-rw-r--r--include/linux/firmware.h15
-rw-r--r--include/linux/hyperv.h101
-rw-r--r--include/linux/netdevice.h3
-rw-r--r--include/linux/platform_device.h4
-rw-r--r--include/linux/pm.h5
-rw-r--r--init/Kconfig8
-rw-r--r--lib/dynamic_debug.c56
-rw-r--r--net/core/dev.c20
-rwxr-xr-xtools/hv/hv_get_dhcp_info.sh28
-rwxr-xr-xtools/hv/hv_get_dns_info.sh13
-rw-r--r--tools/hv/hv_kvp_daemon.c1036
-rwxr-xr-xtools/hv/hv_set_ifconfig.sh68
55 files changed, 5807 insertions, 545 deletions
diff --git a/Documentation/ABI/testing/sysfs-class-extcon b/Documentation/ABI/testing/sysfs-class-extcon
index 20ab361bd8c..57a72623291 100644
--- a/Documentation/ABI/testing/sysfs-class-extcon
+++ b/Documentation/ABI/testing/sysfs-class-extcon
@@ -13,7 +13,7 @@ Description:
13 accessory cables have such capability. For example, 13 accessory cables have such capability. For example,
14 the 30-pin port of Nuri board (/arch/arm/mach-exynos) 14 the 30-pin port of Nuri board (/arch/arm/mach-exynos)
15 may have both HDMI and Charger attached, or analog audio, 15 may have both HDMI and Charger attached, or analog audio,
16 video, and USB cables attached simulteneously. 16 video, and USB cables attached simultaneously.
17 17
18 If there are cables mutually exclusive with each other, 18 If there are cables mutually exclusive with each other,
19 such binary relations may be expressed with extcon_dev's 19 such binary relations may be expressed with extcon_dev's
@@ -35,7 +35,7 @@ Description:
35 The /sys/class/extcon/.../state shows and stores the cable 35 The /sys/class/extcon/.../state shows and stores the cable
36 attach/detach information of the corresponding extcon object. 36 attach/detach information of the corresponding extcon object.
37 If the extcon object has an optional callback "show_state" 37 If the extcon object has an optional callback "show_state"
38 defined, the showing function is overriden with the optional 38 defined, the showing function is overridden with the optional
39 callback. 39 callback.
40 40
41 If the default callback for showing function is used, the 41 If the default callback for showing function is used, the
@@ -46,19 +46,19 @@ Description:
46 TA=1 46 TA=1
47 EAR_JACK=0 47 EAR_JACK=0
48 # 48 #
49 In this example, the extcon device have USB_OTG and TA 49 In this example, the extcon device has USB_OTG and TA
50 cables attached and HDMI and EAR_JACK cables detached. 50 cables attached and HDMI and EAR_JACK cables detached.
51 51
52 In order to update the state of an extcon device, enter a hex 52 In order to update the state of an extcon device, enter a hex
53 state number starting with 0x. 53 state number starting with 0x:
54 echo 0xHEX > state 54 # echo 0xHEX > state
55 55
56 This updates the whole state of the extcon dev. 56 This updates the whole state of the extcon device.
57 Inputs of all the methods are required to meet the 57 Inputs of all the methods are required to meet the
58 mutually_exclusive contidions if they exist. 58 mutually_exclusive conditions if they exist.
59 59
60 It is recommended to use this "global" state interface if 60 It is recommended to use this "global" state interface if
61 you need to enter the value atomically. The later state 61 you need to set the value atomically. The later state
62 interface associated with each cable cannot update 62 interface associated with each cable cannot update
63 multiple cable states of an extcon device simultaneously. 63 multiple cable states of an extcon device simultaneously.
64 64
@@ -73,7 +73,7 @@ What: /sys/class/extcon/.../cable.x/state
73Date: February 2012 73Date: February 2012
74Contact: MyungJoo Ham <myungjoo.ham@samsung.com> 74Contact: MyungJoo Ham <myungjoo.ham@samsung.com>
75Description: 75Description:
76 The /sys/class/extcon/.../cable.x/name shows and stores the 76 The /sys/class/extcon/.../cable.x/state shows and stores the
77 state of cable "x" (integer between 0 and 31) of an extcon 77 state of cable "x" (integer between 0 and 31) of an extcon
78 device. The state value is either 0 (detached) or 1 78 device. The state value is either 0 (detached) or 1
79 (attached). 79 (attached).
@@ -83,8 +83,8 @@ Date: December 2011
83Contact: MyungJoo Ham <myungjoo.ham@samsung.com> 83Contact: MyungJoo Ham <myungjoo.ham@samsung.com>
84Description: 84Description:
85 Shows the relations of mutually exclusiveness. For example, 85 Shows the relations of mutually exclusiveness. For example,
86 if the mutually_exclusive array of extcon_dev is 86 if the mutually_exclusive array of extcon device is
87 {0x3, 0x5, 0xC, 0x0}, the, the output is: 87 {0x3, 0x5, 0xC, 0x0}, then the output is:
88 # ls mutually_exclusive/ 88 # ls mutually_exclusive/
89 0x3 89 0x3
90 0x5 90 0x5
diff --git a/Documentation/filesystems/debugfs.txt b/Documentation/filesystems/debugfs.txt
index 7a34f827989..3a863f69272 100644
--- a/Documentation/filesystems/debugfs.txt
+++ b/Documentation/filesystems/debugfs.txt
@@ -15,8 +15,8 @@ Debugfs is typically mounted with a command like:
15 mount -t debugfs none /sys/kernel/debug 15 mount -t debugfs none /sys/kernel/debug
16 16
17(Or an equivalent /etc/fstab line). 17(Or an equivalent /etc/fstab line).
18The debugfs root directory is accessible by anyone by default. To 18The debugfs root directory is accessible only to the root user by
19restrict access to the tree the "uid", "gid" and "mode" mount 19default. To change access to the tree the "uid", "gid" and "mode" mount
20options can be used. 20options can be used.
21 21
22Note that the debugfs API is exported GPL-only to modules. 22Note that the debugfs API is exported GPL-only to modules.
diff --git a/Documentation/kobject.txt b/Documentation/kobject.txt
index 49578cf1aea..c5182bb2c16 100644
--- a/Documentation/kobject.txt
+++ b/Documentation/kobject.txt
@@ -284,9 +284,11 @@ instead, it is associated with the ktype. So let us introduce struct
284kobj_type: 284kobj_type:
285 285
286 struct kobj_type { 286 struct kobj_type {
287 void (*release)(struct kobject *); 287 void (*release)(struct kobject *kobj);
288 const struct sysfs_ops *sysfs_ops; 288 const struct sysfs_ops *sysfs_ops;
289 struct attribute **default_attrs; 289 struct attribute **default_attrs;
290 const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
291 const void *(*namespace)(struct kobject *kobj);
290 }; 292 };
291 293
292This structure is used to describe a particular type of kobject (or, more 294This structure is used to describe a particular type of kobject (or, more
diff --git a/Documentation/zh_CN/arm/Booting b/Documentation/zh_CN/arm/Booting
new file mode 100644
index 00000000000..6158a64df80
--- /dev/null
+++ b/Documentation/zh_CN/arm/Booting
@@ -0,0 +1,175 @@
1Chinese translated version of Documentation/arm/Booting
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Maintainer: Russell King <linux@arm.linux.org.uk>
10Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
11---------------------------------------------------------------------
12Documentation/arm/Booting 的中文翻译
13
14如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
15交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
16译存在问题,请联系中文版维护者。
17
18英文版维护者: Russell King <linux@arm.linux.org.uk>
19中文版维护者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
20中文版翻译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
21中文版校译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
22
23以下为正文
24---------------------------------------------------------------------
25
26 启动 ARM Linux
27 ==============
28
29作者:Russell King
30日期:2002年5月18日
31
32以下文档适用于 2.4.18-rmk6 及以上版本。
33
34为了启动 ARM Linux,你需要一个引导装载程序(boot loader),
35它是一个在主内核启动前运行的一个小程序。引导装载程序需要初始化各种
36设备,并最终调用 Linux 内核,将信息传递给内核。
37
38从本质上讲,引导装载程序应提供(至少)以下功能:
39
401、设置和初始化 RAM。
412、初始化一个串口。
423、检测机器的类型(machine type)。
434、设置内核标签列表(tagged list)。
445、调用内核映像。
45
46
471、设置和初始化 RAM
48-------------------
49
50现有的引导加载程序: 强制
51新开发的引导加载程序: 强制
52
53引导装载程序应该找到并初始化系统中所有内核用于保持系统变量数据的 RAM。
54这个操作的执行是设备依赖的。(它可能使用内部算法来自动定位和计算所有
55RAM,或可能使用对这个设备已知的 RAM 信息,还可能使用任何引导装载程序
56设计者想到的匹配方法。)
57
58
592、初始化一个串口
60-----------------------------
61
62现有的引导加载程序: 可选、建议
63新开发的引导加载程序: 可选、建议
64
65引导加载程序应该初始化并使能一个目标板上的串口。这允许内核串口驱动
66自动检测哪个串口用于内核控制台。(一般用于调试或与目标板通信。)
67
68作为替代方案,引导加载程序也可以通过标签列表传递相关的'console='
69选项给内核以指定某个串口,而串口数据格式的选项在以下文档中描述:
70
71 Documentation/kernel-parameters.txt。
72
73
743、检测机器类型
75--------------------------
76
77现有的引导加载程序: 可选
78新开发的引导加载程序: 强制
79
80引导加载程序应该通过某些方式检测自身所处的机器类型。这是一个硬件
81代码或通过查看所连接的硬件用某些算法得到,这些超出了本文档的范围。
82引导加载程序最终必须能提供一个 MACH_TYPE_xxx 值给内核。
83(详见 linux/arch/arm/tools/mach-types )。
84
854、设置启动数据
86------------------
87
88现有的引导加载程序: 可选、强烈建议
89新开发的引导加载程序: 强制
90
91引导加载程序必须提供标签列表或者 dtb 映像以传递配置数据给内核。启动
92数据的物理地址通过寄存器 r2 传递给内核。
93
944a、设置内核标签列表
95--------------------------------
96
97bootloader 必须创建和初始化内核标签列表。一个有效的标签列表以
98ATAG_CORE 标签开始,并以 ATAG_NONE 标签结束。ATAG_CORE 标签可以是
99空的,也可以是非空。一个空 ATAG_CORE 标签其 size 域设置为
100‘2’(0x00000002)。ATAG_NONE 标签的 size 域必须设置为零。
101
102在列表中可以保存任意数量的标签。对于一个重复的标签是追加到之前标签
103所携带的信息之后,还是会覆盖原来的信息,是未定义的。某些标签的行为
104是前者,其他是后者。
105
106bootloader 必须传递一个系统内存的位置和最小值,以及根文件系统位置。
107因此,最小的标签列表如下所示:
108
109 +-----------+
110基地址 -> | ATAG_CORE | |
111 +-----------+ |
112 | ATAG_MEM | | 地址增长方向
113 +-----------+ |
114 | ATAG_NONE | |
115 +-----------+ v
116
117标签列表应该保存在系统的 RAM 中。
118
119标签列表必须置于内核自解压和 initrd'bootp' 程序都不会覆盖的内存区。
120建议放在 RAM 的头 16KiB 中。
121
1224b、设置设备树
123-------------------------
124
125bootloader 必须以 64bit 地址对齐的形式加载一个设备树映像(dtb)到系统
126RAM 中,并用启动数据初始化它。dtb 格式在文档
127Documentation/devicetree/booting-without-of.txt 中。内核将会在
128dtb 物理地址处查找 dtb 魔数值(0xd00dfeed),以确定 dtb 是否已经代替
129标签列表被传递进来。
130
131bootloader 必须传递一个系统内存的位置和最小值,以及根文件系统位置。
132dtb 必须置于内核自解压不会覆盖的内存区。建议将其放置于 RAM 的头 16KiB
133中。但是不可将其放置于“0”物理地址处,因为内核认为:r2 中为 0,意味着
134没有标签列表和 dtb 传递过来。
135
1365、调用内核映像
137---------------------------
138
139现有的引导加载程序: 强制
140新开发的引导加载程序: 强制
141
142调用内核映像 zImage 有两个选择。如果 zImge 保存在 flash 中,且是为了
143在 flash 中直接运行而被正确链接的。这样引导加载程序就可以在 flash 中
144直接调用 zImage。
145
146zImage 也可以被放在系统 RAM(任意位置)中被调用。注意:内核使用映像
147基地址的前 16KB RAM 空间来保存页表。建议将映像置于 RAM 的 32KB 处。
148
149对于以上任意一种情况,都必须符合以下启动状态:
150
151- 停止所有 DMA 设备,这样内存数据就不会因为虚假网络包或磁盘数据而被破坏。
152 这可能可以节省你许多的调试时间。
153
154- CPU 寄存器配置
155 r0 = 0,
156 r1 = (在上面 3 中获取的)机器类型码。
157 r2 = 标签列表在系统 RAM 中的物理地址,或
158 设备树块(dtb)在系统 RAM 中的物理地址
159
160- CPU 模式
161 所有形式的中断必须被禁止 (IRQs 和 FIQs)
162 CPU 必须处于 SVC 模式。(对于 Angel 调试有特例存在)
163
164- 缓存,MMUs
165 MMU 必须关闭。
166 指令缓存开启或关闭都可以。
167 数据缓存必须关闭。
168
169- 引导加载程序应该通过直接跳转到内核映像的第一条指令来调用内核映像。
170
171 对于支持 ARM 指令集的 CPU,跳入内核入口时必须处在 ARM 状态,即使
172 对于 Thumb-2 内核也是如此。
173
174 对于仅支持 Thumb 指令集的 CPU,比如 Cortex-M 系列的 CPU,跳入
175 内核入口时必须处于 Thumb 状态。
diff --git a/Documentation/zh_CN/basic_profiling.txt b/Documentation/zh_CN/basic_profiling.txt
new file mode 100644
index 00000000000..1e6bf0bdf8f
--- /dev/null
+++ b/Documentation/zh_CN/basic_profiling.txt
@@ -0,0 +1,71 @@
1Chinese translated version of Documentation/basic_profiling
2
3If you have any comment or update to the content, please post to LKML directly.
4However, if you have problem communicating in English you can also ask the
5Chinese maintainer for help. Contact the Chinese maintainer, if this
6translation is outdated or there is problem with translation.
7
8Chinese maintainer: Liang Xie <xieliang@xiaomi.com>
9---------------------------------------------------------------------
10Documentation/basic_profiling的中文翻译
11
12如果想评论或更新本文的内容,请直接发信到LKML。如果你使用英文交流有困难的话,也可
13以向中文版维护者求助。如果本翻译更新不及时或者翻译存在问题,请联系中文版维护者。
14
15中文版维护者: 谢良 Liang Xie <xieliang007@gmail.com>
16中文版翻译者: 谢良 Liang Xie <xieliang007@gmail.com>
17中文版校译者:
18以下为正文
19---------------------------------------------------------------------
20
21下面这些说明指令都是非常基础的,如果你想进一步了解请阅读相关专业文档:)
22请不要再在本文档增加新的内容,但可以修复文档中的错误:)(mbligh@aracnet.com)
23感谢John Levon,Dave Hansen等在撰写时的帮助
24
25<test> 用于表示要测量的目标
26请先确保您已经有正确的System.map / vmlinux配置!
27
28对于linux系统来说,配置vmlinuz最容易的方法可能就是使用“make install”,然后修改
29/sbin/installkernel将vmlinux拷贝到/boot目录,而System.map通常是默认安装好的
30
31Readprofile
32-----------
332.6系列内核需要版本相对较新的readprofile,比如util-linux 2.12a中包含的,可以从:
34
35http://www.kernel.org/pub/linux/utils/util-linux/ 下载
36
37大部分linux发行版已经包含了.
38
39启用readprofile需要在kernel启动命令行增加”profile=2“
40
41clear readprofile -r
42 <test>
43dump output readprofile -m /boot/System.map > captured_profile
44
45Oprofile
46--------
47
48从http://oprofile.sourceforge.net/获取源代码(请参考Changes以获取匹配的版本)
49在kernel启动命令行增加“idle=poll”
50
51配置CONFIG_PROFILING=y和CONFIG_OPROFILE=y然后重启进入新kernel
52
53./configure --with-kernel-support
54make install
55
56想得到好的测量结果,请确保启用了本地APIC特性。如果opreport显示有0Hz CPU,
57说明APIC特性没有开启。另外注意idle=poll选项可能有损性能。
58
59One time setup:
60 opcontrol --setup --vmlinux=/boot/vmlinux
61
62clear opcontrol --reset
63start opcontrol --start
64 <test>
65stop opcontrol --stop
66dump output opreport > output_file
67
68如果只看kernel相关的报告结果,请运行命令 opreport -l /boot/vmlinux > output_file
69
70通过reset选项可以清理过期统计数据,相当于重启的效果。
71
diff --git a/Documentation/zh_CN/filesystems/sysfs.txt b/Documentation/zh_CN/filesystems/sysfs.txt
new file mode 100644
index 00000000000..e230eaa3312
--- /dev/null
+++ b/Documentation/zh_CN/filesystems/sysfs.txt
@@ -0,0 +1,372 @@
1Chinese translated version of Documentation/filesystems/sysfs.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Maintainer: Patrick Mochel <mochel@osdl.org>
10 Mike Murphy <mamurph@cs.clemson.edu>
11Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
12---------------------------------------------------------------------
13Documentation/filesystems/sysfs.txt 的中文翻译
14
15如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
16交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
17译存在问题,请联系中文版维护者。
18英文版维护者: Patrick Mochel <mochel@osdl.org>
19 Mike Murphy <mamurph@cs.clemson.edu>
20中文版维护者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
21中文版翻译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
22中文版校译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
23
24
25以下为正文
26---------------------------------------------------------------------
27sysfs - 用于导出内核对象(kobject)的文件系统
28
29Patrick Mochel <mochel@osdl.org>
30Mike Murphy <mamurph@cs.clemson.edu>
31
32修订: 16 August 2011
33原始版本: 10 January 2003
34
35
36sysfs 简介:
37~~~~~~~~~~
38
39sysfs 是一个最初基于 ramfs 且位于内存的文件系统。它提供导出内核
40数据结构及其属性,以及它们之间的关联到用户空间的方法。
41
42sysfs 始终与 kobject 的底层结构紧密相关。请阅读
43Documentation/kobject.txt 文档以获得更多关于 kobject 接口的
44信息。
45
46
47使用 sysfs
48~~~~~~~~~~~
49
50只要内核配置中定义了 CONFIG_SYSFS ,sysfs 总是被编译进内核。你可
51通过以下命令挂载它:
52
53 mount -t sysfs sysfs /sys
54
55
56创建目录
57~~~~~~~~
58
59任何 kobject 在系统中注册,就会有一个目录在 sysfs 中被创建。这个
60目录是作为该 kobject 的父对象所在目录的子目录创建的,以准确地传递
61内核的对象层次到用户空间。sysfs 中的顶层目录代表着内核对象层次的
62共同祖先;例如:某些对象属于某个子系统。
63
64Sysfs 在与其目录关联的 sysfs_dirent 对象中内部保存一个指向实现
65目录的 kobject 的指针。以前,这个 kobject 指针被 sysfs 直接用于
66kobject 文件打开和关闭的引用计数。而现在的 sysfs 实现中,kobject
67引用计数只能通过 sysfs_schedule_callback() 函数直接修改。
68
69
70属性
71~~~~
72
73kobject 的属性可在文件系统中以普通文件的形式导出。Sysfs 为属性定义
74了面向文件 I/O 操作的方法,以提供对内核属性的读写。
75
76
77属性应为 ASCII 码文本文件。以一个文件只存储一个属性值为宜。但一个
78文件只包含一个属性值可能影响效率,所以一个包含相同数据类型的属性值
79数组也被广泛地接受。
80
81混合类型、表达多行数据以及一些怪异的数据格式会遭到强烈反对。这样做是
82很丢脸的,而且其代码会在未通知作者的情况下被重写。
83
84
85一个简单的属性结构定义如下:
86
87struct attribute {
88 char * name;
89 struct module *owner;
90 umode_t mode;
91};
92
93
94int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
95void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
96
97
98一个单独的属性结构并不包含读写其属性值的方法。子系统最好为增删特定
99对象类型的属性定义自己的属性结构体和封装函数。
100
101例如:驱动程序模型定义的 device_attribute 结构体如下:
102
103struct device_attribute {
104 struct attribute attr;
105 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
106 char *buf);
107 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
108 const char *buf, size_t count);
109};
110
111int device_create_file(struct device *, const struct device_attribute *);
112void device_remove_file(struct device *, const struct device_attribute *);
113
114为了定义设备属性,同时定义了一下辅助宏:
115
116#define DEVICE_ATTR(_name, _mode, _show, _store) \
117struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
118
119例如:声明
120
121static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
122
123等同于如下代码:
124
125static struct device_attribute dev_attr_foo = {
126 .attr = {
127 .name = "foo",
128 .mode = S_IWUSR | S_IRUGO,
129 .show = show_foo,
130 .store = store_foo,
131 },
132};
133
134
135子系统特有的回调函数
136~~~~~~~~~~~~~~~~~~~
137
138当一个子系统定义一个新的属性类型时,必须实现一系列的 sysfs 操作,
139以帮助读写调用实现属性所有者的显示和储存方法。
140
141struct sysfs_ops {
142 ssize_t (*show)(struct kobject *, struct attribute *, char *);
143 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
144};
145
146[子系统应已经定义了一个 struct kobj_type 结构体作为这个类型的
147描述符,并在此保存 sysfs_ops 的指针。更多的信息参见 kobject 的
148文档]
149
150sysfs 会为这个类型调用适当的方法。当一个文件被读写时,这个方法会
151将一般的kobject 和 attribute 结构体指针转换为适当的指针类型后
152调用相关联的函数。
153
154
155示例:
156
157#define to_dev(obj) container_of(obj, struct device, kobj)
158#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
159
160static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
161 char *buf)
162{
163 struct device_attribute *dev_attr = to_dev_attr(attr);
164 struct device *dev = to_dev(kobj);
165 ssize_t ret = -EIO;
166
167 if (dev_attr->show)
168 ret = dev_attr->show(dev, dev_attr, buf);
169 if (ret >= (ssize_t)PAGE_SIZE) {
170 print_symbol("dev_attr_show: %s returned bad count\n",
171 (unsigned long)dev_attr->show);
172 }
173 return ret;
174}
175
176
177
178读写属性数据
179~~~~~~~~~~~~
180
181在声明属性时,必须指定 show() 或 store() 方法,以实现属性的
182读或写。这些方法的类型应该和以下的设备属性定义一样简单。
183
184ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
185ssize_t (*store)(struct device *dev, struct device_attribute *attr,
186 const char *buf, size_t count);
187
188也就是说,他们应只以一个处理对象、一个属性和一个缓冲指针作为参数。
189
190sysfs 会分配一个大小为 (PAGE_SIZE) 的缓冲区并传递给这个方法。
191Sysfs 将会为每次读写操作调用一次这个方法。这使得这些方法在执行时
192会出现以下的行为:
193
194- 在读方面(read(2)),show() 方法应该填充整个缓冲区。回想属性
195 应只导出了一个属性值或是一个同类型属性值的数组,所以这个代价将
196 不会不太高。
197
198 这使得用户空间可以局部地读和任意的向前搜索整个文件。如果用户空间
199 向后搜索到零或使用‘0’偏移执行一个pread(2)操作,show()方法将
200 再次被调用,以重新填充缓存。
201
202- 在写方面(write(2)),sysfs 希望在第一次写操作时得到整个缓冲区。
203 之后 Sysfs 传递整个缓冲区给 store() 方法。
204
205 当要写 sysfs 文件时,用户空间进程应首先读取整个文件,修该想要
206 改变的值,然后回写整个缓冲区。
207
208 在读写属性值时,属性方法的执行应操作相同的缓冲区。
209
210注记:
211
212- 写操作导致的 show() 方法重载,会忽略当前文件位置。
213
214- 缓冲区应总是 PAGE_SIZE 大小。对于i386,这个值为4096。
215
216- show() 方法应该返回写入缓冲区的字节数,也就是 snprintf()的
217 返回值。
218
219- show() 应始终使用 snprintf()。
220
221- store() 应返回缓冲区的已用字节数。如果整个缓存都已填满,只需返回
222 count 参数。
223
224- show() 或 store() 可以返回错误值。当得到一个非法值,必须返回一个
225 错误值。
226
227- 一个传递给方法的对象将会通过 sysfs 调用对象内嵌的引用计数固定在
228 内存中。尽管如此,对象代表的物理实体(如设备)可能已不存在。如有必要,
229 应该实现一个检测机制。
230
231一个简单的(未经实验证实的)设备属性实现如下:
232
233static ssize_t show_name(struct device *dev, struct device_attribute *attr,
234 char *buf)
235{
236 return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name);
237}
238
239static ssize_t store_name(struct device *dev, struct device_attribute *attr,
240 const char *buf, size_t count)
241{
242 snprintf(dev->name, sizeof(dev->name), "%.*s",
243 (int)min(count, sizeof(dev->name) - 1), buf);
244 return count;
245}
246
247static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
248
249
250(注意:真正的实现不允许用户空间设置设备名。)
251
252顶层目录布局
253~~~~~~~~~~~~
254
255sysfs 目录的安排显示了内核数据结构之间的关系。
256
257顶层 sysfs 目录如下:
258
259block/
260bus/
261class/
262dev/
263devices/
264firmware/
265net/
266fs/
267
268devices/ 包含了一个设备树的文件系统表示。他直接映射了内部的内核
269设备树,反映了设备的层次结构。
270
271bus/ 包含了内核中各种总线类型的平面目录布局。每个总线目录包含两个
272子目录:
273
274 devices/
275 drivers/
276
277devices/ 包含了系统中出现的每个设备的符号链接,他们指向 root/ 下的
278设备目录。
279
280drivers/ 包含了每个已为特定总线上的设备而挂载的驱动程序的目录(这里
281假定驱动没有跨越多个总线类型)。
282
283fs/ 包含了一个为文件系统设立的目录。现在每个想要导出属性的文件系统必须
284在 fs/ 下创建自己的层次结构(参见Documentation/filesystems/fuse.txt)。
285
286dev/ 包含两个子目录: char/ 和 block/。在这两个子目录中,有以
287<major>:<minor> 格式命名的符号链接。这些符号链接指向 sysfs 目录
288中相应的设备。/sys/dev 提供一个通过一个 stat(2) 操作结果,查找
289设备 sysfs 接口快捷的方法。
290
291更多有关 driver-model 的特性信息可以在 Documentation/driver-model/
292中找到。
293
294
295TODO: 完成这一节。
296
297
298当前接口
299~~~~~~~~
300
301以下的接口层普遍存在于当前的sysfs中:
302
303- 设备 (include/linux/device.h)
304----------------------------------
305结构体:
306
307struct device_attribute {
308 struct attribute attr;
309 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
310 char *buf);
311 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
312 const char *buf, size_t count);
313};
314
315声明:
316
317DEVICE_ATTR(_name, _mode, _show, _store);
318
319增/删属性:
320
321int device_create_file(struct device *dev, const struct device_attribute * attr);
322void device_remove_file(struct device *dev, const struct device_attribute * attr);
323
324
325- 总线驱动程序 (include/linux/device.h)
326--------------------------------------
327结构体:
328
329struct bus_attribute {
330 struct attribute attr;
331 ssize_t (*show)(struct bus_type *, char * buf);
332 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
333};
334
335声明:
336
337BUS_ATTR(_name, _mode, _show, _store)
338
339增/删属性:
340
341int bus_create_file(struct bus_type *, struct bus_attribute *);
342void bus_remove_file(struct bus_type *, struct bus_attribute *);
343
344
345- 设备驱动程序 (include/linux/device.h)
346-----------------------------------------
347
348结构体:
349
350struct driver_attribute {
351 struct attribute attr;
352 ssize_t (*show)(struct device_driver *, char * buf);
353 ssize_t (*store)(struct device_driver *, const char * buf,
354 size_t count);
355};
356
357声明:
358
359DRIVER_ATTR(_name, _mode, _show, _store)
360
361增/删属性:
362
363int driver_create_file(struct device_driver *, const struct driver_attribute *);
364void driver_remove_file(struct device_driver *, const struct driver_attribute *);
365
366
367文档
368~~~~
369
370sysfs 目录结构以及其中包含的属性定义了一个内核与用户空间之间的 ABI。
371对于任何 ABI,其自身的稳定和适当的文档是非常重要的。所有新的 sysfs
372属性必须在 Documentation/ABI 中有文档。详见 Documentation/ABI/README。
diff --git a/Documentation/zh_CN/gpio.txt b/Documentation/zh_CN/gpio.txt
new file mode 100644
index 00000000000..4fa7b4e6f85
--- /dev/null
+++ b/Documentation/zh_CN/gpio.txt
@@ -0,0 +1,658 @@
1Chinese translated version of Documentation/gpio.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Maintainer: Grant Likely <grant.likely@secretlab.ca>
10 Linus Walleij <linus.walleij@linaro.org>
11Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
12---------------------------------------------------------------------
13Documentation/gpio.txt 的中文翻译
14
15如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
16交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
17译存在问题,请联系中文版维护者。
18英文版维护者: Grant Likely <grant.likely@secretlab.ca>
19 Linus Walleij <linus.walleij@linaro.org>
20中文版维护者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
21中文版翻译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
22中文版校译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
23
24
25以下为正文
26---------------------------------------------------------------------
27GPIO 接口
28
29本文档提供了一个在Linux下访问GPIO的公约概述。
30
31这些函数以 gpio_* 作为前缀。其他的函数不允许使用这样的前缀或相关的
32__gpio_* 前缀。
33
34
35什么是GPIO?
36==========
37"通用输入/输出口"(GPIO)是一个灵活的由软件控制的数字信号。他们可
38由多种芯片提供,且对于从事嵌入式和定制硬件的 Linux 开发者来说是
39比较熟悉。每个GPIO 都代表一个连接到特定引脚或球栅阵列(BGA)封装中
40“球珠”的一个位。电路板原理图显示了 GPIO 与外部硬件的连接关系。
41驱动可以编写成通用代码,以使板级启动代码可传递引脚配置数据给驱动。
42
43片上系统 (SOC) 处理器对 GPIO 有很大的依赖。在某些情况下,每个
44非专用引脚都可配置为 GPIO,且大多数芯片都最少有一些 GPIO。
45可编程逻辑器件(类似 FPGA) 可以方便地提供 GPIO。像电源管理和
46音频编解码器这样的多功能芯片经常留有一些这样的引脚来帮助那些引脚
47匮乏的 SOC。同时还有通过 I2C 或 SPI 串行总线连接的“GPIO扩展器”
48芯片。大多数 PC 的南桥有一些拥有 GPIO 能力的引脚 (只有BIOS
49固件才知道如何使用他们)。
50
51GPIO 的实际功能因系统而异。通常用法有:
52
53 - 输出值可写 (高电平=1,低电平=0)。一些芯片也有如何驱动这些值的选项,
54 例如只允许输出一个值、支持“线与”及其他取值类似的模式(值得注意的是
55 “开漏”信号)
56
57 - 输入值可读(1、0)。一些芯片支持引脚在配置为“输出”时回读,这对于类似
58 “线与”的情况(以支持双向信号)是非常有用的。GPIO 控制器可能有输入
59 去毛刺/消抖逻辑,这有时需要软件控制。
60
61 - 输入通常可作为 IRQ 信号,一般是沿触发,但有时是电平触发。这样的 IRQ
62 可能配置为系统唤醒事件,以将系统从低功耗状态下唤醒。
63
64 - 通常一个 GPIO 根据不同产品电路板的需求,可以配置为输入或输出,也有仅
65 支持单向的。
66
67 - 大部分 GPIO 可以在持有自旋锁时访问,但是通常由串行总线扩展的 GPIO
68 不允许持有自旋锁。但某些系统也支持这种类型。
69
70对于给定的电路板,每个 GPIO 都用于某个特定的目的,如监控 MMC/SD 卡的
71插入/移除、检测卡的写保护状态、驱动 LED、配置收发器、模拟串行总线、
72复位硬件看门狗、感知开关状态等等。
73
74
75GPIO 公约
76=========
77注意,这个叫做“公约”,因为这不是强制性的,不遵循这个公约是无伤大雅的,
78因为此时可移植性并不重要。GPIO 常用于板级特定的电路逻辑,甚至可能
79随着电路板的版本而改变,且不可能在不同走线的电路板上使用。仅有在少数
80功能上才具有可移植性,其他功能是平台特定。这也是由于“胶合”的逻辑造成的。
81
82此外,这不需要任何的执行框架,只是一个接口。某个平台可能通过一个简单地
83访问芯片寄存器的内联函数来实现它,其他平台可能通过委托一系列不同的GPIO
84控制器的抽象函数来实现它。(有一些可选的代码能支持这种策略的实现,本文档
85后面会介绍,但作为 GPIO 接口的客户端驱动程序必须与它的实现无关。)
86
87也就是说,如果在他们的平台上支持这个公约,驱动应尽可能的使用它。平台
88必须在 Kconfig 中声明对 GENERIC_GPIO的支持 (布尔型 true),并提供
89一个 <asm/gpio.h> 文件。那些调用标准 GPIO 函数的驱动应该在 Kconfig
90入口中声明依赖GENERIC_GPIO。当驱动包含文件:
91
92 #include <linux/gpio.h>
93
94则 GPIO 函数是可用,无论是“真实代码”还是经优化过的语句。如果你遵守
95这个公约,当你的代码完成后,对其他的开发者来说会更容易看懂和维护。
96
97注意,这些操作包含所用平台的 I/O 屏障代码,驱动无须显式地调用他们。
98
99
100标识 GPIO
101---------
102GPIO 是通过无符号整型来标识的,范围是 0 到 MAX_INT。保留“负”数
103用于其他目的,例如标识信号“在这个板子上不可用”或指示错误。未接触底层
104硬件的代码会忽略这些整数。
105
106平台会定义这些整数的用法,且通常使用 #define 来定义 GPIO,这样
107板级特定的启动代码可以直接关联相应的原理图。相对来说,驱动应该仅使用
108启动代码传递过来的 GPIO 编号,使用 platform_data 保存板级特定
109引脚配置数据 (同时还有其他须要的板级特定数据),避免可能出现的问题。
110
111例如一个平台使用编号 32-159 来标识 GPIO,而在另一个平台使用编号0-63
112标识一组 GPIO 控制器,64-79标识另一类 GPIO 控制器,且在一个含有
113FPGA 的特定板子上使用 80-95。编号不一定要连续,那些平台中,也可以
114使用编号2000-2063来标识一个 I2C 接口的 GPIO 扩展器中的 GPIO。
115
116如果你要初始化一个带有无效 GPIO 编号的结构体,可以使用一些负编码
117(如"-EINVAL"),那将使其永远不会是有效。来测试这样一个结构体中的编号
118是否关联一个 GPIO,你可使用以下断言:
119
120 int gpio_is_valid(int number);
121
122如果编号不存在,则请求和释放 GPIO 的函数将拒绝执行相关操作(见下文)。
123其他编号也可能被拒绝,比如一个编号可能存在,但暂时在给定的电路上不可用。
124
125一个平台是否支持多个 GPIO 控制器为平台特定的实现问题,就像是否可以
126在 GPIO 编号空间中有“空洞”和是否可以在运行时添加新的控制器一样。
127这些问题会影响其他事情,包括相邻的 GPIO 编号是否存在等。
128
129使用 GPIO
130---------
131对于一个 GPIO,系统应该做的第一件事情就是通过 gpio_request()
132函数分配它,见下文。
133
134接下来是设置I/O方向,这通常是在板级启动代码中为所使用的 GPIO 设置
135platform_device 时完成。
136
137 /* 设置为输入或输出, 返回 0 或负的错误代码 */
138 int gpio_direction_input(unsigned gpio);
139 int gpio_direction_output(unsigned gpio, int value);
140
141返回值为零代表成功,否则返回一个负的错误代码。这个返回值需要检查,因为
142get/set(获取/设置)函数调用没法返回错误,且有可能是配置错误。通常,
143你应该在进程上下文中调用这些函数。然而,对于自旋锁安全的 GPIO,在板子
144启动的早期、进程启动前使用他们也是可以的。
145
146对于作为输出的 GPIO,为其提供初始输出值,对于避免在系统启动期间出现
147信号毛刺是很有帮助的。
148
149为了与传统的 GPIO 接口兼容, 在设置一个 GPIO 方向时,如果它还未被申请,
150则隐含了申请那个 GPIO 的操作(见下文)。这种兼容性正在从可选的 gpiolib
151框架中移除。
152
153如果这个 GPIO 编码不存在,或者特定的 GPIO 不能用于那种模式,则方向
154设置可能失败。依赖启动固件来正确地设置方向通常是一个坏主意,因为它可能
155除了启动Linux,并没有做更多的验证工作。(同理, 板子的启动代码可能需要
156将这个复用的引脚设置为 GPIO,并正确地配置上拉/下拉电阻。)
157
158
159访问自旋锁安全的 GPIO
160-------------------
161大多数 GPIO 控制器可以通过内存读/写指令来访问。这些指令不会休眠,可以
162安全地在硬(非线程)中断例程和类似的上下文中完成。
163
164对于那些用 gpio_cansleep()测试总是返回失败的 GPIO(见下文),使用
165以下的函数访问:
166
167 /* GPIO 输入:返回零或非零 */
168 int gpio_get_value(unsigned gpio);
169
170 /* GPIO 输出 */
171 void gpio_set_value(unsigned gpio, int value);
172
173GPIO值是布尔值,零表示低电平,非零表示高电平。当读取一个输出引脚的值时,
174返回值应该是引脚上的值。这个值不总是和输出值相符,因为存在开漏输出信号和
175输出延迟问题。
176
177以上的 get/set 函数无错误返回值,因为之前 gpio_direction_*()应已检查过
178其是否为“无效GPIO”。此外,还需要注意的是并不是所有平台都可以从输出引脚
179中读取数据,对于不能读取的引脚应总返回零。另外,对那些在原子上下文中无法
180安全访问的 GPIO (译者注:因为访问可能导致休眠)使用这些函数是不合适的
181(见下文)。
182
183在 GPIO 编号(还有输出、值)为常数的情况下,鼓励通过平台特定的实现来优化
184这两个函数来访问 GPIO 值。这种情况(读写一个硬件寄存器)下只需要几条指令
185是很正常的,且无须自旋锁。这种优化函数比起那些在子程序上花费许多指令的
186函数可以使得模拟接口(译者注:例如 GPIO 模拟 I2C、1-wire 或 SPI)的
187应用(在空间和时间上都)更具效率。
188
189
190访问可能休眠的 GPIO
191-----------------
192某些 GPIO 控制器必须通过基于总线(如 I2C 或 SPI)的消息访问。读或写这些
193GPIO 值的命令需要等待其信息排到队首才发送命令,再获得其反馈。期间需要
194休眠,这不能在 IRQ 例程(中断上下文)中执行。
195
196支持此类 GPIO 的平台通过以下函数返回非零值来区分出这种 GPIO。(此函数需要
197一个之前通过 gpio_request 分配到的有效 GPIO 编号):
198
199 int gpio_cansleep(unsigned gpio);
200
201为了访问这种 GPIO,内核定义了一套不同的函数:
202
203 /* GPIO 输入:返回零或非零 ,可能会休眠 */
204 int gpio_get_value_cansleep(unsigned gpio);
205
206 /* GPIO 输出,可能会休眠 */
207 void gpio_set_value_cansleep(unsigned gpio, int value);
208
209
210访问这样的 GPIO 需要一个允许休眠的上下文,例如线程 IRQ 处理例程,并用以上的
211访问函数替换那些没有 cansleep()后缀的自旋锁安全访问函数。
212
213除了这些访问函数可能休眠,且它们操作的 GPIO 不能在硬件 IRQ 处理例程中访问的
214事实,这些处理例程实际上和自旋锁安全的函数是一样的。
215
216** 除此之外 ** 调用设置和配置此类 GPIO 的函数也必须在允许休眠的上下文中,
217因为它们可能也需要访问 GPIO 控制器芯片: (这些设置函数通常在板级启动代码或者
218驱动探测/断开代码中,所以这是一个容易满足的约束条件。)
219
220 gpio_direction_input()
221 gpio_direction_output()
222 gpio_request()
223
224## gpio_request_one()
225## gpio_request_array()
226## gpio_free_array()
227
228 gpio_free()
229 gpio_set_debounce()
230
231
232
233声明和释放 GPIO
234----------------------------
235为了有助于捕获系统配置错误,定义了两个函数。
236
237 /* 申请 GPIO, 返回 0 或负的错误代码.
238 * 非空标签可能有助于诊断.
239 */
240 int gpio_request(unsigned gpio, const char *label);
241
242 /* 释放之前声明的 GPIO */
243 void gpio_free(unsigned gpio);
244
245将无效的 GPIO 编码传递给 gpio_request()会导致失败,申请一个已使用这个
246函数声明过的 GPIO 也会失败。gpio_request()的返回值必须检查。你应该在
247进程上下文中调用这些函数。然而,对于自旋锁安全的 GPIO,在板子启动的早期、
248进入进程之前是可以申请的。
249
250这个函数完成两个基本的目标。一是标识那些实际上已作为 GPIO 使用的信号线,
251这样便于更好地诊断;系统可能需要服务几百个可用的 GPIO,但是对于任何一个
252给定的电路板通常只有一些被使用。另一个目的是捕获冲突,查明错误:如两个或
253更多驱动错误地认为他们已经独占了某个信号线,或是错误地认为移除一个管理着
254某个已激活信号的驱动是安全的。也就是说,申请 GPIO 的作用类似一种锁机制。
255
256某些平台可能也使用 GPIO 作为电源管理激活信号(例如通过关闭未使用芯片区和
257简单地关闭未使用时钟)。
258
259对于 GPIO 使用 pinctrl 子系统已知的引脚,子系统应该被告知其使用情况;
260一个 gpiolib 驱动的 .request()操作应调用 pinctrl_request_gpio(),
261而 gpiolib 驱动的 .free()操作应调用 pinctrl_free_gpio()。pinctrl
262子系统允许 pinctrl_request_gpio()在某个引脚或引脚组以复用形式“属于”
263一个设备时都成功返回。
264
265任何须将 GPIO 信号导向适当引脚的引脚复用硬件的编程应该发生在 GPIO
266驱动的 .direction_input()或 .direction_output()函数中,以及
267任何输出 GPIO 值的设置之后。这样可使从引脚特殊功能到 GPIO 的转换
268不会在引脚产生毛刺波形。有时当用一个 GPIO 实现其信号驱动一个非 GPIO
269硬件模块的解决方案时,就需要这种机制。
270
271某些平台允许部分或所有 GPIO 信号使用不同的引脚。类似的,GPIO 或引脚的
272其他方面也需要配置,如上拉/下拉。平台软件应该在对这些 GPIO 调用
273gpio_request()前将这类细节配置好,例如使用 pinctrl 子系统的映射表,
274使得 GPIO 的用户无须关注这些细节。
275
276还有一个值得注意的是在释放 GPIO 前,你必须停止使用它。
277
278
279注意:申请一个 GPIO 并没有以任何方式配置它,只不过标识那个 GPIO 处于使用
280状态。必须有另外的代码来处理引脚配置(如控制 GPIO 使用的引脚、上拉/下拉)。
281考虑到大多数情况下声明 GPIO 之后就会立即配置它们,所以定义了以下三个辅助函数:
282
283 /* 申请一个 GPIO 信号, 同时通过特定的'flags'初始化配置,
284 * 其他和 gpio_request()的参数和返回值相同
285 *
286 */
287 int gpio_request_one(unsigned gpio, unsigned long flags, const char *label);
288
289 /* 在单个函数中申请多个 GPIO
290 */
291 int gpio_request_array(struct gpio *array, size_t num);
292
293 /* 在单个函数中释放多个 GPIO
294 */
295 void gpio_free_array(struct gpio *array, size_t num);
296
297这里 'flags' 当前定义可指定以下属性:
298
299 * GPIOF_DIR_IN - 配置方向为输入
300 * GPIOF_DIR_OUT - 配置方向为输出
301
302 * GPIOF_INIT_LOW - 在作为输出时,初始值为低电平
303 * GPIOF_INIT_HIGH - 在作为输出时,初始值为高电平
304 * GPIOF_OPEN_DRAIN - gpio引脚为开漏信号
305 * GPIOF_OPEN_SOURCE - gpio引脚为源极开路信号
306
307 * GPIOF_EXPORT_DIR_FIXED - 将 gpio 导出到 sysfs,并保持方向
308 * GPIOF_EXPORT_DIR_CHANGEABLE - 同样是导出, 但允许改变方向
309
310因为 GPIOF_INIT_* 仅有在配置为输出的时候才存在,所以有效的组合为:
311
312 * GPIOF_IN - 配置为输入
313 * GPIOF_OUT_INIT_LOW - 配置为输出,并初始化为低电平
314 * GPIOF_OUT_INIT_HIGH - 配置为输出,并初始化为高电平
315
316当设置 flag 为 GPIOF_OPEN_DRAIN 时,则假设引脚是开漏信号。这样的引脚
317将不会在输出模式下置1。这样的引脚需要连接上拉电阻。通过使能这个标志,gpio库
318将会在被要求输出模式下置1时将引脚变为输入状态来使引脚置高。引脚在输出模式下
319通过置0使其输出低电平。
320
321当设置 flag 为 GPIOF_OPEN_SOURCE 时,则假设引脚为源极开路信号。这样的引脚
322将不会在输出模式下置0。这样的引脚需要连接下拉电阻。通过使能这个标志,gpio库
323将会在被要求输出模式下置0时将引脚变为输入状态来使引脚置低。引脚在输出模式下
324通过置1使其输出高电平。
325
326将来这些标志可能扩展到支持更多的属性。
327
328更进一步,为了更简单地声明/释放多个 GPIO,'struct gpio'被引进来封装所有
329这三个领域:
330
331 struct gpio {
332 unsigned gpio;
333 unsigned long flags;
334 const char *label;
335 };
336
337一个典型的用例:
338
339 static struct gpio leds_gpios[] = {
340 { 32, GPIOF_OUT_INIT_HIGH, "Power LED" }, /* 默认开启 */
341 { 33, GPIOF_OUT_INIT_LOW, "Green LED" }, /* 默认关闭 */
342 { 34, GPIOF_OUT_INIT_LOW, "Red LED" }, /* 默认关闭 */
343 { 35, GPIOF_OUT_INIT_LOW, "Blue LED" }, /* 默认关闭 */
344 { ... },
345 };
346
347 err = gpio_request_one(31, GPIOF_IN, "Reset Button");
348 if (err)
349 ...
350
351 err = gpio_request_array(leds_gpios, ARRAY_SIZE(leds_gpios));
352 if (err)
353 ...
354
355 gpio_free_array(leds_gpios, ARRAY_SIZE(leds_gpios));
356
357
358GPIO 映射到 IRQ
359--------------------
360GPIO 编号是无符号整数;IRQ 编号也是。这些构成了两个逻辑上不同的命名空间
361(GPIO 0 不一定使用 IRQ 0)。你可以通过以下函数在它们之间实现映射:
362
363 /* 映射 GPIO 编号到 IRQ 编号 */
364 int gpio_to_irq(unsigned gpio);
365
366 /* 映射 IRQ 编号到 GPIO 编号 (尽量避免使用) */
367 int irq_to_gpio(unsigned irq);
368
369它们的返回值为对应命名空间的相关编号,或是负的错误代码(如果无法映射)。
370(例如,某些 GPIO 无法做为 IRQ 使用。)以下的编号错误是未经检测的:使用一个
371未通过 gpio_direction_input()配置为输入的 GPIO 编号,或者使用一个
372并非来源于gpio_to_irq()的 IRQ 编号。
373
374这两个映射函数可能会在信号编号的加减计算过程上花些时间。它们不可休眠。
375
376gpio_to_irq()返回的非错误值可以传递给 request_irq()或者 free_irq()。
377它们通常通过板级特定的初始化代码存放到平台设备的 IRQ 资源中。注意:IRQ
378触发选项是 IRQ 接口的一部分,如 IRQF_TRIGGER_FALLING,系统唤醒能力
379也是如此。
380
381irq_to_gpio()返回的非错误值大多数通常可以被 gpio_get_value()所使用,
382比如在 IRQ 是沿触发时初始化或更新驱动状态。注意某些平台不支持反映射,所以
383你应该尽量避免使用它。
384
385
386模拟开漏信号
387----------------------------
388有时在只有低电平信号作为实际驱动结果(译者注:多个输出连接于一点,逻辑电平
389结果为所有输出的逻辑与)的时候,共享的信号线需要使用“开漏”信号。(该术语
390适用于 CMOS 管;而 TTL 用“集电极开路”。)一个上拉电阻使信号为高电平。这
391有时被称为“线与”。实际上,从负逻辑(低电平为真)的角度来看,这是一个“线或”。
392
393一个开漏信号的常见例子是共享的低电平使能 IRQ 信号线。此外,有时双向数据总线
394信号也使用漏极开路信号。
395
396某些 GPIO 控制器直接支持开漏输出,还有许多不支持。当你需要开漏信号,但
397硬件又不直接支持的时候,一个常用的方法是用任何即可作输入也可作输出的 GPIO
398引脚来模拟:
399
400 LOW: gpio_direction_output(gpio, 0) ... 这代码驱动信号并覆盖
401 上拉配置。
402
403 HIGH: gpio_direction_input(gpio) ... 这代码关闭输出,所以上拉电阻
404 (或其他的一些器件)控制了信号。
405
406如果你将信号线“驱动”为高电平,但是 gpio_get_value(gpio)报告了一个
407低电平(在适当的上升时间后),你就可以知道是其他的一些组件将共享信号线拉低了。
408这不一定是错误的。一个常见的例子就是 I2C 时钟的延长:一个需要较慢时钟的
409从设备延迟 SCK 的上升沿,而 I2C 主设备相应地调整其信号传输速率。
410
411
412这些公约忽略了什么?
413================
414这些公约忽略的最大一件事就是引脚复用,因为这属于高度芯片特定的属性且
415没有可移植性。某个平台可能不需要明确的复用信息;有的对于任意给定的引脚
416可能只有两个功能选项;有的可能每个引脚有八个功能选项;有的可能可以将
417几个引脚中的任何一个作为给定的 GPIO。(是的,这些例子都来自于当前运行
418Linux 的系统。)
419
420在某些系统中,与引脚复用相关的是配置和使能集成的上、下拉模式。并不是所有
421平台都支持这种模式,或者不会以相同的方式来支持这种模式;且任何给定的电路板
422可能使用外置的上拉(或下拉)电阻,这时芯片上的就不应该使用。(当一个电路需要
4235kOhm 的拉动电阻,芯片上的 100 kOhm 电阻就不能做到。)同样的,驱动能力
424(2 mA vs 20 mA)和电压(1.8V vs 3.3V)是平台特定问题,就像模型一样在
425可配置引脚和 GPIO 之间(没)有一一对应的关系。
426
427还有其他一些系统特定的机制没有在这里指出,例如上述的输入去毛刺和线与输出
428选项。硬件可能支持批量读或写 GPIO,但是那一般是配置相关的:对于处于同一
429块区(bank)的GPIO。(GPIO 通常以 16 或 32 个组成一个区块,一个给定的
430片上系统一般有几个这样的区块。)某些系统可以通过输出 GPIO 触发 IRQ,
431或者从并非以 GPIO 管理的引脚取值。这些机制的相关代码没有必要具有可移植性。
432
433当前,动态定义 GPIO 并不是标准的,例如作为配置一个带有某些 GPIO 扩展器的
434附加电路板的副作用。
435
436GPIO 实现者的框架 (可选)
437=====================
438前面提到了,有一个可选的实现框架,让平台使用相同的编程接口,更加简单地支持
439不同种类的 GPIO 控制器。这个框架称为"gpiolib"。
440
441作为一个辅助调试功能,如果 debugfs 可用,就会有一个 /sys/kernel/debug/gpio
442文件。通过这个框架,它可以列出所有注册的控制器,以及当前正在使用中的 GPIO
443的状态。
444
445
446控制器驱动: gpio_chip
447-------------------
448在框架中每个 GPIO 控制器都包装为一个 "struct gpio_chip",他包含了
449该类型的每个控制器的常用信息:
450
451 - 设置 GPIO 方向的方法
452 - 用于访问 GPIO 值的方法
453 - 告知调用其方法是否可能休眠的标志
454 - 可选的 debugfs 信息导出方法 (显示类似上拉配置一样的额外状态)
455 - 诊断标签
456
457也包含了来自 device.platform_data 的每个实例的数据:它第一个 GPIO 的
458编号和它可用的 GPIO 的数量。
459
460实现 gpio_chip 的代码应支持多控制器实例,这可能使用驱动模型。那些代码要
461配置每个 gpio_chip,并发起gpiochip_add()。卸载一个 GPIO 控制器很少见,
462但在必要的时候可以使用 gpiochip_remove()。
463
464大部分 gpio_chip 是一个实例特定结构体的一部分,而并不将 GPIO 接口单独
465暴露出来,比如编址、电源管理等。类似编解码器这样的芯片会有复杂的非 GPIO
466状态。
467
468任何一个 debugfs 信息导出方法通常应该忽略还未申请作为 GPIO 的信号线。
469他们可以使用 gpiochip_is_requested()测试,当这个 GPIO 已经申请过了
470就返回相关的标签,否则返回 NULL。
471
472
473平台支持
474-------
475为了支持这个框架,一个平台的 Kconfig 文件将会 "select"(选择)
476ARCH_REQUIRE_GPIOLIB 或 ARCH_WANT_OPTIONAL_GPIOLIB,并让它的
477<asm/gpio.h> 包含 <asm-generic/gpio.h>,同时定义三个方法:
478gpio_get_value()、gpio_set_value()和 gpio_cansleep()。
479
480它也应提供一个 ARCH_NR_GPIOS 的定义值,这样可以更好地反映该平台 GPIO
481的实际数量,节省静态表的空间。(这个定义值应该包含片上系统内建 GPIO 和
482GPIO 扩展器中的数据。)
483
484ARCH_REQUIRE_GPIOLIB 意味着 gpiolib 核心在这个构架中将总是编译进内核。
485
486ARCH_WANT_OPTIONAL_GPIOLIB 意味着 gpiolib 核心默认关闭,且用户可以
487使能它,并将其编译进内核(可选)。
488
489如果这些选项都没被选择,该平台就不通过 GPIO-lib 支持 GPIO,且代码不可以
490被用户使能。
491
492以下这些方法的实现可以直接使用框架代码,并总是通过 gpio_chip 调度:
493
494 #define gpio_get_value __gpio_get_value
495 #define gpio_set_value __gpio_set_value
496 #define gpio_cansleep __gpio_cansleep
497
498这些定义可以用更理想的实现方法替代,那就是使用经过逻辑优化的内联函数来访问
499基于特定片上系统的 GPIO。例如,若引用的 GPIO (寄存器位偏移)是常量“12”,
500读取或设置它可能只需少则两或三个指令,且不会休眠。当这样的优化无法实现时,
501那些函数必须使用框架提供的代码,那就至少要几十条指令才可以实现。对于用 GPIO
502模拟的 I/O 接口, 如此精简指令是很有意义的。
503
504对于片上系统,平台特定代码为片上 GPIO 每个区(bank)定义并注册 gpio_chip
505实例。那些 GPIO 应该根据芯片厂商的文档进行编码/标签,并直接和电路板原理图
506对应。他们应该开始于零并终止于平台特定的限制。这些 GPIO(代码)通常从
507arch_initcall()或者更早的地方集成进平台初始化代码,使这些 GPIO 总是可用,
508且他们通常可以作为 IRQ 使用。
509
510板级支持
511-------
512对于外部 GPIO 控制器(例如 I2C 或 SPI 扩展器、专用芯片、多功能器件、FPGA
513或 CPLD),大多数常用板级特定代码都可以注册控制器设备,并保证他们的驱动知道
514gpiochip_add()所使用的 GPIO 编号。他们的起始编号通常跟在平台特定的 GPIO
515编号之后。
516
517例如板级启动代码应该创建结构体指明芯片公开的 GPIO 范围,并使用 platform_data
518将其传递给每个 GPIO 扩展器芯片。然后芯片驱动中的 probe()例程可以将这个
519数据传递给 gpiochip_add()。
520
521初始化顺序很重要。例如,如果一个设备依赖基于 I2C 的(扩展)GPIO,那么它的
522probe()例程就应该在那个 GPIO 有效以后才可以被调用。这意味着设备应该在
523GPIO 可以工作之后才可被注册。解决这类依赖的的一种方法是让这种 gpio_chip
524控制器向板级特定代码提供 setup()和 teardown()回调函数。一旦所有必须的
525资源可用之后,这些板级特定的回调函数将会注册设备,并可以在这些 GPIO 控制器
526设备变成无效时移除它们。
527
528
529用户空间的 Sysfs 接口(可选)
530========================
531使用“gpiolib”实现框架的平台可以选择配置一个 GPIO 的 sysfs 用户接口。
532这不同于 debugfs 接口,因为它提供的是对 GPIO方向和值的控制,而不只显示
533一个GPIO 的状态摘要。此外,它可以出现在没有调试支持的产品级系统中。
534
535例如,通过适当的系统硬件文档,用户空间可以知道 GIOP #23 控制 Flash
536存储器的写保护(用于保护其中 Bootloader 分区)。产品的系统升级可能需要
537临时解除这个保护:首先导入一个 GPIO,改变其输出状态,然后在重新使能写保护
538前升级代码。通常情况下,GPIO #23 是不会被触及的,并且内核也不需要知道他。
539
540根据适当的硬件文档,某些系统的用户空间 GPIO 可以用于确定系统配置数据,
541这些数据是标准内核不知道的。在某些任务中,简单的用户空间 GPIO 驱动可能是
542系统真正需要的。
543
544注意:标准内核驱动中已经存在通用的“LED 和按键”GPIO 任务,分别是:
545"leds-gpio" 和 "gpio_keys"。请使用这些来替代直接访问 GPIO,因为集成在
546内核框架中的这类驱动比你在用户空间的代码更好。
547
548
549Sysfs 中的路径
550--------------
551在/sys/class/gpio 中有 3 类入口:
552
553 - 用于在用户空间控制 GPIO 的控制接口;
554
555 - GPIOs 本身;以及
556
557 - GPIO 控制器 ("gpio_chip" 实例)。
558
559除了这些标准的文件,还包含“device”符号链接。
560
561控制接口是只写的:
562
563 /sys/class/gpio/
564
565 "export" ... 用户空间可以通过写其编号到这个文件,要求内核导出
566 一个 GPIO 的控制到用户空间。
567
568 例如: 如果内核代码没有申请 GPIO #19,"echo 19 > export"
569 将会为 GPIO #19 创建一个 "gpio19" 节点。
570
571 "unexport" ... 导出到用户空间的逆操作。
572
573 例如: "echo 19 > unexport" 将会移除使用"export"文件导出的
574 "gpio19" 节点。
575
576GPIO 信号的路径类似 /sys/class/gpio/gpio42/ (对于 GPIO #42 来说),
577并有如下的读/写属性:
578
579 /sys/class/gpio/gpioN/
580
581 "direction" ... 读取得到 "in" 或 "out"。这个值通常运行写入。
582 写入"out" 时,其引脚的默认输出为低电平。为了确保无故障运行,
583 "low" 或 "high" 的电平值应该写入 GPIO 的配置,作为初始输出值。
584
585 注意:如果内核不支持改变 GPIO 的方向,或者在导出时内核代码没有
586 明确允许用户空间可以重新配置 GPIO 方向,那么这个属性将不存在。
587
588 "value" ... 读取得到 0 (低电平) 或 1 (高电平)。如果 GPIO 配置为
589 输出,这个值允许写操作。任何非零值都以高电平看待。
590
591 如果引脚可以配置为中断信号,且如果已经配置了产生中断的模式
592 (见"edge"的描述),你可以对这个文件使用轮询操作(poll(2)),
593 且轮询操作会在任何中断触发时返回。如果你使用轮询操作(poll(2)),
594 请在 events 中设置 POLLPRI 和 POLLERR。如果你使用轮询操作
595 (select(2)),请在 exceptfds 设置你期望的文件描述符。在
596 轮询操作(poll(2))返回之后,既可以通过 lseek(2)操作读取
597 sysfs 文件的开始部分,也可以关闭这个文件并重新打开它来读取数据。
598
599 "edge" ... 读取得到“none”、“rising”、“falling”或者“both”。
600 将这些字符串写入这个文件可以选择沿触发模式,会使得轮询操作
601 (select(2))在"value"文件中返回。
602
603 这个文件仅有在这个引脚可以配置为可产生中断输入引脚时,才存在。
604
605 "active_low" ... 读取得到 0 (假) 或 1 (真)。写入任何非零值可以
606 翻转这个属性的(读写)值。已存在或之后通过"edge"属性设置了"rising"
607 和 "falling" 沿触发模式的轮询操作(poll(2))将会遵循这个设置。
608
609GPIO 控制器的路径类似 /sys/class/gpio/gpiochip42/ (对于从#42 GPIO
610开始实现控制的控制器),并有着以下只读属性:
611
612 /sys/class/gpio/gpiochipN/
613
614 "base" ... 与以上的 N 相同,代表此芯片管理的第一个 GPIO 的编号
615
616 "label" ... 用于诊断 (并不总是只有唯一值)
617
618 "ngpio" ... 此控制器所管理的 GPIO 数量(而 GPIO 编号从 N 到
619 N + ngpio - 1)
620
621大多数情况下,电路板的文档应当标明每个 GPIO 的使用目的。但是那些编号并不总是
622固定的,例如在扩展卡上的 GPIO会根据所使用的主板或所在堆叠架构中其他的板子而
623有所不同。在这种情况下,你可能需要使用 gpiochip 节点(尽可能地结合电路图)来
624确定给定信号所用的 GPIO 编号。
625
626
627从内核代码中导出
628-------------
629内核代码可以明确地管理那些已通过 gpio_request()申请的 GPIO 的导出:
630
631 /* 导出 GPIO 到用户空间 */
632 int gpio_export(unsigned gpio, bool direction_may_change);
633
634 /* gpio_export()的逆操作 */
635 void gpio_unexport();
636
637 /* 创建一个 sysfs 连接到已导出的 GPIO 节点 */
638 int gpio_export_link(struct device *dev, const char *name,
639 unsigned gpio)
640
641 /* 改变 sysfs 中的一个 GPIO 节点的极性 */
642 int gpio_sysfs_set_active_low(unsigned gpio, int value);
643
644在一个内核驱动申请一个 GPIO 之后,它可以通过 gpio_export()使其在 sysfs
645接口中可见。该驱动可以控制信号方向是否可修改。这有助于防止用户空间代码无意间
646破坏重要的系统状态。
647
648这个明确的导出有助于(通过使某些实验更容易来)调试,也可以提供一个始终存在的接口,
649与文档配合作为板级支持包的一部分。
650
651在 GPIO 被导出之后,gpio_export_link()允许在 sysfs 文件系统的任何地方
652创建一个到这个 GPIO sysfs 节点的符号链接。这样驱动就可以通过一个描述性的
653名字,在 sysfs 中他们所拥有的设备下提供一个(到这个 GPIO sysfs 节点的)接口。
654
655驱动可以使用 gpio_sysfs_set_active_low() 来在用户空间隐藏电路板之间
656GPIO 线的极性差异。这个仅对 sysfs 接口起作用。极性的改变可以在 gpio_export()
657前后进行,且之前使能的轮询操作(poll(2))支持(上升或下降沿)将会被重新配置来遵循
658这个设置。
diff --git a/Documentation/zh_CN/video4linux/omap3isp.txt b/Documentation/zh_CN/video4linux/omap3isp.txt
new file mode 100644
index 00000000000..67ffbf352ae
--- /dev/null
+++ b/Documentation/zh_CN/video4linux/omap3isp.txt
@@ -0,0 +1,277 @@
1Chinese translated version of Documentation/video4linux/omap3isp.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Maintainer: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 Sakari Ailus <sakari.ailus@iki.fi>
11 David Cohen <dacohen@gmail.com>
12Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
13---------------------------------------------------------------------
14Documentation/video4linux/omap3isp.txt 的中文翻译
15
16如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
17交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
18译存在问题,请联系中文版维护者。
19英文版维护者: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
20 Sakari Ailus <sakari.ailus@iki.fi>
21 David Cohen <dacohen@gmail.com>
22中文版维护者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
23中文版翻译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
24中文版校译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
25
26
27以下为正文
28---------------------------------------------------------------------
29OMAP 3 图像信号处理器 (ISP) 驱动
30
31Copyright (C) 2010 Nokia Corporation
32Copyright (C) 2009 Texas Instruments, Inc.
33
34联系人: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
35 Sakari Ailus <sakari.ailus@iki.fi>
36 David Cohen <dacohen@gmail.com>
37
38
39介绍
40===
41
42本文档介绍了由 drivers/media/video/omap3isp 加载的德州仪器
43(TI)OMAP 3 图像信号处理器 (ISP) 驱动。原始驱动由德州仪器(TI)
44编写,但此后由诺基亚重写了两次。
45
46驱动已在以下 OMAP 3 系列的芯片中成功使用:
47
48 3430
49 3530
50 3630
51
52驱动实现了 V4L2、媒体控制器和 v4l2_subdev 接口。支持内核中使用
53v4l2_subdev 接口的传感器、镜头和闪光灯驱动。
54
55
56拆分为子设备
57==========
58
59OMAP 3 ISP 被拆分为 V4L2 子设备,ISP中的每个模块都由一个子设备
60来表示。每个子设备向用户空间提供一个 V4L2 子设备接口。
61
62 OMAP3 ISP CCP2
63 OMAP3 ISP CSI2a
64 OMAP3 ISP CCDC
65 OMAP3 ISP preview
66 OMAP3 ISP resizer
67 OMAP3 ISP AEWB
68 OMAP3 ISP AF
69 OMAP3 ISP histogram
70
71ISP 中每个可能的连接都通过一个链接嵌入到媒体控制器接口中。详见例程 [2]。
72
73
74控制 OMAP 3 ISP
75==============
76
77通常,对 OMAP 3 ISP 的配置会在下一帧起始时生效。在传感器垂直消隐期间,
78模块变为空闲时完成配置。在内存到内存的操作中,视频管道一次处理一帧。
79应用配置应在帧间完成。
80
81ISP 中的所有模块,除 CSI-2 和 (可能存在的)CCP2 接收器外,都必须
82接收完整的帧数据。因此,传感器必须保证从不发送部分帧数据给ISP。
83
84Autoidle(自动空闲)功能至少在 3430 的 ISP 模块中确实存在一些问题。
85当 omap3isp 模块参数 autoidle 非零时,autoidle(自动空闲)功能
86仅在 3630 中启用了。
87
88
89事件机制
90======
91
92OMAP 3 ISP 驱动在 CCDC 和统计(AEWB、AF 和 直方图)子设备中支持
93V4L2 事件机制接口。
94
95CCDC 子设备通过 HS_VS 中断,处理 V4L2_EVENT_FRAME_SYNC 类型
96事件,用于告知帧起始。早期版本的驱动则使用 V4L2_EVENT_OMAP3ISP_HS_VS。
97当在 CCDC 模块中接收到起始帧的第一行时,会准确地触发事件。这个事件
98可以在 CCDC 子设备中“订阅”。
99
100(当使用并行接口时,必须注意正确地配置 VS 信号极性。而当使用串行接收时
101这个会自动校正。)
102
103每个统计子设备都可以产生事件。每当一个统计缓冲区可由用户空间应用程序
104通过 VIDIOC_OMAP3ISP_STAT_REQ IOCTL 操作获取时,就会产生一个
105事件。当前存在以下事件:
106
107 V4L2_EVENT_OMAP3ISP_AEWB
108 V4L2_EVENT_OMAP3ISP_AF
109 V4L2_EVENT_OMAP3ISP_HIST
110
111这些 ioctl 的事件数据类型为 struct omap3isp_stat_event_status
112结构体。如果出现计算错误的统计,也同样会产生一个事件,但没有相关的统计
113数据缓冲区。这种情况下 omap3isp_stat_event_status.buf_err 会被
114设置为非零值。
115
116
117私有 IOCTL
118==========
119
120OMAP 3 ISP 驱动支持标准的 V4L2 IOCTL 以及可能存在且实用的控制。但
121ISP 提供的许多功能都不在标准 IOCTL 之列,例如 gamma(伽马)表和统计
122数据采集配置等。
123
124通常,会有一个私有 ioctl 用于配置每个包含硬件依赖功能的模块。
125
126支持以下私有 IOCTL:
127
128 VIDIOC_OMAP3ISP_CCDC_CFG
129 VIDIOC_OMAP3ISP_PRV_CFG
130 VIDIOC_OMAP3ISP_AEWB_CFG
131 VIDIOC_OMAP3ISP_HIST_CFG
132 VIDIOC_OMAP3ISP_AF_CFG
133 VIDIOC_OMAP3ISP_STAT_REQ
134 VIDIOC_OMAP3ISP_STAT_EN
135
136在 include/linux/omap3isp.h 中描述了这些 ioctl 使用的参数结构体。
137与特定 ISP 模块相关的 ISP 自身的详细功能在技术参考手册 (TRMs)中有
138描述,详见文档结尾。
139
140虽然在不使用任何私有 IOCTL 的情况下使用 ISP 驱动是可能的,但这样无法
141获得最佳的图像质量。AEWB、AF 和 直方图(译者注:一般用于自动曝光和增益
142控制,以及图像均衡等)模块无法在未使用适当的私有 IOCTL 配置的情况下使用。
143
144
145CCDC 和 preview(预览)模块 IOCTL
146===============================
147
148VIDIOC_OMAP3ISP_CCDC_CFG 和 VIDIOC_OMAP3ISP_PRV_CFG IOCTL
149被分别用于配置、启用和禁用 CCDC 和 preview(预览)模块的功能。在它们
150所控制的模块中,两个 IOCTL 控制多种功能。VIDIOC_OMAP3ISP_CCDC_CFG IOCTL
151接受一个指向 omap3isp_ccdc_update_config 结构体的指针作为它的参数。
152同样的,VIDIOC_OMAP3ISP_PRV_CFG 接受一个指向 omap3isp_prev_update_config
153结构体的指针。以上两个结构体定义位于 [1]。
154
155这些结构体中的 update 域标识是否针对指定的功能更新配置,而 flag 域
156则标识是启用还是禁用此功能。
157
158update 和 flag 位接受以下掩码值。CCDC 和 preview(预览)模块的
159每个单独功能都与一个 flag 关联(禁用或启用;在结构体中 flag 域的
160一部分)和一个指向功能配置数据的指针。
161
162对于 VIDIOC_OMAP3ISP_CCDC_CFG,下面列出了 update 和 flag 域
163中的有效值。 这些值可能会在同一个 IOCTL 调用中配置多个功能。
164
165 OMAP3ISP_CCDC_ALAW
166 OMAP3ISP_CCDC_LPF
167 OMAP3ISP_CCDC_BLCLAMP
168 OMAP3ISP_CCDC_BCOMP
169 OMAP3ISP_CCDC_FPC
170 OMAP3ISP_CCDC_CULL
171 OMAP3ISP_CCDC_CONFIG_LSC
172 OMAP3ISP_CCDC_TBL_LSC
173
174针对 VIDIOC_OMAP3ISP_PRV_CFG 的相应值如下:
175
176 OMAP3ISP_PREV_LUMAENH
177 OMAP3ISP_PREV_INVALAW
178 OMAP3ISP_PREV_HRZ_MED
179 OMAP3ISP_PREV_CFA
180 OMAP3ISP_PREV_CHROMA_SUPP
181 OMAP3ISP_PREV_WB
182 OMAP3ISP_PREV_BLKADJ
183 OMAP3ISP_PREV_RGB2RGB
184 OMAP3ISP_PREV_COLOR_CONV
185 OMAP3ISP_PREV_YC_LIMIT
186 OMAP3ISP_PREV_DEFECT_COR
187 OMAP3ISP_PREV_GAMMABYPASS
188 OMAP3ISP_PREV_DRK_FRM_CAPTURE
189 OMAP3ISP_PREV_DRK_FRM_SUBTRACT
190 OMAP3ISP_PREV_LENS_SHADING
191 OMAP3ISP_PREV_NF
192 OMAP3ISP_PREV_GAMMA
193
194在启用某个功能的时候,相关的配置数据指针不可为 NULL。在禁用某个功能时,
195配置数据指针会被忽略。
196
197
198统计模块 IOCTL
199=============
200
201统计子设备相较于其他子设备提供了更多动态配置选项。在图像处理流水线处于
202工作状态时,它们可以被启用、禁用和重配。
203
204统计模块总是从 CCDC 中获取输入的图像数据(由于直方图内存读取未实现)。
205统计数据可由用户通过统计子设备节点使用私有 IOCTL 获取。
206
207AEWB、AF 和 直方图子设备提供的私有 IOCTL 极大程度上反应了 ISP 硬件
208提供的寄存器级接口。有些方面纯粹和驱动程序的实现相关,这些将在下面讨论。
209
210VIDIOC_OMAP3ISP_STAT_EN
211-----------------------
212
213这个私有 IOCTL 启用/禁用 一个统计模块。如果这个申请在视频流启动前完成,
214它将在视频流水线开始工作时生效。如果视频流水线已经处于工作状态了,它将在
215CCDC 变为空闲时生效。
216
217VIDIOC_OMAP3ISP_AEWB_CFG, VIDIOC_OMAP3ISP_HIST_CFG and VIDIOC_OMAP3ISP_AF_CFG
218-----------------------------------------------------------------------------
219
220这些 IOCTL 用于配置模块。它们要求用户应用程序对硬件有深入的认识。对
221大多数域的解释可以在 OMAP 的 TRM 中找到。以下两个域对于以上所有的
222私有 IOCTL 配置都很常见,由于他们没有在 TRM 中提及,故需要对其有
223更好的认识。
224
225omap3isp_[h3a_af/h3a_aewb/hist]_config.buf_size:
226
227模块在内部处理自身缓冲。对模块数据输出所必需的缓存大小依赖于已申请的配置。
228虽然驱动支持在视频流工作时重新配置,但对于所需缓存量大于模块启用时内部
229所分配数量的情况,则不支持重新配置。在这种情况下将返回 -EBUSY。为了避免
230此类状况,无论是禁用/重配/启用模块,还是第一次配置时申请必须的缓存大小,
231都应在模块禁用的情况下进行。
232
233内部缓冲分配的大小需综合考虑所申请配置的最小缓存量以及 buf_size 域中
234所设的值。如果 buf_size 域在[minimum(最小值), maximum(最大值)]
235缓冲大小范围之外,则应该将其调整到其范围中。驱动则会选择最大值。正确的
236buf_size 值将回写到用户应用程序中。
237
238omap3isp_[h3a_af/h3a_aewb/hist]_config.config_counter:
239
240由于配置并未在申请之后同步生效,驱动必须提供一个跟踪这类信息的方法,
241以提供更准确的数据。在一个配置被申请之后,返回到用户空间应用程序的
242config_counter 是一个与其配置相关的唯一值。当用户应用程序接收到
243一个缓冲可用或一个新的缓冲申请事件时,这个 config_counter 用于
244一个缓冲数据和一个配置的匹配。
245
246VIDIOC_OMAP3ISP_STAT_REQ
247------------------------
248
249将内部缓冲队列中最早的数据发送到用户空间,然后丢弃此缓冲区。
250omap3isp_stat_data.frame_number 域与视频缓冲的 field_count
251域相匹配。
252
253
254技术参考手册 (TRMs) 和其他文档
255==========================
256
257OMAP 3430 TRM:
258<URL:http://focus.ti.com/pdfs/wtbu/OMAP34xx_ES3.1.x_PUBLIC_TRM_vZM.zip>
259参考于 2011-03-05.
260
261OMAP 35xx TRM:
262<URL:http://www.ti.com/litv/pdf/spruf98o> 参考于 2011-03-05.
263
264OMAP 3630 TRM:
265<URL:http://focus.ti.com/pdfs/wtbu/OMAP36xx_ES1.x_PUBLIC_TRM_vQ.zip>
266参考于 2011-03-05.
267
268DM 3730 TRM:
269<URL:http://www.ti.com/litv/pdf/sprugn4h> 参考于 2011-03-06.
270
271
272参考资料
273=======
274
275[1] include/linux/omap3isp.h
276
277[2] http://git.ideasonboard.org/?p=media-ctl.git;a=summary
diff --git a/Documentation/zh_CN/video4linux/v4l2-framework.txt b/Documentation/zh_CN/video4linux/v4l2-framework.txt
new file mode 100644
index 00000000000..3e74f13af42
--- /dev/null
+++ b/Documentation/zh_CN/video4linux/v4l2-framework.txt
@@ -0,0 +1,983 @@
1Chinese translated version of Documentation/video4linux/v4l2-framework.txt
2
3If you have any comment or update to the content, please contact the
4original document maintainer directly. However, if you have a problem
5communicating in English you can also ask the Chinese maintainer for
6help. Contact the Chinese maintainer if this translation is outdated
7or if there is a problem with the translation.
8
9Maintainer: Mauro Carvalho Chehab <mchehab@infradead.org>
10Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
11---------------------------------------------------------------------
12Documentation/video4linux/v4l2-framework.txt 的中文翻译
13
14如果想评论或更新本文的内容,请直接联系原文档的维护者。如果你使用英文
15交流有困难的话,也可以向中文版维护者求助。如果本翻译更新不及时或者翻
16译存在问题,请联系中文版维护者。
17英文版维护者: Mauro Carvalho Chehab <mchehab@infradead.org>
18中文版维护者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
19中文版翻译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
20中文版校译者: 傅炜 Fu Wei <tekkamanninja@gmail.com>
21
22
23以下为正文
24---------------------------------------------------------------------
25V4L2 驱动框架概览
26==============
27
28本文档描述 V4L2 框架所提供的各种结构和它们之间的关系。
29
30
31介绍
32----
33
34大部分现代 V4L2 设备由多个 IC 组成,在 /dev 下导出多个设备节点,
35并同时创建非 V4L2 设备(如 DVB、ALSA、FB、I2C 和红外输入设备)。
36由于这种硬件的复杂性,V4L2 驱动也变得非常复杂。
37
38尤其是 V4L2 必须支持 IC 实现音视频的多路复用和编解码,这就更增加了其
39复杂性。通常这些 IC 通过一个或多个 I2C 总线连接到主桥驱动器,但也可
40使用其他总线。这些设备称为“子设备”。
41
42长期以来,这个框架仅限于通过 video_device 结构体创建 V4L 设备节点,
43并使用 video_buf 处理视频缓冲(注:本文不讨论 video_buf 框架)。
44
45这意味着所有驱动必须自己设置设备实例并连接到子设备。其中一部分要正确地
46完成是比较复杂的,使得许多驱动都没有正确地实现。
47
48由于框架的缺失,有很多通用代码都不可重复利用。
49
50因此,这个框架构建所有驱动都需要的基本结构块,而统一的框架将使通用代码
51创建成实用函数并在所有驱动中共享变得更加容易。
52
53
54驱动结构
55-------
56
57所有 V4L2 驱动都有如下结构:
58
591) 每个设备实例的结构体--包含其设备状态。
60
612) 初始化和控制子设备的方法(如果有)。
62
633) 创建 V4L2 设备节点 (/dev/videoX、/dev/vbiX 和 /dev/radioX)
64 并跟踪设备节点的特定数据。
65
664) 特定文件句柄结构体--包含每个文件句柄的数据。
67
685) 视频缓冲处理。
69
70以下是它们的初略关系图:
71
72 device instances(设备实例)
73 |
74 +-sub-device instances(子设备实例)
75 |
76 \-V4L2 device nodes(V4L2 设备节点)
77 |
78 \-filehandle instances(文件句柄实例)
79
80
81框架结构
82-------
83
84该框架非常类似驱动结构:它有一个 v4l2_device 结构用于保存设备
85实例的数据;一个 v4l2_subdev 结构体代表子设备实例;video_device
86结构体保存 V4L2 设备节点的数据;将来 v4l2_fh 结构体将跟踪文件句柄
87实例(暂未尚未实现)。
88
89V4L2 框架也可与媒体框架整合(可选的)。如果驱动设置了 v4l2_device
90结构体的 mdev 域,子设备和视频节点的入口将自动出现在媒体框架中。
91
92
93v4l2_device 结构体
94----------------
95
96每个设备实例都通过 v4l2_device (v4l2-device.h)结构体来表示。
97简单设备可以仅分配这个结构体,但在大多数情况下,都会将这个结构体
98嵌入到一个更大的结构体中。
99
100你必须注册这个设备实例:
101
102 v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev);
103
104注册操作将会初始化 v4l2_device 结构体。如果 dev->driver_data 域
105为 NULL,就将其指向 v4l2_dev。
106
107需要与媒体框架整合的驱动必须手动设置 dev->driver_data,指向包含
108v4l2_device 结构体实例的驱动特定设备结构体。这可以在注册 V4L2 设备
109实例前通过 dev_set_drvdata() 函数完成。同时必须设置 v4l2_device
110结构体的 mdev 域,指向适当的初始化并注册过的 media_device 实例。
111
112如果 v4l2_dev->name 为空,则它将被设置为从 dev 中衍生出的值(为了
113更加精确,形式为驱动名后跟 bus_id)。如果你在调用 v4l2_device_register
114前已经设置好了,则不会被修改。如果 dev 为 NULL,则你*必须*在调用
115v4l2_device_register 前设置 v4l2_dev->name。
116
117你可以基于驱动名和驱动的全局 atomic_t 类型的实例编号,通过
118v4l2_device_set_name() 设置 name。这样会生成类似 ivtv0、ivtv1 等
119名字。若驱动名以数字结尾,则会在编号和驱动名间插入一个破折号,如:
120cx18-0、cx18-1 等。此函数返回实例编号。
121
122第一个 “dev” 参数通常是一个指向 pci_dev、usb_interface 或
123platform_device 的指针。很少使其为 NULL,除非是一个ISA设备或者
124当一个设备创建了多个 PCI 设备,使得 v4l2_dev 无法与一个特定的父设备
125关联。
126
127你也可以提供一个 notify() 回调,使子设备可以调用它实现事件通知。
128但这个设置与子设备相关。子设备支持的任何通知必须在
129include/media/<subdevice>.h 中定义一个消息头。
130
131注销 v4l2_device 使用如下函数:
132
133 v4l2_device_unregister(struct v4l2_device *v4l2_dev);
134
135如果 dev->driver_data 域指向 v4l2_dev,将会被重置为 NULL。注销同时
136会自动从设备中注销所有子设备。
137
138如果你有一个热插拔设备(如USB设备),则当断开发生时,父设备将无效。
139由于 v4l2_device 有一个指向父设备的指针必须被清除,同时标志父设备
140已消失,所以必须调用以下函数:
141
142 v4l2_device_disconnect(struct v4l2_device *v4l2_dev);
143
144这个函数并*不*注销子设备,因此你依然要调用 v4l2_device_unregister()
145函数。如果你的驱动器并非热插拔的,就没有必要调用 v4l2_device_disconnect()。
146
147有时你需要遍历所有被特定驱动注册的设备。这通常发生在多个设备驱动使用
148同一个硬件的情况下。如:ivtvfb 驱动是一个使用 ivtv 硬件的帧缓冲驱动,
149同时 alsa 驱动也使用此硬件。
150
151你可以使用如下例程遍历所有注册的设备:
152
153static int callback(struct device *dev, void *p)
154{
155 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
156
157 /* 测试这个设备是否已经初始化 */
158 if (v4l2_dev == NULL)
159 return 0;
160 ...
161 return 0;
162}
163
164int iterate(void *p)
165{
166 struct device_driver *drv;
167 int err;
168
169 /* 在PCI 总线上查找ivtv驱动。
170 pci_bus_type是全局的. 对于USB总线使用usb_bus_type。 */
171 drv = driver_find("ivtv", &pci_bus_type);
172 /* 遍历所有的ivtv设备实例 */
173 err = driver_for_each_device(drv, NULL, p, callback);
174 put_driver(drv);
175 return err;
176}
177
178有时你需要一个设备实例的运行计数。这个通常用于映射一个设备实例到一个
179模块选择数组的索引。
180
181推荐方法如下:
182
183static atomic_t drv_instance = ATOMIC_INIT(0);
184
185static int __devinit drv_probe(struct pci_dev *pdev,
186 const struct pci_device_id *pci_id)
187{
188 ...
189 state->instance = atomic_inc_return(&drv_instance) - 1;
190}
191
192如果你有多个设备节点,对于热插拔设备,知道何时注销 v4l2_device 结构体
193就比较困难。为此 v4l2_device 有引用计数支持。当调用 video_register_device
194时增加引用计数,而设备节点释放时减小引用计数。当引用计数为零,则
195v4l2_device 的release() 回调将被执行。你就可以在此时做最后的清理工作。
196
197如果创建了其他设备节点(比如 ALSA),则你可以通过以下函数手动增减
198引用计数:
199
200void v4l2_device_get(struct v4l2_device *v4l2_dev);
201
202或:
203
204int v4l2_device_put(struct v4l2_device *v4l2_dev);
205
206由于引用技术初始化为 1 ,你也需要在 disconnect() 回调(对于 USB 设备)中
207调用 v4l2_device_put,或者 remove() 回调(例如对于 PCI 设备),否则
208引用计数将永远不会为 0 。
209
210v4l2_subdev结构体
211------------------
212
213许多驱动需要与子设备通信。这些设备可以完成各种任务,但通常他们负责
214音视频复用和编解码。如网络摄像头的子设备通常是传感器和摄像头控制器。
215
216这些一般为 I2C 接口设备,但并不一定都是。为了给驱动提供调用子设备的
217统一接口,v4l2_subdev 结构体(v4l2-subdev.h)产生了。
218
219每个子设备驱动都必须有一个 v4l2_subdev 结构体。这个结构体可以单独
220代表一个简单的子设备,也可以嵌入到一个更大的结构体中,与更多设备状态
221信息保存在一起。通常有一个下级设备结构体(比如:i2c_client)包含了
222内核创建的设备数据。建议使用 v4l2_set_subdevdata() 将这个结构体的
223指针保存在 v4l2_subdev 的私有数据域(dev_priv)中。这使得通过 v4l2_subdev
224找到实际的低层总线特定设备数据变得容易。
225
226你同时需要一个从低层结构体获取 v4l2_subdev 指针的方法。对于常用的
227i2c_client 结构体,i2c_set_clientdata() 函数可用于保存一个 v4l2_subdev
228指针;对于其他总线你可能需要使用其他相关函数。
229
230桥驱动中也应保存每个子设备的私有数据,比如一个指向特定桥的各设备私有
231数据的指针。为此 v4l2_subdev 结构体提供主机私有数据域(host_priv),
232并可通过 v4l2_get_subdev_hostdata() 和 v4l2_set_subdev_hostdata()
233访问。
234
235从总线桥驱动的视角,驱动加载子设备模块并以某种方式获得 v4l2_subdev
236结构体指针。对于 i2c 总线设备相对简单:调用 i2c_get_clientdata()。
237对于其他总线也需要做类似的操作。针对 I2C 总线上的子设备辅助函数帮你
238完成了大部分复杂的工作。
239
240每个 v4l2_subdev 都包含子设备驱动需要实现的函数指针(如果对此设备
241不适用,可为NULL)。由于子设备可完成许多不同的工作,而在一个庞大的
242函数指针结构体中通常仅有少数有用的函数实现其功能肯定不合适。所以,
243函数指针根据其实现的功能被分类,每一类都有自己的函数指针结构体。
244
245顶层函数指针结构体包含了指向各类函数指针结构体的指针,如果子设备驱动
246不支持该类函数中的任何一个功能,则指向该类结构体的指针为NULL。
247
248这些结构体定义如下:
249
250struct v4l2_subdev_core_ops {
251 int (*g_chip_ident)(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip);
252 int (*log_status)(struct v4l2_subdev *sd);
253 int (*init)(struct v4l2_subdev *sd, u32 val);
254 ...
255};
256
257struct v4l2_subdev_tuner_ops {
258 ...
259};
260
261struct v4l2_subdev_audio_ops {
262 ...
263};
264
265struct v4l2_subdev_video_ops {
266 ...
267};
268
269struct v4l2_subdev_pad_ops {
270 ...
271};
272
273struct v4l2_subdev_ops {
274 const struct v4l2_subdev_core_ops *core;
275 const struct v4l2_subdev_tuner_ops *tuner;
276 const struct v4l2_subdev_audio_ops *audio;
277 const struct v4l2_subdev_video_ops *video;
278 const struct v4l2_subdev_pad_ops *video;
279};
280
281其中 core(核心)函数集通常可用于所有子设备,其他类别的实现依赖于
282子设备。如视频设备可能不支持音频操作函数,反之亦然。
283
284这样的设置在限制了函数指针数量的同时,还使增加新的操作函数和分类
285变得较为容易。
286
287子设备驱动可使用如下函数初始化 v4l2_subdev 结构体:
288
289 v4l2_subdev_init(sd, &ops);
290
291然后,你必须用一个唯一的名字初始化 subdev->name,并初始化模块的
292owner 域。若使用 i2c 辅助函数,这些都会帮你处理好。
293
294若需同媒体框架整合,你必须调用 media_entity_init() 初始化 v4l2_subdev
295结构体中的 media_entity 结构体(entity 域):
296
297 struct media_pad *pads = &my_sd->pads;
298 int err;
299
300 err = media_entity_init(&sd->entity, npads, pads, 0);
301
302pads 数组必须预先初始化。无须手动设置 media_entity 的 type 和
303name 域,但如有必要,revision 域必须初始化。
304
305当(任何)子设备节点被打开/关闭,对 entity 的引用将被自动获取/释放。
306
307在子设备被注销之后,不要忘记清理 media_entity 结构体:
308
309 media_entity_cleanup(&sd->entity);
310
311如果子设备驱动趋向于处理视频并整合进了媒体框架,必须使用 v4l2_subdev_pad_ops
312替代 v4l2_subdev_video_ops 实现格式相关的功能。
313
314这种情况下,子设备驱动应该设置 link_validate 域,以提供它自身的链接
315验证函数。链接验证函数应对管道(两端链接的都是 V4L2 子设备)中的每个
316链接调用。驱动还要负责验证子设备和视频节点间格式配置的正确性。
317
318如果 link_validate 操作没有设置,默认的 v4l2_subdev_link_validate_default()
319函数将会被调用。这个函数保证宽、高和媒体总线像素格式在链接的收发两端
320都一致。子设备驱动除了它们自己的检测外,也可以自由使用这个函数以执行
321上面提到的检查。
322
323设备(桥)驱动程序必须向 v4l2_device 注册 v4l2_subdev:
324
325 int err = v4l2_device_register_subdev(v4l2_dev, sd);
326
327如果子设备模块在它注册前消失,这个操作可能失败。在这个函数成功返回后,
328subdev->dev 域就指向了 v4l2_device。
329
330如果 v4l2_device 父设备的 mdev 域为非 NULL 值,则子设备实体将被自动
331注册为媒体设备。
332
333注销子设备则可用如下函数:
334
335 v4l2_device_unregister_subdev(sd);
336
337此后,子设备模块就可卸载,且 sd->dev == NULL。
338
339注册之设备后,可通过以下方式直接调用其操作函数:
340
341 err = sd->ops->core->g_chip_ident(sd, &chip);
342
343但使用如下宏会比较容易且合适:
344
345 err = v4l2_subdev_call(sd, core, g_chip_ident, &chip);
346
347这个宏将会做 NULL 指针检查,如果 subdev 为 NULL,则返回-ENODEV;如果
348subdev->core 或 subdev->core->g_chip_ident 为 NULL,则返回 -ENOIOCTLCMD;
349否则将返回 subdev->ops->core->g_chip_ident ops 调用的实际结果。
350
351有时也可能同时调用所有或一系列子设备的某个操作函数:
352
353 v4l2_device_call_all(v4l2_dev, 0, core, g_chip_ident, &chip);
354
355任何不支持此操作的子设备都会被跳过,并忽略错误返回值。但如果你需要
356检查出错码,则可使用如下函数:
357
358 err = v4l2_device_call_until_err(v4l2_dev, 0, core, g_chip_ident, &chip);
359
360除 -ENOIOCTLCMD 外的任何错误都会跳出循环并返回错误值。如果(除 -ENOIOCTLCMD
361外)没有错误发生,则返回 0。
362
363对于以上两个函数的第二个参数为组 ID。如果为 0,则所有子设备都会执行
364这个操作。如果为非 0 值,则只有那些组 ID 匹配的子设备才会执行此操作。
365在桥驱动注册一个子设备前,可以设置 sd->grp_id 为任何期望值(默认值为
3660)。这个值属于桥驱动,且子设备驱动将不会修改和使用它。
367
368组 ID 赋予了桥驱动更多对于如何调用回调的控制。例如,电路板上有多个
369音频芯片,每个都有改变音量的能力。但当用户想要改变音量的时候,通常
370只有一个会被实际使用。你可以对这样的子设备设置组 ID 为(例如 AUDIO_CONTROLLER)
371并在调用 v4l2_device_call_all() 时指定它为组 ID 值。这就保证了只有
372需要的子设备才会执行这个回调。
373
374如果子设备需要通知它的 v4l2_device 父设备一个事件,可以调用
375v4l2_subdev_notify(sd, notification, arg)。这个宏检查是否有一个
376notify() 回调被注册,如果没有,返回 -ENODEV。否则返回 notify() 调用
377结果。
378
379使用 v4l2_subdev 的好处在于它是一个通用结构体,且不包含任何底层硬件
380信息。所有驱动可以包含多个 I2C 总线的子设备,但也有子设备是通过 GPIO
381控制。这个区别仅在配置设备时有关系,一旦子设备注册完成,对于 v4l2
382子系统来说就完全透明了。
383
384
385V4L2 子设备用户空间API
386--------------------
387
388除了通过 v4l2_subdev_ops 结构导出的内核 API,V4L2 子设备也可以直接
389通过用户空间应用程序来控制。
390
391可以在 /dev 中创建名为 v4l-subdevX 设备节点,以通过其直接访问子设备。
392如果子设备支持用户空间直接配置,必须在注册前设置 V4L2_SUBDEV_FL_HAS_DEVNODE
393标志。
394
395注册子设备之后, v4l2_device 驱动会通过调用 v4l2_device_register_subdev_nodes()
396函数为所有已注册并设置了 V4L2_SUBDEV_FL_HAS_DEVNODE 的子设备创建
397设备节点。这些设备节点会在子设备注销时自动删除。
398
399这些设备节点处理 V4L2 API 的一个子集。
400
401VIDIOC_QUERYCTRL
402VIDIOC_QUERYMENU
403VIDIOC_G_CTRL
404VIDIOC_S_CTRL
405VIDIOC_G_EXT_CTRLS
406VIDIOC_S_EXT_CTRLS
407VIDIOC_TRY_EXT_CTRLS
408
409 这些 ioctls 控制与 V4L2 中定义的一致。他们行为相同,唯一的
410 不同是他们只处理子设备的控制实现。根据驱动程序,这些控制也
411 可以通过一个(或多个) V4L2 设备节点访问。
412
413VIDIOC_DQEVENT
414VIDIOC_SUBSCRIBE_EVENT
415VIDIOC_UNSUBSCRIBE_EVENT
416
417 这些 ioctls 事件与 V4L2 中定义的一致。他们行为相同,唯一的
418 不同是他们只处理子设备产生的事件。根据驱动程序,这些事件也
419 可以通过一个(或多个) V4L2 设备节点上报。
420
421 要使用事件通知的子设备驱动,在注册子设备前必须在 v4l2_subdev::flags
422 中设置 V4L2_SUBDEV_USES_EVENTS 并在 v4l2_subdev::nevents
423 中初始化事件队列深度。注册完成后,事件会在 v4l2_subdev::devnode
424 设备节点中像通常一样被排队。
425
426 为正确支持事件机制,poll() 文件操作也应被实现。
427
428私有 ioctls
429
430 不在以上列表中的所有 ioctls 会通过 core::ioctl 操作直接传递
431 给子设备驱动。
432
433
434I2C 子设备驱动
435-------------
436
437由于这些驱动很常见,所以内特提供了特定的辅助函数(v4l2-common.h)让这些
438设备的使用更加容易。
439
440添加 v4l2_subdev 支持的推荐方法是让 I2C 驱动将 v4l2_subdev 结构体
441嵌入到为每个 I2C 设备实例创建的状态结构体中。而最简单的设备没有状态
442结构体,此时可以直接创建一个 v4l2_subdev 结构体。
443
444一个典型的状态结构体如下所示(‘chipname’用芯片名代替):
445
446struct chipname_state {
447 struct v4l2_subdev sd;
448 ... /* 附加的状态域*/
449};
450
451初始化 v4l2_subdev 结构体的方法如下:
452
453 v4l2_i2c_subdev_init(&state->sd, client, subdev_ops);
454
455这个函数将填充 v4l2_subdev 结构体中的所有域,并保证 v4l2_subdev 和
456i2c_client 都指向彼此。
457
458同时,你也应该为从 v4l2_subdev 指针找到 chipname_state 结构体指针
459添加一个辅助内联函数。
460
461static inline struct chipname_state *to_state(struct v4l2_subdev *sd)
462{
463 return container_of(sd, struct chipname_state, sd);
464}
465
466使用以下函数可以通过 v4l2_subdev 结构体指针获得 i2c_client 结构体
467指针:
468
469 struct i2c_client *client = v4l2_get_subdevdata(sd);
470
471而以下函数则相反,通过 i2c_client 结构体指针获得 v4l2_subdev 结构体
472指针:
473
474 struct v4l2_subdev *sd = i2c_get_clientdata(client);
475
476当 remove()函数被调用前,必须保证先调用 v4l2_device_unregister_subdev(sd)。
477此操作将会从桥驱动中注销子设备。即使子设备没有注册,调用此函数也是
478安全的。
479
480必须这样做的原因是:当桥驱动注销 i2c 适配器时,remove()回调函数
481会被那个适配器上的 i2c 设备调用。此后,相应的 v4l2_subdev 结构体
482就不存在了,所有它们必须先被注销。在 remove()回调函数中调用
483v4l2_device_unregister_subdev(sd),可以保证执行总是正确的。
484
485
486桥驱动也有一些辅组函数可用:
487
488struct v4l2_subdev *sd = v4l2_i2c_new_subdev(v4l2_dev, adapter,
489 "module_foo", "chipid", 0x36, NULL);
490
491这个函数会加载给定的模块(如果没有模块需要加载,可以为 NULL),
492并用给定的 i2c 适配器结构体指针(i2c_adapter)和 器件地址(chip/address)
493作为参数调用 i2c_new_device()。如果一切顺利,则就在 v4l2_device
494中注册了子设备。
495
496你也可以利用 v4l2_i2c_new_subdev()的最后一个参数,传递一个可能的
497I2C 地址数组,让函数自动探测。这些探测地址只有在前一个参数为 0 的
498情况下使用。非零参数意味着你知道准确的 i2c 地址,所以此时无须进行
499探测。
500
501如果出错,两个函数都返回 NULL。
502
503注意:传递给 v4l2_i2c_new_subdev()的 chipid 通常与模块名一致。
504它允许你指定一个芯片的变体,比如“saa7114”或“saa7115”。一般通过
505i2c 驱动自动探测。chipid 的使用是在今后需要深入了解的事情。这个与
506i2c 驱动不同,较容易混淆。要知道支持哪些芯片变体,你可以查阅 i2c
507驱动代码的 i2c_device_id 表,上面列出了所有可能支持的芯片。
508
509还有两个辅助函数:
510
511v4l2_i2c_new_subdev_cfg:这个函数添加新的 irq 和 platform_data
512参数,并有‘addr’和‘probed_addrs’参数:如果 addr 非零,则被使用
513(不探测变体),否则 probed_addrs 中的地址将用于自动探测。
514
515例如:以下代码将会探测地址(0x10):
516
517struct v4l2_subdev *sd = v4l2_i2c_new_subdev_cfg(v4l2_dev, adapter,
518 "module_foo", "chipid", 0, NULL, 0, I2C_ADDRS(0x10));
519
520v4l2_i2c_new_subdev_board 使用一个 i2c_board_info 结构体,将其
521替代 irq、platform_data 和 add r参数传递给 i2c 驱动。
522
523如果子设备支持 s_config 核心操作,这个操作会在子设备配置好之后以 irq 和
524platform_data 为参数调用。早期的 v4l2_i2c_new_(probed_)subdev 函数
525同样也会调用 s_config,但仅在 irq 为 0 且 platform_data 为 NULL 时。
526
527video_device结构体
528-----------------
529
530在 /dev 目录下的实际设备节点根据 video_device 结构体(v4l2-dev.h)
531创建。此结构体既可以动态分配也可以嵌入到一个更大的结构体中。
532
533动态分配方法如下:
534
535 struct video_device *vdev = video_device_alloc();
536
537 if (vdev == NULL)
538 return -ENOMEM;
539
540 vdev->release = video_device_release;
541
542如果将其嵌入到一个大结构体中,则必须自己实现 release()回调。
543
544 struct video_device *vdev = &my_vdev->vdev;
545
546 vdev->release = my_vdev_release;
547
548release()回调必须被设置,且在最后一个 video_device 用户退出之后
549被调用。
550
551默认的 video_device_release()回调只是调用 kfree 来释放之前分配的
552内存。
553
554你应该设置这些域:
555
556- v4l2_dev: 设置为 v4l2_device 父设备。
557
558- name: 设置为唯一的描述性设备名。
559
560- fops: 设置为已有的 v4l2_file_operations 结构体。
561
562- ioctl_ops: 如果你使用v4l2_ioctl_ops 来简化 ioctl 的维护
563 (强烈建议使用,且将来可能变为强制性的!),然后设置你自己的
564 v4l2_ioctl_ops 结构体.
565
566- lock: 如果你要在驱动中实现所有的锁操作,则设为 NULL 。否则
567 就要设置一个指向 struct mutex_lock 结构体的指针,这个锁将
568 在 unlocked_ioctl 文件操作被调用前由内核获得,并在调用返回后
569 释放。详见下一节。
570
571- prio: 保持对优先级的跟踪。用于实现 VIDIOC_G/S_PRIORITY。如果
572 设置为 NULL,则会使用 v4l2_device 中的 v4l2_prio_state 结构体。
573 如果要对每个设备节点(组)实现独立的优先级,可以将其指向自己
574 实现的 v4l2_prio_state 结构体。
575
576- parent: 仅在使用 NULL 作为父设备结构体参数注册 v4l2_device 时
577 设置此参数。只有在一个硬件设备包含多一个 PCI 设备,共享同一个
578 v4l2_device 核心时才会发生。
579
580 cx88 驱动就是一个例子:一个 v4l2_device 结构体核心,被一个裸的
581 视频 PCI 设备(cx8800)和一个 MPEG PCI 设备(cx8802)共用。由于
582 v4l2_device 无法与特定的 PCI 设备关联,所有没有设置父设备。但当
583 video_device 配置后,就知道使用哪个父 PCI 设备了。
584
585- flags:可选。如果你要让框架处理设置 VIDIOC_G/S_PRIORITY ioctls,
586 请设置 V4L2_FL_USE_FH_PRIO。这要求你使用 v4l2_fh 结构体。
587 一旦所有驱动使用了核心的优先级处理,最终这个标志将消失。但现在它
588 必须被显式设置。
589
590如果你使用 v4l2_ioctl_ops,则应该在 v4l2_file_operations 结构体中
591设置 .unlocked_ioctl 指向 video_ioctl2。
592
593请勿使用 .ioctl!它已被废弃,今后将消失。
594
595某些情况下你要告诉核心:你在 v4l2_ioctl_ops 指定的某个函数应被忽略。
596你可以在 video_device_register 被调用前通过以下函数标记这个 ioctls。
597
598void v4l2_disable_ioctl(struct video_device *vdev, unsigned int cmd);
599
600基于外部因素(例如某个板卡已被使用),在不创建新结构体的情况下,你想
601要关闭 v4l2_ioctl_ops 中某个特性往往需要这个机制。
602
603v4l2_file_operations 结构体是 file_operations 的一个子集。其主要
604区别在于:因 inode 参数从未被使用,它将被忽略。
605
606如果需要与媒体框架整合,你必须通过调用 media_entity_init() 初始化
607嵌入在 video_device 结构体中的 media_entity(entity 域)结构体:
608
609 struct media_pad *pad = &my_vdev->pad;
610 int err;
611
612 err = media_entity_init(&vdev->entity, 1, pad, 0);
613
614pads 数组必须预先初始化。没有必要手动设置 media_entity 的 type 和
615name 域。
616
617当(任何)子设备节点被打开/关闭,对 entity 的引用将被自动获取/释放。
618
619v4l2_file_operations 与锁
620--------------------------
621
622你可以在 video_device 结构体中设置一个指向 mutex_lock 的指针。通常
623这既可是一个顶层互斥锁也可为设备节点自身的互斥锁。默认情况下,此锁
624用于 unlocked_ioctl,但为了使用 ioctls 你通过以下函数可禁用锁定:
625
626 void v4l2_disable_ioctl_locking(struct video_device *vdev, unsigned int cmd);
627
628例如: v4l2_disable_ioctl_locking(vdev, VIDIOC_DQBUF);
629
630你必须在注册 video_device 前调用这个函数。
631
632特别是对于 USB 驱动程序,某些命令(如设置控制)需要很长的时间,可能
633需要自行为缓冲区队列的 ioctls 实现锁定。
634
635如果你需要更细粒度的锁,你必须设置 mutex_lock 为 NULL,并完全自己实现
636锁机制。
637
638这完全由驱动开发者决定使用何种方法。然而,如果你的驱动存在长延时操作
639(例如,改变 USB 摄像头的曝光时间可能需要较长时间),而你又想让用户
640在等待长延时操作完成期间做其他的事,则你最好自己实现锁机制。
641
642如果指定一个锁,则所有 ioctl 操作将在这个锁的作用下串行执行。如果你
643使用 videobuf,则必须将同一个锁传递给 videobuf 队列初始化函数;如
644videobuf 必须等待一帧的到达,则可临时解锁并在这之后重新上锁。如果驱动
645也在代码执行期间等待,则可做同样的工作(临时解锁,再上锁)让其他进程
646可以在第一个进程阻塞时访问设备节点。
647
648在使用 videobuf2 的情况下,必须实现 wait_prepare 和 wait_finish 回调
649在适当的时候解锁/加锁。进一步来说,如果你在 video_device 结构体中使用
650锁,则必须在 wait_prepare 和 wait_finish 中对这个互斥锁进行解锁/加锁。
651
652热插拔的断开实现也必须在调用 v4l2_device_disconnect 前获得锁。
653
654video_device注册
655---------------
656
657接下来你需要注册视频设备:这会为你创建一个字符设备。
658
659 err = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
660 if (err) {
661 video_device_release(vdev); /* or kfree(my_vdev); */
662 return err;
663 }
664
665如果 v4l2_device 父设备的 mdev 域为非 NULL 值,视频设备实体将自动
666注册为媒体设备。
667
668注册哪种设备是根据类型(type)参数。存在以下类型:
669
670VFL_TYPE_GRABBER: 用于视频输入/输出设备的 videoX
671VFL_TYPE_VBI: 用于垂直消隐数据的 vbiX (例如,隐藏式字幕,图文电视)
672VFL_TYPE_RADIO: 用于广播调谐器的 radioX
673
674最后一个参数让你确定一个所控制设备的设备节点号数量(例如 videoX 中的 X)。
675通常你可以传入-1,让 v4l2 框架自己选择第一个空闲的编号。但是有时用户
676需要选择一个特定的节点号。驱动允许用户通过驱动模块参数选择一个特定的
677设备节点号是很普遍的。这个编号将会传递给这个函数,且 video_register_device
678将会试图选择这个设备节点号。如果这个编号被占用,下一个空闲的设备节点
679编号将被选中,并向内核日志中发送一个警告信息。
680
681另一个使用场景是当驱动创建多个设备时。这种情况下,对不同的视频设备在
682编号上使用不同的范围是很有用的。例如,视频捕获设备从 0 开始,视频
683输出设备从 16 开始。所以你可以使用最后一个参数来指定设备节点号最小值,
684而 v4l2 框架会试图选择第一个的空闲编号(等于或大于你提供的编号)。
685如果失败,则它会就选择第一个空闲的编号。
686
687由于这种情况下,你会忽略无法选择特定设备节点号的警告,则可调用
688video_register_device_no_warn() 函数避免警告信息的产生。
689
690只要设备节点被创建,一些属性也会同时创建。在 /sys/class/video4linux
691目录中你会找到这些设备。例如进入其中的 video0 目录,你会看到‘name’和
692‘index’属性。‘name’属性值就是 video_device 结构体中的‘name’域。
693
694‘index’属性值就是设备节点的索引值:每次调用 video_register_device(),
695索引值都递增 1 。第一个视频设备节点总是从索引值 0 开始。
696
697用户可以设置 udev 规则,利用索引属性生成花哨的设备名(例如:用‘mpegX’
698代表 MPEG 视频捕获设备节点)。
699
700在设备成功注册后,就可以使用这些域:
701
702- vfl_type: 传递给 video_register_device 的设备类型。
703- minor: 已指派的次设备号。
704- num: 设备节点编号 (例如 videoX 中的 X)。
705- index: 设备索引号。
706
707如果注册失败,你必须调用 video_device_release() 来释放已分配的
708video_device 结构体;如果 video_device 是嵌入在自己创建的结构体中,
709你也必须释放它。vdev->release() 回调不会在注册失败之后被调用,
710你也不应试图在注册失败后注销设备。
711
712
713video_device 注销
714----------------
715
716当视频设备节点已被移除,不论是卸载驱动还是USB设备断开,你都应注销
717它们:
718
719 video_unregister_device(vdev);
720
721这个操作将从 sysfs 中移除设备节点(导致 udev 将其从 /dev 中移除)。
722
723video_unregister_device() 返回之后,就无法完成打开操作。尽管如此,
724USB 设备的情况则不同,某些应用程序可能依然打开着其中一个已注销设备
725节点。所以在注销之后,所有文件操作(当然除了 release )也应返回错误值。
726
727当最后一个视频设备节点的用户退出,则 vdev->release() 回调会被调用,
728并且你可以做最后的清理操作。
729
730不要忘记清理与视频设备相关的媒体入口(如果被初始化过):
731
732 media_entity_cleanup(&vdev->entity);
733
734这可以在 release 回调中完成。
735
736
737video_device 辅助函数
738---------------------
739
740一些有用的辅助函数如下:
741
742- file/video_device 私有数据
743
744你可以用以下函数在 video_device 结构体中设置/获取驱动私有数据:
745
746void *video_get_drvdata(struct video_device *vdev);
747void video_set_drvdata(struct video_device *vdev, void *data);
748
749注意:在调用 video_register_device() 前执行 video_set_drvdata()
750是安全的。
751
752而以下函数:
753
754struct video_device *video_devdata(struct file *file);
755
756返回 file 结构体中拥有的的 video_device 指针。
757
758video_drvdata 辅助函数结合了 video_get_drvdata 和 video_devdata
759的功能:
760
761void *video_drvdata(struct file *file);
762
763你可以使用如下代码从 video_device 结构体中获取 v4l2_device 结构体
764指针:
765
766struct v4l2_device *v4l2_dev = vdev->v4l2_dev;
767
768- 设备节点名
769
770video_device 设备节点在内核中的名称可以通过以下函数获得
771
772const char *video_device_node_name(struct video_device *vdev);
773
774这个名字被用户空间工具(例如 udev)作为提示信息使用。应尽可能使用
775此功能,而非访问 video_device::num 和 video_device::minor 域。
776
777
778视频缓冲辅助函数
779---------------
780
781v4l2 核心 API 提供了一个处理视频缓冲的标准方法(称为“videobuf”)。
782这些方法使驱动可以通过统一的方式实现 read()、mmap() 和 overlay()。
783目前在设备上支持视频缓冲的方法有分散/聚集 DMA(videobuf-dma-sg)、
784线性 DMA(videobuf-dma-contig)以及大多用于 USB 设备的用 vmalloc
785分配的缓冲(videobuf-vmalloc)。
786
787请参阅 Documentation/video4linux/videobuf,以获得更多关于 videobuf
788层的使用信息。
789
790v4l2_fh 结构体
791-------------
792
793v4l2_fh 结构体提供一个保存用于 V4L2 框架的文件句柄特定数据的简单方法。
794如果 video_device 的 flag 设置了 V4L2_FL_USE_FH_PRIO 标志,新驱动
795必须使用 v4l2_fh 结构体,因为它也用于实现优先级处理(VIDIOC_G/S_PRIORITY)。
796
797v4l2_fh 的用户(位于 V4l2 框架中,并非驱动)可通过测试
798video_device->flags 中的 V4L2_FL_USES_V4L2_FH 位得知驱动是否使用
799v4l2_fh 作为他的 file->private_data 指针。这个位会在调用 v4l2_fh_init()
800时被设置。
801
802v4l2_fh 结构体作为驱动自身文件句柄结构体的一部分被分配,且驱动在
803其打开函数中将 file->private_data 指向它。
804
805在许多情况下,v4l2_fh 结构体会嵌入到一个更大的结构体中。这钟情况下,
806应该在 open() 中调用 v4l2_fh_init+v4l2_fh_add,并在 release() 中
807调用 v4l2_fh_del+v4l2_fh_exit。
808
809驱动可以通过使用 container_of 宏提取他们自己的文件句柄结构体。例如:
810
811struct my_fh {
812 int blah;
813 struct v4l2_fh fh;
814};
815
816...
817
818int my_open(struct file *file)
819{
820 struct my_fh *my_fh;
821 struct video_device *vfd;
822 int ret;
823
824 ...
825
826 my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);
827
828 ...
829
830 v4l2_fh_init(&my_fh->fh, vfd);
831
832 ...
833
834 file->private_data = &my_fh->fh;
835 v4l2_fh_add(&my_fh->fh);
836 return 0;
837}
838
839int my_release(struct file *file)
840{
841 struct v4l2_fh *fh = file->private_data;
842 struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
843
844 ...
845 v4l2_fh_del(&my_fh->fh);
846 v4l2_fh_exit(&my_fh->fh);
847 kfree(my_fh);
848 return 0;
849}
850
851以下是 v4l2_fh 函数使用的简介:
852
853void v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev)
854
855 初始化文件句柄。这*必须*在驱动的 v4l2_file_operations->open()
856 函数中执行。
857
858void v4l2_fh_add(struct v4l2_fh *fh)
859
860 添加一个 v4l2_fh 到 video_device 文件句柄列表。一旦文件句柄
861 初始化完成就必须调用。
862
863void v4l2_fh_del(struct v4l2_fh *fh)
864
865 从 video_device() 中解除文件句柄的关联。文件句柄的退出函数也
866 将被调用。
867
868void v4l2_fh_exit(struct v4l2_fh *fh)
869
870 清理文件句柄。在清理完 v4l2_fh 后,相关内存会被释放。
871
872
873如果 v4l2_fh 不是嵌入在其他结构体中的,则可以用这些辅助函数:
874
875int v4l2_fh_open(struct file *filp)
876
877 分配一个 v4l2_fh 结构体空间,初始化并将其添加到 file 结构体相关的
878 video_device 结构体中。
879
880int v4l2_fh_release(struct file *filp)
881
882 从 file 结构体相关的 video_device 结构体中删除 v4l2_fh ,清理
883 v4l2_fh 并释放空间。
884
885这两个函数可以插入到 v4l2_file_operation 的 open() 和 release()
886操作中。
887
888
889某些驱动需要在第一个文件句柄打开和最后一个文件句柄关闭的时候做些
890工作。所以加入了两个辅助函数以检查 v4l2_fh 结构体是否是相关设备
891节点打开的唯一文件句柄。
892
893int v4l2_fh_is_singular(struct v4l2_fh *fh)
894
895 如果此文件句柄是唯一打开的文件句柄,则返回 1 ,否则返回 0 。
896
897int v4l2_fh_is_singular_file(struct file *filp)
898
899 功能相同,但通过 filp->private_data 调用 v4l2_fh_is_singular。
900
901
902V4L2 事件机制
903-----------
904
905V4L2 事件机制提供了一个通用的方法将事件传递到用户空间。驱动必须使用
906v4l2_fh 才能支持 V4L2 事件机制。
907
908
909事件通过一个类型和选择 ID 来定义。ID 对应一个 V4L2 对象,例如
910一个控制 ID。如果未使用,则 ID 为 0。
911
912当用户订阅一个事件,驱动会为此分配一些 kevent 结构体。所以每个
913事件组(类型、ID)都会有自己的一套 kevent 结构体。这保证了如果
914一个驱动短时间内产生了许多同类事件,不会覆盖其他类型的事件。
915
916但如果你收到的事件数量大于同类事件 kevent 的保存数量,则最早的
917事件将被丢弃,并加入新事件。
918
919此外,v4l2_subscribed_event 结构体内部有可供驱动设置的 merge() 和
920replace() 回调,这些回调会在新事件产生且没有多余空间的时候被调用。
921replace() 回调让你可以将早期事件的净荷替换为新事件的净荷,将早期
922净荷的相关数据合并到替换进来的新净荷中。当该类型的事件仅分配了一个
923kevent 结构体时,它将被调用。merge() 回调让你可以合并最早的事件净荷
924到在它之后的那个事件净荷中。当该类型的事件分配了两个或更多 kevent
925结构体时,它将被调用。
926
927这种方法不会有状态信息丢失,只会导致中间步骤信息丢失。
928
929
930关于 replace/merge 回调的一个不错的例子在 v4l2-event.c 中:用于
931控制事件的 ctrls_replace() 和 ctrls_merge() 回调。
932
933注意:这些回调可以在中断上下文中调用,所以它们必须尽快完成并退出。
934
935有用的函数:
936
937void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
938
939 将事件加入视频设备的队列。驱动仅负责填充 type 和 data 域。
940 其他域由 V4L2 填充。
941
942int v4l2_event_subscribe(struct v4l2_fh *fh,
943 struct v4l2_event_subscription *sub, unsigned elems,
944 const struct v4l2_subscribed_event_ops *ops)
945
946 video_device->ioctl_ops->vidioc_subscribe_event 必须检测驱动能
947 产生特定 id 的事件。然后调用 v4l2_event_subscribe() 来订阅该事件。
948
949 elems 参数是该事件的队列大小。若为 0,V4L2 框架将会(根据事件类型)
950 填充默认值。
951
952 ops 参数允许驱动指定一系列回调:
953 * add: 当添加一个新监听者时调用(重复订阅同一个事件,此回调
954 仅被执行一次)。
955 * del: 当一个监听者停止监听时调用。
956 * replace: 用‘新’事件替换‘早期‘事件。
957 * merge: 将‘早期‘事件合并到‘新’事件中。
958 这四个调用都是可选的,如果不想指定任何回调,则 ops 可为 NULL。
959
960int v4l2_event_unsubscribe(struct v4l2_fh *fh,
961 struct v4l2_event_subscription *sub)
962
963 v4l2_ioctl_ops 结构体中的 vidioc_unsubscribe_event 回调函数。
964 驱动程序可以直接使用 v4l2_event_unsubscribe() 实现退订事件过程。
965
966 特殊的 V4L2_EVENT_ALL 类型,可用于退订所有事件。驱动可能在特殊
967 情况下需要做此操作。
968
969int v4l2_event_pending(struct v4l2_fh *fh)
970
971 返回未决事件的数量。有助于实现轮询(poll)操作。
972
973事件通过 poll 系统调用传递到用户空间。驱动可用
974v4l2_fh->wait (wait_queue_head_t 类型)作为参数调用 poll_wait()。
975
976事件分为标准事件和私有事件。新的标准事件必须使用可用的最小事件类型
977编号。驱动必须从他们本类型的编号起始处分配事件。类型的编号起始为
978V4L2_EVENT_PRIVATE_START + n * 1000 ,其中 n 为可用最小编号。每个
979类型中的第一个事件类型编号是为以后的使用保留的,所以第一个可用事件
980类型编号是‘class base + 1’。
981
982V4L2 事件机制的使用实例可以在 OMAP3 ISP 的驱动
983(drivers/media/video/omap3isp)中找到。
diff --git a/MAINTAINERS b/MAINTAINERS
index c98b1a1520d..0318e626434 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2865,7 +2865,9 @@ F: include/linux/firewire*.h
2865F: tools/firewire/ 2865F: tools/firewire/
2866 2866
2867FIRMWARE LOADER (request_firmware) 2867FIRMWARE LOADER (request_firmware)
2868S: Orphan 2868M: Ming Lei <ming.lei@canonical.com>
2869L: linux-kernel@vger.kernel.org
2870S: Maintained
2869F: Documentation/firmware_class/ 2871F: Documentation/firmware_class/
2870F: drivers/base/firmware*.c 2872F: drivers/base/firmware*.c
2871F: include/linux/firmware.h 2873F: include/linux/firmware.h
diff --git a/arch/tile/Kconfig b/arch/tile/Kconfig
index 932e4430f7f..c9a3c1fe729 100644
--- a/arch/tile/Kconfig
+++ b/arch/tile/Kconfig
@@ -412,14 +412,6 @@ config TILE_USB
412config NEED_BOUNCE_POOL 412config NEED_BOUNCE_POOL
413 def_bool USB_OHCI_HCD 413 def_bool USB_OHCI_HCD
414 414
415config HOTPLUG
416 bool "Support for hot-pluggable devices"
417 ---help---
418 Say Y here if you want to plug devices into your computer while
419 the system is running, and be able to use them quickly. In many
420 cases, the devices can likewise be unplugged at any time too.
421 One well-known example of this is USB.
422
423source "drivers/pci/hotplug/Kconfig" 415source "drivers/pci/hotplug/Kconfig"
424 416
425endmenu 417endmenu
diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig
index 8ed64cfae4f..744f5ee4ba4 100644
--- a/arch/xtensa/Kconfig
+++ b/arch/xtensa/Kconfig
@@ -172,24 +172,6 @@ config CMDLINE
172 172
173source "mm/Kconfig" 173source "mm/Kconfig"
174 174
175config HOTPLUG
176 bool "Support for hot-pluggable devices"
177 help
178 Say Y here if you want to plug devices into your computer while
179 the system is running, and be able to use them quickly. In many
180 cases, the devices can likewise be unplugged at any time too.
181
182 One well known example of this is PCMCIA- or PC-cards, credit-card
183 size devices such as network cards, modems or hard drives which are
184 plugged into slots found on all modern laptop computers. Another
185 example, used on modern desktops as well as laptops, is USB.
186
187 Enable HOTPLUG and build a modular kernel. Get agent software
188 (from <http://linux-hotplug.sourceforge.net/>) and install it.
189 Then your kernel will automatically call out to a user mode "policy
190 agent" (/sbin/hotplug) to load modules and set up software needed
191 to use devices as you hotplug them.
192
193source "drivers/pcmcia/Kconfig" 175source "drivers/pcmcia/Kconfig"
194 176
195source "drivers/pci/hotplug/Kconfig" 177source "drivers/pci/hotplug/Kconfig"
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 5e6e00bc165..abea76c36a4 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -184,6 +184,17 @@ static void device_release(struct kobject *kobj)
184 struct device *dev = kobj_to_dev(kobj); 184 struct device *dev = kobj_to_dev(kobj);
185 struct device_private *p = dev->p; 185 struct device_private *p = dev->p;
186 186
187 /*
188 * Some platform devices are driven without driver attached
189 * and managed resources may have been acquired. Make sure
190 * all resources are released.
191 *
192 * Drivers still can add resources into device after device
193 * is deleted but alive, so release devres here to avoid
194 * possible memory leak.
195 */
196 devres_release_all(dev);
197
187 if (dev->release) 198 if (dev->release)
188 dev->release(dev); 199 dev->release(dev);
189 else if (dev->type && dev->type->release) 200 else if (dev->type && dev->type->release)
@@ -1196,13 +1207,6 @@ void device_del(struct device *dev)
1196 bus_remove_device(dev); 1207 bus_remove_device(dev);
1197 driver_deferred_probe_del(dev); 1208 driver_deferred_probe_del(dev);
1198 1209
1199 /*
1200 * Some platform devices are driven without driver attached
1201 * and managed resources may have been acquired. Make sure
1202 * all resources are released.
1203 */
1204 devres_release_all(dev);
1205
1206 /* Notify the platform of the removal, in case they 1210 /* Notify the platform of the removal, in case they
1207 * need to do anything... 1211 * need to do anything...
1208 */ 1212 */
@@ -1861,26 +1865,20 @@ void device_shutdown(void)
1861 */ 1865 */
1862 1866
1863#ifdef CONFIG_PRINTK 1867#ifdef CONFIG_PRINTK
1864int __dev_printk(const char *level, const struct device *dev, 1868static int
1865 struct va_format *vaf) 1869create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
1866{ 1870{
1867 char dict[128];
1868 const char *level_extra = "";
1869 size_t dictlen = 0;
1870 const char *subsys; 1871 const char *subsys;
1871 1872 size_t pos = 0;
1872 if (!dev)
1873 return printk("%s(NULL device *): %pV", level, vaf);
1874 1873
1875 if (dev->class) 1874 if (dev->class)
1876 subsys = dev->class->name; 1875 subsys = dev->class->name;
1877 else if (dev->bus) 1876 else if (dev->bus)
1878 subsys = dev->bus->name; 1877 subsys = dev->bus->name;
1879 else 1878 else
1880 goto skip; 1879 return 0;
1881 1880
1882 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen, 1881 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
1883 "SUBSYSTEM=%s", subsys);
1884 1882
1885 /* 1883 /*
1886 * Add device identifier DEVICE=: 1884 * Add device identifier DEVICE=:
@@ -1896,32 +1894,63 @@ int __dev_printk(const char *level, const struct device *dev,
1896 c = 'b'; 1894 c = 'b';
1897 else 1895 else
1898 c = 'c'; 1896 c = 'c';
1899 dictlen++; 1897 pos++;
1900 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen, 1898 pos += snprintf(hdr + pos, hdrlen - pos,
1901 "DEVICE=%c%u:%u", 1899 "DEVICE=%c%u:%u",
1902 c, MAJOR(dev->devt), MINOR(dev->devt)); 1900 c, MAJOR(dev->devt), MINOR(dev->devt));
1903 } else if (strcmp(subsys, "net") == 0) { 1901 } else if (strcmp(subsys, "net") == 0) {
1904 struct net_device *net = to_net_dev(dev); 1902 struct net_device *net = to_net_dev(dev);
1905 1903
1906 dictlen++; 1904 pos++;
1907 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen, 1905 pos += snprintf(hdr + pos, hdrlen - pos,
1908 "DEVICE=n%u", net->ifindex); 1906 "DEVICE=n%u", net->ifindex);
1909 } else { 1907 } else {
1910 dictlen++; 1908 pos++;
1911 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen, 1909 pos += snprintf(hdr + pos, hdrlen - pos,
1912 "DEVICE=+%s:%s", subsys, dev_name(dev)); 1910 "DEVICE=+%s:%s", subsys, dev_name(dev));
1913 } 1911 }
1914skip:
1915 if (level[2])
1916 level_extra = &level[2]; /* skip past KERN_SOH "L" */
1917 1912
1918 return printk_emit(0, level[1] - '0', 1913 return pos;
1919 dictlen ? dict : NULL, dictlen, 1914}
1920 "%s %s: %s%pV", 1915EXPORT_SYMBOL(create_syslog_header);
1921 dev_driver_string(dev), dev_name(dev), 1916
1922 level_extra, vaf); 1917int dev_vprintk_emit(int level, const struct device *dev,
1918 const char *fmt, va_list args)
1919{
1920 char hdr[128];
1921 size_t hdrlen;
1922
1923 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
1924
1925 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
1926}
1927EXPORT_SYMBOL(dev_vprintk_emit);
1928
1929int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1930{
1931 va_list args;
1932 int r;
1933
1934 va_start(args, fmt);
1935
1936 r = dev_vprintk_emit(level, dev, fmt, args);
1937
1938 va_end(args);
1939
1940 return r;
1941}
1942EXPORT_SYMBOL(dev_printk_emit);
1943
1944static int __dev_printk(const char *level, const struct device *dev,
1945 struct va_format *vaf)
1946{
1947 if (!dev)
1948 return printk("%s(NULL device *): %pV", level, vaf);
1949
1950 return dev_printk_emit(level[1] - '0', dev,
1951 "%s %s: %pV",
1952 dev_driver_string(dev), dev_name(dev), vaf);
1923} 1953}
1924EXPORT_SYMBOL(__dev_printk);
1925 1954
1926int dev_printk(const char *level, const struct device *dev, 1955int dev_printk(const char *level, const struct device *dev,
1927 const char *fmt, ...) 1956 const char *fmt, ...)
@@ -1936,6 +1965,7 @@ int dev_printk(const char *level, const struct device *dev,
1936 vaf.va = &args; 1965 vaf.va = &args;
1937 1966
1938 r = __dev_printk(level, dev, &vaf); 1967 r = __dev_printk(level, dev, &vaf);
1968
1939 va_end(args); 1969 va_end(args);
1940 1970
1941 return r; 1971 return r;
@@ -1955,6 +1985,7 @@ int func(const struct device *dev, const char *fmt, ...) \
1955 vaf.va = &args; \ 1985 vaf.va = &args; \
1956 \ 1986 \
1957 r = __dev_printk(kern_level, dev, &vaf); \ 1987 r = __dev_printk(kern_level, dev, &vaf); \
1988 \
1958 va_end(args); \ 1989 va_end(args); \
1959 \ 1990 \
1960 return r; \ 1991 return r; \
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 2360adb7a58..8731979d668 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -144,6 +144,48 @@ EXPORT_SYMBOL_GPL(devres_alloc);
144#endif 144#endif
145 145
146/** 146/**
147 * devres_for_each_res - Resource iterator
148 * @dev: Device to iterate resource from
149 * @release: Look for resources associated with this release function
150 * @match: Match function (optional)
151 * @match_data: Data for the match function
152 * @fn: Function to be called for each matched resource.
153 * @data: Data for @fn, the 3rd parameter of @fn
154 *
155 * Call @fn for each devres of @dev which is associated with @release
156 * and for which @match returns 1.
157 *
158 * RETURNS:
159 * void
160 */
161void devres_for_each_res(struct device *dev, dr_release_t release,
162 dr_match_t match, void *match_data,
163 void (*fn)(struct device *, void *, void *),
164 void *data)
165{
166 struct devres_node *node;
167 struct devres_node *tmp;
168 unsigned long flags;
169
170 if (!fn)
171 return;
172
173 spin_lock_irqsave(&dev->devres_lock, flags);
174 list_for_each_entry_safe_reverse(node, tmp,
175 &dev->devres_head, entry) {
176 struct devres *dr = container_of(node, struct devres, node);
177
178 if (node->release != release)
179 continue;
180 if (match && !match(dev, dr->data, match_data))
181 continue;
182 fn(dev, dr->data, data);
183 }
184 spin_unlock_irqrestore(&dev->devres_lock, flags);
185}
186EXPORT_SYMBOL_GPL(devres_for_each_res);
187
188/**
147 * devres_free - Free device resource data 189 * devres_free - Free device resource data
148 * @res: Pointer to devres data to free 190 * @res: Pointer to devres data to free
149 * 191 *
diff --git a/drivers/base/firmware_class.c b/drivers/base/firmware_class.c
index 803cfc1597a..6e210802c37 100644
--- a/drivers/base/firmware_class.c
+++ b/drivers/base/firmware_class.c
@@ -21,6 +21,13 @@
21#include <linux/firmware.h> 21#include <linux/firmware.h>
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/sched.h> 23#include <linux/sched.h>
24#include <linux/list.h>
25#include <linux/async.h>
26#include <linux/pm.h>
27#include <linux/suspend.h>
28#include <linux/syscore_ops.h>
29
30#include "base.h"
24 31
25MODULE_AUTHOR("Manuel Estrada Sainz"); 32MODULE_AUTHOR("Manuel Estrada Sainz");
26MODULE_DESCRIPTION("Multi purpose firmware loading support"); 33MODULE_DESCRIPTION("Multi purpose firmware loading support");
@@ -85,23 +92,168 @@ static inline long firmware_loading_timeout(void)
85 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT; 92 return loading_timeout > 0 ? loading_timeout * HZ : MAX_SCHEDULE_TIMEOUT;
86} 93}
87 94
88/* fw_lock could be moved to 'struct firmware_priv' but since it is just 95struct firmware_cache {
89 * guarding for corner cases a global lock should be OK */ 96 /* firmware_buf instance will be added into the below list */
90static DEFINE_MUTEX(fw_lock); 97 spinlock_t lock;
98 struct list_head head;
99 int state;
100
101#ifdef CONFIG_PM_SLEEP
102 /*
103 * Names of firmware images which have been cached successfully
104 * will be added into the below list so that device uncache
105 * helper can trace which firmware images have been cached
106 * before.
107 */
108 spinlock_t name_lock;
109 struct list_head fw_names;
110
111 wait_queue_head_t wait_queue;
112 int cnt;
113 struct delayed_work work;
114
115 struct notifier_block pm_notify;
116#endif
117};
91 118
92struct firmware_priv { 119struct firmware_buf {
120 struct kref ref;
121 struct list_head list;
93 struct completion completion; 122 struct completion completion;
94 struct firmware *fw; 123 struct firmware_cache *fwc;
95 unsigned long status; 124 unsigned long status;
125 void *data;
126 size_t size;
96 struct page **pages; 127 struct page **pages;
97 int nr_pages; 128 int nr_pages;
98 int page_array_size; 129 int page_array_size;
130 char fw_id[];
131};
132
133struct fw_cache_entry {
134 struct list_head list;
135 char name[];
136};
137
138struct firmware_priv {
99 struct timer_list timeout; 139 struct timer_list timeout;
100 struct device dev;
101 bool nowait; 140 bool nowait;
102 char fw_id[]; 141 struct device dev;
142 struct firmware_buf *buf;
143 struct firmware *fw;
144};
145
146struct fw_name_devm {
147 unsigned long magic;
148 char name[];
103}; 149};
104 150
151#define to_fwbuf(d) container_of(d, struct firmware_buf, ref)
152
153#define FW_LOADER_NO_CACHE 0
154#define FW_LOADER_START_CACHE 1
155
156static int fw_cache_piggyback_on_request(const char *name);
157
158/* fw_lock could be moved to 'struct firmware_priv' but since it is just
159 * guarding for corner cases a global lock should be OK */
160static DEFINE_MUTEX(fw_lock);
161
162static struct firmware_cache fw_cache;
163
164static struct firmware_buf *__allocate_fw_buf(const char *fw_name,
165 struct firmware_cache *fwc)
166{
167 struct firmware_buf *buf;
168
169 buf = kzalloc(sizeof(*buf) + strlen(fw_name) + 1 , GFP_ATOMIC);
170
171 if (!buf)
172 return buf;
173
174 kref_init(&buf->ref);
175 strcpy(buf->fw_id, fw_name);
176 buf->fwc = fwc;
177 init_completion(&buf->completion);
178
179 pr_debug("%s: fw-%s buf=%p\n", __func__, fw_name, buf);
180
181 return buf;
182}
183
184static struct firmware_buf *__fw_lookup_buf(const char *fw_name)
185{
186 struct firmware_buf *tmp;
187 struct firmware_cache *fwc = &fw_cache;
188
189 list_for_each_entry(tmp, &fwc->head, list)
190 if (!strcmp(tmp->fw_id, fw_name))
191 return tmp;
192 return NULL;
193}
194
195static int fw_lookup_and_allocate_buf(const char *fw_name,
196 struct firmware_cache *fwc,
197 struct firmware_buf **buf)
198{
199 struct firmware_buf *tmp;
200
201 spin_lock(&fwc->lock);
202 tmp = __fw_lookup_buf(fw_name);
203 if (tmp) {
204 kref_get(&tmp->ref);
205 spin_unlock(&fwc->lock);
206 *buf = tmp;
207 return 1;
208 }
209 tmp = __allocate_fw_buf(fw_name, fwc);
210 if (tmp)
211 list_add(&tmp->list, &fwc->head);
212 spin_unlock(&fwc->lock);
213
214 *buf = tmp;
215
216 return tmp ? 0 : -ENOMEM;
217}
218
219static struct firmware_buf *fw_lookup_buf(const char *fw_name)
220{
221 struct firmware_buf *tmp;
222 struct firmware_cache *fwc = &fw_cache;
223
224 spin_lock(&fwc->lock);
225 tmp = __fw_lookup_buf(fw_name);
226 spin_unlock(&fwc->lock);
227
228 return tmp;
229}
230
231static void __fw_free_buf(struct kref *ref)
232{
233 struct firmware_buf *buf = to_fwbuf(ref);
234 struct firmware_cache *fwc = buf->fwc;
235 int i;
236
237 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
238 __func__, buf->fw_id, buf, buf->data,
239 (unsigned int)buf->size);
240
241 spin_lock(&fwc->lock);
242 list_del(&buf->list);
243 spin_unlock(&fwc->lock);
244
245 vunmap(buf->data);
246 for (i = 0; i < buf->nr_pages; i++)
247 __free_page(buf->pages[i]);
248 kfree(buf->pages);
249 kfree(buf);
250}
251
252static void fw_free_buf(struct firmware_buf *buf)
253{
254 kref_put(&buf->ref, __fw_free_buf);
255}
256
105static struct firmware_priv *to_firmware_priv(struct device *dev) 257static struct firmware_priv *to_firmware_priv(struct device *dev)
106{ 258{
107 return container_of(dev, struct firmware_priv, dev); 259 return container_of(dev, struct firmware_priv, dev);
@@ -109,9 +261,10 @@ static struct firmware_priv *to_firmware_priv(struct device *dev)
109 261
110static void fw_load_abort(struct firmware_priv *fw_priv) 262static void fw_load_abort(struct firmware_priv *fw_priv)
111{ 263{
112 set_bit(FW_STATUS_ABORT, &fw_priv->status); 264 struct firmware_buf *buf = fw_priv->buf;
113 wmb(); 265
114 complete(&fw_priv->completion); 266 set_bit(FW_STATUS_ABORT, &buf->status);
267 complete_all(&buf->completion);
115} 268}
116 269
117static ssize_t firmware_timeout_show(struct class *class, 270static ssize_t firmware_timeout_show(struct class *class,
@@ -154,11 +307,7 @@ static struct class_attribute firmware_class_attrs[] = {
154static void fw_dev_release(struct device *dev) 307static void fw_dev_release(struct device *dev)
155{ 308{
156 struct firmware_priv *fw_priv = to_firmware_priv(dev); 309 struct firmware_priv *fw_priv = to_firmware_priv(dev);
157 int i;
158 310
159 for (i = 0; i < fw_priv->nr_pages; i++)
160 __free_page(fw_priv->pages[i]);
161 kfree(fw_priv->pages);
162 kfree(fw_priv); 311 kfree(fw_priv);
163 312
164 module_put(THIS_MODULE); 313 module_put(THIS_MODULE);
@@ -168,7 +317,7 @@ static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env)
168{ 317{
169 struct firmware_priv *fw_priv = to_firmware_priv(dev); 318 struct firmware_priv *fw_priv = to_firmware_priv(dev);
170 319
171 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->fw_id)) 320 if (add_uevent_var(env, "FIRMWARE=%s", fw_priv->buf->fw_id))
172 return -ENOMEM; 321 return -ENOMEM;
173 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout)) 322 if (add_uevent_var(env, "TIMEOUT=%i", loading_timeout))
174 return -ENOMEM; 323 return -ENOMEM;
@@ -189,20 +338,16 @@ static ssize_t firmware_loading_show(struct device *dev,
189 struct device_attribute *attr, char *buf) 338 struct device_attribute *attr, char *buf)
190{ 339{
191 struct firmware_priv *fw_priv = to_firmware_priv(dev); 340 struct firmware_priv *fw_priv = to_firmware_priv(dev);
192 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->status); 341 int loading = test_bit(FW_STATUS_LOADING, &fw_priv->buf->status);
193 342
194 return sprintf(buf, "%d\n", loading); 343 return sprintf(buf, "%d\n", loading);
195} 344}
196 345
346/* firmware holds the ownership of pages */
197static void firmware_free_data(const struct firmware *fw) 347static void firmware_free_data(const struct firmware *fw)
198{ 348{
199 int i; 349 WARN_ON(!fw->priv);
200 vunmap(fw->data); 350 fw_free_buf(fw->priv);
201 if (fw->pages) {
202 for (i = 0; i < PFN_UP(fw->size); i++)
203 __free_page(fw->pages[i]);
204 kfree(fw->pages);
205 }
206} 351}
207 352
208/* Some architectures don't have PAGE_KERNEL_RO */ 353/* Some architectures don't have PAGE_KERNEL_RO */
@@ -227,45 +372,33 @@ static ssize_t firmware_loading_store(struct device *dev,
227 const char *buf, size_t count) 372 const char *buf, size_t count)
228{ 373{
229 struct firmware_priv *fw_priv = to_firmware_priv(dev); 374 struct firmware_priv *fw_priv = to_firmware_priv(dev);
375 struct firmware_buf *fw_buf = fw_priv->buf;
230 int loading = simple_strtol(buf, NULL, 10); 376 int loading = simple_strtol(buf, NULL, 10);
231 int i; 377 int i;
232 378
233 mutex_lock(&fw_lock); 379 mutex_lock(&fw_lock);
234 380
235 if (!fw_priv->fw) 381 if (!fw_buf)
236 goto out; 382 goto out;
237 383
238 switch (loading) { 384 switch (loading) {
239 case 1: 385 case 1:
240 firmware_free_data(fw_priv->fw); 386 /* discarding any previous partial load */
241 memset(fw_priv->fw, 0, sizeof(struct firmware)); 387 if (!test_bit(FW_STATUS_DONE, &fw_buf->status)) {
242 /* If the pages are not owned by 'struct firmware' */ 388 for (i = 0; i < fw_buf->nr_pages; i++)
243 for (i = 0; i < fw_priv->nr_pages; i++) 389 __free_page(fw_buf->pages[i]);
244 __free_page(fw_priv->pages[i]); 390 kfree(fw_buf->pages);
245 kfree(fw_priv->pages); 391 fw_buf->pages = NULL;
246 fw_priv->pages = NULL; 392 fw_buf->page_array_size = 0;
247 fw_priv->page_array_size = 0; 393 fw_buf->nr_pages = 0;
248 fw_priv->nr_pages = 0; 394 set_bit(FW_STATUS_LOADING, &fw_buf->status);
249 set_bit(FW_STATUS_LOADING, &fw_priv->status); 395 }
250 break; 396 break;
251 case 0: 397 case 0:
252 if (test_bit(FW_STATUS_LOADING, &fw_priv->status)) { 398 if (test_bit(FW_STATUS_LOADING, &fw_buf->status)) {
253 vunmap(fw_priv->fw->data); 399 set_bit(FW_STATUS_DONE, &fw_buf->status);
254 fw_priv->fw->data = vmap(fw_priv->pages, 400 clear_bit(FW_STATUS_LOADING, &fw_buf->status);
255 fw_priv->nr_pages, 401 complete_all(&fw_buf->completion);
256 0, PAGE_KERNEL_RO);
257 if (!fw_priv->fw->data) {
258 dev_err(dev, "%s: vmap() failed\n", __func__);
259 goto err;
260 }
261 /* Pages are now owned by 'struct firmware' */
262 fw_priv->fw->pages = fw_priv->pages;
263 fw_priv->pages = NULL;
264
265 fw_priv->page_array_size = 0;
266 fw_priv->nr_pages = 0;
267 complete(&fw_priv->completion);
268 clear_bit(FW_STATUS_LOADING, &fw_priv->status);
269 break; 402 break;
270 } 403 }
271 /* fallthrough */ 404 /* fallthrough */
@@ -273,7 +406,6 @@ static ssize_t firmware_loading_store(struct device *dev,
273 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); 406 dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading);
274 /* fallthrough */ 407 /* fallthrough */
275 case -1: 408 case -1:
276 err:
277 fw_load_abort(fw_priv); 409 fw_load_abort(fw_priv);
278 break; 410 break;
279 } 411 }
@@ -290,21 +422,21 @@ static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
290{ 422{
291 struct device *dev = kobj_to_dev(kobj); 423 struct device *dev = kobj_to_dev(kobj);
292 struct firmware_priv *fw_priv = to_firmware_priv(dev); 424 struct firmware_priv *fw_priv = to_firmware_priv(dev);
293 struct firmware *fw; 425 struct firmware_buf *buf;
294 ssize_t ret_count; 426 ssize_t ret_count;
295 427
296 mutex_lock(&fw_lock); 428 mutex_lock(&fw_lock);
297 fw = fw_priv->fw; 429 buf = fw_priv->buf;
298 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { 430 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
299 ret_count = -ENODEV; 431 ret_count = -ENODEV;
300 goto out; 432 goto out;
301 } 433 }
302 if (offset > fw->size) { 434 if (offset > buf->size) {
303 ret_count = 0; 435 ret_count = 0;
304 goto out; 436 goto out;
305 } 437 }
306 if (count > fw->size - offset) 438 if (count > buf->size - offset)
307 count = fw->size - offset; 439 count = buf->size - offset;
308 440
309 ret_count = count; 441 ret_count = count;
310 442
@@ -314,11 +446,11 @@ static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj,
314 int page_ofs = offset & (PAGE_SIZE-1); 446 int page_ofs = offset & (PAGE_SIZE-1);
315 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); 447 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
316 448
317 page_data = kmap(fw_priv->pages[page_nr]); 449 page_data = kmap(buf->pages[page_nr]);
318 450
319 memcpy(buffer, page_data + page_ofs, page_cnt); 451 memcpy(buffer, page_data + page_ofs, page_cnt);
320 452
321 kunmap(fw_priv->pages[page_nr]); 453 kunmap(buf->pages[page_nr]);
322 buffer += page_cnt; 454 buffer += page_cnt;
323 offset += page_cnt; 455 offset += page_cnt;
324 count -= page_cnt; 456 count -= page_cnt;
@@ -330,12 +462,13 @@ out:
330 462
331static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size) 463static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
332{ 464{
465 struct firmware_buf *buf = fw_priv->buf;
333 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT; 466 int pages_needed = ALIGN(min_size, PAGE_SIZE) >> PAGE_SHIFT;
334 467
335 /* If the array of pages is too small, grow it... */ 468 /* If the array of pages is too small, grow it... */
336 if (fw_priv->page_array_size < pages_needed) { 469 if (buf->page_array_size < pages_needed) {
337 int new_array_size = max(pages_needed, 470 int new_array_size = max(pages_needed,
338 fw_priv->page_array_size * 2); 471 buf->page_array_size * 2);
339 struct page **new_pages; 472 struct page **new_pages;
340 473
341 new_pages = kmalloc(new_array_size * sizeof(void *), 474 new_pages = kmalloc(new_array_size * sizeof(void *),
@@ -344,24 +477,24 @@ static int fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
344 fw_load_abort(fw_priv); 477 fw_load_abort(fw_priv);
345 return -ENOMEM; 478 return -ENOMEM;
346 } 479 }
347 memcpy(new_pages, fw_priv->pages, 480 memcpy(new_pages, buf->pages,
348 fw_priv->page_array_size * sizeof(void *)); 481 buf->page_array_size * sizeof(void *));
349 memset(&new_pages[fw_priv->page_array_size], 0, sizeof(void *) * 482 memset(&new_pages[buf->page_array_size], 0, sizeof(void *) *
350 (new_array_size - fw_priv->page_array_size)); 483 (new_array_size - buf->page_array_size));
351 kfree(fw_priv->pages); 484 kfree(buf->pages);
352 fw_priv->pages = new_pages; 485 buf->pages = new_pages;
353 fw_priv->page_array_size = new_array_size; 486 buf->page_array_size = new_array_size;
354 } 487 }
355 488
356 while (fw_priv->nr_pages < pages_needed) { 489 while (buf->nr_pages < pages_needed) {
357 fw_priv->pages[fw_priv->nr_pages] = 490 buf->pages[buf->nr_pages] =
358 alloc_page(GFP_KERNEL | __GFP_HIGHMEM); 491 alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
359 492
360 if (!fw_priv->pages[fw_priv->nr_pages]) { 493 if (!buf->pages[buf->nr_pages]) {
361 fw_load_abort(fw_priv); 494 fw_load_abort(fw_priv);
362 return -ENOMEM; 495 return -ENOMEM;
363 } 496 }
364 fw_priv->nr_pages++; 497 buf->nr_pages++;
365 } 498 }
366 return 0; 499 return 0;
367} 500}
@@ -384,18 +517,19 @@ static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
384{ 517{
385 struct device *dev = kobj_to_dev(kobj); 518 struct device *dev = kobj_to_dev(kobj);
386 struct firmware_priv *fw_priv = to_firmware_priv(dev); 519 struct firmware_priv *fw_priv = to_firmware_priv(dev);
387 struct firmware *fw; 520 struct firmware_buf *buf;
388 ssize_t retval; 521 ssize_t retval;
389 522
390 if (!capable(CAP_SYS_RAWIO)) 523 if (!capable(CAP_SYS_RAWIO))
391 return -EPERM; 524 return -EPERM;
392 525
393 mutex_lock(&fw_lock); 526 mutex_lock(&fw_lock);
394 fw = fw_priv->fw; 527 buf = fw_priv->buf;
395 if (!fw || test_bit(FW_STATUS_DONE, &fw_priv->status)) { 528 if (!buf || test_bit(FW_STATUS_DONE, &buf->status)) {
396 retval = -ENODEV; 529 retval = -ENODEV;
397 goto out; 530 goto out;
398 } 531 }
532
399 retval = fw_realloc_buffer(fw_priv, offset + count); 533 retval = fw_realloc_buffer(fw_priv, offset + count);
400 if (retval) 534 if (retval)
401 goto out; 535 goto out;
@@ -408,17 +542,17 @@ static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj,
408 int page_ofs = offset & (PAGE_SIZE - 1); 542 int page_ofs = offset & (PAGE_SIZE - 1);
409 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count); 543 int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
410 544
411 page_data = kmap(fw_priv->pages[page_nr]); 545 page_data = kmap(buf->pages[page_nr]);
412 546
413 memcpy(page_data + page_ofs, buffer, page_cnt); 547 memcpy(page_data + page_ofs, buffer, page_cnt);
414 548
415 kunmap(fw_priv->pages[page_nr]); 549 kunmap(buf->pages[page_nr]);
416 buffer += page_cnt; 550 buffer += page_cnt;
417 offset += page_cnt; 551 offset += page_cnt;
418 count -= page_cnt; 552 count -= page_cnt;
419 } 553 }
420 554
421 fw->size = max_t(size_t, offset, fw->size); 555 buf->size = max_t(size_t, offset, buf->size);
422out: 556out:
423 mutex_unlock(&fw_lock); 557 mutex_unlock(&fw_lock);
424 return retval; 558 return retval;
@@ -445,35 +579,120 @@ fw_create_instance(struct firmware *firmware, const char *fw_name,
445 struct firmware_priv *fw_priv; 579 struct firmware_priv *fw_priv;
446 struct device *f_dev; 580 struct device *f_dev;
447 581
448 fw_priv = kzalloc(sizeof(*fw_priv) + strlen(fw_name) + 1 , GFP_KERNEL); 582 fw_priv = kzalloc(sizeof(*fw_priv), GFP_KERNEL);
449 if (!fw_priv) { 583 if (!fw_priv) {
450 dev_err(device, "%s: kmalloc failed\n", __func__); 584 dev_err(device, "%s: kmalloc failed\n", __func__);
451 return ERR_PTR(-ENOMEM); 585 fw_priv = ERR_PTR(-ENOMEM);
586 goto exit;
452 } 587 }
453 588
454 fw_priv->fw = firmware;
455 fw_priv->nowait = nowait; 589 fw_priv->nowait = nowait;
456 strcpy(fw_priv->fw_id, fw_name); 590 fw_priv->fw = firmware;
457 init_completion(&fw_priv->completion);
458 setup_timer(&fw_priv->timeout, 591 setup_timer(&fw_priv->timeout,
459 firmware_class_timeout, (u_long) fw_priv); 592 firmware_class_timeout, (u_long) fw_priv);
460 593
461 f_dev = &fw_priv->dev; 594 f_dev = &fw_priv->dev;
462 595
463 device_initialize(f_dev); 596 device_initialize(f_dev);
464 dev_set_name(f_dev, "%s", dev_name(device)); 597 dev_set_name(f_dev, "%s", fw_name);
465 f_dev->parent = device; 598 f_dev->parent = device;
466 f_dev->class = &firmware_class; 599 f_dev->class = &firmware_class;
467 600exit:
468 return fw_priv; 601 return fw_priv;
469} 602}
470 603
604/* one pages buffer is mapped/unmapped only once */
605static int fw_map_pages_buf(struct firmware_buf *buf)
606{
607 buf->data = vmap(buf->pages, buf->nr_pages, 0, PAGE_KERNEL_RO);
608 if (!buf->data)
609 return -ENOMEM;
610 return 0;
611}
612
613/* store the pages buffer info firmware from buf */
614static void fw_set_page_data(struct firmware_buf *buf, struct firmware *fw)
615{
616 fw->priv = buf;
617 fw->pages = buf->pages;
618 fw->size = buf->size;
619 fw->data = buf->data;
620
621 pr_debug("%s: fw-%s buf=%p data=%p size=%u\n",
622 __func__, buf->fw_id, buf, buf->data,
623 (unsigned int)buf->size);
624}
625
626#ifdef CONFIG_PM_SLEEP
627static void fw_name_devm_release(struct device *dev, void *res)
628{
629 struct fw_name_devm *fwn = res;
630
631 if (fwn->magic == (unsigned long)&fw_cache)
632 pr_debug("%s: fw_name-%s devm-%p released\n",
633 __func__, fwn->name, res);
634}
635
636static int fw_devm_match(struct device *dev, void *res,
637 void *match_data)
638{
639 struct fw_name_devm *fwn = res;
640
641 return (fwn->magic == (unsigned long)&fw_cache) &&
642 !strcmp(fwn->name, match_data);
643}
644
645static struct fw_name_devm *fw_find_devm_name(struct device *dev,
646 const char *name)
647{
648 struct fw_name_devm *fwn;
649
650 fwn = devres_find(dev, fw_name_devm_release,
651 fw_devm_match, (void *)name);
652 return fwn;
653}
654
655/* add firmware name into devres list */
656static int fw_add_devm_name(struct device *dev, const char *name)
657{
658 struct fw_name_devm *fwn;
659
660 fwn = fw_find_devm_name(dev, name);
661 if (fwn)
662 return 1;
663
664 fwn = devres_alloc(fw_name_devm_release, sizeof(struct fw_name_devm) +
665 strlen(name) + 1, GFP_KERNEL);
666 if (!fwn)
667 return -ENOMEM;
668
669 fwn->magic = (unsigned long)&fw_cache;
670 strcpy(fwn->name, name);
671 devres_add(dev, fwn);
672
673 return 0;
674}
675#else
676static int fw_add_devm_name(struct device *dev, const char *name)
677{
678 return 0;
679}
680#endif
681
682static void _request_firmware_cleanup(const struct firmware **firmware_p)
683{
684 release_firmware(*firmware_p);
685 *firmware_p = NULL;
686}
687
471static struct firmware_priv * 688static struct firmware_priv *
472_request_firmware_prepare(const struct firmware **firmware_p, const char *name, 689_request_firmware_prepare(const struct firmware **firmware_p, const char *name,
473 struct device *device, bool uevent, bool nowait) 690 struct device *device, bool uevent, bool nowait)
474{ 691{
475 struct firmware *firmware; 692 struct firmware *firmware;
476 struct firmware_priv *fw_priv; 693 struct firmware_priv *fw_priv = NULL;
694 struct firmware_buf *buf;
695 int ret;
477 696
478 if (!firmware_p) 697 if (!firmware_p)
479 return ERR_PTR(-EINVAL); 698 return ERR_PTR(-EINVAL);
@@ -490,18 +709,46 @@ _request_firmware_prepare(const struct firmware **firmware_p, const char *name,
490 return NULL; 709 return NULL;
491 } 710 }
492 711
493 fw_priv = fw_create_instance(firmware, name, device, uevent, nowait); 712 ret = fw_lookup_and_allocate_buf(name, &fw_cache, &buf);
494 if (IS_ERR(fw_priv)) { 713 if (!ret)
495 release_firmware(firmware); 714 fw_priv = fw_create_instance(firmware, name, device,
715 uevent, nowait);
716
717 if (IS_ERR(fw_priv) || ret < 0) {
718 kfree(firmware);
496 *firmware_p = NULL; 719 *firmware_p = NULL;
720 return ERR_PTR(-ENOMEM);
721 } else if (fw_priv) {
722 fw_priv->buf = buf;
723
724 /*
725 * bind with 'buf' now to avoid warning in failure path
726 * of requesting firmware.
727 */
728 firmware->priv = buf;
729 return fw_priv;
497 } 730 }
498 return fw_priv;
499}
500 731
501static void _request_firmware_cleanup(const struct firmware **firmware_p) 732 /* share the cached buf, which is inprogessing or completed */
502{ 733 check_status:
503 release_firmware(*firmware_p); 734 mutex_lock(&fw_lock);
504 *firmware_p = NULL; 735 if (test_bit(FW_STATUS_ABORT, &buf->status)) {
736 fw_priv = ERR_PTR(-ENOENT);
737 firmware->priv = buf;
738 _request_firmware_cleanup(firmware_p);
739 goto exit;
740 } else if (test_bit(FW_STATUS_DONE, &buf->status)) {
741 fw_priv = NULL;
742 fw_set_page_data(buf, firmware);
743 goto exit;
744 }
745 mutex_unlock(&fw_lock);
746 wait_for_completion(&buf->completion);
747 goto check_status;
748
749exit:
750 mutex_unlock(&fw_lock);
751 return fw_priv;
505} 752}
506 753
507static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent, 754static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
@@ -509,6 +756,8 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
509{ 756{
510 int retval = 0; 757 int retval = 0;
511 struct device *f_dev = &fw_priv->dev; 758 struct device *f_dev = &fw_priv->dev;
759 struct firmware_buf *buf = fw_priv->buf;
760 struct firmware_cache *fwc = &fw_cache;
512 761
513 dev_set_uevent_suppress(f_dev, true); 762 dev_set_uevent_suppress(f_dev, true);
514 763
@@ -535,7 +784,7 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
535 784
536 if (uevent) { 785 if (uevent) {
537 dev_set_uevent_suppress(f_dev, false); 786 dev_set_uevent_suppress(f_dev, false);
538 dev_dbg(f_dev, "firmware: requesting %s\n", fw_priv->fw_id); 787 dev_dbg(f_dev, "firmware: requesting %s\n", buf->fw_id);
539 if (timeout != MAX_SCHEDULE_TIMEOUT) 788 if (timeout != MAX_SCHEDULE_TIMEOUT)
540 mod_timer(&fw_priv->timeout, 789 mod_timer(&fw_priv->timeout,
541 round_jiffies_up(jiffies + timeout)); 790 round_jiffies_up(jiffies + timeout));
@@ -543,15 +792,40 @@ static int _request_firmware_load(struct firmware_priv *fw_priv, bool uevent,
543 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD); 792 kobject_uevent(&fw_priv->dev.kobj, KOBJ_ADD);
544 } 793 }
545 794
546 wait_for_completion(&fw_priv->completion); 795 wait_for_completion(&buf->completion);
547 796
548 set_bit(FW_STATUS_DONE, &fw_priv->status);
549 del_timer_sync(&fw_priv->timeout); 797 del_timer_sync(&fw_priv->timeout);
550 798
551 mutex_lock(&fw_lock); 799 mutex_lock(&fw_lock);
552 if (!fw_priv->fw->size || test_bit(FW_STATUS_ABORT, &fw_priv->status)) 800 if (!buf->size || test_bit(FW_STATUS_ABORT, &buf->status))
553 retval = -ENOENT; 801 retval = -ENOENT;
554 fw_priv->fw = NULL; 802
803 /*
804 * add firmware name into devres list so that we can auto cache
805 * and uncache firmware for device.
806 *
807 * f_dev->parent may has been deleted already, but the problem
808 * should be fixed in devres or driver core.
809 */
810 if (!retval && f_dev->parent)
811 fw_add_devm_name(f_dev->parent, buf->fw_id);
812
813 if (!retval)
814 retval = fw_map_pages_buf(buf);
815
816 /*
817 * After caching firmware image is started, let it piggyback
818 * on request firmware.
819 */
820 if (!retval && fwc->state == FW_LOADER_START_CACHE) {
821 if (fw_cache_piggyback_on_request(buf->fw_id))
822 kref_get(&buf->ref);
823 }
824
825 /* pass the pages buffer to driver at the last minute */
826 fw_set_page_data(buf, fw_priv->fw);
827
828 fw_priv->buf = NULL;
555 mutex_unlock(&fw_lock); 829 mutex_unlock(&fw_lock);
556 830
557 device_remove_file(f_dev, &dev_attr_loading); 831 device_remove_file(f_dev, &dev_attr_loading);
@@ -578,6 +852,8 @@ err_put_dev:
578 * @name will be used as $FIRMWARE in the uevent environment and 852 * @name will be used as $FIRMWARE in the uevent environment and
579 * should be distinctive enough not to be confused with any other 853 * should be distinctive enough not to be confused with any other
580 * firmware image for this or any other device. 854 * firmware image for this or any other device.
855 *
856 * Caller must hold the reference count of @device.
581 **/ 857 **/
582int 858int
583request_firmware(const struct firmware **firmware_p, const char *name, 859request_firmware(const struct firmware **firmware_p, const char *name,
@@ -659,6 +935,7 @@ static void request_firmware_work_func(struct work_struct *work)
659 935
660 out: 936 out:
661 fw_work->cont(fw, fw_work->context); 937 fw_work->cont(fw, fw_work->context);
938 put_device(fw_work->device);
662 939
663 module_put(fw_work->module); 940 module_put(fw_work->module);
664 kfree(fw_work); 941 kfree(fw_work);
@@ -677,9 +954,15 @@ static void request_firmware_work_func(struct work_struct *work)
677 * @cont: function will be called asynchronously when the firmware 954 * @cont: function will be called asynchronously when the firmware
678 * request is over. 955 * request is over.
679 * 956 *
680 * Asynchronous variant of request_firmware() for user contexts where 957 * Caller must hold the reference count of @device.
681 * it is not possible to sleep for long time. It can't be called 958 *
682 * in atomic contexts. 959 * Asynchronous variant of request_firmware() for user contexts:
960 * - sleep for as small periods as possible since it may
961 * increase kernel boot time of built-in device drivers
962 * requesting firmware in their ->probe() methods, if
963 * @gfp is GFP_KERNEL.
964 *
965 * - can't sleep at all if @gfp is GFP_ATOMIC.
683 **/ 966 **/
684int 967int
685request_firmware_nowait( 968request_firmware_nowait(
@@ -705,18 +988,363 @@ request_firmware_nowait(
705 return -EFAULT; 988 return -EFAULT;
706 } 989 }
707 990
991 get_device(fw_work->device);
708 INIT_WORK(&fw_work->work, request_firmware_work_func); 992 INIT_WORK(&fw_work->work, request_firmware_work_func);
709 schedule_work(&fw_work->work); 993 schedule_work(&fw_work->work);
710 return 0; 994 return 0;
711} 995}
712 996
997/**
998 * cache_firmware - cache one firmware image in kernel memory space
999 * @fw_name: the firmware image name
1000 *
1001 * Cache firmware in kernel memory so that drivers can use it when
1002 * system isn't ready for them to request firmware image from userspace.
1003 * Once it returns successfully, driver can use request_firmware or its
1004 * nowait version to get the cached firmware without any interacting
1005 * with userspace
1006 *
1007 * Return 0 if the firmware image has been cached successfully
1008 * Return !0 otherwise
1009 *
1010 */
1011int cache_firmware(const char *fw_name)
1012{
1013 int ret;
1014 const struct firmware *fw;
1015
1016 pr_debug("%s: %s\n", __func__, fw_name);
1017
1018 ret = request_firmware(&fw, fw_name, NULL);
1019 if (!ret)
1020 kfree(fw);
1021
1022 pr_debug("%s: %s ret=%d\n", __func__, fw_name, ret);
1023
1024 return ret;
1025}
1026
1027/**
1028 * uncache_firmware - remove one cached firmware image
1029 * @fw_name: the firmware image name
1030 *
1031 * Uncache one firmware image which has been cached successfully
1032 * before.
1033 *
1034 * Return 0 if the firmware cache has been removed successfully
1035 * Return !0 otherwise
1036 *
1037 */
1038int uncache_firmware(const char *fw_name)
1039{
1040 struct firmware_buf *buf;
1041 struct firmware fw;
1042
1043 pr_debug("%s: %s\n", __func__, fw_name);
1044
1045 if (fw_get_builtin_firmware(&fw, fw_name))
1046 return 0;
1047
1048 buf = fw_lookup_buf(fw_name);
1049 if (buf) {
1050 fw_free_buf(buf);
1051 return 0;
1052 }
1053
1054 return -EINVAL;
1055}
1056
1057#ifdef CONFIG_PM_SLEEP
1058static struct fw_cache_entry *alloc_fw_cache_entry(const char *name)
1059{
1060 struct fw_cache_entry *fce;
1061
1062 fce = kzalloc(sizeof(*fce) + strlen(name) + 1, GFP_ATOMIC);
1063 if (!fce)
1064 goto exit;
1065
1066 strcpy(fce->name, name);
1067exit:
1068 return fce;
1069}
1070
1071static int fw_cache_piggyback_on_request(const char *name)
1072{
1073 struct firmware_cache *fwc = &fw_cache;
1074 struct fw_cache_entry *fce;
1075 int ret = 0;
1076
1077 spin_lock(&fwc->name_lock);
1078 list_for_each_entry(fce, &fwc->fw_names, list) {
1079 if (!strcmp(fce->name, name))
1080 goto found;
1081 }
1082
1083 fce = alloc_fw_cache_entry(name);
1084 if (fce) {
1085 ret = 1;
1086 list_add(&fce->list, &fwc->fw_names);
1087 pr_debug("%s: fw: %s\n", __func__, name);
1088 }
1089found:
1090 spin_unlock(&fwc->name_lock);
1091 return ret;
1092}
1093
1094static void free_fw_cache_entry(struct fw_cache_entry *fce)
1095{
1096 kfree(fce);
1097}
1098
1099static void __async_dev_cache_fw_image(void *fw_entry,
1100 async_cookie_t cookie)
1101{
1102 struct fw_cache_entry *fce = fw_entry;
1103 struct firmware_cache *fwc = &fw_cache;
1104 int ret;
1105
1106 ret = cache_firmware(fce->name);
1107 if (ret) {
1108 spin_lock(&fwc->name_lock);
1109 list_del(&fce->list);
1110 spin_unlock(&fwc->name_lock);
1111
1112 free_fw_cache_entry(fce);
1113 }
1114
1115 spin_lock(&fwc->name_lock);
1116 fwc->cnt--;
1117 spin_unlock(&fwc->name_lock);
1118
1119 wake_up(&fwc->wait_queue);
1120}
1121
1122/* called with dev->devres_lock held */
1123static void dev_create_fw_entry(struct device *dev, void *res,
1124 void *data)
1125{
1126 struct fw_name_devm *fwn = res;
1127 const char *fw_name = fwn->name;
1128 struct list_head *head = data;
1129 struct fw_cache_entry *fce;
1130
1131 fce = alloc_fw_cache_entry(fw_name);
1132 if (fce)
1133 list_add(&fce->list, head);
1134}
1135
1136static int devm_name_match(struct device *dev, void *res,
1137 void *match_data)
1138{
1139 struct fw_name_devm *fwn = res;
1140 return (fwn->magic == (unsigned long)match_data);
1141}
1142
1143static void dev_cache_fw_image(struct device *dev, void *data)
1144{
1145 LIST_HEAD(todo);
1146 struct fw_cache_entry *fce;
1147 struct fw_cache_entry *fce_next;
1148 struct firmware_cache *fwc = &fw_cache;
1149
1150 devres_for_each_res(dev, fw_name_devm_release,
1151 devm_name_match, &fw_cache,
1152 dev_create_fw_entry, &todo);
1153
1154 list_for_each_entry_safe(fce, fce_next, &todo, list) {
1155 list_del(&fce->list);
1156
1157 spin_lock(&fwc->name_lock);
1158 fwc->cnt++;
1159 list_add(&fce->list, &fwc->fw_names);
1160 spin_unlock(&fwc->name_lock);
1161
1162 async_schedule(__async_dev_cache_fw_image, (void *)fce);
1163 }
1164}
1165
1166static void __device_uncache_fw_images(void)
1167{
1168 struct firmware_cache *fwc = &fw_cache;
1169 struct fw_cache_entry *fce;
1170
1171 spin_lock(&fwc->name_lock);
1172 while (!list_empty(&fwc->fw_names)) {
1173 fce = list_entry(fwc->fw_names.next,
1174 struct fw_cache_entry, list);
1175 list_del(&fce->list);
1176 spin_unlock(&fwc->name_lock);
1177
1178 uncache_firmware(fce->name);
1179 free_fw_cache_entry(fce);
1180
1181 spin_lock(&fwc->name_lock);
1182 }
1183 spin_unlock(&fwc->name_lock);
1184}
1185
1186/**
1187 * device_cache_fw_images - cache devices' firmware
1188 *
1189 * If one device called request_firmware or its nowait version
1190 * successfully before, the firmware names are recored into the
1191 * device's devres link list, so device_cache_fw_images can call
1192 * cache_firmware() to cache these firmwares for the device,
1193 * then the device driver can load its firmwares easily at
1194 * time when system is not ready to complete loading firmware.
1195 */
1196static void device_cache_fw_images(void)
1197{
1198 struct firmware_cache *fwc = &fw_cache;
1199 int old_timeout;
1200 DEFINE_WAIT(wait);
1201
1202 pr_debug("%s\n", __func__);
1203
1204 /*
1205 * use small loading timeout for caching devices' firmware
1206 * because all these firmware images have been loaded
1207 * successfully at lease once, also system is ready for
1208 * completing firmware loading now. The maximum size of
1209 * firmware in current distributions is about 2M bytes,
1210 * so 10 secs should be enough.
1211 */
1212 old_timeout = loading_timeout;
1213 loading_timeout = 10;
1214
1215 mutex_lock(&fw_lock);
1216 fwc->state = FW_LOADER_START_CACHE;
1217 dpm_for_each_dev(NULL, dev_cache_fw_image);
1218 mutex_unlock(&fw_lock);
1219
1220 /* wait for completion of caching firmware for all devices */
1221 spin_lock(&fwc->name_lock);
1222 for (;;) {
1223 prepare_to_wait(&fwc->wait_queue, &wait,
1224 TASK_UNINTERRUPTIBLE);
1225 if (!fwc->cnt)
1226 break;
1227
1228 spin_unlock(&fwc->name_lock);
1229
1230 schedule();
1231
1232 spin_lock(&fwc->name_lock);
1233 }
1234 spin_unlock(&fwc->name_lock);
1235 finish_wait(&fwc->wait_queue, &wait);
1236
1237 loading_timeout = old_timeout;
1238}
1239
1240/**
1241 * device_uncache_fw_images - uncache devices' firmware
1242 *
1243 * uncache all firmwares which have been cached successfully
1244 * by device_uncache_fw_images earlier
1245 */
1246static void device_uncache_fw_images(void)
1247{
1248 pr_debug("%s\n", __func__);
1249 __device_uncache_fw_images();
1250}
1251
1252static void device_uncache_fw_images_work(struct work_struct *work)
1253{
1254 device_uncache_fw_images();
1255}
1256
1257/**
1258 * device_uncache_fw_images_delay - uncache devices firmwares
1259 * @delay: number of milliseconds to delay uncache device firmwares
1260 *
1261 * uncache all devices's firmwares which has been cached successfully
1262 * by device_cache_fw_images after @delay milliseconds.
1263 */
1264static void device_uncache_fw_images_delay(unsigned long delay)
1265{
1266 schedule_delayed_work(&fw_cache.work,
1267 msecs_to_jiffies(delay));
1268}
1269
1270static int fw_pm_notify(struct notifier_block *notify_block,
1271 unsigned long mode, void *unused)
1272{
1273 switch (mode) {
1274 case PM_HIBERNATION_PREPARE:
1275 case PM_SUSPEND_PREPARE:
1276 device_cache_fw_images();
1277 break;
1278
1279 case PM_POST_SUSPEND:
1280 case PM_POST_HIBERNATION:
1281 case PM_POST_RESTORE:
1282 /*
1283 * In case that system sleep failed and syscore_suspend is
1284 * not called.
1285 */
1286 mutex_lock(&fw_lock);
1287 fw_cache.state = FW_LOADER_NO_CACHE;
1288 mutex_unlock(&fw_lock);
1289
1290 device_uncache_fw_images_delay(10 * MSEC_PER_SEC);
1291 break;
1292 }
1293
1294 return 0;
1295}
1296
1297/* stop caching firmware once syscore_suspend is reached */
1298static int fw_suspend(void)
1299{
1300 fw_cache.state = FW_LOADER_NO_CACHE;
1301 return 0;
1302}
1303
1304static struct syscore_ops fw_syscore_ops = {
1305 .suspend = fw_suspend,
1306};
1307#else
1308static int fw_cache_piggyback_on_request(const char *name)
1309{
1310 return 0;
1311}
1312#endif
1313
1314static void __init fw_cache_init(void)
1315{
1316 spin_lock_init(&fw_cache.lock);
1317 INIT_LIST_HEAD(&fw_cache.head);
1318 fw_cache.state = FW_LOADER_NO_CACHE;
1319
1320#ifdef CONFIG_PM_SLEEP
1321 spin_lock_init(&fw_cache.name_lock);
1322 INIT_LIST_HEAD(&fw_cache.fw_names);
1323 fw_cache.cnt = 0;
1324
1325 init_waitqueue_head(&fw_cache.wait_queue);
1326 INIT_DELAYED_WORK(&fw_cache.work,
1327 device_uncache_fw_images_work);
1328
1329 fw_cache.pm_notify.notifier_call = fw_pm_notify;
1330 register_pm_notifier(&fw_cache.pm_notify);
1331
1332 register_syscore_ops(&fw_syscore_ops);
1333#endif
1334}
1335
713static int __init firmware_class_init(void) 1336static int __init firmware_class_init(void)
714{ 1337{
1338 fw_cache_init();
715 return class_register(&firmware_class); 1339 return class_register(&firmware_class);
716} 1340}
717 1341
718static void __exit firmware_class_exit(void) 1342static void __exit firmware_class_exit(void)
719{ 1343{
1344#ifdef CONFIG_PM_SLEEP
1345 unregister_syscore_ops(&fw_syscore_ops);
1346 unregister_pm_notifier(&fw_cache.pm_notify);
1347#endif
720 class_unregister(&firmware_class); 1348 class_unregister(&firmware_class);
721} 1349}
722 1350
@@ -726,3 +1354,5 @@ module_exit(firmware_class_exit);
726EXPORT_SYMBOL(release_firmware); 1354EXPORT_SYMBOL(release_firmware);
727EXPORT_SYMBOL(request_firmware); 1355EXPORT_SYMBOL(request_firmware);
728EXPORT_SYMBOL(request_firmware_nowait); 1356EXPORT_SYMBOL(request_firmware_nowait);
1357EXPORT_SYMBOL_GPL(cache_firmware);
1358EXPORT_SYMBOL_GPL(uncache_firmware);
diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index a1a72250258..ddeca142293 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -20,9 +20,13 @@
20#include <linux/err.h> 20#include <linux/err.h>
21#include <linux/slab.h> 21#include <linux/slab.h>
22#include <linux/pm_runtime.h> 22#include <linux/pm_runtime.h>
23#include <linux/idr.h>
23 24
24#include "base.h" 25#include "base.h"
25 26
27/* For automatically allocated device IDs */
28static DEFINE_IDA(platform_devid_ida);
29
26#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \ 30#define to_platform_driver(drv) (container_of((drv), struct platform_driver, \
27 driver)) 31 driver))
28 32
@@ -99,6 +103,9 @@ struct resource *platform_get_resource_byname(struct platform_device *dev,
99 for (i = 0; i < dev->num_resources; i++) { 103 for (i = 0; i < dev->num_resources; i++) {
100 struct resource *r = &dev->resource[i]; 104 struct resource *r = &dev->resource[i];
101 105
106 if (unlikely(!r->name))
107 continue;
108
102 if (type == resource_type(r) && !strcmp(r->name, name)) 109 if (type == resource_type(r) && !strcmp(r->name, name))
103 return r; 110 return r;
104 } 111 }
@@ -263,7 +270,7 @@ EXPORT_SYMBOL_GPL(platform_device_add_data);
263 */ 270 */
264int platform_device_add(struct platform_device *pdev) 271int platform_device_add(struct platform_device *pdev)
265{ 272{
266 int i, ret = 0; 273 int i, ret;
267 274
268 if (!pdev) 275 if (!pdev)
269 return -EINVAL; 276 return -EINVAL;
@@ -273,10 +280,27 @@ int platform_device_add(struct platform_device *pdev)
273 280
274 pdev->dev.bus = &platform_bus_type; 281 pdev->dev.bus = &platform_bus_type;
275 282
276 if (pdev->id != -1) 283 switch (pdev->id) {
284 default:
277 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id); 285 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
278 else 286 break;
287 case PLATFORM_DEVID_NONE:
279 dev_set_name(&pdev->dev, "%s", pdev->name); 288 dev_set_name(&pdev->dev, "%s", pdev->name);
289 break;
290 case PLATFORM_DEVID_AUTO:
291 /*
292 * Automatically allocated device ID. We mark it as such so
293 * that we remember it must be freed, and we append a suffix
294 * to avoid namespace collision with explicit IDs.
295 */
296 ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
297 if (ret < 0)
298 goto err_out;
299 pdev->id = ret;
300 pdev->id_auto = true;
301 dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
302 break;
303 }
280 304
281 for (i = 0; i < pdev->num_resources; i++) { 305 for (i = 0; i < pdev->num_resources; i++) {
282 struct resource *p, *r = &pdev->resource[i]; 306 struct resource *p, *r = &pdev->resource[i];
@@ -309,6 +333,11 @@ int platform_device_add(struct platform_device *pdev)
309 return ret; 333 return ret;
310 334
311 failed: 335 failed:
336 if (pdev->id_auto) {
337 ida_simple_remove(&platform_devid_ida, pdev->id);
338 pdev->id = PLATFORM_DEVID_AUTO;
339 }
340
312 while (--i >= 0) { 341 while (--i >= 0) {
313 struct resource *r = &pdev->resource[i]; 342 struct resource *r = &pdev->resource[i];
314 unsigned long type = resource_type(r); 343 unsigned long type = resource_type(r);
@@ -317,6 +346,7 @@ int platform_device_add(struct platform_device *pdev)
317 release_resource(r); 346 release_resource(r);
318 } 347 }
319 348
349 err_out:
320 return ret; 350 return ret;
321} 351}
322EXPORT_SYMBOL_GPL(platform_device_add); 352EXPORT_SYMBOL_GPL(platform_device_add);
@@ -336,6 +366,11 @@ void platform_device_del(struct platform_device *pdev)
336 if (pdev) { 366 if (pdev) {
337 device_del(&pdev->dev); 367 device_del(&pdev->dev);
338 368
369 if (pdev->id_auto) {
370 ida_simple_remove(&platform_devid_ida, pdev->id);
371 pdev->id = PLATFORM_DEVID_AUTO;
372 }
373
339 for (i = 0; i < pdev->num_resources; i++) { 374 for (i = 0; i < pdev->num_resources; i++) {
340 struct resource *r = &pdev->resource[i]; 375 struct resource *r = &pdev->resource[i];
341 unsigned long type = resource_type(r); 376 unsigned long type = resource_type(r);
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index 0113adc310d..b0b072a88f5 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -1324,3 +1324,25 @@ int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1324 return async_error; 1324 return async_error;
1325} 1325}
1326EXPORT_SYMBOL_GPL(device_pm_wait_for_dev); 1326EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1327
1328/**
1329 * dpm_for_each_dev - device iterator.
1330 * @data: data for the callback.
1331 * @fn: function to be called for each device.
1332 *
1333 * Iterate over devices in dpm_list, and call @fn for each device,
1334 * passing it @data.
1335 */
1336void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1337{
1338 struct device *dev;
1339
1340 if (!fn)
1341 return;
1342
1343 device_pm_lock();
1344 list_for_each_entry(dev, &dpm_list, power.entry)
1345 fn(dev, data);
1346 device_pm_unlock();
1347}
1348EXPORT_SYMBOL_GPL(dpm_for_each_dev);
diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
index e175c8ed4ec..07122a9ef36 100644
--- a/drivers/extcon/Kconfig
+++ b/drivers/extcon/Kconfig
@@ -21,6 +21,12 @@ config EXTCON_GPIO
21 Say Y here to enable GPIO based extcon support. Note that GPIO 21 Say Y here to enable GPIO based extcon support. Note that GPIO
22 extcon supports single state per extcon instance. 22 extcon supports single state per extcon instance.
23 23
24config EXTCON_ADC_JACK
25 tristate "ADC Jack extcon support"
26 depends on IIO
27 help
28 Say Y here to enable extcon device driver based on ADC values.
29
24config EXTCON_MAX77693 30config EXTCON_MAX77693
25 tristate "MAX77693 EXTCON Support" 31 tristate "MAX77693 EXTCON Support"
26 depends on MFD_MAX77693 32 depends on MFD_MAX77693
@@ -41,7 +47,7 @@ config EXTCON_MAX8997
41 47
42config EXTCON_ARIZONA 48config EXTCON_ARIZONA
43 tristate "Wolfson Arizona EXTCON support" 49 tristate "Wolfson Arizona EXTCON support"
44 depends on MFD_ARIZONA 50 depends on MFD_ARIZONA && INPUT
45 help 51 help
46 Say Y here to enable support for external accessory detection 52 Say Y here to enable support for external accessory detection
47 with Wolfson Arizona devices. These are audio CODECs with 53 with Wolfson Arizona devices. These are audio CODECs with
diff --git a/drivers/extcon/Makefile b/drivers/extcon/Makefile
index 88961b33234..f98a3c4d46e 100644
--- a/drivers/extcon/Makefile
+++ b/drivers/extcon/Makefile
@@ -2,8 +2,9 @@
2# Makefile for external connector class (extcon) devices 2# Makefile for external connector class (extcon) devices
3# 3#
4 4
5obj-$(CONFIG_EXTCON) += extcon_class.o 5obj-$(CONFIG_EXTCON) += extcon-class.o
6obj-$(CONFIG_EXTCON_GPIO) += extcon_gpio.o 6obj-$(CONFIG_EXTCON_GPIO) += extcon-gpio.o
7obj-$(CONFIG_EXTCON_ADC_JACK) += extcon-adc-jack.o
7obj-$(CONFIG_EXTCON_MAX77693) += extcon-max77693.o 8obj-$(CONFIG_EXTCON_MAX77693) += extcon-max77693.o
8obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o 9obj-$(CONFIG_EXTCON_MAX8997) += extcon-max8997.o
9obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o 10obj-$(CONFIG_EXTCON_ARIZONA) += extcon-arizona.o
diff --git a/drivers/extcon/extcon-adc-jack.c b/drivers/extcon/extcon-adc-jack.c
new file mode 100644
index 00000000000..60ac3fbb4cd
--- /dev/null
+++ b/drivers/extcon/extcon-adc-jack.c
@@ -0,0 +1,198 @@
1/*
2 * drivers/extcon/extcon-adc-jack.c
3 *
4 * Analog Jack extcon driver with ADC-based detection capability.
5 *
6 * Copyright (C) 2012 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 *
9 * Modified for calling to IIO to get adc by <anish.singh@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/slab.h>
18#include <linux/device.h>
19#include <linux/platform_device.h>
20#include <linux/err.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
23#include <linux/iio/consumer.h>
24#include <linux/extcon/extcon-adc-jack.h>
25#include <linux/extcon.h>
26
27/**
28 * struct adc_jack_data - internal data for adc_jack device driver
29 * @edev - extcon device.
30 * @cable_names - list of supported cables.
31 * @num_cables - size of cable_names.
32 * @adc_conditions - list of adc value conditions.
33 * @num_conditions - size of adc_conditions.
34 * @irq - irq number of attach/detach event (0 if not exist).
35 * @handling_delay - interrupt handler will schedule extcon event
36 * handling at handling_delay jiffies.
37 * @handler - extcon event handler called by interrupt handler.
38 * @chan - iio channel being queried.
39 */
40struct adc_jack_data {
41 struct extcon_dev edev;
42
43 const char **cable_names;
44 int num_cables;
45 struct adc_jack_cond *adc_conditions;
46 int num_conditions;
47
48 int irq;
49 unsigned long handling_delay; /* in jiffies */
50 struct delayed_work handler;
51
52 struct iio_channel *chan;
53};
54
55static void adc_jack_handler(struct work_struct *work)
56{
57 struct adc_jack_data *data = container_of(to_delayed_work(work),
58 struct adc_jack_data,
59 handler);
60 u32 state = 0;
61 int ret, adc_val;
62 int i;
63
64 ret = iio_read_channel_raw(data->chan, &adc_val);
65 if (ret < 0) {
66 dev_err(data->edev.dev, "read channel() error: %d\n", ret);
67 return;
68 }
69
70 /* Get state from adc value with adc_conditions */
71 for (i = 0; i < data->num_conditions; i++) {
72 struct adc_jack_cond *def = &data->adc_conditions[i];
73 if (!def->state)
74 break;
75 if (def->min_adc <= adc_val && def->max_adc >= adc_val) {
76 state = def->state;
77 break;
78 }
79 }
80 /* if no def has met, it means state = 0 (no cables attached) */
81
82 extcon_set_state(&data->edev, state);
83}
84
85static irqreturn_t adc_jack_irq_thread(int irq, void *_data)
86{
87 struct adc_jack_data *data = _data;
88
89 schedule_delayed_work(&data->handler, data->handling_delay);
90 return IRQ_HANDLED;
91}
92
93static int __devinit adc_jack_probe(struct platform_device *pdev)
94{
95 struct adc_jack_data *data;
96 struct adc_jack_pdata *pdata = pdev->dev.platform_data;
97 int i, err = 0;
98
99 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
100 if (!data)
101 return -ENOMEM;
102
103 data->edev.name = pdata->name;
104
105 if (!pdata->cable_names) {
106 err = -EINVAL;
107 dev_err(&pdev->dev, "error: cable_names not defined.\n");
108 goto out;
109 }
110
111 data->edev.supported_cable = pdata->cable_names;
112
113 /* Check the length of array and set num_cables */
114 for (i = 0; data->edev.supported_cable[i]; i++)
115 ;
116 if (i == 0 || i > SUPPORTED_CABLE_MAX) {
117 err = -EINVAL;
118 dev_err(&pdev->dev, "error: pdata->cable_names size = %d\n",
119 i - 1);
120 goto out;
121 }
122 data->num_cables = i;
123
124 if (!pdata->adc_conditions ||
125 !pdata->adc_conditions[0].state) {
126 err = -EINVAL;
127 dev_err(&pdev->dev, "error: adc_conditions not defined.\n");
128 goto out;
129 }
130 data->adc_conditions = pdata->adc_conditions;
131
132 /* Check the length of array and set num_conditions */
133 for (i = 0; data->adc_conditions[i].state; i++)
134 ;
135 data->num_conditions = i;
136
137 data->chan = iio_channel_get(dev_name(&pdev->dev),
138 pdata->consumer_channel);
139 if (IS_ERR(data->chan)) {
140 err = PTR_ERR(data->chan);
141 goto out;
142 }
143
144 data->handling_delay = msecs_to_jiffies(pdata->handling_delay_ms);
145
146 INIT_DELAYED_WORK_DEFERRABLE(&data->handler, adc_jack_handler);
147
148 platform_set_drvdata(pdev, data);
149
150 err = extcon_dev_register(&data->edev, &pdev->dev);
151 if (err)
152 goto out;
153
154 data->irq = platform_get_irq(pdev, 0);
155 if (!data->irq) {
156 dev_err(&pdev->dev, "platform_get_irq failed\n");
157 err = -ENODEV;
158 goto err_irq;
159 }
160
161 err = request_any_context_irq(data->irq, adc_jack_irq_thread,
162 pdata->irq_flags, pdata->name, data);
163
164 if (err) {
165 dev_err(&pdev->dev, "error: irq %d\n", data->irq);
166 err = -EINVAL;
167 goto err_irq;
168 }
169
170 goto out;
171
172err_irq:
173 extcon_dev_unregister(&data->edev);
174out:
175 return err;
176}
177
178static int __devexit adc_jack_remove(struct platform_device *pdev)
179{
180 struct adc_jack_data *data = platform_get_drvdata(pdev);
181
182 free_irq(data->irq, data);
183 cancel_work_sync(&data->handler.work);
184 extcon_dev_unregister(&data->edev);
185
186 return 0;
187}
188
189static struct platform_driver adc_jack_driver = {
190 .probe = adc_jack_probe,
191 .remove = __devexit_p(adc_jack_remove),
192 .driver = {
193 .name = "adc-jack",
194 .owner = THIS_MODULE,
195 },
196};
197
198module_platform_driver(adc_jack_driver);
diff --git a/drivers/extcon/extcon-arizona.c b/drivers/extcon/extcon-arizona.c
index 6c19833ed2d..cdab9e59829 100644
--- a/drivers/extcon/extcon-arizona.c
+++ b/drivers/extcon/extcon-arizona.c
@@ -21,6 +21,7 @@
21#include <linux/interrupt.h> 21#include <linux/interrupt.h>
22#include <linux/err.h> 22#include <linux/err.h>
23#include <linux/gpio.h> 23#include <linux/gpio.h>
24#include <linux/input.h>
24#include <linux/platform_device.h> 25#include <linux/platform_device.h>
25#include <linux/pm_runtime.h> 26#include <linux/pm_runtime.h>
26#include <linux/regulator/consumer.h> 27#include <linux/regulator/consumer.h>
@@ -30,11 +31,14 @@
30#include <linux/mfd/arizona/pdata.h> 31#include <linux/mfd/arizona/pdata.h>
31#include <linux/mfd/arizona/registers.h> 32#include <linux/mfd/arizona/registers.h>
32 33
34#define ARIZONA_NUM_BUTTONS 6
35
33struct arizona_extcon_info { 36struct arizona_extcon_info {
34 struct device *dev; 37 struct device *dev;
35 struct arizona *arizona; 38 struct arizona *arizona;
36 struct mutex lock; 39 struct mutex lock;
37 struct regulator *micvdd; 40 struct regulator *micvdd;
41 struct input_dev *input;
38 42
39 int micd_mode; 43 int micd_mode;
40 const struct arizona_micd_config *micd_modes; 44 const struct arizona_micd_config *micd_modes;
@@ -54,6 +58,18 @@ static const struct arizona_micd_config micd_default_modes[] = {
54 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 }, 58 { 0, 2 << ARIZONA_MICD_BIAS_SRC_SHIFT, 1 },
55}; 59};
56 60
61static struct {
62 u16 status;
63 int report;
64} arizona_lvl_to_key[ARIZONA_NUM_BUTTONS] = {
65 { 0x1, BTN_0 },
66 { 0x2, BTN_1 },
67 { 0x4, BTN_2 },
68 { 0x8, BTN_3 },
69 { 0x10, BTN_4 },
70 { 0x20, BTN_5 },
71};
72
57#define ARIZONA_CABLE_MECHANICAL 0 73#define ARIZONA_CABLE_MECHANICAL 0
58#define ARIZONA_CABLE_MICROPHONE 1 74#define ARIZONA_CABLE_MICROPHONE 1
59#define ARIZONA_CABLE_HEADPHONE 2 75#define ARIZONA_CABLE_HEADPHONE 2
@@ -133,6 +149,7 @@ static void arizona_stop_mic(struct arizona_extcon_info *info)
133 149
134 if (change) { 150 if (change) {
135 regulator_disable(info->micvdd); 151 regulator_disable(info->micvdd);
152 pm_runtime_mark_last_busy(info->dev);
136 pm_runtime_put_autosuspend(info->dev); 153 pm_runtime_put_autosuspend(info->dev);
137 } 154 }
138} 155}
@@ -141,8 +158,8 @@ static irqreturn_t arizona_micdet(int irq, void *data)
141{ 158{
142 struct arizona_extcon_info *info = data; 159 struct arizona_extcon_info *info = data;
143 struct arizona *arizona = info->arizona; 160 struct arizona *arizona = info->arizona;
144 unsigned int val; 161 unsigned int val, lvl;
145 int ret; 162 int ret, i;
146 163
147 mutex_lock(&info->lock); 164 mutex_lock(&info->lock);
148 165
@@ -219,13 +236,22 @@ static irqreturn_t arizona_micdet(int irq, void *data)
219 236
220 /* 237 /*
221 * If we're still detecting and we detect a short then we've 238 * If we're still detecting and we detect a short then we've
222 * got a headphone. Otherwise it's a button press, the 239 * got a headphone. Otherwise it's a button press.
223 * button reporting is stubbed out for now.
224 */ 240 */
225 if (val & 0x3fc) { 241 if (val & 0x3fc) {
226 if (info->mic) { 242 if (info->mic) {
227 dev_dbg(arizona->dev, "Mic button detected\n"); 243 dev_dbg(arizona->dev, "Mic button detected\n");
228 244
245 lvl = val & ARIZONA_MICD_LVL_MASK;
246 lvl >>= ARIZONA_MICD_LVL_SHIFT;
247
248 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
249 if (lvl & arizona_lvl_to_key[i].status)
250 input_report_key(info->input,
251 arizona_lvl_to_key[i].report,
252 1);
253 input_sync(info->input);
254
229 } else if (info->detecting) { 255 } else if (info->detecting) {
230 dev_dbg(arizona->dev, "Headphone detected\n"); 256 dev_dbg(arizona->dev, "Headphone detected\n");
231 info->detecting = false; 257 info->detecting = false;
@@ -244,6 +270,10 @@ static irqreturn_t arizona_micdet(int irq, void *data)
244 } 270 }
245 } else { 271 } else {
246 dev_dbg(arizona->dev, "Mic button released\n"); 272 dev_dbg(arizona->dev, "Mic button released\n");
273 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
274 input_report_key(info->input,
275 arizona_lvl_to_key[i].report, 0);
276 input_sync(info->input);
247 } 277 }
248 278
249handled: 279handled:
@@ -258,7 +288,7 @@ static irqreturn_t arizona_jackdet(int irq, void *data)
258 struct arizona_extcon_info *info = data; 288 struct arizona_extcon_info *info = data;
259 struct arizona *arizona = info->arizona; 289 struct arizona *arizona = info->arizona;
260 unsigned int val; 290 unsigned int val;
261 int ret; 291 int ret, i;
262 292
263 pm_runtime_get_sync(info->dev); 293 pm_runtime_get_sync(info->dev);
264 294
@@ -288,6 +318,11 @@ static irqreturn_t arizona_jackdet(int irq, void *data)
288 318
289 arizona_stop_mic(info); 319 arizona_stop_mic(info);
290 320
321 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
322 input_report_key(info->input,
323 arizona_lvl_to_key[i].report, 0);
324 input_sync(info->input);
325
291 ret = extcon_update_state(&info->edev, 0xffffffff, 0); 326 ret = extcon_update_state(&info->edev, 0xffffffff, 0);
292 if (ret != 0) 327 if (ret != 0)
293 dev_err(arizona->dev, "Removal report failed: %d\n", 328 dev_err(arizona->dev, "Removal report failed: %d\n",
@@ -307,13 +342,13 @@ static int __devinit arizona_extcon_probe(struct platform_device *pdev)
307 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent); 342 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
308 struct arizona_pdata *pdata; 343 struct arizona_pdata *pdata;
309 struct arizona_extcon_info *info; 344 struct arizona_extcon_info *info;
310 int ret, mode; 345 int ret, mode, i;
311 346
312 pdata = dev_get_platdata(arizona->dev); 347 pdata = dev_get_platdata(arizona->dev);
313 348
314 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 349 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
315 if (!info) { 350 if (!info) {
316 dev_err(&pdev->dev, "failed to allocate memory\n"); 351 dev_err(&pdev->dev, "Failed to allocate memory\n");
317 ret = -ENOMEM; 352 ret = -ENOMEM;
318 goto err; 353 goto err;
319 } 354 }
@@ -350,7 +385,7 @@ static int __devinit arizona_extcon_probe(struct platform_device *pdev)
350 385
351 ret = extcon_dev_register(&info->edev, arizona->dev); 386 ret = extcon_dev_register(&info->edev, arizona->dev);
352 if (ret < 0) { 387 if (ret < 0) {
353 dev_err(arizona->dev, "extcon_dev_regster() failed: %d\n", 388 dev_err(arizona->dev, "extcon_dev_register() failed: %d\n",
354 ret); 389 ret);
355 goto err; 390 goto err;
356 } 391 }
@@ -382,6 +417,20 @@ static int __devinit arizona_extcon_probe(struct platform_device *pdev)
382 417
383 arizona_extcon_set_mode(info, 0); 418 arizona_extcon_set_mode(info, 0);
384 419
420 info->input = input_allocate_device();
421 if (!info->input) {
422 dev_err(arizona->dev, "Can't allocate input dev\n");
423 ret = -ENOMEM;
424 goto err_register;
425 }
426
427 for (i = 0; i < ARIZONA_NUM_BUTTONS; i++)
428 input_set_capability(info->input, EV_KEY,
429 arizona_lvl_to_key[i].report);
430 info->input->name = "Headset";
431 info->input->phys = "arizona/extcon";
432 info->input->dev.parent = &pdev->dev;
433
385 pm_runtime_enable(&pdev->dev); 434 pm_runtime_enable(&pdev->dev);
386 pm_runtime_idle(&pdev->dev); 435 pm_runtime_idle(&pdev->dev);
387 pm_runtime_get_sync(&pdev->dev); 436 pm_runtime_get_sync(&pdev->dev);
@@ -391,7 +440,7 @@ static int __devinit arizona_extcon_probe(struct platform_device *pdev)
391 if (ret != 0) { 440 if (ret != 0) {
392 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n", 441 dev_err(&pdev->dev, "Failed to get JACKDET rise IRQ: %d\n",
393 ret); 442 ret);
394 goto err_register; 443 goto err_input;
395 } 444 }
396 445
397 ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1); 446 ret = arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 1);
@@ -441,8 +490,16 @@ static int __devinit arizona_extcon_probe(struct platform_device *pdev)
441 490
442 pm_runtime_put(&pdev->dev); 491 pm_runtime_put(&pdev->dev);
443 492
493 ret = input_register_device(info->input);
494 if (ret) {
495 dev_err(&pdev->dev, "Can't register input device: %d\n", ret);
496 goto err_micdet;
497 }
498
444 return 0; 499 return 0;
445 500
501err_micdet:
502 arizona_free_irq(arizona, ARIZONA_IRQ_MICDET, info);
446err_fall_wake: 503err_fall_wake:
447 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0); 504 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_FALL, 0);
448err_fall: 505err_fall:
@@ -451,6 +508,8 @@ err_rise_wake:
451 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0); 508 arizona_set_irq_wake(arizona, ARIZONA_IRQ_JD_RISE, 0);
452err_rise: 509err_rise:
453 arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info); 510 arizona_free_irq(arizona, ARIZONA_IRQ_JD_RISE, info);
511err_input:
512 input_free_device(info->input);
454err_register: 513err_register:
455 pm_runtime_disable(&pdev->dev); 514 pm_runtime_disable(&pdev->dev);
456 extcon_dev_unregister(&info->edev); 515 extcon_dev_unregister(&info->edev);
@@ -473,6 +532,7 @@ static int __devexit arizona_extcon_remove(struct platform_device *pdev)
473 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, 532 regmap_update_bits(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE,
474 ARIZONA_JD1_ENA, 0); 533 ARIZONA_JD1_ENA, 0);
475 arizona_clk32k_disable(arizona); 534 arizona_clk32k_disable(arizona);
535 input_unregister_device(info->input);
476 extcon_dev_unregister(&info->edev); 536 extcon_dev_unregister(&info->edev);
477 537
478 return 0; 538 return 0;
diff --git a/drivers/extcon/extcon_class.c b/drivers/extcon/extcon-class.c
index f6419f9db76..946a3188b2b 100644
--- a/drivers/extcon/extcon_class.c
+++ b/drivers/extcon/extcon-class.c
@@ -30,6 +30,7 @@
30#include <linux/err.h> 30#include <linux/err.h>
31#include <linux/extcon.h> 31#include <linux/extcon.h>
32#include <linux/slab.h> 32#include <linux/slab.h>
33#include <linux/sysfs.h>
33 34
34/* 35/*
35 * extcon_cable_name suggests the standard cable names for commonly used 36 * extcon_cable_name suggests the standard cable names for commonly used
@@ -442,7 +443,7 @@ static int _call_per_cable(struct notifier_block *nb, unsigned long val,
442 443
443/** 444/**
444 * extcon_register_interest() - Register a notifier for a state change of a 445 * extcon_register_interest() - Register a notifier for a state change of a
445 * specific cable, not a entier set of cables of a 446 * specific cable, not an entier set of cables of a
446 * extcon device. 447 * extcon device.
447 * @obj: an empty extcon_specific_cable_nb object to be returned. 448 * @obj: an empty extcon_specific_cable_nb object to be returned.
448 * @extcon_name: the name of extcon device. 449 * @extcon_name: the name of extcon device.
@@ -498,7 +499,7 @@ int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
498} 499}
499 500
500/** 501/**
501 * extcon_register_notifier() - Register a notifee to get notified by 502 * extcon_register_notifier() - Register a notifiee to get notified by
502 * any attach status changes from the extcon. 503 * any attach status changes from the extcon.
503 * @edev: the extcon device. 504 * @edev: the extcon device.
504 * @nb: a notifier block to be registered. 505 * @nb: a notifier block to be registered.
@@ -515,7 +516,7 @@ int extcon_register_notifier(struct extcon_dev *edev,
515EXPORT_SYMBOL_GPL(extcon_register_notifier); 516EXPORT_SYMBOL_GPL(extcon_register_notifier);
516 517
517/** 518/**
518 * extcon_unregister_notifier() - Unregister a notifee from the extcon device. 519 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
519 * @edev: the extcon device. 520 * @edev: the extcon device.
520 * @nb: a registered notifier block to be unregistered. 521 * @nb: a registered notifier block to be unregistered.
521 */ 522 */
@@ -673,10 +674,12 @@ int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
673 cable->attr_g.name = str; 674 cable->attr_g.name = str;
674 cable->attr_g.attrs = cable->attrs; 675 cable->attr_g.attrs = cable->attrs;
675 676
677 sysfs_attr_init(&cable->attr_name.attr);
676 cable->attr_name.attr.name = "name"; 678 cable->attr_name.attr.name = "name";
677 cable->attr_name.attr.mode = 0444; 679 cable->attr_name.attr.mode = 0444;
678 cable->attr_name.show = cable_name_show; 680 cable->attr_name.show = cable_name_show;
679 681
682 sysfs_attr_init(&cable->attr_state.attr);
680 cable->attr_state.attr.name = "state"; 683 cable->attr_state.attr.name = "state";
681 cable->attr_state.attr.mode = 0644; 684 cable->attr_state.attr.mode = 0644;
682 cable->attr_state.show = cable_state_show; 685 cable->attr_state.show = cable_state_show;
@@ -722,6 +725,7 @@ int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
722 goto err_muex; 725 goto err_muex;
723 } 726 }
724 strcpy(name, buf); 727 strcpy(name, buf);
728 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
725 edev->d_attrs_muex[index].attr.name = name; 729 edev->d_attrs_muex[index].attr.name = name;
726 edev->d_attrs_muex[index].attr.mode = 0000; 730 edev->d_attrs_muex[index].attr.mode = 0000;
727 edev->attrs_muex[index] = &edev->d_attrs_muex[index] 731 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
@@ -802,7 +806,7 @@ EXPORT_SYMBOL_GPL(extcon_dev_register);
802 806
803/** 807/**
804 * extcon_dev_unregister() - Unregister the extcon device. 808 * extcon_dev_unregister() - Unregister the extcon device.
805 * @edev: the extcon device instance to be unregitered. 809 * @edev: the extcon device instance to be unregistered.
806 * 810 *
807 * Note that this does not call kfree(edev) because edev was not allocated 811 * Note that this does not call kfree(edev) because edev was not allocated
808 * by this class. 812 * by this class.
diff --git a/drivers/extcon/extcon_gpio.c b/drivers/extcon/extcon-gpio.c
index 3cc152e690b..3cc152e690b 100644
--- a/drivers/extcon/extcon_gpio.c
+++ b/drivers/extcon/extcon-gpio.c
diff --git a/drivers/extcon/extcon-max77693.c b/drivers/extcon/extcon-max77693.c
index 38f9e52f358..e21387e2da5 100644
--- a/drivers/extcon/extcon-max77693.c
+++ b/drivers/extcon/extcon-max77693.c
@@ -356,7 +356,7 @@ static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info,
356 extcon_set_cable_state(info->edev, "MHL", attached); 356 extcon_set_cable_state(info->edev, "MHL", attached);
357 break; 357 break;
358 default: 358 default:
359 dev_err(info->dev, "faild to detect %s accessory\n", 359 dev_err(info->dev, "failed to detect %s accessory\n",
360 attached ? "attached" : "detached"); 360 attached ? "attached" : "detached");
361 dev_err(info->dev, "- adc:0x%x, adclow:0x%x, adc1k:0x%x\n", 361 dev_err(info->dev, "- adc:0x%x, adclow:0x%x, adc1k:0x%x\n",
362 adc, adclow, adc1k); 362 adc, adclow, adc1k);
@@ -548,7 +548,7 @@ static void max77693_muic_irq_work(struct work_struct *work)
548 curr_adc = info->status[0] & STATUS1_ADC_MASK; 548 curr_adc = info->status[0] & STATUS1_ADC_MASK;
549 curr_adc >>= STATUS1_ADC_SHIFT; 549 curr_adc >>= STATUS1_ADC_SHIFT;
550 550
551 /* Check accossory state which is either detached or attached */ 551 /* Check accessory state which is either detached or attached */
552 if (curr_adc == MAX77693_MUIC_ADC_OPEN) 552 if (curr_adc == MAX77693_MUIC_ADC_OPEN)
553 attached = false; 553 attached = false;
554 554
@@ -564,7 +564,7 @@ static void max77693_muic_irq_work(struct work_struct *work)
564 curr_chg_type = info->status[1] & STATUS2_CHGTYP_MASK; 564 curr_chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
565 curr_chg_type >>= STATUS2_CHGTYP_SHIFT; 565 curr_chg_type >>= STATUS2_CHGTYP_SHIFT;
566 566
567 /* Check charger accossory state which 567 /* Check charger accessory state which
568 is either detached or attached */ 568 is either detached or attached */
569 if (curr_chg_type == MAX77693_CHARGER_TYPE_NONE) 569 if (curr_chg_type == MAX77693_CHARGER_TYPE_NONE)
570 attached = false; 570 attached = false;
@@ -699,7 +699,7 @@ static int __devinit max77693_muic_probe(struct platform_device *pdev)
699 699
700 ret = request_threaded_irq(virq, NULL, 700 ret = request_threaded_irq(virq, NULL,
701 max77693_muic_irq_handler, 701 max77693_muic_irq_handler,
702 0, muic_irq->name, info); 702 IRQF_ONESHOT, muic_irq->name, info);
703 if (ret) { 703 if (ret) {
704 dev_err(&pdev->dev, 704 dev_err(&pdev->dev,
705 "failed: irq request (IRQ: %d," 705 "failed: irq request (IRQ: %d,"
diff --git a/drivers/firmware/efivars.c b/drivers/firmware/efivars.c
index 47408e802ab..d10c9873dd9 100644
--- a/drivers/firmware/efivars.c
+++ b/drivers/firmware/efivars.c
@@ -435,12 +435,23 @@ efivar_attr_read(struct efivar_entry *entry, char *buf)
435 if (status != EFI_SUCCESS) 435 if (status != EFI_SUCCESS)
436 return -EIO; 436 return -EIO;
437 437
438 if (var->Attributes & 0x1) 438 if (var->Attributes & EFI_VARIABLE_NON_VOLATILE)
439 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n"); 439 str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n");
440 if (var->Attributes & 0x2) 440 if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS)
441 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n"); 441 str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n");
442 if (var->Attributes & 0x4) 442 if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)
443 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n"); 443 str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n");
444 if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD)
445 str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n");
446 if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)
447 str += sprintf(str,
448 "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n");
449 if (var->Attributes &
450 EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)
451 str += sprintf(str,
452 "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n");
453 if (var->Attributes & EFI_VARIABLE_APPEND_WRITE)
454 str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n");
444 return str - buf; 455 return str - buf;
445} 456}
446 457
diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c
index 86f8885aeb4..3648f8f0f36 100644
--- a/drivers/hv/hv.c
+++ b/drivers/hv/hv.c
@@ -26,6 +26,7 @@
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/vmalloc.h> 27#include <linux/vmalloc.h>
28#include <linux/hyperv.h> 28#include <linux/hyperv.h>
29#include <linux/version.h>
29#include <asm/hyperv.h> 30#include <asm/hyperv.h>
30#include "hyperv_vmbus.h" 31#include "hyperv_vmbus.h"
31 32
@@ -38,28 +39,6 @@ struct hv_context hv_context = {
38}; 39};
39 40
40/* 41/*
41 * query_hypervisor_presence
42 * - Query the cpuid for presence of windows hypervisor
43 */
44static int query_hypervisor_presence(void)
45{
46 unsigned int eax;
47 unsigned int ebx;
48 unsigned int ecx;
49 unsigned int edx;
50 unsigned int op;
51
52 eax = 0;
53 ebx = 0;
54 ecx = 0;
55 edx = 0;
56 op = HVCPUID_VERSION_FEATURES;
57 cpuid(op, &eax, &ebx, &ecx, &edx);
58
59 return ecx & HV_PRESENT_BIT;
60}
61
62/*
63 * query_hypervisor_info - Get version info of the windows hypervisor 42 * query_hypervisor_info - Get version info of the windows hypervisor
64 */ 43 */
65static int query_hypervisor_info(void) 44static int query_hypervisor_info(void)
@@ -159,14 +138,13 @@ int hv_init(void)
159 memset(hv_context.synic_message_page, 0, 138 memset(hv_context.synic_message_page, 0,
160 sizeof(void *) * NR_CPUS); 139 sizeof(void *) * NR_CPUS);
161 140
162 if (!query_hypervisor_presence())
163 goto cleanup;
164
165 max_leaf = query_hypervisor_info(); 141 max_leaf = query_hypervisor_info();
166 142
167 /* Write our OS info */ 143 /*
168 wrmsrl(HV_X64_MSR_GUEST_OS_ID, HV_LINUX_GUEST_ID); 144 * Write our OS ID.
169 hv_context.guestid = HV_LINUX_GUEST_ID; 145 */
146 hv_context.guestid = generate_guest_id(0, LINUX_VERSION_CODE, 0);
147 wrmsrl(HV_X64_MSR_GUEST_OS_ID, hv_context.guestid);
170 148
171 /* See if the hypercall page is already set */ 149 /* See if the hypercall page is already set */
172 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 150 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
diff --git a/drivers/hv/hv_kvp.c b/drivers/hv/hv_kvp.c
index 0012eed6d87..ed50e9e83c6 100644
--- a/drivers/hv/hv_kvp.c
+++ b/drivers/hv/hv_kvp.c
@@ -48,13 +48,24 @@ static struct {
48 void *kvp_context; /* for the channel callback */ 48 void *kvp_context; /* for the channel callback */
49} kvp_transaction; 49} kvp_transaction;
50 50
51/*
52 * Before we can accept KVP messages from the host, we need
53 * to handshake with the user level daemon. This state tracks
54 * if we are in the handshake phase.
55 */
56static bool in_hand_shake = true;
57
58/*
59 * This state maintains the version number registered by the daemon.
60 */
61static int dm_reg_value;
62
51static void kvp_send_key(struct work_struct *dummy); 63static void kvp_send_key(struct work_struct *dummy);
52 64
53#define TIMEOUT_FIRED 1
54 65
55static void kvp_respond_to_host(char *key, char *value, int error); 66static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
56static void kvp_work_func(struct work_struct *dummy); 67static void kvp_work_func(struct work_struct *dummy);
57static void kvp_register(void); 68static void kvp_register(int);
58 69
59static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func); 70static DECLARE_DELAYED_WORK(kvp_work, kvp_work_func);
60static DECLARE_WORK(kvp_sendkey_work, kvp_send_key); 71static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
@@ -68,7 +79,7 @@ static u8 *recv_buffer;
68 */ 79 */
69 80
70static void 81static void
71kvp_register(void) 82kvp_register(int reg_value)
72{ 83{
73 84
74 struct cn_msg *msg; 85 struct cn_msg *msg;
@@ -83,7 +94,7 @@ kvp_register(void)
83 msg->id.idx = CN_KVP_IDX; 94 msg->id.idx = CN_KVP_IDX;
84 msg->id.val = CN_KVP_VAL; 95 msg->id.val = CN_KVP_VAL;
85 96
86 kvp_msg->kvp_hdr.operation = KVP_OP_REGISTER; 97 kvp_msg->kvp_hdr.operation = reg_value;
87 strcpy(version, HV_DRV_VERSION); 98 strcpy(version, HV_DRV_VERSION);
88 msg->len = sizeof(struct hv_kvp_msg); 99 msg->len = sizeof(struct hv_kvp_msg);
89 cn_netlink_send(msg, 0, GFP_ATOMIC); 100 cn_netlink_send(msg, 0, GFP_ATOMIC);
@@ -97,9 +108,43 @@ kvp_work_func(struct work_struct *dummy)
97 * If the timer fires, the user-mode component has not responded; 108 * If the timer fires, the user-mode component has not responded;
98 * process the pending transaction. 109 * process the pending transaction.
99 */ 110 */
100 kvp_respond_to_host("Unknown key", "Guest timed out", TIMEOUT_FIRED); 111 kvp_respond_to_host(NULL, HV_E_FAIL);
112}
113
114static int kvp_handle_handshake(struct hv_kvp_msg *msg)
115{
116 int ret = 1;
117
118 switch (msg->kvp_hdr.operation) {
119 case KVP_OP_REGISTER:
120 dm_reg_value = KVP_OP_REGISTER;
121 pr_info("KVP: IP injection functionality not available\n");
122 pr_info("KVP: Upgrade the KVP daemon\n");
123 break;
124 case KVP_OP_REGISTER1:
125 dm_reg_value = KVP_OP_REGISTER1;
126 break;
127 default:
128 pr_info("KVP: incompatible daemon\n");
129 pr_info("KVP: KVP version: %d, Daemon version: %d\n",
130 KVP_OP_REGISTER1, msg->kvp_hdr.operation);
131 ret = 0;
132 }
133
134 if (ret) {
135 /*
136 * We have a compatible daemon; complete the handshake.
137 */
138 pr_info("KVP: user-mode registering done.\n");
139 kvp_register(dm_reg_value);
140 kvp_transaction.active = false;
141 if (kvp_transaction.kvp_context)
142 hv_kvp_onchannelcallback(kvp_transaction.kvp_context);
143 }
144 return ret;
101} 145}
102 146
147
103/* 148/*
104 * Callback when data is received from user mode. 149 * Callback when data is received from user mode.
105 */ 150 */
@@ -109,29 +154,165 @@ kvp_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
109{ 154{
110 struct hv_kvp_msg *message; 155 struct hv_kvp_msg *message;
111 struct hv_kvp_msg_enumerate *data; 156 struct hv_kvp_msg_enumerate *data;
157 int error = 0;
112 158
113 message = (struct hv_kvp_msg *)msg->data; 159 message = (struct hv_kvp_msg *)msg->data;
114 switch (message->kvp_hdr.operation) { 160
161 /*
162 * If we are negotiating the version information
163 * with the daemon; handle that first.
164 */
165
166 if (in_hand_shake) {
167 if (kvp_handle_handshake(message))
168 in_hand_shake = false;
169 return;
170 }
171
172 /*
173 * Based on the version of the daemon, we propagate errors from the
174 * daemon differently.
175 */
176
177 data = &message->body.kvp_enum_data;
178
179 switch (dm_reg_value) {
115 case KVP_OP_REGISTER: 180 case KVP_OP_REGISTER:
116 pr_info("KVP: user-mode registering done.\n"); 181 /*
117 kvp_register(); 182 * Null string is used to pass back error condition.
118 kvp_transaction.active = false; 183 */
119 hv_kvp_onchannelcallback(kvp_transaction.kvp_context); 184 if (data->data.key[0] == 0)
185 error = HV_S_CONT;
120 break; 186 break;
121 187
122 default: 188 case KVP_OP_REGISTER1:
123 data = &message->body.kvp_enum_data;
124 /* 189 /*
125 * Complete the transaction by forwarding the key value 190 * We use the message header information from
126 * to the host. But first, cancel the timeout. 191 * the user level daemon to transmit errors.
127 */ 192 */
128 if (cancel_delayed_work_sync(&kvp_work)) 193 error = message->error;
129 kvp_respond_to_host(data->data.key, 194 break;
130 data->data.value, 195 }
131 !strlen(data->data.key)); 196
197 /*
198 * Complete the transaction by forwarding the key value
199 * to the host. But first, cancel the timeout.
200 */
201 if (cancel_delayed_work_sync(&kvp_work))
202 kvp_respond_to_host(message, error);
203}
204
205
206static int process_ob_ipinfo(void *in_msg, void *out_msg, int op)
207{
208 struct hv_kvp_msg *in = in_msg;
209 struct hv_kvp_ip_msg *out = out_msg;
210 int len;
211
212 switch (op) {
213 case KVP_OP_GET_IP_INFO:
214 /*
215 * Transform all parameters into utf16 encoding.
216 */
217 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.ip_addr,
218 strlen((char *)in->body.kvp_ip_val.ip_addr),
219 UTF16_HOST_ENDIAN,
220 (wchar_t *)out->kvp_ip_val.ip_addr,
221 MAX_IP_ADDR_SIZE);
222 if (len < 0)
223 return len;
224
225 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.sub_net,
226 strlen((char *)in->body.kvp_ip_val.sub_net),
227 UTF16_HOST_ENDIAN,
228 (wchar_t *)out->kvp_ip_val.sub_net,
229 MAX_IP_ADDR_SIZE);
230 if (len < 0)
231 return len;
232
233 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.gate_way,
234 strlen((char *)in->body.kvp_ip_val.gate_way),
235 UTF16_HOST_ENDIAN,
236 (wchar_t *)out->kvp_ip_val.gate_way,
237 MAX_GATEWAY_SIZE);
238 if (len < 0)
239 return len;
240
241 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.dns_addr,
242 strlen((char *)in->body.kvp_ip_val.dns_addr),
243 UTF16_HOST_ENDIAN,
244 (wchar_t *)out->kvp_ip_val.dns_addr,
245 MAX_IP_ADDR_SIZE);
246 if (len < 0)
247 return len;
248
249 len = utf8s_to_utf16s((char *)in->body.kvp_ip_val.adapter_id,
250 strlen((char *)in->body.kvp_ip_val.adapter_id),
251 UTF16_HOST_ENDIAN,
252 (wchar_t *)out->kvp_ip_val.adapter_id,
253 MAX_IP_ADDR_SIZE);
254 if (len < 0)
255 return len;
256
257 out->kvp_ip_val.dhcp_enabled =
258 in->body.kvp_ip_val.dhcp_enabled;
259 out->kvp_ip_val.addr_family =
260 in->body.kvp_ip_val.addr_family;
261 }
262
263 return 0;
264}
265
266static void process_ib_ipinfo(void *in_msg, void *out_msg, int op)
267{
268 struct hv_kvp_ip_msg *in = in_msg;
269 struct hv_kvp_msg *out = out_msg;
270
271 switch (op) {
272 case KVP_OP_SET_IP_INFO:
273 /*
274 * Transform all parameters into utf8 encoding.
275 */
276 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.ip_addr,
277 MAX_IP_ADDR_SIZE,
278 UTF16_LITTLE_ENDIAN,
279 (__u8 *)out->body.kvp_ip_val.ip_addr,
280 MAX_IP_ADDR_SIZE);
281
282 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.sub_net,
283 MAX_IP_ADDR_SIZE,
284 UTF16_LITTLE_ENDIAN,
285 (__u8 *)out->body.kvp_ip_val.sub_net,
286 MAX_IP_ADDR_SIZE);
287
288 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.gate_way,
289 MAX_GATEWAY_SIZE,
290 UTF16_LITTLE_ENDIAN,
291 (__u8 *)out->body.kvp_ip_val.gate_way,
292 MAX_GATEWAY_SIZE);
293
294 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.dns_addr,
295 MAX_IP_ADDR_SIZE,
296 UTF16_LITTLE_ENDIAN,
297 (__u8 *)out->body.kvp_ip_val.dns_addr,
298 MAX_IP_ADDR_SIZE);
299
300 out->body.kvp_ip_val.dhcp_enabled = in->kvp_ip_val.dhcp_enabled;
301
302 default:
303 utf16s_to_utf8s((wchar_t *)in->kvp_ip_val.adapter_id,
304 MAX_ADAPTER_ID_SIZE,
305 UTF16_LITTLE_ENDIAN,
306 (__u8 *)out->body.kvp_ip_val.adapter_id,
307 MAX_ADAPTER_ID_SIZE);
308
309 out->body.kvp_ip_val.addr_family = in->kvp_ip_val.addr_family;
132 } 310 }
133} 311}
134 312
313
314
315
135static void 316static void
136kvp_send_key(struct work_struct *dummy) 317kvp_send_key(struct work_struct *dummy)
137{ 318{
@@ -167,6 +348,12 @@ kvp_send_key(struct work_struct *dummy)
167 */ 348 */
168 349
169 switch (message->kvp_hdr.operation) { 350 switch (message->kvp_hdr.operation) {
351 case KVP_OP_SET_IP_INFO:
352 process_ib_ipinfo(in_msg, message, KVP_OP_SET_IP_INFO);
353 break;
354 case KVP_OP_GET_IP_INFO:
355 process_ib_ipinfo(in_msg, message, KVP_OP_GET_IP_INFO);
356 break;
170 case KVP_OP_SET: 357 case KVP_OP_SET:
171 switch (in_msg->body.kvp_set.data.value_type) { 358 switch (in_msg->body.kvp_set.data.value_type) {
172 case REG_SZ: 359 case REG_SZ:
@@ -243,17 +430,19 @@ kvp_send_key(struct work_struct *dummy)
243 */ 430 */
244 431
245static void 432static void
246kvp_respond_to_host(char *key, char *value, int error) 433kvp_respond_to_host(struct hv_kvp_msg *msg_to_host, int error)
247{ 434{
248 struct hv_kvp_msg *kvp_msg; 435 struct hv_kvp_msg *kvp_msg;
249 struct hv_kvp_exchg_msg_value *kvp_data; 436 struct hv_kvp_exchg_msg_value *kvp_data;
250 char *key_name; 437 char *key_name;
438 char *value;
251 struct icmsg_hdr *icmsghdrp; 439 struct icmsg_hdr *icmsghdrp;
252 int keylen = 0; 440 int keylen = 0;
253 int valuelen = 0; 441 int valuelen = 0;
254 u32 buf_len; 442 u32 buf_len;
255 struct vmbus_channel *channel; 443 struct vmbus_channel *channel;
256 u64 req_id; 444 u64 req_id;
445 int ret;
257 446
258 /* 447 /*
259 * If a transaction is not active; log and return. 448 * If a transaction is not active; log and return.
@@ -287,6 +476,7 @@ kvp_respond_to_host(char *key, char *value, int error)
287 */ 476 */
288 return; 477 return;
289 478
479 icmsghdrp->status = error;
290 480
291 /* 481 /*
292 * If the error parameter is set, terminate the host's enumeration 482 * If the error parameter is set, terminate the host's enumeration
@@ -294,20 +484,27 @@ kvp_respond_to_host(char *key, char *value, int error)
294 */ 484 */
295 if (error) { 485 if (error) {
296 /* 486 /*
297 * Something failed or the we have timedout; 487 * Something failed or we have timedout;
298 * terminate the current host-side iteration. 488 * terminate the current host-side iteration.
299 */ 489 */
300 icmsghdrp->status = HV_S_CONT;
301 goto response_done; 490 goto response_done;
302 } 491 }
303 492
304 icmsghdrp->status = HV_S_OK;
305
306 kvp_msg = (struct hv_kvp_msg *) 493 kvp_msg = (struct hv_kvp_msg *)
307 &recv_buffer[sizeof(struct vmbuspipe_hdr) + 494 &recv_buffer[sizeof(struct vmbuspipe_hdr) +
308 sizeof(struct icmsg_hdr)]; 495 sizeof(struct icmsg_hdr)];
309 496
310 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) { 497 switch (kvp_transaction.kvp_msg->kvp_hdr.operation) {
498 case KVP_OP_GET_IP_INFO:
499 ret = process_ob_ipinfo(msg_to_host,
500 (struct hv_kvp_ip_msg *)kvp_msg,
501 KVP_OP_GET_IP_INFO);
502 if (ret < 0)
503 icmsghdrp->status = HV_E_FAIL;
504
505 goto response_done;
506 case KVP_OP_SET_IP_INFO:
507 goto response_done;
311 case KVP_OP_GET: 508 case KVP_OP_GET:
312 kvp_data = &kvp_msg->body.kvp_get.data; 509 kvp_data = &kvp_msg->body.kvp_get.data;
313 goto copy_value; 510 goto copy_value;
@@ -321,7 +518,7 @@ kvp_respond_to_host(char *key, char *value, int error)
321 } 518 }
322 519
323 kvp_data = &kvp_msg->body.kvp_enum_data.data; 520 kvp_data = &kvp_msg->body.kvp_enum_data.data;
324 key_name = key; 521 key_name = msg_to_host->body.kvp_enum_data.data.key;
325 522
326 /* 523 /*
327 * The windows host expects the key/value pair to be encoded 524 * The windows host expects the key/value pair to be encoded
@@ -335,6 +532,7 @@ kvp_respond_to_host(char *key, char *value, int error)
335 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */ 532 kvp_data->key_size = 2*(keylen + 1); /* utf16 encoding */
336 533
337copy_value: 534copy_value:
535 value = msg_to_host->body.kvp_enum_data.data.value;
338 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN, 536 valuelen = utf8s_to_utf16s(value, strlen(value), UTF16_HOST_ENDIAN,
339 (wchar_t *) kvp_data->value, 537 (wchar_t *) kvp_data->value,
340 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2); 538 (HV_KVP_EXCHANGE_MAX_VALUE_SIZE / 2) - 2);
@@ -387,7 +585,8 @@ void hv_kvp_onchannelcallback(void *context)
387 return; 585 return;
388 } 586 }
389 587
390 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE, &recvlen, &requestid); 588 vmbus_recvpacket(channel, recv_buffer, PAGE_SIZE * 2, &recvlen,
589 &requestid);
391 590
392 if (recvlen > 0) { 591 if (recvlen > 0) {
393 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[ 592 icmsghdrp = (struct icmsg_hdr *)&recv_buffer[
diff --git a/drivers/hv/hv_util.c b/drivers/hv/hv_util.c
index d3ac6a40118..a0667de7a04 100644
--- a/drivers/hv/hv_util.c
+++ b/drivers/hv/hv_util.c
@@ -263,7 +263,7 @@ static int util_probe(struct hv_device *dev,
263 (struct hv_util_service *)dev_id->driver_data; 263 (struct hv_util_service *)dev_id->driver_data;
264 int ret; 264 int ret;
265 265
266 srv->recv_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); 266 srv->recv_buffer = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
267 if (!srv->recv_buffer) 267 if (!srv->recv_buffer)
268 return -ENOMEM; 268 return -ENOMEM;
269 if (srv->util_init) { 269 if (srv->util_init) {
@@ -274,7 +274,7 @@ static int util_probe(struct hv_device *dev,
274 } 274 }
275 } 275 }
276 276
277 ret = vmbus_open(dev->channel, 2 * PAGE_SIZE, 2 * PAGE_SIZE, NULL, 0, 277 ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0,
278 srv->util_cb, dev->channel); 278 srv->util_cb, dev->channel);
279 if (ret) 279 if (ret)
280 goto error; 280 goto error;
diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h
index 0614ff3a7d7..d8d1fadb398 100644
--- a/drivers/hv/hyperv_vmbus.h
+++ b/drivers/hv/hyperv_vmbus.h
@@ -410,10 +410,49 @@ enum {
410 410
411#define HV_PRESENT_BIT 0x80000000 411#define HV_PRESENT_BIT 0x80000000
412 412
413#define HV_LINUX_GUEST_ID_LO 0x00000000 413/*
414#define HV_LINUX_GUEST_ID_HI 2976579765 414 * The guest OS needs to register the guest ID with the hypervisor.
415#define HV_LINUX_GUEST_ID (((u64)HV_LINUX_GUEST_ID_HI << 32) | \ 415 * The guest ID is a 64 bit entity and the structure of this ID is
416 HV_LINUX_GUEST_ID_LO) 416 * specified in the Hyper-V specification:
417 *
418 * http://msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
419 *
420 * While the current guideline does not specify how Linux guest ID(s)
421 * need to be generated, our plan is to publish the guidelines for
422 * Linux and other guest operating systems that currently are hosted
423 * on Hyper-V. The implementation here conforms to this yet
424 * unpublished guidelines.
425 *
426 *
427 * Bit(s)
428 * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
429 * 62:56 - Os Type; Linux is 0x100
430 * 55:48 - Distro specific identification
431 * 47:16 - Linux kernel version number
432 * 15:0 - Distro specific identification
433 *
434 *
435 */
436
437#define HV_LINUX_VENDOR_ID 0x8100
438
439/*
440 * Generate the guest ID based on the guideline described above.
441 */
442
443static inline __u64 generate_guest_id(__u8 d_info1, __u32 kernel_version,
444 __u16 d_info2)
445{
446 __u64 guest_id = 0;
447
448 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
449 guest_id |= (((__u64)(d_info1)) << 48);
450 guest_id |= (((__u64)(kernel_version)) << 16);
451 guest_id |= ((__u64)(d_info2));
452
453 return guest_id;
454}
455
417 456
418#define HV_CPU_POWER_MANAGEMENT (1 << 0) 457#define HV_CPU_POWER_MANAGEMENT (1 << 0)
419#define HV_RECOMMENDATIONS_MAX 4 458#define HV_RECOMMENDATIONS_MAX 4
diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
index 4748086eaaf..8e1a9ec5300 100644
--- a/drivers/hv/vmbus_drv.c
+++ b/drivers/hv/vmbus_drv.c
@@ -34,6 +34,7 @@
34#include <linux/completion.h> 34#include <linux/completion.h>
35#include <linux/hyperv.h> 35#include <linux/hyperv.h>
36#include <asm/hyperv.h> 36#include <asm/hyperv.h>
37#include <asm/hypervisor.h>
37#include "hyperv_vmbus.h" 38#include "hyperv_vmbus.h"
38 39
39 40
@@ -146,43 +147,9 @@ static ssize_t vmbus_show_device_attr(struct device *dev,
146 get_channel_info(hv_dev, device_info); 147 get_channel_info(hv_dev, device_info);
147 148
148 if (!strcmp(dev_attr->attr.name, "class_id")) { 149 if (!strcmp(dev_attr->attr.name, "class_id")) {
149 ret = sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-" 150 ret = sprintf(buf, "{%pUl}\n", device_info->chn_type.b);
150 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
151 device_info->chn_type.b[3],
152 device_info->chn_type.b[2],
153 device_info->chn_type.b[1],
154 device_info->chn_type.b[0],
155 device_info->chn_type.b[5],
156 device_info->chn_type.b[4],
157 device_info->chn_type.b[7],
158 device_info->chn_type.b[6],
159 device_info->chn_type.b[8],
160 device_info->chn_type.b[9],
161 device_info->chn_type.b[10],
162 device_info->chn_type.b[11],
163 device_info->chn_type.b[12],
164 device_info->chn_type.b[13],
165 device_info->chn_type.b[14],
166 device_info->chn_type.b[15]);
167 } else if (!strcmp(dev_attr->attr.name, "device_id")) { 151 } else if (!strcmp(dev_attr->attr.name, "device_id")) {
168 ret = sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-" 152 ret = sprintf(buf, "{%pUl}\n", device_info->chn_instance.b);
169 "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
170 device_info->chn_instance.b[3],
171 device_info->chn_instance.b[2],
172 device_info->chn_instance.b[1],
173 device_info->chn_instance.b[0],
174 device_info->chn_instance.b[5],
175 device_info->chn_instance.b[4],
176 device_info->chn_instance.b[7],
177 device_info->chn_instance.b[6],
178 device_info->chn_instance.b[8],
179 device_info->chn_instance.b[9],
180 device_info->chn_instance.b[10],
181 device_info->chn_instance.b[11],
182 device_info->chn_instance.b[12],
183 device_info->chn_instance.b[13],
184 device_info->chn_instance.b[14],
185 device_info->chn_instance.b[15]);
186 } else if (!strcmp(dev_attr->attr.name, "modalias")) { 153 } else if (!strcmp(dev_attr->attr.name, "modalias")) {
187 print_alias_name(hv_dev, alias_name); 154 print_alias_name(hv_dev, alias_name);
188 ret = sprintf(buf, "vmbus:%s\n", alias_name); 155 ret = sprintf(buf, "vmbus:%s\n", alias_name);
@@ -757,6 +724,9 @@ static int __init hv_acpi_init(void)
757{ 724{
758 int ret, t; 725 int ret, t;
759 726
727 if (x86_hyper != &x86_hyper_ms_hyperv)
728 return -ENODEV;
729
760 init_completion(&probe_event); 730 init_completion(&probe_event);
761 731
762 /* 732 /*
diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile
index 42b3ce9d80f..9cce5d70ed5 100644
--- a/drivers/memory/Makefile
+++ b/drivers/memory/Makefile
@@ -2,6 +2,9 @@
2# Makefile for memory devices 2# Makefile for memory devices
3# 3#
4 4
5ifeq ($(CONFIG_DDR),y)
6obj-$(CONFIG_OF) += of_memory.o
7endif
5obj-$(CONFIG_TI_EMIF) += emif.o 8obj-$(CONFIG_TI_EMIF) += emif.o
6obj-$(CONFIG_TEGRA20_MC) += tegra20-mc.o 9obj-$(CONFIG_TEGRA20_MC) += tegra20-mc.o
7obj-$(CONFIG_TEGRA30_MC) += tegra30-mc.o 10obj-$(CONFIG_TEGRA30_MC) += tegra30-mc.o
diff --git a/drivers/memory/emif.c b/drivers/memory/emif.c
index 33a4396b24c..06d31c99e6a 100644
--- a/drivers/memory/emif.c
+++ b/drivers/memory/emif.c
@@ -18,6 +18,7 @@
18#include <linux/platform_device.h> 18#include <linux/platform_device.h>
19#include <linux/interrupt.h> 19#include <linux/interrupt.h>
20#include <linux/slab.h> 20#include <linux/slab.h>
21#include <linux/of.h>
21#include <linux/debugfs.h> 22#include <linux/debugfs.h>
22#include <linux/seq_file.h> 23#include <linux/seq_file.h>
23#include <linux/module.h> 24#include <linux/module.h>
@@ -25,6 +26,7 @@
25#include <linux/spinlock.h> 26#include <linux/spinlock.h>
26#include <memory/jedec_ddr.h> 27#include <memory/jedec_ddr.h>
27#include "emif.h" 28#include "emif.h"
29#include "of_memory.h"
28 30
29/** 31/**
30 * struct emif_data - Per device static data for driver's use 32 * struct emif_data - Per device static data for driver's use
@@ -49,6 +51,7 @@
49 * frequency in effect at the moment) 51 * frequency in effect at the moment)
50 * @plat_data: Pointer to saved platform data. 52 * @plat_data: Pointer to saved platform data.
51 * @debugfs_root: dentry to the root folder for EMIF in debugfs 53 * @debugfs_root: dentry to the root folder for EMIF in debugfs
54 * @np_ddr: Pointer to ddr device tree node
52 */ 55 */
53struct emif_data { 56struct emif_data {
54 u8 duplicate; 57 u8 duplicate;
@@ -63,6 +66,7 @@ struct emif_data {
63 struct emif_regs *curr_regs; 66 struct emif_regs *curr_regs;
64 struct emif_platform_data *plat_data; 67 struct emif_platform_data *plat_data;
65 struct dentry *debugfs_root; 68 struct dentry *debugfs_root;
69 struct device_node *np_ddr;
66}; 70};
67 71
68static struct emif_data *emif1; 72static struct emif_data *emif1;
@@ -71,6 +75,7 @@ static unsigned long irq_state;
71static u32 t_ck; /* DDR clock period in ps */ 75static u32 t_ck; /* DDR clock period in ps */
72static LIST_HEAD(device_list); 76static LIST_HEAD(device_list);
73 77
78#ifdef CONFIG_DEBUG_FS
74static void do_emif_regdump_show(struct seq_file *s, struct emif_data *emif, 79static void do_emif_regdump_show(struct seq_file *s, struct emif_data *emif,
75 struct emif_regs *regs) 80 struct emif_regs *regs)
76{ 81{
@@ -162,23 +167,23 @@ static int __init_or_module emif_debugfs_init(struct emif_data *emif)
162 int ret; 167 int ret;
163 168
164 dentry = debugfs_create_dir(dev_name(emif->dev), NULL); 169 dentry = debugfs_create_dir(dev_name(emif->dev), NULL);
165 if (IS_ERR(dentry)) { 170 if (!dentry) {
166 ret = PTR_ERR(dentry); 171 ret = -ENOMEM;
167 goto err0; 172 goto err0;
168 } 173 }
169 emif->debugfs_root = dentry; 174 emif->debugfs_root = dentry;
170 175
171 dentry = debugfs_create_file("regcache_dump", S_IRUGO, 176 dentry = debugfs_create_file("regcache_dump", S_IRUGO,
172 emif->debugfs_root, emif, &emif_regdump_fops); 177 emif->debugfs_root, emif, &emif_regdump_fops);
173 if (IS_ERR(dentry)) { 178 if (!dentry) {
174 ret = PTR_ERR(dentry); 179 ret = -ENOMEM;
175 goto err1; 180 goto err1;
176 } 181 }
177 182
178 dentry = debugfs_create_file("mr4", S_IRUGO, 183 dentry = debugfs_create_file("mr4", S_IRUGO,
179 emif->debugfs_root, emif, &emif_mr4_fops); 184 emif->debugfs_root, emif, &emif_mr4_fops);
180 if (IS_ERR(dentry)) { 185 if (!dentry) {
181 ret = PTR_ERR(dentry); 186 ret = -ENOMEM;
182 goto err1; 187 goto err1;
183 } 188 }
184 189
@@ -194,6 +199,16 @@ static void __exit emif_debugfs_exit(struct emif_data *emif)
194 debugfs_remove_recursive(emif->debugfs_root); 199 debugfs_remove_recursive(emif->debugfs_root);
195 emif->debugfs_root = NULL; 200 emif->debugfs_root = NULL;
196} 201}
202#else
203static inline int __init_or_module emif_debugfs_init(struct emif_data *emif)
204{
205 return 0;
206}
207
208static inline void __exit emif_debugfs_exit(struct emif_data *emif)
209{
210}
211#endif
197 212
198/* 213/*
199 * Calculate the period of DDR clock from frequency value 214 * Calculate the period of DDR clock from frequency value
@@ -1148,6 +1163,168 @@ static int is_custom_config_valid(struct emif_custom_configs *cust_cfgs,
1148 return valid; 1163 return valid;
1149} 1164}
1150 1165
1166#if defined(CONFIG_OF)
1167static void __init_or_module of_get_custom_configs(struct device_node *np_emif,
1168 struct emif_data *emif)
1169{
1170 struct emif_custom_configs *cust_cfgs = NULL;
1171 int len;
1172 const int *lpmode, *poll_intvl;
1173
1174 lpmode = of_get_property(np_emif, "low-power-mode", &len);
1175 poll_intvl = of_get_property(np_emif, "temp-alert-poll-interval", &len);
1176
1177 if (lpmode || poll_intvl)
1178 cust_cfgs = devm_kzalloc(emif->dev, sizeof(*cust_cfgs),
1179 GFP_KERNEL);
1180
1181 if (!cust_cfgs)
1182 return;
1183
1184 if (lpmode) {
1185 cust_cfgs->mask |= EMIF_CUSTOM_CONFIG_LPMODE;
1186 cust_cfgs->lpmode = *lpmode;
1187 of_property_read_u32(np_emif,
1188 "low-power-mode-timeout-performance",
1189 &cust_cfgs->lpmode_timeout_performance);
1190 of_property_read_u32(np_emif,
1191 "low-power-mode-timeout-power",
1192 &cust_cfgs->lpmode_timeout_power);
1193 of_property_read_u32(np_emif,
1194 "low-power-mode-freq-threshold",
1195 &cust_cfgs->lpmode_freq_threshold);
1196 }
1197
1198 if (poll_intvl) {
1199 cust_cfgs->mask |=
1200 EMIF_CUSTOM_CONFIG_TEMP_ALERT_POLL_INTERVAL;
1201 cust_cfgs->temp_alert_poll_interval_ms = *poll_intvl;
1202 }
1203
1204 if (!is_custom_config_valid(cust_cfgs, emif->dev)) {
1205 devm_kfree(emif->dev, cust_cfgs);
1206 return;
1207 }
1208
1209 emif->plat_data->custom_configs = cust_cfgs;
1210}
1211
1212static void __init_or_module of_get_ddr_info(struct device_node *np_emif,
1213 struct device_node *np_ddr,
1214 struct ddr_device_info *dev_info)
1215{
1216 u32 density = 0, io_width = 0;
1217 int len;
1218
1219 if (of_find_property(np_emif, "cs1-used", &len))
1220 dev_info->cs1_used = true;
1221
1222 if (of_find_property(np_emif, "cal-resistor-per-cs", &len))
1223 dev_info->cal_resistors_per_cs = true;
1224
1225 if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s4"))
1226 dev_info->type = DDR_TYPE_LPDDR2_S4;
1227 else if (of_device_is_compatible(np_ddr , "jedec,lpddr2-s2"))
1228 dev_info->type = DDR_TYPE_LPDDR2_S2;
1229
1230 of_property_read_u32(np_ddr, "density", &density);
1231 of_property_read_u32(np_ddr, "io-width", &io_width);
1232
1233 /* Convert from density in Mb to the density encoding in jedc_ddr.h */
1234 if (density & (density - 1))
1235 dev_info->density = 0;
1236 else
1237 dev_info->density = __fls(density) - 5;
1238
1239 /* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
1240 if (io_width & (io_width - 1))
1241 dev_info->io_width = 0;
1242 else
1243 dev_info->io_width = __fls(io_width) - 1;
1244}
1245
1246static struct emif_data * __init_or_module of_get_memory_device_details(
1247 struct device_node *np_emif, struct device *dev)
1248{
1249 struct emif_data *emif = NULL;
1250 struct ddr_device_info *dev_info = NULL;
1251 struct emif_platform_data *pd = NULL;
1252 struct device_node *np_ddr;
1253 int len;
1254
1255 np_ddr = of_parse_phandle(np_emif, "device-handle", 0);
1256 if (!np_ddr)
1257 goto error;
1258 emif = devm_kzalloc(dev, sizeof(struct emif_data), GFP_KERNEL);
1259 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
1260 dev_info = devm_kzalloc(dev, sizeof(*dev_info), GFP_KERNEL);
1261
1262 if (!emif || !pd || !dev_info) {
1263 dev_err(dev, "%s: Out of memory!!\n",
1264 __func__);
1265 goto error;
1266 }
1267
1268 emif->plat_data = pd;
1269 pd->device_info = dev_info;
1270 emif->dev = dev;
1271 emif->np_ddr = np_ddr;
1272 emif->temperature_level = SDRAM_TEMP_NOMINAL;
1273
1274 if (of_device_is_compatible(np_emif, "ti,emif-4d"))
1275 emif->plat_data->ip_rev = EMIF_4D;
1276 else if (of_device_is_compatible(np_emif, "ti,emif-4d5"))
1277 emif->plat_data->ip_rev = EMIF_4D5;
1278
1279 of_property_read_u32(np_emif, "phy-type", &pd->phy_type);
1280
1281 if (of_find_property(np_emif, "hw-caps-ll-interface", &len))
1282 pd->hw_caps |= EMIF_HW_CAPS_LL_INTERFACE;
1283
1284 of_get_ddr_info(np_emif, np_ddr, dev_info);
1285 if (!is_dev_data_valid(pd->device_info->type, pd->device_info->density,
1286 pd->device_info->io_width, pd->phy_type, pd->ip_rev,
1287 emif->dev)) {
1288 dev_err(dev, "%s: invalid device data!!\n", __func__);
1289 goto error;
1290 }
1291 /*
1292 * For EMIF instances other than EMIF1 see if the devices connected
1293 * are exactly same as on EMIF1(which is typically the case). If so,
1294 * mark it as a duplicate of EMIF1. This will save some memory and
1295 * computation.
1296 */
1297 if (emif1 && emif1->np_ddr == np_ddr) {
1298 emif->duplicate = true;
1299 goto out;
1300 } else if (emif1) {
1301 dev_warn(emif->dev, "%s: Non-symmetric DDR geometry\n",
1302 __func__);
1303 }
1304
1305 of_get_custom_configs(np_emif, emif);
1306 emif->plat_data->timings = of_get_ddr_timings(np_ddr, emif->dev,
1307 emif->plat_data->device_info->type,
1308 &emif->plat_data->timings_arr_size);
1309
1310 emif->plat_data->min_tck = of_get_min_tck(np_ddr, emif->dev);
1311 goto out;
1312
1313error:
1314 return NULL;
1315out:
1316 return emif;
1317}
1318
1319#else
1320
1321static struct emif_data * __init_or_module of_get_memory_device_details(
1322 struct device_node *np_emif, struct device *dev)
1323{
1324 return NULL;
1325}
1326#endif
1327
1151static struct emif_data *__init_or_module get_device_details( 1328static struct emif_data *__init_or_module get_device_details(
1152 struct platform_device *pdev) 1329 struct platform_device *pdev)
1153{ 1330{
@@ -1267,7 +1444,11 @@ static int __init_or_module emif_probe(struct platform_device *pdev)
1267 struct resource *res; 1444 struct resource *res;
1268 int irq; 1445 int irq;
1269 1446
1270 emif = get_device_details(pdev); 1447 if (pdev->dev.of_node)
1448 emif = of_get_memory_device_details(pdev->dev.of_node, &pdev->dev);
1449 else
1450 emif = get_device_details(pdev);
1451
1271 if (!emif) { 1452 if (!emif) {
1272 pr_err("%s: error getting device data\n", __func__); 1453 pr_err("%s: error getting device data\n", __func__);
1273 goto error; 1454 goto error;
@@ -1644,11 +1825,21 @@ static void __attribute__((unused)) freq_post_notify_handling(void)
1644 spin_unlock_irqrestore(&emif_lock, irq_state); 1825 spin_unlock_irqrestore(&emif_lock, irq_state);
1645} 1826}
1646 1827
1828#if defined(CONFIG_OF)
1829static const struct of_device_id emif_of_match[] = {
1830 { .compatible = "ti,emif-4d" },
1831 { .compatible = "ti,emif-4d5" },
1832 {},
1833};
1834MODULE_DEVICE_TABLE(of, emif_of_match);
1835#endif
1836
1647static struct platform_driver emif_driver = { 1837static struct platform_driver emif_driver = {
1648 .remove = __exit_p(emif_remove), 1838 .remove = __exit_p(emif_remove),
1649 .shutdown = emif_shutdown, 1839 .shutdown = emif_shutdown,
1650 .driver = { 1840 .driver = {
1651 .name = "emif", 1841 .name = "emif",
1842 .of_match_table = of_match_ptr(emif_of_match),
1652 }, 1843 },
1653}; 1844};
1654 1845
diff --git a/drivers/memory/of_memory.c b/drivers/memory/of_memory.c
new file mode 100644
index 00000000000..60074351f17
--- /dev/null
+++ b/drivers/memory/of_memory.c
@@ -0,0 +1,153 @@
1/*
2 * OpenFirmware helpers for memory drivers
3 *
4 * Copyright (C) 2012 Texas Instruments, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/device.h>
13#include <linux/platform_device.h>
14#include <linux/list.h>
15#include <linux/of.h>
16#include <linux/gfp.h>
17#include <memory/jedec_ddr.h>
18#include <linux/export.h>
19
20/**
21 * of_get_min_tck() - extract min timing values for ddr
22 * @np: pointer to ddr device tree node
23 * @device: device requesting for min timing values
24 *
25 * Populates the lpddr2_min_tck structure by extracting data
26 * from device tree node. Returns a pointer to the populated
27 * structure. If any error in populating the structure, returns
28 * default min timings provided by JEDEC.
29 */
30const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
31 struct device *dev)
32{
33 int ret = 0;
34 struct lpddr2_min_tck *min;
35
36 min = devm_kzalloc(dev, sizeof(*min), GFP_KERNEL);
37 if (!min)
38 goto default_min_tck;
39
40 ret |= of_property_read_u32(np, "tRPab-min-tck", &min->tRPab);
41 ret |= of_property_read_u32(np, "tRCD-min-tck", &min->tRCD);
42 ret |= of_property_read_u32(np, "tWR-min-tck", &min->tWR);
43 ret |= of_property_read_u32(np, "tRASmin-min-tck", &min->tRASmin);
44 ret |= of_property_read_u32(np, "tRRD-min-tck", &min->tRRD);
45 ret |= of_property_read_u32(np, "tWTR-min-tck", &min->tWTR);
46 ret |= of_property_read_u32(np, "tXP-min-tck", &min->tXP);
47 ret |= of_property_read_u32(np, "tRTP-min-tck", &min->tRTP);
48 ret |= of_property_read_u32(np, "tCKE-min-tck", &min->tCKE);
49 ret |= of_property_read_u32(np, "tCKESR-min-tck", &min->tCKESR);
50 ret |= of_property_read_u32(np, "tFAW-min-tck", &min->tFAW);
51
52 if (ret) {
53 devm_kfree(dev, min);
54 goto default_min_tck;
55 }
56
57 return min;
58
59default_min_tck:
60 dev_warn(dev, "%s: using default min-tck values\n", __func__);
61 return &lpddr2_jedec_min_tck;
62}
63EXPORT_SYMBOL(of_get_min_tck);
64
65static int of_do_get_timings(struct device_node *np,
66 struct lpddr2_timings *tim)
67{
68 int ret;
69
70 ret = of_property_read_u32(np, "max-freq", &tim->max_freq);
71 ret |= of_property_read_u32(np, "min-freq", &tim->min_freq);
72 ret |= of_property_read_u32(np, "tRPab", &tim->tRPab);
73 ret |= of_property_read_u32(np, "tRCD", &tim->tRCD);
74 ret |= of_property_read_u32(np, "tWR", &tim->tWR);
75 ret |= of_property_read_u32(np, "tRAS-min", &tim->tRAS_min);
76 ret |= of_property_read_u32(np, "tRRD", &tim->tRRD);
77 ret |= of_property_read_u32(np, "tWTR", &tim->tWTR);
78 ret |= of_property_read_u32(np, "tXP", &tim->tXP);
79 ret |= of_property_read_u32(np, "tRTP", &tim->tRTP);
80 ret |= of_property_read_u32(np, "tCKESR", &tim->tCKESR);
81 ret |= of_property_read_u32(np, "tDQSCK-max", &tim->tDQSCK_max);
82 ret |= of_property_read_u32(np, "tFAW", &tim->tFAW);
83 ret |= of_property_read_u32(np, "tZQCS", &tim->tZQCS);
84 ret |= of_property_read_u32(np, "tZQCL", &tim->tZQCL);
85 ret |= of_property_read_u32(np, "tZQinit", &tim->tZQinit);
86 ret |= of_property_read_u32(np, "tRAS-max-ns", &tim->tRAS_max_ns);
87 ret |= of_property_read_u32(np, "tDQSCK-max-derated",
88 &tim->tDQSCK_max_derated);
89
90 return ret;
91}
92
93/**
94 * of_get_ddr_timings() - extracts the ddr timings and updates no of
95 * frequencies available.
96 * @np_ddr: Pointer to ddr device tree node
97 * @dev: Device requesting for ddr timings
98 * @device_type: Type of ddr(LPDDR2 S2/S4)
99 * @nr_frequencies: No of frequencies available for ddr
100 * (updated by this function)
101 *
102 * Populates lpddr2_timings structure by extracting data from device
103 * tree node. Returns pointer to populated structure. If any error
104 * while populating, returns default timings provided by JEDEC.
105 */
106const struct lpddr2_timings *of_get_ddr_timings(struct device_node *np_ddr,
107 struct device *dev, u32 device_type, u32 *nr_frequencies)
108{
109 struct lpddr2_timings *timings = NULL;
110 u32 arr_sz = 0, i = 0;
111 struct device_node *np_tim;
112 char *tim_compat;
113
114 switch (device_type) {
115 case DDR_TYPE_LPDDR2_S2:
116 case DDR_TYPE_LPDDR2_S4:
117 tim_compat = "jedec,lpddr2-timings";
118 break;
119 default:
120 dev_warn(dev, "%s: un-supported memory type\n", __func__);
121 }
122
123 for_each_child_of_node(np_ddr, np_tim)
124 if (of_device_is_compatible(np_tim, tim_compat))
125 arr_sz++;
126
127 if (arr_sz)
128 timings = devm_kzalloc(dev, sizeof(*timings) * arr_sz,
129 GFP_KERNEL);
130
131 if (!timings)
132 goto default_timings;
133
134 for_each_child_of_node(np_ddr, np_tim) {
135 if (of_device_is_compatible(np_tim, tim_compat)) {
136 if (of_do_get_timings(np_tim, &timings[i])) {
137 devm_kfree(dev, timings);
138 goto default_timings;
139 }
140 i++;
141 }
142 }
143
144 *nr_frequencies = arr_sz;
145
146 return timings;
147
148default_timings:
149 dev_warn(dev, "%s: using default timings\n", __func__);
150 *nr_frequencies = ARRAY_SIZE(lpddr2_jedec_timings);
151 return lpddr2_jedec_timings;
152}
153EXPORT_SYMBOL(of_get_ddr_timings);
diff --git a/drivers/memory/of_memory.h b/drivers/memory/of_memory.h
new file mode 100644
index 00000000000..ef2514f553d
--- /dev/null
+++ b/drivers/memory/of_memory.h
@@ -0,0 +1,36 @@
1/*
2 * OpenFirmware helpers for memory drivers
3 *
4 * Copyright (C) 2012 Texas Instruments, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#ifndef __LINUX_MEMORY_OF_REG_H
13#define __LINUX_MEMORY_OF_REG_H
14
15#if defined(CONFIG_OF) && defined(CONFIG_DDR)
16extern const struct lpddr2_min_tck *of_get_min_tck(struct device_node *np,
17 struct device *dev);
18extern const struct lpddr2_timings
19 *of_get_ddr_timings(struct device_node *np_ddr, struct device *dev,
20 u32 device_type, u32 *nr_frequencies);
21#else
22static inline const struct lpddr2_min_tck
23 *of_get_min_tck(struct device_node *np, struct device *dev)
24{
25 return NULL;
26}
27
28static inline const struct lpddr2_timings
29 *of_get_ddr_timings(struct device_node *np_ddr, struct device *dev,
30 u32 device_type, u32 *nr_frequencies)
31{
32 return NULL;
33}
34#endif /* CONFIG_OF && CONFIG_DDR */
35
36#endif /* __LINUX_MEMORY_OF_REG_ */
diff --git a/drivers/memory/tegra20-mc.c b/drivers/memory/tegra20-mc.c
index 3ed49c1c2b9..e6764bb41cb 100644
--- a/drivers/memory/tegra20-mc.c
+++ b/drivers/memory/tegra20-mc.c
@@ -57,7 +57,7 @@ static inline u32 mc_readl(struct tegra20_mc *mc, u32 offs)
57 57
58 if (offs < 0x24) 58 if (offs < 0x24)
59 val = readl(mc->regs[0] + offs); 59 val = readl(mc->regs[0] + offs);
60 if (offs < 0x400) 60 else if (offs < 0x400)
61 val = readl(mc->regs[1] + offs - 0x3c); 61 val = readl(mc->regs[1] + offs - 0x3c);
62 62
63 return val; 63 return val;
@@ -65,14 +65,10 @@ static inline u32 mc_readl(struct tegra20_mc *mc, u32 offs)
65 65
66static inline void mc_writel(struct tegra20_mc *mc, u32 val, u32 offs) 66static inline void mc_writel(struct tegra20_mc *mc, u32 val, u32 offs)
67{ 67{
68 if (offs < 0x24) { 68 if (offs < 0x24)
69 writel(val, mc->regs[0] + offs); 69 writel(val, mc->regs[0] + offs);
70 return; 70 else if (offs < 0x400)
71 }
72 if (offs < 0x400) {
73 writel(val, mc->regs[1] + offs - 0x3c); 71 writel(val, mc->regs[1] + offs - 0x3c);
74 return;
75 }
76} 72}
77 73
78static const char * const tegra20_mc_client[] = { 74static const char * const tegra20_mc_client[] = {
diff --git a/drivers/memory/tegra30-mc.c b/drivers/memory/tegra30-mc.c
index e56ff04eb5c..802b9ea431f 100644
--- a/drivers/memory/tegra30-mc.c
+++ b/drivers/memory/tegra30-mc.c
@@ -95,11 +95,11 @@ static inline u32 mc_readl(struct tegra30_mc *mc, u32 offs)
95 95
96 if (offs < 0x10) 96 if (offs < 0x10)
97 val = readl(mc->regs[0] + offs); 97 val = readl(mc->regs[0] + offs);
98 if (offs < 0x1f0) 98 else if (offs < 0x1f0)
99 val = readl(mc->regs[1] + offs - 0x3c); 99 val = readl(mc->regs[1] + offs - 0x3c);
100 if (offs < 0x228) 100 else if (offs < 0x228)
101 val = readl(mc->regs[2] + offs - 0x200); 101 val = readl(mc->regs[2] + offs - 0x200);
102 if (offs < 0x400) 102 else if (offs < 0x400)
103 val = readl(mc->regs[3] + offs - 0x284); 103 val = readl(mc->regs[3] + offs - 0x284);
104 104
105 return val; 105 return val;
@@ -107,22 +107,14 @@ static inline u32 mc_readl(struct tegra30_mc *mc, u32 offs)
107 107
108static inline void mc_writel(struct tegra30_mc *mc, u32 val, u32 offs) 108static inline void mc_writel(struct tegra30_mc *mc, u32 val, u32 offs)
109{ 109{
110 if (offs < 0x10) { 110 if (offs < 0x10)
111 writel(val, mc->regs[0] + offs); 111 writel(val, mc->regs[0] + offs);
112 return; 112 else if (offs < 0x1f0)
113 }
114 if (offs < 0x1f0) {
115 writel(val, mc->regs[1] + offs - 0x3c); 113 writel(val, mc->regs[1] + offs - 0x3c);
116 return; 114 else if (offs < 0x228)
117 }
118 if (offs < 0x228) {
119 writel(val, mc->regs[2] + offs - 0x200); 115 writel(val, mc->regs[2] + offs - 0x200);
120 return; 116 else if (offs < 0x400)
121 }
122 if (offs < 0x400) {
123 writel(val, mc->regs[3] + offs - 0x284); 117 writel(val, mc->regs[3] + offs - 0x284);
124 return;
125 }
126} 118}
127 119
128static const char * const tegra30_mc_client[] = { 120static const char * const tegra30_mc_client[] = {
diff --git a/drivers/vme/bridges/vme_ca91cx42.c b/drivers/vme/bridges/vme_ca91cx42.c
index e0df92ec44b..1425d22cf95 100644
--- a/drivers/vme/bridges/vme_ca91cx42.c
+++ b/drivers/vme/bridges/vme_ca91cx42.c
@@ -1603,7 +1603,7 @@ static int ca91cx42_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1603{ 1603{
1604 int retval, i; 1604 int retval, i;
1605 u32 data; 1605 u32 data;
1606 struct list_head *pos = NULL; 1606 struct list_head *pos = NULL, *n;
1607 struct vme_bridge *ca91cx42_bridge; 1607 struct vme_bridge *ca91cx42_bridge;
1608 struct ca91cx42_driver *ca91cx42_device; 1608 struct ca91cx42_driver *ca91cx42_device;
1609 struct vme_master_resource *master_image; 1609 struct vme_master_resource *master_image;
@@ -1821,28 +1821,28 @@ err_reg:
1821 ca91cx42_crcsr_exit(ca91cx42_bridge, pdev); 1821 ca91cx42_crcsr_exit(ca91cx42_bridge, pdev);
1822err_lm: 1822err_lm:
1823 /* resources are stored in link list */ 1823 /* resources are stored in link list */
1824 list_for_each(pos, &ca91cx42_bridge->lm_resources) { 1824 list_for_each_safe(pos, n, &ca91cx42_bridge->lm_resources) {
1825 lm = list_entry(pos, struct vme_lm_resource, list); 1825 lm = list_entry(pos, struct vme_lm_resource, list);
1826 list_del(pos); 1826 list_del(pos);
1827 kfree(lm); 1827 kfree(lm);
1828 } 1828 }
1829err_dma: 1829err_dma:
1830 /* resources are stored in link list */ 1830 /* resources are stored in link list */
1831 list_for_each(pos, &ca91cx42_bridge->dma_resources) { 1831 list_for_each_safe(pos, n, &ca91cx42_bridge->dma_resources) {
1832 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list); 1832 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
1833 list_del(pos); 1833 list_del(pos);
1834 kfree(dma_ctrlr); 1834 kfree(dma_ctrlr);
1835 } 1835 }
1836err_slave: 1836err_slave:
1837 /* resources are stored in link list */ 1837 /* resources are stored in link list */
1838 list_for_each(pos, &ca91cx42_bridge->slave_resources) { 1838 list_for_each_safe(pos, n, &ca91cx42_bridge->slave_resources) {
1839 slave_image = list_entry(pos, struct vme_slave_resource, list); 1839 slave_image = list_entry(pos, struct vme_slave_resource, list);
1840 list_del(pos); 1840 list_del(pos);
1841 kfree(slave_image); 1841 kfree(slave_image);
1842 } 1842 }
1843err_master: 1843err_master:
1844 /* resources are stored in link list */ 1844 /* resources are stored in link list */
1845 list_for_each(pos, &ca91cx42_bridge->master_resources) { 1845 list_for_each_safe(pos, n, &ca91cx42_bridge->master_resources) {
1846 master_image = list_entry(pos, struct vme_master_resource, 1846 master_image = list_entry(pos, struct vme_master_resource,
1847 list); 1847 list);
1848 list_del(pos); 1848 list_del(pos);
@@ -1868,7 +1868,7 @@ err_struct:
1868 1868
1869static void ca91cx42_remove(struct pci_dev *pdev) 1869static void ca91cx42_remove(struct pci_dev *pdev)
1870{ 1870{
1871 struct list_head *pos = NULL; 1871 struct list_head *pos = NULL, *n;
1872 struct vme_master_resource *master_image; 1872 struct vme_master_resource *master_image;
1873 struct vme_slave_resource *slave_image; 1873 struct vme_slave_resource *slave_image;
1874 struct vme_dma_resource *dma_ctrlr; 1874 struct vme_dma_resource *dma_ctrlr;
@@ -1905,28 +1905,28 @@ static void ca91cx42_remove(struct pci_dev *pdev)
1905 ca91cx42_crcsr_exit(ca91cx42_bridge, pdev); 1905 ca91cx42_crcsr_exit(ca91cx42_bridge, pdev);
1906 1906
1907 /* resources are stored in link list */ 1907 /* resources are stored in link list */
1908 list_for_each(pos, &ca91cx42_bridge->lm_resources) { 1908 list_for_each_safe(pos, n, &ca91cx42_bridge->lm_resources) {
1909 lm = list_entry(pos, struct vme_lm_resource, list); 1909 lm = list_entry(pos, struct vme_lm_resource, list);
1910 list_del(pos); 1910 list_del(pos);
1911 kfree(lm); 1911 kfree(lm);
1912 } 1912 }
1913 1913
1914 /* resources are stored in link list */ 1914 /* resources are stored in link list */
1915 list_for_each(pos, &ca91cx42_bridge->dma_resources) { 1915 list_for_each_safe(pos, n, &ca91cx42_bridge->dma_resources) {
1916 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list); 1916 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
1917 list_del(pos); 1917 list_del(pos);
1918 kfree(dma_ctrlr); 1918 kfree(dma_ctrlr);
1919 } 1919 }
1920 1920
1921 /* resources are stored in link list */ 1921 /* resources are stored in link list */
1922 list_for_each(pos, &ca91cx42_bridge->slave_resources) { 1922 list_for_each_safe(pos, n, &ca91cx42_bridge->slave_resources) {
1923 slave_image = list_entry(pos, struct vme_slave_resource, list); 1923 slave_image = list_entry(pos, struct vme_slave_resource, list);
1924 list_del(pos); 1924 list_del(pos);
1925 kfree(slave_image); 1925 kfree(slave_image);
1926 } 1926 }
1927 1927
1928 /* resources are stored in link list */ 1928 /* resources are stored in link list */
1929 list_for_each(pos, &ca91cx42_bridge->master_resources) { 1929 list_for_each_safe(pos, n, &ca91cx42_bridge->master_resources) {
1930 master_image = list_entry(pos, struct vme_master_resource, 1930 master_image = list_entry(pos, struct vme_master_resource,
1931 list); 1931 list);
1932 list_del(pos); 1932 list_del(pos);
diff --git a/drivers/vme/bridges/vme_tsi148.c b/drivers/vme/bridges/vme_tsi148.c
index 880d9242e34..5fbd08ffb9c 100644
--- a/drivers/vme/bridges/vme_tsi148.c
+++ b/drivers/vme/bridges/vme_tsi148.c
@@ -2350,7 +2350,7 @@ static int tsi148_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2350{ 2350{
2351 int retval, i, master_num; 2351 int retval, i, master_num;
2352 u32 data; 2352 u32 data;
2353 struct list_head *pos = NULL; 2353 struct list_head *pos = NULL, *n;
2354 struct vme_bridge *tsi148_bridge; 2354 struct vme_bridge *tsi148_bridge;
2355 struct tsi148_driver *tsi148_device; 2355 struct tsi148_driver *tsi148_device;
2356 struct vme_master_resource *master_image; 2356 struct vme_master_resource *master_image;
@@ -2615,28 +2615,28 @@ err_reg:
2615err_crcsr: 2615err_crcsr:
2616err_lm: 2616err_lm:
2617 /* resources are stored in link list */ 2617 /* resources are stored in link list */
2618 list_for_each(pos, &tsi148_bridge->lm_resources) { 2618 list_for_each_safe(pos, n, &tsi148_bridge->lm_resources) {
2619 lm = list_entry(pos, struct vme_lm_resource, list); 2619 lm = list_entry(pos, struct vme_lm_resource, list);
2620 list_del(pos); 2620 list_del(pos);
2621 kfree(lm); 2621 kfree(lm);
2622 } 2622 }
2623err_dma: 2623err_dma:
2624 /* resources are stored in link list */ 2624 /* resources are stored in link list */
2625 list_for_each(pos, &tsi148_bridge->dma_resources) { 2625 list_for_each_safe(pos, n, &tsi148_bridge->dma_resources) {
2626 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list); 2626 dma_ctrlr = list_entry(pos, struct vme_dma_resource, list);
2627 list_del(pos); 2627 list_del(pos);
2628 kfree(dma_ctrlr); 2628 kfree(dma_ctrlr);
2629 } 2629 }
2630err_slave: 2630err_slave:
2631 /* resources are stored in link list */ 2631 /* resources are stored in link list */
2632 list_for_each(pos, &tsi148_bridge->slave_resources) { 2632 list_for_each_safe(pos, n, &tsi148_bridge->slave_resources) {
2633 slave_image = list_entry(pos, struct vme_slave_resource, list); 2633 slave_image = list_entry(pos, struct vme_slave_resource, list);
2634 list_del(pos); 2634 list_del(pos);
2635 kfree(slave_image); 2635 kfree(slave_image);
2636 } 2636 }
2637err_master: 2637err_master:
2638 /* resources are stored in link list */ 2638 /* resources are stored in link list */
2639 list_for_each(pos, &tsi148_bridge->master_resources) { 2639 list_for_each_safe(pos, n, &tsi148_bridge->master_resources) {
2640 master_image = list_entry(pos, struct vme_master_resource, 2640 master_image = list_entry(pos, struct vme_master_resource,
2641 list); 2641 list);
2642 list_del(pos); 2642 list_del(pos);
diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
index 4733eab34a2..6393fd61d5c 100644
--- a/fs/debugfs/inode.c
+++ b/fs/debugfs/inode.c
@@ -28,7 +28,7 @@
28#include <linux/magic.h> 28#include <linux/magic.h>
29#include <linux/slab.h> 29#include <linux/slab.h>
30 30
31#define DEBUGFS_DEFAULT_MODE 0755 31#define DEBUGFS_DEFAULT_MODE 0700
32 32
33static struct vfsmount *debugfs_mount; 33static struct vfsmount *debugfs_mount;
34static int debugfs_mount_count; 34static int debugfs_mount_count;
@@ -291,9 +291,9 @@ static struct file_system_type debug_fs_type = {
291 .kill_sb = kill_litter_super, 291 .kill_sb = kill_litter_super,
292}; 292};
293 293
294struct dentry *__create_file(const char *name, umode_t mode, 294static struct dentry *__create_file(const char *name, umode_t mode,
295 struct dentry *parent, void *data, 295 struct dentry *parent, void *data,
296 const struct file_operations *fops) 296 const struct file_operations *fops)
297{ 297{
298 struct dentry *dentry = NULL; 298 struct dentry *dentry = NULL;
299 int error; 299 int error;
diff --git a/fs/sysfs/symlink.c b/fs/sysfs/symlink.c
index a7ac78f8e67..3c9eb5624f5 100644
--- a/fs/sysfs/symlink.c
+++ b/fs/sysfs/symlink.c
@@ -113,7 +113,7 @@ int sysfs_create_link(struct kobject *kobj, struct kobject *target,
113 * @target: object we're pointing to. 113 * @target: object we're pointing to.
114 * @name: name of the symlink. 114 * @name: name of the symlink.
115 * 115 *
116 * This function does the same as sysf_create_link(), but it 116 * This function does the same as sysfs_create_link(), but it
117 * doesn't warn if the link already exists. 117 * doesn't warn if the link already exists.
118 */ 118 */
119int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target, 119int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
diff --git a/include/linux/device.h b/include/linux/device.h
index 52a5f15a222..af92883bb4a 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -536,6 +536,10 @@ extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
536#else 536#else
537extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp); 537extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
538#endif 538#endif
539extern void devres_for_each_res(struct device *dev, dr_release_t release,
540 dr_match_t match, void *match_data,
541 void (*fn)(struct device *, void *, void *),
542 void *data);
539extern void devres_free(void *res); 543extern void devres_free(void *res);
540extern void devres_add(struct device *dev, void *res); 544extern void devres_add(struct device *dev, void *res);
541extern void *devres_find(struct device *dev, dr_release_t release, 545extern void *devres_find(struct device *dev, dr_release_t release,
@@ -891,12 +895,15 @@ extern const char *dev_driver_string(const struct device *dev);
891 895
892#ifdef CONFIG_PRINTK 896#ifdef CONFIG_PRINTK
893 897
894extern int __dev_printk(const char *level, const struct device *dev, 898extern __printf(3, 0)
895 struct va_format *vaf); 899int dev_vprintk_emit(int level, const struct device *dev,
900 const char *fmt, va_list args);
901extern __printf(3, 4)
902int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
903
896extern __printf(3, 4) 904extern __printf(3, 4)
897int dev_printk(const char *level, const struct device *dev, 905int dev_printk(const char *level, const struct device *dev,
898 const char *fmt, ...) 906 const char *fmt, ...);
899 ;
900extern __printf(2, 3) 907extern __printf(2, 3)
901int dev_emerg(const struct device *dev, const char *fmt, ...); 908int dev_emerg(const struct device *dev, const char *fmt, ...);
902extern __printf(2, 3) 909extern __printf(2, 3)
@@ -914,6 +921,14 @@ int _dev_info(const struct device *dev, const char *fmt, ...);
914 921
915#else 922#else
916 923
924static inline __printf(3, 0)
925int dev_vprintk_emit(int level, const struct device *dev,
926 const char *fmt, va_list args)
927{ return 0; }
928static inline __printf(3, 4)
929int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
930{ return 0; }
931
917static inline int __dev_printk(const char *level, const struct device *dev, 932static inline int __dev_printk(const char *level, const struct device *dev,
918 struct va_format *vaf) 933 struct va_format *vaf)
919{ return 0; } 934{ return 0; }
@@ -946,6 +961,32 @@ int _dev_info(const struct device *dev, const char *fmt, ...)
946 961
947#endif 962#endif
948 963
964/*
965 * Stupid hackaround for existing uses of non-printk uses dev_info
966 *
967 * Note that the definition of dev_info below is actually _dev_info
968 * and a macro is used to avoid redefining dev_info
969 */
970
971#define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
972
973#if defined(CONFIG_DYNAMIC_DEBUG)
974#define dev_dbg(dev, format, ...) \
975do { \
976 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
977} while (0)
978#elif defined(DEBUG)
979#define dev_dbg(dev, format, arg...) \
980 dev_printk(KERN_DEBUG, dev, format, ##arg)
981#else
982#define dev_dbg(dev, format, arg...) \
983({ \
984 if (0) \
985 dev_printk(KERN_DEBUG, dev, format, ##arg); \
986 0; \
987})
988#endif
989
949#define dev_level_ratelimited(dev_level, dev, fmt, ...) \ 990#define dev_level_ratelimited(dev_level, dev, fmt, ...) \
950do { \ 991do { \
951 static DEFINE_RATELIMIT_STATE(_rs, \ 992 static DEFINE_RATELIMIT_STATE(_rs, \
@@ -969,33 +1010,21 @@ do { \
969 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__) 1010 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
970#define dev_info_ratelimited(dev, fmt, ...) \ 1011#define dev_info_ratelimited(dev, fmt, ...) \
971 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__) 1012 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1013#if defined(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
972#define dev_dbg_ratelimited(dev, fmt, ...) \ 1014#define dev_dbg_ratelimited(dev, fmt, ...) \
973 dev_level_ratelimited(dev_dbg, dev, fmt, ##__VA_ARGS__) 1015do { \
974 1016 static DEFINE_RATELIMIT_STATE(_rs, \
975/* 1017 DEFAULT_RATELIMIT_INTERVAL, \
976 * Stupid hackaround for existing uses of non-printk uses dev_info 1018 DEFAULT_RATELIMIT_BURST); \
977 * 1019 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
978 * Note that the definition of dev_info below is actually _dev_info 1020 if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
979 * and a macro is used to avoid redefining dev_info 1021 __ratelimit(&_rs)) \
980 */ 1022 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
981 1023 ##__VA_ARGS__); \
982#define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
983
984#if defined(CONFIG_DYNAMIC_DEBUG)
985#define dev_dbg(dev, format, ...) \
986do { \
987 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
988} while (0) 1024} while (0)
989#elif defined(DEBUG)
990#define dev_dbg(dev, format, arg...) \
991 dev_printk(KERN_DEBUG, dev, format, ##arg)
992#else 1025#else
993#define dev_dbg(dev, format, arg...) \ 1026#define dev_dbg_ratelimited(dev, fmt, ...) \
994({ \ 1027 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
995 if (0) \
996 dev_printk(KERN_DEBUG, dev, format, ##arg); \
997 0; \
998})
999#endif 1028#endif
1000 1029
1001#ifdef VERBOSE_DEBUG 1030#ifdef VERBOSE_DEBUG
diff --git a/include/linux/extcon.h b/include/linux/extcon.h
index cdd40147765..7443a560c9d 100644
--- a/include/linux/extcon.h
+++ b/include/linux/extcon.h
@@ -30,19 +30,19 @@
30 30
31/* 31/*
32 * The standard cable name is to help support general notifier 32 * The standard cable name is to help support general notifier
33 * and notifee device drivers to share the common names. 33 * and notifiee device drivers to share the common names.
34 * Please use standard cable names unless your notifier device has 34 * Please use standard cable names unless your notifier device has
35 * a very unique and abnormal cable or 35 * a very unique and abnormal cable or
36 * the cable type is supposed to be used with only one unique 36 * the cable type is supposed to be used with only one unique
37 * pair of notifier/notifee devices. 37 * pair of notifier/notifiee devices.
38 * 38 *
39 * Please add any other "standard" cables used with extcon dev. 39 * Please add any other "standard" cables used with extcon dev.
40 * 40 *
41 * You may add a dot and number to specify version or specification 41 * You may add a dot and number to specify version or specification
42 * of the specific cable if it is required. (e.g., "Fast-charger.18" 42 * of the specific cable if it is required. (e.g., "Fast-charger.18"
43 * and "Fast-charger.10" for 1.8A and 1.0A chargers) 43 * and "Fast-charger.10" for 1.8A and 1.0A chargers)
44 * However, the notifee and notifier should be able to handle such 44 * However, the notifiee and notifier should be able to handle such
45 * string and if the notifee can negotiate the protocol or idenify, 45 * string and if the notifiee can negotiate the protocol or identify,
46 * you don't need such convention. This convention is helpful when 46 * you don't need such convention. This convention is helpful when
47 * notifier can distinguish but notifiee cannot. 47 * notifier can distinguish but notifiee cannot.
48 */ 48 */
@@ -76,7 +76,7 @@ struct extcon_cable;
76 * struct extcon_dev - An extcon device represents one external connector. 76 * struct extcon_dev - An extcon device represents one external connector.
77 * @name The name of this extcon device. Parent device name is used 77 * @name The name of this extcon device. Parent device name is used
78 * if NULL. 78 * if NULL.
79 * @supported_cable Array of supported cable name ending with NULL. 79 * @supported_cable Array of supported cable names ending with NULL.
80 * If supported_cable is NULL, cable name related APIs 80 * If supported_cable is NULL, cable name related APIs
81 * are disabled. 81 * are disabled.
82 * @mutually_exclusive Array of mutually exclusive set of cables that cannot 82 * @mutually_exclusive Array of mutually exclusive set of cables that cannot
@@ -95,7 +95,7 @@ struct extcon_cable;
95 * @state Attach/detach state of this extcon. Do not provide at 95 * @state Attach/detach state of this extcon. Do not provide at
96 * register-time 96 * register-time
97 * @nh Notifier for the state change events from this extcon 97 * @nh Notifier for the state change events from this extcon
98 * @entry To support list of extcon devices so that uses can search 98 * @entry To support list of extcon devices so that users can search
99 * for extcon devices based on the extcon name. 99 * for extcon devices based on the extcon name.
100 * @lock 100 * @lock
101 * @max_supported Internal value to store the number of cables. 101 * @max_supported Internal value to store the number of cables.
@@ -199,7 +199,7 @@ extern int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state);
199/* 199/*
200 * get/set_cable_state access each bit of the 32b encoded state value. 200 * get/set_cable_state access each bit of the 32b encoded state value.
201 * They are used to access the status of each cable based on the cable_name 201 * They are used to access the status of each cable based on the cable_name
202 * or cable_index, which is retrived by extcon_find_cable_index 202 * or cable_index, which is retrieved by extcon_find_cable_index
203 */ 203 */
204extern int extcon_find_cable_index(struct extcon_dev *sdev, 204extern int extcon_find_cable_index(struct extcon_dev *sdev,
205 const char *cable_name); 205 const char *cable_name);
@@ -226,9 +226,9 @@ extern int extcon_unregister_interest(struct extcon_specific_cable_nb *nb);
226 226
227/* 227/*
228 * Following APIs are to monitor every action of a notifier. 228 * Following APIs are to monitor every action of a notifier.
229 * Registerer gets notified for every external port of a connection device. 229 * Registrar gets notified for every external port of a connection device.
230 * Probably this could be used to debug an action of notifier; however, 230 * Probably this could be used to debug an action of notifier; however,
231 * we do not recommend to use this at normal 'notifiee' device drivers who 231 * we do not recommend to use this for normal 'notifiee' device drivers who
232 * want to be notified by a specific external port of the notifier. 232 * want to be notified by a specific external port of the notifier.
233 */ 233 */
234extern int extcon_register_notifier(struct extcon_dev *edev, 234extern int extcon_register_notifier(struct extcon_dev *edev,
diff --git a/include/linux/extcon/extcon-adc-jack.h b/include/linux/extcon/extcon-adc-jack.h
new file mode 100644
index 00000000000..20e9eef25d4
--- /dev/null
+++ b/include/linux/extcon/extcon-adc-jack.h
@@ -0,0 +1,71 @@
1/*
2 * include/linux/extcon/extcon-adc-jack.h
3 *
4 * Analog Jack extcon driver with ADC-based detection capability.
5 *
6 * Copyright (C) 2012 Samsung Electronics
7 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#ifndef _EXTCON_ADC_JACK_H_
16#define _EXTCON_ADC_JACK_H_ __FILE__
17
18#include <linux/module.h>
19#include <linux/extcon.h>
20
21/**
22 * struct adc_jack_cond - condition to use an extcon state
23 * @state - the corresponding extcon state (if 0, this struct denotes
24 * the last adc_jack_cond element among the array)
25 * @min_adc - min adc value for this condition
26 * @max_adc - max adc value for this condition
27 *
28 * For example, if { .state = 0x3, .min_adc = 100, .max_adc = 200}, it means
29 * that if ADC value is between (inclusive) 100 and 200, than the cable 0 and
30 * 1 are attached (1<<0 | 1<<1 == 0x3)
31 *
32 * Note that you don't need to describe condition for "no cable attached"
33 * because when no adc_jack_cond is met, state = 0 is automatically chosen.
34 */
35struct adc_jack_cond {
36 u32 state; /* extcon state value. 0 if invalid */
37 u32 min_adc;
38 u32 max_adc;
39};
40
41/**
42 * struct adc_jack_pdata - platform data for adc jack device.
43 * @name - name of the extcon device. If null, "adc-jack" is used.
44 * @consumer_channel - Unique name to identify the channel on the consumer
45 * side. This typically describes the channels used within
46 * the consumer. E.g. 'battery_voltage'
47 * @cable_names - array of cable names ending with null.
48 * @adc_contitions - array of struct adc_jack_cond conditions ending
49 * with .state = 0 entry. This describes how to decode
50 * adc values into extcon state.
51 * @irq_flags - irq flags used for the @irq
52 * @handling_delay_ms - in some devices, we need to read ADC value some
53 * milli-seconds after the interrupt occurs. You may
54 * describe such delays with @handling_delay_ms, which
55 * is rounded-off by jiffies.
56 */
57struct adc_jack_pdata {
58 const char *name;
59 const char *consumer_channel;
60 /*
61 * The last entry should be NULL
62 */
63 const char **cable_names;
64 /* The last entry's state should be 0 */
65 struct adc_jack_cond *adc_conditions;
66
67 unsigned long irq_flags;
68 unsigned long handling_delay_ms; /* in ms */
69};
70
71#endif /* _EXTCON_ADC_JACK_H */
diff --git a/include/linux/firmware.h b/include/linux/firmware.h
index 1e7c01189fa..e4279fedb93 100644
--- a/include/linux/firmware.h
+++ b/include/linux/firmware.h
@@ -12,6 +12,9 @@ struct firmware {
12 size_t size; 12 size_t size;
13 const u8 *data; 13 const u8 *data;
14 struct page **pages; 14 struct page **pages;
15
16 /* firmware loader private fields */
17 void *priv;
15}; 18};
16 19
17struct module; 20struct module;
@@ -44,6 +47,8 @@ int request_firmware_nowait(
44 void (*cont)(const struct firmware *fw, void *context)); 47 void (*cont)(const struct firmware *fw, void *context));
45 48
46void release_firmware(const struct firmware *fw); 49void release_firmware(const struct firmware *fw);
50int cache_firmware(const char *name);
51int uncache_firmware(const char *name);
47#else 52#else
48static inline int request_firmware(const struct firmware **fw, 53static inline int request_firmware(const struct firmware **fw,
49 const char *name, 54 const char *name,
@@ -62,6 +67,16 @@ static inline int request_firmware_nowait(
62static inline void release_firmware(const struct firmware *fw) 67static inline void release_firmware(const struct firmware *fw)
63{ 68{
64} 69}
70
71static inline int cache_firmware(const char *name)
72{
73 return -ENOENT;
74}
75
76static inline int uncache_firmware(const char *name)
77{
78 return -EINVAL;
79}
65#endif 80#endif
66 81
67#endif 82#endif
diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
index 68ed7f7e1fc..e73b852156b 100644
--- a/include/linux/hyperv.h
+++ b/include/linux/hyperv.h
@@ -122,12 +122,53 @@
122#define REG_U32 4 122#define REG_U32 4
123#define REG_U64 8 123#define REG_U64 8
124 124
125/*
126 * As we look at expanding the KVP functionality to include
127 * IP injection functionality, we need to maintain binary
128 * compatibility with older daemons.
129 *
130 * The KVP opcodes are defined by the host and it was unfortunate
131 * that I chose to treat the registration operation as part of the
132 * KVP operations defined by the host.
133 * Here is the level of compatibility
134 * (between the user level daemon and the kernel KVP driver) that we
135 * will implement:
136 *
137 * An older daemon will always be supported on a newer driver.
138 * A given user level daemon will require a minimal version of the
139 * kernel driver.
140 * If we cannot handle the version differences, we will fail gracefully
141 * (this can happen when we have a user level daemon that is more
142 * advanced than the KVP driver.
143 *
144 * We will use values used in this handshake for determining if we have
145 * workable user level daemon and the kernel driver. We begin by taking the
146 * registration opcode out of the KVP opcode namespace. We will however,
147 * maintain compatibility with the existing user-level daemon code.
148 */
149
150/*
151 * Daemon code not supporting IP injection (legacy daemon).
152 */
153
154#define KVP_OP_REGISTER 4
155
156/*
157 * Daemon code supporting IP injection.
158 * The KVP opcode field is used to communicate the
159 * registration information; so define a namespace that
160 * will be distinct from the host defined KVP opcode.
161 */
162
163#define KVP_OP_REGISTER1 100
164
125enum hv_kvp_exchg_op { 165enum hv_kvp_exchg_op {
126 KVP_OP_GET = 0, 166 KVP_OP_GET = 0,
127 KVP_OP_SET, 167 KVP_OP_SET,
128 KVP_OP_DELETE, 168 KVP_OP_DELETE,
129 KVP_OP_ENUMERATE, 169 KVP_OP_ENUMERATE,
130 KVP_OP_REGISTER, 170 KVP_OP_GET_IP_INFO,
171 KVP_OP_SET_IP_INFO,
131 KVP_OP_COUNT /* Number of operations, must be last. */ 172 KVP_OP_COUNT /* Number of operations, must be last. */
132}; 173};
133 174
@@ -140,6 +181,39 @@ enum hv_kvp_exchg_pool {
140 KVP_POOL_COUNT /* Number of pools, must be last. */ 181 KVP_POOL_COUNT /* Number of pools, must be last. */
141}; 182};
142 183
184/*
185 * Some Hyper-V status codes.
186 */
187
188#define HV_S_OK 0x00000000
189#define HV_E_FAIL 0x80004005
190#define HV_S_CONT 0x80070103
191#define HV_ERROR_NOT_SUPPORTED 0x80070032
192#define HV_ERROR_MACHINE_LOCKED 0x800704F7
193#define HV_ERROR_DEVICE_NOT_CONNECTED 0x8007048F
194#define HV_INVALIDARG 0x80070057
195#define HV_GUID_NOTFOUND 0x80041002
196
197#define ADDR_FAMILY_NONE 0x00
198#define ADDR_FAMILY_IPV4 0x01
199#define ADDR_FAMILY_IPV6 0x02
200
201#define MAX_ADAPTER_ID_SIZE 128
202#define MAX_IP_ADDR_SIZE 1024
203#define MAX_GATEWAY_SIZE 512
204
205
206struct hv_kvp_ipaddr_value {
207 __u16 adapter_id[MAX_ADAPTER_ID_SIZE];
208 __u8 addr_family;
209 __u8 dhcp_enabled;
210 __u16 ip_addr[MAX_IP_ADDR_SIZE];
211 __u16 sub_net[MAX_IP_ADDR_SIZE];
212 __u16 gate_way[MAX_GATEWAY_SIZE];
213 __u16 dns_addr[MAX_IP_ADDR_SIZE];
214} __attribute__((packed));
215
216
143struct hv_kvp_hdr { 217struct hv_kvp_hdr {
144 __u8 operation; 218 __u8 operation;
145 __u8 pool; 219 __u8 pool;
@@ -181,16 +255,26 @@ struct hv_kvp_register {
181}; 255};
182 256
183struct hv_kvp_msg { 257struct hv_kvp_msg {
184 struct hv_kvp_hdr kvp_hdr; 258 union {
259 struct hv_kvp_hdr kvp_hdr;
260 int error;
261 };
185 union { 262 union {
186 struct hv_kvp_msg_get kvp_get; 263 struct hv_kvp_msg_get kvp_get;
187 struct hv_kvp_msg_set kvp_set; 264 struct hv_kvp_msg_set kvp_set;
188 struct hv_kvp_msg_delete kvp_delete; 265 struct hv_kvp_msg_delete kvp_delete;
189 struct hv_kvp_msg_enumerate kvp_enum_data; 266 struct hv_kvp_msg_enumerate kvp_enum_data;
267 struct hv_kvp_ipaddr_value kvp_ip_val;
190 struct hv_kvp_register kvp_register; 268 struct hv_kvp_register kvp_register;
191 } body; 269 } body;
192} __attribute__((packed)); 270} __attribute__((packed));
193 271
272struct hv_kvp_ip_msg {
273 __u8 operation;
274 __u8 pool;
275 struct hv_kvp_ipaddr_value kvp_ip_val;
276} __attribute__((packed));
277
194#ifdef __KERNEL__ 278#ifdef __KERNEL__
195#include <linux/scatterlist.h> 279#include <linux/scatterlist.h>
196#include <linux/list.h> 280#include <linux/list.h>
@@ -405,7 +489,7 @@ struct vmtransfer_page_range {
405struct vmtransfer_page_packet_header { 489struct vmtransfer_page_packet_header {
406 struct vmpacket_descriptor d; 490 struct vmpacket_descriptor d;
407 u16 xfer_pageset_id; 491 u16 xfer_pageset_id;
408 bool sender_owns_set; 492 u8 sender_owns_set;
409 u8 reserved; 493 u8 reserved;
410 u32 range_cnt; 494 u32 range_cnt;
411 struct vmtransfer_page_range ranges[1]; 495 struct vmtransfer_page_range ranges[1];
@@ -559,7 +643,7 @@ struct vmbus_channel_query_vmbus_version {
559/* VMBus Version Supported parameters */ 643/* VMBus Version Supported parameters */
560struct vmbus_channel_version_supported { 644struct vmbus_channel_version_supported {
561 struct vmbus_channel_message_header header; 645 struct vmbus_channel_message_header header;
562 bool version_supported; 646 u8 version_supported;
563} __packed; 647} __packed;
564 648
565/* Offer Channel parameters */ 649/* Offer Channel parameters */
@@ -568,7 +652,7 @@ struct vmbus_channel_offer_channel {
568 struct vmbus_channel_offer offer; 652 struct vmbus_channel_offer offer;
569 u32 child_relid; 653 u32 child_relid;
570 u8 monitorid; 654 u8 monitorid;
571 bool monitor_allocated; 655 u8 monitor_allocated;
572} __packed; 656} __packed;
573 657
574/* Rescind Offer parameters */ 658/* Rescind Offer parameters */
@@ -704,7 +788,7 @@ struct vmbus_channel_initiate_contact {
704 788
705struct vmbus_channel_version_response { 789struct vmbus_channel_version_response {
706 struct vmbus_channel_message_header header; 790 struct vmbus_channel_message_header header;
707 bool version_supported; 791 u8 version_supported;
708} __packed; 792} __packed;
709 793
710enum vmbus_channel_state { 794enum vmbus_channel_state {
@@ -977,11 +1061,6 @@ void vmbus_driver_unregister(struct hv_driver *hv_driver);
977#define ICMSGHDRFLAG_REQUEST 2 1061#define ICMSGHDRFLAG_REQUEST 2
978#define ICMSGHDRFLAG_RESPONSE 4 1062#define ICMSGHDRFLAG_RESPONSE 4
979 1063
980#define HV_S_OK 0x00000000
981#define HV_E_FAIL 0x80004005
982#define HV_S_CONT 0x80070103
983#define HV_ERROR_NOT_SUPPORTED 0x80070032
984#define HV_ERROR_MACHINE_LOCKED 0x800704F7
985 1064
986/* 1065/*
987 * While we want to handle util services as regular devices, 1066 * While we want to handle util services as regular devices,
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 59dc05f3824..5f49cc0a107 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2720,9 +2720,6 @@ static inline const char *netdev_name(const struct net_device *dev)
2720 return dev->name; 2720 return dev->name;
2721} 2721}
2722 2722
2723extern int __netdev_printk(const char *level, const struct net_device *dev,
2724 struct va_format *vaf);
2725
2726extern __printf(3, 4) 2723extern __printf(3, 4)
2727int netdev_printk(const char *level, const struct net_device *dev, 2724int netdev_printk(const char *level, const struct net_device *dev,
2728 const char *format, ...); 2725 const char *format, ...);
diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index 60e9994ef40..5711e9525a2 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -14,11 +14,15 @@
14#include <linux/device.h> 14#include <linux/device.h>
15#include <linux/mod_devicetable.h> 15#include <linux/mod_devicetable.h>
16 16
17#define PLATFORM_DEVID_NONE (-1)
18#define PLATFORM_DEVID_AUTO (-2)
19
17struct mfd_cell; 20struct mfd_cell;
18 21
19struct platform_device { 22struct platform_device {
20 const char * name; 23 const char * name;
21 int id; 24 int id;
25 bool id_auto;
22 struct device dev; 26 struct device dev;
23 u32 num_resources; 27 u32 num_resources;
24 struct resource * resource; 28 struct resource * resource;
diff --git a/include/linux/pm.h b/include/linux/pm.h
index f067e60a383..88f034a23f2 100644
--- a/include/linux/pm.h
+++ b/include/linux/pm.h
@@ -638,6 +638,7 @@ extern void __suspend_report_result(const char *function, void *fn, int ret);
638 } while (0) 638 } while (0)
639 639
640extern int device_pm_wait_for_dev(struct device *sub, struct device *dev); 640extern int device_pm_wait_for_dev(struct device *sub, struct device *dev);
641extern void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *));
641 642
642extern int pm_generic_prepare(struct device *dev); 643extern int pm_generic_prepare(struct device *dev);
643extern int pm_generic_suspend_late(struct device *dev); 644extern int pm_generic_suspend_late(struct device *dev);
@@ -677,6 +678,10 @@ static inline int device_pm_wait_for_dev(struct device *a, struct device *b)
677 return 0; 678 return 0;
678} 679}
679 680
681static inline void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
682{
683}
684
680#define pm_generic_prepare NULL 685#define pm_generic_prepare NULL
681#define pm_generic_suspend NULL 686#define pm_generic_suspend NULL
682#define pm_generic_resume NULL 687#define pm_generic_resume NULL
diff --git a/init/Kconfig b/init/Kconfig
index 495e6e9632d..73e4adfa91d 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1265,13 +1265,7 @@ config KALLSYMS_ALL
1265 Say N unless you really need all symbols. 1265 Say N unless you really need all symbols.
1266 1266
1267config HOTPLUG 1267config HOTPLUG
1268 bool "Support for hot-pluggable devices" if EXPERT 1268 def_bool y
1269 default y
1270 help
1271 This option is provided for the case where no hotplug or uevent
1272 capabilities is wanted by the kernel. You should only consider
1273 disabling this option for embedded systems that do not use modules, a
1274 dynamic /dev tree, or dynamic device discovery. Just say Y.
1275 1269
1276config PRINTK 1270config PRINTK
1277 default y 1271 default y
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 7ca29a0a301..e7f7d993357 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -521,25 +521,25 @@ static char *dynamic_emit_prefix(const struct _ddebug *desc, char *buf)
521 int pos_after_tid; 521 int pos_after_tid;
522 int pos = 0; 522 int pos = 0;
523 523
524 pos += snprintf(buf + pos, remaining(pos), "%s", KERN_DEBUG); 524 *buf = '\0';
525
525 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) { 526 if (desc->flags & _DPRINTK_FLAGS_INCL_TID) {
526 if (in_interrupt()) 527 if (in_interrupt())
527 pos += snprintf(buf + pos, remaining(pos), "%s ", 528 pos += snprintf(buf + pos, remaining(pos), "<intr> ");
528 "<intr>");
529 else 529 else
530 pos += snprintf(buf + pos, remaining(pos), "[%d] ", 530 pos += snprintf(buf + pos, remaining(pos), "[%d] ",
531 task_pid_vnr(current)); 531 task_pid_vnr(current));
532 } 532 }
533 pos_after_tid = pos; 533 pos_after_tid = pos;
534 if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME) 534 if (desc->flags & _DPRINTK_FLAGS_INCL_MODNAME)
535 pos += snprintf(buf + pos, remaining(pos), "%s:", 535 pos += snprintf(buf + pos, remaining(pos), "%s:",
536 desc->modname); 536 desc->modname);
537 if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME) 537 if (desc->flags & _DPRINTK_FLAGS_INCL_FUNCNAME)
538 pos += snprintf(buf + pos, remaining(pos), "%s:", 538 pos += snprintf(buf + pos, remaining(pos), "%s:",
539 desc->function); 539 desc->function);
540 if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO) 540 if (desc->flags & _DPRINTK_FLAGS_INCL_LINENO)
541 pos += snprintf(buf + pos, remaining(pos), "%d:", 541 pos += snprintf(buf + pos, remaining(pos), "%d:",
542 desc->lineno); 542 desc->lineno);
543 if (pos - pos_after_tid) 543 if (pos - pos_after_tid)
544 pos += snprintf(buf + pos, remaining(pos), " "); 544 pos += snprintf(buf + pos, remaining(pos), " ");
545 if (pos >= PREFIX_SIZE) 545 if (pos >= PREFIX_SIZE)
@@ -559,9 +559,13 @@ int __dynamic_pr_debug(struct _ddebug *descriptor, const char *fmt, ...)
559 BUG_ON(!fmt); 559 BUG_ON(!fmt);
560 560
561 va_start(args, fmt); 561 va_start(args, fmt);
562
562 vaf.fmt = fmt; 563 vaf.fmt = fmt;
563 vaf.va = &args; 564 vaf.va = &args;
564 res = printk("%s%pV", dynamic_emit_prefix(descriptor, buf), &vaf); 565
566 res = printk(KERN_DEBUG "%s%pV",
567 dynamic_emit_prefix(descriptor, buf), &vaf);
568
565 va_end(args); 569 va_end(args);
566 570
567 return res; 571 return res;
@@ -574,15 +578,26 @@ int __dynamic_dev_dbg(struct _ddebug *descriptor,
574 struct va_format vaf; 578 struct va_format vaf;
575 va_list args; 579 va_list args;
576 int res; 580 int res;
577 char buf[PREFIX_SIZE];
578 581
579 BUG_ON(!descriptor); 582 BUG_ON(!descriptor);
580 BUG_ON(!fmt); 583 BUG_ON(!fmt);
581 584
582 va_start(args, fmt); 585 va_start(args, fmt);
586
583 vaf.fmt = fmt; 587 vaf.fmt = fmt;
584 vaf.va = &args; 588 vaf.va = &args;
585 res = __dev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); 589
590 if (!dev) {
591 res = printk(KERN_DEBUG "(NULL device *): %pV", &vaf);
592 } else {
593 char buf[PREFIX_SIZE];
594
595 res = dev_printk_emit(7, dev, "%s%s %s: %pV",
596 dynamic_emit_prefix(descriptor, buf),
597 dev_driver_string(dev), dev_name(dev),
598 &vaf);
599 }
600
586 va_end(args); 601 va_end(args);
587 602
588 return res; 603 return res;
@@ -592,20 +607,35 @@ EXPORT_SYMBOL(__dynamic_dev_dbg);
592#ifdef CONFIG_NET 607#ifdef CONFIG_NET
593 608
594int __dynamic_netdev_dbg(struct _ddebug *descriptor, 609int __dynamic_netdev_dbg(struct _ddebug *descriptor,
595 const struct net_device *dev, const char *fmt, ...) 610 const struct net_device *dev, const char *fmt, ...)
596{ 611{
597 struct va_format vaf; 612 struct va_format vaf;
598 va_list args; 613 va_list args;
599 int res; 614 int res;
600 char buf[PREFIX_SIZE];
601 615
602 BUG_ON(!descriptor); 616 BUG_ON(!descriptor);
603 BUG_ON(!fmt); 617 BUG_ON(!fmt);
604 618
605 va_start(args, fmt); 619 va_start(args, fmt);
620
606 vaf.fmt = fmt; 621 vaf.fmt = fmt;
607 vaf.va = &args; 622 vaf.va = &args;
608 res = __netdev_printk(dynamic_emit_prefix(descriptor, buf), dev, &vaf); 623
624 if (dev && dev->dev.parent) {
625 char buf[PREFIX_SIZE];
626
627 res = dev_printk_emit(7, dev->dev.parent,
628 "%s%s %s %s: %pV",
629 dynamic_emit_prefix(descriptor, buf),
630 dev_driver_string(dev->dev.parent),
631 dev_name(dev->dev.parent),
632 netdev_name(dev), &vaf);
633 } else if (dev) {
634 res = printk(KERN_DEBUG "%s: %pV", netdev_name(dev), &vaf);
635 } else {
636 res = printk(KERN_DEBUG "(NULL net_device): %pV", &vaf);
637 }
638
609 va_end(args); 639 va_end(args);
610 640
611 return res; 641 return res;
diff --git a/net/core/dev.c b/net/core/dev.c
index 89e33a5d4d9..36c4a0cdb6c 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -6424,22 +6424,26 @@ const char *netdev_drivername(const struct net_device *dev)
6424 return empty; 6424 return empty;
6425} 6425}
6426 6426
6427int __netdev_printk(const char *level, const struct net_device *dev, 6427static int __netdev_printk(const char *level, const struct net_device *dev,
6428 struct va_format *vaf) 6428 struct va_format *vaf)
6429{ 6429{
6430 int r; 6430 int r;
6431 6431
6432 if (dev && dev->dev.parent) 6432 if (dev && dev->dev.parent) {
6433 r = dev_printk(level, dev->dev.parent, "%s: %pV", 6433 r = dev_printk_emit(level[1] - '0',
6434 netdev_name(dev), vaf); 6434 dev->dev.parent,
6435 else if (dev) 6435 "%s %s %s: %pV",
6436 dev_driver_string(dev->dev.parent),
6437 dev_name(dev->dev.parent),
6438 netdev_name(dev), vaf);
6439 } else if (dev) {
6436 r = printk("%s%s: %pV", level, netdev_name(dev), vaf); 6440 r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
6437 else 6441 } else {
6438 r = printk("%s(NULL net_device): %pV", level, vaf); 6442 r = printk("%s(NULL net_device): %pV", level, vaf);
6443 }
6439 6444
6440 return r; 6445 return r;
6441} 6446}
6442EXPORT_SYMBOL(__netdev_printk);
6443 6447
6444int netdev_printk(const char *level, const struct net_device *dev, 6448int netdev_printk(const char *level, const struct net_device *dev,
6445 const char *format, ...) 6449 const char *format, ...)
@@ -6454,6 +6458,7 @@ int netdev_printk(const char *level, const struct net_device *dev,
6454 vaf.va = &args; 6458 vaf.va = &args;
6455 6459
6456 r = __netdev_printk(level, dev, &vaf); 6460 r = __netdev_printk(level, dev, &vaf);
6461
6457 va_end(args); 6462 va_end(args);
6458 6463
6459 return r; 6464 return r;
@@ -6473,6 +6478,7 @@ int func(const struct net_device *dev, const char *fmt, ...) \
6473 vaf.va = &args; \ 6478 vaf.va = &args; \
6474 \ 6479 \
6475 r = __netdev_printk(level, dev, &vaf); \ 6480 r = __netdev_printk(level, dev, &vaf); \
6481 \
6476 va_end(args); \ 6482 va_end(args); \
6477 \ 6483 \
6478 return r; \ 6484 return r; \
diff --git a/tools/hv/hv_get_dhcp_info.sh b/tools/hv/hv_get_dhcp_info.sh
new file mode 100755
index 00000000000..ccd3e953276
--- /dev/null
+++ b/tools/hv/hv_get_dhcp_info.sh
@@ -0,0 +1,28 @@
1#!/bin/bash
2
3# This example script retrieves the DHCP state of a given interface.
4# In the interest of keeping the KVP daemon code free of distro specific
5# information; the kvp daemon code invokes this external script to gather
6# DHCP setting for the specific interface.
7#
8# Input: Name of the interface
9#
10# Output: The script prints the string "Enabled" to stdout to indicate
11# that DHCP is enabled on the interface. If DHCP is not enabled,
12# the script prints the string "Disabled" to stdout.
13#
14# Each Distro is expected to implement this script in a distro specific
15# fashion. For instance on Distros that ship with Network Manager enabled,
16# this script can be based on the Network Manager APIs for retrieving DHCP
17# information.
18
19if_file="/etc/sysconfig/network-scripts/ifcfg-"$1
20
21dhcp=$(grep "dhcp" $if_file 2>/dev/null)
22
23if [ "$dhcp" != "" ];
24then
25echo "Enabled"
26else
27echo "Disabled"
28fi
diff --git a/tools/hv/hv_get_dns_info.sh b/tools/hv/hv_get_dns_info.sh
new file mode 100755
index 00000000000..058c17b46ff
--- /dev/null
+++ b/tools/hv/hv_get_dns_info.sh
@@ -0,0 +1,13 @@
1#!/bin/bash
2
3# This example script parses /etc/resolv.conf to retrive DNS information.
4# In the interest of keeping the KVP daemon code free of distro specific
5# information; the kvp daemon code invokes this external script to gather
6# DNS information.
7# This script is expected to print the nameserver values to stdout.
8# Each Distro is expected to implement this script in a distro specific
9# fashion. For instance on Distros that ship with Network Manager enabled,
10# this script can be based on the Network Manager APIs for retrieving DNS
11# entries.
12
13cat /etc/resolv.conf 2>/dev/null | awk '/^nameserver/ { print $2 }'
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index d9834b36294..5959affd882 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -31,6 +31,7 @@
31#include <stdlib.h> 31#include <stdlib.h>
32#include <unistd.h> 32#include <unistd.h>
33#include <string.h> 33#include <string.h>
34#include <ctype.h>
34#include <errno.h> 35#include <errno.h>
35#include <arpa/inet.h> 36#include <arpa/inet.h>
36#include <linux/connector.h> 37#include <linux/connector.h>
@@ -41,6 +42,7 @@
41#include <syslog.h> 42#include <syslog.h>
42#include <sys/stat.h> 43#include <sys/stat.h>
43#include <fcntl.h> 44#include <fcntl.h>
45#include <dirent.h>
44 46
45/* 47/*
46 * KVP protocol: The user mode component first registers with the 48 * KVP protocol: The user mode component first registers with the
@@ -68,25 +70,39 @@ enum key_index {
68 ProcessorArchitecture 70 ProcessorArchitecture
69}; 71};
70 72
73
74enum {
75 IPADDR = 0,
76 NETMASK,
77 GATEWAY,
78 DNS
79};
80
71static char kvp_send_buffer[4096]; 81static char kvp_send_buffer[4096];
72static char kvp_recv_buffer[4096]; 82static char kvp_recv_buffer[4096 * 2];
73static struct sockaddr_nl addr; 83static struct sockaddr_nl addr;
84static int in_hand_shake = 1;
74 85
75static char *os_name = ""; 86static char *os_name = "";
76static char *os_major = ""; 87static char *os_major = "";
77static char *os_minor = ""; 88static char *os_minor = "";
78static char *processor_arch; 89static char *processor_arch;
79static char *os_build; 90static char *os_build;
80static char *lic_version; 91static char *lic_version = "Unknown version";
81static struct utsname uts_buf; 92static struct utsname uts_buf;
82 93
94/*
95 * The location of the interface configuration file.
96 */
97
98#define KVP_CONFIG_LOC "/var/opt/"
83 99
84#define MAX_FILE_NAME 100 100#define MAX_FILE_NAME 100
85#define ENTRIES_PER_BLOCK 50 101#define ENTRIES_PER_BLOCK 50
86 102
87struct kvp_record { 103struct kvp_record {
88 __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; 104 char key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
89 __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; 105 char value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
90}; 106};
91 107
92struct kvp_file_state { 108struct kvp_file_state {
@@ -94,7 +110,7 @@ struct kvp_file_state {
94 int num_blocks; 110 int num_blocks;
95 struct kvp_record *records; 111 struct kvp_record *records;
96 int num_records; 112 int num_records;
97 __u8 fname[MAX_FILE_NAME]; 113 char fname[MAX_FILE_NAME];
98}; 114};
99 115
100static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT]; 116static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
@@ -106,7 +122,7 @@ static void kvp_acquire_lock(int pool)
106 122
107 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) { 123 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
108 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool); 124 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
109 exit(-1); 125 exit(EXIT_FAILURE);
110 } 126 }
111} 127}
112 128
@@ -118,7 +134,7 @@ static void kvp_release_lock(int pool)
118 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) { 134 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
119 perror("fcntl"); 135 perror("fcntl");
120 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool); 136 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
121 exit(-1); 137 exit(EXIT_FAILURE);
122 } 138 }
123} 139}
124 140
@@ -137,14 +153,19 @@ static void kvp_update_file(int pool)
137 if (!filep) { 153 if (!filep) {
138 kvp_release_lock(pool); 154 kvp_release_lock(pool);
139 syslog(LOG_ERR, "Failed to open file, pool: %d", pool); 155 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
140 exit(-1); 156 exit(EXIT_FAILURE);
141 } 157 }
142 158
143 bytes_written = fwrite(kvp_file_info[pool].records, 159 bytes_written = fwrite(kvp_file_info[pool].records,
144 sizeof(struct kvp_record), 160 sizeof(struct kvp_record),
145 kvp_file_info[pool].num_records, filep); 161 kvp_file_info[pool].num_records, filep);
146 162
147 fflush(filep); 163 if (ferror(filep) || fclose(filep)) {
164 kvp_release_lock(pool);
165 syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
166 exit(EXIT_FAILURE);
167 }
168
148 kvp_release_lock(pool); 169 kvp_release_lock(pool);
149} 170}
150 171
@@ -163,14 +184,19 @@ static void kvp_update_mem_state(int pool)
163 if (!filep) { 184 if (!filep) {
164 kvp_release_lock(pool); 185 kvp_release_lock(pool);
165 syslog(LOG_ERR, "Failed to open file, pool: %d", pool); 186 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
166 exit(-1); 187 exit(EXIT_FAILURE);
167 } 188 }
168 while (!feof(filep)) { 189 for (;;) {
169 readp = &record[records_read]; 190 readp = &record[records_read];
170 records_read += fread(readp, sizeof(struct kvp_record), 191 records_read += fread(readp, sizeof(struct kvp_record),
171 ENTRIES_PER_BLOCK * num_blocks, 192 ENTRIES_PER_BLOCK * num_blocks,
172 filep); 193 filep);
173 194
195 if (ferror(filep)) {
196 syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
197 exit(EXIT_FAILURE);
198 }
199
174 if (!feof(filep)) { 200 if (!feof(filep)) {
175 /* 201 /*
176 * We have more data to read. 202 * We have more data to read.
@@ -180,7 +206,7 @@ static void kvp_update_mem_state(int pool)
180 206
181 if (record == NULL) { 207 if (record == NULL) {
182 syslog(LOG_ERR, "malloc failed"); 208 syslog(LOG_ERR, "malloc failed");
183 exit(-1); 209 exit(EXIT_FAILURE);
184 } 210 }
185 continue; 211 continue;
186 } 212 }
@@ -191,14 +217,15 @@ static void kvp_update_mem_state(int pool)
191 kvp_file_info[pool].records = record; 217 kvp_file_info[pool].records = record;
192 kvp_file_info[pool].num_records = records_read; 218 kvp_file_info[pool].num_records = records_read;
193 219
220 fclose(filep);
194 kvp_release_lock(pool); 221 kvp_release_lock(pool);
195} 222}
196static int kvp_file_init(void) 223static int kvp_file_init(void)
197{ 224{
198 int ret, fd; 225 int fd;
199 FILE *filep; 226 FILE *filep;
200 size_t records_read; 227 size_t records_read;
201 __u8 *fname; 228 char *fname;
202 struct kvp_record *record; 229 struct kvp_record *record;
203 struct kvp_record *readp; 230 struct kvp_record *readp;
204 int num_blocks; 231 int num_blocks;
@@ -208,7 +235,7 @@ static int kvp_file_init(void)
208 if (access("/var/opt/hyperv", F_OK)) { 235 if (access("/var/opt/hyperv", F_OK)) {
209 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) { 236 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
210 syslog(LOG_ERR, " Failed to create /var/opt/hyperv"); 237 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
211 exit(-1); 238 exit(EXIT_FAILURE);
212 } 239 }
213 } 240 }
214 241
@@ -232,12 +259,18 @@ static int kvp_file_init(void)
232 fclose(filep); 259 fclose(filep);
233 return 1; 260 return 1;
234 } 261 }
235 while (!feof(filep)) { 262 for (;;) {
236 readp = &record[records_read]; 263 readp = &record[records_read];
237 records_read += fread(readp, sizeof(struct kvp_record), 264 records_read += fread(readp, sizeof(struct kvp_record),
238 ENTRIES_PER_BLOCK, 265 ENTRIES_PER_BLOCK,
239 filep); 266 filep);
240 267
268 if (ferror(filep)) {
269 syslog(LOG_ERR, "Failed to read file, pool: %d",
270 i);
271 exit(EXIT_FAILURE);
272 }
273
241 if (!feof(filep)) { 274 if (!feof(filep)) {
242 /* 275 /*
243 * We have more data to read. 276 * We have more data to read.
@@ -311,7 +344,6 @@ static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
311 int value_size) 344 int value_size)
312{ 345{
313 int i; 346 int i;
314 int j, k;
315 int num_records; 347 int num_records;
316 struct kvp_record *record; 348 struct kvp_record *record;
317 int num_blocks; 349 int num_blocks;
@@ -394,7 +426,7 @@ static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
394 return 1; 426 return 1;
395} 427}
396 428
397static void kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size, 429static int kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
398 __u8 *value, int value_size) 430 __u8 *value, int value_size)
399{ 431{
400 struct kvp_record *record; 432 struct kvp_record *record;
@@ -406,16 +438,12 @@ static void kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
406 record = kvp_file_info[pool].records; 438 record = kvp_file_info[pool].records;
407 439
408 if (index >= kvp_file_info[pool].num_records) { 440 if (index >= kvp_file_info[pool].num_records) {
409 /* 441 return 1;
410 * This is an invalid index; terminate enumeration;
411 * - a NULL value will do the trick.
412 */
413 strcpy(value, "");
414 return;
415 } 442 }
416 443
417 memcpy(key, record[index].key, key_size); 444 memcpy(key, record[index].key, key_size);
418 memcpy(value, record[index].value, value_size); 445 memcpy(value, record[index].value, value_size);
446 return 0;
419} 447}
420 448
421 449
@@ -426,6 +454,7 @@ void kvp_get_os_info(void)
426 454
427 uname(&uts_buf); 455 uname(&uts_buf);
428 os_build = uts_buf.release; 456 os_build = uts_buf.release;
457 os_name = uts_buf.sysname;
429 processor_arch = uts_buf.machine; 458 processor_arch = uts_buf.machine;
430 459
431 /* 460 /*
@@ -437,20 +466,70 @@ void kvp_get_os_info(void)
437 if (p) 466 if (p)
438 *p = '\0'; 467 *p = '\0';
439 468
469 /*
470 * Parse the /etc/os-release file if present:
471 * http://www.freedesktop.org/software/systemd/man/os-release.html
472 */
473 file = fopen("/etc/os-release", "r");
474 if (file != NULL) {
475 while (fgets(buf, sizeof(buf), file)) {
476 char *value, *q;
477
478 /* Ignore comments */
479 if (buf[0] == '#')
480 continue;
481
482 /* Split into name=value */
483 p = strchr(buf, '=');
484 if (!p)
485 continue;
486 *p++ = 0;
487
488 /* Remove quotes and newline; un-escape */
489 value = p;
490 q = p;
491 while (*p) {
492 if (*p == '\\') {
493 ++p;
494 if (!*p)
495 break;
496 *q++ = *p++;
497 } else if (*p == '\'' || *p == '"' ||
498 *p == '\n') {
499 ++p;
500 } else {
501 *q++ = *p++;
502 }
503 }
504 *q = 0;
505
506 if (!strcmp(buf, "NAME")) {
507 p = strdup(value);
508 if (!p)
509 break;
510 os_name = p;
511 } else if (!strcmp(buf, "VERSION_ID")) {
512 p = strdup(value);
513 if (!p)
514 break;
515 os_major = p;
516 }
517 }
518 fclose(file);
519 return;
520 }
521
522 /* Fallback for older RH/SUSE releases */
440 file = fopen("/etc/SuSE-release", "r"); 523 file = fopen("/etc/SuSE-release", "r");
441 if (file != NULL) 524 if (file != NULL)
442 goto kvp_osinfo_found; 525 goto kvp_osinfo_found;
443 file = fopen("/etc/redhat-release", "r"); 526 file = fopen("/etc/redhat-release", "r");
444 if (file != NULL) 527 if (file != NULL)
445 goto kvp_osinfo_found; 528 goto kvp_osinfo_found;
446 /*
447 * Add code for other supported platforms.
448 */
449 529
450 /* 530 /*
451 * We don't have information about the os. 531 * We don't have information about the os.
452 */ 532 */
453 os_name = uts_buf.sysname;
454 return; 533 return;
455 534
456kvp_osinfo_found: 535kvp_osinfo_found:
@@ -494,82 +573,458 @@ done:
494 return; 573 return;
495} 574}
496 575
576
577
578/*
579 * Retrieve an interface name corresponding to the specified guid.
580 * If there is a match, the function returns a pointer
581 * to the interface name and if not, a NULL is returned.
582 * If a match is found, the caller is responsible for
583 * freeing the memory.
584 */
585
586static char *kvp_get_if_name(char *guid)
587{
588 DIR *dir;
589 struct dirent *entry;
590 FILE *file;
591 char *p, *q, *x;
592 char *if_name = NULL;
593 char buf[256];
594 char *kvp_net_dir = "/sys/class/net/";
595 char dev_id[256];
596
597 dir = opendir(kvp_net_dir);
598 if (dir == NULL)
599 return NULL;
600
601 snprintf(dev_id, sizeof(dev_id), "%s", kvp_net_dir);
602 q = dev_id + strlen(kvp_net_dir);
603
604 while ((entry = readdir(dir)) != NULL) {
605 /*
606 * Set the state for the next pass.
607 */
608 *q = '\0';
609 strcat(dev_id, entry->d_name);
610 strcat(dev_id, "/device/device_id");
611
612 file = fopen(dev_id, "r");
613 if (file == NULL)
614 continue;
615
616 p = fgets(buf, sizeof(buf), file);
617 if (p) {
618 x = strchr(p, '\n');
619 if (x)
620 *x = '\0';
621
622 if (!strcmp(p, guid)) {
623 /*
624 * Found the guid match; return the interface
625 * name. The caller will free the memory.
626 */
627 if_name = strdup(entry->d_name);
628 fclose(file);
629 break;
630 }
631 }
632 fclose(file);
633 }
634
635 closedir(dir);
636 return if_name;
637}
638
639/*
640 * Retrieve the MAC address given the interface name.
641 */
642
643static char *kvp_if_name_to_mac(char *if_name)
644{
645 FILE *file;
646 char *p, *x;
647 char buf[256];
648 char addr_file[256];
649 int i;
650 char *mac_addr = NULL;
651
652 snprintf(addr_file, sizeof(addr_file), "%s%s%s", "/sys/class/net/",
653 if_name, "/address");
654
655 file = fopen(addr_file, "r");
656 if (file == NULL)
657 return NULL;
658
659 p = fgets(buf, sizeof(buf), file);
660 if (p) {
661 x = strchr(p, '\n');
662 if (x)
663 *x = '\0';
664 for (i = 0; i < strlen(p); i++)
665 p[i] = toupper(p[i]);
666 mac_addr = strdup(p);
667 }
668
669 fclose(file);
670 return mac_addr;
671}
672
673
674/*
675 * Retrieve the interface name given tha MAC address.
676 */
677
678static char *kvp_mac_to_if_name(char *mac)
679{
680 DIR *dir;
681 struct dirent *entry;
682 FILE *file;
683 char *p, *q, *x;
684 char *if_name = NULL;
685 char buf[256];
686 char *kvp_net_dir = "/sys/class/net/";
687 char dev_id[256];
688 int i;
689
690 dir = opendir(kvp_net_dir);
691 if (dir == NULL)
692 return NULL;
693
694 snprintf(dev_id, sizeof(dev_id), kvp_net_dir);
695 q = dev_id + strlen(kvp_net_dir);
696
697 while ((entry = readdir(dir)) != NULL) {
698 /*
699 * Set the state for the next pass.
700 */
701 *q = '\0';
702
703 strcat(dev_id, entry->d_name);
704 strcat(dev_id, "/address");
705
706 file = fopen(dev_id, "r");
707 if (file == NULL)
708 continue;
709
710 p = fgets(buf, sizeof(buf), file);
711 if (p) {
712 x = strchr(p, '\n');
713 if (x)
714 *x = '\0';
715
716 for (i = 0; i < strlen(p); i++)
717 p[i] = toupper(p[i]);
718
719 if (!strcmp(p, mac)) {
720 /*
721 * Found the MAC match; return the interface
722 * name. The caller will free the memory.
723 */
724 if_name = strdup(entry->d_name);
725 fclose(file);
726 break;
727 }
728 }
729 fclose(file);
730 }
731
732 closedir(dir);
733 return if_name;
734}
735
736
737static void kvp_process_ipconfig_file(char *cmd,
738 char *config_buf, int len,
739 int element_size, int offset)
740{
741 char buf[256];
742 char *p;
743 char *x;
744 FILE *file;
745
746 /*
747 * First execute the command.
748 */
749 file = popen(cmd, "r");
750 if (file == NULL)
751 return;
752
753 if (offset == 0)
754 memset(config_buf, 0, len);
755 while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
756 if ((len - strlen(config_buf)) < (element_size + 1))
757 break;
758
759 x = strchr(p, '\n');
760 *x = '\0';
761 strcat(config_buf, p);
762 strcat(config_buf, ";");
763 }
764 pclose(file);
765}
766
767static void kvp_get_ipconfig_info(char *if_name,
768 struct hv_kvp_ipaddr_value *buffer)
769{
770 char cmd[512];
771 char dhcp_info[128];
772 char *p;
773 FILE *file;
774
775 /*
776 * Get the address of default gateway (ipv4).
777 */
778 sprintf(cmd, "%s %s", "ip route show dev", if_name);
779 strcat(cmd, " | awk '/default/ {print $3 }'");
780
781 /*
782 * Execute the command to gather gateway info.
783 */
784 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
785 (MAX_GATEWAY_SIZE * 2), INET_ADDRSTRLEN, 0);
786
787 /*
788 * Get the address of default gateway (ipv6).
789 */
790 sprintf(cmd, "%s %s", "ip -f inet6 route show dev", if_name);
791 strcat(cmd, " | awk '/default/ {print $3 }'");
792
793 /*
794 * Execute the command to gather gateway info (ipv6).
795 */
796 kvp_process_ipconfig_file(cmd, (char *)buffer->gate_way,
797 (MAX_GATEWAY_SIZE * 2), INET6_ADDRSTRLEN, 1);
798
799
800 /*
801 * Gather the DNS state.
802 * Since there is no standard way to get this information
803 * across various distributions of interest; we just invoke
804 * an external script that needs to be ported across distros
805 * of interest.
806 *
807 * Following is the expected format of the information from the script:
808 *
809 * ipaddr1 (nameserver1)
810 * ipaddr2 (nameserver2)
811 * .
812 * .
813 */
814
815 sprintf(cmd, "%s", "hv_get_dns_info");
816
817 /*
818 * Execute the command to gather DNS info.
819 */
820 kvp_process_ipconfig_file(cmd, (char *)buffer->dns_addr,
821 (MAX_IP_ADDR_SIZE * 2), INET_ADDRSTRLEN, 0);
822
823 /*
824 * Gather the DHCP state.
825 * We will gather this state by invoking an external script.
826 * The parameter to the script is the interface name.
827 * Here is the expected output:
828 *
829 * Enabled: DHCP enabled.
830 */
831
832 sprintf(cmd, "%s %s", "hv_get_dhcp_info", if_name);
833
834 file = popen(cmd, "r");
835 if (file == NULL)
836 return;
837
838 p = fgets(dhcp_info, sizeof(dhcp_info), file);
839 if (p == NULL) {
840 pclose(file);
841 return;
842 }
843
844 if (!strncmp(p, "Enabled", 7))
845 buffer->dhcp_enabled = 1;
846 else
847 buffer->dhcp_enabled = 0;
848
849 pclose(file);
850}
851
852
853static unsigned int hweight32(unsigned int *w)
854{
855 unsigned int res = *w - ((*w >> 1) & 0x55555555);
856 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
857 res = (res + (res >> 4)) & 0x0F0F0F0F;
858 res = res + (res >> 8);
859 return (res + (res >> 16)) & 0x000000FF;
860}
861
862static int kvp_process_ip_address(void *addrp,
863 int family, char *buffer,
864 int length, int *offset)
865{
866 struct sockaddr_in *addr;
867 struct sockaddr_in6 *addr6;
868 int addr_length;
869 char tmp[50];
870 const char *str;
871
872 if (family == AF_INET) {
873 addr = (struct sockaddr_in *)addrp;
874 str = inet_ntop(family, &addr->sin_addr, tmp, 50);
875 addr_length = INET_ADDRSTRLEN;
876 } else {
877 addr6 = (struct sockaddr_in6 *)addrp;
878 str = inet_ntop(family, &addr6->sin6_addr.s6_addr, tmp, 50);
879 addr_length = INET6_ADDRSTRLEN;
880 }
881
882 if ((length - *offset) < addr_length + 1)
883 return HV_E_FAIL;
884 if (str == NULL) {
885 strcpy(buffer, "inet_ntop failed\n");
886 return HV_E_FAIL;
887 }
888 if (*offset == 0)
889 strcpy(buffer, tmp);
890 else
891 strcat(buffer, tmp);
892 strcat(buffer, ";");
893
894 *offset += strlen(str) + 1;
895 return 0;
896}
897
497static int 898static int
498kvp_get_ip_address(int family, char *buffer, int length) 899kvp_get_ip_info(int family, char *if_name, int op,
900 void *out_buffer, int length)
499{ 901{
500 struct ifaddrs *ifap; 902 struct ifaddrs *ifap;
501 struct ifaddrs *curp; 903 struct ifaddrs *curp;
502 int ipv4_len = strlen("255.255.255.255") + 1;
503 int ipv6_len = strlen("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")+1;
504 int offset = 0; 904 int offset = 0;
505 const char *str; 905 int sn_offset = 0;
506 char tmp[50];
507 int error = 0; 906 int error = 0;
508 907 char *buffer;
908 struct hv_kvp_ipaddr_value *ip_buffer;
909 char cidr_mask[5]; /* /xyz */
910 int weight;
911 int i;
912 unsigned int *w;
913 char *sn_str;
914 struct sockaddr_in6 *addr6;
915
916 if (op == KVP_OP_ENUMERATE) {
917 buffer = out_buffer;
918 } else {
919 ip_buffer = out_buffer;
920 buffer = (char *)ip_buffer->ip_addr;
921 ip_buffer->addr_family = 0;
922 }
509 /* 923 /*
510 * On entry into this function, the buffer is capable of holding the 924 * On entry into this function, the buffer is capable of holding the
511 * maximum key value (2048 bytes). 925 * maximum key value.
512 */ 926 */
513 927
514 if (getifaddrs(&ifap)) { 928 if (getifaddrs(&ifap)) {
515 strcpy(buffer, "getifaddrs failed\n"); 929 strcpy(buffer, "getifaddrs failed\n");
516 return 1; 930 return HV_E_FAIL;
517 } 931 }
518 932
519 curp = ifap; 933 curp = ifap;
520 while (curp != NULL) { 934 while (curp != NULL) {
521 if ((curp->ifa_addr != NULL) && 935 if (curp->ifa_addr == NULL) {
522 (curp->ifa_addr->sa_family == family)) { 936 curp = curp->ifa_next;
523 if (family == AF_INET) { 937 continue;
524 struct sockaddr_in *addr = 938 }
525 (struct sockaddr_in *) curp->ifa_addr;
526
527 str = inet_ntop(family, &addr->sin_addr,
528 tmp, 50);
529 if (str == NULL) {
530 strcpy(buffer, "inet_ntop failed\n");
531 error = 1;
532 goto getaddr_done;
533 }
534 if (offset == 0)
535 strcpy(buffer, tmp);
536 else
537 strcat(buffer, tmp);
538 strcat(buffer, ";");
539 939
540 offset += strlen(str) + 1; 940 if ((if_name != NULL) &&
541 if ((length - offset) < (ipv4_len + 1)) 941 (strncmp(curp->ifa_name, if_name, strlen(if_name)))) {
542 goto getaddr_done; 942 /*
943 * We want info about a specific interface;
944 * just continue.
945 */
946 curp = curp->ifa_next;
947 continue;
948 }
543 949
544 } else { 950 /*
951 * We only support two address families: AF_INET and AF_INET6.
952 * If a family value of 0 is specified, we collect both
953 * supported address families; if not we gather info on
954 * the specified address family.
955 */
956 if ((family != 0) && (curp->ifa_addr->sa_family != family)) {
957 curp = curp->ifa_next;
958 continue;
959 }
960 if ((curp->ifa_addr->sa_family != AF_INET) &&
961 (curp->ifa_addr->sa_family != AF_INET6)) {
962 curp = curp->ifa_next;
963 continue;
964 }
545 965
966 if (op == KVP_OP_GET_IP_INFO) {
546 /* 967 /*
547 * We only support AF_INET and AF_INET6 968 * Gather info other than the IP address.
548 * and the list of addresses is separated by a ";". 969 * IP address info will be gathered later.
549 */ 970 */
550 struct sockaddr_in6 *addr = 971 if (curp->ifa_addr->sa_family == AF_INET) {
551 (struct sockaddr_in6 *) curp->ifa_addr; 972 ip_buffer->addr_family |= ADDR_FAMILY_IPV4;
552 973 /*
553 str = inet_ntop(family, 974 * Get subnet info.
554 &addr->sin6_addr.s6_addr, 975 */
555 tmp, 50); 976 error = kvp_process_ip_address(
556 if (str == NULL) { 977 curp->ifa_netmask,
557 strcpy(buffer, "inet_ntop failed\n"); 978 AF_INET,
558 error = 1; 979 (char *)
559 goto getaddr_done; 980 ip_buffer->sub_net,
560 } 981 length,
561 if (offset == 0) 982 &sn_offset);
562 strcpy(buffer, tmp); 983 if (error)
563 else 984 goto gather_ipaddr;
564 strcat(buffer, tmp); 985 } else {
565 strcat(buffer, ";"); 986 ip_buffer->addr_family |= ADDR_FAMILY_IPV6;
566 offset += strlen(str) + 1;
567 if ((length - offset) < (ipv6_len + 1))
568 goto getaddr_done;
569 987
988 /*
989 * Get subnet info in CIDR format.
990 */
991 weight = 0;
992 sn_str = (char *)ip_buffer->sub_net;
993 addr6 = (struct sockaddr_in6 *)
994 curp->ifa_netmask;
995 w = addr6->sin6_addr.s6_addr32;
996
997 for (i = 0; i < 4; i++)
998 weight += hweight32(&w[i]);
999
1000 sprintf(cidr_mask, "/%d", weight);
1001 if ((length - sn_offset) <
1002 (strlen(cidr_mask) + 1))
1003 goto gather_ipaddr;
1004
1005 if (sn_offset == 0)
1006 strcpy(sn_str, cidr_mask);
1007 else
1008 strcat(sn_str, cidr_mask);
1009 strcat((char *)ip_buffer->sub_net, ";");
1010 sn_offset += strlen(sn_str) + 1;
570 } 1011 }
571 1012
1013 /*
1014 * Collect other ip related configuration info.
1015 */
1016
1017 kvp_get_ipconfig_info(if_name, ip_buffer);
572 } 1018 }
1019
1020gather_ipaddr:
1021 error = kvp_process_ip_address(curp->ifa_addr,
1022 curp->ifa_addr->sa_family,
1023 buffer,
1024 length, &offset);
1025 if (error)
1026 goto getaddr_done;
1027
573 curp = curp->ifa_next; 1028 curp = curp->ifa_next;
574 } 1029 }
575 1030
@@ -579,6 +1034,315 @@ getaddr_done:
579} 1034}
580 1035
581 1036
1037static int expand_ipv6(char *addr, int type)
1038{
1039 int ret;
1040 struct in6_addr v6_addr;
1041
1042 ret = inet_pton(AF_INET6, addr, &v6_addr);
1043
1044 if (ret != 1) {
1045 if (type == NETMASK)
1046 return 1;
1047 return 0;
1048 }
1049
1050 sprintf(addr, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
1051 "%02x%02x:%02x%02x:%02x%02x",
1052 (int)v6_addr.s6_addr[0], (int)v6_addr.s6_addr[1],
1053 (int)v6_addr.s6_addr[2], (int)v6_addr.s6_addr[3],
1054 (int)v6_addr.s6_addr[4], (int)v6_addr.s6_addr[5],
1055 (int)v6_addr.s6_addr[6], (int)v6_addr.s6_addr[7],
1056 (int)v6_addr.s6_addr[8], (int)v6_addr.s6_addr[9],
1057 (int)v6_addr.s6_addr[10], (int)v6_addr.s6_addr[11],
1058 (int)v6_addr.s6_addr[12], (int)v6_addr.s6_addr[13],
1059 (int)v6_addr.s6_addr[14], (int)v6_addr.s6_addr[15]);
1060
1061 return 1;
1062
1063}
1064
1065static int is_ipv4(char *addr)
1066{
1067 int ret;
1068 struct in_addr ipv4_addr;
1069
1070 ret = inet_pton(AF_INET, addr, &ipv4_addr);
1071
1072 if (ret == 1)
1073 return 1;
1074 return 0;
1075}
1076
1077static int parse_ip_val_buffer(char *in_buf, int *offset,
1078 char *out_buf, int out_len)
1079{
1080 char *x;
1081 char *start;
1082
1083 /*
1084 * in_buf has sequence of characters that are seperated by
1085 * the character ';'. The last sequence does not have the
1086 * terminating ";" character.
1087 */
1088 start = in_buf + *offset;
1089
1090 x = strchr(start, ';');
1091 if (x)
1092 *x = 0;
1093 else
1094 x = start + strlen(start);
1095
1096 if (strlen(start) != 0) {
1097 int i = 0;
1098 /*
1099 * Get rid of leading spaces.
1100 */
1101 while (start[i] == ' ')
1102 i++;
1103
1104 if ((x - start) <= out_len) {
1105 strcpy(out_buf, (start + i));
1106 *offset += (x - start) + 1;
1107 return 1;
1108 }
1109 }
1110 return 0;
1111}
1112
1113static int kvp_write_file(FILE *f, char *s1, char *s2, char *s3)
1114{
1115 int ret;
1116
1117 ret = fprintf(f, "%s%s%s%s\n", s1, s2, "=", s3);
1118
1119 if (ret < 0)
1120 return HV_E_FAIL;
1121
1122 return 0;
1123}
1124
1125
1126static int process_ip_string(FILE *f, char *ip_string, int type)
1127{
1128 int error = 0;
1129 char addr[INET6_ADDRSTRLEN];
1130 int i = 0;
1131 int j = 0;
1132 char str[256];
1133 char sub_str[10];
1134 int offset = 0;
1135
1136 memset(addr, 0, sizeof(addr));
1137
1138 while (parse_ip_val_buffer(ip_string, &offset, addr,
1139 (MAX_IP_ADDR_SIZE * 2))) {
1140
1141 sub_str[0] = 0;
1142 if (is_ipv4(addr)) {
1143 switch (type) {
1144 case IPADDR:
1145 snprintf(str, sizeof(str), "%s", "IPADDR");
1146 break;
1147 case NETMASK:
1148 snprintf(str, sizeof(str), "%s", "NETMASK");
1149 break;
1150 case GATEWAY:
1151 snprintf(str, sizeof(str), "%s", "GATEWAY");
1152 break;
1153 case DNS:
1154 snprintf(str, sizeof(str), "%s", "DNS");
1155 break;
1156 }
1157 if (i != 0) {
1158 if (type != DNS) {
1159 snprintf(sub_str, sizeof(sub_str),
1160 "_%d", i++);
1161 } else {
1162 snprintf(sub_str, sizeof(sub_str),
1163 "%d", ++i);
1164 }
1165 } else if (type == DNS) {
1166 snprintf(sub_str, sizeof(sub_str), "%d", ++i);
1167 }
1168
1169
1170 } else if (expand_ipv6(addr, type)) {
1171 switch (type) {
1172 case IPADDR:
1173 snprintf(str, sizeof(str), "%s", "IPV6ADDR");
1174 break;
1175 case NETMASK:
1176 snprintf(str, sizeof(str), "%s", "IPV6NETMASK");
1177 break;
1178 case GATEWAY:
1179 snprintf(str, sizeof(str), "%s",
1180 "IPV6_DEFAULTGW");
1181 break;
1182 case DNS:
1183 snprintf(str, sizeof(str), "%s", "DNS");
1184 break;
1185 }
1186 if ((j != 0) || (type == DNS)) {
1187 if (type != DNS) {
1188 snprintf(sub_str, sizeof(sub_str),
1189 "_%d", j++);
1190 } else {
1191 snprintf(sub_str, sizeof(sub_str),
1192 "%d", ++i);
1193 }
1194 } else if (type == DNS) {
1195 snprintf(sub_str, sizeof(sub_str),
1196 "%d", ++i);
1197 }
1198 } else {
1199 return HV_INVALIDARG;
1200 }
1201
1202 error = kvp_write_file(f, str, sub_str, addr);
1203 if (error)
1204 return error;
1205 memset(addr, 0, sizeof(addr));
1206 }
1207
1208 return 0;
1209}
1210
1211static int kvp_set_ip_info(char *if_name, struct hv_kvp_ipaddr_value *new_val)
1212{
1213 int error = 0;
1214 char if_file[128];
1215 FILE *file;
1216 char cmd[512];
1217 char *mac_addr;
1218
1219 /*
1220 * Set the configuration for the specified interface with
1221 * the information provided. Since there is no standard
1222 * way to configure an interface, we will have an external
1223 * script that does the job of configuring the interface and
1224 * flushing the configuration.
1225 *
1226 * The parameters passed to this external script are:
1227 * 1. A configuration file that has the specified configuration.
1228 *
1229 * We will embed the name of the interface in the configuration
1230 * file: ifcfg-ethx (where ethx is the interface name).
1231 *
1232 * The information provided here may be more than what is needed
1233 * in a given distro to configure the interface and so are free
1234 * ignore information that may not be relevant.
1235 *
1236 * Here is the format of the ip configuration file:
1237 *
1238 * HWADDR=macaddr
1239 * IF_NAME=interface name
1240 * DHCP=yes (This is optional; if yes, DHCP is configured)
1241 *
1242 * IPADDR=ipaddr1
1243 * IPADDR_1=ipaddr2
1244 * IPADDR_x=ipaddry (where y = x + 1)
1245 *
1246 * NETMASK=netmask1
1247 * NETMASK_x=netmasky (where y = x + 1)
1248 *
1249 * GATEWAY=ipaddr1
1250 * GATEWAY_x=ipaddry (where y = x + 1)
1251 *
1252 * DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
1253 *
1254 * IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
1255 * tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
1256 * IPV6NETMASK.
1257 *
1258 * The host can specify multiple ipv4 and ipv6 addresses to be
1259 * configured for the interface. Furthermore, the configuration
1260 * needs to be persistent. A subsequent GET call on the interface
1261 * is expected to return the configuration that is set via the SET
1262 * call.
1263 */
1264
1265 snprintf(if_file, sizeof(if_file), "%s%s%s", KVP_CONFIG_LOC,
1266 "hyperv/ifcfg-", if_name);
1267
1268 file = fopen(if_file, "w");
1269
1270 if (file == NULL) {
1271 syslog(LOG_ERR, "Failed to open config file");
1272 return HV_E_FAIL;
1273 }
1274
1275 /*
1276 * First write out the MAC address.
1277 */
1278
1279 mac_addr = kvp_if_name_to_mac(if_name);
1280 if (mac_addr == NULL) {
1281 error = HV_E_FAIL;
1282 goto setval_error;
1283 }
1284
1285 error = kvp_write_file(file, "HWADDR", "", mac_addr);
1286 if (error)
1287 goto setval_error;
1288
1289 error = kvp_write_file(file, "IF_NAME", "", if_name);
1290 if (error)
1291 goto setval_error;
1292
1293 if (new_val->dhcp_enabled) {
1294 error = kvp_write_file(file, "DHCP", "", "yes");
1295 if (error)
1296 goto setval_error;
1297
1298 /*
1299 * We are done!.
1300 */
1301 goto setval_done;
1302 }
1303
1304 /*
1305 * Write the configuration for ipaddress, netmask, gateway and
1306 * name servers.
1307 */
1308
1309 error = process_ip_string(file, (char *)new_val->ip_addr, IPADDR);
1310 if (error)
1311 goto setval_error;
1312
1313 error = process_ip_string(file, (char *)new_val->sub_net, NETMASK);
1314 if (error)
1315 goto setval_error;
1316
1317 error = process_ip_string(file, (char *)new_val->gate_way, GATEWAY);
1318 if (error)
1319 goto setval_error;
1320
1321 error = process_ip_string(file, (char *)new_val->dns_addr, DNS);
1322 if (error)
1323 goto setval_error;
1324
1325setval_done:
1326 free(mac_addr);
1327 fclose(file);
1328
1329 /*
1330 * Now that we have populated the configuration file,
1331 * invoke the external script to do its magic.
1332 */
1333
1334 snprintf(cmd, sizeof(cmd), "%s %s", "hv_set_ifconfig", if_file);
1335 system(cmd);
1336 return 0;
1337
1338setval_error:
1339 syslog(LOG_ERR, "Failed to write config file");
1340 free(mac_addr);
1341 fclose(file);
1342 return error;
1343}
1344
1345
582static int 1346static int
583kvp_get_domain_name(char *buffer, int length) 1347kvp_get_domain_name(char *buffer, int length)
584{ 1348{
@@ -646,6 +1410,10 @@ int main(void)
646 char *p; 1410 char *p;
647 char *key_value; 1411 char *key_value;
648 char *key_name; 1412 char *key_name;
1413 int op;
1414 int pool;
1415 char *if_name;
1416 struct hv_kvp_ipaddr_value *kvp_ip_val;
649 1417
650 daemon(1, 0); 1418 daemon(1, 0);
651 openlog("KVP", 0, LOG_USER); 1419 openlog("KVP", 0, LOG_USER);
@@ -657,13 +1425,13 @@ int main(void)
657 1425
658 if (kvp_file_init()) { 1426 if (kvp_file_init()) {
659 syslog(LOG_ERR, "Failed to initialize the pools"); 1427 syslog(LOG_ERR, "Failed to initialize the pools");
660 exit(-1); 1428 exit(EXIT_FAILURE);
661 } 1429 }
662 1430
663 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); 1431 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
664 if (fd < 0) { 1432 if (fd < 0) {
665 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd); 1433 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
666 exit(-1); 1434 exit(EXIT_FAILURE);
667 } 1435 }
668 addr.nl_family = AF_NETLINK; 1436 addr.nl_family = AF_NETLINK;
669 addr.nl_pad = 0; 1437 addr.nl_pad = 0;
@@ -675,7 +1443,7 @@ int main(void)
675 if (error < 0) { 1443 if (error < 0) {
676 syslog(LOG_ERR, "bind failed; error:%d", error); 1444 syslog(LOG_ERR, "bind failed; error:%d", error);
677 close(fd); 1445 close(fd);
678 exit(-1); 1446 exit(EXIT_FAILURE);
679 } 1447 }
680 sock_opt = addr.nl_groups; 1448 sock_opt = addr.nl_groups;
681 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt)); 1449 setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
@@ -687,7 +1455,7 @@ int main(void)
687 message->id.val = CN_KVP_VAL; 1455 message->id.val = CN_KVP_VAL;
688 1456
689 hv_msg = (struct hv_kvp_msg *)message->data; 1457 hv_msg = (struct hv_kvp_msg *)message->data;
690 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER; 1458 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER1;
691 message->ack = 0; 1459 message->ack = 0;
692 message->len = sizeof(struct hv_kvp_msg); 1460 message->len = sizeof(struct hv_kvp_msg);
693 1461
@@ -695,7 +1463,7 @@ int main(void)
695 if (len < 0) { 1463 if (len < 0) {
696 syslog(LOG_ERR, "netlink_send failed; error:%d", len); 1464 syslog(LOG_ERR, "netlink_send failed; error:%d", len);
697 close(fd); 1465 close(fd);
698 exit(-1); 1466 exit(EXIT_FAILURE);
699 } 1467 }
700 1468
701 pfd.fd = fd; 1469 pfd.fd = fd;
@@ -721,12 +1489,21 @@ int main(void)
721 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg); 1489 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
722 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data; 1490 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
723 1491
724 switch (hv_msg->kvp_hdr.operation) { 1492 /*
725 case KVP_OP_REGISTER: 1493 * We will use the KVP header information to pass back
1494 * the error from this daemon. So, first copy the state
1495 * and set the error code to success.
1496 */
1497 op = hv_msg->kvp_hdr.operation;
1498 pool = hv_msg->kvp_hdr.pool;
1499 hv_msg->error = HV_S_OK;
1500
1501 if ((in_hand_shake) && (op == KVP_OP_REGISTER1)) {
726 /* 1502 /*
727 * Driver is registering with us; stash away the version 1503 * Driver is registering with us; stash away the version
728 * information. 1504 * information.
729 */ 1505 */
1506 in_hand_shake = 0;
730 p = (char *)hv_msg->body.kvp_register.version; 1507 p = (char *)hv_msg->body.kvp_register.version;
731 lic_version = malloc(strlen(p) + 1); 1508 lic_version = malloc(strlen(p) + 1);
732 if (lic_version) { 1509 if (lic_version) {
@@ -737,44 +1514,82 @@ int main(void)
737 syslog(LOG_ERR, "malloc failed"); 1514 syslog(LOG_ERR, "malloc failed");
738 } 1515 }
739 continue; 1516 continue;
1517 }
740 1518
741 /* 1519 switch (op) {
742 * The current protocol with the kernel component uses a 1520 case KVP_OP_GET_IP_INFO:
743 * NULL key name to pass an error condition. 1521 kvp_ip_val = &hv_msg->body.kvp_ip_val;
744 * For the SET, GET and DELETE operations, 1522 if_name =
745 * use the existing protocol to pass back error. 1523 kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id);
746 */ 1524
1525 if (if_name == NULL) {
1526 /*
1527 * We could not map the mac address to an
1528 * interface name; return error.
1529 */
1530 hv_msg->error = HV_E_FAIL;
1531 break;
1532 }
1533 error = kvp_get_ip_info(
1534 0, if_name, KVP_OP_GET_IP_INFO,
1535 kvp_ip_val,
1536 (MAX_IP_ADDR_SIZE * 2));
1537
1538 if (error)
1539 hv_msg->error = error;
1540
1541 free(if_name);
1542 break;
1543
1544 case KVP_OP_SET_IP_INFO:
1545 kvp_ip_val = &hv_msg->body.kvp_ip_val;
1546 if_name = kvp_get_if_name(
1547 (char *)kvp_ip_val->adapter_id);
1548 if (if_name == NULL) {
1549 /*
1550 * We could not map the guid to an
1551 * interface name; return error.
1552 */
1553 hv_msg->error = HV_GUID_NOTFOUND;
1554 break;
1555 }
1556 error = kvp_set_ip_info(if_name, kvp_ip_val);
1557 if (error)
1558 hv_msg->error = error;
1559
1560 free(if_name);
1561 break;
747 1562
748 case KVP_OP_SET: 1563 case KVP_OP_SET:
749 if (kvp_key_add_or_modify(hv_msg->kvp_hdr.pool, 1564 if (kvp_key_add_or_modify(pool,
750 hv_msg->body.kvp_set.data.key, 1565 hv_msg->body.kvp_set.data.key,
751 hv_msg->body.kvp_set.data.key_size, 1566 hv_msg->body.kvp_set.data.key_size,
752 hv_msg->body.kvp_set.data.value, 1567 hv_msg->body.kvp_set.data.value,
753 hv_msg->body.kvp_set.data.value_size)) 1568 hv_msg->body.kvp_set.data.value_size))
754 strcpy(hv_msg->body.kvp_set.data.key, ""); 1569 hv_msg->error = HV_S_CONT;
755 break; 1570 break;
756 1571
757 case KVP_OP_GET: 1572 case KVP_OP_GET:
758 if (kvp_get_value(hv_msg->kvp_hdr.pool, 1573 if (kvp_get_value(pool,
759 hv_msg->body.kvp_set.data.key, 1574 hv_msg->body.kvp_set.data.key,
760 hv_msg->body.kvp_set.data.key_size, 1575 hv_msg->body.kvp_set.data.key_size,
761 hv_msg->body.kvp_set.data.value, 1576 hv_msg->body.kvp_set.data.value,
762 hv_msg->body.kvp_set.data.value_size)) 1577 hv_msg->body.kvp_set.data.value_size))
763 strcpy(hv_msg->body.kvp_set.data.key, ""); 1578 hv_msg->error = HV_S_CONT;
764 break; 1579 break;
765 1580
766 case KVP_OP_DELETE: 1581 case KVP_OP_DELETE:
767 if (kvp_key_delete(hv_msg->kvp_hdr.pool, 1582 if (kvp_key_delete(pool,
768 hv_msg->body.kvp_delete.key, 1583 hv_msg->body.kvp_delete.key,
769 hv_msg->body.kvp_delete.key_size)) 1584 hv_msg->body.kvp_delete.key_size))
770 strcpy(hv_msg->body.kvp_delete.key, ""); 1585 hv_msg->error = HV_S_CONT;
771 break; 1586 break;
772 1587
773 default: 1588 default:
774 break; 1589 break;
775 } 1590 }
776 1591
777 if (hv_msg->kvp_hdr.operation != KVP_OP_ENUMERATE) 1592 if (op != KVP_OP_ENUMERATE)
778 goto kvp_done; 1593 goto kvp_done;
779 1594
780 /* 1595 /*
@@ -782,13 +1597,14 @@ int main(void)
782 * both the key and the value; if not read from the 1597 * both the key and the value; if not read from the
783 * appropriate pool. 1598 * appropriate pool.
784 */ 1599 */
785 if (hv_msg->kvp_hdr.pool != KVP_POOL_AUTO) { 1600 if (pool != KVP_POOL_AUTO) {
786 kvp_pool_enumerate(hv_msg->kvp_hdr.pool, 1601 if (kvp_pool_enumerate(pool,
787 hv_msg->body.kvp_enum_data.index, 1602 hv_msg->body.kvp_enum_data.index,
788 hv_msg->body.kvp_enum_data.data.key, 1603 hv_msg->body.kvp_enum_data.data.key,
789 HV_KVP_EXCHANGE_MAX_KEY_SIZE, 1604 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
790 hv_msg->body.kvp_enum_data.data.value, 1605 hv_msg->body.kvp_enum_data.data.value,
791 HV_KVP_EXCHANGE_MAX_VALUE_SIZE); 1606 HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
1607 hv_msg->error = HV_S_CONT;
792 goto kvp_done; 1608 goto kvp_done;
793 } 1609 }
794 1610
@@ -807,13 +1623,13 @@ int main(void)
807 strcpy(key_value, lic_version); 1623 strcpy(key_value, lic_version);
808 break; 1624 break;
809 case NetworkAddressIPv4: 1625 case NetworkAddressIPv4:
810 kvp_get_ip_address(AF_INET, key_value, 1626 kvp_get_ip_info(AF_INET, NULL, KVP_OP_ENUMERATE,
811 HV_KVP_EXCHANGE_MAX_VALUE_SIZE); 1627 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
812 strcpy(key_name, "NetworkAddressIPv4"); 1628 strcpy(key_name, "NetworkAddressIPv4");
813 break; 1629 break;
814 case NetworkAddressIPv6: 1630 case NetworkAddressIPv6:
815 kvp_get_ip_address(AF_INET6, key_value, 1631 kvp_get_ip_info(AF_INET6, NULL, KVP_OP_ENUMERATE,
816 HV_KVP_EXCHANGE_MAX_VALUE_SIZE); 1632 key_value, HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
817 strcpy(key_name, "NetworkAddressIPv6"); 1633 strcpy(key_name, "NetworkAddressIPv6");
818 break; 1634 break;
819 case OSBuildNumber: 1635 case OSBuildNumber:
@@ -841,11 +1657,7 @@ int main(void)
841 strcpy(key_name, "ProcessorArchitecture"); 1657 strcpy(key_name, "ProcessorArchitecture");
842 break; 1658 break;
843 default: 1659 default:
844 strcpy(key_value, "Unknown Key"); 1660 hv_msg->error = HV_S_CONT;
845 /*
846 * We use a null key name to terminate enumeration.
847 */
848 strcpy(key_name, "");
849 break; 1661 break;
850 } 1662 }
851 /* 1663 /*
@@ -863,7 +1675,7 @@ kvp_done:
863 len = netlink_send(fd, incoming_cn_msg); 1675 len = netlink_send(fd, incoming_cn_msg);
864 if (len < 0) { 1676 if (len < 0) {
865 syslog(LOG_ERR, "net_link send failed; error:%d", len); 1677 syslog(LOG_ERR, "net_link send failed; error:%d", len);
866 exit(-1); 1678 exit(EXIT_FAILURE);
867 } 1679 }
868 } 1680 }
869 1681
diff --git a/tools/hv/hv_set_ifconfig.sh b/tools/hv/hv_set_ifconfig.sh
new file mode 100755
index 00000000000..3e9427e08d8
--- /dev/null
+++ b/tools/hv/hv_set_ifconfig.sh
@@ -0,0 +1,68 @@
1#!/bin/bash
2
3# This example script activates an interface based on the specified
4# configuration.
5#
6# In the interest of keeping the KVP daemon code free of distro specific
7# information; the kvp daemon code invokes this external script to configure
8# the interface.
9#
10# The only argument to this script is the configuration file that is to
11# be used to configure the interface.
12#
13# Each Distro is expected to implement this script in a distro specific
14# fashion. For instance on Distros that ship with Network Manager enabled,
15# this script can be based on the Network Manager APIs for configuring the
16# interface.
17#
18# This example script is based on a RHEL environment.
19#
20# Here is the format of the ip configuration file:
21#
22# HWADDR=macaddr
23# IF_NAME=interface name
24# DHCP=yes (This is optional; if yes, DHCP is configured)
25#
26# IPADDR=ipaddr1
27# IPADDR_1=ipaddr2
28# IPADDR_x=ipaddry (where y = x + 1)
29#
30# NETMASK=netmask1
31# NETMASK_x=netmasky (where y = x + 1)
32#
33# GATEWAY=ipaddr1
34# GATEWAY_x=ipaddry (where y = x + 1)
35#
36# DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
37#
38# IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
39# tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
40# IPV6NETMASK.
41#
42# The host can specify multiple ipv4 and ipv6 addresses to be
43# configured for the interface. Furthermore, the configuration
44# needs to be persistent. A subsequent GET call on the interface
45# is expected to return the configuration that is set via the SET
46# call.
47#
48
49
50
51echo "IPV6INIT=yes" >> $1
52echo "NM_CONTROLLED=no" >> $1
53echo "PEERDNS=yes" >> $1
54echo "ONBOOT=yes" >> $1
55
56dhcp=$(grep "DHCP" $1 2>/dev/null)
57if [ "$dhcp" != "" ];
58then
59echo "BOOTPROTO=dhcp" >> $1;
60fi
61
62cp $1 /etc/sysconfig/network-scripts/
63
64
65interface=$(echo $1 | awk -F - '{ print $2 }')
66
67/sbin/ifdown $interface 2>/dev/null
68/sbin/ifup $interfac 2>/dev/null